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