15f024827SGleb Smirnoff /* from $OpenBSD: ifconfig.c,v 1.82 2003/10/19 05:43:35 mcbride Exp $ */ 25f024827SGleb Smirnoff 31de7b4b8SPedro F. Giffuni /*- 44d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 51de7b4b8SPedro F. Giffuni * 65f024827SGleb Smirnoff * Copyright (c) 2002 Michael Shalayeff. All rights reserved. 75f024827SGleb Smirnoff * Copyright (c) 2003 Ryan McBride. All rights reserved. 85f024827SGleb Smirnoff * 95f024827SGleb Smirnoff * Redistribution and use in source and binary forms, with or without 105f024827SGleb Smirnoff * modification, are permitted provided that the following conditions 115f024827SGleb Smirnoff * are met: 125f024827SGleb Smirnoff * 1. Redistributions of source code must retain the above copyright 135f024827SGleb Smirnoff * notice, this list of conditions and the following disclaimer. 145f024827SGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright 155f024827SGleb Smirnoff * notice, this list of conditions and the following disclaimer in the 165f024827SGleb Smirnoff * documentation and/or other materials provided with the distribution. 175f024827SGleb Smirnoff * 185f024827SGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 195f024827SGleb Smirnoff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 205f024827SGleb Smirnoff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 215f024827SGleb Smirnoff * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, 225f024827SGleb Smirnoff * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 235f024827SGleb Smirnoff * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 245f024827SGleb Smirnoff * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 255f024827SGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 265f024827SGleb Smirnoff * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 275f024827SGleb Smirnoff * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 285f024827SGleb Smirnoff * THE POSSIBILITY OF SUCH DAMAGE. 295f024827SGleb Smirnoff */ 305f024827SGleb Smirnoff 315f024827SGleb Smirnoff #include <sys/param.h> 325f024827SGleb Smirnoff #include <sys/ioctl.h> 335f024827SGleb Smirnoff #include <sys/socket.h> 345f024827SGleb Smirnoff #include <sys/sockio.h> 355f024827SGleb Smirnoff 365f024827SGleb Smirnoff #include <stdlib.h> 375f024827SGleb Smirnoff #include <unistd.h> 385f024827SGleb Smirnoff 395f024827SGleb Smirnoff #include <net/if.h> 405f024827SGleb Smirnoff #include <netinet/in.h> 415f024827SGleb Smirnoff #include <netinet/in_var.h> 425f024827SGleb Smirnoff #include <netinet/ip_carp.h> 435f024827SGleb Smirnoff 4413781800SKristof Provost #include <arpa/inet.h> 4513781800SKristof Provost 465f024827SGleb Smirnoff #include <ctype.h> 4713781800SKristof Provost #include <stdbool.h> 485f024827SGleb Smirnoff #include <stdio.h> 495f024827SGleb Smirnoff #include <string.h> 505f024827SGleb Smirnoff #include <stdlib.h> 515f024827SGleb Smirnoff #include <unistd.h> 525f024827SGleb Smirnoff #include <err.h> 535f024827SGleb Smirnoff #include <errno.h> 5413781800SKristof Provost #include <netdb.h> 555f024827SGleb Smirnoff 56da393346SRyan Moeller #include <libifconfig.h> 57da393346SRyan Moeller 585f024827SGleb Smirnoff #include "ifconfig.h" 595f024827SGleb Smirnoff 605f024827SGleb Smirnoff static const char *carp_states[] = { CARP_STATES }; 615f024827SGleb Smirnoff 6274b42611SAlexander V. Chernikov static void setcarp_callback(if_ctx *, void *); 635f024827SGleb Smirnoff 645f024827SGleb Smirnoff static int carpr_vhid = -1; 655f024827SGleb Smirnoff static int carpr_advskew = -1; 665f024827SGleb Smirnoff static int carpr_advbase = -1; 675f024827SGleb Smirnoff static int carpr_state = -1; 6813781800SKristof Provost static struct in_addr carp_addr; 6913781800SKristof Provost static struct in6_addr carp_addr6; 705f024827SGleb Smirnoff static unsigned char const *carpr_key; 71*37115154SKristof Provost static carp_version_t carpr_version; 72*37115154SKristof Provost static uint8_t carpr_vrrp_prio; 73*37115154SKristof Provost static uint16_t carpr_vrrp_adv_inter; 745f024827SGleb Smirnoff 755f024827SGleb Smirnoff static void 76c6885dbdSAlexander V. Chernikov carp_status(if_ctx *ctx) 775f024827SGleb Smirnoff { 7840e04359SKristof Provost struct ifconfig_carp carpr[CARP_MAXVHID]; 7913781800SKristof Provost char addr_buf[NI_MAXHOST]; 805f024827SGleb Smirnoff 8185e0016aSAlexander V. Chernikov if (ifconfig_carp_get_info(lifh, ctx->ifname, carpr, CARP_MAXVHID) == -1) 828b222425SRyan Moeller return; 83da393346SRyan Moeller 84da393346SRyan Moeller for (size_t i = 0; i < carpr[0].carpr_count; i++) { 85*37115154SKristof Provost switch (carpr[i].carpr_version) { 86*37115154SKristof Provost case CARP_VERSION_CARP: 875f024827SGleb Smirnoff printf("\tcarp: %s vhid %d advbase %d advskew %d", 885f024827SGleb Smirnoff carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid, 895f024827SGleb Smirnoff carpr[i].carpr_advbase, carpr[i].carpr_advskew); 90c6885dbdSAlexander V. Chernikov if (ctx->args->printkeys && carpr[i].carpr_key[0] != '\0') 915f024827SGleb Smirnoff printf(" key \"%s\"\n", carpr[i].carpr_key); 925f024827SGleb Smirnoff else 935f024827SGleb Smirnoff printf("\n"); 9413781800SKristof Provost 9513781800SKristof Provost inet_ntop(AF_INET6, &carpr[i].carpr_addr6, addr_buf, 9613781800SKristof Provost sizeof(addr_buf)); 9713781800SKristof Provost 9813781800SKristof Provost printf("\t peer %s peer6 %s\n", 9913781800SKristof Provost inet_ntoa(carpr[i].carpr_addr), addr_buf); 100*37115154SKristof Provost break; 101*37115154SKristof Provost case CARP_VERSION_VRRPv3: 102*37115154SKristof Provost printf("\tvrrp: %s vrid %d prio %d interval %d\n", 103*37115154SKristof Provost carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid, 104*37115154SKristof Provost carpr[i].carpr_vrrp_prio, carpr[i].carpr_vrrp_adv_inter); 105*37115154SKristof Provost break; 106*37115154SKristof Provost } 1075f024827SGleb Smirnoff } 1085f024827SGleb Smirnoff } 1095f024827SGleb Smirnoff 1105f024827SGleb Smirnoff static void 1116e3a9d7fSAlexander V. Chernikov setcarp_vhid(if_ctx *ctx, const char *val, int dummy __unused) 1125f024827SGleb Smirnoff { 1136e3a9d7fSAlexander V. Chernikov const struct afswtch *afp = ctx->afp; 1145f024827SGleb Smirnoff 1155f024827SGleb Smirnoff carpr_vhid = atoi(val); 1165f024827SGleb Smirnoff 1175f024827SGleb Smirnoff if (carpr_vhid <= 0 || carpr_vhid > CARP_MAXVHID) 1185f024827SGleb Smirnoff errx(1, "vhid must be greater than 0 and less than %u", 1195f024827SGleb Smirnoff CARP_MAXVHID); 1205f024827SGleb Smirnoff 121a72b7890SAlexander V. Chernikov if (afp->af_setvhid == NULL) 1225f024827SGleb Smirnoff errx(1, "%s doesn't support carp(4)", afp->af_name); 123a72b7890SAlexander V. Chernikov afp->af_setvhid(carpr_vhid); 1245f024827SGleb Smirnoff callback_register(setcarp_callback, NULL); 1255f024827SGleb Smirnoff } 1265f024827SGleb Smirnoff 1275f024827SGleb Smirnoff static void 12885e0016aSAlexander V. Chernikov setcarp_callback(if_ctx *ctx, void *arg __unused) 1295f024827SGleb Smirnoff { 13040e04359SKristof Provost struct ifconfig_carp carpr = { }; 1315f024827SGleb Smirnoff 13285e0016aSAlexander V. Chernikov if (ifconfig_carp_get_vhid(lifh, ctx->ifname, &carpr, carpr_vhid) == -1) { 13340e04359SKristof Provost if (ifconfig_err_errno(lifh) != ENOENT) 13440e04359SKristof Provost return; 13540e04359SKristof Provost } 13640e04359SKristof Provost 1375f024827SGleb Smirnoff carpr.carpr_vhid = carpr_vhid; 1385f024827SGleb Smirnoff if (carpr_key != NULL) 1395f024827SGleb Smirnoff /* XXX Should hash the password into the key here? */ 1405f024827SGleb Smirnoff strlcpy(carpr.carpr_key, carpr_key, CARP_KEY_LEN); 1415f024827SGleb Smirnoff if (carpr_advskew > -1) 1425f024827SGleb Smirnoff carpr.carpr_advskew = carpr_advskew; 1435f024827SGleb Smirnoff if (carpr_advbase > -1) 1445f024827SGleb Smirnoff carpr.carpr_advbase = carpr_advbase; 1455f024827SGleb Smirnoff if (carpr_state > -1) 1465f024827SGleb Smirnoff carpr.carpr_state = carpr_state; 14713781800SKristof Provost if (carp_addr.s_addr != INADDR_ANY) 14813781800SKristof Provost carpr.carpr_addr = carp_addr; 14913781800SKristof Provost if (! IN6_IS_ADDR_UNSPECIFIED(&carp_addr6)) 15013781800SKristof Provost memcpy(&carpr.carpr_addr6, &carp_addr6, 15113781800SKristof Provost sizeof(carp_addr6)); 152*37115154SKristof Provost if (carpr_version != 0) 153*37115154SKristof Provost carpr.carpr_version = carpr_version; 154*37115154SKristof Provost if (carpr_vrrp_prio != 0) 155*37115154SKristof Provost carpr.carpr_vrrp_prio = carpr_vrrp_prio; 156*37115154SKristof Provost if (carpr_vrrp_adv_inter != 0) 157*37115154SKristof Provost carpr.carpr_vrrp_adv_inter = carpr_vrrp_adv_inter; 1585f024827SGleb Smirnoff 15985e0016aSAlexander V. Chernikov if (ifconfig_carp_set_info(lifh, ctx->ifname, &carpr)) 1605f024827SGleb Smirnoff err(1, "SIOCSVH"); 1615f024827SGleb Smirnoff } 1625f024827SGleb Smirnoff 1635f024827SGleb Smirnoff static void 1646e3a9d7fSAlexander V. Chernikov setcarp_passwd(if_ctx *ctx __unused, const char *val, int dummy __unused) 1655f024827SGleb Smirnoff { 1665f024827SGleb Smirnoff 1675f024827SGleb Smirnoff if (carpr_vhid == -1) 1685f024827SGleb Smirnoff errx(1, "passwd requires vhid"); 1695f024827SGleb Smirnoff 1705f024827SGleb Smirnoff carpr_key = val; 1715f024827SGleb Smirnoff } 1725f024827SGleb Smirnoff 1735f024827SGleb Smirnoff static void 1746e3a9d7fSAlexander V. Chernikov setcarp_advskew(if_ctx *ctx __unused, const char *val, int dummy __unused) 1755f024827SGleb Smirnoff { 1765f024827SGleb Smirnoff 1775f024827SGleb Smirnoff if (carpr_vhid == -1) 1785f024827SGleb Smirnoff errx(1, "advskew requires vhid"); 1795f024827SGleb Smirnoff 1805f024827SGleb Smirnoff carpr_advskew = atoi(val); 1815f024827SGleb Smirnoff } 1825f024827SGleb Smirnoff 1835f024827SGleb Smirnoff static void 1846e3a9d7fSAlexander V. Chernikov setcarp_advbase(if_ctx *ctx __unused, const char *val, int dummy __unused) 1855f024827SGleb Smirnoff { 1865f024827SGleb Smirnoff 1875f024827SGleb Smirnoff if (carpr_vhid == -1) 1885f024827SGleb Smirnoff errx(1, "advbase requires vhid"); 1895f024827SGleb Smirnoff 1905f024827SGleb Smirnoff carpr_advbase = atoi(val); 1915f024827SGleb Smirnoff } 1925f024827SGleb Smirnoff 1935f024827SGleb Smirnoff static void 1946e3a9d7fSAlexander V. Chernikov setcarp_state(if_ctx *ctx __unused, const char *val, int dummy __unused) 1955f024827SGleb Smirnoff { 1965f024827SGleb Smirnoff int i; 1975f024827SGleb Smirnoff 1985f024827SGleb Smirnoff if (carpr_vhid == -1) 1995f024827SGleb Smirnoff errx(1, "state requires vhid"); 2005f024827SGleb Smirnoff 2015f024827SGleb Smirnoff for (i = 0; i <= CARP_MAXSTATE; i++) 2025f024827SGleb Smirnoff if (strcasecmp(carp_states[i], val) == 0) { 2035f024827SGleb Smirnoff carpr_state = i; 2045f024827SGleb Smirnoff return; 2055f024827SGleb Smirnoff } 2065f024827SGleb Smirnoff 2075f024827SGleb Smirnoff errx(1, "unknown state"); 2085f024827SGleb Smirnoff } 2095f024827SGleb Smirnoff 21013781800SKristof Provost static void 2116e3a9d7fSAlexander V. Chernikov setcarp_peer(if_ctx *ctx __unused, const char *val, int dummy __unused) 21213781800SKristof Provost { 21313781800SKristof Provost carp_addr.s_addr = inet_addr(val); 21413781800SKristof Provost } 21513781800SKristof Provost 21613781800SKristof Provost static void 2176e3a9d7fSAlexander V. Chernikov setcarp_mcast(if_ctx *ctx __unused, const char *val __unused, int dummy __unused) 21813781800SKristof Provost { 21913781800SKristof Provost carp_addr.s_addr = htonl(INADDR_CARP_GROUP); 22013781800SKristof Provost } 22113781800SKristof Provost 22213781800SKristof Provost static void 2236e3a9d7fSAlexander V. Chernikov setcarp_peer6(if_ctx *ctx __unused, const char *val, int dummy __unused) 22413781800SKristof Provost { 22513781800SKristof Provost struct addrinfo hints, *res; 22613781800SKristof Provost 22713781800SKristof Provost memset(&hints, 0, sizeof(hints)); 22813781800SKristof Provost hints.ai_family = AF_INET6; 22913781800SKristof Provost hints.ai_flags = AI_NUMERICHOST; 23013781800SKristof Provost 2312d9d97e6SKristof Provost if (getaddrinfo(val, NULL, &hints, &res) != 0) 23213781800SKristof Provost errx(1, "Invalid IPv6 address %s", val); 23313781800SKristof Provost 2340c2beef7SAlexander V. Chernikov memcpy(&carp_addr6, &(satosin6(res->ai_addr))->sin6_addr, sizeof(carp_addr6)); 2350ab2c922SKristof Provost freeaddrinfo(res); 23613781800SKristof Provost } 23713781800SKristof Provost 23813781800SKristof Provost static void 2396e3a9d7fSAlexander V. Chernikov setcarp_mcast6(if_ctx *ctx __unused, const char *val __unused, int dummy __unused) 24013781800SKristof Provost { 24113781800SKristof Provost bzero(&carp_addr6, sizeof(carp_addr6)); 24213781800SKristof Provost carp_addr6.s6_addr[0] = 0xff; 24313781800SKristof Provost carp_addr6.s6_addr[1] = 0x02; 24413781800SKristof Provost carp_addr6.s6_addr[15] = 0x12; 24513781800SKristof Provost } 24613781800SKristof Provost 247*37115154SKristof Provost static void 248*37115154SKristof Provost setcarp_version(if_ctx *ctx __unused, const char *val, int dummy __unused) 249*37115154SKristof Provost { 250*37115154SKristof Provost carpr_version = atoi(val); 251*37115154SKristof Provost 252*37115154SKristof Provost if (carpr_version != CARP_VERSION_CARP && carpr_version != CARP_VERSION_VRRPv3) 253*37115154SKristof Provost errx(1, "version must be %d or %d", CARP_VERSION_CARP, 254*37115154SKristof Provost CARP_VERSION_VRRPv3); 255*37115154SKristof Provost } 256*37115154SKristof Provost 257*37115154SKristof Provost static void 258*37115154SKristof Provost setvrrp_prio(if_ctx *ctx __unused, const char *val, int dummy __unused) 259*37115154SKristof Provost { 260*37115154SKristof Provost carpr_vrrp_prio = atoi(val); 261*37115154SKristof Provost } 262*37115154SKristof Provost 263*37115154SKristof Provost static void 264*37115154SKristof Provost setvrrp_interval(if_ctx *ctx __unused, const char *val, int dummy __unused) 265*37115154SKristof Provost { 266*37115154SKristof Provost carpr_vrrp_adv_inter = atoi(val); 267*37115154SKristof Provost 268*37115154SKristof Provost if (carpr_vrrp_adv_inter == 0 || carpr_vrrp_adv_inter > VRRP_MAX_INTERVAL) 269*37115154SKristof Provost errx(1, "vrrpinterval must be greater than 0 and less than %d", VRRP_MAX_INTERVAL); 270*37115154SKristof Provost } 271*37115154SKristof Provost 2725f024827SGleb Smirnoff static struct cmd carp_cmds[] = { 2735f024827SGleb Smirnoff DEF_CMD_ARG("advbase", setcarp_advbase), 2745f024827SGleb Smirnoff DEF_CMD_ARG("advskew", setcarp_advskew), 2755f024827SGleb Smirnoff DEF_CMD_ARG("pass", setcarp_passwd), 2765f024827SGleb Smirnoff DEF_CMD_ARG("vhid", setcarp_vhid), 2775f024827SGleb Smirnoff DEF_CMD_ARG("state", setcarp_state), 27813781800SKristof Provost DEF_CMD_ARG("peer", setcarp_peer), 279f0311046SReid Linnemann DEF_CMD("mcast", 0, setcarp_mcast), 28013781800SKristof Provost DEF_CMD_ARG("peer6", setcarp_peer6), 281f0311046SReid Linnemann DEF_CMD("mcast6", 0, setcarp_mcast6), 282*37115154SKristof Provost DEF_CMD_ARG("carpver", setcarp_version), 283*37115154SKristof Provost DEF_CMD_ARG("vrrpprio", setvrrp_prio), 284*37115154SKristof Provost DEF_CMD_ARG("vrrpinterval", setvrrp_interval), 2855f024827SGleb Smirnoff }; 2865f024827SGleb Smirnoff static struct afswtch af_carp = { 2875f024827SGleb Smirnoff .af_name = "af_carp", 2885f024827SGleb Smirnoff .af_af = AF_UNSPEC, 2895f024827SGleb Smirnoff .af_other_status = carp_status, 2905f024827SGleb Smirnoff }; 2915f024827SGleb Smirnoff 2925f024827SGleb Smirnoff static __constructor void 2935f024827SGleb Smirnoff carp_ctor(void) 2945f024827SGleb Smirnoff { 29513781800SKristof Provost /* Default to multicast. */ 2966e3a9d7fSAlexander V. Chernikov setcarp_mcast(NULL, NULL, 0); 2976e3a9d7fSAlexander V. Chernikov setcarp_mcast6(NULL, NULL, 0); 29813781800SKristof Provost 29944cd85d4SAlexander V. Chernikov for (size_t i = 0; i < nitems(carp_cmds); i++) 3005f024827SGleb Smirnoff cmd_register(&carp_cmds[i]); 3015f024827SGleb Smirnoff af_register(&af_carp); 3025f024827SGleb Smirnoff } 303