Changeset View
Changeset View
Standalone View
Standalone View
sys/netpfil/pf/if_pfsync.c
| Show First 20 Lines • Show All 147 Lines • ▼ Show 20 Lines | static int (*pfsync_acts[])(struct mbuf *, int, int, int, int) = { | ||||
| pfsync_in_del_c, /* PFSYNC_ACT_DEL_C */ | pfsync_in_del_c, /* PFSYNC_ACT_DEL_C */ | ||||
| pfsync_in_error, /* PFSYNC_ACT_INS_F */ | pfsync_in_error, /* PFSYNC_ACT_INS_F */ | ||||
| pfsync_in_error, /* PFSYNC_ACT_DEL_F */ | pfsync_in_error, /* PFSYNC_ACT_DEL_F */ | ||||
| pfsync_in_bus, /* PFSYNC_ACT_BUS */ | pfsync_in_bus, /* PFSYNC_ACT_BUS */ | ||||
| pfsync_in_tdb, /* PFSYNC_ACT_TDB */ | pfsync_in_tdb, /* PFSYNC_ACT_TDB */ | ||||
| pfsync_in_eof, /* PFSYNC_ACT_EOF */ | pfsync_in_eof, /* PFSYNC_ACT_EOF */ | ||||
| pfsync_in_ins, /* PFSYNC_ACT_INS_1400 */ | pfsync_in_ins, /* PFSYNC_ACT_INS_1400 */ | ||||
| pfsync_in_upd, /* PFSYNC_ACT_UPD_1400 */ | pfsync_in_upd, /* PFSYNC_ACT_UPD_1400 */ | ||||
| pfsync_in_ins, /* PFSYNC_ACT_INS_1500 */ | |||||
| pfsync_in_upd, /* PFSYNC_ACT_UPD_1500 */ | |||||
| }; | }; | ||||
| struct pfsync_q { | struct pfsync_q { | ||||
| void (*write)(struct pf_kstate *, void *); | void (*write)(struct pf_kstate *, void *); | ||||
| size_t len; | size_t len; | ||||
| u_int8_t action; | u_int8_t action; | ||||
| }; | }; | ||||
| /* We have the following sync queues */ | /* We have the following sync queues */ | ||||
| enum pfsync_q_id { | enum pfsync_q_id { | ||||
| PFSYNC_Q_INS_1301, | PFSYNC_Q_INS_1301, | ||||
| PFSYNC_Q_INS_1400, | PFSYNC_Q_INS_1400, | ||||
| PFSYNC_Q_INS_1500, | |||||
| PFSYNC_Q_IACK, | PFSYNC_Q_IACK, | ||||
| PFSYNC_Q_UPD_1301, | PFSYNC_Q_UPD_1301, | ||||
| PFSYNC_Q_UPD_1400, | PFSYNC_Q_UPD_1400, | ||||
| PFSYNC_Q_UPD_1500, | |||||
| PFSYNC_Q_UPD_C, | PFSYNC_Q_UPD_C, | ||||
| PFSYNC_Q_DEL_C, | PFSYNC_Q_DEL_C, | ||||
| PFSYNC_Q_COUNT, | PFSYNC_Q_COUNT, | ||||
| }; | }; | ||||
| /* Functions for building messages for given queue */ | /* Functions for building messages for given queue */ | ||||
| static void pfsync_out_state_1301(struct pf_kstate *, void *); | static void pfsync_out_state_1301(struct pf_kstate *, void *); | ||||
| static void pfsync_out_state_1400(struct pf_kstate *, void *); | static void pfsync_out_state_1400(struct pf_kstate *, void *); | ||||
| static void pfsync_out_state_1500(struct pf_kstate *, void *); | |||||
| static void pfsync_out_iack(struct pf_kstate *, void *); | static void pfsync_out_iack(struct pf_kstate *, void *); | ||||
| static void pfsync_out_upd_c(struct pf_kstate *, void *); | static void pfsync_out_upd_c(struct pf_kstate *, void *); | ||||
| static void pfsync_out_del_c(struct pf_kstate *, void *); | static void pfsync_out_del_c(struct pf_kstate *, void *); | ||||
| /* Attach those functions to queue */ | /* Attach those functions to queue */ | ||||
| static struct pfsync_q pfsync_qs[] = { | static struct pfsync_q pfsync_qs[] = { | ||||
| { pfsync_out_state_1301, sizeof(struct pfsync_state_1301), PFSYNC_ACT_INS_1301 }, | { pfsync_out_state_1301, sizeof(struct pfsync_state_1301), PFSYNC_ACT_INS_1301 }, | ||||
| { pfsync_out_state_1400, sizeof(struct pfsync_state_1400), PFSYNC_ACT_INS_1400 }, | { pfsync_out_state_1400, sizeof(struct pfsync_state_1400), PFSYNC_ACT_INS_1400 }, | ||||
| { pfsync_out_state_1500, sizeof(struct pfsync_state_1500), PFSYNC_ACT_INS_1500 }, | |||||
| { pfsync_out_iack, sizeof(struct pfsync_ins_ack), PFSYNC_ACT_INS_ACK }, | { pfsync_out_iack, sizeof(struct pfsync_ins_ack), PFSYNC_ACT_INS_ACK }, | ||||
| { pfsync_out_state_1301, sizeof(struct pfsync_state_1301), PFSYNC_ACT_UPD_1301 }, | { pfsync_out_state_1301, sizeof(struct pfsync_state_1301), PFSYNC_ACT_UPD_1301 }, | ||||
| { pfsync_out_state_1400, sizeof(struct pfsync_state_1400), PFSYNC_ACT_UPD_1400 }, | { pfsync_out_state_1400, sizeof(struct pfsync_state_1400), PFSYNC_ACT_UPD_1400 }, | ||||
| { pfsync_out_state_1500, sizeof(struct pfsync_state_1500), PFSYNC_ACT_UPD_1500 }, | |||||
| { pfsync_out_upd_c, sizeof(struct pfsync_upd_c), PFSYNC_ACT_UPD_C }, | { pfsync_out_upd_c, sizeof(struct pfsync_upd_c), PFSYNC_ACT_UPD_C }, | ||||
| { pfsync_out_del_c, sizeof(struct pfsync_del_c), PFSYNC_ACT_DEL_C } | { pfsync_out_del_c, sizeof(struct pfsync_del_c), PFSYNC_ACT_DEL_C } | ||||
| }; | }; | ||||
| /* Map queue to pf_kstate->sync_state */ | /* Map queue to pf_kstate->sync_state */ | ||||
| static u_int8_t pfsync_qid_sstate[] = { | static u_int8_t pfsync_qid_sstate[] = { | ||||
| PFSYNC_S_INS, /* PFSYNC_Q_INS_1301 */ | PFSYNC_S_INS, /* PFSYNC_Q_INS_1301 */ | ||||
| PFSYNC_S_INS, /* PFSYNC_Q_INS_1400 */ | PFSYNC_S_INS, /* PFSYNC_Q_INS_1400 */ | ||||
| PFSYNC_S_INS, /* PFSYNC_Q_INS_1500 */ | |||||
| PFSYNC_S_IACK, /* PFSYNC_Q_IACK */ | PFSYNC_S_IACK, /* PFSYNC_Q_IACK */ | ||||
| PFSYNC_S_UPD, /* PFSYNC_Q_UPD_1301 */ | PFSYNC_S_UPD, /* PFSYNC_Q_UPD_1301 */ | ||||
| PFSYNC_S_UPD, /* PFSYNC_Q_UPD_1400 */ | PFSYNC_S_UPD, /* PFSYNC_Q_UPD_1400 */ | ||||
| PFSYNC_S_UPD, /* PFSYNC_Q_UPD_1500 */ | |||||
| PFSYNC_S_UPD_C, /* PFSYNC_Q_UPD_C */ | PFSYNC_S_UPD_C, /* PFSYNC_Q_UPD_C */ | ||||
| PFSYNC_S_DEL_C, /* PFSYNC_Q_DEL_C */ | PFSYNC_S_DEL_C, /* PFSYNC_Q_DEL_C */ | ||||
| }; | }; | ||||
| /* Map pf_kstate->sync_state to queue */ | /* Map pf_kstate->sync_state to queue */ | ||||
| static enum pfsync_q_id pfsync_sstate_to_qid(u_int8_t); | static enum pfsync_q_id pfsync_sstate_to_qid(u_int8_t); | ||||
| static void pfsync_q_ins(struct pf_kstate *, int sync_state, bool); | static void pfsync_q_ins(struct pf_kstate *, int sync_state, bool); | ||||
| ▲ Show 20 Lines • Show All 311 Lines • ▼ Show 20 Lines | pfsync_state_import(union pfsync_state_union *sp, int flags, int msg_version) | ||||
| struct pfsync_softc *sc = V_pfsyncif; | struct pfsync_softc *sc = V_pfsyncif; | ||||
| #ifndef __NO_STRICT_ALIGNMENT | #ifndef __NO_STRICT_ALIGNMENT | ||||
| struct pfsync_state_key key[2]; | struct pfsync_state_key key[2]; | ||||
| #endif | #endif | ||||
| struct pfsync_state_key *kw, *ks; | struct pfsync_state_key *kw, *ks; | ||||
| struct pf_kstate *st = NULL; | struct pf_kstate *st = NULL; | ||||
| struct pf_state_key *skw = NULL, *sks = NULL; | struct pf_state_key *skw = NULL, *sks = NULL; | ||||
| struct pf_krule *r = NULL; | struct pf_krule *r = NULL; | ||||
| struct pfi_kkif *kif; | struct pfi_kkif *kif, *orig_kif; | ||||
| struct pfi_kkif *rt_kif = NULL; | struct pfi_kkif *rt_kif = NULL; | ||||
| struct pf_kpooladdr *rpool_first; | struct pf_kpooladdr *rpool_first; | ||||
| int error; | int error; | ||||
| int n = 0; | |||||
| sa_family_t rt_af = 0; | sa_family_t rt_af = 0; | ||||
| uint8_t rt = 0; | uint8_t rt = 0; | ||||
| int n = 0; | sa_family_t wire_af, stack_af; | ||||
| u_int8_t wire_proto, stack_proto; | |||||
| PF_RULES_RASSERT(); | PF_RULES_RASSERT(); | ||||
| if (sp->pfs_1301.creatorid == 0) { | if (sp->pfs_1301.creatorid == 0) { | ||||
| if (V_pf_status.debug >= PF_DEBUG_MISC) | if (V_pf_status.debug >= PF_DEBUG_MISC) | ||||
| printf("%s: invalid creator id: %08x\n", __func__, | printf("%s: invalid creator id: %08x\n", __func__, | ||||
| ntohl(sp->pfs_1301.creatorid)); | ntohl(sp->pfs_1301.creatorid)); | ||||
| return (EINVAL); | return (EINVAL); | ||||
| } | } | ||||
| if ((kif = pfi_kkif_find(sp->pfs_1301.ifname)) == NULL) { | /* | ||||
| * Check interfaces early on. Do it before allocating memory etc. | |||||
| * Because there is a high chance there will be a lot more such states. | |||||
| */ | |||||
| if ((kif = orig_kif = pfi_kkif_find(sp->pfs_1301.ifname)) == NULL) { | |||||
| if (V_pf_status.debug >= PF_DEBUG_MISC) | if (V_pf_status.debug >= PF_DEBUG_MISC) | ||||
| printf("%s: unknown interface: %s\n", __func__, | printf("%s: unknown interface: %s\n", __func__, | ||||
| sp->pfs_1301.ifname); | sp->pfs_1301.ifname); | ||||
| if (flags & PFSYNC_SI_IOCTL) | |||||
| return (EINVAL); | |||||
| return (0); /* skip this state */ | |||||
| } | |||||
| /* | |||||
| * States created with floating interface policy can be synchronized to | |||||
| * hosts with different interfaces, because they are bound to V_pfi_all. | |||||
| * But s->orig_kif still points to a real interface. Don't abort | |||||
| * importing the state if orig_kif does not exists on the importing host | |||||
| * but the state is not interface-bound. | |||||
| */ | |||||
| if (msg_version == PFSYNC_MSG_VERSION_1500) { | |||||
| orig_kif = pfi_kkif_find(sp->pfs_1500.orig_ifname); | |||||
| if (orig_kif == NULL) { | |||||
| if (kif == V_pfi_all) { | |||||
| orig_kif = kif; | |||||
| } else { | |||||
| if (V_pf_status.debug >= PF_DEBUG_MISC) | |||||
| printf("%s: unknown original interface:" | |||||
| " %s\n", __func__, | |||||
| sp->pfs_1500.orig_ifname); | |||||
| if (flags & PFSYNC_SI_IOCTL) | if (flags & PFSYNC_SI_IOCTL) | ||||
| return (EINVAL); | return (EINVAL); | ||||
| return (0); /* skip this state */ | return (0); /* skip this state */ | ||||
| } | } | ||||
| } | |||||
| } | |||||
| /* | /* | ||||
| * If the ruleset checksums match or the state is coming from the ioctl, | * If the ruleset checksums match or the state is coming from the ioctl, | ||||
| * it's safe to associate the state with the rule of that number. | * it's safe to associate the state with the rule of that number. | ||||
| */ | */ | ||||
| if (sp->pfs_1301.rule != htonl(-1) && sp->pfs_1301.anchor == htonl(-1) && | if (sp->pfs_1301.rule != htonl(-1) && sp->pfs_1301.anchor == htonl(-1) && | ||||
| (flags & (PFSYNC_SI_IOCTL | PFSYNC_SI_CKSUM)) && ntohl(sp->pfs_1301.rule) < | (flags & (PFSYNC_SI_IOCTL | PFSYNC_SI_CKSUM)) && ntohl(sp->pfs_1301.rule) < | ||||
| pf_main_ruleset.rules[PF_RULESET_FILTER].active.rcount) { | pf_main_ruleset.rules[PF_RULESET_FILTER].active.rcount) { | ||||
| TAILQ_FOREACH(r, pf_main_ruleset.rules[ | TAILQ_FOREACH(r, pf_main_ruleset.rules[ | ||||
| PF_RULESET_FILTER].active.ptr, entries) | PF_RULESET_FILTER].active.ptr, entries) | ||||
| if (ntohl(sp->pfs_1301.rule) == n++) | if (ntohl(sp->pfs_1301.rule) == n++) | ||||
| break; | break; | ||||
| } else | } else | ||||
| r = &V_pf_default_rule; | r = &V_pf_default_rule; | ||||
| /* | |||||
| * Check routing interface early on. Do it before allocating memory etc. | |||||
| * because there is a high chance there will be a lot more such states. | |||||
| */ | |||||
| switch (msg_version) { | switch (msg_version) { | ||||
| case PFSYNC_MSG_VERSION_1301: | case PFSYNC_MSG_VERSION_1301: | ||||
| /* | /* | ||||
| * On FreeBSD <= 13 the routing interface and routing operation | * On FreeBSD <= 13 the routing interface and routing operation | ||||
| * are not sent over pfsync. If the ruleset is identical, | * are not sent over pfsync. If the ruleset is identical, | ||||
| * though, we might be able to recover the routing information | * though, we might be able to recover the routing information | ||||
| * from the local ruleset. | * from the local ruleset. | ||||
| */ | */ | ||||
| Show All 34 Lines | if (r != &V_pf_default_rule) { | ||||
| * Ruleset different, routing *supposedly* requested, | * Ruleset different, routing *supposedly* requested, | ||||
| * give up on recovering. | * give up on recovering. | ||||
| */ | */ | ||||
| DPFPRINTF(PF_DEBUG_MISC, | DPFPRINTF(PF_DEBUG_MISC, | ||||
| "%s: can't recover routing information " | "%s: can't recover routing information " | ||||
| "because of different ruleset", __func__); | "because of different ruleset", __func__); | ||||
| return ((flags & PFSYNC_SI_IOCTL) ? EINVAL : 0); | return ((flags & PFSYNC_SI_IOCTL) ? EINVAL : 0); | ||||
| } | } | ||||
| wire_af = stack_af = sp->pfs_1301.af; | |||||
| wire_proto = stack_proto = sp->pfs_1301.proto; | |||||
| break; | break; | ||||
| case PFSYNC_MSG_VERSION_1400: | case PFSYNC_MSG_VERSION_1400: | ||||
| /* | /* | ||||
| * On FreeBSD 14 and above we're not taking any chances. | * On FreeBSD 14 we're not taking any chances. | ||||
| * We use the information synced to us. | * We use the information synced to us. | ||||
| */ | */ | ||||
| if (sp->pfs_1400.rt) { | if (sp->pfs_1400.rt) { | ||||
| rt_kif = pfi_kkif_find(sp->pfs_1400.rt_ifname); | rt_kif = pfi_kkif_find(sp->pfs_1400.rt_ifname); | ||||
| if (rt_kif == NULL) { | if (rt_kif == NULL) { | ||||
| DPFPRINTF(PF_DEBUG_MISC, | DPFPRINTF(PF_DEBUG_MISC, | ||||
| "%s: unknown route interface: %s", | "%s: unknown route interface: %s", | ||||
| __func__, sp->pfs_1400.rt_ifname); | __func__, sp->pfs_1400.rt_ifname); | ||||
| return ((flags & PFSYNC_SI_IOCTL) ? EINVAL : 0); | return ((flags & PFSYNC_SI_IOCTL) ? EINVAL : 0); | ||||
| } | } | ||||
| rt = sp->pfs_1400.rt; | rt = sp->pfs_1400.rt; | ||||
| /* | /* | ||||
| * Guess the AF of the route address, FreeBSD 14 does | * Guess the AF of the route address, FreeBSD 14 does | ||||
| * not support af-to nor prefer-ipv6-nexthop | * not support af-to nor prefer-ipv6-nexthop | ||||
| * so it should be safe. | * so it should be safe. | ||||
| */ | */ | ||||
| rt_af = sp->pfs_1400.af; | rt_af = sp->pfs_1400.af; | ||||
| } | |||||
| wire_af = stack_af = sp->pfs_1400.af; | |||||
| wire_proto = stack_proto = sp->pfs_1400.proto; | |||||
| break; | |||||
| case PFSYNC_MSG_VERSION_1500: | |||||
| /* | |||||
| * On FreeBSD 15 and above we're not taking any chances. | |||||
| * We use the information synced to us. | |||||
| */ | |||||
| if (sp->pfs_1500.rt) { | |||||
| rt_kif = pfi_kkif_find(sp->pfs_1500.rt_ifname); | |||||
| if (rt_kif == NULL) { | |||||
| DPFPRINTF(PF_DEBUG_MISC, | |||||
| "%s: unknown route interface: %s", | |||||
| __func__, sp->pfs_1500.rt_ifname); | |||||
| return ((flags & PFSYNC_SI_IOCTL) ? EINVAL : 0); | |||||
| } | } | ||||
| rt = sp->pfs_1500.rt; | |||||
| rt_af = sp->pfs_1500.rt_af; | |||||
| } | |||||
| wire_af = sp->pfs_1500.wire_af; | |||||
| stack_af = sp->pfs_1500.stack_af; | |||||
| wire_proto = sp->pfs_1500.wire_proto; | |||||
| stack_proto = sp->pfs_1500.stack_proto; | |||||
| break; | break; | ||||
| } | } | ||||
| if ((r->max_states && | if ((r->max_states && | ||||
| counter_u64_fetch(r->states_cur) >= r->max_states)) | counter_u64_fetch(r->states_cur) >= r->max_states)) | ||||
| goto cleanup; | goto cleanup; | ||||
| /* | /* | ||||
| Show All 10 Lines | #ifndef __NO_STRICT_ALIGNMENT | ||||
| bcopy(&sp->pfs_1301.key, key, sizeof(struct pfsync_state_key) * 2); | bcopy(&sp->pfs_1301.key, key, sizeof(struct pfsync_state_key) * 2); | ||||
| kw = &key[PF_SK_WIRE]; | kw = &key[PF_SK_WIRE]; | ||||
| ks = &key[PF_SK_STACK]; | ks = &key[PF_SK_STACK]; | ||||
| #else | #else | ||||
| kw = &sp->pfs_1301.key[PF_SK_WIRE]; | kw = &sp->pfs_1301.key[PF_SK_WIRE]; | ||||
| ks = &sp->pfs_1301.key[PF_SK_STACK]; | ks = &sp->pfs_1301.key[PF_SK_STACK]; | ||||
| #endif | #endif | ||||
| if (PF_ANEQ(&kw->addr[0], &ks->addr[0], sp->pfs_1301.af) || | if (wire_af != stack_af || | ||||
| PF_ANEQ(&kw->addr[1], &ks->addr[1], sp->pfs_1301.af) || | PF_ANEQ(&kw->addr[0], &ks->addr[0], wire_af) || | ||||
| PF_ANEQ(&kw->addr[1], &ks->addr[1], wire_af) || | |||||
| kw->port[0] != ks->port[0] || | kw->port[0] != ks->port[0] || | ||||
| kw->port[1] != ks->port[1]) { | kw->port[1] != ks->port[1]) { | ||||
| sks = uma_zalloc(V_pf_state_key_z, M_NOWAIT); | sks = uma_zalloc(V_pf_state_key_z, M_NOWAIT); | ||||
| if (sks == NULL) | if (sks == NULL) | ||||
| goto cleanup; | goto cleanup; | ||||
| } else | } else | ||||
| sks = skw; | sks = skw; | ||||
| /* allocate memory for scrub info */ | /* allocate memory for scrub info */ | ||||
| if (pfsync_alloc_scrub_memory(&sp->pfs_1301.src, &st->src) || | if (pfsync_alloc_scrub_memory(&sp->pfs_1301.src, &st->src) || | ||||
| pfsync_alloc_scrub_memory(&sp->pfs_1301.dst, &st->dst)) | pfsync_alloc_scrub_memory(&sp->pfs_1301.dst, &st->dst)) | ||||
| goto cleanup; | goto cleanup; | ||||
| /* Copy to state key(s). */ | /* Copy to state key(s). */ | ||||
| skw->addr[0] = kw->addr[0]; | skw->addr[0] = kw->addr[0]; | ||||
| skw->addr[1] = kw->addr[1]; | skw->addr[1] = kw->addr[1]; | ||||
| skw->port[0] = kw->port[0]; | skw->port[0] = kw->port[0]; | ||||
| skw->port[1] = kw->port[1]; | skw->port[1] = kw->port[1]; | ||||
| skw->proto = sp->pfs_1301.proto; | skw->proto = wire_proto; | ||||
| skw->af = sp->pfs_1301.af; | skw->af = wire_af; | ||||
| if (sks != skw) { | if (sks != skw) { | ||||
| sks->addr[0] = ks->addr[0]; | sks->addr[0] = ks->addr[0]; | ||||
| sks->addr[1] = ks->addr[1]; | sks->addr[1] = ks->addr[1]; | ||||
| sks->port[0] = ks->port[0]; | sks->port[0] = ks->port[0]; | ||||
| sks->port[1] = ks->port[1]; | sks->port[1] = ks->port[1]; | ||||
| sks->proto = sp->pfs_1301.proto; | sks->proto = stack_proto; | ||||
| sks->af = sp->pfs_1301.af; | sks->af = stack_af; | ||||
| } | } | ||||
| /* copy to state */ | /* copy to state */ | ||||
| bcopy(&sp->pfs_1301.rt_addr, &st->act.rt_addr, sizeof(st->act.rt_addr)); | |||||
| st->creation = (time_uptime - ntohl(sp->pfs_1301.creation)) * 1000; | st->creation = (time_uptime - ntohl(sp->pfs_1301.creation)) * 1000; | ||||
| st->expire = pf_get_uptime(); | |||||
| if (sp->pfs_1301.expire) { | |||||
| uint32_t timeout; | |||||
| timeout = r->timeout[sp->pfs_1301.timeout]; | |||||
| if (!timeout) | |||||
| timeout = V_pf_default_rule.timeout[sp->pfs_1301.timeout]; | |||||
| /* sp->expire may have been adaptively scaled by export. */ | |||||
| st->expire -= (timeout - ntohl(sp->pfs_1301.expire)) * 1000; | |||||
| } | |||||
| st->direction = sp->pfs_1301.direction; | |||||
| st->act.log = sp->pfs_1301.log; | |||||
| st->timeout = sp->pfs_1301.timeout; | |||||
| st->act.rt = rt; | st->act.rt = rt; | ||||
| st->act.rt_kif = rt_kif; | st->act.rt_kif = rt_kif; | ||||
| st->act.rt_af = rt_af; | st->act.rt_af = rt_af; | ||||
| switch (msg_version) { | switch (msg_version) { | ||||
| case PFSYNC_MSG_VERSION_1301: | case PFSYNC_MSG_VERSION_1301: | ||||
| st->state_flags = sp->pfs_1301.state_flags; | st->state_flags = sp->pfs_1301.state_flags; | ||||
| st->direction = sp->pfs_1301.direction; | |||||
| st->act.log = sp->pfs_1301.log; | |||||
| st->timeout = sp->pfs_1301.timeout; | |||||
| if (rt) | |||||
| bcopy(&sp->pfs_1301.rt_addr, &st->act.rt_addr, | |||||
| sizeof(st->act.rt_addr)); | |||||
| /* | /* | ||||
| * In FreeBSD 13 pfsync lacks many attributes. Copy them | * In FreeBSD 13 pfsync lacks many attributes. Copy them | ||||
| * from the rule if possible. If rule can't be matched | * from the rule if possible. If rule can't be matched | ||||
| * clear any set options as we can't recover their | * clear any set options as we can't recover their | ||||
| * parameters. | * parameters. | ||||
| */ | */ | ||||
| if (r == &V_pf_default_rule) { | if (r == &V_pf_default_rule) { | ||||
| st->state_flags &= ~PFSTATE_SETMASK; | st->state_flags &= ~PFSTATE_SETMASK; | ||||
| Show All 22 Lines | case PFSYNC_MSG_VERSION_1301: | ||||
| st->state_flags &= ~PFSTATE_DN_IS_PIPE; | st->state_flags &= ~PFSTATE_DN_IS_PIPE; | ||||
| } | } | ||||
| st->act.dnpipe = r->dnpipe; | st->act.dnpipe = r->dnpipe; | ||||
| st->act.dnrpipe = r->dnrpipe; | st->act.dnrpipe = r->dnrpipe; | ||||
| } | } | ||||
| break; | break; | ||||
| case PFSYNC_MSG_VERSION_1400: | case PFSYNC_MSG_VERSION_1400: | ||||
| st->state_flags = ntohs(sp->pfs_1400.state_flags); | st->state_flags = ntohs(sp->pfs_1400.state_flags); | ||||
| st->direction = sp->pfs_1400.direction; | |||||
| st->act.log = sp->pfs_1400.log; | |||||
| st->timeout = sp->pfs_1400.timeout; | |||||
| st->act.qid = ntohs(sp->pfs_1400.qid); | st->act.qid = ntohs(sp->pfs_1400.qid); | ||||
| st->act.pqid = ntohs(sp->pfs_1400.pqid); | st->act.pqid = ntohs(sp->pfs_1400.pqid); | ||||
| st->act.dnpipe = ntohs(sp->pfs_1400.dnpipe); | st->act.dnpipe = ntohs(sp->pfs_1400.dnpipe); | ||||
| st->act.dnrpipe = ntohs(sp->pfs_1400.dnrpipe); | st->act.dnrpipe = ntohs(sp->pfs_1400.dnrpipe); | ||||
| st->act.rtableid = ntohl(sp->pfs_1400.rtableid); | st->act.rtableid = ntohl(sp->pfs_1400.rtableid); | ||||
| st->act.min_ttl = sp->pfs_1400.min_ttl; | st->act.min_ttl = sp->pfs_1400.min_ttl; | ||||
| st->act.set_tos = sp->pfs_1400.set_tos; | st->act.set_tos = sp->pfs_1400.set_tos; | ||||
| st->act.max_mss = ntohs(sp->pfs_1400.max_mss); | st->act.max_mss = ntohs(sp->pfs_1400.max_mss); | ||||
| st->act.set_prio[0] = sp->pfs_1400.set_prio[0]; | st->act.set_prio[0] = sp->pfs_1400.set_prio[0]; | ||||
| st->act.set_prio[1] = sp->pfs_1400.set_prio[1]; | st->act.set_prio[1] = sp->pfs_1400.set_prio[1]; | ||||
| if (rt) | |||||
| bcopy(&sp->pfs_1400.rt_addr, &st->act.rt_addr, | |||||
| sizeof(st->act.rt_addr)); | |||||
| break; | break; | ||||
| case PFSYNC_MSG_VERSION_1500: | |||||
| st->state_flags = ntohs(sp->pfs_1500.state_flags); | |||||
| st->direction = sp->pfs_1500.direction; | |||||
| st->act.log = sp->pfs_1500.log; | |||||
| st->timeout = sp->pfs_1500.timeout; | |||||
| st->act.qid = ntohs(sp->pfs_1500.qid); | |||||
| st->act.pqid = ntohs(sp->pfs_1500.pqid); | |||||
vegeta_tuxpowered.net: Under which conditions can queues have different ids on two routers? Can this happen even if… | |||||
Not Done Inline ActionsI suspect the answer there is "If the rules/queues config has ever been different in those two pf instances". That's not ideal, but at least it's no more broken than it's ever been. kp: I suspect the answer there is "If the rules/queues config has ever been different in those two… | |||||
| st->act.dnpipe = ntohs(sp->pfs_1500.dnpipe); | |||||
| st->act.dnrpipe = ntohs(sp->pfs_1500.dnrpipe); | |||||
| st->act.rtableid = ntohl(sp->pfs_1500.rtableid); | |||||
| st->act.min_ttl = sp->pfs_1500.min_ttl; | |||||
| st->act.set_tos = sp->pfs_1500.set_tos; | |||||
| st->act.max_mss = ntohs(sp->pfs_1500.max_mss); | |||||
| st->act.set_prio[0] = sp->pfs_1500.set_prio[0]; | |||||
| st->act.set_prio[1] = sp->pfs_1500.set_prio[1]; | |||||
| if (rt) | |||||
| bcopy(&sp->pfs_1500.rt_addr, &st->act.rt_addr, | |||||
| sizeof(st->act.rt_addr)); | |||||
| if (sp->pfs_1500.tagname[0] != 0) | |||||
| st->tag = pf_tagname2tag(sp->pfs_1500.tagname); | |||||
| break; | |||||
| default: | default: | ||||
| panic("%s: Unsupported pfsync_msg_version %d", | panic("%s: Unsupported pfsync_msg_version %d", | ||||
| __func__, msg_version); | __func__, msg_version); | ||||
| } | |||||
| st->expire = pf_get_uptime(); | |||||
| if (sp->pfs_1301.expire) { | |||||
| uint32_t timeout; | |||||
| timeout = r->timeout[st->timeout]; | |||||
| if (!timeout) | |||||
| timeout = V_pf_default_rule.timeout[st->timeout]; | |||||
| /* sp->expire may have been adaptively scaled by export. */ | |||||
| st->expire -= (timeout - ntohl(sp->pfs_1301.expire)) * 1000; | |||||
| } | } | ||||
| if (! (st->act.rtableid == -1 || | if (! (st->act.rtableid == -1 || | ||||
| (st->act.rtableid >= 0 && st->act.rtableid < rt_numfibs))) | (st->act.rtableid >= 0 && st->act.rtableid < rt_numfibs))) | ||||
| goto cleanup; | goto cleanup; | ||||
| st->id = sp->pfs_1301.id; | st->id = sp->pfs_1301.id; | ||||
| st->creatorid = sp->pfs_1301.creatorid; | st->creatorid = sp->pfs_1301.creatorid; | ||||
| pf_state_peer_ntoh(&sp->pfs_1301.src, &st->src); | pf_state_peer_ntoh(&sp->pfs_1301.src, &st->src); | ||||
| pf_state_peer_ntoh(&sp->pfs_1301.dst, &st->dst); | pf_state_peer_ntoh(&sp->pfs_1301.dst, &st->dst); | ||||
| st->rule = r; | st->rule = r; | ||||
| st->nat_rule = NULL; | st->nat_rule = NULL; | ||||
| st->anchor = NULL; | st->anchor = NULL; | ||||
| st->pfsync_time = time_uptime; | st->pfsync_time = time_uptime; | ||||
| st->sync_state = PFSYNC_S_NONE; | st->sync_state = PFSYNC_S_NONE; | ||||
| if (!(flags & PFSYNC_SI_IOCTL)) | if (!(flags & PFSYNC_SI_IOCTL)) | ||||
| st->state_flags |= PFSTATE_NOSYNC; | st->state_flags |= PFSTATE_NOSYNC; | ||||
| if ((error = pf_state_insert(kif, kif, skw, sks, st)) != 0) | if ((error = pf_state_insert(kif, orig_kif, skw, sks, st)) != 0) | ||||
| goto cleanup_state; | goto cleanup_state; | ||||
| /* XXX when we have nat_rule/anchors, use STATE_INC_COUNTERS */ | /* XXX when we have nat_rule/anchors, use STATE_INC_COUNTERS */ | ||||
| counter_u64_add(r->states_cur, 1); | counter_u64_add(r->states_cur, 1); | ||||
| counter_u64_add(r->states_tot, 1); | counter_u64_add(r->states_tot, 1); | ||||
| if (!(flags & PFSYNC_SI_IOCTL)) { | if (!(flags & PFSYNC_SI_IOCTL)) { | ||||
| st->state_flags &= ~PFSTATE_NOSYNC; | st->state_flags &= ~PFSTATE_NOSYNC; | ||||
| ▲ Show 20 Lines • Show All 275 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| static int | static int | ||||
| pfsync_in_ins(struct mbuf *m, int offset, int count, int flags, int action) | pfsync_in_ins(struct mbuf *m, int offset, int count, int flags, int action) | ||||
| { | { | ||||
| struct mbuf *mp; | struct mbuf *mp; | ||||
| union pfsync_state_union *sa, *sp; | union pfsync_state_union *sa, *sp; | ||||
| int i, offp, total_len, msg_version, msg_len; | int i, offp, total_len, msg_version, msg_len; | ||||
| u_int8_t timeout, direction; | |||||
| sa_family_t af; | |||||
| switch (action) { | switch (action) { | ||||
| case PFSYNC_ACT_INS_1301: | case PFSYNC_ACT_INS_1301: | ||||
| msg_len = sizeof(struct pfsync_state_1301); | msg_len = sizeof(struct pfsync_state_1301); | ||||
| total_len = msg_len * count; | |||||
| msg_version = PFSYNC_MSG_VERSION_1301; | msg_version = PFSYNC_MSG_VERSION_1301; | ||||
| break; | break; | ||||
| case PFSYNC_ACT_INS_1400: | case PFSYNC_ACT_INS_1400: | ||||
| msg_len = sizeof(struct pfsync_state_1400); | msg_len = sizeof(struct pfsync_state_1400); | ||||
| total_len = msg_len * count; | |||||
| msg_version = PFSYNC_MSG_VERSION_1400; | msg_version = PFSYNC_MSG_VERSION_1400; | ||||
| break; | break; | ||||
| case PFSYNC_ACT_INS_1500: | |||||
| msg_len = sizeof(struct pfsync_state_1500); | |||||
| msg_version = PFSYNC_MSG_VERSION_1500; | |||||
| break; | |||||
| default: | default: | ||||
| V_pfsyncstats.pfsyncs_badver++; | V_pfsyncstats.pfsyncs_badver++; | ||||
| return (-1); | return (-1); | ||||
| } | } | ||||
| total_len = msg_len * count; | |||||
| mp = m_pulldown(m, offset, total_len, &offp); | mp = m_pulldown(m, offset, total_len, &offp); | ||||
| if (mp == NULL) { | if (mp == NULL) { | ||||
| V_pfsyncstats.pfsyncs_badlen++; | V_pfsyncstats.pfsyncs_badlen++; | ||||
| return (-1); | return (-1); | ||||
| } | } | ||||
| sa = (union pfsync_state_union *)(mp->m_data + offp); | sa = (union pfsync_state_union *)(mp->m_data + offp); | ||||
| for (i = 0; i < count; i++) { | for (i = 0; i < count; i++) { | ||||
| sp = (union pfsync_state_union *)((char *)sa + msg_len * i); | sp = (union pfsync_state_union *)((char *)sa + msg_len * i); | ||||
| switch (msg_version) { | |||||
| case PFSYNC_MSG_VERSION_1301: | |||||
| case PFSYNC_MSG_VERSION_1400: | |||||
| af = sp->pfs_1301.af; | |||||
| timeout = sp->pfs_1301.timeout; | |||||
| direction = sp->pfs_1301.direction; | |||||
| break; | |||||
| case PFSYNC_MSG_VERSION_1500: | |||||
| af = sp->pfs_1500.wire_af; | |||||
| timeout = sp->pfs_1500.timeout; | |||||
| direction = sp->pfs_1500.direction; | |||||
| break; | |||||
| } | |||||
| /* Check for invalid values. */ | /* Check for invalid values. */ | ||||
| if (sp->pfs_1301.timeout >= PFTM_MAX || | if (timeout >= PFTM_MAX || | ||||
| sp->pfs_1301.src.state > PF_TCPS_PROXY_DST || | sp->pfs_1301.src.state > PF_TCPS_PROXY_DST || | ||||
| sp->pfs_1301.dst.state > PF_TCPS_PROXY_DST || | sp->pfs_1301.dst.state > PF_TCPS_PROXY_DST || | ||||
| sp->pfs_1301.direction > PF_OUT || | direction > PF_OUT || | ||||
| (sp->pfs_1301.af != AF_INET && | (af != AF_INET && af != AF_INET6)) { | ||||
| sp->pfs_1301.af != AF_INET6)) { | |||||
| if (V_pf_status.debug >= PF_DEBUG_MISC) | if (V_pf_status.debug >= PF_DEBUG_MISC) | ||||
| printf("%s: invalid value\n", __func__); | printf("%s: invalid value\n", __func__); | ||||
| V_pfsyncstats.pfsyncs_badval++; | V_pfsyncstats.pfsyncs_badval++; | ||||
| continue; | continue; | ||||
| } | } | ||||
| if (pfsync_state_import(sp, flags, msg_version) != 0) | if (pfsync_state_import(sp, flags, msg_version) != 0) | ||||
| V_pfsyncstats.pfsyncs_badact++; | V_pfsyncstats.pfsyncs_badact++; | ||||
| ▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | |||||
| static int | static int | ||||
| pfsync_in_upd(struct mbuf *m, int offset, int count, int flags, int action) | pfsync_in_upd(struct mbuf *m, int offset, int count, int flags, int action) | ||||
| { | { | ||||
| struct pfsync_softc *sc = V_pfsyncif; | struct pfsync_softc *sc = V_pfsyncif; | ||||
| union pfsync_state_union *sa, *sp; | union pfsync_state_union *sa, *sp; | ||||
| struct pf_kstate *st; | struct pf_kstate *st; | ||||
| struct mbuf *mp; | struct mbuf *mp; | ||||
| int sync, offp, i, total_len, msg_len, msg_version; | int sync, offp, i, total_len, msg_len, msg_version; | ||||
| u_int8_t timeout; | |||||
| switch (action) { | switch (action) { | ||||
| case PFSYNC_ACT_UPD_1301: | case PFSYNC_ACT_UPD_1301: | ||||
| msg_len = sizeof(struct pfsync_state_1301); | msg_len = sizeof(struct pfsync_state_1301); | ||||
| total_len = msg_len * count; | |||||
| msg_version = PFSYNC_MSG_VERSION_1301; | msg_version = PFSYNC_MSG_VERSION_1301; | ||||
| break; | break; | ||||
| case PFSYNC_ACT_UPD_1400: | case PFSYNC_ACT_UPD_1400: | ||||
| msg_len = sizeof(struct pfsync_state_1400); | msg_len = sizeof(struct pfsync_state_1400); | ||||
| total_len = msg_len * count; | |||||
| msg_version = PFSYNC_MSG_VERSION_1400; | msg_version = PFSYNC_MSG_VERSION_1400; | ||||
| break; | break; | ||||
| case PFSYNC_ACT_UPD_1500: | |||||
| msg_len = sizeof(struct pfsync_state_1500); | |||||
| msg_version = PFSYNC_MSG_VERSION_1500; | |||||
| break; | |||||
| default: | default: | ||||
| V_pfsyncstats.pfsyncs_badact++; | V_pfsyncstats.pfsyncs_badact++; | ||||
| return (-1); | return (-1); | ||||
| } | } | ||||
| total_len = msg_len * count; | |||||
| mp = m_pulldown(m, offset, total_len, &offp); | mp = m_pulldown(m, offset, total_len, &offp); | ||||
| if (mp == NULL) { | if (mp == NULL) { | ||||
| V_pfsyncstats.pfsyncs_badlen++; | V_pfsyncstats.pfsyncs_badlen++; | ||||
| return (-1); | return (-1); | ||||
| } | } | ||||
| sa = (union pfsync_state_union *)(mp->m_data + offp); | sa = (union pfsync_state_union *)(mp->m_data + offp); | ||||
| for (i = 0; i < count; i++) { | for (i = 0; i < count; i++) { | ||||
| sp = (union pfsync_state_union *)((char *)sa + msg_len * i); | sp = (union pfsync_state_union *)((char *)sa + msg_len * i); | ||||
| switch (msg_version) { | |||||
| case PFSYNC_MSG_VERSION_1301: | |||||
| case PFSYNC_MSG_VERSION_1400: | |||||
| timeout = sp->pfs_1301.timeout; | |||||
| break; | |||||
| case PFSYNC_MSG_VERSION_1500: | |||||
| timeout = sp->pfs_1500.timeout; | |||||
| break; | |||||
| } | |||||
| /* check for invalid values */ | /* check for invalid values */ | ||||
| if (sp->pfs_1301.timeout >= PFTM_MAX || | if (timeout >= PFTM_MAX || | ||||
| sp->pfs_1301.src.state > PF_TCPS_PROXY_DST || | sp->pfs_1301.src.state > PF_TCPS_PROXY_DST || | ||||
| sp->pfs_1301.dst.state > PF_TCPS_PROXY_DST) { | sp->pfs_1301.dst.state > PF_TCPS_PROXY_DST) { | ||||
| if (V_pf_status.debug >= PF_DEBUG_MISC) { | if (V_pf_status.debug >= PF_DEBUG_MISC) { | ||||
| printf("pfsync_input: PFSYNC_ACT_UPD: " | printf("pfsync_input: PFSYNC_ACT_UPD: " | ||||
| "invalid value\n"); | "invalid value\n"); | ||||
| } | } | ||||
| V_pfsyncstats.pfsyncs_badval++; | V_pfsyncstats.pfsyncs_badval++; | ||||
| continue; | continue; | ||||
| Show All 28 Lines | else { | ||||
| sync++; | sync++; | ||||
| else | else | ||||
| pf_state_peer_ntoh(&sp->pfs_1301.dst, &st->dst); | pf_state_peer_ntoh(&sp->pfs_1301.dst, &st->dst); | ||||
| } | } | ||||
| if (sync < 2) { | if (sync < 2) { | ||||
| pfsync_alloc_scrub_memory(&sp->pfs_1301.dst, &st->dst); | pfsync_alloc_scrub_memory(&sp->pfs_1301.dst, &st->dst); | ||||
| pf_state_peer_ntoh(&sp->pfs_1301.dst, &st->dst); | pf_state_peer_ntoh(&sp->pfs_1301.dst, &st->dst); | ||||
| st->expire = pf_get_uptime(); | st->expire = pf_get_uptime(); | ||||
| st->timeout = sp->pfs_1301.timeout; | st->timeout = timeout; | ||||
| } | } | ||||
| st->pfsync_time = time_uptime; | st->pfsync_time = time_uptime; | ||||
| if (sync) { | if (sync) { | ||||
| V_pfsyncstats.pfsyncs_stale++; | V_pfsyncstats.pfsyncs_stale++; | ||||
| pfsync_update_state(st); | pfsync_update_state(st); | ||||
| PF_STATE_UNLOCK(st); | PF_STATE_UNLOCK(st); | ||||
| ▲ Show 20 Lines • Show All 484 Lines • ▼ Show 20 Lines | |||||
| pfsync_out_state_1400(struct pf_kstate *st, void *buf) | pfsync_out_state_1400(struct pf_kstate *st, void *buf) | ||||
| { | { | ||||
| union pfsync_state_union *sp = buf; | union pfsync_state_union *sp = buf; | ||||
| pfsync_state_export(sp, st, PFSYNC_MSG_VERSION_1400); | pfsync_state_export(sp, st, PFSYNC_MSG_VERSION_1400); | ||||
| } | } | ||||
| static void | static void | ||||
| pfsync_out_state_1500(struct pf_kstate *st, void *buf) | |||||
| { | |||||
| union pfsync_state_union *sp = buf; | |||||
| pfsync_state_export(sp, st, PFSYNC_MSG_VERSION_1500); | |||||
| } | |||||
| static void | |||||
| pfsync_out_iack(struct pf_kstate *st, void *buf) | pfsync_out_iack(struct pf_kstate *st, void *buf) | ||||
| { | { | ||||
| struct pfsync_ins_ack *iack = buf; | struct pfsync_ins_ack *iack = buf; | ||||
| iack->id = st->id; | iack->id = st->id; | ||||
| iack->creatorid = st->creatorid; | iack->creatorid = st->creatorid; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 650 Lines • ▼ Show 20 Lines | pfsync_sstate_to_qid(u_int8_t sync_state) | ||||
| switch (sync_state) { | switch (sync_state) { | ||||
| case PFSYNC_S_INS: | case PFSYNC_S_INS: | ||||
| switch (sc->sc_version) { | switch (sc->sc_version) { | ||||
| case PFSYNC_MSG_VERSION_1301: | case PFSYNC_MSG_VERSION_1301: | ||||
| return PFSYNC_Q_INS_1301; | return PFSYNC_Q_INS_1301; | ||||
| case PFSYNC_MSG_VERSION_1400: | case PFSYNC_MSG_VERSION_1400: | ||||
| return PFSYNC_Q_INS_1400; | return PFSYNC_Q_INS_1400; | ||||
| case PFSYNC_MSG_VERSION_1500: | |||||
| return PFSYNC_Q_INS_1500; | |||||
| } | } | ||||
| break; | break; | ||||
| case PFSYNC_S_IACK: | case PFSYNC_S_IACK: | ||||
| return PFSYNC_Q_IACK; | return PFSYNC_Q_IACK; | ||||
| case PFSYNC_S_UPD: | case PFSYNC_S_UPD: | ||||
| switch (sc->sc_version) { | switch (sc->sc_version) { | ||||
| case PFSYNC_MSG_VERSION_1301: | case PFSYNC_MSG_VERSION_1301: | ||||
| return PFSYNC_Q_UPD_1301; | return PFSYNC_Q_UPD_1301; | ||||
| case PFSYNC_MSG_VERSION_1400: | case PFSYNC_MSG_VERSION_1400: | ||||
| return PFSYNC_Q_UPD_1400; | return PFSYNC_Q_UPD_1400; | ||||
| case PFSYNC_MSG_VERSION_1500: | |||||
| return PFSYNC_Q_UPD_1500; | |||||
| } | } | ||||
| break; | break; | ||||
| case PFSYNC_S_UPD_C: | case PFSYNC_S_UPD_C: | ||||
| return PFSYNC_Q_UPD_C; | return PFSYNC_Q_UPD_C; | ||||
| case PFSYNC_S_DEL_C: | case PFSYNC_S_DEL_C: | ||||
| return PFSYNC_Q_DEL_C; | return PFSYNC_Q_DEL_C; | ||||
| default: | default: | ||||
| panic("%s: Unsupported st->sync_state 0x%02x", | panic("%s: Unsupported st->sync_state 0x%02x", | ||||
| ▲ Show 20 Lines • Show All 540 Lines • ▼ Show 20 Lines | #endif | ||||
| PFSYNC_LOCK(sc); | PFSYNC_LOCK(sc); | ||||
| switch (status->version) { | switch (status->version) { | ||||
| case PFSYNC_MSG_VERSION_UNSPECIFIED: | case PFSYNC_MSG_VERSION_UNSPECIFIED: | ||||
| sc->sc_version = PFSYNC_MSG_VERSION_DEFAULT; | sc->sc_version = PFSYNC_MSG_VERSION_DEFAULT; | ||||
| break; | break; | ||||
| case PFSYNC_MSG_VERSION_1301: | case PFSYNC_MSG_VERSION_1301: | ||||
| case PFSYNC_MSG_VERSION_1400: | case PFSYNC_MSG_VERSION_1400: | ||||
| case PFSYNC_MSG_VERSION_1500: | |||||
| sc->sc_version = status->version; | sc->sc_version = status->version; | ||||
| break; | break; | ||||
| default: | default: | ||||
| PFSYNC_UNLOCK(sc); | PFSYNC_UNLOCK(sc); | ||||
| return (EINVAL); | return (EINVAL); | ||||
| } | } | ||||
| switch (status->syncpeer.ss_family) { | switch (status->syncpeer.ss_family) { | ||||
| ▲ Show 20 Lines • Show All 270 Lines • Show Last 20 Lines | |||||
Under which conditions can queues have different ids on two routers? Can this happen even if the ruleset is eventually identical but one of routers went though some intermediate versions? Syncing wrong information could send packets to a wrong queue, potentially some very slow one. Could we not sync it at all and trust the rule if rulesets are identical, like we did previously for other synced parameters? Or introduce a checksum just for altq queues?