xref: /freebsd/sbin/ifconfig/ifgif.c (revision a68e4f7a065218f0bcc5b34ff8d2e73a240b59b2)
1dbe59260SHiroki Sato /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4dbe59260SHiroki Sato  * Copyright (c) 2009 Hiroki Sato.  All rights reserved.
5dbe59260SHiroki Sato  *
6dbe59260SHiroki Sato  * Redistribution and use in source and binary forms, with or without
7dbe59260SHiroki Sato  * modification, are permitted provided that the following conditions
8dbe59260SHiroki Sato  * are met:
9dbe59260SHiroki Sato  * 1. Redistributions of source code must retain the above copyright
10dbe59260SHiroki Sato  *    notice, this list of conditions and the following disclaimer.
11dbe59260SHiroki Sato  * 2. Redistributions in binary form must reproduce the above copyright
12dbe59260SHiroki Sato  *    notice, this list of conditions and the following disclaimer in the
13dbe59260SHiroki Sato  *    documentation and/or other materials provided with the distribution.
14dbe59260SHiroki Sato  *
15dbe59260SHiroki Sato  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16dbe59260SHiroki Sato  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17dbe59260SHiroki Sato  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18dbe59260SHiroki Sato  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
19dbe59260SHiroki Sato  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20dbe59260SHiroki Sato  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21dbe59260SHiroki Sato  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22dbe59260SHiroki Sato  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23dbe59260SHiroki Sato  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24dbe59260SHiroki Sato  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25dbe59260SHiroki Sato  * THE POSSIBILITY OF SUCH DAMAGE.
26dbe59260SHiroki Sato  */
27dbe59260SHiroki Sato 
28dbe59260SHiroki Sato #include <sys/param.h>
29dbe59260SHiroki Sato #include <sys/ioctl.h>
30dbe59260SHiroki Sato #include <sys/socket.h>
31dbe59260SHiroki Sato #include <sys/sockio.h>
32dbe59260SHiroki Sato 
33dbe59260SHiroki Sato #include <stdlib.h>
34dbe59260SHiroki Sato #include <unistd.h>
35dbe59260SHiroki Sato 
36dbe59260SHiroki Sato #include <net/ethernet.h>
37dbe59260SHiroki Sato #include <net/if.h>
38dbe59260SHiroki Sato #include <net/if_gif.h>
39dbe59260SHiroki Sato #include <net/route.h>
40dbe59260SHiroki Sato 
41dbe59260SHiroki Sato #include <ctype.h>
42dbe59260SHiroki Sato #include <stdio.h>
43dbe59260SHiroki Sato #include <string.h>
44dbe59260SHiroki Sato #include <stdlib.h>
45dbe59260SHiroki Sato #include <unistd.h>
46dbe59260SHiroki Sato #include <err.h>
47dbe59260SHiroki Sato #include <errno.h>
48dbe59260SHiroki Sato 
49dbe59260SHiroki Sato #include "ifconfig.h"
50dbe59260SHiroki Sato 
51*a68e4f7aSGordon Tetlow static const char *GIFBITS[] = {
52*a68e4f7aSGordon Tetlow 	[1] = "IGNORE_SOURCE",
53*a68e4f7aSGordon Tetlow };
54dbe59260SHiroki Sato 
55dbe59260SHiroki Sato static void
566e3a9d7fSAlexander V. Chernikov gif_status(if_ctx *ctx)
57dbe59260SHiroki Sato {
58dbe59260SHiroki Sato 	int opts;
597fa282e6SAlexander V. Chernikov 	struct ifreq ifr = { .ifr_data = (caddr_t)&opts };
60dbe59260SHiroki Sato 
617fa282e6SAlexander V. Chernikov 	if (ioctl_ctx_ifr(ctx, GIFGOPTS, &ifr) == -1)
62dbe59260SHiroki Sato 		return;
63ad334ef0SHajimu UMEMOTO 	if (opts == 0)
64ad334ef0SHajimu UMEMOTO 		return;
65*a68e4f7aSGordon Tetlow 	printf("\toptions=%x", opts);
66*a68e4f7aSGordon Tetlow 	print_bits("options", &opts, 1, GIFBITS, nitems(GIFBITS));
6753ac3451SHiroki Sato 	putchar('\n');
68dbe59260SHiroki Sato }
69dbe59260SHiroki Sato 
70dbe59260SHiroki Sato static void
710c2beef7SAlexander V. Chernikov setgifopts(if_ctx *ctx, const char *val __unused, int d)
72dbe59260SHiroki Sato {
73dbe59260SHiroki Sato 	int opts;
747fa282e6SAlexander V. Chernikov 	struct ifreq ifr = { .ifr_data = (caddr_t)&opts };
75dbe59260SHiroki Sato 
767fa282e6SAlexander V. Chernikov 	if (ioctl_ctx_ifr(ctx, GIFGOPTS, &ifr) == -1) {
77dbe59260SHiroki Sato 		warn("ioctl(GIFGOPTS)");
78dbe59260SHiroki Sato 		return;
79dbe59260SHiroki Sato 	}
80dbe59260SHiroki Sato 
81dbe59260SHiroki Sato 	if (d < 0)
82dbe59260SHiroki Sato 		opts &= ~(-d);
83dbe59260SHiroki Sato 	else
84dbe59260SHiroki Sato 		opts |= d;
85dbe59260SHiroki Sato 
866e3a9d7fSAlexander V. Chernikov 	if (ioctl_ctx(ctx, GIFSOPTS, &ifr) == -1) {
87dbe59260SHiroki Sato 		warn("ioctl(GIFSOPTS)");
88dbe59260SHiroki Sato 		return;
89dbe59260SHiroki Sato 	}
90dbe59260SHiroki Sato }
91dbe59260SHiroki Sato 
92dbe59260SHiroki Sato static struct cmd gif_cmds[] = {
93c1b4f79dSAndrey V. Elsukov 	DEF_CMD("ignore_source",	GIF_IGNORE_SOURCE,	setgifopts),
94c1b4f79dSAndrey V. Elsukov 	DEF_CMD("-ignore_source",	-GIF_IGNORE_SOURCE,	setgifopts),
95dbe59260SHiroki Sato };
96dbe59260SHiroki Sato 
97dbe59260SHiroki Sato static struct afswtch af_gif = {
98dbe59260SHiroki Sato 	.af_name	= "af_gif",
99dbe59260SHiroki Sato 	.af_af		= AF_UNSPEC,
100dbe59260SHiroki Sato 	.af_other_status = gif_status,
101dbe59260SHiroki Sato };
102dbe59260SHiroki Sato 
103dbe59260SHiroki Sato static __constructor void
104dbe59260SHiroki Sato gif_ctor(void)
105dbe59260SHiroki Sato {
106b59dcaeeSXin LI 	size_t i;
107dbe59260SHiroki Sato 
108b1c250ffSHiroki Sato 	for (i = 0; i < nitems(gif_cmds); i++)
109dbe59260SHiroki Sato 		cmd_register(&gif_cmds[i]);
110dbe59260SHiroki Sato 	af_register(&af_gif);
111dbe59260SHiroki Sato }
112