xref: /freebsd/sbin/ifconfig/ifgre.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008 Andrew Thompson. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25  * THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <sys/sockio.h>
32 #include <net/if.h>
33 #include <net/if_gre.h>
34 
35 #include <ctype.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <err.h>
41 
42 #include "ifconfig.h"
43 
44 #define	GREBITS	"\020\01ENABLE_CSUM\02ENABLE_SEQ\03UDPENCAP"
45 
46 static void
47 gre_status(if_ctx *ctx)
48 {
49 	uint32_t opts = 0, port;
50 	struct ifreq ifr = { .ifr_data = (caddr_t)&opts };
51 
52 	if (ioctl_ctx(ctx, GREGKEY, &ifr) == 0)
53 		if (opts != 0)
54 			printf("\tgrekey: 0x%x (%u)\n", opts, opts);
55 	opts = 0;
56 	if (ioctl_ctx(ctx, GREGOPTS, &ifr) != 0 || opts == 0)
57 		return;
58 
59 	port = 0;
60 	ifr.ifr_data = (caddr_t)&port;
61 	if (ioctl_ctx_ifr(ctx, GREGPORT, &ifr) == 0 && port != 0)
62 		printf("\tudpport: %u\n", port);
63 	printb("\toptions", opts, GREBITS);
64 	putchar('\n');
65 }
66 
67 static void
68 setifgrekey(if_ctx *ctx, const char *val, int dummy __unused)
69 {
70 	uint32_t grekey = strtol(val, NULL, 0);
71 	struct ifreq ifr = { .ifr_data = (caddr_t)&grekey };
72 
73 	ifr.ifr_data = (caddr_t)&grekey;
74 	if (ioctl_ctx_ifr(ctx, GRESKEY, &ifr) < 0)
75 		warn("ioctl (set grekey)");
76 }
77 
78 static void
79 setifgreport(if_ctx *ctx, const char *val, int dummy __unused)
80 {
81 	uint32_t udpport = strtol(val, NULL, 0);
82 	struct ifreq ifr = { .ifr_data = (caddr_t)&udpport };
83 
84 	if (ioctl_ctx_ifr(ctx, GRESPORT, &ifr) < 0)
85 		warn("ioctl (set udpport)");
86 }
87 
88 static void
89 setifgreopts(if_ctx *ctx, const char *val __unused, int d)
90 {
91 	uint32_t opts;
92 	struct ifreq ifr = { .ifr_data = (caddr_t)&opts };
93 
94 	if (ioctl_ctx_ifr(ctx, GREGOPTS, &ifr) == -1) {
95 		warn("ioctl(GREGOPTS)");
96 		return;
97 	}
98 
99 	if (d < 0)
100 		opts &= ~(-d);
101 	else
102 		opts |= d;
103 
104 	if (ioctl_ctx(ctx, GRESOPTS, &ifr) == -1) {
105 		warn("ioctl(GIFSOPTS)");
106 		return;
107 	}
108 }
109 
110 
111 static struct cmd gre_cmds[] = {
112 	DEF_CMD_ARG("grekey",			setifgrekey),
113 	DEF_CMD_ARG("udpport",			setifgreport),
114 	DEF_CMD("enable_csum", GRE_ENABLE_CSUM,	setifgreopts),
115 	DEF_CMD("-enable_csum",-GRE_ENABLE_CSUM,setifgreopts),
116 	DEF_CMD("enable_seq", GRE_ENABLE_SEQ,	setifgreopts),
117 	DEF_CMD("-enable_seq",-GRE_ENABLE_SEQ,	setifgreopts),
118 	DEF_CMD("udpencap", GRE_UDPENCAP,	setifgreopts),
119 	DEF_CMD("-udpencap",-GRE_UDPENCAP,	setifgreopts),
120 };
121 static struct afswtch af_gre = {
122 	.af_name	= "af_gre",
123 	.af_af		= AF_UNSPEC,
124 	.af_other_status = gre_status,
125 };
126 
127 static __constructor void
128 gre_ctor(void)
129 {
130 	size_t i;
131 
132 	for (i = 0; i < nitems(gre_cmds);  i++)
133 		cmd_register(&gre_cmds[i]);
134 	af_register(&af_gre);
135 }
136