xref: /f-stack/tools/ifconfig/ifgre.c (revision d4a07e70)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
29 #ifndef FSTACK
30 __FBSDID("$FreeBSD$");
31 #endif
32 
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
37 #include <net/if.h>
38 #include <net/if_gre.h>
39 
40 #include <ctype.h>
41 #include <limits.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <err.h>
46 
47 #include "ifconfig.h"
48 
49 #define	GREBITS	"\020\01ENABLE_CSUM\02ENABLE_SEQ\03UDPENCAP"
50 
51 static	void gre_status(int s);
52 
53 static void
gre_status(int s)54 gre_status(int s)
55 {
56 	uint32_t opts, port;
57 
58 	opts = 0;
59 	ifr.ifr_data = (caddr_t)&opts;
60 #ifndef FSTACK
61 	if (ioctl(s, GREGKEY, &ifr) == 0)
62 #else
63 	size_t offset = (char *)&(ifr.ifr_data) - (char *)&(ifr);
64 	size_t clen = sizeof(uint32_t);
65 	if (ioctl_va(s, GREGKEY, &ifr, 3, offset, ifr.ifr_data, clen) == 0)
66 #endif
67 		if (opts != 0)
68 			printf("\tgrekey: 0x%x (%u)\n", opts, opts);
69 	opts = 0;
70 #ifndef FSTACK
71 	if (ioctl(s, GREGOPTS, &ifr) != 0 || opts == 0)
72 #else
73 	if (ioctl_va(s, GREGOPTS, &ifr, 3, offset, ifr.ifr_data, clen) != 0 || opts == 0)
74 #endif
75 		return;
76 
77 	port = 0;
78 	ifr.ifr_data = (caddr_t)&port;
79 #ifndef FSTACK
80 	if (ioctl(s, GREGPORT, &ifr) == 0 && port != 0)
81 #else
82 	if (ioctl_va(s, GREGOPTS, &ifr, 3, offset, ifr.ifr_data, clen) != 0 || opts == 0)
83 #endif
84 		printf("\tudpport: %u\n", port);
85 	printb("\toptions", opts, GREBITS);
86 	putchar('\n');
87 }
88 
89 static void
setifgrekey(const char * val,int dummy __unused,int s,const struct afswtch * afp)90 setifgrekey(const char *val, int dummy __unused, int s,
91     const struct afswtch *afp)
92 {
93 	uint32_t grekey = strtol(val, NULL, 0);
94 
95 	strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
96 	ifr.ifr_data = (caddr_t)&grekey;
97 #ifndef FSTACK
98 	if (ioctl(s, GRESKEY, (caddr_t)&ifr) < 0)
99 #else
100 	size_t offset = (char *)&(ifr.ifr_data) - (char *)&(ifr);
101 	size_t clen = sizeof(uint32_t);
102 	if (ioctl_va(s, GRESKEY, (caddr_t)&ifr, 3, offset, ifr.ifr_data, clen) < 0)
103 #endif
104 		warn("ioctl (set grekey)");
105 }
106 
107 static void
setifgreport(const char * val,int dummy __unused,int s,const struct afswtch * afp)108 setifgreport(const char *val, int dummy __unused, int s,
109     const struct afswtch *afp)
110 {
111 	uint32_t udpport = strtol(val, NULL, 0);
112 
113 	strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
114 	ifr.ifr_data = (caddr_t)&udpport;
115 #ifndef FSTACK
116 	if (ioctl(s, GRESPORT, (caddr_t)&ifr) < 0)
117 #else
118 	size_t offset = (char *)&(ifr.ifr_data) - (char *)&(ifr);
119 	size_t clen = sizeof(uint32_t);
120 	if (ioctl_va(s, GRESPORT, (caddr_t)&ifr, 3, offset, ifr.ifr_data, clen) < 0)
121 #endif
122 		warn("ioctl (set udpport)");
123 }
124 
125 static void
setifgreopts(const char * val,int d,int s,const struct afswtch * afp)126 setifgreopts(const char *val, int d, int s, const struct afswtch *afp)
127 {
128 	uint32_t opts;
129 
130 	ifr.ifr_data = (caddr_t)&opts;
131 #ifndef FSTACK
132 	if (ioctl(s, GREGOPTS, &ifr) == -1) {
133 #else
134 	size_t offset = (char *)&(ifr.ifr_data) - (char *)&(ifr);
135 	size_t clen = sizeof(uint32_t);
136 	if (ioctl_va(s, GREGOPTS, (caddr_t)&ifr, 3, offset, ifr.ifr_data, clen) == -1) {
137 #endif
138 		warn("ioctl(GREGOPTS)");
139 		return;
140 	}
141 
142 	if (d < 0)
143 		opts &= ~(-d);
144 	else
145 		opts |= d;
146 
147 #ifndef FSTACK
148 	if (ioctl(s, GRESOPTS, &ifr) == -1) {
149 #else
150 	if (ioctl_va(s, GRESOPTS, (caddr_t)&ifr, 3, offset, ifr.ifr_data, clen) == -1) {
151 #endif
152 		warn("ioctl(GIFSOPTS)");
153 		return;
154 	}
155 }
156 
157 
158 static struct cmd gre_cmds[] = {
159 	DEF_CMD_ARG("grekey",			setifgrekey),
160 	DEF_CMD_ARG("udpport",			setifgreport),
161 	DEF_CMD("enable_csum", GRE_ENABLE_CSUM,	setifgreopts),
162 	DEF_CMD("-enable_csum",-GRE_ENABLE_CSUM,setifgreopts),
163 	DEF_CMD("enable_seq", GRE_ENABLE_SEQ,	setifgreopts),
164 	DEF_CMD("-enable_seq",-GRE_ENABLE_SEQ,	setifgreopts),
165 	DEF_CMD("udpencap", GRE_UDPENCAP,	setifgreopts),
166 	DEF_CMD("-udpencap",-GRE_UDPENCAP,	setifgreopts),
167 };
168 static struct afswtch af_gre = {
169 	.af_name	= "af_gre",
170 	.af_af		= AF_UNSPEC,
171 	.af_other_status = gre_status,
172 };
173 
174 static __constructor void
175 gre_ctor(void)
176 {
177 	size_t i;
178 
179 	for (i = 0; i < nitems(gre_cmds);  i++)
180 		cmd_register(&gre_cmds[i]);
181 	af_register(&af_gre);
182 }
183