1/*- 2 * Copyright 2013 Ermal Luci 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 16 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <sys/param.h> 28#include <sys/ioctl.h> 29#include <sys/socket.h> 30#include <sys/sockio.h> 31 32#include <stdlib.h> 33#include <unistd.h> 34 35#include <net/ethernet.h> 36#include <net/if.h> 37#include <net/route.h> 38 39#include <netinet/in.h> 40#include <sys/mbuf.h> 41#include <net/if_stf.h> 42#include <arpa/inet.h> 43 44#include <ctype.h> 45#include <stdio.h> 46#include <string.h> 47#include <stdlib.h> 48#include <unistd.h> 49#include <err.h> 50#include <errno.h> 51 52#include "ifconfig.h" 53 54static int 55do_cmd(if_ctx *ctx, u_long op, void *arg, size_t argsize, int set) 56{ 57 struct ifdrv ifd = {}; 58 59 strlcpy(ifd.ifd_name, ctx->ifname, sizeof(ifd.ifd_name)); 60 ifd.ifd_cmd = op; 61 ifd.ifd_len = argsize; 62 ifd.ifd_data = arg; 63 64 return (ioctl_ctx(ctx, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd)); 65} 66 67static void 68stf_status(if_ctx *ctx) 69{ 70 struct stfv4args param; 71 72 if (do_cmd(ctx, STF6RD_GV4NET, ¶m, sizeof(param), 0) < 0) 73 return; 74 75 printf("\tv4net %s/%d -> ", inet_ntoa(param.srcv4_addr), 76 param.v4_prefixlen ? param.v4_prefixlen : 32); 77 printf("tv4br %s\n", inet_ntoa(param.braddr)); 78} 79 80static void 81setstf_br(if_ctx *ctx, const char *val, int d __unused) 82{ 83 struct stfv4args req; 84 struct sockaddr_in sin; 85 86 memset(&req, 0, sizeof(req)); 87 88 sin.sin_len = sizeof(sin); 89 sin.sin_family = AF_INET; 90 91 if (!inet_aton(val, &sin.sin_addr)) 92 errx(1, "%s: bad value", val); 93 94 req.braddr = sin.sin_addr; 95 if (do_cmd(ctx, STF6RD_SBR, &req, sizeof(req), 1) < 0) 96 err(1, "STF6RD_SBR%s", val); 97} 98 99static void 100setstf_set(if_ctx *ctx, const char *val, int d __unused) 101{ 102 struct stfv4args req; 103 struct sockaddr_in sin; 104 const char *errstr; 105 char *p = NULL; 106 107 memset(&req, 0, sizeof(req)); 108 109 sin.sin_len = sizeof(sin); 110 sin.sin_family = AF_INET; 111 112 p = strrchr(val, '/'); 113 if (p == NULL) 114 errx(2, "Wrong argument given"); 115 116 *p = '\0'; 117 req.v4_prefixlen = (int)strtonum(p + 1, 0, 32, &errstr); 118 if (errstr != NULL || req.v4_prefixlen == 0) { 119 *p = '/'; 120 errx(1, "%s: bad value (prefix length %s)", val, errstr); 121 } 122 123 if (!inet_aton(val, &sin.sin_addr)) 124 errx(1, "%s: bad value", val); 125 126 memcpy(&req.srcv4_addr, &sin.sin_addr, sizeof(req.srcv4_addr)); 127 if (do_cmd(ctx, STF6RD_SV4NET, &req, sizeof(req), 1) < 0) 128 err(1, "STF6RD_SV4NET %s", val); 129} 130 131static struct cmd stf_cmds[] = { 132 DEF_CMD_ARG("stfv4net", setstf_set), 133 DEF_CMD_ARG("stfv4br", setstf_br), 134}; 135 136static struct afswtch af_stf = { 137 .af_name = "af_stf", 138 .af_af = AF_UNSPEC, 139 .af_other_status = stf_status, 140}; 141 142static __constructor void 143stf_ctor(void) 144{ 145 for (size_t i = 0; i < nitems(stf_cmds); i++) 146 cmd_register(&stf_cmds[i]); 147 af_register(&af_stf); 148} 149