ip_encap.c revision 1.55 1 1.55 knakahar /* $NetBSD: ip_encap.c,v 1.55 2016/07/04 04:26:00 knakahara Exp $ */
2 1.7 itojun /* $KAME: ip_encap.c,v 1.73 2001/10/02 08:30:58 itojun Exp $ */
3 1.1 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 1.1 itojun * All rights reserved.
7 1.1 itojun *
8 1.1 itojun * Redistribution and use in source and binary forms, with or without
9 1.1 itojun * modification, are permitted provided that the following conditions
10 1.1 itojun * are met:
11 1.1 itojun * 1. Redistributions of source code must retain the above copyright
12 1.1 itojun * notice, this list of conditions and the following disclaimer.
13 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 itojun * notice, this list of conditions and the following disclaimer in the
15 1.1 itojun * documentation and/or other materials provided with the distribution.
16 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.1 itojun * may be used to endorse or promote products derived from this software
18 1.1 itojun * without specific prior written permission.
19 1.1 itojun *
20 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 itojun * SUCH DAMAGE.
31 1.1 itojun */
32 1.1 itojun /*
33 1.1 itojun * My grandfather said that there's a devil inside tunnelling technology...
34 1.1 itojun *
35 1.1 itojun * We have surprisingly many protocols that want packets with IP protocol
36 1.1 itojun * #4 or #41. Here's a list of protocols that want protocol #41:
37 1.1 itojun * RFC1933 configured tunnel
38 1.1 itojun * RFC1933 automatic tunnel
39 1.1 itojun * RFC2401 IPsec tunnel
40 1.1 itojun * RFC2473 IPv6 generic packet tunnelling
41 1.1 itojun * RFC2529 6over4 tunnel
42 1.7 itojun * RFC3056 6to4 tunnel
43 1.7 itojun * isatap tunnel
44 1.1 itojun * mobile-ip6 (uses RFC2473)
45 1.1 itojun * Here's a list of protocol that want protocol #4:
46 1.1 itojun * RFC1853 IPv4-in-IPv4 tunnelling
47 1.1 itojun * RFC2003 IPv4 encapsulation within IPv4
48 1.1 itojun * RFC2344 reverse tunnelling for mobile-ip4
49 1.1 itojun * RFC2401 IPsec tunnel
50 1.1 itojun * Well, what can I say. They impose different en/decapsulation mechanism
51 1.1 itojun * from each other, so they need separate protocol handler. The only one
52 1.1 itojun * we can easily determine by protocol # is IPsec, which always has
53 1.1 itojun * AH/ESP/IPComp header right after outer IP header.
54 1.1 itojun *
55 1.1 itojun * So, clearly good old protosw does not work for protocol #4 and #41.
56 1.1 itojun * The code will let you match protocol via src/dst address pair.
57 1.1 itojun */
58 1.1 itojun /* XXX is M_NETADDR correct? */
59 1.6 lukem
60 1.7 itojun /*
61 1.55 knakahar * With USE_RADIX the code will use radix table for tunnel lookup, for
62 1.7 itojun * tunnels registered with encap_attach() with a addr/mask pair.
63 1.7 itojun * Faster on machines with thousands of tunnel registerations (= interfaces).
64 1.7 itojun *
65 1.7 itojun * The code assumes that radix table code can handle non-continuous netmask,
66 1.7 itojun * as it will pass radix table memory region with (src + dst) sockaddr pair.
67 1.7 itojun */
68 1.55 knakahar #define USE_RADIX
69 1.7 itojun
70 1.6 lukem #include <sys/cdefs.h>
71 1.55 knakahar __KERNEL_RCSID(0, "$NetBSD: ip_encap.c,v 1.55 2016/07/04 04:26:00 knakahara Exp $");
72 1.1 itojun
73 1.46 pooka #ifdef _KERNEL_OPT
74 1.4 itojun #include "opt_mrouting.h"
75 1.4 itojun #include "opt_inet.h"
76 1.46 pooka #endif
77 1.1 itojun
78 1.1 itojun #include <sys/param.h>
79 1.1 itojun #include <sys/systm.h>
80 1.1 itojun #include <sys/socket.h>
81 1.1 itojun #include <sys/sockio.h>
82 1.1 itojun #include <sys/mbuf.h>
83 1.1 itojun #include <sys/errno.h>
84 1.4 itojun #include <sys/queue.h>
85 1.47 knakahar #include <sys/kmem.h>
86 1.54 knakahar #include <sys/once.h>
87 1.54 knakahar #include <sys/rwlock.h>
88 1.1 itojun
89 1.1 itojun #include <net/if.h>
90 1.1 itojun
91 1.1 itojun #include <netinet/in.h>
92 1.1 itojun #include <netinet/in_systm.h>
93 1.1 itojun #include <netinet/ip.h>
94 1.1 itojun #include <netinet/ip_var.h>
95 1.1 itojun #include <netinet/ip_encap.h>
96 1.1 itojun #ifdef MROUTING
97 1.1 itojun #include <netinet/ip_mroute.h>
98 1.1 itojun #endif /* MROUTING */
99 1.1 itojun
100 1.1 itojun #ifdef INET6
101 1.1 itojun #include <netinet/ip6.h>
102 1.1 itojun #include <netinet6/ip6_var.h>
103 1.51 knakahar #include <netinet6/ip6protosw.h> /* for struct ip6ctlparam */
104 1.7 itojun #include <netinet6/in6_var.h>
105 1.7 itojun #include <netinet6/in6_pcb.h>
106 1.7 itojun #include <netinet/icmp6.h>
107 1.1 itojun #endif
108 1.1 itojun
109 1.1 itojun #include <net/net_osdep.h>
110 1.1 itojun
111 1.7 itojun enum direction { INBOUND, OUTBOUND };
112 1.7 itojun
113 1.7 itojun #ifdef INET
114 1.22 perry static struct encaptab *encap4_lookup(struct mbuf *, int, int, enum direction);
115 1.7 itojun #endif
116 1.7 itojun #ifdef INET6
117 1.22 perry static struct encaptab *encap6_lookup(struct mbuf *, int, int, enum direction);
118 1.7 itojun #endif
119 1.22 perry static int encap_add(struct encaptab *);
120 1.22 perry static int encap_remove(struct encaptab *);
121 1.22 perry static int encap_afcheck(int, const struct sockaddr *, const struct sockaddr *);
122 1.55 knakahar #ifdef USE_RADIX
123 1.22 perry static struct radix_node_head *encap_rnh(int);
124 1.22 perry static int mask_matchlen(const struct sockaddr *);
125 1.55 knakahar #else
126 1.55 knakahar static int mask_match(const struct encaptab *, const struct sockaddr *,
127 1.55 knakahar const struct sockaddr *);
128 1.55 knakahar #endif
129 1.22 perry static void encap_fillarg(struct mbuf *, const struct encaptab *);
130 1.1 itojun
131 1.2 thorpej LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab);
132 1.1 itojun
133 1.55 knakahar #ifdef USE_RADIX
134 1.7 itojun struct radix_node_head *encap_head[2]; /* 0 for AF_INET, 1 for AF_INET6 */
135 1.55 knakahar #endif
136 1.7 itojun
137 1.54 knakahar static ONCE_DECL(encap_init_control);
138 1.54 knakahar
139 1.54 knakahar static krwlock_t encap_whole_lock __cacheline_aligned;
140 1.54 knakahar
141 1.54 knakahar static int encap_init_once(void);
142 1.54 knakahar
143 1.1 itojun void
144 1.23 perry encap_init(void)
145 1.1 itojun {
146 1.7 itojun static int initialized = 0;
147 1.7 itojun
148 1.7 itojun if (initialized)
149 1.7 itojun return;
150 1.7 itojun initialized++;
151 1.1 itojun #if 0
152 1.1 itojun /*
153 1.1 itojun * we cannot use LIST_INIT() here, since drivers may want to call
154 1.4 itojun * encap_attach(), on driver attach. encap_init() will be called
155 1.1 itojun * on AF_INET{,6} initialization, which happens after driver
156 1.1 itojun * initialization - using LIST_INIT() here can nuke encap_attach()
157 1.1 itojun * from drivers.
158 1.1 itojun */
159 1.1 itojun LIST_INIT(&encaptab);
160 1.1 itojun #endif
161 1.7 itojun
162 1.55 knakahar #ifdef USE_RADIX
163 1.7 itojun /*
164 1.38 pooka * initialize radix lookup table when the radix subsystem is inited.
165 1.7 itojun */
166 1.38 pooka rn_delayedinit((void *)&encap_head[0],
167 1.38 pooka sizeof(struct sockaddr_pack) << 3);
168 1.7 itojun #ifdef INET6
169 1.38 pooka rn_delayedinit((void *)&encap_head[1],
170 1.38 pooka sizeof(struct sockaddr_pack) << 3);
171 1.7 itojun #endif
172 1.55 knakahar #endif
173 1.1 itojun }
174 1.1 itojun
175 1.4 itojun #ifdef INET
176 1.7 itojun static struct encaptab *
177 1.23 perry encap4_lookup(struct mbuf *m, int off, int proto, enum direction dir)
178 1.1 itojun {
179 1.1 itojun struct ip *ip;
180 1.33 pooka struct ip_pack4 pack;
181 1.1 itojun struct encaptab *ep, *match;
182 1.1 itojun int prio, matchprio;
183 1.55 knakahar #ifdef USE_RADIX
184 1.7 itojun struct radix_node_head *rnh = encap_rnh(AF_INET);
185 1.7 itojun struct radix_node *rn;
186 1.55 knakahar #endif
187 1.1 itojun
188 1.41 ozaki KASSERT(m->m_len >= sizeof(*ip));
189 1.54 knakahar KASSERT(rw_read_held(&encap_whole_lock));
190 1.41 ozaki
191 1.1 itojun ip = mtod(m, struct ip *);
192 1.1 itojun
193 1.35 cegger memset(&pack, 0, sizeof(pack));
194 1.7 itojun pack.p.sp_len = sizeof(pack);
195 1.7 itojun pack.mine.sin_family = pack.yours.sin_family = AF_INET;
196 1.7 itojun pack.mine.sin_len = pack.yours.sin_len = sizeof(struct sockaddr_in);
197 1.7 itojun if (dir == INBOUND) {
198 1.7 itojun pack.mine.sin_addr = ip->ip_dst;
199 1.7 itojun pack.yours.sin_addr = ip->ip_src;
200 1.7 itojun } else {
201 1.7 itojun pack.mine.sin_addr = ip->ip_src;
202 1.7 itojun pack.yours.sin_addr = ip->ip_dst;
203 1.7 itojun }
204 1.1 itojun
205 1.1 itojun match = NULL;
206 1.1 itojun matchprio = 0;
207 1.7 itojun
208 1.55 knakahar #ifdef USE_RADIX
209 1.30 christos rn = rnh->rnh_matchaddr((void *)&pack, rnh);
210 1.7 itojun if (rn && (rn->rn_flags & RNF_ROOT) == 0) {
211 1.7 itojun match = (struct encaptab *)rn;
212 1.7 itojun matchprio = mask_matchlen(match->srcmask) +
213 1.7 itojun mask_matchlen(match->dstmask);
214 1.7 itojun }
215 1.55 knakahar #endif
216 1.7 itojun
217 1.31 dyoung LIST_FOREACH(ep, &encaptab, chain) {
218 1.1 itojun if (ep->af != AF_INET)
219 1.1 itojun continue;
220 1.1 itojun if (ep->proto >= 0 && ep->proto != proto)
221 1.1 itojun continue;
222 1.1 itojun if (ep->func)
223 1.1 itojun prio = (*ep->func)(m, off, proto, ep->arg);
224 1.55 knakahar else {
225 1.55 knakahar #ifdef USE_RADIX
226 1.7 itojun continue;
227 1.55 knakahar #else
228 1.55 knakahar prio = mask_match(ep, (struct sockaddr *)&pack.mine,
229 1.55 knakahar (struct sockaddr *)&pack.yours);
230 1.55 knakahar #endif
231 1.55 knakahar }
232 1.1 itojun
233 1.1 itojun /*
234 1.1 itojun * We prioritize the matches by using bit length of the
235 1.1 itojun * matches. mask_match() and user-supplied matching function
236 1.1 itojun * should return the bit length of the matches (for example,
237 1.1 itojun * if both src/dst are matched for IPv4, 64 should be returned).
238 1.1 itojun * 0 or negative return value means "it did not match".
239 1.1 itojun *
240 1.1 itojun * The question is, since we have two "mask" portion, we
241 1.1 itojun * cannot really define total order between entries.
242 1.1 itojun * For example, which of these should be preferred?
243 1.1 itojun * mask_match() returns 48 (32 + 16) for both of them.
244 1.1 itojun * src=3ffe::/16, dst=3ffe:501::/32
245 1.1 itojun * src=3ffe:501::/32, dst=3ffe::/16
246 1.1 itojun *
247 1.1 itojun * We need to loop through all the possible candidates
248 1.1 itojun * to get the best match - the search takes O(n) for
249 1.1 itojun * n attachments (i.e. interfaces).
250 1.7 itojun *
251 1.7 itojun * For radix-based lookup, I guess source takes precedence.
252 1.7 itojun * See rn_{refines,lexobetter} for the correct answer.
253 1.1 itojun */
254 1.1 itojun if (prio <= 0)
255 1.1 itojun continue;
256 1.1 itojun if (prio > matchprio) {
257 1.1 itojun matchprio = prio;
258 1.1 itojun match = ep;
259 1.1 itojun }
260 1.1 itojun }
261 1.1 itojun
262 1.7 itojun return match;
263 1.7 itojun }
264 1.7 itojun
265 1.7 itojun void
266 1.7 itojun encap4_input(struct mbuf *m, ...)
267 1.7 itojun {
268 1.7 itojun int off, proto;
269 1.7 itojun va_list ap;
270 1.51 knakahar const struct encapsw *esw;
271 1.7 itojun struct encaptab *match;
272 1.7 itojun
273 1.7 itojun va_start(ap, m);
274 1.7 itojun off = va_arg(ap, int);
275 1.7 itojun proto = va_arg(ap, int);
276 1.7 itojun va_end(ap);
277 1.7 itojun
278 1.54 knakahar rw_enter(&encap_whole_lock, RW_READER);
279 1.7 itojun match = encap4_lookup(m, off, proto, INBOUND);
280 1.7 itojun
281 1.1 itojun if (match) {
282 1.1 itojun /* found a match, "match" has the best one */
283 1.51 knakahar esw = match->esw;
284 1.51 knakahar if (esw && esw->encapsw4.pr_input) {
285 1.1 itojun encap_fillarg(m, match);
286 1.54 knakahar rw_exit(&encap_whole_lock);
287 1.51 knakahar (*esw->encapsw4.pr_input)(m, off, proto);
288 1.54 knakahar } else {
289 1.54 knakahar rw_exit(&encap_whole_lock);
290 1.1 itojun m_freem(m);
291 1.54 knakahar }
292 1.1 itojun return;
293 1.1 itojun }
294 1.54 knakahar rw_exit(&encap_whole_lock);
295 1.1 itojun
296 1.1 itojun /* last resort: inject to raw socket */
297 1.1 itojun rip_input(m, off, proto);
298 1.1 itojun }
299 1.1 itojun #endif
300 1.1 itojun
301 1.1 itojun #ifdef INET6
302 1.7 itojun static struct encaptab *
303 1.23 perry encap6_lookup(struct mbuf *m, int off, int proto, enum direction dir)
304 1.1 itojun {
305 1.1 itojun struct ip6_hdr *ip6;
306 1.33 pooka struct ip_pack6 pack;
307 1.7 itojun int prio, matchprio;
308 1.1 itojun struct encaptab *ep, *match;
309 1.55 knakahar #ifdef USE_RADIX
310 1.7 itojun struct radix_node_head *rnh = encap_rnh(AF_INET6);
311 1.7 itojun struct radix_node *rn;
312 1.55 knakahar #endif
313 1.1 itojun
314 1.41 ozaki KASSERT(m->m_len >= sizeof(*ip6));
315 1.54 knakahar KASSERT(rw_read_held(&encap_whole_lock));
316 1.41 ozaki
317 1.1 itojun ip6 = mtod(m, struct ip6_hdr *);
318 1.1 itojun
319 1.35 cegger memset(&pack, 0, sizeof(pack));
320 1.7 itojun pack.p.sp_len = sizeof(pack);
321 1.7 itojun pack.mine.sin6_family = pack.yours.sin6_family = AF_INET6;
322 1.7 itojun pack.mine.sin6_len = pack.yours.sin6_len = sizeof(struct sockaddr_in6);
323 1.7 itojun if (dir == INBOUND) {
324 1.7 itojun pack.mine.sin6_addr = ip6->ip6_dst;
325 1.7 itojun pack.yours.sin6_addr = ip6->ip6_src;
326 1.7 itojun } else {
327 1.7 itojun pack.mine.sin6_addr = ip6->ip6_src;
328 1.7 itojun pack.yours.sin6_addr = ip6->ip6_dst;
329 1.7 itojun }
330 1.1 itojun
331 1.1 itojun match = NULL;
332 1.1 itojun matchprio = 0;
333 1.7 itojun
334 1.55 knakahar #ifdef USE_RADIX
335 1.30 christos rn = rnh->rnh_matchaddr((void *)&pack, rnh);
336 1.7 itojun if (rn && (rn->rn_flags & RNF_ROOT) == 0) {
337 1.7 itojun match = (struct encaptab *)rn;
338 1.7 itojun matchprio = mask_matchlen(match->srcmask) +
339 1.7 itojun mask_matchlen(match->dstmask);
340 1.7 itojun }
341 1.55 knakahar #endif
342 1.7 itojun
343 1.31 dyoung LIST_FOREACH(ep, &encaptab, chain) {
344 1.1 itojun if (ep->af != AF_INET6)
345 1.1 itojun continue;
346 1.1 itojun if (ep->proto >= 0 && ep->proto != proto)
347 1.1 itojun continue;
348 1.1 itojun if (ep->func)
349 1.7 itojun prio = (*ep->func)(m, off, proto, ep->arg);
350 1.55 knakahar else {
351 1.55 knakahar #ifdef USE_RADIX
352 1.7 itojun continue;
353 1.55 knakahar #else
354 1.55 knakahar prio = mask_match(ep, (struct sockaddr *)&pack.mine,
355 1.55 knakahar (struct sockaddr *)&pack.yours);
356 1.55 knakahar #endif
357 1.55 knakahar }
358 1.1 itojun
359 1.7 itojun /* see encap4_lookup() for issues here */
360 1.1 itojun if (prio <= 0)
361 1.1 itojun continue;
362 1.1 itojun if (prio > matchprio) {
363 1.1 itojun matchprio = prio;
364 1.1 itojun match = ep;
365 1.1 itojun }
366 1.1 itojun }
367 1.1 itojun
368 1.7 itojun return match;
369 1.7 itojun }
370 1.7 itojun
371 1.7 itojun int
372 1.23 perry encap6_input(struct mbuf **mp, int *offp, int proto)
373 1.7 itojun {
374 1.7 itojun struct mbuf *m = *mp;
375 1.51 knakahar const struct encapsw *esw;
376 1.7 itojun struct encaptab *match;
377 1.7 itojun
378 1.54 knakahar rw_enter(&encap_whole_lock, RW_READER);
379 1.7 itojun match = encap6_lookup(m, *offp, proto, INBOUND);
380 1.7 itojun
381 1.1 itojun if (match) {
382 1.1 itojun /* found a match */
383 1.51 knakahar esw = match->esw;
384 1.51 knakahar if (esw && esw->encapsw6.pr_input) {
385 1.1 itojun encap_fillarg(m, match);
386 1.54 knakahar rw_exit(&encap_whole_lock);
387 1.51 knakahar return (*esw->encapsw6.pr_input)(mp, offp, proto);
388 1.1 itojun } else {
389 1.54 knakahar rw_exit(&encap_whole_lock);
390 1.1 itojun m_freem(m);
391 1.1 itojun return IPPROTO_DONE;
392 1.1 itojun }
393 1.1 itojun }
394 1.54 knakahar rw_exit(&encap_whole_lock);
395 1.1 itojun
396 1.1 itojun /* last resort: inject to raw socket */
397 1.1 itojun return rip6_input(mp, offp, proto);
398 1.1 itojun }
399 1.1 itojun #endif
400 1.1 itojun
401 1.54 knakahar /*
402 1.54 knakahar * XXX
403 1.54 knakahar * The encaptab list and the rnh radix tree must be manipulated atomically.
404 1.54 knakahar */
405 1.7 itojun static int
406 1.23 perry encap_add(struct encaptab *ep)
407 1.1 itojun {
408 1.55 knakahar #ifdef USE_RADIX
409 1.7 itojun struct radix_node_head *rnh = encap_rnh(ep->af);
410 1.55 knakahar #endif
411 1.7 itojun int error = 0;
412 1.1 itojun
413 1.54 knakahar KASSERT(rw_write_held(&encap_whole_lock));
414 1.54 knakahar
415 1.1 itojun LIST_INSERT_HEAD(&encaptab, ep, chain);
416 1.55 knakahar #ifdef USE_RADIX
417 1.7 itojun if (!ep->func && rnh) {
418 1.30 christos if (!rnh->rnh_addaddr((void *)ep->addrpack,
419 1.30 christos (void *)ep->maskpack, rnh, ep->nodes)) {
420 1.7 itojun error = EEXIST;
421 1.7 itojun goto fail;
422 1.7 itojun }
423 1.7 itojun }
424 1.55 knakahar #endif
425 1.7 itojun return error;
426 1.7 itojun
427 1.55 knakahar #ifdef USE_RADIX
428 1.7 itojun fail:
429 1.7 itojun LIST_REMOVE(ep, chain);
430 1.7 itojun return error;
431 1.55 knakahar #endif
432 1.7 itojun }
433 1.7 itojun
434 1.54 knakahar /*
435 1.54 knakahar * XXX
436 1.54 knakahar * The encaptab list and the rnh radix tree must be manipulated atomically.
437 1.54 knakahar */
438 1.7 itojun static int
439 1.23 perry encap_remove(struct encaptab *ep)
440 1.7 itojun {
441 1.55 knakahar #ifdef USE_RADIX
442 1.7 itojun struct radix_node_head *rnh = encap_rnh(ep->af);
443 1.55 knakahar #endif
444 1.7 itojun int error = 0;
445 1.7 itojun
446 1.54 knakahar KASSERT(rw_write_held(&encap_whole_lock));
447 1.54 knakahar
448 1.7 itojun LIST_REMOVE(ep, chain);
449 1.55 knakahar #ifdef USE_RADIX
450 1.7 itojun if (!ep->func && rnh) {
451 1.30 christos if (!rnh->rnh_deladdr((void *)ep->addrpack,
452 1.30 christos (void *)ep->maskpack, rnh))
453 1.7 itojun error = ESRCH;
454 1.7 itojun }
455 1.55 knakahar #endif
456 1.7 itojun return error;
457 1.7 itojun }
458 1.7 itojun
459 1.7 itojun static int
460 1.23 perry encap_afcheck(int af, const struct sockaddr *sp, const struct sockaddr *dp)
461 1.7 itojun {
462 1.7 itojun if (sp && dp) {
463 1.7 itojun if (sp->sa_len != dp->sa_len)
464 1.7 itojun return EINVAL;
465 1.7 itojun if (af != sp->sa_family || af != dp->sa_family)
466 1.7 itojun return EINVAL;
467 1.7 itojun } else if (!sp && !dp)
468 1.7 itojun ;
469 1.7 itojun else
470 1.7 itojun return EINVAL;
471 1.7 itojun
472 1.7 itojun switch (af) {
473 1.7 itojun case AF_INET:
474 1.7 itojun if (sp && sp->sa_len != sizeof(struct sockaddr_in))
475 1.7 itojun return EINVAL;
476 1.7 itojun if (dp && dp->sa_len != sizeof(struct sockaddr_in))
477 1.7 itojun return EINVAL;
478 1.7 itojun break;
479 1.7 itojun #ifdef INET6
480 1.7 itojun case AF_INET6:
481 1.7 itojun if (sp && sp->sa_len != sizeof(struct sockaddr_in6))
482 1.7 itojun return EINVAL;
483 1.7 itojun if (dp && dp->sa_len != sizeof(struct sockaddr_in6))
484 1.7 itojun return EINVAL;
485 1.7 itojun break;
486 1.7 itojun #endif
487 1.7 itojun default:
488 1.7 itojun return EAFNOSUPPORT;
489 1.7 itojun }
490 1.7 itojun
491 1.7 itojun return 0;
492 1.1 itojun }
493 1.1 itojun
494 1.54 knakahar static int
495 1.54 knakahar encap_init_once(void)
496 1.54 knakahar {
497 1.54 knakahar
498 1.54 knakahar rw_init(&encap_whole_lock);
499 1.54 knakahar
500 1.54 knakahar return 0;
501 1.54 knakahar }
502 1.54 knakahar
503 1.1 itojun /*
504 1.1 itojun * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
505 1.1 itojun * length of mask (sm and dm) is assumed to be same as sp/dp.
506 1.1 itojun * Return value will be necessary as input (cookie) for encap_detach().
507 1.1 itojun */
508 1.1 itojun const struct encaptab *
509 1.23 perry encap_attach(int af, int proto,
510 1.23 perry const struct sockaddr *sp, const struct sockaddr *sm,
511 1.23 perry const struct sockaddr *dp, const struct sockaddr *dm,
512 1.51 knakahar const struct encapsw *esw, void *arg)
513 1.1 itojun {
514 1.1 itojun struct encaptab *ep;
515 1.1 itojun int error;
516 1.1 itojun int s;
517 1.7 itojun size_t l;
518 1.33 pooka struct ip_pack4 *pack4;
519 1.7 itojun #ifdef INET6
520 1.33 pooka struct ip_pack6 *pack6;
521 1.7 itojun #endif
522 1.1 itojun
523 1.54 knakahar RUN_ONCE(&encap_init_control, encap_init_once);
524 1.54 knakahar
525 1.1 itojun s = splsoftnet();
526 1.1 itojun /* sanity check on args */
527 1.7 itojun error = encap_afcheck(af, sp, dp);
528 1.7 itojun if (error)
529 1.1 itojun goto fail;
530 1.1 itojun
531 1.1 itojun /* check if anyone have already attached with exactly same config */
532 1.31 dyoung LIST_FOREACH(ep, &encaptab, chain) {
533 1.1 itojun if (ep->af != af)
534 1.1 itojun continue;
535 1.1 itojun if (ep->proto != proto)
536 1.1 itojun continue;
537 1.7 itojun if (ep->func)
538 1.7 itojun continue;
539 1.41 ozaki
540 1.43 riastrad KASSERT(ep->src != NULL);
541 1.43 riastrad KASSERT(ep->dst != NULL);
542 1.43 riastrad KASSERT(ep->srcmask != NULL);
543 1.43 riastrad KASSERT(ep->dstmask != NULL);
544 1.41 ozaki
545 1.7 itojun if (ep->src->sa_len != sp->sa_len ||
546 1.34 cegger memcmp(ep->src, sp, sp->sa_len) != 0 ||
547 1.34 cegger memcmp(ep->srcmask, sm, sp->sa_len) != 0)
548 1.7 itojun continue;
549 1.7 itojun if (ep->dst->sa_len != dp->sa_len ||
550 1.34 cegger memcmp(ep->dst, dp, dp->sa_len) != 0 ||
551 1.34 cegger memcmp(ep->dstmask, dm, dp->sa_len) != 0)
552 1.1 itojun continue;
553 1.1 itojun
554 1.1 itojun error = EEXIST;
555 1.1 itojun goto fail;
556 1.1 itojun }
557 1.3 thorpej
558 1.7 itojun switch (af) {
559 1.7 itojun case AF_INET:
560 1.7 itojun l = sizeof(*pack4);
561 1.7 itojun break;
562 1.7 itojun #ifdef INET6
563 1.7 itojun case AF_INET6:
564 1.7 itojun l = sizeof(*pack6);
565 1.7 itojun break;
566 1.7 itojun #endif
567 1.7 itojun default:
568 1.7 itojun goto fail;
569 1.7 itojun }
570 1.7 itojun
571 1.20 itojun /* M_NETADDR ok? */
572 1.47 knakahar ep = kmem_zalloc(sizeof(*ep), KM_NOSLEEP);
573 1.1 itojun if (ep == NULL) {
574 1.1 itojun error = ENOBUFS;
575 1.1 itojun goto fail;
576 1.1 itojun }
577 1.47 knakahar ep->addrpack = kmem_zalloc(l, KM_NOSLEEP);
578 1.7 itojun if (ep->addrpack == NULL) {
579 1.7 itojun error = ENOBUFS;
580 1.7 itojun goto gc;
581 1.7 itojun }
582 1.47 knakahar ep->maskpack = kmem_zalloc(l, KM_NOSLEEP);
583 1.7 itojun if (ep->maskpack == NULL) {
584 1.7 itojun error = ENOBUFS;
585 1.7 itojun goto gc;
586 1.7 itojun }
587 1.1 itojun
588 1.1 itojun ep->af = af;
589 1.1 itojun ep->proto = proto;
590 1.7 itojun ep->addrpack->sa_len = l & 0xff;
591 1.7 itojun ep->maskpack->sa_len = l & 0xff;
592 1.7 itojun switch (af) {
593 1.7 itojun case AF_INET:
594 1.33 pooka pack4 = (struct ip_pack4 *)ep->addrpack;
595 1.7 itojun ep->src = (struct sockaddr *)&pack4->mine;
596 1.7 itojun ep->dst = (struct sockaddr *)&pack4->yours;
597 1.33 pooka pack4 = (struct ip_pack4 *)ep->maskpack;
598 1.7 itojun ep->srcmask = (struct sockaddr *)&pack4->mine;
599 1.7 itojun ep->dstmask = (struct sockaddr *)&pack4->yours;
600 1.7 itojun break;
601 1.7 itojun #ifdef INET6
602 1.7 itojun case AF_INET6:
603 1.33 pooka pack6 = (struct ip_pack6 *)ep->addrpack;
604 1.7 itojun ep->src = (struct sockaddr *)&pack6->mine;
605 1.7 itojun ep->dst = (struct sockaddr *)&pack6->yours;
606 1.33 pooka pack6 = (struct ip_pack6 *)ep->maskpack;
607 1.7 itojun ep->srcmask = (struct sockaddr *)&pack6->mine;
608 1.7 itojun ep->dstmask = (struct sockaddr *)&pack6->yours;
609 1.7 itojun break;
610 1.7 itojun #endif
611 1.7 itojun }
612 1.7 itojun
613 1.37 tsutsui memcpy(ep->src, sp, sp->sa_len);
614 1.37 tsutsui memcpy(ep->srcmask, sm, sp->sa_len);
615 1.37 tsutsui memcpy(ep->dst, dp, dp->sa_len);
616 1.37 tsutsui memcpy(ep->dstmask, dm, dp->sa_len);
617 1.51 knakahar ep->esw = esw;
618 1.1 itojun ep->arg = arg;
619 1.1 itojun
620 1.54 knakahar rw_enter(&encap_whole_lock, RW_WRITER);
621 1.7 itojun error = encap_add(ep);
622 1.54 knakahar rw_exit(&encap_whole_lock);
623 1.7 itojun if (error)
624 1.7 itojun goto gc;
625 1.1 itojun
626 1.1 itojun error = 0;
627 1.1 itojun splx(s);
628 1.1 itojun return ep;
629 1.1 itojun
630 1.7 itojun gc:
631 1.7 itojun if (ep->addrpack)
632 1.47 knakahar kmem_free(ep->addrpack, l);
633 1.7 itojun if (ep->maskpack)
634 1.47 knakahar kmem_free(ep->maskpack, l);
635 1.7 itojun if (ep)
636 1.47 knakahar kmem_free(ep, sizeof(*ep));
637 1.1 itojun fail:
638 1.1 itojun splx(s);
639 1.1 itojun return NULL;
640 1.1 itojun }
641 1.1 itojun
642 1.1 itojun const struct encaptab *
643 1.23 perry encap_attach_func(int af, int proto,
644 1.26 martin int (*func)(struct mbuf *, int, int, void *),
645 1.51 knakahar const struct encapsw *esw, void *arg)
646 1.1 itojun {
647 1.1 itojun struct encaptab *ep;
648 1.1 itojun int error;
649 1.1 itojun int s;
650 1.1 itojun
651 1.54 knakahar RUN_ONCE(&encap_init_control, encap_init_once);
652 1.54 knakahar
653 1.1 itojun s = splsoftnet();
654 1.1 itojun /* sanity check on args */
655 1.1 itojun if (!func) {
656 1.1 itojun error = EINVAL;
657 1.1 itojun goto fail;
658 1.1 itojun }
659 1.1 itojun
660 1.7 itojun error = encap_afcheck(af, NULL, NULL);
661 1.7 itojun if (error)
662 1.7 itojun goto fail;
663 1.7 itojun
664 1.47 knakahar ep = kmem_alloc(sizeof(*ep), KM_NOSLEEP); /*XXX*/
665 1.1 itojun if (ep == NULL) {
666 1.1 itojun error = ENOBUFS;
667 1.1 itojun goto fail;
668 1.1 itojun }
669 1.35 cegger memset(ep, 0, sizeof(*ep));
670 1.1 itojun
671 1.1 itojun ep->af = af;
672 1.1 itojun ep->proto = proto;
673 1.1 itojun ep->func = func;
674 1.51 knakahar ep->esw = esw;
675 1.1 itojun ep->arg = arg;
676 1.1 itojun
677 1.54 knakahar rw_enter(&encap_whole_lock, RW_WRITER);
678 1.7 itojun error = encap_add(ep);
679 1.54 knakahar rw_exit(&encap_whole_lock);
680 1.7 itojun if (error)
681 1.7 itojun goto fail;
682 1.1 itojun
683 1.1 itojun error = 0;
684 1.1 itojun splx(s);
685 1.1 itojun return ep;
686 1.1 itojun
687 1.1 itojun fail:
688 1.1 itojun splx(s);
689 1.1 itojun return NULL;
690 1.1 itojun }
691 1.1 itojun
692 1.7 itojun /* XXX encap4_ctlinput() is necessary if we set DF=1 on outer IPv4 header */
693 1.7 itojun
694 1.7 itojun #ifdef INET6
695 1.32 ad void *
696 1.29 dyoung encap6_ctlinput(int cmd, const struct sockaddr *sa, void *d0)
697 1.7 itojun {
698 1.7 itojun void *d = d0;
699 1.7 itojun struct ip6_hdr *ip6;
700 1.7 itojun struct mbuf *m;
701 1.7 itojun int off;
702 1.7 itojun struct ip6ctlparam *ip6cp = NULL;
703 1.7 itojun int nxt;
704 1.7 itojun struct encaptab *ep;
705 1.51 knakahar const struct encapsw *esw;
706 1.7 itojun
707 1.7 itojun if (sa->sa_family != AF_INET6 ||
708 1.7 itojun sa->sa_len != sizeof(struct sockaddr_in6))
709 1.32 ad return NULL;
710 1.7 itojun
711 1.7 itojun if ((unsigned)cmd >= PRC_NCMDS)
712 1.32 ad return NULL;
713 1.7 itojun if (cmd == PRC_HOSTDEAD)
714 1.7 itojun d = NULL;
715 1.7 itojun else if (cmd == PRC_MSGSIZE)
716 1.7 itojun ; /* special code is present, see below */
717 1.7 itojun else if (inet6ctlerrmap[cmd] == 0)
718 1.32 ad return NULL;
719 1.7 itojun
720 1.7 itojun /* if the parameter is from icmp6, decode it. */
721 1.7 itojun if (d != NULL) {
722 1.7 itojun ip6cp = (struct ip6ctlparam *)d;
723 1.7 itojun m = ip6cp->ip6c_m;
724 1.7 itojun ip6 = ip6cp->ip6c_ip6;
725 1.7 itojun off = ip6cp->ip6c_off;
726 1.7 itojun nxt = ip6cp->ip6c_nxt;
727 1.15 mycroft
728 1.15 mycroft if (ip6 && cmd == PRC_MSGSIZE) {
729 1.15 mycroft int valid = 0;
730 1.15 mycroft struct encaptab *match;
731 1.15 mycroft
732 1.15 mycroft /*
733 1.15 mycroft * Check to see if we have a valid encap configuration.
734 1.15 mycroft */
735 1.54 knakahar rw_enter(&encap_whole_lock, RW_READER);
736 1.15 mycroft match = encap6_lookup(m, off, nxt, OUTBOUND);
737 1.15 mycroft if (match)
738 1.15 mycroft valid++;
739 1.54 knakahar rw_exit(&encap_whole_lock);
740 1.15 mycroft
741 1.15 mycroft /*
742 1.15 mycroft * Depending on the value of "valid" and routing table
743 1.15 mycroft * size (mtudisc_{hi,lo}wat), we will:
744 1.15 mycroft * - recalcurate the new MTU and create the
745 1.15 mycroft * corresponding routing entry, or
746 1.15 mycroft * - ignore the MTU change notification.
747 1.15 mycroft */
748 1.15 mycroft icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
749 1.15 mycroft }
750 1.7 itojun } else {
751 1.7 itojun m = NULL;
752 1.7 itojun ip6 = NULL;
753 1.7 itojun nxt = -1;
754 1.7 itojun }
755 1.7 itojun
756 1.7 itojun /* inform all listeners */
757 1.54 knakahar rw_enter(&encap_whole_lock, RW_READER);
758 1.31 dyoung LIST_FOREACH(ep, &encaptab, chain) {
759 1.7 itojun if (ep->af != AF_INET6)
760 1.7 itojun continue;
761 1.7 itojun if (ep->proto >= 0 && ep->proto != nxt)
762 1.7 itojun continue;
763 1.7 itojun
764 1.7 itojun /* should optimize by looking at address pairs */
765 1.7 itojun
766 1.7 itojun /* XXX need to pass ep->arg or ep itself to listeners */
767 1.51 knakahar esw = ep->esw;
768 1.51 knakahar if (esw && esw->encapsw6.pr_ctlinput) {
769 1.52 knakahar (*esw->encapsw6.pr_ctlinput)(cmd, sa, d, ep->arg);
770 1.51 knakahar }
771 1.7 itojun }
772 1.54 knakahar rw_exit(&encap_whole_lock);
773 1.7 itojun
774 1.7 itojun rip6_ctlinput(cmd, sa, d0);
775 1.32 ad return NULL;
776 1.7 itojun }
777 1.7 itojun #endif
778 1.7 itojun
779 1.1 itojun int
780 1.23 perry encap_detach(const struct encaptab *cookie)
781 1.1 itojun {
782 1.1 itojun const struct encaptab *ep = cookie;
783 1.42 ozaki struct encaptab *p, *np;
784 1.7 itojun int error;
785 1.1 itojun
786 1.54 knakahar rw_enter(&encap_whole_lock, RW_WRITER);
787 1.42 ozaki LIST_FOREACH_SAFE(p, &encaptab, chain, np) {
788 1.1 itojun if (p == ep) {
789 1.7 itojun error = encap_remove(p);
790 1.54 knakahar rw_exit(&encap_whole_lock);
791 1.7 itojun if (error)
792 1.7 itojun return error;
793 1.54 knakahar
794 1.7 itojun if (!ep->func) {
795 1.47 knakahar kmem_free(p->addrpack, ep->addrpack->sa_len);
796 1.47 knakahar kmem_free(p->maskpack, ep->maskpack->sa_len);
797 1.7 itojun }
798 1.47 knakahar kmem_free(p, sizeof(*p)); /*XXX*/
799 1.1 itojun return 0;
800 1.1 itojun }
801 1.1 itojun }
802 1.54 knakahar rw_exit(&encap_whole_lock);
803 1.1 itojun
804 1.7 itojun return ENOENT;
805 1.7 itojun }
806 1.7 itojun
807 1.55 knakahar #ifdef USE_RADIX
808 1.7 itojun static struct radix_node_head *
809 1.23 perry encap_rnh(int af)
810 1.7 itojun {
811 1.7 itojun
812 1.7 itojun switch (af) {
813 1.7 itojun case AF_INET:
814 1.7 itojun return encap_head[0];
815 1.7 itojun #ifdef INET6
816 1.7 itojun case AF_INET6:
817 1.7 itojun return encap_head[1];
818 1.7 itojun #endif
819 1.7 itojun default:
820 1.7 itojun return NULL;
821 1.7 itojun }
822 1.7 itojun }
823 1.7 itojun
824 1.7 itojun static int
825 1.23 perry mask_matchlen(const struct sockaddr *sa)
826 1.7 itojun {
827 1.7 itojun const char *p, *ep;
828 1.7 itojun int l;
829 1.7 itojun
830 1.7 itojun p = (const char *)sa;
831 1.7 itojun ep = p + sa->sa_len;
832 1.7 itojun p += 2; /* sa_len + sa_family */
833 1.7 itojun
834 1.7 itojun l = 0;
835 1.7 itojun while (p < ep) {
836 1.7 itojun l += (*p ? 8 : 0); /* estimate */
837 1.7 itojun p++;
838 1.7 itojun }
839 1.7 itojun return l;
840 1.1 itojun }
841 1.55 knakahar #endif
842 1.55 knakahar
843 1.55 knakahar #ifndef USE_RADIX
844 1.55 knakahar static int
845 1.55 knakahar mask_match(const struct encaptab *ep,
846 1.55 knakahar const struct sockaddr *sp,
847 1.55 knakahar const struct sockaddr *dp)
848 1.55 knakahar {
849 1.55 knakahar struct sockaddr_storage s;
850 1.55 knakahar struct sockaddr_storage d;
851 1.55 knakahar int i;
852 1.55 knakahar const u_int8_t *p, *q;
853 1.55 knakahar u_int8_t *r;
854 1.55 knakahar int matchlen;
855 1.55 knakahar
856 1.55 knakahar KASSERTMSG(ep->func == NULL, "wrong encaptab passed to mask_match");
857 1.55 knakahar
858 1.55 knakahar if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
859 1.55 knakahar return 0;
860 1.55 knakahar if (sp->sa_family != ep->af || dp->sa_family != ep->af)
861 1.55 knakahar return 0;
862 1.55 knakahar if (sp->sa_len != ep->src->sa_len || dp->sa_len != ep->dst->sa_len)
863 1.55 knakahar return 0;
864 1.55 knakahar
865 1.55 knakahar matchlen = 0;
866 1.55 knakahar
867 1.55 knakahar p = (const u_int8_t *)sp;
868 1.55 knakahar q = (const u_int8_t *)ep->srcmask;
869 1.55 knakahar r = (u_int8_t *)&s;
870 1.55 knakahar for (i = 0 ; i < sp->sa_len; i++) {
871 1.55 knakahar r[i] = p[i] & q[i];
872 1.55 knakahar /* XXX estimate */
873 1.55 knakahar matchlen += (q[i] ? 8 : 0);
874 1.55 knakahar }
875 1.55 knakahar
876 1.55 knakahar p = (const u_int8_t *)dp;
877 1.55 knakahar q = (const u_int8_t *)ep->dstmask;
878 1.55 knakahar r = (u_int8_t *)&d;
879 1.55 knakahar for (i = 0 ; i < dp->sa_len; i++) {
880 1.55 knakahar r[i] = p[i] & q[i];
881 1.55 knakahar /* XXX rough estimate */
882 1.55 knakahar matchlen += (q[i] ? 8 : 0);
883 1.55 knakahar }
884 1.55 knakahar
885 1.55 knakahar /* need to overwrite len/family portion as we don't compare them */
886 1.55 knakahar s.ss_len = sp->sa_len;
887 1.55 knakahar s.ss_family = sp->sa_family;
888 1.55 knakahar d.ss_len = dp->sa_len;
889 1.55 knakahar d.ss_family = dp->sa_family;
890 1.55 knakahar
891 1.55 knakahar if (memcmp(&s, ep->src, ep->src->sa_len) == 0 &&
892 1.55 knakahar memcmp(&d, ep->dst, ep->dst->sa_len) == 0) {
893 1.55 knakahar return matchlen;
894 1.55 knakahar } else
895 1.55 knakahar return 0;
896 1.55 knakahar }
897 1.55 knakahar #endif
898 1.1 itojun
899 1.1 itojun static void
900 1.23 perry encap_fillarg(struct mbuf *m, const struct encaptab *ep)
901 1.1 itojun {
902 1.12 itojun struct m_tag *mtag;
903 1.1 itojun
904 1.54 knakahar KASSERT(rw_read_held(&encap_whole_lock));
905 1.54 knakahar
906 1.12 itojun mtag = m_tag_get(PACKET_TAG_ENCAP, sizeof(void *), M_NOWAIT);
907 1.12 itojun if (mtag) {
908 1.12 itojun *(void **)(mtag + 1) = ep->arg;
909 1.12 itojun m_tag_prepend(m, mtag);
910 1.1 itojun }
911 1.1 itojun }
912 1.1 itojun
913 1.1 itojun void *
914 1.23 perry encap_getarg(struct mbuf *m)
915 1.1 itojun {
916 1.1 itojun void *p;
917 1.12 itojun struct m_tag *mtag;
918 1.1 itojun
919 1.1 itojun p = NULL;
920 1.12 itojun mtag = m_tag_find(m, PACKET_TAG_ENCAP, NULL);
921 1.12 itojun if (mtag != NULL) {
922 1.13 itojun p = *(void **)(mtag + 1);
923 1.12 itojun m_tag_delete(m, mtag);
924 1.1 itojun }
925 1.1 itojun return p;
926 1.1 itojun }
927 1.54 knakahar
928 1.54 knakahar void
929 1.54 knakahar encap_lock_enter(void)
930 1.54 knakahar {
931 1.54 knakahar
932 1.54 knakahar /* XXX future work
933 1.54 knakahar * change interruptable lock.
934 1.54 knakahar */
935 1.54 knakahar KERNEL_LOCK(1, NULL);
936 1.54 knakahar }
937 1.54 knakahar
938 1.54 knakahar void
939 1.54 knakahar encap_lock_exit(void)
940 1.54 knakahar {
941 1.54 knakahar
942 1.54 knakahar /* XXX future work
943 1.54 knakahar * change interruptable lock
944 1.54 knakahar */
945 1.54 knakahar KERNEL_UNLOCK_ONE(NULL);
946 1.54 knakahar }
947