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