ifgre.c revision 194799
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
26181224Sthompsa#ifndef lint
27181224Sthompsastatic const char rcsid[] =
28181224Sthompsa  "$FreeBSD: head/sbin/ifconfig/ifgre.c 194799 2009-06-23 23:49:52Z delphij $";
29181224Sthompsa#endif
30181224Sthompsa
31181224Sthompsa#include <sys/param.h>
32181224Sthompsa#include <sys/ioctl.h>
33181224Sthompsa#include <sys/socket.h>
34181224Sthompsa#include <sys/sockio.h>
35181224Sthompsa
36181224Sthompsa#include <stdlib.h>
37181224Sthompsa#include <unistd.h>
38181224Sthompsa
39181224Sthompsa#include <net/ethernet.h>
40181224Sthompsa#include <net/if.h>
41181224Sthompsa#include <net/if_gre.h>
42181224Sthompsa#include <net/route.h>
43181224Sthompsa
44181224Sthompsa#include <ctype.h>
45181224Sthompsa#include <stdio.h>
46181224Sthompsa#include <string.h>
47181224Sthompsa#include <stdlib.h>
48181224Sthompsa#include <unistd.h>
49181224Sthompsa#include <err.h>
50181224Sthompsa#include <errno.h>
51181224Sthompsa
52181224Sthompsa#include "ifconfig.h"
53181224Sthompsa
54181224Sthompsastatic	void gre_status(int s);
55181224Sthompsa
56181224Sthompsastatic void
57181224Sthompsagre_status(int s)
58181224Sthompsa{
59181224Sthompsa	int grekey = 0;
60181224Sthompsa
61181224Sthompsa	ifr.ifr_data = (caddr_t)&grekey;
62181224Sthompsa	if (ioctl(s, GREGKEY, &ifr) == 0)
63181224Sthompsa		if (grekey != 0)
64181224Sthompsa			printf("\tgrekey: %d\n", grekey);
65181224Sthompsa}
66181224Sthompsa
67181224Sthompsastatic void
68181224Sthompsasetifgrekey(const char *val, int dummy __unused, int s,
69181224Sthompsa    const struct afswtch *afp)
70181224Sthompsa{
71181224Sthompsa	uint32_t grekey = atol(val);
72181224Sthompsa
73181224Sthompsa	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
74181224Sthompsa	ifr.ifr_data = (caddr_t)&grekey;
75181224Sthompsa	if (ioctl(s, GRESKEY, (caddr_t)&ifr) < 0)
76181224Sthompsa		warn("ioctl (set grekey)");
77181224Sthompsa}
78181224Sthompsa
79181224Sthompsastatic struct cmd gre_cmds[] = {
80181224Sthompsa	DEF_CMD_ARG("grekey",			setifgrekey),
81181224Sthompsa};
82181224Sthompsastatic struct afswtch af_gre = {
83181224Sthompsa	.af_name	= "af_gre",
84181224Sthompsa	.af_af		= AF_UNSPEC,
85181224Sthompsa	.af_other_status = gre_status,
86181224Sthompsa};
87181224Sthompsa
88181224Sthompsastatic __constructor void
89181224Sthompsagre_ctor(void)
90181224Sthompsa{
91181224Sthompsa#define	N(a)	(sizeof(a) / sizeof(a[0]))
92194799Sdelphij	size_t i;
93181224Sthompsa
94181224Sthompsa	for (i = 0; i < N(gre_cmds);  i++)
95181224Sthompsa		cmd_register(&gre_cmds[i]);
96181224Sthompsa	af_register(&af_gre);
97181224Sthompsa#undef N
98181224Sthompsa}
99