npf_inet.c revision 1.2 1 1.2 rmind /* $NetBSD: npf_inet.c,v 1.2 2010/09/16 04:53:27 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.1 rmind * Copyright (c) 2009-2010 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 #ifdef _KERNEL
37 1.1 rmind #include <sys/cdefs.h>
38 1.2 rmind __KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.2 2010/09/16 04:53:27 rmind Exp $");
39 1.1 rmind
40 1.1 rmind #include <sys/param.h>
41 1.1 rmind #include <sys/kernel.h>
42 1.1 rmind
43 1.1 rmind #include <netinet/in_systm.h>
44 1.1 rmind #include <netinet/in.h>
45 1.1 rmind #include <netinet/ip.h>
46 1.1 rmind #include <netinet/tcp.h>
47 1.1 rmind #include <netinet/udp.h>
48 1.1 rmind #include <netinet/ip_icmp.h>
49 1.1 rmind
50 1.1 rmind #include <net/if.h>
51 1.1 rmind #include <net/ethertypes.h>
52 1.1 rmind #include <net/if_ether.h>
53 1.1 rmind #endif
54 1.1 rmind #include <net/pfil.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.1 rmind * npf_ip4_proto: check IPv4 header length and match protocol number.
90 1.1 rmind *
91 1.1 rmind * => Returns pointer to protocol header or NULL on failure.
92 1.1 rmind * => Stores protocol number in the cache.
93 1.1 rmind * => Updates nbuf pointer to header's nbuf.
94 1.1 rmind */
95 1.1 rmind bool
96 1.1 rmind npf_ip4_proto(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
97 1.1 rmind {
98 1.1 rmind u_int hlen, offby;
99 1.1 rmind uint8_t val8;
100 1.1 rmind int error;
101 1.1 rmind
102 1.1 rmind /* IPv4 header: check IP version and header length. */
103 1.1 rmind error = nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint8_t), &val8);
104 1.1 rmind if (error || (val8 >> 4) != IPVERSION)
105 1.1 rmind return false;
106 1.1 rmind hlen = (val8 & 0xf) << 2;
107 1.1 rmind if (hlen < sizeof(struct ip))
108 1.1 rmind return false;
109 1.1 rmind offby = offsetof(struct ip, ip_off);
110 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
111 1.1 rmind return false;
112 1.1 rmind
113 1.1 rmind /* IPv4 header: check fragment offset. */
114 1.1 rmind error = nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint8_t), &val8);
115 1.1 rmind if (error || (val8 & ~htons(IP_DF | IP_RF)))
116 1.1 rmind return false;
117 1.1 rmind
118 1.1 rmind /* Get and match protocol. */
119 1.1 rmind KASSERT(offsetof(struct ip, ip_p) > offby);
120 1.1 rmind offby = offsetof(struct ip, ip_p) - offby;
121 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
122 1.1 rmind return false;
123 1.1 rmind if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint8_t), &val8))
124 1.1 rmind return false;
125 1.1 rmind
126 1.1 rmind /* IP checksum. */
127 1.1 rmind offby = offsetof(struct ip, ip_sum) - offsetof(struct ip, ip_p);
128 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
129 1.1 rmind return false;
130 1.1 rmind if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint16_t), &npc->npc_ipsum))
131 1.1 rmind return false;
132 1.1 rmind
133 1.1 rmind /* Cache: IPv4, protocol, header length. */
134 1.1 rmind npc->npc_info |= NPC_IP46;
135 1.1 rmind npc->npc_proto = val8;
136 1.1 rmind npc->npc_hlen = hlen;
137 1.1 rmind return true;
138 1.1 rmind }
139 1.1 rmind
140 1.1 rmind /*
141 1.1 rmind * npf_fetch_ip4addrs: fetch source and destination address from IPv4 header.
142 1.1 rmind *
143 1.1 rmind * => Stores both source and destination addresses into the cache.
144 1.1 rmind */
145 1.1 rmind bool
146 1.1 rmind npf_fetch_ip4addrs(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
147 1.1 rmind {
148 1.1 rmind u_int offby;
149 1.1 rmind
150 1.1 rmind /* Source address. */
151 1.1 rmind offby = offsetof(struct ip, ip_src);
152 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
153 1.1 rmind return false;
154 1.1 rmind if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(in_addr_t), &npc->npc_srcip))
155 1.1 rmind return false;
156 1.1 rmind
157 1.1 rmind /* Destination address. */
158 1.1 rmind offby = offsetof(struct ip, ip_dst) - offby;
159 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
160 1.1 rmind return false;
161 1.1 rmind if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(in_addr_t), &npc->npc_dstip))
162 1.1 rmind return false;
163 1.1 rmind
164 1.1 rmind /* Both addresses are cached. */
165 1.1 rmind npc->npc_info |= NPC_ADDRS;
166 1.1 rmind return true;
167 1.1 rmind }
168 1.1 rmind
169 1.1 rmind /*
170 1.1 rmind * npf_fetch_ports: fetch ports from either TCP or UDP header.
171 1.1 rmind *
172 1.1 rmind * => Stores both source and destination ports into the cache.
173 1.1 rmind */
174 1.1 rmind bool
175 1.1 rmind npf_fetch_ports(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr, const int proto)
176 1.1 rmind {
177 1.1 rmind u_int dst_off;
178 1.1 rmind
179 1.1 rmind /* Perform checks, advance to TCP/UDP header. */
180 1.1 rmind if (!npf_iscached(npc, NPC_IP46) && !npf_ip4_proto(npc, nbuf, n_ptr))
181 1.1 rmind return false;
182 1.1 rmind n_ptr = nbuf_advance(&nbuf, n_ptr, npc->npc_hlen);
183 1.1 rmind if (n_ptr == NULL || npc->npc_proto != proto)
184 1.1 rmind return false;
185 1.1 rmind
186 1.1 rmind /*
187 1.1 rmind * TCP/UDP header: fetch source and destination ports. For both
188 1.1 rmind * protocols offset of the source port offset is 0.
189 1.1 rmind */
190 1.1 rmind CTASSERT(offsetof(struct tcphdr, th_sport) == 0);
191 1.1 rmind CTASSERT(offsetof(struct udphdr, uh_sport) == 0);
192 1.1 rmind if (proto == IPPROTO_TCP) {
193 1.1 rmind dst_off = offsetof(struct tcphdr, th_dport);
194 1.1 rmind } else {
195 1.1 rmind KASSERT(proto == IPPROTO_UDP);
196 1.1 rmind dst_off = offsetof(struct udphdr, uh_dport);
197 1.1 rmind }
198 1.1 rmind
199 1.1 rmind if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(in_port_t), &npc->npc_sport))
200 1.1 rmind return false;
201 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, dst_off)) == NULL)
202 1.1 rmind return false;
203 1.1 rmind if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(in_port_t), &npc->npc_dport))
204 1.1 rmind return false;
205 1.1 rmind
206 1.1 rmind /* Both ports are cached. */
207 1.1 rmind npc->npc_info |= NPC_PORTS;
208 1.1 rmind return true;
209 1.1 rmind }
210 1.1 rmind
211 1.1 rmind /*
212 1.1 rmind * npf_fetch_icmp: fetch ICMP code, type and possible query ID.
213 1.1 rmind *
214 1.1 rmind * => Stores both all fetched items into the cache.
215 1.1 rmind */
216 1.1 rmind bool
217 1.1 rmind npf_fetch_icmp(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
218 1.1 rmind {
219 1.1 rmind u_int offby;
220 1.1 rmind uint8_t type;
221 1.1 rmind
222 1.1 rmind KASSERT(npf_iscached(npc, NPC_IP46));
223 1.1 rmind
224 1.1 rmind /* ICMP type. */
225 1.1 rmind offby = npc->npc_hlen;
226 1.1 rmind CTASSERT(offsetof(struct icmp, icmp_type) == 0);
227 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
228 1.1 rmind return false;
229 1.1 rmind if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint8_t), &type))
230 1.1 rmind return false;
231 1.1 rmind
232 1.1 rmind /* ICMP code. */
233 1.1 rmind offby = offsetof(struct icmp, icmp_code);
234 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
235 1.1 rmind return false;
236 1.1 rmind if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint8_t), &npc->npc_icmp_code))
237 1.1 rmind return false;
238 1.1 rmind
239 1.1 rmind /* Mark as cached. */
240 1.1 rmind npc->npc_icmp_type = type;
241 1.1 rmind npc->npc_info |= NPC_ICMP;
242 1.1 rmind return true;
243 1.1 rmind }
244 1.1 rmind
245 1.2 rmind /*
246 1.2 rmind * npf_fetch_tcpfl: fetch TCP flags and store into the cache.
247 1.2 rmind */
248 1.2 rmind bool
249 1.1 rmind npf_fetch_tcpfl(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
250 1.1 rmind {
251 1.1 rmind u_int offby;
252 1.1 rmind
253 1.1 rmind /* Get TCP flags. */
254 1.1 rmind offby = npc->npc_hlen + offsetof(struct tcphdr, th_flags);
255 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
256 1.1 rmind return false;
257 1.1 rmind if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint8_t), &npc->npc_tcp_flags))
258 1.1 rmind return false;
259 1.1 rmind return true;
260 1.1 rmind }
261 1.1 rmind
262 1.1 rmind /*
263 1.2 rmind * npf_cache_all: general routine to cache all relevant IPv4 and
264 1.1 rmind * TCP, UDP or ICMP data.
265 1.1 rmind */
266 1.1 rmind bool
267 1.2 rmind npf_cache_all(npf_cache_t *npc, nbuf_t *nbuf)
268 1.1 rmind {
269 1.1 rmind void *n_ptr = nbuf_dataptr(nbuf);
270 1.1 rmind
271 1.1 rmind /* IPv4: get protocol, source and destination addresses. */
272 1.1 rmind if (!npf_iscached(npc, NPC_IP46) && !npf_ip4_proto(npc, nbuf, n_ptr)) {
273 1.1 rmind return false;
274 1.1 rmind }
275 1.1 rmind if (!npf_iscached(npc, NPC_ADDRS) &&
276 1.1 rmind !npf_fetch_ip4addrs(npc, nbuf, n_ptr)) {
277 1.1 rmind return false;
278 1.1 rmind }
279 1.1 rmind switch (npc->npc_proto) {
280 1.1 rmind case IPPROTO_TCP:
281 1.1 rmind /* TCP flags. */
282 1.1 rmind if (!npf_fetch_tcpfl(npc, nbuf, n_ptr)) {
283 1.1 rmind return false;
284 1.1 rmind }
285 1.1 rmind /* FALLTHROUGH */
286 1.1 rmind
287 1.1 rmind case IPPROTO_UDP:
288 1.1 rmind /* Fetch TCP/UDP ports. */
289 1.1 rmind return npf_fetch_ports(npc, nbuf, n_ptr, npc->npc_proto);
290 1.1 rmind
291 1.1 rmind case IPPROTO_ICMP:
292 1.1 rmind /* Fetch ICMP data. */
293 1.1 rmind return npf_fetch_icmp(npc, nbuf, n_ptr);
294 1.1 rmind }
295 1.1 rmind return false;
296 1.1 rmind }
297 1.1 rmind
298 1.1 rmind /*
299 1.1 rmind * npf_rwrport: rewrite required TCP/UDP port and update checksum.
300 1.1 rmind */
301 1.1 rmind bool
302 1.1 rmind npf_rwrport(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr, const int di,
303 1.1 rmind in_port_t port, in_addr_t naddr)
304 1.1 rmind {
305 1.1 rmind const int proto = npc->npc_proto;
306 1.1 rmind u_int offby, toff;
307 1.1 rmind in_addr_t oaddr;
308 1.1 rmind in_port_t oport;
309 1.1 rmind uint16_t cksum;
310 1.1 rmind
311 1.1 rmind KASSERT(npf_iscached(npc, NPC_PORTS));
312 1.1 rmind KASSERT(proto == IPPROTO_TCP || proto == IPPROTO_UDP);
313 1.1 rmind
314 1.1 rmind offby = npc->npc_hlen;
315 1.1 rmind
316 1.1 rmind if (di == PFIL_OUT) {
317 1.1 rmind /* Offset to the source port is zero. */
318 1.1 rmind CTASSERT(offsetof(struct tcphdr, th_sport) == 0);
319 1.1 rmind CTASSERT(offsetof(struct udphdr, uh_sport) == 0);
320 1.1 rmind if (proto == IPPROTO_TCP) {
321 1.1 rmind toff = offsetof(struct tcphdr, th_sum);
322 1.1 rmind } else {
323 1.1 rmind toff = offsetof(struct udphdr, uh_sum);
324 1.1 rmind }
325 1.1 rmind oaddr = npc->npc_srcip;
326 1.1 rmind oport = npc->npc_sport;
327 1.1 rmind } else {
328 1.1 rmind /* Calculate offset to destination port and checksum. */
329 1.1 rmind u_int poff;
330 1.1 rmind if (proto == IPPROTO_TCP) {
331 1.1 rmind poff = offsetof(struct tcphdr, th_dport);
332 1.1 rmind toff = offsetof(struct tcphdr, th_sum) - poff;
333 1.1 rmind } else {
334 1.1 rmind poff = offsetof(struct udphdr, uh_dport);
335 1.1 rmind toff = offsetof(struct udphdr, uh_sum) - poff;
336 1.1 rmind }
337 1.1 rmind oaddr = npc->npc_dstip;
338 1.1 rmind oport = npc->npc_dport;
339 1.1 rmind offby += poff;
340 1.1 rmind }
341 1.1 rmind
342 1.1 rmind /* Advance and rewrite port. */
343 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
344 1.1 rmind return false;
345 1.1 rmind if (nbuf_store_datum(nbuf, n_ptr, sizeof(in_port_t), &port))
346 1.1 rmind return false;
347 1.1 rmind
348 1.1 rmind /* Advance and update TCP/UDP checksum. */
349 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, toff)) == NULL)
350 1.1 rmind return false;
351 1.1 rmind if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint16_t), &cksum))
352 1.1 rmind return false;
353 1.1 rmind if (__predict_true(cksum || proto == IPPROTO_TCP)) {
354 1.1 rmind cksum = npf_fixup32_cksum(cksum, oaddr, naddr);
355 1.1 rmind cksum = npf_fixup16_cksum(cksum, oport, port);
356 1.1 rmind if (nbuf_store_datum(nbuf, n_ptr, sizeof(uint16_t), &cksum))
357 1.1 rmind return false;
358 1.1 rmind }
359 1.1 rmind return true;
360 1.1 rmind }
361 1.1 rmind
362 1.1 rmind /*
363 1.1 rmind * npf_rwrip: rewrite required IP address and update checksum.
364 1.1 rmind */
365 1.1 rmind bool
366 1.1 rmind npf_rwrip(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr, const int di,
367 1.1 rmind in_addr_t addr)
368 1.1 rmind {
369 1.1 rmind u_int offby;
370 1.1 rmind in_addr_t oaddr;
371 1.1 rmind
372 1.1 rmind KASSERT(npf_iscached(npc, NPC_IP46 | NPC_ADDRS));
373 1.1 rmind
374 1.1 rmind /* Advance to the checksum in IP header and fetch it. */
375 1.1 rmind offby = offsetof(struct ip, ip_sum);
376 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
377 1.1 rmind return false;
378 1.1 rmind
379 1.1 rmind if (di == PFIL_OUT) {
380 1.1 rmind /* Rewrite source address, if outgoing. */
381 1.1 rmind offby = offsetof(struct ip, ip_src) - offby;
382 1.1 rmind oaddr = npc->npc_srcip;
383 1.1 rmind } else {
384 1.1 rmind /* Rewrite destination, if incoming. */
385 1.1 rmind offby = offsetof(struct ip, ip_dst) - offby;
386 1.1 rmind oaddr = npc->npc_dstip;
387 1.1 rmind }
388 1.1 rmind
389 1.1 rmind /* Write new IP checksum (it is acceptable to do this earlier). */
390 1.1 rmind uint16_t cksum = npf_fixup32_cksum(npc->npc_ipsum, oaddr, addr);
391 1.1 rmind if (nbuf_store_datum(nbuf, n_ptr, sizeof(uint16_t), &cksum))
392 1.1 rmind return false;
393 1.1 rmind
394 1.1 rmind /* Advance to address and rewrite it. */
395 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL)
396 1.1 rmind return false;
397 1.1 rmind if (nbuf_store_datum(nbuf, n_ptr, sizeof(in_addr_t), &addr))
398 1.1 rmind return false;
399 1.1 rmind
400 1.1 rmind npc->npc_ipsum = cksum;
401 1.1 rmind return true;
402 1.1 rmind }
403