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