ifgre.c revision 194799
110154Sache/*- 27768Sache * Copyright (c) 2008 Andrew Thompson. All rights reserved. 37768Sache * 4939Snate * Redistribution and use in source and binary forms, with or without 5939Snate * modification, are permitted provided that the following conditions 6939Snate * are met: 7939Snate * 1. Redistributions of source code must retain the above copyright 8939Snate * notice, this list of conditions and the following disclaimer. 9939Snate * 2. Redistributions in binary form must reproduce the above copyright 10939Snate * notice, this list of conditions and the following disclaimer in the 11939Snate * documentation and/or other materials provided with the distribution. 12939Snate * 13939Snate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14939Snate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15939Snate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16939Snate * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, 1710154Sache * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18939Snate * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19939Snate * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20939Snate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 21939Snate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 22939Snate * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23939Snate * THE POSSIBILITY OF SUCH DAMAGE. 24939Snate */ 25939Snate 2631306Scharnier#ifndef lint 2731306Scharnierstatic const char rcsid[] = 2850476Speter "$FreeBSD: head/sbin/ifconfig/ifgre.c 194799 2009-06-23 23:49:52Z delphij $"; 2931306Scharnier#endif 3031306Scharnier 31939Snate#include <sys/param.h> 32939Snate#include <sys/ioctl.h> 33939Snate#include <sys/socket.h> 34251625Sghelmer#include <sys/sockio.h> 35939Snate 36939Snate#include <stdlib.h> 37242372Smjg#include <unistd.h> 38242372Smjg 39242372Smjg#include <net/ethernet.h> 40939Snate#include <net/if.h> 4124829Sdavidn#include <net/if_gre.h> 427768Sache#include <net/route.h> 43939Snate 4431306Scharnier#include <ctype.h> 4531306Scharnier#include <stdio.h> 46939Snate#include <string.h> 47939Snate#include <stdlib.h> 48170767Syar#include <unistd.h> 49939Snate#include <err.h> 50939Snate#include <errno.h> 51939Snate 52939Snate#include "ifconfig.h" 5331306Scharnier 54939Snatestatic void gre_status(int s); 55939Snate 5610154Sachestatic void 5710154Sachegre_status(int s) 5810154Sache{ 597768Sache int grekey = 0; 607768Sache 61170741Syar ifr.ifr_data = (caddr_t)&grekey; 62170741Syar if (ioctl(s, GREGKEY, &ifr) == 0) 63170741Syar if (grekey != 0) 64170773Syar printf("\tgrekey: %d\n", grekey); 65170773Syar} 66170773Syar 67170773Syarstatic void 68939Snatesetifgrekey(const char *val, int dummy __unused, int s, 69939Snate const struct afswtch *afp) 70939Snate{ 717768Sache uint32_t grekey = atol(val); 72939Snate 73939Snate strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 74939Snate ifr.ifr_data = (caddr_t)&grekey; 757768Sache if (ioctl(s, GRESKEY, (caddr_t)&ifr) < 0) 767768Sache warn("ioctl (set grekey)"); 777768Sache} 787768Sache 797768Sachestatic struct cmd gre_cmds[] = { 807768Sache DEF_CMD_ARG("grekey", setifgrekey), 817768Sache}; 827768Sachestatic struct afswtch af_gre = { 837768Sache .af_name = "af_gre", 847768Sache .af_af = AF_UNSPEC, 857768Sache .af_other_status = gre_status, 867768Sache}; 877768Sache 887768Sachestatic __constructor void 89939Snategre_ctor(void) 90939Snate{ 91170773Syar#define N(a) (sizeof(a) / sizeof(a[0])) 92131990Sstefanf size_t i; 93939Snate 94170767Syar for (i = 0; i < N(gre_cmds); i++) 95170767Syar cmd_register(&gre_cmds[i]); 9690148Simp af_register(&af_gre); 9710154Sache#undef N 98939Snate} 99939Snate