npf_inet.c revision 1.7 1 1.7 zoltan /* $NetBSD: npf_inet.c,v 1.7 2011/11/04 01:00:27 zoltan Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.6 rmind * Copyright (c) 2009-2011 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.1 rmind * Various procotol related helper routines.
34 1.1 rmind */
35 1.1 rmind
36 1.1 rmind #include <sys/cdefs.h>
37 1.7 zoltan __KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.7 2011/11/04 01:00:27 zoltan Exp $");
38 1.1 rmind
39 1.1 rmind #include <sys/param.h>
40 1.1 rmind #include <sys/kernel.h>
41 1.1 rmind
42 1.4 rmind #include <net/pfil.h>
43 1.4 rmind #include <net/if.h>
44 1.4 rmind #include <net/ethertypes.h>
45 1.4 rmind #include <net/if_ether.h>
46 1.4 rmind
47 1.1 rmind #include <netinet/in_systm.h>
48 1.1 rmind #include <netinet/in.h>
49 1.4 rmind #include <netinet/in_var.h>
50 1.1 rmind #include <netinet/ip.h>
51 1.4 rmind #include <netinet/ip6.h>
52 1.1 rmind #include <netinet/tcp.h>
53 1.1 rmind #include <netinet/udp.h>
54 1.1 rmind #include <netinet/ip_icmp.h>
55 1.1 rmind
56 1.1 rmind #include "npf_impl.h"
57 1.1 rmind
58 1.1 rmind /*
59 1.1 rmind * npf_fixup{16,32}_cksum: update IPv4 checksum.
60 1.1 rmind */
61 1.1 rmind
62 1.1 rmind uint16_t
63 1.1 rmind npf_fixup16_cksum(uint16_t cksum, uint16_t odatum, uint16_t ndatum)
64 1.1 rmind {
65 1.1 rmind uint32_t sum;
66 1.1 rmind
67 1.1 rmind /*
68 1.1 rmind * RFC 1624:
69 1.1 rmind * HC' = ~(~HC + ~m + m')
70 1.1 rmind */
71 1.1 rmind sum = ~ntohs(cksum) & 0xffff;
72 1.1 rmind sum += (~ntohs(odatum) & 0xffff) + ntohs(ndatum);
73 1.1 rmind sum = (sum >> 16) + (sum & 0xffff);
74 1.1 rmind sum += (sum >> 16);
75 1.1 rmind
76 1.1 rmind return htons(~sum & 0xffff);
77 1.1 rmind }
78 1.1 rmind
79 1.1 rmind uint16_t
80 1.1 rmind npf_fixup32_cksum(uint16_t cksum, uint32_t odatum, uint32_t ndatum)
81 1.1 rmind {
82 1.1 rmind
83 1.1 rmind cksum = npf_fixup16_cksum(cksum, odatum & 0xffff, ndatum & 0xffff);
84 1.1 rmind cksum = npf_fixup16_cksum(cksum, odatum >> 16, ndatum >> 16);
85 1.1 rmind return cksum;
86 1.1 rmind }
87 1.1 rmind
88 1.1 rmind /*
89 1.4 rmind * npf_addr_cksum: calculate checksum of the address, either IPv4 or IPv6.
90 1.4 rmind */
91 1.4 rmind uint16_t
92 1.4 rmind npf_addr_cksum(uint16_t cksum, int sz, npf_addr_t *oaddr, npf_addr_t *naddr)
93 1.4 rmind {
94 1.4 rmind uint32_t *oip32 = (uint32_t *)oaddr, *nip32 = (uint32_t *)naddr;
95 1.4 rmind
96 1.4 rmind KASSERT(sz % sizeof(uint32_t) == 0);
97 1.4 rmind do {
98 1.4 rmind cksum = npf_fixup32_cksum(cksum, *oip32++, *nip32++);
99 1.4 rmind sz -= sizeof(uint32_t);
100 1.4 rmind } while (sz);
101 1.4 rmind
102 1.4 rmind return cksum;
103 1.4 rmind }
104 1.4 rmind
105 1.4 rmind /*
106 1.4 rmind * npf_addr_sum: provide IP address as a summed (if needed) 32-bit integer.
107 1.4 rmind * Note: used for hash function.
108 1.1 rmind */
109 1.4 rmind uint32_t
110 1.4 rmind npf_addr_sum(const int sz, const npf_addr_t *a1, const npf_addr_t *a2)
111 1.1 rmind {
112 1.4 rmind uint32_t mix = 0;
113 1.4 rmind int i;
114 1.1 rmind
115 1.5 rmind KASSERT(sz > 0 && a1 != NULL && a2 != NULL);
116 1.5 rmind
117 1.4 rmind for (i = 0; i < (sz >> 2); i++) {
118 1.4 rmind mix += a1->s6_addr32[i];
119 1.4 rmind mix += a2->s6_addr32[i];
120 1.4 rmind }
121 1.4 rmind return mix;
122 1.4 rmind }
123 1.1 rmind
124 1.4 rmind /*
125 1.4 rmind * npf_tcpsaw: helper to fetch SEQ, ACK, WIN and return TCP data length.
126 1.4 rmind * Returns all values in host byte-order.
127 1.4 rmind */
128 1.4 rmind int
129 1.7 zoltan npf_tcpsaw(npf_cache_t *npc, nbuf_t *nbuf, tcp_seq *seq, tcp_seq *ack, uint32_t *win)
130 1.4 rmind {
131 1.4 rmind struct tcphdr *th = &npc->npc_l4.tcp;
132 1.1 rmind
133 1.7 zoltan KASSERT(npf_iscached(npc, NPC_TCP));
134 1.1 rmind
135 1.4 rmind *seq = ntohl(th->th_seq);
136 1.4 rmind *ack = ntohl(th->th_ack);
137 1.4 rmind *win = (uint32_t)ntohs(th->th_win);
138 1.1 rmind
139 1.7 zoltan /*
140 1.7 zoltan * total length of packet - header length - tcp header length
141 1.7 zoltan */
142 1.7 zoltan if (npf_iscached(npc, NPC_IP4)) {
143 1.7 zoltan struct ip *ip = &npc->npc_ip.v4;
144 1.7 zoltan return ntohs(ip->ip_len) - npf_cache_hlen(npc, nbuf) - (th->th_off << 2);
145 1.7 zoltan } else {
146 1.7 zoltan KASSERT(npf_iscached(npc, NPC_IP6));
147 1.7 zoltan struct ip6_hdr *ip6 = &npc->npc_ip.v6;
148 1.7 zoltan return ntohs(ip6->ip6_plen) - (th->th_off << 2);
149 1.7 zoltan }
150 1.7 zoltan
151 1.7 zoltan return 0;
152 1.1 rmind }
153 1.1 rmind
154 1.1 rmind /*
155 1.4 rmind * npf_fetch_tcpopts: parse and return TCP options.
156 1.1 rmind */
157 1.1 rmind bool
158 1.4 rmind npf_fetch_tcpopts(const npf_cache_t *npc, nbuf_t *nbuf,
159 1.4 rmind uint16_t *mss, int *wscale)
160 1.1 rmind {
161 1.4 rmind void *n_ptr = nbuf_dataptr(nbuf);
162 1.4 rmind const struct tcphdr *th = &npc->npc_l4.tcp;
163 1.4 rmind int topts_len, step;
164 1.4 rmind uint16_t val16;
165 1.4 rmind uint8_t val;
166 1.4 rmind
167 1.7 zoltan KASSERT(npf_iscached(npc, NPC_IP46));
168 1.7 zoltan KASSERT(npf_iscached(npc, NPC_TCP));
169 1.4 rmind /* Determine if there are any TCP options, get their length. */
170 1.4 rmind topts_len = (th->th_off << 2) - sizeof(struct tcphdr);
171 1.4 rmind if (topts_len <= 0) {
172 1.4 rmind /* No options. */
173 1.1 rmind return false;
174 1.4 rmind }
175 1.4 rmind KASSERT(topts_len <= MAX_TCPOPTLEN);
176 1.1 rmind
177 1.4 rmind /* First step: IP and TCP header up to options. */
178 1.7 zoltan step = npf_cache_hlen(npc, nbuf) + sizeof(struct tcphdr);
179 1.4 rmind next:
180 1.4 rmind if (nbuf_advfetch(&nbuf, &n_ptr, step, sizeof(val), &val)) {
181 1.1 rmind return false;
182 1.4 rmind }
183 1.4 rmind switch (val) {
184 1.4 rmind case TCPOPT_EOL:
185 1.4 rmind /* Done. */
186 1.4 rmind return true;
187 1.4 rmind case TCPOPT_NOP:
188 1.4 rmind topts_len--;
189 1.4 rmind step = 1;
190 1.4 rmind break;
191 1.4 rmind case TCPOPT_MAXSEG:
192 1.4 rmind /*
193 1.4 rmind * XXX: clean this mess.
194 1.4 rmind */
195 1.4 rmind if (mss && *mss) {
196 1.4 rmind val16 = *mss;
197 1.4 rmind if (nbuf_advstore(&nbuf, &n_ptr, 2,
198 1.4 rmind sizeof(val16), &val16))
199 1.4 rmind return false;
200 1.4 rmind } else if (nbuf_advfetch(&nbuf, &n_ptr, 2,
201 1.4 rmind sizeof(val16), &val16)) {
202 1.4 rmind return false;
203 1.4 rmind }
204 1.4 rmind if (mss) {
205 1.4 rmind *mss = val16;
206 1.4 rmind }
207 1.4 rmind topts_len -= TCPOLEN_MAXSEG;
208 1.4 rmind step = sizeof(val16);
209 1.4 rmind break;
210 1.4 rmind case TCPOPT_WINDOW:
211 1.4 rmind if (nbuf_advfetch(&nbuf, &n_ptr, 2, sizeof(val), &val)) {
212 1.4 rmind return false;
213 1.4 rmind }
214 1.4 rmind *wscale = (val > TCP_MAX_WINSHIFT) ? TCP_MAX_WINSHIFT : val;
215 1.4 rmind topts_len -= TCPOLEN_WINDOW;
216 1.4 rmind step = sizeof(val);
217 1.4 rmind break;
218 1.4 rmind default:
219 1.4 rmind if (nbuf_advfetch(&nbuf, &n_ptr, 1, sizeof(val), &val)) {
220 1.4 rmind return false;
221 1.4 rmind }
222 1.4 rmind if (val < 2 || val >= topts_len) {
223 1.4 rmind return false;
224 1.4 rmind }
225 1.4 rmind topts_len -= val;
226 1.4 rmind step = val - 1;
227 1.4 rmind }
228 1.6 rmind /* Any options left? */
229 1.4 rmind if (__predict_true(topts_len > 0)) {
230 1.4 rmind goto next;
231 1.4 rmind }
232 1.6 rmind return true;
233 1.1 rmind }
234 1.1 rmind
235 1.1 rmind /*
236 1.4 rmind * npf_fetch_ip: fetch, check and cache IP header.
237 1.1 rmind */
238 1.1 rmind bool
239 1.4 rmind npf_fetch_ip(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
240 1.1 rmind {
241 1.4 rmind struct ip *ip;
242 1.7 zoltan struct ip6_hdr *ip6;
243 1.4 rmind uint8_t ver;
244 1.1 rmind
245 1.4 rmind if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint8_t), &ver)) {
246 1.1 rmind return false;
247 1.4 rmind }
248 1.4 rmind switch (ver >> 4) {
249 1.4 rmind case IPVERSION:
250 1.4 rmind /* IPv4 */
251 1.4 rmind ip = &npc->npc_ip.v4;
252 1.4 rmind /* Fetch the header. */
253 1.4 rmind if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(struct ip), ip)) {
254 1.4 rmind return false;
255 1.4 rmind }
256 1.4 rmind /* Check header length and fragment offset. */
257 1.4 rmind if ((ip->ip_hl << 2) < sizeof(struct ip)) {
258 1.4 rmind return false;
259 1.4 rmind }
260 1.4 rmind if (ip->ip_off & ~htons(IP_DF | IP_RF)) {
261 1.4 rmind /* Note fragmentation. */
262 1.4 rmind npc->npc_info |= NPC_IPFRAG;
263 1.4 rmind }
264 1.4 rmind /* Cache: layer 3 - IPv4. */
265 1.4 rmind npc->npc_ipsz = sizeof(struct in_addr);
266 1.4 rmind npc->npc_srcip = (npf_addr_t *)&ip->ip_src;
267 1.4 rmind npc->npc_dstip = (npf_addr_t *)&ip->ip_dst;
268 1.4 rmind npc->npc_info |= NPC_IP4;
269 1.7 zoltan npc->npc_hlen = ip->ip_hl << 2;
270 1.7 zoltan npc->npc_next_proto = npc->npc_ip.v4.ip_p;
271 1.4 rmind break;
272 1.4 rmind
273 1.4 rmind case (IPV6_VERSION >> 4):
274 1.7 zoltan ip6 = &npc->npc_ip.v6;
275 1.7 zoltan if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(struct ip6_hdr), ip6)) {
276 1.7 zoltan return false;
277 1.7 zoltan }
278 1.7 zoltan
279 1.7 zoltan struct ip6_ext ip6e;
280 1.7 zoltan size_t toskip = sizeof(struct ip6_hdr);
281 1.7 zoltan bool processing_ends = false;
282 1.7 zoltan npc->npc_next_proto = ip6->ip6_nxt;
283 1.7 zoltan npc->npc_hlen = 0;
284 1.7 zoltan
285 1.7 zoltan do {
286 1.7 zoltan /* advance the length of the previous known header,
287 1.7 zoltan and fetch the next extension header's length */
288 1.7 zoltan if (nbuf_advfetch(&nbuf, &n_ptr, toskip, sizeof(struct ip6_ext), &ip6e)) {
289 1.7 zoltan return false;
290 1.7 zoltan }
291 1.7 zoltan
292 1.7 zoltan switch (npc->npc_next_proto) {
293 1.7 zoltan case IPPROTO_DSTOPTS:
294 1.7 zoltan case IPPROTO_ROUTING:
295 1.7 zoltan toskip = (ip6e.ip6e_len + 1) << 3;
296 1.7 zoltan break;
297 1.7 zoltan case IPPROTO_FRAGMENT:
298 1.7 zoltan npc->npc_info |= NPC_IPFRAG;
299 1.7 zoltan toskip = sizeof(struct ip6_frag);
300 1.7 zoltan break;
301 1.7 zoltan case IPPROTO_AH:
302 1.7 zoltan toskip = (ip6e.ip6e_len + 2) << 2;
303 1.7 zoltan break;
304 1.7 zoltan default:
305 1.7 zoltan processing_ends = true;
306 1.7 zoltan break;
307 1.7 zoltan }
308 1.7 zoltan
309 1.7 zoltan npc->npc_hlen += toskip;
310 1.7 zoltan
311 1.7 zoltan if (!processing_ends) {
312 1.7 zoltan npc->npc_next_proto = ip6e.ip6e_nxt;
313 1.7 zoltan }
314 1.7 zoltan } while (!processing_ends);
315 1.7 zoltan
316 1.7 zoltan npc->npc_ipsz = sizeof(struct in6_addr);
317 1.7 zoltan npc->npc_srcip = (npf_addr_t *)&ip6->ip6_src;
318 1.7 zoltan npc->npc_dstip = (npf_addr_t *)&ip6->ip6_dst;
319 1.7 zoltan npc->npc_info |= NPC_IP6;
320 1.7 zoltan break;
321 1.7 zoltan
322 1.4 rmind default:
323 1.1 rmind return false;
324 1.4 rmind }
325 1.4 rmind return true;
326 1.4 rmind }
327 1.1 rmind
328 1.4 rmind bool
329 1.4 rmind npf_fetch_tcp(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
330 1.4 rmind {
331 1.4 rmind struct tcphdr *th;
332 1.1 rmind
333 1.4 rmind /* Must have IP header processed for its length and protocol. */
334 1.4 rmind if (!npf_iscached(npc, NPC_IP46) && !npf_fetch_ip(npc, nbuf, n_ptr)) {
335 1.1 rmind return false;
336 1.4 rmind }
337 1.7 zoltan if (npf_cache_ipproto(npc) != IPPROTO_TCP) {
338 1.1 rmind return false;
339 1.4 rmind }
340 1.4 rmind th = &npc->npc_l4.tcp;
341 1.4 rmind
342 1.4 rmind /* Fetch TCP header. */
343 1.7 zoltan if (nbuf_advfetch(&nbuf, &n_ptr, npf_cache_hlen(npc, nbuf), sizeof(struct tcphdr), th)) {
344 1.1 rmind return false;
345 1.4 rmind }
346 1.1 rmind
347 1.4 rmind /* Cache: layer 4 - TCP. */
348 1.4 rmind npc->npc_info |= (NPC_LAYER4 | NPC_TCP);
349 1.1 rmind return true;
350 1.1 rmind }
351 1.1 rmind
352 1.1 rmind bool
353 1.4 rmind npf_fetch_udp(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
354 1.1 rmind {
355 1.4 rmind struct ip *ip = &npc->npc_ip.v4;
356 1.4 rmind struct udphdr *uh;
357 1.7 zoltan size_t hlen;
358 1.1 rmind
359 1.4 rmind /* Must have IP header processed for its length and protocol. */
360 1.4 rmind if (!npf_iscached(npc, NPC_IP46) && !npf_fetch_ip(npc, nbuf, n_ptr)) {
361 1.4 rmind return false;
362 1.4 rmind }
363 1.4 rmind if (ip->ip_p != IPPROTO_UDP) {
364 1.1 rmind return false;
365 1.4 rmind }
366 1.4 rmind uh = &npc->npc_l4.udp;
367 1.7 zoltan hlen = npf_cache_hlen(npc, nbuf);
368 1.1 rmind
369 1.4 rmind /* Fetch ICMP header. */
370 1.4 rmind if (nbuf_advfetch(&nbuf, &n_ptr, hlen, sizeof(struct udphdr), uh)) {
371 1.1 rmind return false;
372 1.4 rmind }
373 1.1 rmind
374 1.4 rmind /* Cache: layer 4 - ICMP. */
375 1.4 rmind npc->npc_info |= (NPC_LAYER4 | NPC_UDP);
376 1.1 rmind return true;
377 1.1 rmind }
378 1.1 rmind
379 1.2 rmind /*
380 1.4 rmind * npf_fetch_icmp: fetch ICMP code, type and possible query ID.
381 1.4 rmind *
382 1.4 rmind * => Stores both all fetched items into the cache.
383 1.2 rmind */
384 1.2 rmind bool
385 1.4 rmind npf_fetch_icmp(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
386 1.1 rmind {
387 1.4 rmind struct ip *ip = &npc->npc_ip.v4;
388 1.4 rmind struct icmp *ic;
389 1.7 zoltan u_int iclen;
390 1.7 zoltan size_t hlen;
391 1.1 rmind
392 1.4 rmind /* Must have IP header processed for its length and protocol. */
393 1.4 rmind if (!npf_iscached(npc, NPC_IP46) && !npf_fetch_ip(npc, nbuf, n_ptr)) {
394 1.4 rmind return false;
395 1.4 rmind }
396 1.4 rmind if (ip->ip_p != IPPROTO_ICMP) {
397 1.1 rmind return false;
398 1.3 rmind }
399 1.4 rmind ic = &npc->npc_l4.icmp;
400 1.7 zoltan hlen = npf_cache_hlen(npc, nbuf);
401 1.4 rmind
402 1.4 rmind /* Fetch basic ICMP header, up to the "data" point. */
403 1.6 rmind iclen = offsetof(struct icmp, icmp_data);
404 1.6 rmind if (nbuf_advfetch(&nbuf, &n_ptr, hlen, iclen, ic)) {
405 1.4 rmind return false;
406 1.4 rmind }
407 1.4 rmind
408 1.4 rmind /* Cache: layer 4 - ICMP. */
409 1.4 rmind npc->npc_info |= (NPC_LAYER4 | NPC_ICMP);
410 1.1 rmind return true;
411 1.1 rmind }
412 1.1 rmind
413 1.1 rmind /*
414 1.4 rmind * npf_cache_all: general routine to cache all relevant IP (v4 or v6)
415 1.4 rmind * and TCP, UDP or ICMP data.
416 1.1 rmind */
417 1.1 rmind bool
418 1.2 rmind npf_cache_all(npf_cache_t *npc, nbuf_t *nbuf)
419 1.1 rmind {
420 1.1 rmind void *n_ptr = nbuf_dataptr(nbuf);
421 1.1 rmind
422 1.4 rmind if (!npf_iscached(npc, NPC_IP46) && !npf_fetch_ip(npc, nbuf, n_ptr)) {
423 1.1 rmind return false;
424 1.1 rmind }
425 1.4 rmind if (npf_iscached(npc, NPC_IPFRAG)) {
426 1.4 rmind return true;
427 1.1 rmind }
428 1.4 rmind switch (npf_cache_ipproto(npc)) {
429 1.1 rmind case IPPROTO_TCP:
430 1.4 rmind return npf_fetch_tcp(npc, nbuf, n_ptr);
431 1.1 rmind case IPPROTO_UDP:
432 1.4 rmind return npf_fetch_udp(npc, nbuf, n_ptr);
433 1.1 rmind case IPPROTO_ICMP:
434 1.1 rmind return npf_fetch_icmp(npc, nbuf, n_ptr);
435 1.1 rmind }
436 1.1 rmind return false;
437 1.1 rmind }
438 1.1 rmind
439 1.1 rmind /*
440 1.4 rmind * npf_rwrip: rewrite required IP address, update the cache.
441 1.4 rmind */
442 1.4 rmind bool
443 1.4 rmind npf_rwrip(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr, const int di,
444 1.4 rmind npf_addr_t *addr)
445 1.4 rmind {
446 1.4 rmind npf_addr_t *oaddr;
447 1.4 rmind u_int offby;
448 1.4 rmind
449 1.4 rmind KASSERT(npf_iscached(npc, NPC_IP46));
450 1.4 rmind
451 1.4 rmind if (di == PFIL_OUT) {
452 1.4 rmind /* Rewrite source address, if outgoing. */
453 1.4 rmind offby = offsetof(struct ip, ip_src);
454 1.4 rmind oaddr = npc->npc_srcip;
455 1.4 rmind } else {
456 1.4 rmind /* Rewrite destination, if incoming. */
457 1.4 rmind offby = offsetof(struct ip, ip_dst);
458 1.4 rmind oaddr = npc->npc_dstip;
459 1.4 rmind }
460 1.4 rmind
461 1.4 rmind /* Advance to the address and rewrite it. */
462 1.4 rmind if (nbuf_advstore(&nbuf, &n_ptr, offby, npc->npc_ipsz, addr))
463 1.4 rmind return false;
464 1.4 rmind
465 1.4 rmind /* Cache: IP address. */
466 1.4 rmind memcpy(oaddr, addr, npc->npc_ipsz);
467 1.4 rmind return true;
468 1.4 rmind }
469 1.4 rmind
470 1.4 rmind /*
471 1.4 rmind * npf_rwrport: rewrite required TCP/UDP port, update the cache.
472 1.1 rmind */
473 1.1 rmind bool
474 1.1 rmind npf_rwrport(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr, const int di,
475 1.4 rmind in_port_t port)
476 1.1 rmind {
477 1.4 rmind const int proto = npf_cache_ipproto(npc);
478 1.7 zoltan u_int offby = npf_cache_hlen(npc, nbuf);
479 1.4 rmind in_port_t *oport;
480 1.1 rmind
481 1.4 rmind KASSERT(npf_iscached(npc, NPC_TCP) || npf_iscached(npc, NPC_UDP));
482 1.1 rmind KASSERT(proto == IPPROTO_TCP || proto == IPPROTO_UDP);
483 1.1 rmind
484 1.4 rmind /* Offset to the port and pointer in the cache. */
485 1.4 rmind if (proto == IPPROTO_TCP) {
486 1.4 rmind struct tcphdr *th = &npc->npc_l4.tcp;
487 1.4 rmind if (di == PFIL_OUT) {
488 1.4 rmind CTASSERT(offsetof(struct tcphdr, th_sport) == 0);
489 1.4 rmind oport = &th->th_sport;
490 1.1 rmind } else {
491 1.4 rmind offby += offsetof(struct tcphdr, th_dport);
492 1.4 rmind oport = &th->th_dport;
493 1.1 rmind }
494 1.1 rmind } else {
495 1.4 rmind struct udphdr *uh = &npc->npc_l4.udp;
496 1.4 rmind if (di == PFIL_OUT) {
497 1.4 rmind CTASSERT(offsetof(struct udphdr, uh_sport) == 0);
498 1.4 rmind oport = &uh->uh_sport;
499 1.1 rmind } else {
500 1.4 rmind offby += offsetof(struct udphdr, uh_dport);
501 1.4 rmind oport = &uh->uh_dport;
502 1.1 rmind }
503 1.1 rmind }
504 1.1 rmind
505 1.4 rmind /* Advance and rewrite the port. */
506 1.4 rmind if (nbuf_advstore(&nbuf, &n_ptr, offby, sizeof(in_port_t), &port))
507 1.1 rmind return false;
508 1.1 rmind
509 1.4 rmind /* Cache: TCP/UDP port. */
510 1.4 rmind *oport = port;
511 1.1 rmind return true;
512 1.1 rmind }
513 1.1 rmind
514 1.1 rmind /*
515 1.6 rmind * npf_rwrcksum: rewrite IPv4 and/or TCP/UDP checksum, update the cache.
516 1.1 rmind */
517 1.1 rmind bool
518 1.4 rmind npf_rwrcksum(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr, const int di,
519 1.4 rmind npf_addr_t *addr, in_port_t port)
520 1.1 rmind {
521 1.4 rmind const int proto = npf_cache_ipproto(npc);
522 1.4 rmind npf_addr_t *oaddr;
523 1.4 rmind in_port_t *oport;
524 1.4 rmind uint16_t *cksum;
525 1.1 rmind u_int offby;
526 1.1 rmind
527 1.4 rmind /* Checksum update for IPv4 header. */
528 1.4 rmind if (npf_iscached(npc, NPC_IP4)) {
529 1.4 rmind struct ip *ip = &npc->npc_ip.v4;
530 1.4 rmind uint16_t ipsum;
531 1.4 rmind
532 1.4 rmind oaddr = (di == PFIL_OUT) ? npc->npc_srcip : npc->npc_dstip;
533 1.4 rmind ipsum = npf_addr_cksum(ip->ip_sum, npc->npc_ipsz, oaddr, addr);
534 1.4 rmind
535 1.4 rmind /* Advance to the IPv4 checksum and rewrite it. */
536 1.4 rmind offby = offsetof(struct ip, ip_sum);
537 1.4 rmind if (nbuf_advstore(&nbuf, &n_ptr, offby, sizeof(ipsum), &ipsum))
538 1.4 rmind return false;
539 1.4 rmind
540 1.4 rmind ip->ip_sum = ipsum;
541 1.7 zoltan offby = npf_cache_hlen(npc, nbuf) - offby;
542 1.4 rmind } else {
543 1.4 rmind /* No checksum for IPv6. */
544 1.4 rmind KASSERT(npf_iscached(npc, NPC_IP6));
545 1.4 rmind oaddr = NULL;
546 1.4 rmind offby = 0;
547 1.6 rmind return false; /* XXX: Not yet supported. */
548 1.4 rmind }
549 1.4 rmind
550 1.4 rmind /* Determine whether TCP/UDP checksum update is needed. */
551 1.6 rmind if (proto == IPPROTO_ICMP || port == 0) {
552 1.4 rmind return true;
553 1.4 rmind }
554 1.7 zoltan KASSERT(npf_iscached(npc, NPC_TCP) || npf_iscached(npc, NPC_UDP));
555 1.4 rmind
556 1.4 rmind /* Calculate TCP/UDP checksum. */
557 1.4 rmind if (proto == IPPROTO_TCP) {
558 1.4 rmind struct tcphdr *th = &npc->npc_l4.tcp;
559 1.4 rmind
560 1.4 rmind cksum = &th->th_sum;
561 1.4 rmind offby += offsetof(struct tcphdr, th_sum);
562 1.4 rmind oport = (di == PFIL_OUT) ? &th->th_sport : &th->th_dport;
563 1.4 rmind } else {
564 1.4 rmind struct udphdr *uh = &npc->npc_l4.udp;
565 1.4 rmind
566 1.4 rmind KASSERT(proto == IPPROTO_UDP);
567 1.4 rmind cksum = &uh->uh_sum;
568 1.4 rmind if (*cksum == 0) {
569 1.4 rmind /* No need to update. */
570 1.4 rmind return true;
571 1.4 rmind }
572 1.4 rmind offby += offsetof(struct udphdr, uh_sum);
573 1.4 rmind oport = (di == PFIL_OUT) ? &uh->uh_sport : &uh->uh_dport;
574 1.4 rmind }
575 1.4 rmind *cksum = npf_addr_cksum(*cksum, npc->npc_ipsz, oaddr, addr);
576 1.4 rmind *cksum = npf_fixup16_cksum(*cksum, *oport, port);
577 1.1 rmind
578 1.4 rmind /* Advance to TCP/UDP checksum and rewrite it. */
579 1.4 rmind if (nbuf_advstore(&nbuf, &n_ptr, offby, sizeof(uint16_t), cksum)) {
580 1.1 rmind return false;
581 1.4 rmind }
582 1.4 rmind return true;
583 1.4 rmind }
584 1.4 rmind
585 1.4 rmind static inline bool
586 1.5 rmind npf_normalize_ip4(npf_cache_t *npc, nbuf_t *nbuf,
587 1.5 rmind bool rnd, bool no_df, int minttl)
588 1.4 rmind {
589 1.4 rmind void *n_ptr = nbuf_dataptr(nbuf);
590 1.4 rmind struct ip *ip = &npc->npc_ip.v4;
591 1.4 rmind uint16_t cksum = ip->ip_sum;
592 1.5 rmind uint16_t ip_off = ip->ip_off;
593 1.4 rmind uint8_t ttl = ip->ip_ttl;
594 1.4 rmind u_int offby = 0;
595 1.4 rmind
596 1.5 rmind KASSERT(rnd || minttl || no_df);
597 1.4 rmind
598 1.4 rmind /* Randomize IPv4 ID. */
599 1.4 rmind if (rnd) {
600 1.4 rmind uint16_t oid = ip->ip_id, nid;
601 1.4 rmind
602 1.4 rmind nid = htons(ip_randomid(ip_ids, 0));
603 1.4 rmind offby = offsetof(struct ip, ip_id);
604 1.4 rmind if (nbuf_advstore(&nbuf, &n_ptr, offby, sizeof(nid), &nid)) {
605 1.4 rmind return false;
606 1.4 rmind }
607 1.4 rmind cksum = npf_fixup16_cksum(cksum, oid, nid);
608 1.4 rmind ip->ip_id = nid;
609 1.4 rmind }
610 1.1 rmind
611 1.5 rmind /* IP_DF flag cleansing. */
612 1.5 rmind if (no_df && (ip_off & htons(IP_DF)) != 0) {
613 1.5 rmind uint16_t nip_off = ip_off & ~htons(IP_DF);
614 1.5 rmind
615 1.5 rmind if (nbuf_advstore(&nbuf, &n_ptr,
616 1.5 rmind offsetof(struct ip, ip_off) - offby,
617 1.6 rmind sizeof(uint16_t), &nip_off)) {
618 1.5 rmind return false;
619 1.5 rmind }
620 1.5 rmind cksum = npf_fixup16_cksum(cksum, ip_off, nip_off);
621 1.5 rmind ip->ip_off = nip_off;
622 1.5 rmind offby = offsetof(struct ip, ip_off);
623 1.5 rmind }
624 1.5 rmind
625 1.4 rmind /* Enforce minimum TTL. */
626 1.4 rmind if (minttl && ttl < minttl) {
627 1.4 rmind if (nbuf_advstore(&nbuf, &n_ptr,
628 1.4 rmind offsetof(struct ip, ip_ttl) - offby,
629 1.4 rmind sizeof(uint8_t), &minttl)) {
630 1.4 rmind return false;
631 1.4 rmind }
632 1.4 rmind cksum = npf_fixup16_cksum(cksum, ttl, minttl);
633 1.4 rmind ip->ip_ttl = minttl;
634 1.4 rmind offby = offsetof(struct ip, ip_ttl);
635 1.1 rmind }
636 1.1 rmind
637 1.4 rmind /* Update IP checksum. */
638 1.4 rmind offby = offsetof(struct ip, ip_sum) - offby;
639 1.4 rmind if (nbuf_advstore(&nbuf, &n_ptr, offby, sizeof(cksum), &cksum)) {
640 1.1 rmind return false;
641 1.4 rmind }
642 1.4 rmind ip->ip_sum = cksum;
643 1.4 rmind return true;
644 1.4 rmind }
645 1.4 rmind
646 1.4 rmind bool
647 1.4 rmind npf_normalize(npf_cache_t *npc, nbuf_t *nbuf,
648 1.5 rmind bool no_df, bool rnd, u_int minttl, u_int maxmss)
649 1.4 rmind {
650 1.4 rmind void *n_ptr = nbuf_dataptr(nbuf);
651 1.4 rmind struct tcphdr *th = &npc->npc_l4.tcp;
652 1.4 rmind uint16_t cksum, mss;
653 1.4 rmind int offby, wscale;
654 1.4 rmind
655 1.4 rmind /* Normalize IPv4. */
656 1.4 rmind if (npf_iscached(npc, NPC_IP4) && (rnd || minttl)) {
657 1.5 rmind if (!npf_normalize_ip4(npc, nbuf, rnd, no_df, minttl)) {
658 1.4 rmind return false;
659 1.4 rmind }
660 1.6 rmind } else if (!npf_iscached(npc, NPC_IP4)) {
661 1.6 rmind /* XXX: no IPv6 */
662 1.6 rmind return false;
663 1.4 rmind }
664 1.1 rmind
665 1.4 rmind /*
666 1.4 rmind * TCP Maximum Segment Size (MSS) "clamping". Only if SYN packet.
667 1.6 rmind * Fetch MSS and check whether rewrite to lower is needed.
668 1.4 rmind */
669 1.4 rmind if (maxmss == 0 || !npf_iscached(npc, NPC_TCP) ||
670 1.4 rmind (th->th_flags & TH_SYN) == 0) {
671 1.4 rmind /* Not required; done. */
672 1.4 rmind return true;
673 1.4 rmind }
674 1.4 rmind mss = 0;
675 1.4 rmind if (!npf_fetch_tcpopts(npc, nbuf, &mss, &wscale)) {
676 1.4 rmind return false;
677 1.4 rmind }
678 1.4 rmind if (ntohs(mss) <= maxmss) {
679 1.4 rmind return true;
680 1.4 rmind }
681 1.4 rmind
682 1.6 rmind /* Calculate TCP checksum, then rewrite MSS and the checksum. */
683 1.4 rmind maxmss = htons(maxmss);
684 1.4 rmind cksum = npf_fixup16_cksum(th->th_sum, mss, maxmss);
685 1.4 rmind th->th_sum = cksum;
686 1.4 rmind mss = maxmss;
687 1.4 rmind if (!npf_fetch_tcpopts(npc, nbuf, &mss, &wscale)) {
688 1.1 rmind return false;
689 1.4 rmind }
690 1.7 zoltan offby = npf_cache_hlen(npc, nbuf) + offsetof(struct tcphdr, th_sum);
691 1.4 rmind if (nbuf_advstore(&nbuf, &n_ptr, offby, sizeof(cksum), &cksum)) {
692 1.4 rmind return false;
693 1.4 rmind }
694 1.1 rmind return true;
695 1.1 rmind }
696