npf_inet.c revision 1.16.2.4 1 1.16.2.3 tls /* $NetBSD: npf_inet.c,v 1.16.2.4 2014/08/20 00:04:35 tls Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.16.2.4 tls * Copyright (c) 2009-2014 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * This material is based upon work partially supported by The
8 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 1.1 rmind *
10 1.1 rmind * Redistribution and use in source and binary forms, with or without
11 1.1 rmind * modification, are permitted provided that the following conditions
12 1.1 rmind * are met:
13 1.1 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer.
15 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1 rmind * documentation and/or other materials provided with the distribution.
18 1.1 rmind *
19 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1 rmind */
31 1.1 rmind
32 1.1 rmind /*
33 1.16.2.3 tls * Various protocol related helper routines.
34 1.12 rmind *
35 1.12 rmind * This layer manipulates npf_cache_t structure i.e. caches requested headers
36 1.12 rmind * and stores which information was cached in the information bit field.
37 1.12 rmind * It is also responsibility of this layer to update or invalidate the cache
38 1.12 rmind * on rewrites (e.g. by translation routines).
39 1.1 rmind */
40 1.1 rmind
41 1.1 rmind #include <sys/cdefs.h>
42 1.16.2.3 tls __KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.16.2.4 2014/08/20 00:04:35 tls Exp $");
43 1.1 rmind
44 1.1 rmind #include <sys/param.h>
45 1.11 rmind #include <sys/types.h>
46 1.1 rmind
47 1.4 rmind #include <net/pfil.h>
48 1.4 rmind #include <net/if.h>
49 1.4 rmind #include <net/ethertypes.h>
50 1.4 rmind #include <net/if_ether.h>
51 1.4 rmind
52 1.1 rmind #include <netinet/in_systm.h>
53 1.1 rmind #include <netinet/in.h>
54 1.1 rmind #include <netinet/ip.h>
55 1.4 rmind #include <netinet/ip6.h>
56 1.1 rmind #include <netinet/tcp.h>
57 1.1 rmind #include <netinet/udp.h>
58 1.1 rmind #include <netinet/ip_icmp.h>
59 1.1 rmind
60 1.1 rmind #include "npf_impl.h"
61 1.1 rmind
62 1.1 rmind /*
63 1.16.2.4 tls * npf_fixup{16,32}_cksum: incremental update of the Internet checksum.
64 1.1 rmind */
65 1.1 rmind
66 1.1 rmind uint16_t
67 1.1 rmind npf_fixup16_cksum(uint16_t cksum, uint16_t odatum, uint16_t ndatum)
68 1.1 rmind {
69 1.1 rmind uint32_t sum;
70 1.1 rmind
71 1.1 rmind /*
72 1.1 rmind * RFC 1624:
73 1.1 rmind * HC' = ~(~HC + ~m + m')
74 1.16.2.4 tls *
75 1.16.2.4 tls * Note: 1's complement sum is endian-independent (RFC 1071, page 2).
76 1.1 rmind */
77 1.16.2.4 tls sum = ~cksum & 0xffff;
78 1.16.2.4 tls sum += (~odatum & 0xffff) + ndatum;
79 1.1 rmind sum = (sum >> 16) + (sum & 0xffff);
80 1.1 rmind sum += (sum >> 16);
81 1.1 rmind
82 1.16.2.4 tls return ~sum & 0xffff;
83 1.1 rmind }
84 1.1 rmind
85 1.1 rmind uint16_t
86 1.1 rmind npf_fixup32_cksum(uint16_t cksum, uint32_t odatum, uint32_t ndatum)
87 1.1 rmind {
88 1.16.2.4 tls uint32_t sum;
89 1.1 rmind
90 1.16.2.4 tls /*
91 1.16.2.4 tls * Checksum 32-bit datum as as two 16-bit. Note, the first
92 1.16.2.4 tls * 32->16 bit reduction is not necessary.
93 1.16.2.4 tls */
94 1.16.2.4 tls sum = ~cksum & 0xffff;
95 1.16.2.4 tls sum += (~odatum & 0xffff) + (ndatum & 0xffff);
96 1.16.2.4 tls
97 1.16.2.4 tls sum += (~odatum >> 16) + (ndatum >> 16);
98 1.16.2.4 tls sum = (sum >> 16) + (sum & 0xffff);
99 1.16.2.4 tls sum += (sum >> 16);
100 1.16.2.4 tls return ~sum & 0xffff;
101 1.1 rmind }
102 1.1 rmind
103 1.1 rmind /*
104 1.4 rmind * npf_addr_cksum: calculate checksum of the address, either IPv4 or IPv6.
105 1.4 rmind */
106 1.4 rmind uint16_t
107 1.16.2.2 tls npf_addr_cksum(uint16_t cksum, int sz, const npf_addr_t *oaddr,
108 1.16.2.2 tls const npf_addr_t *naddr)
109 1.4 rmind {
110 1.16.2.2 tls const uint32_t *oip32 = (const uint32_t *)oaddr;
111 1.16.2.2 tls const uint32_t *nip32 = (const uint32_t *)naddr;
112 1.4 rmind
113 1.4 rmind KASSERT(sz % sizeof(uint32_t) == 0);
114 1.4 rmind do {
115 1.4 rmind cksum = npf_fixup32_cksum(cksum, *oip32++, *nip32++);
116 1.4 rmind sz -= sizeof(uint32_t);
117 1.4 rmind } while (sz);
118 1.4 rmind
119 1.4 rmind return cksum;
120 1.4 rmind }
121 1.4 rmind
122 1.4 rmind /*
123 1.16.2.4 tls * npf_addr_sum: provide IP addresses as a XORed 32-bit integer.
124 1.4 rmind * Note: used for hash function.
125 1.1 rmind */
126 1.4 rmind uint32_t
127 1.16.2.4 tls npf_addr_mix(const int sz, const npf_addr_t *a1, const npf_addr_t *a2)
128 1.1 rmind {
129 1.4 rmind uint32_t mix = 0;
130 1.1 rmind
131 1.5 rmind KASSERT(sz > 0 && a1 != NULL && a2 != NULL);
132 1.5 rmind
133 1.16.2.4 tls for (int i = 0; i < (sz >> 2); i++) {
134 1.16.2.4 tls mix ^= a1->s6_addr32[i];
135 1.16.2.4 tls mix ^= a2->s6_addr32[i];
136 1.4 rmind }
137 1.4 rmind return mix;
138 1.4 rmind }
139 1.1 rmind
140 1.13 rmind /*
141 1.13 rmind * npf_addr_mask: apply the mask to a given address and store the result.
142 1.13 rmind */
143 1.13 rmind void
144 1.13 rmind npf_addr_mask(const npf_addr_t *addr, const npf_netmask_t mask,
145 1.13 rmind const int alen, npf_addr_t *out)
146 1.12 rmind {
147 1.13 rmind const int nwords = alen >> 2;
148 1.12 rmind uint_fast8_t length = mask;
149 1.12 rmind
150 1.12 rmind /* Note: maximum length is 32 for IPv4 and 128 for IPv6. */
151 1.12 rmind KASSERT(length <= NPF_MAX_NETMASK);
152 1.12 rmind
153 1.13 rmind for (int i = 0; i < nwords; i++) {
154 1.13 rmind uint32_t wordmask;
155 1.13 rmind
156 1.12 rmind if (length >= 32) {
157 1.13 rmind wordmask = htonl(0xffffffff);
158 1.12 rmind length -= 32;
159 1.13 rmind } else if (length) {
160 1.13 rmind wordmask = htonl(0xffffffff << (32 - length));
161 1.13 rmind length = 0;
162 1.12 rmind } else {
163 1.13 rmind wordmask = 0;
164 1.12 rmind }
165 1.13 rmind out->s6_addr32[i] = addr->s6_addr32[i] & wordmask;
166 1.12 rmind }
167 1.12 rmind }
168 1.12 rmind
169 1.12 rmind /*
170 1.12 rmind * npf_addr_cmp: compare two addresses, either IPv4 or IPv6.
171 1.12 rmind *
172 1.13 rmind * => Return 0 if equal and negative/positive if less/greater accordingly.
173 1.12 rmind * => Ignore the mask, if NPF_NO_NETMASK is specified.
174 1.12 rmind */
175 1.12 rmind int
176 1.12 rmind npf_addr_cmp(const npf_addr_t *addr1, const npf_netmask_t mask1,
177 1.13 rmind const npf_addr_t *addr2, const npf_netmask_t mask2, const int alen)
178 1.12 rmind {
179 1.13 rmind npf_addr_t realaddr1, realaddr2;
180 1.12 rmind
181 1.12 rmind if (mask1 != NPF_NO_NETMASK) {
182 1.13 rmind npf_addr_mask(addr1, mask1, alen, &realaddr1);
183 1.13 rmind addr1 = &realaddr1;
184 1.12 rmind }
185 1.12 rmind if (mask2 != NPF_NO_NETMASK) {
186 1.13 rmind npf_addr_mask(addr2, mask2, alen, &realaddr2);
187 1.13 rmind addr2 = &realaddr2;
188 1.12 rmind }
189 1.13 rmind return memcmp(addr1, addr2, alen);
190 1.12 rmind }
191 1.12 rmind
192 1.4 rmind /*
193 1.4 rmind * npf_tcpsaw: helper to fetch SEQ, ACK, WIN and return TCP data length.
194 1.12 rmind *
195 1.12 rmind * => Returns all values in host byte-order.
196 1.4 rmind */
197 1.4 rmind int
198 1.12 rmind npf_tcpsaw(const npf_cache_t *npc, tcp_seq *seq, tcp_seq *ack, uint32_t *win)
199 1.4 rmind {
200 1.16.2.2 tls const struct tcphdr *th = npc->npc_l4.tcp;
201 1.8 rmind u_int thlen;
202 1.1 rmind
203 1.7 zoltan KASSERT(npf_iscached(npc, NPC_TCP));
204 1.1 rmind
205 1.4 rmind *seq = ntohl(th->th_seq);
206 1.4 rmind *ack = ntohl(th->th_ack);
207 1.4 rmind *win = (uint32_t)ntohs(th->th_win);
208 1.8 rmind thlen = th->th_off << 2;
209 1.1 rmind
210 1.7 zoltan if (npf_iscached(npc, NPC_IP4)) {
211 1.16.2.2 tls const struct ip *ip = npc->npc_ip.v4;
212 1.16.2.2 tls return ntohs(ip->ip_len) - npc->npc_hlen - thlen;
213 1.12 rmind } else if (npf_iscached(npc, NPC_IP6)) {
214 1.16.2.2 tls const struct ip6_hdr *ip6 = npc->npc_ip.v6;
215 1.8 rmind return ntohs(ip6->ip6_plen) - thlen;
216 1.7 zoltan }
217 1.7 zoltan return 0;
218 1.1 rmind }
219 1.1 rmind
220 1.1 rmind /*
221 1.4 rmind * npf_fetch_tcpopts: parse and return TCP options.
222 1.1 rmind */
223 1.1 rmind bool
224 1.16.2.4 tls npf_fetch_tcpopts(npf_cache_t *npc, uint16_t *mss, int *wscale)
225 1.1 rmind {
226 1.16.2.4 tls nbuf_t *nbuf = npc->npc_nbuf;
227 1.16.2.2 tls const struct tcphdr *th = npc->npc_l4.tcp;
228 1.4 rmind int topts_len, step;
229 1.16.2.2 tls void *nptr;
230 1.4 rmind uint8_t val;
231 1.16.2.2 tls bool ok;
232 1.4 rmind
233 1.7 zoltan KASSERT(npf_iscached(npc, NPC_IP46));
234 1.7 zoltan KASSERT(npf_iscached(npc, NPC_TCP));
235 1.10 rmind
236 1.4 rmind /* Determine if there are any TCP options, get their length. */
237 1.4 rmind topts_len = (th->th_off << 2) - sizeof(struct tcphdr);
238 1.4 rmind if (topts_len <= 0) {
239 1.4 rmind /* No options. */
240 1.1 rmind return false;
241 1.4 rmind }
242 1.4 rmind KASSERT(topts_len <= MAX_TCPOPTLEN);
243 1.1 rmind
244 1.4 rmind /* First step: IP and TCP header up to options. */
245 1.16.2.2 tls step = npc->npc_hlen + sizeof(struct tcphdr);
246 1.16.2.2 tls nbuf_reset(nbuf);
247 1.4 rmind next:
248 1.16.2.2 tls if ((nptr = nbuf_advance(nbuf, step, 1)) == NULL) {
249 1.16.2.2 tls ok = false;
250 1.16.2.2 tls goto done;
251 1.4 rmind }
252 1.16.2.2 tls val = *(uint8_t *)nptr;
253 1.12 rmind
254 1.4 rmind switch (val) {
255 1.4 rmind case TCPOPT_EOL:
256 1.4 rmind /* Done. */
257 1.16.2.2 tls ok = true;
258 1.16.2.2 tls goto done;
259 1.4 rmind case TCPOPT_NOP:
260 1.4 rmind topts_len--;
261 1.4 rmind step = 1;
262 1.4 rmind break;
263 1.4 rmind case TCPOPT_MAXSEG:
264 1.16.2.2 tls if ((nptr = nbuf_advance(nbuf, 2, 2)) == NULL) {
265 1.16.2.2 tls ok = false;
266 1.16.2.2 tls goto done;
267 1.4 rmind }
268 1.4 rmind if (mss) {
269 1.16.2.2 tls if (*mss) {
270 1.16.2.2 tls memcpy(nptr, mss, sizeof(uint16_t));
271 1.16.2.2 tls } else {
272 1.16.2.2 tls memcpy(mss, nptr, sizeof(uint16_t));
273 1.16.2.2 tls }
274 1.4 rmind }
275 1.4 rmind topts_len -= TCPOLEN_MAXSEG;
276 1.16.2.2 tls step = 2;
277 1.4 rmind break;
278 1.4 rmind case TCPOPT_WINDOW:
279 1.10 rmind /* TCP Window Scaling (RFC 1323). */
280 1.16.2.2 tls if ((nptr = nbuf_advance(nbuf, 2, 1)) == NULL) {
281 1.16.2.2 tls ok = false;
282 1.16.2.2 tls goto done;
283 1.4 rmind }
284 1.16.2.2 tls val = *(uint8_t *)nptr;
285 1.4 rmind *wscale = (val > TCP_MAX_WINSHIFT) ? TCP_MAX_WINSHIFT : val;
286 1.4 rmind topts_len -= TCPOLEN_WINDOW;
287 1.16.2.2 tls step = 1;
288 1.4 rmind break;
289 1.4 rmind default:
290 1.16.2.2 tls if ((nptr = nbuf_advance(nbuf, 1, 1)) == NULL) {
291 1.16.2.2 tls ok = false;
292 1.16.2.2 tls goto done;
293 1.4 rmind }
294 1.16.2.2 tls val = *(uint8_t *)nptr;
295 1.16 rmind if (val < 2 || val > topts_len) {
296 1.16.2.2 tls ok = false;
297 1.16.2.2 tls goto done;
298 1.4 rmind }
299 1.4 rmind topts_len -= val;
300 1.4 rmind step = val - 1;
301 1.4 rmind }
302 1.12 rmind
303 1.6 rmind /* Any options left? */
304 1.4 rmind if (__predict_true(topts_len > 0)) {
305 1.4 rmind goto next;
306 1.4 rmind }
307 1.16.2.2 tls ok = true;
308 1.16.2.2 tls done:
309 1.16.2.2 tls if (nbuf_flag_p(nbuf, NBUF_DATAREF_RESET)) {
310 1.16.2.4 tls npf_recache(npc);
311 1.16.2.2 tls }
312 1.16.2.2 tls return ok;
313 1.1 rmind }
314 1.1 rmind
315 1.16.2.2 tls static int
316 1.16.2.2 tls npf_cache_ip(npf_cache_t *npc, nbuf_t *nbuf)
317 1.1 rmind {
318 1.16.2.2 tls const void *nptr = nbuf_dataptr(nbuf);
319 1.16.2.2 tls const uint8_t ver = *(const uint8_t *)nptr;
320 1.16.2.2 tls int flags = 0;
321 1.12 rmind
322 1.4 rmind switch (ver >> 4) {
323 1.12 rmind case IPVERSION: {
324 1.16.2.2 tls struct ip *ip;
325 1.12 rmind
326 1.16.2.2 tls ip = nbuf_ensure_contig(nbuf, sizeof(struct ip));
327 1.16.2.2 tls if (ip == NULL) {
328 1.16.2.2 tls return 0;
329 1.4 rmind }
330 1.12 rmind
331 1.4 rmind /* Check header length and fragment offset. */
332 1.10 rmind if ((u_int)(ip->ip_hl << 2) < sizeof(struct ip)) {
333 1.16.2.2 tls return 0;
334 1.4 rmind }
335 1.4 rmind if (ip->ip_off & ~htons(IP_DF | IP_RF)) {
336 1.4 rmind /* Note fragmentation. */
337 1.16.2.2 tls flags |= NPC_IPFRAG;
338 1.4 rmind }
339 1.12 rmind
340 1.4 rmind /* Cache: layer 3 - IPv4. */
341 1.14 rmind npc->npc_alen = sizeof(struct in_addr);
342 1.16.2.4 tls npc->npc_ips[NPF_SRC] = (npf_addr_t *)&ip->ip_src;
343 1.16.2.4 tls npc->npc_ips[NPF_DST] = (npf_addr_t *)&ip->ip_dst;
344 1.7 zoltan npc->npc_hlen = ip->ip_hl << 2;
345 1.16.2.2 tls npc->npc_proto = ip->ip_p;
346 1.16.2.2 tls
347 1.16.2.2 tls npc->npc_ip.v4 = ip;
348 1.16.2.2 tls flags |= NPC_IP4;
349 1.4 rmind break;
350 1.12 rmind }
351 1.4 rmind
352 1.12 rmind case (IPV6_VERSION >> 4): {
353 1.16.2.2 tls struct ip6_hdr *ip6;
354 1.16.2.2 tls struct ip6_ext *ip6e;
355 1.16.2.2 tls size_t off, hlen;
356 1.16.2.2 tls
357 1.16.2.2 tls ip6 = nbuf_ensure_contig(nbuf, sizeof(struct ip6_hdr));
358 1.16.2.2 tls if (ip6 == NULL) {
359 1.16.2.2 tls return 0;
360 1.7 zoltan }
361 1.16.2.2 tls
362 1.16.2.2 tls /* Set initial next-protocol value. */
363 1.16.2.2 tls hlen = sizeof(struct ip6_hdr);
364 1.16.2.2 tls npc->npc_proto = ip6->ip6_nxt;
365 1.13 rmind npc->npc_hlen = hlen;
366 1.7 zoltan
367 1.12 rmind /*
368 1.16.2.2 tls * Advance by the length of the current header.
369 1.12 rmind */
370 1.16.2.2 tls off = nbuf_offset(nbuf);
371 1.16.2.2 tls while (nbuf_advance(nbuf, hlen, 0) != NULL) {
372 1.16.2.2 tls ip6e = nbuf_ensure_contig(nbuf, sizeof(*ip6e));
373 1.16.2.2 tls if (ip6e == NULL) {
374 1.16.2.2 tls return 0;
375 1.16.2.2 tls }
376 1.16.2.2 tls
377 1.13 rmind /*
378 1.13 rmind * Determine whether we are going to continue.
379 1.13 rmind */
380 1.16.2.2 tls switch (npc->npc_proto) {
381 1.13 rmind case IPPROTO_HOPOPTS:
382 1.7 zoltan case IPPROTO_DSTOPTS:
383 1.7 zoltan case IPPROTO_ROUTING:
384 1.16.2.2 tls hlen = (ip6e->ip6e_len + 1) << 3;
385 1.7 zoltan break;
386 1.7 zoltan case IPPROTO_FRAGMENT:
387 1.13 rmind hlen = sizeof(struct ip6_frag);
388 1.16.2.2 tls flags |= NPC_IPFRAG;
389 1.7 zoltan break;
390 1.7 zoltan case IPPROTO_AH:
391 1.16.2.2 tls hlen = (ip6e->ip6e_len + 2) << 2;
392 1.7 zoltan break;
393 1.7 zoltan default:
394 1.13 rmind hlen = 0;
395 1.13 rmind break;
396 1.13 rmind }
397 1.13 rmind
398 1.13 rmind if (!hlen) {
399 1.7 zoltan break;
400 1.7 zoltan }
401 1.16.2.2 tls npc->npc_proto = ip6e->ip6e_nxt;
402 1.13 rmind npc->npc_hlen += hlen;
403 1.13 rmind }
404 1.7 zoltan
405 1.16.2.4 tls /*
406 1.16.2.4 tls * Re-fetch the header pointers (nbufs might have been
407 1.16.2.4 tls * reallocated). Restore the original offset (if any).
408 1.16.2.4 tls */
409 1.16.2.2 tls nbuf_reset(nbuf);
410 1.16.2.4 tls ip6 = nbuf_dataptr(nbuf);
411 1.16.2.2 tls if (off) {
412 1.16.2.2 tls nbuf_advance(nbuf, off, 0);
413 1.16.2.2 tls }
414 1.16.2.2 tls
415 1.12 rmind /* Cache: layer 3 - IPv6. */
416 1.14 rmind npc->npc_alen = sizeof(struct in6_addr);
417 1.16.2.4 tls npc->npc_ips[NPF_SRC] = (npf_addr_t *)&ip6->ip6_src;
418 1.16.2.4 tls npc->npc_ips[NPF_DST]= (npf_addr_t *)&ip6->ip6_dst;
419 1.16.2.2 tls
420 1.16.2.2 tls npc->npc_ip.v6 = ip6;
421 1.16.2.2 tls flags |= NPC_IP6;
422 1.7 zoltan break;
423 1.12 rmind }
424 1.4 rmind default:
425 1.16.2.2 tls break;
426 1.4 rmind }
427 1.16.2.2 tls return flags;
428 1.1 rmind }
429 1.1 rmind
430 1.1 rmind /*
431 1.4 rmind * npf_cache_all: general routine to cache all relevant IP (v4 or v6)
432 1.12 rmind * and TCP, UDP or ICMP headers.
433 1.16.2.2 tls *
434 1.16.2.2 tls * => nbuf offset shall be set accordingly.
435 1.1 rmind */
436 1.10 rmind int
437 1.16.2.4 tls npf_cache_all(npf_cache_t *npc)
438 1.1 rmind {
439 1.16.2.4 tls nbuf_t *nbuf = npc->npc_nbuf;
440 1.16.2.2 tls int flags, l4flags;
441 1.16.2.2 tls u_int hlen;
442 1.1 rmind
443 1.16.2.2 tls /*
444 1.16.2.2 tls * This routine is a main point where the references are cached,
445 1.16.2.2 tls * therefore clear the flag as we reset.
446 1.16.2.2 tls */
447 1.16.2.2 tls again:
448 1.16.2.2 tls nbuf_unset_flag(nbuf, NBUF_DATAREF_RESET);
449 1.16.2.2 tls
450 1.16.2.2 tls /*
451 1.16.2.2 tls * First, cache the L3 header (IPv4 or IPv6). If IP packet is
452 1.16.2.2 tls * fragmented, then we cannot look into L4.
453 1.16.2.2 tls */
454 1.16.2.2 tls flags = npf_cache_ip(npc, nbuf);
455 1.16.2.2 tls if ((flags & NPC_IP46) == 0 || (flags & NPC_IPFRAG) != 0) {
456 1.16.2.4 tls nbuf_unset_flag(nbuf, NBUF_DATAREF_RESET);
457 1.16.2.2 tls npc->npc_info |= flags;
458 1.16.2.2 tls return flags;
459 1.1 rmind }
460 1.16.2.2 tls hlen = npc->npc_hlen;
461 1.16.2.2 tls
462 1.16.2.2 tls switch (npc->npc_proto) {
463 1.1 rmind case IPPROTO_TCP:
464 1.16.2.2 tls /* Cache: layer 4 - TCP. */
465 1.16.2.2 tls npc->npc_l4.tcp = nbuf_advance(nbuf, hlen,
466 1.16.2.2 tls sizeof(struct tcphdr));
467 1.16.2.2 tls l4flags = NPC_LAYER4 | NPC_TCP;
468 1.10 rmind break;
469 1.1 rmind case IPPROTO_UDP:
470 1.16.2.2 tls /* Cache: layer 4 - UDP. */
471 1.16.2.2 tls npc->npc_l4.udp = nbuf_advance(nbuf, hlen,
472 1.16.2.2 tls sizeof(struct udphdr));
473 1.16.2.2 tls l4flags = NPC_LAYER4 | NPC_UDP;
474 1.10 rmind break;
475 1.1 rmind case IPPROTO_ICMP:
476 1.16.2.2 tls /* Cache: layer 4 - ICMPv4. */
477 1.16.2.2 tls npc->npc_l4.icmp = nbuf_advance(nbuf, hlen,
478 1.16.2.2 tls offsetof(struct icmp, icmp_void));
479 1.16.2.2 tls l4flags = NPC_LAYER4 | NPC_ICMP;
480 1.16.2.2 tls break;
481 1.15 spz case IPPROTO_ICMPV6:
482 1.16.2.2 tls /* Cache: layer 4 - ICMPv6. */
483 1.16.2.2 tls npc->npc_l4.icmp6 = nbuf_advance(nbuf, hlen,
484 1.16.2.2 tls offsetof(struct icmp6_hdr, icmp6_data32));
485 1.16.2.2 tls l4flags = NPC_LAYER4 | NPC_ICMP;
486 1.10 rmind break;
487 1.16.2.2 tls default:
488 1.16.2.2 tls l4flags = 0;
489 1.16.2.2 tls break;
490 1.16.2.2 tls }
491 1.16.2.2 tls
492 1.16.2.2 tls if (nbuf_flag_p(nbuf, NBUF_DATAREF_RESET)) {
493 1.16.2.2 tls goto again;
494 1.1 rmind }
495 1.16.2.2 tls
496 1.16.2.2 tls /* Add the L4 flags if nbuf_advance() succeeded. */
497 1.16.2.2 tls if (l4flags && npc->npc_l4.hdr) {
498 1.16.2.2 tls flags |= l4flags;
499 1.16.2.2 tls }
500 1.16.2.2 tls npc->npc_info |= flags;
501 1.16.2.2 tls return flags;
502 1.16.2.2 tls }
503 1.16.2.2 tls
504 1.16.2.2 tls void
505 1.16.2.4 tls npf_recache(npf_cache_t *npc)
506 1.16.2.2 tls {
507 1.16.2.4 tls nbuf_t *nbuf = npc->npc_nbuf;
508 1.16.2.4 tls const int mflags __diagused = npc->npc_info & (NPC_IP46 | NPC_LAYER4);
509 1.16.2.4 tls int flags __diagused;
510 1.16.2.2 tls
511 1.16.2.2 tls nbuf_reset(nbuf);
512 1.16.2.2 tls npc->npc_info = 0;
513 1.16.2.4 tls flags = npf_cache_all(npc);
514 1.16.2.4 tls
515 1.16.2.2 tls KASSERT((flags & mflags) == mflags);
516 1.16.2.2 tls KASSERT(nbuf_flag_p(nbuf, NBUF_DATAREF_RESET) == 0);
517 1.1 rmind }
518 1.1 rmind
519 1.1 rmind /*
520 1.16.2.2 tls * npf_rwrip: rewrite required IP address.
521 1.4 rmind */
522 1.4 rmind bool
523 1.16.2.4 tls npf_rwrip(const npf_cache_t *npc, u_int which, const npf_addr_t *addr)
524 1.4 rmind {
525 1.4 rmind KASSERT(npf_iscached(npc, NPC_IP46));
526 1.16.2.4 tls KASSERT(which == NPF_SRC || which == NPF_DST);
527 1.4 rmind
528 1.16.2.4 tls memcpy(npc->npc_ips[which], addr, npc->npc_alen);
529 1.4 rmind return true;
530 1.4 rmind }
531 1.4 rmind
532 1.4 rmind /*
533 1.16.2.2 tls * npf_rwrport: rewrite required TCP/UDP port.
534 1.1 rmind */
535 1.1 rmind bool
536 1.16.2.4 tls npf_rwrport(const npf_cache_t *npc, u_int which, const in_port_t port)
537 1.1 rmind {
538 1.16.2.2 tls const int proto = npc->npc_proto;
539 1.4 rmind in_port_t *oport;
540 1.1 rmind
541 1.4 rmind KASSERT(npf_iscached(npc, NPC_TCP) || npf_iscached(npc, NPC_UDP));
542 1.1 rmind KASSERT(proto == IPPROTO_TCP || proto == IPPROTO_UDP);
543 1.16.2.4 tls KASSERT(which == NPF_SRC || which == NPF_DST);
544 1.1 rmind
545 1.16.2.2 tls /* Get the offset and store the port in it. */
546 1.4 rmind if (proto == IPPROTO_TCP) {
547 1.16.2.2 tls struct tcphdr *th = npc->npc_l4.tcp;
548 1.16.2.4 tls oport = (which == NPF_SRC) ? &th->th_sport : &th->th_dport;
549 1.1 rmind } else {
550 1.16.2.2 tls struct udphdr *uh = npc->npc_l4.udp;
551 1.16.2.4 tls oport = (which == NPF_SRC) ? &uh->uh_sport : &uh->uh_dport;
552 1.1 rmind }
553 1.16.2.2 tls memcpy(oport, &port, sizeof(in_port_t));
554 1.1 rmind return true;
555 1.1 rmind }
556 1.1 rmind
557 1.1 rmind /*
558 1.16.2.2 tls * npf_rwrcksum: rewrite IPv4 and/or TCP/UDP checksum.
559 1.1 rmind */
560 1.1 rmind bool
561 1.16.2.4 tls npf_rwrcksum(const npf_cache_t *npc, u_int which,
562 1.16.2.2 tls const npf_addr_t *addr, const in_port_t port)
563 1.1 rmind {
564 1.16.2.4 tls const npf_addr_t *oaddr = npc->npc_ips[which];
565 1.16.2.2 tls const int proto = npc->npc_proto;
566 1.16.2.2 tls const int alen = npc->npc_alen;
567 1.16.2.2 tls uint16_t *ocksum;
568 1.16.2.2 tls in_port_t oport;
569 1.4 rmind
570 1.16.2.2 tls KASSERT(npf_iscached(npc, NPC_LAYER4));
571 1.16.2.4 tls KASSERT(which == NPF_SRC || which == NPF_DST);
572 1.4 rmind
573 1.16.2.2 tls if (npf_iscached(npc, NPC_IP4)) {
574 1.16.2.2 tls struct ip *ip = npc->npc_ip.v4;
575 1.16.2.2 tls uint16_t ipsum = ip->ip_sum;
576 1.4 rmind
577 1.16.2.2 tls /* Recalculate IPv4 checksum and rewrite. */
578 1.16.2.2 tls ip->ip_sum = npf_addr_cksum(ipsum, alen, oaddr, addr);
579 1.4 rmind } else {
580 1.4 rmind /* No checksum for IPv6. */
581 1.4 rmind KASSERT(npf_iscached(npc, NPC_IP6));
582 1.4 rmind }
583 1.4 rmind
584 1.16.2.2 tls /* Nothing else to do for ICMP. */
585 1.16.2.4 tls if (proto == IPPROTO_ICMP || proto == IPPROTO_ICMPV6) {
586 1.4 rmind return true;
587 1.4 rmind }
588 1.7 zoltan KASSERT(npf_iscached(npc, NPC_TCP) || npf_iscached(npc, NPC_UDP));
589 1.4 rmind
590 1.16.2.2 tls /*
591 1.16.2.2 tls * Calculate TCP/UDP checksum:
592 1.16.2.2 tls * - Skip if UDP and the current checksum is zero.
593 1.16.2.2 tls * - Fixup the IP address change.
594 1.16.2.2 tls * - Fixup the port change, if required (non-zero).
595 1.16.2.2 tls */
596 1.4 rmind if (proto == IPPROTO_TCP) {
597 1.16.2.2 tls struct tcphdr *th = npc->npc_l4.tcp;
598 1.4 rmind
599 1.16.2.2 tls ocksum = &th->th_sum;
600 1.16.2.4 tls oport = (which == NPF_SRC) ? th->th_sport : th->th_dport;
601 1.4 rmind } else {
602 1.16.2.2 tls struct udphdr *uh = npc->npc_l4.udp;
603 1.4 rmind
604 1.4 rmind KASSERT(proto == IPPROTO_UDP);
605 1.16.2.2 tls ocksum = &uh->uh_sum;
606 1.16.2.2 tls if (*ocksum == 0) {
607 1.4 rmind /* No need to update. */
608 1.4 rmind return true;
609 1.4 rmind }
610 1.16.2.4 tls oport = (which == NPF_SRC) ? uh->uh_sport : uh->uh_dport;
611 1.4 rmind }
612 1.1 rmind
613 1.16.2.2 tls uint16_t cksum = npf_addr_cksum(*ocksum, alen, oaddr, addr);
614 1.16.2.2 tls if (port) {
615 1.16.2.2 tls cksum = npf_fixup16_cksum(cksum, oport, port);
616 1.4 rmind }
617 1.16.2.2 tls
618 1.16.2.2 tls /* Rewrite TCP/UDP checksum. */
619 1.16.2.2 tls memcpy(ocksum, &cksum, sizeof(uint16_t));
620 1.4 rmind return true;
621 1.4 rmind }
622 1.4 rmind
623 1.16.2.4 tls /*
624 1.16.2.4 tls * npf_napt_rwr: perform address and/or port translation.
625 1.16.2.4 tls */
626 1.16.2.4 tls int
627 1.16.2.4 tls npf_napt_rwr(const npf_cache_t *npc, u_int which,
628 1.16.2.4 tls const npf_addr_t *addr, const in_addr_t port)
629 1.16.2.4 tls {
630 1.16.2.4 tls const unsigned proto = npc->npc_proto;
631 1.16.2.4 tls
632 1.16.2.4 tls /*
633 1.16.2.4 tls * Rewrite IP and/or TCP/UDP checksums first, since we need the
634 1.16.2.4 tls * current (old) address/port for the calculations. Then perform
635 1.16.2.4 tls * the address translation i.e. rewrite source or destination.
636 1.16.2.4 tls */
637 1.16.2.4 tls if (!npf_rwrcksum(npc, which, addr, port)) {
638 1.16.2.4 tls return EINVAL;
639 1.16.2.4 tls }
640 1.16.2.4 tls if (!npf_rwrip(npc, which, addr)) {
641 1.16.2.4 tls return EINVAL;
642 1.16.2.4 tls }
643 1.16.2.4 tls if (port == 0) {
644 1.16.2.4 tls /* Done. */
645 1.16.2.4 tls return 0;
646 1.16.2.4 tls }
647 1.16.2.4 tls
648 1.16.2.4 tls switch (proto) {
649 1.16.2.4 tls case IPPROTO_TCP:
650 1.16.2.4 tls case IPPROTO_UDP:
651 1.16.2.4 tls /* Rewrite source/destination port. */
652 1.16.2.4 tls if (!npf_rwrport(npc, which, port)) {
653 1.16.2.4 tls return EINVAL;
654 1.16.2.4 tls }
655 1.16.2.4 tls break;
656 1.16.2.4 tls case IPPROTO_ICMP:
657 1.16.2.4 tls case IPPROTO_ICMPV6:
658 1.16.2.4 tls KASSERT(npf_iscached(npc, NPC_ICMP));
659 1.16.2.4 tls /* Nothing. */
660 1.16.2.4 tls break;
661 1.16.2.4 tls default:
662 1.16.2.4 tls return ENOTSUP;
663 1.16.2.4 tls }
664 1.16.2.4 tls return 0;
665 1.16.2.4 tls }
666 1.16.2.4 tls
667 1.16.2.4 tls /*
668 1.16.2.4 tls * IPv6-to-IPv6 Network Prefix Translation (NPTv6), as per RFC 6296.
669 1.16.2.4 tls */
670 1.16.2.4 tls
671 1.16.2.4 tls int
672 1.16.2.4 tls npf_npt66_rwr(const npf_cache_t *npc, u_int which, const npf_addr_t *pref,
673 1.16.2.4 tls npf_netmask_t len, uint16_t adj)
674 1.16.2.4 tls {
675 1.16.2.4 tls npf_addr_t *addr = npc->npc_ips[which];
676 1.16.2.4 tls unsigned remnant, word, preflen = len >> 4;
677 1.16.2.4 tls uint32_t sum;
678 1.16.2.4 tls
679 1.16.2.4 tls KASSERT(which == NPF_SRC || which == NPF_DST);
680 1.16.2.4 tls
681 1.16.2.4 tls if (!npf_iscached(npc, NPC_IP6)) {
682 1.16.2.4 tls return EINVAL;
683 1.16.2.4 tls }
684 1.16.2.4 tls if (len <= 48) {
685 1.16.2.4 tls /*
686 1.16.2.4 tls * The word to adjust. Cannot translate the 0xffff
687 1.16.2.4 tls * subnet if /48 or shorter.
688 1.16.2.4 tls */
689 1.16.2.4 tls word = 3;
690 1.16.2.4 tls if (addr->s6_addr16[word] == 0xffff) {
691 1.16.2.4 tls return EINVAL;
692 1.16.2.4 tls }
693 1.16.2.4 tls } else {
694 1.16.2.4 tls /*
695 1.16.2.4 tls * Also, all 0s or 1s in the host part are disallowed for
696 1.16.2.4 tls * longer than /48 prefixes.
697 1.16.2.4 tls */
698 1.16.2.4 tls if ((addr->s6_addr32[2] == 0 && addr->s6_addr32[3] == 0) ||
699 1.16.2.4 tls (addr->s6_addr32[2] == ~0U && addr->s6_addr32[3] == ~0U))
700 1.16.2.4 tls return EINVAL;
701 1.16.2.4 tls
702 1.16.2.4 tls /* Determine the 16-bit word to adjust. */
703 1.16.2.4 tls for (word = 4; word < 8; word++)
704 1.16.2.4 tls if (addr->s6_addr16[word] != 0xffff)
705 1.16.2.4 tls break;
706 1.16.2.4 tls }
707 1.16.2.4 tls
708 1.16.2.4 tls /* Rewrite the prefix. */
709 1.16.2.4 tls for (unsigned i = 0; i < preflen; i++) {
710 1.16.2.4 tls addr->s6_addr16[i] = pref->s6_addr16[i];
711 1.16.2.4 tls }
712 1.16.2.4 tls
713 1.16.2.4 tls /*
714 1.16.2.4 tls * If prefix length is within a 16-bit word (not dividable by 16),
715 1.16.2.4 tls * then prepare a mask, determine the word and adjust it.
716 1.16.2.4 tls */
717 1.16.2.4 tls if ((remnant = len - (preflen << 4)) != 0) {
718 1.16.2.4 tls const uint16_t wordmask = (1U << remnant) - 1;
719 1.16.2.4 tls const unsigned i = preflen;
720 1.16.2.4 tls
721 1.16.2.4 tls addr->s6_addr16[i] = (pref->s6_addr16[i] & wordmask) |
722 1.16.2.4 tls (addr->s6_addr16[i] & ~wordmask);
723 1.16.2.4 tls }
724 1.16.2.4 tls
725 1.16.2.4 tls /*
726 1.16.2.4 tls * Performing 1's complement sum/difference.
727 1.16.2.4 tls */
728 1.16.2.4 tls sum = addr->s6_addr16[word] + adj;
729 1.16.2.4 tls while (sum >> 16) {
730 1.16.2.4 tls sum = (sum >> 16) + (sum & 0xffff);
731 1.16.2.4 tls }
732 1.16.2.4 tls if (sum == 0xffff) {
733 1.16.2.4 tls /* RFC 1071. */
734 1.16.2.4 tls sum = 0x0000;
735 1.16.2.4 tls }
736 1.16.2.4 tls addr->s6_addr16[word] = sum;
737 1.16.2.4 tls return 0;
738 1.16.2.4 tls }
739 1.16.2.4 tls
740 1.13 rmind #if defined(DDB) || defined(_NPF_TESTING)
741 1.13 rmind
742 1.16.2.4 tls const char *
743 1.16.2.4 tls npf_addr_dump(const npf_addr_t *addr, int alen)
744 1.13 rmind {
745 1.16.2.4 tls if (alen == sizeof(struct in_addr)) {
746 1.16.2.4 tls struct in_addr ip;
747 1.16.2.4 tls memcpy(&ip, addr, alen);
748 1.16.2.4 tls return inet_ntoa(ip);
749 1.16.2.4 tls }
750 1.16.2.4 tls return "[IPv6]"; // XXX
751 1.13 rmind }
752 1.13 rmind
753 1.13 rmind #endif
754