Deleted Added
full compact
ip_carp.c (191528) ip_carp.c (191672)
1/*
2 * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3 * Copyright (c) 2003 Ryan McBride. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*
2 * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3 * Copyright (c) 2003 Ryan McBride. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/netinet/ip_carp.c 191528 2009-04-26 19:05:40Z rwatson $");
28__FBSDID("$FreeBSD: head/sys/netinet/ip_carp.c 191672 2009-04-29 19:19:13Z bms $");
29
30#include "opt_carp.h"
31#include "opt_bpf.h"
32#include "opt_inet.h"
33#include "opt_inet6.h"
34
35#include <sys/types.h>
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/conf.h>
39#include <sys/kernel.h>
40#include <sys/limits.h>
41#include <sys/malloc.h>
42#include <sys/mbuf.h>
43#include <sys/module.h>
44#include <sys/time.h>
45#include <sys/priv.h>
46#include <sys/proc.h>
47#include <sys/sysctl.h>
48#include <sys/syslog.h>
49#include <sys/signalvar.h>
50#include <sys/filio.h>
51#include <sys/sockio.h>
52
53#include <sys/socket.h>
54#include <sys/vnode.h>
55#include <sys/vimage.h>
56
57#include <machine/stdarg.h>
58
59#include <net/bpf.h>
60#include <net/ethernet.h>
61#include <net/fddi.h>
62#include <net/iso88025.h>
63#include <net/if.h>
64#include <net/if_clone.h>
65#include <net/if_dl.h>
66#include <net/if_types.h>
67#include <net/route.h>
68
69#ifdef INET
70#include <netinet/in.h>
71#include <netinet/in_var.h>
72#include <netinet/in_systm.h>
73#include <netinet/ip.h>
74#include <netinet/ip_var.h>
75#include <netinet/if_ether.h>
76#include <machine/in_cksum.h>
77#include <netinet/vinet.h>
78#endif
79
80#ifdef INET6
81#include <netinet/icmp6.h>
82#include <netinet/ip6.h>
83#include <netinet6/ip6_var.h>
84#include <netinet6/scope6_var.h>
85#include <netinet6/nd6.h>
86#include <netinet6/vinet6.h>
87#endif
88
89#include <crypto/sha1.h>
90#include <netinet/ip_carp.h>
91
92#define CARP_IFNAME "carp"
93static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
94SYSCTL_DECL(_net_inet_carp);
95
96struct carp_softc {
97 struct ifnet *sc_ifp; /* Interface clue */
98 struct ifnet *sc_carpdev; /* Pointer to parent interface */
99 struct in_ifaddr *sc_ia; /* primary iface address */
100 struct ip_moptions sc_imo;
101#ifdef INET6
102 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
103 struct ip6_moptions sc_im6o;
104#endif /* INET6 */
105 TAILQ_ENTRY(carp_softc) sc_list;
106
107 enum { INIT = 0, BACKUP, MASTER } sc_state;
108
109 int sc_flags_backup;
110 int sc_suppress;
111
112 int sc_sendad_errors;
113#define CARP_SENDAD_MAX_ERRORS 3
114 int sc_sendad_success;
115#define CARP_SENDAD_MIN_SUCCESS 3
116
117 int sc_vhid;
118 int sc_advskew;
119 int sc_naddrs;
120 int sc_naddrs6;
121 int sc_advbase; /* seconds */
122 int sc_init_counter;
123 u_int64_t sc_counter;
124
125 /* authentication */
126#define CARP_HMAC_PAD 64
127 unsigned char sc_key[CARP_KEY_LEN];
128 unsigned char sc_pad[CARP_HMAC_PAD];
129 SHA1_CTX sc_sha1;
130
131 struct callout sc_ad_tmo; /* advertisement timeout */
132 struct callout sc_md_tmo; /* master down timeout */
133 struct callout sc_md6_tmo; /* master down timeout */
134
135 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
136};
137#define SC2IFP(sc) ((sc)->sc_ifp)
138
139int carp_suppress_preempt = 0;
140int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
141SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
142 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
143SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
144 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
145SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
146 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
147SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
148 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
149SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
150 &carp_suppress_preempt, 0, "Preemption is suppressed");
151
152struct carpstats carpstats;
153SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
154 &carpstats, carpstats,
155 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
156
157struct carp_if {
158 TAILQ_HEAD(, carp_softc) vhif_vrs;
159 int vhif_nvrs;
160
161 struct ifnet *vhif_ifp;
162 struct mtx vhif_mtx;
163};
164
165/* Get carp_if from softc. Valid after carp_set_addr{,6}. */
166#define SC2CIF(sc) ((struct carp_if *)(sc)->sc_carpdev->if_carp)
167
168/* lock per carp_if queue */
169#define CARP_LOCK_INIT(cif) mtx_init(&(cif)->vhif_mtx, "carp_if", \
170 NULL, MTX_DEF)
171#define CARP_LOCK_DESTROY(cif) mtx_destroy(&(cif)->vhif_mtx)
172#define CARP_LOCK_ASSERT(cif) mtx_assert(&(cif)->vhif_mtx, MA_OWNED)
173#define CARP_LOCK(cif) mtx_lock(&(cif)->vhif_mtx)
174#define CARP_UNLOCK(cif) mtx_unlock(&(cif)->vhif_mtx)
175
176#define CARP_SCLOCK(sc) mtx_lock(&SC2CIF(sc)->vhif_mtx)
177#define CARP_SCUNLOCK(sc) mtx_unlock(&SC2CIF(sc)->vhif_mtx)
178#define CARP_SCLOCK_ASSERT(sc) mtx_assert(&SC2CIF(sc)->vhif_mtx, MA_OWNED)
179
180#define CARP_LOG(...) do { \
181 if (carp_opts[CARPCTL_LOG] > 0) \
182 log(LOG_INFO, __VA_ARGS__); \
183} while (0)
184
185#define CARP_DEBUG(...) do { \
186 if (carp_opts[CARPCTL_LOG] > 1) \
187 log(LOG_DEBUG, __VA_ARGS__); \
188} while (0)
189
190static void carp_hmac_prepare(struct carp_softc *);
191static void carp_hmac_generate(struct carp_softc *, u_int32_t *,
192 unsigned char *);
193static int carp_hmac_verify(struct carp_softc *, u_int32_t *,
194 unsigned char *);
195static void carp_setroute(struct carp_softc *, int);
196static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
197static int carp_clone_create(struct if_clone *, int, caddr_t);
198static void carp_clone_destroy(struct ifnet *);
199static void carpdetach(struct carp_softc *, int);
200static int carp_prepare_ad(struct mbuf *, struct carp_softc *,
201 struct carp_header *);
202static void carp_send_ad_all(void);
203static void carp_send_ad(void *);
204static void carp_send_ad_locked(struct carp_softc *);
205static void carp_send_arp(struct carp_softc *);
206static void carp_master_down(void *);
207static void carp_master_down_locked(struct carp_softc *);
208static int carp_ioctl(struct ifnet *, u_long, caddr_t);
209static int carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
210 struct route *);
211static void carp_start(struct ifnet *);
212static void carp_setrun(struct carp_softc *, sa_family_t);
213static void carp_set_state(struct carp_softc *, int);
214static int carp_addrcount(struct carp_if *, struct in_ifaddr *, int);
215enum { CARP_COUNT_MASTER, CARP_COUNT_RUNNING };
216
217static void carp_multicast_cleanup(struct carp_softc *);
218static int carp_set_addr(struct carp_softc *, struct sockaddr_in *);
219static int carp_del_addr(struct carp_softc *, struct sockaddr_in *);
220static void carp_carpdev_state_locked(struct carp_if *);
221static void carp_sc_state_locked(struct carp_softc *);
222#ifdef INET6
223static void carp_send_na(struct carp_softc *);
224static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
225static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
226static void carp_multicast6_cleanup(struct carp_softc *);
227#endif
228
229static LIST_HEAD(, carp_softc) carpif_list;
230static struct mtx carp_mtx;
231IFC_SIMPLE_DECLARE(carp, 0);
232
233static eventhandler_tag if_detach_event_tag;
234
235static __inline u_int16_t
236carp_cksum(struct mbuf *m, int len)
237{
238 return (in_cksum(m, len));
239}
240
241static void
242carp_hmac_prepare(struct carp_softc *sc)
243{
244 u_int8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
245 u_int8_t vhid = sc->sc_vhid & 0xff;
246 struct ifaddr *ifa;
247 int i, found;
248#ifdef INET
249 struct in_addr last, cur, in;
250#endif
251#ifdef INET6
252 struct in6_addr last6, cur6, in6;
253#endif
254
255 if (sc->sc_carpdev)
256 CARP_SCLOCK(sc);
257
258 /* XXX: possible race here */
259
260 /* compute ipad from key */
261 bzero(sc->sc_pad, sizeof(sc->sc_pad));
262 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
263 for (i = 0; i < sizeof(sc->sc_pad); i++)
264 sc->sc_pad[i] ^= 0x36;
265
266 /* precompute first part of inner hash */
267 SHA1Init(&sc->sc_sha1);
268 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
269 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
270 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
271 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
272#ifdef INET
273 cur.s_addr = 0;
274 do {
275 found = 0;
276 last = cur;
277 cur.s_addr = 0xffffffff;
278 IF_ADDR_LOCK(SC2IFP(sc));
279 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
280 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
281 if (ifa->ifa_addr->sa_family == AF_INET &&
282 ntohl(in.s_addr) > ntohl(last.s_addr) &&
283 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
284 cur.s_addr = in.s_addr;
285 found++;
286 }
287 }
288 IF_ADDR_UNLOCK(SC2IFP(sc));
289 if (found)
290 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
291 } while (found);
292#endif /* INET */
293#ifdef INET6
294 memset(&cur6, 0, sizeof(cur6));
295 do {
296 found = 0;
297 last6 = cur6;
298 memset(&cur6, 0xff, sizeof(cur6));
299 IF_ADDR_LOCK(SC2IFP(sc));
300 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
301 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
302 if (IN6_IS_SCOPE_EMBED(&in6))
303 in6.s6_addr16[1] = 0;
304 if (ifa->ifa_addr->sa_family == AF_INET6 &&
305 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
306 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
307 cur6 = in6;
308 found++;
309 }
310 }
311 IF_ADDR_UNLOCK(SC2IFP(sc));
312 if (found)
313 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
314 } while (found);
315#endif /* INET6 */
316
317 /* convert ipad to opad */
318 for (i = 0; i < sizeof(sc->sc_pad); i++)
319 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
320
321 if (sc->sc_carpdev)
322 CARP_SCUNLOCK(sc);
323}
324
325static void
326carp_hmac_generate(struct carp_softc *sc, u_int32_t counter[2],
327 unsigned char md[20])
328{
329 SHA1_CTX sha1ctx;
330
331 /* fetch first half of inner hash */
332 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
333
334 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
335 SHA1Final(md, &sha1ctx);
336
337 /* outer hash */
338 SHA1Init(&sha1ctx);
339 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
340 SHA1Update(&sha1ctx, md, 20);
341 SHA1Final(md, &sha1ctx);
342}
343
344static int
345carp_hmac_verify(struct carp_softc *sc, u_int32_t counter[2],
346 unsigned char md[20])
347{
348 unsigned char md2[20];
349
350 CARP_SCLOCK_ASSERT(sc);
351
352 carp_hmac_generate(sc, counter, md2);
353
354 return (bcmp(md, md2, sizeof(md2)));
355}
356
357static void
358carp_setroute(struct carp_softc *sc, int cmd)
359{
360 struct ifaddr *ifa;
361 int s;
362
363 if (sc->sc_carpdev)
364 CARP_SCLOCK_ASSERT(sc);
365
366 s = splnet();
367 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
368 if (ifa->ifa_addr->sa_family == AF_INET &&
369 sc->sc_carpdev != NULL) {
370 int count = carp_addrcount(
371 (struct carp_if *)sc->sc_carpdev->if_carp,
372 ifatoia(ifa), CARP_COUNT_MASTER);
373
374 if ((cmd == RTM_ADD && count == 1) ||
375 (cmd == RTM_DELETE && count == 0))
376 rtinit(ifa, cmd, RTF_UP | RTF_HOST);
377 }
378 }
379 splx(s);
380}
381
382static int
383carp_clone_create(struct if_clone *ifc, int unit, caddr_t params)
384{
385
386 struct carp_softc *sc;
387 struct ifnet *ifp;
388
389 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
390 ifp = SC2IFP(sc) = if_alloc(IFT_ETHER);
391 if (ifp == NULL) {
392 free(sc, M_CARP);
393 return (ENOSPC);
394 }
395
396 sc->sc_flags_backup = 0;
397 sc->sc_suppress = 0;
398 sc->sc_advbase = CARP_DFLTINTV;
399 sc->sc_vhid = -1; /* required setting */
400 sc->sc_advskew = 0;
401 sc->sc_init_counter = 1;
402 sc->sc_naddrs = sc->sc_naddrs6 = 0; /* M_ZERO? */
29
30#include "opt_carp.h"
31#include "opt_bpf.h"
32#include "opt_inet.h"
33#include "opt_inet6.h"
34
35#include <sys/types.h>
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/conf.h>
39#include <sys/kernel.h>
40#include <sys/limits.h>
41#include <sys/malloc.h>
42#include <sys/mbuf.h>
43#include <sys/module.h>
44#include <sys/time.h>
45#include <sys/priv.h>
46#include <sys/proc.h>
47#include <sys/sysctl.h>
48#include <sys/syslog.h>
49#include <sys/signalvar.h>
50#include <sys/filio.h>
51#include <sys/sockio.h>
52
53#include <sys/socket.h>
54#include <sys/vnode.h>
55#include <sys/vimage.h>
56
57#include <machine/stdarg.h>
58
59#include <net/bpf.h>
60#include <net/ethernet.h>
61#include <net/fddi.h>
62#include <net/iso88025.h>
63#include <net/if.h>
64#include <net/if_clone.h>
65#include <net/if_dl.h>
66#include <net/if_types.h>
67#include <net/route.h>
68
69#ifdef INET
70#include <netinet/in.h>
71#include <netinet/in_var.h>
72#include <netinet/in_systm.h>
73#include <netinet/ip.h>
74#include <netinet/ip_var.h>
75#include <netinet/if_ether.h>
76#include <machine/in_cksum.h>
77#include <netinet/vinet.h>
78#endif
79
80#ifdef INET6
81#include <netinet/icmp6.h>
82#include <netinet/ip6.h>
83#include <netinet6/ip6_var.h>
84#include <netinet6/scope6_var.h>
85#include <netinet6/nd6.h>
86#include <netinet6/vinet6.h>
87#endif
88
89#include <crypto/sha1.h>
90#include <netinet/ip_carp.h>
91
92#define CARP_IFNAME "carp"
93static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
94SYSCTL_DECL(_net_inet_carp);
95
96struct carp_softc {
97 struct ifnet *sc_ifp; /* Interface clue */
98 struct ifnet *sc_carpdev; /* Pointer to parent interface */
99 struct in_ifaddr *sc_ia; /* primary iface address */
100 struct ip_moptions sc_imo;
101#ifdef INET6
102 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
103 struct ip6_moptions sc_im6o;
104#endif /* INET6 */
105 TAILQ_ENTRY(carp_softc) sc_list;
106
107 enum { INIT = 0, BACKUP, MASTER } sc_state;
108
109 int sc_flags_backup;
110 int sc_suppress;
111
112 int sc_sendad_errors;
113#define CARP_SENDAD_MAX_ERRORS 3
114 int sc_sendad_success;
115#define CARP_SENDAD_MIN_SUCCESS 3
116
117 int sc_vhid;
118 int sc_advskew;
119 int sc_naddrs;
120 int sc_naddrs6;
121 int sc_advbase; /* seconds */
122 int sc_init_counter;
123 u_int64_t sc_counter;
124
125 /* authentication */
126#define CARP_HMAC_PAD 64
127 unsigned char sc_key[CARP_KEY_LEN];
128 unsigned char sc_pad[CARP_HMAC_PAD];
129 SHA1_CTX sc_sha1;
130
131 struct callout sc_ad_tmo; /* advertisement timeout */
132 struct callout sc_md_tmo; /* master down timeout */
133 struct callout sc_md6_tmo; /* master down timeout */
134
135 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
136};
137#define SC2IFP(sc) ((sc)->sc_ifp)
138
139int carp_suppress_preempt = 0;
140int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
141SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
142 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
143SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
144 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
145SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
146 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
147SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
148 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
149SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
150 &carp_suppress_preempt, 0, "Preemption is suppressed");
151
152struct carpstats carpstats;
153SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
154 &carpstats, carpstats,
155 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
156
157struct carp_if {
158 TAILQ_HEAD(, carp_softc) vhif_vrs;
159 int vhif_nvrs;
160
161 struct ifnet *vhif_ifp;
162 struct mtx vhif_mtx;
163};
164
165/* Get carp_if from softc. Valid after carp_set_addr{,6}. */
166#define SC2CIF(sc) ((struct carp_if *)(sc)->sc_carpdev->if_carp)
167
168/* lock per carp_if queue */
169#define CARP_LOCK_INIT(cif) mtx_init(&(cif)->vhif_mtx, "carp_if", \
170 NULL, MTX_DEF)
171#define CARP_LOCK_DESTROY(cif) mtx_destroy(&(cif)->vhif_mtx)
172#define CARP_LOCK_ASSERT(cif) mtx_assert(&(cif)->vhif_mtx, MA_OWNED)
173#define CARP_LOCK(cif) mtx_lock(&(cif)->vhif_mtx)
174#define CARP_UNLOCK(cif) mtx_unlock(&(cif)->vhif_mtx)
175
176#define CARP_SCLOCK(sc) mtx_lock(&SC2CIF(sc)->vhif_mtx)
177#define CARP_SCUNLOCK(sc) mtx_unlock(&SC2CIF(sc)->vhif_mtx)
178#define CARP_SCLOCK_ASSERT(sc) mtx_assert(&SC2CIF(sc)->vhif_mtx, MA_OWNED)
179
180#define CARP_LOG(...) do { \
181 if (carp_opts[CARPCTL_LOG] > 0) \
182 log(LOG_INFO, __VA_ARGS__); \
183} while (0)
184
185#define CARP_DEBUG(...) do { \
186 if (carp_opts[CARPCTL_LOG] > 1) \
187 log(LOG_DEBUG, __VA_ARGS__); \
188} while (0)
189
190static void carp_hmac_prepare(struct carp_softc *);
191static void carp_hmac_generate(struct carp_softc *, u_int32_t *,
192 unsigned char *);
193static int carp_hmac_verify(struct carp_softc *, u_int32_t *,
194 unsigned char *);
195static void carp_setroute(struct carp_softc *, int);
196static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
197static int carp_clone_create(struct if_clone *, int, caddr_t);
198static void carp_clone_destroy(struct ifnet *);
199static void carpdetach(struct carp_softc *, int);
200static int carp_prepare_ad(struct mbuf *, struct carp_softc *,
201 struct carp_header *);
202static void carp_send_ad_all(void);
203static void carp_send_ad(void *);
204static void carp_send_ad_locked(struct carp_softc *);
205static void carp_send_arp(struct carp_softc *);
206static void carp_master_down(void *);
207static void carp_master_down_locked(struct carp_softc *);
208static int carp_ioctl(struct ifnet *, u_long, caddr_t);
209static int carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
210 struct route *);
211static void carp_start(struct ifnet *);
212static void carp_setrun(struct carp_softc *, sa_family_t);
213static void carp_set_state(struct carp_softc *, int);
214static int carp_addrcount(struct carp_if *, struct in_ifaddr *, int);
215enum { CARP_COUNT_MASTER, CARP_COUNT_RUNNING };
216
217static void carp_multicast_cleanup(struct carp_softc *);
218static int carp_set_addr(struct carp_softc *, struct sockaddr_in *);
219static int carp_del_addr(struct carp_softc *, struct sockaddr_in *);
220static void carp_carpdev_state_locked(struct carp_if *);
221static void carp_sc_state_locked(struct carp_softc *);
222#ifdef INET6
223static void carp_send_na(struct carp_softc *);
224static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
225static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
226static void carp_multicast6_cleanup(struct carp_softc *);
227#endif
228
229static LIST_HEAD(, carp_softc) carpif_list;
230static struct mtx carp_mtx;
231IFC_SIMPLE_DECLARE(carp, 0);
232
233static eventhandler_tag if_detach_event_tag;
234
235static __inline u_int16_t
236carp_cksum(struct mbuf *m, int len)
237{
238 return (in_cksum(m, len));
239}
240
241static void
242carp_hmac_prepare(struct carp_softc *sc)
243{
244 u_int8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
245 u_int8_t vhid = sc->sc_vhid & 0xff;
246 struct ifaddr *ifa;
247 int i, found;
248#ifdef INET
249 struct in_addr last, cur, in;
250#endif
251#ifdef INET6
252 struct in6_addr last6, cur6, in6;
253#endif
254
255 if (sc->sc_carpdev)
256 CARP_SCLOCK(sc);
257
258 /* XXX: possible race here */
259
260 /* compute ipad from key */
261 bzero(sc->sc_pad, sizeof(sc->sc_pad));
262 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
263 for (i = 0; i < sizeof(sc->sc_pad); i++)
264 sc->sc_pad[i] ^= 0x36;
265
266 /* precompute first part of inner hash */
267 SHA1Init(&sc->sc_sha1);
268 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
269 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
270 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
271 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
272#ifdef INET
273 cur.s_addr = 0;
274 do {
275 found = 0;
276 last = cur;
277 cur.s_addr = 0xffffffff;
278 IF_ADDR_LOCK(SC2IFP(sc));
279 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
280 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
281 if (ifa->ifa_addr->sa_family == AF_INET &&
282 ntohl(in.s_addr) > ntohl(last.s_addr) &&
283 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
284 cur.s_addr = in.s_addr;
285 found++;
286 }
287 }
288 IF_ADDR_UNLOCK(SC2IFP(sc));
289 if (found)
290 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
291 } while (found);
292#endif /* INET */
293#ifdef INET6
294 memset(&cur6, 0, sizeof(cur6));
295 do {
296 found = 0;
297 last6 = cur6;
298 memset(&cur6, 0xff, sizeof(cur6));
299 IF_ADDR_LOCK(SC2IFP(sc));
300 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
301 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
302 if (IN6_IS_SCOPE_EMBED(&in6))
303 in6.s6_addr16[1] = 0;
304 if (ifa->ifa_addr->sa_family == AF_INET6 &&
305 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
306 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
307 cur6 = in6;
308 found++;
309 }
310 }
311 IF_ADDR_UNLOCK(SC2IFP(sc));
312 if (found)
313 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
314 } while (found);
315#endif /* INET6 */
316
317 /* convert ipad to opad */
318 for (i = 0; i < sizeof(sc->sc_pad); i++)
319 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
320
321 if (sc->sc_carpdev)
322 CARP_SCUNLOCK(sc);
323}
324
325static void
326carp_hmac_generate(struct carp_softc *sc, u_int32_t counter[2],
327 unsigned char md[20])
328{
329 SHA1_CTX sha1ctx;
330
331 /* fetch first half of inner hash */
332 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
333
334 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
335 SHA1Final(md, &sha1ctx);
336
337 /* outer hash */
338 SHA1Init(&sha1ctx);
339 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
340 SHA1Update(&sha1ctx, md, 20);
341 SHA1Final(md, &sha1ctx);
342}
343
344static int
345carp_hmac_verify(struct carp_softc *sc, u_int32_t counter[2],
346 unsigned char md[20])
347{
348 unsigned char md2[20];
349
350 CARP_SCLOCK_ASSERT(sc);
351
352 carp_hmac_generate(sc, counter, md2);
353
354 return (bcmp(md, md2, sizeof(md2)));
355}
356
357static void
358carp_setroute(struct carp_softc *sc, int cmd)
359{
360 struct ifaddr *ifa;
361 int s;
362
363 if (sc->sc_carpdev)
364 CARP_SCLOCK_ASSERT(sc);
365
366 s = splnet();
367 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
368 if (ifa->ifa_addr->sa_family == AF_INET &&
369 sc->sc_carpdev != NULL) {
370 int count = carp_addrcount(
371 (struct carp_if *)sc->sc_carpdev->if_carp,
372 ifatoia(ifa), CARP_COUNT_MASTER);
373
374 if ((cmd == RTM_ADD && count == 1) ||
375 (cmd == RTM_DELETE && count == 0))
376 rtinit(ifa, cmd, RTF_UP | RTF_HOST);
377 }
378 }
379 splx(s);
380}
381
382static int
383carp_clone_create(struct if_clone *ifc, int unit, caddr_t params)
384{
385
386 struct carp_softc *sc;
387 struct ifnet *ifp;
388
389 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
390 ifp = SC2IFP(sc) = if_alloc(IFT_ETHER);
391 if (ifp == NULL) {
392 free(sc, M_CARP);
393 return (ENOSPC);
394 }
395
396 sc->sc_flags_backup = 0;
397 sc->sc_suppress = 0;
398 sc->sc_advbase = CARP_DFLTINTV;
399 sc->sc_vhid = -1; /* required setting */
400 sc->sc_advskew = 0;
401 sc->sc_init_counter = 1;
402 sc->sc_naddrs = sc->sc_naddrs6 = 0; /* M_ZERO? */
403#ifdef INET6
404 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
405#endif
406 sc->sc_imo.imo_membership = (struct in_multi **)malloc(
407 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
408 M_WAITOK);
409 sc->sc_imo.imo_mfilters = NULL;
410 sc->sc_imo.imo_max_memberships = IP_MIN_MEMBERSHIPS;
411 sc->sc_imo.imo_multicast_vif = -1;
403 sc->sc_imo.imo_membership = (struct in_multi **)malloc(
404 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
405 M_WAITOK);
406 sc->sc_imo.imo_mfilters = NULL;
407 sc->sc_imo.imo_max_memberships = IP_MIN_MEMBERSHIPS;
408 sc->sc_imo.imo_multicast_vif = -1;
409#ifdef INET6
410 sc->sc_im6o.im6o_membership = (struct in6_multi **)malloc(
411 (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
412 M_WAITOK);
413 sc->sc_im6o.im6o_mfilters = NULL;
414 sc->sc_im6o.im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
415 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
416#endif
412
413 callout_init(&sc->sc_ad_tmo, CALLOUT_MPSAFE);
414 callout_init(&sc->sc_md_tmo, CALLOUT_MPSAFE);
415 callout_init(&sc->sc_md6_tmo, CALLOUT_MPSAFE);
416
417 ifp->if_softc = sc;
418 if_initname(ifp, CARP_IFNAME, unit);
419 ifp->if_mtu = ETHERMTU;
420 ifp->if_flags = IFF_LOOPBACK;
421 ifp->if_ioctl = carp_ioctl;
422 ifp->if_output = carp_looutput;
423 ifp->if_start = carp_start;
424 ifp->if_type = IFT_CARP;
425 ifp->if_snd.ifq_maxlen = ifqmaxlen;
426 ifp->if_hdrlen = 0;
427 if_attach(ifp);
428 bpfattach(SC2IFP(sc), DLT_NULL, sizeof(u_int32_t));
429 mtx_lock(&carp_mtx);
430 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
431 mtx_unlock(&carp_mtx);
432 return (0);
433}
434
435static void
436carp_clone_destroy(struct ifnet *ifp)
437{
438 struct carp_softc *sc = ifp->if_softc;
439
440 if (sc->sc_carpdev)
441 CARP_SCLOCK(sc);
442 carpdetach(sc, 1); /* Returns unlocked. */
443
444 mtx_lock(&carp_mtx);
445 LIST_REMOVE(sc, sc_next);
446 mtx_unlock(&carp_mtx);
447 bpfdetach(ifp);
448 if_detach(ifp);
449 if_free_type(ifp, IFT_ETHER);
450 free(sc->sc_imo.imo_membership, M_CARP);
417
418 callout_init(&sc->sc_ad_tmo, CALLOUT_MPSAFE);
419 callout_init(&sc->sc_md_tmo, CALLOUT_MPSAFE);
420 callout_init(&sc->sc_md6_tmo, CALLOUT_MPSAFE);
421
422 ifp->if_softc = sc;
423 if_initname(ifp, CARP_IFNAME, unit);
424 ifp->if_mtu = ETHERMTU;
425 ifp->if_flags = IFF_LOOPBACK;
426 ifp->if_ioctl = carp_ioctl;
427 ifp->if_output = carp_looutput;
428 ifp->if_start = carp_start;
429 ifp->if_type = IFT_CARP;
430 ifp->if_snd.ifq_maxlen = ifqmaxlen;
431 ifp->if_hdrlen = 0;
432 if_attach(ifp);
433 bpfattach(SC2IFP(sc), DLT_NULL, sizeof(u_int32_t));
434 mtx_lock(&carp_mtx);
435 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
436 mtx_unlock(&carp_mtx);
437 return (0);
438}
439
440static void
441carp_clone_destroy(struct ifnet *ifp)
442{
443 struct carp_softc *sc = ifp->if_softc;
444
445 if (sc->sc_carpdev)
446 CARP_SCLOCK(sc);
447 carpdetach(sc, 1); /* Returns unlocked. */
448
449 mtx_lock(&carp_mtx);
450 LIST_REMOVE(sc, sc_next);
451 mtx_unlock(&carp_mtx);
452 bpfdetach(ifp);
453 if_detach(ifp);
454 if_free_type(ifp, IFT_ETHER);
455 free(sc->sc_imo.imo_membership, M_CARP);
456#ifdef INET6
457 free(sc->sc_im6o.im6o_membership, M_CARP);
458#endif
451 free(sc, M_CARP);
452}
453
454/*
455 * This function can be called on CARP interface destroy path,
456 * and in case of the removal of the underlying interface as
457 * well. We differentiate these two cases. In the latter case
458 * we do not cleanup our multicast memberships, since they
459 * are already freed. Also, in the latter case we do not
460 * release the lock on return, because the function will be
461 * called once more, for another CARP instance on the same
462 * interface.
463 */
464static void
465carpdetach(struct carp_softc *sc, int unlock)
466{
467 struct carp_if *cif;
468
469 callout_stop(&sc->sc_ad_tmo);
470 callout_stop(&sc->sc_md_tmo);
471 callout_stop(&sc->sc_md6_tmo);
472
473 if (sc->sc_suppress)
474 carp_suppress_preempt--;
475 sc->sc_suppress = 0;
476
477 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
478 carp_suppress_preempt--;
479 sc->sc_sendad_errors = 0;
480
481 carp_set_state(sc, INIT);
482 SC2IFP(sc)->if_flags &= ~IFF_UP;
483 carp_setrun(sc, 0);
484 if (unlock)
485 carp_multicast_cleanup(sc);
486#ifdef INET6
487 carp_multicast6_cleanup(sc);
488#endif
489
490 if (sc->sc_carpdev != NULL) {
491 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
492 CARP_LOCK_ASSERT(cif);
493 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
494 if (!--cif->vhif_nvrs) {
495 ifpromisc(sc->sc_carpdev, 0);
496 sc->sc_carpdev->if_carp = NULL;
497 CARP_LOCK_DESTROY(cif);
498 free(cif, M_IFADDR);
499 } else if (unlock)
500 CARP_UNLOCK(cif);
501 sc->sc_carpdev = NULL;
502 }
503}
504
505/* Detach an interface from the carp. */
506static void
507carp_ifdetach(void *arg __unused, struct ifnet *ifp)
508{
509 struct carp_if *cif = (struct carp_if *)ifp->if_carp;
510 struct carp_softc *sc, *nextsc;
511
512 if (cif == NULL)
513 return;
514
515 /*
516 * XXX: At the end of for() cycle the lock will be destroyed.
517 */
518 CARP_LOCK(cif);
519 for (sc = TAILQ_FIRST(&cif->vhif_vrs); sc; sc = nextsc) {
520 nextsc = TAILQ_NEXT(sc, sc_list);
521 carpdetach(sc, 0);
522 }
523}
524
525/*
526 * process input packet.
527 * we have rearranged checks order compared to the rfc,
528 * but it seems more efficient this way or not possible otherwise.
529 */
530void
531carp_input(struct mbuf *m, int hlen)
532{
533 struct ip *ip = mtod(m, struct ip *);
534 struct carp_header *ch;
535 int iplen, len;
536
537 CARPSTATS_INC(carps_ipackets);
538
539 if (!carp_opts[CARPCTL_ALLOW]) {
540 m_freem(m);
541 return;
542 }
543
544 /* check if received on a valid carp interface */
545 if (m->m_pkthdr.rcvif->if_carp == NULL) {
546 CARPSTATS_INC(carps_badif);
547 CARP_LOG("carp_input: packet received on non-carp "
548 "interface: %s\n",
549 m->m_pkthdr.rcvif->if_xname);
550 m_freem(m);
551 return;
552 }
553
554 /* verify that the IP TTL is 255. */
555 if (ip->ip_ttl != CARP_DFLTTL) {
556 CARPSTATS_INC(carps_badttl);
557 CARP_LOG("carp_input: received ttl %d != 255i on %s\n",
558 ip->ip_ttl,
559 m->m_pkthdr.rcvif->if_xname);
560 m_freem(m);
561 return;
562 }
563
564 iplen = ip->ip_hl << 2;
565
566 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
567 CARPSTATS_INC(carps_badlen);
568 CARP_LOG("carp_input: received len %zd < "
569 "sizeof(struct carp_header)\n",
570 m->m_len - sizeof(struct ip));
571 m_freem(m);
572 return;
573 }
574
575 if (iplen + sizeof(*ch) < m->m_len) {
576 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
577 CARPSTATS_INC(carps_hdrops);
578 CARP_LOG("carp_input: pullup failed\n");
579 return;
580 }
581 ip = mtod(m, struct ip *);
582 }
583 ch = (struct carp_header *)((char *)ip + iplen);
584
585 /*
586 * verify that the received packet length is
587 * equal to the CARP header
588 */
589 len = iplen + sizeof(*ch);
590 if (len > m->m_pkthdr.len) {
591 CARPSTATS_INC(carps_badlen);
592 CARP_LOG("carp_input: packet too short %d on %s\n",
593 m->m_pkthdr.len,
594 m->m_pkthdr.rcvif->if_xname);
595 m_freem(m);
596 return;
597 }
598
599 if ((m = m_pullup(m, len)) == NULL) {
600 CARPSTATS_INC(carps_hdrops);
601 return;
602 }
603 ip = mtod(m, struct ip *);
604 ch = (struct carp_header *)((char *)ip + iplen);
605
606 /* verify the CARP checksum */
607 m->m_data += iplen;
608 if (carp_cksum(m, len - iplen)) {
609 CARPSTATS_INC(carps_badsum);
610 CARP_LOG("carp_input: checksum failed on %s\n",
611 m->m_pkthdr.rcvif->if_xname);
612 m_freem(m);
613 return;
614 }
615 m->m_data -= iplen;
616
617 carp_input_c(m, ch, AF_INET);
618}
619
620#ifdef INET6
621int
622carp6_input(struct mbuf **mp, int *offp, int proto)
623{
624 struct mbuf *m = *mp;
625 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
626 struct carp_header *ch;
627 u_int len;
628
629 CARPSTATS_INC(carps_ipackets6);
630
631 if (!carp_opts[CARPCTL_ALLOW]) {
632 m_freem(m);
633 return (IPPROTO_DONE);
634 }
635
636 /* check if received on a valid carp interface */
637 if (m->m_pkthdr.rcvif->if_carp == NULL) {
638 CARPSTATS_INC(carps_badif);
639 CARP_LOG("carp6_input: packet received on non-carp "
640 "interface: %s\n",
641 m->m_pkthdr.rcvif->if_xname);
642 m_freem(m);
643 return (IPPROTO_DONE);
644 }
645
646 /* verify that the IP TTL is 255 */
647 if (ip6->ip6_hlim != CARP_DFLTTL) {
648 CARPSTATS_INC(carps_badttl);
649 CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
650 ip6->ip6_hlim,
651 m->m_pkthdr.rcvif->if_xname);
652 m_freem(m);
653 return (IPPROTO_DONE);
654 }
655
656 /* verify that we have a complete carp packet */
657 len = m->m_len;
658 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
659 if (ch == NULL) {
660 CARPSTATS_INC(carps_badlen);
661 CARP_LOG("carp6_input: packet size %u too small\n", len);
662 return (IPPROTO_DONE);
663 }
664
665
666 /* verify the CARP checksum */
667 m->m_data += *offp;
668 if (carp_cksum(m, sizeof(*ch))) {
669 CARPSTATS_INC(carps_badsum);
670 CARP_LOG("carp6_input: checksum failed, on %s\n",
671 m->m_pkthdr.rcvif->if_xname);
672 m_freem(m);
673 return (IPPROTO_DONE);
674 }
675 m->m_data -= *offp;
676
677 carp_input_c(m, ch, AF_INET6);
678 return (IPPROTO_DONE);
679}
680#endif /* INET6 */
681
682static void
683carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
684{
685 struct ifnet *ifp = m->m_pkthdr.rcvif;
686 struct carp_softc *sc;
687 u_int64_t tmp_counter;
688 struct timeval sc_tv, ch_tv;
689
690 /* verify that the VHID is valid on the receiving interface */
691 CARP_LOCK(ifp->if_carp);
692 TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
693 if (sc->sc_vhid == ch->carp_vhid)
694 break;
695
696 if (!sc || !((SC2IFP(sc)->if_flags & IFF_UP) &&
697 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
698 CARPSTATS_INC(carps_badvhid);
699 CARP_UNLOCK(ifp->if_carp);
700 m_freem(m);
701 return;
702 }
703
704 getmicrotime(&SC2IFP(sc)->if_lastchange);
705 SC2IFP(sc)->if_ipackets++;
706 SC2IFP(sc)->if_ibytes += m->m_pkthdr.len;
707
708 if (bpf_peers_present(SC2IFP(sc)->if_bpf)) {
709 struct ip *ip = mtod(m, struct ip *);
710 uint32_t af1 = af;
711
712 /* BPF wants net byte order */
713 ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
714 ip->ip_off = htons(ip->ip_off);
715 bpf_mtap2(SC2IFP(sc)->if_bpf, &af1, sizeof(af1), m);
716 }
717
718 /* verify the CARP version. */
719 if (ch->carp_version != CARP_VERSION) {
720 CARPSTATS_INC(carps_badver);
721 SC2IFP(sc)->if_ierrors++;
722 CARP_UNLOCK(ifp->if_carp);
723 CARP_LOG("%s; invalid version %d\n",
724 SC2IFP(sc)->if_xname,
725 ch->carp_version);
726 m_freem(m);
727 return;
728 }
729
730 /* verify the hash */
731 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
732 CARPSTATS_INC(carps_badauth);
733 SC2IFP(sc)->if_ierrors++;
734 CARP_UNLOCK(ifp->if_carp);
735 CARP_LOG("%s: incorrect hash\n", SC2IFP(sc)->if_xname);
736 m_freem(m);
737 return;
738 }
739
740 tmp_counter = ntohl(ch->carp_counter[0]);
741 tmp_counter = tmp_counter<<32;
742 tmp_counter += ntohl(ch->carp_counter[1]);
743
744 /* XXX Replay protection goes here */
745
746 sc->sc_init_counter = 0;
747 sc->sc_counter = tmp_counter;
748
749 sc_tv.tv_sec = sc->sc_advbase;
750 if (carp_suppress_preempt && sc->sc_advskew < 240)
751 sc_tv.tv_usec = 240 * 1000000 / 256;
752 else
753 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
754 ch_tv.tv_sec = ch->carp_advbase;
755 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
756
757 switch (sc->sc_state) {
758 case INIT:
759 break;
760 case MASTER:
761 /*
762 * If we receive an advertisement from a master who's going to
763 * be more frequent than us, go into BACKUP state.
764 */
765 if (timevalcmp(&sc_tv, &ch_tv, >) ||
766 timevalcmp(&sc_tv, &ch_tv, ==)) {
767 callout_stop(&sc->sc_ad_tmo);
768 CARP_DEBUG("%s: MASTER -> BACKUP "
769 "(more frequent advertisement received)\n",
770 SC2IFP(sc)->if_xname);
771 carp_set_state(sc, BACKUP);
772 carp_setrun(sc, 0);
773 carp_setroute(sc, RTM_DELETE);
774 }
775 break;
776 case BACKUP:
777 /*
778 * If we're pre-empting masters who advertise slower than us,
779 * and this one claims to be slower, treat him as down.
780 */
781 if (carp_opts[CARPCTL_PREEMPT] &&
782 timevalcmp(&sc_tv, &ch_tv, <)) {
783 CARP_DEBUG("%s: BACKUP -> MASTER "
784 "(preempting a slower master)\n",
785 SC2IFP(sc)->if_xname);
786 carp_master_down_locked(sc);
787 break;
788 }
789
790 /*
791 * If the master is going to advertise at such a low frequency
792 * that he's guaranteed to time out, we'd might as well just
793 * treat him as timed out now.
794 */
795 sc_tv.tv_sec = sc->sc_advbase * 3;
796 if (timevalcmp(&sc_tv, &ch_tv, <)) {
797 CARP_DEBUG("%s: BACKUP -> MASTER "
798 "(master timed out)\n",
799 SC2IFP(sc)->if_xname);
800 carp_master_down_locked(sc);
801 break;
802 }
803
804 /*
805 * Otherwise, we reset the counter and wait for the next
806 * advertisement.
807 */
808 carp_setrun(sc, af);
809 break;
810 }
811
812 CARP_UNLOCK(ifp->if_carp);
813
814 m_freem(m);
815 return;
816}
817
818static int
819carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
820{
821 struct m_tag *mtag;
822 struct ifnet *ifp = SC2IFP(sc);
823
824 if (sc->sc_init_counter) {
825 /* this could also be seconds since unix epoch */
826 sc->sc_counter = arc4random();
827 sc->sc_counter = sc->sc_counter << 32;
828 sc->sc_counter += arc4random();
829 } else
830 sc->sc_counter++;
831
832 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
833 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
834
835 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
836
837 /* Tag packet for carp_output */
838 mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), M_NOWAIT);
839 if (mtag == NULL) {
840 m_freem(m);
841 SC2IFP(sc)->if_oerrors++;
842 return (ENOMEM);
843 }
844 bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
845 m_tag_prepend(m, mtag);
846
847 return (0);
848}
849
850static void
851carp_send_ad_all(void)
852{
853 struct carp_softc *sc;
854
855 mtx_lock(&carp_mtx);
856 LIST_FOREACH(sc, &carpif_list, sc_next) {
857 if (sc->sc_carpdev == NULL)
858 continue;
859 CARP_SCLOCK(sc);
860 if ((SC2IFP(sc)->if_flags & IFF_UP) &&
861 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING) &&
862 sc->sc_state == MASTER)
863 carp_send_ad_locked(sc);
864 CARP_SCUNLOCK(sc);
865 }
866 mtx_unlock(&carp_mtx);
867}
868
869static void
870carp_send_ad(void *v)
871{
872 struct carp_softc *sc = v;
873
874 CARP_SCLOCK(sc);
875 carp_send_ad_locked(sc);
876 CARP_SCUNLOCK(sc);
877}
878
879static void
880carp_send_ad_locked(struct carp_softc *sc)
881{
882 struct carp_header ch;
883 struct timeval tv;
884 struct carp_header *ch_ptr;
885 struct mbuf *m;
886 int len, advbase, advskew;
887
888 CARP_SCLOCK_ASSERT(sc);
889
890 /* bow out if we've lost our UPness or RUNNINGuiness */
891 if (!((SC2IFP(sc)->if_flags & IFF_UP) &&
892 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
893 advbase = 255;
894 advskew = 255;
895 } else {
896 advbase = sc->sc_advbase;
897 if (!carp_suppress_preempt || sc->sc_advskew > 240)
898 advskew = sc->sc_advskew;
899 else
900 advskew = 240;
901 tv.tv_sec = advbase;
902 tv.tv_usec = advskew * 1000000 / 256;
903 }
904
905 ch.carp_version = CARP_VERSION;
906 ch.carp_type = CARP_ADVERTISEMENT;
907 ch.carp_vhid = sc->sc_vhid;
908 ch.carp_advbase = advbase;
909 ch.carp_advskew = advskew;
910 ch.carp_authlen = 7; /* XXX DEFINE */
911 ch.carp_pad1 = 0; /* must be zero */
912 ch.carp_cksum = 0;
913
914#ifdef INET
915 INIT_VNET_INET(curvnet);
916 if (sc->sc_ia) {
917 struct ip *ip;
918
919 MGETHDR(m, M_DONTWAIT, MT_HEADER);
920 if (m == NULL) {
921 SC2IFP(sc)->if_oerrors++;
922 CARPSTATS_INC(carps_onomem);
923 /* XXX maybe less ? */
924 if (advbase != 255 || advskew != 255)
925 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
926 carp_send_ad, sc);
927 return;
928 }
929 len = sizeof(*ip) + sizeof(ch);
930 m->m_pkthdr.len = len;
931 m->m_pkthdr.rcvif = NULL;
932 m->m_len = len;
933 MH_ALIGN(m, m->m_len);
934 m->m_flags |= M_MCAST;
935 ip = mtod(m, struct ip *);
936 ip->ip_v = IPVERSION;
937 ip->ip_hl = sizeof(*ip) >> 2;
938 ip->ip_tos = IPTOS_LOWDELAY;
939 ip->ip_len = len;
940 ip->ip_id = ip_newid();
941 ip->ip_off = IP_DF;
942 ip->ip_ttl = CARP_DFLTTL;
943 ip->ip_p = IPPROTO_CARP;
944 ip->ip_sum = 0;
945 ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr;
946 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
947
948 ch_ptr = (struct carp_header *)(&ip[1]);
949 bcopy(&ch, ch_ptr, sizeof(ch));
950 if (carp_prepare_ad(m, sc, ch_ptr))
951 return;
952
953 m->m_data += sizeof(*ip);
954 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
955 m->m_data -= sizeof(*ip);
956
957 getmicrotime(&SC2IFP(sc)->if_lastchange);
958 SC2IFP(sc)->if_opackets++;
959 SC2IFP(sc)->if_obytes += len;
960 CARPSTATS_INC(carps_opackets);
961
962 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
963 SC2IFP(sc)->if_oerrors++;
964 if (sc->sc_sendad_errors < INT_MAX)
965 sc->sc_sendad_errors++;
966 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
967 carp_suppress_preempt++;
968 if (carp_suppress_preempt == 1) {
969 CARP_SCUNLOCK(sc);
970 carp_send_ad_all();
971 CARP_SCLOCK(sc);
972 }
973 }
974 sc->sc_sendad_success = 0;
975 } else {
976 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
977 if (++sc->sc_sendad_success >=
978 CARP_SENDAD_MIN_SUCCESS) {
979 carp_suppress_preempt--;
980 sc->sc_sendad_errors = 0;
981 }
982 } else
983 sc->sc_sendad_errors = 0;
984 }
985 }
986#endif /* INET */
987#ifdef INET6
988 if (sc->sc_ia6) {
989 struct ip6_hdr *ip6;
990
991 MGETHDR(m, M_DONTWAIT, MT_HEADER);
992 if (m == NULL) {
993 SC2IFP(sc)->if_oerrors++;
994 CARPSTATS_INC(carps_onomem);
995 /* XXX maybe less ? */
996 if (advbase != 255 || advskew != 255)
997 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
998 carp_send_ad, sc);
999 return;
1000 }
1001 len = sizeof(*ip6) + sizeof(ch);
1002 m->m_pkthdr.len = len;
1003 m->m_pkthdr.rcvif = NULL;
1004 m->m_len = len;
1005 MH_ALIGN(m, m->m_len);
1006 m->m_flags |= M_MCAST;
1007 ip6 = mtod(m, struct ip6_hdr *);
1008 bzero(ip6, sizeof(*ip6));
1009 ip6->ip6_vfc |= IPV6_VERSION;
1010 ip6->ip6_hlim = CARP_DFLTTL;
1011 ip6->ip6_nxt = IPPROTO_CARP;
1012 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
1013 sizeof(struct in6_addr));
1014 /* set the multicast destination */
1015
1016 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1017 ip6->ip6_dst.s6_addr8[15] = 0x12;
1018 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1019 SC2IFP(sc)->if_oerrors++;
1020 m_freem(m);
1021 CARP_LOG("%s: in6_setscope failed\n", __func__);
1022 return;
1023 }
1024
1025 ch_ptr = (struct carp_header *)(&ip6[1]);
1026 bcopy(&ch, ch_ptr, sizeof(ch));
1027 if (carp_prepare_ad(m, sc, ch_ptr))
1028 return;
1029
1030 m->m_data += sizeof(*ip6);
1031 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
1032 m->m_data -= sizeof(*ip6);
1033
1034 getmicrotime(&SC2IFP(sc)->if_lastchange);
1035 SC2IFP(sc)->if_opackets++;
1036 SC2IFP(sc)->if_obytes += len;
1037 CARPSTATS_INC(carps_opackets6);
1038
1039 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1040 SC2IFP(sc)->if_oerrors++;
1041 if (sc->sc_sendad_errors < INT_MAX)
1042 sc->sc_sendad_errors++;
1043 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1044 carp_suppress_preempt++;
1045 if (carp_suppress_preempt == 1) {
1046 CARP_SCUNLOCK(sc);
1047 carp_send_ad_all();
1048 CARP_SCLOCK(sc);
1049 }
1050 }
1051 sc->sc_sendad_success = 0;
1052 } else {
1053 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1054 if (++sc->sc_sendad_success >=
1055 CARP_SENDAD_MIN_SUCCESS) {
1056 carp_suppress_preempt--;
1057 sc->sc_sendad_errors = 0;
1058 }
1059 } else
1060 sc->sc_sendad_errors = 0;
1061 }
1062 }
1063#endif /* INET6 */
1064
1065 if (advbase != 255 || advskew != 255)
1066 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1067 carp_send_ad, sc);
1068
1069}
1070
1071/*
1072 * Broadcast a gratuitous ARP request containing
1073 * the virtual router MAC address for each IP address
1074 * associated with the virtual router.
1075 */
1076static void
1077carp_send_arp(struct carp_softc *sc)
1078{
1079 struct ifaddr *ifa;
1080
1081 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1082
1083 if (ifa->ifa_addr->sa_family != AF_INET)
1084 continue;
1085
1086/* arprequest(sc->sc_carpdev, &in, &in, IF_LLADDR(sc->sc_ifp)); */
1087 arp_ifinit2(sc->sc_carpdev, ifa, IF_LLADDR(sc->sc_ifp));
1088
1089 DELAY(1000); /* XXX */
1090 }
1091}
1092
1093#ifdef INET6
1094static void
1095carp_send_na(struct carp_softc *sc)
1096{
1097 struct ifaddr *ifa;
1098 struct in6_addr *in6;
1099 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1100
1101 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1102
1103 if (ifa->ifa_addr->sa_family != AF_INET6)
1104 continue;
1105
1106 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1107 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1108 ND_NA_FLAG_OVERRIDE, 1, NULL);
1109 DELAY(1000); /* XXX */
1110 }
1111}
1112#endif /* INET6 */
1113
1114static int
1115carp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type)
1116{
1117 struct carp_softc *vh;
1118 struct ifaddr *ifa;
1119 int count = 0;
1120
1121 CARP_LOCK_ASSERT(cif);
1122
1123 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1124 if ((type == CARP_COUNT_RUNNING &&
1125 (SC2IFP(vh)->if_flags & IFF_UP) &&
1126 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) ||
1127 (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) {
1128 IF_ADDR_LOCK(SC2IFP(vh));
1129 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1130 ifa_list) {
1131 if (ifa->ifa_addr->sa_family == AF_INET &&
1132 ia->ia_addr.sin_addr.s_addr ==
1133 ifatoia(ifa)->ia_addr.sin_addr.s_addr)
1134 count++;
1135 }
1136 IF_ADDR_UNLOCK(SC2IFP(vh));
1137 }
1138 }
1139 return (count);
1140}
1141
1142int
1143carp_iamatch(void *v, struct in_ifaddr *ia,
1144 struct in_addr *isaddr, u_int8_t **enaddr)
1145{
1146 struct carp_if *cif = v;
1147 struct carp_softc *vh;
1148 int index, count = 0;
1149 struct ifaddr *ifa;
1150
1151 CARP_LOCK(cif);
1152
1153 if (carp_opts[CARPCTL_ARPBALANCE]) {
1154 /*
1155 * XXX proof of concept implementation.
1156 * We use the source ip to decide which virtual host should
1157 * handle the request. If we're master of that virtual host,
1158 * then we respond, otherwise, just drop the arp packet on
1159 * the floor.
1160 */
1161 count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING);
1162 if (count == 0) {
1163 /* should never reach this */
1164 CARP_UNLOCK(cif);
1165 return (0);
1166 }
1167
1168 /* this should be a hash, like pf_hash() */
1169 index = ntohl(isaddr->s_addr) % count;
1170 count = 0;
1171
1172 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1173 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1174 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) {
1175 IF_ADDR_LOCK(SC2IFP(vh));
1176 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1177 ifa_list) {
1178 if (ifa->ifa_addr->sa_family ==
1179 AF_INET &&
1180 ia->ia_addr.sin_addr.s_addr ==
1181 ifatoia(ifa)->ia_addr.sin_addr.s_addr) {
1182 if (count == index) {
1183 if (vh->sc_state ==
1184 MASTER) {
1185 *enaddr = IF_LLADDR(vh->sc_ifp);
1186 IF_ADDR_UNLOCK(SC2IFP(vh));
1187 CARP_UNLOCK(cif);
1188 return (1);
1189 } else {
1190 IF_ADDR_UNLOCK(SC2IFP(vh));
1191 CARP_UNLOCK(cif);
1192 return (0);
1193 }
1194 }
1195 count++;
1196 }
1197 }
1198 IF_ADDR_UNLOCK(SC2IFP(vh));
1199 }
1200 }
1201 } else {
1202 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1203 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1204 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1205 ia->ia_ifp == SC2IFP(vh) &&
1206 vh->sc_state == MASTER) {
1207 *enaddr = IF_LLADDR(vh->sc_ifp);
1208 CARP_UNLOCK(cif);
1209 return (1);
1210 }
1211 }
1212 }
1213 CARP_UNLOCK(cif);
1214 return (0);
1215}
1216
1217#ifdef INET6
1218struct ifaddr *
1219carp_iamatch6(void *v, struct in6_addr *taddr)
1220{
1221 struct carp_if *cif = v;
1222 struct carp_softc *vh;
1223 struct ifaddr *ifa;
1224
1225 CARP_LOCK(cif);
1226 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1227 IF_ADDR_LOCK(SC2IFP(vh));
1228 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist, ifa_list) {
1229 if (IN6_ARE_ADDR_EQUAL(taddr,
1230 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1231 (SC2IFP(vh)->if_flags & IFF_UP) &&
1232 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1233 vh->sc_state == MASTER) {
1234 IF_ADDR_UNLOCK(SC2IFP(vh));
1235 CARP_UNLOCK(cif);
1236 return (ifa);
1237 }
1238 }
1239 IF_ADDR_UNLOCK(SC2IFP(vh));
1240 }
1241 CARP_UNLOCK(cif);
1242
1243 return (NULL);
1244}
1245
1246void *
1247carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1248{
1249 struct m_tag *mtag;
1250 struct carp_if *cif = v;
1251 struct carp_softc *sc;
1252 struct ifaddr *ifa;
1253
1254 CARP_LOCK(cif);
1255 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1256 IF_ADDR_LOCK(SC2IFP(sc));
1257 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1258 if (IN6_ARE_ADDR_EQUAL(taddr,
1259 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1260 (SC2IFP(sc)->if_flags & IFF_UP) &&
1261 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING)) {
1262 struct ifnet *ifp = SC2IFP(sc);
1263 mtag = m_tag_get(PACKET_TAG_CARP,
1264 sizeof(struct ifnet *), M_NOWAIT);
1265 if (mtag == NULL) {
1266 /* better a bit than nothing */
1267 IF_ADDR_UNLOCK(SC2IFP(sc));
1268 CARP_UNLOCK(cif);
1269 return (IF_LLADDR(sc->sc_ifp));
1270 }
1271 bcopy(&ifp, (caddr_t)(mtag + 1),
1272 sizeof(struct ifnet *));
1273 m_tag_prepend(m, mtag);
1274
1275 IF_ADDR_UNLOCK(SC2IFP(sc));
1276 CARP_UNLOCK(cif);
1277 return (IF_LLADDR(sc->sc_ifp));
1278 }
1279 }
1280 IF_ADDR_UNLOCK(SC2IFP(sc));
1281 }
1282 CARP_UNLOCK(cif);
1283
1284 return (NULL);
1285}
1286#endif
1287
1288struct ifnet *
1289carp_forus(void *v, void *dhost)
1290{
1291 struct carp_if *cif = v;
1292 struct carp_softc *vh;
1293 u_int8_t *ena = dhost;
1294
1295 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1296 return (NULL);
1297
1298 CARP_LOCK(cif);
1299 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list)
1300 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1301 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1302 vh->sc_state == MASTER &&
1303 !bcmp(dhost, IF_LLADDR(vh->sc_ifp), ETHER_ADDR_LEN)) {
1304 CARP_UNLOCK(cif);
1305 return (SC2IFP(vh));
1306 }
1307
1308 CARP_UNLOCK(cif);
1309 return (NULL);
1310}
1311
1312static void
1313carp_master_down(void *v)
1314{
1315 struct carp_softc *sc = v;
1316
1317 CARP_SCLOCK(sc);
1318 carp_master_down_locked(sc);
1319 CARP_SCUNLOCK(sc);
1320}
1321
1322static void
1323carp_master_down_locked(struct carp_softc *sc)
1324{
1325 if (sc->sc_carpdev)
1326 CARP_SCLOCK_ASSERT(sc);
1327
1328 switch (sc->sc_state) {
1329 case INIT:
1330 printf("%s: master_down event in INIT state\n",
1331 SC2IFP(sc)->if_xname);
1332 break;
1333 case MASTER:
1334 break;
1335 case BACKUP:
1336 carp_set_state(sc, MASTER);
1337 carp_send_ad_locked(sc);
1338 carp_send_arp(sc);
1339#ifdef INET6
1340 carp_send_na(sc);
1341#endif /* INET6 */
1342 carp_setrun(sc, 0);
1343 carp_setroute(sc, RTM_ADD);
1344 break;
1345 }
1346}
1347
1348/*
1349 * When in backup state, af indicates whether to reset the master down timer
1350 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1351 */
1352static void
1353carp_setrun(struct carp_softc *sc, sa_family_t af)
1354{
1355 struct timeval tv;
1356
1357 if (sc->sc_carpdev == NULL) {
1358 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1359 carp_set_state(sc, INIT);
1360 return;
1361 } else
1362 CARP_SCLOCK_ASSERT(sc);
1363
1364 if (SC2IFP(sc)->if_flags & IFF_UP &&
1365 sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6))
1366 SC2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
1367 else {
1368 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1369 carp_setroute(sc, RTM_DELETE);
1370 return;
1371 }
1372
1373 switch (sc->sc_state) {
1374 case INIT:
1375 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1376 carp_send_ad_locked(sc);
1377 carp_send_arp(sc);
1378#ifdef INET6
1379 carp_send_na(sc);
1380#endif /* INET6 */
1381 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1382 SC2IFP(sc)->if_xname);
1383 carp_set_state(sc, MASTER);
1384 carp_setroute(sc, RTM_ADD);
1385 } else {
1386 CARP_DEBUG("%s: INIT -> BACKUP\n", SC2IFP(sc)->if_xname);
1387 carp_set_state(sc, BACKUP);
1388 carp_setroute(sc, RTM_DELETE);
1389 carp_setrun(sc, 0);
1390 }
1391 break;
1392 case BACKUP:
1393 callout_stop(&sc->sc_ad_tmo);
1394 tv.tv_sec = 3 * sc->sc_advbase;
1395 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1396 switch (af) {
1397#ifdef INET
1398 case AF_INET:
1399 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1400 carp_master_down, sc);
1401 break;
1402#endif /* INET */
1403#ifdef INET6
1404 case AF_INET6:
1405 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1406 carp_master_down, sc);
1407 break;
1408#endif /* INET6 */
1409 default:
1410 if (sc->sc_naddrs)
1411 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1412 carp_master_down, sc);
1413 if (sc->sc_naddrs6)
1414 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1415 carp_master_down, sc);
1416 break;
1417 }
1418 break;
1419 case MASTER:
1420 tv.tv_sec = sc->sc_advbase;
1421 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1422 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1423 carp_send_ad, sc);
1424 break;
1425 }
1426}
1427
1428static void
1429carp_multicast_cleanup(struct carp_softc *sc)
1430{
1431 struct ip_moptions *imo = &sc->sc_imo;
1432 u_int16_t n = imo->imo_num_memberships;
1433
1434 /* Clean up our own multicast memberships */
1435 while (n-- > 0) {
1436 if (imo->imo_membership[n] != NULL) {
1437 in_delmulti(imo->imo_membership[n]);
1438 imo->imo_membership[n] = NULL;
1439 }
1440 }
1441 KASSERT(imo->imo_mfilters == NULL,
1442 ("%s: imo_mfilters != NULL", __func__));
1443 imo->imo_num_memberships = 0;
1444 imo->imo_multicast_ifp = NULL;
1445}
1446
1447#ifdef INET6
1448static void
1449carp_multicast6_cleanup(struct carp_softc *sc)
1450{
1451 struct ip6_moptions *im6o = &sc->sc_im6o;
459 free(sc, M_CARP);
460}
461
462/*
463 * This function can be called on CARP interface destroy path,
464 * and in case of the removal of the underlying interface as
465 * well. We differentiate these two cases. In the latter case
466 * we do not cleanup our multicast memberships, since they
467 * are already freed. Also, in the latter case we do not
468 * release the lock on return, because the function will be
469 * called once more, for another CARP instance on the same
470 * interface.
471 */
472static void
473carpdetach(struct carp_softc *sc, int unlock)
474{
475 struct carp_if *cif;
476
477 callout_stop(&sc->sc_ad_tmo);
478 callout_stop(&sc->sc_md_tmo);
479 callout_stop(&sc->sc_md6_tmo);
480
481 if (sc->sc_suppress)
482 carp_suppress_preempt--;
483 sc->sc_suppress = 0;
484
485 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
486 carp_suppress_preempt--;
487 sc->sc_sendad_errors = 0;
488
489 carp_set_state(sc, INIT);
490 SC2IFP(sc)->if_flags &= ~IFF_UP;
491 carp_setrun(sc, 0);
492 if (unlock)
493 carp_multicast_cleanup(sc);
494#ifdef INET6
495 carp_multicast6_cleanup(sc);
496#endif
497
498 if (sc->sc_carpdev != NULL) {
499 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
500 CARP_LOCK_ASSERT(cif);
501 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
502 if (!--cif->vhif_nvrs) {
503 ifpromisc(sc->sc_carpdev, 0);
504 sc->sc_carpdev->if_carp = NULL;
505 CARP_LOCK_DESTROY(cif);
506 free(cif, M_IFADDR);
507 } else if (unlock)
508 CARP_UNLOCK(cif);
509 sc->sc_carpdev = NULL;
510 }
511}
512
513/* Detach an interface from the carp. */
514static void
515carp_ifdetach(void *arg __unused, struct ifnet *ifp)
516{
517 struct carp_if *cif = (struct carp_if *)ifp->if_carp;
518 struct carp_softc *sc, *nextsc;
519
520 if (cif == NULL)
521 return;
522
523 /*
524 * XXX: At the end of for() cycle the lock will be destroyed.
525 */
526 CARP_LOCK(cif);
527 for (sc = TAILQ_FIRST(&cif->vhif_vrs); sc; sc = nextsc) {
528 nextsc = TAILQ_NEXT(sc, sc_list);
529 carpdetach(sc, 0);
530 }
531}
532
533/*
534 * process input packet.
535 * we have rearranged checks order compared to the rfc,
536 * but it seems more efficient this way or not possible otherwise.
537 */
538void
539carp_input(struct mbuf *m, int hlen)
540{
541 struct ip *ip = mtod(m, struct ip *);
542 struct carp_header *ch;
543 int iplen, len;
544
545 CARPSTATS_INC(carps_ipackets);
546
547 if (!carp_opts[CARPCTL_ALLOW]) {
548 m_freem(m);
549 return;
550 }
551
552 /* check if received on a valid carp interface */
553 if (m->m_pkthdr.rcvif->if_carp == NULL) {
554 CARPSTATS_INC(carps_badif);
555 CARP_LOG("carp_input: packet received on non-carp "
556 "interface: %s\n",
557 m->m_pkthdr.rcvif->if_xname);
558 m_freem(m);
559 return;
560 }
561
562 /* verify that the IP TTL is 255. */
563 if (ip->ip_ttl != CARP_DFLTTL) {
564 CARPSTATS_INC(carps_badttl);
565 CARP_LOG("carp_input: received ttl %d != 255i on %s\n",
566 ip->ip_ttl,
567 m->m_pkthdr.rcvif->if_xname);
568 m_freem(m);
569 return;
570 }
571
572 iplen = ip->ip_hl << 2;
573
574 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
575 CARPSTATS_INC(carps_badlen);
576 CARP_LOG("carp_input: received len %zd < "
577 "sizeof(struct carp_header)\n",
578 m->m_len - sizeof(struct ip));
579 m_freem(m);
580 return;
581 }
582
583 if (iplen + sizeof(*ch) < m->m_len) {
584 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
585 CARPSTATS_INC(carps_hdrops);
586 CARP_LOG("carp_input: pullup failed\n");
587 return;
588 }
589 ip = mtod(m, struct ip *);
590 }
591 ch = (struct carp_header *)((char *)ip + iplen);
592
593 /*
594 * verify that the received packet length is
595 * equal to the CARP header
596 */
597 len = iplen + sizeof(*ch);
598 if (len > m->m_pkthdr.len) {
599 CARPSTATS_INC(carps_badlen);
600 CARP_LOG("carp_input: packet too short %d on %s\n",
601 m->m_pkthdr.len,
602 m->m_pkthdr.rcvif->if_xname);
603 m_freem(m);
604 return;
605 }
606
607 if ((m = m_pullup(m, len)) == NULL) {
608 CARPSTATS_INC(carps_hdrops);
609 return;
610 }
611 ip = mtod(m, struct ip *);
612 ch = (struct carp_header *)((char *)ip + iplen);
613
614 /* verify the CARP checksum */
615 m->m_data += iplen;
616 if (carp_cksum(m, len - iplen)) {
617 CARPSTATS_INC(carps_badsum);
618 CARP_LOG("carp_input: checksum failed on %s\n",
619 m->m_pkthdr.rcvif->if_xname);
620 m_freem(m);
621 return;
622 }
623 m->m_data -= iplen;
624
625 carp_input_c(m, ch, AF_INET);
626}
627
628#ifdef INET6
629int
630carp6_input(struct mbuf **mp, int *offp, int proto)
631{
632 struct mbuf *m = *mp;
633 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
634 struct carp_header *ch;
635 u_int len;
636
637 CARPSTATS_INC(carps_ipackets6);
638
639 if (!carp_opts[CARPCTL_ALLOW]) {
640 m_freem(m);
641 return (IPPROTO_DONE);
642 }
643
644 /* check if received on a valid carp interface */
645 if (m->m_pkthdr.rcvif->if_carp == NULL) {
646 CARPSTATS_INC(carps_badif);
647 CARP_LOG("carp6_input: packet received on non-carp "
648 "interface: %s\n",
649 m->m_pkthdr.rcvif->if_xname);
650 m_freem(m);
651 return (IPPROTO_DONE);
652 }
653
654 /* verify that the IP TTL is 255 */
655 if (ip6->ip6_hlim != CARP_DFLTTL) {
656 CARPSTATS_INC(carps_badttl);
657 CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
658 ip6->ip6_hlim,
659 m->m_pkthdr.rcvif->if_xname);
660 m_freem(m);
661 return (IPPROTO_DONE);
662 }
663
664 /* verify that we have a complete carp packet */
665 len = m->m_len;
666 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
667 if (ch == NULL) {
668 CARPSTATS_INC(carps_badlen);
669 CARP_LOG("carp6_input: packet size %u too small\n", len);
670 return (IPPROTO_DONE);
671 }
672
673
674 /* verify the CARP checksum */
675 m->m_data += *offp;
676 if (carp_cksum(m, sizeof(*ch))) {
677 CARPSTATS_INC(carps_badsum);
678 CARP_LOG("carp6_input: checksum failed, on %s\n",
679 m->m_pkthdr.rcvif->if_xname);
680 m_freem(m);
681 return (IPPROTO_DONE);
682 }
683 m->m_data -= *offp;
684
685 carp_input_c(m, ch, AF_INET6);
686 return (IPPROTO_DONE);
687}
688#endif /* INET6 */
689
690static void
691carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
692{
693 struct ifnet *ifp = m->m_pkthdr.rcvif;
694 struct carp_softc *sc;
695 u_int64_t tmp_counter;
696 struct timeval sc_tv, ch_tv;
697
698 /* verify that the VHID is valid on the receiving interface */
699 CARP_LOCK(ifp->if_carp);
700 TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
701 if (sc->sc_vhid == ch->carp_vhid)
702 break;
703
704 if (!sc || !((SC2IFP(sc)->if_flags & IFF_UP) &&
705 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
706 CARPSTATS_INC(carps_badvhid);
707 CARP_UNLOCK(ifp->if_carp);
708 m_freem(m);
709 return;
710 }
711
712 getmicrotime(&SC2IFP(sc)->if_lastchange);
713 SC2IFP(sc)->if_ipackets++;
714 SC2IFP(sc)->if_ibytes += m->m_pkthdr.len;
715
716 if (bpf_peers_present(SC2IFP(sc)->if_bpf)) {
717 struct ip *ip = mtod(m, struct ip *);
718 uint32_t af1 = af;
719
720 /* BPF wants net byte order */
721 ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
722 ip->ip_off = htons(ip->ip_off);
723 bpf_mtap2(SC2IFP(sc)->if_bpf, &af1, sizeof(af1), m);
724 }
725
726 /* verify the CARP version. */
727 if (ch->carp_version != CARP_VERSION) {
728 CARPSTATS_INC(carps_badver);
729 SC2IFP(sc)->if_ierrors++;
730 CARP_UNLOCK(ifp->if_carp);
731 CARP_LOG("%s; invalid version %d\n",
732 SC2IFP(sc)->if_xname,
733 ch->carp_version);
734 m_freem(m);
735 return;
736 }
737
738 /* verify the hash */
739 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
740 CARPSTATS_INC(carps_badauth);
741 SC2IFP(sc)->if_ierrors++;
742 CARP_UNLOCK(ifp->if_carp);
743 CARP_LOG("%s: incorrect hash\n", SC2IFP(sc)->if_xname);
744 m_freem(m);
745 return;
746 }
747
748 tmp_counter = ntohl(ch->carp_counter[0]);
749 tmp_counter = tmp_counter<<32;
750 tmp_counter += ntohl(ch->carp_counter[1]);
751
752 /* XXX Replay protection goes here */
753
754 sc->sc_init_counter = 0;
755 sc->sc_counter = tmp_counter;
756
757 sc_tv.tv_sec = sc->sc_advbase;
758 if (carp_suppress_preempt && sc->sc_advskew < 240)
759 sc_tv.tv_usec = 240 * 1000000 / 256;
760 else
761 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
762 ch_tv.tv_sec = ch->carp_advbase;
763 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
764
765 switch (sc->sc_state) {
766 case INIT:
767 break;
768 case MASTER:
769 /*
770 * If we receive an advertisement from a master who's going to
771 * be more frequent than us, go into BACKUP state.
772 */
773 if (timevalcmp(&sc_tv, &ch_tv, >) ||
774 timevalcmp(&sc_tv, &ch_tv, ==)) {
775 callout_stop(&sc->sc_ad_tmo);
776 CARP_DEBUG("%s: MASTER -> BACKUP "
777 "(more frequent advertisement received)\n",
778 SC2IFP(sc)->if_xname);
779 carp_set_state(sc, BACKUP);
780 carp_setrun(sc, 0);
781 carp_setroute(sc, RTM_DELETE);
782 }
783 break;
784 case BACKUP:
785 /*
786 * If we're pre-empting masters who advertise slower than us,
787 * and this one claims to be slower, treat him as down.
788 */
789 if (carp_opts[CARPCTL_PREEMPT] &&
790 timevalcmp(&sc_tv, &ch_tv, <)) {
791 CARP_DEBUG("%s: BACKUP -> MASTER "
792 "(preempting a slower master)\n",
793 SC2IFP(sc)->if_xname);
794 carp_master_down_locked(sc);
795 break;
796 }
797
798 /*
799 * If the master is going to advertise at such a low frequency
800 * that he's guaranteed to time out, we'd might as well just
801 * treat him as timed out now.
802 */
803 sc_tv.tv_sec = sc->sc_advbase * 3;
804 if (timevalcmp(&sc_tv, &ch_tv, <)) {
805 CARP_DEBUG("%s: BACKUP -> MASTER "
806 "(master timed out)\n",
807 SC2IFP(sc)->if_xname);
808 carp_master_down_locked(sc);
809 break;
810 }
811
812 /*
813 * Otherwise, we reset the counter and wait for the next
814 * advertisement.
815 */
816 carp_setrun(sc, af);
817 break;
818 }
819
820 CARP_UNLOCK(ifp->if_carp);
821
822 m_freem(m);
823 return;
824}
825
826static int
827carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
828{
829 struct m_tag *mtag;
830 struct ifnet *ifp = SC2IFP(sc);
831
832 if (sc->sc_init_counter) {
833 /* this could also be seconds since unix epoch */
834 sc->sc_counter = arc4random();
835 sc->sc_counter = sc->sc_counter << 32;
836 sc->sc_counter += arc4random();
837 } else
838 sc->sc_counter++;
839
840 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
841 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
842
843 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
844
845 /* Tag packet for carp_output */
846 mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), M_NOWAIT);
847 if (mtag == NULL) {
848 m_freem(m);
849 SC2IFP(sc)->if_oerrors++;
850 return (ENOMEM);
851 }
852 bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
853 m_tag_prepend(m, mtag);
854
855 return (0);
856}
857
858static void
859carp_send_ad_all(void)
860{
861 struct carp_softc *sc;
862
863 mtx_lock(&carp_mtx);
864 LIST_FOREACH(sc, &carpif_list, sc_next) {
865 if (sc->sc_carpdev == NULL)
866 continue;
867 CARP_SCLOCK(sc);
868 if ((SC2IFP(sc)->if_flags & IFF_UP) &&
869 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING) &&
870 sc->sc_state == MASTER)
871 carp_send_ad_locked(sc);
872 CARP_SCUNLOCK(sc);
873 }
874 mtx_unlock(&carp_mtx);
875}
876
877static void
878carp_send_ad(void *v)
879{
880 struct carp_softc *sc = v;
881
882 CARP_SCLOCK(sc);
883 carp_send_ad_locked(sc);
884 CARP_SCUNLOCK(sc);
885}
886
887static void
888carp_send_ad_locked(struct carp_softc *sc)
889{
890 struct carp_header ch;
891 struct timeval tv;
892 struct carp_header *ch_ptr;
893 struct mbuf *m;
894 int len, advbase, advskew;
895
896 CARP_SCLOCK_ASSERT(sc);
897
898 /* bow out if we've lost our UPness or RUNNINGuiness */
899 if (!((SC2IFP(sc)->if_flags & IFF_UP) &&
900 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
901 advbase = 255;
902 advskew = 255;
903 } else {
904 advbase = sc->sc_advbase;
905 if (!carp_suppress_preempt || sc->sc_advskew > 240)
906 advskew = sc->sc_advskew;
907 else
908 advskew = 240;
909 tv.tv_sec = advbase;
910 tv.tv_usec = advskew * 1000000 / 256;
911 }
912
913 ch.carp_version = CARP_VERSION;
914 ch.carp_type = CARP_ADVERTISEMENT;
915 ch.carp_vhid = sc->sc_vhid;
916 ch.carp_advbase = advbase;
917 ch.carp_advskew = advskew;
918 ch.carp_authlen = 7; /* XXX DEFINE */
919 ch.carp_pad1 = 0; /* must be zero */
920 ch.carp_cksum = 0;
921
922#ifdef INET
923 INIT_VNET_INET(curvnet);
924 if (sc->sc_ia) {
925 struct ip *ip;
926
927 MGETHDR(m, M_DONTWAIT, MT_HEADER);
928 if (m == NULL) {
929 SC2IFP(sc)->if_oerrors++;
930 CARPSTATS_INC(carps_onomem);
931 /* XXX maybe less ? */
932 if (advbase != 255 || advskew != 255)
933 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
934 carp_send_ad, sc);
935 return;
936 }
937 len = sizeof(*ip) + sizeof(ch);
938 m->m_pkthdr.len = len;
939 m->m_pkthdr.rcvif = NULL;
940 m->m_len = len;
941 MH_ALIGN(m, m->m_len);
942 m->m_flags |= M_MCAST;
943 ip = mtod(m, struct ip *);
944 ip->ip_v = IPVERSION;
945 ip->ip_hl = sizeof(*ip) >> 2;
946 ip->ip_tos = IPTOS_LOWDELAY;
947 ip->ip_len = len;
948 ip->ip_id = ip_newid();
949 ip->ip_off = IP_DF;
950 ip->ip_ttl = CARP_DFLTTL;
951 ip->ip_p = IPPROTO_CARP;
952 ip->ip_sum = 0;
953 ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr;
954 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
955
956 ch_ptr = (struct carp_header *)(&ip[1]);
957 bcopy(&ch, ch_ptr, sizeof(ch));
958 if (carp_prepare_ad(m, sc, ch_ptr))
959 return;
960
961 m->m_data += sizeof(*ip);
962 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
963 m->m_data -= sizeof(*ip);
964
965 getmicrotime(&SC2IFP(sc)->if_lastchange);
966 SC2IFP(sc)->if_opackets++;
967 SC2IFP(sc)->if_obytes += len;
968 CARPSTATS_INC(carps_opackets);
969
970 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
971 SC2IFP(sc)->if_oerrors++;
972 if (sc->sc_sendad_errors < INT_MAX)
973 sc->sc_sendad_errors++;
974 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
975 carp_suppress_preempt++;
976 if (carp_suppress_preempt == 1) {
977 CARP_SCUNLOCK(sc);
978 carp_send_ad_all();
979 CARP_SCLOCK(sc);
980 }
981 }
982 sc->sc_sendad_success = 0;
983 } else {
984 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
985 if (++sc->sc_sendad_success >=
986 CARP_SENDAD_MIN_SUCCESS) {
987 carp_suppress_preempt--;
988 sc->sc_sendad_errors = 0;
989 }
990 } else
991 sc->sc_sendad_errors = 0;
992 }
993 }
994#endif /* INET */
995#ifdef INET6
996 if (sc->sc_ia6) {
997 struct ip6_hdr *ip6;
998
999 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1000 if (m == NULL) {
1001 SC2IFP(sc)->if_oerrors++;
1002 CARPSTATS_INC(carps_onomem);
1003 /* XXX maybe less ? */
1004 if (advbase != 255 || advskew != 255)
1005 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1006 carp_send_ad, sc);
1007 return;
1008 }
1009 len = sizeof(*ip6) + sizeof(ch);
1010 m->m_pkthdr.len = len;
1011 m->m_pkthdr.rcvif = NULL;
1012 m->m_len = len;
1013 MH_ALIGN(m, m->m_len);
1014 m->m_flags |= M_MCAST;
1015 ip6 = mtod(m, struct ip6_hdr *);
1016 bzero(ip6, sizeof(*ip6));
1017 ip6->ip6_vfc |= IPV6_VERSION;
1018 ip6->ip6_hlim = CARP_DFLTTL;
1019 ip6->ip6_nxt = IPPROTO_CARP;
1020 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
1021 sizeof(struct in6_addr));
1022 /* set the multicast destination */
1023
1024 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1025 ip6->ip6_dst.s6_addr8[15] = 0x12;
1026 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1027 SC2IFP(sc)->if_oerrors++;
1028 m_freem(m);
1029 CARP_LOG("%s: in6_setscope failed\n", __func__);
1030 return;
1031 }
1032
1033 ch_ptr = (struct carp_header *)(&ip6[1]);
1034 bcopy(&ch, ch_ptr, sizeof(ch));
1035 if (carp_prepare_ad(m, sc, ch_ptr))
1036 return;
1037
1038 m->m_data += sizeof(*ip6);
1039 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
1040 m->m_data -= sizeof(*ip6);
1041
1042 getmicrotime(&SC2IFP(sc)->if_lastchange);
1043 SC2IFP(sc)->if_opackets++;
1044 SC2IFP(sc)->if_obytes += len;
1045 CARPSTATS_INC(carps_opackets6);
1046
1047 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1048 SC2IFP(sc)->if_oerrors++;
1049 if (sc->sc_sendad_errors < INT_MAX)
1050 sc->sc_sendad_errors++;
1051 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1052 carp_suppress_preempt++;
1053 if (carp_suppress_preempt == 1) {
1054 CARP_SCUNLOCK(sc);
1055 carp_send_ad_all();
1056 CARP_SCLOCK(sc);
1057 }
1058 }
1059 sc->sc_sendad_success = 0;
1060 } else {
1061 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1062 if (++sc->sc_sendad_success >=
1063 CARP_SENDAD_MIN_SUCCESS) {
1064 carp_suppress_preempt--;
1065 sc->sc_sendad_errors = 0;
1066 }
1067 } else
1068 sc->sc_sendad_errors = 0;
1069 }
1070 }
1071#endif /* INET6 */
1072
1073 if (advbase != 255 || advskew != 255)
1074 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1075 carp_send_ad, sc);
1076
1077}
1078
1079/*
1080 * Broadcast a gratuitous ARP request containing
1081 * the virtual router MAC address for each IP address
1082 * associated with the virtual router.
1083 */
1084static void
1085carp_send_arp(struct carp_softc *sc)
1086{
1087 struct ifaddr *ifa;
1088
1089 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1090
1091 if (ifa->ifa_addr->sa_family != AF_INET)
1092 continue;
1093
1094/* arprequest(sc->sc_carpdev, &in, &in, IF_LLADDR(sc->sc_ifp)); */
1095 arp_ifinit2(sc->sc_carpdev, ifa, IF_LLADDR(sc->sc_ifp));
1096
1097 DELAY(1000); /* XXX */
1098 }
1099}
1100
1101#ifdef INET6
1102static void
1103carp_send_na(struct carp_softc *sc)
1104{
1105 struct ifaddr *ifa;
1106 struct in6_addr *in6;
1107 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1108
1109 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1110
1111 if (ifa->ifa_addr->sa_family != AF_INET6)
1112 continue;
1113
1114 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1115 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1116 ND_NA_FLAG_OVERRIDE, 1, NULL);
1117 DELAY(1000); /* XXX */
1118 }
1119}
1120#endif /* INET6 */
1121
1122static int
1123carp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type)
1124{
1125 struct carp_softc *vh;
1126 struct ifaddr *ifa;
1127 int count = 0;
1128
1129 CARP_LOCK_ASSERT(cif);
1130
1131 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1132 if ((type == CARP_COUNT_RUNNING &&
1133 (SC2IFP(vh)->if_flags & IFF_UP) &&
1134 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) ||
1135 (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) {
1136 IF_ADDR_LOCK(SC2IFP(vh));
1137 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1138 ifa_list) {
1139 if (ifa->ifa_addr->sa_family == AF_INET &&
1140 ia->ia_addr.sin_addr.s_addr ==
1141 ifatoia(ifa)->ia_addr.sin_addr.s_addr)
1142 count++;
1143 }
1144 IF_ADDR_UNLOCK(SC2IFP(vh));
1145 }
1146 }
1147 return (count);
1148}
1149
1150int
1151carp_iamatch(void *v, struct in_ifaddr *ia,
1152 struct in_addr *isaddr, u_int8_t **enaddr)
1153{
1154 struct carp_if *cif = v;
1155 struct carp_softc *vh;
1156 int index, count = 0;
1157 struct ifaddr *ifa;
1158
1159 CARP_LOCK(cif);
1160
1161 if (carp_opts[CARPCTL_ARPBALANCE]) {
1162 /*
1163 * XXX proof of concept implementation.
1164 * We use the source ip to decide which virtual host should
1165 * handle the request. If we're master of that virtual host,
1166 * then we respond, otherwise, just drop the arp packet on
1167 * the floor.
1168 */
1169 count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING);
1170 if (count == 0) {
1171 /* should never reach this */
1172 CARP_UNLOCK(cif);
1173 return (0);
1174 }
1175
1176 /* this should be a hash, like pf_hash() */
1177 index = ntohl(isaddr->s_addr) % count;
1178 count = 0;
1179
1180 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1181 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1182 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) {
1183 IF_ADDR_LOCK(SC2IFP(vh));
1184 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1185 ifa_list) {
1186 if (ifa->ifa_addr->sa_family ==
1187 AF_INET &&
1188 ia->ia_addr.sin_addr.s_addr ==
1189 ifatoia(ifa)->ia_addr.sin_addr.s_addr) {
1190 if (count == index) {
1191 if (vh->sc_state ==
1192 MASTER) {
1193 *enaddr = IF_LLADDR(vh->sc_ifp);
1194 IF_ADDR_UNLOCK(SC2IFP(vh));
1195 CARP_UNLOCK(cif);
1196 return (1);
1197 } else {
1198 IF_ADDR_UNLOCK(SC2IFP(vh));
1199 CARP_UNLOCK(cif);
1200 return (0);
1201 }
1202 }
1203 count++;
1204 }
1205 }
1206 IF_ADDR_UNLOCK(SC2IFP(vh));
1207 }
1208 }
1209 } else {
1210 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1211 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1212 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1213 ia->ia_ifp == SC2IFP(vh) &&
1214 vh->sc_state == MASTER) {
1215 *enaddr = IF_LLADDR(vh->sc_ifp);
1216 CARP_UNLOCK(cif);
1217 return (1);
1218 }
1219 }
1220 }
1221 CARP_UNLOCK(cif);
1222 return (0);
1223}
1224
1225#ifdef INET6
1226struct ifaddr *
1227carp_iamatch6(void *v, struct in6_addr *taddr)
1228{
1229 struct carp_if *cif = v;
1230 struct carp_softc *vh;
1231 struct ifaddr *ifa;
1232
1233 CARP_LOCK(cif);
1234 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1235 IF_ADDR_LOCK(SC2IFP(vh));
1236 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist, ifa_list) {
1237 if (IN6_ARE_ADDR_EQUAL(taddr,
1238 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1239 (SC2IFP(vh)->if_flags & IFF_UP) &&
1240 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1241 vh->sc_state == MASTER) {
1242 IF_ADDR_UNLOCK(SC2IFP(vh));
1243 CARP_UNLOCK(cif);
1244 return (ifa);
1245 }
1246 }
1247 IF_ADDR_UNLOCK(SC2IFP(vh));
1248 }
1249 CARP_UNLOCK(cif);
1250
1251 return (NULL);
1252}
1253
1254void *
1255carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1256{
1257 struct m_tag *mtag;
1258 struct carp_if *cif = v;
1259 struct carp_softc *sc;
1260 struct ifaddr *ifa;
1261
1262 CARP_LOCK(cif);
1263 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1264 IF_ADDR_LOCK(SC2IFP(sc));
1265 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1266 if (IN6_ARE_ADDR_EQUAL(taddr,
1267 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1268 (SC2IFP(sc)->if_flags & IFF_UP) &&
1269 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING)) {
1270 struct ifnet *ifp = SC2IFP(sc);
1271 mtag = m_tag_get(PACKET_TAG_CARP,
1272 sizeof(struct ifnet *), M_NOWAIT);
1273 if (mtag == NULL) {
1274 /* better a bit than nothing */
1275 IF_ADDR_UNLOCK(SC2IFP(sc));
1276 CARP_UNLOCK(cif);
1277 return (IF_LLADDR(sc->sc_ifp));
1278 }
1279 bcopy(&ifp, (caddr_t)(mtag + 1),
1280 sizeof(struct ifnet *));
1281 m_tag_prepend(m, mtag);
1282
1283 IF_ADDR_UNLOCK(SC2IFP(sc));
1284 CARP_UNLOCK(cif);
1285 return (IF_LLADDR(sc->sc_ifp));
1286 }
1287 }
1288 IF_ADDR_UNLOCK(SC2IFP(sc));
1289 }
1290 CARP_UNLOCK(cif);
1291
1292 return (NULL);
1293}
1294#endif
1295
1296struct ifnet *
1297carp_forus(void *v, void *dhost)
1298{
1299 struct carp_if *cif = v;
1300 struct carp_softc *vh;
1301 u_int8_t *ena = dhost;
1302
1303 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1304 return (NULL);
1305
1306 CARP_LOCK(cif);
1307 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list)
1308 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1309 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1310 vh->sc_state == MASTER &&
1311 !bcmp(dhost, IF_LLADDR(vh->sc_ifp), ETHER_ADDR_LEN)) {
1312 CARP_UNLOCK(cif);
1313 return (SC2IFP(vh));
1314 }
1315
1316 CARP_UNLOCK(cif);
1317 return (NULL);
1318}
1319
1320static void
1321carp_master_down(void *v)
1322{
1323 struct carp_softc *sc = v;
1324
1325 CARP_SCLOCK(sc);
1326 carp_master_down_locked(sc);
1327 CARP_SCUNLOCK(sc);
1328}
1329
1330static void
1331carp_master_down_locked(struct carp_softc *sc)
1332{
1333 if (sc->sc_carpdev)
1334 CARP_SCLOCK_ASSERT(sc);
1335
1336 switch (sc->sc_state) {
1337 case INIT:
1338 printf("%s: master_down event in INIT state\n",
1339 SC2IFP(sc)->if_xname);
1340 break;
1341 case MASTER:
1342 break;
1343 case BACKUP:
1344 carp_set_state(sc, MASTER);
1345 carp_send_ad_locked(sc);
1346 carp_send_arp(sc);
1347#ifdef INET6
1348 carp_send_na(sc);
1349#endif /* INET6 */
1350 carp_setrun(sc, 0);
1351 carp_setroute(sc, RTM_ADD);
1352 break;
1353 }
1354}
1355
1356/*
1357 * When in backup state, af indicates whether to reset the master down timer
1358 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1359 */
1360static void
1361carp_setrun(struct carp_softc *sc, sa_family_t af)
1362{
1363 struct timeval tv;
1364
1365 if (sc->sc_carpdev == NULL) {
1366 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1367 carp_set_state(sc, INIT);
1368 return;
1369 } else
1370 CARP_SCLOCK_ASSERT(sc);
1371
1372 if (SC2IFP(sc)->if_flags & IFF_UP &&
1373 sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6))
1374 SC2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
1375 else {
1376 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1377 carp_setroute(sc, RTM_DELETE);
1378 return;
1379 }
1380
1381 switch (sc->sc_state) {
1382 case INIT:
1383 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1384 carp_send_ad_locked(sc);
1385 carp_send_arp(sc);
1386#ifdef INET6
1387 carp_send_na(sc);
1388#endif /* INET6 */
1389 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1390 SC2IFP(sc)->if_xname);
1391 carp_set_state(sc, MASTER);
1392 carp_setroute(sc, RTM_ADD);
1393 } else {
1394 CARP_DEBUG("%s: INIT -> BACKUP\n", SC2IFP(sc)->if_xname);
1395 carp_set_state(sc, BACKUP);
1396 carp_setroute(sc, RTM_DELETE);
1397 carp_setrun(sc, 0);
1398 }
1399 break;
1400 case BACKUP:
1401 callout_stop(&sc->sc_ad_tmo);
1402 tv.tv_sec = 3 * sc->sc_advbase;
1403 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1404 switch (af) {
1405#ifdef INET
1406 case AF_INET:
1407 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1408 carp_master_down, sc);
1409 break;
1410#endif /* INET */
1411#ifdef INET6
1412 case AF_INET6:
1413 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1414 carp_master_down, sc);
1415 break;
1416#endif /* INET6 */
1417 default:
1418 if (sc->sc_naddrs)
1419 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1420 carp_master_down, sc);
1421 if (sc->sc_naddrs6)
1422 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1423 carp_master_down, sc);
1424 break;
1425 }
1426 break;
1427 case MASTER:
1428 tv.tv_sec = sc->sc_advbase;
1429 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1430 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1431 carp_send_ad, sc);
1432 break;
1433 }
1434}
1435
1436static void
1437carp_multicast_cleanup(struct carp_softc *sc)
1438{
1439 struct ip_moptions *imo = &sc->sc_imo;
1440 u_int16_t n = imo->imo_num_memberships;
1441
1442 /* Clean up our own multicast memberships */
1443 while (n-- > 0) {
1444 if (imo->imo_membership[n] != NULL) {
1445 in_delmulti(imo->imo_membership[n]);
1446 imo->imo_membership[n] = NULL;
1447 }
1448 }
1449 KASSERT(imo->imo_mfilters == NULL,
1450 ("%s: imo_mfilters != NULL", __func__));
1451 imo->imo_num_memberships = 0;
1452 imo->imo_multicast_ifp = NULL;
1453}
1454
1455#ifdef INET6
1456static void
1457carp_multicast6_cleanup(struct carp_softc *sc)
1458{
1459 struct ip6_moptions *im6o = &sc->sc_im6o;
1460 u_int16_t n = im6o->im6o_num_memberships;
1452
1461
1453 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1454 struct in6_multi_mship *imm =
1455 LIST_FIRST(&im6o->im6o_memberships);
1456
1457 LIST_REMOVE(imm, i6mm_chain);
1458 in6_leavegroup(imm);
1462 while (n-- > 0) {
1463 if (im6o->im6o_membership[n] != NULL) {
1464 in6_mc_leave(im6o->im6o_membership[n], NULL);
1465 im6o->im6o_membership[n] = NULL;
1466 }
1459 }
1467 }
1468 KASSERT(im6o->im6o_mfilters == NULL,
1469 ("%s: im6o_mfilters != NULL", __func__));
1470 im6o->im6o_num_memberships = 0;
1460 im6o->im6o_multicast_ifp = NULL;
1461}
1462#endif
1463
1464static int
1465carp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1466{
1467 INIT_VNET_INET(curvnet);
1468 struct ifnet *ifp;
1469 struct carp_if *cif;
1470 struct in_ifaddr *ia, *ia_if;
1471 struct ip_moptions *imo = &sc->sc_imo;
1472 struct in_addr addr;
1473 u_long iaddr = htonl(sin->sin_addr.s_addr);
1474 int own, error;
1475
1476 if (sin->sin_addr.s_addr == 0) {
1477 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1478 carp_set_state(sc, INIT);
1479 if (sc->sc_naddrs)
1480 SC2IFP(sc)->if_flags |= IFF_UP;
1481 if (sc->sc_carpdev)
1482 CARP_SCLOCK(sc);
1483 carp_setrun(sc, 0);
1484 if (sc->sc_carpdev)
1485 CARP_SCUNLOCK(sc);
1486 return (0);
1487 }
1488
1489 /* we have to do it by hands to check we won't match on us */
1490 ia_if = NULL; own = 0;
1491 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1492 /* and, yeah, we need a multicast-capable iface too */
1493 if (ia->ia_ifp != SC2IFP(sc) &&
1494 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1495 (iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1496 if (!ia_if)
1497 ia_if = ia;
1498 if (sin->sin_addr.s_addr ==
1499 ia->ia_addr.sin_addr.s_addr)
1500 own++;
1501 }
1502 }
1503
1504 if (!ia_if)
1505 return (EADDRNOTAVAIL);
1506
1507 ia = ia_if;
1508 ifp = ia->ia_ifp;
1509
1510 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1511 (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp))
1512 return (EADDRNOTAVAIL);
1513
1514 if (imo->imo_num_memberships == 0) {
1515 addr.s_addr = htonl(INADDR_CARP_GROUP);
1516 if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL)
1517 return (ENOBUFS);
1518 imo->imo_num_memberships++;
1519 imo->imo_multicast_ifp = ifp;
1520 imo->imo_multicast_ttl = CARP_DFLTTL;
1521 imo->imo_multicast_loop = 0;
1522 }
1523
1524 if (!ifp->if_carp) {
1525
1526 cif = malloc(sizeof(*cif), M_CARP,
1527 M_WAITOK|M_ZERO);
1528 if (!cif) {
1529 error = ENOBUFS;
1530 goto cleanup;
1531 }
1532 if ((error = ifpromisc(ifp, 1))) {
1533 free(cif, M_CARP);
1534 goto cleanup;
1535 }
1536
1537 CARP_LOCK_INIT(cif);
1538 CARP_LOCK(cif);
1539 cif->vhif_ifp = ifp;
1540 TAILQ_INIT(&cif->vhif_vrs);
1541 ifp->if_carp = cif;
1542
1543 } else {
1544 struct carp_softc *vr;
1545
1546 cif = (struct carp_if *)ifp->if_carp;
1547 CARP_LOCK(cif);
1548 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1549 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1550 CARP_UNLOCK(cif);
1551 error = EEXIST;
1552 goto cleanup;
1553 }
1554 }
1555 sc->sc_ia = ia;
1556 sc->sc_carpdev = ifp;
1557
1558 { /* XXX prevent endless loop if already in queue */
1559 struct carp_softc *vr, *after = NULL;
1560 int myself = 0;
1561 cif = (struct carp_if *)ifp->if_carp;
1562
1563 /* XXX: cif should not change, right? So we still hold the lock */
1564 CARP_LOCK_ASSERT(cif);
1565
1566 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1567 if (vr == sc)
1568 myself = 1;
1569 if (vr->sc_vhid < sc->sc_vhid)
1570 after = vr;
1571 }
1572
1573 if (!myself) {
1574 /* We're trying to keep things in order */
1575 if (after == NULL) {
1576 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1577 } else {
1578 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1579 }
1580 cif->vhif_nvrs++;
1581 }
1582 }
1583
1584 sc->sc_naddrs++;
1585 SC2IFP(sc)->if_flags |= IFF_UP;
1586 if (own)
1587 sc->sc_advskew = 0;
1588 carp_sc_state_locked(sc);
1589 carp_setrun(sc, 0);
1590
1591 CARP_UNLOCK(cif);
1592
1593 return (0);
1594
1595cleanup:
1596 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1597 return (error);
1598}
1599
1600static int
1601carp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1602{
1603 int error = 0;
1604
1605 if (!--sc->sc_naddrs) {
1606 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1607 struct ip_moptions *imo = &sc->sc_imo;
1608
1609 CARP_LOCK(cif);
1610 callout_stop(&sc->sc_ad_tmo);
1611 SC2IFP(sc)->if_flags &= ~IFF_UP;
1612 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1613 sc->sc_vhid = -1;
1614 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1615 imo->imo_multicast_ifp = NULL;
1616 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1617 if (!--cif->vhif_nvrs) {
1618 sc->sc_carpdev->if_carp = NULL;
1619 CARP_LOCK_DESTROY(cif);
1620 free(cif, M_IFADDR);
1621 } else {
1622 CARP_UNLOCK(cif);
1623 }
1624 }
1625
1626 return (error);
1627}
1628
1629#ifdef INET6
1630static int
1631carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1632{
1633 INIT_VNET_INET6(curvnet);
1634 struct ifnet *ifp;
1635 struct carp_if *cif;
1636 struct in6_ifaddr *ia, *ia_if;
1637 struct ip6_moptions *im6o = &sc->sc_im6o;
1471 im6o->im6o_multicast_ifp = NULL;
1472}
1473#endif
1474
1475static int
1476carp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1477{
1478 INIT_VNET_INET(curvnet);
1479 struct ifnet *ifp;
1480 struct carp_if *cif;
1481 struct in_ifaddr *ia, *ia_if;
1482 struct ip_moptions *imo = &sc->sc_imo;
1483 struct in_addr addr;
1484 u_long iaddr = htonl(sin->sin_addr.s_addr);
1485 int own, error;
1486
1487 if (sin->sin_addr.s_addr == 0) {
1488 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1489 carp_set_state(sc, INIT);
1490 if (sc->sc_naddrs)
1491 SC2IFP(sc)->if_flags |= IFF_UP;
1492 if (sc->sc_carpdev)
1493 CARP_SCLOCK(sc);
1494 carp_setrun(sc, 0);
1495 if (sc->sc_carpdev)
1496 CARP_SCUNLOCK(sc);
1497 return (0);
1498 }
1499
1500 /* we have to do it by hands to check we won't match on us */
1501 ia_if = NULL; own = 0;
1502 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1503 /* and, yeah, we need a multicast-capable iface too */
1504 if (ia->ia_ifp != SC2IFP(sc) &&
1505 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1506 (iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1507 if (!ia_if)
1508 ia_if = ia;
1509 if (sin->sin_addr.s_addr ==
1510 ia->ia_addr.sin_addr.s_addr)
1511 own++;
1512 }
1513 }
1514
1515 if (!ia_if)
1516 return (EADDRNOTAVAIL);
1517
1518 ia = ia_if;
1519 ifp = ia->ia_ifp;
1520
1521 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1522 (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp))
1523 return (EADDRNOTAVAIL);
1524
1525 if (imo->imo_num_memberships == 0) {
1526 addr.s_addr = htonl(INADDR_CARP_GROUP);
1527 if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL)
1528 return (ENOBUFS);
1529 imo->imo_num_memberships++;
1530 imo->imo_multicast_ifp = ifp;
1531 imo->imo_multicast_ttl = CARP_DFLTTL;
1532 imo->imo_multicast_loop = 0;
1533 }
1534
1535 if (!ifp->if_carp) {
1536
1537 cif = malloc(sizeof(*cif), M_CARP,
1538 M_WAITOK|M_ZERO);
1539 if (!cif) {
1540 error = ENOBUFS;
1541 goto cleanup;
1542 }
1543 if ((error = ifpromisc(ifp, 1))) {
1544 free(cif, M_CARP);
1545 goto cleanup;
1546 }
1547
1548 CARP_LOCK_INIT(cif);
1549 CARP_LOCK(cif);
1550 cif->vhif_ifp = ifp;
1551 TAILQ_INIT(&cif->vhif_vrs);
1552 ifp->if_carp = cif;
1553
1554 } else {
1555 struct carp_softc *vr;
1556
1557 cif = (struct carp_if *)ifp->if_carp;
1558 CARP_LOCK(cif);
1559 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1560 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1561 CARP_UNLOCK(cif);
1562 error = EEXIST;
1563 goto cleanup;
1564 }
1565 }
1566 sc->sc_ia = ia;
1567 sc->sc_carpdev = ifp;
1568
1569 { /* XXX prevent endless loop if already in queue */
1570 struct carp_softc *vr, *after = NULL;
1571 int myself = 0;
1572 cif = (struct carp_if *)ifp->if_carp;
1573
1574 /* XXX: cif should not change, right? So we still hold the lock */
1575 CARP_LOCK_ASSERT(cif);
1576
1577 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1578 if (vr == sc)
1579 myself = 1;
1580 if (vr->sc_vhid < sc->sc_vhid)
1581 after = vr;
1582 }
1583
1584 if (!myself) {
1585 /* We're trying to keep things in order */
1586 if (after == NULL) {
1587 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1588 } else {
1589 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1590 }
1591 cif->vhif_nvrs++;
1592 }
1593 }
1594
1595 sc->sc_naddrs++;
1596 SC2IFP(sc)->if_flags |= IFF_UP;
1597 if (own)
1598 sc->sc_advskew = 0;
1599 carp_sc_state_locked(sc);
1600 carp_setrun(sc, 0);
1601
1602 CARP_UNLOCK(cif);
1603
1604 return (0);
1605
1606cleanup:
1607 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1608 return (error);
1609}
1610
1611static int
1612carp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1613{
1614 int error = 0;
1615
1616 if (!--sc->sc_naddrs) {
1617 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1618 struct ip_moptions *imo = &sc->sc_imo;
1619
1620 CARP_LOCK(cif);
1621 callout_stop(&sc->sc_ad_tmo);
1622 SC2IFP(sc)->if_flags &= ~IFF_UP;
1623 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1624 sc->sc_vhid = -1;
1625 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1626 imo->imo_multicast_ifp = NULL;
1627 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1628 if (!--cif->vhif_nvrs) {
1629 sc->sc_carpdev->if_carp = NULL;
1630 CARP_LOCK_DESTROY(cif);
1631 free(cif, M_IFADDR);
1632 } else {
1633 CARP_UNLOCK(cif);
1634 }
1635 }
1636
1637 return (error);
1638}
1639
1640#ifdef INET6
1641static int
1642carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1643{
1644 INIT_VNET_INET6(curvnet);
1645 struct ifnet *ifp;
1646 struct carp_if *cif;
1647 struct in6_ifaddr *ia, *ia_if;
1648 struct ip6_moptions *im6o = &sc->sc_im6o;
1638 struct in6_multi_mship *imm;
1639 struct in6_addr in6;
1640 int own, error;
1641
1649 struct in6_addr in6;
1650 int own, error;
1651
1652 error = 0;
1653
1642 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1643 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1644 carp_set_state(sc, INIT);
1645 if (sc->sc_naddrs6)
1646 SC2IFP(sc)->if_flags |= IFF_UP;
1647 if (sc->sc_carpdev)
1648 CARP_SCLOCK(sc);
1649 carp_setrun(sc, 0);
1650 if (sc->sc_carpdev)
1651 CARP_SCUNLOCK(sc);
1652 return (0);
1653 }
1654
1655 /* we have to do it by hands to check we won't match on us */
1656 ia_if = NULL; own = 0;
1657 for (ia = V_in6_ifaddr; ia; ia = ia->ia_next) {
1658 int i;
1659
1660 for (i = 0; i < 4; i++) {
1661 if ((sin6->sin6_addr.s6_addr32[i] &
1662 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1663 (ia->ia_addr.sin6_addr.s6_addr32[i] &
1664 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1665 break;
1666 }
1667 /* and, yeah, we need a multicast-capable iface too */
1668 if (ia->ia_ifp != SC2IFP(sc) &&
1669 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1670 (i == 4)) {
1671 if (!ia_if)
1672 ia_if = ia;
1673 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1674 &ia->ia_addr.sin6_addr))
1675 own++;
1676 }
1677 }
1678
1679 if (!ia_if)
1680 return (EADDRNOTAVAIL);
1681 ia = ia_if;
1682 ifp = ia->ia_ifp;
1683
1684 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1685 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1686 return (EADDRNOTAVAIL);
1687
1688 if (!sc->sc_naddrs6) {
1654 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1655 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1656 carp_set_state(sc, INIT);
1657 if (sc->sc_naddrs6)
1658 SC2IFP(sc)->if_flags |= IFF_UP;
1659 if (sc->sc_carpdev)
1660 CARP_SCLOCK(sc);
1661 carp_setrun(sc, 0);
1662 if (sc->sc_carpdev)
1663 CARP_SCUNLOCK(sc);
1664 return (0);
1665 }
1666
1667 /* we have to do it by hands to check we won't match on us */
1668 ia_if = NULL; own = 0;
1669 for (ia = V_in6_ifaddr; ia; ia = ia->ia_next) {
1670 int i;
1671
1672 for (i = 0; i < 4; i++) {
1673 if ((sin6->sin6_addr.s6_addr32[i] &
1674 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1675 (ia->ia_addr.sin6_addr.s6_addr32[i] &
1676 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1677 break;
1678 }
1679 /* and, yeah, we need a multicast-capable iface too */
1680 if (ia->ia_ifp != SC2IFP(sc) &&
1681 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1682 (i == 4)) {
1683 if (!ia_if)
1684 ia_if = ia;
1685 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1686 &ia->ia_addr.sin6_addr))
1687 own++;
1688 }
1689 }
1690
1691 if (!ia_if)
1692 return (EADDRNOTAVAIL);
1693 ia = ia_if;
1694 ifp = ia->ia_ifp;
1695
1696 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1697 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1698 return (EADDRNOTAVAIL);
1699
1700 if (!sc->sc_naddrs6) {
1701 struct in6_multi *in6m;
1702
1689 im6o->im6o_multicast_ifp = ifp;
1690
1691 /* join CARP multicast address */
1692 bzero(&in6, sizeof(in6));
1693 in6.s6_addr16[0] = htons(0xff02);
1694 in6.s6_addr8[15] = 0x12;
1695 if (in6_setscope(&in6, ifp, NULL) != 0)
1696 goto cleanup;
1703 im6o->im6o_multicast_ifp = ifp;
1704
1705 /* join CARP multicast address */
1706 bzero(&in6, sizeof(in6));
1707 in6.s6_addr16[0] = htons(0xff02);
1708 in6.s6_addr8[15] = 0x12;
1709 if (in6_setscope(&in6, ifp, NULL) != 0)
1710 goto cleanup;
1697 if ((imm = in6_joingroup(ifp, &in6, &error, 0)) == NULL)
1711 in6m = NULL;
1712 error = in6_mc_join(ifp, &in6, NULL, &in6m, 0);
1713 if (error)
1698 goto cleanup;
1714 goto cleanup;
1699 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1715 im6o->im6o_membership[0] = in6m;
1716 im6o->im6o_num_memberships++;
1700
1701 /* join solicited multicast address */
1702 bzero(&in6, sizeof(in6));
1703 in6.s6_addr16[0] = htons(0xff02);
1704 in6.s6_addr32[1] = 0;
1705 in6.s6_addr32[2] = htonl(1);
1706 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1707 in6.s6_addr8[12] = 0xff;
1708 if (in6_setscope(&in6, ifp, NULL) != 0)
1709 goto cleanup;
1717
1718 /* join solicited multicast address */
1719 bzero(&in6, sizeof(in6));
1720 in6.s6_addr16[0] = htons(0xff02);
1721 in6.s6_addr32[1] = 0;
1722 in6.s6_addr32[2] = htonl(1);
1723 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1724 in6.s6_addr8[12] = 0xff;
1725 if (in6_setscope(&in6, ifp, NULL) != 0)
1726 goto cleanup;
1710 if ((imm = in6_joingroup(ifp, &in6, &error, 0)) == NULL)
1727 in6m = NULL;
1728 error = in6_mc_join(ifp, &in6, NULL, &in6m, 0);
1729 if (error)
1711 goto cleanup;
1730 goto cleanup;
1712 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1731 im6o->im6o_membership[1] = in6m;
1732 im6o->im6o_num_memberships++;
1713 }
1714
1715 if (!ifp->if_carp) {
1716 cif = malloc(sizeof(*cif), M_CARP,
1717 M_WAITOK|M_ZERO);
1718 if (!cif) {
1719 error = ENOBUFS;
1720 goto cleanup;
1721 }
1722 if ((error = ifpromisc(ifp, 1))) {
1723 free(cif, M_CARP);
1724 goto cleanup;
1725 }
1726
1727 CARP_LOCK_INIT(cif);
1728 CARP_LOCK(cif);
1729 cif->vhif_ifp = ifp;
1730 TAILQ_INIT(&cif->vhif_vrs);
1731 ifp->if_carp = cif;
1732
1733 } else {
1734 struct carp_softc *vr;
1735
1736 cif = (struct carp_if *)ifp->if_carp;
1737 CARP_LOCK(cif);
1738 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1739 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1740 CARP_UNLOCK(cif);
1741 error = EINVAL;
1742 goto cleanup;
1743 }
1744 }
1745 sc->sc_ia6 = ia;
1746 sc->sc_carpdev = ifp;
1747
1748 { /* XXX prevent endless loop if already in queue */
1749 struct carp_softc *vr, *after = NULL;
1750 int myself = 0;
1751 cif = (struct carp_if *)ifp->if_carp;
1752 CARP_LOCK_ASSERT(cif);
1753
1754 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1755 if (vr == sc)
1756 myself = 1;
1757 if (vr->sc_vhid < sc->sc_vhid)
1758 after = vr;
1759 }
1760
1761 if (!myself) {
1762 /* We're trying to keep things in order */
1763 if (after == NULL) {
1764 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1765 } else {
1766 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1767 }
1768 cif->vhif_nvrs++;
1769 }
1770 }
1771
1772 sc->sc_naddrs6++;
1773 SC2IFP(sc)->if_flags |= IFF_UP;
1774 if (own)
1775 sc->sc_advskew = 0;
1776 carp_sc_state_locked(sc);
1777 carp_setrun(sc, 0);
1778
1779 CARP_UNLOCK(cif);
1780
1781 return (0);
1782
1783cleanup:
1733 }
1734
1735 if (!ifp->if_carp) {
1736 cif = malloc(sizeof(*cif), M_CARP,
1737 M_WAITOK|M_ZERO);
1738 if (!cif) {
1739 error = ENOBUFS;
1740 goto cleanup;
1741 }
1742 if ((error = ifpromisc(ifp, 1))) {
1743 free(cif, M_CARP);
1744 goto cleanup;
1745 }
1746
1747 CARP_LOCK_INIT(cif);
1748 CARP_LOCK(cif);
1749 cif->vhif_ifp = ifp;
1750 TAILQ_INIT(&cif->vhif_vrs);
1751 ifp->if_carp = cif;
1752
1753 } else {
1754 struct carp_softc *vr;
1755
1756 cif = (struct carp_if *)ifp->if_carp;
1757 CARP_LOCK(cif);
1758 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1759 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1760 CARP_UNLOCK(cif);
1761 error = EINVAL;
1762 goto cleanup;
1763 }
1764 }
1765 sc->sc_ia6 = ia;
1766 sc->sc_carpdev = ifp;
1767
1768 { /* XXX prevent endless loop if already in queue */
1769 struct carp_softc *vr, *after = NULL;
1770 int myself = 0;
1771 cif = (struct carp_if *)ifp->if_carp;
1772 CARP_LOCK_ASSERT(cif);
1773
1774 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1775 if (vr == sc)
1776 myself = 1;
1777 if (vr->sc_vhid < sc->sc_vhid)
1778 after = vr;
1779 }
1780
1781 if (!myself) {
1782 /* We're trying to keep things in order */
1783 if (after == NULL) {
1784 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1785 } else {
1786 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1787 }
1788 cif->vhif_nvrs++;
1789 }
1790 }
1791
1792 sc->sc_naddrs6++;
1793 SC2IFP(sc)->if_flags |= IFF_UP;
1794 if (own)
1795 sc->sc_advskew = 0;
1796 carp_sc_state_locked(sc);
1797 carp_setrun(sc, 0);
1798
1799 CARP_UNLOCK(cif);
1800
1801 return (0);
1802
1803cleanup:
1784 /* clean up multicast memberships */
1785 if (!sc->sc_naddrs6) {
1786 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1787 imm = LIST_FIRST(&im6o->im6o_memberships);
1788 LIST_REMOVE(imm, i6mm_chain);
1789 in6_leavegroup(imm);
1790 }
1791 }
1804 if (!sc->sc_naddrs6)
1805 carp_multicast6_cleanup(sc);
1792 return (error);
1793}
1794
1795static int
1796carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1797{
1798 int error = 0;
1799
1800 if (!--sc->sc_naddrs6) {
1801 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1806 return (error);
1807}
1808
1809static int
1810carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1811{
1812 int error = 0;
1813
1814 if (!--sc->sc_naddrs6) {
1815 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1802 struct ip6_moptions *im6o = &sc->sc_im6o;
1803
1804 CARP_LOCK(cif);
1805 callout_stop(&sc->sc_ad_tmo);
1806 SC2IFP(sc)->if_flags &= ~IFF_UP;
1807 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1808 sc->sc_vhid = -1;
1816
1817 CARP_LOCK(cif);
1818 callout_stop(&sc->sc_ad_tmo);
1819 SC2IFP(sc)->if_flags &= ~IFF_UP;
1820 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1821 sc->sc_vhid = -1;
1809 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1810 struct in6_multi_mship *imm =
1811 LIST_FIRST(&im6o->im6o_memberships);
1812
1813 LIST_REMOVE(imm, i6mm_chain);
1814 in6_leavegroup(imm);
1815 }
1816 im6o->im6o_multicast_ifp = NULL;
1822 carp_multicast6_cleanup(sc);
1817 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1818 if (!--cif->vhif_nvrs) {
1819 CARP_LOCK_DESTROY(cif);
1820 sc->sc_carpdev->if_carp = NULL;
1821 free(cif, M_IFADDR);
1822 } else
1823 CARP_UNLOCK(cif);
1824 }
1825
1826 return (error);
1827}
1828#endif /* INET6 */
1829
1830static int
1831carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr)
1832{
1833 struct carp_softc *sc = ifp->if_softc, *vr;
1834 struct carpreq carpr;
1835 struct ifaddr *ifa;
1836 struct ifreq *ifr;
1837 struct ifaliasreq *ifra;
1838 int locked = 0, error = 0;
1839
1840 ifa = (struct ifaddr *)addr;
1841 ifra = (struct ifaliasreq *)addr;
1842 ifr = (struct ifreq *)addr;
1843
1844 switch (cmd) {
1845 case SIOCSIFADDR:
1846 switch (ifa->ifa_addr->sa_family) {
1847#ifdef INET
1848 case AF_INET:
1849 SC2IFP(sc)->if_flags |= IFF_UP;
1850 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1851 sizeof(struct sockaddr));
1852 error = carp_set_addr(sc, satosin(ifa->ifa_addr));
1853 break;
1854#endif /* INET */
1855#ifdef INET6
1856 case AF_INET6:
1857 SC2IFP(sc)->if_flags |= IFF_UP;
1858 error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1859 break;
1860#endif /* INET6 */
1861 default:
1862 error = EAFNOSUPPORT;
1863 break;
1864 }
1865 break;
1866
1867 case SIOCAIFADDR:
1868 switch (ifa->ifa_addr->sa_family) {
1869#ifdef INET
1870 case AF_INET:
1871 SC2IFP(sc)->if_flags |= IFF_UP;
1872 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1873 sizeof(struct sockaddr));
1874 error = carp_set_addr(sc, satosin(&ifra->ifra_addr));
1875 break;
1876#endif /* INET */
1877#ifdef INET6
1878 case AF_INET6:
1879 SC2IFP(sc)->if_flags |= IFF_UP;
1880 error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1881 break;
1882#endif /* INET6 */
1883 default:
1884 error = EAFNOSUPPORT;
1885 break;
1886 }
1887 break;
1888
1889 case SIOCDIFADDR:
1890 switch (ifa->ifa_addr->sa_family) {
1891#ifdef INET
1892 case AF_INET:
1893 error = carp_del_addr(sc, satosin(&ifra->ifra_addr));
1894 break;
1895#endif /* INET */
1896#ifdef INET6
1897 case AF_INET6:
1898 error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1899 break;
1900#endif /* INET6 */
1901 default:
1902 error = EAFNOSUPPORT;
1903 break;
1904 }
1905 break;
1906
1907 case SIOCSIFFLAGS:
1908 if (sc->sc_carpdev) {
1909 locked = 1;
1910 CARP_SCLOCK(sc);
1911 }
1912 if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) {
1913 callout_stop(&sc->sc_ad_tmo);
1914 callout_stop(&sc->sc_md_tmo);
1915 callout_stop(&sc->sc_md6_tmo);
1916 if (sc->sc_state == MASTER)
1917 carp_send_ad_locked(sc);
1918 carp_set_state(sc, INIT);
1919 carp_setrun(sc, 0);
1920 } else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) {
1921 SC2IFP(sc)->if_flags |= IFF_UP;
1922 carp_setrun(sc, 0);
1923 }
1924 break;
1925
1926 case SIOCSVH:
1927 error = priv_check(curthread, PRIV_NETINET_CARP);
1928 if (error)
1929 break;
1930 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1931 break;
1932 error = 1;
1933 if (sc->sc_carpdev) {
1934 locked = 1;
1935 CARP_SCLOCK(sc);
1936 }
1937 if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1938 switch (carpr.carpr_state) {
1939 case BACKUP:
1940 callout_stop(&sc->sc_ad_tmo);
1941 carp_set_state(sc, BACKUP);
1942 carp_setrun(sc, 0);
1943 carp_setroute(sc, RTM_DELETE);
1944 break;
1945 case MASTER:
1946 carp_master_down_locked(sc);
1947 break;
1948 default:
1949 break;
1950 }
1951 }
1952 if (carpr.carpr_vhid > 0) {
1953 if (carpr.carpr_vhid > 255) {
1954 error = EINVAL;
1955 break;
1956 }
1957 if (sc->sc_carpdev) {
1958 struct carp_if *cif;
1959 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1960 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1961 if (vr != sc &&
1962 vr->sc_vhid == carpr.carpr_vhid) {
1963 error = EEXIST;
1964 break;
1965 }
1966 if (error == EEXIST)
1967 break;
1968 }
1969 sc->sc_vhid = carpr.carpr_vhid;
1970 IF_LLADDR(sc->sc_ifp)[0] = 0;
1971 IF_LLADDR(sc->sc_ifp)[1] = 0;
1972 IF_LLADDR(sc->sc_ifp)[2] = 0x5e;
1973 IF_LLADDR(sc->sc_ifp)[3] = 0;
1974 IF_LLADDR(sc->sc_ifp)[4] = 1;
1975 IF_LLADDR(sc->sc_ifp)[5] = sc->sc_vhid;
1976 error--;
1977 }
1978 if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
1979 if (carpr.carpr_advskew >= 255) {
1980 error = EINVAL;
1981 break;
1982 }
1983 if (carpr.carpr_advbase > 255) {
1984 error = EINVAL;
1985 break;
1986 }
1987 sc->sc_advbase = carpr.carpr_advbase;
1988 sc->sc_advskew = carpr.carpr_advskew;
1989 error--;
1990 }
1991 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1992 if (error > 0)
1993 error = EINVAL;
1994 else {
1995 error = 0;
1996 carp_setrun(sc, 0);
1997 }
1998 break;
1999
2000 case SIOCGVH:
2001 /* XXX: lockless read */
2002 bzero(&carpr, sizeof(carpr));
2003 carpr.carpr_state = sc->sc_state;
2004 carpr.carpr_vhid = sc->sc_vhid;
2005 carpr.carpr_advbase = sc->sc_advbase;
2006 carpr.carpr_advskew = sc->sc_advskew;
2007 error = priv_check(curthread, PRIV_NETINET_CARP);
2008 if (error == 0)
2009 bcopy(sc->sc_key, carpr.carpr_key,
2010 sizeof(carpr.carpr_key));
2011 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
2012 break;
2013
2014 default:
2015 error = EINVAL;
2016 }
2017
2018 if (locked)
2019 CARP_SCUNLOCK(sc);
2020
2021 carp_hmac_prepare(sc);
2022
2023 return (error);
2024}
2025
2026/*
2027 * XXX: this is looutput. We should eventually use it from there.
2028 */
2029static int
2030carp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
2031 struct route *ro)
2032{
2033 u_int32_t af;
2034 struct rtentry *rt = NULL;
2035
2036 M_ASSERTPKTHDR(m); /* check if we have the packet header */
2037
2038 if (ro != NULL)
2039 rt = ro->ro_rt;
2040 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
2041 m_freem(m);
2042 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
2043 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
2044 }
2045
2046 ifp->if_opackets++;
2047 ifp->if_obytes += m->m_pkthdr.len;
2048
2049 /* BPF writes need to be handled specially. */
2050 if (dst->sa_family == AF_UNSPEC) {
2051 bcopy(dst->sa_data, &af, sizeof(af));
2052 dst->sa_family = af;
2053 }
2054
2055#if 1 /* XXX */
2056 switch (dst->sa_family) {
2057 case AF_INET:
2058 case AF_INET6:
2059 case AF_IPX:
2060 case AF_APPLETALK:
2061 break;
2062 default:
2063 printf("carp_looutput: af=%d unexpected\n", dst->sa_family);
2064 m_freem(m);
2065 return (EAFNOSUPPORT);
2066 }
2067#endif
2068 return(if_simloop(ifp, m, dst->sa_family, 0));
2069}
2070
2071/*
2072 * Start output on carp interface. This function should never be called.
2073 */
2074static void
2075carp_start(struct ifnet *ifp)
2076{
2077#ifdef DEBUG
2078 printf("%s: start called\n", ifp->if_xname);
2079#endif
2080}
2081
2082int
2083carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
2084 struct rtentry *rt)
2085{
2086 struct m_tag *mtag;
2087 struct carp_softc *sc;
2088 struct ifnet *carp_ifp;
2089
2090 if (!sa)
2091 return (0);
2092
2093 switch (sa->sa_family) {
2094#ifdef INET
2095 case AF_INET:
2096 break;
2097#endif /* INET */
2098#ifdef INET6
2099 case AF_INET6:
2100 break;
2101#endif /* INET6 */
2102 default:
2103 return (0);
2104 }
2105
2106 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
2107 if (mtag == NULL)
2108 return (0);
2109
2110 bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
2111 sc = carp_ifp->if_softc;
2112
2113 /* Set the source MAC address to Virtual Router MAC Address */
2114 switch (ifp->if_type) {
2115 case IFT_ETHER:
2116 case IFT_L2VLAN: {
2117 struct ether_header *eh;
2118
2119 eh = mtod(m, struct ether_header *);
2120 eh->ether_shost[0] = 0;
2121 eh->ether_shost[1] = 0;
2122 eh->ether_shost[2] = 0x5e;
2123 eh->ether_shost[3] = 0;
2124 eh->ether_shost[4] = 1;
2125 eh->ether_shost[5] = sc->sc_vhid;
2126 }
2127 break;
2128 case IFT_FDDI: {
2129 struct fddi_header *fh;
2130
2131 fh = mtod(m, struct fddi_header *);
2132 fh->fddi_shost[0] = 0;
2133 fh->fddi_shost[1] = 0;
2134 fh->fddi_shost[2] = 0x5e;
2135 fh->fddi_shost[3] = 0;
2136 fh->fddi_shost[4] = 1;
2137 fh->fddi_shost[5] = sc->sc_vhid;
2138 }
2139 break;
2140 case IFT_ISO88025: {
2141 struct iso88025_header *th;
2142 th = mtod(m, struct iso88025_header *);
2143 th->iso88025_shost[0] = 3;
2144 th->iso88025_shost[1] = 0;
2145 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
2146 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
2147 th->iso88025_shost[4] = 0;
2148 th->iso88025_shost[5] = 0;
2149 }
2150 break;
2151 default:
2152 printf("%s: carp is not supported for this interface type\n",
2153 ifp->if_xname);
2154 return (EOPNOTSUPP);
2155 }
2156
2157 return (0);
2158}
2159
2160static void
2161carp_set_state(struct carp_softc *sc, int state)
2162{
2163 int link_state;
2164
2165 if (sc->sc_carpdev)
2166 CARP_SCLOCK_ASSERT(sc);
2167
2168 if (sc->sc_state == state)
2169 return;
2170
2171 sc->sc_state = state;
2172 switch (state) {
2173 case BACKUP:
2174 link_state = LINK_STATE_DOWN;
2175 break;
2176 case MASTER:
2177 link_state = LINK_STATE_UP;
2178 break;
2179 default:
2180 link_state = LINK_STATE_UNKNOWN;
2181 break;
2182 }
2183 if_link_state_change(SC2IFP(sc), link_state);
2184}
2185
2186void
2187carp_carpdev_state(void *v)
2188{
2189 struct carp_if *cif = v;
2190
2191 CARP_LOCK(cif);
2192 carp_carpdev_state_locked(cif);
2193 CARP_UNLOCK(cif);
2194}
2195
2196static void
2197carp_carpdev_state_locked(struct carp_if *cif)
2198{
2199 struct carp_softc *sc;
2200
2201 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2202 carp_sc_state_locked(sc);
2203}
2204
2205static void
2206carp_sc_state_locked(struct carp_softc *sc)
2207{
2208 CARP_SCLOCK_ASSERT(sc);
2209
2210 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2211 !(sc->sc_carpdev->if_flags & IFF_UP)) {
2212 sc->sc_flags_backup = SC2IFP(sc)->if_flags;
2213 SC2IFP(sc)->if_flags &= ~IFF_UP;
2214 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
2215 callout_stop(&sc->sc_ad_tmo);
2216 callout_stop(&sc->sc_md_tmo);
2217 callout_stop(&sc->sc_md6_tmo);
2218 carp_set_state(sc, INIT);
2219 carp_setrun(sc, 0);
2220 if (!sc->sc_suppress) {
2221 carp_suppress_preempt++;
2222 if (carp_suppress_preempt == 1) {
2223 CARP_SCUNLOCK(sc);
2224 carp_send_ad_all();
2225 CARP_SCLOCK(sc);
2226 }
2227 }
2228 sc->sc_suppress = 1;
2229 } else {
2230 SC2IFP(sc)->if_flags |= sc->sc_flags_backup;
2231 carp_set_state(sc, INIT);
2232 carp_setrun(sc, 0);
2233 if (sc->sc_suppress)
2234 carp_suppress_preempt--;
2235 sc->sc_suppress = 0;
2236 }
2237
2238 return;
2239}
2240
2241static int
2242carp_modevent(module_t mod, int type, void *data)
2243{
2244 switch (type) {
2245 case MOD_LOAD:
2246 if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
2247 carp_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
2248 if (if_detach_event_tag == NULL)
2249 return (ENOMEM);
2250 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2251 LIST_INIT(&carpif_list);
2252 if_clone_attach(&carp_cloner);
2253 break;
2254
2255 case MOD_UNLOAD:
2256 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
2257 if_clone_detach(&carp_cloner);
2258 mtx_destroy(&carp_mtx);
2259 break;
2260
2261 default:
2262 return (EINVAL);
2263 }
2264
2265 return (0);
2266}
2267
2268static moduledata_t carp_mod = {
2269 "carp",
2270 carp_modevent,
2271 0
2272};
2273
2274DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1823 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1824 if (!--cif->vhif_nvrs) {
1825 CARP_LOCK_DESTROY(cif);
1826 sc->sc_carpdev->if_carp = NULL;
1827 free(cif, M_IFADDR);
1828 } else
1829 CARP_UNLOCK(cif);
1830 }
1831
1832 return (error);
1833}
1834#endif /* INET6 */
1835
1836static int
1837carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr)
1838{
1839 struct carp_softc *sc = ifp->if_softc, *vr;
1840 struct carpreq carpr;
1841 struct ifaddr *ifa;
1842 struct ifreq *ifr;
1843 struct ifaliasreq *ifra;
1844 int locked = 0, error = 0;
1845
1846 ifa = (struct ifaddr *)addr;
1847 ifra = (struct ifaliasreq *)addr;
1848 ifr = (struct ifreq *)addr;
1849
1850 switch (cmd) {
1851 case SIOCSIFADDR:
1852 switch (ifa->ifa_addr->sa_family) {
1853#ifdef INET
1854 case AF_INET:
1855 SC2IFP(sc)->if_flags |= IFF_UP;
1856 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1857 sizeof(struct sockaddr));
1858 error = carp_set_addr(sc, satosin(ifa->ifa_addr));
1859 break;
1860#endif /* INET */
1861#ifdef INET6
1862 case AF_INET6:
1863 SC2IFP(sc)->if_flags |= IFF_UP;
1864 error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1865 break;
1866#endif /* INET6 */
1867 default:
1868 error = EAFNOSUPPORT;
1869 break;
1870 }
1871 break;
1872
1873 case SIOCAIFADDR:
1874 switch (ifa->ifa_addr->sa_family) {
1875#ifdef INET
1876 case AF_INET:
1877 SC2IFP(sc)->if_flags |= IFF_UP;
1878 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1879 sizeof(struct sockaddr));
1880 error = carp_set_addr(sc, satosin(&ifra->ifra_addr));
1881 break;
1882#endif /* INET */
1883#ifdef INET6
1884 case AF_INET6:
1885 SC2IFP(sc)->if_flags |= IFF_UP;
1886 error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1887 break;
1888#endif /* INET6 */
1889 default:
1890 error = EAFNOSUPPORT;
1891 break;
1892 }
1893 break;
1894
1895 case SIOCDIFADDR:
1896 switch (ifa->ifa_addr->sa_family) {
1897#ifdef INET
1898 case AF_INET:
1899 error = carp_del_addr(sc, satosin(&ifra->ifra_addr));
1900 break;
1901#endif /* INET */
1902#ifdef INET6
1903 case AF_INET6:
1904 error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1905 break;
1906#endif /* INET6 */
1907 default:
1908 error = EAFNOSUPPORT;
1909 break;
1910 }
1911 break;
1912
1913 case SIOCSIFFLAGS:
1914 if (sc->sc_carpdev) {
1915 locked = 1;
1916 CARP_SCLOCK(sc);
1917 }
1918 if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) {
1919 callout_stop(&sc->sc_ad_tmo);
1920 callout_stop(&sc->sc_md_tmo);
1921 callout_stop(&sc->sc_md6_tmo);
1922 if (sc->sc_state == MASTER)
1923 carp_send_ad_locked(sc);
1924 carp_set_state(sc, INIT);
1925 carp_setrun(sc, 0);
1926 } else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) {
1927 SC2IFP(sc)->if_flags |= IFF_UP;
1928 carp_setrun(sc, 0);
1929 }
1930 break;
1931
1932 case SIOCSVH:
1933 error = priv_check(curthread, PRIV_NETINET_CARP);
1934 if (error)
1935 break;
1936 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1937 break;
1938 error = 1;
1939 if (sc->sc_carpdev) {
1940 locked = 1;
1941 CARP_SCLOCK(sc);
1942 }
1943 if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1944 switch (carpr.carpr_state) {
1945 case BACKUP:
1946 callout_stop(&sc->sc_ad_tmo);
1947 carp_set_state(sc, BACKUP);
1948 carp_setrun(sc, 0);
1949 carp_setroute(sc, RTM_DELETE);
1950 break;
1951 case MASTER:
1952 carp_master_down_locked(sc);
1953 break;
1954 default:
1955 break;
1956 }
1957 }
1958 if (carpr.carpr_vhid > 0) {
1959 if (carpr.carpr_vhid > 255) {
1960 error = EINVAL;
1961 break;
1962 }
1963 if (sc->sc_carpdev) {
1964 struct carp_if *cif;
1965 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1966 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1967 if (vr != sc &&
1968 vr->sc_vhid == carpr.carpr_vhid) {
1969 error = EEXIST;
1970 break;
1971 }
1972 if (error == EEXIST)
1973 break;
1974 }
1975 sc->sc_vhid = carpr.carpr_vhid;
1976 IF_LLADDR(sc->sc_ifp)[0] = 0;
1977 IF_LLADDR(sc->sc_ifp)[1] = 0;
1978 IF_LLADDR(sc->sc_ifp)[2] = 0x5e;
1979 IF_LLADDR(sc->sc_ifp)[3] = 0;
1980 IF_LLADDR(sc->sc_ifp)[4] = 1;
1981 IF_LLADDR(sc->sc_ifp)[5] = sc->sc_vhid;
1982 error--;
1983 }
1984 if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
1985 if (carpr.carpr_advskew >= 255) {
1986 error = EINVAL;
1987 break;
1988 }
1989 if (carpr.carpr_advbase > 255) {
1990 error = EINVAL;
1991 break;
1992 }
1993 sc->sc_advbase = carpr.carpr_advbase;
1994 sc->sc_advskew = carpr.carpr_advskew;
1995 error--;
1996 }
1997 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1998 if (error > 0)
1999 error = EINVAL;
2000 else {
2001 error = 0;
2002 carp_setrun(sc, 0);
2003 }
2004 break;
2005
2006 case SIOCGVH:
2007 /* XXX: lockless read */
2008 bzero(&carpr, sizeof(carpr));
2009 carpr.carpr_state = sc->sc_state;
2010 carpr.carpr_vhid = sc->sc_vhid;
2011 carpr.carpr_advbase = sc->sc_advbase;
2012 carpr.carpr_advskew = sc->sc_advskew;
2013 error = priv_check(curthread, PRIV_NETINET_CARP);
2014 if (error == 0)
2015 bcopy(sc->sc_key, carpr.carpr_key,
2016 sizeof(carpr.carpr_key));
2017 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
2018 break;
2019
2020 default:
2021 error = EINVAL;
2022 }
2023
2024 if (locked)
2025 CARP_SCUNLOCK(sc);
2026
2027 carp_hmac_prepare(sc);
2028
2029 return (error);
2030}
2031
2032/*
2033 * XXX: this is looutput. We should eventually use it from there.
2034 */
2035static int
2036carp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
2037 struct route *ro)
2038{
2039 u_int32_t af;
2040 struct rtentry *rt = NULL;
2041
2042 M_ASSERTPKTHDR(m); /* check if we have the packet header */
2043
2044 if (ro != NULL)
2045 rt = ro->ro_rt;
2046 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
2047 m_freem(m);
2048 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
2049 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
2050 }
2051
2052 ifp->if_opackets++;
2053 ifp->if_obytes += m->m_pkthdr.len;
2054
2055 /* BPF writes need to be handled specially. */
2056 if (dst->sa_family == AF_UNSPEC) {
2057 bcopy(dst->sa_data, &af, sizeof(af));
2058 dst->sa_family = af;
2059 }
2060
2061#if 1 /* XXX */
2062 switch (dst->sa_family) {
2063 case AF_INET:
2064 case AF_INET6:
2065 case AF_IPX:
2066 case AF_APPLETALK:
2067 break;
2068 default:
2069 printf("carp_looutput: af=%d unexpected\n", dst->sa_family);
2070 m_freem(m);
2071 return (EAFNOSUPPORT);
2072 }
2073#endif
2074 return(if_simloop(ifp, m, dst->sa_family, 0));
2075}
2076
2077/*
2078 * Start output on carp interface. This function should never be called.
2079 */
2080static void
2081carp_start(struct ifnet *ifp)
2082{
2083#ifdef DEBUG
2084 printf("%s: start called\n", ifp->if_xname);
2085#endif
2086}
2087
2088int
2089carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
2090 struct rtentry *rt)
2091{
2092 struct m_tag *mtag;
2093 struct carp_softc *sc;
2094 struct ifnet *carp_ifp;
2095
2096 if (!sa)
2097 return (0);
2098
2099 switch (sa->sa_family) {
2100#ifdef INET
2101 case AF_INET:
2102 break;
2103#endif /* INET */
2104#ifdef INET6
2105 case AF_INET6:
2106 break;
2107#endif /* INET6 */
2108 default:
2109 return (0);
2110 }
2111
2112 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
2113 if (mtag == NULL)
2114 return (0);
2115
2116 bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
2117 sc = carp_ifp->if_softc;
2118
2119 /* Set the source MAC address to Virtual Router MAC Address */
2120 switch (ifp->if_type) {
2121 case IFT_ETHER:
2122 case IFT_L2VLAN: {
2123 struct ether_header *eh;
2124
2125 eh = mtod(m, struct ether_header *);
2126 eh->ether_shost[0] = 0;
2127 eh->ether_shost[1] = 0;
2128 eh->ether_shost[2] = 0x5e;
2129 eh->ether_shost[3] = 0;
2130 eh->ether_shost[4] = 1;
2131 eh->ether_shost[5] = sc->sc_vhid;
2132 }
2133 break;
2134 case IFT_FDDI: {
2135 struct fddi_header *fh;
2136
2137 fh = mtod(m, struct fddi_header *);
2138 fh->fddi_shost[0] = 0;
2139 fh->fddi_shost[1] = 0;
2140 fh->fddi_shost[2] = 0x5e;
2141 fh->fddi_shost[3] = 0;
2142 fh->fddi_shost[4] = 1;
2143 fh->fddi_shost[5] = sc->sc_vhid;
2144 }
2145 break;
2146 case IFT_ISO88025: {
2147 struct iso88025_header *th;
2148 th = mtod(m, struct iso88025_header *);
2149 th->iso88025_shost[0] = 3;
2150 th->iso88025_shost[1] = 0;
2151 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
2152 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
2153 th->iso88025_shost[4] = 0;
2154 th->iso88025_shost[5] = 0;
2155 }
2156 break;
2157 default:
2158 printf("%s: carp is not supported for this interface type\n",
2159 ifp->if_xname);
2160 return (EOPNOTSUPP);
2161 }
2162
2163 return (0);
2164}
2165
2166static void
2167carp_set_state(struct carp_softc *sc, int state)
2168{
2169 int link_state;
2170
2171 if (sc->sc_carpdev)
2172 CARP_SCLOCK_ASSERT(sc);
2173
2174 if (sc->sc_state == state)
2175 return;
2176
2177 sc->sc_state = state;
2178 switch (state) {
2179 case BACKUP:
2180 link_state = LINK_STATE_DOWN;
2181 break;
2182 case MASTER:
2183 link_state = LINK_STATE_UP;
2184 break;
2185 default:
2186 link_state = LINK_STATE_UNKNOWN;
2187 break;
2188 }
2189 if_link_state_change(SC2IFP(sc), link_state);
2190}
2191
2192void
2193carp_carpdev_state(void *v)
2194{
2195 struct carp_if *cif = v;
2196
2197 CARP_LOCK(cif);
2198 carp_carpdev_state_locked(cif);
2199 CARP_UNLOCK(cif);
2200}
2201
2202static void
2203carp_carpdev_state_locked(struct carp_if *cif)
2204{
2205 struct carp_softc *sc;
2206
2207 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2208 carp_sc_state_locked(sc);
2209}
2210
2211static void
2212carp_sc_state_locked(struct carp_softc *sc)
2213{
2214 CARP_SCLOCK_ASSERT(sc);
2215
2216 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2217 !(sc->sc_carpdev->if_flags & IFF_UP)) {
2218 sc->sc_flags_backup = SC2IFP(sc)->if_flags;
2219 SC2IFP(sc)->if_flags &= ~IFF_UP;
2220 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
2221 callout_stop(&sc->sc_ad_tmo);
2222 callout_stop(&sc->sc_md_tmo);
2223 callout_stop(&sc->sc_md6_tmo);
2224 carp_set_state(sc, INIT);
2225 carp_setrun(sc, 0);
2226 if (!sc->sc_suppress) {
2227 carp_suppress_preempt++;
2228 if (carp_suppress_preempt == 1) {
2229 CARP_SCUNLOCK(sc);
2230 carp_send_ad_all();
2231 CARP_SCLOCK(sc);
2232 }
2233 }
2234 sc->sc_suppress = 1;
2235 } else {
2236 SC2IFP(sc)->if_flags |= sc->sc_flags_backup;
2237 carp_set_state(sc, INIT);
2238 carp_setrun(sc, 0);
2239 if (sc->sc_suppress)
2240 carp_suppress_preempt--;
2241 sc->sc_suppress = 0;
2242 }
2243
2244 return;
2245}
2246
2247static int
2248carp_modevent(module_t mod, int type, void *data)
2249{
2250 switch (type) {
2251 case MOD_LOAD:
2252 if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
2253 carp_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
2254 if (if_detach_event_tag == NULL)
2255 return (ENOMEM);
2256 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2257 LIST_INIT(&carpif_list);
2258 if_clone_attach(&carp_cloner);
2259 break;
2260
2261 case MOD_UNLOAD:
2262 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
2263 if_clone_detach(&carp_cloner);
2264 mtx_destroy(&carp_mtx);
2265 break;
2266
2267 default:
2268 return (EINVAL);
2269 }
2270
2271 return (0);
2272}
2273
2274static moduledata_t carp_mod = {
2275 "carp",
2276 carp_modevent,
2277 0
2278};
2279
2280DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);