1 /* $KAME: ip_encap.c,v 1.41 2001/03/15 08:35:08 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Copyright (c) 2018 Andrey V. Elsukov <ae@FreeBSD.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the project nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * My grandfather said that there's a devil inside tunnelling technology...
37 * We have surprisingly many protocols that want packets with IP protocol
38 * #4 or #41. Here's a list of protocols that want protocol #41:
39 * RFC1933 configured tunnel
40 * RFC1933 automatic tunnel
41 * RFC2401 IPsec tunnel
42 * RFC2473 IPv6 generic packet tunnelling
43 * RFC2529 6over4 tunnel
44 * mobile-ip6 (uses RFC2473)
47 * Here's a list of protocol that want protocol #4:
48 * RFC1853 IPv4-in-IPv4 tunnelling
49 * RFC2003 IPv4 encapsulation within IPv4
50 * RFC2344 reverse tunnelling for mobile-ip4
51 * RFC2401 IPsec tunnel
52 * Well, what can I say. They impose different en/decapsulation mechanism
53 * from each other, so they need separate protocol handler. The only one
54 * we can easily determine by protocol # is IPsec, which always has
55 * AH/ESP/IPComp header right after outer IP header.
57 * So, clearly good old protosw does not work for protocol #4 and #41.
58 * The code will let you match protocol via src/dst address pair.
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
65 #include "opt_inet6.h"
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/eventhandler.h>
70 #include <sys/kernel.h>
72 #include <sys/malloc.h>
73 #include <sys/mutex.h>
75 #include <sys/errno.h>
76 #include <sys/socket.h>
79 #include <net/if_var.h>
81 #include <netinet/in.h>
82 #include <netinet/ip_var.h>
83 #include <netinet/ip_encap.h>
86 #include <netinet6/ip6_var.h>
89 static MALLOC_DEFINE(M_NETADDR, "encap_export_host",
90 "Export host address structure");
93 CK_LIST_ENTRY(encaptab) chain;
99 encap_lookup_t lookup;
105 CK_LIST_ENTRY(srcaddrtab) chain;
107 encap_srcaddr_t srcaddr;
111 CK_LIST_HEAD(encaptab_head, encaptab);
112 CK_LIST_HEAD(srcaddrtab_head, srcaddrtab);
114 static struct encaptab_head ipv4_encaptab = CK_LIST_HEAD_INITIALIZER();
115 static struct srcaddrtab_head ipv4_srcaddrtab = CK_LIST_HEAD_INITIALIZER();
118 static struct encaptab_head ipv6_encaptab = CK_LIST_HEAD_INITIALIZER();
119 static struct srcaddrtab_head ipv6_srcaddrtab = CK_LIST_HEAD_INITIALIZER();
122 static struct mtx encapmtx, srcaddrmtx;
123 MTX_SYSINIT(encapmtx, &encapmtx, "encapmtx", MTX_DEF);
124 MTX_SYSINIT(srcaddrmtx, &srcaddrmtx, "srcaddrmtx", MTX_DEF);
125 #define ENCAP_WLOCK() mtx_lock(&encapmtx)
126 #define ENCAP_WUNLOCK() mtx_unlock(&encapmtx)
127 #define ENCAP_RLOCK_TRACKER struct epoch_tracker encap_et
128 #define ENCAP_RLOCK() NET_EPOCH_ENTER(encap_et)
129 #define ENCAP_RUNLOCK() NET_EPOCH_EXIT(encap_et)
130 #define ENCAP_WAIT() NET_EPOCH_WAIT()
132 #define SRCADDR_WLOCK() mtx_lock(&srcaddrmtx)
133 #define SRCADDR_WUNLOCK() mtx_unlock(&srcaddrmtx)
134 #define SRCADDR_RLOCK_TRACKER struct epoch_tracker srcaddr_et
135 #define SRCADDR_RLOCK() \
136 epoch_enter_preempt(net_epoch_preempt, &srcaddr_et)
137 #define SRCADDR_RUNLOCK() \
138 epoch_exit_preempt(net_epoch_preempt, &srcaddr_et)
139 #define SRCADDR_WAIT() epoch_wait_preempt(net_epoch_preempt)
142 * ifaddr_event_ext handler.
144 * Tunnelling interfaces may request the kernel to notify when
145 * some interface addresses appears or disappears. Usually tunnelling
146 * interface must use an address configured on the local machine as
147 * ingress address to be able receive datagramms and do not send
151 srcaddr_change_event(void *arg __unused, struct ifnet *ifp,
152 struct ifaddr *ifa, int event)
154 SRCADDR_RLOCK_TRACKER;
155 struct srcaddrtab_head *head;
156 struct srcaddrtab *p;
158 /* Support for old ifaddr_event. */
159 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
161 switch (ifa->ifa_addr->sa_family) {
164 head = &ipv4_srcaddrtab;
169 head = &ipv6_srcaddrtab;
178 CK_LIST_FOREACH(p, head, chain) {
179 (*p->srcaddr)(p->arg, ifa->ifa_addr, event);
183 EVENTHANDLER_DEFINE(ifaddr_event_ext, srcaddr_change_event, NULL, 0);
185 static struct srcaddrtab *
186 encap_register_srcaddr(struct srcaddrtab_head *head, encap_srcaddr_t func,
187 void *arg, int mflags)
189 struct srcaddrtab *p, *tmp;
193 p = malloc(sizeof(*p), M_NETADDR, mflags);
200 CK_LIST_FOREACH(tmp, head, chain) {
201 if (func == tmp->srcaddr && arg == tmp->arg)
205 CK_LIST_INSERT_HEAD(head, p, chain);
216 encap_unregister_srcaddr(struct srcaddrtab_head *head,
217 const struct srcaddrtab *cookie)
219 struct srcaddrtab *p;
222 CK_LIST_FOREACH(p, head, chain) {
224 CK_LIST_REMOVE(p, chain);
235 static struct encaptab *
236 encap_attach(struct encaptab_head *head, const struct encap_config *cfg,
237 void *arg, int mflags)
239 struct encaptab *ep, *tmp;
241 if (cfg == NULL || cfg->input == NULL ||
242 (cfg->check == NULL && cfg->lookup == NULL) ||
243 (cfg->lookup != NULL && cfg->exact_match != ENCAP_DRV_LOOKUP) ||
244 (cfg->exact_match == ENCAP_DRV_LOOKUP && cfg->lookup == NULL))
247 ep = malloc(sizeof(*ep), M_NETADDR, mflags);
251 ep->proto = cfg->proto;
252 ep->min_length = cfg->min_length;
253 ep->exact_match = cfg->exact_match;
255 ep->lookup = cfg->exact_match == ENCAP_DRV_LOOKUP ? cfg->lookup: NULL;
256 ep->check = cfg->exact_match != ENCAP_DRV_LOOKUP ? cfg->check: NULL;
257 ep->input = cfg->input;
260 CK_LIST_FOREACH(tmp, head, chain) {
261 if (tmp->exact_match <= ep->exact_match)
265 CK_LIST_INSERT_HEAD(head, ep, chain);
267 CK_LIST_INSERT_BEFORE(tmp, ep, chain);
273 encap_detach(struct encaptab_head *head, const struct encaptab *cookie)
278 CK_LIST_FOREACH(ep, head, chain) {
280 CK_LIST_REMOVE(ep, chain);
292 encap_input(struct encaptab_head *head, struct mbuf *m, int off, int proto)
295 struct encaptab *ep, *match;
303 CK_LIST_FOREACH(ep, head, chain) {
304 if (ep->proto >= 0 && ep->proto != proto)
306 if (ep->min_length > m->m_pkthdr.len)
308 if (ep->exact_match == ENCAP_DRV_LOOKUP)
309 ret = (*ep->lookup)(m, off, proto, &arg);
311 ret = (*ep->check)(m, off, proto, ep->arg);
314 if (ret > matchprio) {
316 if (ep->exact_match != ENCAP_DRV_LOOKUP)
319 * No need to continue the search, we got the
322 if (ret >= ep->exact_match)
329 /* found a match, "match" has the best one */
330 ret = (*match->input)(m, off, proto, arg);
332 MPASS(ret == IPPROTO_DONE);
333 return (IPPROTO_DONE);
340 const struct srcaddrtab *
341 ip_encap_register_srcaddr(encap_srcaddr_t func, void *arg, int mflags)
344 return (encap_register_srcaddr(&ipv4_srcaddrtab, func, arg, mflags));
348 ip_encap_unregister_srcaddr(const struct srcaddrtab *cookie)
351 return (encap_unregister_srcaddr(&ipv4_srcaddrtab, cookie));
354 const struct encaptab *
355 ip_encap_attach(const struct encap_config *cfg, void *arg, int mflags)
358 return (encap_attach(&ipv4_encaptab, cfg, arg, mflags));
362 ip_encap_detach(const struct encaptab *cookie)
365 return (encap_detach(&ipv4_encaptab, cookie));
369 encap4_input(struct mbuf **mp, int *offp, int proto)
372 if (encap_input(&ipv4_encaptab, *mp, *offp, proto) != IPPROTO_DONE)
373 return (rip_input(mp, offp, proto));
374 return (IPPROTO_DONE);
379 const struct srcaddrtab *
380 ip6_encap_register_srcaddr(encap_srcaddr_t func, void *arg, int mflags)
383 return (encap_register_srcaddr(&ipv6_srcaddrtab, func, arg, mflags));
387 ip6_encap_unregister_srcaddr(const struct srcaddrtab *cookie)
390 return (encap_unregister_srcaddr(&ipv6_srcaddrtab, cookie));
393 const struct encaptab *
394 ip6_encap_attach(const struct encap_config *cfg, void *arg, int mflags)
397 return (encap_attach(&ipv6_encaptab, cfg, arg, mflags));
401 ip6_encap_detach(const struct encaptab *cookie)
404 return (encap_detach(&ipv6_encaptab, cookie));
408 encap6_input(struct mbuf **mp, int *offp, int proto)
411 if (encap_input(&ipv6_encaptab, *mp, *offp, proto) != IPPROTO_DONE)
412 return (rip6_input(mp, offp, proto));
413 return (IPPROTO_DONE);