Home | History | Annotate | Line # | Download | only in net
if_pfsync.c revision 1.1
      1  1.1  itojun /*	$OpenBSD: if_pfsync.c,v 1.26 2004/03/28 18:14:20 mcbride Exp $	*/
      2  1.1  itojun 
      3  1.1  itojun /*
      4  1.1  itojun  * Copyright (c) 2002 Michael Shalayeff
      5  1.1  itojun  * All rights reserved.
      6  1.1  itojun  *
      7  1.1  itojun  * Redistribution and use in source and binary forms, with or without
      8  1.1  itojun  * modification, are permitted provided that the following conditions
      9  1.1  itojun  * are met:
     10  1.1  itojun  * 1. Redistributions of source code must retain the above copyright
     11  1.1  itojun  *    notice, this list of conditions and the following disclaimer.
     12  1.1  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  itojun  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  itojun  *    documentation and/or other materials provided with the distribution.
     15  1.1  itojun  *
     16  1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  itojun  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  itojun  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  itojun  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
     20  1.1  itojun  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  1.1  itojun  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  1.1  itojun  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  1.1  itojun  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     25  1.1  itojun  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     26  1.1  itojun  * THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  itojun  */
     28  1.1  itojun 
     29  1.1  itojun #include "bpfilter.h"
     30  1.1  itojun #include "pfsync.h"
     31  1.1  itojun 
     32  1.1  itojun #include <sys/param.h>
     33  1.1  itojun #include <sys/proc.h>
     34  1.1  itojun #include <sys/systm.h>
     35  1.1  itojun #include <sys/time.h>
     36  1.1  itojun #include <sys/mbuf.h>
     37  1.1  itojun #include <sys/socket.h>
     38  1.1  itojun #include <sys/ioctl.h>
     39  1.1  itojun #include <sys/timeout.h>
     40  1.1  itojun 
     41  1.1  itojun #include <net/if.h>
     42  1.1  itojun #include <net/if_types.h>
     43  1.1  itojun #include <net/route.h>
     44  1.1  itojun #include <net/bpf.h>
     45  1.1  itojun 
     46  1.1  itojun #ifdef	INET
     47  1.1  itojun #include <netinet/in.h>
     48  1.1  itojun #include <netinet/in_systm.h>
     49  1.1  itojun #include <netinet/in_var.h>
     50  1.1  itojun #include <netinet/ip.h>
     51  1.1  itojun #include <netinet/ip_var.h>
     52  1.1  itojun #endif
     53  1.1  itojun 
     54  1.1  itojun #ifdef INET6
     55  1.1  itojun #ifndef INET
     56  1.1  itojun #include <netinet/in.h>
     57  1.1  itojun #endif
     58  1.1  itojun #include <netinet6/nd6.h>
     59  1.1  itojun #endif /* INET6 */
     60  1.1  itojun 
     61  1.1  itojun #include <net/pfvar.h>
     62  1.1  itojun #include <net/if_pfsync.h>
     63  1.1  itojun 
     64  1.1  itojun #define PFSYNC_MINMTU	\
     65  1.1  itojun     (sizeof(struct pfsync_header) + sizeof(struct pf_state))
     66  1.1  itojun 
     67  1.1  itojun #ifdef PFSYNCDEBUG
     68  1.1  itojun #define DPRINTF(x)    do { if (pfsyncdebug) printf x ; } while (0)
     69  1.1  itojun int pfsyncdebug;
     70  1.1  itojun #else
     71  1.1  itojun #define DPRINTF(x)
     72  1.1  itojun #endif
     73  1.1  itojun 
     74  1.1  itojun struct pfsync_softc	pfsyncif;
     75  1.1  itojun int			pfsync_sync_ok;
     76  1.1  itojun struct pfsyncstats	pfsyncstats;
     77  1.1  itojun 
     78  1.1  itojun void	pfsyncattach(int);
     79  1.1  itojun void	pfsync_setmtu(struct pfsync_softc *, int);
     80  1.1  itojun int	pfsync_insert_net_state(struct pfsync_state *);
     81  1.1  itojun int	pfsyncoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
     82  1.1  itojun 	    struct rtentry *);
     83  1.1  itojun int	pfsyncioctl(struct ifnet *, u_long, caddr_t);
     84  1.1  itojun void	pfsyncstart(struct ifnet *);
     85  1.1  itojun 
     86  1.1  itojun struct mbuf *pfsync_get_mbuf(struct pfsync_softc *, u_int8_t, void **);
     87  1.1  itojun int	pfsync_request_update(struct pfsync_state_upd *, struct in_addr *);
     88  1.1  itojun int	pfsync_sendout(struct pfsync_softc *);
     89  1.1  itojun void	pfsync_timeout(void *);
     90  1.1  itojun void	pfsync_send_bus(struct pfsync_softc *, u_int8_t);
     91  1.1  itojun void	pfsync_bulk_update(void *);
     92  1.1  itojun void	pfsync_bulkfail(void *);
     93  1.1  itojun 
     94  1.1  itojun extern int ifqmaxlen;
     95  1.1  itojun extern struct timeval time;
     96  1.1  itojun extern struct timeval mono_time;
     97  1.1  itojun extern int hz;
     98  1.1  itojun 
     99  1.1  itojun void
    100  1.1  itojun pfsyncattach(int npfsync)
    101  1.1  itojun {
    102  1.1  itojun 	struct ifnet *ifp;
    103  1.1  itojun 
    104  1.1  itojun 	pfsync_sync_ok = 1;
    105  1.1  itojun 	bzero(&pfsyncif, sizeof(pfsyncif));
    106  1.1  itojun 	pfsyncif.sc_mbuf = NULL;
    107  1.1  itojun 	pfsyncif.sc_mbuf_net = NULL;
    108  1.1  itojun 	pfsyncif.sc_statep.s = NULL;
    109  1.1  itojun 	pfsyncif.sc_statep_net.s = NULL;
    110  1.1  itojun 	pfsyncif.sc_maxupdates = 128;
    111  1.1  itojun 	pfsyncif.sc_sendaddr.s_addr = INADDR_PFSYNC_GROUP;
    112  1.1  itojun 	pfsyncif.sc_ureq_received = 0;
    113  1.1  itojun 	pfsyncif.sc_ureq_sent = 0;
    114  1.1  itojun 	ifp = &pfsyncif.sc_if;
    115  1.1  itojun 	strlcpy(ifp->if_xname, "pfsync0", sizeof ifp->if_xname);
    116  1.1  itojun 	ifp->if_softc = &pfsyncif;
    117  1.1  itojun 	ifp->if_ioctl = pfsyncioctl;
    118  1.1  itojun 	ifp->if_output = pfsyncoutput;
    119  1.1  itojun 	ifp->if_start = pfsyncstart;
    120  1.1  itojun 	ifp->if_type = IFT_PFSYNC;
    121  1.1  itojun 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
    122  1.1  itojun 	ifp->if_hdrlen = PFSYNC_HDRLEN;
    123  1.1  itojun 	pfsync_setmtu(&pfsyncif, MCLBYTES);
    124  1.1  itojun 	timeout_set(&pfsyncif.sc_tmo, pfsync_timeout, &pfsyncif);
    125  1.1  itojun 	timeout_set(&pfsyncif.sc_bulk_tmo, pfsync_bulk_update, &pfsyncif);
    126  1.1  itojun 	timeout_set(&pfsyncif.sc_bulkfail_tmo, pfsync_bulkfail, &pfsyncif);
    127  1.1  itojun 	if_attach(ifp);
    128  1.1  itojun 	if_alloc_sadl(ifp);
    129  1.1  itojun 
    130  1.1  itojun #if NBPFILTER > 0
    131  1.1  itojun 	bpfattach(&pfsyncif.sc_if.if_bpf, ifp, DLT_PFSYNC, PFSYNC_HDRLEN);
    132  1.1  itojun #endif
    133  1.1  itojun }
    134  1.1  itojun 
    135  1.1  itojun /*
    136  1.1  itojun  * Start output on the pfsync interface.
    137  1.1  itojun  */
    138  1.1  itojun void
    139  1.1  itojun pfsyncstart(struct ifnet *ifp)
    140  1.1  itojun {
    141  1.1  itojun 	struct mbuf *m;
    142  1.1  itojun 	int s;
    143  1.1  itojun 
    144  1.1  itojun 	for (;;) {
    145  1.1  itojun 		s = splimp();
    146  1.1  itojun 		IF_DROP(&ifp->if_snd);
    147  1.1  itojun 		IF_DEQUEUE(&ifp->if_snd, m);
    148  1.1  itojun 		splx(s);
    149  1.1  itojun 
    150  1.1  itojun 		if (m == NULL)
    151  1.1  itojun 			return;
    152  1.1  itojun 		else
    153  1.1  itojun 			m_freem(m);
    154  1.1  itojun 	}
    155  1.1  itojun }
    156  1.1  itojun 
    157  1.1  itojun int
    158  1.1  itojun pfsync_insert_net_state(struct pfsync_state *sp)
    159  1.1  itojun {
    160  1.1  itojun 	struct pf_state	*st = NULL;
    161  1.1  itojun 	struct pf_rule *r = NULL;
    162  1.1  itojun 	struct pfi_kif	*kif;
    163  1.1  itojun 
    164  1.1  itojun 	if (sp->creatorid == 0 && pf_status.debug >= PF_DEBUG_MISC) {
    165  1.1  itojun 		printf("pfsync_insert_net_state: invalid creator id:"
    166  1.1  itojun 		    " %08x\n", ntohl(sp->creatorid));
    167  1.1  itojun 		return (EINVAL);
    168  1.1  itojun 	}
    169  1.1  itojun 
    170  1.1  itojun 	kif = pfi_lookup_create(sp->ifname);
    171  1.1  itojun 	if (kif == NULL) {
    172  1.1  itojun 		if (pf_status.debug >= PF_DEBUG_MISC)
    173  1.1  itojun 			printf("pfsync_insert_net_state: "
    174  1.1  itojun 			    "unknown interface: %s\n", sp->ifname);
    175  1.1  itojun 		/* skip this state */
    176  1.1  itojun 		return (0);
    177  1.1  itojun 	}
    178  1.1  itojun 
    179  1.1  itojun 	/*
    180  1.1  itojun 	 * Just use the default rule until we have infrastructure to find the
    181  1.1  itojun 	 * best matching rule.
    182  1.1  itojun 	 */
    183  1.1  itojun 	r = &pf_default_rule;
    184  1.1  itojun 
    185  1.1  itojun 	if (!r->max_states || r->states < r->max_states)
    186  1.1  itojun 		st = pool_get(&pf_state_pl, PR_NOWAIT);
    187  1.1  itojun 	if (st == NULL) {
    188  1.1  itojun 		pfi_maybe_destroy(kif);
    189  1.1  itojun 		return (ENOMEM);
    190  1.1  itojun 	}
    191  1.1  itojun 	bzero(st, sizeof(*st));
    192  1.1  itojun 
    193  1.1  itojun 	st->rule.ptr = r;
    194  1.1  itojun 	/* XXX get pointers to nat_rule and anchor */
    195  1.1  itojun 
    196  1.1  itojun 	/* fill in the rest of the state entry */
    197  1.1  itojun 	pf_state_host_ntoh(&sp->lan, &st->lan);
    198  1.1  itojun 	pf_state_host_ntoh(&sp->gwy, &st->gwy);
    199  1.1  itojun 	pf_state_host_ntoh(&sp->ext, &st->ext);
    200  1.1  itojun 
    201  1.1  itojun 	pf_state_peer_ntoh(&sp->src, &st->src);
    202  1.1  itojun 	pf_state_peer_ntoh(&sp->dst, &st->dst);
    203  1.1  itojun 
    204  1.1  itojun 	bcopy(&sp->rt_addr, &st->rt_addr, sizeof(st->rt_addr));
    205  1.1  itojun 	st->creation = ntohl(sp->creation) + time.tv_sec;
    206  1.1  itojun 	st->expire = ntohl(sp->expire) + time.tv_sec;
    207  1.1  itojun 
    208  1.1  itojun 	st->af = sp->af;
    209  1.1  itojun 	st->proto = sp->proto;
    210  1.1  itojun 	st->direction = sp->direction;
    211  1.1  itojun 	st->log = sp->log;
    212  1.1  itojun 	st->timeout = sp->timeout;
    213  1.1  itojun 	st->allow_opts = sp->allow_opts;
    214  1.1  itojun 
    215  1.1  itojun 	bcopy(sp->id, &st->id, sizeof(st->id));
    216  1.1  itojun 	st->creatorid = sp->creatorid;
    217  1.1  itojun 	st->sync_flags = sp->sync_flags | PFSTATE_FROMSYNC;
    218  1.1  itojun 
    219  1.1  itojun 
    220  1.1  itojun 	if (pf_insert_state(kif, st)) {
    221  1.1  itojun 		pfi_maybe_destroy(kif);
    222  1.1  itojun 		pool_put(&pf_state_pl, st);
    223  1.1  itojun 		return (EINVAL);
    224  1.1  itojun 	}
    225  1.1  itojun 
    226  1.1  itojun 	return (0);
    227  1.1  itojun }
    228  1.1  itojun 
    229  1.1  itojun void
    230  1.1  itojun pfsync_input(struct mbuf *m, ...)
    231  1.1  itojun {
    232  1.1  itojun 	struct ip *ip = mtod(m, struct ip *);
    233  1.1  itojun 	struct pfsync_header *ph;
    234  1.1  itojun 	struct pfsync_softc *sc = &pfsyncif;
    235  1.1  itojun 	struct pf_state *st, key;
    236  1.1  itojun 	struct pfsync_state *sp;
    237  1.1  itojun 	struct pfsync_state_upd *up;
    238  1.1  itojun 	struct pfsync_state_del *dp;
    239  1.1  itojun 	struct pfsync_state_clr *cp;
    240  1.1  itojun 	struct pfsync_state_upd_req *rup;
    241  1.1  itojun 	struct pfsync_state_bus *bus;
    242  1.1  itojun 	struct in_addr src;
    243  1.1  itojun 	struct mbuf *mp;
    244  1.1  itojun 	int iplen, action, error, i, s, count, offp;
    245  1.1  itojun 
    246  1.1  itojun 	pfsyncstats.pfsyncs_ipackets++;
    247  1.1  itojun 
    248  1.1  itojun 	/* verify that we have a sync interface configured */
    249  1.1  itojun 	if (!sc->sc_sync_ifp || !pf_status.running)
    250  1.1  itojun 		goto done;
    251  1.1  itojun 
    252  1.1  itojun 	/* verify that the packet came in on the right interface */
    253  1.1  itojun 	if (sc->sc_sync_ifp != m->m_pkthdr.rcvif) {
    254  1.1  itojun 		pfsyncstats.pfsyncs_badif++;
    255  1.1  itojun 		goto done;
    256  1.1  itojun 	}
    257  1.1  itojun 
    258  1.1  itojun 	/* verify that the IP TTL is 255.  */
    259  1.1  itojun 	if (ip->ip_ttl != PFSYNC_DFLTTL) {
    260  1.1  itojun 		pfsyncstats.pfsyncs_badttl++;
    261  1.1  itojun 		goto done;
    262  1.1  itojun 	}
    263  1.1  itojun 
    264  1.1  itojun 	iplen = ip->ip_hl << 2;
    265  1.1  itojun 
    266  1.1  itojun 	if (m->m_pkthdr.len < iplen + sizeof(*ph)) {
    267  1.1  itojun 		pfsyncstats.pfsyncs_hdrops++;
    268  1.1  itojun 		goto done;
    269  1.1  itojun 	}
    270  1.1  itojun 
    271  1.1  itojun 	if (iplen + sizeof(*ph) > m->m_len) {
    272  1.1  itojun 		if ((m = m_pullup(m, iplen + sizeof(*ph))) == NULL) {
    273  1.1  itojun 			pfsyncstats.pfsyncs_hdrops++;
    274  1.1  itojun 			goto done;
    275  1.1  itojun 		}
    276  1.1  itojun 		ip = mtod(m, struct ip *);
    277  1.1  itojun 	}
    278  1.1  itojun 	ph = (struct pfsync_header *)((char *)ip + iplen);
    279  1.1  itojun 
    280  1.1  itojun 	/* verify the version */
    281  1.1  itojun 	if (ph->version != PFSYNC_VERSION) {
    282  1.1  itojun 		pfsyncstats.pfsyncs_badver++;
    283  1.1  itojun 		goto done;
    284  1.1  itojun 	}
    285  1.1  itojun 
    286  1.1  itojun 	action = ph->action;
    287  1.1  itojun 	count = ph->count;
    288  1.1  itojun 
    289  1.1  itojun 	/* make sure it's a valid action code */
    290  1.1  itojun 	if (action >= PFSYNC_ACT_MAX) {
    291  1.1  itojun 		pfsyncstats.pfsyncs_badact++;
    292  1.1  itojun 		goto done;
    293  1.1  itojun 	}
    294  1.1  itojun 
    295  1.1  itojun 	/* Cheaper to grab this now than having to mess with mbufs later */
    296  1.1  itojun 	src = ip->ip_src;
    297  1.1  itojun 
    298  1.1  itojun 	switch (action) {
    299  1.1  itojun 	case PFSYNC_ACT_CLR: {
    300  1.1  itojun 		struct pfi_kif	*kif;
    301  1.1  itojun 		u_int32_t creatorid;
    302  1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    303  1.1  itojun 		    sizeof(*cp), &offp)) == NULL) {
    304  1.1  itojun 			pfsyncstats.pfsyncs_badlen++;
    305  1.1  itojun 			return;
    306  1.1  itojun 		}
    307  1.1  itojun 		cp = (struct pfsync_state_clr *)(mp->m_data + offp);
    308  1.1  itojun 		creatorid = cp->creatorid;
    309  1.1  itojun 
    310  1.1  itojun 		s = splsoftnet();
    311  1.1  itojun 		if (cp->ifname[0] == '\0') {
    312  1.1  itojun 			RB_FOREACH(st, pf_state_tree_id, &tree_id) {
    313  1.1  itojun 				if (st->creatorid == creatorid)
    314  1.1  itojun 					st->timeout = PFTM_PURGE;
    315  1.1  itojun 			}
    316  1.1  itojun 		} else {
    317  1.1  itojun 			kif = pfi_lookup_if(cp->ifname);
    318  1.1  itojun 			if (kif == NULL) {
    319  1.1  itojun 				if (pf_status.debug >= PF_DEBUG_MISC)
    320  1.1  itojun 					printf("pfsync_input: PFSYNC_ACT_CLR "
    321  1.1  itojun 					    "bad interface: %s\n", cp->ifname);
    322  1.1  itojun 				splx(s);
    323  1.1  itojun 				goto done;
    324  1.1  itojun 			}
    325  1.1  itojun 			RB_FOREACH(st, pf_state_tree_lan_ext,
    326  1.1  itojun 			    &kif->pfik_lan_ext) {
    327  1.1  itojun 				if (st->creatorid == creatorid)
    328  1.1  itojun 					st->timeout = PFTM_PURGE;
    329  1.1  itojun 			}
    330  1.1  itojun 		}
    331  1.1  itojun 		pf_purge_expired_states();
    332  1.1  itojun 		splx(s);
    333  1.1  itojun 
    334  1.1  itojun 		break;
    335  1.1  itojun 	}
    336  1.1  itojun 	case PFSYNC_ACT_INS:
    337  1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    338  1.1  itojun 		    count * sizeof(*sp), &offp)) == NULL) {
    339  1.1  itojun 			pfsyncstats.pfsyncs_badlen++;
    340  1.1  itojun 			return;
    341  1.1  itojun 		}
    342  1.1  itojun 
    343  1.1  itojun 		s = splsoftnet();
    344  1.1  itojun 		for (i = 0, sp = (struct pfsync_state *)(mp->m_data + offp);
    345  1.1  itojun 		    i < count; i++, sp++) {
    346  1.1  itojun 			/* check for invalid values */
    347  1.1  itojun 			if (sp->timeout >= PFTM_MAX ||
    348  1.1  itojun 			    sp->src.state > PF_TCPS_PROXY_DST ||
    349  1.1  itojun 			    sp->dst.state > PF_TCPS_PROXY_DST ||
    350  1.1  itojun 			    sp->direction > PF_OUT ||
    351  1.1  itojun 			    (sp->af != AF_INET && sp->af != AF_INET6)) {
    352  1.1  itojun 				if (pf_status.debug >= PF_DEBUG_MISC)
    353  1.1  itojun 					printf("pfsync_insert: PFSYNC_ACT_INS: "
    354  1.1  itojun 					    "invalid value\n");
    355  1.1  itojun 				pfsyncstats.pfsyncs_badstate++;
    356  1.1  itojun 				continue;
    357  1.1  itojun 			}
    358  1.1  itojun 
    359  1.1  itojun 			if ((error = pfsync_insert_net_state(sp))) {
    360  1.1  itojun 				if (error == ENOMEM) {
    361  1.1  itojun 					splx(s);
    362  1.1  itojun 					goto done;
    363  1.1  itojun 				}
    364  1.1  itojun 				continue;
    365  1.1  itojun 			}
    366  1.1  itojun 		}
    367  1.1  itojun 		splx(s);
    368  1.1  itojun 		break;
    369  1.1  itojun 	case PFSYNC_ACT_UPD:
    370  1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    371  1.1  itojun 		    count * sizeof(*sp), &offp)) == NULL) {
    372  1.1  itojun 			pfsyncstats.pfsyncs_badlen++;
    373  1.1  itojun 			return;
    374  1.1  itojun 		}
    375  1.1  itojun 
    376  1.1  itojun 		s = splsoftnet();
    377  1.1  itojun 		for (i = 0, sp = (struct pfsync_state *)(mp->m_data + offp);
    378  1.1  itojun 		    i < count; i++, sp++) {
    379  1.1  itojun 			/* check for invalid values */
    380  1.1  itojun 			if (sp->timeout >= PFTM_MAX ||
    381  1.1  itojun 			    sp->src.state > PF_TCPS_PROXY_DST ||
    382  1.1  itojun 			    sp->dst.state > PF_TCPS_PROXY_DST) {
    383  1.1  itojun 				if (pf_status.debug >= PF_DEBUG_MISC)
    384  1.1  itojun 					printf("pfsync_insert: PFSYNC_ACT_UPD: "
    385  1.1  itojun 					    "invalid value\n");
    386  1.1  itojun 				pfsyncstats.pfsyncs_badstate++;
    387  1.1  itojun 				continue;
    388  1.1  itojun 			}
    389  1.1  itojun 
    390  1.1  itojun 			bcopy(sp->id, &key.id, sizeof(key.id));
    391  1.1  itojun 			key.creatorid = sp->creatorid;
    392  1.1  itojun 
    393  1.1  itojun 			st = pf_find_state_byid(&key);
    394  1.1  itojun 			if (st == NULL) {
    395  1.1  itojun 				/* insert the update */
    396  1.1  itojun 				if (pfsync_insert_net_state(sp))
    397  1.1  itojun 					pfsyncstats.pfsyncs_badstate++;
    398  1.1  itojun 				continue;
    399  1.1  itojun 			}
    400  1.1  itojun 			pf_state_peer_ntoh(&sp->src, &st->src);
    401  1.1  itojun 			pf_state_peer_ntoh(&sp->dst, &st->dst);
    402  1.1  itojun 			st->expire = ntohl(sp->expire) + time.tv_sec;
    403  1.1  itojun 			st->timeout = sp->timeout;
    404  1.1  itojun 
    405  1.1  itojun 		}
    406  1.1  itojun 		splx(s);
    407  1.1  itojun 		break;
    408  1.1  itojun 	/*
    409  1.1  itojun 	 * It's not strictly necessary for us to support the "uncompressed"
    410  1.1  itojun 	 * delete action, but it's relatively simple and maintains consistency.
    411  1.1  itojun 	 */
    412  1.1  itojun 	case PFSYNC_ACT_DEL:
    413  1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    414  1.1  itojun 		    count * sizeof(*sp), &offp)) == NULL) {
    415  1.1  itojun 			pfsyncstats.pfsyncs_badlen++;
    416  1.1  itojun 			return;
    417  1.1  itojun 		}
    418  1.1  itojun 
    419  1.1  itojun 		s = splsoftnet();
    420  1.1  itojun 		for (i = 0, sp = (struct pfsync_state *)(mp->m_data + offp);
    421  1.1  itojun 		    i < count; i++, sp++) {
    422  1.1  itojun 			bcopy(sp->id, &key.id, sizeof(key.id));
    423  1.1  itojun 			key.creatorid = sp->creatorid;
    424  1.1  itojun 
    425  1.1  itojun 			st = pf_find_state_byid(&key);
    426  1.1  itojun 			if (st == NULL) {
    427  1.1  itojun 				pfsyncstats.pfsyncs_badstate++;
    428  1.1  itojun 				continue;
    429  1.1  itojun 			}
    430  1.1  itojun 			/*
    431  1.1  itojun 			 * XXX
    432  1.1  itojun 			 * pf_purge_expired_states() is expensive,
    433  1.1  itojun 			 * we really want to purge the state directly.
    434  1.1  itojun 			 */
    435  1.1  itojun 			st->timeout = PFTM_PURGE;
    436  1.1  itojun 			st->sync_flags |= PFSTATE_FROMSYNC;
    437  1.1  itojun 		}
    438  1.1  itojun 		pf_purge_expired_states();
    439  1.1  itojun 		splx(s);
    440  1.1  itojun 		break;
    441  1.1  itojun 	case PFSYNC_ACT_UPD_C: {
    442  1.1  itojun 		int update_requested = 0;
    443  1.1  itojun 
    444  1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    445  1.1  itojun 		    count * sizeof(*up), &offp)) == NULL) {
    446  1.1  itojun 			pfsyncstats.pfsyncs_badlen++;
    447  1.1  itojun 			return;
    448  1.1  itojun 		}
    449  1.1  itojun 
    450  1.1  itojun 		s = splsoftnet();
    451  1.1  itojun 		for (i = 0, up = (struct pfsync_state_upd *)(mp->m_data + offp);
    452  1.1  itojun 		    i < count; i++, up++) {
    453  1.1  itojun 			/* check for invalid values */
    454  1.1  itojun 			if (up->timeout >= PFTM_MAX ||
    455  1.1  itojun 			    up->src.state > PF_TCPS_PROXY_DST ||
    456  1.1  itojun 			    up->dst.state > PF_TCPS_PROXY_DST) {
    457  1.1  itojun 				if (pf_status.debug >= PF_DEBUG_MISC)
    458  1.1  itojun 					printf("pfsync_insert: "
    459  1.1  itojun 					    "PFSYNC_ACT_UPD_C: "
    460  1.1  itojun 					    "invalid value\n");
    461  1.1  itojun 				pfsyncstats.pfsyncs_badstate++;
    462  1.1  itojun 				continue;
    463  1.1  itojun 			}
    464  1.1  itojun 
    465  1.1  itojun 			bcopy(up->id, &key.id, sizeof(key.id));
    466  1.1  itojun 			key.creatorid = up->creatorid;
    467  1.1  itojun 
    468  1.1  itojun 			st = pf_find_state_byid(&key);
    469  1.1  itojun 			if (st == NULL) {
    470  1.1  itojun 				/* We don't have this state. Ask for it. */
    471  1.1  itojun 				pfsync_request_update(up, &src);
    472  1.1  itojun 				update_requested = 1;
    473  1.1  itojun 				pfsyncstats.pfsyncs_badstate++;
    474  1.1  itojun 				continue;
    475  1.1  itojun 			}
    476  1.1  itojun 			pf_state_peer_ntoh(&up->src, &st->src);
    477  1.1  itojun 			pf_state_peer_ntoh(&up->dst, &st->dst);
    478  1.1  itojun 			st->expire = ntohl(up->expire) + time.tv_sec;
    479  1.1  itojun 			st->timeout = up->timeout;
    480  1.1  itojun 		}
    481  1.1  itojun 		if (update_requested)
    482  1.1  itojun 			pfsync_sendout(sc);
    483  1.1  itojun 		splx(s);
    484  1.1  itojun 		break;
    485  1.1  itojun 	}
    486  1.1  itojun 	case PFSYNC_ACT_DEL_C:
    487  1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    488  1.1  itojun 		    count * sizeof(*dp), &offp)) == NULL) {
    489  1.1  itojun 			pfsyncstats.pfsyncs_badlen++;
    490  1.1  itojun 			return;
    491  1.1  itojun 		}
    492  1.1  itojun 
    493  1.1  itojun 		s = splsoftnet();
    494  1.1  itojun 		for (i = 0, dp = (struct pfsync_state_del *)(mp->m_data + offp);
    495  1.1  itojun 		    i < count; i++, dp++) {
    496  1.1  itojun 			bcopy(dp->id, &key.id, sizeof(key.id));
    497  1.1  itojun 			key.creatorid = dp->creatorid;
    498  1.1  itojun 
    499  1.1  itojun 			st = pf_find_state_byid(&key);
    500  1.1  itojun 			if (st == NULL) {
    501  1.1  itojun 				pfsyncstats.pfsyncs_badstate++;
    502  1.1  itojun 				continue;
    503  1.1  itojun 			}
    504  1.1  itojun 			/*
    505  1.1  itojun 			 * XXX
    506  1.1  itojun 			 * pf_purge_expired_states() is expensive,
    507  1.1  itojun 			 * we really want to purge the state directly.
    508  1.1  itojun 			 */
    509  1.1  itojun 			st->timeout = PFTM_PURGE;
    510  1.1  itojun 			st->sync_flags |= PFSTATE_FROMSYNC;
    511  1.1  itojun 		}
    512  1.1  itojun 		pf_purge_expired_states();
    513  1.1  itojun 		splx(s);
    514  1.1  itojun 		break;
    515  1.1  itojun 	case PFSYNC_ACT_INS_F:
    516  1.1  itojun 	case PFSYNC_ACT_DEL_F:
    517  1.1  itojun 		/* not implemented */
    518  1.1  itojun 		break;
    519  1.1  itojun 	case PFSYNC_ACT_UREQ:
    520  1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    521  1.1  itojun 		    count * sizeof(*rup), &offp)) == NULL) {
    522  1.1  itojun 			pfsyncstats.pfsyncs_badlen++;
    523  1.1  itojun 			return;
    524  1.1  itojun 		}
    525  1.1  itojun 
    526  1.1  itojun 		s = splsoftnet();
    527  1.1  itojun 		/* XXX send existing. pfsync_pack_state should handle this. */
    528  1.1  itojun 		if (sc->sc_mbuf != NULL)
    529  1.1  itojun 			pfsync_sendout(sc);
    530  1.1  itojun 		for (i = 0,
    531  1.1  itojun 		    rup = (struct pfsync_state_upd_req *)(mp->m_data + offp);
    532  1.1  itojun 		    i < count; i++, rup++) {
    533  1.1  itojun 			bcopy(rup->id, &key.id, sizeof(key.id));
    534  1.1  itojun 			key.creatorid = rup->creatorid;
    535  1.1  itojun 
    536  1.1  itojun 			if (key.id == 0 && key.creatorid == 0) {
    537  1.1  itojun 				sc->sc_ureq_received = mono_time.tv_sec;
    538  1.1  itojun 				if (pf_status.debug >= PF_DEBUG_MISC)
    539  1.1  itojun 					printf("pfsync: received "
    540  1.1  itojun 					    "bulk update request\n");
    541  1.1  itojun 				pfsync_send_bus(sc, PFSYNC_BUS_START);
    542  1.1  itojun 				timeout_add(&sc->sc_bulk_tmo, 1 * hz);
    543  1.1  itojun 			} else {
    544  1.1  itojun 				st = pf_find_state_byid(&key);
    545  1.1  itojun 				if (st == NULL) {
    546  1.1  itojun 					pfsyncstats.pfsyncs_badstate++;
    547  1.1  itojun 					continue;
    548  1.1  itojun 				}
    549  1.1  itojun 				pfsync_pack_state(PFSYNC_ACT_UPD, st, 0);
    550  1.1  itojun 			}
    551  1.1  itojun 		}
    552  1.1  itojun 		if (sc->sc_mbuf != NULL)
    553  1.1  itojun 			pfsync_sendout(sc);
    554  1.1  itojun 		splx(s);
    555  1.1  itojun 		break;
    556  1.1  itojun 	case PFSYNC_ACT_BUS:
    557  1.1  itojun 		/* If we're not waiting for a bulk update, who cares. */
    558  1.1  itojun 		if (sc->sc_ureq_sent == 0)
    559  1.1  itojun 			break;
    560  1.1  itojun 
    561  1.1  itojun 		if ((mp = m_pulldown(m, iplen + sizeof(*ph),
    562  1.1  itojun 		    sizeof(*bus), &offp)) == NULL) {
    563  1.1  itojun 			pfsyncstats.pfsyncs_badlen++;
    564  1.1  itojun 			return;
    565  1.1  itojun 		}
    566  1.1  itojun 		bus = (struct pfsync_state_bus *)(mp->m_data + offp);
    567  1.1  itojun 		switch (bus->status) {
    568  1.1  itojun 		case PFSYNC_BUS_START:
    569  1.1  itojun 			timeout_add(&sc->sc_bulkfail_tmo,
    570  1.1  itojun 			    pf_pool_limits[PF_LIMIT_STATES].limit /
    571  1.1  itojun 			    (PFSYNC_BULKPACKETS * sc->sc_maxcount));
    572  1.1  itojun 			if (pf_status.debug >= PF_DEBUG_MISC)
    573  1.1  itojun 				printf("pfsync: received bulk "
    574  1.1  itojun 				    "update start\n");
    575  1.1  itojun 			break;
    576  1.1  itojun 		case PFSYNC_BUS_END:
    577  1.1  itojun 			if (mono_time.tv_sec - ntohl(bus->endtime) >=
    578  1.1  itojun 			    sc->sc_ureq_sent) {
    579  1.1  itojun 				/* that's it, we're happy */
    580  1.1  itojun 				sc->sc_ureq_sent = 0;
    581  1.1  itojun 				sc->sc_bulk_tries = 0;
    582  1.1  itojun 				timeout_del(&sc->sc_bulkfail_tmo);
    583  1.1  itojun 				pfsync_sync_ok = 1;
    584  1.1  itojun 				if (pf_status.debug >= PF_DEBUG_MISC)
    585  1.1  itojun 					printf("pfsync: received valid "
    586  1.1  itojun 					    "bulk update end\n");
    587  1.1  itojun 			} else {
    588  1.1  itojun 				if (pf_status.debug >= PF_DEBUG_MISC)
    589  1.1  itojun 					printf("pfsync: received invalid "
    590  1.1  itojun 					    "bulk update end: bad timestamp\n");
    591  1.1  itojun 			}
    592  1.1  itojun 			break;
    593  1.1  itojun 		}
    594  1.1  itojun 		break;
    595  1.1  itojun 	}
    596  1.1  itojun 
    597  1.1  itojun done:
    598  1.1  itojun 	if (m)
    599  1.1  itojun 		m_freem(m);
    600  1.1  itojun }
    601  1.1  itojun 
    602  1.1  itojun int
    603  1.1  itojun pfsyncoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
    604  1.1  itojun 	struct rtentry *rt)
    605  1.1  itojun {
    606  1.1  itojun 	m_freem(m);
    607  1.1  itojun 	return (0);
    608  1.1  itojun }
    609  1.1  itojun 
    610  1.1  itojun /* ARGSUSED */
    611  1.1  itojun int
    612  1.1  itojun pfsyncioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    613  1.1  itojun {
    614  1.1  itojun 	struct proc *p = curproc;
    615  1.1  itojun 	struct pfsync_softc *sc = ifp->if_softc;
    616  1.1  itojun 	struct ifreq *ifr = (struct ifreq *)data;
    617  1.1  itojun 	struct ip_moptions *imo = &sc->sc_imo;
    618  1.1  itojun 	struct pfsyncreq pfsyncr;
    619  1.1  itojun 	struct ifnet    *sifp;
    620  1.1  itojun 	int s, error;
    621  1.1  itojun 
    622  1.1  itojun 	switch (cmd) {
    623  1.1  itojun 	case SIOCSIFADDR:
    624  1.1  itojun 	case SIOCAIFADDR:
    625  1.1  itojun 	case SIOCSIFDSTADDR:
    626  1.1  itojun 	case SIOCSIFFLAGS:
    627  1.1  itojun 		if (ifp->if_flags & IFF_UP)
    628  1.1  itojun 			ifp->if_flags |= IFF_RUNNING;
    629  1.1  itojun 		else
    630  1.1  itojun 			ifp->if_flags &= ~IFF_RUNNING;
    631  1.1  itojun 		break;
    632  1.1  itojun 	case SIOCSIFMTU:
    633  1.1  itojun 		if (ifr->ifr_mtu < PFSYNC_MINMTU)
    634  1.1  itojun 			return (EINVAL);
    635  1.1  itojun 		if (ifr->ifr_mtu > MCLBYTES)
    636  1.1  itojun 			ifr->ifr_mtu = MCLBYTES;
    637  1.1  itojun 		s = splnet();
    638  1.1  itojun 		if (ifr->ifr_mtu < ifp->if_mtu)
    639  1.1  itojun 			pfsync_sendout(sc);
    640  1.1  itojun 		pfsync_setmtu(sc, ifr->ifr_mtu);
    641  1.1  itojun 		splx(s);
    642  1.1  itojun 		break;
    643  1.1  itojun 	case SIOCGETPFSYNC:
    644  1.1  itojun 		bzero(&pfsyncr, sizeof(pfsyncr));
    645  1.1  itojun 		if (sc->sc_sync_ifp)
    646  1.1  itojun 			strlcpy(pfsyncr.pfsyncr_syncif,
    647  1.1  itojun 			    sc->sc_sync_ifp->if_xname, IFNAMSIZ);
    648  1.1  itojun 		pfsyncr.pfsyncr_maxupdates = sc->sc_maxupdates;
    649  1.1  itojun 		if ((error = copyout(&pfsyncr, ifr->ifr_data, sizeof(pfsyncr))))
    650  1.1  itojun 			return (error);
    651  1.1  itojun 		break;
    652  1.1  itojun 	case SIOCSETPFSYNC:
    653  1.1  itojun 		if ((error = suser(p, p->p_acflag)) != 0)
    654  1.1  itojun 			return (error);
    655  1.1  itojun 		if ((error = copyin(ifr->ifr_data, &pfsyncr, sizeof(pfsyncr))))
    656  1.1  itojun 			return (error);
    657  1.1  itojun 
    658  1.1  itojun 		if (pfsyncr.pfsyncr_maxupdates > 255)
    659  1.1  itojun 			return (EINVAL);
    660  1.1  itojun 		sc->sc_maxupdates = pfsyncr.pfsyncr_maxupdates;
    661  1.1  itojun 
    662  1.1  itojun 		if (pfsyncr.pfsyncr_syncif[0] == 0) {
    663  1.1  itojun 			sc->sc_sync_ifp = NULL;
    664  1.1  itojun 			if (sc->sc_mbuf_net != NULL) {
    665  1.1  itojun 				/* Don't keep stale pfsync packets around. */
    666  1.1  itojun 				s = splnet();
    667  1.1  itojun 				m_freem(sc->sc_mbuf_net);
    668  1.1  itojun 				sc->sc_mbuf_net = NULL;
    669  1.1  itojun 				sc->sc_statep_net.s = NULL;
    670  1.1  itojun 				splx(s);
    671  1.1  itojun 			}
    672  1.1  itojun 			break;
    673  1.1  itojun 		}
    674  1.1  itojun 		if ((sifp = ifunit(pfsyncr.pfsyncr_syncif)) == NULL)
    675  1.1  itojun 			return (EINVAL);
    676  1.1  itojun 		else if (sifp == sc->sc_sync_ifp)
    677  1.1  itojun 			break;
    678  1.1  itojun 
    679  1.1  itojun 		s = splnet();
    680  1.1  itojun 		if (sifp->if_mtu < sc->sc_if.if_mtu ||
    681  1.1  itojun 		    (sc->sc_sync_ifp != NULL &&
    682  1.1  itojun 		    sifp->if_mtu < sc->sc_sync_ifp->if_mtu) ||
    683  1.1  itojun 		    sifp->if_mtu < MCLBYTES - sizeof(struct ip))
    684  1.1  itojun 			pfsync_sendout(sc);
    685  1.1  itojun 		sc->sc_sync_ifp = sifp;
    686  1.1  itojun 
    687  1.1  itojun 		pfsync_setmtu(sc, sc->sc_if.if_mtu);
    688  1.1  itojun 
    689  1.1  itojun 		if (imo->imo_num_memberships > 0) {
    690  1.1  itojun 			in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
    691  1.1  itojun 			imo->imo_multicast_ifp = NULL;
    692  1.1  itojun 		}
    693  1.1  itojun 
    694  1.1  itojun 		if (sc->sc_sync_ifp) {
    695  1.1  itojun 			struct in_addr addr;
    696  1.1  itojun 
    697  1.1  itojun 			addr.s_addr = INADDR_PFSYNC_GROUP;
    698  1.1  itojun 			if ((imo->imo_membership[0] =
    699  1.1  itojun 			    in_addmulti(&addr, sc->sc_sync_ifp)) == NULL) {
    700  1.1  itojun 				splx(s);
    701  1.1  itojun 				return (ENOBUFS);
    702  1.1  itojun 			}
    703  1.1  itojun 			imo->imo_num_memberships++;
    704  1.1  itojun 			imo->imo_multicast_ifp = sc->sc_sync_ifp;
    705  1.1  itojun 			imo->imo_multicast_ttl = PFSYNC_DFLTTL;
    706  1.1  itojun 			imo->imo_multicast_loop = 0;
    707  1.1  itojun 
    708  1.1  itojun 			/* Request a full state table update. */
    709  1.1  itojun 			sc->sc_ureq_sent = mono_time.tv_sec;
    710  1.1  itojun 			pfsync_sync_ok = 0;
    711  1.1  itojun 			if (pf_status.debug >= PF_DEBUG_MISC)
    712  1.1  itojun 				printf("pfsync: requesting bulk update\n");
    713  1.1  itojun 			timeout_add(&sc->sc_bulkfail_tmo, 5 * hz);
    714  1.1  itojun 			pfsync_request_update(NULL, NULL);
    715  1.1  itojun 			pfsync_sendout(sc);
    716  1.1  itojun 		}
    717  1.1  itojun 		splx(s);
    718  1.1  itojun 
    719  1.1  itojun 		break;
    720  1.1  itojun 
    721  1.1  itojun 	default:
    722  1.1  itojun 		return (ENOTTY);
    723  1.1  itojun 	}
    724  1.1  itojun 
    725  1.1  itojun 	return (0);
    726  1.1  itojun }
    727  1.1  itojun 
    728  1.1  itojun void
    729  1.1  itojun pfsync_setmtu(struct pfsync_softc *sc, int mtu_req)
    730  1.1  itojun {
    731  1.1  itojun 	int mtu;
    732  1.1  itojun 
    733  1.1  itojun 	if (sc->sc_sync_ifp && sc->sc_sync_ifp->if_mtu < mtu_req)
    734  1.1  itojun 		mtu = sc->sc_sync_ifp->if_mtu;
    735  1.1  itojun 	else
    736  1.1  itojun 		mtu = mtu_req;
    737  1.1  itojun 
    738  1.1  itojun 	sc->sc_maxcount = (mtu - sizeof(struct pfsync_header)) /
    739  1.1  itojun 	    sizeof(struct pfsync_state);
    740  1.1  itojun 	if (sc->sc_maxcount > 254)
    741  1.1  itojun 	    sc->sc_maxcount = 254;
    742  1.1  itojun 	sc->sc_if.if_mtu = sizeof(struct pfsync_header) +
    743  1.1  itojun 	    sc->sc_maxcount * sizeof(struct pfsync_state);
    744  1.1  itojun }
    745  1.1  itojun 
    746  1.1  itojun struct mbuf *
    747  1.1  itojun pfsync_get_mbuf(struct pfsync_softc *sc, u_int8_t action, void **sp)
    748  1.1  itojun {
    749  1.1  itojun 	struct pfsync_header *h;
    750  1.1  itojun 	struct mbuf *m;
    751  1.1  itojun 	int len;
    752  1.1  itojun 
    753  1.1  itojun 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    754  1.1  itojun 	if (m == NULL) {
    755  1.1  itojun 		sc->sc_if.if_oerrors++;
    756  1.1  itojun 		return (NULL);
    757  1.1  itojun 	}
    758  1.1  itojun 
    759  1.1  itojun 	switch (action) {
    760  1.1  itojun 	case PFSYNC_ACT_CLR:
    761  1.1  itojun 		len = sizeof(struct pfsync_header) +
    762  1.1  itojun 		    sizeof(struct pfsync_state_clr);
    763  1.1  itojun 		break;
    764  1.1  itojun 	case PFSYNC_ACT_UPD_C:
    765  1.1  itojun 		len = (sc->sc_maxcount * sizeof(struct pfsync_state_upd)) +
    766  1.1  itojun 		    sizeof(struct pfsync_header);
    767  1.1  itojun 		break;
    768  1.1  itojun 	case PFSYNC_ACT_DEL_C:
    769  1.1  itojun 		len = (sc->sc_maxcount * sizeof(struct pfsync_state_del)) +
    770  1.1  itojun 		    sizeof(struct pfsync_header);
    771  1.1  itojun 		break;
    772  1.1  itojun 	case PFSYNC_ACT_UREQ:
    773  1.1  itojun 		len = (sc->sc_maxcount * sizeof(struct pfsync_state_upd_req)) +
    774  1.1  itojun 		    sizeof(struct pfsync_header);
    775  1.1  itojun 		break;
    776  1.1  itojun 	case PFSYNC_ACT_BUS:
    777  1.1  itojun 		len = sizeof(struct pfsync_header) +
    778  1.1  itojun 		    sizeof(struct pfsync_state_bus);
    779  1.1  itojun 		break;
    780  1.1  itojun 	default:
    781  1.1  itojun 		len = (sc->sc_maxcount * sizeof(struct pfsync_state)) +
    782  1.1  itojun 		    sizeof(struct pfsync_header);
    783  1.1  itojun 		break;
    784  1.1  itojun 	}
    785  1.1  itojun 
    786  1.1  itojun 	if (len > MHLEN) {
    787  1.1  itojun 		MCLGET(m, M_DONTWAIT);
    788  1.1  itojun 		if ((m->m_flags & M_EXT) == 0) {
    789  1.1  itojun 			m_free(m);
    790  1.1  itojun 			sc->sc_if.if_oerrors++;
    791  1.1  itojun 			return (NULL);
    792  1.1  itojun 		}
    793  1.1  itojun 		m->m_data += (MCLBYTES - len) &~ (sizeof(long) - 1);
    794  1.1  itojun 	} else
    795  1.1  itojun 		MH_ALIGN(m, len);
    796  1.1  itojun 
    797  1.1  itojun 	m->m_pkthdr.rcvif = NULL;
    798  1.1  itojun 	m->m_pkthdr.len = m->m_len = sizeof(struct pfsync_header);
    799  1.1  itojun 	h = mtod(m, struct pfsync_header *);
    800  1.1  itojun 	h->version = PFSYNC_VERSION;
    801  1.1  itojun 	h->af = 0;
    802  1.1  itojun 	h->count = 0;
    803  1.1  itojun 	h->action = action;
    804  1.1  itojun 
    805  1.1  itojun 	*sp = (void *)((char *)h + PFSYNC_HDRLEN);
    806  1.1  itojun 	timeout_add(&sc->sc_tmo, hz);
    807  1.1  itojun 	return (m);
    808  1.1  itojun }
    809  1.1  itojun 
    810  1.1  itojun int
    811  1.1  itojun pfsync_pack_state(u_int8_t action, struct pf_state *st, int compress)
    812  1.1  itojun {
    813  1.1  itojun 	struct ifnet *ifp = &pfsyncif.sc_if;
    814  1.1  itojun 	struct pfsync_softc *sc = ifp->if_softc;
    815  1.1  itojun 	struct pfsync_header *h, *h_net;
    816  1.1  itojun 	struct pfsync_state *sp = NULL;
    817  1.1  itojun 	struct pfsync_state_upd *up = NULL;
    818  1.1  itojun 	struct pfsync_state_del *dp = NULL;
    819  1.1  itojun 	struct pf_rule *r;
    820  1.1  itojun 	u_long secs;
    821  1.1  itojun 	int s, ret = 0;
    822  1.1  itojun 	u_int8_t i = 255, newaction = 0;
    823  1.1  itojun 
    824  1.1  itojun 	/*
    825  1.1  itojun 	 * If a packet falls in the forest and there's nobody around to
    826  1.1  itojun 	 * hear, does it make a sound?
    827  1.1  itojun 	 */
    828  1.1  itojun 	if (ifp->if_bpf == NULL && sc->sc_sync_ifp == NULL) {
    829  1.1  itojun 		/* Don't leave any stale pfsync packets hanging around. */
    830  1.1  itojun 		if (sc->sc_mbuf != NULL) {
    831  1.1  itojun 			m_freem(sc->sc_mbuf);
    832  1.1  itojun 			sc->sc_mbuf = NULL;
    833  1.1  itojun 			sc->sc_statep.s = NULL;
    834  1.1  itojun 		}
    835  1.1  itojun 		return (0);
    836  1.1  itojun 	}
    837  1.1  itojun 
    838  1.1  itojun 	if (action >= PFSYNC_ACT_MAX)
    839  1.1  itojun 		return (EINVAL);
    840  1.1  itojun 
    841  1.1  itojun 	s = splnet();
    842  1.1  itojun 	if (sc->sc_mbuf == NULL) {
    843  1.1  itojun 		if ((sc->sc_mbuf = pfsync_get_mbuf(sc, action,
    844  1.1  itojun 		    (void *)&sc->sc_statep.s)) == NULL) {
    845  1.1  itojun 			splx(s);
    846  1.1  itojun 			return (ENOMEM);
    847  1.1  itojun 		}
    848  1.1  itojun 		h = mtod(sc->sc_mbuf, struct pfsync_header *);
    849  1.1  itojun 	} else {
    850  1.1  itojun 		h = mtod(sc->sc_mbuf, struct pfsync_header *);
    851  1.1  itojun 		if (h->action != action) {
    852  1.1  itojun 			pfsync_sendout(sc);
    853  1.1  itojun 			if ((sc->sc_mbuf = pfsync_get_mbuf(sc, action,
    854  1.1  itojun 			    (void *)&sc->sc_statep.s)) == NULL) {
    855  1.1  itojun 				splx(s);
    856  1.1  itojun 				return (ENOMEM);
    857  1.1  itojun 			}
    858  1.1  itojun 			h = mtod(sc->sc_mbuf, struct pfsync_header *);
    859  1.1  itojun 		} else {
    860  1.1  itojun 			/*
    861  1.1  itojun 			 * If it's an update, look in the packet to see if
    862  1.1  itojun 			 * we already have an update for the state.
    863  1.1  itojun 			 */
    864  1.1  itojun 			if (action == PFSYNC_ACT_UPD && sc->sc_maxupdates) {
    865  1.1  itojun 				struct pfsync_state *usp =
    866  1.1  itojun 				    (void *)((char *)h + PFSYNC_HDRLEN);
    867  1.1  itojun 
    868  1.1  itojun 				for (i = 0; i < h->count; i++) {
    869  1.1  itojun 					if (!memcmp(usp->id, &st->id,
    870  1.1  itojun 					    PFSYNC_ID_LEN) &&
    871  1.1  itojun 					    usp->creatorid == st->creatorid) {
    872  1.1  itojun 						sp = usp;
    873  1.1  itojun 						sp->updates++;
    874  1.1  itojun 						break;
    875  1.1  itojun 					}
    876  1.1  itojun 					usp++;
    877  1.1  itojun 				}
    878  1.1  itojun 			}
    879  1.1  itojun 		}
    880  1.1  itojun 	}
    881  1.1  itojun 
    882  1.1  itojun 	secs = time.tv_sec;
    883  1.1  itojun 
    884  1.1  itojun 	st->pfsync_time = mono_time.tv_sec;
    885  1.1  itojun 	TAILQ_REMOVE(&state_updates, st, u.s.entry_updates);
    886  1.1  itojun 	TAILQ_INSERT_TAIL(&state_updates, st, u.s.entry_updates);
    887  1.1  itojun 
    888  1.1  itojun 	if (sp == NULL) {
    889  1.1  itojun 		/* not a "duplicate" update */
    890  1.1  itojun 		i = 255;
    891  1.1  itojun 		sp = sc->sc_statep.s++;
    892  1.1  itojun 		sc->sc_mbuf->m_pkthdr.len =
    893  1.1  itojun 		    sc->sc_mbuf->m_len += sizeof(struct pfsync_state);
    894  1.1  itojun 		h->count++;
    895  1.1  itojun 		bzero(sp, sizeof(*sp));
    896  1.1  itojun 
    897  1.1  itojun 		bcopy(&st->id, sp->id, sizeof(sp->id));
    898  1.1  itojun 		sp->creatorid = st->creatorid;
    899  1.1  itojun 
    900  1.1  itojun 		strlcpy(sp->ifname, st->u.s.kif->pfik_name, sizeof(sp->ifname));
    901  1.1  itojun 		pf_state_host_hton(&st->lan, &sp->lan);
    902  1.1  itojun 		pf_state_host_hton(&st->gwy, &sp->gwy);
    903  1.1  itojun 		pf_state_host_hton(&st->ext, &sp->ext);
    904  1.1  itojun 
    905  1.1  itojun 		bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr));
    906  1.1  itojun 
    907  1.1  itojun 		sp->creation = htonl(secs - st->creation);
    908  1.1  itojun 		sp->packets[0] = htonl(st->packets[0]);
    909  1.1  itojun 		sp->packets[1] = htonl(st->packets[1]);
    910  1.1  itojun 		sp->bytes[0] = htonl(st->bytes[0]);
    911  1.1  itojun 		sp->bytes[1] = htonl(st->bytes[1]);
    912  1.1  itojun 		if ((r = st->rule.ptr) == NULL)
    913  1.1  itojun 			sp->rule = htonl(-1);
    914  1.1  itojun 		else
    915  1.1  itojun 			sp->rule = htonl(r->nr);
    916  1.1  itojun 		if ((r = st->anchor.ptr) == NULL)
    917  1.1  itojun 			sp->anchor = htonl(-1);
    918  1.1  itojun 		else
    919  1.1  itojun 			sp->anchor = htonl(r->nr);
    920  1.1  itojun 		sp->af = st->af;
    921  1.1  itojun 		sp->proto = st->proto;
    922  1.1  itojun 		sp->direction = st->direction;
    923  1.1  itojun 		sp->log = st->log;
    924  1.1  itojun 		sp->allow_opts = st->allow_opts;
    925  1.1  itojun 		sp->timeout = st->timeout;
    926  1.1  itojun 
    927  1.1  itojun 		sp->sync_flags = st->sync_flags & PFSTATE_NOSYNC;
    928  1.1  itojun 	}
    929  1.1  itojun 
    930  1.1  itojun 	pf_state_peer_hton(&st->src, &sp->src);
    931  1.1  itojun 	pf_state_peer_hton(&st->dst, &sp->dst);
    932  1.1  itojun 
    933  1.1  itojun 	if (st->expire <= secs)
    934  1.1  itojun 		sp->expire = htonl(0);
    935  1.1  itojun 	else
    936  1.1  itojun 		sp->expire = htonl(st->expire - secs);
    937  1.1  itojun 
    938  1.1  itojun 	/* do we need to build "compressed" actions for network transfer? */
    939  1.1  itojun 	if (sc->sc_sync_ifp && compress) {
    940  1.1  itojun 		switch (action) {
    941  1.1  itojun 		case PFSYNC_ACT_UPD:
    942  1.1  itojun 			newaction = PFSYNC_ACT_UPD_C;
    943  1.1  itojun 			break;
    944  1.1  itojun 		case PFSYNC_ACT_DEL:
    945  1.1  itojun 			newaction = PFSYNC_ACT_DEL_C;
    946  1.1  itojun 			break;
    947  1.1  itojun 		default:
    948  1.1  itojun 			/* by default we just send the uncompressed states */
    949  1.1  itojun 			break;
    950  1.1  itojun 		}
    951  1.1  itojun 	}
    952  1.1  itojun 
    953  1.1  itojun 	if (newaction) {
    954  1.1  itojun 		if (sc->sc_mbuf_net == NULL) {
    955  1.1  itojun 			if ((sc->sc_mbuf_net = pfsync_get_mbuf(sc, newaction,
    956  1.1  itojun 			    (void *)&sc->sc_statep_net.s)) == NULL) {
    957  1.1  itojun 				splx(s);
    958  1.1  itojun 				return (ENOMEM);
    959  1.1  itojun 			}
    960  1.1  itojun 		}
    961  1.1  itojun 		h_net = mtod(sc->sc_mbuf_net, struct pfsync_header *);
    962  1.1  itojun 
    963  1.1  itojun 		switch (newaction) {
    964  1.1  itojun 		case PFSYNC_ACT_UPD_C:
    965  1.1  itojun 			if (i != 255) {
    966  1.1  itojun 				up = (void *)((char *)h_net +
    967  1.1  itojun 				    PFSYNC_HDRLEN + (i * sizeof(*up)));
    968  1.1  itojun 				up->updates++;
    969  1.1  itojun 			} else {
    970  1.1  itojun 				h_net->count++;
    971  1.1  itojun 				sc->sc_mbuf_net->m_pkthdr.len =
    972  1.1  itojun 				    sc->sc_mbuf_net->m_len += sizeof(*up);
    973  1.1  itojun 				up = sc->sc_statep_net.u++;
    974  1.1  itojun 
    975  1.1  itojun 				bzero(up, sizeof(*up));
    976  1.1  itojun 				bcopy(&st->id, up->id, sizeof(up->id));
    977  1.1  itojun 				up->creatorid = st->creatorid;
    978  1.1  itojun 			}
    979  1.1  itojun 			up->timeout = st->timeout;
    980  1.1  itojun 			up->expire = sp->expire;
    981  1.1  itojun 			up->src = sp->src;
    982  1.1  itojun 			up->dst = sp->dst;
    983  1.1  itojun 			break;
    984  1.1  itojun 		case PFSYNC_ACT_DEL_C:
    985  1.1  itojun 			sc->sc_mbuf_net->m_pkthdr.len =
    986  1.1  itojun 			    sc->sc_mbuf_net->m_len += sizeof(*dp);
    987  1.1  itojun 			dp = sc->sc_statep_net.d++;
    988  1.1  itojun 			h_net->count++;
    989  1.1  itojun 
    990  1.1  itojun 			bzero(dp, sizeof(*dp));
    991  1.1  itojun 			bcopy(&st->id, dp->id, sizeof(dp->id));
    992  1.1  itojun 			dp->creatorid = st->creatorid;
    993  1.1  itojun 			break;
    994  1.1  itojun 		}
    995  1.1  itojun 	}
    996  1.1  itojun 
    997  1.1  itojun 	if (h->count == sc->sc_maxcount ||
    998  1.1  itojun 	    (sc->sc_maxupdates && (sp->updates >= sc->sc_maxupdates)))
    999  1.1  itojun 		ret = pfsync_sendout(sc);
   1000  1.1  itojun 
   1001  1.1  itojun 	splx(s);
   1002  1.1  itojun 	return (ret);
   1003  1.1  itojun }
   1004  1.1  itojun 
   1005  1.1  itojun /* This must be called in splnet() */
   1006  1.1  itojun int
   1007  1.1  itojun pfsync_request_update(struct pfsync_state_upd *up, struct in_addr *src)
   1008  1.1  itojun {
   1009  1.1  itojun 	struct ifnet *ifp = &pfsyncif.sc_if;
   1010  1.1  itojun 	struct pfsync_header *h;
   1011  1.1  itojun 	struct pfsync_softc *sc = ifp->if_softc;
   1012  1.1  itojun 	struct pfsync_state_upd_req *rup;
   1013  1.1  itojun 	int s, ret;
   1014  1.1  itojun 
   1015  1.1  itojun 	if (sc->sc_mbuf == NULL) {
   1016  1.1  itojun 		if ((sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_UREQ,
   1017  1.1  itojun 		    (void *)&sc->sc_statep.s)) == NULL) {
   1018  1.1  itojun 			splx(s);
   1019  1.1  itojun 			return (ENOMEM);
   1020  1.1  itojun 		}
   1021  1.1  itojun 		h = mtod(sc->sc_mbuf, struct pfsync_header *);
   1022  1.1  itojun 	} else {
   1023  1.1  itojun 		h = mtod(sc->sc_mbuf, struct pfsync_header *);
   1024  1.1  itojun 		if (h->action != PFSYNC_ACT_UREQ) {
   1025  1.1  itojun 			pfsync_sendout(sc);
   1026  1.1  itojun 			if ((sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_UREQ,
   1027  1.1  itojun 			    (void *)&sc->sc_statep.s)) == NULL) {
   1028  1.1  itojun 				splx(s);
   1029  1.1  itojun 				return (ENOMEM);
   1030  1.1  itojun 			}
   1031  1.1  itojun 			h = mtod(sc->sc_mbuf, struct pfsync_header *);
   1032  1.1  itojun 		}
   1033  1.1  itojun 	}
   1034  1.1  itojun 
   1035  1.1  itojun 	if (src != NULL)
   1036  1.1  itojun 		sc->sc_sendaddr = *src;
   1037  1.1  itojun 	sc->sc_mbuf->m_pkthdr.len = sc->sc_mbuf->m_len += sizeof(*rup);
   1038  1.1  itojun 	h->count++;
   1039  1.1  itojun 	rup = sc->sc_statep.r++;
   1040  1.1  itojun 	bzero(rup, sizeof(*rup));
   1041  1.1  itojun 	if (up != NULL) {
   1042  1.1  itojun 		bcopy(up->id, rup->id, sizeof(rup->id));
   1043  1.1  itojun 		rup->creatorid = up->creatorid;
   1044  1.1  itojun 	}
   1045  1.1  itojun 
   1046  1.1  itojun 	if (h->count == sc->sc_maxcount)
   1047  1.1  itojun 		ret = pfsync_sendout(sc);
   1048  1.1  itojun 
   1049  1.1  itojun 	return (ret);
   1050  1.1  itojun }
   1051  1.1  itojun 
   1052  1.1  itojun int
   1053  1.1  itojun pfsync_clear_states(u_int32_t creatorid, char *ifname)
   1054  1.1  itojun {
   1055  1.1  itojun 	struct ifnet *ifp = &pfsyncif.sc_if;
   1056  1.1  itojun 	struct pfsync_softc *sc = ifp->if_softc;
   1057  1.1  itojun 	struct pfsync_state_clr *cp;
   1058  1.1  itojun 	int s, ret;
   1059  1.1  itojun 
   1060  1.1  itojun 	s = splnet();
   1061  1.1  itojun 	if (sc->sc_mbuf != NULL)
   1062  1.1  itojun 		pfsync_sendout(sc);
   1063  1.1  itojun 	if ((sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_CLR,
   1064  1.1  itojun 	    (void *)&sc->sc_statep.c)) == NULL) {
   1065  1.1  itojun 		splx(s);
   1066  1.1  itojun 		return (ENOMEM);
   1067  1.1  itojun 	}
   1068  1.1  itojun 	sc->sc_mbuf->m_pkthdr.len = sc->sc_mbuf->m_len += sizeof(*cp);
   1069  1.1  itojun 	cp = sc->sc_statep.c;
   1070  1.1  itojun 	cp->creatorid = creatorid;
   1071  1.1  itojun 	if (ifname != NULL)
   1072  1.1  itojun 		strlcpy(cp->ifname, ifname, IFNAMSIZ);
   1073  1.1  itojun 
   1074  1.1  itojun 	ret = (pfsync_sendout(sc));
   1075  1.1  itojun 	splx(s);
   1076  1.1  itojun 	return (ret);
   1077  1.1  itojun }
   1078  1.1  itojun 
   1079  1.1  itojun void
   1080  1.1  itojun pfsync_timeout(void *v)
   1081  1.1  itojun {
   1082  1.1  itojun 	struct pfsync_softc *sc = v;
   1083  1.1  itojun 	int s;
   1084  1.1  itojun 
   1085  1.1  itojun 	s = splnet();
   1086  1.1  itojun 	pfsync_sendout(sc);
   1087  1.1  itojun 	splx(s);
   1088  1.1  itojun }
   1089  1.1  itojun 
   1090  1.1  itojun void
   1091  1.1  itojun pfsync_send_bus(struct pfsync_softc *sc, u_int8_t status)
   1092  1.1  itojun {
   1093  1.1  itojun 	struct pfsync_state_bus *bus;
   1094  1.1  itojun 
   1095  1.1  itojun 	if (sc->sc_mbuf != NULL)
   1096  1.1  itojun 		pfsync_sendout(sc);
   1097  1.1  itojun 
   1098  1.1  itojun 	if (pfsync_sync_ok &&
   1099  1.1  itojun 	    (sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_BUS,
   1100  1.1  itojun 	    (void *)&sc->sc_statep.b)) != NULL) {
   1101  1.1  itojun 		sc->sc_mbuf->m_pkthdr.len = sc->sc_mbuf->m_len += sizeof(*bus);
   1102  1.1  itojun 		bus = sc->sc_statep.b;
   1103  1.1  itojun 		bus->creatorid = pf_status.hostid;
   1104  1.1  itojun 		bus->status = status;
   1105  1.1  itojun 		bus->endtime = htonl(mono_time.tv_sec - sc->sc_ureq_received);
   1106  1.1  itojun 		pfsync_sendout(sc);
   1107  1.1  itojun 	}
   1108  1.1  itojun }
   1109  1.1  itojun 
   1110  1.1  itojun void
   1111  1.1  itojun pfsync_bulk_update(void *v)
   1112  1.1  itojun {
   1113  1.1  itojun 	struct pfsync_softc *sc = v;
   1114  1.1  itojun 	int s, i = 0;
   1115  1.1  itojun 	struct pf_state *state;
   1116  1.1  itojun 
   1117  1.1  itojun 	s = splnet();
   1118  1.1  itojun 	if (sc->sc_mbuf != NULL)
   1119  1.1  itojun 		pfsync_sendout(sc);
   1120  1.1  itojun 
   1121  1.1  itojun 	/*
   1122  1.1  itojun 	 * Grab at most PFSYNC_BULKPACKETS worth of states which have not
   1123  1.1  itojun 	 * been sent since the latest request was made.
   1124  1.1  itojun 	 */
   1125  1.1  itojun 	while ((state = TAILQ_FIRST(&state_updates)) != NULL &&
   1126  1.1  itojun 	    ++i < (sc->sc_maxcount * PFSYNC_BULKPACKETS)) {
   1127  1.1  itojun 		if (state->pfsync_time > sc->sc_ureq_received) {
   1128  1.1  itojun 			/* we're done */
   1129  1.1  itojun 			pfsync_send_bus(sc, PFSYNC_BUS_END);
   1130  1.1  itojun 			sc->sc_ureq_received = 0;
   1131  1.1  itojun 			timeout_del(&sc->sc_bulk_tmo);
   1132  1.1  itojun 			if (pf_status.debug >= PF_DEBUG_MISC)
   1133  1.1  itojun 				printf("pfsync: bulk update complete\n");
   1134  1.1  itojun 			break;
   1135  1.1  itojun 		} else {
   1136  1.1  itojun 			/* send an update and move to end of list */
   1137  1.1  itojun 			if (!state->sync_flags)
   1138  1.1  itojun 				pfsync_pack_state(PFSYNC_ACT_UPD, state, 0);
   1139  1.1  itojun 			state->pfsync_time = mono_time.tv_sec;
   1140  1.1  itojun 			TAILQ_REMOVE(&state_updates, state, u.s.entry_updates);
   1141  1.1  itojun 			TAILQ_INSERT_TAIL(&state_updates, state,
   1142  1.1  itojun 			    u.s.entry_updates);
   1143  1.1  itojun 
   1144  1.1  itojun 			/* look again for more in a bit */
   1145  1.1  itojun 			timeout_add(&sc->sc_bulk_tmo, 1);
   1146  1.1  itojun 		}
   1147  1.1  itojun 	}
   1148  1.1  itojun 	if (sc->sc_mbuf != NULL)
   1149  1.1  itojun 		pfsync_sendout(sc);
   1150  1.1  itojun 	splx(s);
   1151  1.1  itojun }
   1152  1.1  itojun 
   1153  1.1  itojun void
   1154  1.1  itojun pfsync_bulkfail(void *v)
   1155  1.1  itojun {
   1156  1.1  itojun 	struct pfsync_softc *sc = v;
   1157  1.1  itojun 
   1158  1.1  itojun 	if (sc->sc_bulk_tries++ < PFSYNC_MAX_BULKTRIES) {
   1159  1.1  itojun 		/* Try again in a bit */
   1160  1.1  itojun 		timeout_add(&sc->sc_bulkfail_tmo, 5 * hz);
   1161  1.1  itojun 		pfsync_request_update(NULL, NULL);
   1162  1.1  itojun 		pfsync_sendout(sc);
   1163  1.1  itojun 	} else {
   1164  1.1  itojun 		/* Pretend like the transfer was ok */
   1165  1.1  itojun 		sc->sc_ureq_sent = 0;
   1166  1.1  itojun 		sc->sc_bulk_tries = 0;
   1167  1.1  itojun 		pfsync_sync_ok = 1;
   1168  1.1  itojun 		if (pf_status.debug >= PF_DEBUG_MISC)
   1169  1.1  itojun 			printf("pfsync: failed to receive "
   1170  1.1  itojun 			    "bulk update status\n");
   1171  1.1  itojun 		timeout_del(&sc->sc_bulkfail_tmo);
   1172  1.1  itojun 	}
   1173  1.1  itojun }
   1174  1.1  itojun 
   1175  1.1  itojun int
   1176  1.1  itojun pfsync_sendout(sc)
   1177  1.1  itojun 	struct pfsync_softc *sc;
   1178  1.1  itojun {
   1179  1.1  itojun 	struct ifnet *ifp = &sc->sc_if;
   1180  1.1  itojun 	struct mbuf *m;
   1181  1.1  itojun 
   1182  1.1  itojun 	timeout_del(&sc->sc_tmo);
   1183  1.1  itojun 
   1184  1.1  itojun 	if (sc->sc_mbuf == NULL)
   1185  1.1  itojun 		return (0);
   1186  1.1  itojun 	m = sc->sc_mbuf;
   1187  1.1  itojun 	sc->sc_mbuf = NULL;
   1188  1.1  itojun 	sc->sc_statep.s = NULL;
   1189  1.1  itojun 
   1190  1.1  itojun #if NBPFILTER > 0
   1191  1.1  itojun 	if (ifp->if_bpf)
   1192  1.1  itojun 		bpf_mtap(ifp->if_bpf, m);
   1193  1.1  itojun #endif
   1194  1.1  itojun 
   1195  1.1  itojun 	if (sc->sc_mbuf_net) {
   1196  1.1  itojun 		m_freem(m);
   1197  1.1  itojun 		m = sc->sc_mbuf_net;
   1198  1.1  itojun 		sc->sc_mbuf_net = NULL;
   1199  1.1  itojun 		sc->sc_statep_net.s = NULL;
   1200  1.1  itojun 	}
   1201  1.1  itojun 
   1202  1.1  itojun 	if (sc->sc_sync_ifp) {
   1203  1.1  itojun 		struct ip *ip;
   1204  1.1  itojun 		struct ifaddr *ifa;
   1205  1.1  itojun 		struct sockaddr sa;
   1206  1.1  itojun 
   1207  1.1  itojun 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
   1208  1.1  itojun 		if (m == NULL) {
   1209  1.1  itojun 			pfsyncstats.pfsyncs_onomem++;
   1210  1.1  itojun 			return (0);
   1211  1.1  itojun 		}
   1212  1.1  itojun 		ip = mtod(m, struct ip *);
   1213  1.1  itojun 		ip->ip_v = IPVERSION;
   1214  1.1  itojun 		ip->ip_hl = sizeof(*ip) >> 2;
   1215  1.1  itojun 		ip->ip_tos = IPTOS_LOWDELAY;
   1216  1.1  itojun 		ip->ip_len = htons(m->m_pkthdr.len);
   1217  1.1  itojun 		ip->ip_id = htons(ip_randomid());
   1218  1.1  itojun 		ip->ip_off = htons(IP_DF);
   1219  1.1  itojun 		ip->ip_ttl = PFSYNC_DFLTTL;
   1220  1.1  itojun 		ip->ip_p = IPPROTO_PFSYNC;
   1221  1.1  itojun 		ip->ip_sum = 0;
   1222  1.1  itojun 
   1223  1.1  itojun 		bzero(&sa, sizeof(sa));
   1224  1.1  itojun 		sa.sa_family = AF_INET;
   1225  1.1  itojun 		ifa = ifaof_ifpforaddr(&sa, sc->sc_sync_ifp);
   1226  1.1  itojun 		if (ifa == NULL)
   1227  1.1  itojun 			return (0);
   1228  1.1  itojun 		ip->ip_src.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
   1229  1.1  itojun 
   1230  1.1  itojun 		if (sc->sc_sendaddr.s_addr == INADDR_PFSYNC_GROUP)
   1231  1.1  itojun 			m->m_flags |= M_MCAST;
   1232  1.1  itojun 		ip->ip_dst = sc->sc_sendaddr;
   1233  1.1  itojun 		sc->sc_sendaddr.s_addr = INADDR_PFSYNC_GROUP;
   1234  1.1  itojun 
   1235  1.1  itojun 		pfsyncstats.pfsyncs_opackets++;
   1236  1.1  itojun 
   1237  1.1  itojun 		if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL))
   1238  1.1  itojun 			pfsyncstats.pfsyncs_oerrors++;
   1239  1.1  itojun 	} else
   1240  1.1  itojun 		m_freem(m);
   1241  1.1  itojun 
   1242  1.1  itojun 	return (0);
   1243  1.1  itojun }
   1244