1181224Sthompsa/*-
2181224Sthompsa * Copyright (c) 2008 Andrew Thompson. All rights reserved.
3181224Sthompsa *
4181224Sthompsa * Redistribution and use in source and binary forms, with or without
5181224Sthompsa * modification, are permitted provided that the following conditions
6181224Sthompsa * are met:
7181224Sthompsa * 1. Redistributions of source code must retain the above copyright
8181224Sthompsa *    notice, this list of conditions and the following disclaimer.
9181224Sthompsa * 2. Redistributions in binary form must reproduce the above copyright
10181224Sthompsa *    notice, this list of conditions and the following disclaimer in the
11181224Sthompsa *    documentation and/or other materials provided with the distribution.
12181224Sthompsa *
13181224Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14181224Sthompsa * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15181224Sthompsa * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16181224Sthompsa * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
17181224Sthompsa * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18181224Sthompsa * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19181224Sthompsa * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20181224Sthompsa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21181224Sthompsa * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
22181224Sthompsa * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23181224Sthompsa * THE POSSIBILITY OF SUCH DAMAGE.
24181224Sthompsa */
25181224Sthompsa
26284066Sae#include <sys/cdefs.h>
27284066Sae__FBSDID("$FreeBSD: releng/10.2/sbin/ifconfig/ifgre.c 284066 2015-06-06 12:44:42Z ae $");
28181224Sthompsa
29181224Sthompsa#include <sys/param.h>
30181224Sthompsa#include <sys/ioctl.h>
31181224Sthompsa#include <sys/socket.h>
32181224Sthompsa#include <sys/sockio.h>
33181224Sthompsa#include <net/if.h>
34181224Sthompsa#include <net/if_gre.h>
35181224Sthompsa
36181224Sthompsa#include <ctype.h>
37284066Sae#include <limits.h>
38181224Sthompsa#include <stdio.h>
39284066Sae#include <stdlib.h>
40181224Sthompsa#include <string.h>
41181224Sthompsa#include <err.h>
42181224Sthompsa
43181224Sthompsa#include "ifconfig.h"
44181224Sthompsa
45284066Sae#define	GREBITS	"\020\01ENABLE_CSUM\02ENABLE_SEQ"
46284066Sae
47181224Sthompsastatic	void gre_status(int s);
48181224Sthompsa
49181224Sthompsastatic void
50181224Sthompsagre_status(int s)
51181224Sthompsa{
52284066Sae	uint32_t opts = 0;
53181224Sthompsa
54284066Sae	ifr.ifr_data = (caddr_t)&opts;
55181224Sthompsa	if (ioctl(s, GREGKEY, &ifr) == 0)
56284066Sae		if (opts != 0)
57284066Sae			printf("\tgrekey: 0x%x (%u)\n", opts, opts);
58284066Sae	opts = 0;
59284066Sae	if (ioctl(s, GREGOPTS, &ifr) != 0 || opts == 0)
60284066Sae		return;
61284066Sae	printb("\toptions", opts, GREBITS);
62284066Sae	putchar('\n');
63181224Sthompsa}
64181224Sthompsa
65181224Sthompsastatic void
66181224Sthompsasetifgrekey(const char *val, int dummy __unused, int s,
67181224Sthompsa    const struct afswtch *afp)
68181224Sthompsa{
69284066Sae	uint32_t grekey = strtol(val, NULL, 0);
70181224Sthompsa
71181224Sthompsa	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
72181224Sthompsa	ifr.ifr_data = (caddr_t)&grekey;
73181224Sthompsa	if (ioctl(s, GRESKEY, (caddr_t)&ifr) < 0)
74181224Sthompsa		warn("ioctl (set grekey)");
75181224Sthompsa}
76181224Sthompsa
77284066Saestatic void
78284066Saesetifgreopts(const char *val, int d, int s, const struct afswtch *afp)
79284066Sae{
80284066Sae	uint32_t opts;
81284066Sae
82284066Sae	ifr.ifr_data = (caddr_t)&opts;
83284066Sae	if (ioctl(s, GREGOPTS, &ifr) == -1) {
84284066Sae		warn("ioctl(GREGOPTS)");
85284066Sae		return;
86284066Sae	}
87284066Sae
88284066Sae	if (d < 0)
89284066Sae		opts &= ~(-d);
90284066Sae	else
91284066Sae		opts |= d;
92284066Sae
93284066Sae	if (ioctl(s, GRESOPTS, &ifr) == -1) {
94284066Sae		warn("ioctl(GIFSOPTS)");
95284066Sae		return;
96284066Sae	}
97284066Sae}
98284066Sae
99284066Sae
100181224Sthompsastatic struct cmd gre_cmds[] = {
101181224Sthompsa	DEF_CMD_ARG("grekey",			setifgrekey),
102284066Sae	DEF_CMD("enable_csum", GRE_ENABLE_CSUM,	setifgreopts),
103284066Sae	DEF_CMD("-enable_csum",-GRE_ENABLE_CSUM,setifgreopts),
104284066Sae	DEF_CMD("enable_seq", GRE_ENABLE_SEQ,	setifgreopts),
105284066Sae	DEF_CMD("-enable_seq",-GRE_ENABLE_SEQ,	setifgreopts),
106181224Sthompsa};
107181224Sthompsastatic struct afswtch af_gre = {
108181224Sthompsa	.af_name	= "af_gre",
109181224Sthompsa	.af_af		= AF_UNSPEC,
110181224Sthompsa	.af_other_status = gre_status,
111181224Sthompsa};
112181224Sthompsa
113181224Sthompsastatic __constructor void
114181224Sthompsagre_ctor(void)
115181224Sthompsa{
116181224Sthompsa#define	N(a)	(sizeof(a) / sizeof(a[0]))
117194799Sdelphij	size_t i;
118181224Sthompsa
119181224Sthompsa	for (i = 0; i < N(gre_cmds);  i++)
120181224Sthompsa		cmd_register(&gre_cmds[i]);
121181224Sthompsa	af_register(&af_gre);
122181224Sthompsa#undef N
123181224Sthompsa}
124