xref: /freebsd/sbin/ifconfig/ifgre.c (revision 6e3a9d7f2c8dc18dc901d4f860a65028c0a82a64)
13b0edf7dSAndrew Thompson /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
43b0edf7dSAndrew Thompson  * Copyright (c) 2008 Andrew Thompson. All rights reserved.
53b0edf7dSAndrew Thompson  *
63b0edf7dSAndrew Thompson  * Redistribution and use in source and binary forms, with or without
73b0edf7dSAndrew Thompson  * modification, are permitted provided that the following conditions
83b0edf7dSAndrew Thompson  * are met:
93b0edf7dSAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
103b0edf7dSAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
113b0edf7dSAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
123b0edf7dSAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
133b0edf7dSAndrew Thompson  *    documentation and/or other materials provided with the distribution.
143b0edf7dSAndrew Thompson  *
153b0edf7dSAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
163b0edf7dSAndrew Thompson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
173b0edf7dSAndrew Thompson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
183b0edf7dSAndrew Thompson  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
193b0edf7dSAndrew Thompson  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
203b0edf7dSAndrew Thompson  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
213b0edf7dSAndrew Thompson  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
223b0edf7dSAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
233b0edf7dSAndrew Thompson  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
243b0edf7dSAndrew Thompson  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
253b0edf7dSAndrew Thompson  * THE POSSIBILITY OF SUCH DAMAGE.
263b0edf7dSAndrew Thompson  */
273b0edf7dSAndrew Thompson 
28f325335cSAndrey V. Elsukov #include <sys/cdefs.h>
29f325335cSAndrey V. Elsukov __FBSDID("$FreeBSD$");
303b0edf7dSAndrew Thompson 
313b0edf7dSAndrew Thompson #include <sys/param.h>
323b0edf7dSAndrew Thompson #include <sys/ioctl.h>
333b0edf7dSAndrew Thompson #include <sys/socket.h>
343b0edf7dSAndrew Thompson #include <sys/sockio.h>
353b0edf7dSAndrew Thompson #include <net/if.h>
363b0edf7dSAndrew Thompson #include <net/if_gre.h>
373b0edf7dSAndrew Thompson 
383b0edf7dSAndrew Thompson #include <ctype.h>
39f325335cSAndrey V. Elsukov #include <limits.h>
403b0edf7dSAndrew Thompson #include <stdio.h>
413b0edf7dSAndrew Thompson #include <stdlib.h>
42f325335cSAndrey V. Elsukov #include <string.h>
433b0edf7dSAndrew Thompson #include <err.h>
443b0edf7dSAndrew Thompson 
453b0edf7dSAndrew Thompson #include "ifconfig.h"
463b0edf7dSAndrew Thompson 
47aee793eeSAndrey V. Elsukov #define	GREBITS	"\020\01ENABLE_CSUM\02ENABLE_SEQ\03UDPENCAP"
48f325335cSAndrey V. Elsukov 
493b0edf7dSAndrew Thompson static void
50*6e3a9d7fSAlexander V. Chernikov gre_status(if_ctx *ctx)
513b0edf7dSAndrew Thompson {
52aee793eeSAndrey V. Elsukov 	uint32_t opts, port;
533b0edf7dSAndrew Thompson 
54aee793eeSAndrey V. Elsukov 	opts = 0;
55f325335cSAndrey V. Elsukov 	ifr.ifr_data = (caddr_t)&opts;
56*6e3a9d7fSAlexander V. Chernikov 	if (ioctl_ctx(ctx, GREGKEY, &ifr) == 0)
57f325335cSAndrey V. Elsukov 		if (opts != 0)
58f325335cSAndrey V. Elsukov 			printf("\tgrekey: 0x%x (%u)\n", opts, opts);
59f325335cSAndrey V. Elsukov 	opts = 0;
60*6e3a9d7fSAlexander V. Chernikov 	if (ioctl_ctx(ctx, GREGOPTS, &ifr) != 0 || opts == 0)
61f325335cSAndrey V. Elsukov 		return;
62aee793eeSAndrey V. Elsukov 
63aee793eeSAndrey V. Elsukov 	port = 0;
64aee793eeSAndrey V. Elsukov 	ifr.ifr_data = (caddr_t)&port;
65*6e3a9d7fSAlexander V. Chernikov 	if (ioctl_ctx(ctx, GREGPORT, &ifr) == 0 && port != 0)
66aee793eeSAndrey V. Elsukov 		printf("\tudpport: %u\n", port);
67f325335cSAndrey V. Elsukov 	printb("\toptions", opts, GREBITS);
68f325335cSAndrey V. Elsukov 	putchar('\n');
693b0edf7dSAndrew Thompson }
703b0edf7dSAndrew Thompson 
713b0edf7dSAndrew Thompson static void
72*6e3a9d7fSAlexander V. Chernikov setifgrekey(if_ctx *ctx, const char *val, int dummy __unused)
733b0edf7dSAndrew Thompson {
74f325335cSAndrey V. Elsukov 	uint32_t grekey = strtol(val, NULL, 0);
753b0edf7dSAndrew Thompson 
76c8711749SDon Lewis 	strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
773b0edf7dSAndrew Thompson 	ifr.ifr_data = (caddr_t)&grekey;
78*6e3a9d7fSAlexander V. Chernikov 	if (ioctl(ctx->io_s, GRESKEY, (caddr_t)&ifr) < 0)
793b0edf7dSAndrew Thompson 		warn("ioctl (set grekey)");
803b0edf7dSAndrew Thompson }
813b0edf7dSAndrew Thompson 
82f325335cSAndrey V. Elsukov static void
83*6e3a9d7fSAlexander V. Chernikov setifgreport(if_ctx *ctx, const char *val, int dummy __unused)
84aee793eeSAndrey V. Elsukov {
85aee793eeSAndrey V. Elsukov 	uint32_t udpport = strtol(val, NULL, 0);
86aee793eeSAndrey V. Elsukov 
87aee793eeSAndrey V. Elsukov 	strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
88aee793eeSAndrey V. Elsukov 	ifr.ifr_data = (caddr_t)&udpport;
89*6e3a9d7fSAlexander V. Chernikov 	if (ioctl(ctx->io_s, GRESPORT, (caddr_t)&ifr) < 0)
90aee793eeSAndrey V. Elsukov 		warn("ioctl (set udpport)");
91aee793eeSAndrey V. Elsukov }
92aee793eeSAndrey V. Elsukov 
93aee793eeSAndrey V. Elsukov static void
94*6e3a9d7fSAlexander V. Chernikov setifgreopts(if_ctx *ctx, const char *val, int d)
95f325335cSAndrey V. Elsukov {
96f325335cSAndrey V. Elsukov 	uint32_t opts;
97f325335cSAndrey V. Elsukov 
98f325335cSAndrey V. Elsukov 	ifr.ifr_data = (caddr_t)&opts;
99*6e3a9d7fSAlexander V. Chernikov 	if (ioctl(ctx->io_s, GREGOPTS, &ifr) == -1) {
100f325335cSAndrey V. Elsukov 		warn("ioctl(GREGOPTS)");
101f325335cSAndrey V. Elsukov 		return;
102f325335cSAndrey V. Elsukov 	}
103f325335cSAndrey V. Elsukov 
104f325335cSAndrey V. Elsukov 	if (d < 0)
105f325335cSAndrey V. Elsukov 		opts &= ~(-d);
106f325335cSAndrey V. Elsukov 	else
107f325335cSAndrey V. Elsukov 		opts |= d;
108f325335cSAndrey V. Elsukov 
109*6e3a9d7fSAlexander V. Chernikov 	if (ioctl(ctx->io_s, GRESOPTS, &ifr) == -1) {
110f325335cSAndrey V. Elsukov 		warn("ioctl(GIFSOPTS)");
111f325335cSAndrey V. Elsukov 		return;
112f325335cSAndrey V. Elsukov 	}
113f325335cSAndrey V. Elsukov }
114f325335cSAndrey V. Elsukov 
115f325335cSAndrey V. Elsukov 
1163b0edf7dSAndrew Thompson static struct cmd gre_cmds[] = {
1173b0edf7dSAndrew Thompson 	DEF_CMD_ARG("grekey",			setifgrekey),
118aee793eeSAndrey V. Elsukov 	DEF_CMD_ARG("udpport",			setifgreport),
119f325335cSAndrey V. Elsukov 	DEF_CMD("enable_csum", GRE_ENABLE_CSUM,	setifgreopts),
120f325335cSAndrey V. Elsukov 	DEF_CMD("-enable_csum",-GRE_ENABLE_CSUM,setifgreopts),
121f325335cSAndrey V. Elsukov 	DEF_CMD("enable_seq", GRE_ENABLE_SEQ,	setifgreopts),
122f325335cSAndrey V. Elsukov 	DEF_CMD("-enable_seq",-GRE_ENABLE_SEQ,	setifgreopts),
123aee793eeSAndrey V. Elsukov 	DEF_CMD("udpencap", GRE_UDPENCAP,	setifgreopts),
124aee793eeSAndrey V. Elsukov 	DEF_CMD("-udpencap",-GRE_UDPENCAP,	setifgreopts),
1253b0edf7dSAndrew Thompson };
1263b0edf7dSAndrew Thompson static struct afswtch af_gre = {
1273b0edf7dSAndrew Thompson 	.af_name	= "af_gre",
1283b0edf7dSAndrew Thompson 	.af_af		= AF_UNSPEC,
1293b0edf7dSAndrew Thompson 	.af_other_status = gre_status,
1303b0edf7dSAndrew Thompson };
1313b0edf7dSAndrew Thompson 
1323b0edf7dSAndrew Thompson static __constructor void
1333b0edf7dSAndrew Thompson gre_ctor(void)
1343b0edf7dSAndrew Thompson {
135b59dcaeeSXin LI 	size_t i;
1363b0edf7dSAndrew Thompson 
137abd71050SEnji Cooper 	for (i = 0; i < nitems(gre_cmds);  i++)
1383b0edf7dSAndrew Thompson 		cmd_register(&gre_cmds[i]);
1393b0edf7dSAndrew Thompson 	af_register(&af_gre);
1403b0edf7dSAndrew Thompson }
141