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