Home | History | Annotate | Line # | Download | only in net
if_pfsync.c revision 1.2.58.1
      1  1.2.58.1    yamt /*	$NetBSD: if_pfsync.c,v 1.2.58.1 2009/09/16 13:37:59 yamt Exp $	*/
      2  1.2.58.1    yamt /*	$OpenBSD: if_pfsync.c,v 1.83 2007/06/26 14:44:12 mcbride Exp $	*/
      3       1.1  itojun 
      4       1.1  itojun /*
      5       1.1  itojun  * Copyright (c) 2002 Michael Shalayeff
      6       1.1  itojun  * All rights reserved.
      7       1.1  itojun  *
      8       1.1  itojun  * Redistribution and use in source and binary forms, with or without
      9       1.1  itojun  * modification, are permitted provided that the following conditions
     10       1.1  itojun  * are met:
     11       1.1  itojun  * 1. Redistributions of source code must retain the above copyright
     12       1.1  itojun  *    notice, this list of conditions and the following disclaimer.
     13       1.1  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1  itojun  *    notice, this list of conditions and the following disclaimer in the
     15       1.1  itojun  *    documentation and/or other materials provided with the distribution.
     16       1.1  itojun  *
     17       1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18       1.1  itojun  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19       1.1  itojun  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20       1.1  itojun  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
     21       1.1  itojun  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22       1.1  itojun  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23       1.1  itojun  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24       1.1  itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     25       1.1  itojun  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     26       1.1  itojun  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     27       1.1  itojun  * THE POSSIBILITY OF SUCH DAMAGE.
     28       1.1  itojun  */
     29       1.1  itojun 
     30  1.2.58.1    yamt #include <sys/cdefs.h>
     31  1.2.58.1    yamt __KERNEL_RCSID(0, "$NetBSD: if_pfsync.c,v 1.2.58.1 2009/09/16 13:37:59 yamt Exp $");
     32  1.2.58.1    yamt 
     33  1.2.58.1    yamt #ifdef _KERNEL_OPT
     34  1.2.58.1    yamt #include "opt_inet.h"
     35  1.2.58.1    yamt #include "opt_inet6.h"
     36  1.2.58.1    yamt #endif
     37       1.1  itojun 
     38       1.1  itojun #include <sys/param.h>
     39       1.1  itojun #include <sys/proc.h>
     40       1.1  itojun #include <sys/systm.h>
     41       1.1  itojun #include <sys/time.h>
     42       1.1  itojun #include <sys/mbuf.h>
     43       1.1  itojun #include <sys/socket.h>
     44       1.1  itojun #include <sys/ioctl.h>
     45  1.2.58.1    yamt #include <sys/callout.h>
     46  1.2.58.1    yamt #include <sys/kernel.h>
     47       1.1  itojun 
     48       1.1  itojun #include <net/if.h>
     49       1.1  itojun #include <net/if_types.h>
     50       1.1  itojun #include <net/route.h>
     51       1.1  itojun #include <net/bpf.h>
     52  1.2.58.1    yamt #include <netinet/in.h>
     53  1.2.58.1    yamt #ifndef __NetBSD__
     54  1.2.58.1    yamt #include <netinet/if_ether.h>
     55  1.2.58.1    yamt #else
     56  1.2.58.1    yamt #include <net/if_ether.h>
     57  1.2.58.1    yamt #endif /* __NetBSD__ */
     58  1.2.58.1    yamt #include <netinet/tcp.h>
     59  1.2.58.1    yamt #include <netinet/tcp_seq.h>
     60       1.1  itojun 
     61       1.1  itojun #ifdef	INET
     62       1.1  itojun #include <netinet/in_systm.h>
     63       1.1  itojun #include <netinet/in_var.h>
     64       1.1  itojun #include <netinet/ip.h>
     65       1.1  itojun #include <netinet/ip_var.h>
     66       1.1  itojun #endif
     67       1.1  itojun 
     68       1.1  itojun #ifdef INET6
     69       1.1  itojun #include <netinet6/nd6.h>
     70       1.1  itojun #endif /* INET6 */
     71       1.1  itojun 
     72  1.2.58.1    yamt #include "carp.h"
     73  1.2.58.1    yamt #if NCARP > 0
     74  1.2.58.1    yamt extern int carp_suppress_preempt;
     75  1.2.58.1    yamt #endif
     76  1.2.58.1    yamt 
     77       1.1  itojun #include <net/pfvar.h>
     78       1.1  itojun #include <net/if_pfsync.h>
     79       1.1  itojun 
     80  1.2.58.1    yamt #ifdef __NetBSD__
     81  1.2.58.1    yamt #include <sys/conf.h>
     82  1.2.58.1    yamt #include <sys/lwp.h>
     83  1.2.58.1    yamt #include <sys/kauth.h>
     84  1.2.58.1    yamt #include <sys/sysctl.h>
     85  1.2.58.1    yamt 
     86  1.2.58.1    yamt #include <net/net_stats.h>
     87  1.2.58.1    yamt 
     88  1.2.58.1    yamt percpu_t	*pfsyncstat_percpu;
     89  1.2.58.1    yamt 
     90  1.2.58.1    yamt #define	PFSYNC_STATINC(x) _NET_STATINC(pfsyncstat_percpu, x)
     91  1.2.58.1    yamt #endif /* __NetBSD__ */
     92  1.2.58.1    yamt 
     93  1.2.58.1    yamt #include "bpfilter.h"
     94  1.2.58.1    yamt #include "pfsync.h"
     95  1.2.58.1    yamt 
     96       1.1  itojun #define PFSYNC_MINMTU	\
     97       1.1  itojun     (sizeof(struct pfsync_header) + sizeof(struct pf_state))
     98       1.1  itojun 
     99       1.1  itojun #ifdef PFSYNCDEBUG
    100       1.1  itojun #define DPRINTF(x)    do { if (pfsyncdebug) printf x ; } while (0)
    101       1.1  itojun int pfsyncdebug;
    102       1.1  itojun #else
    103       1.1  itojun #define DPRINTF(x)
    104       1.1  itojun #endif
    105       1.1  itojun 
    106  1.2.58.1    yamt extern int ifqmaxlen; /* XXX */
    107  1.2.58.1    yamt 
    108  1.2.58.1    yamt struct pfsync_softc	*pfsyncif = NULL;
    109       1.1  itojun 
    110       1.1  itojun void	pfsyncattach(int);
    111  1.2.58.1    yamt int	pfsync_clone_create(struct if_clone *, int);
    112  1.2.58.1    yamt int	pfsync_clone_destroy(struct ifnet *);
    113       1.1  itojun void	pfsync_setmtu(struct pfsync_softc *, int);
    114  1.2.58.1    yamt int	pfsync_alloc_scrub_memory(struct pfsync_state_peer *,
    115  1.2.58.1    yamt 	    struct pf_state_peer *);
    116  1.2.58.1    yamt int	pfsync_insert_net_state(struct pfsync_state *, u_int8_t);
    117  1.2.58.1    yamt void	pfsync_update_net_tdb(struct pfsync_tdb *);
    118  1.2.58.1    yamt int	pfsyncoutput(struct ifnet *, struct mbuf *, const struct sockaddr *,
    119       1.1  itojun 	    struct rtentry *);
    120  1.2.58.1    yamt int	pfsyncioctl(struct ifnet *, u_long, void*);
    121       1.1  itojun void	pfsyncstart(struct ifnet *);
    122       1.1  itojun 
    123       1.1  itojun struct mbuf *pfsync_get_mbuf(struct pfsync_softc *, u_int8_t, void **);
    124       1.1  itojun int	pfsync_request_update(struct pfsync_state_upd *, struct in_addr *);
    125       1.1  itojun int	pfsync_sendout(struct pfsync_softc *);
    126  1.2.58.1    yamt int	pfsync_tdb_sendout(struct pfsync_softc *);
    127  1.2.58.1    yamt int	pfsync_sendout_mbuf(struct pfsync_softc *, struct mbuf *);
    128       1.1  itojun void	pfsync_timeout(void *);
    129  1.2.58.1    yamt void	pfsync_tdb_timeout(void *);
    130       1.1  itojun void	pfsync_send_bus(struct pfsync_softc *, u_int8_t);
    131       1.1  itojun void	pfsync_bulk_update(void *);
    132       1.1  itojun void	pfsync_bulkfail(void *);
    133       1.1  itojun 
    134  1.2.58.1    yamt int	pfsync_sync_ok;
    135  1.2.58.1    yamt 
    136  1.2.58.1    yamt struct if_clone	pfsync_cloner =
    137  1.2.58.1    yamt     IF_CLONE_INITIALIZER("pfsync", pfsync_clone_create, pfsync_clone_destroy);
    138       1.1  itojun 
    139       1.1  itojun void
    140       1.1  itojun pfsyncattach(int npfsync)
    141       1.1  itojun {
    142  1.2.58.1    yamt 	if_clone_attach(&pfsync_cloner);
    143  1.2.58.1    yamt 
    144  1.2.58.1    yamt 	pfsyncstat_percpu = percpu_alloc(sizeof(uint64_t) * PFSYNC_NSTATS);
    145  1.2.58.1    yamt }
    146  1.2.58.1    yamt 
    147  1.2.58.1    yamt int
    148  1.2.58.1    yamt pfsync_clone_create(struct if_clone *ifc, int unit)
    149  1.2.58.1    yamt {
    150       1.1  itojun 	struct ifnet *ifp;
    151       1.1  itojun 
    152  1.2.58.1    yamt 	if (unit != 0)
    153  1.2.58.1    yamt 		return (EINVAL);
    154  1.2.58.1    yamt 
    155       1.1  itojun 	pfsync_sync_ok = 1;
    156  1.2.58.1    yamt 	if ((pfsyncif = malloc(sizeof(*pfsyncif), M_DEVBUF, M_NOWAIT)) == NULL)
    157  1.2.58.1    yamt 		return (ENOMEM);
    158  1.2.58.1    yamt 	memset(pfsyncif, 0, sizeof(*pfsyncif));
    159  1.2.58.1    yamt 	pfsyncif->sc_mbuf = NULL;
    160  1.2.58.1    yamt 	pfsyncif->sc_mbuf_net = NULL;
    161  1.2.58.1    yamt 	pfsyncif->sc_mbuf_tdb = NULL;
    162  1.2.58.1    yamt 	pfsyncif->sc_statep.s = NULL;
    163  1.2.58.1    yamt 	pfsyncif->sc_statep_net.s = NULL;
    164  1.2.58.1    yamt 	pfsyncif->sc_statep_tdb.t = NULL;
    165  1.2.58.1    yamt 	pfsyncif->sc_maxupdates = 128;
    166  1.2.58.1    yamt 	pfsyncif->sc_sync_peer.s_addr = INADDR_PFSYNC_GROUP;
    167  1.2.58.1    yamt 	pfsyncif->sc_sendaddr.s_addr = INADDR_PFSYNC_GROUP;
    168  1.2.58.1    yamt 	pfsyncif->sc_ureq_received = 0;
    169  1.2.58.1    yamt 	pfsyncif->sc_ureq_sent = 0;
    170  1.2.58.1    yamt 	pfsyncif->sc_bulk_send_next = NULL;
    171  1.2.58.1    yamt 	pfsyncif->sc_bulk_terminator = NULL;
    172  1.2.58.1    yamt 	ifp = &pfsyncif->sc_if;
    173  1.2.58.1    yamt 	snprintf(ifp->if_xname, sizeof ifp->if_xname, "pfsync%d", unit);
    174  1.2.58.1    yamt 	ifp->if_softc = pfsyncif;
    175       1.1  itojun 	ifp->if_ioctl = pfsyncioctl;
    176       1.1  itojun 	ifp->if_output = pfsyncoutput;
    177       1.1  itojun 	ifp->if_start = pfsyncstart;
    178       1.1  itojun 	ifp->if_type = IFT_PFSYNC;
    179       1.1  itojun 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
    180       1.1  itojun 	ifp->if_hdrlen = PFSYNC_HDRLEN;
    181  1.2.58.1    yamt 	pfsync_setmtu(pfsyncif, ETHERMTU);
    182  1.2.58.1    yamt 
    183  1.2.58.1    yamt 	callout_init(&pfsyncif->sc_tmo, 0);
    184  1.2.58.1    yamt 	callout_init(&pfsyncif->sc_tdb_tmo, 0);
    185  1.2.58.1    yamt 	callout_init(&pfsyncif->sc_bulk_tmo, 0);
    186  1.2.58.1    yamt 	callout_init(&pfsyncif->sc_bulkfail_tmo, 0);
    187  1.2.58.1    yamt 	callout_setfunc(&pfsyncif->sc_tmo, pfsync_timeout, pfsyncif);
    188  1.2.58.1    yamt 	callout_setfunc(&pfsyncif->sc_tdb_tmo, pfsync_tdb_timeout, pfsyncif);
    189  1.2.58.1    yamt 	callout_setfunc(&pfsyncif->sc_bulk_tmo, pfsync_bulk_update, pfsyncif);
    190  1.2.58.1    yamt 	callout_setfunc(&pfsyncif->sc_bulkfail_tmo, pfsync_bulkfail, pfsyncif);
    191  1.2.58.1    yamt 
    192       1.1  itojun 	if_attach(ifp);
    193       1.1  itojun 	if_alloc_sadl(ifp);
    194       1.1  itojun 
    195       1.1  itojun #if NBPFILTER > 0
    196  1.2.58.1    yamt 	bpfattach(&pfsyncif->sc_if, DLT_PFSYNC, PFSYNC_HDRLEN);
    197  1.2.58.1    yamt #endif
    198  1.2.58.1    yamt 
    199  1.2.58.1    yamt 	return (0);
    200  1.2.58.1    yamt }
    201  1.2.58.1    yamt 
    202  1.2.58.1    yamt int
    203  1.2.58.1    yamt pfsync_clone_destroy(struct ifnet *ifp)
    204  1.2.58.1    yamt {
    205  1.2.58.1    yamt #if NBPFILTER > 0
    206  1.2.58.1    yamt 	bpfdetach(ifp);
    207       1.1  itojun #endif
    208  1.2.58.1    yamt 	if_detach(ifp);
    209  1.2.58.1    yamt 	free(pfsyncif, M_DEVBUF);
    210  1.2.58.1    yamt 	pfsyncif = NULL;
    211  1.2.58.1    yamt 	return (0);
    212       1.1  itojun }
    213       1.1  itojun 
    214       1.1  itojun /*
    215       1.1  itojun  * Start output on the pfsync interface.
    216       1.1  itojun  */
    217       1.1  itojun void
    218       1.1  itojun pfsyncstart(struct ifnet *ifp)
    219       1.1  itojun {
    220       1.1  itojun 	struct mbuf *m;
    221       1.1  itojun 	int s;
    222       1.1  itojun 
    223       1.1  itojun 	for (;;) {
    224  1.2.58.1    yamt 		s = splnet();
    225       1.1  itojun 		IF_DROP(&ifp->if_snd);
    226       1.1  itojun 		IF_DEQUEUE(&ifp->if_snd, m);
    227       1.1  itojun 		splx(s);
    228       1.1  itojun 
    229       1.1  itojun 		if (m == NULL)
    230       1.1  itojun 			return;
    231       1.1  itojun 		else
    232       1.1  itojun 			m_freem(m);
    233       1.1  itojun 	}
    234       1.1  itojun }
    235       1.1  itojun 
    236       1.1  itojun int
    237  1.2.58.1    yamt pfsync_alloc_scrub_memory(struct pfsync_state_peer *s,
    238  1.2.58.1    yamt     struct pf_state_peer *d)
    239  1.2.58.1    yamt {
    240  1.2.58.1    yamt 	if (s->scrub.scrub_flag && d->scrub == NULL) {
    241  1.2.58.1    yamt 		d->scrub = pool_get(&pf_state_scrub_pl, PR_NOWAIT);
    242  1.2.58.1    yamt 		if (d->scrub == NULL)
    243  1.2.58.1    yamt 			return (ENOMEM);
    244  1.2.58.1    yamt 		memset(d->scrub, 0, sizeof(*d->scrub));
    245  1.2.58.1    yamt 	}
    246  1.2.58.1    yamt 
    247  1.2.58.1    yamt 	return (0);
    248  1.2.58.1    yamt }
    249  1.2.58.1    yamt 
    250  1.2.58.1    yamt int
    251  1.2.58.1    yamt pfsync_insert_net_state(struct pfsync_state *sp, u_int8_t chksum_flag)
    252       1.1  itojun {
    253       1.1  itojun 	struct pf_state	*st = NULL;
    254  1.2.58.1    yamt 	struct pf_state_key *sk = NULL;
    255       1.1  itojun 	struct pf_rule *r = NULL;
    256       1.1  itojun 	struct pfi_kif	*kif;
    257       1.1  itojun 
    258       1.1  itojun 	if (sp->creatorid == 0 && pf_status.debug >= PF_DEBUG_MISC) {
    259       1.1  itojun 		printf("pfsync_insert_net_state: invalid creator id:"
    260       1.1  itojun 		    " %08x\n", ntohl(sp->creatorid));
    261       1.1  itojun 		return (EINVAL);
    262       1.1  itojun 	}
    263       1.1  itojun 
    264  1.2.58.1    yamt 	kif = pfi_kif_get(sp->ifname);
    265       1.1  itojun 	if (kif == NULL) {
    266       1.1  itojun 		if (pf_status.debug >= PF_DEBUG_MISC)
    267       1.1  itojun 			printf("pfsync_insert_net_state: "
    268       1.1  itojun 			    "unknown interface: %s\n", sp->ifname);
    269       1.1  itojun 		/* skip this state */
    270       1.1  itojun 		return (0);
    271       1.1  itojun 	}
    272       1.1  itojun 
    273       1.1  itojun 	/*
    274  1.2.58.1    yamt 	 * If the ruleset checksums match, it's safe to associate the state
    275  1.2.58.1    yamt 	 * with the rule of that number.
    276       1.1  itojun 	 */
    277  1.2.58.1    yamt 	if (sp->rule != htonl(-1) && sp->anchor == htonl(-1) && chksum_flag &&
    278  1.2.58.1    yamt 	    ntohl(sp->rule) <
    279  1.2.58.1    yamt 	    pf_main_ruleset.rules[PF_RULESET_FILTER].active.rcount)
    280  1.2.58.1    yamt 		r = pf_main_ruleset.rules[
    281  1.2.58.1    yamt 		    PF_RULESET_FILTER].active.ptr_array[ntohl(sp->rule)];
    282  1.2.58.1    yamt 	else
    283  1.2.58.1    yamt 		r = &pf_default_rule;
    284       1.1  itojun 
    285       1.1  itojun 	if (!r->max_states || r->states < r->max_states)
    286       1.1  itojun 		st = pool_get(&pf_state_pl, PR_NOWAIT);
    287       1.1  itojun 	if (st == NULL) {
    288  1.2.58.1    yamt 		pfi_kif_unref(kif, PFI_KIF_REF_NONE);
    289  1.2.58.1    yamt 		return (ENOMEM);
    290  1.2.58.1    yamt 	}
    291  1.2.58.1    yamt 	memset(st, 0, sizeof(*st));
    292  1.2.58.1    yamt 
    293  1.2.58.1    yamt 	if ((sk = pf_alloc_state_key(st)) == NULL) {
    294  1.2.58.1    yamt 		pool_put(&pf_state_pl, st);
    295  1.2.58.1    yamt 		pfi_kif_unref(kif, PFI_KIF_REF_NONE);
    296  1.2.58.1    yamt 		return (ENOMEM);
    297  1.2.58.1    yamt 	}
    298  1.2.58.1    yamt 
    299  1.2.58.1    yamt 	/* allocate memory for scrub info */
    300  1.2.58.1    yamt 	if (pfsync_alloc_scrub_memory(&sp->src, &st->src) ||
    301  1.2.58.1    yamt 	    pfsync_alloc_scrub_memory(&sp->dst, &st->dst)) {
    302  1.2.58.1    yamt 		pfi_kif_unref(kif, PFI_KIF_REF_NONE);
    303  1.2.58.1    yamt 		if (st->src.scrub)
    304  1.2.58.1    yamt 			pool_put(&pf_state_scrub_pl, st->src.scrub);
    305  1.2.58.1    yamt 		pool_put(&pf_state_pl, st);
    306  1.2.58.1    yamt 		pool_put(&pf_state_key_pl, sk);
    307       1.1  itojun 		return (ENOMEM);
    308       1.1  itojun 	}
    309       1.1  itojun 
    310       1.1  itojun 	st->rule.ptr = r;
    311       1.1  itojun 	/* XXX get pointers to nat_rule and anchor */
    312       1.1  itojun 
    313  1.2.58.1    yamt 	/* XXX when we have nat_rule/anchors, use STATE_INC_COUNTERS */
    314  1.2.58.1    yamt 	r->states++;
    315  1.2.58.1    yamt 
    316       1.1  itojun 	/* fill in the rest of the state entry */
    317  1.2.58.1    yamt 	pf_state_host_ntoh(&sp->lan, &sk->lan);
    318  1.2.58.1    yamt 	pf_state_host_ntoh(&sp->gwy, &sk->gwy);
    319  1.2.58.1    yamt 	pf_state_host_ntoh(&sp->ext, &sk->ext);
    320       1.1  itojun 
    321       1.1  itojun 	pf_state_peer_ntoh(&sp->src, &st->src);
    322       1.1  itojun 	pf_state_peer_ntoh(&sp->dst, &st->dst);
    323       1.1  itojun 
    324  1.2.58.1    yamt 	memcpy(&st->rt_addr, &sp->rt_addr, sizeof(st->rt_addr));
    325  1.2.58.1    yamt 	st->creation = time_second - ntohl(sp->creation);
    326  1.2.58.1    yamt 	st->expire = ntohl(sp->expire) + time_second;
    327  1.2.58.1    yamt 
    328  1.2.58.1    yamt 	sk->af = sp->af;
    329  1.2.58.1    yamt 	sk->proto = sp->proto;
    330  1.2.58.1    yamt 	sk->direction = sp->direction;
    331       1.1  itojun 	st->log = sp->log;
    332       1.1  itojun 	st->timeout = sp->timeout;
    333       1.1  itojun 	st->allow_opts = sp->allow_opts;
    334       1.1  itojun 
    335  1.2.58.1    yamt 	memcpy(&st->id, sp->id, sizeof(st->id));
    336       1.1  itojun 	st->creatorid = sp->creatorid;
    337  1.2.58.1    yamt 	st->sync_flags = PFSTATE_FROMSYNC;
    338       1.1  itojun 
    339       1.1  itojun 	if (pf_insert_state(kif, st)) {
    340  1.2.58.1    yamt 		pfi_kif_unref(kif, PFI_KIF_REF_NONE);
    341  1.2.58.1    yamt 		/* XXX when we have nat_rule/anchors, use STATE_DEC_COUNTERS */
    342  1.2.58.1    yamt 		r->states--;
    343  1.2.58.1    yamt 		if (st->dst.scrub)
    344  1.2.58.1    yamt 			pool_put(&pf_state_scrub_pl, st->dst.scrub);
    345  1.2.58.1    yamt 		if (st->src.scrub)
    346  1.2.58.1    yamt 			pool_put(&pf_state_scrub_pl, st->src.scrub);
    347       1.1  itojun 		pool_put(&pf_state_pl, st);
    348       1.1  itojun 		return (EINVAL);
    349       1.1  itojun 	}
    350       1.1  itojun 
    351       1.1  itojun 	return (0);
    352       1.1  itojun }
    353       1.1  itojun 
    354       1.1  itojun void
    355       1.1  itojun pfsync_input(struct mbuf *m, ...)
    356       1.1  itojun {
    357       1.1  itojun 	struct ip *ip = mtod(m, struct ip *);
    358       1.1  itojun 	struct pfsync_header *ph;
    359  1.2.58.1    yamt 	struct pfsync_softc *sc = pfsyncif;
    360  1.2.58.1    yamt 	struct pf_state *st;
    361  1.2.58.1    yamt 	struct pf_state_key *sk;
    362  1.2.58.1    yamt 	struct pf_state_cmp id_key;
    363       1.1  itojun 	struct pfsync_state *sp;
    364       1.1  itojun 	struct pfsync_state_upd *up;
    365       1.1  itojun 	struct pfsync_state_del *dp;
    366       1.1  itojun 	struct pfsync_state_clr *cp;
    367       1.1  itojun 	struct pfsync_state_upd_req *rup;
    368       1.1  itojun 	struct pfsync_state_bus *bus;
    369  1.2.58.1    yamt #ifdef IPSEC
    370  1.2.58.1    yamt 	struct pfsync_tdb *pt;
    371  1.2.58.1    yamt #endif
    372       1.1  itojun 	struct in_addr src;
    373       1.1  itojun 	struct mbuf *mp;
    374  1.2.58.1    yamt 	int iplen, action, error, i, s, count, offp, sfail, stale = 0;
    375  1.2.58.1    yamt 	u_int8_t chksum_flag = 0;
    376       1.1  itojun 
    377  1.2.58.1    yamt 	PFSYNC_STATINC(PFSYNC_STAT_IPACKETS);
    378       1.1  itojun 
    379       1.1  itojun 	/* verify that we have a sync interface configured */
    380  1.2.58.1    yamt 	if (!sc || !sc->sc_sync_ifp || !pf_status.running)
    381       1.1  itojun 		goto done;
    382       1.1  itojun 
    383       1.1  itojun 	/* verify that the packet came in on the right interface */
    384       1.1  itojun 	if (sc->sc_sync_ifp != m->m_pkthdr.rcvif) {
    385  1.2.58.1    yamt 		PFSYNC_STATINC(PFSYNC_STAT_BADIF);
    386       1.1  itojun 		goto done;
    387       1.1  itojun 	}
    388       1.1  itojun 
    389       1.1  itojun 	/* verify that the IP TTL is 255.  */
    390       1.1  itojun 	if (ip->ip_ttl != PFSYNC_DFLTTL) {
    391  1.2.58.1    yamt 		PFSYNC_STATINC(PFSYNC_STAT_BADTTL);
    392       1.1  itojun 		goto done;
    393       1.1  itojun 	}
    394       1.1  itojun 
    395       1.1  itojun 	iplen = ip->ip_hl << 2;
    396       1.1  itojun 
    397       1.1  itojun 	if (m->m_pkthdr.len < iplen + sizeof(*ph)) {
    398  1.2.58.1    yamt 		PFSYNC_STATINC(PFSYNC_STAT_HDROPS);
    399       1.1  itojun 		goto done;
    400       1.1  itojun 	}
    401       1.1  itojun 
    402       1.1  itojun 	if (iplen + sizeof(*ph) > m->m_len) {
    403       1.1  itojun 		if ((m = m_pullup(m, iplen + sizeof(*ph))) == NULL) {
    404  1.2.58.1    yamt 			PFSYNC_STATINC(PFSYNC_STAT_HDROPS);
    405       1.1  itojun 			goto done;
    406       1.1  itojun 		}
    407       1.1  itojun 		ip = mtod(m, struct ip *);
    408       1.1  itojun 	}
    409       1.1  itojun 	ph = (struct pfsync_header *)((char *)ip + iplen);
    410       1.1  itojun 
    411       1.1  itojun 	/* verify the version */
    412       1.1  itojun 	if (ph->version != PFSYNC_VERSION) {
    413  1.2.58.1    yamt 		PFSYNC_STATINC(PFSYNC_STAT_BADVER);
    414       1.1  itojun 		goto done;
    415       1.1  itojun 	}
    416       1.1  itojun 
    417       1.1  itojun 	action = ph->action;
    418       1.1  itojun 	count = ph->count;
    419       1.1  itojun 
    420       1.1  itojun 	/* make sure it's a valid action code */
    421       1.1  itojun 	if (action >= PFSYNC_ACT_MAX) {
    422  1.2.58.1    yamt 		PFSYNC_STATINC(PFSYNC_STAT_BADACT);
    423       1.1  itojun 		goto done;
    424       1.1  itojun 	}
    425       1.1  itojun 
    426       1.1  itojun 	/* Cheaper to grab this now than having to mess with mbufs later */
    427       1.1  itojun 	src = ip->ip_src;
    428       1.1  itojun 
    429  1.2.58.1    yamt 	if (!bcmp(&ph->pf_chksum, &pf_status.pf_chksum, PF_MD5_DIGEST_LENGTH))
    430  1.2.58.1    yamt 		chksum_flag++;
    431  1.2.58.1    yamt 
    432       1.1  itojun 	switch (action) {
    433       1.1  itojun 	case PFSYNC_ACT_CLR: {
    434  1.2.58.1    yamt 		struct pf_state *nexts;
    435  1.2.58.1    yamt 		struct pf_state_key *nextsk;
    436  1.2.58.1    yamt 		struct pfi_kif *kif;
    437       1.1  itojun 		u_int32_t creatorid;
    438       1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    439       1.1  itojun 		    sizeof(*cp), &offp)) == NULL) {
    440  1.2.58.1    yamt 			PFSYNC_STATINC(PFSYNC_STAT_BADLEN);
    441       1.1  itojun 			return;
    442       1.1  itojun 		}
    443       1.1  itojun 		cp = (struct pfsync_state_clr *)(mp->m_data + offp);
    444       1.1  itojun 		creatorid = cp->creatorid;
    445       1.1  itojun 
    446       1.1  itojun 		s = splsoftnet();
    447       1.1  itojun 		if (cp->ifname[0] == '\0') {
    448  1.2.58.1    yamt 			for (st = RB_MIN(pf_state_tree_id, &tree_id);
    449  1.2.58.1    yamt 			    st; st = nexts) {
    450  1.2.58.1    yamt 				nexts = RB_NEXT(pf_state_tree_id, &tree_id, st);
    451  1.2.58.1    yamt 				if (st->creatorid == creatorid) {
    452  1.2.58.1    yamt 					st->sync_flags |= PFSTATE_FROMSYNC;
    453  1.2.58.1    yamt 					pf_unlink_state(st);
    454  1.2.58.1    yamt 				}
    455       1.1  itojun 			}
    456       1.1  itojun 		} else {
    457  1.2.58.1    yamt 			if ((kif = pfi_kif_get(cp->ifname)) == NULL) {
    458       1.1  itojun 				splx(s);
    459  1.2.58.1    yamt 				return;
    460       1.1  itojun 			}
    461  1.2.58.1    yamt 			for (sk = RB_MIN(pf_state_tree_lan_ext,
    462  1.2.58.1    yamt 			    &pf_statetbl_lan_ext); sk; sk = nextsk) {
    463  1.2.58.1    yamt 				nextsk = RB_NEXT(pf_state_tree_lan_ext,
    464  1.2.58.1    yamt 				    &pf_statetbl_lan_ext, sk);
    465  1.2.58.1    yamt 				TAILQ_FOREACH(st, &sk->states, next) {
    466  1.2.58.1    yamt 					if (st->creatorid == creatorid) {
    467  1.2.58.1    yamt 						st->sync_flags |=
    468  1.2.58.1    yamt 						    PFSTATE_FROMSYNC;
    469  1.2.58.1    yamt 						pf_unlink_state(st);
    470  1.2.58.1    yamt 					}
    471  1.2.58.1    yamt 				}
    472       1.1  itojun 			}
    473       1.1  itojun 		}
    474       1.1  itojun 		splx(s);
    475       1.1  itojun 
    476       1.1  itojun 		break;
    477       1.1  itojun 	}
    478       1.1  itojun 	case PFSYNC_ACT_INS:
    479       1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    480       1.1  itojun 		    count * sizeof(*sp), &offp)) == NULL) {
    481  1.2.58.1    yamt 			PFSYNC_STATINC(PFSYNC_STAT_BADLEN);
    482       1.1  itojun 			return;
    483       1.1  itojun 		}
    484       1.1  itojun 
    485       1.1  itojun 		s = splsoftnet();
    486       1.1  itojun 		for (i = 0, sp = (struct pfsync_state *)(mp->m_data + offp);
    487       1.1  itojun 		    i < count; i++, sp++) {
    488       1.1  itojun 			/* check for invalid values */
    489       1.1  itojun 			if (sp->timeout >= PFTM_MAX ||
    490       1.1  itojun 			    sp->src.state > PF_TCPS_PROXY_DST ||
    491       1.1  itojun 			    sp->dst.state > PF_TCPS_PROXY_DST ||
    492       1.1  itojun 			    sp->direction > PF_OUT ||
    493       1.1  itojun 			    (sp->af != AF_INET && sp->af != AF_INET6)) {
    494       1.1  itojun 				if (pf_status.debug >= PF_DEBUG_MISC)
    495       1.1  itojun 					printf("pfsync_insert: PFSYNC_ACT_INS: "
    496       1.1  itojun 					    "invalid value\n");
    497  1.2.58.1    yamt 				PFSYNC_STATINC(PFSYNC_STAT_BADSTATE);
    498       1.1  itojun 				continue;
    499       1.1  itojun 			}
    500       1.1  itojun 
    501  1.2.58.1    yamt 			if ((error = pfsync_insert_net_state(sp,
    502  1.2.58.1    yamt 			    chksum_flag))) {
    503       1.1  itojun 				if (error == ENOMEM) {
    504       1.1  itojun 					splx(s);
    505       1.1  itojun 					goto done;
    506       1.1  itojun 				}
    507       1.1  itojun 				continue;
    508       1.1  itojun 			}
    509       1.1  itojun 		}
    510       1.1  itojun 		splx(s);
    511       1.1  itojun 		break;
    512       1.1  itojun 	case PFSYNC_ACT_UPD:
    513       1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    514       1.1  itojun 		    count * sizeof(*sp), &offp)) == NULL) {
    515  1.2.58.1    yamt 			PFSYNC_STATINC(PFSYNC_STAT_BADLEN);
    516       1.1  itojun 			return;
    517       1.1  itojun 		}
    518       1.1  itojun 
    519       1.1  itojun 		s = splsoftnet();
    520       1.1  itojun 		for (i = 0, sp = (struct pfsync_state *)(mp->m_data + offp);
    521       1.1  itojun 		    i < count; i++, sp++) {
    522  1.2.58.1    yamt 			int flags = PFSYNC_FLAG_STALE;
    523  1.2.58.1    yamt 
    524       1.1  itojun 			/* check for invalid values */
    525       1.1  itojun 			if (sp->timeout >= PFTM_MAX ||
    526       1.1  itojun 			    sp->src.state > PF_TCPS_PROXY_DST ||
    527       1.1  itojun 			    sp->dst.state > PF_TCPS_PROXY_DST) {
    528       1.1  itojun 				if (pf_status.debug >= PF_DEBUG_MISC)
    529       1.1  itojun 					printf("pfsync_insert: PFSYNC_ACT_UPD: "
    530       1.1  itojun 					    "invalid value\n");
    531  1.2.58.1    yamt 				PFSYNC_STATINC(PFSYNC_STAT_BADSTATE);
    532       1.1  itojun 				continue;
    533       1.1  itojun 			}
    534       1.1  itojun 
    535  1.2.58.1    yamt 			memcpy(&id_key.id, sp->id, sizeof(id_key.id));
    536  1.2.58.1    yamt 			id_key.creatorid = sp->creatorid;
    537       1.1  itojun 
    538  1.2.58.1    yamt 			st = pf_find_state_byid(&id_key);
    539       1.1  itojun 			if (st == NULL) {
    540       1.1  itojun 				/* insert the update */
    541  1.2.58.1    yamt 				if (pfsync_insert_net_state(sp, chksum_flag)) {
    542  1.2.58.1    yamt 					PFSYNC_STATINC(PFSYNC_STAT_BADSTATE);
    543  1.2.58.1    yamt 				}
    544  1.2.58.1    yamt 				continue;
    545  1.2.58.1    yamt 			}
    546  1.2.58.1    yamt 			sk = st->state_key;
    547  1.2.58.1    yamt 			sfail = 0;
    548  1.2.58.1    yamt 			if (sk->proto == IPPROTO_TCP) {
    549  1.2.58.1    yamt 				/*
    550  1.2.58.1    yamt 				 * The state should never go backwards except
    551  1.2.58.1    yamt 				 * for syn-proxy states.  Neither should the
    552  1.2.58.1    yamt 				 * sequence window slide backwards.
    553  1.2.58.1    yamt 				 */
    554  1.2.58.1    yamt 				if (st->src.state > sp->src.state &&
    555  1.2.58.1    yamt 				    (st->src.state < PF_TCPS_PROXY_SRC ||
    556  1.2.58.1    yamt 				    sp->src.state >= PF_TCPS_PROXY_SRC))
    557  1.2.58.1    yamt 					sfail = 1;
    558  1.2.58.1    yamt 				else if (SEQ_GT(st->src.seqlo,
    559  1.2.58.1    yamt 				    ntohl(sp->src.seqlo)))
    560  1.2.58.1    yamt 					sfail = 3;
    561  1.2.58.1    yamt 				else if (st->dst.state > sp->dst.state) {
    562  1.2.58.1    yamt 					/* There might still be useful
    563  1.2.58.1    yamt 					 * information about the src state here,
    564  1.2.58.1    yamt 					 * so import that part of the update,
    565  1.2.58.1    yamt 					 * then "fail" so we send the updated
    566  1.2.58.1    yamt 					 * state back to the peer who is missing
    567  1.2.58.1    yamt 					 * our what we know. */
    568  1.2.58.1    yamt 					pf_state_peer_ntoh(&sp->src, &st->src);
    569  1.2.58.1    yamt 					/* XXX do anything with timeouts? */
    570  1.2.58.1    yamt 					sfail = 7;
    571  1.2.58.1    yamt 					flags = 0;
    572  1.2.58.1    yamt 				} else if (st->dst.state >= TCPS_SYN_SENT &&
    573  1.2.58.1    yamt 				    SEQ_GT(st->dst.seqlo, ntohl(sp->dst.seqlo)))
    574  1.2.58.1    yamt 					sfail = 4;
    575  1.2.58.1    yamt 			} else {
    576  1.2.58.1    yamt 				/*
    577  1.2.58.1    yamt 				 * Non-TCP protocol state machine always go
    578  1.2.58.1    yamt 				 * forwards
    579  1.2.58.1    yamt 				 */
    580  1.2.58.1    yamt 				if (st->src.state > sp->src.state)
    581  1.2.58.1    yamt 					sfail = 5;
    582  1.2.58.1    yamt 				else if (st->dst.state > sp->dst.state)
    583  1.2.58.1    yamt 					sfail = 6;
    584  1.2.58.1    yamt 			}
    585  1.2.58.1    yamt 			if (sfail) {
    586  1.2.58.1    yamt 				if (pf_status.debug >= PF_DEBUG_MISC)
    587  1.2.58.1    yamt 					printf("pfsync: %s stale update "
    588  1.2.58.1    yamt 					    "(%d) id: %016" PRIu64 ""
    589  1.2.58.1    yamt 					    "creatorid: %08x\n",
    590  1.2.58.1    yamt 					    (sfail < 7 ?  "ignoring"
    591  1.2.58.1    yamt 					     : "partial"), sfail,
    592  1.2.58.1    yamt 					    be64toh(st->id),
    593  1.2.58.1    yamt 					    ntohl(st->creatorid));
    594  1.2.58.1    yamt 				PFSYNC_STATINC(PFSYNC_STAT_BADSTATE);
    595  1.2.58.1    yamt 
    596  1.2.58.1    yamt 				if (!(sp->sync_flags & PFSTATE_STALE)) {
    597  1.2.58.1    yamt 					/* we have a better state, send it */
    598  1.2.58.1    yamt 					if (sc->sc_mbuf != NULL && !stale)
    599  1.2.58.1    yamt 						pfsync_sendout(sc);
    600  1.2.58.1    yamt 					stale++;
    601  1.2.58.1    yamt 					if (!st->sync_flags)
    602  1.2.58.1    yamt 						pfsync_pack_state(
    603  1.2.58.1    yamt 						    PFSYNC_ACT_UPD, st, flags);
    604  1.2.58.1    yamt 				}
    605       1.1  itojun 				continue;
    606       1.1  itojun 			}
    607  1.2.58.1    yamt 	    		pfsync_alloc_scrub_memory(&sp->dst, &st->dst);
    608       1.1  itojun 			pf_state_peer_ntoh(&sp->src, &st->src);
    609       1.1  itojun 			pf_state_peer_ntoh(&sp->dst, &st->dst);
    610  1.2.58.1    yamt 			st->expire = ntohl(sp->expire) + time_second;
    611       1.1  itojun 			st->timeout = sp->timeout;
    612       1.1  itojun 		}
    613  1.2.58.1    yamt 		if (stale && sc->sc_mbuf != NULL)
    614  1.2.58.1    yamt 			pfsync_sendout(sc);
    615       1.1  itojun 		splx(s);
    616       1.1  itojun 		break;
    617       1.1  itojun 	/*
    618       1.1  itojun 	 * It's not strictly necessary for us to support the "uncompressed"
    619       1.1  itojun 	 * delete action, but it's relatively simple and maintains consistency.
    620       1.1  itojun 	 */
    621       1.1  itojun 	case PFSYNC_ACT_DEL:
    622       1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    623       1.1  itojun 		    count * sizeof(*sp), &offp)) == NULL) {
    624  1.2.58.1    yamt 			PFSYNC_STATINC(PFSYNC_STAT_BADLEN);
    625       1.1  itojun 			return;
    626       1.1  itojun 		}
    627       1.1  itojun 
    628       1.1  itojun 		s = splsoftnet();
    629       1.1  itojun 		for (i = 0, sp = (struct pfsync_state *)(mp->m_data + offp);
    630       1.1  itojun 		    i < count; i++, sp++) {
    631  1.2.58.1    yamt 			memcpy(&id_key.id, sp->id, sizeof(id_key.id));
    632  1.2.58.1    yamt 			id_key.creatorid = sp->creatorid;
    633       1.1  itojun 
    634  1.2.58.1    yamt 			st = pf_find_state_byid(&id_key);
    635       1.1  itojun 			if (st == NULL) {
    636  1.2.58.1    yamt 				PFSYNC_STATINC(PFSYNC_STAT_BADSTATE);
    637       1.1  itojun 				continue;
    638       1.1  itojun 			}
    639       1.1  itojun 			st->sync_flags |= PFSTATE_FROMSYNC;
    640  1.2.58.1    yamt 			pf_unlink_state(st);
    641       1.1  itojun 		}
    642       1.1  itojun 		splx(s);
    643       1.1  itojun 		break;
    644       1.1  itojun 	case PFSYNC_ACT_UPD_C: {
    645       1.1  itojun 		int update_requested = 0;
    646       1.1  itojun 
    647       1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    648       1.1  itojun 		    count * sizeof(*up), &offp)) == NULL) {
    649  1.2.58.1    yamt 			PFSYNC_STATINC(PFSYNC_STAT_BADLEN);
    650       1.1  itojun 			return;
    651       1.1  itojun 		}
    652       1.1  itojun 
    653       1.1  itojun 		s = splsoftnet();
    654       1.1  itojun 		for (i = 0, up = (struct pfsync_state_upd *)(mp->m_data + offp);
    655       1.1  itojun 		    i < count; i++, up++) {
    656       1.1  itojun 			/* check for invalid values */
    657       1.1  itojun 			if (up->timeout >= PFTM_MAX ||
    658       1.1  itojun 			    up->src.state > PF_TCPS_PROXY_DST ||
    659       1.1  itojun 			    up->dst.state > PF_TCPS_PROXY_DST) {
    660       1.1  itojun 				if (pf_status.debug >= PF_DEBUG_MISC)
    661       1.1  itojun 					printf("pfsync_insert: "
    662       1.1  itojun 					    "PFSYNC_ACT_UPD_C: "
    663       1.1  itojun 					    "invalid value\n");
    664  1.2.58.1    yamt 				PFSYNC_STATINC(PFSYNC_STAT_BADSTATE);
    665       1.1  itojun 				continue;
    666       1.1  itojun 			}
    667       1.1  itojun 
    668  1.2.58.1    yamt 			memcpy(&id_key.id, up->id, sizeof(id_key.id));
    669  1.2.58.1    yamt 			id_key.creatorid = up->creatorid;
    670       1.1  itojun 
    671  1.2.58.1    yamt 			st = pf_find_state_byid(&id_key);
    672       1.1  itojun 			if (st == NULL) {
    673       1.1  itojun 				/* We don't have this state. Ask for it. */
    674  1.2.58.1    yamt 				error = pfsync_request_update(up, &src);
    675  1.2.58.1    yamt 				if (error == ENOMEM) {
    676  1.2.58.1    yamt 					splx(s);
    677  1.2.58.1    yamt 					goto done;
    678  1.2.58.1    yamt 				}
    679       1.1  itojun 				update_requested = 1;
    680  1.2.58.1    yamt 				PFSYNC_STATINC(PFSYNC_STAT_BADSTATE);
    681       1.1  itojun 				continue;
    682       1.1  itojun 			}
    683  1.2.58.1    yamt 			sk = st->state_key;
    684  1.2.58.1    yamt 			sfail = 0;
    685  1.2.58.1    yamt 			if (sk->proto == IPPROTO_TCP) {
    686  1.2.58.1    yamt 				/*
    687  1.2.58.1    yamt 				 * The state should never go backwards except
    688  1.2.58.1    yamt 				 * for syn-proxy states.  Neither should the
    689  1.2.58.1    yamt 				 * sequence window slide backwards.
    690  1.2.58.1    yamt 				 */
    691  1.2.58.1    yamt 				if (st->src.state > up->src.state &&
    692  1.2.58.1    yamt 				    (st->src.state < PF_TCPS_PROXY_SRC ||
    693  1.2.58.1    yamt 				    up->src.state >= PF_TCPS_PROXY_SRC))
    694  1.2.58.1    yamt 					sfail = 1;
    695  1.2.58.1    yamt 				else if (st->dst.state > up->dst.state)
    696  1.2.58.1    yamt 					sfail = 2;
    697  1.2.58.1    yamt 				else if (SEQ_GT(st->src.seqlo,
    698  1.2.58.1    yamt 				    ntohl(up->src.seqlo)))
    699  1.2.58.1    yamt 					sfail = 3;
    700  1.2.58.1    yamt 				else if (st->dst.state >= TCPS_SYN_SENT &&
    701  1.2.58.1    yamt 				    SEQ_GT(st->dst.seqlo, ntohl(up->dst.seqlo)))
    702  1.2.58.1    yamt 					sfail = 4;
    703  1.2.58.1    yamt 			} else {
    704  1.2.58.1    yamt 				/*
    705  1.2.58.1    yamt 				 * Non-TCP protocol state machine always go
    706  1.2.58.1    yamt 				 * forwards
    707  1.2.58.1    yamt 				 */
    708  1.2.58.1    yamt 				if (st->src.state > up->src.state)
    709  1.2.58.1    yamt 					sfail = 5;
    710  1.2.58.1    yamt 				else if (st->dst.state > up->dst.state)
    711  1.2.58.1    yamt 					sfail = 6;
    712  1.2.58.1    yamt 			}
    713  1.2.58.1    yamt 			if (sfail) {
    714  1.2.58.1    yamt 				if (pf_status.debug >= PF_DEBUG_MISC)
    715  1.2.58.1    yamt 					printf("pfsync: ignoring stale update "
    716  1.2.58.1    yamt 					    "(%d) id: %016" PRIu64 ""
    717  1.2.58.1    yamt 					    "creatorid: %08x\n", sfail,
    718  1.2.58.1    yamt 					    be64toh(st->id),
    719  1.2.58.1    yamt 					    ntohl(st->creatorid));
    720  1.2.58.1    yamt 				PFSYNC_STATINC(PFSYNC_STAT_BADSTATE);
    721  1.2.58.1    yamt 
    722  1.2.58.1    yamt 				/* we have a better state, send it out */
    723  1.2.58.1    yamt 				if ((!stale || update_requested) &&
    724  1.2.58.1    yamt 				    sc->sc_mbuf != NULL) {
    725  1.2.58.1    yamt 					pfsync_sendout(sc);
    726  1.2.58.1    yamt 					update_requested = 0;
    727  1.2.58.1    yamt 				}
    728  1.2.58.1    yamt 				stale++;
    729  1.2.58.1    yamt 				if (!st->sync_flags)
    730  1.2.58.1    yamt 					pfsync_pack_state(PFSYNC_ACT_UPD, st,
    731  1.2.58.1    yamt 					    PFSYNC_FLAG_STALE);
    732  1.2.58.1    yamt 				continue;
    733  1.2.58.1    yamt 			}
    734  1.2.58.1    yamt 	    		pfsync_alloc_scrub_memory(&up->dst, &st->dst);
    735       1.1  itojun 			pf_state_peer_ntoh(&up->src, &st->src);
    736       1.1  itojun 			pf_state_peer_ntoh(&up->dst, &st->dst);
    737  1.2.58.1    yamt 			st->expire = ntohl(up->expire) + time_second;
    738       1.1  itojun 			st->timeout = up->timeout;
    739       1.1  itojun 		}
    740  1.2.58.1    yamt 		if ((update_requested || stale) && sc->sc_mbuf)
    741       1.1  itojun 			pfsync_sendout(sc);
    742       1.1  itojun 		splx(s);
    743       1.1  itojun 		break;
    744       1.1  itojun 	}
    745       1.1  itojun 	case PFSYNC_ACT_DEL_C:
    746       1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    747       1.1  itojun 		    count * sizeof(*dp), &offp)) == NULL) {
    748  1.2.58.1    yamt 			PFSYNC_STATINC(PFSYNC_STAT_BADLEN);
    749       1.1  itojun 			return;
    750       1.1  itojun 		}
    751       1.1  itojun 
    752       1.1  itojun 		s = splsoftnet();
    753       1.1  itojun 		for (i = 0, dp = (struct pfsync_state_del *)(mp->m_data + offp);
    754       1.1  itojun 		    i < count; i++, dp++) {
    755  1.2.58.1    yamt 			memcpy(&id_key.id, dp->id, sizeof(id_key.id));
    756  1.2.58.1    yamt 			id_key.creatorid = dp->creatorid;
    757       1.1  itojun 
    758  1.2.58.1    yamt 			st = pf_find_state_byid(&id_key);
    759       1.1  itojun 			if (st == NULL) {
    760  1.2.58.1    yamt 				PFSYNC_STATINC(PFSYNC_STAT_BADSTATE);
    761       1.1  itojun 				continue;
    762       1.1  itojun 			}
    763       1.1  itojun 			st->sync_flags |= PFSTATE_FROMSYNC;
    764  1.2.58.1    yamt 			pf_unlink_state(st);
    765       1.1  itojun 		}
    766       1.1  itojun 		splx(s);
    767       1.1  itojun 		break;
    768       1.1  itojun 	case PFSYNC_ACT_INS_F:
    769       1.1  itojun 	case PFSYNC_ACT_DEL_F:
    770       1.1  itojun 		/* not implemented */
    771       1.1  itojun 		break;
    772       1.1  itojun 	case PFSYNC_ACT_UREQ:
    773       1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    774       1.1  itojun 		    count * sizeof(*rup), &offp)) == NULL) {
    775  1.2.58.1    yamt 			PFSYNC_STATINC(PFSYNC_STAT_BADLEN);
    776       1.1  itojun 			return;
    777       1.1  itojun 		}
    778       1.1  itojun 
    779       1.1  itojun 		s = splsoftnet();
    780       1.1  itojun 		if (sc->sc_mbuf != NULL)
    781       1.1  itojun 			pfsync_sendout(sc);
    782       1.1  itojun 		for (i = 0,
    783       1.1  itojun 		    rup = (struct pfsync_state_upd_req *)(mp->m_data + offp);
    784       1.1  itojun 		    i < count; i++, rup++) {
    785  1.2.58.1    yamt 			memcpy(&id_key.id, rup->id, sizeof(id_key.id));
    786  1.2.58.1    yamt 			id_key.creatorid = rup->creatorid;
    787       1.1  itojun 
    788  1.2.58.1    yamt 			if (id_key.id == 0 && id_key.creatorid == 0) {
    789  1.2.58.1    yamt 				sc->sc_ureq_received = time_uptime;
    790  1.2.58.1    yamt 				if (sc->sc_bulk_send_next == NULL)
    791  1.2.58.1    yamt 					sc->sc_bulk_send_next =
    792  1.2.58.1    yamt 					    TAILQ_FIRST(&state_list);
    793  1.2.58.1    yamt 				sc->sc_bulk_terminator = sc->sc_bulk_send_next;
    794       1.1  itojun 				if (pf_status.debug >= PF_DEBUG_MISC)
    795       1.1  itojun 					printf("pfsync: received "
    796       1.1  itojun 					    "bulk update request\n");
    797       1.1  itojun 				pfsync_send_bus(sc, PFSYNC_BUS_START);
    798  1.2.58.1    yamt 				callout_schedule(&sc->sc_bulk_tmo, 1 * hz);
    799       1.1  itojun 			} else {
    800  1.2.58.1    yamt 				st = pf_find_state_byid(&id_key);
    801       1.1  itojun 				if (st == NULL) {
    802  1.2.58.1    yamt 					PFSYNC_STATINC(PFSYNC_STAT_BADSTATE);
    803       1.1  itojun 					continue;
    804       1.1  itojun 				}
    805  1.2.58.1    yamt 				if (!st->sync_flags)
    806  1.2.58.1    yamt 					pfsync_pack_state(PFSYNC_ACT_UPD,
    807  1.2.58.1    yamt 					    st, 0);
    808       1.1  itojun 			}
    809       1.1  itojun 		}
    810       1.1  itojun 		if (sc->sc_mbuf != NULL)
    811       1.1  itojun 			pfsync_sendout(sc);
    812       1.1  itojun 		splx(s);
    813       1.1  itojun 		break;
    814       1.1  itojun 	case PFSYNC_ACT_BUS:
    815       1.1  itojun 		/* If we're not waiting for a bulk update, who cares. */
    816       1.1  itojun 		if (sc->sc_ureq_sent == 0)
    817       1.1  itojun 			break;
    818       1.1  itojun 
    819       1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    820       1.1  itojun 		    sizeof(*bus), &offp)) == NULL) {
    821  1.2.58.1    yamt 			PFSYNC_STATINC(PFSYNC_STAT_BADLEN);
    822       1.1  itojun 			return;
    823       1.1  itojun 		}
    824       1.1  itojun 		bus = (struct pfsync_state_bus *)(mp->m_data + offp);
    825       1.1  itojun 		switch (bus->status) {
    826       1.1  itojun 		case PFSYNC_BUS_START:
    827  1.2.58.1    yamt 			callout_schedule(&sc->sc_bulkfail_tmo,
    828       1.1  itojun 			    pf_pool_limits[PF_LIMIT_STATES].limit /
    829       1.1  itojun 			    (PFSYNC_BULKPACKETS * sc->sc_maxcount));
    830       1.1  itojun 			if (pf_status.debug >= PF_DEBUG_MISC)
    831       1.1  itojun 				printf("pfsync: received bulk "
    832       1.1  itojun 				    "update start\n");
    833       1.1  itojun 			break;
    834       1.1  itojun 		case PFSYNC_BUS_END:
    835  1.2.58.1    yamt 			if (time_uptime - ntohl(bus->endtime) >=
    836       1.1  itojun 			    sc->sc_ureq_sent) {
    837       1.1  itojun 				/* that's it, we're happy */
    838       1.1  itojun 				sc->sc_ureq_sent = 0;
    839       1.1  itojun 				sc->sc_bulk_tries = 0;
    840  1.2.58.1    yamt 				callout_stop(&sc->sc_bulkfail_tmo);
    841  1.2.58.1    yamt #if NCARP > 0
    842  1.2.58.1    yamt 				if (!pfsync_sync_ok)
    843  1.2.58.1    yamt 					carp_suppress_preempt--;
    844  1.2.58.1    yamt #endif
    845       1.1  itojun 				pfsync_sync_ok = 1;
    846       1.1  itojun 				if (pf_status.debug >= PF_DEBUG_MISC)
    847       1.1  itojun 					printf("pfsync: received valid "
    848       1.1  itojun 					    "bulk update end\n");
    849       1.1  itojun 			} else {
    850       1.1  itojun 				if (pf_status.debug >= PF_DEBUG_MISC)
    851       1.1  itojun 					printf("pfsync: received invalid "
    852       1.1  itojun 					    "bulk update end: bad timestamp\n");
    853       1.1  itojun 			}
    854       1.1  itojun 			break;
    855       1.1  itojun 		}
    856       1.1  itojun 		break;
    857  1.2.58.1    yamt #ifdef IPSEC
    858  1.2.58.1    yamt 	case PFSYNC_ACT_TDB_UPD:
    859  1.2.58.1    yamt 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    860  1.2.58.1    yamt 		    count * sizeof(*pt), &offp)) == NULL) {
    861  1.2.58.1    yamt 			PFSYNC_STATINC(PFSYNC_STAT_BADLEN);
    862  1.2.58.1    yamt 			return;
    863  1.2.58.1    yamt 		}
    864  1.2.58.1    yamt 		s = splsoftnet();
    865  1.2.58.1    yamt 		for (i = 0, pt = (struct pfsync_tdb *)(mp->m_data + offp);
    866  1.2.58.1    yamt 		    i < count; i++, pt++)
    867  1.2.58.1    yamt 			pfsync_update_net_tdb(pt);
    868  1.2.58.1    yamt 		splx(s);
    869  1.2.58.1    yamt 		break;
    870  1.2.58.1    yamt #endif
    871       1.1  itojun 	}
    872       1.1  itojun 
    873       1.1  itojun done:
    874       1.1  itojun 	if (m)
    875       1.1  itojun 		m_freem(m);
    876       1.1  itojun }
    877       1.1  itojun 
    878       1.1  itojun int
    879  1.2.58.1    yamt pfsyncoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    880       1.1  itojun 	struct rtentry *rt)
    881       1.1  itojun {
    882       1.1  itojun 	m_freem(m);
    883       1.1  itojun 	return (0);
    884       1.1  itojun }
    885       1.1  itojun 
    886       1.1  itojun /* ARGSUSED */
    887       1.1  itojun int
    888  1.2.58.1    yamt pfsyncioctl(struct ifnet *ifp, u_long cmd, void*  data)
    889       1.1  itojun {
    890  1.2.58.1    yamt 	struct lwp *l = curlwp;
    891       1.1  itojun 	struct pfsync_softc *sc = ifp->if_softc;
    892       1.1  itojun 	struct ifreq *ifr = (struct ifreq *)data;
    893       1.1  itojun 	struct ip_moptions *imo = &sc->sc_imo;
    894       1.1  itojun 	struct pfsyncreq pfsyncr;
    895       1.1  itojun 	struct ifnet    *sifp;
    896       1.1  itojun 	int s, error;
    897       1.1  itojun 
    898       1.1  itojun 	switch (cmd) {
    899       1.1  itojun 	case SIOCSIFADDR:
    900       1.1  itojun 	case SIOCAIFADDR:
    901       1.1  itojun 	case SIOCSIFDSTADDR:
    902       1.1  itojun 	case SIOCSIFFLAGS:
    903       1.1  itojun 		if (ifp->if_flags & IFF_UP)
    904       1.1  itojun 			ifp->if_flags |= IFF_RUNNING;
    905       1.1  itojun 		else
    906       1.1  itojun 			ifp->if_flags &= ~IFF_RUNNING;
    907       1.1  itojun 		break;
    908       1.1  itojun 	case SIOCSIFMTU:
    909       1.1  itojun 		if (ifr->ifr_mtu < PFSYNC_MINMTU)
    910       1.1  itojun 			return (EINVAL);
    911       1.1  itojun 		if (ifr->ifr_mtu > MCLBYTES)
    912       1.1  itojun 			ifr->ifr_mtu = MCLBYTES;
    913       1.1  itojun 		s = splnet();
    914       1.1  itojun 		if (ifr->ifr_mtu < ifp->if_mtu)
    915       1.1  itojun 			pfsync_sendout(sc);
    916       1.1  itojun 		pfsync_setmtu(sc, ifr->ifr_mtu);
    917       1.1  itojun 		splx(s);
    918       1.1  itojun 		break;
    919       1.1  itojun 	case SIOCGETPFSYNC:
    920  1.2.58.1    yamt 		if ((error = kauth_authorize_network(l->l_cred,
    921  1.2.58.1    yamt 		    KAUTH_NETWORK_INTERFACE,
    922  1.2.58.1    yamt 		    KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, ifp, (void *)cmd,
    923  1.2.58.1    yamt 		    NULL)) != 0)
    924  1.2.58.1    yamt 			return (error);
    925  1.2.58.1    yamt 		memset(&pfsyncr, 0, sizeof(pfsyncr));
    926       1.1  itojun 		if (sc->sc_sync_ifp)
    927  1.2.58.1    yamt 			strlcpy(pfsyncr.pfsyncr_syncdev,
    928       1.1  itojun 			    sc->sc_sync_ifp->if_xname, IFNAMSIZ);
    929  1.2.58.1    yamt 		pfsyncr.pfsyncr_syncpeer = sc->sc_sync_peer;
    930       1.1  itojun 		pfsyncr.pfsyncr_maxupdates = sc->sc_maxupdates;
    931       1.1  itojun 		if ((error = copyout(&pfsyncr, ifr->ifr_data, sizeof(pfsyncr))))
    932       1.1  itojun 			return (error);
    933       1.1  itojun 		break;
    934       1.1  itojun 	case SIOCSETPFSYNC:
    935  1.2.58.1    yamt 		if ((error = kauth_authorize_network(l->l_cred,
    936  1.2.58.1    yamt 		    KAUTH_NETWORK_INTERFACE,
    937  1.2.58.1    yamt 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    938  1.2.58.1    yamt 		    NULL)) != 0)
    939       1.1  itojun 			return (error);
    940       1.1  itojun 		if ((error = copyin(ifr->ifr_data, &pfsyncr, sizeof(pfsyncr))))
    941       1.1  itojun 			return (error);
    942       1.1  itojun 
    943  1.2.58.1    yamt 		if (pfsyncr.pfsyncr_syncpeer.s_addr == 0)
    944  1.2.58.1    yamt 			sc->sc_sync_peer.s_addr = INADDR_PFSYNC_GROUP;
    945  1.2.58.1    yamt 		else
    946  1.2.58.1    yamt 			sc->sc_sync_peer.s_addr =
    947  1.2.58.1    yamt 			    pfsyncr.pfsyncr_syncpeer.s_addr;
    948  1.2.58.1    yamt 
    949       1.1  itojun 		if (pfsyncr.pfsyncr_maxupdates > 255)
    950       1.1  itojun 			return (EINVAL);
    951       1.1  itojun 		sc->sc_maxupdates = pfsyncr.pfsyncr_maxupdates;
    952       1.1  itojun 
    953  1.2.58.1    yamt 		if (pfsyncr.pfsyncr_syncdev[0] == 0) {
    954       1.1  itojun 			sc->sc_sync_ifp = NULL;
    955       1.1  itojun 			if (sc->sc_mbuf_net != NULL) {
    956       1.1  itojun 				/* Don't keep stale pfsync packets around. */
    957       1.1  itojun 				s = splnet();
    958       1.1  itojun 				m_freem(sc->sc_mbuf_net);
    959       1.1  itojun 				sc->sc_mbuf_net = NULL;
    960       1.1  itojun 				sc->sc_statep_net.s = NULL;
    961       1.1  itojun 				splx(s);
    962       1.1  itojun 			}
    963  1.2.58.1    yamt 			if (imo->imo_num_memberships > 0) {
    964  1.2.58.1    yamt 				in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
    965  1.2.58.1    yamt 				imo->imo_multicast_ifp = NULL;
    966  1.2.58.1    yamt 			}
    967       1.1  itojun 			break;
    968       1.1  itojun 		}
    969  1.2.58.1    yamt 
    970  1.2.58.1    yamt 		if ((sifp = ifunit(pfsyncr.pfsyncr_syncdev)) == NULL)
    971       1.1  itojun 			return (EINVAL);
    972       1.1  itojun 
    973       1.1  itojun 		s = splnet();
    974       1.1  itojun 		if (sifp->if_mtu < sc->sc_if.if_mtu ||
    975       1.1  itojun 		    (sc->sc_sync_ifp != NULL &&
    976       1.1  itojun 		    sifp->if_mtu < sc->sc_sync_ifp->if_mtu) ||
    977       1.1  itojun 		    sifp->if_mtu < MCLBYTES - sizeof(struct ip))
    978       1.1  itojun 			pfsync_sendout(sc);
    979       1.1  itojun 		sc->sc_sync_ifp = sifp;
    980       1.1  itojun 
    981       1.1  itojun 		pfsync_setmtu(sc, sc->sc_if.if_mtu);
    982       1.1  itojun 
    983       1.1  itojun 		if (imo->imo_num_memberships > 0) {
    984       1.1  itojun 			in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
    985       1.1  itojun 			imo->imo_multicast_ifp = NULL;
    986       1.1  itojun 		}
    987       1.1  itojun 
    988  1.2.58.1    yamt 		if (sc->sc_sync_ifp &&
    989  1.2.58.1    yamt 		    sc->sc_sync_peer.s_addr == INADDR_PFSYNC_GROUP) {
    990       1.1  itojun 			struct in_addr addr;
    991       1.1  itojun 
    992  1.2.58.1    yamt 			if (!(sc->sc_sync_ifp->if_flags & IFF_MULTICAST)) {
    993  1.2.58.1    yamt 				sc->sc_sync_ifp = NULL;
    994  1.2.58.1    yamt 				splx(s);
    995  1.2.58.1    yamt 				return (EADDRNOTAVAIL);
    996  1.2.58.1    yamt 			}
    997  1.2.58.1    yamt 
    998       1.1  itojun 			addr.s_addr = INADDR_PFSYNC_GROUP;
    999  1.2.58.1    yamt 
   1000       1.1  itojun 			if ((imo->imo_membership[0] =
   1001       1.1  itojun 			    in_addmulti(&addr, sc->sc_sync_ifp)) == NULL) {
   1002  1.2.58.1    yamt 				sc->sc_sync_ifp = NULL;
   1003       1.1  itojun 				splx(s);
   1004       1.1  itojun 				return (ENOBUFS);
   1005       1.1  itojun 			}
   1006       1.1  itojun 			imo->imo_num_memberships++;
   1007       1.1  itojun 			imo->imo_multicast_ifp = sc->sc_sync_ifp;
   1008       1.1  itojun 			imo->imo_multicast_ttl = PFSYNC_DFLTTL;
   1009       1.1  itojun 			imo->imo_multicast_loop = 0;
   1010  1.2.58.1    yamt 		}
   1011       1.1  itojun 
   1012  1.2.58.1    yamt 		if (sc->sc_sync_ifp ||
   1013  1.2.58.1    yamt 		    sc->sc_sendaddr.s_addr != INADDR_PFSYNC_GROUP) {
   1014       1.1  itojun 			/* Request a full state table update. */
   1015  1.2.58.1    yamt 			sc->sc_ureq_sent = time_uptime;
   1016  1.2.58.1    yamt #if NCARP > 0
   1017  1.2.58.1    yamt 			if (pfsync_sync_ok)
   1018  1.2.58.1    yamt 				carp_suppress_preempt ++;
   1019  1.2.58.1    yamt #endif
   1020       1.1  itojun 			pfsync_sync_ok = 0;
   1021       1.1  itojun 			if (pf_status.debug >= PF_DEBUG_MISC)
   1022       1.1  itojun 				printf("pfsync: requesting bulk update\n");
   1023  1.2.58.1    yamt 			callout_schedule(&sc->sc_bulkfail_tmo, 5 * hz);
   1024  1.2.58.1    yamt 			error = pfsync_request_update(NULL, NULL);
   1025  1.2.58.1    yamt 			if (error == ENOMEM) {
   1026  1.2.58.1    yamt 				splx(s);
   1027  1.2.58.1    yamt 				return (ENOMEM);
   1028  1.2.58.1    yamt 			}
   1029       1.1  itojun 			pfsync_sendout(sc);
   1030       1.1  itojun 		}
   1031       1.1  itojun 		splx(s);
   1032       1.1  itojun 
   1033       1.1  itojun 		break;
   1034       1.1  itojun 
   1035       1.1  itojun 	default:
   1036  1.2.58.1    yamt 		return ifioctl_common(ifp, cmd, data);
   1037       1.1  itojun 	}
   1038       1.1  itojun 
   1039       1.1  itojun 	return (0);
   1040       1.1  itojun }
   1041       1.1  itojun 
   1042       1.1  itojun void
   1043       1.1  itojun pfsync_setmtu(struct pfsync_softc *sc, int mtu_req)
   1044       1.1  itojun {
   1045       1.1  itojun 	int mtu;
   1046       1.1  itojun 
   1047       1.1  itojun 	if (sc->sc_sync_ifp && sc->sc_sync_ifp->if_mtu < mtu_req)
   1048       1.1  itojun 		mtu = sc->sc_sync_ifp->if_mtu;
   1049       1.1  itojun 	else
   1050       1.1  itojun 		mtu = mtu_req;
   1051       1.1  itojun 
   1052       1.1  itojun 	sc->sc_maxcount = (mtu - sizeof(struct pfsync_header)) /
   1053       1.1  itojun 	    sizeof(struct pfsync_state);
   1054       1.1  itojun 	if (sc->sc_maxcount > 254)
   1055       1.1  itojun 	    sc->sc_maxcount = 254;
   1056       1.1  itojun 	sc->sc_if.if_mtu = sizeof(struct pfsync_header) +
   1057       1.1  itojun 	    sc->sc_maxcount * sizeof(struct pfsync_state);
   1058       1.1  itojun }
   1059       1.1  itojun 
   1060       1.1  itojun struct mbuf *
   1061       1.1  itojun pfsync_get_mbuf(struct pfsync_softc *sc, u_int8_t action, void **sp)
   1062       1.1  itojun {
   1063       1.1  itojun 	struct pfsync_header *h;
   1064       1.1  itojun 	struct mbuf *m;
   1065       1.1  itojun 	int len;
   1066       1.1  itojun 
   1067       1.1  itojun 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1068       1.1  itojun 	if (m == NULL) {
   1069       1.1  itojun 		sc->sc_if.if_oerrors++;
   1070       1.1  itojun 		return (NULL);
   1071       1.1  itojun 	}
   1072       1.1  itojun 
   1073       1.1  itojun 	switch (action) {
   1074       1.1  itojun 	case PFSYNC_ACT_CLR:
   1075       1.1  itojun 		len = sizeof(struct pfsync_header) +
   1076       1.1  itojun 		    sizeof(struct pfsync_state_clr);
   1077       1.1  itojun 		break;
   1078       1.1  itojun 	case PFSYNC_ACT_UPD_C:
   1079       1.1  itojun 		len = (sc->sc_maxcount * sizeof(struct pfsync_state_upd)) +
   1080       1.1  itojun 		    sizeof(struct pfsync_header);
   1081       1.1  itojun 		break;
   1082       1.1  itojun 	case PFSYNC_ACT_DEL_C:
   1083       1.1  itojun 		len = (sc->sc_maxcount * sizeof(struct pfsync_state_del)) +
   1084       1.1  itojun 		    sizeof(struct pfsync_header);
   1085       1.1  itojun 		break;
   1086       1.1  itojun 	case PFSYNC_ACT_UREQ:
   1087       1.1  itojun 		len = (sc->sc_maxcount * sizeof(struct pfsync_state_upd_req)) +
   1088       1.1  itojun 		    sizeof(struct pfsync_header);
   1089       1.1  itojun 		break;
   1090       1.1  itojun 	case PFSYNC_ACT_BUS:
   1091       1.1  itojun 		len = sizeof(struct pfsync_header) +
   1092       1.1  itojun 		    sizeof(struct pfsync_state_bus);
   1093       1.1  itojun 		break;
   1094  1.2.58.1    yamt 	case PFSYNC_ACT_TDB_UPD:
   1095  1.2.58.1    yamt 		len = (sc->sc_maxcount * sizeof(struct pfsync_tdb)) +
   1096  1.2.58.1    yamt 		    sizeof(struct pfsync_header);
   1097  1.2.58.1    yamt 		break;
   1098       1.1  itojun 	default:
   1099       1.1  itojun 		len = (sc->sc_maxcount * sizeof(struct pfsync_state)) +
   1100       1.1  itojun 		    sizeof(struct pfsync_header);
   1101       1.1  itojun 		break;
   1102       1.1  itojun 	}
   1103       1.1  itojun 
   1104       1.1  itojun 	if (len > MHLEN) {
   1105       1.1  itojun 		MCLGET(m, M_DONTWAIT);
   1106       1.1  itojun 		if ((m->m_flags & M_EXT) == 0) {
   1107       1.1  itojun 			m_free(m);
   1108       1.1  itojun 			sc->sc_if.if_oerrors++;
   1109       1.1  itojun 			return (NULL);
   1110       1.1  itojun 		}
   1111       1.1  itojun 		m->m_data += (MCLBYTES - len) &~ (sizeof(long) - 1);
   1112       1.1  itojun 	} else
   1113       1.1  itojun 		MH_ALIGN(m, len);
   1114       1.1  itojun 
   1115       1.1  itojun 	m->m_pkthdr.rcvif = NULL;
   1116       1.1  itojun 	m->m_pkthdr.len = m->m_len = sizeof(struct pfsync_header);
   1117       1.1  itojun 	h = mtod(m, struct pfsync_header *);
   1118       1.1  itojun 	h->version = PFSYNC_VERSION;
   1119       1.1  itojun 	h->af = 0;
   1120       1.1  itojun 	h->count = 0;
   1121       1.1  itojun 	h->action = action;
   1122  1.2.58.1    yamt 	if (action != PFSYNC_ACT_TDB_UPD)
   1123  1.2.58.1    yamt 		memcpy(&h->pf_chksum, &pf_status.pf_chksum,
   1124  1.2.58.1    yamt 		    PF_MD5_DIGEST_LENGTH);
   1125       1.1  itojun 
   1126       1.1  itojun 	*sp = (void *)((char *)h + PFSYNC_HDRLEN);
   1127  1.2.58.1    yamt 	if (action == PFSYNC_ACT_TDB_UPD)
   1128  1.2.58.1    yamt 		callout_schedule(&sc->sc_tdb_tmo, hz);
   1129  1.2.58.1    yamt 	else
   1130  1.2.58.1    yamt 		callout_schedule(&sc->sc_tmo, hz);
   1131       1.1  itojun 	return (m);
   1132       1.1  itojun }
   1133       1.1  itojun 
   1134       1.1  itojun int
   1135  1.2.58.1    yamt pfsync_pack_state(u_int8_t action, struct pf_state *st, int flags)
   1136       1.1  itojun {
   1137  1.2.58.1    yamt 	struct ifnet *ifp = NULL;
   1138  1.2.58.1    yamt 	struct pfsync_softc *sc = pfsyncif;
   1139       1.1  itojun 	struct pfsync_header *h, *h_net;
   1140       1.1  itojun 	struct pfsync_state *sp = NULL;
   1141       1.1  itojun 	struct pfsync_state_upd *up = NULL;
   1142       1.1  itojun 	struct pfsync_state_del *dp = NULL;
   1143  1.2.58.1    yamt 	struct pf_state_key *sk = st->state_key;
   1144       1.1  itojun 	struct pf_rule *r;
   1145       1.1  itojun 	u_long secs;
   1146       1.1  itojun 	int s, ret = 0;
   1147       1.1  itojun 	u_int8_t i = 255, newaction = 0;
   1148       1.1  itojun 
   1149  1.2.58.1    yamt 	if (sc == NULL)
   1150  1.2.58.1    yamt 		return (0);
   1151  1.2.58.1    yamt 	ifp = &sc->sc_if;
   1152  1.2.58.1    yamt 
   1153       1.1  itojun 	/*
   1154       1.1  itojun 	 * If a packet falls in the forest and there's nobody around to
   1155       1.1  itojun 	 * hear, does it make a sound?
   1156       1.1  itojun 	 */
   1157  1.2.58.1    yamt 	if (ifp->if_bpf == NULL && sc->sc_sync_ifp == NULL &&
   1158  1.2.58.1    yamt 	    sc->sc_sync_peer.s_addr == INADDR_PFSYNC_GROUP) {
   1159       1.1  itojun 		/* Don't leave any stale pfsync packets hanging around. */
   1160       1.1  itojun 		if (sc->sc_mbuf != NULL) {
   1161       1.1  itojun 			m_freem(sc->sc_mbuf);
   1162       1.1  itojun 			sc->sc_mbuf = NULL;
   1163       1.1  itojun 			sc->sc_statep.s = NULL;
   1164       1.1  itojun 		}
   1165       1.1  itojun 		return (0);
   1166       1.1  itojun 	}
   1167       1.1  itojun 
   1168       1.1  itojun 	if (action >= PFSYNC_ACT_MAX)
   1169       1.1  itojun 		return (EINVAL);
   1170       1.1  itojun 
   1171       1.1  itojun 	s = splnet();
   1172       1.1  itojun 	if (sc->sc_mbuf == NULL) {
   1173       1.1  itojun 		if ((sc->sc_mbuf = pfsync_get_mbuf(sc, action,
   1174       1.1  itojun 		    (void *)&sc->sc_statep.s)) == NULL) {
   1175       1.1  itojun 			splx(s);
   1176       1.1  itojun 			return (ENOMEM);
   1177       1.1  itojun 		}
   1178       1.1  itojun 		h = mtod(sc->sc_mbuf, struct pfsync_header *);
   1179       1.1  itojun 	} else {
   1180       1.1  itojun 		h = mtod(sc->sc_mbuf, struct pfsync_header *);
   1181       1.1  itojun 		if (h->action != action) {
   1182       1.1  itojun 			pfsync_sendout(sc);
   1183       1.1  itojun 			if ((sc->sc_mbuf = pfsync_get_mbuf(sc, action,
   1184       1.1  itojun 			    (void *)&sc->sc_statep.s)) == NULL) {
   1185       1.1  itojun 				splx(s);
   1186       1.1  itojun 				return (ENOMEM);
   1187       1.1  itojun 			}
   1188       1.1  itojun 			h = mtod(sc->sc_mbuf, struct pfsync_header *);
   1189       1.1  itojun 		} else {
   1190       1.1  itojun 			/*
   1191       1.1  itojun 			 * If it's an update, look in the packet to see if
   1192       1.1  itojun 			 * we already have an update for the state.
   1193       1.1  itojun 			 */
   1194       1.1  itojun 			if (action == PFSYNC_ACT_UPD && sc->sc_maxupdates) {
   1195       1.1  itojun 				struct pfsync_state *usp =
   1196       1.1  itojun 				    (void *)((char *)h + PFSYNC_HDRLEN);
   1197       1.1  itojun 
   1198       1.1  itojun 				for (i = 0; i < h->count; i++) {
   1199       1.1  itojun 					if (!memcmp(usp->id, &st->id,
   1200       1.1  itojun 					    PFSYNC_ID_LEN) &&
   1201       1.1  itojun 					    usp->creatorid == st->creatorid) {
   1202       1.1  itojun 						sp = usp;
   1203       1.1  itojun 						sp->updates++;
   1204       1.1  itojun 						break;
   1205       1.1  itojun 					}
   1206       1.1  itojun 					usp++;
   1207       1.1  itojun 				}
   1208       1.1  itojun 			}
   1209       1.1  itojun 		}
   1210       1.1  itojun 	}
   1211       1.1  itojun 
   1212  1.2.58.1    yamt 	secs = time_second;
   1213       1.1  itojun 
   1214  1.2.58.1    yamt 	st->pfsync_time = time_uptime;
   1215       1.1  itojun 
   1216       1.1  itojun 	if (sp == NULL) {
   1217       1.1  itojun 		/* not a "duplicate" update */
   1218       1.1  itojun 		i = 255;
   1219       1.1  itojun 		sp = sc->sc_statep.s++;
   1220       1.1  itojun 		sc->sc_mbuf->m_pkthdr.len =
   1221       1.1  itojun 		    sc->sc_mbuf->m_len += sizeof(struct pfsync_state);
   1222       1.1  itojun 		h->count++;
   1223  1.2.58.1    yamt 		memset(sp, 0, sizeof(*sp));
   1224       1.1  itojun 
   1225  1.2.58.1    yamt 		memcpy(sp->id, &st->id, sizeof(sp->id));
   1226       1.1  itojun 		sp->creatorid = st->creatorid;
   1227       1.1  itojun 
   1228  1.2.58.1    yamt 		strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname));
   1229  1.2.58.1    yamt 		pf_state_host_hton(&sk->lan, &sp->lan);
   1230  1.2.58.1    yamt 		pf_state_host_hton(&sk->gwy, &sp->gwy);
   1231  1.2.58.1    yamt 		pf_state_host_hton(&sk->ext, &sp->ext);
   1232       1.1  itojun 
   1233  1.2.58.1    yamt 		memcpy(&sp->rt_addr, &st->rt_addr, sizeof(sp->rt_addr));
   1234       1.1  itojun 
   1235       1.1  itojun 		sp->creation = htonl(secs - st->creation);
   1236  1.2.58.1    yamt 		pf_state_counter_hton(st->packets[0], sp->packets[0]);
   1237  1.2.58.1    yamt 		pf_state_counter_hton(st->packets[1], sp->packets[1]);
   1238  1.2.58.1    yamt 		pf_state_counter_hton(st->bytes[0], sp->bytes[0]);
   1239  1.2.58.1    yamt 		pf_state_counter_hton(st->bytes[1], sp->bytes[1]);
   1240       1.1  itojun 		if ((r = st->rule.ptr) == NULL)
   1241       1.1  itojun 			sp->rule = htonl(-1);
   1242       1.1  itojun 		else
   1243       1.1  itojun 			sp->rule = htonl(r->nr);
   1244       1.1  itojun 		if ((r = st->anchor.ptr) == NULL)
   1245       1.1  itojun 			sp->anchor = htonl(-1);
   1246       1.1  itojun 		else
   1247       1.1  itojun 			sp->anchor = htonl(r->nr);
   1248  1.2.58.1    yamt 		sp->af = sk->af;
   1249  1.2.58.1    yamt 		sp->proto = sk->proto;
   1250  1.2.58.1    yamt 		sp->direction = sk->direction;
   1251       1.1  itojun 		sp->log = st->log;
   1252       1.1  itojun 		sp->allow_opts = st->allow_opts;
   1253       1.1  itojun 		sp->timeout = st->timeout;
   1254       1.1  itojun 
   1255  1.2.58.1    yamt 		if (flags & PFSYNC_FLAG_STALE)
   1256  1.2.58.1    yamt 			sp->sync_flags |= PFSTATE_STALE;
   1257       1.1  itojun 	}
   1258       1.1  itojun 
   1259       1.1  itojun 	pf_state_peer_hton(&st->src, &sp->src);
   1260       1.1  itojun 	pf_state_peer_hton(&st->dst, &sp->dst);
   1261       1.1  itojun 
   1262       1.1  itojun 	if (st->expire <= secs)
   1263       1.1  itojun 		sp->expire = htonl(0);
   1264       1.1  itojun 	else
   1265       1.1  itojun 		sp->expire = htonl(st->expire - secs);
   1266       1.1  itojun 
   1267       1.1  itojun 	/* do we need to build "compressed" actions for network transfer? */
   1268  1.2.58.1    yamt 	if (sc->sc_sync_ifp && flags & PFSYNC_FLAG_COMPRESS) {
   1269       1.1  itojun 		switch (action) {
   1270       1.1  itojun 		case PFSYNC_ACT_UPD:
   1271       1.1  itojun 			newaction = PFSYNC_ACT_UPD_C;
   1272       1.1  itojun 			break;
   1273       1.1  itojun 		case PFSYNC_ACT_DEL:
   1274       1.1  itojun 			newaction = PFSYNC_ACT_DEL_C;
   1275       1.1  itojun 			break;
   1276       1.1  itojun 		default:
   1277       1.1  itojun 			/* by default we just send the uncompressed states */
   1278       1.1  itojun 			break;
   1279       1.1  itojun 		}
   1280       1.1  itojun 	}
   1281       1.1  itojun 
   1282       1.1  itojun 	if (newaction) {
   1283       1.1  itojun 		if (sc->sc_mbuf_net == NULL) {
   1284       1.1  itojun 			if ((sc->sc_mbuf_net = pfsync_get_mbuf(sc, newaction,
   1285       1.1  itojun 			    (void *)&sc->sc_statep_net.s)) == NULL) {
   1286       1.1  itojun 				splx(s);
   1287       1.1  itojun 				return (ENOMEM);
   1288       1.1  itojun 			}
   1289       1.1  itojun 		}
   1290       1.1  itojun 		h_net = mtod(sc->sc_mbuf_net, struct pfsync_header *);
   1291       1.1  itojun 
   1292       1.1  itojun 		switch (newaction) {
   1293       1.1  itojun 		case PFSYNC_ACT_UPD_C:
   1294       1.1  itojun 			if (i != 255) {
   1295       1.1  itojun 				up = (void *)((char *)h_net +
   1296       1.1  itojun 				    PFSYNC_HDRLEN + (i * sizeof(*up)));
   1297       1.1  itojun 				up->updates++;
   1298       1.1  itojun 			} else {
   1299       1.1  itojun 				h_net->count++;
   1300       1.1  itojun 				sc->sc_mbuf_net->m_pkthdr.len =
   1301       1.1  itojun 				    sc->sc_mbuf_net->m_len += sizeof(*up);
   1302       1.1  itojun 				up = sc->sc_statep_net.u++;
   1303       1.1  itojun 
   1304  1.2.58.1    yamt 				memset(up, 0, sizeof(*up));
   1305  1.2.58.1    yamt 				memcpy(up->id, &st->id, sizeof(up->id));
   1306       1.1  itojun 				up->creatorid = st->creatorid;
   1307       1.1  itojun 			}
   1308       1.1  itojun 			up->timeout = st->timeout;
   1309       1.1  itojun 			up->expire = sp->expire;
   1310       1.1  itojun 			up->src = sp->src;
   1311       1.1  itojun 			up->dst = sp->dst;
   1312       1.1  itojun 			break;
   1313       1.1  itojun 		case PFSYNC_ACT_DEL_C:
   1314       1.1  itojun 			sc->sc_mbuf_net->m_pkthdr.len =
   1315       1.1  itojun 			    sc->sc_mbuf_net->m_len += sizeof(*dp);
   1316       1.1  itojun 			dp = sc->sc_statep_net.d++;
   1317       1.1  itojun 			h_net->count++;
   1318       1.1  itojun 
   1319  1.2.58.1    yamt 			memset(dp, 0, sizeof(*dp));
   1320  1.2.58.1    yamt 			memcpy(dp->id, &st->id, sizeof(dp->id));
   1321       1.1  itojun 			dp->creatorid = st->creatorid;
   1322       1.1  itojun 			break;
   1323       1.1  itojun 		}
   1324       1.1  itojun 	}
   1325       1.1  itojun 
   1326       1.1  itojun 	if (h->count == sc->sc_maxcount ||
   1327       1.1  itojun 	    (sc->sc_maxupdates && (sp->updates >= sc->sc_maxupdates)))
   1328       1.1  itojun 		ret = pfsync_sendout(sc);
   1329       1.1  itojun 
   1330       1.1  itojun 	splx(s);
   1331       1.1  itojun 	return (ret);
   1332       1.1  itojun }
   1333       1.1  itojun 
   1334       1.1  itojun /* This must be called in splnet() */
   1335       1.1  itojun int
   1336       1.1  itojun pfsync_request_update(struct pfsync_state_upd *up, struct in_addr *src)
   1337       1.1  itojun {
   1338  1.2.58.1    yamt 	struct ifnet *ifp = NULL;
   1339       1.1  itojun 	struct pfsync_header *h;
   1340  1.2.58.1    yamt 	struct pfsync_softc *sc = pfsyncif;
   1341       1.1  itojun 	struct pfsync_state_upd_req *rup;
   1342  1.2.58.1    yamt 	int ret = 0;
   1343       1.1  itojun 
   1344  1.2.58.1    yamt 	if (sc == NULL)
   1345  1.2.58.1    yamt 		return (0);
   1346  1.2.58.1    yamt 
   1347  1.2.58.1    yamt 	ifp = &sc->sc_if;
   1348       1.1  itojun 	if (sc->sc_mbuf == NULL) {
   1349       1.1  itojun 		if ((sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_UREQ,
   1350  1.2.58.1    yamt 		    (void *)&sc->sc_statep.s)) == NULL)
   1351       1.1  itojun 			return (ENOMEM);
   1352       1.1  itojun 		h = mtod(sc->sc_mbuf, struct pfsync_header *);
   1353       1.1  itojun 	} else {
   1354       1.1  itojun 		h = mtod(sc->sc_mbuf, struct pfsync_header *);
   1355       1.1  itojun 		if (h->action != PFSYNC_ACT_UREQ) {
   1356       1.1  itojun 			pfsync_sendout(sc);
   1357       1.1  itojun 			if ((sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_UREQ,
   1358  1.2.58.1    yamt 			    (void *)&sc->sc_statep.s)) == NULL)
   1359       1.1  itojun 				return (ENOMEM);
   1360       1.1  itojun 			h = mtod(sc->sc_mbuf, struct pfsync_header *);
   1361       1.1  itojun 		}
   1362       1.1  itojun 	}
   1363       1.1  itojun 
   1364       1.1  itojun 	if (src != NULL)
   1365       1.1  itojun 		sc->sc_sendaddr = *src;
   1366       1.1  itojun 	sc->sc_mbuf->m_pkthdr.len = sc->sc_mbuf->m_len += sizeof(*rup);
   1367       1.1  itojun 	h->count++;
   1368       1.1  itojun 	rup = sc->sc_statep.r++;
   1369  1.2.58.1    yamt 	memset(rup, 0, sizeof(*rup));
   1370       1.1  itojun 	if (up != NULL) {
   1371  1.2.58.1    yamt 		memcpy(rup->id, up->id, sizeof(rup->id));
   1372       1.1  itojun 		rup->creatorid = up->creatorid;
   1373       1.1  itojun 	}
   1374       1.1  itojun 
   1375       1.1  itojun 	if (h->count == sc->sc_maxcount)
   1376       1.1  itojun 		ret = pfsync_sendout(sc);
   1377       1.1  itojun 
   1378       1.1  itojun 	return (ret);
   1379       1.1  itojun }
   1380       1.1  itojun 
   1381       1.1  itojun int
   1382       1.1  itojun pfsync_clear_states(u_int32_t creatorid, char *ifname)
   1383       1.1  itojun {
   1384  1.2.58.1    yamt 	struct ifnet *ifp = NULL;
   1385  1.2.58.1    yamt 	struct pfsync_softc *sc = pfsyncif;
   1386       1.1  itojun 	struct pfsync_state_clr *cp;
   1387       1.1  itojun 	int s, ret;
   1388       1.1  itojun 
   1389  1.2.58.1    yamt 	if (sc == NULL)
   1390  1.2.58.1    yamt 		return (0);
   1391  1.2.58.1    yamt 
   1392  1.2.58.1    yamt 	ifp = &sc->sc_if;
   1393       1.1  itojun 	s = splnet();
   1394       1.1  itojun 	if (sc->sc_mbuf != NULL)
   1395       1.1  itojun 		pfsync_sendout(sc);
   1396       1.1  itojun 	if ((sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_CLR,
   1397       1.1  itojun 	    (void *)&sc->sc_statep.c)) == NULL) {
   1398       1.1  itojun 		splx(s);
   1399       1.1  itojun 		return (ENOMEM);
   1400       1.1  itojun 	}
   1401       1.1  itojun 	sc->sc_mbuf->m_pkthdr.len = sc->sc_mbuf->m_len += sizeof(*cp);
   1402       1.1  itojun 	cp = sc->sc_statep.c;
   1403       1.1  itojun 	cp->creatorid = creatorid;
   1404       1.1  itojun 	if (ifname != NULL)
   1405       1.1  itojun 		strlcpy(cp->ifname, ifname, IFNAMSIZ);
   1406       1.1  itojun 
   1407       1.1  itojun 	ret = (pfsync_sendout(sc));
   1408       1.1  itojun 	splx(s);
   1409       1.1  itojun 	return (ret);
   1410       1.1  itojun }
   1411       1.1  itojun 
   1412       1.1  itojun void
   1413       1.1  itojun pfsync_timeout(void *v)
   1414       1.1  itojun {
   1415       1.1  itojun 	struct pfsync_softc *sc = v;
   1416       1.1  itojun 	int s;
   1417       1.1  itojun 
   1418       1.1  itojun 	s = splnet();
   1419       1.1  itojun 	pfsync_sendout(sc);
   1420       1.1  itojun 	splx(s);
   1421       1.1  itojun }
   1422       1.1  itojun 
   1423       1.1  itojun void
   1424  1.2.58.1    yamt pfsync_tdb_timeout(void *v)
   1425  1.2.58.1    yamt {
   1426  1.2.58.1    yamt 	struct pfsync_softc *sc = v;
   1427  1.2.58.1    yamt 	int s;
   1428  1.2.58.1    yamt 
   1429  1.2.58.1    yamt 	s = splnet();
   1430  1.2.58.1    yamt 	pfsync_tdb_sendout(sc);
   1431  1.2.58.1    yamt 	splx(s);
   1432  1.2.58.1    yamt }
   1433  1.2.58.1    yamt 
   1434  1.2.58.1    yamt /* This must be called in splnet() */
   1435  1.2.58.1    yamt void
   1436       1.1  itojun pfsync_send_bus(struct pfsync_softc *sc, u_int8_t status)
   1437       1.1  itojun {
   1438       1.1  itojun 	struct pfsync_state_bus *bus;
   1439       1.1  itojun 
   1440       1.1  itojun 	if (sc->sc_mbuf != NULL)
   1441       1.1  itojun 		pfsync_sendout(sc);
   1442       1.1  itojun 
   1443       1.1  itojun 	if (pfsync_sync_ok &&
   1444       1.1  itojun 	    (sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_BUS,
   1445       1.1  itojun 	    (void *)&sc->sc_statep.b)) != NULL) {
   1446       1.1  itojun 		sc->sc_mbuf->m_pkthdr.len = sc->sc_mbuf->m_len += sizeof(*bus);
   1447       1.1  itojun 		bus = sc->sc_statep.b;
   1448       1.1  itojun 		bus->creatorid = pf_status.hostid;
   1449       1.1  itojun 		bus->status = status;
   1450  1.2.58.1    yamt 		bus->endtime = htonl(time_uptime - sc->sc_ureq_received);
   1451       1.1  itojun 		pfsync_sendout(sc);
   1452       1.1  itojun 	}
   1453       1.1  itojun }
   1454       1.1  itojun 
   1455       1.1  itojun void
   1456       1.1  itojun pfsync_bulk_update(void *v)
   1457       1.1  itojun {
   1458       1.1  itojun 	struct pfsync_softc *sc = v;
   1459       1.1  itojun 	int s, i = 0;
   1460       1.1  itojun 	struct pf_state *state;
   1461       1.1  itojun 
   1462       1.1  itojun 	s = splnet();
   1463       1.1  itojun 	if (sc->sc_mbuf != NULL)
   1464       1.1  itojun 		pfsync_sendout(sc);
   1465       1.1  itojun 
   1466       1.1  itojun 	/*
   1467       1.1  itojun 	 * Grab at most PFSYNC_BULKPACKETS worth of states which have not
   1468       1.1  itojun 	 * been sent since the latest request was made.
   1469       1.1  itojun 	 */
   1470  1.2.58.1    yamt 	state = sc->sc_bulk_send_next;
   1471  1.2.58.1    yamt 	if (state)
   1472  1.2.58.1    yamt 		do {
   1473  1.2.58.1    yamt 			/* send state update if syncable and not already sent */
   1474  1.2.58.1    yamt 			if (!state->sync_flags
   1475  1.2.58.1    yamt 			    && state->timeout < PFTM_MAX
   1476  1.2.58.1    yamt 			    && state->pfsync_time <= sc->sc_ureq_received) {
   1477       1.1  itojun 				pfsync_pack_state(PFSYNC_ACT_UPD, state, 0);
   1478  1.2.58.1    yamt 				i++;
   1479  1.2.58.1    yamt 			}
   1480       1.1  itojun 
   1481  1.2.58.1    yamt 			/* figure next state to send */
   1482  1.2.58.1    yamt 			state = TAILQ_NEXT(state, entry_list);
   1483  1.2.58.1    yamt 
   1484  1.2.58.1    yamt 			/* wrap to start of list if we hit the end */
   1485  1.2.58.1    yamt 			if (!state)
   1486  1.2.58.1    yamt 				state = TAILQ_FIRST(&state_list);
   1487  1.2.58.1    yamt 		} while (i < sc->sc_maxcount * PFSYNC_BULKPACKETS &&
   1488  1.2.58.1    yamt 		    state != sc->sc_bulk_terminator);
   1489  1.2.58.1    yamt 
   1490  1.2.58.1    yamt 	if (!state || state == sc->sc_bulk_terminator) {
   1491  1.2.58.1    yamt 		/* we're done */
   1492  1.2.58.1    yamt 		pfsync_send_bus(sc, PFSYNC_BUS_END);
   1493  1.2.58.1    yamt 		sc->sc_ureq_received = 0;
   1494  1.2.58.1    yamt 		sc->sc_bulk_send_next = NULL;
   1495  1.2.58.1    yamt 		sc->sc_bulk_terminator = NULL;
   1496  1.2.58.1    yamt 		callout_stop(&sc->sc_bulk_tmo);
   1497  1.2.58.1    yamt 		if (pf_status.debug >= PF_DEBUG_MISC)
   1498  1.2.58.1    yamt 			printf("pfsync: bulk update complete\n");
   1499  1.2.58.1    yamt 	} else {
   1500  1.2.58.1    yamt 		/* look again for more in a bit */
   1501  1.2.58.1    yamt 		callout_schedule(&sc->sc_bulk_tmo, 1);
   1502  1.2.58.1    yamt 		sc->sc_bulk_send_next = state;
   1503       1.1  itojun 	}
   1504       1.1  itojun 	if (sc->sc_mbuf != NULL)
   1505       1.1  itojun 		pfsync_sendout(sc);
   1506       1.1  itojun 	splx(s);
   1507       1.1  itojun }
   1508       1.1  itojun 
   1509       1.1  itojun void
   1510       1.1  itojun pfsync_bulkfail(void *v)
   1511       1.1  itojun {
   1512       1.1  itojun 	struct pfsync_softc *sc = v;
   1513  1.2.58.1    yamt 	int s, error;
   1514       1.1  itojun 
   1515       1.1  itojun 	if (sc->sc_bulk_tries++ < PFSYNC_MAX_BULKTRIES) {
   1516       1.1  itojun 		/* Try again in a bit */
   1517  1.2.58.1    yamt 		callout_schedule(&sc->sc_bulkfail_tmo, 5 * hz);
   1518  1.2.58.1    yamt 		s = splnet();
   1519  1.2.58.1    yamt 		error = pfsync_request_update(NULL, NULL);
   1520  1.2.58.1    yamt 		if (error == ENOMEM) {
   1521  1.2.58.1    yamt 			if (pf_status.debug >= PF_DEBUG_MISC)
   1522  1.2.58.1    yamt 				printf("pfsync: cannot allocate mbufs for "
   1523  1.2.58.1    yamt 				    "bulk update\n");
   1524  1.2.58.1    yamt 		} else
   1525  1.2.58.1    yamt 			pfsync_sendout(sc);
   1526  1.2.58.1    yamt 		splx(s);
   1527       1.1  itojun 	} else {
   1528       1.1  itojun 		/* Pretend like the transfer was ok */
   1529       1.1  itojun 		sc->sc_ureq_sent = 0;
   1530       1.1  itojun 		sc->sc_bulk_tries = 0;
   1531  1.2.58.1    yamt #if NCARP > 0
   1532  1.2.58.1    yamt 		if (!pfsync_sync_ok)
   1533  1.2.58.1    yamt 			carp_suppress_preempt --;
   1534  1.2.58.1    yamt #endif
   1535       1.1  itojun 		pfsync_sync_ok = 1;
   1536       1.1  itojun 		if (pf_status.debug >= PF_DEBUG_MISC)
   1537       1.1  itojun 			printf("pfsync: failed to receive "
   1538       1.1  itojun 			    "bulk update status\n");
   1539  1.2.58.1    yamt 		callout_stop(&sc->sc_bulkfail_tmo);
   1540       1.1  itojun 	}
   1541       1.1  itojun }
   1542       1.1  itojun 
   1543  1.2.58.1    yamt /* This must be called in splnet() */
   1544       1.1  itojun int
   1545  1.2.58.1    yamt pfsync_sendout(struct pfsync_softc *sc)
   1546       1.1  itojun {
   1547  1.2.58.1    yamt #if NBPFILTER > 0
   1548       1.1  itojun 	struct ifnet *ifp = &sc->sc_if;
   1549  1.2.58.1    yamt #endif
   1550       1.1  itojun 	struct mbuf *m;
   1551       1.1  itojun 
   1552  1.2.58.1    yamt 	callout_stop(&sc->sc_tmo);
   1553       1.1  itojun 
   1554       1.1  itojun 	if (sc->sc_mbuf == NULL)
   1555       1.1  itojun 		return (0);
   1556       1.1  itojun 	m = sc->sc_mbuf;
   1557       1.1  itojun 	sc->sc_mbuf = NULL;
   1558       1.1  itojun 	sc->sc_statep.s = NULL;
   1559       1.1  itojun 
   1560       1.1  itojun #if NBPFILTER > 0
   1561       1.1  itojun 	if (ifp->if_bpf)
   1562       1.1  itojun 		bpf_mtap(ifp->if_bpf, m);
   1563       1.1  itojun #endif
   1564       1.1  itojun 
   1565       1.1  itojun 	if (sc->sc_mbuf_net) {
   1566       1.1  itojun 		m_freem(m);
   1567       1.1  itojun 		m = sc->sc_mbuf_net;
   1568       1.1  itojun 		sc->sc_mbuf_net = NULL;
   1569       1.1  itojun 		sc->sc_statep_net.s = NULL;
   1570       1.1  itojun 	}
   1571       1.1  itojun 
   1572  1.2.58.1    yamt 	return pfsync_sendout_mbuf(sc, m);
   1573  1.2.58.1    yamt }
   1574  1.2.58.1    yamt 
   1575  1.2.58.1    yamt int
   1576  1.2.58.1    yamt pfsync_tdb_sendout(struct pfsync_softc *sc)
   1577  1.2.58.1    yamt {
   1578  1.2.58.1    yamt #if NBPFILTER > 0
   1579  1.2.58.1    yamt 	struct ifnet *ifp = &sc->sc_if;
   1580  1.2.58.1    yamt #endif
   1581  1.2.58.1    yamt 	struct mbuf *m;
   1582  1.2.58.1    yamt 
   1583  1.2.58.1    yamt 	callout_stop(&sc->sc_tdb_tmo);
   1584  1.2.58.1    yamt 
   1585  1.2.58.1    yamt 	if (sc->sc_mbuf_tdb == NULL)
   1586  1.2.58.1    yamt 		return (0);
   1587  1.2.58.1    yamt 	m = sc->sc_mbuf_tdb;
   1588  1.2.58.1    yamt 	sc->sc_mbuf_tdb = NULL;
   1589  1.2.58.1    yamt 	sc->sc_statep_tdb.t = NULL;
   1590       1.1  itojun 
   1591  1.2.58.1    yamt #if NBPFILTER > 0
   1592  1.2.58.1    yamt 	if (ifp->if_bpf)
   1593  1.2.58.1    yamt 		bpf_mtap(ifp->if_bpf, m);
   1594  1.2.58.1    yamt #endif
   1595  1.2.58.1    yamt 
   1596  1.2.58.1    yamt 	return pfsync_sendout_mbuf(sc, m);
   1597  1.2.58.1    yamt }
   1598  1.2.58.1    yamt 
   1599  1.2.58.1    yamt int
   1600  1.2.58.1    yamt pfsync_sendout_mbuf(struct pfsync_softc *sc, struct mbuf *m)
   1601  1.2.58.1    yamt {
   1602  1.2.58.1    yamt 	struct sockaddr sa;
   1603  1.2.58.1    yamt 	struct ip *ip;
   1604  1.2.58.1    yamt 
   1605  1.2.58.1    yamt 	if (sc->sc_sync_ifp ||
   1606  1.2.58.1    yamt 	    sc->sc_sync_peer.s_addr != INADDR_PFSYNC_GROUP) {
   1607       1.1  itojun 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
   1608       1.1  itojun 		if (m == NULL) {
   1609  1.2.58.1    yamt 			PFSYNC_STATINC(PFSYNC_STAT_ONOMEM);
   1610       1.1  itojun 			return (0);
   1611       1.1  itojun 		}
   1612       1.1  itojun 		ip = mtod(m, struct ip *);
   1613       1.1  itojun 		ip->ip_v = IPVERSION;
   1614       1.1  itojun 		ip->ip_hl = sizeof(*ip) >> 2;
   1615       1.1  itojun 		ip->ip_tos = IPTOS_LOWDELAY;
   1616       1.1  itojun 		ip->ip_len = htons(m->m_pkthdr.len);
   1617  1.2.58.1    yamt 		ip->ip_id = htons(ip_randomid(0));
   1618       1.1  itojun 		ip->ip_off = htons(IP_DF);
   1619       1.1  itojun 		ip->ip_ttl = PFSYNC_DFLTTL;
   1620       1.1  itojun 		ip->ip_p = IPPROTO_PFSYNC;
   1621       1.1  itojun 		ip->ip_sum = 0;
   1622       1.1  itojun 
   1623  1.2.58.1    yamt 		memset(&sa, 0, sizeof(sa));
   1624  1.2.58.1    yamt 		ip->ip_src.s_addr = INADDR_ANY;
   1625       1.1  itojun 
   1626       1.1  itojun 		if (sc->sc_sendaddr.s_addr == INADDR_PFSYNC_GROUP)
   1627       1.1  itojun 			m->m_flags |= M_MCAST;
   1628       1.1  itojun 		ip->ip_dst = sc->sc_sendaddr;
   1629  1.2.58.1    yamt 		sc->sc_sendaddr.s_addr = sc->sc_sync_peer.s_addr;
   1630       1.1  itojun 
   1631  1.2.58.1    yamt 		PFSYNC_STATINC(PFSYNC_STAT_OPACKETS);
   1632       1.1  itojun 
   1633  1.2.58.1    yamt 		if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
   1634  1.2.58.1    yamt 			PFSYNC_STATINC(PFSYNC_STAT_OERRORS);
   1635  1.2.58.1    yamt 		}
   1636       1.1  itojun 	} else
   1637       1.1  itojun 		m_freem(m);
   1638       1.1  itojun 
   1639       1.1  itojun 	return (0);
   1640       1.1  itojun }
   1641  1.2.58.1    yamt 
   1642  1.2.58.1    yamt #ifdef IPSEC
   1643  1.2.58.1    yamt /* Update an in-kernel tdb. Silently fail if no tdb is found. */
   1644  1.2.58.1    yamt void
   1645  1.2.58.1    yamt pfsync_update_net_tdb(struct pfsync_tdb *pt)
   1646  1.2.58.1    yamt {
   1647  1.2.58.1    yamt 	struct tdb		*tdb;
   1648  1.2.58.1    yamt 	int			 s;
   1649  1.2.58.1    yamt 
   1650  1.2.58.1    yamt 	/* check for invalid values */
   1651  1.2.58.1    yamt 	if (ntohl(pt->spi) <= SPI_RESERVED_MAX ||
   1652  1.2.58.1    yamt 	    (pt->dst.sa.sa_family != AF_INET &&
   1653  1.2.58.1    yamt 	     pt->dst.sa.sa_family != AF_INET6))
   1654  1.2.58.1    yamt 		goto bad;
   1655  1.2.58.1    yamt 
   1656  1.2.58.1    yamt 	s = spltdb();
   1657  1.2.58.1    yamt 	tdb = gettdb(pt->spi, &pt->dst, pt->sproto);
   1658  1.2.58.1    yamt 	if (tdb) {
   1659  1.2.58.1    yamt 		pt->rpl = ntohl(pt->rpl);
   1660  1.2.58.1    yamt 		pt->cur_bytes = betoh64(pt->cur_bytes);
   1661  1.2.58.1    yamt 
   1662  1.2.58.1    yamt 		/* Neither replay nor byte counter should ever decrease. */
   1663  1.2.58.1    yamt 		if (pt->rpl < tdb->tdb_rpl ||
   1664  1.2.58.1    yamt 		    pt->cur_bytes < tdb->tdb_cur_bytes) {
   1665  1.2.58.1    yamt 			splx(s);
   1666  1.2.58.1    yamt 			goto bad;
   1667  1.2.58.1    yamt 		}
   1668  1.2.58.1    yamt 
   1669  1.2.58.1    yamt 		tdb->tdb_rpl = pt->rpl;
   1670  1.2.58.1    yamt 		tdb->tdb_cur_bytes = pt->cur_bytes;
   1671  1.2.58.1    yamt 	}
   1672  1.2.58.1    yamt 	splx(s);
   1673  1.2.58.1    yamt 	return;
   1674  1.2.58.1    yamt 
   1675  1.2.58.1    yamt  bad:
   1676  1.2.58.1    yamt 	if (pf_status.debug >= PF_DEBUG_MISC)
   1677  1.2.58.1    yamt 		printf("pfsync_insert: PFSYNC_ACT_TDB_UPD: "
   1678  1.2.58.1    yamt 		    "invalid value\n");
   1679  1.2.58.1    yamt 	PFSYNC_STATINC(PFSYNC_STAT_BADSTATE);
   1680  1.2.58.1    yamt 	return;
   1681  1.2.58.1    yamt }
   1682  1.2.58.1    yamt 
   1683  1.2.58.1    yamt /* One of our local tdbs have been updated, need to sync rpl with others */
   1684  1.2.58.1    yamt int
   1685  1.2.58.1    yamt pfsync_update_tdb(struct tdb *tdb, int output)
   1686  1.2.58.1    yamt {
   1687  1.2.58.1    yamt 	struct ifnet *ifp = NULL;
   1688  1.2.58.1    yamt 	struct pfsync_softc *sc = pfsyncif;
   1689  1.2.58.1    yamt 	struct pfsync_header *h;
   1690  1.2.58.1    yamt 	struct pfsync_tdb *pt = NULL;
   1691  1.2.58.1    yamt 	int s, i, ret;
   1692  1.2.58.1    yamt 
   1693  1.2.58.1    yamt 	if (sc == NULL)
   1694  1.2.58.1    yamt 		return (0);
   1695  1.2.58.1    yamt 
   1696  1.2.58.1    yamt 	ifp = &sc->sc_if;
   1697  1.2.58.1    yamt 	if (ifp->if_bpf == NULL && sc->sc_sync_ifp == NULL &&
   1698  1.2.58.1    yamt 	    sc->sc_sync_peer.s_addr == INADDR_PFSYNC_GROUP) {
   1699  1.2.58.1    yamt 		/* Don't leave any stale pfsync packets hanging around. */
   1700  1.2.58.1    yamt 		if (sc->sc_mbuf_tdb != NULL) {
   1701  1.2.58.1    yamt 			m_freem(sc->sc_mbuf_tdb);
   1702  1.2.58.1    yamt 			sc->sc_mbuf_tdb = NULL;
   1703  1.2.58.1    yamt 			sc->sc_statep_tdb.t = NULL;
   1704  1.2.58.1    yamt 		}
   1705  1.2.58.1    yamt 		return (0);
   1706  1.2.58.1    yamt 	}
   1707  1.2.58.1    yamt 
   1708  1.2.58.1    yamt 	s = splnet();
   1709  1.2.58.1    yamt 	if (sc->sc_mbuf_tdb == NULL) {
   1710  1.2.58.1    yamt 		if ((sc->sc_mbuf_tdb = pfsync_get_mbuf(sc, PFSYNC_ACT_TDB_UPD,
   1711  1.2.58.1    yamt 		    (void *)&sc->sc_statep_tdb.t)) == NULL) {
   1712  1.2.58.1    yamt 			splx(s);
   1713  1.2.58.1    yamt 			return (ENOMEM);
   1714  1.2.58.1    yamt 		}
   1715  1.2.58.1    yamt 		h = mtod(sc->sc_mbuf_tdb, struct pfsync_header *);
   1716  1.2.58.1    yamt 	} else {
   1717  1.2.58.1    yamt 		h = mtod(sc->sc_mbuf_tdb, struct pfsync_header *);
   1718  1.2.58.1    yamt 		if (h->action != PFSYNC_ACT_TDB_UPD) {
   1719  1.2.58.1    yamt 			/*
   1720  1.2.58.1    yamt 			 * XXX will never happen as long as there's
   1721  1.2.58.1    yamt 			 * only one "TDB action".
   1722  1.2.58.1    yamt 			 */
   1723  1.2.58.1    yamt 			pfsync_tdb_sendout(sc);
   1724  1.2.58.1    yamt 			sc->sc_mbuf_tdb = pfsync_get_mbuf(sc,
   1725  1.2.58.1    yamt 			    PFSYNC_ACT_TDB_UPD, (void *)&sc->sc_statep_tdb.t);
   1726  1.2.58.1    yamt 			if (sc->sc_mbuf_tdb == NULL) {
   1727  1.2.58.1    yamt 				splx(s);
   1728  1.2.58.1    yamt 				return (ENOMEM);
   1729  1.2.58.1    yamt 			}
   1730  1.2.58.1    yamt 			h = mtod(sc->sc_mbuf_tdb, struct pfsync_header *);
   1731  1.2.58.1    yamt 		} else if (sc->sc_maxupdates) {
   1732  1.2.58.1    yamt 			/*
   1733  1.2.58.1    yamt 			 * If it's an update, look in the packet to see if
   1734  1.2.58.1    yamt 			 * we already have an update for the state.
   1735  1.2.58.1    yamt 			 */
   1736  1.2.58.1    yamt 			struct pfsync_tdb *u =
   1737  1.2.58.1    yamt 			    (void *)((char *)h + PFSYNC_HDRLEN);
   1738  1.2.58.1    yamt 
   1739  1.2.58.1    yamt 			for (i = 0; !pt && i < h->count; i++) {
   1740  1.2.58.1    yamt 				if (tdb->tdb_spi == u->spi &&
   1741  1.2.58.1    yamt 				    tdb->tdb_sproto == u->sproto &&
   1742  1.2.58.1    yamt 			            !bcmp(&tdb->tdb_dst, &u->dst,
   1743  1.2.58.1    yamt 				    SA_LEN(&u->dst.sa))) {
   1744  1.2.58.1    yamt 					pt = u;
   1745  1.2.58.1    yamt 					pt->updates++;
   1746  1.2.58.1    yamt 				}
   1747  1.2.58.1    yamt 				u++;
   1748  1.2.58.1    yamt 			}
   1749  1.2.58.1    yamt 		}
   1750  1.2.58.1    yamt 	}
   1751  1.2.58.1    yamt 
   1752  1.2.58.1    yamt 	if (pt == NULL) {
   1753  1.2.58.1    yamt 		/* not a "duplicate" update */
   1754  1.2.58.1    yamt 		pt = sc->sc_statep_tdb.t++;
   1755  1.2.58.1    yamt 		sc->sc_mbuf_tdb->m_pkthdr.len =
   1756  1.2.58.1    yamt 		    sc->sc_mbuf_tdb->m_len += sizeof(struct pfsync_tdb);
   1757  1.2.58.1    yamt 		h->count++;
   1758  1.2.58.1    yamt 		memset(pt, 0, sizeof(*pt));
   1759  1.2.58.1    yamt 
   1760  1.2.58.1    yamt 		pt->spi = tdb->tdb_spi;
   1761  1.2.58.1    yamt 		memcpy(&pt->dst, &tdb->tdb_dst, sizeof pt->dst);
   1762  1.2.58.1    yamt 		pt->sproto = tdb->tdb_sproto;
   1763  1.2.58.1    yamt 	}
   1764  1.2.58.1    yamt 
   1765  1.2.58.1    yamt 	/*
   1766  1.2.58.1    yamt 	 * When a failover happens, the master's rpl is probably above
   1767  1.2.58.1    yamt 	 * what we see here (we may be up to a second late), so
   1768  1.2.58.1    yamt 	 * increase it a bit for outbound tdbs to manage most such
   1769  1.2.58.1    yamt 	 * situations.
   1770  1.2.58.1    yamt 	 *
   1771  1.2.58.1    yamt 	 * For now, just add an offset that is likely to be larger
   1772  1.2.58.1    yamt 	 * than the number of packets we can see in one second. The RFC
   1773  1.2.58.1    yamt 	 * just says the next packet must have a higher seq value.
   1774  1.2.58.1    yamt 	 *
   1775  1.2.58.1    yamt 	 * XXX What is a good algorithm for this? We could use
   1776  1.2.58.1    yamt 	 * a rate-determined increase, but to know it, we would have
   1777  1.2.58.1    yamt 	 * to extend struct tdb.
   1778  1.2.58.1    yamt 	 * XXX pt->rpl can wrap over MAXINT, but if so the real tdb
   1779  1.2.58.1    yamt 	 * will soon be replaced anyway. For now, just don't handle
   1780  1.2.58.1    yamt 	 * this edge case.
   1781  1.2.58.1    yamt 	 */
   1782  1.2.58.1    yamt #define RPL_INCR 16384
   1783  1.2.58.1    yamt 	pt->rpl = htonl(tdb->tdb_rpl + (output ? RPL_INCR : 0));
   1784  1.2.58.1    yamt 	pt->cur_bytes = htobe64(tdb->tdb_cur_bytes);
   1785  1.2.58.1    yamt 
   1786  1.2.58.1    yamt 	if (h->count == sc->sc_maxcount ||
   1787  1.2.58.1    yamt 	    (sc->sc_maxupdates && (pt->updates >= sc->sc_maxupdates)))
   1788  1.2.58.1    yamt 		ret = pfsync_tdb_sendout(sc);
   1789  1.2.58.1    yamt 
   1790  1.2.58.1    yamt 	splx(s);
   1791  1.2.58.1    yamt 	return (ret);
   1792  1.2.58.1    yamt }
   1793  1.2.58.1    yamt #endif
   1794  1.2.58.1    yamt 
   1795  1.2.58.1    yamt static int
   1796  1.2.58.1    yamt sysctl_net_inet_pfsync_stats(SYSCTLFN_ARGS)
   1797  1.2.58.1    yamt {
   1798  1.2.58.1    yamt 
   1799  1.2.58.1    yamt 	return (NETSTAT_SYSCTL(pfsyncstat_percpu, PFSYNC_NSTATS));
   1800  1.2.58.1    yamt }
   1801  1.2.58.1    yamt 
   1802  1.2.58.1    yamt SYSCTL_SETUP(sysctl_net_inet_pfsync_setup, "sysctl net.inet.pfsync subtree setup")
   1803  1.2.58.1    yamt {
   1804  1.2.58.1    yamt 
   1805  1.2.58.1    yamt 	sysctl_createv(clog, 0, NULL, NULL,
   1806  1.2.58.1    yamt 		       CTLFLAG_PERMANENT,
   1807  1.2.58.1    yamt 		       CTLTYPE_NODE, "net", NULL,
   1808  1.2.58.1    yamt 		       NULL, 0, NULL, 0,
   1809  1.2.58.1    yamt 		       CTL_NET, CTL_EOL);
   1810  1.2.58.1    yamt 	sysctl_createv(clog, 0, NULL, NULL,
   1811  1.2.58.1    yamt 		       CTLFLAG_PERMANENT,
   1812  1.2.58.1    yamt 		       CTLTYPE_NODE, "inet", NULL,
   1813  1.2.58.1    yamt 		       NULL, 0, NULL, 0,
   1814  1.2.58.1    yamt 		       CTL_NET, PF_INET, CTL_EOL);
   1815  1.2.58.1    yamt 	sysctl_createv(clog, 0, NULL, NULL,
   1816  1.2.58.1    yamt 		       CTLFLAG_PERMANENT,
   1817  1.2.58.1    yamt 		       CTLTYPE_NODE, "pfsync",
   1818  1.2.58.1    yamt 		       SYSCTL_DESCR("pfsync related settings"),
   1819  1.2.58.1    yamt 		       NULL, 0, NULL, 0,
   1820  1.2.58.1    yamt 		       CTL_NET, PF_INET, IPPROTO_PFSYNC, CTL_EOL);
   1821  1.2.58.1    yamt 	sysctl_createv(clog, 0, NULL, NULL,
   1822  1.2.58.1    yamt 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   1823  1.2.58.1    yamt 		       CTLTYPE_STRUCT, "stats",
   1824  1.2.58.1    yamt 			   SYSCTL_DESCR("pfsync statistics"),
   1825  1.2.58.1    yamt 		       sysctl_net_inet_pfsync_stats, 0, NULL, 0,
   1826  1.2.58.1    yamt 		       CTL_NET, PF_INET, IPPROTO_PFSYNC,
   1827  1.2.58.1    yamt 	       CTL_CREATE, CTL_EOL);
   1828  1.2.58.1    yamt }
   1829