xref: /f-stack/tools/ifconfig/ifgif.c (revision d4a07e70)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Hiroki Sato.  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 #ifndef lint
29 static const char rcsid[] =
30   "$FreeBSD$";
31 #endif
32 
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
37 
38 #include <stdlib.h>
39 #include <unistd.h>
40 
41 #include <net/ethernet.h>
42 #include <net/if.h>
43 #include <net/if_gif.h>
44 #include <net/route.h>
45 
46 #include <ctype.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <err.h>
52 #include <errno.h>
53 
54 #include "ifconfig.h"
55 
56 #define	GIFBITS	"\020\2IGNORE_SOURCE"
57 
58 static void	gif_status(int);
59 
60 static void
gif_status(int s)61 gif_status(int s)
62 {
63 	int opts;
64 
65 	ifr.ifr_data = (caddr_t)&opts;
66 #ifndef FSTACK
67 	if (ioctl(s, GIFGOPTS, &ifr) == -1)
68 #else
69 	size_t offset = (char *)&(ifr.ifr_data) - (char *)&(ifr);
70 	size_t clen = sizeof(int);
71 	if (ioctl_va(s, GIFGOPTS, &ifr, 3, offset, ifr.ifr_data, clen) == -1)
72 #endif
73 		return;
74 	if (opts == 0)
75 		return;
76 	printb("\toptions", opts, GIFBITS);
77 	putchar('\n');
78 }
79 
80 static void
setgifopts(const char * val,int d,int s,const struct afswtch * afp)81 setgifopts(const char *val, int d, int s, const struct afswtch *afp)
82 {
83 	int opts;
84 
85 	ifr.ifr_data = (caddr_t)&opts;
86 #ifndef FSTACK
87 	if (ioctl(s, GIFGOPTS, &ifr) == -1) {
88 #else
89 	size_t offset = (char *)&(ifr.ifr_data) - (char *)&(ifr);
90 	size_t clen = sizeof(int);
91 	if (ioctl_va(s, GIFGOPTS, &ifr, 3, offset, ifr.ifr_data, clen) == -1) {
92 #endif
93 		warn("ioctl(GIFGOPTS)");
94 		return;
95 	}
96 
97 	if (d < 0)
98 		opts &= ~(-d);
99 	else
100 		opts |= d;
101 
102 #ifndef FSTACK
103 	if (ioctl(s, GIFSOPTS, &ifr) == -1) {
104 #else
105 	if (ioctl_va(s, GIFSOPTS, &ifr, 3, offset, ifr.ifr_data, clen) == -1) {
106 #endif
107 		warn("ioctl(GIFSOPTS)");
108 		return;
109 	}
110 }
111 
112 static struct cmd gif_cmds[] = {
113 	DEF_CMD("ignore_source",	GIF_IGNORE_SOURCE,	setgifopts),
114 	DEF_CMD("-ignore_source",	-GIF_IGNORE_SOURCE,	setgifopts),
115 };
116 
117 static struct afswtch af_gif = {
118 	.af_name	= "af_gif",
119 	.af_af		= AF_UNSPEC,
120 	.af_other_status = gif_status,
121 };
122 
123 static __constructor void
124 gif_ctor(void)
125 {
126 	size_t i;
127 
128 	for (i = 0; i < nitems(gif_cmds); i++)
129 		cmd_register(&gif_cmds[i]);
130 	af_register(&af_gif);
131 }
132