ip_carp.c revision 142451
1142215Sglebius/* $FreeBSD: head/sys/netinet/ip_carp.c 142451 2005-02-25 10:49:47Z 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++; 436142446Sglebius CARP_LOG("carp_input: packet received on non-carp interface: %s", 437142446Sglebius m->m_pkthdr.rcvif->if_xname); 438142215Sglebius m_freem(m); 439142215Sglebius return; 440142215Sglebius } 441142215Sglebius 442142215Sglebius /* verify that the IP TTL is 255. */ 443142215Sglebius if (ip->ip_ttl != CARP_DFLTTL) { 444142215Sglebius carpstats.carps_badttl++; 445142446Sglebius CARP_LOG("carp_input: received ttl %d != 255i on %s", 446142446Sglebius ip->ip_ttl, 447142446Sglebius m->m_pkthdr.rcvif->if_xname); 448142215Sglebius m_freem(m); 449142215Sglebius return; 450142215Sglebius } 451142215Sglebius 452142215Sglebius iplen = ip->ip_hl << 2; 453142215Sglebius 454142215Sglebius if (m->m_pkthdr.len < iplen + sizeof(*ch)) { 455142215Sglebius carpstats.carps_badlen++; 456142446Sglebius CARP_LOG("carp_input: received len %zd < " 457142446Sglebius "sizeof(struct carp_header)", 458142446Sglebius m->m_len - sizeof(struct ip)); 459142215Sglebius m_freem(m); 460142215Sglebius return; 461142215Sglebius } 462142215Sglebius 463142215Sglebius if (iplen + sizeof(*ch) < m->m_len) { 464142215Sglebius if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) { 465142215Sglebius carpstats.carps_hdrops++; 466142446Sglebius CARP_LOG("carp_input: pullup failed"); 467142215Sglebius return; 468142215Sglebius } 469142215Sglebius ip = mtod(m, struct ip *); 470142215Sglebius } 471142215Sglebius ch = (struct carp_header *)((char *)ip + iplen); 472142215Sglebius 473142215Sglebius /* 474142215Sglebius * verify that the received packet length is 475142215Sglebius * equal to the CARP header 476142215Sglebius */ 477142215Sglebius len = iplen + sizeof(*ch); 478142215Sglebius if (len > m->m_pkthdr.len) { 479142215Sglebius carpstats.carps_badlen++; 480142446Sglebius CARP_LOG("carp_input: packet too short %d on %s", 481142446Sglebius m->m_pkthdr.len, 482142446Sglebius m->m_pkthdr.rcvif->if_xname); 483142215Sglebius m_freem(m); 484142215Sglebius return; 485142215Sglebius } 486142215Sglebius 487142215Sglebius if ((m = m_pullup(m, len)) == NULL) { 488142215Sglebius carpstats.carps_hdrops++; 489142215Sglebius return; 490142215Sglebius } 491142215Sglebius ip = mtod(m, struct ip *); 492142215Sglebius ch = (struct carp_header *)((char *)ip + iplen); 493142215Sglebius 494142215Sglebius /* verify the CARP checksum */ 495142215Sglebius m->m_data += iplen; 496142215Sglebius if (carp_cksum(m, len - iplen)) { 497142215Sglebius carpstats.carps_badsum++; 498142446Sglebius CARP_LOG("carp_input: checksum failed on %s", 499142446Sglebius m->m_pkthdr.rcvif->if_xname); 500142215Sglebius m_freem(m); 501142215Sglebius return; 502142215Sglebius } 503142215Sglebius m->m_data -= iplen; 504142215Sglebius 505142446Sglebius carp_input_c(m, ch, AF_INET); 506142215Sglebius} 507142215Sglebius 508142215Sglebius#ifdef INET6 509142215Sglebiusint 510142215Sglebiuscarp6_input(struct mbuf **mp, int *offp, int proto) 511142215Sglebius{ 512142215Sglebius struct mbuf *m = *mp; 513142215Sglebius struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 514142215Sglebius struct carp_header *ch; 515142215Sglebius u_int len; 516142215Sglebius 517142215Sglebius carpstats.carps_ipackets6++; 518142215Sglebius 519142215Sglebius if (!carp_opts[CARPCTL_ALLOW]) { 520142215Sglebius m_freem(m); 521142215Sglebius return (IPPROTO_DONE); 522142215Sglebius } 523142215Sglebius 524142215Sglebius /* check if received on a valid carp interface */ 525142215Sglebius if (m->m_pkthdr.rcvif->if_carp == NULL) { 526142215Sglebius carpstats.carps_badif++; 527142446Sglebius CARP_LOG("carp6_input: packet received on non-carp " 528142446Sglebius "interface: %s", 529142446Sglebius m->m_pkthdr.rcvif->if_xname); 530142215Sglebius m_freem(m); 531142215Sglebius return (IPPROTO_DONE); 532142215Sglebius } 533142215Sglebius 534142215Sglebius /* verify that the IP TTL is 255 */ 535142215Sglebius if (ip6->ip6_hlim != CARP_DFLTTL) { 536142215Sglebius carpstats.carps_badttl++; 537142446Sglebius CARP_LOG("carp6_input: received ttl %d != 255 on %s", 538142446Sglebius ip6->ip6_hlim, 539142446Sglebius m->m_pkthdr.rcvif->if_xname); 540142215Sglebius m_freem(m); 541142215Sglebius return (IPPROTO_DONE); 542142215Sglebius } 543142215Sglebius 544142215Sglebius /* verify that we have a complete carp packet */ 545142215Sglebius len = m->m_len; 546142215Sglebius IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch)); 547142215Sglebius if (ch == NULL) { 548142215Sglebius carpstats.carps_badlen++; 549142446Sglebius CARP_LOG("carp6_input: packet size %u too small on %s", len, 550142446Sglebius m->m_pkthdr.rcvif->if_xname); 551142215Sglebius return (IPPROTO_DONE); 552142215Sglebius } 553142215Sglebius 554142215Sglebius 555142215Sglebius /* verify the CARP checksum */ 556142215Sglebius m->m_data += *offp; 557142215Sglebius if (carp_cksum(m, sizeof(*ch))) { 558142215Sglebius carpstats.carps_badsum++; 559142446Sglebius CARP_LOG("carp6_input: checksum failed, on %s", 560142446Sglebius m->m_pkthdr.rcvif->if_xname); 561142215Sglebius m_freem(m); 562142215Sglebius return (IPPROTO_DONE); 563142215Sglebius } 564142215Sglebius m->m_data -= *offp; 565142215Sglebius 566142446Sglebius carp_input_c(m, ch, AF_INET6); 567142215Sglebius return (IPPROTO_DONE); 568142215Sglebius} 569142215Sglebius#endif /* INET6 */ 570142215Sglebius 571142215Sglebiusvoid 572142446Sglebiuscarp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af) 573142215Sglebius{ 574142215Sglebius struct ifnet *ifp = m->m_pkthdr.rcvif; 575142446Sglebius struct carp_softc *sc; 576142215Sglebius u_int64_t tmp_counter; 577142215Sglebius struct timeval sc_tv, ch_tv; 578142215Sglebius 579142215Sglebius /* verify that the VHID is valid on the receiving interface */ 580142215Sglebius CARP_LOCK(ifp->if_carp); 581142215Sglebius TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list) 582142215Sglebius if (sc->sc_vhid == ch->carp_vhid) 583142215Sglebius break; 584142215Sglebius CARP_UNLOCK(ifp->if_carp); 585142215Sglebius if (!sc || (sc->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) != 586142215Sglebius (IFF_UP|IFF_RUNNING)) { 587142215Sglebius carpstats.carps_badvhid++; 588142215Sglebius m_freem(m); 589142215Sglebius return; 590142215Sglebius } 591142215Sglebius 592142215Sglebius getmicrotime(&sc->sc_if.if_lastchange); 593142215Sglebius sc->sc_if.if_ipackets++; 594142215Sglebius sc->sc_if.if_ibytes += m->m_pkthdr.len; 595142215Sglebius 596142215Sglebius if (sc->sc_if.if_bpf) { 597142215Sglebius /* 598142215Sglebius * We need to prepend the address family as 599142215Sglebius * a four byte field. Cons up a dummy header 600142215Sglebius * to pacify bpf. This is safe because bpf 601142215Sglebius * will only read from the mbuf (i.e., it won't 602142215Sglebius * try to free it or keep a pointer to it). 603142215Sglebius */ 604142215Sglebius struct mbuf m0; 605142215Sglebius struct ip *ip = mtod(m, struct ip *); 606142215Sglebius u_int32_t maf = htonl(af); 607142215Sglebius 608142215Sglebius m0.m_next = m; 609142215Sglebius m0.m_len = sizeof(maf); 610142215Sglebius m0.m_data = (char *)&maf; 611142215Sglebius /* BPF wants net byte order */ 612142215Sglebius ip->ip_len = htonl(ip->ip_len); 613142215Sglebius ip->ip_off = htonl(ip->ip_off); 614142215Sglebius BPF_MTAP(&sc->sc_if, &m0); 615142215Sglebius } 616142215Sglebius 617142215Sglebius /* verify the CARP version. */ 618142215Sglebius if (ch->carp_version != CARP_VERSION) { 619142215Sglebius carpstats.carps_badver++; 620142215Sglebius sc->sc_if.if_ierrors++; 621142446Sglebius CARP_LOG("%s; invalid version %d", 622142446Sglebius sc->sc_if.if_xname, 623142446Sglebius ch->carp_version); 624142215Sglebius m_freem(m); 625142215Sglebius return; 626142215Sglebius } 627142215Sglebius 628142215Sglebius /* verify the hash */ 629142215Sglebius if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) { 630142215Sglebius carpstats.carps_badauth++; 631142215Sglebius sc->sc_if.if_ierrors++; 632142446Sglebius CARP_LOG("%s: incorrect hash", sc->sc_if.if_xname); 633142215Sglebius m_freem(m); 634142215Sglebius return; 635142215Sglebius } 636142215Sglebius 637142215Sglebius tmp_counter = ntohl(ch->carp_counter[0]); 638142215Sglebius tmp_counter = tmp_counter<<32; 639142215Sglebius tmp_counter += ntohl(ch->carp_counter[1]); 640142215Sglebius 641142215Sglebius /* XXX Replay protection goes here */ 642142215Sglebius 643142215Sglebius sc->sc_init_counter = 0; 644142215Sglebius sc->sc_counter = tmp_counter; 645142215Sglebius 646142215Sglebius sc_tv.tv_sec = sc->sc_advbase; 647142215Sglebius if (carp_suppress_preempt && sc->sc_advskew < 240) 648142215Sglebius sc_tv.tv_usec = 240 * 1000000 / 256; 649142215Sglebius else 650142215Sglebius sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256; 651142215Sglebius ch_tv.tv_sec = ch->carp_advbase; 652142215Sglebius ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256; 653142215Sglebius 654142215Sglebius switch (sc->sc_state) { 655142215Sglebius case INIT: 656142215Sglebius break; 657142215Sglebius case MASTER: 658142215Sglebius /* 659142215Sglebius * If we receive an advertisement from a master who's going to 660142215Sglebius * be more frequent than us, go into BACKUP state. 661142215Sglebius */ 662142215Sglebius if (timevalcmp(&sc_tv, &ch_tv, >) || 663142215Sglebius timevalcmp(&sc_tv, &ch_tv, ==)) { 664142215Sglebius callout_stop(&sc->sc_ad_tmo); 665142446Sglebius CARP_DEBUG("%s: MASTER -> BACKUP " 666142446Sglebius "(more frequent advertisement received)", 667142446Sglebius sc->sc_if.if_xname); 668142215Sglebius carp_set_state(sc, BACKUP); 669142215Sglebius carp_setrun(sc, 0); 670142215Sglebius carp_setroute(sc, RTM_DELETE); 671142215Sglebius } 672142215Sglebius break; 673142215Sglebius case BACKUP: 674142215Sglebius /* 675142215Sglebius * If we're pre-empting masters who advertise slower than us, 676142215Sglebius * and this one claims to be slower, treat him as down. 677142215Sglebius */ 678142215Sglebius if (carp_opts[CARPCTL_PREEMPT] && 679142215Sglebius timevalcmp(&sc_tv, &ch_tv, <)) { 680142446Sglebius CARP_DEBUG("%s: BACKUP -> MASTER " 681142446Sglebius "(preempting a slower master)", 682142446Sglebius sc->sc_if.if_xname); 683142215Sglebius carp_master_down(sc); 684142215Sglebius break; 685142215Sglebius } 686142215Sglebius 687142215Sglebius /* 688142215Sglebius * If the master is going to advertise at such a low frequency 689142215Sglebius * that he's guaranteed to time out, we'd might as well just 690142215Sglebius * treat him as timed out now. 691142215Sglebius */ 692142215Sglebius sc_tv.tv_sec = sc->sc_advbase * 3; 693142215Sglebius if (timevalcmp(&sc_tv, &ch_tv, <)) { 694142446Sglebius CARP_DEBUG("%s: BACKUP -> MASTER " 695142446Sglebius "(master timed out)", 696142446Sglebius sc->sc_if.if_xname); 697142215Sglebius carp_master_down(sc); 698142215Sglebius break; 699142215Sglebius } 700142215Sglebius 701142215Sglebius /* 702142215Sglebius * Otherwise, we reset the counter and wait for the next 703142215Sglebius * advertisement. 704142215Sglebius */ 705142215Sglebius carp_setrun(sc, af); 706142215Sglebius break; 707142215Sglebius } 708142215Sglebius 709142215Sglebius m_freem(m); 710142215Sglebius return; 711142215Sglebius} 712142215Sglebius 713142215Sglebiusvoid 714142215Sglebiuscarpdetach(struct carp_softc *sc) 715142215Sglebius{ 716142215Sglebius struct ifaddr *ifa; 717142215Sglebius 718142215Sglebius callout_stop(&sc->sc_ad_tmo); 719142215Sglebius callout_stop(&sc->sc_md_tmo); 720142215Sglebius callout_stop(&sc->sc_md6_tmo); 721142215Sglebius 722142215Sglebius while ((ifa = TAILQ_FIRST(&sc->sc_if.if_addrlist)) != NULL) 723142215Sglebius if (ifa->ifa_addr->sa_family == AF_INET) { 724142215Sglebius struct in_ifaddr *ia = ifatoia(ifa); 725142215Sglebius 726142215Sglebius carp_del_addr(sc, &ia->ia_addr); 727142215Sglebius 728142215Sglebius /* ripped screaming from in_control(SIOCDIFADDR) */ 729142215Sglebius in_ifscrub(&sc->sc_if, ia); 730142215Sglebius TAILQ_REMOVE(&sc->sc_if.if_addrlist, ifa, ifa_link); 731142215Sglebius TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link); 732142215Sglebius IFAFREE((&ia->ia_ifa)); 733142215Sglebius } 734142215Sglebius} 735142215Sglebius 736142215Sglebius/* Detach an interface from the carp. */ 737142215Sglebiusvoid 738142215Sglebiuscarp_ifdetach(struct ifnet *ifp) 739142215Sglebius{ 740142215Sglebius struct carp_softc *sc; 741142215Sglebius struct carp_if *cif = (struct carp_if *)ifp->if_carp; 742142215Sglebius 743142215Sglebius CARP_LOCK(cif); 744142215Sglebius TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) 745142215Sglebius carpdetach(sc); 746142215Sglebius CARP_UNLOCK(cif); 747142215Sglebius} 748142215Sglebius 749142215Sglebiusint 750142215Sglebiuscarp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch) 751142215Sglebius{ 752142215Sglebius struct m_tag *mtag; 753142215Sglebius struct ifnet *ifp = &sc->sc_if; 754142215Sglebius 755142215Sglebius if (sc->sc_init_counter) { 756142215Sglebius /* this could also be seconds since unix epoch */ 757142215Sglebius sc->sc_counter = arc4random(); 758142215Sglebius sc->sc_counter = sc->sc_counter << 32; 759142215Sglebius sc->sc_counter += arc4random(); 760142215Sglebius } else 761142215Sglebius sc->sc_counter++; 762142215Sglebius 763142215Sglebius ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff); 764142215Sglebius ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff); 765142215Sglebius 766142215Sglebius carp_hmac_generate(sc, ch->carp_counter, ch->carp_md); 767142215Sglebius 768142215Sglebius /* Tag packet for carp_output */ 769142215Sglebius mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), M_NOWAIT); 770142215Sglebius if (mtag == NULL) { 771142215Sglebius m_freem(m); 772142215Sglebius sc->sc_if.if_oerrors++; 773142215Sglebius return (ENOMEM); 774142215Sglebius } 775142215Sglebius bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *)); 776142215Sglebius m_tag_prepend(m, mtag); 777142215Sglebius 778142215Sglebius return (0); 779142215Sglebius} 780142215Sglebius 781142215Sglebiusvoid 782142215Sglebiuscarp_send_ad_all(void) 783142215Sglebius{ 784142215Sglebius struct ifnet *ifp; 785142215Sglebius struct carp_if *cif; 786142215Sglebius struct carp_softc *vh; 787142215Sglebius 788142215Sglebius TAILQ_FOREACH(ifp, &ifnet, if_list) { 789142215Sglebius if (ifp->if_carp == NULL) 790142215Sglebius continue; 791142215Sglebius 792142215Sglebius cif = (struct carp_if *)ifp->if_carp; 793142215Sglebius CARP_LOCK(cif); 794142215Sglebius TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 795142215Sglebius if ((vh->sc_ac.ac_if.if_flags & (IFF_UP|IFF_RUNNING)) && 796142215Sglebius vh->sc_state == MASTER) 797142215Sglebius carp_send_ad(vh); 798142215Sglebius } 799142215Sglebius CARP_UNLOCK(cif); 800142215Sglebius } 801142215Sglebius} 802142215Sglebius 803142215Sglebiusvoid 804142215Sglebiuscarp_send_ad(void *v) 805142215Sglebius{ 806142215Sglebius struct carp_header ch; 807142215Sglebius struct timeval tv; 808142215Sglebius struct carp_softc *sc = v; 809142215Sglebius struct carp_header *ch_ptr; 810142215Sglebius struct mbuf *m; 811142215Sglebius int len, advbase, advskew; 812142215Sglebius 813142215Sglebius /* bow out if we've lost our UPness or RUNNINGuiness */ 814142215Sglebius if ((sc->sc_if.if_flags & 815142215Sglebius (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 816142215Sglebius advbase = 255; 817142215Sglebius advskew = 255; 818142215Sglebius } else { 819142215Sglebius advbase = sc->sc_advbase; 820142215Sglebius if (!carp_suppress_preempt || sc->sc_advskew > 240) 821142215Sglebius advskew = sc->sc_advskew; 822142215Sglebius else 823142215Sglebius advskew = 240; 824142215Sglebius tv.tv_sec = advbase; 825142215Sglebius tv.tv_usec = advskew * 1000000 / 256; 826142215Sglebius } 827142215Sglebius 828142215Sglebius ch.carp_version = CARP_VERSION; 829142215Sglebius ch.carp_type = CARP_ADVERTISEMENT; 830142215Sglebius ch.carp_vhid = sc->sc_vhid; 831142215Sglebius ch.carp_advbase = advbase; 832142215Sglebius ch.carp_advskew = advskew; 833142215Sglebius ch.carp_authlen = 7; /* XXX DEFINE */ 834142215Sglebius ch.carp_pad1 = 0; /* must be zero */ 835142215Sglebius ch.carp_cksum = 0; 836142215Sglebius 837142215Sglebius#ifdef INET 838142215Sglebius if (sc->sc_ia) { 839142215Sglebius struct ip *ip; 840142215Sglebius 841142215Sglebius MGETHDR(m, M_DONTWAIT, MT_HEADER); 842142215Sglebius if (m == NULL) { 843142215Sglebius sc->sc_ac.ac_if.if_oerrors++; 844142215Sglebius carpstats.carps_onomem++; 845142215Sglebius /* XXX maybe less ? */ 846142215Sglebius if (advbase != 255 || advskew != 255) 847142215Sglebius callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), 848142215Sglebius carp_send_ad, sc); 849142215Sglebius return; 850142215Sglebius } 851142215Sglebius len = sizeof(*ip) + sizeof(ch); 852142215Sglebius m->m_pkthdr.len = len; 853142215Sglebius m->m_pkthdr.rcvif = NULL; 854142215Sglebius m->m_len = len; 855142215Sglebius MH_ALIGN(m, m->m_len); 856142215Sglebius m->m_flags |= M_MCAST; 857142215Sglebius ip = mtod(m, struct ip *); 858142215Sglebius ip->ip_v = IPVERSION; 859142215Sglebius ip->ip_hl = sizeof(*ip) >> 2; 860142215Sglebius ip->ip_tos = IPTOS_LOWDELAY; 861142215Sglebius ip->ip_len = len; 862142215Sglebius ip->ip_id = ip_newid(); 863142215Sglebius ip->ip_off = IP_DF; 864142215Sglebius ip->ip_ttl = CARP_DFLTTL; 865142215Sglebius ip->ip_p = IPPROTO_CARP; 866142215Sglebius ip->ip_sum = 0; 867142215Sglebius ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr; 868142215Sglebius ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP); 869142215Sglebius 870142215Sglebius ch_ptr = (struct carp_header *)(&ip[1]); 871142215Sglebius bcopy(&ch, ch_ptr, sizeof(ch)); 872142215Sglebius if (carp_prepare_ad(m, sc, ch_ptr)) 873142215Sglebius return; 874142215Sglebius 875142215Sglebius m->m_data += sizeof(*ip); 876142215Sglebius ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip)); 877142215Sglebius m->m_data -= sizeof(*ip); 878142215Sglebius 879142215Sglebius getmicrotime(&sc->sc_if.if_lastchange); 880142215Sglebius sc->sc_ac.ac_if.if_opackets++; 881142215Sglebius sc->sc_ac.ac_if.if_obytes += len; 882142215Sglebius carpstats.carps_opackets++; 883142215Sglebius 884142215Sglebius if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) { 885142215Sglebius sc->sc_if.if_oerrors++; 886142215Sglebius if (sc->sc_sendad_errors < INT_MAX) 887142215Sglebius sc->sc_sendad_errors++; 888142215Sglebius if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { 889142215Sglebius carp_suppress_preempt++; 890142215Sglebius if (carp_suppress_preempt == 1) 891142215Sglebius carp_send_ad_all(); 892142215Sglebius } 893142215Sglebius sc->sc_sendad_success = 0; 894142215Sglebius } else { 895142215Sglebius if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { 896142215Sglebius if (++sc->sc_sendad_success >= 897142215Sglebius CARP_SENDAD_MIN_SUCCESS) { 898142215Sglebius carp_suppress_preempt--; 899142215Sglebius sc->sc_sendad_errors = 0; 900142215Sglebius } 901142215Sglebius } else 902142215Sglebius sc->sc_sendad_errors = 0; 903142215Sglebius } 904142215Sglebius } 905142215Sglebius#endif /* INET */ 906142215Sglebius#ifdef INET6 907142215Sglebius if (sc->sc_ia6) { 908142215Sglebius struct ip6_hdr *ip6; 909142215Sglebius 910142215Sglebius MGETHDR(m, M_DONTWAIT, MT_HEADER); 911142215Sglebius if (m == NULL) { 912142215Sglebius sc->sc_ac.ac_if.if_oerrors++; 913142215Sglebius carpstats.carps_onomem++; 914142215Sglebius /* XXX maybe less ? */ 915142215Sglebius if (advbase != 255 || advskew != 255) 916142215Sglebius callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), 917142215Sglebius carp_send_ad, sc); 918142215Sglebius return; 919142215Sglebius } 920142215Sglebius len = sizeof(*ip6) + sizeof(ch); 921142215Sglebius m->m_pkthdr.len = len; 922142215Sglebius m->m_pkthdr.rcvif = NULL; 923142215Sglebius m->m_len = len; 924142215Sglebius MH_ALIGN(m, m->m_len); 925142215Sglebius m->m_flags |= M_MCAST; 926142215Sglebius ip6 = mtod(m, struct ip6_hdr *); 927142215Sglebius bzero(ip6, sizeof(*ip6)); 928142215Sglebius ip6->ip6_vfc |= IPV6_VERSION; 929142215Sglebius ip6->ip6_hlim = CARP_DFLTTL; 930142215Sglebius ip6->ip6_nxt = IPPROTO_CARP; 931142215Sglebius bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src, 932142215Sglebius sizeof(struct in6_addr)); 933142215Sglebius /* set the multicast destination */ 934142215Sglebius 935142215Sglebius ip6->ip6_dst.s6_addr8[0] = 0xff; 936142215Sglebius ip6->ip6_dst.s6_addr8[1] = 0x02; 937142215Sglebius ip6->ip6_dst.s6_addr8[15] = 0x12; 938142215Sglebius 939142215Sglebius ch_ptr = (struct carp_header *)(&ip6[1]); 940142215Sglebius bcopy(&ch, ch_ptr, sizeof(ch)); 941142215Sglebius if (carp_prepare_ad(m, sc, ch_ptr)) 942142215Sglebius return; 943142215Sglebius 944142215Sglebius m->m_data += sizeof(*ip6); 945142215Sglebius ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6)); 946142215Sglebius m->m_data -= sizeof(*ip6); 947142215Sglebius 948142215Sglebius getmicrotime(&sc->sc_if.if_lastchange); 949142215Sglebius sc->sc_if.if_opackets++; 950142215Sglebius sc->sc_if.if_obytes += len; 951142215Sglebius carpstats.carps_opackets6++; 952142215Sglebius 953142215Sglebius if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) { 954142215Sglebius sc->sc_if.if_oerrors++; 955142215Sglebius if (sc->sc_sendad_errors < INT_MAX) 956142215Sglebius sc->sc_sendad_errors++; 957142215Sglebius if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { 958142215Sglebius carp_suppress_preempt++; 959142215Sglebius if (carp_suppress_preempt == 1) 960142215Sglebius carp_send_ad_all(); 961142215Sglebius } 962142215Sglebius sc->sc_sendad_success = 0; 963142215Sglebius } else { 964142215Sglebius if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { 965142215Sglebius if (++sc->sc_sendad_success >= 966142215Sglebius CARP_SENDAD_MIN_SUCCESS) { 967142215Sglebius carp_suppress_preempt--; 968142215Sglebius sc->sc_sendad_errors = 0; 969142215Sglebius } 970142215Sglebius } else 971142215Sglebius sc->sc_sendad_errors = 0; 972142215Sglebius } 973142215Sglebius } 974142215Sglebius#endif /* INET6 */ 975142215Sglebius 976142215Sglebius if (advbase != 255 || advskew != 255) 977142215Sglebius callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), 978142215Sglebius carp_send_ad, sc); 979142215Sglebius 980142215Sglebius} 981142215Sglebius 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 */ 987142215Sglebiusvoid 988142215Sglebiuscarp_send_arp(struct carp_softc *sc) 989142215Sglebius{ 990142215Sglebius struct ifaddr *ifa; 991142215Sglebius 992142215Sglebius TAILQ_FOREACH(ifa, &sc->sc_if.if_addrlist, ifa_list) { 993142215Sglebius 994142215Sglebius if (ifa->ifa_addr->sa_family != AF_INET) 995142215Sglebius continue; 996142215Sglebius 997142215Sglebius/* arprequest(sc->sc_ifp, &in, &in, sc->sc_ac.ac_enaddr); */ 998142215Sglebius arp_ifinit2(sc->sc_ifp, ifa, sc->sc_ac.ac_enaddr); 999142215Sglebius 1000142215Sglebius DELAY(1000); /* XXX */ 1001142215Sglebius } 1002142215Sglebius} 1003142215Sglebius 1004142215Sglebius#ifdef INET6 1005142215Sglebiusvoid 1006142215Sglebiuscarp_send_na(struct carp_softc *sc) 1007142215Sglebius{ 1008142215Sglebius struct ifaddr *ifa; 1009142215Sglebius struct in6_addr *in6; 1010142215Sglebius static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 1011142215Sglebius 1012142215Sglebius TAILQ_FOREACH(ifa, &sc->sc_if.if_addrlist, ifa_list) { 1013142215Sglebius 1014142215Sglebius if (ifa->ifa_addr->sa_family != AF_INET6) 1015142215Sglebius continue; 1016142215Sglebius 1017142215Sglebius in6 = &ifatoia6(ifa)->ia_addr.sin6_addr; 1018142215Sglebius nd6_na_output(sc->sc_ifp, &mcast, in6, 1019142215Sglebius ND_NA_FLAG_OVERRIDE, 1, NULL); 1020142215Sglebius DELAY(1000); /* XXX */ 1021142215Sglebius } 1022142215Sglebius} 1023142215Sglebius#endif /* INET6 */ 1024142215Sglebius 1025142215Sglebiusint 1026142215Sglebiuscarp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type) 1027142215Sglebius{ 1028142215Sglebius struct carp_softc *vh; 1029142215Sglebius struct ifaddr *ifa; 1030142215Sglebius int count = 0; 1031142215Sglebius 1032142215Sglebius TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1033142215Sglebius if ((type == CARP_COUNT_RUNNING && 1034142215Sglebius (vh->sc_ac.ac_if.if_flags & (IFF_UP|IFF_RUNNING)) == 1035142215Sglebius (IFF_UP|IFF_RUNNING)) || 1036142215Sglebius (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) { 1037142215Sglebius TAILQ_FOREACH(ifa, &vh->sc_ac.ac_if.if_addrlist, 1038142215Sglebius ifa_list) { 1039142215Sglebius if (ifa->ifa_addr->sa_family == AF_INET && 1040142215Sglebius ia->ia_addr.sin_addr.s_addr == 1041142215Sglebius ifatoia(ifa)->ia_addr.sin_addr.s_addr) 1042142215Sglebius count++; 1043142215Sglebius } 1044142215Sglebius } 1045142215Sglebius } 1046142215Sglebius return (count); 1047142215Sglebius} 1048142215Sglebius 1049142215Sglebiusint 1050142215Sglebiuscarp_iamatch(void *v, struct in_ifaddr *ia, 1051142215Sglebius struct in_addr *isaddr, u_int8_t **enaddr) 1052142215Sglebius{ 1053142215Sglebius struct carp_if *cif = v; 1054142215Sglebius struct carp_softc *vh; 1055142215Sglebius int index, count = 0; 1056142215Sglebius struct ifaddr *ifa; 1057142215Sglebius 1058142215Sglebius CARP_LOCK(cif); 1059142215Sglebius 1060142215Sglebius if (carp_opts[CARPCTL_ARPBALANCE]) { 1061142215Sglebius /* 1062142215Sglebius * XXX proof of concept implementation. 1063142215Sglebius * We use the source ip to decide which virtual host should 1064142215Sglebius * handle the request. If we're master of that virtual host, 1065142215Sglebius * then we respond, otherwise, just drop the arp packet on 1066142215Sglebius * the floor. 1067142215Sglebius */ 1068142215Sglebius count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING); 1069142215Sglebius if (count == 0) { 1070142215Sglebius /* should never reach this */ 1071142215Sglebius CARP_UNLOCK(cif); 1072142215Sglebius return (0); 1073142215Sglebius } 1074142215Sglebius 1075142215Sglebius /* this should be a hash, like pf_hash() */ 1076142215Sglebius index = isaddr->s_addr % count; 1077142215Sglebius count = 0; 1078142215Sglebius 1079142215Sglebius TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1080142215Sglebius if ((vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) == 1081142215Sglebius (IFF_UP|IFF_RUNNING)) { 1082142215Sglebius TAILQ_FOREACH(ifa, &vh->sc_if.if_addrlist, 1083142215Sglebius ifa_list) { 1084142215Sglebius if (ifa->ifa_addr->sa_family == 1085142215Sglebius AF_INET && 1086142215Sglebius ia->ia_addr.sin_addr.s_addr == 1087142215Sglebius ifatoia(ifa)->ia_addr.sin_addr.s_addr) { 1088142215Sglebius if (count == index) { 1089142215Sglebius if (vh->sc_state == 1090142215Sglebius MASTER) { 1091142215Sglebius *enaddr = vh->sc_ac.ac_enaddr; 1092142215Sglebius CARP_UNLOCK(cif); 1093142215Sglebius return (1); 1094142215Sglebius } else { 1095142215Sglebius CARP_UNLOCK(cif); 1096142215Sglebius return (0); 1097142215Sglebius } 1098142215Sglebius } 1099142215Sglebius count++; 1100142215Sglebius } 1101142215Sglebius } 1102142215Sglebius } 1103142215Sglebius } 1104142215Sglebius } else { 1105142215Sglebius TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1106142215Sglebius if ((vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) == 1107142215Sglebius (IFF_UP|IFF_RUNNING) && ia->ia_ifp == 1108142215Sglebius &vh->sc_if) { 1109142215Sglebius *enaddr = vh->sc_ac.ac_enaddr; 1110142215Sglebius CARP_UNLOCK(cif); 1111142215Sglebius return (1); 1112142215Sglebius } 1113142215Sglebius } 1114142215Sglebius } 1115142215Sglebius CARP_UNLOCK(cif); 1116142215Sglebius return (0); 1117142215Sglebius} 1118142215Sglebius 1119142215Sglebius#ifdef INET6 1120142215Sglebiusstruct ifaddr * 1121142215Sglebiuscarp_iamatch6(void *v, struct in6_addr *taddr) 1122142215Sglebius{ 1123142215Sglebius struct carp_if *cif = v; 1124142215Sglebius struct carp_softc *vh; 1125142215Sglebius struct ifaddr *ifa; 1126142215Sglebius 1127142215Sglebius CARP_LOCK(cif); 1128142215Sglebius TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1129142215Sglebius TAILQ_FOREACH(ifa, &vh->sc_if.if_addrlist, ifa_list) { 1130142215Sglebius if (IN6_ARE_ADDR_EQUAL(taddr, 1131142215Sglebius &ifatoia6(ifa)->ia_addr.sin6_addr) && 1132142215Sglebius ((vh->sc_if.if_flags & 1133142215Sglebius (IFF_UP|IFF_RUNNING)) == (IFF_UP|IFF_RUNNING))) { 1134142215Sglebius CARP_UNLOCK(cif); 1135142215Sglebius return (ifa); 1136142215Sglebius } 1137142215Sglebius } 1138142215Sglebius } 1139142215Sglebius CARP_UNLOCK(cif); 1140142215Sglebius 1141142215Sglebius return (NULL); 1142142215Sglebius} 1143142215Sglebius 1144142215Sglebiusvoid * 1145142215Sglebiuscarp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr) 1146142215Sglebius{ 1147142215Sglebius struct m_tag *mtag; 1148142215Sglebius struct carp_if *cif = v; 1149142215Sglebius struct carp_softc *sc; 1150142215Sglebius struct ifaddr *ifa; 1151142215Sglebius 1152142215Sglebius CARP_LOCK(cif); 1153142215Sglebius TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) { 1154142215Sglebius TAILQ_FOREACH(ifa, &sc->sc_if.if_addrlist, ifa_list) { 1155142215Sglebius if (IN6_ARE_ADDR_EQUAL(taddr, 1156142215Sglebius &ifatoia6(ifa)->ia_addr.sin6_addr) && 1157142215Sglebius ((sc->sc_if.if_flags & 1158142215Sglebius (IFF_UP|IFF_RUNNING)) == (IFF_UP|IFF_RUNNING))) { 1159142215Sglebius struct ifnet *ifp = &sc->sc_if; 1160142215Sglebius mtag = m_tag_get(PACKET_TAG_CARP, 1161142215Sglebius sizeof(struct ifnet *), M_NOWAIT); 1162142215Sglebius if (mtag == NULL) { 1163142215Sglebius /* better a bit than nothing */ 1164142215Sglebius CARP_UNLOCK(cif); 1165142215Sglebius return (sc->sc_ac.ac_enaddr); 1166142215Sglebius } 1167142215Sglebius bcopy(&ifp, (caddr_t)(mtag + 1), 1168142215Sglebius sizeof(struct ifnet *)); 1169142215Sglebius m_tag_prepend(m, mtag); 1170142215Sglebius 1171142215Sglebius CARP_UNLOCK(cif); 1172142215Sglebius return (sc->sc_ac.ac_enaddr); 1173142215Sglebius } 1174142215Sglebius } 1175142215Sglebius } 1176142215Sglebius CARP_UNLOCK(cif); 1177142215Sglebius 1178142215Sglebius return (NULL); 1179142215Sglebius} 1180142215Sglebius#endif 1181142215Sglebius 1182142215Sglebiusstruct ifnet * 1183142215Sglebiuscarp_forus(void *v, void *dhost) 1184142215Sglebius{ 1185142215Sglebius struct carp_if *cif = v; 1186142215Sglebius struct carp_softc *vh; 1187142215Sglebius u_int8_t *ena = dhost; 1188142215Sglebius 1189142215Sglebius if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1) 1190142215Sglebius return (NULL); 1191142215Sglebius 1192142215Sglebius CARP_LOCK(cif); 1193142215Sglebius TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) 1194142215Sglebius if ((vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) == 1195142215Sglebius (IFF_UP|IFF_RUNNING) && vh->sc_state == MASTER && 1196142215Sglebius !bcmp(dhost, vh->sc_ac.ac_enaddr, ETHER_ADDR_LEN)) { 1197142215Sglebius CARP_UNLOCK(cif); 1198142215Sglebius return (&vh->sc_if); 1199142215Sglebius } 1200142215Sglebius 1201142215Sglebius CARP_UNLOCK(cif); 1202142215Sglebius return (NULL); 1203142215Sglebius} 1204142215Sglebius 1205142215Sglebiusvoid 1206142215Sglebiuscarp_master_down(void *v) 1207142215Sglebius{ 1208142215Sglebius struct carp_softc *sc = v; 1209142215Sglebius 1210142215Sglebius switch (sc->sc_state) { 1211142215Sglebius case INIT: 1212142215Sglebius printf("%s: master_down event in INIT state\n", 1213142215Sglebius sc->sc_if.if_xname); 1214142215Sglebius break; 1215142215Sglebius case MASTER: 1216142215Sglebius break; 1217142215Sglebius case BACKUP: 1218142215Sglebius carp_set_state(sc, MASTER); 1219142215Sglebius carp_send_ad(sc); 1220142215Sglebius carp_send_arp(sc); 1221142215Sglebius#ifdef INET6 1222142215Sglebius carp_send_na(sc); 1223142215Sglebius#endif /* INET6 */ 1224142215Sglebius carp_setrun(sc, 0); 1225142215Sglebius carp_setroute(sc, RTM_ADD); 1226142215Sglebius break; 1227142215Sglebius } 1228142215Sglebius} 1229142215Sglebius 1230142215Sglebius/* 1231142215Sglebius * When in backup state, af indicates whether to reset the master down timer 1232142215Sglebius * for v4 or v6. If it's set to zero, reset the ones which are already pending. 1233142215Sglebius */ 1234142215Sglebiusvoid 1235142215Sglebiuscarp_setrun(struct carp_softc *sc, sa_family_t af) 1236142215Sglebius{ 1237142215Sglebius struct timeval tv; 1238142215Sglebius 1239142215Sglebius if (sc->sc_if.if_flags & IFF_UP && 1240142215Sglebius sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6)) 1241142215Sglebius sc->sc_if.if_flags |= IFF_RUNNING; 1242142215Sglebius else { 1243142215Sglebius sc->sc_if.if_flags &= ~IFF_RUNNING; 1244142215Sglebius carp_setroute(sc, RTM_DELETE); 1245142215Sglebius return; 1246142215Sglebius } 1247142215Sglebius 1248142215Sglebius switch (sc->sc_state) { 1249142215Sglebius case INIT: 1250142215Sglebius if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) { 1251142215Sglebius carp_send_ad(sc); 1252142215Sglebius carp_send_arp(sc); 1253142215Sglebius#ifdef INET6 1254142215Sglebius carp_send_na(sc); 1255142215Sglebius#endif /* INET6 */ 1256142446Sglebius CARP_DEBUG("%s: INIT -> MASTER (preempting)", 1257142446Sglebius sc->sc_if.if_xname); 1258142215Sglebius carp_set_state(sc, MASTER); 1259142215Sglebius carp_setroute(sc, RTM_ADD); 1260142215Sglebius } else { 1261142446Sglebius CARP_DEBUG("%s: INIT -> BACKUP", sc->sc_if.if_xname); 1262142215Sglebius carp_set_state(sc, BACKUP); 1263142215Sglebius carp_setroute(sc, RTM_DELETE); 1264142215Sglebius carp_setrun(sc, 0); 1265142215Sglebius } 1266142215Sglebius break; 1267142215Sglebius case BACKUP: 1268142215Sglebius callout_stop(&sc->sc_ad_tmo); 1269142215Sglebius tv.tv_sec = 3 * sc->sc_advbase; 1270142215Sglebius tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1271142215Sglebius switch (af) { 1272142215Sglebius#ifdef INET 1273142215Sglebius case AF_INET: 1274142215Sglebius callout_reset(&sc->sc_md_tmo, tvtohz(&tv), 1275142215Sglebius carp_master_down, sc); 1276142215Sglebius break; 1277142215Sglebius#endif /* INET */ 1278142215Sglebius#ifdef INET6 1279142215Sglebius case AF_INET6: 1280142215Sglebius callout_reset(&sc->sc_md6_tmo, tvtohz(&tv), 1281142215Sglebius carp_master_down, sc); 1282142215Sglebius break; 1283142215Sglebius#endif /* INET6 */ 1284142215Sglebius default: 1285142215Sglebius if (sc->sc_naddrs) 1286142215Sglebius callout_reset(&sc->sc_md_tmo, tvtohz(&tv), 1287142215Sglebius carp_master_down, sc); 1288142215Sglebius if (sc->sc_naddrs6) 1289142215Sglebius callout_reset(&sc->sc_md6_tmo, tvtohz(&tv), 1290142215Sglebius carp_master_down, sc); 1291142215Sglebius break; 1292142215Sglebius } 1293142215Sglebius break; 1294142215Sglebius case MASTER: 1295142215Sglebius tv.tv_sec = sc->sc_advbase; 1296142215Sglebius tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1297142215Sglebius callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), 1298142215Sglebius carp_send_ad, sc); 1299142215Sglebius break; 1300142215Sglebius } 1301142215Sglebius} 1302142215Sglebius 1303142215Sglebiusint 1304142215Sglebiuscarp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin) 1305142215Sglebius{ 1306142215Sglebius struct ifnet *ifp; 1307142215Sglebius struct carp_if *cif; 1308142215Sglebius struct in_ifaddr *ia, *ia_if; 1309142215Sglebius struct ip_moptions *imo = &sc->sc_imo; 1310142215Sglebius struct in_addr addr; 1311142215Sglebius u_long iaddr = htonl(sin->sin_addr.s_addr); 1312142215Sglebius int own, error; 1313142215Sglebius 1314142215Sglebius if (sin->sin_addr.s_addr == 0) { 1315142215Sglebius if (!(sc->sc_if.if_flags & IFF_UP)) 1316142215Sglebius carp_set_state(sc, INIT); 1317142215Sglebius if (sc->sc_naddrs) 1318142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1319142215Sglebius carp_setrun(sc, 0); 1320142215Sglebius return (0); 1321142215Sglebius } 1322142215Sglebius 1323142215Sglebius /* we have to do it by hands to check we won't match on us */ 1324142215Sglebius ia_if = NULL; own = 0; 1325142215Sglebius TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 1326142215Sglebius /* and, yeah, we need a multicast-capable iface too */ 1327142215Sglebius if (ia->ia_ifp != &sc->sc_if && 1328142215Sglebius (ia->ia_ifp->if_flags & IFF_MULTICAST) && 1329142215Sglebius (iaddr & ia->ia_subnetmask) == ia->ia_subnet) { 1330142215Sglebius if (!ia_if) 1331142215Sglebius ia_if = ia; 1332142215Sglebius if (sin->sin_addr.s_addr == 1333142215Sglebius ia->ia_addr.sin_addr.s_addr) 1334142215Sglebius own++; 1335142215Sglebius } 1336142215Sglebius } 1337142215Sglebius 1338142215Sglebius if (!ia_if) 1339142215Sglebius return (EADDRNOTAVAIL); 1340142215Sglebius 1341142215Sglebius ia = ia_if; 1342142215Sglebius ifp = ia->ia_ifp; 1343142215Sglebius 1344142215Sglebius if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 || 1345142215Sglebius (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp)) 1346142215Sglebius return (EADDRNOTAVAIL); 1347142215Sglebius 1348142215Sglebius if (imo->imo_num_memberships == 0) { 1349142215Sglebius addr.s_addr = htonl(INADDR_CARP_GROUP); 1350142215Sglebius if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL) 1351142215Sglebius return (ENOBUFS); 1352142215Sglebius imo->imo_num_memberships++; 1353142215Sglebius imo->imo_multicast_ifp = ifp; 1354142215Sglebius imo->imo_multicast_ttl = CARP_DFLTTL; 1355142215Sglebius imo->imo_multicast_loop = 0; 1356142215Sglebius } 1357142215Sglebius 1358142215Sglebius if (!ifp->if_carp) { 1359142215Sglebius 1360142215Sglebius MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP, 1361142215Sglebius M_WAITOK|M_ZERO); 1362142215Sglebius if (!cif) { 1363142215Sglebius error = ENOBUFS; 1364142215Sglebius goto cleanup; 1365142215Sglebius } 1366142215Sglebius if ((error = ifpromisc(ifp, 1))) { 1367142215Sglebius FREE(cif, M_CARP); 1368142215Sglebius goto cleanup; 1369142215Sglebius } 1370142215Sglebius 1371142215Sglebius CARP_LOCK_INIT(cif); 1372142215Sglebius CARP_LOCK(cif); 1373142215Sglebius cif->vhif_ifp = ifp; 1374142215Sglebius TAILQ_INIT(&cif->vhif_vrs); 1375142215Sglebius ifp->if_carp = cif; 1376142215Sglebius 1377142215Sglebius } else { 1378142215Sglebius struct carp_softc *vr; 1379142215Sglebius 1380142215Sglebius cif = (struct carp_if *)ifp->if_carp; 1381142215Sglebius CARP_LOCK(cif); 1382142215Sglebius TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) 1383142215Sglebius if (vr != sc && vr->sc_vhid == sc->sc_vhid) { 1384142215Sglebius CARP_UNLOCK(cif); 1385142215Sglebius error = EINVAL; 1386142215Sglebius goto cleanup; 1387142215Sglebius } 1388142215Sglebius } 1389142215Sglebius sc->sc_ia = ia; 1390142215Sglebius sc->sc_ifp = ifp; 1391142215Sglebius 1392142215Sglebius { /* XXX prevent endless loop if already in queue */ 1393142215Sglebius struct carp_softc *vr, *after = NULL; 1394142215Sglebius int myself = 0; 1395142215Sglebius cif = (struct carp_if *)ifp->if_carp; 1396142215Sglebius 1397142215Sglebius /* XXX: cif should not change, right? So we still hold the lock */ 1398142215Sglebius CARP_LOCK_ASSERT(cif); 1399142215Sglebius 1400142215Sglebius TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) { 1401142215Sglebius if (vr == sc) 1402142215Sglebius myself = 1; 1403142215Sglebius if (vr->sc_vhid < sc->sc_vhid) 1404142215Sglebius after = vr; 1405142215Sglebius } 1406142215Sglebius 1407142215Sglebius if (!myself) { 1408142215Sglebius /* We're trying to keep things in order */ 1409142215Sglebius if (after == NULL) { 1410142215Sglebius TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list); 1411142215Sglebius } else { 1412142215Sglebius TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list); 1413142215Sglebius } 1414142215Sglebius cif->vhif_nvrs++; 1415142215Sglebius } 1416142215Sglebius } 1417142215Sglebius 1418142215Sglebius CARP_UNLOCK(cif); 1419142215Sglebius 1420142215Sglebius sc->sc_naddrs++; 1421142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1422142215Sglebius if (own) 1423142215Sglebius sc->sc_advskew = 0; 1424142371Sglebius carp_carpdev_state(cif); 1425142215Sglebius carp_setrun(sc, 0); 1426142215Sglebius 1427142215Sglebius return (0); 1428142215Sglebius 1429142215Sglebiuscleanup: 1430142215Sglebius in_delmulti(imo->imo_membership[--imo->imo_num_memberships]); 1431142215Sglebius return (error); 1432142215Sglebius} 1433142215Sglebius 1434142215Sglebiusint 1435142215Sglebiuscarp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin) 1436142215Sglebius{ 1437142215Sglebius int error = 0; 1438142215Sglebius 1439142215Sglebius if (!--sc->sc_naddrs) { 1440142215Sglebius struct carp_if *cif = (struct carp_if *)sc->sc_ifp->if_carp; 1441142215Sglebius struct ip_moptions *imo = &sc->sc_imo; 1442142215Sglebius 1443142215Sglebius callout_stop(&sc->sc_ad_tmo); 1444142215Sglebius sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING); 1445142215Sglebius sc->sc_vhid = -1; 1446142215Sglebius in_delmulti(imo->imo_membership[--imo->imo_num_memberships]); 1447142215Sglebius imo->imo_multicast_ifp = NULL; 1448142215Sglebius CARP_LOCK(cif); 1449142215Sglebius TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list); 1450142215Sglebius if (!--cif->vhif_nvrs) { 1451142215Sglebius sc->sc_ifp->if_carp = NULL; 1452142215Sglebius CARP_LOCK_DESTROY(cif); 1453142215Sglebius FREE(cif, M_IFADDR); 1454142215Sglebius } else { 1455142215Sglebius CARP_UNLOCK(cif); 1456142215Sglebius } 1457142215Sglebius } 1458142215Sglebius 1459142215Sglebius return (error); 1460142215Sglebius} 1461142215Sglebius 1462142215Sglebius#ifdef INET6 1463142215Sglebiusint 1464142215Sglebiuscarp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6) 1465142215Sglebius{ 1466142215Sglebius struct ifnet *ifp; 1467142215Sglebius struct carp_if *cif; 1468142215Sglebius struct in6_ifaddr *ia, *ia_if; 1469142215Sglebius struct ip6_moptions *im6o = &sc->sc_im6o; 1470142215Sglebius struct in6_multi_mship *imm; 1471142215Sglebius struct sockaddr_in6 addr; 1472142215Sglebius int own, error; 1473142215Sglebius 1474142215Sglebius if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1475142215Sglebius if (!(sc->sc_if.if_flags & IFF_UP)) 1476142215Sglebius carp_set_state(sc, INIT); 1477142215Sglebius if (sc->sc_naddrs6) 1478142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1479142215Sglebius carp_setrun(sc, 0); 1480142215Sglebius return (0); 1481142215Sglebius } 1482142215Sglebius 1483142215Sglebius /* we have to do it by hands to check we won't match on us */ 1484142215Sglebius ia_if = NULL; own = 0; 1485142215Sglebius for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 1486142215Sglebius int i; 1487142215Sglebius 1488142215Sglebius for (i = 0; i < 4; i++) { 1489142215Sglebius if ((sin6->sin6_addr.s6_addr32[i] & 1490142215Sglebius ia->ia_prefixmask.sin6_addr.s6_addr32[i]) != 1491142215Sglebius (ia->ia_addr.sin6_addr.s6_addr32[i] & 1492142215Sglebius ia->ia_prefixmask.sin6_addr.s6_addr32[i])) 1493142215Sglebius break; 1494142215Sglebius } 1495142215Sglebius /* and, yeah, we need a multicast-capable iface too */ 1496142215Sglebius if (ia->ia_ifp != &sc->sc_ac.ac_if && 1497142215Sglebius (ia->ia_ifp->if_flags & IFF_MULTICAST) && 1498142215Sglebius (i == 4)) { 1499142215Sglebius if (!ia_if) 1500142215Sglebius ia_if = ia; 1501142215Sglebius if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 1502142215Sglebius &ia->ia_addr.sin6_addr)) 1503142215Sglebius own++; 1504142215Sglebius } 1505142215Sglebius } 1506142215Sglebius 1507142215Sglebius if (!ia_if) 1508142215Sglebius return (EADDRNOTAVAIL); 1509142215Sglebius ia = ia_if; 1510142215Sglebius ifp = ia->ia_ifp; 1511142215Sglebius 1512142215Sglebius if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 || 1513142215Sglebius (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp)) 1514142215Sglebius return (EADDRNOTAVAIL); 1515142215Sglebius 1516142215Sglebius if (!sc->sc_naddrs6) { 1517142215Sglebius im6o->im6o_multicast_ifp = ifp; 1518142215Sglebius 1519142215Sglebius /* join CARP multicast address */ 1520142215Sglebius bzero(&addr, sizeof(addr)); 1521142215Sglebius addr.sin6_family = AF_INET6; 1522142215Sglebius addr.sin6_len = sizeof(addr); 1523142215Sglebius addr.sin6_addr.s6_addr16[0] = htons(0xff02); 1524142215Sglebius addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 1525142215Sglebius addr.sin6_addr.s6_addr8[15] = 0x12; 1526142215Sglebius if ((imm = in6_joingroup(ifp, &addr.sin6_addr, &error)) == NULL) 1527142215Sglebius goto cleanup; 1528142215Sglebius LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 1529142215Sglebius 1530142215Sglebius /* join solicited multicast address */ 1531142215Sglebius bzero(&addr.sin6_addr, sizeof(addr.sin6_addr)); 1532142215Sglebius addr.sin6_addr.s6_addr16[0] = htons(0xff02); 1533142215Sglebius addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 1534142215Sglebius addr.sin6_addr.s6_addr32[1] = 0; 1535142215Sglebius addr.sin6_addr.s6_addr32[2] = htonl(1); 1536142215Sglebius addr.sin6_addr.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3]; 1537142215Sglebius addr.sin6_addr.s6_addr8[12] = 0xff; 1538142215Sglebius if ((imm = in6_joingroup(ifp, &addr.sin6_addr, &error)) == NULL) 1539142215Sglebius goto cleanup; 1540142215Sglebius LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 1541142215Sglebius } 1542142215Sglebius 1543142215Sglebius if (!ifp->if_carp) { 1544142215Sglebius MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP, 1545142215Sglebius M_WAITOK|M_ZERO); 1546142215Sglebius if (!cif) { 1547142215Sglebius error = ENOBUFS; 1548142215Sglebius goto cleanup; 1549142215Sglebius } 1550142215Sglebius if ((error = ifpromisc(ifp, 1))) { 1551142215Sglebius FREE(cif, M_CARP); 1552142215Sglebius goto cleanup; 1553142215Sglebius } 1554142215Sglebius 1555142215Sglebius CARP_LOCK_INIT(cif); 1556142215Sglebius CARP_LOCK(cif); 1557142215Sglebius cif->vhif_ifp = ifp; 1558142215Sglebius TAILQ_INIT(&cif->vhif_vrs); 1559142215Sglebius ifp->if_carp = cif; 1560142215Sglebius 1561142215Sglebius } else { 1562142215Sglebius struct carp_softc *vr; 1563142215Sglebius 1564142215Sglebius cif = (struct carp_if *)ifp->if_carp; 1565142215Sglebius CARP_LOCK(cif); 1566142215Sglebius TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) 1567142215Sglebius if (vr != sc && vr->sc_vhid == sc->sc_vhid) { 1568142215Sglebius CARP_UNLOCK(cif); 1569142215Sglebius error = EINVAL; 1570142215Sglebius goto cleanup; 1571142215Sglebius } 1572142215Sglebius } 1573142215Sglebius sc->sc_ia6 = ia; 1574142215Sglebius sc->sc_ifp = ifp; 1575142215Sglebius 1576142215Sglebius { /* XXX prevent endless loop if already in queue */ 1577142215Sglebius struct carp_softc *vr, *after = NULL; 1578142215Sglebius int myself = 0; 1579142215Sglebius cif = (struct carp_if *)ifp->if_carp; 1580142215Sglebius CARP_LOCK_ASSERT(cif); 1581142215Sglebius 1582142215Sglebius TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) { 1583142215Sglebius if (vr == sc) 1584142215Sglebius myself = 1; 1585142215Sglebius if (vr->sc_vhid < sc->sc_vhid) 1586142215Sglebius after = vr; 1587142215Sglebius } 1588142215Sglebius 1589142215Sglebius if (!myself) { 1590142215Sglebius /* We're trying to keep things in order */ 1591142215Sglebius if (after == NULL) { 1592142215Sglebius TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list); 1593142215Sglebius } else { 1594142215Sglebius TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list); 1595142215Sglebius } 1596142215Sglebius cif->vhif_nvrs++; 1597142215Sglebius } 1598142215Sglebius } 1599142215Sglebius 1600142215Sglebius CARP_UNLOCK(cif); 1601142215Sglebius sc->sc_naddrs6++; 1602142215Sglebius sc->sc_ac.ac_if.if_flags |= IFF_UP; 1603142215Sglebius if (own) 1604142215Sglebius sc->sc_advskew = 0; 1605142447Sglebius carp_carpdev_state(cif); 1606142215Sglebius carp_setrun(sc, 0); 1607142215Sglebius 1608142215Sglebius return (0); 1609142215Sglebius 1610142215Sglebiuscleanup: 1611142215Sglebius /* clean up multicast memberships */ 1612142215Sglebius if (!sc->sc_naddrs6) { 1613142215Sglebius while (!LIST_EMPTY(&im6o->im6o_memberships)) { 1614142215Sglebius imm = LIST_FIRST(&im6o->im6o_memberships); 1615142215Sglebius LIST_REMOVE(imm, i6mm_chain); 1616142215Sglebius in6_leavegroup(imm); 1617142215Sglebius } 1618142215Sglebius } 1619142215Sglebius return (error); 1620142215Sglebius} 1621142215Sglebius 1622142215Sglebiusint 1623142215Sglebiuscarp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6) 1624142215Sglebius{ 1625142215Sglebius int error = 0; 1626142215Sglebius 1627142215Sglebius if (!--sc->sc_naddrs6) { 1628142215Sglebius struct carp_if *cif = (struct carp_if *)sc->sc_ifp->if_carp; 1629142215Sglebius struct ip6_moptions *im6o = &sc->sc_im6o; 1630142215Sglebius 1631142215Sglebius callout_stop(&sc->sc_ad_tmo); 1632142215Sglebius sc->sc_ac.ac_if.if_flags &= ~(IFF_UP|IFF_RUNNING); 1633142215Sglebius sc->sc_vhid = -1; 1634142215Sglebius CARP_LOCK(cif); 1635142215Sglebius while (!LIST_EMPTY(&im6o->im6o_memberships)) { 1636142215Sglebius struct in6_multi_mship *imm = 1637142215Sglebius LIST_FIRST(&im6o->im6o_memberships); 1638142215Sglebius 1639142215Sglebius LIST_REMOVE(imm, i6mm_chain); 1640142215Sglebius in6_leavegroup(imm); 1641142215Sglebius } 1642142215Sglebius im6o->im6o_multicast_ifp = NULL; 1643142215Sglebius TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list); 1644142215Sglebius if (!--cif->vhif_nvrs) { 1645142215Sglebius CARP_LOCK_DESTROY(cif); 1646142215Sglebius sc->sc_ifp->if_carp = NULL; 1647142215Sglebius FREE(cif, M_IFADDR); 1648142215Sglebius } else 1649142215Sglebius CARP_UNLOCK(cif); 1650142215Sglebius } 1651142215Sglebius 1652142215Sglebius return (error); 1653142215Sglebius} 1654142215Sglebius#endif /* INET6 */ 1655142215Sglebius 1656142215Sglebiusint 1657142215Sglebiuscarp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr) 1658142215Sglebius{ 1659142215Sglebius struct carp_softc *sc = ifp->if_softc, *vr; 1660142215Sglebius struct carpreq carpr; 1661142215Sglebius struct ifaddr *ifa; 1662142215Sglebius struct ifreq *ifr; 1663142215Sglebius struct ifaliasreq *ifra; 1664142215Sglebius int error = 0; 1665142215Sglebius 1666142215Sglebius ifa = (struct ifaddr *)addr; 1667142215Sglebius ifra = (struct ifaliasreq *)addr; 1668142215Sglebius ifr = (struct ifreq *)addr; 1669142215Sglebius 1670142215Sglebius switch (cmd) { 1671142215Sglebius case SIOCSIFADDR: 1672142215Sglebius switch (ifa->ifa_addr->sa_family) { 1673142215Sglebius#ifdef INET 1674142215Sglebius case AF_INET: 1675142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1676142215Sglebius bcopy(ifa->ifa_addr, ifa->ifa_dstaddr, 1677142215Sglebius sizeof(struct sockaddr)); 1678142215Sglebius error = carp_set_addr(sc, satosin(ifa->ifa_addr)); 1679142215Sglebius break; 1680142215Sglebius#endif /* INET */ 1681142215Sglebius#ifdef INET6 1682142215Sglebius case AF_INET6: 1683142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1684142215Sglebius error = carp_set_addr6(sc, satosin6(ifa->ifa_addr)); 1685142215Sglebius break; 1686142215Sglebius#endif /* INET6 */ 1687142215Sglebius default: 1688142215Sglebius error = EAFNOSUPPORT; 1689142215Sglebius break; 1690142215Sglebius } 1691142215Sglebius break; 1692142215Sglebius 1693142215Sglebius case SIOCAIFADDR: 1694142215Sglebius switch (ifa->ifa_addr->sa_family) { 1695142215Sglebius#ifdef INET 1696142215Sglebius case AF_INET: 1697142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1698142215Sglebius bcopy(ifa->ifa_addr, ifa->ifa_dstaddr, 1699142215Sglebius sizeof(struct sockaddr)); 1700142215Sglebius error = carp_set_addr(sc, satosin(&ifra->ifra_addr)); 1701142215Sglebius break; 1702142215Sglebius#endif /* INET */ 1703142215Sglebius#ifdef INET6 1704142215Sglebius case AF_INET6: 1705142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1706142215Sglebius error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr)); 1707142215Sglebius break; 1708142215Sglebius#endif /* INET6 */ 1709142215Sglebius default: 1710142215Sglebius error = EAFNOSUPPORT; 1711142215Sglebius break; 1712142215Sglebius } 1713142215Sglebius break; 1714142215Sglebius 1715142215Sglebius case SIOCDIFADDR: 1716142215Sglebius sc->if_flags &= ~IFF_UP; 1717142215Sglebius switch (ifa->ifa_addr->sa_family) { 1718142215Sglebius#ifdef INET 1719142215Sglebius case AF_INET: 1720142215Sglebius error = carp_del_addr(sc, satosin(&ifra->ifra_addr)); 1721142215Sglebius break; 1722142215Sglebius#endif /* INET */ 1723142215Sglebius#ifdef INET6 1724142215Sglebius case AF_INET6: 1725142215Sglebius error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr)); 1726142215Sglebius break; 1727142215Sglebius#endif /* INET6 */ 1728142215Sglebius default: 1729142215Sglebius error = EAFNOSUPPORT; 1730142215Sglebius break; 1731142215Sglebius } 1732142215Sglebius break; 1733142215Sglebius 1734142215Sglebius case SIOCSIFFLAGS: 1735142215Sglebius if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) { 1736142215Sglebius sc->if_flags &= ~IFF_UP; 1737142215Sglebius callout_stop(&sc->sc_ad_tmo); 1738142215Sglebius callout_stop(&sc->sc_md_tmo); 1739142215Sglebius callout_stop(&sc->sc_md6_tmo); 1740142215Sglebius if (sc->sc_state == MASTER) 1741142215Sglebius carp_send_ad(sc); 1742142215Sglebius carp_set_state(sc, INIT); 1743142215Sglebius carp_setrun(sc, 0); 1744142215Sglebius } else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) { 1745142215Sglebius sc->sc_if.if_flags |= IFF_UP; 1746142215Sglebius carp_setrun(sc, 0); 1747142215Sglebius } 1748142215Sglebius break; 1749142215Sglebius 1750142215Sglebius case SIOCSVH: 1751142215Sglebius if ((error = suser(curthread)) != 0) 1752142215Sglebius break; 1753142215Sglebius if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr))) 1754142215Sglebius break; 1755142215Sglebius error = 1; 1756142215Sglebius if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) { 1757142215Sglebius switch (carpr.carpr_state) { 1758142215Sglebius case BACKUP: 1759142215Sglebius callout_stop(&sc->sc_ad_tmo); 1760142215Sglebius carp_set_state(sc, BACKUP); 1761142215Sglebius carp_setrun(sc, 0); 1762142215Sglebius carp_setroute(sc, RTM_DELETE); 1763142215Sglebius break; 1764142215Sglebius case MASTER: 1765142215Sglebius carp_master_down(sc); 1766142215Sglebius break; 1767142215Sglebius default: 1768142215Sglebius break; 1769142215Sglebius } 1770142215Sglebius } 1771142215Sglebius if (carpr.carpr_vhid > 0) { 1772142215Sglebius if (carpr.carpr_vhid > 255) { 1773142215Sglebius error = EINVAL; 1774142215Sglebius break; 1775142215Sglebius } 1776142215Sglebius if (sc->sc_ifp) { 1777142215Sglebius struct carp_if *cif; 1778142215Sglebius cif = (struct carp_if *)sc->sc_ifp->if_carp; 1779142215Sglebius CARP_LOCK(cif); 1780142215Sglebius TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) 1781142215Sglebius if (vr != sc && 1782142215Sglebius vr->sc_vhid == carpr.carpr_vhid) { 1783142215Sglebius CARP_UNLOCK(cif); 1784142215Sglebius return EINVAL; 1785142215Sglebius } 1786142215Sglebius CARP_UNLOCK(cif); 1787142215Sglebius } 1788142215Sglebius sc->sc_vhid = carpr.carpr_vhid; 1789142215Sglebius sc->sc_ac.ac_enaddr[0] = 0; 1790142215Sglebius sc->sc_ac.ac_enaddr[1] = 0; 1791142215Sglebius sc->sc_ac.ac_enaddr[2] = 0x5e; 1792142215Sglebius sc->sc_ac.ac_enaddr[3] = 0; 1793142215Sglebius sc->sc_ac.ac_enaddr[4] = 1; 1794142215Sglebius sc->sc_ac.ac_enaddr[5] = sc->sc_vhid; 1795142215Sglebius error--; 1796142215Sglebius } 1797142215Sglebius if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) { 1798142215Sglebius if (carpr.carpr_advskew >= 255) { 1799142215Sglebius error = EINVAL; 1800142215Sglebius break; 1801142215Sglebius } 1802142215Sglebius if (carpr.carpr_advbase > 255) { 1803142215Sglebius error = EINVAL; 1804142215Sglebius break; 1805142215Sglebius } 1806142215Sglebius sc->sc_advbase = carpr.carpr_advbase; 1807142215Sglebius sc->sc_advskew = carpr.carpr_advskew; 1808142215Sglebius error--; 1809142215Sglebius } 1810142215Sglebius bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key)); 1811142215Sglebius if (error > 0) 1812142215Sglebius error = EINVAL; 1813142215Sglebius else { 1814142215Sglebius error = 0; 1815142215Sglebius carp_setrun(sc, 0); 1816142215Sglebius } 1817142215Sglebius break; 1818142215Sglebius 1819142215Sglebius case SIOCGVH: 1820142215Sglebius bzero(&carpr, sizeof(carpr)); 1821142215Sglebius carpr.carpr_state = sc->sc_state; 1822142215Sglebius carpr.carpr_vhid = sc->sc_vhid; 1823142215Sglebius carpr.carpr_advbase = sc->sc_advbase; 1824142215Sglebius carpr.carpr_advskew = sc->sc_advskew; 1825142215Sglebius if (suser(curthread) == 0) 1826142215Sglebius bcopy(sc->sc_key, carpr.carpr_key, 1827142215Sglebius sizeof(carpr.carpr_key)); 1828142215Sglebius error = copyout(&carpr, ifr->ifr_data, sizeof(carpr)); 1829142215Sglebius break; 1830142215Sglebius 1831142215Sglebius default: 1832142215Sglebius error = EINVAL; 1833142215Sglebius } 1834142215Sglebius 1835142215Sglebius carp_hmac_prepare(sc); 1836142215Sglebius return (error); 1837142215Sglebius} 1838142215Sglebius 1839142215Sglebius/* 1840142215Sglebius * XXX: this is looutput. We should eventually use it from there. 1841142215Sglebius */ 1842142215Sglebiusstatic int 1843142215Sglebiuscarp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 1844142215Sglebius struct rtentry *rt) 1845142215Sglebius{ 1846142215Sglebius M_ASSERTPKTHDR(m); /* check if we have the packet header */ 1847142215Sglebius 1848142215Sglebius if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 1849142215Sglebius m_freem(m); 1850142215Sglebius return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 1851142215Sglebius rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 1852142215Sglebius } 1853142215Sglebius 1854142215Sglebius ifp->if_opackets++; 1855142215Sglebius ifp->if_obytes += m->m_pkthdr.len; 1856142215Sglebius#if 1 /* XXX */ 1857142215Sglebius switch (dst->sa_family) { 1858142215Sglebius case AF_INET: 1859142215Sglebius case AF_INET6: 1860142215Sglebius case AF_IPX: 1861142215Sglebius case AF_APPLETALK: 1862142215Sglebius break; 1863142215Sglebius default: 1864142215Sglebius printf("carp_looutput: af=%d unexpected\n", dst->sa_family); 1865142215Sglebius m_freem(m); 1866142215Sglebius return (EAFNOSUPPORT); 1867142215Sglebius } 1868142215Sglebius#endif 1869142215Sglebius return(if_simloop(ifp, m, dst->sa_family, 0)); 1870142215Sglebius} 1871142215Sglebius 1872142215Sglebius/* 1873142215Sglebius * Start output on carp interface. This function should never be called. 1874142215Sglebius */ 1875142215Sglebiusvoid 1876142215Sglebiuscarp_start(struct ifnet *ifp) 1877142215Sglebius{ 1878142215Sglebius#ifdef DEBUG 1879142215Sglebius printf("%s: start called\n", ifp->if_xname); 1880142215Sglebius#endif 1881142215Sglebius} 1882142215Sglebius 1883142215Sglebiusint 1884142215Sglebiuscarp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa, 1885142215Sglebius struct rtentry *rt) 1886142215Sglebius{ 1887142215Sglebius struct m_tag *mtag; 1888142215Sglebius struct carp_softc *sc; 1889142215Sglebius struct ifnet *carp_ifp; 1890142215Sglebius 1891142215Sglebius if (!sa) 1892142215Sglebius return (0); 1893142215Sglebius 1894142215Sglebius switch (sa->sa_family) { 1895142215Sglebius#ifdef INET 1896142215Sglebius case AF_INET: 1897142215Sglebius break; 1898142215Sglebius#endif /* INET */ 1899142215Sglebius#ifdef INET6 1900142215Sglebius case AF_INET6: 1901142215Sglebius break; 1902142215Sglebius#endif /* INET6 */ 1903142215Sglebius default: 1904142215Sglebius return (0); 1905142215Sglebius } 1906142215Sglebius 1907142215Sglebius mtag = m_tag_find(m, PACKET_TAG_CARP, NULL); 1908142215Sglebius if (mtag == NULL) 1909142215Sglebius return (0); 1910142215Sglebius 1911142215Sglebius bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *)); 1912142215Sglebius sc = carp_ifp->if_softc; 1913142215Sglebius 1914142215Sglebius /* Set the source MAC address to Virtual Router MAC Address */ 1915142215Sglebius switch (ifp->if_type) { 1916142215Sglebius case IFT_ETHER: { 1917142215Sglebius struct ether_header *eh; 1918142215Sglebius 1919142215Sglebius eh = mtod(m, struct ether_header *); 1920142215Sglebius eh->ether_shost[0] = 0; 1921142215Sglebius eh->ether_shost[1] = 0; 1922142215Sglebius eh->ether_shost[2] = 0x5e; 1923142215Sglebius eh->ether_shost[3] = 0; 1924142215Sglebius eh->ether_shost[4] = 1; 1925142215Sglebius eh->ether_shost[5] = sc->sc_vhid; 1926142215Sglebius } 1927142215Sglebius break; 1928142215Sglebius case IFT_FDDI: { 1929142215Sglebius struct fddi_header *fh; 1930142215Sglebius 1931142215Sglebius fh = mtod(m, struct fddi_header *); 1932142215Sglebius fh->fddi_shost[0] = 0; 1933142215Sglebius fh->fddi_shost[1] = 0; 1934142215Sglebius fh->fddi_shost[2] = 0x5e; 1935142215Sglebius fh->fddi_shost[3] = 0; 1936142215Sglebius fh->fddi_shost[4] = 1; 1937142215Sglebius fh->fddi_shost[5] = sc->sc_vhid; 1938142215Sglebius } 1939142215Sglebius break; 1940142215Sglebius case IFT_ISO88025: { 1941142215Sglebius struct iso88025_header *th; 1942142215Sglebius th = mtod(m, struct iso88025_header *); 1943142215Sglebius th->iso88025_shost[0] = 3; 1944142215Sglebius th->iso88025_shost[1] = 0; 1945142215Sglebius th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1); 1946142215Sglebius th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1); 1947142215Sglebius th->iso88025_shost[4] = 0; 1948142215Sglebius th->iso88025_shost[5] = 0; 1949142215Sglebius } 1950142215Sglebius break; 1951142215Sglebius default: 1952142215Sglebius printf("%s: carp is not supported for this interface type\n", 1953142215Sglebius ifp->if_xname); 1954142215Sglebius return (EOPNOTSUPP); 1955142215Sglebius } 1956142215Sglebius 1957142215Sglebius return (0); 1958142215Sglebius} 1959142215Sglebius 1960142215Sglebiusvoid 1961142215Sglebiuscarp_set_state(struct carp_softc *sc, int state) 1962142215Sglebius{ 1963142215Sglebius if (sc->sc_state == state) 1964142215Sglebius return; 1965142215Sglebius 1966142215Sglebius sc->sc_state = state; 1967142215Sglebius switch (state) { 1968142215Sglebius case BACKUP: 1969142215Sglebius sc->sc_ac.ac_if.if_link_state = LINK_STATE_DOWN; 1970142215Sglebius break; 1971142215Sglebius case MASTER: 1972142215Sglebius sc->sc_ac.ac_if.if_link_state = LINK_STATE_UP; 1973142215Sglebius break; 1974142215Sglebius default: 1975142215Sglebius sc->sc_ac.ac_if.if_link_state = LINK_STATE_UNKNOWN; 1976142215Sglebius break; 1977142215Sglebius } 1978142215Sglebius rt_ifmsg(&sc->sc_ac.ac_if); 1979142215Sglebius} 1980142215Sglebius 1981142215Sglebiusvoid 1982142215Sglebiuscarp_carpdev_state(void *v) 1983142215Sglebius{ 1984142215Sglebius struct carp_if *cif = v; 1985142215Sglebius struct carp_softc *sc; 1986142215Sglebius 1987142215Sglebius CARP_LOCK(cif); 1988142215Sglebius TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) { 1989142371Sglebius if (sc->sc_ifp->if_link_state != LINK_STATE_UP || 1990142215Sglebius !(sc->sc_ifp->if_flags & IFF_UP)) { 1991142215Sglebius sc->sc_flags_backup = sc->sc_if.if_flags; 1992142215Sglebius sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING); 1993142215Sglebius callout_stop(&sc->sc_ad_tmo); 1994142215Sglebius callout_stop(&sc->sc_md_tmo); 1995142215Sglebius callout_stop(&sc->sc_md6_tmo); 1996142215Sglebius carp_set_state(sc, INIT); 1997142215Sglebius carp_setrun(sc, 0); 1998142215Sglebius if (!sc->sc_suppress) { 1999142215Sglebius carp_suppress_preempt++; 2000142215Sglebius if (carp_suppress_preempt == 1) 2001142215Sglebius carp_send_ad_all(); 2002142215Sglebius } 2003142215Sglebius sc->sc_suppress = 1; 2004142215Sglebius } else { 2005142215Sglebius sc->sc_if.if_flags |= sc->sc_flags_backup; 2006142215Sglebius carp_set_state(sc, INIT); 2007142215Sglebius carp_setrun(sc, 0); 2008142215Sglebius if (sc->sc_suppress) 2009142215Sglebius carp_suppress_preempt--; 2010142215Sglebius sc->sc_suppress = 0; 2011142215Sglebius } 2012142215Sglebius } 2013142215Sglebius CARP_UNLOCK(cif); 2014142215Sglebius} 2015142215Sglebius 2016142215Sglebiusstatic int 2017142215Sglebiuscarp_modevent(module_t mod, int type, void *data) 2018142215Sglebius{ 2019142215Sglebius int error = 0; 2020142215Sglebius 2021142215Sglebius switch (type) { 2022142215Sglebius case MOD_LOAD: 2023142215Sglebius LIST_INIT(&carpif_list); 2024142215Sglebius if_clone_attach(&carp_cloner); 2025142215Sglebius printf("carp: attached\n"); 2026142215Sglebius break; 2027142215Sglebius 2028142215Sglebius case MOD_UNLOAD: 2029142215Sglebius if_clone_detach(&carp_cloner); 2030142215Sglebius while (!LIST_EMPTY(&carpif_list)) 2031142215Sglebius carp_clone_destroy( 2032142215Sglebius &LIST_FIRST(&carpif_list)->sc_if); 2033142215Sglebius break; 2034142215Sglebius 2035142215Sglebius default: 2036142215Sglebius error = EINVAL; 2037142215Sglebius break; 2038142215Sglebius } 2039142215Sglebius 2040142215Sglebius return error; 2041142215Sglebius} 2042142215Sglebius 2043142215Sglebiusstatic moduledata_t carp_mod = { 2044142215Sglebius "carp", 2045142215Sglebius carp_modevent, 2046142215Sglebius 0 2047142215Sglebius}; 2048142215Sglebius 2049142215SglebiusDECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 2050