ip_encap.c revision 1.39.30.5 1 1.39.30.5 skrll /* $NetBSD: ip_encap.c,v 1.39.30.5 2016/05/29 08:44:38 skrll 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.39.30.1 skrll * 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.7 itojun
69 1.6 lukem #include <sys/cdefs.h>
70 1.39.30.5 skrll __KERNEL_RCSID(0, "$NetBSD: ip_encap.c,v 1.39.30.5 2016/05/29 08:44:38 skrll Exp $");
71 1.1 itojun
72 1.39.30.2 skrll #ifdef _KERNEL_OPT
73 1.4 itojun #include "opt_mrouting.h"
74 1.4 itojun #include "opt_inet.h"
75 1.39.30.2 skrll #endif
76 1.1 itojun
77 1.1 itojun #include <sys/param.h>
78 1.1 itojun #include <sys/systm.h>
79 1.1 itojun #include <sys/socket.h>
80 1.1 itojun #include <sys/sockio.h>
81 1.1 itojun #include <sys/mbuf.h>
82 1.1 itojun #include <sys/errno.h>
83 1.4 itojun #include <sys/queue.h>
84 1.39.30.3 skrll #include <sys/kmem.h>
85 1.1 itojun
86 1.1 itojun #include <net/if.h>
87 1.1 itojun
88 1.1 itojun #include <netinet/in.h>
89 1.1 itojun #include <netinet/in_systm.h>
90 1.1 itojun #include <netinet/ip.h>
91 1.1 itojun #include <netinet/ip_var.h>
92 1.1 itojun #include <netinet/ip_encap.h>
93 1.1 itojun #ifdef MROUTING
94 1.1 itojun #include <netinet/ip_mroute.h>
95 1.1 itojun #endif /* MROUTING */
96 1.1 itojun
97 1.1 itojun #ifdef INET6
98 1.1 itojun #include <netinet/ip6.h>
99 1.1 itojun #include <netinet6/ip6_var.h>
100 1.39.30.4 skrll #include <netinet6/ip6protosw.h> /* for struct ip6ctlparam */
101 1.7 itojun #include <netinet6/in6_var.h>
102 1.7 itojun #include <netinet6/in6_pcb.h>
103 1.7 itojun #include <netinet/icmp6.h>
104 1.1 itojun #endif
105 1.1 itojun
106 1.1 itojun #include <net/net_osdep.h>
107 1.1 itojun
108 1.7 itojun enum direction { INBOUND, OUTBOUND };
109 1.7 itojun
110 1.7 itojun #ifdef INET
111 1.22 perry static struct encaptab *encap4_lookup(struct mbuf *, int, int, enum direction);
112 1.7 itojun #endif
113 1.7 itojun #ifdef INET6
114 1.22 perry static struct encaptab *encap6_lookup(struct mbuf *, int, int, enum direction);
115 1.7 itojun #endif
116 1.22 perry static int encap_add(struct encaptab *);
117 1.22 perry static int encap_remove(struct encaptab *);
118 1.22 perry static int encap_afcheck(int, const struct sockaddr *, const struct sockaddr *);
119 1.22 perry static struct radix_node_head *encap_rnh(int);
120 1.22 perry static int mask_matchlen(const struct sockaddr *);
121 1.22 perry static void encap_fillarg(struct mbuf *, const struct encaptab *);
122 1.1 itojun
123 1.2 thorpej LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab);
124 1.1 itojun
125 1.7 itojun struct radix_node_head *encap_head[2]; /* 0 for AF_INET, 1 for AF_INET6 */
126 1.7 itojun
127 1.1 itojun void
128 1.23 perry encap_init(void)
129 1.1 itojun {
130 1.7 itojun static int initialized = 0;
131 1.7 itojun
132 1.7 itojun if (initialized)
133 1.7 itojun return;
134 1.7 itojun initialized++;
135 1.1 itojun #if 0
136 1.1 itojun /*
137 1.1 itojun * we cannot use LIST_INIT() here, since drivers may want to call
138 1.4 itojun * encap_attach(), on driver attach. encap_init() will be called
139 1.1 itojun * on AF_INET{,6} initialization, which happens after driver
140 1.1 itojun * initialization - using LIST_INIT() here can nuke encap_attach()
141 1.1 itojun * from drivers.
142 1.1 itojun */
143 1.1 itojun LIST_INIT(&encaptab);
144 1.1 itojun #endif
145 1.7 itojun
146 1.7 itojun /*
147 1.38 pooka * initialize radix lookup table when the radix subsystem is inited.
148 1.7 itojun */
149 1.38 pooka rn_delayedinit((void *)&encap_head[0],
150 1.38 pooka sizeof(struct sockaddr_pack) << 3);
151 1.7 itojun #ifdef INET6
152 1.38 pooka rn_delayedinit((void *)&encap_head[1],
153 1.38 pooka sizeof(struct sockaddr_pack) << 3);
154 1.7 itojun #endif
155 1.1 itojun }
156 1.1 itojun
157 1.4 itojun #ifdef INET
158 1.7 itojun static struct encaptab *
159 1.23 perry encap4_lookup(struct mbuf *m, int off, int proto, enum direction dir)
160 1.1 itojun {
161 1.1 itojun struct ip *ip;
162 1.33 pooka struct ip_pack4 pack;
163 1.1 itojun struct encaptab *ep, *match;
164 1.1 itojun int prio, matchprio;
165 1.7 itojun struct radix_node_head *rnh = encap_rnh(AF_INET);
166 1.7 itojun struct radix_node *rn;
167 1.1 itojun
168 1.39.30.1 skrll KASSERT(m->m_len >= sizeof(*ip));
169 1.39.30.1 skrll
170 1.1 itojun ip = mtod(m, struct ip *);
171 1.1 itojun
172 1.35 cegger memset(&pack, 0, sizeof(pack));
173 1.7 itojun pack.p.sp_len = sizeof(pack);
174 1.7 itojun pack.mine.sin_family = pack.yours.sin_family = AF_INET;
175 1.7 itojun pack.mine.sin_len = pack.yours.sin_len = sizeof(struct sockaddr_in);
176 1.7 itojun if (dir == INBOUND) {
177 1.7 itojun pack.mine.sin_addr = ip->ip_dst;
178 1.7 itojun pack.yours.sin_addr = ip->ip_src;
179 1.7 itojun } else {
180 1.7 itojun pack.mine.sin_addr = ip->ip_src;
181 1.7 itojun pack.yours.sin_addr = ip->ip_dst;
182 1.7 itojun }
183 1.1 itojun
184 1.1 itojun match = NULL;
185 1.1 itojun matchprio = 0;
186 1.7 itojun
187 1.30 christos rn = rnh->rnh_matchaddr((void *)&pack, rnh);
188 1.7 itojun if (rn && (rn->rn_flags & RNF_ROOT) == 0) {
189 1.7 itojun match = (struct encaptab *)rn;
190 1.7 itojun matchprio = mask_matchlen(match->srcmask) +
191 1.7 itojun mask_matchlen(match->dstmask);
192 1.7 itojun }
193 1.7 itojun
194 1.31 dyoung LIST_FOREACH(ep, &encaptab, chain) {
195 1.1 itojun if (ep->af != AF_INET)
196 1.1 itojun continue;
197 1.1 itojun if (ep->proto >= 0 && ep->proto != proto)
198 1.1 itojun continue;
199 1.1 itojun if (ep->func)
200 1.1 itojun prio = (*ep->func)(m, off, proto, ep->arg);
201 1.39.30.1 skrll else
202 1.7 itojun continue;
203 1.1 itojun
204 1.1 itojun /*
205 1.1 itojun * We prioritize the matches by using bit length of the
206 1.1 itojun * matches. mask_match() and user-supplied matching function
207 1.1 itojun * should return the bit length of the matches (for example,
208 1.1 itojun * if both src/dst are matched for IPv4, 64 should be returned).
209 1.1 itojun * 0 or negative return value means "it did not match".
210 1.1 itojun *
211 1.1 itojun * The question is, since we have two "mask" portion, we
212 1.1 itojun * cannot really define total order between entries.
213 1.1 itojun * For example, which of these should be preferred?
214 1.1 itojun * mask_match() returns 48 (32 + 16) for both of them.
215 1.1 itojun * src=3ffe::/16, dst=3ffe:501::/32
216 1.1 itojun * src=3ffe:501::/32, dst=3ffe::/16
217 1.1 itojun *
218 1.1 itojun * We need to loop through all the possible candidates
219 1.1 itojun * to get the best match - the search takes O(n) for
220 1.1 itojun * n attachments (i.e. interfaces).
221 1.7 itojun *
222 1.7 itojun * For radix-based lookup, I guess source takes precedence.
223 1.7 itojun * See rn_{refines,lexobetter} for the correct answer.
224 1.1 itojun */
225 1.1 itojun if (prio <= 0)
226 1.1 itojun continue;
227 1.1 itojun if (prio > matchprio) {
228 1.1 itojun matchprio = prio;
229 1.1 itojun match = ep;
230 1.1 itojun }
231 1.1 itojun }
232 1.1 itojun
233 1.7 itojun return match;
234 1.7 itojun }
235 1.7 itojun
236 1.7 itojun void
237 1.7 itojun encap4_input(struct mbuf *m, ...)
238 1.7 itojun {
239 1.7 itojun int off, proto;
240 1.7 itojun va_list ap;
241 1.39.30.4 skrll const struct encapsw *esw;
242 1.7 itojun struct encaptab *match;
243 1.7 itojun
244 1.7 itojun va_start(ap, m);
245 1.7 itojun off = va_arg(ap, int);
246 1.7 itojun proto = va_arg(ap, int);
247 1.7 itojun va_end(ap);
248 1.7 itojun
249 1.7 itojun match = encap4_lookup(m, off, proto, INBOUND);
250 1.7 itojun
251 1.1 itojun if (match) {
252 1.1 itojun /* found a match, "match" has the best one */
253 1.39.30.4 skrll esw = match->esw;
254 1.39.30.4 skrll if (esw && esw->encapsw4.pr_input) {
255 1.1 itojun encap_fillarg(m, match);
256 1.39.30.4 skrll (*esw->encapsw4.pr_input)(m, off, proto);
257 1.1 itojun } else
258 1.1 itojun m_freem(m);
259 1.1 itojun return;
260 1.1 itojun }
261 1.1 itojun
262 1.1 itojun /* last resort: inject to raw socket */
263 1.1 itojun rip_input(m, off, proto);
264 1.1 itojun }
265 1.1 itojun #endif
266 1.1 itojun
267 1.1 itojun #ifdef INET6
268 1.7 itojun static struct encaptab *
269 1.23 perry encap6_lookup(struct mbuf *m, int off, int proto, enum direction dir)
270 1.1 itojun {
271 1.1 itojun struct ip6_hdr *ip6;
272 1.33 pooka struct ip_pack6 pack;
273 1.7 itojun int prio, matchprio;
274 1.1 itojun struct encaptab *ep, *match;
275 1.7 itojun struct radix_node_head *rnh = encap_rnh(AF_INET6);
276 1.7 itojun struct radix_node *rn;
277 1.1 itojun
278 1.39.30.1 skrll KASSERT(m->m_len >= sizeof(*ip6));
279 1.39.30.1 skrll
280 1.1 itojun ip6 = mtod(m, struct ip6_hdr *);
281 1.1 itojun
282 1.35 cegger memset(&pack, 0, sizeof(pack));
283 1.7 itojun pack.p.sp_len = sizeof(pack);
284 1.7 itojun pack.mine.sin6_family = pack.yours.sin6_family = AF_INET6;
285 1.7 itojun pack.mine.sin6_len = pack.yours.sin6_len = sizeof(struct sockaddr_in6);
286 1.7 itojun if (dir == INBOUND) {
287 1.7 itojun pack.mine.sin6_addr = ip6->ip6_dst;
288 1.7 itojun pack.yours.sin6_addr = ip6->ip6_src;
289 1.7 itojun } else {
290 1.7 itojun pack.mine.sin6_addr = ip6->ip6_src;
291 1.7 itojun pack.yours.sin6_addr = ip6->ip6_dst;
292 1.7 itojun }
293 1.1 itojun
294 1.1 itojun match = NULL;
295 1.1 itojun matchprio = 0;
296 1.7 itojun
297 1.30 christos rn = rnh->rnh_matchaddr((void *)&pack, rnh);
298 1.7 itojun if (rn && (rn->rn_flags & RNF_ROOT) == 0) {
299 1.7 itojun match = (struct encaptab *)rn;
300 1.7 itojun matchprio = mask_matchlen(match->srcmask) +
301 1.7 itojun mask_matchlen(match->dstmask);
302 1.7 itojun }
303 1.7 itojun
304 1.31 dyoung LIST_FOREACH(ep, &encaptab, chain) {
305 1.1 itojun if (ep->af != AF_INET6)
306 1.1 itojun continue;
307 1.1 itojun if (ep->proto >= 0 && ep->proto != proto)
308 1.1 itojun continue;
309 1.1 itojun if (ep->func)
310 1.7 itojun prio = (*ep->func)(m, off, proto, ep->arg);
311 1.39.30.1 skrll else
312 1.7 itojun continue;
313 1.1 itojun
314 1.7 itojun /* see encap4_lookup() for issues here */
315 1.1 itojun if (prio <= 0)
316 1.1 itojun continue;
317 1.1 itojun if (prio > matchprio) {
318 1.1 itojun matchprio = prio;
319 1.1 itojun match = ep;
320 1.1 itojun }
321 1.1 itojun }
322 1.1 itojun
323 1.7 itojun return match;
324 1.7 itojun }
325 1.7 itojun
326 1.7 itojun int
327 1.23 perry encap6_input(struct mbuf **mp, int *offp, int proto)
328 1.7 itojun {
329 1.7 itojun struct mbuf *m = *mp;
330 1.39.30.4 skrll const struct encapsw *esw;
331 1.7 itojun struct encaptab *match;
332 1.7 itojun
333 1.7 itojun match = encap6_lookup(m, *offp, proto, INBOUND);
334 1.7 itojun
335 1.1 itojun if (match) {
336 1.1 itojun /* found a match */
337 1.39.30.4 skrll esw = match->esw;
338 1.39.30.4 skrll if (esw && esw->encapsw6.pr_input) {
339 1.1 itojun encap_fillarg(m, match);
340 1.39.30.4 skrll return (*esw->encapsw6.pr_input)(mp, offp, proto);
341 1.1 itojun } else {
342 1.1 itojun m_freem(m);
343 1.1 itojun return IPPROTO_DONE;
344 1.1 itojun }
345 1.1 itojun }
346 1.1 itojun
347 1.1 itojun /* last resort: inject to raw socket */
348 1.1 itojun return rip6_input(mp, offp, proto);
349 1.1 itojun }
350 1.1 itojun #endif
351 1.1 itojun
352 1.7 itojun static int
353 1.23 perry encap_add(struct encaptab *ep)
354 1.1 itojun {
355 1.7 itojun struct radix_node_head *rnh = encap_rnh(ep->af);
356 1.7 itojun int error = 0;
357 1.1 itojun
358 1.1 itojun LIST_INSERT_HEAD(&encaptab, ep, chain);
359 1.7 itojun if (!ep->func && rnh) {
360 1.30 christos if (!rnh->rnh_addaddr((void *)ep->addrpack,
361 1.30 christos (void *)ep->maskpack, rnh, ep->nodes)) {
362 1.7 itojun error = EEXIST;
363 1.7 itojun goto fail;
364 1.7 itojun }
365 1.7 itojun }
366 1.7 itojun return error;
367 1.7 itojun
368 1.7 itojun fail:
369 1.7 itojun LIST_REMOVE(ep, chain);
370 1.7 itojun return error;
371 1.7 itojun }
372 1.7 itojun
373 1.7 itojun static int
374 1.23 perry encap_remove(struct encaptab *ep)
375 1.7 itojun {
376 1.7 itojun struct radix_node_head *rnh = encap_rnh(ep->af);
377 1.7 itojun int error = 0;
378 1.7 itojun
379 1.7 itojun LIST_REMOVE(ep, chain);
380 1.7 itojun if (!ep->func && rnh) {
381 1.30 christos if (!rnh->rnh_deladdr((void *)ep->addrpack,
382 1.30 christos (void *)ep->maskpack, rnh))
383 1.7 itojun error = ESRCH;
384 1.7 itojun }
385 1.7 itojun return error;
386 1.7 itojun }
387 1.7 itojun
388 1.7 itojun static int
389 1.23 perry encap_afcheck(int af, const struct sockaddr *sp, const struct sockaddr *dp)
390 1.7 itojun {
391 1.7 itojun if (sp && dp) {
392 1.7 itojun if (sp->sa_len != dp->sa_len)
393 1.7 itojun return EINVAL;
394 1.7 itojun if (af != sp->sa_family || af != dp->sa_family)
395 1.7 itojun return EINVAL;
396 1.7 itojun } else if (!sp && !dp)
397 1.7 itojun ;
398 1.7 itojun else
399 1.7 itojun return EINVAL;
400 1.7 itojun
401 1.7 itojun switch (af) {
402 1.7 itojun case AF_INET:
403 1.7 itojun if (sp && sp->sa_len != sizeof(struct sockaddr_in))
404 1.7 itojun return EINVAL;
405 1.7 itojun if (dp && dp->sa_len != sizeof(struct sockaddr_in))
406 1.7 itojun return EINVAL;
407 1.7 itojun break;
408 1.7 itojun #ifdef INET6
409 1.7 itojun case AF_INET6:
410 1.7 itojun if (sp && sp->sa_len != sizeof(struct sockaddr_in6))
411 1.7 itojun return EINVAL;
412 1.7 itojun if (dp && dp->sa_len != sizeof(struct sockaddr_in6))
413 1.7 itojun return EINVAL;
414 1.7 itojun break;
415 1.7 itojun #endif
416 1.7 itojun default:
417 1.7 itojun return EAFNOSUPPORT;
418 1.7 itojun }
419 1.7 itojun
420 1.7 itojun return 0;
421 1.1 itojun }
422 1.1 itojun
423 1.1 itojun /*
424 1.1 itojun * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
425 1.1 itojun * length of mask (sm and dm) is assumed to be same as sp/dp.
426 1.1 itojun * Return value will be necessary as input (cookie) for encap_detach().
427 1.1 itojun */
428 1.1 itojun const struct encaptab *
429 1.23 perry encap_attach(int af, int proto,
430 1.23 perry const struct sockaddr *sp, const struct sockaddr *sm,
431 1.23 perry const struct sockaddr *dp, const struct sockaddr *dm,
432 1.39.30.4 skrll const struct encapsw *esw, void *arg)
433 1.1 itojun {
434 1.1 itojun struct encaptab *ep;
435 1.1 itojun int error;
436 1.1 itojun int s;
437 1.7 itojun size_t l;
438 1.33 pooka struct ip_pack4 *pack4;
439 1.7 itojun #ifdef INET6
440 1.33 pooka struct ip_pack6 *pack6;
441 1.7 itojun #endif
442 1.1 itojun
443 1.1 itojun s = splsoftnet();
444 1.1 itojun /* sanity check on args */
445 1.7 itojun error = encap_afcheck(af, sp, dp);
446 1.7 itojun if (error)
447 1.1 itojun goto fail;
448 1.1 itojun
449 1.1 itojun /* check if anyone have already attached with exactly same config */
450 1.31 dyoung LIST_FOREACH(ep, &encaptab, chain) {
451 1.1 itojun if (ep->af != af)
452 1.1 itojun continue;
453 1.1 itojun if (ep->proto != proto)
454 1.1 itojun continue;
455 1.7 itojun if (ep->func)
456 1.7 itojun continue;
457 1.39.30.1 skrll
458 1.39.30.1 skrll KASSERT(ep->src != NULL);
459 1.39.30.1 skrll KASSERT(ep->dst != NULL);
460 1.39.30.1 skrll KASSERT(ep->srcmask != NULL);
461 1.39.30.1 skrll KASSERT(ep->dstmask != NULL);
462 1.39.30.1 skrll
463 1.7 itojun if (ep->src->sa_len != sp->sa_len ||
464 1.34 cegger memcmp(ep->src, sp, sp->sa_len) != 0 ||
465 1.34 cegger memcmp(ep->srcmask, sm, sp->sa_len) != 0)
466 1.7 itojun continue;
467 1.7 itojun if (ep->dst->sa_len != dp->sa_len ||
468 1.34 cegger memcmp(ep->dst, dp, dp->sa_len) != 0 ||
469 1.34 cegger memcmp(ep->dstmask, dm, dp->sa_len) != 0)
470 1.1 itojun continue;
471 1.1 itojun
472 1.1 itojun error = EEXIST;
473 1.1 itojun goto fail;
474 1.1 itojun }
475 1.3 thorpej
476 1.7 itojun switch (af) {
477 1.7 itojun case AF_INET:
478 1.7 itojun l = sizeof(*pack4);
479 1.7 itojun break;
480 1.7 itojun #ifdef INET6
481 1.7 itojun case AF_INET6:
482 1.7 itojun l = sizeof(*pack6);
483 1.7 itojun break;
484 1.7 itojun #endif
485 1.7 itojun default:
486 1.7 itojun goto fail;
487 1.7 itojun }
488 1.7 itojun
489 1.20 itojun /* M_NETADDR ok? */
490 1.39.30.3 skrll ep = kmem_zalloc(sizeof(*ep), KM_NOSLEEP);
491 1.1 itojun if (ep == NULL) {
492 1.1 itojun error = ENOBUFS;
493 1.1 itojun goto fail;
494 1.1 itojun }
495 1.39.30.3 skrll ep->addrpack = kmem_zalloc(l, KM_NOSLEEP);
496 1.7 itojun if (ep->addrpack == NULL) {
497 1.7 itojun error = ENOBUFS;
498 1.7 itojun goto gc;
499 1.7 itojun }
500 1.39.30.3 skrll ep->maskpack = kmem_zalloc(l, KM_NOSLEEP);
501 1.7 itojun if (ep->maskpack == NULL) {
502 1.7 itojun error = ENOBUFS;
503 1.7 itojun goto gc;
504 1.7 itojun }
505 1.1 itojun
506 1.1 itojun ep->af = af;
507 1.1 itojun ep->proto = proto;
508 1.7 itojun ep->addrpack->sa_len = l & 0xff;
509 1.7 itojun ep->maskpack->sa_len = l & 0xff;
510 1.7 itojun switch (af) {
511 1.7 itojun case AF_INET:
512 1.33 pooka pack4 = (struct ip_pack4 *)ep->addrpack;
513 1.7 itojun ep->src = (struct sockaddr *)&pack4->mine;
514 1.7 itojun ep->dst = (struct sockaddr *)&pack4->yours;
515 1.33 pooka pack4 = (struct ip_pack4 *)ep->maskpack;
516 1.7 itojun ep->srcmask = (struct sockaddr *)&pack4->mine;
517 1.7 itojun ep->dstmask = (struct sockaddr *)&pack4->yours;
518 1.7 itojun break;
519 1.7 itojun #ifdef INET6
520 1.7 itojun case AF_INET6:
521 1.33 pooka pack6 = (struct ip_pack6 *)ep->addrpack;
522 1.7 itojun ep->src = (struct sockaddr *)&pack6->mine;
523 1.7 itojun ep->dst = (struct sockaddr *)&pack6->yours;
524 1.33 pooka pack6 = (struct ip_pack6 *)ep->maskpack;
525 1.7 itojun ep->srcmask = (struct sockaddr *)&pack6->mine;
526 1.7 itojun ep->dstmask = (struct sockaddr *)&pack6->yours;
527 1.7 itojun break;
528 1.7 itojun #endif
529 1.7 itojun }
530 1.7 itojun
531 1.37 tsutsui memcpy(ep->src, sp, sp->sa_len);
532 1.37 tsutsui memcpy(ep->srcmask, sm, sp->sa_len);
533 1.37 tsutsui memcpy(ep->dst, dp, dp->sa_len);
534 1.37 tsutsui memcpy(ep->dstmask, dm, dp->sa_len);
535 1.39.30.4 skrll ep->esw = esw;
536 1.1 itojun ep->arg = arg;
537 1.1 itojun
538 1.7 itojun error = encap_add(ep);
539 1.7 itojun if (error)
540 1.7 itojun goto gc;
541 1.1 itojun
542 1.1 itojun error = 0;
543 1.1 itojun splx(s);
544 1.1 itojun return ep;
545 1.1 itojun
546 1.7 itojun gc:
547 1.7 itojun if (ep->addrpack)
548 1.39.30.3 skrll kmem_free(ep->addrpack, l);
549 1.7 itojun if (ep->maskpack)
550 1.39.30.3 skrll kmem_free(ep->maskpack, l);
551 1.7 itojun if (ep)
552 1.39.30.3 skrll kmem_free(ep, sizeof(*ep));
553 1.1 itojun fail:
554 1.1 itojun splx(s);
555 1.1 itojun return NULL;
556 1.1 itojun }
557 1.1 itojun
558 1.1 itojun const struct encaptab *
559 1.23 perry encap_attach_func(int af, int proto,
560 1.26 martin int (*func)(struct mbuf *, int, int, void *),
561 1.39.30.4 skrll const struct encapsw *esw, void *arg)
562 1.1 itojun {
563 1.1 itojun struct encaptab *ep;
564 1.1 itojun int error;
565 1.1 itojun int s;
566 1.1 itojun
567 1.1 itojun s = splsoftnet();
568 1.1 itojun /* sanity check on args */
569 1.1 itojun if (!func) {
570 1.1 itojun error = EINVAL;
571 1.1 itojun goto fail;
572 1.1 itojun }
573 1.1 itojun
574 1.7 itojun error = encap_afcheck(af, NULL, NULL);
575 1.7 itojun if (error)
576 1.7 itojun goto fail;
577 1.7 itojun
578 1.39.30.3 skrll ep = kmem_alloc(sizeof(*ep), KM_NOSLEEP); /*XXX*/
579 1.1 itojun if (ep == NULL) {
580 1.1 itojun error = ENOBUFS;
581 1.1 itojun goto fail;
582 1.1 itojun }
583 1.35 cegger memset(ep, 0, sizeof(*ep));
584 1.1 itojun
585 1.1 itojun ep->af = af;
586 1.1 itojun ep->proto = proto;
587 1.1 itojun ep->func = func;
588 1.39.30.4 skrll ep->esw = esw;
589 1.1 itojun ep->arg = arg;
590 1.1 itojun
591 1.7 itojun error = encap_add(ep);
592 1.7 itojun if (error)
593 1.7 itojun goto fail;
594 1.1 itojun
595 1.1 itojun error = 0;
596 1.1 itojun splx(s);
597 1.1 itojun return ep;
598 1.1 itojun
599 1.1 itojun fail:
600 1.1 itojun splx(s);
601 1.1 itojun return NULL;
602 1.1 itojun }
603 1.1 itojun
604 1.7 itojun /* XXX encap4_ctlinput() is necessary if we set DF=1 on outer IPv4 header */
605 1.7 itojun
606 1.7 itojun #ifdef INET6
607 1.32 ad void *
608 1.29 dyoung encap6_ctlinput(int cmd, const struct sockaddr *sa, void *d0)
609 1.7 itojun {
610 1.7 itojun void *d = d0;
611 1.7 itojun struct ip6_hdr *ip6;
612 1.7 itojun struct mbuf *m;
613 1.7 itojun int off;
614 1.7 itojun struct ip6ctlparam *ip6cp = NULL;
615 1.7 itojun int nxt;
616 1.7 itojun struct encaptab *ep;
617 1.39.30.4 skrll const struct encapsw *esw;
618 1.7 itojun
619 1.7 itojun if (sa->sa_family != AF_INET6 ||
620 1.7 itojun sa->sa_len != sizeof(struct sockaddr_in6))
621 1.32 ad return NULL;
622 1.7 itojun
623 1.7 itojun if ((unsigned)cmd >= PRC_NCMDS)
624 1.32 ad return NULL;
625 1.7 itojun if (cmd == PRC_HOSTDEAD)
626 1.7 itojun d = NULL;
627 1.7 itojun else if (cmd == PRC_MSGSIZE)
628 1.7 itojun ; /* special code is present, see below */
629 1.7 itojun else if (inet6ctlerrmap[cmd] == 0)
630 1.32 ad return NULL;
631 1.7 itojun
632 1.7 itojun /* if the parameter is from icmp6, decode it. */
633 1.7 itojun if (d != NULL) {
634 1.7 itojun ip6cp = (struct ip6ctlparam *)d;
635 1.7 itojun m = ip6cp->ip6c_m;
636 1.7 itojun ip6 = ip6cp->ip6c_ip6;
637 1.7 itojun off = ip6cp->ip6c_off;
638 1.7 itojun nxt = ip6cp->ip6c_nxt;
639 1.15 mycroft
640 1.15 mycroft if (ip6 && cmd == PRC_MSGSIZE) {
641 1.15 mycroft int valid = 0;
642 1.15 mycroft struct encaptab *match;
643 1.15 mycroft
644 1.15 mycroft /*
645 1.15 mycroft * Check to see if we have a valid encap configuration.
646 1.15 mycroft */
647 1.15 mycroft match = encap6_lookup(m, off, nxt, OUTBOUND);
648 1.15 mycroft if (match)
649 1.15 mycroft valid++;
650 1.15 mycroft
651 1.15 mycroft /*
652 1.15 mycroft * Depending on the value of "valid" and routing table
653 1.15 mycroft * size (mtudisc_{hi,lo}wat), we will:
654 1.15 mycroft * - recalcurate the new MTU and create the
655 1.15 mycroft * corresponding routing entry, or
656 1.15 mycroft * - ignore the MTU change notification.
657 1.15 mycroft */
658 1.15 mycroft icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
659 1.15 mycroft }
660 1.7 itojun } else {
661 1.7 itojun m = NULL;
662 1.7 itojun ip6 = NULL;
663 1.7 itojun nxt = -1;
664 1.7 itojun }
665 1.7 itojun
666 1.7 itojun /* inform all listeners */
667 1.31 dyoung LIST_FOREACH(ep, &encaptab, chain) {
668 1.7 itojun if (ep->af != AF_INET6)
669 1.7 itojun continue;
670 1.7 itojun if (ep->proto >= 0 && ep->proto != nxt)
671 1.7 itojun continue;
672 1.7 itojun
673 1.7 itojun /* should optimize by looking at address pairs */
674 1.7 itojun
675 1.7 itojun /* XXX need to pass ep->arg or ep itself to listeners */
676 1.39.30.4 skrll esw = ep->esw;
677 1.39.30.4 skrll if (esw && esw->encapsw6.pr_ctlinput) {
678 1.39.30.4 skrll (*esw->encapsw6.pr_ctlinput)(cmd, sa, d, ep->arg);
679 1.39.30.4 skrll }
680 1.7 itojun }
681 1.7 itojun
682 1.7 itojun rip6_ctlinput(cmd, sa, d0);
683 1.32 ad return NULL;
684 1.7 itojun }
685 1.7 itojun #endif
686 1.7 itojun
687 1.1 itojun int
688 1.23 perry encap_detach(const struct encaptab *cookie)
689 1.1 itojun {
690 1.1 itojun const struct encaptab *ep = cookie;
691 1.39.30.1 skrll struct encaptab *p, *np;
692 1.7 itojun int error;
693 1.1 itojun
694 1.39.30.1 skrll LIST_FOREACH_SAFE(p, &encaptab, chain, np) {
695 1.1 itojun if (p == ep) {
696 1.7 itojun error = encap_remove(p);
697 1.7 itojun if (error)
698 1.7 itojun return error;
699 1.7 itojun if (!ep->func) {
700 1.39.30.3 skrll kmem_free(p->addrpack, ep->addrpack->sa_len);
701 1.39.30.3 skrll kmem_free(p->maskpack, ep->maskpack->sa_len);
702 1.7 itojun }
703 1.39.30.3 skrll kmem_free(p, sizeof(*p)); /*XXX*/
704 1.1 itojun return 0;
705 1.1 itojun }
706 1.1 itojun }
707 1.1 itojun
708 1.7 itojun return ENOENT;
709 1.7 itojun }
710 1.7 itojun
711 1.7 itojun static struct radix_node_head *
712 1.23 perry encap_rnh(int af)
713 1.7 itojun {
714 1.7 itojun
715 1.7 itojun switch (af) {
716 1.7 itojun case AF_INET:
717 1.7 itojun return encap_head[0];
718 1.7 itojun #ifdef INET6
719 1.7 itojun case AF_INET6:
720 1.7 itojun return encap_head[1];
721 1.7 itojun #endif
722 1.7 itojun default:
723 1.7 itojun return NULL;
724 1.7 itojun }
725 1.7 itojun }
726 1.7 itojun
727 1.7 itojun static int
728 1.23 perry mask_matchlen(const struct sockaddr *sa)
729 1.7 itojun {
730 1.7 itojun const char *p, *ep;
731 1.7 itojun int l;
732 1.7 itojun
733 1.7 itojun p = (const char *)sa;
734 1.7 itojun ep = p + sa->sa_len;
735 1.7 itojun p += 2; /* sa_len + sa_family */
736 1.7 itojun
737 1.7 itojun l = 0;
738 1.7 itojun while (p < ep) {
739 1.7 itojun l += (*p ? 8 : 0); /* estimate */
740 1.7 itojun p++;
741 1.7 itojun }
742 1.7 itojun return l;
743 1.1 itojun }
744 1.1 itojun
745 1.1 itojun static void
746 1.23 perry encap_fillarg(struct mbuf *m, const struct encaptab *ep)
747 1.1 itojun {
748 1.12 itojun struct m_tag *mtag;
749 1.1 itojun
750 1.12 itojun mtag = m_tag_get(PACKET_TAG_ENCAP, sizeof(void *), M_NOWAIT);
751 1.12 itojun if (mtag) {
752 1.12 itojun *(void **)(mtag + 1) = ep->arg;
753 1.12 itojun m_tag_prepend(m, mtag);
754 1.1 itojun }
755 1.1 itojun }
756 1.1 itojun
757 1.1 itojun void *
758 1.23 perry encap_getarg(struct mbuf *m)
759 1.1 itojun {
760 1.1 itojun void *p;
761 1.12 itojun struct m_tag *mtag;
762 1.1 itojun
763 1.1 itojun p = NULL;
764 1.12 itojun mtag = m_tag_find(m, PACKET_TAG_ENCAP, NULL);
765 1.12 itojun if (mtag != NULL) {
766 1.13 itojun p = *(void **)(mtag + 1);
767 1.12 itojun m_tag_delete(m, mtag);
768 1.1 itojun }
769 1.1 itojun return p;
770 1.1 itojun }
771