Index: net/pf.c =================================================================== RCS file: /cvs/src/sys/net/pf.c,v retrieving revision 1.1194 diff -u -p -r1.1194 pf.c --- net/pf.c 12 Apr 2024 16:07:09 -0000 1.1194 +++ net/pf.c 22 Apr 2024 05:18:27 -0000 @@ -6564,12 +6564,7 @@ void pf_route(struct pf_pdesc *pd, struct pf_state *st) { struct mbuf *m0; - struct mbuf_list ml; - struct sockaddr_in *dst, sin; - struct rtentry *rt = NULL; - struct ip *ip; - struct ifnet *ifp = NULL; - unsigned int rtableid; + struct m_tag *mtag; if (pd->m->m_pkthdr.pf.routed++ > 3) { m_freem(pd->m); @@ -6578,103 +6573,37 @@ pf_route(struct pf_pdesc *pd, struct pf_ } if (st->rt == PF_DUPTO) { - if ((m0 = m_dup_pkt(pd->m, max_linkhdr, M_NOWAIT)) == NULL) - return; - } else { - if ((st->rt == PF_REPLYTO) == (st->direction == pd->dir)) + m0 = m_dup_pkt(pd->m, max_linkhdr, M_NOWAIT); + if (m0 == NULL) return; + } else if ((st->rt == PF_REPLYTO) == (st->direction == pd->dir)) + return; + else m0 = pd->m; - pd->m = NULL; - } - if (m0->m_len < sizeof(struct ip)) { - DPFPRINTF(LOG_ERR, - "%s: m0->m_len < sizeof(struct ip)", __func__); - goto bad; + mtag = m_tag_find(m0, PACKET_TAG_PF_ROUTE, NULL); + if (mtag == NULL) { + mtag = m_tag_get(PACKET_TAG_PF_ROUTE, sizeof(struct in_addr), + M_NOWAIT); + if (mtag == NULL) + goto drop; } - ip = mtod(m0, struct ip *); + *(struct in_addr *)(mtag + 1) = st->rt_addr.v4; + m_tag_prepend(m0, mtag); - if (pd->dir == PF_IN) { - if (ip->ip_ttl <= IPTTLDEC) { - if (st->rt != PF_DUPTO) { - pf_send_icmp(m0, ICMP_TIMXCEED, - ICMP_TIMXCEED_INTRANS, 0, - pd->af, st->rule.ptr, pd->rdomain); - } - goto bad; - } - ip->ip_ttl -= IPTTLDEC; - } - - memset(&sin, 0, sizeof(sin)); - dst = &sin; - dst->sin_family = AF_INET; - dst->sin_len = sizeof(*dst); - dst->sin_addr = st->rt_addr.v4; - rtableid = m0->m_pkthdr.ph_rtableid; - - rt = rtalloc_mpath(sintosa(dst), &ip->ip_src.s_addr, rtableid); - if (!rtisvalid(rt)) { - if (st->rt != PF_DUPTO) { - pf_send_icmp(m0, ICMP_UNREACH, ICMP_UNREACH_HOST, - 0, pd->af, st->rule.ptr, pd->rdomain); - } - ipstat_inc(ips_noroute); - goto bad; - } - - ifp = if_get(rt->rt_ifidx); - if (ifp == NULL) - goto bad; - - /* A locally generated packet may have invalid source address. */ - if ((ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET && - (ifp->if_flags & IFF_LOOPBACK) == 0) - ip->ip_src = ifatoia(rt->rt_ifa)->ia_addr.sin_addr; - - if (st->rt != PF_DUPTO && pd->dir == PF_IN) { - if (pf_test(AF_INET, PF_OUT, ifp, &m0) != PF_PASS) - goto bad; - else if (m0 == NULL) - goto done; - if (m0->m_len < sizeof(struct ip)) { - DPFPRINTF(LOG_ERR, - "%s: m0->m_len < sizeof(struct ip)", __func__); - goto bad; - } - ip = mtod(m0, struct ip *); - } + if (st->rt == PF_DUPTO) { + SET(m0->m_pkthdr.pf.flags, PF_TAG_GENERATED); - if (if_output_tso(ifp, &m0, sintosa(dst), rt, ifp->if_mtu) || - m0 == NULL) - goto done; - - /* - * Too large for interface; fragment if possible. - * Must be able to put at least 8 bytes per fragment. - */ - if (ip->ip_off & htons(IP_DF)) { - ipstat_inc(ips_cantfrag); - if (st->rt != PF_DUPTO) - pf_send_icmp(m0, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, - ifp->if_mtu, pd->af, st->rule.ptr, pd->rdomain); - goto bad; - } + ip_output(m0, NULL, NULL, 0, NULL, NULL, 0); + } else if (pd->dir == PF_OUT) + SET(m0->m_pkthdr.pf.flags, PF_TAG_REROUTE); - if (ip_fragment(m0, &ml, ifp, ifp->if_mtu) || - if_output_ml(ifp, &ml, sintosa(dst), rt)) - goto done; - ipstat_inc(ips_fragmented); - -done: - if_put(ifp); - rtfree(rt); return; - -bad: +drop: + if (m0 == pd->m) + pd->m = NULL; m_freem(m0); - goto done; } #ifdef INET6 Index: netinet/ip_input.c =================================================================== RCS file: /cvs/src/sys/netinet/ip_input.c,v retrieving revision 1.393 diff -u -p -r1.393 ip_input.c --- netinet/ip_input.c 16 Apr 2024 12:56:39 -0000 1.393 +++ netinet/ip_input.c 22 Apr 2024 05:18:27 -0000 @@ -813,6 +813,9 @@ in_ouraddr(struct mbuf *m, struct ifnet int match = 0; #if NPF > 0 + if (ISSET(m->m_pkthdr.ph_tagsset, PACKET_TAG_PF_ROUTE)) + return (0); + switch (pf_ouraddr(m)) { case 0: return (0); Index: netinet/ip_output.c =================================================================== RCS file: /cvs/src/sys/netinet/ip_output.c,v retrieving revision 1.398 diff -u -p -r1.398 ip_output.c --- netinet/ip_output.c 17 Apr 2024 20:48:51 -0000 1.398 +++ netinet/ip_output.c 22 Apr 2024 05:18:27 -0000 @@ -110,7 +110,9 @@ ip_output(struct mbuf *m, struct mbuf *o struct sockaddr_in *dst; struct tdb *tdb = NULL; u_long mtu; + struct in_addr *rt_dst; #if NPF > 0 + struct m_tag *rt_mtag; u_int orig_rtableid; #endif @@ -128,7 +130,7 @@ ip_output(struct mbuf *m, struct mbuf *o /* * Fill in IP header. */ - if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { + if (!ISSET(flags, IP_FORWARDING|IP_RAWOUTPUT)) { ip->ip_v = IPVERSION; ip->ip_off &= htons(IP_DF); ip->ip_id = htons(ip_randomid()); @@ -151,6 +153,7 @@ ip_output(struct mbuf *m, struct mbuf *o orig_rtableid = m->m_pkthdr.ph_rtableid; reroute: #endif + rt_dst = &ip->ip_dst; /* * Do a route lookup now in case we need the source address to @@ -163,11 +166,17 @@ reroute: ro->ro_rt = NULL; } +#if NPF > 0 + rt_mtag = m_tag_find(m, PACKET_TAG_PF_ROUTE, NULL); + if (rt_mtag != NULL) + rt_dst = (struct in_addr *)(rt_mtag + 1); +#endif + /* * If there is a cached route, check that it is to the same * destination and is still up. If not, free it and try again. */ - route_cache(ro, &ip->ip_dst, &ip->ip_src, m->m_pkthdr.ph_rtableid); + route_cache(ro, rt_dst, &ip->ip_src, m->m_pkthdr.ph_rtableid); dst = &ro->ro_dstsin; if ((IN_MULTICAST(ip->ip_dst.s_addr) || Index: sys/mbuf.h =================================================================== RCS file: /cvs/src/sys/sys/mbuf.h,v retrieving revision 1.263 diff -u -p -r1.263 mbuf.h --- sys/mbuf.h 14 Apr 2024 20:46:27 -0000 1.263 +++ sys/mbuf.h 22 Apr 2024 05:18:27 -0000 @@ -477,6 +477,7 @@ struct m_tag *m_tag_next(struct mbuf *, #define PACKET_TAG_GRE 0x0080 /* GRE processing done */ #define PACKET_TAG_DLT 0x0100 /* data link layer type */ #define PACKET_TAG_PF_DIVERT 0x0200 /* pf(4) diverted packet */ +#define PACKET_TAG_PF_ROUTE 0x0400 /* pf(4) route-to */ #define PACKET_TAG_PF_REASSEMBLED 0x0800 /* pf reassembled ipv6 packet */ #define PACKET_TAG_SRCROUTE 0x1000 /* IPv4 source routing options */ #define PACKET_TAG_TUNNEL 0x2000 /* Tunnel endpoint address */