xref: /f-stack/freebsd/netinet/ip_encap.c (revision 22ce4aff)
1a9643ea8Slogwang /*	$KAME: ip_encap.c,v 1.41 2001/03/15 08:35:08 itojun Exp $	*/
2a9643ea8Slogwang 
3a9643ea8Slogwang /*-
4*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-3-Clause
5*22ce4affSfengbojiang  *
6a9643ea8Slogwang  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7*22ce4affSfengbojiang  * Copyright (c) 2018 Andrey V. Elsukov <[email protected]>
8a9643ea8Slogwang  * All rights reserved.
9a9643ea8Slogwang  *
10a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
11a9643ea8Slogwang  * modification, are permitted provided that the following conditions
12a9643ea8Slogwang  * are met:
13a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
14a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer.
15a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
16a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
17a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
18a9643ea8Slogwang  * 3. Neither the name of the project nor the names of its contributors
19a9643ea8Slogwang  *    may be used to endorse or promote products derived from this software
20a9643ea8Slogwang  *    without specific prior written permission.
21a9643ea8Slogwang  *
22a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23a9643ea8Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24a9643ea8Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25a9643ea8Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26a9643ea8Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27a9643ea8Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28a9643ea8Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29a9643ea8Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30a9643ea8Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31a9643ea8Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32a9643ea8Slogwang  * SUCH DAMAGE.
33a9643ea8Slogwang  */
34a9643ea8Slogwang /*
35a9643ea8Slogwang  * My grandfather said that there's a devil inside tunnelling technology...
36a9643ea8Slogwang  *
37a9643ea8Slogwang  * We have surprisingly many protocols that want packets with IP protocol
38a9643ea8Slogwang  * #4 or #41.  Here's a list of protocols that want protocol #41:
39a9643ea8Slogwang  *	RFC1933 configured tunnel
40a9643ea8Slogwang  *	RFC1933 automatic tunnel
41a9643ea8Slogwang  *	RFC2401 IPsec tunnel
42a9643ea8Slogwang  *	RFC2473 IPv6 generic packet tunnelling
43a9643ea8Slogwang  *	RFC2529 6over4 tunnel
44a9643ea8Slogwang  *	mobile-ip6 (uses RFC2473)
45a9643ea8Slogwang  *	RFC3056 6to4 tunnel
46a9643ea8Slogwang  *	isatap tunnel
47a9643ea8Slogwang  * Here's a list of protocol that want protocol #4:
48a9643ea8Slogwang  *	RFC1853 IPv4-in-IPv4 tunnelling
49a9643ea8Slogwang  *	RFC2003 IPv4 encapsulation within IPv4
50a9643ea8Slogwang  *	RFC2344 reverse tunnelling for mobile-ip4
51a9643ea8Slogwang  *	RFC2401 IPsec tunnel
52a9643ea8Slogwang  * Well, what can I say.  They impose different en/decapsulation mechanism
53a9643ea8Slogwang  * from each other, so they need separate protocol handler.  The only one
54a9643ea8Slogwang  * we can easily determine by protocol # is IPsec, which always has
55a9643ea8Slogwang  * AH/ESP/IPComp header right after outer IP header.
56a9643ea8Slogwang  *
57a9643ea8Slogwang  * So, clearly good old protosw does not work for protocol #4 and #41.
58a9643ea8Slogwang  * The code will let you match protocol via src/dst address pair.
59a9643ea8Slogwang  */
60a9643ea8Slogwang 
61a9643ea8Slogwang #include <sys/cdefs.h>
62a9643ea8Slogwang __FBSDID("$FreeBSD$");
63a9643ea8Slogwang 
64a9643ea8Slogwang #include "opt_inet.h"
65a9643ea8Slogwang #include "opt_inet6.h"
66a9643ea8Slogwang 
67a9643ea8Slogwang #include <sys/param.h>
68a9643ea8Slogwang #include <sys/systm.h>
69*22ce4affSfengbojiang #include <sys/eventhandler.h>
70*22ce4affSfengbojiang #include <sys/kernel.h>
71a9643ea8Slogwang #include <sys/lock.h>
72*22ce4affSfengbojiang #include <sys/malloc.h>
73a9643ea8Slogwang #include <sys/mutex.h>
74a9643ea8Slogwang #include <sys/mbuf.h>
75a9643ea8Slogwang #include <sys/errno.h>
76*22ce4affSfengbojiang #include <sys/socket.h>
77a9643ea8Slogwang 
78a9643ea8Slogwang #include <net/if.h>
79*22ce4affSfengbojiang #include <net/if_var.h>
80a9643ea8Slogwang 
81a9643ea8Slogwang #include <netinet/in.h>
82a9643ea8Slogwang #include <netinet/ip_var.h>
83a9643ea8Slogwang #include <netinet/ip_encap.h>
84a9643ea8Slogwang 
85a9643ea8Slogwang #ifdef INET6
86a9643ea8Slogwang #include <netinet6/ip6_var.h>
87a9643ea8Slogwang #endif
88a9643ea8Slogwang 
89*22ce4affSfengbojiang static MALLOC_DEFINE(M_NETADDR, "encap_export_host",
90*22ce4affSfengbojiang     "Export host address structure");
91a9643ea8Slogwang 
92*22ce4affSfengbojiang struct encaptab {
93*22ce4affSfengbojiang 	CK_LIST_ENTRY(encaptab) chain;
94*22ce4affSfengbojiang 	int		proto;
95*22ce4affSfengbojiang 	int		min_length;
96*22ce4affSfengbojiang 	int		exact_match;
97*22ce4affSfengbojiang 	void		*arg;
98a9643ea8Slogwang 
99*22ce4affSfengbojiang 	encap_lookup_t	lookup;
100*22ce4affSfengbojiang 	encap_check_t	check;
101*22ce4affSfengbojiang 	encap_input_t	input;
102*22ce4affSfengbojiang };
103a9643ea8Slogwang 
104*22ce4affSfengbojiang struct srcaddrtab {
105*22ce4affSfengbojiang 	CK_LIST_ENTRY(srcaddrtab) chain;
106a9643ea8Slogwang 
107*22ce4affSfengbojiang 	encap_srcaddr_t	srcaddr;
108*22ce4affSfengbojiang 	void		*arg;
109*22ce4affSfengbojiang };
110a9643ea8Slogwang 
111*22ce4affSfengbojiang CK_LIST_HEAD(encaptab_head, encaptab);
112*22ce4affSfengbojiang CK_LIST_HEAD(srcaddrtab_head, srcaddrtab);
113a9643ea8Slogwang #ifdef INET
114*22ce4affSfengbojiang static struct encaptab_head ipv4_encaptab = CK_LIST_HEAD_INITIALIZER();
115*22ce4affSfengbojiang static struct srcaddrtab_head ipv4_srcaddrtab = CK_LIST_HEAD_INITIALIZER();
116a9643ea8Slogwang #endif
117a9643ea8Slogwang #ifdef INET6
118*22ce4affSfengbojiang static struct encaptab_head ipv6_encaptab = CK_LIST_HEAD_INITIALIZER();
119*22ce4affSfengbojiang static struct srcaddrtab_head ipv6_srcaddrtab = CK_LIST_HEAD_INITIALIZER();
120a9643ea8Slogwang #endif
121a9643ea8Slogwang 
122*22ce4affSfengbojiang static struct mtx encapmtx, srcaddrmtx;
123*22ce4affSfengbojiang MTX_SYSINIT(encapmtx, &encapmtx, "encapmtx", MTX_DEF);
124*22ce4affSfengbojiang MTX_SYSINIT(srcaddrmtx, &srcaddrmtx, "srcaddrmtx", MTX_DEF);
125*22ce4affSfengbojiang #define	ENCAP_WLOCK()		mtx_lock(&encapmtx)
126*22ce4affSfengbojiang #define	ENCAP_WUNLOCK()		mtx_unlock(&encapmtx)
127*22ce4affSfengbojiang #define	ENCAP_RLOCK_TRACKER	struct epoch_tracker encap_et
128*22ce4affSfengbojiang #define	ENCAP_RLOCK()		NET_EPOCH_ENTER(encap_et)
129*22ce4affSfengbojiang #define	ENCAP_RUNLOCK()		NET_EPOCH_EXIT(encap_et)
130*22ce4affSfengbojiang #define	ENCAP_WAIT()		NET_EPOCH_WAIT()
131a9643ea8Slogwang 
132*22ce4affSfengbojiang #define	SRCADDR_WLOCK()		mtx_lock(&srcaddrmtx)
133*22ce4affSfengbojiang #define	SRCADDR_WUNLOCK()	mtx_unlock(&srcaddrmtx)
134*22ce4affSfengbojiang #define	SRCADDR_RLOCK_TRACKER	struct epoch_tracker srcaddr_et
135*22ce4affSfengbojiang #define	SRCADDR_RLOCK()		\
136*22ce4affSfengbojiang     epoch_enter_preempt(net_epoch_preempt, &srcaddr_et)
137*22ce4affSfengbojiang #define	SRCADDR_RUNLOCK()	\
138*22ce4affSfengbojiang     epoch_exit_preempt(net_epoch_preempt, &srcaddr_et)
139*22ce4affSfengbojiang #define	SRCADDR_WAIT()		epoch_wait_preempt(net_epoch_preempt)
140a9643ea8Slogwang 
141a9643ea8Slogwang /*
142*22ce4affSfengbojiang  * ifaddr_event_ext handler.
143*22ce4affSfengbojiang  *
144*22ce4affSfengbojiang  * Tunnelling interfaces may request the kernel to notify when
145*22ce4affSfengbojiang  * some interface addresses appears or disappears. Usually tunnelling
146*22ce4affSfengbojiang  * interface must use an address configured on the local machine as
147*22ce4affSfengbojiang  * ingress address to be able receive datagramms and do not send
148*22ce4affSfengbojiang  * spoofed packets.
149a9643ea8Slogwang  */
150*22ce4affSfengbojiang static void
srcaddr_change_event(void * arg __unused,struct ifnet * ifp,struct ifaddr * ifa,int event)151*22ce4affSfengbojiang srcaddr_change_event(void *arg __unused, struct ifnet *ifp,
152*22ce4affSfengbojiang     struct ifaddr *ifa, int event)
153a9643ea8Slogwang {
154*22ce4affSfengbojiang 	SRCADDR_RLOCK_TRACKER;
155*22ce4affSfengbojiang 	struct srcaddrtab_head *head;
156*22ce4affSfengbojiang 	struct srcaddrtab *p;
157a9643ea8Slogwang 
158*22ce4affSfengbojiang 	/* Support for old ifaddr_event. */
159*22ce4affSfengbojiang 	EVENTHANDLER_INVOKE(ifaddr_event, ifp);
160a9643ea8Slogwang 
161*22ce4affSfengbojiang 	switch (ifa->ifa_addr->sa_family) {
162*22ce4affSfengbojiang #ifdef INET
163*22ce4affSfengbojiang 	case AF_INET:
164*22ce4affSfengbojiang 		head = &ipv4_srcaddrtab;
165*22ce4affSfengbojiang 		break;
166*22ce4affSfengbojiang #endif
167*22ce4affSfengbojiang #ifdef INET6
168*22ce4affSfengbojiang 	case AF_INET6:
169*22ce4affSfengbojiang 		head = &ipv6_srcaddrtab;
170*22ce4affSfengbojiang 		break;
171*22ce4affSfengbojiang #endif
172*22ce4affSfengbojiang 	default:
173*22ce4affSfengbojiang 		/* ignore event */
174*22ce4affSfengbojiang 		return;
175a9643ea8Slogwang 	}
176a9643ea8Slogwang 
177*22ce4affSfengbojiang 	SRCADDR_RLOCK();
178*22ce4affSfengbojiang 	CK_LIST_FOREACH(p, head, chain) {
179*22ce4affSfengbojiang 		(*p->srcaddr)(p->arg, ifa->ifa_addr, event);
180a9643ea8Slogwang 	}
181*22ce4affSfengbojiang 	SRCADDR_RUNLOCK();
182a9643ea8Slogwang }
183*22ce4affSfengbojiang EVENTHANDLER_DEFINE(ifaddr_event_ext, srcaddr_change_event, NULL, 0);
184a9643ea8Slogwang 
185*22ce4affSfengbojiang static struct srcaddrtab *
encap_register_srcaddr(struct srcaddrtab_head * head,encap_srcaddr_t func,void * arg,int mflags)186*22ce4affSfengbojiang encap_register_srcaddr(struct srcaddrtab_head *head, encap_srcaddr_t func,
187*22ce4affSfengbojiang     void *arg, int mflags)
188a9643ea8Slogwang {
189*22ce4affSfengbojiang 	struct srcaddrtab *p, *tmp;
190a9643ea8Slogwang 
191*22ce4affSfengbojiang 	if (func == NULL)
192a9643ea8Slogwang 		return (NULL);
193*22ce4affSfengbojiang 	p = malloc(sizeof(*p), M_NETADDR, mflags);
194*22ce4affSfengbojiang 	if (p == NULL)
195a9643ea8Slogwang 		return (NULL);
196*22ce4affSfengbojiang 	p->srcaddr = func;
197*22ce4affSfengbojiang 	p->arg = arg;
198a9643ea8Slogwang 
199*22ce4affSfengbojiang 	SRCADDR_WLOCK();
200*22ce4affSfengbojiang 	CK_LIST_FOREACH(tmp, head, chain) {
201*22ce4affSfengbojiang 		if (func == tmp->srcaddr && arg == tmp->arg)
202*22ce4affSfengbojiang 			break;
203a9643ea8Slogwang 	}
204*22ce4affSfengbojiang 	if (tmp == NULL)
205*22ce4affSfengbojiang 		CK_LIST_INSERT_HEAD(head, p, chain);
206*22ce4affSfengbojiang 	SRCADDR_WUNLOCK();
207a9643ea8Slogwang 
208*22ce4affSfengbojiang 	if (tmp != NULL) {
209*22ce4affSfengbojiang 		free(p, M_NETADDR);
210*22ce4affSfengbojiang 		p = tmp;
211a9643ea8Slogwang 	}
212*22ce4affSfengbojiang 	return (p);
213a9643ea8Slogwang }
214a9643ea8Slogwang 
215a9643ea8Slogwang static int
encap_unregister_srcaddr(struct srcaddrtab_head * head,const struct srcaddrtab * cookie)216*22ce4affSfengbojiang encap_unregister_srcaddr(struct srcaddrtab_head *head,
217*22ce4affSfengbojiang     const struct srcaddrtab *cookie)
218a9643ea8Slogwang {
219*22ce4affSfengbojiang 	struct srcaddrtab *p;
220a9643ea8Slogwang 
221*22ce4affSfengbojiang 	SRCADDR_WLOCK();
222*22ce4affSfengbojiang 	CK_LIST_FOREACH(p, head, chain) {
223*22ce4affSfengbojiang 		if (p == cookie) {
224*22ce4affSfengbojiang 			CK_LIST_REMOVE(p, chain);
225*22ce4affSfengbojiang 			SRCADDR_WUNLOCK();
226*22ce4affSfengbojiang 			SRCADDR_WAIT();
227*22ce4affSfengbojiang 			free(p, M_NETADDR);
228*22ce4affSfengbojiang 			return (0);
229*22ce4affSfengbojiang 		}
230*22ce4affSfengbojiang 	}
231*22ce4affSfengbojiang 	SRCADDR_WUNLOCK();
232*22ce4affSfengbojiang 	return (EINVAL);
233a9643ea8Slogwang }
234a9643ea8Slogwang 
235*22ce4affSfengbojiang static struct encaptab *
encap_attach(struct encaptab_head * head,const struct encap_config * cfg,void * arg,int mflags)236*22ce4affSfengbojiang encap_attach(struct encaptab_head *head, const struct encap_config *cfg,
237*22ce4affSfengbojiang     void *arg, int mflags)
238a9643ea8Slogwang {
239*22ce4affSfengbojiang 	struct encaptab *ep, *tmp;
240a9643ea8Slogwang 
241*22ce4affSfengbojiang 	if (cfg == NULL || cfg->input == NULL ||
242*22ce4affSfengbojiang 	    (cfg->check == NULL && cfg->lookup == NULL) ||
243*22ce4affSfengbojiang 	    (cfg->lookup != NULL && cfg->exact_match != ENCAP_DRV_LOOKUP) ||
244*22ce4affSfengbojiang 	    (cfg->exact_match == ENCAP_DRV_LOOKUP && cfg->lookup == NULL))
245*22ce4affSfengbojiang 		return (NULL);
246*22ce4affSfengbojiang 
247*22ce4affSfengbojiang 	ep = malloc(sizeof(*ep), M_NETADDR, mflags);
248*22ce4affSfengbojiang 	if (ep == NULL)
249*22ce4affSfengbojiang 		return (NULL);
250*22ce4affSfengbojiang 
251*22ce4affSfengbojiang 	ep->proto = cfg->proto;
252*22ce4affSfengbojiang 	ep->min_length = cfg->min_length;
253*22ce4affSfengbojiang 	ep->exact_match = cfg->exact_match;
254*22ce4affSfengbojiang 	ep->arg = arg;
255*22ce4affSfengbojiang 	ep->lookup = cfg->exact_match == ENCAP_DRV_LOOKUP ? cfg->lookup: NULL;
256*22ce4affSfengbojiang 	ep->check = cfg->exact_match != ENCAP_DRV_LOOKUP ? cfg->check: NULL;
257*22ce4affSfengbojiang 	ep->input = cfg->input;
258*22ce4affSfengbojiang 
259*22ce4affSfengbojiang 	ENCAP_WLOCK();
260*22ce4affSfengbojiang 	CK_LIST_FOREACH(tmp, head, chain) {
261*22ce4affSfengbojiang 		if (tmp->exact_match <= ep->exact_match)
262*22ce4affSfengbojiang 			break;
263a9643ea8Slogwang 	}
264*22ce4affSfengbojiang 	if (tmp == NULL)
265*22ce4affSfengbojiang 		CK_LIST_INSERT_HEAD(head, ep, chain);
266*22ce4affSfengbojiang 	else
267*22ce4affSfengbojiang 		CK_LIST_INSERT_BEFORE(tmp, ep, chain);
268*22ce4affSfengbojiang 	ENCAP_WUNLOCK();
269*22ce4affSfengbojiang 	return (ep);
270a9643ea8Slogwang }
271a9643ea8Slogwang 
272*22ce4affSfengbojiang static int
encap_detach(struct encaptab_head * head,const struct encaptab * cookie)273*22ce4affSfengbojiang encap_detach(struct encaptab_head *head, const struct encaptab *cookie)
274a9643ea8Slogwang {
275*22ce4affSfengbojiang 	struct encaptab *ep;
276a9643ea8Slogwang 
277*22ce4affSfengbojiang 	ENCAP_WLOCK();
278*22ce4affSfengbojiang 	CK_LIST_FOREACH(ep, head, chain) {
279*22ce4affSfengbojiang 		if (ep == cookie) {
280*22ce4affSfengbojiang 			CK_LIST_REMOVE(ep, chain);
281*22ce4affSfengbojiang 			ENCAP_WUNLOCK();
282*22ce4affSfengbojiang 			ENCAP_WAIT();
283*22ce4affSfengbojiang 			free(ep, M_NETADDR);
284*22ce4affSfengbojiang 			return (0);
285a9643ea8Slogwang 		}
286a9643ea8Slogwang 	}
287*22ce4affSfengbojiang 	ENCAP_WUNLOCK();
288*22ce4affSfengbojiang 	return (EINVAL);
289*22ce4affSfengbojiang }
290*22ce4affSfengbojiang 
291*22ce4affSfengbojiang static int
encap_input(struct encaptab_head * head,struct mbuf * m,int off,int proto)292*22ce4affSfengbojiang encap_input(struct encaptab_head *head, struct mbuf *m, int off, int proto)
293*22ce4affSfengbojiang {
294*22ce4affSfengbojiang 	ENCAP_RLOCK_TRACKER;
295*22ce4affSfengbojiang 	struct encaptab *ep, *match;
296*22ce4affSfengbojiang 	void *arg;
297*22ce4affSfengbojiang 	int matchprio, ret;
298*22ce4affSfengbojiang 
299*22ce4affSfengbojiang 	match = NULL;
300*22ce4affSfengbojiang 	matchprio = 0;
301*22ce4affSfengbojiang 
302*22ce4affSfengbojiang 	ENCAP_RLOCK();
303*22ce4affSfengbojiang 	CK_LIST_FOREACH(ep, head, chain) {
304*22ce4affSfengbojiang 		if (ep->proto >= 0 && ep->proto != proto)
305*22ce4affSfengbojiang 			continue;
306*22ce4affSfengbojiang 		if (ep->min_length > m->m_pkthdr.len)
307*22ce4affSfengbojiang 			continue;
308*22ce4affSfengbojiang 		if (ep->exact_match == ENCAP_DRV_LOOKUP)
309*22ce4affSfengbojiang 			ret = (*ep->lookup)(m, off, proto, &arg);
310*22ce4affSfengbojiang 		else
311*22ce4affSfengbojiang 			ret = (*ep->check)(m, off, proto, ep->arg);
312*22ce4affSfengbojiang 		if (ret <= 0)
313*22ce4affSfengbojiang 			continue;
314*22ce4affSfengbojiang 		if (ret > matchprio) {
315*22ce4affSfengbojiang 			match = ep;
316*22ce4affSfengbojiang 			if (ep->exact_match != ENCAP_DRV_LOOKUP)
317*22ce4affSfengbojiang 				arg = ep->arg;
318*22ce4affSfengbojiang 			/*
319*22ce4affSfengbojiang 			 * No need to continue the search, we got the
320*22ce4affSfengbojiang 			 * exact match.
321*22ce4affSfengbojiang 			 */
322*22ce4affSfengbojiang 			if (ret >= ep->exact_match)
323*22ce4affSfengbojiang 				break;
324*22ce4affSfengbojiang 			matchprio = ret;
325*22ce4affSfengbojiang 		}
326*22ce4affSfengbojiang 	}
327*22ce4affSfengbojiang 
328*22ce4affSfengbojiang 	if (match != NULL) {
329*22ce4affSfengbojiang 		/* found a match, "match" has the best one */
330*22ce4affSfengbojiang 		ret = (*match->input)(m, off, proto, arg);
331*22ce4affSfengbojiang 		ENCAP_RUNLOCK();
332*22ce4affSfengbojiang 		MPASS(ret == IPPROTO_DONE);
333*22ce4affSfengbojiang 		return (IPPROTO_DONE);
334*22ce4affSfengbojiang 	}
335*22ce4affSfengbojiang 	ENCAP_RUNLOCK();
336*22ce4affSfengbojiang 	return (0);
337*22ce4affSfengbojiang }
338*22ce4affSfengbojiang 
339*22ce4affSfengbojiang #ifdef INET
340*22ce4affSfengbojiang const struct srcaddrtab *
ip_encap_register_srcaddr(encap_srcaddr_t func,void * arg,int mflags)341*22ce4affSfengbojiang ip_encap_register_srcaddr(encap_srcaddr_t func, void *arg, int mflags)
342*22ce4affSfengbojiang {
343*22ce4affSfengbojiang 
344*22ce4affSfengbojiang 	return (encap_register_srcaddr(&ipv4_srcaddrtab, func, arg, mflags));
345*22ce4affSfengbojiang }
346*22ce4affSfengbojiang 
347*22ce4affSfengbojiang int
ip_encap_unregister_srcaddr(const struct srcaddrtab * cookie)348*22ce4affSfengbojiang ip_encap_unregister_srcaddr(const struct srcaddrtab *cookie)
349*22ce4affSfengbojiang {
350*22ce4affSfengbojiang 
351*22ce4affSfengbojiang 	return (encap_unregister_srcaddr(&ipv4_srcaddrtab, cookie));
352*22ce4affSfengbojiang }
353*22ce4affSfengbojiang 
354*22ce4affSfengbojiang const struct encaptab *
ip_encap_attach(const struct encap_config * cfg,void * arg,int mflags)355*22ce4affSfengbojiang ip_encap_attach(const struct encap_config *cfg, void *arg, int mflags)
356*22ce4affSfengbojiang {
357*22ce4affSfengbojiang 
358*22ce4affSfengbojiang 	return (encap_attach(&ipv4_encaptab, cfg, arg, mflags));
359*22ce4affSfengbojiang }
360*22ce4affSfengbojiang 
361*22ce4affSfengbojiang int
ip_encap_detach(const struct encaptab * cookie)362*22ce4affSfengbojiang ip_encap_detach(const struct encaptab *cookie)
363*22ce4affSfengbojiang {
364*22ce4affSfengbojiang 
365*22ce4affSfengbojiang 	return (encap_detach(&ipv4_encaptab, cookie));
366*22ce4affSfengbojiang }
367*22ce4affSfengbojiang 
368*22ce4affSfengbojiang int
encap4_input(struct mbuf ** mp,int * offp,int proto)369*22ce4affSfengbojiang encap4_input(struct mbuf **mp, int *offp, int proto)
370*22ce4affSfengbojiang {
371*22ce4affSfengbojiang 
372*22ce4affSfengbojiang 	if (encap_input(&ipv4_encaptab, *mp, *offp, proto) != IPPROTO_DONE)
373*22ce4affSfengbojiang 		return (rip_input(mp, offp, proto));
374*22ce4affSfengbojiang 	return (IPPROTO_DONE);
375*22ce4affSfengbojiang }
376*22ce4affSfengbojiang #endif /* INET */
377*22ce4affSfengbojiang 
378*22ce4affSfengbojiang #ifdef INET6
379*22ce4affSfengbojiang const struct srcaddrtab *
ip6_encap_register_srcaddr(encap_srcaddr_t func,void * arg,int mflags)380*22ce4affSfengbojiang ip6_encap_register_srcaddr(encap_srcaddr_t func, void *arg, int mflags)
381*22ce4affSfengbojiang {
382*22ce4affSfengbojiang 
383*22ce4affSfengbojiang 	return (encap_register_srcaddr(&ipv6_srcaddrtab, func, arg, mflags));
384*22ce4affSfengbojiang }
385*22ce4affSfengbojiang 
386*22ce4affSfengbojiang int
ip6_encap_unregister_srcaddr(const struct srcaddrtab * cookie)387*22ce4affSfengbojiang ip6_encap_unregister_srcaddr(const struct srcaddrtab *cookie)
388*22ce4affSfengbojiang {
389*22ce4affSfengbojiang 
390*22ce4affSfengbojiang 	return (encap_unregister_srcaddr(&ipv6_srcaddrtab, cookie));
391*22ce4affSfengbojiang }
392*22ce4affSfengbojiang 
393*22ce4affSfengbojiang const struct encaptab *
ip6_encap_attach(const struct encap_config * cfg,void * arg,int mflags)394*22ce4affSfengbojiang ip6_encap_attach(const struct encap_config *cfg, void *arg, int mflags)
395*22ce4affSfengbojiang {
396*22ce4affSfengbojiang 
397*22ce4affSfengbojiang 	return (encap_attach(&ipv6_encaptab, cfg, arg, mflags));
398*22ce4affSfengbojiang }
399*22ce4affSfengbojiang 
400*22ce4affSfengbojiang int
ip6_encap_detach(const struct encaptab * cookie)401*22ce4affSfengbojiang ip6_encap_detach(const struct encaptab *cookie)
402*22ce4affSfengbojiang {
403*22ce4affSfengbojiang 
404*22ce4affSfengbojiang 	return (encap_detach(&ipv6_encaptab, cookie));
405*22ce4affSfengbojiang }
406*22ce4affSfengbojiang 
407*22ce4affSfengbojiang int
encap6_input(struct mbuf ** mp,int * offp,int proto)408*22ce4affSfengbojiang encap6_input(struct mbuf **mp, int *offp, int proto)
409*22ce4affSfengbojiang {
410*22ce4affSfengbojiang 
411*22ce4affSfengbojiang 	if (encap_input(&ipv6_encaptab, *mp, *offp, proto) != IPPROTO_DONE)
412*22ce4affSfengbojiang 		return (rip6_input(mp, offp, proto));
413*22ce4affSfengbojiang 	return (IPPROTO_DONE);
414*22ce4affSfengbojiang }
415*22ce4affSfengbojiang #endif /* INET6 */
416