163012b51Srmind /*-
2fad8b2d7Srmind * Copyright (c) 2010-2011 The NetBSD Foundation, Inc.
363012b51Srmind * All rights reserved.
463012b51Srmind *
563012b51Srmind * This material is based upon work partially supported by The
663012b51Srmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
763012b51Srmind *
863012b51Srmind * Redistribution and use in source and binary forms, with or without
963012b51Srmind * modification, are permitted provided that the following conditions
1063012b51Srmind * are met:
1163012b51Srmind * 1. Redistributions of source code must retain the above copyright
1263012b51Srmind * notice, this list of conditions and the following disclaimer.
1363012b51Srmind * 2. Redistributions in binary form must reproduce the above copyright
1463012b51Srmind * notice, this list of conditions and the following disclaimer in the
1563012b51Srmind * documentation and/or other materials provided with the distribution.
1663012b51Srmind *
1763012b51Srmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1863012b51Srmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1963012b51Srmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2063012b51Srmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2163012b51Srmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2263012b51Srmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2363012b51Srmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2463012b51Srmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2563012b51Srmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2663012b51Srmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2763012b51Srmind * POSSIBILITY OF SUCH DAMAGE.
2863012b51Srmind */
2963012b51Srmind
3063012b51Srmind /*
3163012b51Srmind * NPF module for packet construction routines.
3263012b51Srmind */
3363012b51Srmind
34f75d79ebSchristos #ifdef _KERNEL
3563012b51Srmind #include <sys/cdefs.h>
36*1165567dSkardel __KERNEL_RCSID(0, "$NetBSD: npf_sendpkt.c,v 1.23 2023/02/12 13:38:37 kardel Exp $");
3763012b51Srmind
3863012b51Srmind #include <sys/param.h>
3915d58f91Srmind #include <sys/types.h>
4063012b51Srmind
4163012b51Srmind #include <netinet/in_systm.h>
4263012b51Srmind #include <netinet/in.h>
4363012b51Srmind #include <netinet/ip.h>
4463012b51Srmind #include <netinet/ip_icmp.h>
4563012b51Srmind #include <netinet/ip_var.h>
4663012b51Srmind #include <netinet/tcp.h>
475a5d868dSzoltan #include <netinet/ip6.h>
485a5d868dSzoltan #include <netinet/icmp6.h>
495a5d868dSzoltan #include <netinet6/ip6_var.h>
509e585af8Smaxv #include <netinet6/scope6_var.h>
5163012b51Srmind #include <sys/mbuf.h>
52f75d79ebSchristos #endif
5363012b51Srmind
5463012b51Srmind #include "npf_impl.h"
5563012b51Srmind
5663012b51Srmind #define DEFAULT_IP_TTL (ip_defttl)
5763012b51Srmind
58f75d79ebSchristos #if defined(_NPF_STANDALONE)
59b899bfd9Srmind #define m_gethdr(t, f) (npf)->mbufops->alloc((npf), 0, 0)
60df92f5b5Srmind #define m_freem(m) (npc)->npc_ctx->mbufops->free(m)
61df92f5b5Srmind #define mtod(m,t) ((t)((npc)->npc_ctx->mbufops->getdata(m)))
62f75d79ebSchristos #endif
63f75d79ebSchristos
64f75d79ebSchristos #if !defined(INET6) || defined(_NPF_STANDALONE)
653e491282Srmind #define in6_cksum(...) 0
663e491282Srmind #define ip6_output(...) 0
673e491282Srmind #define icmp6_error(m, ...) m_freem(m)
68df92f5b5Srmind #define npf_ip6_setscope(n, i) ((void)(i), 0)
6939013e66Srmind #endif
7039013e66Srmind
7139013e66Srmind #if defined(INET6)
7239013e66Srmind static int
npf_ip6_setscope(const npf_cache_t * npc,struct ip6_hdr * ip6)7339013e66Srmind npf_ip6_setscope(const npf_cache_t *npc, struct ip6_hdr *ip6)
7439013e66Srmind {
7539013e66Srmind const struct ifnet *rcvif = npc->npc_nbuf->nb_ifp;
7639013e66Srmind
7739013e66Srmind if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
7839013e66Srmind return EINVAL;
7939013e66Srmind }
8039013e66Srmind if (in6_setscope(&ip6->ip6_src, rcvif, NULL) ||
8139013e66Srmind in6_setscope(&ip6->ip6_dst, rcvif, NULL)) {
8239013e66Srmind return EINVAL;
8339013e66Srmind }
8439013e66Srmind return 0;
8539013e66Srmind }
863e491282Srmind #endif
873e491282Srmind
8863012b51Srmind /*
8963012b51Srmind * npf_return_tcp: return a TCP reset (RST) packet.
9063012b51Srmind */
9163012b51Srmind static int
npf_return_tcp(npf_cache_t * npc)92fad8b2d7Srmind npf_return_tcp(npf_cache_t *npc)
9363012b51Srmind {
94f75d79ebSchristos npf_t *npf = npc->npc_ctx;
9563012b51Srmind struct mbuf *m;
965a5d868dSzoltan struct ip *ip = NULL;
975a5d868dSzoltan struct ip6_hdr *ip6 = NULL;
9897b932f1Srmind struct tcphdr *oth, *th;
9963012b51Srmind tcp_seq seq, ack;
10097b932f1Srmind int tcpdlen, len;
10197b932f1Srmind uint32_t win;
10263012b51Srmind
10363012b51Srmind /* Fetch relevant data. */
1045a5d868dSzoltan KASSERT(npf_iscached(npc, NPC_IP46));
1055a5d868dSzoltan KASSERT(npf_iscached(npc, NPC_LAYER4));
106fad8b2d7Srmind tcpdlen = npf_tcpsaw(npc, &seq, &ack, &win);
107352f1606Srmind oth = npc->npc_l4.tcp;
10897b932f1Srmind
10997b932f1Srmind if (oth->th_flags & TH_RST) {
11063012b51Srmind return 0;
11163012b51Srmind }
11263012b51Srmind
11363012b51Srmind /* Create and setup a network buffer. */
114fad8b2d7Srmind if (npf_iscached(npc, NPC_IP4)) {
11563012b51Srmind len = sizeof(struct ip) + sizeof(struct tcphdr);
1163e491282Srmind } else if (npf_iscached(npc, NPC_IP6)) {
1175a5d868dSzoltan len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1183e491282Srmind } else {
1193e491282Srmind return EINVAL;
1207d78d5aeSzoltan }
1215a5d868dSzoltan
12263012b51Srmind m = m_gethdr(M_DONTWAIT, MT_HEADER);
12363012b51Srmind if (m == NULL) {
12463012b51Srmind return ENOMEM;
12563012b51Srmind }
126f75d79ebSchristos #if !defined(_NPF_STANDALONE)
12763012b51Srmind m->m_data += max_linkhdr;
12863012b51Srmind m->m_len = len;
12963012b51Srmind m->m_pkthdr.len = len;
130f75d79ebSchristos (void)npf;
131f75d79ebSchristos #endif
1327d78d5aeSzoltan if (npf_iscached(npc, NPC_IP4)) {
133352f1606Srmind struct ip *oip = npc->npc_ip.v4;
13409cdfd6aSrmind
13563012b51Srmind ip = mtod(m, struct ip *);
13663012b51Srmind memset(ip, 0, len);
13763012b51Srmind
13863012b51Srmind /*
13953e5b8ccSrmind * First, partially fill IPv4 header for TCP checksum.
14063012b51Srmind * Note: IP length contains TCP header length.
14163012b51Srmind */
14263012b51Srmind ip->ip_p = IPPROTO_TCP;
14397b932f1Srmind ip->ip_src.s_addr = oip->ip_dst.s_addr;
14497b932f1Srmind ip->ip_dst.s_addr = oip->ip_src.s_addr;
14563012b51Srmind ip->ip_len = htons(sizeof(struct tcphdr));
14663012b51Srmind
14763012b51Srmind th = (struct tcphdr *)(ip + 1);
1485a5d868dSzoltan } else {
149352f1606Srmind struct ip6_hdr *oip = npc->npc_ip.v6;
15009cdfd6aSrmind
15109cdfd6aSrmind KASSERT(npf_iscached(npc, NPC_IP6));
1525a5d868dSzoltan ip6 = mtod(m, struct ip6_hdr *);
1535a5d868dSzoltan memset(ip6, 0, len);
1545a5d868dSzoltan
1555a5d868dSzoltan ip6->ip6_nxt = IPPROTO_TCP;
1565a5d868dSzoltan ip6->ip6_hlim = IPV6_DEFHLIM;
1575a5d868dSzoltan memcpy(&ip6->ip6_src, &oip->ip6_dst, sizeof(struct in6_addr));
1585a5d868dSzoltan memcpy(&ip6->ip6_dst, &oip->ip6_src, sizeof(struct in6_addr));
1595a5d868dSzoltan ip6->ip6_plen = htons(len);
1605a5d868dSzoltan ip6->ip6_vfc = IPV6_VERSION;
1615a5d868dSzoltan
1625a5d868dSzoltan th = (struct tcphdr *)(ip6 + 1);
1635a5d868dSzoltan }
1645a5d868dSzoltan
16553e5b8ccSrmind /*
16653e5b8ccSrmind * Construct TCP header and compute the checksum.
16753e5b8ccSrmind */
16897b932f1Srmind th->th_sport = oth->th_dport;
16997b932f1Srmind th->th_dport = oth->th_sport;
17063012b51Srmind th->th_seq = htonl(ack);
17197b932f1Srmind if (oth->th_flags & TH_SYN) {
17263012b51Srmind tcpdlen++;
17363012b51Srmind }
17463012b51Srmind th->th_ack = htonl(seq + tcpdlen);
17563012b51Srmind th->th_off = sizeof(struct tcphdr) >> 2;
17663012b51Srmind th->th_flags = TH_ACK | TH_RST;
1775a5d868dSzoltan
1787d78d5aeSzoltan if (npf_iscached(npc, NPC_IP4)) {
17963012b51Srmind th->th_sum = in_cksum(m, len);
18063012b51Srmind
18153e5b8ccSrmind /*
18253e5b8ccSrmind * Second, fill the rest of IPv4 header and correct IP length.
18353e5b8ccSrmind */
18463012b51Srmind ip->ip_v = IPVERSION;
18563012b51Srmind ip->ip_hl = sizeof(struct ip) >> 2;
18663012b51Srmind ip->ip_tos = IPTOS_LOWDELAY;
18763012b51Srmind ip->ip_len = htons(len);
18863012b51Srmind ip->ip_ttl = DEFAULT_IP_TTL;
1895a5d868dSzoltan } else {
1907d78d5aeSzoltan KASSERT(npf_iscached(npc, NPC_IP6));
19109cdfd6aSrmind th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
19209cdfd6aSrmind sizeof(struct tcphdr));
19363012b51Srmind
1949e585af8Smaxv /* Handle IPv6 scopes */
195df92f5b5Srmind if (npf_ip6_setscope(npc, ip6) != 0) {
1969e585af8Smaxv goto bad;
1979e585af8Smaxv }
198df92f5b5Srmind }
1999e585af8Smaxv
200*1165567dSkardel /* don't look at our generated reject packets going out */
201*1165567dSkardel (void)npf_mbuf_add_tag(npc->npc_nbuf, m, NPF_NTAG_PASS);
202*1165567dSkardel
20363012b51Srmind /* Pass to IP layer. */
204fad8b2d7Srmind if (npf_iscached(npc, NPC_IP4)) {
20563012b51Srmind return ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
2065a5d868dSzoltan }
2073e491282Srmind return ip6_output(m, NULL, NULL, IPV6_FORWARDING, NULL, NULL, NULL);
2089e585af8Smaxv bad:
2099e585af8Smaxv m_freem(m);
2109e585af8Smaxv return EINVAL;
21163012b51Srmind }
21263012b51Srmind
21363012b51Srmind /*
21463012b51Srmind * npf_return_icmp: return an ICMP error.
21563012b51Srmind */
21663012b51Srmind static int
npf_return_icmp(const npf_cache_t * npc)217a7d2a608Srmind npf_return_icmp(const npf_cache_t *npc)
21863012b51Srmind {
219a7d2a608Srmind struct mbuf *m = nbuf_head_mbuf(npc->npc_nbuf);
22063012b51Srmind
221*1165567dSkardel /* don't look at our generated reject packets going out */
222*1165567dSkardel (void)nbuf_add_tag(npc->npc_nbuf, NPF_NTAG_PASS);
223*1165567dSkardel
2245a5d868dSzoltan if (npf_iscached(npc, NPC_IP4)) {
22563012b51Srmind icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT, 0, 0);
22663012b51Srmind return 0;
2273e491282Srmind } else if (npf_iscached(npc, NPC_IP6)) {
22818e57226Smaxv /* Handle IPv6 scopes */
22918e57226Smaxv struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
23018e57226Smaxv
23139013e66Srmind if (npf_ip6_setscope(npc, ip6) != 0) {
23239013e66Srmind return EINVAL;
23339013e66Srmind }
2343e491282Srmind icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN, 0);
2353e491282Srmind return 0;
2363e491282Srmind }
2373e491282Srmind return EINVAL;
23863012b51Srmind }
23963012b51Srmind
24063012b51Srmind /*
24163012b51Srmind * npf_return_block: return TCP reset or ICMP host unreachable packet.
2423e491282Srmind *
2433e491282Srmind * => Returns true if the buffer was consumed (freed) and false otherwise.
24463012b51Srmind */
2453e491282Srmind bool
npf_return_block(npf_cache_t * npc,const int retfl)246a7d2a608Srmind npf_return_block(npf_cache_t *npc, const int retfl)
24763012b51Srmind {
248a3b239f6Srmind if (!npf_iscached(npc, NPC_IP46) || !npf_iscached(npc, NPC_LAYER4)) {
2493e491282Srmind return false;
25063012b51Srmind }
2510e218254Srmind switch (npc->npc_proto) {
25263012b51Srmind case IPPROTO_TCP:
25397b932f1Srmind if (retfl & NPF_RULE_RETRST) {
254fad8b2d7Srmind (void)npf_return_tcp(npc);
25597b932f1Srmind }
25663012b51Srmind break;
25763012b51Srmind case IPPROTO_UDP:
2583e491282Srmind if (retfl & NPF_RULE_RETICMP)
259a7d2a608Srmind if (npf_return_icmp(npc) == 0)
2603e491282Srmind return true;
26163012b51Srmind break;
26263012b51Srmind }
2633e491282Srmind return false;
26463012b51Srmind }
265