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