15f024827SGleb Smirnoff /* $FreeBSD$ */ 25f024827SGleb Smirnoff /* from $OpenBSD: ifconfig.c,v 1.82 2003/10/19 05:43:35 mcbride Exp $ */ 35f024827SGleb Smirnoff 41de7b4b8SPedro F. Giffuni /*- 54d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 61de7b4b8SPedro F. Giffuni * 75f024827SGleb Smirnoff * Copyright (c) 2002 Michael Shalayeff. All rights reserved. 85f024827SGleb Smirnoff * Copyright (c) 2003 Ryan McBride. All rights reserved. 95f024827SGleb Smirnoff * 105f024827SGleb Smirnoff * Redistribution and use in source and binary forms, with or without 115f024827SGleb Smirnoff * modification, are permitted provided that the following conditions 125f024827SGleb Smirnoff * are met: 135f024827SGleb Smirnoff * 1. Redistributions of source code must retain the above copyright 145f024827SGleb Smirnoff * notice, this list of conditions and the following disclaimer. 155f024827SGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright 165f024827SGleb Smirnoff * notice, this list of conditions and the following disclaimer in the 175f024827SGleb Smirnoff * documentation and/or other materials provided with the distribution. 185f024827SGleb Smirnoff * 195f024827SGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 205f024827SGleb Smirnoff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 215f024827SGleb Smirnoff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 225f024827SGleb Smirnoff * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, 235f024827SGleb Smirnoff * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 245f024827SGleb Smirnoff * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 255f024827SGleb Smirnoff * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 265f024827SGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 275f024827SGleb Smirnoff * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 285f024827SGleb Smirnoff * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 295f024827SGleb Smirnoff * THE POSSIBILITY OF SUCH DAMAGE. 305f024827SGleb Smirnoff */ 315f024827SGleb Smirnoff 325f024827SGleb Smirnoff #include <sys/param.h> 335f024827SGleb Smirnoff #include <sys/ioctl.h> 345f024827SGleb Smirnoff #include <sys/socket.h> 355f024827SGleb Smirnoff #include <sys/sockio.h> 365f024827SGleb Smirnoff 375f024827SGleb Smirnoff #include <stdlib.h> 385f024827SGleb Smirnoff #include <unistd.h> 395f024827SGleb Smirnoff 405f024827SGleb Smirnoff #include <net/if.h> 415f024827SGleb Smirnoff #include <netinet/in.h> 425f024827SGleb Smirnoff #include <netinet/in_var.h> 435f024827SGleb Smirnoff #include <netinet/ip_carp.h> 445f024827SGleb Smirnoff 4513781800SKristof Provost #include <arpa/inet.h> 4613781800SKristof Provost 475f024827SGleb Smirnoff #include <ctype.h> 4813781800SKristof Provost #include <stdbool.h> 495f024827SGleb Smirnoff #include <stdio.h> 505f024827SGleb Smirnoff #include <string.h> 515f024827SGleb Smirnoff #include <stdlib.h> 525f024827SGleb Smirnoff #include <unistd.h> 535f024827SGleb Smirnoff #include <err.h> 545f024827SGleb Smirnoff #include <errno.h> 5513781800SKristof Provost #include <netdb.h> 565f024827SGleb Smirnoff 57da393346SRyan Moeller #include <libifconfig.h> 58da393346SRyan Moeller 595f024827SGleb Smirnoff #include "ifconfig.h" 605f024827SGleb Smirnoff 615f024827SGleb Smirnoff static const char *carp_states[] = { CARP_STATES }; 625f024827SGleb Smirnoff 6374b42611SAlexander V. Chernikov static void setcarp_callback(if_ctx *, void *); 645f024827SGleb Smirnoff 655f024827SGleb Smirnoff static int carpr_vhid = -1; 665f024827SGleb Smirnoff static int carpr_advskew = -1; 675f024827SGleb Smirnoff static int carpr_advbase = -1; 685f024827SGleb Smirnoff static int carpr_state = -1; 6913781800SKristof Provost static struct in_addr carp_addr; 7013781800SKristof Provost static struct in6_addr carp_addr6; 715f024827SGleb Smirnoff static unsigned char const *carpr_key; 725f024827SGleb Smirnoff 735f024827SGleb Smirnoff static void 74*c6885dbdSAlexander V. Chernikov carp_status(if_ctx *ctx) 755f024827SGleb Smirnoff { 7640e04359SKristof Provost struct ifconfig_carp carpr[CARP_MAXVHID]; 7713781800SKristof Provost char addr_buf[NI_MAXHOST]; 785f024827SGleb Smirnoff 79da393346SRyan Moeller if (ifconfig_carp_get_info(lifh, name, carpr, CARP_MAXVHID) == -1) 808b222425SRyan Moeller return; 81da393346SRyan Moeller 82da393346SRyan Moeller for (size_t i = 0; i < carpr[0].carpr_count; i++) { 835f024827SGleb Smirnoff printf("\tcarp: %s vhid %d advbase %d advskew %d", 845f024827SGleb Smirnoff carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid, 855f024827SGleb Smirnoff carpr[i].carpr_advbase, carpr[i].carpr_advskew); 86*c6885dbdSAlexander V. Chernikov if (ctx->args->printkeys && carpr[i].carpr_key[0] != '\0') 875f024827SGleb Smirnoff printf(" key \"%s\"\n", carpr[i].carpr_key); 885f024827SGleb Smirnoff else 895f024827SGleb Smirnoff printf("\n"); 9013781800SKristof Provost 9113781800SKristof Provost inet_ntop(AF_INET6, &carpr[i].carpr_addr6, addr_buf, 9213781800SKristof Provost sizeof(addr_buf)); 9313781800SKristof Provost 9413781800SKristof Provost printf("\t peer %s peer6 %s\n", 9513781800SKristof Provost inet_ntoa(carpr[i].carpr_addr), addr_buf); 965f024827SGleb Smirnoff } 975f024827SGleb Smirnoff } 985f024827SGleb Smirnoff 995f024827SGleb Smirnoff static void 1006e3a9d7fSAlexander V. Chernikov setcarp_vhid(if_ctx *ctx, const char *val, int dummy __unused) 1015f024827SGleb Smirnoff { 1026e3a9d7fSAlexander V. Chernikov const struct afswtch *afp = ctx->afp; 1035f024827SGleb Smirnoff 1045f024827SGleb Smirnoff carpr_vhid = atoi(val); 1055f024827SGleb Smirnoff 1065f024827SGleb Smirnoff if (carpr_vhid <= 0 || carpr_vhid > CARP_MAXVHID) 1075f024827SGleb Smirnoff errx(1, "vhid must be greater than 0 and less than %u", 1085f024827SGleb Smirnoff CARP_MAXVHID); 1095f024827SGleb Smirnoff 110a72b7890SAlexander V. Chernikov if (afp->af_setvhid == NULL) 1115f024827SGleb Smirnoff errx(1, "%s doesn't support carp(4)", afp->af_name); 112a72b7890SAlexander V. Chernikov afp->af_setvhid(carpr_vhid); 1135f024827SGleb Smirnoff callback_register(setcarp_callback, NULL); 1145f024827SGleb Smirnoff } 1155f024827SGleb Smirnoff 1165f024827SGleb Smirnoff static void 11774b42611SAlexander V. Chernikov setcarp_callback(if_ctx *ctx __unused, void *arg __unused) 1185f024827SGleb Smirnoff { 11940e04359SKristof Provost struct ifconfig_carp carpr = { }; 1205f024827SGleb Smirnoff 12140e04359SKristof Provost if (ifconfig_carp_get_vhid(lifh, name, &carpr, carpr_vhid) == -1) { 12240e04359SKristof Provost if (ifconfig_err_errno(lifh) != ENOENT) 12340e04359SKristof Provost return; 12440e04359SKristof Provost } 12540e04359SKristof Provost 1265f024827SGleb Smirnoff carpr.carpr_vhid = carpr_vhid; 1275f024827SGleb Smirnoff if (carpr_key != NULL) 1285f024827SGleb Smirnoff /* XXX Should hash the password into the key here? */ 1295f024827SGleb Smirnoff strlcpy(carpr.carpr_key, carpr_key, CARP_KEY_LEN); 1305f024827SGleb Smirnoff if (carpr_advskew > -1) 1315f024827SGleb Smirnoff carpr.carpr_advskew = carpr_advskew; 1325f024827SGleb Smirnoff if (carpr_advbase > -1) 1335f024827SGleb Smirnoff carpr.carpr_advbase = carpr_advbase; 1345f024827SGleb Smirnoff if (carpr_state > -1) 1355f024827SGleb Smirnoff carpr.carpr_state = carpr_state; 13613781800SKristof Provost if (carp_addr.s_addr != INADDR_ANY) 13713781800SKristof Provost carpr.carpr_addr = carp_addr; 13813781800SKristof Provost if (! IN6_IS_ADDR_UNSPECIFIED(&carp_addr6)) 13913781800SKristof Provost memcpy(&carpr.carpr_addr6, &carp_addr6, 14013781800SKristof Provost sizeof(carp_addr6)); 1415f024827SGleb Smirnoff 14240e04359SKristof Provost if (ifconfig_carp_set_info(lifh, name, &carpr)) 1435f024827SGleb Smirnoff err(1, "SIOCSVH"); 1445f024827SGleb Smirnoff } 1455f024827SGleb Smirnoff 1465f024827SGleb Smirnoff static void 1476e3a9d7fSAlexander V. Chernikov setcarp_passwd(if_ctx *ctx __unused, const char *val, int dummy __unused) 1485f024827SGleb Smirnoff { 1495f024827SGleb Smirnoff 1505f024827SGleb Smirnoff if (carpr_vhid == -1) 1515f024827SGleb Smirnoff errx(1, "passwd requires vhid"); 1525f024827SGleb Smirnoff 1535f024827SGleb Smirnoff carpr_key = val; 1545f024827SGleb Smirnoff } 1555f024827SGleb Smirnoff 1565f024827SGleb Smirnoff static void 1576e3a9d7fSAlexander V. Chernikov setcarp_advskew(if_ctx *ctx __unused, const char *val, int dummy __unused) 1585f024827SGleb Smirnoff { 1595f024827SGleb Smirnoff 1605f024827SGleb Smirnoff if (carpr_vhid == -1) 1615f024827SGleb Smirnoff errx(1, "advskew requires vhid"); 1625f024827SGleb Smirnoff 1635f024827SGleb Smirnoff carpr_advskew = atoi(val); 1645f024827SGleb Smirnoff } 1655f024827SGleb Smirnoff 1665f024827SGleb Smirnoff static void 1676e3a9d7fSAlexander V. Chernikov setcarp_advbase(if_ctx *ctx __unused, const char *val, int dummy __unused) 1685f024827SGleb Smirnoff { 1695f024827SGleb Smirnoff 1705f024827SGleb Smirnoff if (carpr_vhid == -1) 1715f024827SGleb Smirnoff errx(1, "advbase requires vhid"); 1725f024827SGleb Smirnoff 1735f024827SGleb Smirnoff carpr_advbase = atoi(val); 1745f024827SGleb Smirnoff } 1755f024827SGleb Smirnoff 1765f024827SGleb Smirnoff static void 1776e3a9d7fSAlexander V. Chernikov setcarp_state(if_ctx *ctx __unused, const char *val, int dummy __unused) 1785f024827SGleb Smirnoff { 1795f024827SGleb Smirnoff int i; 1805f024827SGleb Smirnoff 1815f024827SGleb Smirnoff if (carpr_vhid == -1) 1825f024827SGleb Smirnoff errx(1, "state requires vhid"); 1835f024827SGleb Smirnoff 1845f024827SGleb Smirnoff for (i = 0; i <= CARP_MAXSTATE; i++) 1855f024827SGleb Smirnoff if (strcasecmp(carp_states[i], val) == 0) { 1865f024827SGleb Smirnoff carpr_state = i; 1875f024827SGleb Smirnoff return; 1885f024827SGleb Smirnoff } 1895f024827SGleb Smirnoff 1905f024827SGleb Smirnoff errx(1, "unknown state"); 1915f024827SGleb Smirnoff } 1925f024827SGleb Smirnoff 19313781800SKristof Provost static void 1946e3a9d7fSAlexander V. Chernikov setcarp_peer(if_ctx *ctx __unused, const char *val, int dummy __unused) 19513781800SKristof Provost { 19613781800SKristof Provost carp_addr.s_addr = inet_addr(val); 19713781800SKristof Provost } 19813781800SKristof Provost 19913781800SKristof Provost static void 2006e3a9d7fSAlexander V. Chernikov setcarp_mcast(if_ctx *ctx __unused, const char *val __unused, int dummy __unused) 20113781800SKristof Provost { 20213781800SKristof Provost carp_addr.s_addr = htonl(INADDR_CARP_GROUP); 20313781800SKristof Provost } 20413781800SKristof Provost 20513781800SKristof Provost static void 2066e3a9d7fSAlexander V. Chernikov setcarp_peer6(if_ctx *ctx __unused, const char *val, int dummy __unused) 20713781800SKristof Provost { 20813781800SKristof Provost struct addrinfo hints, *res; 20913781800SKristof Provost 21013781800SKristof Provost memset(&hints, 0, sizeof(hints)); 21113781800SKristof Provost hints.ai_family = AF_INET6; 21213781800SKristof Provost hints.ai_flags = AI_NUMERICHOST; 21313781800SKristof Provost 2142d9d97e6SKristof Provost if (getaddrinfo(val, NULL, &hints, &res) != 0) 21513781800SKristof Provost errx(1, "Invalid IPv6 address %s", val); 21613781800SKristof Provost 2170c2beef7SAlexander V. Chernikov memcpy(&carp_addr6, &(satosin6(res->ai_addr))->sin6_addr, sizeof(carp_addr6)); 2180ab2c922SKristof Provost freeaddrinfo(res); 21913781800SKristof Provost } 22013781800SKristof Provost 22113781800SKristof Provost static void 2226e3a9d7fSAlexander V. Chernikov setcarp_mcast6(if_ctx *ctx __unused, const char *val __unused, int dummy __unused) 22313781800SKristof Provost { 22413781800SKristof Provost bzero(&carp_addr6, sizeof(carp_addr6)); 22513781800SKristof Provost carp_addr6.s6_addr[0] = 0xff; 22613781800SKristof Provost carp_addr6.s6_addr[1] = 0x02; 22713781800SKristof Provost carp_addr6.s6_addr[15] = 0x12; 22813781800SKristof Provost } 22913781800SKristof Provost 2305f024827SGleb Smirnoff static struct cmd carp_cmds[] = { 2315f024827SGleb Smirnoff DEF_CMD_ARG("advbase", setcarp_advbase), 2325f024827SGleb Smirnoff DEF_CMD_ARG("advskew", setcarp_advskew), 2335f024827SGleb Smirnoff DEF_CMD_ARG("pass", setcarp_passwd), 2345f024827SGleb Smirnoff DEF_CMD_ARG("vhid", setcarp_vhid), 2355f024827SGleb Smirnoff DEF_CMD_ARG("state", setcarp_state), 23613781800SKristof Provost DEF_CMD_ARG("peer", setcarp_peer), 237f0311046SReid Linnemann DEF_CMD("mcast", 0, setcarp_mcast), 23813781800SKristof Provost DEF_CMD_ARG("peer6", setcarp_peer6), 239f0311046SReid Linnemann DEF_CMD("mcast6", 0, setcarp_mcast6), 2405f024827SGleb Smirnoff }; 2415f024827SGleb Smirnoff static struct afswtch af_carp = { 2425f024827SGleb Smirnoff .af_name = "af_carp", 2435f024827SGleb Smirnoff .af_af = AF_UNSPEC, 2445f024827SGleb Smirnoff .af_other_status = carp_status, 2455f024827SGleb Smirnoff }; 2465f024827SGleb Smirnoff 2475f024827SGleb Smirnoff static __constructor void 2485f024827SGleb Smirnoff carp_ctor(void) 2495f024827SGleb Smirnoff { 25013781800SKristof Provost /* Default to multicast. */ 2516e3a9d7fSAlexander V. Chernikov setcarp_mcast(NULL, NULL, 0); 2526e3a9d7fSAlexander V. Chernikov setcarp_mcast6(NULL, NULL, 0); 25313781800SKristof Provost 25444cd85d4SAlexander V. Chernikov for (size_t i = 0; i < nitems(carp_cmds); i++) 2555f024827SGleb Smirnoff cmd_register(&carp_cmds[i]); 2565f024827SGleb Smirnoff af_register(&af_carp); 2575f024827SGleb Smirnoff } 258