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