ip_carp.c revision 236297
1228571Sglebius/*- 2228571Sglebius * Copyright (c) 2002 Michael Shalayeff. 3228571Sglebius * Copyright (c) 2003 Ryan McBride. 4228571Sglebius * Copyright (c) 2011 Gleb Smirnoff <glebius@FreeBSD.org> 5228571Sglebius * All rights reserved. 6142215Sglebius * 7142215Sglebius * Redistribution and use in source and binary forms, with or without 8142215Sglebius * modification, are permitted provided that the following conditions 9142215Sglebius * are met: 10142215Sglebius * 1. Redistributions of source code must retain the above copyright 11142215Sglebius * notice, this list of conditions and the following disclaimer. 12142215Sglebius * 2. Redistributions in binary form must reproduce the above copyright 13142215Sglebius * notice, this list of conditions and the following disclaimer in the 14142215Sglebius * documentation and/or other materials provided with the distribution. 15142215Sglebius * 16142215Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17142215Sglebius * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18142215Sglebius * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19142215Sglebius * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, 20142215Sglebius * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21142215Sglebius * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22142215Sglebius * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23142215Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24142215Sglebius * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 25142215Sglebius * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 26142215Sglebius * THE POSSIBILITY OF SUCH DAMAGE. 27142215Sglebius */ 28142215Sglebius 29172467Ssilby#include <sys/cdefs.h> 30172467Ssilby__FBSDID("$FreeBSD: head/sys/netinet/ip_carp.c 236297 2012-05-30 07:11:27Z glebius $"); 31172467Ssilby 32142215Sglebius#include "opt_bpf.h" 33142215Sglebius#include "opt_inet.h" 34142215Sglebius#include "opt_inet6.h" 35142215Sglebius 36142215Sglebius#include <sys/param.h> 37142215Sglebius#include <sys/systm.h> 38228571Sglebius#include <sys/bus.h> 39228571Sglebius#include <sys/jail.h> 40142215Sglebius#include <sys/kernel.h> 41142215Sglebius#include <sys/limits.h> 42142215Sglebius#include <sys/malloc.h> 43142215Sglebius#include <sys/mbuf.h> 44142215Sglebius#include <sys/module.h> 45164033Srwatson#include <sys/priv.h> 46142215Sglebius#include <sys/proc.h> 47211157Swill#include <sys/protosw.h> 48228571Sglebius#include <sys/socket.h> 49228571Sglebius#include <sys/sockio.h> 50142215Sglebius#include <sys/sysctl.h> 51142215Sglebius#include <sys/syslog.h> 52228736Sglebius#include <sys/taskqueue.h> 53142215Sglebius 54142215Sglebius#include <net/ethernet.h> 55142215Sglebius#include <net/fddi.h> 56142215Sglebius#include <net/if.h> 57152410Sru#include <net/if_dl.h> 58228571Sglebius#include <net/if_llatbl.h> 59142215Sglebius#include <net/if_types.h> 60228571Sglebius#include <net/iso88025.h> 61142215Sglebius#include <net/route.h> 62196019Srwatson#include <net/vnet.h> 63142215Sglebius 64221130Sbz#if defined(INET) || defined(INET6) 65142215Sglebius#include <netinet/in.h> 66142215Sglebius#include <netinet/in_var.h> 67221130Sbz#include <netinet/ip_carp.h> 68221130Sbz#include <netinet/ip.h> 69221130Sbz#include <machine/in_cksum.h> 70221130Sbz#endif 71221130Sbz#ifdef INET 72142215Sglebius#include <netinet/ip_var.h> 73142215Sglebius#include <netinet/if_ether.h> 74142215Sglebius#endif 75142215Sglebius 76142215Sglebius#ifdef INET6 77142215Sglebius#include <netinet/icmp6.h> 78142215Sglebius#include <netinet/ip6.h> 79211157Swill#include <netinet6/ip6protosw.h> 80228571Sglebius#include <netinet6/in6_var.h> 81142215Sglebius#include <netinet6/ip6_var.h> 82148387Sume#include <netinet6/scope6_var.h> 83142215Sglebius#include <netinet6/nd6.h> 84142215Sglebius#endif 85142215Sglebius 86142215Sglebius#include <crypto/sha1.h> 87142215Sglebius 88228571Sglebiusstatic MALLOC_DEFINE(M_CARP, "CARP", "CARP addresses"); 89142215Sglebius 90142215Sglebiusstruct carp_softc { 91228571Sglebius struct ifnet *sc_carpdev; /* Pointer to parent ifnet. */ 92228571Sglebius struct ifaddr **sc_ifas; /* Our ifaddrs. */ 93228571Sglebius struct sockaddr_dl sc_addr; /* Our link level address. */ 94228571Sglebius struct callout sc_ad_tmo; /* Advertising timeout. */ 95221130Sbz#ifdef INET 96228571Sglebius struct callout sc_md_tmo; /* Master down timeout. */ 97221130Sbz#endif 98142215Sglebius#ifdef INET6 99228571Sglebius struct callout sc_md6_tmo; /* XXX: Master down timeout. */ 100228571Sglebius#endif 101228571Sglebius struct mtx sc_mtx; 102142215Sglebius 103228571Sglebius int sc_vhid; 104228571Sglebius int sc_advskew; 105228571Sglebius int sc_advbase; 106228571Sglebius 107228571Sglebius int sc_naddrs; 108228571Sglebius int sc_naddrs6; 109228571Sglebius int sc_ifasiz; 110142215Sglebius enum { INIT = 0, BACKUP, MASTER } sc_state; 111228571Sglebius int sc_suppress; 112228571Sglebius int sc_sendad_errors; 113142215Sglebius#define CARP_SENDAD_MAX_ERRORS 3 114228571Sglebius int sc_sendad_success; 115142215Sglebius#define CARP_SENDAD_MIN_SUCCESS 3 116142215Sglebius 117228571Sglebius int sc_init_counter; 118228571Sglebius uint64_t sc_counter; 119142215Sglebius 120142215Sglebius /* authentication */ 121228571Sglebius#define CARP_HMAC_PAD 64 122142215Sglebius unsigned char sc_key[CARP_KEY_LEN]; 123142215Sglebius unsigned char sc_pad[CARP_HMAC_PAD]; 124142215Sglebius SHA1_CTX sc_sha1; 125142215Sglebius 126228571Sglebius TAILQ_ENTRY(carp_softc) sc_list; /* On the carp_if list. */ 127228571Sglebius LIST_ENTRY(carp_softc) sc_next; /* On the global list. */ 128142215Sglebius}; 129142215Sglebius 130228571Sglebiusstruct carp_if { 131228571Sglebius#ifdef INET 132228571Sglebius int cif_naddrs; 133228571Sglebius#endif 134228571Sglebius#ifdef INET6 135228571Sglebius int cif_naddrs6; 136228571Sglebius#endif 137228571Sglebius TAILQ_HEAD(, carp_softc) cif_vrs; 138228571Sglebius#ifdef INET 139228571Sglebius struct ip_moptions cif_imo; 140228571Sglebius#endif 141228571Sglebius#ifdef INET6 142228571Sglebius struct ip6_moptions cif_im6o; 143228571Sglebius#endif 144228571Sglebius struct ifnet *cif_ifp; 145228571Sglebius struct mtx cif_mtx; 146228571Sglebius}; 147228571Sglebius 148228571Sglebius#define CARP_INET 0 149228571Sglebius#define CARP_INET6 1 150228571Sglebiusstatic int proto_reg[] = {-1, -1}; 151228571Sglebius 152228571Sglebius/* 153228571Sglebius * Brief design of carp(4). 154228571Sglebius * 155228571Sglebius * Any carp-capable ifnet may have a list of carp softcs hanging off 156228571Sglebius * its ifp->if_carp pointer. Each softc represents one unique virtual 157228571Sglebius * host id, or vhid. The softc has a back pointer to the ifnet. All 158228571Sglebius * softcs are joined in a global list, which has quite limited use. 159228571Sglebius * 160228571Sglebius * Any interface address that takes part in CARP negotiation has a 161228571Sglebius * pointer to the softc of its vhid, ifa->ifa_carp. That could be either 162228571Sglebius * AF_INET or AF_INET6 address. 163228571Sglebius * 164228571Sglebius * Although, one can get the softc's backpointer to ifnet and traverse 165228571Sglebius * through its ifp->if_addrhead queue to find all interface addresses 166228571Sglebius * involved in CARP, we keep a growable array of ifaddr pointers. This 167228571Sglebius * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that 168228571Sglebius * do calls into the network stack, thus avoiding LORs. 169228571Sglebius * 170228571Sglebius * Locking: 171228571Sglebius * 172228571Sglebius * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(), 173228571Sglebius * callout-driven events and ioctl()s. 174228571Sglebius * 175228571Sglebius * To traverse the list of softcs on an ifnet we use CIF_LOCK(), to 176228571Sglebius * traverse the global list we use the mutex carp_mtx. 177228571Sglebius * 178228571Sglebius * Known issues with locking: 179228571Sglebius * 180228571Sglebius * - There is no protection for races between two ioctl() requests, 181228571Sglebius * neither SIOCSVH, nor SIOCAIFADDR & SIOCAIFADDR_IN6. I think that all 182228571Sglebius * interface ioctl()s should be serialized right in net/if.c. 183228571Sglebius * - Sending ad, we put the pointer to the softc in an mtag, and no reference 184228571Sglebius * counting is done on the softc. 185228571Sglebius * - On module unload we may race (?) with packet processing thread 186228571Sglebius * dereferencing our function pointers. 187228571Sglebius */ 188228571Sglebius 189228736Sglebiusstatic int carp_allow = 1; /* Accept incoming CARP packets. */ 190228736Sglebiusstatic int carp_preempt = 0; /* Preempt slower nodes. */ 191228736Sglebiusstatic int carp_log = 1; /* Log level. */ 192228736Sglebiusstatic int carp_demotion = 0; /* Global advskew demotion. */ 193228736Sglebiusstatic int carp_senderr_adj = CARP_MAXSKEW; /* Send error demotion factor */ 194228736Sglebiusstatic int carp_ifdown_adj = CARP_MAXSKEW; /* Iface down demotion factor */ 195228736Sglebius 196211157SwillSYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP"); 197228736SglebiusSYSCTL_INT(_net_inet_carp, OID_AUTO, allow, CTLFLAG_RW, &carp_allow, 0, 198228736Sglebius "Accept incoming CARP packets"); 199228736SglebiusSYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_RW, &carp_preempt, 0, 200228736Sglebius "High-priority backup preemption mode"); 201228736SglebiusSYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_RW, &carp_log, 0, 202228736Sglebius "CARP log level"); 203228736SglebiusSYSCTL_INT(_net_inet_carp, OID_AUTO, demotion, CTLFLAG_RW, &carp_demotion, 0, 204228736Sglebius "Demotion factor (skew of advskew)"); 205228736SglebiusSYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor, CTLFLAG_RW, 206228736Sglebius &carp_senderr_adj, 0, "Send error demotion factor adjustment"); 207228736SglebiusSYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor, CTLFLAG_RW, 208228736Sglebius &carp_ifdown_adj, 0, "Interface down demotion factor adjustment"); 209142215Sglebius 210228736Sglebiusstatic struct carpstats carpstats; 211228736SglebiusSYSCTL_STRUCT(_net_inet_carp, OID_AUTO, stats, CTLFLAG_RW, &carpstats, 212228736Sglebius carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)"); 213142215Sglebius 214228571Sglebius#define CARP_LOCK_INIT(sc) mtx_init(&(sc)->sc_mtx, "carp_softc", \ 215142215Sglebius NULL, MTX_DEF) 216228571Sglebius#define CARP_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx) 217228571Sglebius#define CARP_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED) 218228571Sglebius#define CARP_LOCK(sc) mtx_lock(&(sc)->sc_mtx) 219228571Sglebius#define CARP_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx) 220228571Sglebius#define CIF_LOCK_INIT(cif) mtx_init(&(cif)->cif_mtx, "carp_if", \ 221228571Sglebius NULL, MTX_DEF) 222228571Sglebius#define CIF_LOCK_DESTROY(cif) mtx_destroy(&(cif)->cif_mtx) 223228571Sglebius#define CIF_LOCK_ASSERT(cif) mtx_assert(&(cif)->cif_mtx, MA_OWNED) 224228571Sglebius#define CIF_LOCK(cif) mtx_lock(&(cif)->cif_mtx) 225228571Sglebius#define CIF_UNLOCK(cif) mtx_unlock(&(cif)->cif_mtx) 226234130Sglebius#define CIF_FREE(cif) do { \ 227234130Sglebius CIF_LOCK_ASSERT(cif); \ 228234130Sglebius if (TAILQ_EMPTY(&(cif)->cif_vrs)) \ 229234130Sglebius carp_free_if(cif); \ 230234130Sglebius else \ 231234130Sglebius CIF_UNLOCK(cif); \ 232234130Sglebius} while (0) 233142215Sglebius 234142451Sglebius#define CARP_LOG(...) do { \ 235228736Sglebius if (carp_log > 0) \ 236228571Sglebius log(LOG_INFO, "carp: " __VA_ARGS__); \ 237142451Sglebius} while (0) 238142215Sglebius 239142451Sglebius#define CARP_DEBUG(...) do { \ 240228736Sglebius if (carp_log > 1) \ 241142446Sglebius log(LOG_DEBUG, __VA_ARGS__); \ 242142451Sglebius} while (0) 243142446Sglebius 244228571Sglebius#define IFNET_FOREACH_IFA(ifp, ifa) \ 245228571Sglebius IF_ADDR_LOCK_ASSERT(ifp); \ 246228571Sglebius TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \ 247228571Sglebius if ((ifa)->ifa_carp != NULL) 248228571Sglebius 249228571Sglebius#define CARP_FOREACH_IFA(sc, ifa) \ 250228571Sglebius CARP_LOCK_ASSERT(sc); \ 251228571Sglebius for (int _i = 0; \ 252228571Sglebius _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \ 253228571Sglebius ((ifa) = sc->sc_ifas[_i]) != NULL; \ 254228571Sglebius ++_i) 255228571Sglebius 256228571Sglebius#define IFNET_FOREACH_CARP(ifp, sc) \ 257228571Sglebius CIF_LOCK_ASSERT(ifp->if_carp); \ 258228571Sglebius TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list) 259228571Sglebius 260228736Sglebius#define DEMOTE_ADVSKEW(sc) \ 261228736Sglebius (((sc)->sc_advskew + carp_demotion > CARP_MAXSKEW) ? \ 262228736Sglebius CARP_MAXSKEW : ((sc)->sc_advskew + carp_demotion)) 263228736Sglebius 264142559Sglebiusstatic void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t); 265228571Sglebiusstatic struct carp_softc 266228571Sglebius *carp_alloc(struct ifnet *); 267234130Sglebiusstatic void carp_detach_locked(struct ifaddr *); 268228571Sglebiusstatic void carp_destroy(struct carp_softc *); 269228571Sglebiusstatic struct carp_if 270228571Sglebius *carp_alloc_if(struct ifnet *); 271228571Sglebiusstatic void carp_free_if(struct carp_if *); 272228571Sglebiusstatic void carp_set_state(struct carp_softc *, int); 273228571Sglebiusstatic void carp_sc_state(struct carp_softc *); 274228571Sglebiusstatic void carp_setrun(struct carp_softc *, sa_family_t); 275228571Sglebiusstatic void carp_master_down(void *); 276228571Sglebiusstatic void carp_master_down_locked(struct carp_softc *); 277142559Sglebiusstatic void carp_send_ad(void *); 278142914Sglebiusstatic void carp_send_ad_locked(struct carp_softc *); 279228571Sglebiusstatic void carp_addroute(struct carp_softc *); 280230863Sglebiusstatic void carp_ifa_addroute(struct ifaddr *); 281228571Sglebiusstatic void carp_delroute(struct carp_softc *); 282230863Sglebiusstatic void carp_ifa_delroute(struct ifaddr *); 283228736Sglebiusstatic void carp_send_ad_all(void *, int); 284228736Sglebiusstatic void carp_demote_adj(int, char *); 285142215Sglebius 286228571Sglebiusstatic LIST_HEAD(, carp_softc) carp_list; 287142911Sglebiusstatic struct mtx carp_mtx; 288228736Sglebiusstatic struct task carp_sendall_task = 289228736Sglebius TASK_INITIALIZER(0, carp_send_ad_all, NULL); 290142215Sglebius 291228571Sglebiusstatic __inline uint16_t 292142215Sglebiuscarp_cksum(struct mbuf *m, int len) 293142215Sglebius{ 294142215Sglebius return (in_cksum(m, len)); 295142215Sglebius} 296142215Sglebius 297142559Sglebiusstatic void 298142215Sglebiuscarp_hmac_prepare(struct carp_softc *sc) 299142215Sglebius{ 300228571Sglebius uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT; 301228571Sglebius uint8_t vhid = sc->sc_vhid & 0xff; 302142215Sglebius struct ifaddr *ifa; 303179490Smlaier int i, found; 304179490Smlaier#ifdef INET 305179490Smlaier struct in_addr last, cur, in; 306179490Smlaier#endif 307142215Sglebius#ifdef INET6 308179490Smlaier struct in6_addr last6, cur6, in6; 309142215Sglebius#endif 310142215Sglebius 311228571Sglebius CARP_LOCK_ASSERT(sc); 312142914Sglebius 313228571Sglebius /* Compute ipad from key. */ 314142215Sglebius bzero(sc->sc_pad, sizeof(sc->sc_pad)); 315142215Sglebius bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key)); 316142215Sglebius for (i = 0; i < sizeof(sc->sc_pad); i++) 317142215Sglebius sc->sc_pad[i] ^= 0x36; 318142215Sglebius 319228571Sglebius /* Precompute first part of inner hash. */ 320142215Sglebius SHA1Init(&sc->sc_sha1); 321142215Sglebius SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad)); 322142215Sglebius SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version)); 323142215Sglebius SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type)); 324142215Sglebius SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid)); 325142215Sglebius#ifdef INET 326179490Smlaier cur.s_addr = 0; 327179490Smlaier do { 328179490Smlaier found = 0; 329179490Smlaier last = cur; 330179490Smlaier cur.s_addr = 0xffffffff; 331228571Sglebius CARP_FOREACH_IFA(sc, ifa) { 332179490Smlaier in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr; 333179490Smlaier if (ifa->ifa_addr->sa_family == AF_INET && 334179490Smlaier ntohl(in.s_addr) > ntohl(last.s_addr) && 335179490Smlaier ntohl(in.s_addr) < ntohl(cur.s_addr)) { 336179490Smlaier cur.s_addr = in.s_addr; 337179490Smlaier found++; 338179490Smlaier } 339179490Smlaier } 340179490Smlaier if (found) 341179490Smlaier SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur)); 342179490Smlaier } while (found); 343142215Sglebius#endif /* INET */ 344142215Sglebius#ifdef INET6 345179490Smlaier memset(&cur6, 0, sizeof(cur6)); 346179490Smlaier do { 347179490Smlaier found = 0; 348179490Smlaier last6 = cur6; 349179490Smlaier memset(&cur6, 0xff, sizeof(cur6)); 350228571Sglebius CARP_FOREACH_IFA(sc, ifa) { 351142215Sglebius in6 = ifatoia6(ifa)->ia_addr.sin6_addr; 352179490Smlaier if (IN6_IS_SCOPE_EMBED(&in6)) 353179490Smlaier in6.s6_addr16[1] = 0; 354179490Smlaier if (ifa->ifa_addr->sa_family == AF_INET6 && 355179490Smlaier memcmp(&in6, &last6, sizeof(in6)) > 0 && 356179490Smlaier memcmp(&in6, &cur6, sizeof(in6)) < 0) { 357179490Smlaier cur6 = in6; 358179490Smlaier found++; 359179490Smlaier } 360142215Sglebius } 361179490Smlaier if (found) 362179490Smlaier SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6)); 363179490Smlaier } while (found); 364142215Sglebius#endif /* INET6 */ 365142215Sglebius 366142215Sglebius /* convert ipad to opad */ 367142215Sglebius for (i = 0; i < sizeof(sc->sc_pad); i++) 368142215Sglebius sc->sc_pad[i] ^= 0x36 ^ 0x5c; 369142215Sglebius} 370142215Sglebius 371142559Sglebiusstatic void 372228571Sglebiuscarp_hmac_generate(struct carp_softc *sc, uint32_t counter[2], 373142215Sglebius unsigned char md[20]) 374142215Sglebius{ 375142215Sglebius SHA1_CTX sha1ctx; 376142215Sglebius 377228571Sglebius CARP_LOCK_ASSERT(sc); 378228571Sglebius 379142215Sglebius /* fetch first half of inner hash */ 380142215Sglebius bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx)); 381142215Sglebius 382142215Sglebius SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter)); 383142215Sglebius SHA1Final(md, &sha1ctx); 384142215Sglebius 385142215Sglebius /* outer hash */ 386142215Sglebius SHA1Init(&sha1ctx); 387142215Sglebius SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad)); 388142215Sglebius SHA1Update(&sha1ctx, md, 20); 389142215Sglebius SHA1Final(md, &sha1ctx); 390142215Sglebius} 391142215Sglebius 392142559Sglebiusstatic int 393228571Sglebiuscarp_hmac_verify(struct carp_softc *sc, uint32_t counter[2], 394142215Sglebius unsigned char md[20]) 395142215Sglebius{ 396142215Sglebius unsigned char md2[20]; 397142215Sglebius 398228571Sglebius CARP_LOCK_ASSERT(sc); 399142914Sglebius 400142215Sglebius carp_hmac_generate(sc, counter, md2); 401142215Sglebius 402142215Sglebius return (bcmp(md, md2, sizeof(md2))); 403142215Sglebius} 404142215Sglebius 405166423Sglebius/* 406142215Sglebius * process input packet. 407142215Sglebius * we have rearranged checks order compared to the rfc, 408142215Sglebius * but it seems more efficient this way or not possible otherwise. 409142215Sglebius */ 410221130Sbz#ifdef INET 411142215Sglebiusvoid 412142215Sglebiuscarp_input(struct mbuf *m, int hlen) 413142215Sglebius{ 414142215Sglebius struct ip *ip = mtod(m, struct ip *); 415142215Sglebius struct carp_header *ch; 416142215Sglebius int iplen, len; 417142215Sglebius 418190968Srwatson CARPSTATS_INC(carps_ipackets); 419142215Sglebius 420228736Sglebius if (!carp_allow) { 421142215Sglebius m_freem(m); 422142215Sglebius return; 423142215Sglebius } 424142215Sglebius 425142215Sglebius /* verify that the IP TTL is 255. */ 426142215Sglebius if (ip->ip_ttl != CARP_DFLTTL) { 427190968Srwatson CARPSTATS_INC(carps_badttl); 428228571Sglebius CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__, 429142446Sglebius ip->ip_ttl, 430142446Sglebius m->m_pkthdr.rcvif->if_xname); 431142215Sglebius m_freem(m); 432142215Sglebius return; 433142215Sglebius } 434142215Sglebius 435142215Sglebius iplen = ip->ip_hl << 2; 436142215Sglebius 437142215Sglebius if (m->m_pkthdr.len < iplen + sizeof(*ch)) { 438190968Srwatson CARPSTATS_INC(carps_badlen); 439228571Sglebius CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) " 440228571Sglebius "on %s\n", __func__, m->m_len - sizeof(struct ip), 441195976Sdelphij m->m_pkthdr.rcvif->if_xname); 442142215Sglebius m_freem(m); 443142215Sglebius return; 444142215Sglebius } 445142215Sglebius 446142215Sglebius if (iplen + sizeof(*ch) < m->m_len) { 447142215Sglebius if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) { 448190968Srwatson CARPSTATS_INC(carps_hdrops); 449228571Sglebius CARP_DEBUG("%s: pullup failed\n", __func__); 450142215Sglebius return; 451142215Sglebius } 452142215Sglebius ip = mtod(m, struct ip *); 453142215Sglebius } 454142215Sglebius ch = (struct carp_header *)((char *)ip + iplen); 455142215Sglebius 456142215Sglebius /* 457142215Sglebius * verify that the received packet length is 458142215Sglebius * equal to the CARP header 459142215Sglebius */ 460142215Sglebius len = iplen + sizeof(*ch); 461142215Sglebius if (len > m->m_pkthdr.len) { 462190968Srwatson CARPSTATS_INC(carps_badlen); 463228571Sglebius CARP_DEBUG("%s: packet too short %d on %s\n", __func__, 464142446Sglebius m->m_pkthdr.len, 465142446Sglebius m->m_pkthdr.rcvif->if_xname); 466142215Sglebius m_freem(m); 467142215Sglebius return; 468142215Sglebius } 469142215Sglebius 470142215Sglebius if ((m = m_pullup(m, len)) == NULL) { 471190968Srwatson CARPSTATS_INC(carps_hdrops); 472142215Sglebius return; 473142215Sglebius } 474142215Sglebius ip = mtod(m, struct ip *); 475142215Sglebius ch = (struct carp_header *)((char *)ip + iplen); 476142215Sglebius 477142215Sglebius /* verify the CARP checksum */ 478142215Sglebius m->m_data += iplen; 479142215Sglebius if (carp_cksum(m, len - iplen)) { 480190968Srwatson CARPSTATS_INC(carps_badsum); 481228571Sglebius CARP_DEBUG("%s: checksum failed on %s\n", __func__, 482142446Sglebius m->m_pkthdr.rcvif->if_xname); 483142215Sglebius m_freem(m); 484142215Sglebius return; 485142215Sglebius } 486142215Sglebius m->m_data -= iplen; 487142215Sglebius 488142446Sglebius carp_input_c(m, ch, AF_INET); 489142215Sglebius} 490221130Sbz#endif 491142215Sglebius 492142215Sglebius#ifdef INET6 493142215Sglebiusint 494142215Sglebiuscarp6_input(struct mbuf **mp, int *offp, int proto) 495142215Sglebius{ 496142215Sglebius struct mbuf *m = *mp; 497142215Sglebius struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 498142215Sglebius struct carp_header *ch; 499142215Sglebius u_int len; 500142215Sglebius 501190968Srwatson CARPSTATS_INC(carps_ipackets6); 502142215Sglebius 503228736Sglebius if (!carp_allow) { 504142215Sglebius m_freem(m); 505142215Sglebius return (IPPROTO_DONE); 506142215Sglebius } 507142215Sglebius 508142215Sglebius /* check if received on a valid carp interface */ 509142215Sglebius if (m->m_pkthdr.rcvif->if_carp == NULL) { 510190968Srwatson CARPSTATS_INC(carps_badif); 511228571Sglebius CARP_DEBUG("%s: packet received on non-carp interface: %s\n", 512228571Sglebius __func__, m->m_pkthdr.rcvif->if_xname); 513142215Sglebius m_freem(m); 514142215Sglebius return (IPPROTO_DONE); 515142215Sglebius } 516142215Sglebius 517142215Sglebius /* verify that the IP TTL is 255 */ 518142215Sglebius if (ip6->ip6_hlim != CARP_DFLTTL) { 519190968Srwatson CARPSTATS_INC(carps_badttl); 520228571Sglebius CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__, 521228571Sglebius ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname); 522142215Sglebius m_freem(m); 523142215Sglebius return (IPPROTO_DONE); 524142215Sglebius } 525142215Sglebius 526142215Sglebius /* verify that we have a complete carp packet */ 527142215Sglebius len = m->m_len; 528142215Sglebius IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch)); 529142215Sglebius if (ch == NULL) { 530190968Srwatson CARPSTATS_INC(carps_badlen); 531228571Sglebius CARP_DEBUG("%s: packet size %u too small\n", __func__, len); 532142215Sglebius return (IPPROTO_DONE); 533142215Sglebius } 534142215Sglebius 535142215Sglebius 536142215Sglebius /* verify the CARP checksum */ 537142215Sglebius m->m_data += *offp; 538142215Sglebius if (carp_cksum(m, sizeof(*ch))) { 539190968Srwatson CARPSTATS_INC(carps_badsum); 540228571Sglebius CARP_DEBUG("%s: checksum failed, on %s\n", __func__, 541142446Sglebius m->m_pkthdr.rcvif->if_xname); 542142215Sglebius m_freem(m); 543142215Sglebius return (IPPROTO_DONE); 544142215Sglebius } 545142215Sglebius m->m_data -= *offp; 546142215Sglebius 547142446Sglebius carp_input_c(m, ch, AF_INET6); 548142215Sglebius return (IPPROTO_DONE); 549142215Sglebius} 550142215Sglebius#endif /* INET6 */ 551142215Sglebius 552142559Sglebiusstatic void 553142446Sglebiuscarp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af) 554142215Sglebius{ 555142215Sglebius struct ifnet *ifp = m->m_pkthdr.rcvif; 556228571Sglebius struct ifaddr *ifa; 557142446Sglebius struct carp_softc *sc; 558228571Sglebius uint64_t tmp_counter; 559142215Sglebius struct timeval sc_tv, ch_tv; 560142215Sglebius 561142215Sglebius /* verify that the VHID is valid on the receiving interface */ 562229621Sjhb IF_ADDR_RLOCK(ifp); 563228571Sglebius IFNET_FOREACH_IFA(ifp, ifa) 564228571Sglebius if (ifa->ifa_addr->sa_family == af && 565228571Sglebius ifa->ifa_carp->sc_vhid == ch->carp_vhid) { 566228571Sglebius ifa_ref(ifa); 567142215Sglebius break; 568228571Sglebius } 569229621Sjhb IF_ADDR_RUNLOCK(ifp); 570142914Sglebius 571228571Sglebius if (ifa == NULL) { 572190968Srwatson CARPSTATS_INC(carps_badvhid); 573142215Sglebius m_freem(m); 574142215Sglebius return; 575142215Sglebius } 576142215Sglebius 577142215Sglebius /* verify the CARP version. */ 578142215Sglebius if (ch->carp_version != CARP_VERSION) { 579190968Srwatson CARPSTATS_INC(carps_badver); 580228571Sglebius CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname, 581142446Sglebius ch->carp_version); 582228571Sglebius ifa_free(ifa); 583142215Sglebius m_freem(m); 584142215Sglebius return; 585142215Sglebius } 586142215Sglebius 587228571Sglebius sc = ifa->ifa_carp; 588228571Sglebius CARP_LOCK(sc); 589228571Sglebius ifa_free(ifa); 590228571Sglebius 591142215Sglebius if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) { 592190968Srwatson CARPSTATS_INC(carps_badauth); 593228571Sglebius CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__, 594228571Sglebius sc->sc_vhid, ifp->if_xname); 595228571Sglebius goto out; 596142215Sglebius } 597142215Sglebius 598142215Sglebius tmp_counter = ntohl(ch->carp_counter[0]); 599142215Sglebius tmp_counter = tmp_counter<<32; 600142215Sglebius tmp_counter += ntohl(ch->carp_counter[1]); 601142215Sglebius 602142215Sglebius /* XXX Replay protection goes here */ 603142215Sglebius 604142215Sglebius sc->sc_init_counter = 0; 605142215Sglebius sc->sc_counter = tmp_counter; 606142215Sglebius 607142215Sglebius sc_tv.tv_sec = sc->sc_advbase; 608228736Sglebius sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256; 609142215Sglebius ch_tv.tv_sec = ch->carp_advbase; 610142215Sglebius ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256; 611142215Sglebius 612142215Sglebius switch (sc->sc_state) { 613142215Sglebius case INIT: 614142215Sglebius break; 615142215Sglebius case MASTER: 616142215Sglebius /* 617142215Sglebius * If we receive an advertisement from a master who's going to 618142215Sglebius * be more frequent than us, go into BACKUP state. 619142215Sglebius */ 620142215Sglebius if (timevalcmp(&sc_tv, &ch_tv, >) || 621142215Sglebius timevalcmp(&sc_tv, &ch_tv, ==)) { 622142215Sglebius callout_stop(&sc->sc_ad_tmo); 623228571Sglebius CARP_LOG("VHID %u@%s: MASTER -> BACKUP " 624228571Sglebius "(more frequent advertisement received)\n", 625228571Sglebius sc->sc_vhid, 626228571Sglebius sc->sc_carpdev->if_xname); 627142215Sglebius carp_set_state(sc, BACKUP); 628142215Sglebius carp_setrun(sc, 0); 629228571Sglebius carp_delroute(sc); 630142215Sglebius } 631142215Sglebius break; 632142215Sglebius case BACKUP: 633142215Sglebius /* 634142215Sglebius * If we're pre-empting masters who advertise slower than us, 635142215Sglebius * and this one claims to be slower, treat him as down. 636142215Sglebius */ 637228736Sglebius if (carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) { 638228571Sglebius CARP_LOG("VHID %u@%s: BACKUP -> MASTER " 639142452Sglebius "(preempting a slower master)\n", 640228571Sglebius sc->sc_vhid, 641228571Sglebius sc->sc_carpdev->if_xname); 642142914Sglebius carp_master_down_locked(sc); 643142215Sglebius break; 644142215Sglebius } 645142215Sglebius 646142215Sglebius /* 647142215Sglebius * If the master is going to advertise at such a low frequency 648142215Sglebius * that he's guaranteed to time out, we'd might as well just 649142215Sglebius * treat him as timed out now. 650142215Sglebius */ 651142215Sglebius sc_tv.tv_sec = sc->sc_advbase * 3; 652142215Sglebius if (timevalcmp(&sc_tv, &ch_tv, <)) { 653228571Sglebius CARP_LOG("VHID %u@%s: BACKUP -> MASTER " 654142452Sglebius "(master timed out)\n", 655228571Sglebius sc->sc_vhid, 656228571Sglebius sc->sc_carpdev->if_xname); 657142914Sglebius carp_master_down_locked(sc); 658142215Sglebius break; 659142215Sglebius } 660142215Sglebius 661142215Sglebius /* 662142215Sglebius * Otherwise, we reset the counter and wait for the next 663142215Sglebius * advertisement. 664142215Sglebius */ 665142215Sglebius carp_setrun(sc, af); 666142215Sglebius break; 667142215Sglebius } 668142215Sglebius 669228571Sglebiusout: 670228571Sglebius CARP_UNLOCK(sc); 671142215Sglebius m_freem(m); 672142215Sglebius} 673142215Sglebius 674142559Sglebiusstatic int 675142215Sglebiuscarp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch) 676142215Sglebius{ 677142215Sglebius struct m_tag *mtag; 678142215Sglebius 679142215Sglebius if (sc->sc_init_counter) { 680142215Sglebius /* this could also be seconds since unix epoch */ 681142215Sglebius sc->sc_counter = arc4random(); 682142215Sglebius sc->sc_counter = sc->sc_counter << 32; 683142215Sglebius sc->sc_counter += arc4random(); 684142215Sglebius } else 685142215Sglebius sc->sc_counter++; 686142215Sglebius 687142215Sglebius ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff); 688142215Sglebius ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff); 689142215Sglebius 690142215Sglebius carp_hmac_generate(sc, ch->carp_counter, ch->carp_md); 691142215Sglebius 692142215Sglebius /* Tag packet for carp_output */ 693228571Sglebius if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *), 694228571Sglebius M_NOWAIT)) == NULL) { 695142215Sglebius m_freem(m); 696228571Sglebius CARPSTATS_INC(carps_onomem); 697142215Sglebius return (ENOMEM); 698142215Sglebius } 699228571Sglebius bcopy(&sc, (caddr_t)(mtag + 1), sizeof(struct carp_softc *)); 700142215Sglebius m_tag_prepend(m, mtag); 701142215Sglebius 702142215Sglebius return (0); 703142215Sglebius} 704142215Sglebius 705228736Sglebius/* 706228736Sglebius * To avoid LORs and possible recursions this function shouldn't 707228736Sglebius * be called directly, but scheduled via taskqueue. 708228736Sglebius */ 709142559Sglebiusstatic void 710228736Sglebiuscarp_send_ad_all(void *ctx __unused, int pending __unused) 711142215Sglebius{ 712142911Sglebius struct carp_softc *sc; 713142215Sglebius 714142911Sglebius mtx_lock(&carp_mtx); 715228571Sglebius LIST_FOREACH(sc, &carp_list, sc_next) 716228736Sglebius if (sc->sc_state == MASTER) { 717228571Sglebius CARP_LOCK(sc); 718231201Sglebius CURVNET_SET(sc->sc_carpdev->if_vnet); 719142914Sglebius carp_send_ad_locked(sc); 720231201Sglebius CURVNET_RESTORE(); 721228571Sglebius CARP_UNLOCK(sc); 722228571Sglebius } 723142911Sglebius mtx_unlock(&carp_mtx); 724142215Sglebius} 725142215Sglebius 726231201Sglebius/* Send a periodic advertisement, executed in callout context. */ 727142559Sglebiusstatic void 728142215Sglebiuscarp_send_ad(void *v) 729142215Sglebius{ 730142914Sglebius struct carp_softc *sc = v; 731142914Sglebius 732228571Sglebius CARP_LOCK_ASSERT(sc); 733231201Sglebius CURVNET_SET(sc->sc_carpdev->if_vnet); 734142914Sglebius carp_send_ad_locked(sc); 735231201Sglebius CURVNET_RESTORE(); 736228571Sglebius CARP_UNLOCK(sc); 737142914Sglebius} 738142914Sglebius 739142914Sglebiusstatic void 740142914Sglebiuscarp_send_ad_locked(struct carp_softc *sc) 741142914Sglebius{ 742142215Sglebius struct carp_header ch; 743142215Sglebius struct timeval tv; 744228571Sglebius struct sockaddr sa; 745228571Sglebius struct ifaddr *ifa; 746142215Sglebius struct carp_header *ch_ptr; 747142215Sglebius struct mbuf *m; 748228571Sglebius int len, advskew; 749142215Sglebius 750228571Sglebius CARP_LOCK_ASSERT(sc); 751142914Sglebius 752228736Sglebius advskew = DEMOTE_ADVSKEW(sc); 753228571Sglebius tv.tv_sec = sc->sc_advbase; 754228571Sglebius tv.tv_usec = advskew * 1000000 / 256; 755142215Sglebius 756142215Sglebius ch.carp_version = CARP_VERSION; 757142215Sglebius ch.carp_type = CARP_ADVERTISEMENT; 758142215Sglebius ch.carp_vhid = sc->sc_vhid; 759228571Sglebius ch.carp_advbase = sc->sc_advbase; 760142215Sglebius ch.carp_advskew = advskew; 761142215Sglebius ch.carp_authlen = 7; /* XXX DEFINE */ 762142215Sglebius ch.carp_pad1 = 0; /* must be zero */ 763142215Sglebius ch.carp_cksum = 0; 764142215Sglebius 765228571Sglebius /* XXXGL: OpenBSD picks first ifaddr with needed family. */ 766228571Sglebius 767142215Sglebius#ifdef INET 768228571Sglebius if (sc->sc_naddrs) { 769142215Sglebius struct ip *ip; 770142215Sglebius 771228571Sglebius MGETHDR(m, M_NOWAIT, MT_HEADER); 772142215Sglebius if (m == NULL) { 773190968Srwatson CARPSTATS_INC(carps_onomem); 774142215Sglebius /* XXX maybe less ? */ 775228571Sglebius callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), 776228571Sglebius carp_send_ad, sc); 777142215Sglebius return; 778142215Sglebius } 779142215Sglebius len = sizeof(*ip) + sizeof(ch); 780142215Sglebius m->m_pkthdr.len = len; 781142215Sglebius m->m_pkthdr.rcvif = NULL; 782142215Sglebius m->m_len = len; 783142215Sglebius MH_ALIGN(m, m->m_len); 784142215Sglebius m->m_flags |= M_MCAST; 785142215Sglebius ip = mtod(m, struct ip *); 786142215Sglebius ip->ip_v = IPVERSION; 787142215Sglebius ip->ip_hl = sizeof(*ip) >> 2; 788142215Sglebius ip->ip_tos = IPTOS_LOWDELAY; 789142215Sglebius ip->ip_len = len; 790142215Sglebius ip->ip_id = ip_newid(); 791142215Sglebius ip->ip_off = IP_DF; 792142215Sglebius ip->ip_ttl = CARP_DFLTTL; 793142215Sglebius ip->ip_p = IPPROTO_CARP; 794142215Sglebius ip->ip_sum = 0; 795228571Sglebius 796228571Sglebius bzero(&sa, sizeof(sa)); 797228571Sglebius sa.sa_family = AF_INET; 798228571Sglebius ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev); 799228571Sglebius if (ifa != NULL) { 800228571Sglebius ip->ip_src.s_addr = 801228571Sglebius ifatoia(ifa)->ia_addr.sin_addr.s_addr; 802228571Sglebius ifa_free(ifa); 803228571Sglebius } else 804228571Sglebius ip->ip_src.s_addr = 0; 805142215Sglebius ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP); 806142215Sglebius 807142215Sglebius ch_ptr = (struct carp_header *)(&ip[1]); 808142215Sglebius bcopy(&ch, ch_ptr, sizeof(ch)); 809142215Sglebius if (carp_prepare_ad(m, sc, ch_ptr)) 810142215Sglebius return; 811142215Sglebius 812142215Sglebius m->m_data += sizeof(*ip); 813142215Sglebius ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip)); 814142215Sglebius m->m_data -= sizeof(*ip); 815142215Sglebius 816190968Srwatson CARPSTATS_INC(carps_opackets); 817142215Sglebius 818228571Sglebius if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, 819228571Sglebius &sc->sc_carpdev->if_carp->cif_imo, NULL)) { 820142215Sglebius if (sc->sc_sendad_errors < INT_MAX) 821142215Sglebius sc->sc_sendad_errors++; 822228736Sglebius if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) 823228736Sglebius carp_demote_adj(carp_senderr_adj, "send error"); 824142215Sglebius sc->sc_sendad_success = 0; 825142215Sglebius } else { 826142215Sglebius if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { 827142215Sglebius if (++sc->sc_sendad_success >= 828142215Sglebius CARP_SENDAD_MIN_SUCCESS) { 829228736Sglebius carp_demote_adj(-carp_senderr_adj, 830228736Sglebius "send ok"); 831142215Sglebius sc->sc_sendad_errors = 0; 832142215Sglebius } 833142215Sglebius } else 834142215Sglebius sc->sc_sendad_errors = 0; 835142215Sglebius } 836142215Sglebius } 837142215Sglebius#endif /* INET */ 838142215Sglebius#ifdef INET6 839228571Sglebius if (sc->sc_naddrs6) { 840142215Sglebius struct ip6_hdr *ip6; 841142215Sglebius 842228571Sglebius MGETHDR(m, M_NOWAIT, MT_HEADER); 843142215Sglebius if (m == NULL) { 844190968Srwatson CARPSTATS_INC(carps_onomem); 845142215Sglebius /* XXX maybe less ? */ 846228571Sglebius callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), 847228571Sglebius carp_send_ad, sc); 848142215Sglebius return; 849142215Sglebius } 850142215Sglebius len = sizeof(*ip6) + sizeof(ch); 851142215Sglebius m->m_pkthdr.len = len; 852142215Sglebius m->m_pkthdr.rcvif = NULL; 853142215Sglebius m->m_len = len; 854142215Sglebius MH_ALIGN(m, m->m_len); 855142215Sglebius m->m_flags |= M_MCAST; 856142215Sglebius ip6 = mtod(m, struct ip6_hdr *); 857142215Sglebius bzero(ip6, sizeof(*ip6)); 858142215Sglebius ip6->ip6_vfc |= IPV6_VERSION; 859142215Sglebius ip6->ip6_hlim = CARP_DFLTTL; 860142215Sglebius ip6->ip6_nxt = IPPROTO_CARP; 861228571Sglebius bzero(&sa, sizeof(sa)); 862142215Sglebius 863228571Sglebius /* set the source address */ 864228571Sglebius sa.sa_family = AF_INET6; 865228571Sglebius ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev); 866228571Sglebius if (ifa != NULL) { 867228571Sglebius bcopy(IFA_IN6(ifa), &ip6->ip6_src, 868228571Sglebius sizeof(struct in6_addr)); 869228571Sglebius ifa_free(ifa); 870228571Sglebius } else 871228571Sglebius /* This should never happen with IPv6. */ 872228571Sglebius bzero(&ip6->ip6_src, sizeof(struct in6_addr)); 873228571Sglebius 874228571Sglebius /* Set the multicast destination. */ 875163069Sbz ip6->ip6_dst.s6_addr16[0] = htons(0xff02); 876142215Sglebius ip6->ip6_dst.s6_addr8[15] = 0x12; 877163069Sbz if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) { 878163069Sbz m_freem(m); 879200026Sglebius CARP_DEBUG("%s: in6_setscope failed\n", __func__); 880163069Sbz return; 881163069Sbz } 882142215Sglebius 883142215Sglebius ch_ptr = (struct carp_header *)(&ip6[1]); 884142215Sglebius bcopy(&ch, ch_ptr, sizeof(ch)); 885142215Sglebius if (carp_prepare_ad(m, sc, ch_ptr)) 886142215Sglebius return; 887142215Sglebius 888142215Sglebius m->m_data += sizeof(*ip6); 889142215Sglebius ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6)); 890142215Sglebius m->m_data -= sizeof(*ip6); 891142215Sglebius 892190968Srwatson CARPSTATS_INC(carps_opackets6); 893142215Sglebius 894228571Sglebius if (ip6_output(m, NULL, NULL, 0, 895228571Sglebius &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL)) { 896142215Sglebius if (sc->sc_sendad_errors < INT_MAX) 897142215Sglebius sc->sc_sendad_errors++; 898228736Sglebius if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) 899228736Sglebius carp_demote_adj(carp_senderr_adj, 900228736Sglebius "send6 error"); 901142215Sglebius sc->sc_sendad_success = 0; 902142215Sglebius } else { 903142215Sglebius if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { 904142215Sglebius if (++sc->sc_sendad_success >= 905142215Sglebius CARP_SENDAD_MIN_SUCCESS) { 906228736Sglebius carp_demote_adj(-carp_senderr_adj, 907228736Sglebius "send6 ok"); 908142215Sglebius sc->sc_sendad_errors = 0; 909142215Sglebius } 910142215Sglebius } else 911142215Sglebius sc->sc_sendad_errors = 0; 912142215Sglebius } 913142215Sglebius } 914142215Sglebius#endif /* INET6 */ 915142215Sglebius 916228571Sglebius callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc); 917228571Sglebius} 918142215Sglebius 919228571Sglebiusstatic void 920228571Sglebiuscarp_addroute(struct carp_softc *sc) 921228571Sglebius{ 922228571Sglebius struct ifaddr *ifa; 923228571Sglebius 924228571Sglebius CARP_FOREACH_IFA(sc, ifa) 925230863Sglebius carp_ifa_addroute(ifa); 926230863Sglebius} 927230863Sglebius 928230863Sglebiusstatic void 929230863Sglebiuscarp_ifa_addroute(struct ifaddr *ifa) 930230863Sglebius{ 931230863Sglebius 932230863Sglebius switch (ifa->ifa_addr->sa_family) { 933228571Sglebius#ifdef INET 934230863Sglebius case AF_INET: 935230863Sglebius in_addprefix(ifatoia(ifa), RTF_UP); 936230863Sglebius ifa_add_loopback_route(ifa, 937230863Sglebius (struct sockaddr *)&ifatoia(ifa)->ia_addr); 938230863Sglebius break; 939228571Sglebius#endif 940228571Sglebius#ifdef INET6 941230863Sglebius case AF_INET6: 942230863Sglebius ifa_add_loopback_route(ifa, 943230863Sglebius (struct sockaddr *)&ifatoia6(ifa)->ia_addr); 944230863Sglebius in6_ifaddloop(ifa); 945230863Sglebius break; 946228571Sglebius#endif 947230863Sglebius } 948142215Sglebius} 949142215Sglebius 950228571Sglebiusstatic void 951228571Sglebiuscarp_delroute(struct carp_softc *sc) 952228571Sglebius{ 953228571Sglebius struct ifaddr *ifa; 954228571Sglebius 955228571Sglebius CARP_FOREACH_IFA(sc, ifa) 956230863Sglebius carp_ifa_delroute(ifa); 957230863Sglebius} 958230863Sglebius 959230863Sglebiusstatic void 960230863Sglebiuscarp_ifa_delroute(struct ifaddr *ifa) 961230863Sglebius{ 962230863Sglebius 963230863Sglebius switch (ifa->ifa_addr->sa_family) { 964221130Sbz#ifdef INET 965230863Sglebius case AF_INET: 966230863Sglebius ifa_del_loopback_route(ifa, 967230863Sglebius (struct sockaddr *)&ifatoia(ifa)->ia_addr); 968230863Sglebius in_scrubprefix(ifatoia(ifa), LLE_STATIC); 969230863Sglebius break; 970228571Sglebius#endif 971228571Sglebius#ifdef INET6 972230863Sglebius case AF_INET6: 973230863Sglebius ifa_del_loopback_route(ifa, 974230863Sglebius (struct sockaddr *)&ifatoia6(ifa)->ia_addr); 975230863Sglebius in6_ifremloop(ifa); 976230863Sglebius break; 977228571Sglebius#endif 978230863Sglebius } 979228571Sglebius} 980228571Sglebius 981228571Sglebius#ifdef INET 982142215Sglebius/* 983142215Sglebius * Broadcast a gratuitous ARP request containing 984142215Sglebius * the virtual router MAC address for each IP address 985142215Sglebius * associated with the virtual router. 986142215Sglebius */ 987142559Sglebiusstatic void 988142215Sglebiuscarp_send_arp(struct carp_softc *sc) 989142215Sglebius{ 990142215Sglebius struct ifaddr *ifa; 991142215Sglebius 992228571Sglebius CARP_FOREACH_IFA(sc, ifa) 993228571Sglebius if (ifa->ifa_addr->sa_family == AF_INET) 994228571Sglebius arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr)); 995228571Sglebius} 996142215Sglebius 997228571Sglebiusint 998228571Sglebiuscarp_iamatch(struct ifaddr *ifa, uint8_t **enaddr) 999228571Sglebius{ 1000228571Sglebius struct carp_softc *sc = ifa->ifa_carp; 1001142215Sglebius 1002228571Sglebius if (sc->sc_state == MASTER) { 1003228571Sglebius *enaddr = LLADDR(&sc->sc_addr); 1004228571Sglebius return (1); 1005228571Sglebius } 1006142215Sglebius 1007228571Sglebius return (0); 1008142215Sglebius} 1009221130Sbz#endif 1010142215Sglebius 1011142215Sglebius#ifdef INET6 1012142559Sglebiusstatic void 1013142215Sglebiuscarp_send_na(struct carp_softc *sc) 1014142215Sglebius{ 1015228571Sglebius static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 1016142215Sglebius struct ifaddr *ifa; 1017142215Sglebius struct in6_addr *in6; 1018142215Sglebius 1019228571Sglebius CARP_FOREACH_IFA(sc, ifa) { 1020142215Sglebius if (ifa->ifa_addr->sa_family != AF_INET6) 1021142215Sglebius continue; 1022142215Sglebius 1023228571Sglebius in6 = IFA_IN6(ifa); 1024142564Sglebius nd6_na_output(sc->sc_carpdev, &mcast, in6, 1025142215Sglebius ND_NA_FLAG_OVERRIDE, 1, NULL); 1026142215Sglebius DELAY(1000); /* XXX */ 1027142215Sglebius } 1028142215Sglebius} 1029142215Sglebius 1030142641Smlaierstruct ifaddr * 1031211157Swillcarp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr) 1032142215Sglebius{ 1033142215Sglebius struct ifaddr *ifa; 1034142215Sglebius 1035229621Sjhb IF_ADDR_RLOCK(ifp); 1036228571Sglebius IFNET_FOREACH_IFA(ifp, ifa) 1037228571Sglebius if (ifa->ifa_addr->sa_family == AF_INET6 && 1038228571Sglebius ifa->ifa_carp->sc_state == MASTER && 1039228571Sglebius IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) { 1040228571Sglebius ifa_ref(ifa); 1041229621Sjhb IF_ADDR_RUNLOCK(ifp); 1042228571Sglebius return (ifa); 1043142215Sglebius } 1044229621Sjhb IF_ADDR_RUNLOCK(ifp); 1045228571Sglebius 1046142215Sglebius return (NULL); 1047142215Sglebius} 1048142215Sglebius 1049211157Swillcaddr_t 1050211157Swillcarp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr) 1051142215Sglebius{ 1052142215Sglebius struct ifaddr *ifa; 1053142215Sglebius 1054229621Sjhb IF_ADDR_RLOCK(ifp); 1055228571Sglebius IFNET_FOREACH_IFA(ifp, ifa) 1056228571Sglebius if (ifa->ifa_addr->sa_family == AF_INET6 && 1057228571Sglebius IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) { 1058228571Sglebius struct carp_softc *sc = ifa->ifa_carp; 1059228571Sglebius struct m_tag *mtag; 1060142215Sglebius 1061229621Sjhb IF_ADDR_RUNLOCK(ifp); 1062228571Sglebius 1063228571Sglebius mtag = m_tag_get(PACKET_TAG_CARP, 1064236297Sglebius sizeof(struct carp_softc *), M_NOWAIT); 1065228571Sglebius if (mtag == NULL) 1066228571Sglebius /* Better a bit than nothing. */ 1067228571Sglebius return (LLADDR(&sc->sc_addr)); 1068228571Sglebius 1069236297Sglebius bcopy(&sc, (caddr_t)(mtag + 1), 1070236297Sglebius sizeof(struct carp_softc *)); 1071228571Sglebius m_tag_prepend(m, mtag); 1072228571Sglebius 1073228571Sglebius return (LLADDR(&sc->sc_addr)); 1074142215Sglebius } 1075229621Sjhb IF_ADDR_RUNLOCK(ifp); 1076142215Sglebius 1077142215Sglebius return (NULL); 1078142215Sglebius} 1079228571Sglebius#endif /* INET6 */ 1080142215Sglebius 1081228571Sglebiusint 1082211157Swillcarp_forus(struct ifnet *ifp, u_char *dhost) 1083142215Sglebius{ 1084228571Sglebius struct carp_softc *sc; 1085228571Sglebius uint8_t *ena = dhost; 1086142215Sglebius 1087142215Sglebius if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1) 1088228571Sglebius return (0); 1089142215Sglebius 1090228571Sglebius CIF_LOCK(ifp->if_carp); 1091228571Sglebius IFNET_FOREACH_CARP(ifp, sc) { 1092228571Sglebius CARP_LOCK(sc); 1093228571Sglebius if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr), 1094228571Sglebius ETHER_ADDR_LEN)) { 1095228571Sglebius CARP_UNLOCK(sc); 1096228571Sglebius CIF_UNLOCK(ifp->if_carp); 1097228571Sglebius return (1); 1098142215Sglebius } 1099228571Sglebius CARP_UNLOCK(sc); 1100228571Sglebius } 1101228571Sglebius CIF_UNLOCK(ifp->if_carp); 1102142215Sglebius 1103228571Sglebius return (0); 1104142215Sglebius} 1105142215Sglebius 1106231201Sglebius/* Master down timeout event, executed in callout context. */ 1107142559Sglebiusstatic void 1108142215Sglebiuscarp_master_down(void *v) 1109142215Sglebius{ 1110142215Sglebius struct carp_softc *sc = v; 1111142215Sglebius 1112228571Sglebius CARP_LOCK_ASSERT(sc); 1113228571Sglebius 1114231201Sglebius CURVNET_SET(sc->sc_carpdev->if_vnet); 1115228571Sglebius if (sc->sc_state == BACKUP) { 1116228812Sglebius CARP_LOG("VHID %u@%s: BACKUP -> MASTER (master down)\n", 1117228571Sglebius sc->sc_vhid, 1118228571Sglebius sc->sc_carpdev->if_xname); 1119228571Sglebius carp_master_down_locked(sc); 1120228571Sglebius } 1121231201Sglebius CURVNET_RESTORE(); 1122228571Sglebius 1123228571Sglebius CARP_UNLOCK(sc); 1124142914Sglebius} 1125142914Sglebius 1126142914Sglebiusstatic void 1127142914Sglebiuscarp_master_down_locked(struct carp_softc *sc) 1128142914Sglebius{ 1129142914Sglebius 1130228571Sglebius CARP_LOCK_ASSERT(sc); 1131228571Sglebius 1132142215Sglebius switch (sc->sc_state) { 1133142215Sglebius case BACKUP: 1134142215Sglebius carp_set_state(sc, MASTER); 1135142914Sglebius carp_send_ad_locked(sc); 1136221130Sbz#ifdef INET 1137142215Sglebius carp_send_arp(sc); 1138221130Sbz#endif 1139142215Sglebius#ifdef INET6 1140142215Sglebius carp_send_na(sc); 1141228571Sglebius#endif 1142142215Sglebius carp_setrun(sc, 0); 1143228571Sglebius carp_addroute(sc); 1144142215Sglebius break; 1145228571Sglebius case INIT: 1146228571Sglebius case MASTER: 1147228571Sglebius#ifdef INVARIANTS 1148228571Sglebius panic("carp: VHID %u@%s: master_down event in %s state\n", 1149228571Sglebius sc->sc_vhid, 1150228571Sglebius sc->sc_carpdev->if_xname, 1151228571Sglebius sc->sc_state ? "MASTER" : "INIT"); 1152228571Sglebius#endif 1153228571Sglebius break; 1154142215Sglebius } 1155142215Sglebius} 1156142215Sglebius 1157142215Sglebius/* 1158142215Sglebius * When in backup state, af indicates whether to reset the master down timer 1159142215Sglebius * for v4 or v6. If it's set to zero, reset the ones which are already pending. 1160142215Sglebius */ 1161142559Sglebiusstatic void 1162142215Sglebiuscarp_setrun(struct carp_softc *sc, sa_family_t af) 1163142215Sglebius{ 1164142215Sglebius struct timeval tv; 1165142215Sglebius 1166228571Sglebius CARP_LOCK_ASSERT(sc); 1167142914Sglebius 1168228571Sglebius if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 || 1169228571Sglebius sc->sc_carpdev->if_link_state != LINK_STATE_UP || 1170228571Sglebius (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0)) 1171142215Sglebius return; 1172142215Sglebius 1173142215Sglebius switch (sc->sc_state) { 1174142215Sglebius case INIT: 1175228571Sglebius CARP_LOG("VHID %u@%s: INIT -> BACKUP\n", 1176228571Sglebius sc->sc_vhid, 1177228571Sglebius sc->sc_carpdev->if_xname); 1178226367Sglebius carp_set_state(sc, BACKUP); 1179226367Sglebius carp_setrun(sc, 0); 1180142215Sglebius break; 1181142215Sglebius case BACKUP: 1182142215Sglebius callout_stop(&sc->sc_ad_tmo); 1183142215Sglebius tv.tv_sec = 3 * sc->sc_advbase; 1184142215Sglebius tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1185142215Sglebius switch (af) { 1186142215Sglebius#ifdef INET 1187142215Sglebius case AF_INET: 1188142215Sglebius callout_reset(&sc->sc_md_tmo, tvtohz(&tv), 1189142215Sglebius carp_master_down, sc); 1190142215Sglebius break; 1191228571Sglebius#endif 1192142215Sglebius#ifdef INET6 1193142215Sglebius case AF_INET6: 1194142215Sglebius callout_reset(&sc->sc_md6_tmo, tvtohz(&tv), 1195142215Sglebius carp_master_down, sc); 1196142215Sglebius break; 1197228571Sglebius#endif 1198142215Sglebius default: 1199228571Sglebius#ifdef INET 1200142215Sglebius if (sc->sc_naddrs) 1201142215Sglebius callout_reset(&sc->sc_md_tmo, tvtohz(&tv), 1202142215Sglebius carp_master_down, sc); 1203228571Sglebius#endif 1204228571Sglebius#ifdef INET6 1205142215Sglebius if (sc->sc_naddrs6) 1206142215Sglebius callout_reset(&sc->sc_md6_tmo, tvtohz(&tv), 1207142215Sglebius carp_master_down, sc); 1208228571Sglebius#endif 1209142215Sglebius break; 1210142215Sglebius } 1211142215Sglebius break; 1212142215Sglebius case MASTER: 1213142215Sglebius tv.tv_sec = sc->sc_advbase; 1214142215Sglebius tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1215142215Sglebius callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), 1216142215Sglebius carp_send_ad, sc); 1217142215Sglebius break; 1218142215Sglebius } 1219142215Sglebius} 1220142215Sglebius 1221228571Sglebius/* 1222228571Sglebius * Setup multicast structures. 1223228571Sglebius */ 1224228571Sglebiusstatic int 1225234130Sglebiuscarp_multicast_setup(struct carp_if *cif, sa_family_t sa) 1226156947Sglebius{ 1227234130Sglebius struct ifnet *ifp = cif->cif_ifp; 1228228571Sglebius int error = 0; 1229166226Sglebius 1230234130Sglebius CIF_LOCK_ASSERT(cif); 1231234130Sglebius 1232228571Sglebius switch (sa) { 1233228571Sglebius#ifdef INET 1234228571Sglebius case AF_INET: 1235228571Sglebius { 1236228571Sglebius struct ip_moptions *imo = &cif->cif_imo; 1237228571Sglebius struct in_addr addr; 1238228571Sglebius 1239228571Sglebius if (imo->imo_membership) 1240228571Sglebius return (0); 1241228571Sglebius 1242228571Sglebius imo->imo_membership = (struct in_multi **)malloc( 1243228571Sglebius (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP, 1244234130Sglebius M_NOWAIT); 1245234130Sglebius if (imo->imo_membership == NULL) 1246234130Sglebius return (ENOMEM); 1247228571Sglebius imo->imo_mfilters = NULL; 1248228571Sglebius imo->imo_max_memberships = IP_MIN_MEMBERSHIPS; 1249228571Sglebius imo->imo_multicast_vif = -1; 1250228571Sglebius 1251228571Sglebius addr.s_addr = htonl(INADDR_CARP_GROUP); 1252228571Sglebius if ((error = in_joingroup(ifp, &addr, NULL, 1253228571Sglebius &imo->imo_membership[0])) != 0) { 1254228571Sglebius free(imo->imo_membership, M_CARP); 1255228571Sglebius break; 1256156947Sglebius } 1257228571Sglebius imo->imo_num_memberships++; 1258228571Sglebius imo->imo_multicast_ifp = ifp; 1259228571Sglebius imo->imo_multicast_ttl = CARP_DFLTTL; 1260228571Sglebius imo->imo_multicast_loop = 0; 1261228571Sglebius break; 1262228571Sglebius } 1263228571Sglebius#endif 1264228571Sglebius#ifdef INET6 1265228571Sglebius case AF_INET6: 1266228571Sglebius { 1267228571Sglebius struct ip6_moptions *im6o = &cif->cif_im6o; 1268228571Sglebius struct in6_addr in6; 1269228571Sglebius struct in6_multi *in6m; 1270228571Sglebius 1271228571Sglebius if (im6o->im6o_membership) 1272228571Sglebius return (0); 1273228571Sglebius 1274228571Sglebius im6o->im6o_membership = (struct in6_multi **)malloc( 1275228571Sglebius (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP, 1276234130Sglebius M_ZERO | M_NOWAIT); 1277234130Sglebius if (im6o->im6o_membership == NULL) 1278234130Sglebius return (ENOMEM); 1279228571Sglebius im6o->im6o_mfilters = NULL; 1280228571Sglebius im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS; 1281228571Sglebius im6o->im6o_multicast_hlim = CARP_DFLTTL; 1282228571Sglebius im6o->im6o_multicast_ifp = ifp; 1283228571Sglebius 1284228571Sglebius /* Join IPv6 CARP multicast group. */ 1285228571Sglebius bzero(&in6, sizeof(in6)); 1286228571Sglebius in6.s6_addr16[0] = htons(0xff02); 1287228571Sglebius in6.s6_addr8[15] = 0x12; 1288228571Sglebius if ((error = in6_setscope(&in6, ifp, NULL)) != 0) { 1289228571Sglebius free(im6o->im6o_membership, M_CARP); 1290228571Sglebius break; 1291228571Sglebius } 1292228571Sglebius in6m = NULL; 1293228571Sglebius if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) { 1294228571Sglebius free(im6o->im6o_membership, M_CARP); 1295228571Sglebius break; 1296228571Sglebius } 1297228571Sglebius im6o->im6o_membership[0] = in6m; 1298228571Sglebius im6o->im6o_num_memberships++; 1299228571Sglebius 1300228571Sglebius /* Join solicited multicast address. */ 1301228571Sglebius bzero(&in6, sizeof(in6)); 1302228571Sglebius in6.s6_addr16[0] = htons(0xff02); 1303228571Sglebius in6.s6_addr32[1] = 0; 1304228571Sglebius in6.s6_addr32[2] = htonl(1); 1305228571Sglebius in6.s6_addr32[3] = 0; 1306228571Sglebius in6.s6_addr8[12] = 0xff; 1307228571Sglebius if ((error = in6_setscope(&in6, ifp, NULL)) != 0) { 1308228571Sglebius in6_mc_leave(im6o->im6o_membership[0], NULL); 1309228571Sglebius free(im6o->im6o_membership, M_CARP); 1310228571Sglebius break; 1311228571Sglebius } 1312228571Sglebius in6m = NULL; 1313228571Sglebius if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) { 1314228571Sglebius in6_mc_leave(im6o->im6o_membership[0], NULL); 1315228571Sglebius free(im6o->im6o_membership, M_CARP); 1316228571Sglebius break; 1317228571Sglebius } 1318228571Sglebius im6o->im6o_membership[1] = in6m; 1319228571Sglebius im6o->im6o_num_memberships++; 1320228571Sglebius break; 1321228571Sglebius } 1322228571Sglebius#endif 1323156947Sglebius } 1324228571Sglebius 1325228571Sglebius return (error); 1326166423Sglebius} 1327156947Sglebius 1328228571Sglebius/* 1329228571Sglebius * Free multicast structures. 1330228571Sglebius */ 1331166423Sglebiusstatic void 1332234130Sglebiuscarp_multicast_cleanup(struct carp_if *cif, sa_family_t sa) 1333166423Sglebius{ 1334166423Sglebius 1335234130Sglebius CIF_LOCK_ASSERT(cif); 1336228571Sglebius switch (sa) { 1337228571Sglebius#ifdef INET 1338228571Sglebius case AF_INET: 1339234130Sglebius if (cif->cif_naddrs == 0) { 1340228571Sglebius struct ip_moptions *imo = &cif->cif_imo; 1341228571Sglebius 1342228571Sglebius in_leavegroup(imo->imo_membership[0], NULL); 1343228571Sglebius KASSERT(imo->imo_mfilters == NULL, 1344228571Sglebius ("%s: imo_mfilters != NULL", __func__)); 1345228571Sglebius free(imo->imo_membership, M_CARP); 1346228571Sglebius imo->imo_membership = NULL; 1347228571Sglebius 1348191672Sbms } 1349228571Sglebius break; 1350228571Sglebius#endif 1351228571Sglebius#ifdef INET6 1352228571Sglebius case AF_INET6: 1353234130Sglebius if (cif->cif_naddrs6 == 0) { 1354228571Sglebius struct ip6_moptions *im6o = &cif->cif_im6o; 1355228571Sglebius 1356228571Sglebius in6_mc_leave(im6o->im6o_membership[0], NULL); 1357228571Sglebius in6_mc_leave(im6o->im6o_membership[1], NULL); 1358228571Sglebius KASSERT(im6o->im6o_mfilters == NULL, 1359228571Sglebius ("%s: im6o_mfilters != NULL", __func__)); 1360228571Sglebius free(im6o->im6o_membership, M_CARP); 1361228571Sglebius im6o->im6o_membership = NULL; 1362228571Sglebius } 1363228571Sglebius break; 1364228571Sglebius#endif 1365156947Sglebius } 1366166423Sglebius} 1367156947Sglebius 1368228571Sglebiusint 1369228571Sglebiuscarp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa) 1370142215Sglebius{ 1371228571Sglebius struct m_tag *mtag; 1372228571Sglebius struct carp_softc *sc; 1373142215Sglebius 1374228571Sglebius if (!sa) 1375142215Sglebius return (0); 1376142215Sglebius 1377228571Sglebius switch (sa->sa_family) { 1378228571Sglebius#ifdef INET 1379228571Sglebius case AF_INET: 1380228571Sglebius break; 1381228571Sglebius#endif 1382228571Sglebius#ifdef INET6 1383228571Sglebius case AF_INET6: 1384228571Sglebius break; 1385228571Sglebius#endif 1386228571Sglebius default: 1387228571Sglebius return (0); 1388142215Sglebius } 1389142215Sglebius 1390228571Sglebius mtag = m_tag_find(m, PACKET_TAG_CARP, NULL); 1391228571Sglebius if (mtag == NULL) 1392228571Sglebius return (0); 1393142215Sglebius 1394228571Sglebius bcopy(mtag + 1, &sc, sizeof(struct carp_softc *)); 1395194951Srwatson 1396228571Sglebius /* Set the source MAC address to the Virtual Router MAC Address. */ 1397228571Sglebius switch (ifp->if_type) { 1398228571Sglebius case IFT_ETHER: 1399234084Sglebius case IFT_BRIDGE: 1400228571Sglebius case IFT_L2VLAN: { 1401228571Sglebius struct ether_header *eh; 1402142215Sglebius 1403228571Sglebius eh = mtod(m, struct ether_header *); 1404228571Sglebius eh->ether_shost[0] = 0; 1405228571Sglebius eh->ether_shost[1] = 0; 1406228571Sglebius eh->ether_shost[2] = 0x5e; 1407228571Sglebius eh->ether_shost[3] = 0; 1408228571Sglebius eh->ether_shost[4] = 1; 1409228571Sglebius eh->ether_shost[5] = sc->sc_vhid; 1410228571Sglebius } 1411228571Sglebius break; 1412228571Sglebius case IFT_FDDI: { 1413228571Sglebius struct fddi_header *fh; 1414142215Sglebius 1415228571Sglebius fh = mtod(m, struct fddi_header *); 1416228571Sglebius fh->fddi_shost[0] = 0; 1417228571Sglebius fh->fddi_shost[1] = 0; 1418228571Sglebius fh->fddi_shost[2] = 0x5e; 1419228571Sglebius fh->fddi_shost[3] = 0; 1420228571Sglebius fh->fddi_shost[4] = 1; 1421228571Sglebius fh->fddi_shost[5] = sc->sc_vhid; 1422194951Srwatson } 1423228571Sglebius break; 1424228571Sglebius case IFT_ISO88025: { 1425228571Sglebius struct iso88025_header *th; 1426228571Sglebius th = mtod(m, struct iso88025_header *); 1427228571Sglebius th->iso88025_shost[0] = 3; 1428228571Sglebius th->iso88025_shost[1] = 0; 1429228571Sglebius th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1); 1430228571Sglebius th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1); 1431228571Sglebius th->iso88025_shost[4] = 0; 1432228571Sglebius th->iso88025_shost[5] = 0; 1433228571Sglebius } 1434228571Sglebius break; 1435228571Sglebius default: 1436228571Sglebius printf("%s: carp is not supported for the %d interface type\n", 1437228571Sglebius ifp->if_xname, ifp->if_type); 1438228571Sglebius return (EOPNOTSUPP); 1439142215Sglebius } 1440142215Sglebius 1441228571Sglebius return (0); 1442228571Sglebius} 1443142215Sglebius 1444228571Sglebiusstatic struct carp_softc* 1445228571Sglebiuscarp_alloc(struct ifnet *ifp) 1446228571Sglebius{ 1447228571Sglebius struct carp_softc *sc; 1448228571Sglebius struct carp_if *cif; 1449142215Sglebius 1450228571Sglebius if ((cif = ifp->if_carp) == NULL) { 1451228571Sglebius cif = carp_alloc_if(ifp); 1452228571Sglebius if (cif == NULL) 1453228571Sglebius return (NULL); 1454142215Sglebius } 1455142215Sglebius 1456228571Sglebius sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO); 1457142215Sglebius 1458228571Sglebius sc->sc_advbase = CARP_DFLTINTV; 1459228571Sglebius sc->sc_vhid = -1; /* required setting */ 1460228571Sglebius sc->sc_init_counter = 1; 1461228571Sglebius sc->sc_state = INIT; 1462142215Sglebius 1463228571Sglebius sc->sc_ifasiz = sizeof(struct ifaddr *); 1464228571Sglebius sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO); 1465228571Sglebius sc->sc_carpdev = ifp; 1466142215Sglebius 1467228571Sglebius CARP_LOCK_INIT(sc); 1468228571Sglebius#ifdef INET 1469228571Sglebius callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED); 1470228571Sglebius#endif 1471228571Sglebius#ifdef INET6 1472228571Sglebius callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED); 1473228571Sglebius#endif 1474228571Sglebius callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED); 1475142215Sglebius 1476228571Sglebius CIF_LOCK(cif); 1477228571Sglebius TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list); 1478228571Sglebius CIF_UNLOCK(cif); 1479142215Sglebius 1480228571Sglebius mtx_lock(&carp_mtx); 1481228571Sglebius LIST_INSERT_HEAD(&carp_list, sc, sc_next); 1482228571Sglebius mtx_unlock(&carp_mtx); 1483142914Sglebius 1484228571Sglebius return (sc); 1485142215Sglebius} 1486142215Sglebius 1487142559Sglebiusstatic int 1488228571Sglebiuscarp_grow_ifas(struct carp_softc *sc) 1489142215Sglebius{ 1490228571Sglebius struct ifaddr **new; 1491142215Sglebius 1492228571Sglebius CARP_LOCK_ASSERT(sc); 1493142215Sglebius 1494228571Sglebius new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO); 1495228571Sglebius if (new == NULL) 1496228571Sglebius return (ENOMEM); 1497228571Sglebius bcopy(sc->sc_ifas, new, sc->sc_ifasiz); 1498228571Sglebius free(sc->sc_ifas, M_CARP); 1499228571Sglebius sc->sc_ifas = new; 1500228571Sglebius sc->sc_ifasiz *= 2; 1501142215Sglebius 1502228571Sglebius return (0); 1503142215Sglebius} 1504142215Sglebius 1505228571Sglebiusstatic void 1506228571Sglebiuscarp_destroy(struct carp_softc *sc) 1507142215Sglebius{ 1508228571Sglebius struct ifnet *ifp = sc->sc_carpdev; 1509228571Sglebius struct carp_if *cif = ifp->if_carp; 1510142215Sglebius 1511234130Sglebius CIF_LOCK_ASSERT(cif); 1512234130Sglebius 1513228571Sglebius TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list); 1514191672Sbms 1515228571Sglebius mtx_lock(&carp_mtx); 1516228571Sglebius LIST_REMOVE(sc, sc_next); 1517228571Sglebius mtx_unlock(&carp_mtx); 1518142215Sglebius 1519228571Sglebius CARP_LOCK(sc); 1520228736Sglebius if (sc->sc_suppress) 1521228736Sglebius carp_demote_adj(-carp_ifdown_adj, "vhid removed"); 1522228571Sglebius callout_drain(&sc->sc_ad_tmo); 1523228571Sglebius#ifdef INET 1524228571Sglebius callout_drain(&sc->sc_md_tmo); 1525228571Sglebius#endif 1526228571Sglebius#ifdef INET6 1527228571Sglebius callout_drain(&sc->sc_md6_tmo); 1528228571Sglebius#endif 1529228571Sglebius CARP_LOCK_DESTROY(sc); 1530142215Sglebius 1531228571Sglebius free(sc->sc_ifas, M_CARP); 1532228571Sglebius free(sc, M_CARP); 1533228571Sglebius} 1534142215Sglebius 1535228571Sglebiusstatic struct carp_if* 1536228571Sglebiuscarp_alloc_if(struct ifnet *ifp) 1537228571Sglebius{ 1538228571Sglebius struct carp_if *cif; 1539142215Sglebius 1540228571Sglebius cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO); 1541142215Sglebius 1542228571Sglebius if (ifpromisc(ifp, 1) != 0) 1543228571Sglebius goto cleanup; 1544191672Sbms 1545228571Sglebius CIF_LOCK_INIT(cif); 1546228571Sglebius cif->cif_ifp = ifp; 1547228571Sglebius TAILQ_INIT(&cif->cif_vrs); 1548142215Sglebius 1549229621Sjhb IF_ADDR_WLOCK(ifp); 1550228571Sglebius ifp->if_carp = cif; 1551228571Sglebius if_ref(ifp); 1552229621Sjhb IF_ADDR_WUNLOCK(ifp); 1553142215Sglebius 1554228571Sglebius return (cif); 1555142215Sglebius 1556228571Sglebiuscleanup: 1557228571Sglebius free(cif, M_CARP); 1558142215Sglebius 1559228571Sglebius return (NULL); 1560228571Sglebius} 1561142215Sglebius 1562228571Sglebiusstatic void 1563228571Sglebiuscarp_free_if(struct carp_if *cif) 1564228571Sglebius{ 1565228571Sglebius struct ifnet *ifp = cif->cif_ifp; 1566142215Sglebius 1567228571Sglebius CIF_LOCK_ASSERT(cif); 1568228571Sglebius KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty", 1569228571Sglebius __func__)); 1570142215Sglebius 1571229621Sjhb IF_ADDR_WLOCK(ifp); 1572228571Sglebius ifp->if_carp = NULL; 1573228571Sglebius if_rele(ifp); 1574229621Sjhb IF_ADDR_WUNLOCK(ifp); 1575142215Sglebius 1576228571Sglebius CIF_LOCK_DESTROY(cif); 1577142215Sglebius 1578228571Sglebius ifpromisc(ifp, 0); 1579142215Sglebius 1580228571Sglebius free(cif, M_CARP); 1581142215Sglebius} 1582142215Sglebius 1583228571Sglebiusstatic void 1584228571Sglebiuscarp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv) 1585142215Sglebius{ 1586142215Sglebius 1587228571Sglebius CARP_LOCK(sc); 1588228571Sglebius carpr->carpr_state = sc->sc_state; 1589228571Sglebius carpr->carpr_vhid = sc->sc_vhid; 1590228571Sglebius carpr->carpr_advbase = sc->sc_advbase; 1591228571Sglebius carpr->carpr_advskew = sc->sc_advskew; 1592228571Sglebius if (priv) 1593228571Sglebius bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key)); 1594228571Sglebius else 1595228571Sglebius bzero(carpr->carpr_key, sizeof(carpr->carpr_key)); 1596228571Sglebius CARP_UNLOCK(sc); 1597142215Sglebius} 1598142215Sglebius 1599228571Sglebiusint 1600228571Sglebiuscarp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td) 1601142215Sglebius{ 1602142215Sglebius struct carpreq carpr; 1603228571Sglebius struct ifnet *ifp; 1604228571Sglebius struct carp_softc *sc = NULL; 1605228571Sglebius int error = 0, locked = 0; 1606142215Sglebius 1607228571Sglebius if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr))) 1608228571Sglebius return (error); 1609142215Sglebius 1610228571Sglebius ifp = ifunit_ref(ifr->ifr_name); 1611228571Sglebius if (ifp == NULL) 1612228571Sglebius return (ENXIO); 1613228571Sglebius 1614228571Sglebius switch (ifp->if_type) { 1615228571Sglebius case IFT_ETHER: 1616228571Sglebius case IFT_L2VLAN: 1617234084Sglebius case IFT_BRIDGE: 1618228571Sglebius case IFT_FDDI: 1619228571Sglebius case IFT_ISO88025: 1620142215Sglebius break; 1621228571Sglebius default: 1622228571Sglebius error = EOPNOTSUPP; 1623228571Sglebius goto out; 1624228571Sglebius } 1625142215Sglebius 1626228571Sglebius if ((ifp->if_flags & IFF_MULTICAST) == 0) { 1627228571Sglebius error = EADDRNOTAVAIL; 1628228571Sglebius goto out; 1629228571Sglebius } 1630228571Sglebius 1631228571Sglebius switch (cmd) { 1632228571Sglebius case SIOCSVH: 1633228571Sglebius if ((error = priv_check(td, PRIV_NETINET_CARP))) 1634142215Sglebius break; 1635228571Sglebius if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID || 1636228571Sglebius carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) { 1637228571Sglebius error = EINVAL; 1638142215Sglebius break; 1639142215Sglebius } 1640142215Sglebius 1641228571Sglebius if (ifp->if_carp) { 1642228571Sglebius CIF_LOCK(ifp->if_carp); 1643228571Sglebius IFNET_FOREACH_CARP(ifp, sc) 1644228571Sglebius if (sc->sc_vhid == carpr.carpr_vhid) 1645228571Sglebius break; 1646228571Sglebius CIF_UNLOCK(ifp->if_carp); 1647142215Sglebius } 1648228571Sglebius if (sc == NULL) { 1649228571Sglebius sc = carp_alloc(ifp); 1650228571Sglebius if (sc == NULL) { 1651228571Sglebius error = EINVAL; /* XXX: ifpromisc failed */ 1652228571Sglebius break; 1653228571Sglebius } 1654142215Sglebius 1655228571Sglebius CARP_LOCK(sc); 1656228571Sglebius sc->sc_vhid = carpr.carpr_vhid; 1657228571Sglebius LLADDR(&sc->sc_addr)[0] = 0; 1658228571Sglebius LLADDR(&sc->sc_addr)[1] = 0; 1659228571Sglebius LLADDR(&sc->sc_addr)[2] = 0x5e; 1660228571Sglebius LLADDR(&sc->sc_addr)[3] = 0; 1661228571Sglebius LLADDR(&sc->sc_addr)[4] = 1; 1662228571Sglebius LLADDR(&sc->sc_addr)[5] = sc->sc_vhid; 1663228571Sglebius } else 1664228571Sglebius CARP_LOCK(sc); 1665228571Sglebius locked = 1; 1666228571Sglebius if (carpr.carpr_advbase > 0) { 1667228571Sglebius if (carpr.carpr_advbase > 255 || 1668228571Sglebius carpr.carpr_advbase < CARP_DFLTINTV) { 1669228571Sglebius error = EINVAL; 1670228571Sglebius break; 1671228571Sglebius } 1672228571Sglebius sc->sc_advbase = carpr.carpr_advbase; 1673142914Sglebius } 1674228571Sglebius if (carpr.carpr_advskew > 0) { 1675228571Sglebius if (carpr.carpr_advskew >= 255) { 1676228571Sglebius error = EINVAL; 1677228571Sglebius break; 1678228571Sglebius } 1679228571Sglebius sc->sc_advskew = carpr.carpr_advskew; 1680142215Sglebius } 1681228571Sglebius if (carpr.carpr_key[0] != '\0') { 1682228571Sglebius bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key)); 1683228571Sglebius carp_hmac_prepare(sc); 1684142914Sglebius } 1685228571Sglebius if (sc->sc_state != INIT && 1686228571Sglebius carpr.carpr_state != sc->sc_state) { 1687142215Sglebius switch (carpr.carpr_state) { 1688142215Sglebius case BACKUP: 1689142215Sglebius callout_stop(&sc->sc_ad_tmo); 1690142215Sglebius carp_set_state(sc, BACKUP); 1691142215Sglebius carp_setrun(sc, 0); 1692228571Sglebius carp_delroute(sc); 1693142215Sglebius break; 1694142215Sglebius case MASTER: 1695142914Sglebius carp_master_down_locked(sc); 1696142215Sglebius break; 1697142215Sglebius default: 1698142215Sglebius break; 1699142215Sglebius } 1700142215Sglebius } 1701228571Sglebius break; 1702228571Sglebius 1703228571Sglebius case SIOCGVH: 1704228571Sglebius { 1705228571Sglebius int priveleged; 1706228571Sglebius 1707228571Sglebius if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) { 1708228571Sglebius error = EINVAL; 1709228571Sglebius break; 1710228571Sglebius } 1711228571Sglebius if (carpr.carpr_count < 1) { 1712228571Sglebius error = EMSGSIZE; 1713228571Sglebius break; 1714228571Sglebius } 1715228571Sglebius if (ifp->if_carp == NULL) { 1716228571Sglebius error = ENOENT; 1717228571Sglebius break; 1718228571Sglebius } 1719228571Sglebius 1720228571Sglebius priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0); 1721228571Sglebius if (carpr.carpr_vhid != 0) { 1722228571Sglebius CIF_LOCK(ifp->if_carp); 1723228571Sglebius IFNET_FOREACH_CARP(ifp, sc) 1724228571Sglebius if (sc->sc_vhid == carpr.carpr_vhid) 1725170373Sglebius break; 1726228571Sglebius CIF_UNLOCK(ifp->if_carp); 1727228571Sglebius if (sc == NULL) { 1728228571Sglebius error = ENOENT; 1729142215Sglebius break; 1730142215Sglebius } 1731228571Sglebius carp_carprcp(&carpr, sc, priveleged); 1732228571Sglebius error = copyout(&carpr, ifr->ifr_data, sizeof(carpr)); 1733228571Sglebius } else { 1734228571Sglebius int i, count; 1735228571Sglebius 1736228571Sglebius count = 0; 1737228571Sglebius CIF_LOCK(ifp->if_carp); 1738228571Sglebius IFNET_FOREACH_CARP(ifp, sc) 1739228571Sglebius count++; 1740228571Sglebius 1741228571Sglebius if (count > carpr.carpr_count) { 1742228571Sglebius CIF_UNLOCK(ifp->if_carp); 1743228571Sglebius error = EMSGSIZE; 1744142215Sglebius break; 1745142215Sglebius } 1746228571Sglebius 1747228571Sglebius i = 0; 1748228571Sglebius IFNET_FOREACH_CARP(ifp, sc) { 1749228571Sglebius carp_carprcp(&carpr, sc, priveleged); 1750228571Sglebius carpr.carpr_count = count; 1751228571Sglebius error = copyout(&carpr, ifr->ifr_data + 1752228571Sglebius (i * sizeof(carpr)), sizeof(carpr)); 1753228571Sglebius if (error) { 1754228571Sglebius CIF_UNLOCK(ifp->if_carp); 1755228571Sglebius break; 1756228571Sglebius } 1757228571Sglebius i++; 1758228571Sglebius } 1759228571Sglebius CIF_UNLOCK(ifp->if_carp); 1760142215Sglebius } 1761142215Sglebius break; 1762228571Sglebius } 1763142215Sglebius default: 1764142215Sglebius error = EINVAL; 1765142215Sglebius } 1766142215Sglebius 1767228571Sglebiusout: 1768142914Sglebius if (locked) 1769228571Sglebius CARP_UNLOCK(sc); 1770228571Sglebius if_rele(ifp); 1771142914Sglebius 1772142215Sglebius return (error); 1773142215Sglebius} 1774142215Sglebius 1775142215Sglebiusstatic int 1776228571Sglebiuscarp_get_vhid(struct ifaddr *ifa) 1777142215Sglebius{ 1778147611Sdwmalone 1779228571Sglebius if (ifa == NULL || ifa->ifa_carp == NULL) 1780228571Sglebius return (0); 1781142215Sglebius 1782228571Sglebius return (ifa->ifa_carp->sc_vhid); 1783142215Sglebius} 1784142215Sglebius 1785142215Sglebiusint 1786228571Sglebiuscarp_attach(struct ifaddr *ifa, int vhid) 1787142215Sglebius{ 1788228571Sglebius struct ifnet *ifp = ifa->ifa_ifp; 1789234130Sglebius struct carp_if *cif = ifp->if_carp; 1790142215Sglebius struct carp_softc *sc; 1791228571Sglebius int index, error; 1792142215Sglebius 1793228571Sglebius if (ifp->if_carp == NULL) 1794228571Sglebius return (ENOPROTOOPT); 1795142215Sglebius 1796228571Sglebius switch (ifa->ifa_addr->sa_family) { 1797142215Sglebius#ifdef INET 1798142215Sglebius case AF_INET: 1799228571Sglebius#endif 1800142215Sglebius#ifdef INET6 1801142215Sglebius case AF_INET6: 1802228571Sglebius#endif 1803142215Sglebius break; 1804142215Sglebius default: 1805228571Sglebius return (EPROTOTYPE); 1806142215Sglebius } 1807142215Sglebius 1808234130Sglebius CIF_LOCK(cif); 1809228571Sglebius IFNET_FOREACH_CARP(ifp, sc) 1810228571Sglebius if (sc->sc_vhid == vhid) 1811228571Sglebius break; 1812234130Sglebius if (sc == NULL) { 1813234130Sglebius CIF_UNLOCK(cif); 1814228571Sglebius return (ENOENT); 1815234130Sglebius } 1816142215Sglebius 1817228571Sglebius if (ifa->ifa_carp) { 1818228571Sglebius if (ifa->ifa_carp->sc_vhid != vhid) 1819234130Sglebius carp_detach_locked(ifa); 1820234130Sglebius else { 1821234130Sglebius CIF_UNLOCK(cif); 1822228571Sglebius return (0); 1823234130Sglebius } 1824228571Sglebius } 1825142215Sglebius 1826234130Sglebius error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family); 1827234130Sglebius if (error) { 1828234130Sglebius CIF_FREE(cif); 1829228571Sglebius return (error); 1830234130Sglebius } 1831142215Sglebius 1832228571Sglebius CARP_LOCK(sc); 1833228571Sglebius index = sc->sc_naddrs + sc->sc_naddrs6 + 1; 1834228571Sglebius if (index > sc->sc_ifasiz / sizeof(struct ifaddr *)) 1835228571Sglebius if ((error = carp_grow_ifas(sc)) != 0) { 1836234130Sglebius carp_multicast_cleanup(cif, 1837228571Sglebius ifa->ifa_addr->sa_family); 1838228571Sglebius CARP_UNLOCK(sc); 1839234130Sglebius CIF_FREE(cif); 1840228571Sglebius return (error); 1841142215Sglebius } 1842142215Sglebius 1843228571Sglebius switch (ifa->ifa_addr->sa_family) { 1844228571Sglebius#ifdef INET 1845228571Sglebius case AF_INET: 1846234130Sglebius cif->cif_naddrs++; 1847228571Sglebius sc->sc_naddrs++; 1848142215Sglebius break; 1849228571Sglebius#endif 1850228571Sglebius#ifdef INET6 1851228571Sglebius case AF_INET6: 1852234130Sglebius cif->cif_naddrs6++; 1853228571Sglebius sc->sc_naddrs6++; 1854142215Sglebius break; 1855228571Sglebius#endif 1856142215Sglebius } 1857142215Sglebius 1858228571Sglebius ifa_ref(ifa); 1859228571Sglebius sc->sc_ifas[index - 1] = ifa; 1860228571Sglebius ifa->ifa_carp = sc; 1861228571Sglebius 1862228571Sglebius carp_hmac_prepare(sc); 1863228571Sglebius carp_sc_state(sc); 1864228571Sglebius 1865228571Sglebius CARP_UNLOCK(sc); 1866234130Sglebius CIF_UNLOCK(cif); 1867228571Sglebius 1868142215Sglebius return (0); 1869142215Sglebius} 1870142215Sglebius 1871228571Sglebiusvoid 1872228571Sglebiuscarp_detach(struct ifaddr *ifa) 1873142215Sglebius{ 1874234130Sglebius struct ifnet *ifp = ifa->ifa_ifp; 1875234130Sglebius struct carp_if *cif = ifp->if_carp; 1876234130Sglebius 1877234130Sglebius CIF_LOCK(cif); 1878234130Sglebius carp_detach_locked(ifa); 1879234130Sglebius CIF_FREE(cif); 1880234130Sglebius} 1881234130Sglebius 1882234130Sglebiusstatic void 1883234130Sglebiuscarp_detach_locked(struct ifaddr *ifa) 1884234130Sglebius{ 1885234130Sglebius struct ifnet *ifp = ifa->ifa_ifp; 1886234130Sglebius struct carp_if *cif = ifp->if_carp; 1887228571Sglebius struct carp_softc *sc = ifa->ifa_carp; 1888228571Sglebius int i, index; 1889142914Sglebius 1890228571Sglebius KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa)); 1891142914Sglebius 1892234130Sglebius CIF_LOCK_ASSERT(cif); 1893228571Sglebius CARP_LOCK(sc); 1894142215Sglebius 1895228571Sglebius /* Shift array. */ 1896228571Sglebius index = sc->sc_naddrs + sc->sc_naddrs6; 1897228571Sglebius for (i = 0; i < index; i++) 1898228571Sglebius if (sc->sc_ifas[i] == ifa) 1899228571Sglebius break; 1900228571Sglebius KASSERT(i < index, ("%s: %p no backref", __func__, ifa)); 1901228571Sglebius for (; i < index - 1; i++) 1902228571Sglebius sc->sc_ifas[i] = sc->sc_ifas[i+1]; 1903228571Sglebius sc->sc_ifas[index - 1] = NULL; 1904228571Sglebius 1905228571Sglebius switch (ifa->ifa_addr->sa_family) { 1906228571Sglebius#ifdef INET 1907228571Sglebius case AF_INET: 1908234130Sglebius cif->cif_naddrs--; 1909228571Sglebius sc->sc_naddrs--; 1910142215Sglebius break; 1911228571Sglebius#endif 1912228571Sglebius#ifdef INET6 1913228571Sglebius case AF_INET6: 1914234130Sglebius cif->cif_naddrs6--; 1915228571Sglebius sc->sc_naddrs6--; 1916142215Sglebius break; 1917228571Sglebius#endif 1918142215Sglebius } 1919228571Sglebius 1920230863Sglebius carp_ifa_delroute(ifa); 1921234130Sglebius carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family); 1922228571Sglebius 1923228571Sglebius ifa->ifa_carp = NULL; 1924228571Sglebius ifa_free(ifa); 1925228571Sglebius 1926228571Sglebius carp_hmac_prepare(sc); 1927228571Sglebius carp_sc_state(sc); 1928228571Sglebius 1929228571Sglebius if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) { 1930228571Sglebius CARP_UNLOCK(sc); 1931228571Sglebius carp_destroy(sc); 1932228571Sglebius } else 1933228571Sglebius CARP_UNLOCK(sc); 1934142215Sglebius} 1935142215Sglebius 1936228571Sglebiusstatic void 1937228571Sglebiuscarp_set_state(struct carp_softc *sc, int state) 1938142215Sglebius{ 1939142914Sglebius 1940228571Sglebius CARP_LOCK_ASSERT(sc); 1941228571Sglebius 1942228571Sglebius if (sc->sc_state != state) { 1943228571Sglebius const char *carp_states[] = { CARP_STATES }; 1944228571Sglebius char subsys[IFNAMSIZ+5]; 1945228571Sglebius 1946228571Sglebius sc->sc_state = state; 1947228571Sglebius 1948228571Sglebius snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid, 1949228571Sglebius sc->sc_carpdev->if_xname); 1950228571Sglebius devctl_notify("CARP", subsys, carp_states[state], NULL); 1951228571Sglebius } 1952142914Sglebius} 1953142914Sglebius 1954142914Sglebiusstatic void 1955228571Sglebiuscarp_linkstate(struct ifnet *ifp) 1956142914Sglebius{ 1957142215Sglebius struct carp_softc *sc; 1958142215Sglebius 1959228571Sglebius CIF_LOCK(ifp->if_carp); 1960228571Sglebius IFNET_FOREACH_CARP(ifp, sc) { 1961228571Sglebius CARP_LOCK(sc); 1962228571Sglebius carp_sc_state(sc); 1963228571Sglebius CARP_UNLOCK(sc); 1964228571Sglebius } 1965228571Sglebius CIF_UNLOCK(ifp->if_carp); 1966144329Sglebius} 1967144329Sglebius 1968144329Sglebiusstatic void 1969228571Sglebiuscarp_sc_state(struct carp_softc *sc) 1970144329Sglebius{ 1971144329Sglebius 1972228571Sglebius CARP_LOCK_ASSERT(sc); 1973228571Sglebius 1974144329Sglebius if (sc->sc_carpdev->if_link_state != LINK_STATE_UP || 1975144329Sglebius !(sc->sc_carpdev->if_flags & IFF_UP)) { 1976144329Sglebius callout_stop(&sc->sc_ad_tmo); 1977228571Sglebius#ifdef INET 1978144329Sglebius callout_stop(&sc->sc_md_tmo); 1979228571Sglebius#endif 1980228571Sglebius#ifdef INET6 1981144329Sglebius callout_stop(&sc->sc_md6_tmo); 1982228571Sglebius#endif 1983144329Sglebius carp_set_state(sc, INIT); 1984144329Sglebius carp_setrun(sc, 0); 1985228736Sglebius if (!sc->sc_suppress) 1986228736Sglebius carp_demote_adj(carp_ifdown_adj, "interface down"); 1987144329Sglebius sc->sc_suppress = 1; 1988144329Sglebius } else { 1989144329Sglebius carp_set_state(sc, INIT); 1990144329Sglebius carp_setrun(sc, 0); 1991144329Sglebius if (sc->sc_suppress) 1992228736Sglebius carp_demote_adj(-carp_ifdown_adj, "interface up"); 1993144329Sglebius sc->sc_suppress = 0; 1994142215Sglebius } 1995142215Sglebius} 1996142215Sglebius 1997228736Sglebiusstatic void 1998228736Sglebiuscarp_demote_adj(int adj, char *reason) 1999228736Sglebius{ 2000228736Sglebius carp_demotion += adj; 2001228736Sglebius CARP_LOG("demoted by %d to %d (%s)\n", adj, carp_demotion, reason); 2002228736Sglebius taskqueue_enqueue(taskqueue_swi, &carp_sendall_task); 2003228736Sglebius} 2004228571Sglebius 2005211157Swill#ifdef INET 2006211157Swillextern struct domain inetdomain; 2007211157Swillstatic struct protosw in_carp_protosw = { 2008211157Swill .pr_type = SOCK_RAW, 2009211157Swill .pr_domain = &inetdomain, 2010211157Swill .pr_protocol = IPPROTO_CARP, 2011211157Swill .pr_flags = PR_ATOMIC|PR_ADDR, 2012211157Swill .pr_input = carp_input, 2013211157Swill .pr_output = (pr_output_t *)rip_output, 2014211157Swill .pr_ctloutput = rip_ctloutput, 2015211157Swill .pr_usrreqs = &rip_usrreqs 2016211157Swill}; 2017211157Swill#endif 2018211157Swill 2019211157Swill#ifdef INET6 2020211157Swillextern struct domain inet6domain; 2021211157Swillstatic struct ip6protosw in6_carp_protosw = { 2022211157Swill .pr_type = SOCK_RAW, 2023211157Swill .pr_domain = &inet6domain, 2024211157Swill .pr_protocol = IPPROTO_CARP, 2025211157Swill .pr_flags = PR_ATOMIC|PR_ADDR, 2026211157Swill .pr_input = carp6_input, 2027211157Swill .pr_output = rip6_output, 2028211157Swill .pr_ctloutput = rip6_ctloutput, 2029211157Swill .pr_usrreqs = &rip6_usrreqs 2030211157Swill}; 2031211157Swill#endif 2032211157Swill 2033211157Swillstatic void 2034211157Swillcarp_mod_cleanup(void) 2035211157Swill{ 2036211157Swill 2037211157Swill#ifdef INET 2038211157Swill if (proto_reg[CARP_INET] == 0) { 2039212266Swill (void)ipproto_unregister(IPPROTO_CARP); 2040211157Swill pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW); 2041211157Swill proto_reg[CARP_INET] = -1; 2042211157Swill } 2043211157Swill carp_iamatch_p = NULL; 2044211157Swill#endif 2045211157Swill#ifdef INET6 2046211157Swill if (proto_reg[CARP_INET6] == 0) { 2047212266Swill (void)ip6proto_unregister(IPPROTO_CARP); 2048211157Swill pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW); 2049211157Swill proto_reg[CARP_INET6] = -1; 2050211157Swill } 2051211157Swill carp_iamatch6_p = NULL; 2052211157Swill carp_macmatch6_p = NULL; 2053211157Swill#endif 2054228571Sglebius carp_ioctl_p = NULL; 2055228571Sglebius carp_attach_p = NULL; 2056228571Sglebius carp_detach_p = NULL; 2057228571Sglebius carp_get_vhid_p = NULL; 2058211157Swill carp_linkstate_p = NULL; 2059211157Swill carp_forus_p = NULL; 2060211157Swill carp_output_p = NULL; 2061228736Sglebius carp_demote_adj_p = NULL; 2062228736Sglebius mtx_unlock(&carp_mtx); 2063228736Sglebius taskqueue_drain(taskqueue_swi, &carp_sendall_task); 2064211157Swill mtx_destroy(&carp_mtx); 2065211157Swill} 2066211157Swill 2067142215Sglebiusstatic int 2068211157Swillcarp_mod_load(void) 2069211157Swill{ 2070212266Swill int err; 2071211157Swill 2072211157Swill mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF); 2073228571Sglebius LIST_INIT(&carp_list); 2074228571Sglebius carp_get_vhid_p = carp_get_vhid; 2075211157Swill carp_forus_p = carp_forus; 2076211157Swill carp_output_p = carp_output; 2077228571Sglebius carp_linkstate_p = carp_linkstate; 2078228571Sglebius carp_ioctl_p = carp_ioctl; 2079228571Sglebius carp_attach_p = carp_attach; 2080228571Sglebius carp_detach_p = carp_detach; 2081228736Sglebius carp_demote_adj_p = carp_demote_adj; 2082211157Swill#ifdef INET6 2083211157Swill carp_iamatch6_p = carp_iamatch6; 2084211157Swill carp_macmatch6_p = carp_macmatch6; 2085211157Swill proto_reg[CARP_INET6] = pf_proto_register(PF_INET6, 2086211157Swill (struct protosw *)&in6_carp_protosw); 2087228571Sglebius if (proto_reg[CARP_INET6]) { 2088211157Swill printf("carp: error %d attaching to PF_INET6\n", 2089211157Swill proto_reg[CARP_INET6]); 2090211157Swill carp_mod_cleanup(); 2091212898Sglebius return (proto_reg[CARP_INET6]); 2092211157Swill } 2093212266Swill err = ip6proto_register(IPPROTO_CARP); 2094212266Swill if (err) { 2095212266Swill printf("carp: error %d registering with INET6\n", err); 2096212266Swill carp_mod_cleanup(); 2097212898Sglebius return (err); 2098212266Swill } 2099211157Swill#endif 2100211157Swill#ifdef INET 2101211157Swill carp_iamatch_p = carp_iamatch; 2102211157Swill proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw); 2103228571Sglebius if (proto_reg[CARP_INET]) { 2104211157Swill printf("carp: error %d attaching to PF_INET\n", 2105211157Swill proto_reg[CARP_INET]); 2106211157Swill carp_mod_cleanup(); 2107212898Sglebius return (proto_reg[CARP_INET]); 2108211157Swill } 2109212266Swill err = ipproto_register(IPPROTO_CARP); 2110212266Swill if (err) { 2111212266Swill printf("carp: error %d registering with INET\n", err); 2112212266Swill carp_mod_cleanup(); 2113212898Sglebius return (err); 2114212266Swill } 2115211157Swill#endif 2116228571Sglebius return (0); 2117211157Swill} 2118211157Swill 2119211157Swillstatic int 2120142215Sglebiuscarp_modevent(module_t mod, int type, void *data) 2121142215Sglebius{ 2122142215Sglebius switch (type) { 2123142215Sglebius case MOD_LOAD: 2124211157Swill return carp_mod_load(); 2125211157Swill /* NOTREACHED */ 2126142215Sglebius case MOD_UNLOAD: 2127228571Sglebius mtx_lock(&carp_mtx); 2128228571Sglebius if (LIST_EMPTY(&carp_list)) 2129228571Sglebius carp_mod_cleanup(); 2130228571Sglebius else { 2131228571Sglebius mtx_unlock(&carp_mtx); 2132228571Sglebius return (EBUSY); 2133228571Sglebius } 2134142215Sglebius break; 2135142215Sglebius 2136142215Sglebius default: 2137156947Sglebius return (EINVAL); 2138142215Sglebius } 2139142215Sglebius 2140156947Sglebius return (0); 2141142215Sglebius} 2142142215Sglebius 2143142215Sglebiusstatic moduledata_t carp_mod = { 2144142215Sglebius "carp", 2145142215Sglebius carp_modevent, 2146142215Sglebius 0 2147142215Sglebius}; 2148142215Sglebius 2149212265SwillDECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 2150