163012b51Srmind /*-
2255cf3c4Srmind * 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
340473fe8bSchristos #ifdef _KERNEL
3563012b51Srmind #include <sys/cdefs.h>
36*9a2fd6caSkardel __KERNEL_RCSID(0, "$NetBSD: npf_sendpkt.c,v 1.23 2023/02/12 13:38:37 kardel Exp $");
3763012b51Srmind
3863012b51Srmind #include <sys/param.h>
3975207daeSrmind #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>
47e8a3af9dSzoltan #include <netinet/ip6.h>
48e8a3af9dSzoltan #include <netinet/icmp6.h>
49e8a3af9dSzoltan #include <netinet6/ip6_var.h>
50653bddb6Smaxv #include <netinet6/scope6_var.h>
5163012b51Srmind #include <sys/mbuf.h>
520473fe8bSchristos #endif
5363012b51Srmind
5463012b51Srmind #include "npf_impl.h"
5563012b51Srmind
5663012b51Srmind #define DEFAULT_IP_TTL (ip_defttl)
5763012b51Srmind
580473fe8bSchristos #if defined(_NPF_STANDALONE)
59d6939920Srmind #define m_gethdr(t, f) (npf)->mbufops->alloc((npf), 0, 0)
60f0d65104Srmind #define m_freem(m) (npc)->npc_ctx->mbufops->free(m)
61f0d65104Srmind #define mtod(m,t) ((t)((npc)->npc_ctx->mbufops->getdata(m)))
620473fe8bSchristos #endif
630473fe8bSchristos
640473fe8bSchristos #if !defined(INET6) || defined(_NPF_STANDALONE)
657efbea87Srmind #define in6_cksum(...) 0
667efbea87Srmind #define ip6_output(...) 0
677efbea87Srmind #define icmp6_error(m, ...) m_freem(m)
68f0d65104Srmind #define npf_ip6_setscope(n, i) ((void)(i), 0)
6994550a6dSrmind #endif
7094550a6dSrmind
7194550a6dSrmind #if defined(INET6)
7294550a6dSrmind static int
npf_ip6_setscope(const npf_cache_t * npc,struct ip6_hdr * ip6)7394550a6dSrmind npf_ip6_setscope(const npf_cache_t *npc, struct ip6_hdr *ip6)
7494550a6dSrmind {
7594550a6dSrmind const struct ifnet *rcvif = npc->npc_nbuf->nb_ifp;
7694550a6dSrmind
7794550a6dSrmind if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
7894550a6dSrmind return EINVAL;
7994550a6dSrmind }
8094550a6dSrmind if (in6_setscope(&ip6->ip6_src, rcvif, NULL) ||
8194550a6dSrmind in6_setscope(&ip6->ip6_dst, rcvif, NULL)) {
8294550a6dSrmind return EINVAL;
8394550a6dSrmind }
8494550a6dSrmind return 0;
8594550a6dSrmind }
867efbea87Srmind #endif
877efbea87Srmind
8863012b51Srmind /*
8963012b51Srmind * npf_return_tcp: return a TCP reset (RST) packet.
9063012b51Srmind */
9163012b51Srmind static int
npf_return_tcp(npf_cache_t * npc)92255cf3c4Srmind npf_return_tcp(npf_cache_t *npc)
9363012b51Srmind {
940473fe8bSchristos npf_t *npf = npc->npc_ctx;
9563012b51Srmind struct mbuf *m;
96e8a3af9dSzoltan struct ip *ip = NULL;
97e8a3af9dSzoltan 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. */
104e8a3af9dSzoltan KASSERT(npf_iscached(npc, NPC_IP46));
105e8a3af9dSzoltan KASSERT(npf_iscached(npc, NPC_LAYER4));
106255cf3c4Srmind tcpdlen = npf_tcpsaw(npc, &seq, &ack, &win);
107f49c13e6Srmind 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. */
114255cf3c4Srmind if (npf_iscached(npc, NPC_IP4)) {
11563012b51Srmind len = sizeof(struct ip) + sizeof(struct tcphdr);
1167efbea87Srmind } else if (npf_iscached(npc, NPC_IP6)) {
117e8a3af9dSzoltan len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1187efbea87Srmind } else {
1197efbea87Srmind return EINVAL;
12071ad7745Szoltan }
121e8a3af9dSzoltan
12263012b51Srmind m = m_gethdr(M_DONTWAIT, MT_HEADER);
12363012b51Srmind if (m == NULL) {
12463012b51Srmind return ENOMEM;
12563012b51Srmind }
1260473fe8bSchristos #if !defined(_NPF_STANDALONE)
12763012b51Srmind m->m_data += max_linkhdr;
12863012b51Srmind m->m_len = len;
12963012b51Srmind m->m_pkthdr.len = len;
1300473fe8bSchristos (void)npf;
1310473fe8bSchristos #endif
13271ad7745Szoltan if (npf_iscached(npc, NPC_IP4)) {
133f49c13e6Srmind struct ip *oip = npc->npc_ip.v4;
13407f1dde7Srmind
13563012b51Srmind ip = mtod(m, struct ip *);
13663012b51Srmind memset(ip, 0, len);
13763012b51Srmind
13863012b51Srmind /*
13938bb64a8Srmind * 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);
148e8a3af9dSzoltan } else {
149f49c13e6Srmind struct ip6_hdr *oip = npc->npc_ip.v6;
15007f1dde7Srmind
15107f1dde7Srmind KASSERT(npf_iscached(npc, NPC_IP6));
152e8a3af9dSzoltan ip6 = mtod(m, struct ip6_hdr *);
153e8a3af9dSzoltan memset(ip6, 0, len);
154e8a3af9dSzoltan
155e8a3af9dSzoltan ip6->ip6_nxt = IPPROTO_TCP;
156e8a3af9dSzoltan ip6->ip6_hlim = IPV6_DEFHLIM;
157e8a3af9dSzoltan memcpy(&ip6->ip6_src, &oip->ip6_dst, sizeof(struct in6_addr));
158e8a3af9dSzoltan memcpy(&ip6->ip6_dst, &oip->ip6_src, sizeof(struct in6_addr));
159e8a3af9dSzoltan ip6->ip6_plen = htons(len);
160e8a3af9dSzoltan ip6->ip6_vfc = IPV6_VERSION;
161e8a3af9dSzoltan
162e8a3af9dSzoltan th = (struct tcphdr *)(ip6 + 1);
163e8a3af9dSzoltan }
164e8a3af9dSzoltan
16538bb64a8Srmind /*
16638bb64a8Srmind * Construct TCP header and compute the checksum.
16738bb64a8Srmind */
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;
177e8a3af9dSzoltan
17871ad7745Szoltan if (npf_iscached(npc, NPC_IP4)) {
17963012b51Srmind th->th_sum = in_cksum(m, len);
18063012b51Srmind
18138bb64a8Srmind /*
18238bb64a8Srmind * Second, fill the rest of IPv4 header and correct IP length.
18338bb64a8Srmind */
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;
189e8a3af9dSzoltan } else {
19071ad7745Szoltan KASSERT(npf_iscached(npc, NPC_IP6));
19107f1dde7Srmind th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
19207f1dde7Srmind sizeof(struct tcphdr));
19363012b51Srmind
194653bddb6Smaxv /* Handle IPv6 scopes */
195f0d65104Srmind if (npf_ip6_setscope(npc, ip6) != 0) {
196653bddb6Smaxv goto bad;
197653bddb6Smaxv }
198f0d65104Srmind }
199653bddb6Smaxv
200*9a2fd6caSkardel /* don't look at our generated reject packets going out */
201*9a2fd6caSkardel (void)npf_mbuf_add_tag(npc->npc_nbuf, m, NPF_NTAG_PASS);
202*9a2fd6caSkardel
20363012b51Srmind /* Pass to IP layer. */
204255cf3c4Srmind if (npf_iscached(npc, NPC_IP4)) {
20563012b51Srmind return ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
206e8a3af9dSzoltan }
2077efbea87Srmind return ip6_output(m, NULL, NULL, IPV6_FORWARDING, NULL, NULL, NULL);
208653bddb6Smaxv bad:
209653bddb6Smaxv m_freem(m);
210653bddb6Smaxv 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)217d5cb4211Srmind npf_return_icmp(const npf_cache_t *npc)
21863012b51Srmind {
219d5cb4211Srmind struct mbuf *m = nbuf_head_mbuf(npc->npc_nbuf);
22063012b51Srmind
221*9a2fd6caSkardel /* don't look at our generated reject packets going out */
222*9a2fd6caSkardel (void)nbuf_add_tag(npc->npc_nbuf, NPF_NTAG_PASS);
223*9a2fd6caSkardel
224e8a3af9dSzoltan if (npf_iscached(npc, NPC_IP4)) {
22563012b51Srmind icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT, 0, 0);
22663012b51Srmind return 0;
2277efbea87Srmind } else if (npf_iscached(npc, NPC_IP6)) {
228ec6e715aSmaxv /* Handle IPv6 scopes */
229ec6e715aSmaxv struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
230ec6e715aSmaxv
23194550a6dSrmind if (npf_ip6_setscope(npc, ip6) != 0) {
23294550a6dSrmind return EINVAL;
23394550a6dSrmind }
2347efbea87Srmind icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN, 0);
2357efbea87Srmind return 0;
2367efbea87Srmind }
2377efbea87Srmind return EINVAL;
23863012b51Srmind }
23963012b51Srmind
24063012b51Srmind /*
24163012b51Srmind * npf_return_block: return TCP reset or ICMP host unreachable packet.
2427efbea87Srmind *
2437efbea87Srmind * => Returns true if the buffer was consumed (freed) and false otherwise.
24463012b51Srmind */
2457efbea87Srmind bool
npf_return_block(npf_cache_t * npc,const int retfl)246d5cb4211Srmind npf_return_block(npf_cache_t *npc, const int retfl)
24763012b51Srmind {
248eac94053Srmind if (!npf_iscached(npc, NPC_IP46) || !npf_iscached(npc, NPC_LAYER4)) {
2497efbea87Srmind return false;
25063012b51Srmind }
25137527ec6Srmind switch (npc->npc_proto) {
25263012b51Srmind case IPPROTO_TCP:
25397b932f1Srmind if (retfl & NPF_RULE_RETRST) {
254255cf3c4Srmind (void)npf_return_tcp(npc);
25597b932f1Srmind }
25663012b51Srmind break;
25763012b51Srmind case IPPROTO_UDP:
2587efbea87Srmind if (retfl & NPF_RULE_RETICMP)
259d5cb4211Srmind if (npf_return_icmp(npc) == 0)
2607efbea87Srmind return true;
26163012b51Srmind break;
26263012b51Srmind }
2637efbea87Srmind return false;
26463012b51Srmind }
265