xref: /freebsd/sbin/ifconfig/ifgif.c (revision 93c2d7d5265c56c8734e70c19f838bd56190954a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <sys/sockio.h>
32 
33 #include <stdlib.h>
34 #include <unistd.h>
35 
36 #include <net/ethernet.h>
37 #include <net/if.h>
38 #include <net/if_gif.h>
39 #include <net/route.h>
40 
41 #include <ctype.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <err.h>
47 #include <errno.h>
48 
49 #include "ifconfig.h"
50 
51 static const char *GIFBITS[] = {
52 	[0] = "NOCLAMP",
53 	[1] = "IGNORE_SOURCE",
54 };
55 
56 static void
gif_status(if_ctx * ctx)57 gif_status(if_ctx *ctx)
58 {
59 	int opts;
60 	struct ifreq ifr = { .ifr_data = (caddr_t)&opts };
61 
62 	if (ioctl_ctx_ifr(ctx, GIFGOPTS, &ifr) == -1)
63 		return;
64 	if (opts == 0)
65 		return;
66 	printf("\toptions=%x", opts);
67 	print_bits("options", &opts, 1, GIFBITS, nitems(GIFBITS));
68 	putchar('\n');
69 }
70 
71 static void
setgifopts(if_ctx * ctx,const char * val __unused,int d)72 setgifopts(if_ctx *ctx, const char *val __unused, int d)
73 {
74 	int opts;
75 	struct ifreq ifr = { .ifr_data = (caddr_t)&opts };
76 
77 	if (ioctl_ctx_ifr(ctx, GIFGOPTS, &ifr) == -1) {
78 		warn("ioctl(GIFGOPTS)");
79 		return;
80 	}
81 
82 	if (d < 0)
83 		opts &= ~(-d);
84 	else
85 		opts |= d;
86 
87 	if (ioctl_ctx(ctx, GIFSOPTS, &ifr) == -1) {
88 		warn("ioctl(GIFSOPTS)");
89 		return;
90 	}
91 }
92 
93 static struct cmd gif_cmds[] = {
94 	DEF_CMD("noclamp",		GIF_NOCLAMP,		setgifopts),
95 	DEF_CMD("-noclamp",		-GIF_NOCLAMP,		setgifopts),
96 	DEF_CMD("ignore_source",	GIF_IGNORE_SOURCE,	setgifopts),
97 	DEF_CMD("-ignore_source",	-GIF_IGNORE_SOURCE,	setgifopts),
98 };
99 
100 static struct afswtch af_gif = {
101 	.af_name	= "af_gif",
102 	.af_af		= AF_UNSPEC,
103 	.af_other_status = gif_status,
104 };
105 
106 static __constructor void
gif_ctor(void)107 gif_ctor(void)
108 {
109 	size_t i;
110 
111 	for (i = 0; i < nitems(gif_cmds); i++)
112 		cmd_register(&gif_cmds[i]);
113 	af_register(&af_gif);
114 }
115