ip_carp.c revision 142452
1142215Sglebius/* $FreeBSD: head/sys/netinet/ip_carp.c 142452 2005-02-25 11:26:39Z glebius $ */ 2142215Sglebius 3142215Sglebius/* 4142215Sglebius * Copyright (c) 2002 Michael Shalayeff. All rights reserved. 5142215Sglebius * Copyright (c) 2003 Ryan McBride. 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 29142215Sglebius#include "opt_carp.h" 30142215Sglebius#include "opt_bpf.h" 31142215Sglebius#include "opt_inet.h" 32142215Sglebius#include "opt_inet6.h" 33142215Sglebius 34142215Sglebius#include <sys/types.h> 35142215Sglebius#include <sys/param.h> 36142215Sglebius#include <sys/systm.h> 37142215Sglebius#include <sys/conf.h> 38142215Sglebius#include <sys/kernel.h> 39142215Sglebius#include <sys/limits.h> 40142215Sglebius#include <sys/malloc.h> 41142215Sglebius#include <sys/mbuf.h> 42142215Sglebius#include <sys/module.h> 43142215Sglebius#include <sys/time.h> 44142215Sglebius#include <sys/proc.h> 45142215Sglebius#include <sys/sysctl.h> 46142215Sglebius#include <sys/syslog.h> 47142215Sglebius#include <sys/signalvar.h> 48142215Sglebius#include <sys/filio.h> 49142215Sglebius#include <sys/sockio.h> 50142215Sglebius 51142215Sglebius#include <sys/socket.h> 52142215Sglebius#include <sys/vnode.h> 53142215Sglebius 54142215Sglebius#include <machine/stdarg.h> 55142215Sglebius 56142215Sglebius#include <net/bpf.h> 57142215Sglebius#include <net/ethernet.h> 58142215Sglebius#include <net/fddi.h> 59142215Sglebius#include <net/iso88025.h> 60142215Sglebius#include <net/if.h> 61142215Sglebius#include <net/if_clone.h> 62142215Sglebius#include <net/if_types.h> 63142215Sglebius#include <net/route.h> 64142215Sglebius 65142215Sglebius#ifdef INET 66142215Sglebius#include <netinet/in.h> 67142215Sglebius#include <netinet/in_var.h> 68142215Sglebius#include <netinet/in_systm.h> 69142215Sglebius#include <netinet/ip.h> 70142215Sglebius#include <netinet/ip_var.h> 71142215Sglebius#include <netinet/if_ether.h> 72142215Sglebius#include <machine/in_cksum.h> 73142215Sglebius#endif 74142215Sglebius 75142215Sglebius#ifdef INET6 76142215Sglebius#include <netinet/icmp6.h> 77142215Sglebius#include <netinet/ip6.h> 78142215Sglebius#include <netinet6/ip6_var.h> 79142215Sglebius#include <netinet6/nd6.h> 80142215Sglebius#include <net/if_dl.h> 81142215Sglebius#endif 82142215Sglebius 83142215Sglebius#include <crypto/sha1.h> 84142215Sglebius#include <netinet/ip_carp.h> 85142215Sglebius 86142215Sglebius#define CARP_IFNAME "carp" 87142215Sglebiusstatic MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces"); 88142215SglebiusSYSCTL_DECL(_net_inet_carp); 89142215Sglebius 90142215Sglebiusstruct carp_softc { 91142215Sglebius struct arpcom sc_ac; /* Interface clue */ 92142215Sglebius int if_flags; /* UP/DOWN */ 93142215Sglebius struct ifnet *sc_ifp; /* Parent */ 94142215Sglebius struct in_ifaddr *sc_ia; /* primary iface address */ 95142215Sglebius struct ip_moptions sc_imo; 96142215Sglebius#ifdef INET6 97142215Sglebius struct in6_ifaddr *sc_ia6; /* primary iface address v6 */ 98142215Sglebius struct ip6_moptions sc_im6o; 99142215Sglebius#endif /* INET6 */ 100142215Sglebius TAILQ_ENTRY(carp_softc) sc_list; 101142215Sglebius 102142215Sglebius enum { INIT = 0, BACKUP, MASTER } sc_state; 103142215Sglebius 104142215Sglebius int sc_flags_backup; 105142215Sglebius int sc_suppress; 106142215Sglebius 107142215Sglebius int sc_sendad_errors; 108142215Sglebius#define CARP_SENDAD_MAX_ERRORS 3 109142215Sglebius int sc_sendad_success; 110142215Sglebius#define CARP_SENDAD_MIN_SUCCESS 3 111142215Sglebius 112142215Sglebius int sc_vhid; 113142215Sglebius int sc_advskew; 114142215Sglebius int sc_naddrs; 115142215Sglebius int sc_naddrs6; 116142215Sglebius int sc_advbase; /* seconds */ 117142215Sglebius int sc_init_counter; 118142215Sglebius u_int64_t sc_counter; 119142215Sglebius 120142215Sglebius /* authentication */ 121142215Sglebius#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 126142215Sglebius struct callout sc_ad_tmo; /* advertisement timeout */ 127142215Sglebius struct callout sc_md_tmo; /* master down timeout */ 128142215Sglebius struct callout sc_md6_tmo; /* master down timeout */ 129142215Sglebius 130142215Sglebius LIST_ENTRY(carp_softc) sc_next; /* Interface clue */ 131142215Sglebius}; 132142215Sglebius#define sc_if sc_ac.ac_if 133142215Sglebius 134142215Sglebiusint carp_suppress_preempt = 0; 135142215Sglebiusint carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */ 136142215SglebiusSYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW, 137142215Sglebius &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets"); 138142215SglebiusSYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW, 139142215Sglebius &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode"); 140142215SglebiusSYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW, 141142215Sglebius &carp_opts[CARPCTL_LOG], 0, "log bad carp packets"); 142142215SglebiusSYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW, 143142215Sglebius &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses"); 144142215Sglebius 145142215Sglebiusstruct carpstats carpstats; 146142215SglebiusSYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW, 147142215Sglebius &carpstats, carpstats, 148142215Sglebius "CARP statistics (struct carpstats, netinet/ip_carp.h)"); 149142215Sglebius 150142215Sglebiusstruct carp_if { 151142215Sglebius TAILQ_HEAD(, carp_softc) vhif_vrs; 152142215Sglebius int vhif_nvrs; 153142215Sglebius 154142215Sglebius struct ifnet *vhif_ifp; 155142215Sglebius struct mtx vhif_mtx; 156142215Sglebius}; 157142215Sglebius/* lock per carp_if queue */ 158142215Sglebius#define CARP_LOCK_INIT(cif) mtx_init(&(cif)->vhif_mtx, "carp", \ 159142215Sglebius NULL, MTX_DEF) 160142215Sglebius#define CARP_LOCK_DESTROY(cif) mtx_destroy(&(cif->vhif_mtx)) 161142215Sglebius#define CARP_LOCK_ASSERT(cif) mtx_assert(&(cif)->vhif_mtx, MA_OWNED) 162142215Sglebius#define CARP_LOCK(cif) mtx_lock(&(cif)->vhif_mtx) 163142215Sglebius#define CARP_UNLOCK(cif) mtx_unlock(&(cif)->vhif_mtx) 164142215Sglebius 165142451Sglebius#define CARP_LOG(...) do { \ 166142446Sglebius if (carp_opts[CARPCTL_LOG] > 0) \ 167142446Sglebius log(LOG_INFO, __VA_ARGS__); \ 168142451Sglebius} while (0) 169142215Sglebius 170142451Sglebius#define CARP_DEBUG(...) do { \ 171142446Sglebius if (carp_opts[CARPCTL_LOG] > 1) \ 172142446Sglebius log(LOG_DEBUG, __VA_ARGS__); \ 173142451Sglebius} while (0) 174142446Sglebius 175142215Sglebiusvoid carp_hmac_prepare(struct carp_softc *); 176142215Sglebiusvoid carp_hmac_generate(struct carp_softc *, u_int32_t *, 177142215Sglebius unsigned char *); 178142215Sglebiusint carp_hmac_verify(struct carp_softc *, u_int32_t *, 179142215Sglebius unsigned char *); 180142215Sglebiusvoid carp_setroute(struct carp_softc *, int); 181142446Sglebiusvoid carp_input_c(struct mbuf *, struct carp_header *, sa_family_t); 182142215Sglebiusint carp_clone_create(struct if_clone *, int); 183142215Sglebiusvoid carp_clone_destroy(struct ifnet *); 184142215Sglebiusvoid carpdetach(struct carp_softc *); 185142215Sglebiusint carp_prepare_ad(struct mbuf *, struct carp_softc *, 186142215Sglebius struct carp_header *); 187142215Sglebiusvoid carp_send_ad_all(void); 188142215Sglebiusvoid carp_send_ad(void *); 189142215Sglebiusvoid carp_send_arp(struct carp_softc *); 190142215Sglebiusvoid carp_master_down(void *); 191142215Sglebiusint carp_ioctl(struct ifnet *, u_long, caddr_t); 192142215Sglebiusstatic int carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *, 193142215Sglebius struct rtentry *); 194142215Sglebiusvoid carp_start(struct ifnet *); 195142215Sglebiusvoid carp_setrun(struct carp_softc *, sa_family_t); 196142215Sglebiusvoid carp_set_state(struct carp_softc *, int); 197142215Sglebiusint carp_addrcount(struct carp_if *, struct in_ifaddr *, int); 198142215Sglebiusenum { CARP_COUNT_MASTER, CARP_COUNT_RUNNING }; 199142215Sglebius 200142215Sglebiusint carp_set_addr(struct carp_softc *, struct sockaddr_in *); 201142215Sglebiusint carp_del_addr(struct carp_softc *, struct sockaddr_in *); 202142215Sglebius#ifdef INET6 203142215Sglebiusvoid carp_send_na(struct carp_softc *); 204142215Sglebiusint carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *); 205142215Sglebiusint carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *); 206142215Sglebius#endif 207142215Sglebius 208142215Sglebiusstatic LIST_HEAD(, carp_softc) carpif_list; 209142215SglebiusIFC_SIMPLE_DECLARE(carp, 0); 210142215Sglebius 211142215Sglebiusstatic __inline u_int16_t 212142215Sglebiuscarp_cksum(struct mbuf *m, int len) 213142215Sglebius{ 214142215Sglebius return (in_cksum(m, len)); 215142215Sglebius} 216142215Sglebius 217142215Sglebiusvoid 218142215Sglebiuscarp_hmac_prepare(struct carp_softc *sc) 219142215Sglebius{ 220142215Sglebius u_int8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT; 221142215Sglebius u_int8_t vhid = sc->sc_vhid & 0xff; 222142215Sglebius struct ifaddr *ifa; 223142215Sglebius int i; 224142215Sglebius#ifdef INET6 225142215Sglebius struct in6_addr in6; 226142215Sglebius#endif 227142215Sglebius 228142215Sglebius /* compute ipad from key */ 229142215Sglebius bzero(sc->sc_pad, sizeof(sc->sc_pad)); 230142215Sglebius bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key)); 231142215Sglebius for (i = 0; i < sizeof(sc->sc_pad); i++) 232142215Sglebius sc->sc_pad[i] ^= 0x36; 233142215Sglebius 234142215Sglebius /* precompute first part of inner hash */ 235142215Sglebius SHA1Init(&sc->sc_sha1); 236142215Sglebius SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad)); 237142215Sglebius SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version)); 238142215Sglebius SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type)); 239142215Sglebius SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid)); 240142215Sglebius#ifdef INET 241142215Sglebius TAILQ_FOREACH(ifa, &sc->sc_if.if_addrlist, ifa_list) { 242142215Sglebius if (ifa->ifa_addr->sa_family == AF_INET) 243142215Sglebius SHA1Update(&sc->sc_sha1, 244142215Sglebius (void *)&ifatoia(ifa)->ia_addr.sin_addr.s_addr, 245142215Sglebius sizeof(struct in_addr)); 246142215Sglebius } 247142215Sglebius#endif /* INET */ 248142215Sglebius#ifdef INET6 249142215Sglebius TAILQ_FOREACH(ifa, &sc->sc_if.if_addrlist, ifa_list) { 250142215Sglebius if (ifa->ifa_addr->sa_family == AF_INET6) { 251142215Sglebius in6 = ifatoia6(ifa)->ia_addr.sin6_addr; 252142215Sglebius if (IN6_IS_ADDR_LINKLOCAL(&in6)) 253142215Sglebius in6.s6_addr16[1] = 0; 254142215Sglebius SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6)); 255142215Sglebius } 256142215Sglebius } 257142215Sglebius#endif /* INET6 */ 258142215Sglebius 259142215Sglebius /* convert ipad to opad */ 260142215Sglebius for (i = 0; i < sizeof(sc->sc_pad); i++) 261142215Sglebius sc->sc_pad[i] ^= 0x36 ^ 0x5c; 262142215Sglebius} 263142215Sglebius 264142215Sglebiusvoid 265142215Sglebiuscarp_hmac_generate(struct carp_softc *sc, u_int32_t counter[2], 266142215Sglebius unsigned char md[20]) 267142215Sglebius{ 268142215Sglebius SHA1_CTX sha1ctx; 269142215Sglebius 270142215Sglebius /* fetch first half of inner hash */ 271142215Sglebius bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx)); 272142215Sglebius 273142215Sglebius SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter)); 274142215Sglebius SHA1Final(md, &sha1ctx); 275142215Sglebius 276142215Sglebius /* outer hash */ 277142215Sglebius SHA1Init(&sha1ctx); 278142215Sglebius SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad)); 279142215Sglebius SHA1Update(&sha1ctx, md, 20); 280142215Sglebius SHA1Final(md, &sha1ctx); 281142215Sglebius} 282142215Sglebius 283142215Sglebiusint 284142215Sglebiuscarp_hmac_verify(struct carp_softc *sc, u_int32_t counter[2], 285142215Sglebius unsigned char md[20]) 286142215Sglebius{ 287142215Sglebius unsigned char md2[20]; 288142215Sglebius 289142215Sglebius carp_hmac_generate(sc, counter, md2); 290142215Sglebius 291142215Sglebius return (bcmp(md, md2, sizeof(md2))); 292142215Sglebius} 293142215Sglebius 294142215Sglebiusvoid 295142215Sglebiuscarp_setroute(struct carp_softc *sc, int cmd) 296142215Sglebius{ 297142215Sglebius struct ifaddr *ifa; 298142215Sglebius int s; 299142215Sglebius 300142215Sglebius s = splnet(); 301142215Sglebius TAILQ_FOREACH(ifa, &sc->sc_if.if_addrlist, ifa_list) { 302142215Sglebius if (ifa->ifa_addr->sa_family == AF_INET && sc->sc_ifp != NULL) { 303142215Sglebius int count = carp_addrcount( 304142215Sglebius (struct carp_if *)sc->sc_ifp->if_carp, 305142215Sglebius ifatoia(ifa), CARP_COUNT_MASTER); 306142215Sglebius 307142215Sglebius if ((cmd == RTM_ADD && count == 1) || 308142215Sglebius (cmd == RTM_DELETE && count == 0)) 309142215Sglebius rtinit(ifa, cmd, RTF_UP | RTF_HOST); 310142215Sglebius } 311142215Sglebius#ifdef INET6 312142215Sglebius if (ifa->ifa_addr->sa_family == AF_INET6) { 313142215Sglebius if (cmd == RTM_ADD) 314142215Sglebius in6_ifaddloop(ifa); 315142215Sglebius else 316142215Sglebius in6_ifremloop(ifa); 317142215Sglebius } 318142215Sglebius#endif /* INET6 */ 319142215Sglebius } 320142215Sglebius splx(s); 321142215Sglebius} 322142215Sglebius 323142215Sglebiusint 324142215Sglebiuscarp_clone_create(struct if_clone *ifc, int unit) 325142215Sglebius{ 326142215Sglebius 327142215Sglebius struct carp_softc *sc; 328142215Sglebius struct ifnet *ifp; 329142215Sglebius 330142215Sglebius MALLOC(sc, struct carp_softc *, sizeof(*sc), M_CARP, M_WAITOK|M_ZERO); 331142215Sglebius 332142215Sglebius sc->sc_flags_backup = 0; 333142215Sglebius sc->sc_suppress = 0; 334142215Sglebius sc->sc_advbase = CARP_DFLTINTV; 335142215Sglebius sc->sc_vhid = -1; /* required setting */ 336142215Sglebius sc->sc_advskew = 0; 337142215Sglebius sc->sc_init_counter = 1; 338142215Sglebius sc->sc_naddrs = sc->sc_naddrs6 = 0; /* M_ZERO? */ 339142215Sglebius#ifdef INET6 340142215Sglebius sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL; 341142215Sglebius#endif 342142215Sglebius 343142215Sglebius callout_init(&sc->sc_ad_tmo, 0); 344142215Sglebius callout_init(&sc->sc_md_tmo, 0); 345142215Sglebius callout_init(&sc->sc_md6_tmo, 0); 346142215Sglebius 347142215Sglebius ifp = &sc->sc_if; 348142215Sglebius ifp->if_softc = sc; 349142215Sglebius if_initname(ifp, CARP_IFNAME, unit); 350142215Sglebius ifp->if_mtu = ETHERMTU; 351142215Sglebius ifp->if_flags = 0; 352142215Sglebius ifp->if_ioctl = carp_ioctl; 353142215Sglebius ifp->if_output = carp_looutput; 354142215Sglebius ifp->if_start = carp_start; 355142215Sglebius ifp->if_type = IFT_CARP; 356142215Sglebius ifp->if_snd.ifq_maxlen = ifqmaxlen; 357142215Sglebius ifp->if_hdrlen = 0; 358142215Sglebius if_attach(ifp); 359142215Sglebius LIST_INSERT_HEAD(&carpif_list, sc, sc_next); 360142215Sglebius bpfattach(&sc->sc_if, DLT_LOOP, sizeof(u_int32_t)); 361142215Sglebius return (0); 362142215Sglebius} 363142215Sglebius 364142215Sglebiusvoid 365142215Sglebiuscarp_clone_destroy(struct ifnet *ifp) 366142215Sglebius{ 367142215Sglebius struct carp_softc *sc = ifp->if_softc; 368142215Sglebius struct carp_if *cif; 369142215Sglebius struct ip_moptions *imo = &sc->sc_imo; 370142215Sglebius#ifdef INET6 371142215Sglebius struct ip6_moptions *im6o = &sc->sc_im6o; 372142215Sglebius#endif 373142215Sglebius 374142215Sglebius/* carpdetach(sc); */ 375142215Sglebius 376142215Sglebius callout_stop(&sc->sc_ad_tmo); 377142215Sglebius callout_stop(&sc->sc_md_tmo); 378142215Sglebius callout_stop(&sc->sc_md6_tmo); 379142215Sglebius 380142215Sglebius if (imo->imo_num_memberships) { 381142215Sglebius in_delmulti(imo->imo_membership[--imo->imo_num_memberships]); 382142215Sglebius imo->imo_multicast_ifp = NULL; 383142215Sglebius } 384142215Sglebius#ifdef INET6 385142215Sglebius while (!LIST_EMPTY(&im6o->im6o_memberships)) { 386142215Sglebius struct in6_multi_mship *imm = 387142215Sglebius LIST_FIRST(&im6o->im6o_memberships); 388142215Sglebius LIST_REMOVE(imm, i6mm_chain); 389142215Sglebius in6_leavegroup(imm); 390142215Sglebius } 391142215Sglebius im6o->im6o_multicast_ifp = NULL; 392142215Sglebius#endif 393142215Sglebius 394142215Sglebius /* Remove ourself from parents if_carp queue */ 395142215Sglebius if (sc->sc_ifp && (cif = sc->sc_ifp->if_carp)) { 396142215Sglebius CARP_LOCK(cif); 397142215Sglebius TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list); 398142215Sglebius if (!--cif->vhif_nvrs) { 399142215Sglebius sc->sc_ifp->if_carp = NULL; 400142215Sglebius CARP_LOCK_DESTROY(cif); 401142215Sglebius FREE(cif, M_CARP); 402142243Sglebius ifpromisc(sc->sc_ifp, 0); 403142215Sglebius } else { 404142215Sglebius CARP_UNLOCK(cif); 405142215Sglebius } 406142215Sglebius } 407142215Sglebius 408142215Sglebius bpfdetach(ifp); 409142215Sglebius if_detach(ifp); 410142215Sglebius LIST_REMOVE(sc, sc_next); 411142215Sglebius free(sc, M_CARP); 412142215Sglebius} 413142215Sglebius 414142215Sglebius/* 415142215Sglebius * process input packet. 416142215Sglebius * we have rearranged checks order compared to the rfc, 417142215Sglebius * but it seems more efficient this way or not possible otherwise. 418142215Sglebius */ 419142215Sglebiusvoid 420142215Sglebiuscarp_input(struct mbuf *m, int hlen) 421142215Sglebius{ 422142215Sglebius struct ip *ip = mtod(m, struct ip *); 423142215Sglebius struct carp_header *ch; 424142215Sglebius int iplen, len; 425142215Sglebius 426142215Sglebius carpstats.carps_ipackets++; 427142215Sglebius 428142215Sglebius if (!carp_opts[CARPCTL_ALLOW]) { 429142215Sglebius m_freem(m); 430142215Sglebius return; 431142215Sglebius } 432142215Sglebius 433142215Sglebius /* check if received on a valid carp interface */ 434142215Sglebius if (m->m_pkthdr.rcvif->if_carp == NULL) { 435142215Sglebius carpstats.carps_badif++; 436142452Sglebius CARP_LOG("carp_input: packet received on non-carp " 437142452Sglebius "interface: %s\n", 438142446Sglebius m->m_pkthdr.rcvif->if_xname); 439142215Sglebius m_freem(m); 440142215Sglebius return; 441142215Sglebius } 442142215Sglebius 443142215Sglebius /* verify that the IP TTL is 255. */ 444142215Sglebius if (ip->ip_ttl != CARP_DFLTTL) { 445142215Sglebius carpstats.carps_badttl++; 446142452Sglebius CARP_LOG("carp_input: received ttl %d != 255i on %s\n", 447142446Sglebius ip->ip_ttl, 448142446Sglebius m->m_pkthdr.rcvif->if_xname); 449142215Sglebius m_freem(m); 450142215Sglebius return; 451142215Sglebius } 452142215Sglebius 453142215Sglebius iplen = ip->ip_hl << 2; 454142215Sglebius 455142215Sglebius if (m->m_pkthdr.len < iplen + sizeof(*ch)) { 456142215Sglebius carpstats.carps_badlen++; 457142446Sglebius CARP_LOG("carp_input: received len %zd < " 458142452Sglebius "sizeof(struct carp_header)\n", 459142446Sglebius m->m_len - sizeof(struct ip)); 460142215Sglebius m_freem(m); 461142215Sglebius return; 462142215Sglebius } 463142215Sglebius 464142215Sglebius if (iplen + sizeof(*ch) < m->m_len) { 465142215Sglebius if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) { 466142215Sglebius carpstats.carps_hdrops++; 467142452Sglebius CARP_LOG("carp_input: pullup failed\n"); 468142215Sglebius return; 469142215Sglebius } 470142215Sglebius ip = mtod(m, struct ip *); 471142215Sglebius } 472142215Sglebius ch = (struct carp_header *)((char *)ip + iplen); 473142215Sglebius 474142215Sglebius /* 475142215Sglebius * verify that the received packet length is 476142215Sglebius * equal to the CARP header 477142215Sglebius */ 478142215Sglebius len = iplen + sizeof(*ch); 479142215Sglebius if (len > m->m_pkthdr.len) { 480142215Sglebius carpstats.carps_badlen++; 481142452Sglebius CARP_LOG("carp_input: packet too short %d on %s\n", 482142446Sglebius m->m_pkthdr.len, 483142446Sglebius m->m_pkthdr.rcvif->if_xname); 484142215Sglebius m_freem(m); 485142215Sglebius return; 486142215Sglebius } 487142215Sglebius 488142215Sglebius if ((m = m_pullup(m, len)) == NULL) { 489142215Sglebius carpstats.carps_hdrops++; 490142215Sglebius return; 491142215Sglebius } 492142215Sglebius ip = mtod(m, struct ip *); 493142215Sglebius ch = (struct carp_header *)((char *)ip + iplen); 494142215Sglebius 495142215Sglebius /* verify the CARP checksum */ 496142215Sglebius m->m_data += iplen; 497142215Sglebius if (carp_cksum(m, len - iplen)) { 498142215Sglebius carpstats.carps_badsum++; 499142452Sglebius CARP_LOG("carp_input: checksum failed on %s\n", 500142446Sglebius m->m_pkthdr.rcvif->if_xname); 501142215Sglebius m_freem(m); 502142215Sglebius return; 503142215Sglebius } 504142215Sglebius m->m_data -= iplen; 505142215Sglebius 506142446Sglebius carp_input_c(m, ch, AF_INET); 507142215Sglebius} 508142215Sglebius 509142215Sglebius#ifdef INET6 510142215Sglebiusint 511142215Sglebiuscarp6_input(struct mbuf **mp, int *offp, int proto) 512142215Sglebius{ 513142215Sglebius struct mbuf *m = *mp; 514142215Sglebius struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 515142215Sglebius struct carp_header *ch; 516142215Sglebius u_int len; 517142215Sglebius 518142215Sglebius carpstats.carps_ipackets6++; 519142215Sglebius 520142215Sglebius if (!carp_opts[CARPCTL_ALLOW]) { 521142215Sglebius m_freem(m); 522142215Sglebius return (IPPROTO_DONE); 523142215Sglebius } 524142215Sglebius 525142215Sglebius /* check if received on a valid carp interface */ 526142215Sglebius if (m->m_pkthdr.rcvif->if_carp == NULL) { 527142215Sglebius carpstats.carps_badif++; 528142446Sglebius CARP_LOG("carp6_input: packet received on non-carp " 529142452Sglebius "interface: %s\n", 530142446Sglebius m->m_pkthdr.rcvif->if_xname); 531142215Sglebius m_freem(m); 532142215Sglebius return (IPPROTO_DONE); 533142215Sglebius } 534142215Sglebius 535142215Sglebius /* verify that the IP TTL is 255 */ 536142215Sglebius if (ip6->ip6_hlim != CARP_DFLTTL) { 537142215Sglebius carpstats.carps_badttl++; 538142452Sglebius CARP_LOG("carp6_input: received ttl %d != 255 on %s\n", 539142446Sglebius ip6->ip6_hlim, 540142446Sglebius m->m_pkthdr.rcvif->if_xname); 541142215Sglebius m_freem(m); 542142215Sglebius return (IPPROTO_DONE); 543142215Sglebius } 544142215Sglebius 545142215Sglebius /* verify that we have a complete carp packet */ 546142215Sglebius len = m->m_len; 547142215Sglebius IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch)); 548142215Sglebius if (ch == NULL) { 549142215Sglebius carpstats.carps_badlen++; 550142452Sglebius CARP_LOG("carp6_input: packet size %u too small on %s\n", len, 551142446Sglebius m->m_pkthdr.rcvif->if_xname); 552142215Sglebius return (IPPROTO_DONE); 553142215Sglebius } 554142215Sglebius 555142215Sglebius 556142215Sglebius /* verify the CARP checksum */ 557142215Sglebius m->m_data += *offp; 558142215Sglebius if (carp_cksum(m, sizeof(*ch))) { 559142215Sglebius carpstats.carps_badsum++; 560142452Sglebius CARP_LOG("carp6_input: checksum failed, on %s\n", 561142446Sglebius m->m_pkthdr.rcvif->if_xname); 562142215Sglebius m_freem(m); 563142215Sglebius return (IPPROTO_DONE); 564142215Sglebius } 565142215Sglebius m->m_data -= *offp; 566142215Sglebius 567142446Sglebius carp_input_c(m, ch, AF_INET6); 568142215Sglebius return (IPPROTO_DONE); 569142215Sglebius} 570142215Sglebius#endif /* INET6 */ 571142215Sglebius 572142215Sglebiusvoid 573142446Sglebiuscarp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af) 574142215Sglebius{ 575142215Sglebius struct ifnet *ifp = m->m_pkthdr.rcvif; 576142446Sglebius struct carp_softc *sc; 577142215Sglebius u_int64_t tmp_counter; 578142215Sglebius struct timeval sc_tv, ch_tv; 579142215Sglebius 580142215Sglebius /* verify that the VHID is valid on the receiving interface */ 581142215Sglebius CARP_LOCK(ifp->if_carp); 582142215Sglebius TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list) 583142215Sglebius if (sc->sc_vhid == ch->carp_vhid) 584142215Sglebius break; 585142215Sglebius CARP_UNLOCK(ifp->if_carp); 586142215Sglebius if (!sc || (sc->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) != 587142215Sglebius (IFF_UP|IFF_RUNNING)) { 588142215Sglebius carpstats.carps_badvhid++; 589142215Sglebius m_freem(m); 590142215Sglebius return; 591142215Sglebius } 592142215Sglebius 593142215Sglebius getmicrotime(&sc->sc_if.if_lastchange); 594142215Sglebius sc->sc_if.if_ipackets++; 595142215Sglebius sc->sc_if.if_ibytes += m->m_pkthdr.len; 596142215Sglebius 597142215Sglebius if (sc->sc_if.if_bpf) { 598142215Sglebius /* 599142215Sglebius * We need to prepend the address family as 600142215Sglebius * a four byte field. Cons up a dummy header 601142215Sglebius * to pacify bpf. This is safe because bpf 602142215Sglebius * will only read from the mbuf (i.e., it won't 603142215Sglebius * try to free it or keep a pointer to it). 604142215Sglebius */ 605142215Sglebius struct mbuf m0; 606142215Sglebius struct ip *ip = mtod(m, struct ip *); 607142215Sglebius u_int32_t maf = htonl(af); 608142215Sglebius 609142215Sglebius m0.m_next = m; 610142215Sglebius m0.m_len = sizeof(maf); 611142215Sglebius m0.m_data = (char *)&maf; 612142215Sglebius /* BPF wants net byte order */ 613142215Sglebius ip->ip_len = htonl(ip->ip_len); 614142215Sglebius ip->ip_off = htonl(ip->ip_off); 615142215Sglebius BPF_MTAP(&sc->sc_if, &m0); 616142215Sglebius } 617142215Sglebius 618142215Sglebius /* verify the CARP version. */ 619142215Sglebius if (ch->carp_version != CARP_VERSION) { 620142215Sglebius carpstats.carps_badver++; 621142215Sglebius sc->sc_if.if_ierrors++; 622142452Sglebius CARP_LOG("%s; invalid version %d\n", 623142446Sglebius sc->sc_if.if_xname, 624142446Sglebius ch->carp_version); 625142215Sglebius m_freem(m); 626142215Sglebius return; 627142215Sglebius } 628142215Sglebius 629142215Sglebius /* verify the hash */ 630142215Sglebius if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) { 631142215Sglebius carpstats.carps_badauth++; 632142215Sglebius sc->sc_if.if_ierrors++; 633142452Sglebius CARP_LOG("%s: incorrect hash\n", sc->sc_if.if_xname); 634142215Sglebius m_freem(m); 635142215Sglebius return; 636142215Sglebius } 637142215Sglebius 638142215Sglebius tmp_counter = ntohl(ch->carp_counter[0]); 639142215Sglebius tmp_counter = tmp_counter<<32; 640142215Sglebius tmp_counter += ntohl(ch->carp_counter[1]); 641142215Sglebius 642142215Sglebius /* XXX Replay protection goes here */ 643142215Sglebius 644142215Sglebius sc->sc_init_counter = 0; 645142215Sglebius sc->sc_counter = tmp_counter; 646142215Sglebius 647142215Sglebius sc_tv.tv_sec = sc->sc_advbase; 648142215Sglebius if (carp_suppress_preempt && sc->sc_advskew < 240) 649142215Sglebius sc_tv.tv_usec = 240 * 1000000 / 256; 650142215Sglebius else 651142215Sglebius sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256; 652142215Sglebius ch_tv.tv_sec = ch->carp_advbase; 653142215Sglebius ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256; 654142215Sglebius 655142215Sglebius switch (sc->sc_state) { 656142215Sglebius case INIT: 657142215Sglebius break; 658142215Sglebius case MASTER: 659142215Sglebius /* 660142215Sglebius * If we receive an advertisement from a master who's going to 661142215Sglebius * be more frequent than us, go into BACKUP state. 662142215Sglebius */ 663142215Sglebius if (timevalcmp(&sc_tv, &ch_tv, >) || 664142215Sglebius timevalcmp(&sc_tv, &ch_tv, ==)) { 665142215Sglebius callout_stop(&sc->sc_ad_tmo); 666142446Sglebius CARP_DEBUG("%s: MASTER -> BACKUP " 667142452Sglebius "(more frequent advertisement received)\n", 668142446Sglebius sc->sc_if.if_xname); 669142215Sglebius carp_set_state(sc, BACKUP); 670142215Sglebius carp_setrun(sc, 0); 671142215Sglebius carp_setroute(sc, RTM_DELETE); 672142215Sglebius } 673142215Sglebius break; 674142215Sglebius case BACKUP: 675142215Sglebius /* 676142215Sglebius * If we're pre-empting masters who advertise slower than us, 677142215Sglebius * and this one claims to be slower, treat him as down. 678142215Sglebius */ 679142215Sglebius if (carp_opts[CARPCTL_PREEMPT] && 680142215Sglebius timevalcmp(&sc_tv, &ch_tv, <)) { 681142446Sglebius CARP_DEBUG("%s: BACKUP -> MASTER " 682142452Sglebius "(preempting a slower master)\n", 683142446Sglebius sc->sc_if.if_xname); 684142215Sglebius carp_master_down(sc); 685142215Sglebius break; 686142215Sglebius } 687142215Sglebius 688142215Sglebius /* 689142215Sglebius * If the master is going to advertise at such a low frequency 690142215Sglebius * that he's guaranteed to time out, we'd might as well just 691142215Sglebius * treat him as timed out now. 692142215Sglebius */ 693142215Sglebius sc_tv.tv_sec = sc->sc_advbase * 3; 694142215Sglebius if (timevalcmp(&sc_tv, &ch_tv, <)) { 695142446Sglebius CARP_DEBUG("%s: BACKUP -> MASTER " 696142452Sglebius "(master timed out)\n", 697142446Sglebius sc->sc_if.if_xname); 698142215Sglebius carp_master_down(sc); 699142215Sglebius break; 700142215Sglebius } 701142215Sglebius 702142215Sglebius /* 703142215Sglebius * Otherwise, we reset the counter and wait for the next 704142215Sglebius * advertisement. 705142215Sglebius */ 706142215Sglebius carp_setrun(sc, af); 707142215Sglebius break; 708142215Sglebius } 709142215Sglebius 710142215Sglebius m_freem(m); 711142215Sglebius return; 712142215Sglebius} 713142215Sglebius 714142215Sglebiusvoid 715142215Sglebiuscarpdetach(struct carp_softc *sc) 716142215Sglebius{ 717142215Sglebius struct ifaddr *ifa; 718142215Sglebius 719142215Sglebius callout_stop(&sc->sc_ad_tmo); 720142215Sglebius callout_stop(&sc->sc_md_tmo); 721142215Sglebius callout_stop(&sc->sc_md6_tmo); 722142215Sglebius 723142215Sglebius while ((ifa = TAILQ_FIRST(&sc->sc_if.if_addrlist)) != NULL) 724142215Sglebius if (ifa->ifa_addr->sa_family == AF_INET) { 725142215Sglebius struct in_ifaddr *ia = ifatoia(ifa); 726142215Sglebius 727142215Sglebius carp_del_addr(sc, &ia->ia_addr); 728142215Sglebius 729142215Sglebius /* ripped screaming from in_control(SIOCDIFADDR) */ 730142215Sglebius in_ifscrub(&sc->sc_if, ia); 731142215Sglebius TAILQ_REMOVE(&sc->sc_if.if_addrlist, ifa, ifa_link); 732142215Sglebius TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link); 733142215Sglebius IFAFREE((&ia->ia_ifa)); 734142215Sglebius } 735142215Sglebius} 736142215Sglebius 737142215Sglebius/* Detach an interface from the carp. */ 738142215Sglebiusvoid 739142215Sglebiuscarp_ifdetach(struct ifnet *ifp) 740142215Sglebius{ 741142215Sglebius struct carp_softc *sc; 742142215Sglebius struct carp_if *cif = (struct carp_if *)ifp->if_carp; 743142215Sglebius 744142215Sglebius CARP_LOCK(cif); 745142215Sglebius TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) 746142215Sglebius carpdetach(sc); 747142215Sglebius CARP_UNLOCK(cif); 748142215Sglebius} 749142215Sglebius 750142215Sglebiusint 751142215Sglebiuscarp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch) 752142215Sglebius{ 753142215Sglebius struct m_tag *mtag; 754142215Sglebius struct ifnet *ifp = &sc->sc_if; 755142215Sglebius 756142215Sglebius if (sc->sc_init_counter) { 757142215Sglebius /* this could also be seconds since unix epoch */ 758142215Sglebius sc->sc_counter = arc4random(); 759142215Sglebius sc->sc_counter = sc->sc_counter << 32; 760142215Sglebius sc->sc_counter += arc4random(); 761142215Sglebius } else 762142215Sglebius sc->sc_counter++; 763142215Sglebius 764142215Sglebius ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff); 765142215Sglebius ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff); 766142215Sglebius 767142215Sglebius carp_hmac_generate(sc, ch->carp_counter, ch->carp_md); 768142215Sglebius 769142215Sglebius /* Tag packet for carp_output */ 770142215Sglebius mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), M_NOWAIT); 771142215Sglebius if (mtag == NULL) { 772142215Sglebius m_freem(m); 773142215Sglebius sc->sc_if.if_oerrors++; 774142215Sglebius return (ENOMEM); 775142215Sglebius } 776142215Sglebius bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *)); 777142215Sglebius m_tag_prepend(m, mtag); 778142215Sglebius 779142215Sglebius return (0); 780142215Sglebius} 781142215Sglebius 782142215Sglebiusvoid 783142215Sglebiuscarp_send_ad_all(void) 784142215Sglebius{ 785142215Sglebius struct ifnet *ifp; 786142215Sglebius struct carp_if *cif; 787142215Sglebius struct carp_softc *vh; 788142215Sglebius 789142215Sglebius TAILQ_FOREACH(ifp, &ifnet, if_list) { 790142215Sglebius if (ifp->if_carp == NULL) 791142215Sglebius continue; 792142215Sglebius 793142215Sglebius cif = (struct carp_if *)ifp->if_carp; 794142215Sglebius CARP_LOCK(cif); 795142215Sglebius TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 796142215Sglebius if ((vh->sc_ac.ac_if.if_flags & (IFF_UP|IFF_RUNNING)) && 797142215Sglebius vh->sc_state == MASTER) 798142215Sglebius carp_send_ad(vh); 799142215Sglebius } 800142215Sglebius CARP_UNLOCK(cif); 801142215Sglebius } 802142215Sglebius} 803142215Sglebius 804142215Sglebiusvoid 805142215Sglebiuscarp_send_ad(void *v) 806142215Sglebius{ 807142215Sglebius struct carp_header ch; 808142215Sglebius struct timeval tv; 809142215Sglebius struct carp_softc *sc = v; 810142215Sglebius struct carp_header *ch_ptr; 811142215Sglebius struct mbuf *m; 812142215Sglebius int len, advbase, advskew; 813142215Sglebius 814142215Sglebius /* bow out if we've lost our UPness or RUNNINGuiness */ 815142215Sglebius if ((sc->sc_if.if_flags & 816142215Sglebius (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 817142215Sglebius advbase = 255; 818142215Sglebius advskew = 255; 819142215Sglebius } else { 820142215Sglebius advbase = sc->sc_advbase; 821142215Sglebius if (!carp_suppress_preempt || sc->sc_advskew > 240) 822142215Sglebius advskew = sc->sc_advskew; 823142215Sglebius else 824142215Sglebius advskew = 240; 825142215Sglebius tv.tv_sec = advbase; 826142215Sglebius tv.tv_usec = advskew * 1000000 / 256; 827142215Sglebius } 828142215Sglebius 829142215Sglebius ch.carp_version = CARP_VERSION; 830142215Sglebius ch.carp_type = CARP_ADVERTISEMENT; 831142215Sglebius ch.carp_vhid = sc->sc_vhid; 832142215Sglebius ch.carp_advbase = advbase; 833142215Sglebius ch.carp_advskew = advskew; 834142215Sglebius ch.carp_authlen = 7; /* XXX DEFINE */ 835142215Sglebius ch.carp_pad1 = 0; /* must be zero */ 836142215Sglebius ch.carp_cksum = 0; 837142215Sglebius 838142215Sglebius#ifdef INET 839142215Sglebius if (sc->sc_ia) { 840142215Sglebius struct ip *ip; 841142215Sglebius 842142215Sglebius MGETHDR(m, M_DONTWAIT, MT_HEADER); 843142215Sglebius if (m == NULL) { 844142215Sglebius sc->sc_ac.ac_if.if_oerrors++; 845142215Sglebius carpstats.carps_onomem++; 846142215Sglebius /* XXX maybe less ? */ 847142215Sglebius if (advbase != 255 || advskew != 255) 848142215Sglebius callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), 849142215Sglebius carp_send_ad, sc); 850142215Sglebius return; 851142215Sglebius } 852142215Sglebius len = sizeof(*ip) + sizeof(ch); 853142215Sglebius m->m_pkthdr.len = len; 854142215Sglebius m->m_pkthdr.rcvif = NULL; 855142215Sglebius m->m_len = len; 856142215Sglebius MH_ALIGN(m, m->m_len); 857142215Sglebius m->m_flags |= M_MCAST; 858142215Sglebius ip = mtod(m, struct ip *); 859142215Sglebius ip->ip_v = IPVERSION; 860142215Sglebius ip->ip_hl = sizeof(*ip) >> 2; 861142215Sglebius ip->ip_tos = IPTOS_LOWDELAY; 862142215Sglebius ip->ip_len = len; 863142215Sglebius ip->ip_id = ip_newid(); 864142215Sglebius ip->ip_off = IP_DF; 865142215Sglebius ip->ip_ttl = CARP_DFLTTL; 866142215Sglebius ip->ip_p = IPPROTO_CARP; 867142215Sglebius ip->ip_sum = 0; 868142215Sglebius ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr; 869142215Sglebius ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP); 870142215Sglebius 871142215Sglebius ch_ptr = (struct carp_header *)(&ip[1]); 872142215Sglebius bcopy(&ch, ch_ptr, sizeof(ch)); 873142215Sglebius if (carp_prepare_ad(m, sc, ch_ptr)) 874142215Sglebius return; 875142215Sglebius 876142215Sglebius m->m_data += sizeof(*ip); 877142215Sglebius ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip)); 878142215Sglebius m->m_data -= sizeof(*ip); 879142215Sglebius 880142215Sglebius getmicrotime(&sc->sc_if.if_lastchange); 881142215Sglebius sc->sc_ac.ac_if.if_opackets++; 882142215Sglebius sc->sc_ac.ac_if.if_obytes += len; 883142215Sglebius carpstats.carps_opackets++; 884142215Sglebius 885142215Sglebius if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) { 886142215Sglebius sc->sc_if.if_oerrors++; 887142215Sglebius if (sc->sc_sendad_errors < INT_MAX) 888142215Sglebius sc->sc_sendad_errors++; 889142215Sglebius if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { 890142215Sglebius carp_suppress_preempt++; 891142215Sglebius if (carp_suppress_preempt == 1) 892142215Sglebius carp_send_ad_all(); 893142215Sglebius } 894142215Sglebius sc->sc_sendad_success = 0; 895142215Sglebius } else { 896142215Sglebius if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { 897142215Sglebius if (++sc->sc_sendad_success >= 898142215Sglebius CARP_SENDAD_MIN_SUCCESS) { 899142215Sglebius carp_suppress_preempt--; 900142215Sglebius sc->sc_sendad_errors = 0; 901142215Sglebius } 902142215Sglebius } else 903142215Sglebius sc->sc_sendad_errors = 0; 904142215Sglebius } 905142215Sglebius } 906142215Sglebius#endif /* INET */ 907142215Sglebius#ifdef INET6 908142215Sglebius if (sc->sc_ia6) { 909142215Sglebius struct ip6_hdr *ip6; 910142215Sglebius 911142215Sglebius MGETHDR(m, M_DONTWAIT, MT_HEADER); 912142215Sglebius if (m == NULL) { 913142215Sglebius sc->sc_ac.ac_if.if_oerrors++; 914142215Sglebius carpstats.carps_onomem++; 915142215Sglebius /* XXX maybe less ? */ 916142215Sglebius if (advbase != 255 || advskew != 255) 917142215Sglebius callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), 918142215Sglebius carp_send_ad, sc); 919142215Sglebius return; 920142215Sglebius } 921142215Sglebius len = sizeof(*ip6) + sizeof(ch); 922142215Sglebius m->m_pkthdr.len = len; 923142215Sglebius m->m_pkthdr.rcvif = NULL; 924142215Sglebius m->m_len = len; 925142215Sglebius MH_ALIGN(m, m->m_len); 926142215Sglebius m->m_flags |= M_MCAST; 927142215Sglebius ip6 = mtod(m, struct ip6_hdr *); 928142215Sglebius bzero(ip6, sizeof(*ip6)); 929142215Sglebius ip6->ip6_vfc |= IPV6_VERSION; 930142215Sglebius ip6->ip6_hlim = CARP_DFLTTL; 931142215Sglebius ip6->ip6_nxt = IPPROTO_CARP; 932142215Sglebius bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src, 933142215Sglebius sizeof(struct in6_addr)); 934142215Sglebius /* set the multicast destination */ 935142215Sglebius 936142215Sglebius ip6->ip6_dst.s6_addr8[0] = 0xff; 937142215Sglebius ip6->ip6_dst.s6_addr8[1] = 0x02; 938142215Sglebius ip6->ip6_dst.s6_addr8[15] = 0x12; 939142215Sglebius 940142215Sglebius ch_ptr = (struct carp_header *)(&ip6[1]); 941142215Sglebius bcopy(&ch, ch_ptr, sizeof(ch)); 942142215Sglebius if (carp_prepare_ad(m, sc, ch_ptr)) 943142215Sglebius return; 944142215Sglebius 945142215Sglebius m->m_data += sizeof(*ip6); 946142215Sglebius ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6)); 947142215Sglebius m->m_data -= sizeof(*ip6); 948142215Sglebius 949142215Sglebius getmicrotime(&sc->sc_if.if_lastchange); 950142215Sglebius sc->sc_if.if_opackets++; 951142215Sglebius sc->sc_if.if_obytes += len; 952142215Sglebius carpstats.carps_opackets6++; 953142215Sglebius 954142215Sglebius if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) { 955142215Sglebius sc->sc_if.if_oerrors++; 956142215Sglebius if (sc->sc_sendad_errors < INT_MAX) 957142215Sglebius sc->sc_sendad_errors++; 958142215Sglebius if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { 959142215Sglebius carp_suppress_preempt++; 960142215Sglebius if (carp_suppress_preempt == 1) 961142215Sglebius carp_send_ad_all(); 962142215Sglebius } 963142215Sglebius sc->sc_sendad_success = 0; 964142215Sglebius } else { 965142215Sglebius if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { 966142215Sglebius if (++sc->sc_sendad_success >= 967142215Sglebius CARP_SENDAD_MIN_SUCCESS) { 968142215Sglebius carp_suppress_preempt--; 969142215Sglebius sc->sc_sendad_errors = 0; 970142215Sglebius } 971142215Sglebius } else 972142215Sglebius sc->sc_sendad_errors = 0; 973142215Sglebius } 974142215Sglebius } 975142215Sglebius#endif /* INET6 */ 976142215Sglebius 977142215Sglebius if (advbase != 255 || advskew != 255) 978142215Sglebius callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), 979142215Sglebius carp_send_ad, sc); 980142215Sglebius 981142215Sglebius} 982142215Sglebius 983142215Sglebius/* 984142215Sglebius * Broadcast a gratuitous ARP request containing 985142215Sglebius * the virtual router MAC address for each IP address 986142215Sglebius * associated with the virtual router. 987142215Sglebius */ 988142215Sglebiusvoid 989142215Sglebiuscarp_send_arp(struct carp_softc *sc) 990142215Sglebius{ 991142215Sglebius struct ifaddr *ifa; 992142215Sglebius 993142215Sglebius TAILQ_FOREACH(ifa, &sc->sc_if.if_addrlist, ifa_list) { 994142215Sglebius 995142215Sglebius if (ifa->ifa_addr->sa_family != AF_INET) 996142215Sglebius continue; 997142215Sglebius 998142215Sglebius/* arprequest(sc->sc_ifp, &in, &in, sc->sc_ac.ac_enaddr); */ 999142215Sglebius arp_ifinit2(sc->sc_ifp, ifa, sc->sc_ac.ac_enaddr); 1000142215Sglebius 1001142215Sglebius DELAY(1000); /* XXX */ 1002142215Sglebius } 1003142215Sglebius} 1004142215Sglebius 1005142215Sglebius#ifdef INET6 1006142215Sglebiusvoid 1007142215Sglebiuscarp_send_na(struct carp_softc *sc) 1008142215Sglebius{ 1009142215Sglebius struct ifaddr *ifa; 1010142215Sglebius struct in6_addr *in6; 1011142215Sglebius static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 1012142215Sglebius 1013142215Sglebius TAILQ_FOREACH(ifa, &sc->sc_if.if_addrlist, ifa_list) { 1014142215Sglebius 1015142215Sglebius if (ifa->ifa_addr->sa_family != AF_INET6) 1016142215Sglebius continue; 1017142215Sglebius 1018142215Sglebius in6 = &ifatoia6(ifa)->ia_addr.sin6_addr; 1019142215Sglebius nd6_na_output(sc->sc_ifp, &mcast, in6, 1020142215Sglebius ND_NA_FLAG_OVERRIDE, 1, NULL); 1021142215Sglebius DELAY(1000); /* XXX */ 1022142215Sglebius } 1023142215Sglebius} 1024142215Sglebius#endif /* INET6 */ 1025142215Sglebius 1026142215Sglebiusint 1027142215Sglebiuscarp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type) 1028142215Sglebius{ 1029142215Sglebius struct carp_softc *vh; 1030142215Sglebius struct ifaddr *ifa; 1031142215Sglebius int count = 0; 1032142215Sglebius 1033142215Sglebius TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1034142215Sglebius if ((type == CARP_COUNT_RUNNING && 1035142215Sglebius (vh->sc_ac.ac_if.if_flags & (IFF_UP|IFF_RUNNING)) == 1036142215Sglebius (IFF_UP|IFF_RUNNING)) || 1037142215Sglebius (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) { 1038142215Sglebius TAILQ_FOREACH(ifa, &vh->sc_ac.ac_if.if_addrlist, 1039142215Sglebius ifa_list) { 1040142215Sglebius if (ifa->ifa_addr->sa_family == AF_INET && 1041142215Sglebius ia->ia_addr.sin_addr.s_addr == 1042142215Sglebius ifatoia(ifa)->ia_addr.sin_addr.s_addr) 1043142215Sglebius count++; 1044142215Sglebius } 1045142215Sglebius } 1046142215Sglebius } 1047142215Sglebius return (count); 1048142215Sglebius} 1049142215Sglebius 1050142215Sglebiusint 1051142215Sglebiuscarp_iamatch(void *v, struct in_ifaddr *ia, 1052142215Sglebius struct in_addr *isaddr, u_int8_t **enaddr) 1053142215Sglebius{ 1054142215Sglebius struct carp_if *cif = v; 1055142215Sglebius struct carp_softc *vh; 1056142215Sglebius int index, count = 0; 1057142215Sglebius struct ifaddr *ifa; 1058142215Sglebius 1059142215Sglebius CARP_LOCK(cif); 1060142215Sglebius 1061142215Sglebius if (carp_opts[CARPCTL_ARPBALANCE]) { 1062142215Sglebius /* 1063142215Sglebius * XXX proof of concept implementation. 1064142215Sglebius * We use the source ip to decide which virtual host should 1065142215Sglebius * handle the request. If we're master of that virtual host, 1066142215Sglebius * then we respond, otherwise, just drop the arp packet on 1067142215Sglebius * the floor. 1068142215Sglebius */ 1069142215Sglebius count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING); 1070142215Sglebius if (count == 0) { 1071142215Sglebius /* should never reach this */ 1072142215Sglebius CARP_UNLOCK(cif); 1073142215Sglebius return (0); 1074142215Sglebius } 1075142215Sglebius 1076142215Sglebius /* this should be a hash, like pf_hash() */ 1077142215Sglebius index = isaddr->s_addr % count; 1078142215Sglebius count = 0; 1079142215Sglebius 1080142215Sglebius TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1081142215Sglebius if ((vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) == 1082142215Sglebius (IFF_UP|IFF_RUNNING)) { 1083142215Sglebius TAILQ_FOREACH(ifa, &vh->sc_if.if_addrlist, 1084142215Sglebius ifa_list) { 1085142215Sglebius if (ifa->ifa_addr->sa_family == 1086142215Sglebius AF_INET && 1087142215Sglebius ia->ia_addr.sin_addr.s_addr == 1088142215Sglebius ifatoia(ifa)->ia_addr.sin_addr.s_addr) { 1089142215Sglebius if (count == index) { 1090142215Sglebius if (vh->sc_state == 1091142215Sglebius MASTER) { 1092142215Sglebius *enaddr = vh->sc_ac.ac_enaddr; 1093142215Sglebius CARP_UNLOCK(cif); 1094142215Sglebius return (1); 1095142215Sglebius } else { 1096142215Sglebius CARP_UNLOCK(cif); 1097142215Sglebius return (0); 1098142215Sglebius } 1099142215Sglebius } 1100142215Sglebius count++; 1101142215Sglebius } 1102142215Sglebius } 1103142215Sglebius } 1104142215Sglebius } 1105142215Sglebius } else { 1106142215Sglebius TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1107142215Sglebius if ((vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) == 1108142215Sglebius (IFF_UP|IFF_RUNNING) && ia->ia_ifp == 1109142215Sglebius &vh->sc_if) { 1110142215Sglebius *enaddr = vh->sc_ac.ac_enaddr; 1111142215Sglebius CARP_UNLOCK(cif); 1112142215Sglebius return (1); 1113142215Sglebius } 1114142215Sglebius } 1115142215Sglebius } 1116142215Sglebius CARP_UNLOCK(cif); 1117142215Sglebius return (0); 1118142215Sglebius} 1119142215Sglebius 1120142215Sglebius#ifdef INET6 1121142215Sglebiusstruct ifaddr * 1122142215Sglebiuscarp_iamatch6(void *v, struct in6_addr *taddr) 1123142215Sglebius{ 1124142215Sglebius struct carp_if *cif = v; 1125142215Sglebius struct carp_softc *vh; 1126142215Sglebius struct ifaddr *ifa; 1127142215Sglebius 1128142215Sglebius CARP_LOCK(cif); 1129142215Sglebius TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1130142215Sglebius TAILQ_FOREACH(ifa, &vh->sc_if.if_addrlist, ifa_list) { 1131142215Sglebius if (IN6_ARE_ADDR_EQUAL(taddr, 1132142215Sglebius &ifatoia6(ifa)->ia_addr.sin6_addr) && 1133142215Sglebius ((vh->sc_if.if_flags & 1134142215Sglebius (IFF_UP|IFF_RUNNING)) == (IFF_UP|IFF_RUNNING))) { 1135142215Sglebius CARP_UNLOCK(cif); 1136142215Sglebius return (ifa); 1137142215Sglebius } 1138142215Sglebius } 1139142215Sglebius } 1140142215Sglebius CARP_UNLOCK(cif); 1141142215Sglebius 1142142215Sglebius return (NULL); 1143142215Sglebius} 1144142215Sglebius 1145142215Sglebiusvoid * 1146142215Sglebiuscarp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr) 1147142215Sglebius{ 1148142215Sglebius struct m_tag *mtag; 1149142215Sglebius struct carp_if *cif = v; 1150142215Sglebius struct carp_softc *sc; 1151142215Sglebius struct ifaddr *ifa; 1152142215Sglebius 1153142215Sglebius CARP_LOCK(cif); 1154142215Sglebius TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) { 1155142215Sglebius TAILQ_FOREACH(ifa, &sc->sc_if.if_addrlist, ifa_list) { 1156142215Sglebius if (IN6_ARE_ADDR_EQUAL(taddr, 1157142215Sglebius &ifatoia6(ifa)->ia_addr.sin6_addr) && 1158142215Sglebius ((sc->sc_if.if_flags & 1159142215Sglebius (IFF_UP|IFF_RUNNING)) == (IFF_UP|IFF_RUNNING))) { 1160142215Sglebius struct ifnet *ifp = &sc->sc_if; 1161142215Sglebius mtag = m_tag_get(PACKET_TAG_CARP, 1162142215Sglebius sizeof(struct ifnet *), M_NOWAIT); 1163142215Sglebius if (mtag == NULL) { 1164142215Sglebius /* better a bit than nothing */ 1165142215Sglebius CARP_UNLOCK(cif); 1166142215Sglebius return (sc->sc_ac.ac_enaddr); 1167142215Sglebius } 1168142215Sglebius bcopy(&ifp, (caddr_t)(mtag + 1), 1169142215Sglebius sizeof(struct ifnet *)); 1170142215Sglebius m_tag_prepend(m, mtag); 1171142215Sglebius 1172142215Sglebius CARP_UNLOCK(cif); 1173142215Sglebius return (sc->sc_ac.ac_enaddr); 1174142215Sglebius } 1175142215Sglebius } 1176142215Sglebius } 1177142215Sglebius CARP_UNLOCK(cif); 1178142215Sglebius 1179142215Sglebius return (NULL); 1180142215Sglebius} 1181142215Sglebius#endif 1182142215Sglebius 1183142215Sglebiusstruct ifnet * 1184142215Sglebiuscarp_forus(void *v, void *dhost) 1185142215Sglebius{ 1186142215Sglebius struct carp_if *cif = v; 1187142215Sglebius struct carp_softc *vh; 1188142215Sglebius u_int8_t *ena = dhost; 1189142215Sglebius 1190142215Sglebius if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1) 1191142215Sglebius return (NULL); 1192142215Sglebius 1193142215Sglebius CARP_LOCK(cif); 1194142215Sglebius TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) 1195142215Sglebius if ((vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) == 1196142215Sglebius (IFF_UP|IFF_RUNNING) && vh->sc_state == MASTER && 1197142215Sglebius !bcmp(dhost, vh->sc_ac.ac_enaddr, ETHER_ADDR_LEN)) { 1198142215Sglebius CARP_UNLOCK(cif); 1199142215Sglebius return (&vh->sc_if); 1200142215Sglebius } 1201142215Sglebius 1202142215Sglebius CARP_UNLOCK(cif); 1203142215Sglebius return (NULL); 1204142215Sglebius} 1205142215Sglebius 1206142215Sglebiusvoid 1207142215Sglebiuscarp_master_down(void *v) 1208142215Sglebius{ 1209142215Sglebius struct carp_softc *sc = v; 1210142215Sglebius 1211142215Sglebius switch (sc->sc_state) { 1212142215Sglebius case INIT: 1213142215Sglebius printf("%s: master_down event in INIT state\n", 1214142215Sglebius sc->sc_if.if_xname); 1215142215Sglebius break; 1216142215Sglebius case MASTER: 1217142215Sglebius break; 1218142215Sglebius case BACKUP: 1219142215Sglebius carp_set_state(sc, MASTER); 1220142215Sglebius carp_send_ad(sc); 1221142215Sglebius carp_send_arp(sc); 1222142215Sglebius#ifdef INET6 1223142215Sglebius carp_send_na(sc); 1224142215Sglebius#endif /* INET6 */ 1225142215Sglebius carp_setrun(sc, 0); 1226142215Sglebius carp_setroute(sc, RTM_ADD); 1227142215Sglebius break; 1228142215Sglebius } 1229142215Sglebius} 1230142215Sglebius 1231142215Sglebius/* 1232142215Sglebius * When in backup state, af indicates whether to reset the master down timer 1233142215Sglebius * for v4 or v6. If it's set to zero, reset the ones which are already pending. 1234142215Sglebius */ 1235142215Sglebiusvoid 1236142215Sglebiuscarp_setrun(struct carp_softc *sc, sa_family_t af) 1237142215Sglebius{ 1238142215Sglebius struct timeval tv; 1239142215Sglebius 1240142215Sglebius if (sc->sc_if.if_flags & IFF_UP && 1241142215Sglebius sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6)) 1242142215Sglebius sc->sc_if.if_flags |= IFF_RUNNING; 1243142215Sglebius else { 1244142215Sglebius sc->sc_if.if_flags &= ~IFF_RUNNING; 1245142215Sglebius carp_setroute(sc, RTM_DELETE); 1246142215Sglebius return; 1247142215Sglebius } 1248142215Sglebius 1249142215Sglebius switch (sc->sc_state) { 1250142215Sglebius case INIT: 1251142215Sglebius if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) { 1252142215Sglebius carp_send_ad(sc); 1253142215Sglebius carp_send_arp(sc); 1254142215Sglebius#ifdef INET6 1255142215Sglebius carp_send_na(sc); 1256142215Sglebius#endif /* INET6 */ 1257142452Sglebius CARP_DEBUG("%s: INIT -> MASTER (preempting)\n", 1258142446Sglebius sc->sc_if.if_xname); 1259142215Sglebius carp_set_state(sc, MASTER); 1260142215Sglebius carp_setroute(sc, RTM_ADD); 1261142215Sglebius } else { 1262142452Sglebius CARP_DEBUG("%s: INIT -> BACKUP\n", sc->sc_if.if_xname); 1263142215Sglebius carp_set_state(sc, BACKUP); 1264142215Sglebius carp_setroute(sc, RTM_DELETE); 1265142215Sglebius carp_setrun(sc, 0); 1266142215Sglebius } 1267142215Sglebius break; 1268142215Sglebius case BACKUP: 1269142215Sglebius callout_stop(&sc->sc_ad_tmo); 1270142215Sglebius tv.tv_sec = 3 * sc->sc_advbase; 1271142215Sglebius tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1272142215Sglebius switch (af) { 1273142215Sglebius#ifdef INET 1274142215Sglebius case AF_INET: 1275142215Sglebius callout_reset(&sc->sc_md_tmo, tvtohz(&tv), 1276142215Sglebius carp_master_down, sc); 1277142215Sglebius break; 1278142215Sglebius#endif /* INET */ 1279142215Sglebius#ifdef INET6 1280142215Sglebius case AF_INET6: 1281142215Sglebius callout_reset(&sc->sc_md6_tmo, tvtohz(&tv), 1282142215Sglebius carp_master_down, sc); 1283142215Sglebius break; 1284142215Sglebius#endif /* INET6 */ 1285142215Sglebius default: 1286142215Sglebius if (sc->sc_naddrs) 1287142215Sglebius callout_reset(&sc->sc_md_tmo, tvtohz(&tv), 1288142215Sglebius carp_master_down, sc); 1289142215Sglebius if (sc->sc_naddrs6) 1290142215Sglebius callout_reset(&sc->sc_md6_tmo, tvtohz(&tv), 1291142215Sglebius carp_master_down, sc); 1292142215Sglebius break; 1293142215Sglebius } 1294142215Sglebius break; 1295142215Sglebius case MASTER: 1296142215Sglebius tv.tv_sec = sc->sc_advbase; 1297142215Sglebius tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1298142215Sglebius callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), 1299142215Sglebius carp_send_ad, sc); 1300142215Sglebius break; 1301142215Sglebius } 1302142215Sglebius} 1303142215Sglebius 1304142215Sglebiusint 1305142215Sglebiuscarp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin) 1306142215Sglebius{ 1307142215Sglebius struct ifnet *ifp; 1308142215Sglebius struct carp_if *cif; 1309142215Sglebius struct in_ifaddr *ia, *ia_if; 1310142215Sglebius struct ip_moptions *imo = &sc->sc_imo; 1311142215Sglebius struct in_addr addr; 1312142215Sglebius u_long iaddr = htonl(sin->sin_addr.s_addr); 1313142215Sglebius int own, error; 1314142215Sglebius 1315142215Sglebius if (sin->sin_addr.s_addr == 0) { 1316142215Sglebius if (!(sc->sc_if.if_flags & IFF_UP)) 1317142215Sglebius carp_set_state(sc, INIT); 1318142215Sglebius if (sc->sc_naddrs) 1319142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1320142215Sglebius carp_setrun(sc, 0); 1321142215Sglebius return (0); 1322142215Sglebius } 1323142215Sglebius 1324142215Sglebius /* we have to do it by hands to check we won't match on us */ 1325142215Sglebius ia_if = NULL; own = 0; 1326142215Sglebius TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 1327142215Sglebius /* and, yeah, we need a multicast-capable iface too */ 1328142215Sglebius if (ia->ia_ifp != &sc->sc_if && 1329142215Sglebius (ia->ia_ifp->if_flags & IFF_MULTICAST) && 1330142215Sglebius (iaddr & ia->ia_subnetmask) == ia->ia_subnet) { 1331142215Sglebius if (!ia_if) 1332142215Sglebius ia_if = ia; 1333142215Sglebius if (sin->sin_addr.s_addr == 1334142215Sglebius ia->ia_addr.sin_addr.s_addr) 1335142215Sglebius own++; 1336142215Sglebius } 1337142215Sglebius } 1338142215Sglebius 1339142215Sglebius if (!ia_if) 1340142215Sglebius return (EADDRNOTAVAIL); 1341142215Sglebius 1342142215Sglebius ia = ia_if; 1343142215Sglebius ifp = ia->ia_ifp; 1344142215Sglebius 1345142215Sglebius if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 || 1346142215Sglebius (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp)) 1347142215Sglebius return (EADDRNOTAVAIL); 1348142215Sglebius 1349142215Sglebius if (imo->imo_num_memberships == 0) { 1350142215Sglebius addr.s_addr = htonl(INADDR_CARP_GROUP); 1351142215Sglebius if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL) 1352142215Sglebius return (ENOBUFS); 1353142215Sglebius imo->imo_num_memberships++; 1354142215Sglebius imo->imo_multicast_ifp = ifp; 1355142215Sglebius imo->imo_multicast_ttl = CARP_DFLTTL; 1356142215Sglebius imo->imo_multicast_loop = 0; 1357142215Sglebius } 1358142215Sglebius 1359142215Sglebius if (!ifp->if_carp) { 1360142215Sglebius 1361142215Sglebius MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP, 1362142215Sglebius M_WAITOK|M_ZERO); 1363142215Sglebius if (!cif) { 1364142215Sglebius error = ENOBUFS; 1365142215Sglebius goto cleanup; 1366142215Sglebius } 1367142215Sglebius if ((error = ifpromisc(ifp, 1))) { 1368142215Sglebius FREE(cif, M_CARP); 1369142215Sglebius goto cleanup; 1370142215Sglebius } 1371142215Sglebius 1372142215Sglebius CARP_LOCK_INIT(cif); 1373142215Sglebius CARP_LOCK(cif); 1374142215Sglebius cif->vhif_ifp = ifp; 1375142215Sglebius TAILQ_INIT(&cif->vhif_vrs); 1376142215Sglebius ifp->if_carp = cif; 1377142215Sglebius 1378142215Sglebius } else { 1379142215Sglebius struct carp_softc *vr; 1380142215Sglebius 1381142215Sglebius cif = (struct carp_if *)ifp->if_carp; 1382142215Sglebius CARP_LOCK(cif); 1383142215Sglebius TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) 1384142215Sglebius if (vr != sc && vr->sc_vhid == sc->sc_vhid) { 1385142215Sglebius CARP_UNLOCK(cif); 1386142215Sglebius error = EINVAL; 1387142215Sglebius goto cleanup; 1388142215Sglebius } 1389142215Sglebius } 1390142215Sglebius sc->sc_ia = ia; 1391142215Sglebius sc->sc_ifp = ifp; 1392142215Sglebius 1393142215Sglebius { /* XXX prevent endless loop if already in queue */ 1394142215Sglebius struct carp_softc *vr, *after = NULL; 1395142215Sglebius int myself = 0; 1396142215Sglebius cif = (struct carp_if *)ifp->if_carp; 1397142215Sglebius 1398142215Sglebius /* XXX: cif should not change, right? So we still hold the lock */ 1399142215Sglebius CARP_LOCK_ASSERT(cif); 1400142215Sglebius 1401142215Sglebius TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) { 1402142215Sglebius if (vr == sc) 1403142215Sglebius myself = 1; 1404142215Sglebius if (vr->sc_vhid < sc->sc_vhid) 1405142215Sglebius after = vr; 1406142215Sglebius } 1407142215Sglebius 1408142215Sglebius if (!myself) { 1409142215Sglebius /* We're trying to keep things in order */ 1410142215Sglebius if (after == NULL) { 1411142215Sglebius TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list); 1412142215Sglebius } else { 1413142215Sglebius TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list); 1414142215Sglebius } 1415142215Sglebius cif->vhif_nvrs++; 1416142215Sglebius } 1417142215Sglebius } 1418142215Sglebius 1419142215Sglebius CARP_UNLOCK(cif); 1420142215Sglebius 1421142215Sglebius sc->sc_naddrs++; 1422142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1423142215Sglebius if (own) 1424142215Sglebius sc->sc_advskew = 0; 1425142371Sglebius carp_carpdev_state(cif); 1426142215Sglebius carp_setrun(sc, 0); 1427142215Sglebius 1428142215Sglebius return (0); 1429142215Sglebius 1430142215Sglebiuscleanup: 1431142215Sglebius in_delmulti(imo->imo_membership[--imo->imo_num_memberships]); 1432142215Sglebius return (error); 1433142215Sglebius} 1434142215Sglebius 1435142215Sglebiusint 1436142215Sglebiuscarp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin) 1437142215Sglebius{ 1438142215Sglebius int error = 0; 1439142215Sglebius 1440142215Sglebius if (!--sc->sc_naddrs) { 1441142215Sglebius struct carp_if *cif = (struct carp_if *)sc->sc_ifp->if_carp; 1442142215Sglebius struct ip_moptions *imo = &sc->sc_imo; 1443142215Sglebius 1444142215Sglebius callout_stop(&sc->sc_ad_tmo); 1445142215Sglebius sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING); 1446142215Sglebius sc->sc_vhid = -1; 1447142215Sglebius in_delmulti(imo->imo_membership[--imo->imo_num_memberships]); 1448142215Sglebius imo->imo_multicast_ifp = NULL; 1449142215Sglebius CARP_LOCK(cif); 1450142215Sglebius TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list); 1451142215Sglebius if (!--cif->vhif_nvrs) { 1452142215Sglebius sc->sc_ifp->if_carp = NULL; 1453142215Sglebius CARP_LOCK_DESTROY(cif); 1454142215Sglebius FREE(cif, M_IFADDR); 1455142215Sglebius } else { 1456142215Sglebius CARP_UNLOCK(cif); 1457142215Sglebius } 1458142215Sglebius } 1459142215Sglebius 1460142215Sglebius return (error); 1461142215Sglebius} 1462142215Sglebius 1463142215Sglebius#ifdef INET6 1464142215Sglebiusint 1465142215Sglebiuscarp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6) 1466142215Sglebius{ 1467142215Sglebius struct ifnet *ifp; 1468142215Sglebius struct carp_if *cif; 1469142215Sglebius struct in6_ifaddr *ia, *ia_if; 1470142215Sglebius struct ip6_moptions *im6o = &sc->sc_im6o; 1471142215Sglebius struct in6_multi_mship *imm; 1472142215Sglebius struct sockaddr_in6 addr; 1473142215Sglebius int own, error; 1474142215Sglebius 1475142215Sglebius if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1476142215Sglebius if (!(sc->sc_if.if_flags & IFF_UP)) 1477142215Sglebius carp_set_state(sc, INIT); 1478142215Sglebius if (sc->sc_naddrs6) 1479142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1480142215Sglebius carp_setrun(sc, 0); 1481142215Sglebius return (0); 1482142215Sglebius } 1483142215Sglebius 1484142215Sglebius /* we have to do it by hands to check we won't match on us */ 1485142215Sglebius ia_if = NULL; own = 0; 1486142215Sglebius for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 1487142215Sglebius int i; 1488142215Sglebius 1489142215Sglebius for (i = 0; i < 4; i++) { 1490142215Sglebius if ((sin6->sin6_addr.s6_addr32[i] & 1491142215Sglebius ia->ia_prefixmask.sin6_addr.s6_addr32[i]) != 1492142215Sglebius (ia->ia_addr.sin6_addr.s6_addr32[i] & 1493142215Sglebius ia->ia_prefixmask.sin6_addr.s6_addr32[i])) 1494142215Sglebius break; 1495142215Sglebius } 1496142215Sglebius /* and, yeah, we need a multicast-capable iface too */ 1497142215Sglebius if (ia->ia_ifp != &sc->sc_ac.ac_if && 1498142215Sglebius (ia->ia_ifp->if_flags & IFF_MULTICAST) && 1499142215Sglebius (i == 4)) { 1500142215Sglebius if (!ia_if) 1501142215Sglebius ia_if = ia; 1502142215Sglebius if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 1503142215Sglebius &ia->ia_addr.sin6_addr)) 1504142215Sglebius own++; 1505142215Sglebius } 1506142215Sglebius } 1507142215Sglebius 1508142215Sglebius if (!ia_if) 1509142215Sglebius return (EADDRNOTAVAIL); 1510142215Sglebius ia = ia_if; 1511142215Sglebius ifp = ia->ia_ifp; 1512142215Sglebius 1513142215Sglebius if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 || 1514142215Sglebius (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp)) 1515142215Sglebius return (EADDRNOTAVAIL); 1516142215Sglebius 1517142215Sglebius if (!sc->sc_naddrs6) { 1518142215Sglebius im6o->im6o_multicast_ifp = ifp; 1519142215Sglebius 1520142215Sglebius /* join CARP multicast address */ 1521142215Sglebius bzero(&addr, sizeof(addr)); 1522142215Sglebius addr.sin6_family = AF_INET6; 1523142215Sglebius addr.sin6_len = sizeof(addr); 1524142215Sglebius addr.sin6_addr.s6_addr16[0] = htons(0xff02); 1525142215Sglebius addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 1526142215Sglebius addr.sin6_addr.s6_addr8[15] = 0x12; 1527142215Sglebius if ((imm = in6_joingroup(ifp, &addr.sin6_addr, &error)) == NULL) 1528142215Sglebius goto cleanup; 1529142215Sglebius LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 1530142215Sglebius 1531142215Sglebius /* join solicited multicast address */ 1532142215Sglebius bzero(&addr.sin6_addr, sizeof(addr.sin6_addr)); 1533142215Sglebius addr.sin6_addr.s6_addr16[0] = htons(0xff02); 1534142215Sglebius addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 1535142215Sglebius addr.sin6_addr.s6_addr32[1] = 0; 1536142215Sglebius addr.sin6_addr.s6_addr32[2] = htonl(1); 1537142215Sglebius addr.sin6_addr.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3]; 1538142215Sglebius addr.sin6_addr.s6_addr8[12] = 0xff; 1539142215Sglebius if ((imm = in6_joingroup(ifp, &addr.sin6_addr, &error)) == NULL) 1540142215Sglebius goto cleanup; 1541142215Sglebius LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 1542142215Sglebius } 1543142215Sglebius 1544142215Sglebius if (!ifp->if_carp) { 1545142215Sglebius MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP, 1546142215Sglebius M_WAITOK|M_ZERO); 1547142215Sglebius if (!cif) { 1548142215Sglebius error = ENOBUFS; 1549142215Sglebius goto cleanup; 1550142215Sglebius } 1551142215Sglebius if ((error = ifpromisc(ifp, 1))) { 1552142215Sglebius FREE(cif, M_CARP); 1553142215Sglebius goto cleanup; 1554142215Sglebius } 1555142215Sglebius 1556142215Sglebius CARP_LOCK_INIT(cif); 1557142215Sglebius CARP_LOCK(cif); 1558142215Sglebius cif->vhif_ifp = ifp; 1559142215Sglebius TAILQ_INIT(&cif->vhif_vrs); 1560142215Sglebius ifp->if_carp = cif; 1561142215Sglebius 1562142215Sglebius } else { 1563142215Sglebius struct carp_softc *vr; 1564142215Sglebius 1565142215Sglebius cif = (struct carp_if *)ifp->if_carp; 1566142215Sglebius CARP_LOCK(cif); 1567142215Sglebius TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) 1568142215Sglebius if (vr != sc && vr->sc_vhid == sc->sc_vhid) { 1569142215Sglebius CARP_UNLOCK(cif); 1570142215Sglebius error = EINVAL; 1571142215Sglebius goto cleanup; 1572142215Sglebius } 1573142215Sglebius } 1574142215Sglebius sc->sc_ia6 = ia; 1575142215Sglebius sc->sc_ifp = ifp; 1576142215Sglebius 1577142215Sglebius { /* XXX prevent endless loop if already in queue */ 1578142215Sglebius struct carp_softc *vr, *after = NULL; 1579142215Sglebius int myself = 0; 1580142215Sglebius cif = (struct carp_if *)ifp->if_carp; 1581142215Sglebius CARP_LOCK_ASSERT(cif); 1582142215Sglebius 1583142215Sglebius TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) { 1584142215Sglebius if (vr == sc) 1585142215Sglebius myself = 1; 1586142215Sglebius if (vr->sc_vhid < sc->sc_vhid) 1587142215Sglebius after = vr; 1588142215Sglebius } 1589142215Sglebius 1590142215Sglebius if (!myself) { 1591142215Sglebius /* We're trying to keep things in order */ 1592142215Sglebius if (after == NULL) { 1593142215Sglebius TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list); 1594142215Sglebius } else { 1595142215Sglebius TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list); 1596142215Sglebius } 1597142215Sglebius cif->vhif_nvrs++; 1598142215Sglebius } 1599142215Sglebius } 1600142215Sglebius 1601142215Sglebius CARP_UNLOCK(cif); 1602142215Sglebius sc->sc_naddrs6++; 1603142215Sglebius sc->sc_ac.ac_if.if_flags |= IFF_UP; 1604142215Sglebius if (own) 1605142215Sglebius sc->sc_advskew = 0; 1606142447Sglebius carp_carpdev_state(cif); 1607142215Sglebius carp_setrun(sc, 0); 1608142215Sglebius 1609142215Sglebius return (0); 1610142215Sglebius 1611142215Sglebiuscleanup: 1612142215Sglebius /* clean up multicast memberships */ 1613142215Sglebius if (!sc->sc_naddrs6) { 1614142215Sglebius while (!LIST_EMPTY(&im6o->im6o_memberships)) { 1615142215Sglebius imm = LIST_FIRST(&im6o->im6o_memberships); 1616142215Sglebius LIST_REMOVE(imm, i6mm_chain); 1617142215Sglebius in6_leavegroup(imm); 1618142215Sglebius } 1619142215Sglebius } 1620142215Sglebius return (error); 1621142215Sglebius} 1622142215Sglebius 1623142215Sglebiusint 1624142215Sglebiuscarp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6) 1625142215Sglebius{ 1626142215Sglebius int error = 0; 1627142215Sglebius 1628142215Sglebius if (!--sc->sc_naddrs6) { 1629142215Sglebius struct carp_if *cif = (struct carp_if *)sc->sc_ifp->if_carp; 1630142215Sglebius struct ip6_moptions *im6o = &sc->sc_im6o; 1631142215Sglebius 1632142215Sglebius callout_stop(&sc->sc_ad_tmo); 1633142215Sglebius sc->sc_ac.ac_if.if_flags &= ~(IFF_UP|IFF_RUNNING); 1634142215Sglebius sc->sc_vhid = -1; 1635142215Sglebius CARP_LOCK(cif); 1636142215Sglebius while (!LIST_EMPTY(&im6o->im6o_memberships)) { 1637142215Sglebius struct in6_multi_mship *imm = 1638142215Sglebius LIST_FIRST(&im6o->im6o_memberships); 1639142215Sglebius 1640142215Sglebius LIST_REMOVE(imm, i6mm_chain); 1641142215Sglebius in6_leavegroup(imm); 1642142215Sglebius } 1643142215Sglebius im6o->im6o_multicast_ifp = NULL; 1644142215Sglebius TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list); 1645142215Sglebius if (!--cif->vhif_nvrs) { 1646142215Sglebius CARP_LOCK_DESTROY(cif); 1647142215Sglebius sc->sc_ifp->if_carp = NULL; 1648142215Sglebius FREE(cif, M_IFADDR); 1649142215Sglebius } else 1650142215Sglebius CARP_UNLOCK(cif); 1651142215Sglebius } 1652142215Sglebius 1653142215Sglebius return (error); 1654142215Sglebius} 1655142215Sglebius#endif /* INET6 */ 1656142215Sglebius 1657142215Sglebiusint 1658142215Sglebiuscarp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr) 1659142215Sglebius{ 1660142215Sglebius struct carp_softc *sc = ifp->if_softc, *vr; 1661142215Sglebius struct carpreq carpr; 1662142215Sglebius struct ifaddr *ifa; 1663142215Sglebius struct ifreq *ifr; 1664142215Sglebius struct ifaliasreq *ifra; 1665142215Sglebius int error = 0; 1666142215Sglebius 1667142215Sglebius ifa = (struct ifaddr *)addr; 1668142215Sglebius ifra = (struct ifaliasreq *)addr; 1669142215Sglebius ifr = (struct ifreq *)addr; 1670142215Sglebius 1671142215Sglebius switch (cmd) { 1672142215Sglebius case SIOCSIFADDR: 1673142215Sglebius switch (ifa->ifa_addr->sa_family) { 1674142215Sglebius#ifdef INET 1675142215Sglebius case AF_INET: 1676142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1677142215Sglebius bcopy(ifa->ifa_addr, ifa->ifa_dstaddr, 1678142215Sglebius sizeof(struct sockaddr)); 1679142215Sglebius error = carp_set_addr(sc, satosin(ifa->ifa_addr)); 1680142215Sglebius break; 1681142215Sglebius#endif /* INET */ 1682142215Sglebius#ifdef INET6 1683142215Sglebius case AF_INET6: 1684142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1685142215Sglebius error = carp_set_addr6(sc, satosin6(ifa->ifa_addr)); 1686142215Sglebius break; 1687142215Sglebius#endif /* INET6 */ 1688142215Sglebius default: 1689142215Sglebius error = EAFNOSUPPORT; 1690142215Sglebius break; 1691142215Sglebius } 1692142215Sglebius break; 1693142215Sglebius 1694142215Sglebius case SIOCAIFADDR: 1695142215Sglebius switch (ifa->ifa_addr->sa_family) { 1696142215Sglebius#ifdef INET 1697142215Sglebius case AF_INET: 1698142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1699142215Sglebius bcopy(ifa->ifa_addr, ifa->ifa_dstaddr, 1700142215Sglebius sizeof(struct sockaddr)); 1701142215Sglebius error = carp_set_addr(sc, satosin(&ifra->ifra_addr)); 1702142215Sglebius break; 1703142215Sglebius#endif /* INET */ 1704142215Sglebius#ifdef INET6 1705142215Sglebius case AF_INET6: 1706142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1707142215Sglebius error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr)); 1708142215Sglebius break; 1709142215Sglebius#endif /* INET6 */ 1710142215Sglebius default: 1711142215Sglebius error = EAFNOSUPPORT; 1712142215Sglebius break; 1713142215Sglebius } 1714142215Sglebius break; 1715142215Sglebius 1716142215Sglebius case SIOCDIFADDR: 1717142215Sglebius sc->if_flags &= ~IFF_UP; 1718142215Sglebius switch (ifa->ifa_addr->sa_family) { 1719142215Sglebius#ifdef INET 1720142215Sglebius case AF_INET: 1721142215Sglebius error = carp_del_addr(sc, satosin(&ifra->ifra_addr)); 1722142215Sglebius break; 1723142215Sglebius#endif /* INET */ 1724142215Sglebius#ifdef INET6 1725142215Sglebius case AF_INET6: 1726142215Sglebius error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr)); 1727142215Sglebius break; 1728142215Sglebius#endif /* INET6 */ 1729142215Sglebius default: 1730142215Sglebius error = EAFNOSUPPORT; 1731142215Sglebius break; 1732142215Sglebius } 1733142215Sglebius break; 1734142215Sglebius 1735142215Sglebius case SIOCSIFFLAGS: 1736142215Sglebius if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) { 1737142215Sglebius sc->if_flags &= ~IFF_UP; 1738142215Sglebius callout_stop(&sc->sc_ad_tmo); 1739142215Sglebius callout_stop(&sc->sc_md_tmo); 1740142215Sglebius callout_stop(&sc->sc_md6_tmo); 1741142215Sglebius if (sc->sc_state == MASTER) 1742142215Sglebius carp_send_ad(sc); 1743142215Sglebius carp_set_state(sc, INIT); 1744142215Sglebius carp_setrun(sc, 0); 1745142215Sglebius } else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) { 1746142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1747142215Sglebius carp_setrun(sc, 0); 1748142215Sglebius } 1749142215Sglebius break; 1750142215Sglebius 1751142215Sglebius case SIOCSVH: 1752142215Sglebius if ((error = suser(curthread)) != 0) 1753142215Sglebius break; 1754142215Sglebius if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr))) 1755142215Sglebius break; 1756142215Sglebius error = 1; 1757142215Sglebius if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) { 1758142215Sglebius switch (carpr.carpr_state) { 1759142215Sglebius case BACKUP: 1760142215Sglebius callout_stop(&sc->sc_ad_tmo); 1761142215Sglebius carp_set_state(sc, BACKUP); 1762142215Sglebius carp_setrun(sc, 0); 1763142215Sglebius carp_setroute(sc, RTM_DELETE); 1764142215Sglebius break; 1765142215Sglebius case MASTER: 1766142215Sglebius carp_master_down(sc); 1767142215Sglebius break; 1768142215Sglebius default: 1769142215Sglebius break; 1770142215Sglebius } 1771142215Sglebius } 1772142215Sglebius if (carpr.carpr_vhid > 0) { 1773142215Sglebius if (carpr.carpr_vhid > 255) { 1774142215Sglebius error = EINVAL; 1775142215Sglebius break; 1776142215Sglebius } 1777142215Sglebius if (sc->sc_ifp) { 1778142215Sglebius struct carp_if *cif; 1779142215Sglebius cif = (struct carp_if *)sc->sc_ifp->if_carp; 1780142215Sglebius CARP_LOCK(cif); 1781142215Sglebius TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) 1782142215Sglebius if (vr != sc && 1783142215Sglebius vr->sc_vhid == carpr.carpr_vhid) { 1784142215Sglebius CARP_UNLOCK(cif); 1785142215Sglebius return EINVAL; 1786142215Sglebius } 1787142215Sglebius CARP_UNLOCK(cif); 1788142215Sglebius } 1789142215Sglebius sc->sc_vhid = carpr.carpr_vhid; 1790142215Sglebius sc->sc_ac.ac_enaddr[0] = 0; 1791142215Sglebius sc->sc_ac.ac_enaddr[1] = 0; 1792142215Sglebius sc->sc_ac.ac_enaddr[2] = 0x5e; 1793142215Sglebius sc->sc_ac.ac_enaddr[3] = 0; 1794142215Sglebius sc->sc_ac.ac_enaddr[4] = 1; 1795142215Sglebius sc->sc_ac.ac_enaddr[5] = sc->sc_vhid; 1796142215Sglebius error--; 1797142215Sglebius } 1798142215Sglebius if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) { 1799142215Sglebius if (carpr.carpr_advskew >= 255) { 1800142215Sglebius error = EINVAL; 1801142215Sglebius break; 1802142215Sglebius } 1803142215Sglebius if (carpr.carpr_advbase > 255) { 1804142215Sglebius error = EINVAL; 1805142215Sglebius break; 1806142215Sglebius } 1807142215Sglebius sc->sc_advbase = carpr.carpr_advbase; 1808142215Sglebius sc->sc_advskew = carpr.carpr_advskew; 1809142215Sglebius error--; 1810142215Sglebius } 1811142215Sglebius bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key)); 1812142215Sglebius if (error > 0) 1813142215Sglebius error = EINVAL; 1814142215Sglebius else { 1815142215Sglebius error = 0; 1816142215Sglebius carp_setrun(sc, 0); 1817142215Sglebius } 1818142215Sglebius break; 1819142215Sglebius 1820142215Sglebius case SIOCGVH: 1821142215Sglebius bzero(&carpr, sizeof(carpr)); 1822142215Sglebius carpr.carpr_state = sc->sc_state; 1823142215Sglebius carpr.carpr_vhid = sc->sc_vhid; 1824142215Sglebius carpr.carpr_advbase = sc->sc_advbase; 1825142215Sglebius carpr.carpr_advskew = sc->sc_advskew; 1826142215Sglebius if (suser(curthread) == 0) 1827142215Sglebius bcopy(sc->sc_key, carpr.carpr_key, 1828142215Sglebius sizeof(carpr.carpr_key)); 1829142215Sglebius error = copyout(&carpr, ifr->ifr_data, sizeof(carpr)); 1830142215Sglebius break; 1831142215Sglebius 1832142215Sglebius default: 1833142215Sglebius error = EINVAL; 1834142215Sglebius } 1835142215Sglebius 1836142215Sglebius carp_hmac_prepare(sc); 1837142215Sglebius return (error); 1838142215Sglebius} 1839142215Sglebius 1840142215Sglebius/* 1841142215Sglebius * XXX: this is looutput. We should eventually use it from there. 1842142215Sglebius */ 1843142215Sglebiusstatic int 1844142215Sglebiuscarp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 1845142215Sglebius struct rtentry *rt) 1846142215Sglebius{ 1847142215Sglebius M_ASSERTPKTHDR(m); /* check if we have the packet header */ 1848142215Sglebius 1849142215Sglebius if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 1850142215Sglebius m_freem(m); 1851142215Sglebius return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 1852142215Sglebius rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 1853142215Sglebius } 1854142215Sglebius 1855142215Sglebius ifp->if_opackets++; 1856142215Sglebius ifp->if_obytes += m->m_pkthdr.len; 1857142215Sglebius#if 1 /* XXX */ 1858142215Sglebius switch (dst->sa_family) { 1859142215Sglebius case AF_INET: 1860142215Sglebius case AF_INET6: 1861142215Sglebius case AF_IPX: 1862142215Sglebius case AF_APPLETALK: 1863142215Sglebius break; 1864142215Sglebius default: 1865142215Sglebius printf("carp_looutput: af=%d unexpected\n", dst->sa_family); 1866142215Sglebius m_freem(m); 1867142215Sglebius return (EAFNOSUPPORT); 1868142215Sglebius } 1869142215Sglebius#endif 1870142215Sglebius return(if_simloop(ifp, m, dst->sa_family, 0)); 1871142215Sglebius} 1872142215Sglebius 1873142215Sglebius/* 1874142215Sglebius * Start output on carp interface. This function should never be called. 1875142215Sglebius */ 1876142215Sglebiusvoid 1877142215Sglebiuscarp_start(struct ifnet *ifp) 1878142215Sglebius{ 1879142215Sglebius#ifdef DEBUG 1880142215Sglebius printf("%s: start called\n", ifp->if_xname); 1881142215Sglebius#endif 1882142215Sglebius} 1883142215Sglebius 1884142215Sglebiusint 1885142215Sglebiuscarp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa, 1886142215Sglebius struct rtentry *rt) 1887142215Sglebius{ 1888142215Sglebius struct m_tag *mtag; 1889142215Sglebius struct carp_softc *sc; 1890142215Sglebius struct ifnet *carp_ifp; 1891142215Sglebius 1892142215Sglebius if (!sa) 1893142215Sglebius return (0); 1894142215Sglebius 1895142215Sglebius switch (sa->sa_family) { 1896142215Sglebius#ifdef INET 1897142215Sglebius case AF_INET: 1898142215Sglebius break; 1899142215Sglebius#endif /* INET */ 1900142215Sglebius#ifdef INET6 1901142215Sglebius case AF_INET6: 1902142215Sglebius break; 1903142215Sglebius#endif /* INET6 */ 1904142215Sglebius default: 1905142215Sglebius return (0); 1906142215Sglebius } 1907142215Sglebius 1908142215Sglebius mtag = m_tag_find(m, PACKET_TAG_CARP, NULL); 1909142215Sglebius if (mtag == NULL) 1910142215Sglebius return (0); 1911142215Sglebius 1912142215Sglebius bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *)); 1913142215Sglebius sc = carp_ifp->if_softc; 1914142215Sglebius 1915142215Sglebius /* Set the source MAC address to Virtual Router MAC Address */ 1916142215Sglebius switch (ifp->if_type) { 1917142215Sglebius case IFT_ETHER: { 1918142215Sglebius struct ether_header *eh; 1919142215Sglebius 1920142215Sglebius eh = mtod(m, struct ether_header *); 1921142215Sglebius eh->ether_shost[0] = 0; 1922142215Sglebius eh->ether_shost[1] = 0; 1923142215Sglebius eh->ether_shost[2] = 0x5e; 1924142215Sglebius eh->ether_shost[3] = 0; 1925142215Sglebius eh->ether_shost[4] = 1; 1926142215Sglebius eh->ether_shost[5] = sc->sc_vhid; 1927142215Sglebius } 1928142215Sglebius break; 1929142215Sglebius case IFT_FDDI: { 1930142215Sglebius struct fddi_header *fh; 1931142215Sglebius 1932142215Sglebius fh = mtod(m, struct fddi_header *); 1933142215Sglebius fh->fddi_shost[0] = 0; 1934142215Sglebius fh->fddi_shost[1] = 0; 1935142215Sglebius fh->fddi_shost[2] = 0x5e; 1936142215Sglebius fh->fddi_shost[3] = 0; 1937142215Sglebius fh->fddi_shost[4] = 1; 1938142215Sglebius fh->fddi_shost[5] = sc->sc_vhid; 1939142215Sglebius } 1940142215Sglebius break; 1941142215Sglebius case IFT_ISO88025: { 1942142215Sglebius struct iso88025_header *th; 1943142215Sglebius th = mtod(m, struct iso88025_header *); 1944142215Sglebius th->iso88025_shost[0] = 3; 1945142215Sglebius th->iso88025_shost[1] = 0; 1946142215Sglebius th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1); 1947142215Sglebius th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1); 1948142215Sglebius th->iso88025_shost[4] = 0; 1949142215Sglebius th->iso88025_shost[5] = 0; 1950142215Sglebius } 1951142215Sglebius break; 1952142215Sglebius default: 1953142215Sglebius printf("%s: carp is not supported for this interface type\n", 1954142215Sglebius ifp->if_xname); 1955142215Sglebius return (EOPNOTSUPP); 1956142215Sglebius } 1957142215Sglebius 1958142215Sglebius return (0); 1959142215Sglebius} 1960142215Sglebius 1961142215Sglebiusvoid 1962142215Sglebiuscarp_set_state(struct carp_softc *sc, int state) 1963142215Sglebius{ 1964142215Sglebius if (sc->sc_state == state) 1965142215Sglebius return; 1966142215Sglebius 1967142215Sglebius sc->sc_state = state; 1968142215Sglebius switch (state) { 1969142215Sglebius case BACKUP: 1970142215Sglebius sc->sc_ac.ac_if.if_link_state = LINK_STATE_DOWN; 1971142215Sglebius break; 1972142215Sglebius case MASTER: 1973142215Sglebius sc->sc_ac.ac_if.if_link_state = LINK_STATE_UP; 1974142215Sglebius break; 1975142215Sglebius default: 1976142215Sglebius sc->sc_ac.ac_if.if_link_state = LINK_STATE_UNKNOWN; 1977142215Sglebius break; 1978142215Sglebius } 1979142215Sglebius rt_ifmsg(&sc->sc_ac.ac_if); 1980142215Sglebius} 1981142215Sglebius 1982142215Sglebiusvoid 1983142215Sglebiuscarp_carpdev_state(void *v) 1984142215Sglebius{ 1985142215Sglebius struct carp_if *cif = v; 1986142215Sglebius struct carp_softc *sc; 1987142215Sglebius 1988142215Sglebius CARP_LOCK(cif); 1989142215Sglebius TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) { 1990142371Sglebius if (sc->sc_ifp->if_link_state != LINK_STATE_UP || 1991142215Sglebius !(sc->sc_ifp->if_flags & IFF_UP)) { 1992142215Sglebius sc->sc_flags_backup = sc->sc_if.if_flags; 1993142215Sglebius sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING); 1994142215Sglebius callout_stop(&sc->sc_ad_tmo); 1995142215Sglebius callout_stop(&sc->sc_md_tmo); 1996142215Sglebius callout_stop(&sc->sc_md6_tmo); 1997142215Sglebius carp_set_state(sc, INIT); 1998142215Sglebius carp_setrun(sc, 0); 1999142215Sglebius if (!sc->sc_suppress) { 2000142215Sglebius carp_suppress_preempt++; 2001142215Sglebius if (carp_suppress_preempt == 1) 2002142215Sglebius carp_send_ad_all(); 2003142215Sglebius } 2004142215Sglebius sc->sc_suppress = 1; 2005142215Sglebius } else { 2006142215Sglebius sc->sc_if.if_flags |= sc->sc_flags_backup; 2007142215Sglebius carp_set_state(sc, INIT); 2008142215Sglebius carp_setrun(sc, 0); 2009142215Sglebius if (sc->sc_suppress) 2010142215Sglebius carp_suppress_preempt--; 2011142215Sglebius sc->sc_suppress = 0; 2012142215Sglebius } 2013142215Sglebius } 2014142215Sglebius CARP_UNLOCK(cif); 2015142215Sglebius} 2016142215Sglebius 2017142215Sglebiusstatic int 2018142215Sglebiuscarp_modevent(module_t mod, int type, void *data) 2019142215Sglebius{ 2020142215Sglebius int error = 0; 2021142215Sglebius 2022142215Sglebius switch (type) { 2023142215Sglebius case MOD_LOAD: 2024142215Sglebius LIST_INIT(&carpif_list); 2025142215Sglebius if_clone_attach(&carp_cloner); 2026142215Sglebius printf("carp: attached\n"); 2027142215Sglebius break; 2028142215Sglebius 2029142215Sglebius case MOD_UNLOAD: 2030142215Sglebius if_clone_detach(&carp_cloner); 2031142215Sglebius while (!LIST_EMPTY(&carpif_list)) 2032142215Sglebius carp_clone_destroy( 2033142215Sglebius &LIST_FIRST(&carpif_list)->sc_if); 2034142215Sglebius break; 2035142215Sglebius 2036142215Sglebius default: 2037142215Sglebius error = EINVAL; 2038142215Sglebius break; 2039142215Sglebius } 2040142215Sglebius 2041142215Sglebius return error; 2042142215Sglebius} 2043142215Sglebius 2044142215Sglebiusstatic moduledata_t carp_mod = { 2045142215Sglebius "carp", 2046142215Sglebius carp_modevent, 2047142215Sglebius 0 2048142215Sglebius}; 2049142215Sglebius 2050142215SglebiusDECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 2051