npf_inet.c revision 1.26 1 1.26 rmind /* $NetBSD: npf_inet.c,v 1.26 2013/11/22 01:24:21 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.12 rmind * Copyright (c) 2009-2012 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.22 rmind * 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.26 rmind __KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.26 2013/11/22 01:24:21 rmind 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.1 rmind * npf_fixup{16,32}_cksum: update IPv4 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.1 rmind */
75 1.1 rmind sum = ~ntohs(cksum) & 0xffff;
76 1.1 rmind sum += (~ntohs(odatum) & 0xffff) + ntohs(ndatum);
77 1.1 rmind sum = (sum >> 16) + (sum & 0xffff);
78 1.1 rmind sum += (sum >> 16);
79 1.1 rmind
80 1.1 rmind return htons(~sum & 0xffff);
81 1.1 rmind }
82 1.1 rmind
83 1.1 rmind uint16_t
84 1.1 rmind npf_fixup32_cksum(uint16_t cksum, uint32_t odatum, uint32_t ndatum)
85 1.1 rmind {
86 1.1 rmind
87 1.1 rmind cksum = npf_fixup16_cksum(cksum, odatum & 0xffff, ndatum & 0xffff);
88 1.1 rmind cksum = npf_fixup16_cksum(cksum, odatum >> 16, ndatum >> 16);
89 1.1 rmind return cksum;
90 1.1 rmind }
91 1.1 rmind
92 1.1 rmind /*
93 1.4 rmind * npf_addr_cksum: calculate checksum of the address, either IPv4 or IPv6.
94 1.4 rmind */
95 1.4 rmind uint16_t
96 1.19 rmind npf_addr_cksum(uint16_t cksum, int sz, const npf_addr_t *oaddr,
97 1.19 rmind const npf_addr_t *naddr)
98 1.4 rmind {
99 1.19 rmind const uint32_t *oip32 = (const uint32_t *)oaddr;
100 1.19 rmind const uint32_t *nip32 = (const uint32_t *)naddr;
101 1.4 rmind
102 1.4 rmind KASSERT(sz % sizeof(uint32_t) == 0);
103 1.4 rmind do {
104 1.4 rmind cksum = npf_fixup32_cksum(cksum, *oip32++, *nip32++);
105 1.4 rmind sz -= sizeof(uint32_t);
106 1.4 rmind } while (sz);
107 1.4 rmind
108 1.4 rmind return cksum;
109 1.4 rmind }
110 1.4 rmind
111 1.4 rmind /*
112 1.26 rmind * npf_addr_sum: provide IP addresses as a XORed 32-bit integer.
113 1.4 rmind * Note: used for hash function.
114 1.1 rmind */
115 1.4 rmind uint32_t
116 1.26 rmind npf_addr_mix(const int sz, const npf_addr_t *a1, const npf_addr_t *a2)
117 1.1 rmind {
118 1.4 rmind uint32_t mix = 0;
119 1.1 rmind
120 1.5 rmind KASSERT(sz > 0 && a1 != NULL && a2 != NULL);
121 1.5 rmind
122 1.26 rmind for (int i = 0; i < (sz >> 2); i++) {
123 1.26 rmind mix ^= a1->s6_addr32[i];
124 1.26 rmind mix ^= a2->s6_addr32[i];
125 1.4 rmind }
126 1.4 rmind return mix;
127 1.4 rmind }
128 1.1 rmind
129 1.13 rmind /*
130 1.13 rmind * npf_addr_mask: apply the mask to a given address and store the result.
131 1.13 rmind */
132 1.13 rmind void
133 1.13 rmind npf_addr_mask(const npf_addr_t *addr, const npf_netmask_t mask,
134 1.13 rmind const int alen, npf_addr_t *out)
135 1.12 rmind {
136 1.13 rmind const int nwords = alen >> 2;
137 1.12 rmind uint_fast8_t length = mask;
138 1.12 rmind
139 1.12 rmind /* Note: maximum length is 32 for IPv4 and 128 for IPv6. */
140 1.12 rmind KASSERT(length <= NPF_MAX_NETMASK);
141 1.12 rmind
142 1.13 rmind for (int i = 0; i < nwords; i++) {
143 1.13 rmind uint32_t wordmask;
144 1.13 rmind
145 1.12 rmind if (length >= 32) {
146 1.13 rmind wordmask = htonl(0xffffffff);
147 1.12 rmind length -= 32;
148 1.13 rmind } else if (length) {
149 1.13 rmind wordmask = htonl(0xffffffff << (32 - length));
150 1.13 rmind length = 0;
151 1.12 rmind } else {
152 1.13 rmind wordmask = 0;
153 1.12 rmind }
154 1.13 rmind out->s6_addr32[i] = addr->s6_addr32[i] & wordmask;
155 1.12 rmind }
156 1.12 rmind }
157 1.12 rmind
158 1.12 rmind /*
159 1.12 rmind * npf_addr_cmp: compare two addresses, either IPv4 or IPv6.
160 1.12 rmind *
161 1.13 rmind * => Return 0 if equal and negative/positive if less/greater accordingly.
162 1.12 rmind * => Ignore the mask, if NPF_NO_NETMASK is specified.
163 1.12 rmind */
164 1.12 rmind int
165 1.12 rmind npf_addr_cmp(const npf_addr_t *addr1, const npf_netmask_t mask1,
166 1.13 rmind const npf_addr_t *addr2, const npf_netmask_t mask2, const int alen)
167 1.12 rmind {
168 1.13 rmind npf_addr_t realaddr1, realaddr2;
169 1.12 rmind
170 1.12 rmind if (mask1 != NPF_NO_NETMASK) {
171 1.13 rmind npf_addr_mask(addr1, mask1, alen, &realaddr1);
172 1.13 rmind addr1 = &realaddr1;
173 1.12 rmind }
174 1.12 rmind if (mask2 != NPF_NO_NETMASK) {
175 1.13 rmind npf_addr_mask(addr2, mask2, alen, &realaddr2);
176 1.13 rmind addr2 = &realaddr2;
177 1.12 rmind }
178 1.13 rmind return memcmp(addr1, addr2, alen);
179 1.12 rmind }
180 1.12 rmind
181 1.4 rmind /*
182 1.4 rmind * npf_tcpsaw: helper to fetch SEQ, ACK, WIN and return TCP data length.
183 1.12 rmind *
184 1.12 rmind * => Returns all values in host byte-order.
185 1.4 rmind */
186 1.4 rmind int
187 1.12 rmind npf_tcpsaw(const npf_cache_t *npc, tcp_seq *seq, tcp_seq *ack, uint32_t *win)
188 1.4 rmind {
189 1.19 rmind const struct tcphdr *th = npc->npc_l4.tcp;
190 1.8 rmind u_int thlen;
191 1.1 rmind
192 1.7 zoltan KASSERT(npf_iscached(npc, NPC_TCP));
193 1.1 rmind
194 1.4 rmind *seq = ntohl(th->th_seq);
195 1.4 rmind *ack = ntohl(th->th_ack);
196 1.4 rmind *win = (uint32_t)ntohs(th->th_win);
197 1.8 rmind thlen = th->th_off << 2;
198 1.1 rmind
199 1.7 zoltan if (npf_iscached(npc, NPC_IP4)) {
200 1.19 rmind const struct ip *ip = npc->npc_ip.v4;
201 1.21 rmind return ntohs(ip->ip_len) - npc->npc_hlen - thlen;
202 1.12 rmind } else if (npf_iscached(npc, NPC_IP6)) {
203 1.19 rmind const struct ip6_hdr *ip6 = npc->npc_ip.v6;
204 1.8 rmind return ntohs(ip6->ip6_plen) - thlen;
205 1.7 zoltan }
206 1.7 zoltan return 0;
207 1.1 rmind }
208 1.1 rmind
209 1.1 rmind /*
210 1.4 rmind * npf_fetch_tcpopts: parse and return TCP options.
211 1.1 rmind */
212 1.1 rmind bool
213 1.19 rmind npf_fetch_tcpopts(npf_cache_t *npc, nbuf_t *nbuf, uint16_t *mss, int *wscale)
214 1.1 rmind {
215 1.19 rmind const struct tcphdr *th = npc->npc_l4.tcp;
216 1.4 rmind int topts_len, step;
217 1.19 rmind void *nptr;
218 1.4 rmind uint8_t val;
219 1.19 rmind bool ok;
220 1.4 rmind
221 1.7 zoltan KASSERT(npf_iscached(npc, NPC_IP46));
222 1.7 zoltan KASSERT(npf_iscached(npc, NPC_TCP));
223 1.10 rmind
224 1.4 rmind /* Determine if there are any TCP options, get their length. */
225 1.4 rmind topts_len = (th->th_off << 2) - sizeof(struct tcphdr);
226 1.4 rmind if (topts_len <= 0) {
227 1.4 rmind /* No options. */
228 1.1 rmind return false;
229 1.4 rmind }
230 1.4 rmind KASSERT(topts_len <= MAX_TCPOPTLEN);
231 1.1 rmind
232 1.4 rmind /* First step: IP and TCP header up to options. */
233 1.21 rmind step = npc->npc_hlen + sizeof(struct tcphdr);
234 1.19 rmind nbuf_reset(nbuf);
235 1.4 rmind next:
236 1.19 rmind if ((nptr = nbuf_advance(nbuf, step, 1)) == NULL) {
237 1.19 rmind ok = false;
238 1.19 rmind goto done;
239 1.4 rmind }
240 1.19 rmind val = *(uint8_t *)nptr;
241 1.12 rmind
242 1.4 rmind switch (val) {
243 1.4 rmind case TCPOPT_EOL:
244 1.4 rmind /* Done. */
245 1.19 rmind ok = true;
246 1.19 rmind goto done;
247 1.4 rmind case TCPOPT_NOP:
248 1.4 rmind topts_len--;
249 1.4 rmind step = 1;
250 1.4 rmind break;
251 1.4 rmind case TCPOPT_MAXSEG:
252 1.19 rmind if ((nptr = nbuf_advance(nbuf, 2, 2)) == NULL) {
253 1.19 rmind ok = false;
254 1.19 rmind goto done;
255 1.4 rmind }
256 1.4 rmind if (mss) {
257 1.19 rmind if (*mss) {
258 1.19 rmind memcpy(nptr, mss, sizeof(uint16_t));
259 1.19 rmind } else {
260 1.19 rmind memcpy(mss, nptr, sizeof(uint16_t));
261 1.19 rmind }
262 1.4 rmind }
263 1.4 rmind topts_len -= TCPOLEN_MAXSEG;
264 1.19 rmind step = 2;
265 1.4 rmind break;
266 1.4 rmind case TCPOPT_WINDOW:
267 1.10 rmind /* TCP Window Scaling (RFC 1323). */
268 1.19 rmind if ((nptr = nbuf_advance(nbuf, 2, 1)) == NULL) {
269 1.19 rmind ok = false;
270 1.19 rmind goto done;
271 1.4 rmind }
272 1.19 rmind val = *(uint8_t *)nptr;
273 1.4 rmind *wscale = (val > TCP_MAX_WINSHIFT) ? TCP_MAX_WINSHIFT : val;
274 1.4 rmind topts_len -= TCPOLEN_WINDOW;
275 1.19 rmind step = 1;
276 1.4 rmind break;
277 1.4 rmind default:
278 1.19 rmind if ((nptr = nbuf_advance(nbuf, 1, 1)) == NULL) {
279 1.19 rmind ok = false;
280 1.19 rmind goto done;
281 1.4 rmind }
282 1.19 rmind val = *(uint8_t *)nptr;
283 1.16 rmind if (val < 2 || val > topts_len) {
284 1.19 rmind ok = false;
285 1.19 rmind goto done;
286 1.4 rmind }
287 1.4 rmind topts_len -= val;
288 1.4 rmind step = val - 1;
289 1.4 rmind }
290 1.12 rmind
291 1.6 rmind /* Any options left? */
292 1.4 rmind if (__predict_true(topts_len > 0)) {
293 1.4 rmind goto next;
294 1.4 rmind }
295 1.19 rmind ok = true;
296 1.19 rmind done:
297 1.19 rmind if (nbuf_flag_p(nbuf, NBUF_DATAREF_RESET)) {
298 1.19 rmind npf_recache(npc, nbuf);
299 1.19 rmind }
300 1.19 rmind return ok;
301 1.1 rmind }
302 1.1 rmind
303 1.19 rmind static int
304 1.19 rmind npf_cache_ip(npf_cache_t *npc, nbuf_t *nbuf)
305 1.1 rmind {
306 1.19 rmind const void *nptr = nbuf_dataptr(nbuf);
307 1.19 rmind const uint8_t ver = *(const uint8_t *)nptr;
308 1.19 rmind int flags = 0;
309 1.12 rmind
310 1.4 rmind switch (ver >> 4) {
311 1.12 rmind case IPVERSION: {
312 1.19 rmind struct ip *ip;
313 1.12 rmind
314 1.19 rmind ip = nbuf_ensure_contig(nbuf, sizeof(struct ip));
315 1.19 rmind if (ip == NULL) {
316 1.19 rmind return 0;
317 1.4 rmind }
318 1.12 rmind
319 1.4 rmind /* Check header length and fragment offset. */
320 1.10 rmind if ((u_int)(ip->ip_hl << 2) < sizeof(struct ip)) {
321 1.19 rmind return 0;
322 1.4 rmind }
323 1.4 rmind if (ip->ip_off & ~htons(IP_DF | IP_RF)) {
324 1.4 rmind /* Note fragmentation. */
325 1.19 rmind flags |= NPC_IPFRAG;
326 1.4 rmind }
327 1.12 rmind
328 1.4 rmind /* Cache: layer 3 - IPv4. */
329 1.14 rmind npc->npc_alen = sizeof(struct in_addr);
330 1.4 rmind npc->npc_srcip = (npf_addr_t *)&ip->ip_src;
331 1.4 rmind npc->npc_dstip = (npf_addr_t *)&ip->ip_dst;
332 1.7 zoltan npc->npc_hlen = ip->ip_hl << 2;
333 1.19 rmind npc->npc_proto = ip->ip_p;
334 1.19 rmind
335 1.19 rmind npc->npc_ip.v4 = ip;
336 1.19 rmind flags |= NPC_IP4;
337 1.4 rmind break;
338 1.12 rmind }
339 1.4 rmind
340 1.12 rmind case (IPV6_VERSION >> 4): {
341 1.19 rmind struct ip6_hdr *ip6;
342 1.19 rmind struct ip6_ext *ip6e;
343 1.19 rmind size_t off, hlen;
344 1.19 rmind
345 1.19 rmind ip6 = nbuf_ensure_contig(nbuf, sizeof(struct ip6_hdr));
346 1.19 rmind if (ip6 == NULL) {
347 1.19 rmind return 0;
348 1.7 zoltan }
349 1.19 rmind
350 1.19 rmind /* Set initial next-protocol value. */
351 1.19 rmind hlen = sizeof(struct ip6_hdr);
352 1.19 rmind npc->npc_proto = ip6->ip6_nxt;
353 1.13 rmind npc->npc_hlen = hlen;
354 1.7 zoltan
355 1.12 rmind /*
356 1.19 rmind * Advance by the length of the current header.
357 1.12 rmind */
358 1.19 rmind off = nbuf_offset(nbuf);
359 1.19 rmind while (nbuf_advance(nbuf, hlen, 0) != NULL) {
360 1.19 rmind ip6e = nbuf_ensure_contig(nbuf, sizeof(*ip6e));
361 1.19 rmind if (ip6e == NULL) {
362 1.19 rmind return 0;
363 1.19 rmind }
364 1.19 rmind
365 1.13 rmind /*
366 1.13 rmind * Determine whether we are going to continue.
367 1.13 rmind */
368 1.19 rmind switch (npc->npc_proto) {
369 1.13 rmind case IPPROTO_HOPOPTS:
370 1.7 zoltan case IPPROTO_DSTOPTS:
371 1.7 zoltan case IPPROTO_ROUTING:
372 1.19 rmind hlen = (ip6e->ip6e_len + 1) << 3;
373 1.7 zoltan break;
374 1.7 zoltan case IPPROTO_FRAGMENT:
375 1.13 rmind hlen = sizeof(struct ip6_frag);
376 1.19 rmind flags |= NPC_IPFRAG;
377 1.7 zoltan break;
378 1.7 zoltan case IPPROTO_AH:
379 1.19 rmind hlen = (ip6e->ip6e_len + 2) << 2;
380 1.7 zoltan break;
381 1.7 zoltan default:
382 1.13 rmind hlen = 0;
383 1.13 rmind break;
384 1.13 rmind }
385 1.13 rmind
386 1.13 rmind if (!hlen) {
387 1.7 zoltan break;
388 1.7 zoltan }
389 1.19 rmind npc->npc_proto = ip6e->ip6e_nxt;
390 1.13 rmind npc->npc_hlen += hlen;
391 1.13 rmind }
392 1.7 zoltan
393 1.23 rmind /*
394 1.23 rmind * Re-fetch the header pointers (nbufs might have been
395 1.23 rmind * reallocated). Restore the original offset (if any).
396 1.23 rmind */
397 1.19 rmind nbuf_reset(nbuf);
398 1.23 rmind ip6 = nbuf_dataptr(nbuf);
399 1.19 rmind if (off) {
400 1.19 rmind nbuf_advance(nbuf, off, 0);
401 1.19 rmind }
402 1.19 rmind
403 1.12 rmind /* Cache: layer 3 - IPv6. */
404 1.14 rmind npc->npc_alen = sizeof(struct in6_addr);
405 1.7 zoltan npc->npc_srcip = (npf_addr_t *)&ip6->ip6_src;
406 1.7 zoltan npc->npc_dstip = (npf_addr_t *)&ip6->ip6_dst;
407 1.19 rmind
408 1.19 rmind npc->npc_ip.v6 = ip6;
409 1.19 rmind flags |= NPC_IP6;
410 1.7 zoltan break;
411 1.12 rmind }
412 1.4 rmind default:
413 1.19 rmind break;
414 1.4 rmind }
415 1.19 rmind return flags;
416 1.1 rmind }
417 1.1 rmind
418 1.1 rmind /*
419 1.4 rmind * npf_cache_all: general routine to cache all relevant IP (v4 or v6)
420 1.12 rmind * and TCP, UDP or ICMP headers.
421 1.19 rmind *
422 1.19 rmind * => nbuf offset shall be set accordingly.
423 1.1 rmind */
424 1.10 rmind int
425 1.2 rmind npf_cache_all(npf_cache_t *npc, nbuf_t *nbuf)
426 1.1 rmind {
427 1.19 rmind int flags, l4flags;
428 1.19 rmind u_int hlen;
429 1.19 rmind
430 1.19 rmind /*
431 1.19 rmind * This routine is a main point where the references are cached,
432 1.19 rmind * therefore clear the flag as we reset.
433 1.19 rmind */
434 1.19 rmind again:
435 1.19 rmind nbuf_unset_flag(nbuf, NBUF_DATAREF_RESET);
436 1.1 rmind
437 1.19 rmind /*
438 1.19 rmind * First, cache the L3 header (IPv4 or IPv6). If IP packet is
439 1.19 rmind * fragmented, then we cannot look into L4.
440 1.19 rmind */
441 1.19 rmind flags = npf_cache_ip(npc, nbuf);
442 1.19 rmind if ((flags & NPC_IP46) == 0 || (flags & NPC_IPFRAG) != 0) {
443 1.23 rmind nbuf_unset_flag(nbuf, NBUF_DATAREF_RESET);
444 1.19 rmind npc->npc_info |= flags;
445 1.19 rmind return flags;
446 1.1 rmind }
447 1.19 rmind hlen = npc->npc_hlen;
448 1.19 rmind
449 1.19 rmind switch (npc->npc_proto) {
450 1.1 rmind case IPPROTO_TCP:
451 1.19 rmind /* Cache: layer 4 - TCP. */
452 1.19 rmind npc->npc_l4.tcp = nbuf_advance(nbuf, hlen,
453 1.19 rmind sizeof(struct tcphdr));
454 1.19 rmind l4flags = NPC_LAYER4 | NPC_TCP;
455 1.10 rmind break;
456 1.1 rmind case IPPROTO_UDP:
457 1.19 rmind /* Cache: layer 4 - UDP. */
458 1.19 rmind npc->npc_l4.udp = nbuf_advance(nbuf, hlen,
459 1.19 rmind sizeof(struct udphdr));
460 1.19 rmind l4flags = NPC_LAYER4 | NPC_UDP;
461 1.10 rmind break;
462 1.1 rmind case IPPROTO_ICMP:
463 1.19 rmind /* Cache: layer 4 - ICMPv4. */
464 1.19 rmind npc->npc_l4.icmp = nbuf_advance(nbuf, hlen,
465 1.19 rmind offsetof(struct icmp, icmp_void));
466 1.19 rmind l4flags = NPC_LAYER4 | NPC_ICMP;
467 1.19 rmind break;
468 1.15 spz case IPPROTO_ICMPV6:
469 1.19 rmind /* Cache: layer 4 - ICMPv6. */
470 1.19 rmind npc->npc_l4.icmp6 = nbuf_advance(nbuf, hlen,
471 1.19 rmind offsetof(struct icmp6_hdr, icmp6_data32));
472 1.19 rmind l4flags = NPC_LAYER4 | NPC_ICMP;
473 1.19 rmind break;
474 1.19 rmind default:
475 1.19 rmind l4flags = 0;
476 1.10 rmind break;
477 1.1 rmind }
478 1.19 rmind
479 1.19 rmind if (nbuf_flag_p(nbuf, NBUF_DATAREF_RESET)) {
480 1.19 rmind goto again;
481 1.19 rmind }
482 1.19 rmind
483 1.19 rmind /* Add the L4 flags if nbuf_advance() succeeded. */
484 1.19 rmind if (l4flags && npc->npc_l4.hdr) {
485 1.19 rmind flags |= l4flags;
486 1.19 rmind }
487 1.19 rmind npc->npc_info |= flags;
488 1.19 rmind return flags;
489 1.19 rmind }
490 1.19 rmind
491 1.19 rmind void
492 1.19 rmind npf_recache(npf_cache_t *npc, nbuf_t *nbuf)
493 1.19 rmind {
494 1.24 martin const int mflags __diagused = npc->npc_info & (NPC_IP46 | NPC_LAYER4);
495 1.25 mrg int flags __diagused;
496 1.19 rmind
497 1.19 rmind nbuf_reset(nbuf);
498 1.19 rmind npc->npc_info = 0;
499 1.19 rmind flags = npf_cache_all(npc, nbuf);
500 1.19 rmind KASSERT((flags & mflags) == mflags);
501 1.19 rmind KASSERT(nbuf_flag_p(nbuf, NBUF_DATAREF_RESET) == 0);
502 1.1 rmind }
503 1.1 rmind
504 1.1 rmind /*
505 1.19 rmind * npf_rwrip: rewrite required IP address.
506 1.4 rmind */
507 1.4 rmind bool
508 1.19 rmind npf_rwrip(const npf_cache_t *npc, int di, const npf_addr_t *addr)
509 1.4 rmind {
510 1.4 rmind npf_addr_t *oaddr;
511 1.4 rmind
512 1.4 rmind KASSERT(npf_iscached(npc, NPC_IP46));
513 1.4 rmind
514 1.19 rmind /*
515 1.19 rmind * Rewrite source address if outgoing and destination if incoming.
516 1.19 rmind */
517 1.19 rmind oaddr = (di == PFIL_OUT) ? npc->npc_srcip : npc->npc_dstip;
518 1.14 rmind memcpy(oaddr, addr, npc->npc_alen);
519 1.4 rmind return true;
520 1.4 rmind }
521 1.4 rmind
522 1.4 rmind /*
523 1.19 rmind * npf_rwrport: rewrite required TCP/UDP port.
524 1.1 rmind */
525 1.1 rmind bool
526 1.19 rmind npf_rwrport(const npf_cache_t *npc, int di, const in_port_t port)
527 1.1 rmind {
528 1.21 rmind const int proto = npc->npc_proto;
529 1.4 rmind in_port_t *oport;
530 1.1 rmind
531 1.4 rmind KASSERT(npf_iscached(npc, NPC_TCP) || npf_iscached(npc, NPC_UDP));
532 1.1 rmind KASSERT(proto == IPPROTO_TCP || proto == IPPROTO_UDP);
533 1.1 rmind
534 1.19 rmind /* Get the offset and store the port in it. */
535 1.4 rmind if (proto == IPPROTO_TCP) {
536 1.19 rmind struct tcphdr *th = npc->npc_l4.tcp;
537 1.19 rmind oport = (di == PFIL_OUT) ? &th->th_sport : &th->th_dport;
538 1.1 rmind } else {
539 1.19 rmind struct udphdr *uh = npc->npc_l4.udp;
540 1.19 rmind oport = (di == PFIL_OUT) ? &uh->uh_sport : &uh->uh_dport;
541 1.1 rmind }
542 1.19 rmind memcpy(oport, &port, sizeof(in_port_t));
543 1.1 rmind return true;
544 1.1 rmind }
545 1.1 rmind
546 1.1 rmind /*
547 1.19 rmind * npf_rwrcksum: rewrite IPv4 and/or TCP/UDP checksum.
548 1.1 rmind */
549 1.1 rmind bool
550 1.19 rmind npf_rwrcksum(const npf_cache_t *npc, const int di,
551 1.19 rmind const npf_addr_t *addr, const in_port_t port)
552 1.1 rmind {
553 1.21 rmind const int proto = npc->npc_proto;
554 1.19 rmind const int alen = npc->npc_alen;
555 1.4 rmind npf_addr_t *oaddr;
556 1.18 rmind uint16_t *ocksum;
557 1.18 rmind in_port_t oport;
558 1.18 rmind
559 1.19 rmind KASSERT(npf_iscached(npc, NPC_LAYER4));
560 1.18 rmind oaddr = (di == PFIL_OUT) ? npc->npc_srcip : npc->npc_dstip;
561 1.18 rmind
562 1.4 rmind if (npf_iscached(npc, NPC_IP4)) {
563 1.19 rmind struct ip *ip = npc->npc_ip.v4;
564 1.19 rmind uint16_t ipsum = ip->ip_sum;
565 1.4 rmind
566 1.19 rmind /* Recalculate IPv4 checksum and rewrite. */
567 1.19 rmind ip->ip_sum = npf_addr_cksum(ipsum, alen, oaddr, addr);
568 1.4 rmind } else {
569 1.4 rmind /* No checksum for IPv6. */
570 1.4 rmind KASSERT(npf_iscached(npc, NPC_IP6));
571 1.4 rmind }
572 1.4 rmind
573 1.18 rmind /* Nothing else to do for ICMP. */
574 1.18 rmind if (proto == IPPROTO_ICMP) {
575 1.4 rmind return true;
576 1.4 rmind }
577 1.7 zoltan KASSERT(npf_iscached(npc, NPC_TCP) || npf_iscached(npc, NPC_UDP));
578 1.4 rmind
579 1.18 rmind /*
580 1.18 rmind * Calculate TCP/UDP checksum:
581 1.18 rmind * - Skip if UDP and the current checksum is zero.
582 1.18 rmind * - Fixup the IP address change.
583 1.18 rmind * - Fixup the port change, if required (non-zero).
584 1.18 rmind */
585 1.4 rmind if (proto == IPPROTO_TCP) {
586 1.19 rmind struct tcphdr *th = npc->npc_l4.tcp;
587 1.4 rmind
588 1.18 rmind ocksum = &th->th_sum;
589 1.18 rmind oport = (di == PFIL_OUT) ? th->th_sport : th->th_dport;
590 1.4 rmind } else {
591 1.19 rmind struct udphdr *uh = npc->npc_l4.udp;
592 1.4 rmind
593 1.4 rmind KASSERT(proto == IPPROTO_UDP);
594 1.18 rmind ocksum = &uh->uh_sum;
595 1.18 rmind if (*ocksum == 0) {
596 1.4 rmind /* No need to update. */
597 1.4 rmind return true;
598 1.4 rmind }
599 1.18 rmind oport = (di == PFIL_OUT) ? uh->uh_sport : uh->uh_dport;
600 1.18 rmind }
601 1.18 rmind
602 1.19 rmind uint16_t cksum = npf_addr_cksum(*ocksum, alen, oaddr, addr);
603 1.18 rmind if (port) {
604 1.18 rmind cksum = npf_fixup16_cksum(cksum, oport, port);
605 1.4 rmind }
606 1.1 rmind
607 1.19 rmind /* Rewrite TCP/UDP checksum. */
608 1.19 rmind memcpy(ocksum, &cksum, sizeof(uint16_t));
609 1.4 rmind return true;
610 1.4 rmind }
611 1.4 rmind
612 1.13 rmind #if defined(DDB) || defined(_NPF_TESTING)
613 1.13 rmind
614 1.13 rmind void
615 1.13 rmind npf_addr_dump(const npf_addr_t *addr)
616 1.13 rmind {
617 1.13 rmind printf("IP[%x:%x:%x:%x]\n",
618 1.13 rmind addr->s6_addr32[0], addr->s6_addr32[1],
619 1.13 rmind addr->s6_addr32[2], addr->s6_addr32[3]);
620 1.13 rmind }
621 1.13 rmind
622 1.13 rmind #endif
623