1/* $FreeBSD$ */ 2/* from $OpenBSD: ifconfig.c,v 1.82 2003/10/19 05:43:35 mcbride Exp $ */ 3 4/*- 5 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 6 * 7 * Copyright (c) 2002 Michael Shalayeff. All rights reserved. 8 * Copyright (c) 2003 Ryan McBride. All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, 23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 29 * THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include <sys/param.h> 33#include <sys/ioctl.h> 34#include <sys/socket.h> 35#include <sys/sockio.h> 36 37#include <stdlib.h> 38#include <unistd.h> 39 40#include <net/if.h> 41#include <netinet/in.h> 42#include <netinet/in_var.h> 43#include <netinet/ip_carp.h> 44 45#include <ctype.h> 46#include <stdio.h> 47#include <string.h> 48#include <stdlib.h> 49#include <unistd.h> 50#include <err.h> 51#include <errno.h> 52 53#include "ifconfig.h" 54 55static const char *carp_states[] = { CARP_STATES }; 56 57static void carp_status(int s); 58static void setcarp_vhid(const char *, int, int, const struct afswtch *rafp); 59static void setcarp_callback(int, void *); 60static void setcarp_advbase(const char *,int, int, const struct afswtch *rafp); 61static void setcarp_advskew(const char *, int, int, const struct afswtch *rafp); 62static void setcarp_passwd(const char *, int, int, const struct afswtch *rafp); 63 64static int carpr_vhid = -1; 65static int carpr_advskew = -1; 66static int carpr_advbase = -1; 67static int carpr_state = -1; 68static unsigned char const *carpr_key; 69 70static void 71carp_status(int s) 72{ 73 struct carpreq carpr[CARP_MAXVHID]; 74 int i; 75 76 bzero(carpr, sizeof(struct carpreq) * CARP_MAXVHID); 77 carpr[0].carpr_count = CARP_MAXVHID; 78 ifr.ifr_data = (caddr_t)&carpr; 79 80 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1) 81 return; 82 83 for (i = 0; i < carpr[0].carpr_count; i++) { 84 printf("\tcarp: %s vhid %d advbase %d advskew %d", 85 carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid, 86 carpr[i].carpr_advbase, carpr[i].carpr_advskew); 87 if (printkeys && carpr[i].carpr_key[0] != '\0') 88 printf(" key \"%s\"\n", carpr[i].carpr_key); 89 else 90 printf("\n"); 91 } 92} 93 94static void 95setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp) 96{ 97 98 carpr_vhid = atoi(val); 99 100 if (carpr_vhid <= 0 || carpr_vhid > CARP_MAXVHID) 101 errx(1, "vhid must be greater than 0 and less than %u", 102 CARP_MAXVHID); 103 104 switch (afp->af_af) { 105#ifdef INET 106 case AF_INET: 107 { 108 struct in_aliasreq *ifra; 109 110 ifra = (struct in_aliasreq *)afp->af_addreq; 111 ifra->ifra_vhid = carpr_vhid; 112 break; 113 } 114#endif 115#ifdef INET6 116 case AF_INET6: 117 { 118 struct in6_aliasreq *ifra; 119 120 ifra = (struct in6_aliasreq *)afp->af_addreq; 121 ifra->ifra_vhid = carpr_vhid; 122 break; 123 } 124#endif 125 default: 126 errx(1, "%s doesn't support carp(4)", afp->af_name); 127 } 128 129 callback_register(setcarp_callback, NULL); 130} 131 132static void 133setcarp_callback(int s, void *arg __unused) 134{ 135 struct carpreq carpr; 136 137 bzero(&carpr, sizeof(struct carpreq)); 138 carpr.carpr_vhid = carpr_vhid; 139 carpr.carpr_count = 1; 140 ifr.ifr_data = (caddr_t)&carpr; 141 142 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1 && errno != ENOENT) 143 err(1, "SIOCGVH"); 144 145 if (carpr_key != NULL) 146 /* XXX Should hash the password into the key here? */ 147 strlcpy(carpr.carpr_key, carpr_key, CARP_KEY_LEN); 148 if (carpr_advskew > -1) 149 carpr.carpr_advskew = carpr_advskew; 150 if (carpr_advbase > -1) 151 carpr.carpr_advbase = carpr_advbase; 152 if (carpr_state > -1) 153 carpr.carpr_state = carpr_state; 154 155 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1) 156 err(1, "SIOCSVH"); 157} 158 159static void 160setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp) 161{ 162 163 if (carpr_vhid == -1) 164 errx(1, "passwd requires vhid"); 165 166 carpr_key = val; 167} 168 169static void 170setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp) 171{ 172 173 if (carpr_vhid == -1) 174 errx(1, "advskew requires vhid"); 175 176 carpr_advskew = atoi(val); 177} 178 179static void 180setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp) 181{ 182 183 if (carpr_vhid == -1) 184 errx(1, "advbase requires vhid"); 185 186 carpr_advbase = atoi(val); 187} 188 189static void 190setcarp_state(const char *val, int d, int s, const struct afswtch *afp) 191{ 192 int i; 193 194 if (carpr_vhid == -1) 195 errx(1, "state requires vhid"); 196 197 for (i = 0; i <= CARP_MAXSTATE; i++) 198 if (strcasecmp(carp_states[i], val) == 0) { 199 carpr_state = i; 200 return; 201 } 202 203 errx(1, "unknown state"); 204} 205 206static struct cmd carp_cmds[] = { 207 DEF_CMD_ARG("advbase", setcarp_advbase), 208 DEF_CMD_ARG("advskew", setcarp_advskew), 209 DEF_CMD_ARG("pass", setcarp_passwd), 210 DEF_CMD_ARG("vhid", setcarp_vhid), 211 DEF_CMD_ARG("state", setcarp_state), 212}; 213static struct afswtch af_carp = { 214 .af_name = "af_carp", 215 .af_af = AF_UNSPEC, 216 .af_other_status = carp_status, 217}; 218 219static __constructor void 220carp_ctor(void) 221{ 222 int i; 223 224 for (i = 0; i < nitems(carp_cmds); i++) 225 cmd_register(&carp_cmds[i]); 226 af_register(&af_carp); 227} 228