Deleted Added
full compact
ip_encap.c (201145) ip_encap.c (269699)
1/* $KAME: ip_encap.c,v 1.41 2001/03/15 08:35:08 itojun Exp $ */
2
3/*-
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31/*
32 * My grandfather said that there's a devil inside tunnelling technology...
33 *
34 * We have surprisingly many protocols that want packets with IP protocol
35 * #4 or #41. Here's a list of protocols that want protocol #41:
36 * RFC1933 configured tunnel
37 * RFC1933 automatic tunnel
38 * RFC2401 IPsec tunnel
39 * RFC2473 IPv6 generic packet tunnelling
40 * RFC2529 6over4 tunnel
41 * mobile-ip6 (uses RFC2473)
42 * RFC3056 6to4 tunnel
43 * isatap tunnel
44 * Here's a list of protocol that want protocol #4:
45 * RFC1853 IPv4-in-IPv4 tunnelling
46 * RFC2003 IPv4 encapsulation within IPv4
47 * RFC2344 reverse tunnelling for mobile-ip4
48 * RFC2401 IPsec tunnel
49 * Well, what can I say. They impose different en/decapsulation mechanism
50 * from each other, so they need separate protocol handler. The only one
51 * we can easily determine by protocol # is IPsec, which always has
52 * AH/ESP/IPComp header right after outer IP header.
53 *
54 * So, clearly good old protosw does not work for protocol #4 and #41.
55 * The code will let you match protocol via src/dst address pair.
56 */
57/* XXX is M_NETADDR correct? */
58
59#include <sys/cdefs.h>
1/* $KAME: ip_encap.c,v 1.41 2001/03/15 08:35:08 itojun Exp $ */
2
3/*-
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31/*
32 * My grandfather said that there's a devil inside tunnelling technology...
33 *
34 * We have surprisingly many protocols that want packets with IP protocol
35 * #4 or #41. Here's a list of protocols that want protocol #41:
36 * RFC1933 configured tunnel
37 * RFC1933 automatic tunnel
38 * RFC2401 IPsec tunnel
39 * RFC2473 IPv6 generic packet tunnelling
40 * RFC2529 6over4 tunnel
41 * mobile-ip6 (uses RFC2473)
42 * RFC3056 6to4 tunnel
43 * isatap tunnel
44 * Here's a list of protocol that want protocol #4:
45 * RFC1853 IPv4-in-IPv4 tunnelling
46 * RFC2003 IPv4 encapsulation within IPv4
47 * RFC2344 reverse tunnelling for mobile-ip4
48 * RFC2401 IPsec tunnel
49 * Well, what can I say. They impose different en/decapsulation mechanism
50 * from each other, so they need separate protocol handler. The only one
51 * we can easily determine by protocol # is IPsec, which always has
52 * AH/ESP/IPComp header right after outer IP header.
53 *
54 * So, clearly good old protosw does not work for protocol #4 and #41.
55 * The code will let you match protocol via src/dst address pair.
56 */
57/* XXX is M_NETADDR correct? */
58
59#include <sys/cdefs.h>
60__FBSDID("$FreeBSD: head/sys/netinet/ip_encap.c 201145 2009-12-28 22:56:30Z antoine $");
60__FBSDID("$FreeBSD: head/sys/netinet/ip_encap.c 269699 2014-08-08 01:57:15Z kevlo $");
61
62#include "opt_mrouting.h"
63#include "opt_inet.h"
64#include "opt_inet6.h"
65
66#include <sys/param.h>
67#include <sys/systm.h>
68#include <sys/socket.h>
69#include <sys/sockio.h>
70#include <sys/mbuf.h>
71#include <sys/errno.h>
72#include <sys/protosw.h>
73#include <sys/queue.h>
74
75#include <net/if.h>
76#include <net/route.h>
77
78#include <netinet/in.h>
79#include <netinet/in_systm.h>
80#include <netinet/ip.h>
81#include <netinet/ip_var.h>
82#include <netinet/ip_encap.h>
83
84#ifdef INET6
85#include <netinet/ip6.h>
86#include <netinet6/ip6_var.h>
61
62#include "opt_mrouting.h"
63#include "opt_inet.h"
64#include "opt_inet6.h"
65
66#include <sys/param.h>
67#include <sys/systm.h>
68#include <sys/socket.h>
69#include <sys/sockio.h>
70#include <sys/mbuf.h>
71#include <sys/errno.h>
72#include <sys/protosw.h>
73#include <sys/queue.h>
74
75#include <net/if.h>
76#include <net/route.h>
77
78#include <netinet/in.h>
79#include <netinet/in_systm.h>
80#include <netinet/ip.h>
81#include <netinet/ip_var.h>
82#include <netinet/ip_encap.h>
83
84#ifdef INET6
85#include <netinet/ip6.h>
86#include <netinet6/ip6_var.h>
87#include <netinet6/ip6protosw.h>
88#endif
89
90#include <machine/stdarg.h>
91
92#include <sys/kernel.h>
93#include <sys/malloc.h>
94static MALLOC_DEFINE(M_NETADDR, "encap_export_host", "Export host address structure");
95
96static void encap_add(struct encaptab *);
97static int mask_match(const struct encaptab *, const struct sockaddr *,
98 const struct sockaddr *);
99static void encap_fillarg(struct mbuf *, const struct encaptab *);
100
101/*
102 * All global variables in ip_encap.c are locked using encapmtx.
103 */
104static struct mtx encapmtx;
105MTX_SYSINIT(encapmtx, &encapmtx, "encapmtx", MTX_DEF);
106LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(encaptab);
107
108/*
109 * We currently keey encap_init() for source code compatibility reasons --
110 * it's referenced by KAME pieces in netinet6.
111 */
112void
113encap_init(void)
114{
115}
116
117#ifdef INET
87#endif
88
89#include <machine/stdarg.h>
90
91#include <sys/kernel.h>
92#include <sys/malloc.h>
93static MALLOC_DEFINE(M_NETADDR, "encap_export_host", "Export host address structure");
94
95static void encap_add(struct encaptab *);
96static int mask_match(const struct encaptab *, const struct sockaddr *,
97 const struct sockaddr *);
98static void encap_fillarg(struct mbuf *, const struct encaptab *);
99
100/*
101 * All global variables in ip_encap.c are locked using encapmtx.
102 */
103static struct mtx encapmtx;
104MTX_SYSINIT(encapmtx, &encapmtx, "encapmtx", MTX_DEF);
105LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(encaptab);
106
107/*
108 * We currently keey encap_init() for source code compatibility reasons --
109 * it's referenced by KAME pieces in netinet6.
110 */
111void
112encap_init(void)
113{
114}
115
116#ifdef INET
118void
119encap4_input(struct mbuf *m, int off)
117int
118encap4_input(struct mbuf **mp, int *offp, int proto)
120{
121 struct ip *ip;
119{
120 struct ip *ip;
122 int proto;
121 struct mbuf *m;
123 struct sockaddr_in s, d;
124 const struct protosw *psw;
125 struct encaptab *ep, *match;
122 struct sockaddr_in s, d;
123 const struct protosw *psw;
124 struct encaptab *ep, *match;
126 int prio, matchprio;
125 int matchprio, off, prio;
127
126
127 m = *mp;
128 off = *offp;
128 ip = mtod(m, struct ip *);
129 ip = mtod(m, struct ip *);
129 proto = ip->ip_p;
130 *mp = NULL;
130
131 bzero(&s, sizeof(s));
132 s.sin_family = AF_INET;
133 s.sin_len = sizeof(struct sockaddr_in);
134 s.sin_addr = ip->ip_src;
135 bzero(&d, sizeof(d));
136 d.sin_family = AF_INET;
137 d.sin_len = sizeof(struct sockaddr_in);
138 d.sin_addr = ip->ip_dst;
139
140 match = NULL;
141 matchprio = 0;
142 mtx_lock(&encapmtx);
143 LIST_FOREACH(ep, &encaptab, chain) {
144 if (ep->af != AF_INET)
145 continue;
146 if (ep->proto >= 0 && ep->proto != proto)
147 continue;
148 if (ep->func)
149 prio = (*ep->func)(m, off, proto, ep->arg);
150 else {
151 /*
152 * it's inbound traffic, we need to match in reverse
153 * order
154 */
155 prio = mask_match(ep, (struct sockaddr *)&d,
156 (struct sockaddr *)&s);
157 }
158
159 /*
160 * We prioritize the matches by using bit length of the
161 * matches. mask_match() and user-supplied matching function
162 * should return the bit length of the matches (for example,
163 * if both src/dst are matched for IPv4, 64 should be returned).
164 * 0 or negative return value means "it did not match".
165 *
166 * The question is, since we have two "mask" portion, we
167 * cannot really define total order between entries.
168 * For example, which of these should be preferred?
169 * mask_match() returns 48 (32 + 16) for both of them.
170 * src=3ffe::/16, dst=3ffe:501::/32
171 * src=3ffe:501::/32, dst=3ffe::/16
172 *
173 * We need to loop through all the possible candidates
174 * to get the best match - the search takes O(n) for
175 * n attachments (i.e. interfaces).
176 */
177 if (prio <= 0)
178 continue;
179 if (prio > matchprio) {
180 matchprio = prio;
181 match = ep;
182 }
183 }
184 mtx_unlock(&encapmtx);
185
186 if (match) {
187 /* found a match, "match" has the best one */
188 psw = match->psw;
189 if (psw && psw->pr_input) {
190 encap_fillarg(m, match);
131
132 bzero(&s, sizeof(s));
133 s.sin_family = AF_INET;
134 s.sin_len = sizeof(struct sockaddr_in);
135 s.sin_addr = ip->ip_src;
136 bzero(&d, sizeof(d));
137 d.sin_family = AF_INET;
138 d.sin_len = sizeof(struct sockaddr_in);
139 d.sin_addr = ip->ip_dst;
140
141 match = NULL;
142 matchprio = 0;
143 mtx_lock(&encapmtx);
144 LIST_FOREACH(ep, &encaptab, chain) {
145 if (ep->af != AF_INET)
146 continue;
147 if (ep->proto >= 0 && ep->proto != proto)
148 continue;
149 if (ep->func)
150 prio = (*ep->func)(m, off, proto, ep->arg);
151 else {
152 /*
153 * it's inbound traffic, we need to match in reverse
154 * order
155 */
156 prio = mask_match(ep, (struct sockaddr *)&d,
157 (struct sockaddr *)&s);
158 }
159
160 /*
161 * We prioritize the matches by using bit length of the
162 * matches. mask_match() and user-supplied matching function
163 * should return the bit length of the matches (for example,
164 * if both src/dst are matched for IPv4, 64 should be returned).
165 * 0 or negative return value means "it did not match".
166 *
167 * The question is, since we have two "mask" portion, we
168 * cannot really define total order between entries.
169 * For example, which of these should be preferred?
170 * mask_match() returns 48 (32 + 16) for both of them.
171 * src=3ffe::/16, dst=3ffe:501::/32
172 * src=3ffe:501::/32, dst=3ffe::/16
173 *
174 * We need to loop through all the possible candidates
175 * to get the best match - the search takes O(n) for
176 * n attachments (i.e. interfaces).
177 */
178 if (prio <= 0)
179 continue;
180 if (prio > matchprio) {
181 matchprio = prio;
182 match = ep;
183 }
184 }
185 mtx_unlock(&encapmtx);
186
187 if (match) {
188 /* found a match, "match" has the best one */
189 psw = match->psw;
190 if (psw && psw->pr_input) {
191 encap_fillarg(m, match);
191 (*psw->pr_input)(m, off);
192 *mp = m;
193 (*psw->pr_input)(mp, offp, proto);
192 } else
193 m_freem(m);
194 } else
195 m_freem(m);
194 return;
196 return (IPPROTO_DONE);
195 }
196
197 /* last resort: inject to raw socket */
197 }
198
199 /* last resort: inject to raw socket */
198 rip_input(m, off);
200 *mp = m;
201 return (rip_input(mp, offp, proto));
199}
200#endif
201
202#ifdef INET6
203int
204encap6_input(struct mbuf **mp, int *offp, int proto)
205{
206 struct mbuf *m = *mp;
207 struct ip6_hdr *ip6;
208 struct sockaddr_in6 s, d;
202}
203#endif
204
205#ifdef INET6
206int
207encap6_input(struct mbuf **mp, int *offp, int proto)
208{
209 struct mbuf *m = *mp;
210 struct ip6_hdr *ip6;
211 struct sockaddr_in6 s, d;
209 const struct ip6protosw *psw;
212 const struct protosw *psw;
210 struct encaptab *ep, *match;
211 int prio, matchprio;
212
213 ip6 = mtod(m, struct ip6_hdr *);
214
215 bzero(&s, sizeof(s));
216 s.sin6_family = AF_INET6;
217 s.sin6_len = sizeof(struct sockaddr_in6);
218 s.sin6_addr = ip6->ip6_src;
219 bzero(&d, sizeof(d));
220 d.sin6_family = AF_INET6;
221 d.sin6_len = sizeof(struct sockaddr_in6);
222 d.sin6_addr = ip6->ip6_dst;
223
224 match = NULL;
225 matchprio = 0;
226 mtx_lock(&encapmtx);
227 LIST_FOREACH(ep, &encaptab, chain) {
228 if (ep->af != AF_INET6)
229 continue;
230 if (ep->proto >= 0 && ep->proto != proto)
231 continue;
232 if (ep->func)
233 prio = (*ep->func)(m, *offp, proto, ep->arg);
234 else {
235 /*
236 * it's inbound traffic, we need to match in reverse
237 * order
238 */
239 prio = mask_match(ep, (struct sockaddr *)&d,
240 (struct sockaddr *)&s);
241 }
242
243 /* see encap4_input() for issues here */
244 if (prio <= 0)
245 continue;
246 if (prio > matchprio) {
247 matchprio = prio;
248 match = ep;
249 }
250 }
251 mtx_unlock(&encapmtx);
252
253 if (match) {
254 /* found a match */
213 struct encaptab *ep, *match;
214 int prio, matchprio;
215
216 ip6 = mtod(m, struct ip6_hdr *);
217
218 bzero(&s, sizeof(s));
219 s.sin6_family = AF_INET6;
220 s.sin6_len = sizeof(struct sockaddr_in6);
221 s.sin6_addr = ip6->ip6_src;
222 bzero(&d, sizeof(d));
223 d.sin6_family = AF_INET6;
224 d.sin6_len = sizeof(struct sockaddr_in6);
225 d.sin6_addr = ip6->ip6_dst;
226
227 match = NULL;
228 matchprio = 0;
229 mtx_lock(&encapmtx);
230 LIST_FOREACH(ep, &encaptab, chain) {
231 if (ep->af != AF_INET6)
232 continue;
233 if (ep->proto >= 0 && ep->proto != proto)
234 continue;
235 if (ep->func)
236 prio = (*ep->func)(m, *offp, proto, ep->arg);
237 else {
238 /*
239 * it's inbound traffic, we need to match in reverse
240 * order
241 */
242 prio = mask_match(ep, (struct sockaddr *)&d,
243 (struct sockaddr *)&s);
244 }
245
246 /* see encap4_input() for issues here */
247 if (prio <= 0)
248 continue;
249 if (prio > matchprio) {
250 matchprio = prio;
251 match = ep;
252 }
253 }
254 mtx_unlock(&encapmtx);
255
256 if (match) {
257 /* found a match */
255 psw = (const struct ip6protosw *)match->psw;
258 psw = match->psw;
256 if (psw && psw->pr_input) {
257 encap_fillarg(m, match);
258 return (*psw->pr_input)(mp, offp, proto);
259 } else {
260 m_freem(m);
261 return IPPROTO_DONE;
262 }
263 }
264
265 /* last resort: inject to raw socket */
266 return rip6_input(mp, offp, proto);
267}
268#endif
269
270/*lint -sem(encap_add, custodial(1)) */
271static void
272encap_add(struct encaptab *ep)
273{
274
275 mtx_assert(&encapmtx, MA_OWNED);
276 LIST_INSERT_HEAD(&encaptab, ep, chain);
277}
278
279/*
280 * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
281 * length of mask (sm and dm) is assumed to be same as sp/dp.
282 * Return value will be necessary as input (cookie) for encap_detach().
283 */
284const struct encaptab *
285encap_attach(int af, int proto, const struct sockaddr *sp,
286 const struct sockaddr *sm, const struct sockaddr *dp,
287 const struct sockaddr *dm, const struct protosw *psw, void *arg)
288{
289 struct encaptab *ep;
290
291 /* sanity check on args */
292 if (sp->sa_len > sizeof(ep->src) || dp->sa_len > sizeof(ep->dst))
293 return (NULL);
294 if (sp->sa_len != dp->sa_len)
295 return (NULL);
296 if (af != sp->sa_family || af != dp->sa_family)
297 return (NULL);
298
299 /* check if anyone have already attached with exactly same config */
300 mtx_lock(&encapmtx);
301 LIST_FOREACH(ep, &encaptab, chain) {
302 if (ep->af != af)
303 continue;
304 if (ep->proto != proto)
305 continue;
306 if (ep->src.ss_len != sp->sa_len ||
307 bcmp(&ep->src, sp, sp->sa_len) != 0 ||
308 bcmp(&ep->srcmask, sm, sp->sa_len) != 0)
309 continue;
310 if (ep->dst.ss_len != dp->sa_len ||
311 bcmp(&ep->dst, dp, dp->sa_len) != 0 ||
312 bcmp(&ep->dstmask, dm, dp->sa_len) != 0)
313 continue;
314
315 mtx_unlock(&encapmtx);
316 return (NULL);
317 }
318
319 ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT); /*XXX*/
320 if (ep == NULL) {
321 mtx_unlock(&encapmtx);
322 return (NULL);
323 }
324 bzero(ep, sizeof(*ep));
325
326 ep->af = af;
327 ep->proto = proto;
328 bcopy(sp, &ep->src, sp->sa_len);
329 bcopy(sm, &ep->srcmask, sp->sa_len);
330 bcopy(dp, &ep->dst, dp->sa_len);
331 bcopy(dm, &ep->dstmask, dp->sa_len);
332 ep->psw = psw;
333 ep->arg = arg;
334
335 encap_add(ep);
336 mtx_unlock(&encapmtx);
337 return (ep);
338}
339
340const struct encaptab *
341encap_attach_func(int af, int proto,
342 int (*func)(const struct mbuf *, int, int, void *),
343 const struct protosw *psw, void *arg)
344{
345 struct encaptab *ep;
346
347 /* sanity check on args */
348 if (!func)
349 return (NULL);
350
351 ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT); /*XXX*/
352 if (ep == NULL)
353 return (NULL);
354 bzero(ep, sizeof(*ep));
355
356 ep->af = af;
357 ep->proto = proto;
358 ep->func = func;
359 ep->psw = psw;
360 ep->arg = arg;
361
362 mtx_lock(&encapmtx);
363 encap_add(ep);
364 mtx_unlock(&encapmtx);
365 return (ep);
366}
367
368int
369encap_detach(const struct encaptab *cookie)
370{
371 const struct encaptab *ep = cookie;
372 struct encaptab *p;
373
374 mtx_lock(&encapmtx);
375 LIST_FOREACH(p, &encaptab, chain) {
376 if (p == ep) {
377 LIST_REMOVE(p, chain);
378 mtx_unlock(&encapmtx);
379 free(p, M_NETADDR); /*XXX*/
380 return 0;
381 }
382 }
383 mtx_unlock(&encapmtx);
384
385 return EINVAL;
386}
387
388static int
389mask_match(const struct encaptab *ep, const struct sockaddr *sp,
390 const struct sockaddr *dp)
391{
392 struct sockaddr_storage s;
393 struct sockaddr_storage d;
394 int i;
395 const u_int8_t *p, *q;
396 u_int8_t *r;
397 int matchlen;
398
399 if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
400 return 0;
401 if (sp->sa_family != ep->af || dp->sa_family != ep->af)
402 return 0;
403 if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len)
404 return 0;
405
406 matchlen = 0;
407
408 p = (const u_int8_t *)sp;
409 q = (const u_int8_t *)&ep->srcmask;
410 r = (u_int8_t *)&s;
411 for (i = 0 ; i < sp->sa_len; i++) {
412 r[i] = p[i] & q[i];
413 /* XXX estimate */
414 matchlen += (q[i] ? 8 : 0);
415 }
416
417 p = (const u_int8_t *)dp;
418 q = (const u_int8_t *)&ep->dstmask;
419 r = (u_int8_t *)&d;
420 for (i = 0 ; i < dp->sa_len; i++) {
421 r[i] = p[i] & q[i];
422 /* XXX rough estimate */
423 matchlen += (q[i] ? 8 : 0);
424 }
425
426 /* need to overwrite len/family portion as we don't compare them */
427 s.ss_len = sp->sa_len;
428 s.ss_family = sp->sa_family;
429 d.ss_len = dp->sa_len;
430 d.ss_family = dp->sa_family;
431
432 if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 &&
433 bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) {
434 return matchlen;
435 } else
436 return 0;
437}
438
439static void
440encap_fillarg(struct mbuf *m, const struct encaptab *ep)
441{
442 struct m_tag *tag;
443
444 tag = m_tag_get(PACKET_TAG_ENCAP, sizeof (void*), M_NOWAIT);
445 if (tag) {
446 *(void**)(tag+1) = ep->arg;
447 m_tag_prepend(m, tag);
448 }
449}
450
451void *
452encap_getarg(struct mbuf *m)
453{
454 void *p = NULL;
455 struct m_tag *tag;
456
457 tag = m_tag_find(m, PACKET_TAG_ENCAP, NULL);
458 if (tag) {
459 p = *(void**)(tag+1);
460 m_tag_delete(m, tag);
461 }
462 return p;
463}
259 if (psw && psw->pr_input) {
260 encap_fillarg(m, match);
261 return (*psw->pr_input)(mp, offp, proto);
262 } else {
263 m_freem(m);
264 return IPPROTO_DONE;
265 }
266 }
267
268 /* last resort: inject to raw socket */
269 return rip6_input(mp, offp, proto);
270}
271#endif
272
273/*lint -sem(encap_add, custodial(1)) */
274static void
275encap_add(struct encaptab *ep)
276{
277
278 mtx_assert(&encapmtx, MA_OWNED);
279 LIST_INSERT_HEAD(&encaptab, ep, chain);
280}
281
282/*
283 * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
284 * length of mask (sm and dm) is assumed to be same as sp/dp.
285 * Return value will be necessary as input (cookie) for encap_detach().
286 */
287const struct encaptab *
288encap_attach(int af, int proto, const struct sockaddr *sp,
289 const struct sockaddr *sm, const struct sockaddr *dp,
290 const struct sockaddr *dm, const struct protosw *psw, void *arg)
291{
292 struct encaptab *ep;
293
294 /* sanity check on args */
295 if (sp->sa_len > sizeof(ep->src) || dp->sa_len > sizeof(ep->dst))
296 return (NULL);
297 if (sp->sa_len != dp->sa_len)
298 return (NULL);
299 if (af != sp->sa_family || af != dp->sa_family)
300 return (NULL);
301
302 /* check if anyone have already attached with exactly same config */
303 mtx_lock(&encapmtx);
304 LIST_FOREACH(ep, &encaptab, chain) {
305 if (ep->af != af)
306 continue;
307 if (ep->proto != proto)
308 continue;
309 if (ep->src.ss_len != sp->sa_len ||
310 bcmp(&ep->src, sp, sp->sa_len) != 0 ||
311 bcmp(&ep->srcmask, sm, sp->sa_len) != 0)
312 continue;
313 if (ep->dst.ss_len != dp->sa_len ||
314 bcmp(&ep->dst, dp, dp->sa_len) != 0 ||
315 bcmp(&ep->dstmask, dm, dp->sa_len) != 0)
316 continue;
317
318 mtx_unlock(&encapmtx);
319 return (NULL);
320 }
321
322 ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT); /*XXX*/
323 if (ep == NULL) {
324 mtx_unlock(&encapmtx);
325 return (NULL);
326 }
327 bzero(ep, sizeof(*ep));
328
329 ep->af = af;
330 ep->proto = proto;
331 bcopy(sp, &ep->src, sp->sa_len);
332 bcopy(sm, &ep->srcmask, sp->sa_len);
333 bcopy(dp, &ep->dst, dp->sa_len);
334 bcopy(dm, &ep->dstmask, dp->sa_len);
335 ep->psw = psw;
336 ep->arg = arg;
337
338 encap_add(ep);
339 mtx_unlock(&encapmtx);
340 return (ep);
341}
342
343const struct encaptab *
344encap_attach_func(int af, int proto,
345 int (*func)(const struct mbuf *, int, int, void *),
346 const struct protosw *psw, void *arg)
347{
348 struct encaptab *ep;
349
350 /* sanity check on args */
351 if (!func)
352 return (NULL);
353
354 ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT); /*XXX*/
355 if (ep == NULL)
356 return (NULL);
357 bzero(ep, sizeof(*ep));
358
359 ep->af = af;
360 ep->proto = proto;
361 ep->func = func;
362 ep->psw = psw;
363 ep->arg = arg;
364
365 mtx_lock(&encapmtx);
366 encap_add(ep);
367 mtx_unlock(&encapmtx);
368 return (ep);
369}
370
371int
372encap_detach(const struct encaptab *cookie)
373{
374 const struct encaptab *ep = cookie;
375 struct encaptab *p;
376
377 mtx_lock(&encapmtx);
378 LIST_FOREACH(p, &encaptab, chain) {
379 if (p == ep) {
380 LIST_REMOVE(p, chain);
381 mtx_unlock(&encapmtx);
382 free(p, M_NETADDR); /*XXX*/
383 return 0;
384 }
385 }
386 mtx_unlock(&encapmtx);
387
388 return EINVAL;
389}
390
391static int
392mask_match(const struct encaptab *ep, const struct sockaddr *sp,
393 const struct sockaddr *dp)
394{
395 struct sockaddr_storage s;
396 struct sockaddr_storage d;
397 int i;
398 const u_int8_t *p, *q;
399 u_int8_t *r;
400 int matchlen;
401
402 if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
403 return 0;
404 if (sp->sa_family != ep->af || dp->sa_family != ep->af)
405 return 0;
406 if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len)
407 return 0;
408
409 matchlen = 0;
410
411 p = (const u_int8_t *)sp;
412 q = (const u_int8_t *)&ep->srcmask;
413 r = (u_int8_t *)&s;
414 for (i = 0 ; i < sp->sa_len; i++) {
415 r[i] = p[i] & q[i];
416 /* XXX estimate */
417 matchlen += (q[i] ? 8 : 0);
418 }
419
420 p = (const u_int8_t *)dp;
421 q = (const u_int8_t *)&ep->dstmask;
422 r = (u_int8_t *)&d;
423 for (i = 0 ; i < dp->sa_len; i++) {
424 r[i] = p[i] & q[i];
425 /* XXX rough estimate */
426 matchlen += (q[i] ? 8 : 0);
427 }
428
429 /* need to overwrite len/family portion as we don't compare them */
430 s.ss_len = sp->sa_len;
431 s.ss_family = sp->sa_family;
432 d.ss_len = dp->sa_len;
433 d.ss_family = dp->sa_family;
434
435 if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 &&
436 bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) {
437 return matchlen;
438 } else
439 return 0;
440}
441
442static void
443encap_fillarg(struct mbuf *m, const struct encaptab *ep)
444{
445 struct m_tag *tag;
446
447 tag = m_tag_get(PACKET_TAG_ENCAP, sizeof (void*), M_NOWAIT);
448 if (tag) {
449 *(void**)(tag+1) = ep->arg;
450 m_tag_prepend(m, tag);
451 }
452}
453
454void *
455encap_getarg(struct mbuf *m)
456{
457 void *p = NULL;
458 struct m_tag *tag;
459
460 tag = m_tag_find(m, PACKET_TAG_ENCAP, NULL);
461 if (tag) {
462 p = *(void**)(tag+1);
463 m_tag_delete(m, tag);
464 }
465 return p;
466}