xref: /freebsd/sbin/ifconfig/ifgre.c (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
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 __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
50 gre_status(if_ctx *ctx)
51 {
52 	uint32_t opts = 0, port;
53 	struct ifreq ifr = { .ifr_data = (caddr_t)&opts };
54 
55 	if (ioctl_ctx(ctx, GREGKEY, &ifr) == 0)
56 		if (opts != 0)
57 			printf("\tgrekey: 0x%x (%u)\n", opts, opts);
58 	opts = 0;
59 	if (ioctl_ctx(ctx, GREGOPTS, &ifr) != 0 || opts == 0)
60 		return;
61 
62 	port = 0;
63 	ifr.ifr_data = (caddr_t)&port;
64 	if (ioctl_ctx_ifr(ctx, GREGPORT, &ifr) == 0 && port != 0)
65 		printf("\tudpport: %u\n", port);
66 	printb("\toptions", opts, GREBITS);
67 	putchar('\n');
68 }
69 
70 static void
71 setifgrekey(if_ctx *ctx, const char *val, int dummy __unused)
72 {
73 	uint32_t grekey = strtol(val, NULL, 0);
74 	struct ifreq ifr = { .ifr_data = (caddr_t)&grekey };
75 
76 	ifr.ifr_data = (caddr_t)&grekey;
77 	if (ioctl_ctx_ifr(ctx, GRESKEY, &ifr) < 0)
78 		warn("ioctl (set grekey)");
79 }
80 
81 static void
82 setifgreport(if_ctx *ctx, const char *val, int dummy __unused)
83 {
84 	uint32_t udpport = strtol(val, NULL, 0);
85 	struct ifreq ifr = { .ifr_data = (caddr_t)&udpport };
86 
87 	if (ioctl_ctx_ifr(ctx, GRESPORT, &ifr) < 0)
88 		warn("ioctl (set udpport)");
89 }
90 
91 static void
92 setifgreopts(if_ctx *ctx, const char *val __unused, int d)
93 {
94 	uint32_t opts;
95 	struct ifreq ifr = { .ifr_data = (caddr_t)&opts };
96 
97 	if (ioctl_ctx_ifr(ctx, GREGOPTS, &ifr) == -1) {
98 		warn("ioctl(GREGOPTS)");
99 		return;
100 	}
101 
102 	if (d < 0)
103 		opts &= ~(-d);
104 	else
105 		opts |= d;
106 
107 	if (ioctl_ctx(ctx, GRESOPTS, &ifr) == -1) {
108 		warn("ioctl(GIFSOPTS)");
109 		return;
110 	}
111 }
112 
113 
114 static struct cmd gre_cmds[] = {
115 	DEF_CMD_ARG("grekey",			setifgrekey),
116 	DEF_CMD_ARG("udpport",			setifgreport),
117 	DEF_CMD("enable_csum", GRE_ENABLE_CSUM,	setifgreopts),
118 	DEF_CMD("-enable_csum",-GRE_ENABLE_CSUM,setifgreopts),
119 	DEF_CMD("enable_seq", GRE_ENABLE_SEQ,	setifgreopts),
120 	DEF_CMD("-enable_seq",-GRE_ENABLE_SEQ,	setifgreopts),
121 	DEF_CMD("udpencap", GRE_UDPENCAP,	setifgreopts),
122 	DEF_CMD("-udpencap",-GRE_UDPENCAP,	setifgreopts),
123 };
124 static struct afswtch af_gre = {
125 	.af_name	= "af_gre",
126 	.af_af		= AF_UNSPEC,
127 	.af_other_status = gre_status,
128 };
129 
130 static __constructor void
131 gre_ctor(void)
132 {
133 	size_t i;
134 
135 	for (i = 0; i < nitems(gre_cmds);  i++)
136 		cmd_register(&gre_cmds[i]);
137 	af_register(&af_gre);
138 }
139