npf_sendpkt.c revision 1.22 1 1.1 rmind /*-
2 1.8 rmind * Copyright (c) 2010-2011 The NetBSD Foundation, Inc.
3 1.1 rmind * All rights reserved.
4 1.1 rmind *
5 1.1 rmind * This material is based upon work partially supported by The
6 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
7 1.1 rmind *
8 1.1 rmind * Redistribution and use in source and binary forms, with or without
9 1.1 rmind * modification, are permitted provided that the following conditions
10 1.1 rmind * are met:
11 1.1 rmind * 1. Redistributions of source code must retain the above copyright
12 1.1 rmind * notice, this list of conditions and the following disclaimer.
13 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer in the
15 1.1 rmind * documentation and/or other materials provided with the distribution.
16 1.1 rmind *
17 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
28 1.1 rmind */
29 1.1 rmind
30 1.1 rmind /*
31 1.1 rmind * NPF module for packet construction routines.
32 1.1 rmind */
33 1.1 rmind
34 1.16 christos #ifdef _KERNEL
35 1.1 rmind #include <sys/cdefs.h>
36 1.22 rmind __KERNEL_RCSID(0, "$NetBSD: npf_sendpkt.c,v 1.22 2020/05/30 14:16:56 rmind Exp $");
37 1.1 rmind
38 1.1 rmind #include <sys/param.h>
39 1.9 rmind #include <sys/types.h>
40 1.1 rmind
41 1.1 rmind #include <netinet/in_systm.h>
42 1.1 rmind #include <netinet/in.h>
43 1.1 rmind #include <netinet/ip.h>
44 1.1 rmind #include <netinet/ip_icmp.h>
45 1.1 rmind #include <netinet/ip_var.h>
46 1.1 rmind #include <netinet/tcp.h>
47 1.5 zoltan #include <netinet/ip6.h>
48 1.5 zoltan #include <netinet/icmp6.h>
49 1.5 zoltan #include <netinet6/ip6_var.h>
50 1.17 maxv #include <netinet6/scope6_var.h>
51 1.1 rmind #include <sys/mbuf.h>
52 1.16 christos #endif
53 1.1 rmind
54 1.1 rmind #include "npf_impl.h"
55 1.1 rmind
56 1.1 rmind #define DEFAULT_IP_TTL (ip_defttl)
57 1.1 rmind
58 1.16 christos #if defined(_NPF_STANDALONE)
59 1.22 rmind #define m_gethdr(t, f) (npf)->mbufops->alloc((npf), 0, 0)
60 1.21 rmind #define m_freem(m) (npc)->npc_ctx->mbufops->free(m)
61 1.21 rmind #define mtod(m,t) ((t)((npc)->npc_ctx->mbufops->getdata(m)))
62 1.16 christos #endif
63 1.16 christos
64 1.16 christos #if !defined(INET6) || defined(_NPF_STANDALONE)
65 1.10 rmind #define in6_cksum(...) 0
66 1.10 rmind #define ip6_output(...) 0
67 1.10 rmind #define icmp6_error(m, ...) m_freem(m)
68 1.21 rmind #define npf_ip6_setscope(n, i) ((void)(i), 0)
69 1.20 rmind #endif
70 1.20 rmind
71 1.20 rmind #if defined(INET6)
72 1.20 rmind static int
73 1.20 rmind npf_ip6_setscope(const npf_cache_t *npc, struct ip6_hdr *ip6)
74 1.20 rmind {
75 1.20 rmind const struct ifnet *rcvif = npc->npc_nbuf->nb_ifp;
76 1.20 rmind
77 1.20 rmind if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
78 1.20 rmind return EINVAL;
79 1.20 rmind }
80 1.20 rmind if (in6_setscope(&ip6->ip6_src, rcvif, NULL) ||
81 1.20 rmind in6_setscope(&ip6->ip6_dst, rcvif, NULL)) {
82 1.20 rmind return EINVAL;
83 1.20 rmind }
84 1.20 rmind return 0;
85 1.20 rmind }
86 1.10 rmind #endif
87 1.10 rmind
88 1.1 rmind /*
89 1.1 rmind * npf_return_tcp: return a TCP reset (RST) packet.
90 1.1 rmind */
91 1.1 rmind static int
92 1.8 rmind npf_return_tcp(npf_cache_t *npc)
93 1.1 rmind {
94 1.16 christos npf_t *npf = npc->npc_ctx;
95 1.1 rmind struct mbuf *m;
96 1.5 zoltan struct ip *ip = NULL;
97 1.5 zoltan struct ip6_hdr *ip6 = NULL;
98 1.3 rmind struct tcphdr *oth, *th;
99 1.1 rmind tcp_seq seq, ack;
100 1.3 rmind int tcpdlen, len;
101 1.3 rmind uint32_t win;
102 1.1 rmind
103 1.1 rmind /* Fetch relevant data. */
104 1.5 zoltan KASSERT(npf_iscached(npc, NPC_IP46));
105 1.5 zoltan KASSERT(npf_iscached(npc, NPC_LAYER4));
106 1.8 rmind tcpdlen = npf_tcpsaw(npc, &seq, &ack, &win);
107 1.13 rmind oth = npc->npc_l4.tcp;
108 1.3 rmind
109 1.3 rmind if (oth->th_flags & TH_RST) {
110 1.1 rmind return 0;
111 1.1 rmind }
112 1.1 rmind
113 1.1 rmind /* Create and setup a network buffer. */
114 1.8 rmind if (npf_iscached(npc, NPC_IP4)) {
115 1.5 zoltan len = sizeof(struct ip) + sizeof(struct tcphdr);
116 1.10 rmind } else if (npf_iscached(npc, NPC_IP6)) {
117 1.10 rmind len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
118 1.8 rmind } else {
119 1.10 rmind return EINVAL;
120 1.6 zoltan }
121 1.5 zoltan
122 1.1 rmind m = m_gethdr(M_DONTWAIT, MT_HEADER);
123 1.1 rmind if (m == NULL) {
124 1.1 rmind return ENOMEM;
125 1.1 rmind }
126 1.16 christos #if !defined(_NPF_STANDALONE)
127 1.1 rmind m->m_data += max_linkhdr;
128 1.1 rmind m->m_len = len;
129 1.1 rmind m->m_pkthdr.len = len;
130 1.16 christos (void)npf;
131 1.16 christos #endif
132 1.6 zoltan if (npf_iscached(npc, NPC_IP4)) {
133 1.13 rmind struct ip *oip = npc->npc_ip.v4;
134 1.7 rmind
135 1.5 zoltan ip = mtod(m, struct ip *);
136 1.5 zoltan memset(ip, 0, len);
137 1.5 zoltan
138 1.5 zoltan /*
139 1.11 rmind * First, partially fill IPv4 header for TCP checksum.
140 1.7 rmind * Note: IP length contains TCP header length.
141 1.7 rmind */
142 1.5 zoltan ip->ip_p = IPPROTO_TCP;
143 1.5 zoltan ip->ip_src.s_addr = oip->ip_dst.s_addr;
144 1.5 zoltan ip->ip_dst.s_addr = oip->ip_src.s_addr;
145 1.5 zoltan ip->ip_len = htons(sizeof(struct tcphdr));
146 1.5 zoltan
147 1.5 zoltan th = (struct tcphdr *)(ip + 1);
148 1.5 zoltan } else {
149 1.13 rmind struct ip6_hdr *oip = npc->npc_ip.v6;
150 1.7 rmind
151 1.6 zoltan KASSERT(npf_iscached(npc, NPC_IP6));
152 1.5 zoltan ip6 = mtod(m, struct ip6_hdr *);
153 1.5 zoltan memset(ip6, 0, len);
154 1.5 zoltan
155 1.5 zoltan ip6->ip6_nxt = IPPROTO_TCP;
156 1.5 zoltan ip6->ip6_hlim = IPV6_DEFHLIM;
157 1.5 zoltan memcpy(&ip6->ip6_src, &oip->ip6_dst, sizeof(struct in6_addr));
158 1.7 rmind memcpy(&ip6->ip6_dst, &oip->ip6_src, sizeof(struct in6_addr));
159 1.5 zoltan ip6->ip6_plen = htons(len);
160 1.5 zoltan ip6->ip6_vfc = IPV6_VERSION;
161 1.1 rmind
162 1.5 zoltan th = (struct tcphdr *)(ip6 + 1);
163 1.5 zoltan }
164 1.1 rmind
165 1.11 rmind /*
166 1.11 rmind * Construct TCP header and compute the checksum.
167 1.11 rmind */
168 1.3 rmind th->th_sport = oth->th_dport;
169 1.3 rmind th->th_dport = oth->th_sport;
170 1.1 rmind th->th_seq = htonl(ack);
171 1.3 rmind if (oth->th_flags & TH_SYN) {
172 1.1 rmind tcpdlen++;
173 1.1 rmind }
174 1.1 rmind th->th_ack = htonl(seq + tcpdlen);
175 1.1 rmind th->th_off = sizeof(struct tcphdr) >> 2;
176 1.1 rmind th->th_flags = TH_ACK | TH_RST;
177 1.1 rmind
178 1.6 zoltan if (npf_iscached(npc, NPC_IP4)) {
179 1.5 zoltan th->th_sum = in_cksum(m, len);
180 1.5 zoltan
181 1.11 rmind /*
182 1.11 rmind * Second, fill the rest of IPv4 header and correct IP length.
183 1.11 rmind */
184 1.5 zoltan ip->ip_v = IPVERSION;
185 1.5 zoltan ip->ip_hl = sizeof(struct ip) >> 2;
186 1.5 zoltan ip->ip_tos = IPTOS_LOWDELAY;
187 1.5 zoltan ip->ip_len = htons(len);
188 1.5 zoltan ip->ip_ttl = DEFAULT_IP_TTL;
189 1.5 zoltan } else {
190 1.6 zoltan KASSERT(npf_iscached(npc, NPC_IP6));
191 1.7 rmind th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
192 1.7 rmind sizeof(struct tcphdr));
193 1.1 rmind
194 1.21 rmind /* Handle IPv6 scopes */
195 1.21 rmind if (npf_ip6_setscope(npc, ip6) != 0) {
196 1.21 rmind goto bad;
197 1.21 rmind }
198 1.17 maxv }
199 1.17 maxv
200 1.1 rmind /* Pass to IP layer. */
201 1.8 rmind if (npf_iscached(npc, NPC_IP4)) {
202 1.5 zoltan return ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
203 1.5 zoltan }
204 1.10 rmind return ip6_output(m, NULL, NULL, IPV6_FORWARDING, NULL, NULL, NULL);
205 1.17 maxv bad:
206 1.17 maxv m_freem(m);
207 1.17 maxv return EINVAL;
208 1.1 rmind }
209 1.1 rmind
210 1.1 rmind /*
211 1.1 rmind * npf_return_icmp: return an ICMP error.
212 1.1 rmind */
213 1.1 rmind static int
214 1.15 rmind npf_return_icmp(const npf_cache_t *npc)
215 1.1 rmind {
216 1.15 rmind struct mbuf *m = nbuf_head_mbuf(npc->npc_nbuf);
217 1.1 rmind
218 1.5 zoltan if (npf_iscached(npc, NPC_IP4)) {
219 1.5 zoltan icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT, 0, 0);
220 1.10 rmind return 0;
221 1.10 rmind } else if (npf_iscached(npc, NPC_IP6)) {
222 1.18 maxv /* Handle IPv6 scopes */
223 1.18 maxv struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
224 1.20 rmind
225 1.20 rmind if (npf_ip6_setscope(npc, ip6) != 0) {
226 1.18 maxv return EINVAL;
227 1.18 maxv }
228 1.5 zoltan icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN, 0);
229 1.10 rmind return 0;
230 1.5 zoltan }
231 1.10 rmind return EINVAL;
232 1.1 rmind }
233 1.1 rmind
234 1.1 rmind /*
235 1.1 rmind * npf_return_block: return TCP reset or ICMP host unreachable packet.
236 1.10 rmind *
237 1.10 rmind * => Returns true if the buffer was consumed (freed) and false otherwise.
238 1.1 rmind */
239 1.10 rmind bool
240 1.15 rmind npf_return_block(npf_cache_t *npc, const int retfl)
241 1.1 rmind {
242 1.12 rmind if (!npf_iscached(npc, NPC_IP46) || !npf_iscached(npc, NPC_LAYER4)) {
243 1.10 rmind return false;
244 1.1 rmind }
245 1.14 rmind switch (npc->npc_proto) {
246 1.1 rmind case IPPROTO_TCP:
247 1.3 rmind if (retfl & NPF_RULE_RETRST) {
248 1.8 rmind (void)npf_return_tcp(npc);
249 1.3 rmind }
250 1.1 rmind break;
251 1.1 rmind case IPPROTO_UDP:
252 1.10 rmind if (retfl & NPF_RULE_RETICMP)
253 1.15 rmind if (npf_return_icmp(npc) == 0)
254 1.10 rmind return true;
255 1.1 rmind break;
256 1.1 rmind }
257 1.10 rmind return false;
258 1.1 rmind }
259