]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/pf/net/if_pfsync.c
Update to version 9.8.2, the latest from ISC, which contains numerous bug fixes.
[FreeBSD/FreeBSD.git] / sys / contrib / pf / net / if_pfsync.c
1 /*      $OpenBSD: if_pfsync.c,v 1.110 2009/02/24 05:39:19 dlg Exp $     */
2
3 /*
4  * Copyright (c) 2002 Michael Shalayeff
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  * THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /*
30  * Copyright (c) 2009 David Gwynne <dlg@openbsd.org>
31  *
32  * Permission to use, copy, modify, and distribute this software for any
33  * purpose with or without fee is hereby granted, provided that the above
34  * copyright notice and this permission notice appear in all copies.
35  *
36  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
37  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
38  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
39  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
40  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
41  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
42  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
43  */
44
45 /*
46  * Revisions picked from OpenBSD after revision 1.110 import:
47  * 1.118, 1.124, 1.148, 1.149, 1.151, 1.171 - fixes to bulk updates
48  * 1.120, 1.175 - use monotonic time_uptime
49  * 1.122 - reduce number of updates for non-TCP sessions
50  * 1.128 - cleanups
51  * 1.146 - bzero() mbuf before sparsely filling it with data
52  * 1.170 - SIOCSIFMTU checks
53  * 1.126, 1.142 - deferred packets processing
54  * 1.173 - correct expire time processing
55  */
56
57 #ifdef __FreeBSD__
58 #include "opt_inet.h"
59 #include "opt_inet6.h"
60 #include "opt_pf.h"
61
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
64
65 #define NBPFILTER       1
66 #endif /* __FreeBSD__ */
67
68 #include <sys/param.h>
69 #include <sys/kernel.h>
70 #ifdef __FreeBSD__
71 #include <sys/bus.h>
72 #include <sys/interrupt.h>
73 #include <sys/priv.h>
74 #endif
75 #include <sys/proc.h>
76 #include <sys/systm.h>
77 #include <sys/time.h>
78 #include <sys/mbuf.h>
79 #include <sys/socket.h>
80 #ifdef __FreeBSD__
81 #include <sys/endian.h>
82 #include <sys/malloc.h>
83 #include <sys/module.h>
84 #include <sys/sockio.h>
85 #include <sys/taskqueue.h>
86 #include <sys/lock.h>
87 #include <sys/mutex.h>
88 #include <sys/protosw.h>
89 #else
90 #include <sys/ioctl.h>
91 #include <sys/timeout.h>
92 #endif
93 #include <sys/sysctl.h>
94 #ifndef __FreeBSD__
95 #include <sys/pool.h>
96 #endif
97
98 #include <net/if.h>
99 #ifdef __FreeBSD__
100 #include <net/if_clone.h>
101 #endif
102 #include <net/if_types.h>
103 #include <net/route.h>
104 #include <net/bpf.h>
105 #include <net/netisr.h>
106 #ifdef __FreeBSD__
107 #include <net/vnet.h>
108 #endif
109
110 #include <netinet/in.h>
111 #include <netinet/if_ether.h>
112 #include <netinet/tcp.h>
113 #include <netinet/tcp_seq.h>
114
115 #ifdef  INET
116 #include <netinet/in_systm.h>
117 #include <netinet/in_var.h>
118 #include <netinet/ip.h>
119 #include <netinet/ip_var.h>
120 #endif
121
122 #ifdef INET6
123 #include <netinet6/nd6.h>
124 #endif /* INET6 */
125
126 #ifdef __FreeBSD__
127 #include <netinet/ip_carp.h>
128 #else
129 #include "carp.h"
130 #if NCARP > 0
131 #include <netinet/ip_carp.h>
132 #endif
133 #endif
134
135 #include <net/pfvar.h>
136 #include <net/if_pfsync.h>
137
138 #ifndef __FreeBSD__
139 #include "bpfilter.h"
140 #include "pfsync.h"
141 #endif
142
143 #define PFSYNC_MINPKT ( \
144         sizeof(struct ip) + \
145         sizeof(struct pfsync_header) + \
146         sizeof(struct pfsync_subheader) + \
147         sizeof(struct pfsync_eof))
148
149 struct pfsync_pkt {
150         struct ip *ip;
151         struct in_addr src;
152         u_int8_t flags;
153 };
154
155 int     pfsync_input_hmac(struct mbuf *, int);
156
157 int     pfsync_upd_tcp(struct pf_state *, struct pfsync_state_peer *,
158             struct pfsync_state_peer *);
159
160 int     pfsync_in_clr(struct pfsync_pkt *, struct mbuf *, int, int);
161 int     pfsync_in_ins(struct pfsync_pkt *, struct mbuf *, int, int);
162 int     pfsync_in_iack(struct pfsync_pkt *, struct mbuf *, int, int);
163 int     pfsync_in_upd(struct pfsync_pkt *, struct mbuf *, int, int);
164 int     pfsync_in_upd_c(struct pfsync_pkt *, struct mbuf *, int, int);
165 int     pfsync_in_ureq(struct pfsync_pkt *, struct mbuf *, int, int);
166 int     pfsync_in_del(struct pfsync_pkt *, struct mbuf *, int, int);
167 int     pfsync_in_del_c(struct pfsync_pkt *, struct mbuf *, int, int);
168 int     pfsync_in_bus(struct pfsync_pkt *, struct mbuf *, int, int);
169 int     pfsync_in_tdb(struct pfsync_pkt *, struct mbuf *, int, int);
170 int     pfsync_in_eof(struct pfsync_pkt *, struct mbuf *, int, int);
171
172 int     pfsync_in_error(struct pfsync_pkt *, struct mbuf *, int, int);
173
174 int     (*pfsync_acts[])(struct pfsync_pkt *, struct mbuf *, int, int) = {
175         pfsync_in_clr,                  /* PFSYNC_ACT_CLR */
176         pfsync_in_ins,                  /* PFSYNC_ACT_INS */
177         pfsync_in_iack,                 /* PFSYNC_ACT_INS_ACK */
178         pfsync_in_upd,                  /* PFSYNC_ACT_UPD */
179         pfsync_in_upd_c,                /* PFSYNC_ACT_UPD_C */
180         pfsync_in_ureq,                 /* PFSYNC_ACT_UPD_REQ */
181         pfsync_in_del,                  /* PFSYNC_ACT_DEL */
182         pfsync_in_del_c,                /* PFSYNC_ACT_DEL_C */
183         pfsync_in_error,                /* PFSYNC_ACT_INS_F */
184         pfsync_in_error,                /* PFSYNC_ACT_DEL_F */
185         pfsync_in_bus,                  /* PFSYNC_ACT_BUS */
186         pfsync_in_tdb,                  /* PFSYNC_ACT_TDB */
187         pfsync_in_eof                   /* PFSYNC_ACT_EOF */
188 };
189
190 struct pfsync_q {
191         int             (*write)(struct pf_state *, struct mbuf *, int);
192         size_t          len;
193         u_int8_t        action;
194 };
195
196 /* we have one of these for every PFSYNC_S_ */
197 int     pfsync_out_state(struct pf_state *, struct mbuf *, int);
198 int     pfsync_out_iack(struct pf_state *, struct mbuf *, int);
199 int     pfsync_out_upd_c(struct pf_state *, struct mbuf *, int);
200 int     pfsync_out_del(struct pf_state *, struct mbuf *, int);
201
202 struct pfsync_q pfsync_qs[] = {
203         { pfsync_out_state, sizeof(struct pfsync_state),   PFSYNC_ACT_INS },
204         { pfsync_out_iack,  sizeof(struct pfsync_ins_ack), PFSYNC_ACT_INS_ACK },
205         { pfsync_out_state, sizeof(struct pfsync_state),   PFSYNC_ACT_UPD },
206         { pfsync_out_upd_c, sizeof(struct pfsync_upd_c),   PFSYNC_ACT_UPD_C },
207         { pfsync_out_del,   sizeof(struct pfsync_del_c),   PFSYNC_ACT_DEL_C }
208 };
209
210 void    pfsync_q_ins(struct pf_state *, int);
211 void    pfsync_q_del(struct pf_state *);
212
213 struct pfsync_upd_req_item {
214         TAILQ_ENTRY(pfsync_upd_req_item)        ur_entry;
215         struct pfsync_upd_req                   ur_msg;
216 };
217 TAILQ_HEAD(pfsync_upd_reqs, pfsync_upd_req_item);
218
219 struct pfsync_deferral {
220         TAILQ_ENTRY(pfsync_deferral)             pd_entry;
221         struct pf_state                         *pd_st;
222         struct mbuf                             *pd_m;
223 #ifdef __FreeBSD__
224         struct callout                           pd_tmo;
225 #else
226         struct timeout                           pd_tmo;
227 #endif
228 };
229 TAILQ_HEAD(pfsync_deferrals, pfsync_deferral);
230
231 #define PFSYNC_PLSIZE   MAX(sizeof(struct pfsync_upd_req_item), \
232                             sizeof(struct pfsync_deferral))
233
234 #ifdef notyet
235 int     pfsync_out_tdb(struct tdb *, struct mbuf *, int);
236 #endif
237
238 struct pfsync_softc {
239 #ifdef __FreeBSD__
240         struct ifnet            *sc_ifp;
241 #else
242         struct ifnet             sc_if;
243 #endif
244         struct ifnet            *sc_sync_if;
245
246 #ifdef __FreeBSD__
247         uma_zone_t               sc_pool;
248 #else
249         struct pool              sc_pool;
250 #endif
251
252         struct ip_moptions       sc_imo;
253
254         struct in_addr           sc_sync_peer;
255         u_int8_t                 sc_maxupdates;
256 #ifdef __FreeBSD__
257         int                      pfsync_sync_ok;
258 #endif
259
260         struct ip                sc_template;
261
262         struct pf_state_queue    sc_qs[PFSYNC_S_COUNT];
263         size_t                   sc_len;
264
265         struct pfsync_upd_reqs   sc_upd_req_list;
266
267         int                      sc_defer;
268         struct pfsync_deferrals  sc_deferrals;
269         u_int                    sc_deferred;
270
271         void                    *sc_plus;
272         size_t                   sc_pluslen;
273
274         u_int32_t                sc_ureq_sent;
275         int                      sc_bulk_tries;
276 #ifdef __FreeBSD__
277         struct callout           sc_bulkfail_tmo;
278 #else
279         struct timeout           sc_bulkfail_tmo;
280 #endif
281
282         u_int32_t                sc_ureq_received;
283         struct pf_state         *sc_bulk_next;
284         struct pf_state         *sc_bulk_last;
285 #ifdef __FreeBSD__
286         struct callout           sc_bulk_tmo;
287 #else
288         struct timeout           sc_bulk_tmo;
289 #endif
290
291         TAILQ_HEAD(, tdb)        sc_tdb_q;
292
293 #ifdef __FreeBSD__
294         struct callout           sc_tmo;
295 #else
296         struct timeout           sc_tmo;
297 #endif
298 };
299
300 #ifdef __FreeBSD__
301 static MALLOC_DEFINE(M_PFSYNC, "pfsync", "pfsync data");
302 static VNET_DEFINE(struct pfsync_softc  *, pfsyncif) = NULL;
303 #define V_pfsyncif              VNET(pfsyncif)
304 static VNET_DEFINE(void *, pfsync_swi_cookie) = NULL;
305 #define V_pfsync_swi_cookie     VNET(pfsync_swi_cookie)
306 static VNET_DEFINE(struct pfsyncstats, pfsyncstats);
307 #define V_pfsyncstats           VNET(pfsyncstats)
308 static VNET_DEFINE(int, pfsync_carp_adj) = CARP_MAXSKEW;
309 #define V_pfsync_carp_adj       VNET(pfsync_carp_adj)
310
311 static void     pfsyncintr(void *);
312 static int      pfsync_multicast_setup(struct pfsync_softc *);
313 static void     pfsync_multicast_cleanup(struct pfsync_softc *);
314 static int      pfsync_init(void);
315 static void     pfsync_uninit(void);
316 static void     pfsync_sendout1(int);
317
318 #define schednetisr(NETISR_PFSYNC)      swi_sched(V_pfsync_swi_cookie, 0)
319
320 SYSCTL_NODE(_net, OID_AUTO, pfsync, CTLFLAG_RW, 0, "PFSYNC");
321 SYSCTL_VNET_STRUCT(_net_pfsync, OID_AUTO, stats, CTLFLAG_RW,
322     &VNET_NAME(pfsyncstats), pfsyncstats,
323     "PFSYNC statistics (struct pfsyncstats, net/if_pfsync.h)");
324 SYSCTL_INT(_net_pfsync, OID_AUTO, carp_demotion_factor, CTLFLAG_RW,
325     &VNET_NAME(pfsync_carp_adj), 0, "pfsync's CARP demotion factor adjustment");
326 #else
327 struct pfsync_softc     *pfsyncif = NULL;
328 struct pfsyncstats       pfsyncstats;
329 #define V_pfsyncstats    pfsyncstats
330 #endif
331
332 void    pfsyncattach(int);
333 #ifdef __FreeBSD__
334 int     pfsync_clone_create(struct if_clone *, int, caddr_t);
335 void    pfsync_clone_destroy(struct ifnet *);
336 #else
337 int     pfsync_clone_create(struct if_clone *, int);
338 int     pfsync_clone_destroy(struct ifnet *);
339 #endif
340 int     pfsync_alloc_scrub_memory(struct pfsync_state_peer *,
341             struct pf_state_peer *);
342 void    pfsync_update_net_tdb(struct pfsync_tdb *);
343 int     pfsyncoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
344 #ifdef __FreeBSD__
345             struct route *);
346 #else
347             struct rtentry *);
348 #endif
349 int     pfsyncioctl(struct ifnet *, u_long, caddr_t);
350 void    pfsyncstart(struct ifnet *);
351
352 struct mbuf *pfsync_if_dequeue(struct ifnet *);
353
354 void    pfsync_deferred(struct pf_state *, int);
355 void    pfsync_undefer(struct pfsync_deferral *, int);
356 void    pfsync_defer_tmo(void *);
357
358 void    pfsync_request_update(u_int32_t, u_int64_t);
359 void    pfsync_update_state_req(struct pf_state *);
360
361 void    pfsync_drop(struct pfsync_softc *);
362 void    pfsync_sendout(void);
363 void    pfsync_send_plus(void *, size_t);
364 void    pfsync_timeout(void *);
365 void    pfsync_tdb_timeout(void *);
366
367 void    pfsync_bulk_start(void);
368 void    pfsync_bulk_status(u_int8_t);
369 void    pfsync_bulk_update(void *);
370 void    pfsync_bulk_fail(void *);
371
372 #ifdef __FreeBSD__
373 /* XXX: ugly */
374 #define betoh64         (unsigned long long)be64toh
375 #define timeout_del     callout_stop
376 #endif
377
378 #define PFSYNC_MAX_BULKTRIES    12
379 #ifndef __FreeBSD__
380 int     pfsync_sync_ok;
381 #endif
382
383 #ifdef __FreeBSD__
384 VNET_DEFINE(struct ifc_simple_data, pfsync_cloner_data);
385 VNET_DEFINE(struct if_clone, pfsync_cloner);
386 #define V_pfsync_cloner_data    VNET(pfsync_cloner_data)
387 #define V_pfsync_cloner         VNET(pfsync_cloner)
388 IFC_SIMPLE_DECLARE(pfsync, 1);
389 #else
390 struct if_clone pfsync_cloner =
391     IF_CLONE_INITIALIZER("pfsync", pfsync_clone_create, pfsync_clone_destroy);
392 #endif
393
394 void
395 pfsyncattach(int npfsync)
396 {
397         if_clone_attach(&pfsync_cloner);
398 }
399 int
400 #ifdef __FreeBSD__
401 pfsync_clone_create(struct if_clone *ifc, int unit, caddr_t param)
402 #else
403 pfsync_clone_create(struct if_clone *ifc, int unit)
404 #endif
405 {
406         struct pfsync_softc *sc;
407         struct ifnet *ifp;
408         int q;
409
410         if (unit != 0)
411                 return (EINVAL);
412
413 #ifdef __FreeBSD__
414         sc = malloc(sizeof(struct pfsync_softc), M_PFSYNC, M_WAITOK | M_ZERO);
415         sc->pfsync_sync_ok = 1;
416 #else
417         pfsync_sync_ok = 1;
418         sc = malloc(sizeof(*pfsyncif), M_DEVBUF, M_NOWAIT | M_ZERO);
419 #endif
420
421         for (q = 0; q < PFSYNC_S_COUNT; q++)
422                 TAILQ_INIT(&sc->sc_qs[q]);
423
424 #ifdef __FreeBSD__
425         sc->sc_pool = uma_zcreate("pfsync", PFSYNC_PLSIZE, NULL, NULL, NULL,
426             NULL, UMA_ALIGN_PTR, 0);
427 #else
428         pool_init(&sc->sc_pool, PFSYNC_PLSIZE, 0, 0, 0, "pfsync", NULL);
429 #endif
430         TAILQ_INIT(&sc->sc_upd_req_list);
431         TAILQ_INIT(&sc->sc_deferrals);
432         sc->sc_deferred = 0;
433
434         TAILQ_INIT(&sc->sc_tdb_q);
435
436         sc->sc_len = PFSYNC_MINPKT;
437         sc->sc_maxupdates = 128;
438
439 #ifndef __FreeBSD__
440         sc->sc_imo.imo_membership = (struct in_multi **)malloc(
441             (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_IPMOPTS,
442             M_WAITOK | M_ZERO);
443         sc->sc_imo.imo_max_memberships = IP_MIN_MEMBERSHIPS;
444 #endif
445
446 #ifdef __FreeBSD__
447         ifp = sc->sc_ifp = if_alloc(IFT_PFSYNC);
448         if (ifp == NULL) {
449                 uma_zdestroy(sc->sc_pool);
450                 free(sc, M_PFSYNC);
451                 return (ENOSPC);
452         }
453         if_initname(ifp, ifc->ifc_name, unit);
454 #else
455         ifp = &sc->sc_if;
456         snprintf(ifp->if_xname, sizeof ifp->if_xname, "pfsync%d", unit);
457 #endif
458         ifp->if_softc = sc;
459         ifp->if_ioctl = pfsyncioctl;
460         ifp->if_output = pfsyncoutput;
461         ifp->if_start = pfsyncstart;
462         ifp->if_type = IFT_PFSYNC;
463         ifp->if_snd.ifq_maxlen = ifqmaxlen;
464         ifp->if_hdrlen = sizeof(struct pfsync_header);
465         ifp->if_mtu = ETHERMTU;
466 #ifdef __FreeBSD__
467         callout_init(&sc->sc_tmo, CALLOUT_MPSAFE);
468         callout_init_mtx(&sc->sc_bulk_tmo, &pf_task_mtx, 0);
469         callout_init(&sc->sc_bulkfail_tmo, CALLOUT_MPSAFE);
470 #else
471         timeout_set(&sc->sc_tmo, pfsync_timeout, sc);
472         timeout_set(&sc->sc_bulk_tmo, pfsync_bulk_update, sc);
473         timeout_set(&sc->sc_bulkfail_tmo, pfsync_bulk_fail, sc);
474 #endif
475
476         if_attach(ifp);
477 #ifndef __FreeBSD__
478         if_alloc_sadl(ifp);
479
480 #if NCARP > 0
481         if_addgroup(ifp, "carp");
482 #endif
483 #endif
484
485 #if NBPFILTER > 0
486 #ifdef __FreeBSD__
487         bpfattach(ifp, DLT_PFSYNC, PFSYNC_HDRLEN);
488 #else
489         bpfattach(&sc->sc_if.if_bpf, ifp, DLT_PFSYNC, PFSYNC_HDRLEN);
490 #endif
491 #endif
492
493 #ifdef __FreeBSD__
494         V_pfsyncif = sc;
495 #else
496         pfsyncif = sc;
497 #endif
498
499         return (0);
500 }
501
502 #ifdef __FreeBSD__
503 void
504 #else
505 int
506 #endif
507 pfsync_clone_destroy(struct ifnet *ifp)
508 {
509         struct pfsync_softc *sc = ifp->if_softc;
510
511 #ifdef __FreeBSD__
512         PF_LOCK();
513 #endif
514         timeout_del(&sc->sc_bulkfail_tmo);
515         timeout_del(&sc->sc_bulk_tmo);
516         timeout_del(&sc->sc_tmo);
517 #ifdef __FreeBSD__
518         PF_UNLOCK();
519         if (!sc->pfsync_sync_ok && carp_demote_adj_p)
520                 (*carp_demote_adj_p)(-V_pfsync_carp_adj, "pfsync destroy");
521 #else
522 #if NCARP > 0
523         if (!pfsync_sync_ok)
524                 carp_group_demote_adj(&sc->sc_if, -1);
525 #endif
526 #endif
527 #if NBPFILTER > 0
528         bpfdetach(ifp);
529 #endif
530         if_detach(ifp);
531
532         pfsync_drop(sc);
533
534         while (sc->sc_deferred > 0)
535                 pfsync_undefer(TAILQ_FIRST(&sc->sc_deferrals), 0);
536
537 #ifdef __FreeBSD__
538         UMA_DESTROY(sc->sc_pool);
539 #else
540         pool_destroy(&sc->sc_pool);
541 #endif
542 #ifdef __FreeBSD__
543         if_free(ifp);
544         if (sc->sc_imo.imo_membership)
545                 pfsync_multicast_cleanup(sc);
546         free(sc, M_PFSYNC);
547 #else
548         free(sc->sc_imo.imo_membership, M_IPMOPTS);
549         free(sc, M_DEVBUF);
550 #endif
551
552 #ifdef __FreeBSD__
553         V_pfsyncif = NULL;
554 #else
555         pfsyncif = NULL;
556 #endif
557
558 #ifndef __FreeBSD__
559         return (0);
560 #endif
561 }
562
563 struct mbuf *
564 pfsync_if_dequeue(struct ifnet *ifp)
565 {
566         struct mbuf *m;
567 #ifndef __FreeBSD__
568         int s;
569 #endif
570
571 #ifdef __FreeBSD__
572         IF_LOCK(&ifp->if_snd);
573         _IF_DROP(&ifp->if_snd);
574         _IF_DEQUEUE(&ifp->if_snd, m);
575         IF_UNLOCK(&ifp->if_snd);
576 #else
577         s = splnet();
578         IF_DEQUEUE(&ifp->if_snd, m);
579         splx(s);
580 #endif
581
582         return (m);
583 }
584
585 /*
586  * Start output on the pfsync interface.
587  */
588 void
589 pfsyncstart(struct ifnet *ifp)
590 {
591         struct mbuf *m;
592
593         while ((m = pfsync_if_dequeue(ifp)) != NULL) {
594 #ifndef __FreeBSD__
595                 IF_DROP(&ifp->if_snd);
596 #endif
597                 m_freem(m);
598         }
599 }
600
601 int
602 pfsync_alloc_scrub_memory(struct pfsync_state_peer *s,
603     struct pf_state_peer *d)
604 {
605         if (s->scrub.scrub_flag && d->scrub == NULL) {
606 #ifdef __FreeBSD__
607                 d->scrub = pool_get(&V_pf_state_scrub_pl, PR_NOWAIT | PR_ZERO);
608 #else
609                 d->scrub = pool_get(&pf_state_scrub_pl, PR_NOWAIT | PR_ZERO);
610 #endif
611                 if (d->scrub == NULL)
612                         return (ENOMEM);
613         }
614
615         return (0);
616 }
617
618 #ifndef __FreeBSD__
619 void
620 pfsync_state_export(struct pfsync_state *sp, struct pf_state *st)
621 {
622         bzero(sp, sizeof(struct pfsync_state));
623
624         /* copy from state key */
625         sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0];
626         sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1];
627         sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0];
628         sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1];
629         sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0];
630         sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1];
631         sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0];
632         sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1];
633         sp->proto = st->key[PF_SK_WIRE]->proto;
634         sp->af = st->key[PF_SK_WIRE]->af;
635
636         /* copy from state */
637         strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname));
638         bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr));
639         sp->creation = htonl(time_uptime - st->creation);
640         sp->expire = pf_state_expires(st);
641         if (sp->expire <= time_second)
642                 sp->expire = htonl(0);
643         else
644                 sp->expire = htonl(sp->expire - time_second);
645
646         sp->direction = st->direction;
647         sp->log = st->log;
648         sp->timeout = st->timeout;
649         sp->state_flags = st->state_flags;
650         if (st->src_node)
651                 sp->sync_flags |= PFSYNC_FLAG_SRCNODE;
652         if (st->nat_src_node)
653                 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE;
654
655         bcopy(&st->id, &sp->id, sizeof(sp->id));
656         sp->creatorid = st->creatorid;
657         pf_state_peer_hton(&st->src, &sp->src);
658         pf_state_peer_hton(&st->dst, &sp->dst);
659
660         if (st->rule.ptr == NULL)
661                 sp->rule = htonl(-1);
662         else
663                 sp->rule = htonl(st->rule.ptr->nr);
664         if (st->anchor.ptr == NULL)
665                 sp->anchor = htonl(-1);
666         else
667                 sp->anchor = htonl(st->anchor.ptr->nr);
668         if (st->nat_rule.ptr == NULL)
669                 sp->nat_rule = htonl(-1);
670         else
671                 sp->nat_rule = htonl(st->nat_rule.ptr->nr);
672
673         pf_state_counter_hton(st->packets[0], sp->packets[0]);
674         pf_state_counter_hton(st->packets[1], sp->packets[1]);
675         pf_state_counter_hton(st->bytes[0], sp->bytes[0]);
676         pf_state_counter_hton(st->bytes[1], sp->bytes[1]);
677
678 }
679 #endif
680
681 int
682 pfsync_state_import(struct pfsync_state *sp, u_int8_t flags)
683 {
684         struct pf_state *st = NULL;
685         struct pf_state_key *skw = NULL, *sks = NULL;
686         struct pf_rule *r = NULL;
687         struct pfi_kif  *kif;
688         int pool_flags;
689         int error;
690
691 #ifdef __FreeBSD__
692         PF_LOCK_ASSERT();
693
694         if (sp->creatorid == 0 && V_pf_status.debug >= PF_DEBUG_MISC) {
695 #else
696         if (sp->creatorid == 0 && pf_status.debug >= PF_DEBUG_MISC) {
697 #endif
698                 printf("pfsync_state_import: invalid creator id:"
699                     " %08x\n", ntohl(sp->creatorid));
700                 return (EINVAL);
701         }
702
703         if ((kif = pfi_kif_get(sp->ifname)) == NULL) {
704 #ifdef __FreeBSD__
705                 if (V_pf_status.debug >= PF_DEBUG_MISC)
706 #else
707                 if (pf_status.debug >= PF_DEBUG_MISC)
708 #endif
709                         printf("pfsync_state_import: "
710                             "unknown interface: %s\n", sp->ifname);
711                 if (flags & PFSYNC_SI_IOCTL)
712                         return (EINVAL);
713                 return (0);     /* skip this state */
714         }
715
716         /*
717          * If the ruleset checksums match or the state is coming from the ioctl,
718          * it's safe to associate the state with the rule of that number.
719          */
720         if (sp->rule != htonl(-1) && sp->anchor == htonl(-1) &&
721             (flags & (PFSYNC_SI_IOCTL | PFSYNC_SI_CKSUM)) && ntohl(sp->rule) <
722             pf_main_ruleset.rules[PF_RULESET_FILTER].active.rcount)
723                 r = pf_main_ruleset.rules[
724                     PF_RULESET_FILTER].active.ptr_array[ntohl(sp->rule)];
725         else
726 #ifdef __FreeBSD__
727                 r = &V_pf_default_rule;
728 #else
729                 r = &pf_default_rule;
730 #endif
731
732         if ((r->max_states && r->states_cur >= r->max_states))
733                 goto cleanup;
734
735 #ifdef __FreeBSD__
736         if (flags & PFSYNC_SI_IOCTL)
737                 pool_flags = PR_WAITOK | PR_ZERO;
738         else
739                 pool_flags = PR_NOWAIT | PR_ZERO;
740
741         if ((st = pool_get(&V_pf_state_pl, pool_flags)) == NULL)
742                 goto cleanup;
743 #else
744         if (flags & PFSYNC_SI_IOCTL)
745                 pool_flags = PR_WAITOK | PR_LIMITFAIL | PR_ZERO;
746         else
747                 pool_flags = PR_LIMITFAIL | PR_ZERO;
748
749         if ((st = pool_get(&pf_state_pl, pool_flags)) == NULL)
750                 goto cleanup;
751 #endif
752
753         if ((skw = pf_alloc_state_key(pool_flags)) == NULL)
754                 goto cleanup;
755
756         if (PF_ANEQ(&sp->key[PF_SK_WIRE].addr[0],
757             &sp->key[PF_SK_STACK].addr[0], sp->af) ||
758             PF_ANEQ(&sp->key[PF_SK_WIRE].addr[1],
759             &sp->key[PF_SK_STACK].addr[1], sp->af) ||
760             sp->key[PF_SK_WIRE].port[0] != sp->key[PF_SK_STACK].port[0] ||
761             sp->key[PF_SK_WIRE].port[1] != sp->key[PF_SK_STACK].port[1]) {
762                 if ((sks = pf_alloc_state_key(pool_flags)) == NULL)
763                         goto cleanup;
764         } else
765                 sks = skw;
766
767         /* allocate memory for scrub info */
768         if (pfsync_alloc_scrub_memory(&sp->src, &st->src) ||
769             pfsync_alloc_scrub_memory(&sp->dst, &st->dst))
770                 goto cleanup;
771
772         /* copy to state key(s) */
773         skw->addr[0] = sp->key[PF_SK_WIRE].addr[0];
774         skw->addr[1] = sp->key[PF_SK_WIRE].addr[1];
775         skw->port[0] = sp->key[PF_SK_WIRE].port[0];
776         skw->port[1] = sp->key[PF_SK_WIRE].port[1];
777         skw->proto = sp->proto;
778         skw->af = sp->af;
779         if (sks != skw) {
780                 sks->addr[0] = sp->key[PF_SK_STACK].addr[0];
781                 sks->addr[1] = sp->key[PF_SK_STACK].addr[1];
782                 sks->port[0] = sp->key[PF_SK_STACK].port[0];
783                 sks->port[1] = sp->key[PF_SK_STACK].port[1];
784                 sks->proto = sp->proto;
785                 sks->af = sp->af;
786         }
787
788         /* copy to state */
789         bcopy(&sp->rt_addr, &st->rt_addr, sizeof(st->rt_addr));
790         st->creation = time_uptime - ntohl(sp->creation);
791         st->expire = time_second;
792         if (sp->expire) {
793                 uint32_t timeout;
794
795                 timeout = r->timeout[sp->timeout];
796                 if (!timeout)
797                         timeout = pf_default_rule.timeout[sp->timeout];
798
799                 /* sp->expire may have been adaptively scaled by export. */
800                 st->expire -= timeout - ntohl(sp->expire);
801         }
802
803         st->direction = sp->direction;
804         st->log = sp->log;
805         st->timeout = sp->timeout;
806         st->state_flags = sp->state_flags;
807
808         bcopy(sp->id, &st->id, sizeof(st->id));
809         st->creatorid = sp->creatorid;
810         pf_state_peer_ntoh(&sp->src, &st->src);
811         pf_state_peer_ntoh(&sp->dst, &st->dst);
812
813         st->rule.ptr = r;
814         st->nat_rule.ptr = NULL;
815         st->anchor.ptr = NULL;
816         st->rt_kif = NULL;
817
818         st->pfsync_time = time_uptime;
819         st->sync_state = PFSYNC_S_NONE;
820
821         /* XXX when we have nat_rule/anchors, use STATE_INC_COUNTERS */
822         r->states_cur++;
823         r->states_tot++;
824
825         if (!ISSET(flags, PFSYNC_SI_IOCTL))
826                 SET(st->state_flags, PFSTATE_NOSYNC);
827
828         if ((error = pf_state_insert(kif, skw, sks, st)) != 0) {
829                 /* XXX when we have nat_rule/anchors, use STATE_DEC_COUNTERS */
830                 r->states_cur--;
831                 goto cleanup_state;
832         }
833
834         if (!ISSET(flags, PFSYNC_SI_IOCTL)) {
835                 CLR(st->state_flags, PFSTATE_NOSYNC);
836                 if (ISSET(st->state_flags, PFSTATE_ACK)) {
837                         pfsync_q_ins(st, PFSYNC_S_IACK);
838                         schednetisr(NETISR_PFSYNC);
839                 }
840         }
841         CLR(st->state_flags, PFSTATE_ACK);
842
843         return (0);
844
845 cleanup:
846         error = ENOMEM;
847         if (skw == sks)
848                 sks = NULL;
849 #ifdef __FreeBSD__
850         if (skw != NULL)
851                 pool_put(&V_pf_state_key_pl, skw);
852         if (sks != NULL)
853                 pool_put(&V_pf_state_key_pl, sks);
854 #else
855         if (skw != NULL)
856                 pool_put(&pf_state_key_pl, skw);
857         if (sks != NULL)
858                 pool_put(&pf_state_key_pl, sks);
859 #endif
860
861 cleanup_state:  /* pf_state_insert frees the state keys */
862         if (st) {
863 #ifdef __FreeBSD__
864                 if (st->dst.scrub)
865                         pool_put(&V_pf_state_scrub_pl, st->dst.scrub);
866                 if (st->src.scrub)
867                         pool_put(&V_pf_state_scrub_pl, st->src.scrub);
868                 pool_put(&V_pf_state_pl, st);
869 #else
870                 if (st->dst.scrub)
871                         pool_put(&pf_state_scrub_pl, st->dst.scrub);
872                 if (st->src.scrub)
873                         pool_put(&pf_state_scrub_pl, st->src.scrub);
874                 pool_put(&pf_state_pl, st);
875 #endif
876         }
877         return (error);
878 }
879
880 void
881 #ifdef __FreeBSD__
882 pfsync_input(struct mbuf *m, __unused int off)
883 #else
884 pfsync_input(struct mbuf *m, ...)
885 #endif
886 {
887 #ifdef __FreeBSD__
888         struct pfsync_softc *sc = V_pfsyncif;
889 #else
890         struct pfsync_softc *sc = pfsyncif;
891 #endif
892         struct pfsync_pkt pkt;
893         struct ip *ip = mtod(m, struct ip *);
894         struct pfsync_header *ph;
895         struct pfsync_subheader subh;
896
897         int offset;
898         int rv;
899
900         V_pfsyncstats.pfsyncs_ipackets++;
901
902         /* verify that we have a sync interface configured */
903 #ifdef __FreeBSD__
904         if (!sc || !sc->sc_sync_if || !V_pf_status.running)
905 #else
906         if (!sc || !sc->sc_sync_if || !pf_status.running)
907 #endif
908                 goto done;
909
910         /* verify that the packet came in on the right interface */
911         if (sc->sc_sync_if != m->m_pkthdr.rcvif) {
912                 V_pfsyncstats.pfsyncs_badif++;
913                 goto done;
914         }
915
916 #ifdef __FreeBSD__
917         sc->sc_ifp->if_ipackets++;
918         sc->sc_ifp->if_ibytes += m->m_pkthdr.len;
919 #else
920         sc->sc_if.if_ipackets++;
921         sc->sc_if.if_ibytes += m->m_pkthdr.len;
922 #endif
923         /* verify that the IP TTL is 255. */
924         if (ip->ip_ttl != PFSYNC_DFLTTL) {
925                 V_pfsyncstats.pfsyncs_badttl++;
926                 goto done;
927         }
928
929         offset = ip->ip_hl << 2;
930         if (m->m_pkthdr.len < offset + sizeof(*ph)) {
931                 V_pfsyncstats.pfsyncs_hdrops++;
932                 goto done;
933         }
934
935         if (offset + sizeof(*ph) > m->m_len) {
936                 if (m_pullup(m, offset + sizeof(*ph)) == NULL) {
937                         V_pfsyncstats.pfsyncs_hdrops++;
938                         return;
939                 }
940                 ip = mtod(m, struct ip *);
941         }
942         ph = (struct pfsync_header *)((char *)ip + offset);
943
944         /* verify the version */
945         if (ph->version != PFSYNC_VERSION) {
946                 V_pfsyncstats.pfsyncs_badver++;
947                 goto done;
948         }
949
950 #if 0
951         if (pfsync_input_hmac(m, offset) != 0) {
952                 /* XXX stats */
953                 goto done;
954         }
955 #endif
956
957         /* Cheaper to grab this now than having to mess with mbufs later */
958         pkt.ip = ip;
959         pkt.src = ip->ip_src;
960         pkt.flags = 0;
961
962 #ifdef __FreeBSD__
963         if (!bcmp(&ph->pfcksum, &V_pf_status.pf_chksum, PF_MD5_DIGEST_LENGTH))
964 #else
965         if (!bcmp(&ph->pfcksum, &pf_status.pf_chksum, PF_MD5_DIGEST_LENGTH))
966 #endif
967                 pkt.flags |= PFSYNC_SI_CKSUM;
968
969         offset += sizeof(*ph);
970         for (;;) {
971                 m_copydata(m, offset, sizeof(subh), (caddr_t)&subh);
972                 offset += sizeof(subh);
973
974                 if (subh.action >= PFSYNC_ACT_MAX) {
975                         V_pfsyncstats.pfsyncs_badact++;
976                         goto done;
977                 }
978
979                 rv = (*pfsync_acts[subh.action])(&pkt, m, offset,
980                     ntohs(subh.count));
981                 if (rv == -1)
982                         return;
983
984                 offset += rv;
985         }
986
987 done:
988         m_freem(m);
989 }
990
991 int
992 pfsync_in_clr(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
993 {
994         struct pfsync_clr *clr;
995         struct mbuf *mp;
996         int len = sizeof(*clr) * count;
997         int i, offp;
998
999         struct pf_state *st, *nexts;
1000         struct pf_state_key *sk, *nextsk;
1001         struct pf_state_item *si;
1002         u_int32_t creatorid;
1003         int s;
1004
1005         mp = m_pulldown(m, offset, len, &offp);
1006         if (mp == NULL) {
1007                 V_pfsyncstats.pfsyncs_badlen++;
1008                 return (-1);
1009         }
1010         clr = (struct pfsync_clr *)(mp->m_data + offp);
1011
1012         s = splsoftnet();
1013 #ifdef __FreeBSD__
1014         PF_LOCK();
1015 #endif
1016         for (i = 0; i < count; i++) {
1017                 creatorid = clr[i].creatorid;
1018
1019                 if (clr[i].ifname[0] == '\0') {
1020 #ifdef __FreeBSD__
1021                         for (st = RB_MIN(pf_state_tree_id, &V_tree_id);
1022                             st; st = nexts) {
1023                                 nexts = RB_NEXT(pf_state_tree_id, &V_tree_id, st);
1024 #else
1025                         for (st = RB_MIN(pf_state_tree_id, &tree_id);
1026                             st; st = nexts) {
1027                                 nexts = RB_NEXT(pf_state_tree_id, &tree_id, st);
1028 #endif
1029                                 if (st->creatorid == creatorid) {
1030                                         SET(st->state_flags, PFSTATE_NOSYNC);
1031                                         pf_unlink_state(st);
1032                                 }
1033                         }
1034                 } else {
1035                         if (pfi_kif_get(clr[i].ifname) == NULL)
1036                                 continue;
1037
1038                         /* XXX correct? */
1039 #ifdef __FreeBSD__
1040                         for (sk = RB_MIN(pf_state_tree, &V_pf_statetbl);
1041 #else
1042                         for (sk = RB_MIN(pf_state_tree, &pf_statetbl);
1043 #endif
1044                             sk; sk = nextsk) {
1045                                 nextsk = RB_NEXT(pf_state_tree,
1046 #ifdef __FreeBSD__
1047                                     &V_pf_statetbl, sk);
1048 #else
1049                                     &pf_statetbl, sk);
1050 #endif
1051                                 TAILQ_FOREACH(si, &sk->states, entry) {
1052                                         if (si->s->creatorid == creatorid) {
1053                                                 SET(si->s->state_flags,
1054                                                     PFSTATE_NOSYNC);
1055                                                 pf_unlink_state(si->s);
1056                                         }
1057                                 }
1058                         }
1059                 }
1060         }
1061 #ifdef __FreeBSD__
1062         PF_UNLOCK();
1063 #endif
1064         splx(s);
1065
1066         return (len);
1067 }
1068
1069 int
1070 pfsync_in_ins(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1071 {
1072         struct mbuf *mp;
1073         struct pfsync_state *sa, *sp;
1074         int len = sizeof(*sp) * count;
1075         int i, offp;
1076
1077         int s;
1078
1079         mp = m_pulldown(m, offset, len, &offp);
1080         if (mp == NULL) {
1081                 V_pfsyncstats.pfsyncs_badlen++;
1082                 return (-1);
1083         }
1084         sa = (struct pfsync_state *)(mp->m_data + offp);
1085
1086         s = splsoftnet();
1087 #ifdef __FreeBSD__
1088         PF_LOCK();
1089 #endif
1090         for (i = 0; i < count; i++) {
1091                 sp = &sa[i];
1092
1093                 /* check for invalid values */
1094                 if (sp->timeout >= PFTM_MAX ||
1095                     sp->src.state > PF_TCPS_PROXY_DST ||
1096                     sp->dst.state > PF_TCPS_PROXY_DST ||
1097                     sp->direction > PF_OUT ||
1098                     (sp->af != AF_INET && sp->af != AF_INET6)) {
1099 #ifdef __FreeBSD__
1100                         if (V_pf_status.debug >= PF_DEBUG_MISC) {
1101 #else
1102                         if (pf_status.debug >= PF_DEBUG_MISC) {
1103 #endif
1104                                 printf("pfsync_input: PFSYNC5_ACT_INS: "
1105                                     "invalid value\n");
1106                         }
1107                         V_pfsyncstats.pfsyncs_badval++;
1108                         continue;
1109                 }
1110
1111                 if (pfsync_state_import(sp, pkt->flags) == ENOMEM) {
1112                         /* drop out, but process the rest of the actions */
1113                         break;
1114                 }
1115         }
1116 #ifdef __FreeBSD__
1117         PF_UNLOCK();
1118 #endif
1119         splx(s);
1120
1121         return (len);
1122 }
1123
1124 int
1125 pfsync_in_iack(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1126 {
1127         struct pfsync_ins_ack *ia, *iaa;
1128         struct pf_state_cmp id_key;
1129         struct pf_state *st;
1130
1131         struct mbuf *mp;
1132         int len = count * sizeof(*ia);
1133         int offp, i;
1134         int s;
1135
1136         mp = m_pulldown(m, offset, len, &offp);
1137         if (mp == NULL) {
1138                 V_pfsyncstats.pfsyncs_badlen++;
1139                 return (-1);
1140         }
1141         iaa = (struct pfsync_ins_ack *)(mp->m_data + offp);
1142
1143         s = splsoftnet();
1144 #ifdef __FreeBSD__
1145         PF_LOCK();
1146 #endif
1147         for (i = 0; i < count; i++) {
1148                 ia = &iaa[i];
1149
1150                 bcopy(&ia->id, &id_key.id, sizeof(id_key.id));
1151                 id_key.creatorid = ia->creatorid;
1152
1153                 st = pf_find_state_byid(&id_key);
1154                 if (st == NULL)
1155                         continue;
1156
1157                 if (ISSET(st->state_flags, PFSTATE_ACK))
1158                         pfsync_deferred(st, 0);
1159         }
1160 #ifdef __FreeBSD__
1161         PF_UNLOCK();
1162 #endif
1163         splx(s);
1164         /*
1165          * XXX this is not yet implemented, but we know the size of the
1166          * message so we can skip it.
1167          */
1168
1169         return (count * sizeof(struct pfsync_ins_ack));
1170 }
1171
1172 int
1173 pfsync_upd_tcp(struct pf_state *st, struct pfsync_state_peer *src,
1174     struct pfsync_state_peer *dst)
1175 {
1176         int sfail = 0;
1177
1178         /*
1179          * The state should never go backwards except
1180          * for syn-proxy states.  Neither should the
1181          * sequence window slide backwards.
1182          */
1183         if (st->src.state > src->state &&
1184             (st->src.state < PF_TCPS_PROXY_SRC ||
1185             src->state >= PF_TCPS_PROXY_SRC))
1186                 sfail = 1;
1187         else if (SEQ_GT(st->src.seqlo, ntohl(src->seqlo)))
1188                 sfail = 3;
1189         else if (st->dst.state > dst->state) {
1190                 /* There might still be useful
1191                  * information about the src state here,
1192                  * so import that part of the update,
1193                  * then "fail" so we send the updated
1194                  * state back to the peer who is missing
1195                  * our what we know. */
1196                 pf_state_peer_ntoh(src, &st->src);
1197                 /* XXX do anything with timeouts? */
1198                 sfail = 7;
1199         } else if (st->dst.state >= TCPS_SYN_SENT &&
1200             SEQ_GT(st->dst.seqlo, ntohl(dst->seqlo)))
1201                 sfail = 4;
1202
1203         return (sfail);
1204 }
1205
1206 int
1207 pfsync_in_upd(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1208 {
1209         struct pfsync_state *sa, *sp;
1210         struct pf_state_cmp id_key;
1211         struct pf_state_key *sk;
1212         struct pf_state *st;
1213         int sfail;
1214
1215         struct mbuf *mp;
1216         int len = count * sizeof(*sp);
1217         int offp, i;
1218         int s;
1219
1220         mp = m_pulldown(m, offset, len, &offp);
1221         if (mp == NULL) {
1222                 V_pfsyncstats.pfsyncs_badlen++;
1223                 return (-1);
1224         }
1225         sa = (struct pfsync_state *)(mp->m_data + offp);
1226
1227         s = splsoftnet();
1228 #ifdef __FreeBSD__
1229         PF_LOCK();
1230 #endif
1231         for (i = 0; i < count; i++) {
1232                 sp = &sa[i];
1233
1234                 /* check for invalid values */
1235                 if (sp->timeout >= PFTM_MAX ||
1236                     sp->src.state > PF_TCPS_PROXY_DST ||
1237                     sp->dst.state > PF_TCPS_PROXY_DST) {
1238 #ifdef __FreeBSD__
1239                         if (V_pf_status.debug >= PF_DEBUG_MISC) {
1240 #else
1241                         if (pf_status.debug >= PF_DEBUG_MISC) {
1242 #endif
1243                                 printf("pfsync_input: PFSYNC_ACT_UPD: "
1244                                     "invalid value\n");
1245                         }
1246                         V_pfsyncstats.pfsyncs_badval++;
1247                         continue;
1248                 }
1249
1250                 bcopy(sp->id, &id_key.id, sizeof(id_key.id));
1251                 id_key.creatorid = sp->creatorid;
1252
1253                 st = pf_find_state_byid(&id_key);
1254                 if (st == NULL) {
1255                         /* insert the update */
1256                         if (pfsync_state_import(sp, 0))
1257                                 V_pfsyncstats.pfsyncs_badstate++;
1258                         continue;
1259                 }
1260
1261                 if (ISSET(st->state_flags, PFSTATE_ACK))
1262                         pfsync_deferred(st, 1);
1263
1264                 sk = st->key[PF_SK_WIRE];       /* XXX right one? */
1265                 sfail = 0;
1266                 if (sk->proto == IPPROTO_TCP)
1267                         sfail = pfsync_upd_tcp(st, &sp->src, &sp->dst);
1268                 else {
1269                         /*
1270                          * Non-TCP protocol state machine always go
1271                          * forwards
1272                          */
1273                         if (st->src.state > sp->src.state)
1274                                 sfail = 5;
1275                         else if (st->dst.state > sp->dst.state)
1276                                 sfail = 6;
1277                 }
1278
1279                 if (sfail) {
1280 #ifdef __FreeBSD__
1281                         if (V_pf_status.debug >= PF_DEBUG_MISC) {
1282 #else
1283                         if (pf_status.debug >= PF_DEBUG_MISC) {
1284 #endif
1285                                 printf("pfsync: %s stale update (%d)"
1286                                     " id: %016llx creatorid: %08x\n",
1287                                     (sfail < 7 ?  "ignoring" : "partial"),
1288                                     sfail, betoh64(st->id),
1289                                     ntohl(st->creatorid));
1290                         }
1291                         V_pfsyncstats.pfsyncs_stale++;
1292
1293                         pfsync_update_state(st);
1294                         schednetisr(NETISR_PFSYNC);
1295                         continue;
1296                 }
1297                 pfsync_alloc_scrub_memory(&sp->dst, &st->dst);
1298                 pf_state_peer_ntoh(&sp->src, &st->src);
1299                 pf_state_peer_ntoh(&sp->dst, &st->dst);
1300                 st->expire = time_second;
1301                 st->timeout = sp->timeout;
1302                 st->pfsync_time = time_uptime;
1303         }
1304 #ifdef __FreeBSD__
1305         PF_UNLOCK();
1306 #endif
1307         splx(s);
1308
1309         return (len);
1310 }
1311
1312 int
1313 pfsync_in_upd_c(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1314 {
1315         struct pfsync_upd_c *ua, *up;
1316         struct pf_state_key *sk;
1317         struct pf_state_cmp id_key;
1318         struct pf_state *st;
1319
1320         int len = count * sizeof(*up);
1321         int sfail;
1322
1323         struct mbuf *mp;
1324         int offp, i;
1325         int s;
1326
1327         mp = m_pulldown(m, offset, len, &offp);
1328         if (mp == NULL) {
1329                 V_pfsyncstats.pfsyncs_badlen++;
1330                 return (-1);
1331         }
1332         ua = (struct pfsync_upd_c *)(mp->m_data + offp);
1333
1334         s = splsoftnet();
1335 #ifdef __FreeBSD__
1336         PF_LOCK();
1337 #endif
1338         for (i = 0; i < count; i++) {
1339                 up = &ua[i];
1340
1341                 /* check for invalid values */
1342                 if (up->timeout >= PFTM_MAX ||
1343                     up->src.state > PF_TCPS_PROXY_DST ||
1344                     up->dst.state > PF_TCPS_PROXY_DST) {
1345 #ifdef __FreeBSD__
1346                         if (V_pf_status.debug >= PF_DEBUG_MISC) {
1347 #else
1348                         if (pf_status.debug >= PF_DEBUG_MISC) {
1349 #endif
1350                                 printf("pfsync_input: "
1351                                     "PFSYNC_ACT_UPD_C: "
1352                                     "invalid value\n");
1353                         }
1354                         V_pfsyncstats.pfsyncs_badval++;
1355                         continue;
1356                 }
1357
1358                 bcopy(&up->id, &id_key.id, sizeof(id_key.id));
1359                 id_key.creatorid = up->creatorid;
1360
1361                 st = pf_find_state_byid(&id_key);
1362                 if (st == NULL) {
1363                         /* We don't have this state. Ask for it. */
1364                         pfsync_request_update(id_key.creatorid, id_key.id);
1365                         continue;
1366                 }
1367
1368                 if (ISSET(st->state_flags, PFSTATE_ACK))
1369                         pfsync_deferred(st, 1);
1370
1371                 sk = st->key[PF_SK_WIRE]; /* XXX right one? */
1372                 sfail = 0;
1373                 if (sk->proto == IPPROTO_TCP)
1374                         sfail = pfsync_upd_tcp(st, &up->src, &up->dst);
1375                 else {
1376                         /*
1377                          * Non-TCP protocol state machine always go forwards
1378                          */
1379                         if (st->src.state > up->src.state)
1380                                 sfail = 5;
1381                         else if (st->dst.state > up->dst.state)
1382                                 sfail = 6;
1383                 }
1384
1385                 if (sfail) {
1386 #ifdef __FreeBSD__
1387                         if (V_pf_status.debug >= PF_DEBUG_MISC) {
1388 #else
1389                         if (pf_status.debug >= PF_DEBUG_MISC) {
1390 #endif
1391                                 printf("pfsync: ignoring stale update "
1392                                     "(%d) id: %016llx "
1393                                     "creatorid: %08x\n", sfail,
1394                                     betoh64(st->id),
1395                                     ntohl(st->creatorid));
1396                         }
1397                         V_pfsyncstats.pfsyncs_stale++;
1398
1399                         pfsync_update_state(st);
1400                         schednetisr(NETISR_PFSYNC);
1401                         continue;
1402                 }
1403                 pfsync_alloc_scrub_memory(&up->dst, &st->dst);
1404                 pf_state_peer_ntoh(&up->src, &st->src);
1405                 pf_state_peer_ntoh(&up->dst, &st->dst);
1406                 st->expire = time_second;
1407                 st->timeout = up->timeout;
1408                 st->pfsync_time = time_uptime;
1409         }
1410 #ifdef __FreeBSD__
1411         PF_UNLOCK();
1412 #endif
1413         splx(s);
1414
1415         return (len);
1416 }
1417
1418 int
1419 pfsync_in_ureq(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1420 {
1421         struct pfsync_upd_req *ur, *ura;
1422         struct mbuf *mp;
1423         int len = count * sizeof(*ur);
1424         int i, offp;
1425
1426         struct pf_state_cmp id_key;
1427         struct pf_state *st;
1428
1429         mp = m_pulldown(m, offset, len, &offp);
1430         if (mp == NULL) {
1431                 V_pfsyncstats.pfsyncs_badlen++;
1432                 return (-1);
1433         }
1434         ura = (struct pfsync_upd_req *)(mp->m_data + offp);
1435
1436 #ifdef __FreeBSD__
1437         PF_LOCK();
1438 #endif
1439         for (i = 0; i < count; i++) {
1440                 ur = &ura[i];
1441
1442                 bcopy(&ur->id, &id_key.id, sizeof(id_key.id));
1443                 id_key.creatorid = ur->creatorid;
1444
1445                 if (id_key.id == 0 && id_key.creatorid == 0)
1446                         pfsync_bulk_start();
1447                 else {
1448                         st = pf_find_state_byid(&id_key);
1449                         if (st == NULL) {
1450                                 V_pfsyncstats.pfsyncs_badstate++;
1451                                 continue;
1452                         }
1453                         if (ISSET(st->state_flags, PFSTATE_NOSYNC))
1454                                 continue;
1455
1456                         pfsync_update_state_req(st);
1457                 }
1458         }
1459 #ifdef __FreeBSD__
1460         PF_UNLOCK();
1461 #endif
1462
1463         return (len);
1464 }
1465
1466 int
1467 pfsync_in_del(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1468 {
1469         struct mbuf *mp;
1470         struct pfsync_state *sa, *sp;
1471         struct pf_state_cmp id_key;
1472         struct pf_state *st;
1473         int len = count * sizeof(*sp);
1474         int offp, i;
1475         int s;
1476
1477         mp = m_pulldown(m, offset, len, &offp);
1478         if (mp == NULL) {
1479                 V_pfsyncstats.pfsyncs_badlen++;
1480                 return (-1);
1481         }
1482         sa = (struct pfsync_state *)(mp->m_data + offp);
1483
1484         s = splsoftnet();
1485 #ifdef __FreeBSD__
1486         PF_LOCK();
1487 #endif
1488         for (i = 0; i < count; i++) {
1489                 sp = &sa[i];
1490
1491                 bcopy(sp->id, &id_key.id, sizeof(id_key.id));
1492                 id_key.creatorid = sp->creatorid;
1493
1494                 st = pf_find_state_byid(&id_key);
1495                 if (st == NULL) {
1496                         V_pfsyncstats.pfsyncs_badstate++;
1497                         continue;
1498                 }
1499                 SET(st->state_flags, PFSTATE_NOSYNC);
1500                 pf_unlink_state(st);
1501         }
1502 #ifdef __FreeBSD__
1503         PF_UNLOCK();
1504 #endif
1505         splx(s);
1506
1507         return (len);
1508 }
1509
1510 int
1511 pfsync_in_del_c(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1512 {
1513         struct mbuf *mp;
1514         struct pfsync_del_c *sa, *sp;
1515         struct pf_state_cmp id_key;
1516         struct pf_state *st;
1517         int len = count * sizeof(*sp);
1518         int offp, i;
1519         int s;
1520
1521         mp = m_pulldown(m, offset, len, &offp);
1522         if (mp == NULL) {
1523                 V_pfsyncstats.pfsyncs_badlen++;
1524                 return (-1);
1525         }
1526         sa = (struct pfsync_del_c *)(mp->m_data + offp);
1527
1528         s = splsoftnet();
1529 #ifdef __FreeBSD__
1530         PF_LOCK();
1531 #endif
1532         for (i = 0; i < count; i++) {
1533                 sp = &sa[i];
1534
1535                 bcopy(&sp->id, &id_key.id, sizeof(id_key.id));
1536                 id_key.creatorid = sp->creatorid;
1537
1538                 st = pf_find_state_byid(&id_key);
1539                 if (st == NULL) {
1540                         V_pfsyncstats.pfsyncs_badstate++;
1541                         continue;
1542                 }
1543
1544                 SET(st->state_flags, PFSTATE_NOSYNC);
1545                 pf_unlink_state(st);
1546         }
1547 #ifdef __FreeBSD__
1548         PF_UNLOCK();
1549 #endif
1550         splx(s);
1551
1552         return (len);
1553 }
1554
1555 int
1556 pfsync_in_bus(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1557 {
1558 #ifdef __FreeBSD__
1559         struct pfsync_softc *sc = V_pfsyncif;
1560 #else
1561         struct pfsync_softc *sc = pfsyncif;
1562 #endif
1563         struct pfsync_bus *bus;
1564         struct mbuf *mp;
1565         int len = count * sizeof(*bus);
1566         int offp;
1567
1568         /* If we're not waiting for a bulk update, who cares. */
1569         if (sc->sc_ureq_sent == 0)
1570                 return (len);
1571
1572         mp = m_pulldown(m, offset, len, &offp);
1573         if (mp == NULL) {
1574                 V_pfsyncstats.pfsyncs_badlen++;
1575                 return (-1);
1576         }
1577         bus = (struct pfsync_bus *)(mp->m_data + offp);
1578
1579         switch (bus->status) {
1580         case PFSYNC_BUS_START:
1581 #ifdef __FreeBSD__
1582                 callout_reset(&sc->sc_bulkfail_tmo, 4 * hz +
1583                     V_pf_pool_limits[PF_LIMIT_STATES].limit /
1584                     ((sc->sc_ifp->if_mtu - PFSYNC_MINPKT) /
1585                     sizeof(struct pfsync_state)),
1586                     pfsync_bulk_fail, V_pfsyncif);
1587 #else
1588                 timeout_add(&sc->sc_bulkfail_tmo, 4 * hz +
1589                     pf_pool_limits[PF_LIMIT_STATES].limit /
1590                     ((sc->sc_if.if_mtu - PFSYNC_MINPKT) /
1591                     sizeof(struct pfsync_state)));
1592 #endif
1593 #ifdef __FreeBSD__
1594                 if (V_pf_status.debug >= PF_DEBUG_MISC)
1595 #else
1596                 if (pf_status.debug >= PF_DEBUG_MISC)
1597 #endif
1598                         printf("pfsync: received bulk update start\n");
1599                 break;
1600
1601         case PFSYNC_BUS_END:
1602                 if (time_uptime - ntohl(bus->endtime) >=
1603                     sc->sc_ureq_sent) {
1604                         /* that's it, we're happy */
1605                         sc->sc_ureq_sent = 0;
1606                         sc->sc_bulk_tries = 0;
1607                         timeout_del(&sc->sc_bulkfail_tmo);
1608 #ifdef __FreeBSD__
1609                         if (!sc->pfsync_sync_ok && carp_demote_adj_p)
1610                                 (*carp_demote_adj_p)(-V_pfsync_carp_adj,
1611                                     "pfsync bulk done");
1612                         sc->pfsync_sync_ok = 1;
1613 #else
1614 #if NCARP > 0
1615                         if (!pfsync_sync_ok)
1616                                 carp_group_demote_adj(&sc->sc_if, -1);
1617 #endif
1618                         pfsync_sync_ok = 1;
1619 #endif
1620 #ifdef __FreeBSD__
1621                         if (V_pf_status.debug >= PF_DEBUG_MISC)
1622 #else
1623                         if (pf_status.debug >= PF_DEBUG_MISC)
1624 #endif
1625                                 printf("pfsync: received valid "
1626                                     "bulk update end\n");
1627                 } else {
1628 #ifdef __FreeBSD__
1629                         if (V_pf_status.debug >= PF_DEBUG_MISC)
1630 #else
1631                         if (pf_status.debug >= PF_DEBUG_MISC)
1632 #endif
1633                                 printf("pfsync: received invalid "
1634                                     "bulk update end: bad timestamp\n");
1635                 }
1636                 break;
1637         }
1638
1639         return (len);
1640 }
1641
1642 int
1643 pfsync_in_tdb(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1644 {
1645         int len = count * sizeof(struct pfsync_tdb);
1646
1647 #if defined(IPSEC)
1648         struct pfsync_tdb *tp;
1649         struct mbuf *mp;
1650         int offp;
1651         int i;
1652         int s;
1653
1654         mp = m_pulldown(m, offset, len, &offp);
1655         if (mp == NULL) {
1656                 V_pfsyncstats.pfsyncs_badlen++;
1657                 return (-1);
1658         }
1659         tp = (struct pfsync_tdb *)(mp->m_data + offp);
1660
1661         s = splsoftnet();
1662 #ifdef __FreeBSD__
1663         PF_LOCK();
1664 #endif
1665         for (i = 0; i < count; i++)
1666                 pfsync_update_net_tdb(&tp[i]);
1667 #ifdef __FreeBSD__
1668         PF_UNLOCK();
1669 #endif
1670         splx(s);
1671 #endif
1672
1673         return (len);
1674 }
1675
1676 #if defined(IPSEC)
1677 /* Update an in-kernel tdb. Silently fail if no tdb is found. */
1678 void
1679 pfsync_update_net_tdb(struct pfsync_tdb *pt)
1680 {
1681         struct tdb              *tdb;
1682         int                      s;
1683
1684         /* check for invalid values */
1685         if (ntohl(pt->spi) <= SPI_RESERVED_MAX ||
1686             (pt->dst.sa.sa_family != AF_INET &&
1687              pt->dst.sa.sa_family != AF_INET6))
1688                 goto bad;
1689
1690         s = spltdb();
1691         tdb = gettdb(pt->spi, &pt->dst, pt->sproto);
1692         if (tdb) {
1693                 pt->rpl = ntohl(pt->rpl);
1694                 pt->cur_bytes = betoh64(pt->cur_bytes);
1695
1696                 /* Neither replay nor byte counter should ever decrease. */
1697                 if (pt->rpl < tdb->tdb_rpl ||
1698                     pt->cur_bytes < tdb->tdb_cur_bytes) {
1699                         splx(s);
1700                         goto bad;
1701                 }
1702
1703                 tdb->tdb_rpl = pt->rpl;
1704                 tdb->tdb_cur_bytes = pt->cur_bytes;
1705         }
1706         splx(s);
1707         return;
1708
1709 bad:
1710 #ifdef __FreeBSD__
1711         if (V_pf_status.debug >= PF_DEBUG_MISC)
1712 #else
1713         if (pf_status.debug >= PF_DEBUG_MISC)
1714 #endif
1715                 printf("pfsync_insert: PFSYNC_ACT_TDB_UPD: "
1716                     "invalid value\n");
1717         V_pfsyncstats.pfsyncs_badstate++;
1718         return;
1719 }
1720 #endif
1721
1722
1723 int
1724 pfsync_in_eof(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1725 {
1726         /* check if we are at the right place in the packet */
1727         if (offset != m->m_pkthdr.len - sizeof(struct pfsync_eof))
1728                 V_pfsyncstats.pfsyncs_badact++;
1729
1730         /* we're done. free and let the caller return */
1731         m_freem(m);
1732         return (-1);
1733 }
1734
1735 int
1736 pfsync_in_error(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1737 {
1738         V_pfsyncstats.pfsyncs_badact++;
1739
1740         m_freem(m);
1741         return (-1);
1742 }
1743
1744 int
1745 pfsyncoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
1746 #ifdef __FreeBSD__
1747         struct route *rt)
1748 #else
1749         struct rtentry *rt)
1750 #endif
1751 {
1752         m_freem(m);
1753         return (0);
1754 }
1755
1756 /* ARGSUSED */
1757 int
1758 pfsyncioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1759 {
1760 #ifndef __FreeBSD__
1761         struct proc *p = curproc;
1762 #endif
1763         struct pfsync_softc *sc = ifp->if_softc;
1764         struct ifreq *ifr = (struct ifreq *)data;
1765         struct ip_moptions *imo = &sc->sc_imo;
1766         struct pfsyncreq pfsyncr;
1767         struct ifnet    *sifp;
1768         struct ip *ip;
1769         int s, error;
1770
1771         switch (cmd) {
1772 #if 0
1773         case SIOCSIFADDR:
1774         case SIOCAIFADDR:
1775         case SIOCSIFDSTADDR:
1776 #endif
1777         case SIOCSIFFLAGS:
1778 #ifdef __FreeBSD__
1779                 if (ifp->if_flags & IFF_UP)
1780                         ifp->if_drv_flags |= IFF_DRV_RUNNING;
1781                 else
1782                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1783 #else
1784                 if (ifp->if_flags & IFF_UP)
1785                         ifp->if_flags |= IFF_RUNNING;
1786                 else
1787                         ifp->if_flags &= ~IFF_RUNNING;
1788 #endif
1789                 break;
1790         case SIOCSIFMTU:
1791                 if (!sc->sc_sync_if ||
1792                     ifr->ifr_mtu <= PFSYNC_MINPKT ||
1793                     ifr->ifr_mtu > sc->sc_sync_if->if_mtu)
1794                         return (EINVAL);
1795                 if (ifr->ifr_mtu < ifp->if_mtu) {
1796                         s = splnet();
1797 #ifdef __FreeBSD__
1798                         PF_LOCK();
1799 #endif
1800                         pfsync_sendout();
1801 #ifdef __FreeBSD__
1802                         PF_UNLOCK();
1803 #endif
1804                         splx(s);
1805                 }
1806                 ifp->if_mtu = ifr->ifr_mtu;
1807                 break;
1808         case SIOCGETPFSYNC:
1809                 bzero(&pfsyncr, sizeof(pfsyncr));
1810                 if (sc->sc_sync_if) {
1811                         strlcpy(pfsyncr.pfsyncr_syncdev,
1812                             sc->sc_sync_if->if_xname, IFNAMSIZ);
1813                 }
1814                 pfsyncr.pfsyncr_syncpeer = sc->sc_sync_peer;
1815                 pfsyncr.pfsyncr_maxupdates = sc->sc_maxupdates;
1816                 pfsyncr.pfsyncr_defer = sc->sc_defer;
1817                 return (copyout(&pfsyncr, ifr->ifr_data, sizeof(pfsyncr)));
1818
1819         case SIOCSETPFSYNC:
1820 #ifdef __FreeBSD__
1821                 if ((error = priv_check(curthread, PRIV_NETINET_PF)) != 0)
1822 #else
1823                 if ((error = suser(p, p->p_acflag)) != 0)
1824 #endif
1825                         return (error);
1826                 if ((error = copyin(ifr->ifr_data, &pfsyncr, sizeof(pfsyncr))))
1827                         return (error);
1828
1829 #ifdef __FreeBSD__
1830                 PF_LOCK();
1831 #endif
1832                 if (pfsyncr.pfsyncr_syncpeer.s_addr == 0)
1833 #ifdef __FreeBSD__
1834                         sc->sc_sync_peer.s_addr = htonl(INADDR_PFSYNC_GROUP);
1835 #else
1836                         sc->sc_sync_peer.s_addr = INADDR_PFSYNC_GROUP;
1837 #endif
1838                 else
1839                         sc->sc_sync_peer.s_addr =
1840                             pfsyncr.pfsyncr_syncpeer.s_addr;
1841
1842                 if (pfsyncr.pfsyncr_maxupdates > 255)
1843 #ifdef __FreeBSD__
1844                 {
1845                         PF_UNLOCK();
1846 #endif
1847                         return (EINVAL);
1848 #ifdef __FreeBSD__
1849                 }
1850 #endif
1851                 sc->sc_maxupdates = pfsyncr.pfsyncr_maxupdates;
1852                 sc->sc_defer = pfsyncr.pfsyncr_defer;
1853
1854                 if (pfsyncr.pfsyncr_syncdev[0] == 0) {
1855                         sc->sc_sync_if = NULL;
1856 #ifdef __FreeBSD__
1857                         PF_UNLOCK();
1858                         if (imo->imo_membership)
1859                                 pfsync_multicast_cleanup(sc);
1860 #else
1861                         if (imo->imo_num_memberships > 0) {
1862                                 in_delmulti(imo->imo_membership[
1863                                     --imo->imo_num_memberships]);
1864                                 imo->imo_multicast_ifp = NULL;
1865                         }
1866 #endif
1867                         break;
1868                 }
1869
1870 #ifdef __FreeBSD__
1871                 PF_UNLOCK();
1872 #endif
1873                 if ((sifp = ifunit(pfsyncr.pfsyncr_syncdev)) == NULL)
1874                         return (EINVAL);
1875
1876 #ifdef __FreeBSD__
1877                 PF_LOCK();
1878 #endif
1879                 s = splnet();
1880 #ifdef __FreeBSD__
1881                 if (sifp->if_mtu < sc->sc_ifp->if_mtu ||
1882 #else
1883                 if (sifp->if_mtu < sc->sc_if.if_mtu ||
1884 #endif
1885                     (sc->sc_sync_if != NULL &&
1886                     sifp->if_mtu < sc->sc_sync_if->if_mtu) ||
1887                     sifp->if_mtu < MCLBYTES - sizeof(struct ip))
1888                         pfsync_sendout();
1889                 sc->sc_sync_if = sifp;
1890
1891 #ifdef __FreeBSD__
1892                 if (imo->imo_membership) {
1893                         PF_UNLOCK();
1894                         pfsync_multicast_cleanup(sc);
1895                         PF_LOCK();
1896                 }
1897 #else
1898                 if (imo->imo_num_memberships > 0) {
1899                         in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1900                         imo->imo_multicast_ifp = NULL;
1901                 }
1902 #endif
1903
1904 #ifdef __FreeBSD__
1905                 if (sc->sc_sync_if &&
1906                     sc->sc_sync_peer.s_addr == htonl(INADDR_PFSYNC_GROUP)) {
1907                         PF_UNLOCK();
1908                         error = pfsync_multicast_setup(sc);
1909                         if (error)
1910                                 return (error);
1911                         PF_LOCK();
1912                 }
1913 #else
1914                 if (sc->sc_sync_if &&
1915                     sc->sc_sync_peer.s_addr == INADDR_PFSYNC_GROUP) {
1916                         struct in_addr addr;
1917
1918                         if (!(sc->sc_sync_if->if_flags & IFF_MULTICAST)) {
1919                                 sc->sc_sync_if = NULL;
1920                                 splx(s);
1921                                 return (EADDRNOTAVAIL);
1922                         }
1923
1924                         addr.s_addr = INADDR_PFSYNC_GROUP;
1925
1926                         if ((imo->imo_membership[0] =
1927                             in_addmulti(&addr, sc->sc_sync_if)) == NULL) {
1928                                 sc->sc_sync_if = NULL;
1929                                 splx(s);
1930                                 return (ENOBUFS);
1931                         }
1932                         imo->imo_num_memberships++;
1933                         imo->imo_multicast_ifp = sc->sc_sync_if;
1934                         imo->imo_multicast_ttl = PFSYNC_DFLTTL;
1935                         imo->imo_multicast_loop = 0;
1936                 }
1937 #endif  /* !__FreeBSD__ */
1938
1939                 ip = &sc->sc_template;
1940                 bzero(ip, sizeof(*ip));
1941                 ip->ip_v = IPVERSION;
1942                 ip->ip_hl = sizeof(sc->sc_template) >> 2;
1943                 ip->ip_tos = IPTOS_LOWDELAY;
1944                 /* len and id are set later */
1945 #ifdef __FreeBSD__
1946                 ip->ip_off = IP_DF;
1947 #else
1948                 ip->ip_off = htons(IP_DF);
1949 #endif
1950                 ip->ip_ttl = PFSYNC_DFLTTL;
1951                 ip->ip_p = IPPROTO_PFSYNC;
1952                 ip->ip_src.s_addr = INADDR_ANY;
1953                 ip->ip_dst.s_addr = sc->sc_sync_peer.s_addr;
1954
1955                 if (sc->sc_sync_if) {
1956                         /* Request a full state table update. */
1957                         sc->sc_ureq_sent = time_uptime;
1958 #ifdef __FreeBSD__
1959                         if (sc->pfsync_sync_ok && carp_demote_adj_p)
1960                                 (*carp_demote_adj_p)(V_pfsync_carp_adj,
1961                                     "pfsync bulk start");
1962                         sc->pfsync_sync_ok = 0;
1963 #else
1964 #if NCARP > 0
1965                         if (pfsync_sync_ok)
1966                                 carp_group_demote_adj(&sc->sc_if, 1);
1967 #endif
1968                         pfsync_sync_ok = 0;
1969 #endif
1970 #ifdef __FreeBSD__
1971                         if (V_pf_status.debug >= PF_DEBUG_MISC)
1972 #else
1973                         if (pf_status.debug >= PF_DEBUG_MISC)
1974 #endif
1975                                 printf("pfsync: requesting bulk update\n");
1976 #ifdef __FreeBSD__
1977                         callout_reset(&sc->sc_bulkfail_tmo, 5 * hz,
1978                             pfsync_bulk_fail, V_pfsyncif);
1979 #else
1980                         timeout_add_sec(&sc->sc_bulkfail_tmo, 5);
1981 #endif
1982                         pfsync_request_update(0, 0);
1983                 }
1984 #ifdef __FreeBSD__
1985                 PF_UNLOCK();
1986 #endif
1987                 splx(s);
1988
1989                 break;
1990
1991         default:
1992                 return (ENOTTY);
1993         }
1994
1995         return (0);
1996 }
1997
1998 int
1999 pfsync_out_state(struct pf_state *st, struct mbuf *m, int offset)
2000 {
2001         struct pfsync_state *sp = (struct pfsync_state *)(m->m_data + offset);
2002
2003         pfsync_state_export(sp, st);
2004
2005         return (sizeof(*sp));
2006 }
2007
2008 int
2009 pfsync_out_iack(struct pf_state *st, struct mbuf *m, int offset)
2010 {
2011         struct pfsync_ins_ack *iack =
2012             (struct pfsync_ins_ack *)(m->m_data + offset);
2013
2014         iack->id = st->id;
2015         iack->creatorid = st->creatorid;
2016
2017         return (sizeof(*iack));
2018 }
2019
2020 int
2021 pfsync_out_upd_c(struct pf_state *st, struct mbuf *m, int offset)
2022 {
2023         struct pfsync_upd_c *up = (struct pfsync_upd_c *)(m->m_data + offset);
2024
2025         bzero(up, sizeof(*up));
2026         up->id = st->id;
2027         pf_state_peer_hton(&st->src, &up->src);
2028         pf_state_peer_hton(&st->dst, &up->dst);
2029         up->creatorid = st->creatorid;
2030         up->timeout = st->timeout;
2031
2032         return (sizeof(*up));
2033 }
2034
2035 int
2036 pfsync_out_del(struct pf_state *st, struct mbuf *m, int offset)
2037 {
2038         struct pfsync_del_c *dp = (struct pfsync_del_c *)(m->m_data + offset);
2039
2040         dp->id = st->id;
2041         dp->creatorid = st->creatorid;
2042
2043         SET(st->state_flags, PFSTATE_NOSYNC);
2044
2045         return (sizeof(*dp));
2046 }
2047
2048 void
2049 pfsync_drop(struct pfsync_softc *sc)
2050 {
2051         struct pf_state *st;
2052         struct pfsync_upd_req_item *ur;
2053 #ifdef notyet
2054         struct tdb *t;
2055 #endif
2056         int q;
2057
2058         for (q = 0; q < PFSYNC_S_COUNT; q++) {
2059                 if (TAILQ_EMPTY(&sc->sc_qs[q]))
2060                         continue;
2061
2062                 TAILQ_FOREACH(st, &sc->sc_qs[q], sync_list) {
2063 #ifdef PFSYNC_DEBUG
2064 #ifdef __FreeBSD__
2065                         KASSERT(st->sync_state == q,
2066                                 ("%s: st->sync_state == q",
2067                                         __FUNCTION__));
2068 #else
2069                         KASSERT(st->sync_state == q);
2070 #endif
2071 #endif
2072                         st->sync_state = PFSYNC_S_NONE;
2073                 }
2074                 TAILQ_INIT(&sc->sc_qs[q]);
2075         }
2076
2077         while ((ur = TAILQ_FIRST(&sc->sc_upd_req_list)) != NULL) {
2078                 TAILQ_REMOVE(&sc->sc_upd_req_list, ur, ur_entry);
2079                 pool_put(&sc->sc_pool, ur);
2080         }
2081
2082         sc->sc_plus = NULL;
2083
2084 #ifdef notyet
2085         if (!TAILQ_EMPTY(&sc->sc_tdb_q)) {
2086                 TAILQ_FOREACH(t, &sc->sc_tdb_q, tdb_sync_entry)
2087                         CLR(t->tdb_flags, TDBF_PFSYNC);
2088
2089                 TAILQ_INIT(&sc->sc_tdb_q);
2090         }
2091 #endif
2092
2093         sc->sc_len = PFSYNC_MINPKT;
2094 }
2095
2096 #ifdef __FreeBSD__
2097 void pfsync_sendout()
2098 {
2099         pfsync_sendout1(1);
2100 }
2101
2102 static void
2103 pfsync_sendout1(int schedswi)
2104 {
2105         struct pfsync_softc *sc = V_pfsyncif;
2106 #else
2107 void
2108 pfsync_sendout(void)
2109 {
2110         struct pfsync_softc *sc = pfsyncif;
2111 #endif
2112 #if NBPFILTER > 0
2113 #ifdef __FreeBSD__
2114         struct ifnet *ifp = sc->sc_ifp;
2115 #else
2116         struct ifnet *ifp = &sc->sc_if;
2117 #endif
2118 #endif
2119         struct mbuf *m;
2120         struct ip *ip;
2121         struct pfsync_header *ph;
2122         struct pfsync_subheader *subh;
2123         struct pf_state *st;
2124         struct pfsync_upd_req_item *ur;
2125 #ifdef notyet
2126         struct tdb *t;
2127 #endif
2128         int offset;
2129         int q, count = 0;
2130
2131 #ifdef __FreeBSD__
2132         PF_LOCK_ASSERT();
2133 #else
2134         splassert(IPL_NET);
2135 #endif
2136
2137         if (sc == NULL || sc->sc_len == PFSYNC_MINPKT)
2138                 return;
2139
2140 #if NBPFILTER > 0
2141         if (ifp->if_bpf == NULL && sc->sc_sync_if == NULL) {
2142 #else
2143         if (sc->sc_sync_if == NULL) {
2144 #endif
2145                 pfsync_drop(sc);
2146                 return;
2147         }
2148
2149 #ifdef __FreeBSD__
2150         m = m_get2(M_NOWAIT, MT_DATA, M_PKTHDR, max_linkhdr + sc->sc_len);
2151         if (m == NULL) {
2152                 sc->sc_ifp->if_oerrors++;
2153                 V_pfsyncstats.pfsyncs_onomem++;
2154                 return;
2155         }
2156 #else
2157         MGETHDR(m, M_DONTWAIT, MT_DATA);
2158         if (m == NULL) {
2159                 sc->sc_if.if_oerrors++;
2160                 pfsyncstats.pfsyncs_onomem++;
2161                 pfsync_drop(sc);
2162                 return;
2163         }
2164
2165         if (max_linkhdr + sc->sc_len > MHLEN) {
2166                 MCLGETI(m, M_DONTWAIT, NULL, max_linkhdr + sc->sc_len);
2167                 if (!ISSET(m->m_flags, M_EXT)) {
2168                         m_free(m);
2169                         sc->sc_if.if_oerrors++;
2170                         pfsyncstats.pfsyncs_onomem++;
2171                         pfsync_drop(sc);
2172                         return;
2173                 }
2174         }
2175 #endif
2176         m->m_data += max_linkhdr;
2177         m->m_len = m->m_pkthdr.len = sc->sc_len;
2178
2179         /* build the ip header */
2180         ip = (struct ip *)m->m_data;
2181         bcopy(&sc->sc_template, ip, sizeof(*ip));
2182         offset = sizeof(*ip);
2183
2184 #ifdef __FreeBSD__
2185         ip->ip_len = m->m_pkthdr.len;
2186 #else
2187         ip->ip_len = htons(m->m_pkthdr.len);
2188 #endif
2189         ip->ip_id = htons(ip_randomid());
2190
2191         /* build the pfsync header */
2192         ph = (struct pfsync_header *)(m->m_data + offset);
2193         bzero(ph, sizeof(*ph));
2194         offset += sizeof(*ph);
2195
2196         ph->version = PFSYNC_VERSION;
2197         ph->len = htons(sc->sc_len - sizeof(*ip));
2198 #ifdef __FreeBSD__
2199         bcopy(V_pf_status.pf_chksum, ph->pfcksum, PF_MD5_DIGEST_LENGTH);
2200 #else
2201         bcopy(pf_status.pf_chksum, ph->pfcksum, PF_MD5_DIGEST_LENGTH);
2202 #endif
2203
2204         /* walk the queues */
2205         for (q = 0; q < PFSYNC_S_COUNT; q++) {
2206                 if (TAILQ_EMPTY(&sc->sc_qs[q]))
2207                         continue;
2208
2209                 subh = (struct pfsync_subheader *)(m->m_data + offset);
2210                 offset += sizeof(*subh);
2211
2212                 count = 0;
2213                 TAILQ_FOREACH(st, &sc->sc_qs[q], sync_list) {
2214 #ifdef PFSYNC_DEBUG
2215 #ifdef __FreeBSD__
2216                         KASSERT(st->sync_state == q,
2217                                 ("%s: st->sync_state == q",
2218                                         __FUNCTION__));
2219 #else
2220                         KASSERT(st->sync_state == q);
2221 #endif
2222 #endif
2223
2224                         offset += pfsync_qs[q].write(st, m, offset);
2225                         st->sync_state = PFSYNC_S_NONE;
2226                         count++;
2227                 }
2228                 TAILQ_INIT(&sc->sc_qs[q]);
2229
2230                 bzero(subh, sizeof(*subh));
2231                 subh->action = pfsync_qs[q].action;
2232                 subh->count = htons(count);
2233         }
2234
2235         if (!TAILQ_EMPTY(&sc->sc_upd_req_list)) {
2236                 subh = (struct pfsync_subheader *)(m->m_data + offset);
2237                 offset += sizeof(*subh);
2238
2239                 count = 0;
2240                 while ((ur = TAILQ_FIRST(&sc->sc_upd_req_list)) != NULL) {
2241                         TAILQ_REMOVE(&sc->sc_upd_req_list, ur, ur_entry);
2242
2243                         bcopy(&ur->ur_msg, m->m_data + offset,
2244                             sizeof(ur->ur_msg));
2245                         offset += sizeof(ur->ur_msg);
2246
2247                         pool_put(&sc->sc_pool, ur);
2248
2249                         count++;
2250                 }
2251
2252                 bzero(subh, sizeof(*subh));
2253                 subh->action = PFSYNC_ACT_UPD_REQ;
2254                 subh->count = htons(count);
2255         }
2256
2257         /* has someone built a custom region for us to add? */
2258         if (sc->sc_plus != NULL) {
2259                 bcopy(sc->sc_plus, m->m_data + offset, sc->sc_pluslen);
2260                 offset += sc->sc_pluslen;
2261
2262                 sc->sc_plus = NULL;
2263         }
2264
2265 #ifdef notyet
2266         if (!TAILQ_EMPTY(&sc->sc_tdb_q)) {
2267                 subh = (struct pfsync_subheader *)(m->m_data + offset);
2268                 offset += sizeof(*subh);
2269
2270                 count = 0;
2271                 TAILQ_FOREACH(t, &sc->sc_tdb_q, tdb_sync_entry) {
2272                         offset += pfsync_out_tdb(t, m, offset);
2273                         CLR(t->tdb_flags, TDBF_PFSYNC);
2274
2275                         count++;
2276                 }
2277                 TAILQ_INIT(&sc->sc_tdb_q);
2278
2279                 bzero(subh, sizeof(*subh));
2280                 subh->action = PFSYNC_ACT_TDB;
2281                 subh->count = htons(count);
2282         }
2283 #endif
2284
2285         subh = (struct pfsync_subheader *)(m->m_data + offset);
2286         offset += sizeof(*subh);
2287
2288         bzero(subh, sizeof(*subh));
2289         subh->action = PFSYNC_ACT_EOF;
2290         subh->count = htons(1);
2291
2292         /* XXX write checksum in EOF here */
2293
2294         /* we're done, let's put it on the wire */
2295 #if NBPFILTER > 0
2296         if (ifp->if_bpf) {
2297                 m->m_data += sizeof(*ip);
2298                 m->m_len = m->m_pkthdr.len = sc->sc_len - sizeof(*ip);
2299 #ifdef __FreeBSD__
2300                 BPF_MTAP(ifp, m);
2301 #else
2302                 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
2303 #endif
2304                 m->m_data -= sizeof(*ip);
2305                 m->m_len = m->m_pkthdr.len = sc->sc_len;
2306         }
2307
2308         if (sc->sc_sync_if == NULL) {
2309                 sc->sc_len = PFSYNC_MINPKT;
2310                 m_freem(m);
2311                 return;
2312         }
2313 #endif
2314
2315 #ifdef __FreeBSD__
2316         sc->sc_ifp->if_opackets++;
2317         sc->sc_ifp->if_obytes += m->m_pkthdr.len;
2318         sc->sc_len = PFSYNC_MINPKT;
2319
2320         if (!_IF_QFULL(&sc->sc_ifp->if_snd))
2321                 _IF_ENQUEUE(&sc->sc_ifp->if_snd, m);
2322         else {
2323                 m_freem(m);
2324                 sc->sc_ifp->if_snd.ifq_drops++;
2325         }
2326         if (schedswi)
2327                 swi_sched(V_pfsync_swi_cookie, 0);
2328 #else
2329         sc->sc_if.if_opackets++;
2330         sc->sc_if.if_obytes += m->m_pkthdr.len;
2331
2332         if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL) == 0)
2333                 pfsyncstats.pfsyncs_opackets++;
2334         else
2335                 pfsyncstats.pfsyncs_oerrors++;
2336
2337         /* start again */
2338         sc->sc_len = PFSYNC_MINPKT;
2339 #endif
2340 }
2341
2342 void
2343 pfsync_insert_state(struct pf_state *st)
2344 {
2345 #ifdef __FreeBSD__
2346         struct pfsync_softc *sc = V_pfsyncif;
2347 #else
2348         struct pfsync_softc *sc = pfsyncif;
2349 #endif
2350
2351 #ifdef __FreeBSD__
2352         PF_LOCK_ASSERT();
2353 #else
2354         splassert(IPL_SOFTNET);
2355 #endif
2356
2357         if (ISSET(st->rule.ptr->rule_flag, PFRULE_NOSYNC) ||
2358             st->key[PF_SK_WIRE]->proto == IPPROTO_PFSYNC) {
2359                 SET(st->state_flags, PFSTATE_NOSYNC);
2360                 return;
2361         }
2362
2363         if (sc == NULL || ISSET(st->state_flags, PFSTATE_NOSYNC))
2364                 return;
2365
2366 #ifdef PFSYNC_DEBUG
2367 #ifdef __FreeBSD__
2368         KASSERT(st->sync_state == PFSYNC_S_NONE,
2369                 ("%s: st->sync_state == PFSYNC_S_NONE", __FUNCTION__));
2370 #else
2371         KASSERT(st->sync_state == PFSYNC_S_NONE);
2372 #endif
2373 #endif
2374
2375         if (sc->sc_len == PFSYNC_MINPKT)
2376 #ifdef __FreeBSD__
2377                 callout_reset(&sc->sc_tmo, 1 * hz, pfsync_timeout,
2378                     V_pfsyncif);
2379 #else
2380                 timeout_add_sec(&sc->sc_tmo, 1);
2381 #endif
2382
2383         pfsync_q_ins(st, PFSYNC_S_INS);
2384
2385         st->sync_updates = 0;
2386 }
2387
2388 int defer = 10;
2389
2390 int
2391 pfsync_defer(struct pf_state *st, struct mbuf *m)
2392 {
2393 #ifdef __FreeBSD__
2394         struct pfsync_softc *sc = V_pfsyncif;
2395 #else
2396         struct pfsync_softc *sc = pfsyncif;
2397 #endif
2398         struct pfsync_deferral *pd;
2399
2400 #ifdef __FreeBSD__
2401         PF_LOCK_ASSERT();
2402 #else
2403         splassert(IPL_SOFTNET);
2404 #endif
2405
2406         if (!sc->sc_defer || m->m_flags & (M_BCAST|M_MCAST))
2407                 return (0);
2408
2409         if (sc->sc_deferred >= 128)
2410                 pfsync_undefer(TAILQ_FIRST(&sc->sc_deferrals), 0);
2411
2412         pd = pool_get(&sc->sc_pool, M_NOWAIT);
2413         if (pd == NULL)
2414                 return (0);
2415         sc->sc_deferred++;
2416
2417 #ifdef __FreeBSD__
2418         m->m_flags |= M_SKIP_FIREWALL;
2419 #else
2420         m->m_pkthdr.pf.flags |= PF_TAG_GENERATED;
2421 #endif
2422         SET(st->state_flags, PFSTATE_ACK);
2423
2424         pd->pd_st = st;
2425         pd->pd_m = m;
2426
2427         TAILQ_INSERT_TAIL(&sc->sc_deferrals, pd, pd_entry);
2428 #ifdef __FreeBSD__
2429         callout_init(&pd->pd_tmo, CALLOUT_MPSAFE);
2430         callout_reset(&pd->pd_tmo, defer, pfsync_defer_tmo,
2431                 pd);
2432 #else
2433         timeout_set(&pd->pd_tmo, pfsync_defer_tmo, pd);
2434         timeout_add(&pd->pd_tmo, defer);
2435 #endif
2436
2437         swi_sched(V_pfsync_swi_cookie, 0);
2438
2439         return (1);
2440 }
2441
2442 void
2443 pfsync_undefer(struct pfsync_deferral *pd, int drop)
2444 {
2445 #ifdef __FreeBSD__
2446         struct pfsync_softc *sc = V_pfsyncif;
2447 #else
2448         struct pfsync_softc *sc = pfsyncif;
2449 #endif
2450         int s;
2451
2452 #ifdef __FreeBSD__
2453         PF_LOCK_ASSERT();
2454 #else
2455         splassert(IPL_SOFTNET);
2456 #endif
2457
2458         TAILQ_REMOVE(&sc->sc_deferrals, pd, pd_entry);
2459         sc->sc_deferred--;
2460
2461         CLR(pd->pd_st->state_flags, PFSTATE_ACK);
2462         timeout_del(&pd->pd_tmo); /* bah */
2463         if (drop)
2464                 m_freem(pd->pd_m);
2465         else {
2466                 s = splnet();
2467 #ifdef __FreeBSD__
2468                 /* XXX: use pf_defered?! */
2469                 PF_UNLOCK();
2470 #endif
2471                 ip_output(pd->pd_m, (void *)NULL, (void *)NULL, 0,
2472                     (void *)NULL, (void *)NULL);
2473 #ifdef __FreeBSD__
2474                 PF_LOCK();
2475 #endif
2476                 splx(s);
2477         }
2478
2479         pool_put(&sc->sc_pool, pd);
2480 }
2481
2482 void
2483 pfsync_defer_tmo(void *arg)
2484 {
2485 #if defined(__FreeBSD__) && defined(VIMAGE)
2486         struct pfsync_deferral *pd = arg;
2487 #endif
2488         int s;
2489
2490         s = splsoftnet();
2491 #ifdef __FreeBSD__
2492         CURVNET_SET(pd->pd_m->m_pkthdr.rcvif->if_vnet); /* XXX */
2493         PF_LOCK();
2494 #endif
2495         pfsync_undefer(arg, 0);
2496 #ifdef __FreeBSD__
2497         PF_UNLOCK();
2498         CURVNET_RESTORE();
2499 #endif
2500         splx(s);
2501 }
2502
2503 void
2504 pfsync_deferred(struct pf_state *st, int drop)
2505 {
2506 #ifdef __FreeBSD__
2507         struct pfsync_softc *sc = V_pfsyncif;
2508 #else
2509         struct pfsync_softc *sc = pfsyncif;
2510 #endif
2511         struct pfsync_deferral *pd;
2512
2513         TAILQ_FOREACH(pd, &sc->sc_deferrals, pd_entry) {
2514                  if (pd->pd_st == st) {
2515                         pfsync_undefer(pd, drop);
2516                         return;
2517                 }
2518         }
2519
2520         panic("pfsync_send_deferred: unable to find deferred state");
2521 }
2522
2523 u_int pfsync_upds = 0;
2524
2525 void
2526 pfsync_update_state(struct pf_state *st)
2527 {
2528 #ifdef __FreeBSD__
2529         struct pfsync_softc *sc = V_pfsyncif;
2530 #else
2531         struct pfsync_softc *sc = pfsyncif;
2532 #endif
2533         int sync = 0;
2534
2535 #ifdef __FreeBSD__
2536         PF_LOCK_ASSERT();
2537 #else
2538         splassert(IPL_SOFTNET);
2539 #endif
2540
2541         if (sc == NULL)
2542                 return;
2543
2544         if (ISSET(st->state_flags, PFSTATE_ACK))
2545                 pfsync_deferred(st, 0);
2546         if (ISSET(st->state_flags, PFSTATE_NOSYNC)) {
2547                 if (st->sync_state != PFSYNC_S_NONE)
2548                         pfsync_q_del(st);
2549                 return;
2550         }
2551
2552         if (sc->sc_len == PFSYNC_MINPKT)
2553 #ifdef __FreeBSD__
2554                 callout_reset(&sc->sc_tmo, 1 * hz, pfsync_timeout,
2555                     V_pfsyncif);
2556 #else
2557                 timeout_add_sec(&sc->sc_tmo, 1);
2558 #endif
2559
2560         switch (st->sync_state) {
2561         case PFSYNC_S_UPD_C:
2562         case PFSYNC_S_UPD:
2563         case PFSYNC_S_INS:
2564                 /* we're already handling it */
2565
2566                 if (st->key[PF_SK_WIRE]->proto == IPPROTO_TCP) {
2567                         st->sync_updates++;
2568                         if (st->sync_updates >= sc->sc_maxupdates)
2569                                 sync = 1;
2570                 }
2571                 break;
2572
2573         case PFSYNC_S_IACK:
2574                 pfsync_q_del(st);
2575         case PFSYNC_S_NONE:
2576                 pfsync_q_ins(st, PFSYNC_S_UPD_C);
2577                 st->sync_updates = 0;
2578                 break;
2579
2580         default:
2581                 panic("pfsync_update_state: unexpected sync state %d",
2582                     st->sync_state);
2583         }
2584
2585         if (sync || (time_uptime - st->pfsync_time) < 2) {
2586                 pfsync_upds++;
2587                 schednetisr(NETISR_PFSYNC);
2588         }
2589 }
2590
2591 void
2592 pfsync_request_update(u_int32_t creatorid, u_int64_t id)
2593 {
2594 #ifdef __FreeBSD__
2595         struct pfsync_softc *sc = V_pfsyncif;
2596 #else
2597         struct pfsync_softc *sc = pfsyncif;
2598 #endif
2599         struct pfsync_upd_req_item *item;
2600         size_t nlen = sizeof(struct pfsync_upd_req);
2601         int s;
2602
2603         PF_LOCK_ASSERT();
2604
2605         /*
2606          * this code does nothing to prevent multiple update requests for the
2607          * same state being generated.
2608          */
2609
2610         item = pool_get(&sc->sc_pool, PR_NOWAIT);
2611         if (item == NULL) {
2612                 /* XXX stats */
2613                 return;
2614         }
2615
2616         item->ur_msg.id = id;
2617         item->ur_msg.creatorid = creatorid;
2618
2619         if (TAILQ_EMPTY(&sc->sc_upd_req_list))
2620                 nlen += sizeof(struct pfsync_subheader);
2621
2622 #ifdef __FreeBSD__
2623         if (sc->sc_len + nlen > sc->sc_ifp->if_mtu) {
2624 #else
2625         if (sc->sc_len + nlen > sc->sc_if.if_mtu) {
2626 #endif
2627                 s = splnet();
2628                 pfsync_sendout();
2629                 splx(s);
2630
2631                 nlen = sizeof(struct pfsync_subheader) +
2632                     sizeof(struct pfsync_upd_req);
2633         }
2634
2635         TAILQ_INSERT_TAIL(&sc->sc_upd_req_list, item, ur_entry);
2636         sc->sc_len += nlen;
2637
2638         schednetisr(NETISR_PFSYNC);
2639 }
2640
2641 void
2642 pfsync_update_state_req(struct pf_state *st)
2643 {
2644 #ifdef __FreeBSD__
2645         struct pfsync_softc *sc = V_pfsyncif;
2646 #else
2647         struct pfsync_softc *sc = pfsyncif;
2648 #endif
2649
2650         PF_LOCK_ASSERT();
2651
2652         if (sc == NULL)
2653                 panic("pfsync_update_state_req: nonexistant instance");
2654
2655         if (ISSET(st->state_flags, PFSTATE_NOSYNC)) {
2656                 if (st->sync_state != PFSYNC_S_NONE)
2657                         pfsync_q_del(st);
2658                 return;
2659         }
2660
2661         switch (st->sync_state) {
2662         case PFSYNC_S_UPD_C:
2663         case PFSYNC_S_IACK:
2664                 pfsync_q_del(st);
2665         case PFSYNC_S_NONE:
2666                 pfsync_q_ins(st, PFSYNC_S_UPD);
2667                 schednetisr(NETISR_PFSYNC);
2668                 return;
2669
2670         case PFSYNC_S_INS:
2671         case PFSYNC_S_UPD:
2672         case PFSYNC_S_DEL:
2673                 /* we're already handling it */
2674                 return;
2675
2676         default:
2677                 panic("pfsync_update_state_req: unexpected sync state %d",
2678                     st->sync_state);
2679         }
2680 }
2681
2682 void
2683 pfsync_delete_state(struct pf_state *st)
2684 {
2685 #ifdef __FreeBSD__
2686         struct pfsync_softc *sc = V_pfsyncif;
2687 #else
2688         struct pfsync_softc *sc = pfsyncif;
2689 #endif
2690
2691 #ifdef __FreeBSD__
2692         PF_LOCK_ASSERT();
2693 #else
2694         splassert(IPL_SOFTNET);
2695 #endif
2696
2697         if (sc == NULL)
2698                 return;
2699
2700         if (ISSET(st->state_flags, PFSTATE_ACK))
2701                 pfsync_deferred(st, 1);
2702         if (ISSET(st->state_flags, PFSTATE_NOSYNC)) {
2703                 if (st->sync_state != PFSYNC_S_NONE)
2704                         pfsync_q_del(st);
2705                 return;
2706         }
2707
2708         if (sc->sc_len == PFSYNC_MINPKT)
2709 #ifdef __FreeBSD__
2710                 callout_reset(&sc->sc_tmo, 1 * hz, pfsync_timeout,
2711                     V_pfsyncif);
2712 #else
2713                 timeout_add_sec(&sc->sc_tmo, 1);
2714 #endif
2715
2716         switch (st->sync_state) {
2717         case PFSYNC_S_INS:
2718                 /* we never got to tell the world so just forget about it */
2719                 pfsync_q_del(st);
2720                 return;
2721
2722         case PFSYNC_S_UPD_C:
2723         case PFSYNC_S_UPD:
2724         case PFSYNC_S_IACK:
2725                 pfsync_q_del(st);
2726                 /* FALLTHROUGH to putting it on the del list */
2727
2728         case PFSYNC_S_NONE:
2729                 pfsync_q_ins(st, PFSYNC_S_DEL);
2730                 return;
2731
2732         default:
2733                 panic("pfsync_delete_state: unexpected sync state %d",
2734                     st->sync_state);
2735         }
2736 }
2737
2738 void
2739 pfsync_clear_states(u_int32_t creatorid, const char *ifname)
2740 {
2741         struct {
2742                 struct pfsync_subheader subh;
2743                 struct pfsync_clr clr;
2744         } __packed r;
2745
2746 #ifdef __FreeBSD__
2747         struct pfsync_softc *sc = V_pfsyncif;
2748 #else
2749         struct pfsync_softc *sc = pfsyncif;
2750 #endif
2751
2752 #ifdef __FreeBSD__
2753         PF_LOCK_ASSERT();
2754 #else
2755         splassert(IPL_SOFTNET);
2756 #endif
2757
2758         if (sc == NULL)
2759                 return;
2760
2761         bzero(&r, sizeof(r));
2762
2763         r.subh.action = PFSYNC_ACT_CLR;
2764         r.subh.count = htons(1);
2765
2766         strlcpy(r.clr.ifname, ifname, sizeof(r.clr.ifname));
2767         r.clr.creatorid = creatorid;
2768
2769         pfsync_send_plus(&r, sizeof(r));
2770 }
2771
2772 void
2773 pfsync_q_ins(struct pf_state *st, int q)
2774 {
2775 #ifdef __FreeBSD__
2776         struct pfsync_softc *sc = V_pfsyncif;
2777 #else
2778         struct pfsync_softc *sc = pfsyncif;
2779 #endif
2780         size_t nlen = pfsync_qs[q].len;
2781         int s;
2782
2783         PF_LOCK_ASSERT();
2784
2785 #ifdef __FreeBSD__
2786         KASSERT(st->sync_state == PFSYNC_S_NONE,
2787                 ("%s: st->sync_state == PFSYNC_S_NONE", __FUNCTION__));
2788 #else
2789         KASSERT(st->sync_state == PFSYNC_S_NONE);
2790 #endif
2791
2792 #if 1 || defined(PFSYNC_DEBUG)
2793         if (sc->sc_len < PFSYNC_MINPKT)
2794 #ifdef __FreeBSD__
2795                 panic("pfsync pkt len is too low %zu", sc->sc_len);
2796 #else
2797                 panic("pfsync pkt len is too low %d", sc->sc_len);
2798 #endif
2799 #endif
2800         if (TAILQ_EMPTY(&sc->sc_qs[q]))
2801                 nlen += sizeof(struct pfsync_subheader);
2802
2803 #ifdef __FreeBSD__
2804         if (sc->sc_len + nlen > sc->sc_ifp->if_mtu) {
2805 #else
2806         if (sc->sc_len + nlen > sc->sc_if.if_mtu) {
2807 #endif
2808                 s = splnet();
2809                 pfsync_sendout();
2810                 splx(s);
2811
2812                 nlen = sizeof(struct pfsync_subheader) + pfsync_qs[q].len;
2813         }
2814
2815         sc->sc_len += nlen;
2816         TAILQ_INSERT_TAIL(&sc->sc_qs[q], st, sync_list);
2817         st->sync_state = q;
2818 }
2819
2820 void
2821 pfsync_q_del(struct pf_state *st)
2822 {
2823 #ifdef __FreeBSD__
2824         struct pfsync_softc *sc = V_pfsyncif;
2825 #else
2826         struct pfsync_softc *sc = pfsyncif;
2827 #endif
2828         int q = st->sync_state;
2829
2830 #ifdef __FreeBSD__
2831         KASSERT(st->sync_state != PFSYNC_S_NONE,
2832                 ("%s: st->sync_state != PFSYNC_S_NONE", __FUNCTION__));
2833 #else
2834         KASSERT(st->sync_state != PFSYNC_S_NONE);
2835 #endif
2836
2837         sc->sc_len -= pfsync_qs[q].len;
2838         TAILQ_REMOVE(&sc->sc_qs[q], st, sync_list);
2839         st->sync_state = PFSYNC_S_NONE;
2840
2841         if (TAILQ_EMPTY(&sc->sc_qs[q]))
2842                 sc->sc_len -= sizeof(struct pfsync_subheader);
2843 }
2844
2845 #ifdef notyet
2846 void
2847 pfsync_update_tdb(struct tdb *t, int output)
2848 {
2849 #ifdef __FreeBSD__
2850         struct pfsync_softc *sc = V_pfsyncif;
2851 #else
2852         struct pfsync_softc *sc = pfsyncif;
2853 #endif
2854         size_t nlen = sizeof(struct pfsync_tdb);
2855         int s;
2856
2857         if (sc == NULL)
2858                 return;
2859
2860         if (!ISSET(t->tdb_flags, TDBF_PFSYNC)) {
2861                 if (TAILQ_EMPTY(&sc->sc_tdb_q))
2862                         nlen += sizeof(struct pfsync_subheader);
2863
2864                 if (sc->sc_len + nlen > sc->sc_if.if_mtu) {
2865                         s = splnet();
2866                         PF_LOCK();
2867                         pfsync_sendout();
2868                         PF_UNLOCK();
2869                         splx(s);
2870
2871                         nlen = sizeof(struct pfsync_subheader) +
2872                             sizeof(struct pfsync_tdb);
2873                 }
2874
2875                 sc->sc_len += nlen;
2876                 TAILQ_INSERT_TAIL(&sc->sc_tdb_q, t, tdb_sync_entry);
2877                 SET(t->tdb_flags, TDBF_PFSYNC);
2878                 t->tdb_updates = 0;
2879         } else {
2880                 if (++t->tdb_updates >= sc->sc_maxupdates)
2881                         schednetisr(NETISR_PFSYNC);
2882         }
2883
2884         if (output)
2885                 SET(t->tdb_flags, TDBF_PFSYNC_RPL);
2886         else
2887                 CLR(t->tdb_flags, TDBF_PFSYNC_RPL);
2888 }
2889
2890 void
2891 pfsync_delete_tdb(struct tdb *t)
2892 {
2893 #ifdef __FreeBSD__
2894         struct pfsync_softc *sc = V_pfsyncif;
2895 #else
2896         struct pfsync_softc *sc = pfsyncif;
2897 #endif
2898
2899         if (sc == NULL || !ISSET(t->tdb_flags, TDBF_PFSYNC))
2900                 return;
2901
2902         sc->sc_len -= sizeof(struct pfsync_tdb);
2903         TAILQ_REMOVE(&sc->sc_tdb_q, t, tdb_sync_entry);
2904         CLR(t->tdb_flags, TDBF_PFSYNC);
2905
2906         if (TAILQ_EMPTY(&sc->sc_tdb_q))
2907                 sc->sc_len -= sizeof(struct pfsync_subheader);
2908 }
2909
2910 int
2911 pfsync_out_tdb(struct tdb *t, struct mbuf *m, int offset)
2912 {
2913         struct pfsync_tdb *ut = (struct pfsync_tdb *)(m->m_data + offset);
2914
2915         bzero(ut, sizeof(*ut));
2916         ut->spi = t->tdb_spi;
2917         bcopy(&t->tdb_dst, &ut->dst, sizeof(ut->dst));
2918         /*
2919          * When a failover happens, the master's rpl is probably above
2920          * what we see here (we may be up to a second late), so
2921          * increase it a bit for outbound tdbs to manage most such
2922          * situations.
2923          *
2924          * For now, just add an offset that is likely to be larger
2925          * than the number of packets we can see in one second. The RFC
2926          * just says the next packet must have a higher seq value.
2927          *
2928          * XXX What is a good algorithm for this? We could use
2929          * a rate-determined increase, but to know it, we would have
2930          * to extend struct tdb.
2931          * XXX pt->rpl can wrap over MAXINT, but if so the real tdb
2932          * will soon be replaced anyway. For now, just don't handle
2933          * this edge case.
2934          */
2935 #define RPL_INCR 16384
2936         ut->rpl = htonl(t->tdb_rpl + (ISSET(t->tdb_flags, TDBF_PFSYNC_RPL) ?
2937             RPL_INCR : 0));
2938         ut->cur_bytes = htobe64(t->tdb_cur_bytes);
2939         ut->sproto = t->tdb_sproto;
2940
2941         return (sizeof(*ut));
2942 }
2943 #endif
2944
2945 void
2946 pfsync_bulk_start(void)
2947 {
2948 #ifdef __FreeBSD__
2949         struct pfsync_softc *sc = V_pfsyncif;
2950 #else
2951         struct pfsync_softc *sc = pfsyncif;
2952 #endif
2953
2954 #ifdef __FreeBSD__
2955         if (V_pf_status.debug >= PF_DEBUG_MISC)
2956 #else
2957         if (pf_status.debug >= PF_DEBUG_MISC)
2958 #endif
2959                 printf("pfsync: received bulk update request\n");
2960
2961 #ifdef __FreeBSD__
2962         PF_LOCK_ASSERT();
2963         if (TAILQ_EMPTY(&V_state_list))
2964 #else
2965         if (TAILQ_EMPTY(&state_list))
2966 #endif
2967                 pfsync_bulk_status(PFSYNC_BUS_END);
2968         else {
2969                 sc->sc_ureq_received = time_uptime;
2970                 if (sc->sc_bulk_next == NULL)
2971 #ifdef __FreeBSD__
2972                         sc->sc_bulk_next = TAILQ_FIRST(&V_state_list);
2973 #else
2974                         sc->sc_bulk_next = TAILQ_FIRST(&state_list);
2975 #endif
2976                 sc->sc_bulk_last = sc->sc_bulk_next;
2977
2978                 pfsync_bulk_status(PFSYNC_BUS_START);
2979                 callout_reset(&sc->sc_bulk_tmo, 1, pfsync_bulk_update, sc);
2980         }
2981 }
2982
2983 void
2984 pfsync_bulk_update(void *arg)
2985 {
2986         struct pfsync_softc *sc = arg;
2987         struct pf_state *st = sc->sc_bulk_next;
2988         int i = 0;
2989         int s;
2990
2991         PF_LOCK_ASSERT();
2992
2993         s = splsoftnet();
2994 #ifdef __FreeBSD__
2995         CURVNET_SET(sc->sc_ifp->if_vnet);
2996 #endif
2997         for (;;) {
2998                 if (st->sync_state == PFSYNC_S_NONE &&
2999                     st->timeout < PFTM_MAX &&
3000                     st->pfsync_time <= sc->sc_ureq_received) {
3001                         pfsync_update_state_req(st);
3002                         i++;
3003                 }
3004
3005                 st = TAILQ_NEXT(st, entry_list);
3006                 if (st == NULL)
3007 #ifdef __FreeBSD__
3008                         st = TAILQ_FIRST(&V_state_list);
3009 #else
3010                         st = TAILQ_FIRST(&state_list);
3011 #endif
3012
3013                 if (st == sc->sc_bulk_last) {
3014                         /* we're done */
3015                         sc->sc_bulk_next = NULL;
3016                         sc->sc_bulk_last = NULL;
3017                         pfsync_bulk_status(PFSYNC_BUS_END);
3018                         break;
3019                 }
3020
3021 #ifdef __FreeBSD__
3022                 if (i > 1 && (sc->sc_ifp->if_mtu - sc->sc_len) <
3023 #else
3024                 if (i > 1 && (sc->sc_if.if_mtu - sc->sc_len) <
3025 #endif
3026                     sizeof(struct pfsync_state)) {
3027                         /* we've filled a packet */
3028                         sc->sc_bulk_next = st;
3029 #ifdef __FreeBSD__
3030                         callout_reset(&sc->sc_bulk_tmo, 1,
3031                             pfsync_bulk_update, sc);
3032 #else
3033                         timeout_add(&sc->sc_bulk_tmo, 1);
3034 #endif
3035                         break;
3036                 }
3037         }
3038
3039 #ifdef __FreeBSD__
3040         CURVNET_RESTORE();
3041 #endif
3042         splx(s);
3043 }
3044
3045 void
3046 pfsync_bulk_status(u_int8_t status)
3047 {
3048         struct {
3049                 struct pfsync_subheader subh;
3050                 struct pfsync_bus bus;
3051         } __packed r;
3052
3053 #ifdef __FreeBSD__
3054         struct pfsync_softc *sc = V_pfsyncif;
3055 #else
3056         struct pfsync_softc *sc = pfsyncif;
3057 #endif
3058
3059         PF_LOCK_ASSERT();
3060
3061         bzero(&r, sizeof(r));
3062
3063         r.subh.action = PFSYNC_ACT_BUS;
3064         r.subh.count = htons(1);
3065
3066 #ifdef __FreeBSD__
3067         r.bus.creatorid = V_pf_status.hostid;
3068 #else
3069         r.bus.creatorid = pf_status.hostid;
3070 #endif
3071         r.bus.endtime = htonl(time_uptime - sc->sc_ureq_received);
3072         r.bus.status = status;
3073
3074         pfsync_send_plus(&r, sizeof(r));
3075 }
3076
3077 void
3078 pfsync_bulk_fail(void *arg)
3079 {
3080         struct pfsync_softc *sc = arg;
3081
3082 #ifdef __FreeBSD__
3083         CURVNET_SET(sc->sc_ifp->if_vnet);
3084 #endif
3085
3086         if (sc->sc_bulk_tries++ < PFSYNC_MAX_BULKTRIES) {
3087                 /* Try again */
3088 #ifdef __FreeBSD__
3089                 callout_reset(&sc->sc_bulkfail_tmo, 5 * hz,
3090                     pfsync_bulk_fail, V_pfsyncif);
3091 #else
3092                 timeout_add_sec(&sc->sc_bulkfail_tmo, 5);
3093 #endif
3094                 PF_LOCK();
3095                 pfsync_request_update(0, 0);
3096                 PF_UNLOCK();
3097         } else {
3098                 /* Pretend like the transfer was ok */
3099                 sc->sc_ureq_sent = 0;
3100                 sc->sc_bulk_tries = 0;
3101 #ifdef __FreeBSD__
3102                 if (!sc->pfsync_sync_ok && carp_demote_adj_p)
3103                         (*carp_demote_adj_p)(-V_pfsync_carp_adj,
3104                             "pfsync bulk fail");
3105                 sc->pfsync_sync_ok = 1;
3106 #else
3107 #if NCARP > 0
3108                 if (!pfsync_sync_ok)
3109                         carp_group_demote_adj(&sc->sc_if, -1);
3110 #endif
3111                 pfsync_sync_ok = 1;
3112 #endif
3113 #ifdef __FreeBSD__
3114                 if (V_pf_status.debug >= PF_DEBUG_MISC)
3115 #else
3116                 if (pf_status.debug >= PF_DEBUG_MISC)
3117 #endif
3118                         printf("pfsync: failed to receive bulk update\n");
3119         }
3120
3121 #ifdef __FreeBSD__
3122         CURVNET_RESTORE();
3123 #endif
3124 }
3125
3126 void
3127 pfsync_send_plus(void *plus, size_t pluslen)
3128 {
3129 #ifdef __FreeBSD__
3130         struct pfsync_softc *sc = V_pfsyncif;
3131 #else
3132         struct pfsync_softc *sc = pfsyncif;
3133 #endif
3134         int s;
3135
3136         PF_LOCK_ASSERT();
3137
3138 #ifdef __FreeBSD__
3139         if (sc->sc_len + pluslen > sc->sc_ifp->if_mtu) {
3140 #else
3141         if (sc->sc_len + pluslen > sc->sc_if.if_mtu) {
3142 #endif
3143                 s = splnet();
3144                 pfsync_sendout();
3145                 splx(s);
3146         }
3147
3148         sc->sc_plus = plus;
3149         sc->sc_len += (sc->sc_pluslen = pluslen);
3150
3151         s = splnet();
3152         pfsync_sendout();
3153         splx(s);
3154 }
3155
3156 int
3157 pfsync_up(void)
3158 {
3159 #ifdef __FreeBSD__
3160         struct pfsync_softc *sc = V_pfsyncif;
3161 #else
3162         struct pfsync_softc *sc = pfsyncif;
3163 #endif
3164
3165 #ifdef __FreeBSD__
3166         if (sc == NULL || !ISSET(sc->sc_ifp->if_flags, IFF_DRV_RUNNING))
3167 #else
3168         if (sc == NULL || !ISSET(sc->sc_if.if_flags, IFF_RUNNING))
3169 #endif
3170                 return (0);
3171
3172         return (1);
3173 }
3174
3175 int
3176 pfsync_state_in_use(struct pf_state *st)
3177 {
3178 #ifdef __FreeBSD__
3179         struct pfsync_softc *sc = V_pfsyncif;
3180 #else
3181         struct pfsync_softc *sc = pfsyncif;
3182 #endif
3183
3184         if (sc == NULL)
3185                 return (0);
3186
3187         if (st->sync_state != PFSYNC_S_NONE ||
3188             st == sc->sc_bulk_next ||
3189             st == sc->sc_bulk_last)
3190                 return (1);
3191
3192         return (0);
3193 }
3194
3195 u_int pfsync_ints;
3196 u_int pfsync_tmos;
3197
3198 void
3199 pfsync_timeout(void *arg)
3200 {
3201 #if defined(__FreeBSD__) && defined(VIMAGE)
3202         struct pfsync_softc *sc = arg;
3203 #endif
3204         int s;
3205
3206 #ifdef __FreeBSD__
3207         CURVNET_SET(sc->sc_ifp->if_vnet);
3208 #endif
3209
3210         pfsync_tmos++;
3211
3212         s = splnet();
3213 #ifdef __FreeBSD__
3214         PF_LOCK();
3215 #endif
3216         pfsync_sendout();
3217 #ifdef __FreeBSD__
3218         PF_UNLOCK();
3219 #endif
3220         splx(s);
3221
3222 #ifdef __FreeBSD__
3223         CURVNET_RESTORE();
3224 #endif
3225 }
3226
3227 /* this is a softnet/netisr handler */
3228 void
3229 #ifdef __FreeBSD__
3230 pfsyncintr(void *arg)
3231 {
3232         struct pfsync_softc *sc = arg;
3233         struct mbuf *m, *n;
3234
3235         CURVNET_SET(sc->sc_ifp->if_vnet);
3236         pfsync_ints++;
3237
3238         PF_LOCK();
3239         if (sc->sc_len > PFSYNC_MINPKT)
3240                 pfsync_sendout1(0);
3241         _IF_DEQUEUE_ALL(&sc->sc_ifp->if_snd, m);
3242         PF_UNLOCK();
3243
3244         for (; m != NULL; m = n) {
3245
3246                 n = m->m_nextpkt;
3247                 m->m_nextpkt = NULL;
3248                 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)
3249                     == 0)
3250                         V_pfsyncstats.pfsyncs_opackets++;
3251                 else
3252                         V_pfsyncstats.pfsyncs_oerrors++;
3253         }
3254         CURVNET_RESTORE();
3255 }
3256 #else
3257 pfsyncintr(void)
3258 {
3259         int s;
3260
3261         pfsync_ints++;
3262
3263         s = splnet();
3264         pfsync_sendout();
3265         splx(s);
3266 }
3267 #endif
3268
3269 int
3270 pfsync_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
3271     size_t newlen)
3272 {
3273
3274 #ifdef notyet
3275         /* All sysctl names at this level are terminal. */
3276         if (namelen != 1)
3277                 return (ENOTDIR);
3278
3279         switch (name[0]) {
3280         case PFSYNCCTL_STATS:
3281                 if (newp != NULL)
3282                         return (EPERM);
3283                 return (sysctl_struct(oldp, oldlenp, newp, newlen,
3284                     &V_pfsyncstats, sizeof(V_pfsyncstats)));
3285         }
3286 #endif
3287         return (ENOPROTOOPT);
3288 }
3289
3290 #ifdef __FreeBSD__
3291 static int
3292 pfsync_multicast_setup(struct pfsync_softc *sc)
3293 {
3294         struct ip_moptions *imo = &sc->sc_imo;
3295         int error;
3296
3297         if (!(sc->sc_sync_if->if_flags & IFF_MULTICAST)) {
3298                 sc->sc_sync_if = NULL;
3299                 return (EADDRNOTAVAIL);
3300         }
3301
3302         imo->imo_membership = (struct in_multi **)malloc(
3303             (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_PFSYNC,
3304             M_WAITOK | M_ZERO);
3305         imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
3306         imo->imo_multicast_vif = -1;
3307
3308         if ((error = in_joingroup(sc->sc_sync_if, &sc->sc_sync_peer, NULL,
3309             &imo->imo_membership[0])) != 0) {
3310                 free(imo->imo_membership, M_PFSYNC);
3311                 return (error);
3312         }
3313         imo->imo_num_memberships++;
3314         imo->imo_multicast_ifp = sc->sc_sync_if;
3315         imo->imo_multicast_ttl = PFSYNC_DFLTTL;
3316         imo->imo_multicast_loop = 0;
3317
3318         return (0);
3319 }
3320
3321 static void
3322 pfsync_multicast_cleanup(struct pfsync_softc *sc)
3323 {
3324         struct ip_moptions *imo = &sc->sc_imo;
3325
3326         in_leavegroup(imo->imo_membership[0], NULL);
3327         free(imo->imo_membership, M_PFSYNC);
3328         imo->imo_membership = NULL;
3329         imo->imo_multicast_ifp = NULL;
3330 }
3331
3332 #ifdef INET
3333 extern  struct domain inetdomain;
3334 static struct protosw in_pfsync_protosw = {
3335         .pr_type =              SOCK_RAW,
3336         .pr_domain =            &inetdomain,
3337         .pr_protocol =          IPPROTO_PFSYNC,
3338         .pr_flags =             PR_ATOMIC|PR_ADDR,
3339         .pr_input =             pfsync_input,
3340         .pr_output =            (pr_output_t *)rip_output,
3341         .pr_ctloutput =         rip_ctloutput,
3342         .pr_usrreqs =           &rip_usrreqs
3343 };
3344 #endif
3345
3346 static int
3347 pfsync_init()
3348 {
3349         VNET_ITERATOR_DECL(vnet_iter);
3350         int error = 0;
3351
3352         VNET_LIST_RLOCK();
3353         VNET_FOREACH(vnet_iter) {
3354                 CURVNET_SET(vnet_iter);
3355                 V_pfsync_cloner = pfsync_cloner;
3356                 V_pfsync_cloner_data = pfsync_cloner_data;
3357                 V_pfsync_cloner.ifc_data = &V_pfsync_cloner_data;
3358                 if_clone_attach(&V_pfsync_cloner);
3359                 error = swi_add(NULL, "pfsync", pfsyncintr, V_pfsyncif,
3360                     SWI_NET, INTR_MPSAFE, &V_pfsync_swi_cookie);
3361                 CURVNET_RESTORE();
3362                 if (error)
3363                         goto fail_locked;
3364         }
3365         VNET_LIST_RUNLOCK();
3366 #ifdef INET
3367         error = pf_proto_register(PF_INET, &in_pfsync_protosw);
3368         if (error)
3369                 goto fail;
3370         error = ipproto_register(IPPROTO_PFSYNC);
3371         if (error) {
3372                 pf_proto_unregister(PF_INET, IPPROTO_PFSYNC, SOCK_RAW);
3373                 goto fail;
3374         }
3375 #endif
3376         PF_LOCK();
3377         pfsync_state_import_ptr = pfsync_state_import;
3378         pfsync_up_ptr = pfsync_up;
3379         pfsync_insert_state_ptr = pfsync_insert_state;
3380         pfsync_update_state_ptr = pfsync_update_state;
3381         pfsync_delete_state_ptr = pfsync_delete_state;
3382         pfsync_clear_states_ptr = pfsync_clear_states;
3383         pfsync_state_in_use_ptr = pfsync_state_in_use;
3384         pfsync_defer_ptr = pfsync_defer;
3385         PF_UNLOCK();
3386
3387         return (0);
3388
3389 fail:
3390         VNET_LIST_RLOCK();
3391 fail_locked:
3392         VNET_FOREACH(vnet_iter) {
3393                 CURVNET_SET(vnet_iter);
3394                 if (V_pfsync_swi_cookie) {
3395                         swi_remove(V_pfsync_swi_cookie);
3396                         if_clone_detach(&V_pfsync_cloner);
3397                 }
3398                 CURVNET_RESTORE();
3399         }
3400         VNET_LIST_RUNLOCK();
3401
3402         return (error);
3403 }
3404
3405 static void
3406 pfsync_uninit()
3407 {
3408         VNET_ITERATOR_DECL(vnet_iter);
3409
3410         PF_LOCK();
3411         pfsync_state_import_ptr = NULL;
3412         pfsync_up_ptr = NULL;
3413         pfsync_insert_state_ptr = NULL;
3414         pfsync_update_state_ptr = NULL;
3415         pfsync_delete_state_ptr = NULL;
3416         pfsync_clear_states_ptr = NULL;
3417         pfsync_state_in_use_ptr = NULL;
3418         pfsync_defer_ptr = NULL;
3419         PF_UNLOCK();
3420
3421         ipproto_unregister(IPPROTO_PFSYNC);
3422         pf_proto_unregister(PF_INET, IPPROTO_PFSYNC, SOCK_RAW);
3423         VNET_LIST_RLOCK();
3424         VNET_FOREACH(vnet_iter) {
3425                 CURVNET_SET(vnet_iter);
3426                 swi_remove(V_pfsync_swi_cookie);
3427                 if_clone_detach(&V_pfsync_cloner);
3428                 CURVNET_RESTORE();
3429         }
3430         VNET_LIST_RUNLOCK();
3431 }
3432
3433 static int
3434 pfsync_modevent(module_t mod, int type, void *data)
3435 {
3436         int error = 0;
3437
3438         switch (type) {
3439         case MOD_LOAD:
3440                 error = pfsync_init();
3441                 break;
3442         case MOD_QUIESCE:
3443                 /*
3444                  * Module should not be unloaded due to race conditions.
3445                  */
3446                 error = EPERM;
3447                 break;
3448         case MOD_UNLOAD:
3449                 pfsync_uninit();
3450                 break;
3451         default:
3452                 error = EINVAL;
3453                 break;
3454         }
3455
3456         return (error);
3457 }
3458
3459 static moduledata_t pfsync_mod = {
3460         "pfsync",
3461         pfsync_modevent,
3462         0
3463 };
3464
3465 #define PFSYNC_MODVER 1
3466
3467 DECLARE_MODULE(pfsync, pfsync_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
3468 MODULE_VERSION(pfsync, PFSYNC_MODVER);
3469 MODULE_DEPEND(pfsync, pf, PF_MODVER, PF_MODVER, PF_MODVER);
3470 #endif /* __FreeBSD__ */