Lines 2466-2485
pf_send_tcp(struct mbuf *replyto, const struct pf_rule *r, sa_family_t af,
Link Here
|
2466 |
h6->ip6_hlim = IPV6_DEFHLIM; |
2466 |
h6->ip6_hlim = IPV6_DEFHLIM; |
2467 |
|
2467 |
|
2468 |
pfse->pfse_type = PFSE_IP6; |
2468 |
pfse->pfse_type = PFSE_IP6; |
2469 |
break; |
2469 |
break; |
2470 |
#endif /* INET6 */ |
2470 |
#endif /* INET6 */ |
2471 |
} |
2471 |
} |
2472 |
pfse->pfse_m = m; |
2472 |
pfse->pfse_m = m; |
2473 |
pf_send(pfse); |
2473 |
pf_send(pfse); |
2474 |
} |
2474 |
} |
2475 |
|
2475 |
|
|
|
2476 |
static void |
2477 |
pf_return(struct pf_rule *r, struct pf_rule *nr, struct pf_pdesc *pd, |
2478 |
struct pf_state_key *sk, int off, struct mbuf *m, struct tcphdr *th, |
2479 |
struct pfi_kif *kif, u_int16_t bproto_sum, u_int16_t bip_sum, int hdrlen, |
2480 |
u_short *reason) |
2481 |
{ |
2482 |
struct pf_addr * const saddr = pd->src; |
2483 |
struct pf_addr * const daddr = pd->dst; |
2484 |
sa_family_t af = pd->af; |
2485 |
|
2486 |
/* undo NAT changes, if they have taken place */ |
2487 |
if (nr != NULL) { |
2488 |
PF_ACPY(saddr, &sk->addr[pd->sidx], af); |
2489 |
PF_ACPY(daddr, &sk->addr[pd->didx], af); |
2490 |
if (pd->sport) |
2491 |
*pd->sport = sk->port[pd->sidx]; |
2492 |
if (pd->dport) |
2493 |
*pd->dport = sk->port[pd->didx]; |
2494 |
if (pd->proto_sum) |
2495 |
*pd->proto_sum = bproto_sum; |
2496 |
if (pd->ip_sum) |
2497 |
*pd->ip_sum = bip_sum; |
2498 |
m_copyback(m, off, hdrlen, pd->hdr.any); |
2499 |
} |
2500 |
if (pd->proto == IPPROTO_TCP && |
2501 |
((r->rule_flag & PFRULE_RETURNRST) || |
2502 |
(r->rule_flag & PFRULE_RETURN)) && |
2503 |
!(th->th_flags & TH_RST)) { |
2504 |
u_int32_t ack = ntohl(th->th_seq) + pd->p_len; |
2505 |
int len = 0; |
2506 |
#ifdef INET |
2507 |
struct ip *h4; |
2508 |
#endif |
2509 |
#ifdef INET6 |
2510 |
struct ip6_hdr *h6; |
2511 |
#endif |
2512 |
|
2513 |
switch (af) { |
2514 |
#ifdef INET |
2515 |
case AF_INET: |
2516 |
h4 = mtod(m, struct ip *); |
2517 |
len = ntohs(h4->ip_len) - off; |
2518 |
break; |
2519 |
#endif |
2520 |
#ifdef INET6 |
2521 |
case AF_INET6: |
2522 |
h6 = mtod(m, struct ip6_hdr *); |
2523 |
len = ntohs(h6->ip6_plen) - (off - sizeof(*h6)); |
2524 |
break; |
2525 |
#endif |
2526 |
} |
2527 |
|
2528 |
if (pf_check_proto_cksum(m, off, len, IPPROTO_TCP, af)) |
2529 |
REASON_SET(reason, PFRES_PROTCKSUM); |
2530 |
else { |
2531 |
if (th->th_flags & TH_SYN) |
2532 |
ack++; |
2533 |
if (th->th_flags & TH_FIN) |
2534 |
ack++; |
2535 |
pf_send_tcp(m, r, af, pd->dst, |
2536 |
pd->src, th->th_dport, th->th_sport, |
2537 |
ntohl(th->th_ack), ack, TH_RST|TH_ACK, 0, 0, |
2538 |
r->return_ttl, 1, 0, kif->pfik_ifp); |
2539 |
} |
2540 |
} else if (pd->proto != IPPROTO_ICMP && af == AF_INET && |
2541 |
r->return_icmp) |
2542 |
pf_send_icmp(m, r->return_icmp >> 8, |
2543 |
r->return_icmp & 255, af, r); |
2544 |
else if (pd->proto != IPPROTO_ICMPV6 && af == AF_INET6 && |
2545 |
r->return_icmp6) |
2546 |
pf_send_icmp(m, r->return_icmp6 >> 8, |
2547 |
r->return_icmp6 & 255, af, r); |
2548 |
} |
2549 |
|
2550 |
|
2476 |
static int |
2551 |
static int |
2477 |
pf_ieee8021q_setpcp(struct mbuf *m, u_int8_t prio) |
2552 |
pf_ieee8021q_setpcp(struct mbuf *m, u_int8_t prio) |
2478 |
{ |
2553 |
{ |
2479 |
struct m_tag *mtag; |
2554 |
struct m_tag *mtag; |
2480 |
|
2555 |
|
2481 |
KASSERT(prio <= PF_PRIO_MAX, |
2556 |
KASSERT(prio <= PF_PRIO_MAX, |
2482 |
("%s with invalid pcp", __func__)); |
2557 |
("%s with invalid pcp", __func__)); |
2483 |
|
2558 |
|
2484 |
mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_OUT, NULL); |
2559 |
mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_OUT, NULL); |
2485 |
if (mtag == NULL) { |
2560 |
if (mtag == NULL) { |
Lines 3430-3531
pf_test_rule(struct pf_rule **rm, struct pf_state **sm, int direction,
Link Here
|
3430 |
if (rewrite) |
3505 |
if (rewrite) |
3431 |
m_copyback(m, off, hdrlen, pd->hdr.any); |
3506 |
m_copyback(m, off, hdrlen, pd->hdr.any); |
3432 |
PFLOG_PACKET(kif, m, af, direction, reason, r->log ? r : nr, a, |
3507 |
PFLOG_PACKET(kif, m, af, direction, reason, r->log ? r : nr, a, |
3433 |
ruleset, pd, 1); |
3508 |
ruleset, pd, 1); |
3434 |
} |
3509 |
} |
3435 |
|
3510 |
|
3436 |
if ((r->action == PF_DROP) && |
3511 |
if ((r->action == PF_DROP) && |
3437 |
((r->rule_flag & PFRULE_RETURNRST) || |
3512 |
((r->rule_flag & PFRULE_RETURNRST) || |
3438 |
(r->rule_flag & PFRULE_RETURNICMP) || |
3513 |
(r->rule_flag & PFRULE_RETURNICMP) || |
3439 |
(r->rule_flag & PFRULE_RETURN))) { |
3514 |
(r->rule_flag & PFRULE_RETURN))) { |
3440 |
/* undo NAT changes, if they have taken place */ |
3515 |
pf_return(r, nr, pd, sk, off, m, th, kif, bproto_sum, |
3441 |
if (nr != NULL) { |
3516 |
bip_sum, hdrlen, &reason); |
3442 |
PF_ACPY(saddr, &sk->addr[pd->sidx], af); |
|
|
3443 |
PF_ACPY(daddr, &sk->addr[pd->didx], af); |
3444 |
if (pd->sport) |
3445 |
*pd->sport = sk->port[pd->sidx]; |
3446 |
if (pd->dport) |
3447 |
*pd->dport = sk->port[pd->didx]; |
3448 |
if (pd->proto_sum) |
3449 |
*pd->proto_sum = bproto_sum; |
3450 |
if (pd->ip_sum) |
3451 |
*pd->ip_sum = bip_sum; |
3452 |
m_copyback(m, off, hdrlen, pd->hdr.any); |
3453 |
} |
3454 |
if (pd->proto == IPPROTO_TCP && |
3455 |
((r->rule_flag & PFRULE_RETURNRST) || |
3456 |
(r->rule_flag & PFRULE_RETURN)) && |
3457 |
!(th->th_flags & TH_RST)) { |
3458 |
u_int32_t ack = ntohl(th->th_seq) + pd->p_len; |
3459 |
int len = 0; |
3460 |
#ifdef INET |
3461 |
struct ip *h4; |
3462 |
#endif |
3463 |
#ifdef INET6 |
3464 |
struct ip6_hdr *h6; |
3465 |
#endif |
3466 |
|
3467 |
switch (af) { |
3468 |
#ifdef INET |
3469 |
case AF_INET: |
3470 |
h4 = mtod(m, struct ip *); |
3471 |
len = ntohs(h4->ip_len) - off; |
3472 |
break; |
3473 |
#endif |
3474 |
#ifdef INET6 |
3475 |
case AF_INET6: |
3476 |
h6 = mtod(m, struct ip6_hdr *); |
3477 |
len = ntohs(h6->ip6_plen) - (off - sizeof(*h6)); |
3478 |
break; |
3479 |
#endif |
3480 |
} |
3481 |
|
3482 |
if (pf_check_proto_cksum(m, off, len, IPPROTO_TCP, af)) |
3483 |
REASON_SET(&reason, PFRES_PROTCKSUM); |
3484 |
else { |
3485 |
if (th->th_flags & TH_SYN) |
3486 |
ack++; |
3487 |
if (th->th_flags & TH_FIN) |
3488 |
ack++; |
3489 |
pf_send_tcp(m, r, af, pd->dst, |
3490 |
pd->src, th->th_dport, th->th_sport, |
3491 |
ntohl(th->th_ack), ack, TH_RST|TH_ACK, 0, 0, |
3492 |
r->return_ttl, 1, 0, kif->pfik_ifp); |
3493 |
} |
3494 |
} else if (pd->proto != IPPROTO_ICMP && af == AF_INET && |
3495 |
r->return_icmp) |
3496 |
pf_send_icmp(m, r->return_icmp >> 8, |
3497 |
r->return_icmp & 255, af, r); |
3498 |
else if (pd->proto != IPPROTO_ICMPV6 && af == AF_INET6 && |
3499 |
r->return_icmp6) |
3500 |
pf_send_icmp(m, r->return_icmp6 >> 8, |
3501 |
r->return_icmp6 & 255, af, r); |
3502 |
} |
3517 |
} |
3503 |
|
3518 |
|
3504 |
if (r->action == PF_DROP) |
3519 |
if (r->action == PF_DROP) |
3505 |
goto cleanup; |
3520 |
goto cleanup; |
3506 |
|
3521 |
|
3507 |
if (tag > 0 && pf_tag_packet(m, pd, tag)) { |
3522 |
if (tag > 0 && pf_tag_packet(m, pd, tag)) { |
3508 |
REASON_SET(&reason, PFRES_MEMORY); |
3523 |
REASON_SET(&reason, PFRES_MEMORY); |
3509 |
goto cleanup; |
3524 |
goto cleanup; |
3510 |
} |
3525 |
} |
3511 |
if (rtableid >= 0) |
3526 |
if (rtableid >= 0) |
3512 |
M_SETFIB(m, rtableid); |
3527 |
M_SETFIB(m, rtableid); |
3513 |
|
3528 |
|
3514 |
if (!state_icmp && (r->keep_state || nr != NULL || |
3529 |
if (!state_icmp && (r->keep_state || nr != NULL || |
3515 |
(pd->flags & PFDESC_TCP_NORM))) { |
3530 |
(pd->flags & PFDESC_TCP_NORM))) { |
3516 |
int action; |
3531 |
int action; |
3517 |
action = pf_create_state(r, nr, a, pd, nsn, nk, sk, m, off, |
3532 |
action = pf_create_state(r, nr, a, pd, nsn, nk, sk, m, off, |
3518 |
sport, dport, &rewrite, kif, sm, tag, bproto_sum, bip_sum, |
3533 |
sport, dport, &rewrite, kif, sm, tag, bproto_sum, bip_sum, |
3519 |
hdrlen); |
3534 |
hdrlen); |
3520 |
if (action != PF_PASS) |
3535 |
if (action != PF_PASS && r->rule_flag & PFRULE_FAILRETURN) { |
|
|
3536 |
pf_return(r, nr, pd, sk, off, m, th, kif, |
3537 |
bproto_sum, bip_sum, hdrlen, &reason); |
3521 |
return (action); |
3538 |
return (action); |
|
|
3539 |
} |
3522 |
} else { |
3540 |
} else { |
3523 |
if (sk != NULL) |
3541 |
if (sk != NULL) |
3524 |
uma_zfree(V_pf_state_key_z, sk); |
3542 |
uma_zfree(V_pf_state_key_z, sk); |
3525 |
if (nk != NULL) |
3543 |
if (nk != NULL) |
3526 |
uma_zfree(V_pf_state_key_z, nk); |
3544 |
uma_zfree(V_pf_state_key_z, nk); |
3527 |
} |
3545 |
} |
3528 |
|
3546 |
|
3529 |
/* copy back packet headers if we performed NAT operations */ |
3547 |
/* copy back packet headers if we performed NAT operations */ |
3530 |
if (rewrite) |
3548 |
if (rewrite) |
3531 |
m_copyback(m, off, hdrlen, pd->hdr.any); |
3549 |
m_copyback(m, off, hdrlen, pd->hdr.any); |