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