xref: /freebsd/sys/net/if_gif.c (revision 56abdd33507adcc0f92017b04687137d05fd9c08)
1686cdd19SJun-ichiro itojun Hagino /*	$FreeBSD$	*/
288ff5695SSUZUKI Shinsuke /*	$KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $	*/
3686cdd19SJun-ichiro itojun Hagino 
4c398230bSWarner Losh /*-
5cfa1ca9dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6cfa1ca9dSYoshinobu Inoue  * All rights reserved.
7cfa1ca9dSYoshinobu Inoue  *
8cfa1ca9dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
9cfa1ca9dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
10cfa1ca9dSYoshinobu Inoue  * are met:
11cfa1ca9dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
12cfa1ca9dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
13cfa1ca9dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
14cfa1ca9dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
15cfa1ca9dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
16cfa1ca9dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
17cfa1ca9dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
18cfa1ca9dSYoshinobu Inoue  *    without specific prior written permission.
19cfa1ca9dSYoshinobu Inoue  *
20cfa1ca9dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21cfa1ca9dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22cfa1ca9dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23cfa1ca9dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24cfa1ca9dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25cfa1ca9dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26cfa1ca9dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27cfa1ca9dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28cfa1ca9dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29cfa1ca9dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30cfa1ca9dSYoshinobu Inoue  * SUCH DAMAGE.
31cfa1ca9dSYoshinobu Inoue  */
32cfa1ca9dSYoshinobu Inoue 
33cfa1ca9dSYoshinobu Inoue #include "opt_inet.h"
34cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
35e0852ce2SRobert Watson #include "opt_mac.h"
36cfa1ca9dSYoshinobu Inoue 
37cfa1ca9dSYoshinobu Inoue #include <sys/param.h>
38cfa1ca9dSYoshinobu Inoue #include <sys/systm.h>
39cfa1ca9dSYoshinobu Inoue #include <sys/kernel.h>
40cfa1ca9dSYoshinobu Inoue #include <sys/malloc.h>
41cfa1ca9dSYoshinobu Inoue #include <sys/mbuf.h>
425dba30f1SPoul-Henning Kamp #include <sys/module.h>
43cfa1ca9dSYoshinobu Inoue #include <sys/socket.h>
44cfa1ca9dSYoshinobu Inoue #include <sys/sockio.h>
45cfa1ca9dSYoshinobu Inoue #include <sys/errno.h>
46cfa1ca9dSYoshinobu Inoue #include <sys/time.h>
47872f786aSBrooks Davis #include <sys/sysctl.h>
48cfa1ca9dSYoshinobu Inoue #include <sys/syslog.h>
49686cdd19SJun-ichiro itojun Hagino #include <sys/protosw.h>
5053dab5feSBrooks Davis #include <sys/conf.h>
51cfa1ca9dSYoshinobu Inoue #include <machine/cpu.h>
52cfa1ca9dSYoshinobu Inoue 
53cfa1ca9dSYoshinobu Inoue #include <net/if.h>
54f889d2efSBrooks Davis #include <net/if_clone.h>
55cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
56cfa1ca9dSYoshinobu Inoue #include <net/netisr.h>
57cfa1ca9dSYoshinobu Inoue #include <net/route.h>
58cfa1ca9dSYoshinobu Inoue #include <net/bpf.h>
59cfa1ca9dSYoshinobu Inoue 
60cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
61cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h>
62cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h>
6333841545SHajimu UMEMOTO #ifdef	INET
6433841545SHajimu UMEMOTO #include <netinet/in_var.h>
65cfa1ca9dSYoshinobu Inoue #include <netinet/in_gif.h>
6653dab5feSBrooks Davis #include <netinet/ip_var.h>
67cfa1ca9dSYoshinobu Inoue #endif	/* INET */
68cfa1ca9dSYoshinobu Inoue 
69cfa1ca9dSYoshinobu Inoue #ifdef INET6
70cfa1ca9dSYoshinobu Inoue #ifndef INET
71cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
72cfa1ca9dSYoshinobu Inoue #endif
73cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_var.h>
74cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
75cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
76a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
77cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_gif.h>
78686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6protosw.h>
79cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
80cfa1ca9dSYoshinobu Inoue 
81686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h>
8273ff045cSAndrew Thompson #include <net/ethernet.h>
8373ff045cSAndrew Thompson #include <net/if_bridgevar.h>
84cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h>
85cfa1ca9dSYoshinobu Inoue 
86aed55708SRobert Watson #include <security/mac/mac_framework.h>
87aed55708SRobert Watson 
8853dab5feSBrooks Davis #define GIFNAME		"gif"
89686cdd19SJun-ichiro itojun Hagino 
9017d5cb2dSRobert Watson /*
918c7e1947SRuslan Ermilov  * gif_mtx protects the global gif_softc_list.
9217d5cb2dSRobert Watson  */
9317d5cb2dSRobert Watson static struct mtx gif_mtx;
9453dab5feSBrooks Davis static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
95e8783c4dSMike Smith static LIST_HEAD(, gif_softc) gif_softc_list;
9653dab5feSBrooks Davis 
9794408d94SBrooks Davis void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
9894408d94SBrooks Davis void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
9994408d94SBrooks Davis void	(*ng_gif_attach_p)(struct ifnet *ifp);
10094408d94SBrooks Davis void	(*ng_gif_detach_p)(struct ifnet *ifp);
10194408d94SBrooks Davis 
10273ff045cSAndrew Thompson static void	gif_start(struct ifnet *);
1036b7330e2SSam Leffler static int	gif_clone_create(struct if_clone *, int, caddr_t);
104bb2bfb4fSBrooks Davis static void	gif_clone_destroy(struct ifnet *);
10553dab5feSBrooks Davis 
106f889d2efSBrooks Davis IFC_SIMPLE_DECLARE(gif, 0);
10753dab5feSBrooks Davis 
108929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *);
109cfa1ca9dSYoshinobu Inoue 
110872f786aSBrooks Davis SYSCTL_DECL(_net_link);
111872f786aSBrooks Davis SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
112872f786aSBrooks Davis     "Generic Tunnel Interface");
113686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST
114686cdd19SJun-ichiro itojun Hagino /*
115872f786aSBrooks Davis  * This macro controls the default upper limitation on nesting of gif tunnels.
116686cdd19SJun-ichiro itojun Hagino  * Since, setting a large value to this macro with a careless configuration
117686cdd19SJun-ichiro itojun Hagino  * may introduce system crash, we don't allow any nestings by default.
118686cdd19SJun-ichiro itojun Hagino  * If you need to configure nested gif tunnels, you can define this macro
119686cdd19SJun-ichiro itojun Hagino  * in your kernel configuration file.  However, if you do so, please be
120686cdd19SJun-ichiro itojun Hagino  * careful to configure the tunnels so that it won't make a loop.
121686cdd19SJun-ichiro itojun Hagino  */
122686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1
123686cdd19SJun-ichiro itojun Hagino #endif
124686cdd19SJun-ichiro itojun Hagino static int max_gif_nesting = MAX_GIF_NEST;
125872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
126872f786aSBrooks Davis     &max_gif_nesting, 0, "Max nested tunnels");
127872f786aSBrooks Davis 
128872f786aSBrooks Davis /*
129872f786aSBrooks Davis  * By default, we disallow creation of multiple tunnels between the same
130872f786aSBrooks Davis  * pair of addresses.  Some applications require this functionality so
131872f786aSBrooks Davis  * we allow control over this check here.
132872f786aSBrooks Davis  */
133872f786aSBrooks Davis #ifdef XBONEHACK
134872f786aSBrooks Davis static int parallel_tunnels = 1;
135872f786aSBrooks Davis #else
136872f786aSBrooks Davis static int parallel_tunnels = 0;
137872f786aSBrooks Davis #endif
138872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
139872f786aSBrooks Davis     &parallel_tunnels, 0, "Allow parallel tunnels?");
140cfa1ca9dSYoshinobu Inoue 
14156abdd33SAndrew Thompson /* copy from src/sys/net/if_ethersubr.c */
14256abdd33SAndrew Thompson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
14356abdd33SAndrew Thompson 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
14456abdd33SAndrew Thompson #ifndef ETHER_IS_BROADCAST
14556abdd33SAndrew Thompson #define ETHER_IS_BROADCAST(addr) \
14656abdd33SAndrew Thompson 	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
14756abdd33SAndrew Thompson #endif
14856abdd33SAndrew Thompson 
149bb2bfb4fSBrooks Davis static int
1506b7330e2SSam Leffler gif_clone_create(ifc, unit, params)
15153dab5feSBrooks Davis 	struct if_clone *ifc;
1523b16e7b2SMaxime Henrion 	int unit;
1536b7330e2SSam Leffler 	caddr_t params;
154cfa1ca9dSYoshinobu Inoue {
15533841545SHajimu UMEMOTO 	struct gif_softc *sc;
156cfa1ca9dSYoshinobu Inoue 
157e1a8c3dcSBruce M Simpson 	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
158fc74a9f9SBrooks Davis 	GIF2IFP(sc) = if_alloc(IFT_GIF);
159fc74a9f9SBrooks Davis 	if (GIF2IFP(sc) == NULL) {
160fc74a9f9SBrooks Davis 		free(sc, M_GIF);
161fc74a9f9SBrooks Davis 		return (ENOSPC);
162fc74a9f9SBrooks Davis 	}
16353dab5feSBrooks Davis 
16425af0bb5SGleb Smirnoff 	GIF_LOCK_INIT(sc);
16525af0bb5SGleb Smirnoff 
166fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_softc = sc;
167fc74a9f9SBrooks Davis 	if_initname(GIF2IFP(sc), ifc->ifc_name, unit);
168686cdd19SJun-ichiro itojun Hagino 
1699426aedfSHajimu UMEMOTO 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
1709426aedfSHajimu UMEMOTO 
171fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_addrlen = 0;
172fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_mtu    = GIF_MTU;
173fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
17433841545SHajimu UMEMOTO #if 0
17533841545SHajimu UMEMOTO 	/* turn off ingress filter */
176fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  |= IFF_LINK2;
17733841545SHajimu UMEMOTO #endif
178fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
17973ff045cSAndrew Thompson 	GIF2IFP(sc)->if_start  = gif_start;
180fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_output = gif_output;
181fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
182fc74a9f9SBrooks Davis 	if_attach(GIF2IFP(sc));
18301399f34SDavid Malone 	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
18494408d94SBrooks Davis 	if (ng_gif_attach_p != NULL)
185fc74a9f9SBrooks Davis 		(*ng_gif_attach_p)(GIF2IFP(sc));
18625af0bb5SGleb Smirnoff 
18725af0bb5SGleb Smirnoff 	mtx_lock(&gif_mtx);
18825af0bb5SGleb Smirnoff 	LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
18925af0bb5SGleb Smirnoff 	mtx_unlock(&gif_mtx);
19025af0bb5SGleb Smirnoff 
19125af0bb5SGleb Smirnoff 	return (0);
192cfa1ca9dSYoshinobu Inoue }
193cfa1ca9dSYoshinobu Inoue 
19417d5cb2dSRobert Watson static void
195febd0759SAndrew Thompson gif_clone_destroy(ifp)
196febd0759SAndrew Thompson 	struct ifnet *ifp;
19753dab5feSBrooks Davis {
19853dab5feSBrooks Davis 	int err;
199febd0759SAndrew Thompson 	struct gif_softc *sc = ifp->if_softc;
200febd0759SAndrew Thompson 
201febd0759SAndrew Thompson 	mtx_lock(&gif_mtx);
202febd0759SAndrew Thompson 	LIST_REMOVE(sc, gif_list);
203febd0759SAndrew Thompson 	mtx_unlock(&gif_mtx);
20453dab5feSBrooks Davis 
20517d5cb2dSRobert Watson 	gif_delete_tunnel(ifp);
2069426aedfSHajimu UMEMOTO #ifdef INET6
20753dab5feSBrooks Davis 	if (sc->encap_cookie6 != NULL) {
20853dab5feSBrooks Davis 		err = encap_detach(sc->encap_cookie6);
20953dab5feSBrooks Davis 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
21053dab5feSBrooks Davis 	}
2119426aedfSHajimu UMEMOTO #endif
2129426aedfSHajimu UMEMOTO #ifdef INET
2139426aedfSHajimu UMEMOTO 	if (sc->encap_cookie4 != NULL) {
2149426aedfSHajimu UMEMOTO 		err = encap_detach(sc->encap_cookie4);
2159426aedfSHajimu UMEMOTO 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
2169426aedfSHajimu UMEMOTO 	}
2179426aedfSHajimu UMEMOTO #endif
21853dab5feSBrooks Davis 
21994408d94SBrooks Davis 	if (ng_gif_detach_p != NULL)
22094408d94SBrooks Davis 		(*ng_gif_detach_p)(ifp);
22153dab5feSBrooks Davis 	bpfdetach(ifp);
22253dab5feSBrooks Davis 	if_detach(ifp);
223fc74a9f9SBrooks Davis 	if_free(ifp);
22453dab5feSBrooks Davis 
22525af0bb5SGleb Smirnoff 	GIF_LOCK_DESTROY(sc);
22625af0bb5SGleb Smirnoff 
22753dab5feSBrooks Davis 	free(sc, M_GIF);
22853dab5feSBrooks Davis }
22953dab5feSBrooks Davis 
23053dab5feSBrooks Davis static int
23153dab5feSBrooks Davis gifmodevent(mod, type, data)
23253dab5feSBrooks Davis 	module_t mod;
23353dab5feSBrooks Davis 	int type;
23453dab5feSBrooks Davis 	void *data;
23553dab5feSBrooks Davis {
23653dab5feSBrooks Davis 
23753dab5feSBrooks Davis 	switch (type) {
23853dab5feSBrooks Davis 	case MOD_LOAD:
23917d5cb2dSRobert Watson 		mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
24020af0ffaSBrooks Davis 		LIST_INIT(&gif_softc_list);
24153dab5feSBrooks Davis 		if_clone_attach(&gif_cloner);
24253dab5feSBrooks Davis 
24353dab5feSBrooks Davis #ifdef INET6
24453dab5feSBrooks Davis 		ip6_gif_hlim = GIF_HLIM;
24553dab5feSBrooks Davis #endif
24653dab5feSBrooks Davis 
24753dab5feSBrooks Davis 		break;
24853dab5feSBrooks Davis 	case MOD_UNLOAD:
24953dab5feSBrooks Davis 		if_clone_detach(&gif_cloner);
25017d5cb2dSRobert Watson 		mtx_destroy(&gif_mtx);
25153dab5feSBrooks Davis #ifdef INET6
25253dab5feSBrooks Davis 		ip6_gif_hlim = 0;
25353dab5feSBrooks Davis #endif
25453dab5feSBrooks Davis 		break;
2553e019deaSPoul-Henning Kamp 	default:
2563e019deaSPoul-Henning Kamp 		return EOPNOTSUPP;
25753dab5feSBrooks Davis 	}
25853dab5feSBrooks Davis 	return 0;
25953dab5feSBrooks Davis }
26053dab5feSBrooks Davis 
26153dab5feSBrooks Davis static moduledata_t gif_mod = {
26253dab5feSBrooks Davis 	"if_gif",
26353dab5feSBrooks Davis 	gifmodevent,
26453dab5feSBrooks Davis 	0
26553dab5feSBrooks Davis };
26653dab5feSBrooks Davis 
26753dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
26820af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1);
269cfa1ca9dSYoshinobu Inoue 
2709426aedfSHajimu UMEMOTO int
271686cdd19SJun-ichiro itojun Hagino gif_encapcheck(m, off, proto, arg)
272686cdd19SJun-ichiro itojun Hagino 	const struct mbuf *m;
273686cdd19SJun-ichiro itojun Hagino 	int off;
274686cdd19SJun-ichiro itojun Hagino 	int proto;
275686cdd19SJun-ichiro itojun Hagino 	void *arg;
276686cdd19SJun-ichiro itojun Hagino {
277686cdd19SJun-ichiro itojun Hagino 	struct ip ip;
278686cdd19SJun-ichiro itojun Hagino 	struct gif_softc *sc;
279686cdd19SJun-ichiro itojun Hagino 
280686cdd19SJun-ichiro itojun Hagino 	sc = (struct gif_softc *)arg;
281686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL)
282686cdd19SJun-ichiro itojun Hagino 		return 0;
283686cdd19SJun-ichiro itojun Hagino 
284fc74a9f9SBrooks Davis 	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
285686cdd19SJun-ichiro itojun Hagino 		return 0;
286686cdd19SJun-ichiro itojun Hagino 
287686cdd19SJun-ichiro itojun Hagino 	/* no physical address */
288686cdd19SJun-ichiro itojun Hagino 	if (!sc->gif_psrc || !sc->gif_pdst)
289686cdd19SJun-ichiro itojun Hagino 		return 0;
290686cdd19SJun-ichiro itojun Hagino 
291686cdd19SJun-ichiro itojun Hagino 	switch (proto) {
292686cdd19SJun-ichiro itojun Hagino #ifdef INET
293686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV4:
294686cdd19SJun-ichiro itojun Hagino 		break;
295686cdd19SJun-ichiro itojun Hagino #endif
296686cdd19SJun-ichiro itojun Hagino #ifdef INET6
297686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV6:
298686cdd19SJun-ichiro itojun Hagino 		break;
299686cdd19SJun-ichiro itojun Hagino #endif
30073ff045cSAndrew Thompson 	case IPPROTO_ETHERIP:
30173ff045cSAndrew Thompson 		break;
30273ff045cSAndrew Thompson 
303686cdd19SJun-ichiro itojun Hagino 	default:
304686cdd19SJun-ichiro itojun Hagino 		return 0;
305686cdd19SJun-ichiro itojun Hagino 	}
306686cdd19SJun-ichiro itojun Hagino 
3073bb61ca6SHajimu UMEMOTO 	/* Bail on short packets */
3083bb61ca6SHajimu UMEMOTO 	if (m->m_pkthdr.len < sizeof(ip))
3093bb61ca6SHajimu UMEMOTO 		return 0;
3103bb61ca6SHajimu UMEMOTO 
3116f4ded3aSBrooks Davis 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
312686cdd19SJun-ichiro itojun Hagino 
313686cdd19SJun-ichiro itojun Hagino 	switch (ip.ip_v) {
314686cdd19SJun-ichiro itojun Hagino #ifdef INET
315686cdd19SJun-ichiro itojun Hagino 	case 4:
316686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET ||
317686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET)
318686cdd19SJun-ichiro itojun Hagino 			return 0;
319686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck4(m, off, proto, arg);
320686cdd19SJun-ichiro itojun Hagino #endif
321686cdd19SJun-ichiro itojun Hagino #ifdef INET6
322686cdd19SJun-ichiro itojun Hagino 	case 6:
3239426aedfSHajimu UMEMOTO 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
3249426aedfSHajimu UMEMOTO 			return 0;
325686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET6 ||
326686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET6)
327686cdd19SJun-ichiro itojun Hagino 			return 0;
328686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck6(m, off, proto, arg);
329686cdd19SJun-ichiro itojun Hagino #endif
330686cdd19SJun-ichiro itojun Hagino 	default:
331686cdd19SJun-ichiro itojun Hagino 		return 0;
332686cdd19SJun-ichiro itojun Hagino 	}
333686cdd19SJun-ichiro itojun Hagino }
334686cdd19SJun-ichiro itojun Hagino 
33573ff045cSAndrew Thompson static void
33673ff045cSAndrew Thompson gif_start(struct ifnet *ifp)
33773ff045cSAndrew Thompson {
33873ff045cSAndrew Thompson 	struct gif_softc *sc;
33973ff045cSAndrew Thompson 	struct mbuf *m;
34073ff045cSAndrew Thompson 
34173ff045cSAndrew Thompson 	sc = ifp->if_softc;
34273ff045cSAndrew Thompson 
34373ff045cSAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
34473ff045cSAndrew Thompson 	for (;;) {
34573ff045cSAndrew Thompson 		IFQ_DEQUEUE(&ifp->if_snd, m);
34673ff045cSAndrew Thompson 		if (m == 0)
34773ff045cSAndrew Thompson 			break;
34873ff045cSAndrew Thompson 
34973ff045cSAndrew Thompson 		gif_output(ifp, m, sc->gif_pdst, NULL);
35073ff045cSAndrew Thompson 
35173ff045cSAndrew Thompson 	}
35273ff045cSAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
35373ff045cSAndrew Thompson 
35473ff045cSAndrew Thompson 	return;
35573ff045cSAndrew Thompson }
35673ff045cSAndrew Thompson 
357cfa1ca9dSYoshinobu Inoue int
358cfa1ca9dSYoshinobu Inoue gif_output(ifp, m, dst, rt)
359cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
360cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
361cfa1ca9dSYoshinobu Inoue 	struct sockaddr *dst;
362cfa1ca9dSYoshinobu Inoue 	struct rtentry *rt;	/* added in net2 */
363cfa1ca9dSYoshinobu Inoue {
364fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
3658c7e1947SRuslan Ermilov 	struct m_tag *mtag;
366cfa1ca9dSYoshinobu Inoue 	int error = 0;
3678c7e1947SRuslan Ermilov 	int gif_called;
36801399f34SDavid Malone 	u_int32_t af;
369cfa1ca9dSYoshinobu Inoue 
37010722b85SRobert Watson #ifdef MAC
37130d239bcSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
372e0852ce2SRobert Watson 	if (error) {
373e0852ce2SRobert Watson 		m_freem(m);
374e0852ce2SRobert Watson 		goto end;
375e0852ce2SRobert Watson 	}
37610722b85SRobert Watson #endif
37710722b85SRobert Watson 
378cfa1ca9dSYoshinobu Inoue 	/*
379cfa1ca9dSYoshinobu Inoue 	 * gif may cause infinite recursion calls when misconfigured.
3808c7e1947SRuslan Ermilov 	 * We'll prevent this by detecting loops.
3818c7e1947SRuslan Ermilov 	 *
3828c7e1947SRuslan Ermilov 	 * High nesting level may cause stack exhaustion.
383cfa1ca9dSYoshinobu Inoue 	 * We'll prevent this by introducing upper limit.
384cfa1ca9dSYoshinobu Inoue 	 */
3858c7e1947SRuslan Ermilov 	gif_called = 1;
3868c7e1947SRuslan Ermilov 	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
3878c7e1947SRuslan Ermilov 	while (mtag != NULL) {
3888c7e1947SRuslan Ermilov 		if (*(struct ifnet **)(mtag + 1) == ifp) {
3898c7e1947SRuslan Ermilov 			log(LOG_NOTICE,
3908c7e1947SRuslan Ermilov 			    "gif_output: loop detected on %s\n",
3918c7e1947SRuslan Ermilov 			    (*(struct ifnet **)(mtag + 1))->if_xname);
3928c7e1947SRuslan Ermilov 			m_freem(m);
3938c7e1947SRuslan Ermilov 			error = EIO;	/* is there better errno? */
3948c7e1947SRuslan Ermilov 			goto end;
3958c7e1947SRuslan Ermilov 		}
3968c7e1947SRuslan Ermilov 		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
3978c7e1947SRuslan Ermilov 		gif_called++;
3988c7e1947SRuslan Ermilov 	}
3998c7e1947SRuslan Ermilov 	if (gif_called > max_gif_nesting) {
400cfa1ca9dSYoshinobu Inoue 		log(LOG_NOTICE,
401cfa1ca9dSYoshinobu Inoue 		    "gif_output: recursively called too many times(%d)\n",
402523ebc4eSRobert Watson 		    gif_called);
403cfa1ca9dSYoshinobu Inoue 		m_freem(m);
404cfa1ca9dSYoshinobu Inoue 		error = EIO;	/* is there better errno? */
405cfa1ca9dSYoshinobu Inoue 		goto end;
406cfa1ca9dSYoshinobu Inoue 	}
4078c7e1947SRuslan Ermilov 	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
4088c7e1947SRuslan Ermilov 	    M_NOWAIT);
4098c7e1947SRuslan Ermilov 	if (mtag == NULL) {
4108c7e1947SRuslan Ermilov 		m_freem(m);
4118c7e1947SRuslan Ermilov 		error = ENOMEM;
4128c7e1947SRuslan Ermilov 		goto end;
4138c7e1947SRuslan Ermilov 	}
4148c7e1947SRuslan Ermilov 	*(struct ifnet **)(mtag + 1) = ifp;
4158c7e1947SRuslan Ermilov 	m_tag_prepend(m, mtag);
416686cdd19SJun-ichiro itojun Hagino 
417cfa1ca9dSYoshinobu Inoue 	m->m_flags &= ~(M_BCAST|M_MCAST);
41825af0bb5SGleb Smirnoff 
41925af0bb5SGleb Smirnoff 	GIF_LOCK(sc);
42025af0bb5SGleb Smirnoff 
421cfa1ca9dSYoshinobu Inoue 	if (!(ifp->if_flags & IFF_UP) ||
422cfa1ca9dSYoshinobu Inoue 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
4236e860629SGleb Smirnoff 		GIF_UNLOCK(sc);
424cfa1ca9dSYoshinobu Inoue 		m_freem(m);
425cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
426cfa1ca9dSYoshinobu Inoue 		goto end;
427cfa1ca9dSYoshinobu Inoue 	}
428cfa1ca9dSYoshinobu Inoue 
42901399f34SDavid Malone 	/* BPF writes need to be handled specially. */
43001399f34SDavid Malone 	if (dst->sa_family == AF_UNSPEC) {
43101399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
43201399f34SDavid Malone 		dst->sa_family = af;
43301399f34SDavid Malone 	}
43401399f34SDavid Malone 
43501399f34SDavid Malone 	af = dst->sa_family;
43616d878ccSChristian S.J. Peron 	BPF_MTAP2(ifp, &af, sizeof(af), m);
437cfa1ca9dSYoshinobu Inoue 	ifp->if_opackets++;
438cfa1ca9dSYoshinobu Inoue 	ifp->if_obytes += m->m_pkthdr.len;
439cfa1ca9dSYoshinobu Inoue 
44073ff045cSAndrew Thompson 	/* override to IPPROTO_ETHERIP for bridged traffic */
44173ff045cSAndrew Thompson 	if (ifp->if_bridge)
44273ff045cSAndrew Thompson 		af = AF_LINK;
44373ff045cSAndrew Thompson 
44433841545SHajimu UMEMOTO 	/* inner AF-specific encapsulation */
44533841545SHajimu UMEMOTO 
446686cdd19SJun-ichiro itojun Hagino 	/* XXX should we check if our outer source is legal? */
447686cdd19SJun-ichiro itojun Hagino 
44833841545SHajimu UMEMOTO 	/* dispatch to output logic based on outer AF */
449cfa1ca9dSYoshinobu Inoue 	switch (sc->gif_psrc->sa_family) {
450cfa1ca9dSYoshinobu Inoue #ifdef INET
451cfa1ca9dSYoshinobu Inoue 	case AF_INET:
45273ff045cSAndrew Thompson 		error = in_gif_output(ifp, af, m);
453cfa1ca9dSYoshinobu Inoue 		break;
454cfa1ca9dSYoshinobu Inoue #endif
455cfa1ca9dSYoshinobu Inoue #ifdef INET6
456cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
45773ff045cSAndrew Thompson 		error = in6_gif_output(ifp, af, m);
458cfa1ca9dSYoshinobu Inoue 		break;
459cfa1ca9dSYoshinobu Inoue #endif
460cfa1ca9dSYoshinobu Inoue 	default:
461cfa1ca9dSYoshinobu Inoue 		m_freem(m);
462cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
463cfa1ca9dSYoshinobu Inoue 	}
464cfa1ca9dSYoshinobu Inoue 
4656e860629SGleb Smirnoff 	GIF_UNLOCK(sc);
466cfa1ca9dSYoshinobu Inoue   end:
46733841545SHajimu UMEMOTO 	if (error)
46833841545SHajimu UMEMOTO 		ifp->if_oerrors++;
46925af0bb5SGleb Smirnoff 	return (error);
470cfa1ca9dSYoshinobu Inoue }
471cfa1ca9dSYoshinobu Inoue 
472cfa1ca9dSYoshinobu Inoue void
47321fb391fSHajimu UMEMOTO gif_input(m, af, ifp)
474cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
475cfa1ca9dSYoshinobu Inoue 	int af;
47621fb391fSHajimu UMEMOTO 	struct ifnet *ifp;
477cfa1ca9dSYoshinobu Inoue {
47873ff045cSAndrew Thompson 	int isr, n;
47973ff045cSAndrew Thompson 	struct etherip_header *eip;
48056abdd33SAndrew Thompson 	struct ether_header *eh;
48156abdd33SAndrew Thompson 	struct ifnet *oldifp;
482cfa1ca9dSYoshinobu Inoue 
48321fb391fSHajimu UMEMOTO 	if (ifp == NULL) {
484cfa1ca9dSYoshinobu Inoue 		/* just in case */
485cfa1ca9dSYoshinobu Inoue 		m_freem(m);
486cfa1ca9dSYoshinobu Inoue 		return;
487cfa1ca9dSYoshinobu Inoue 	}
488cfa1ca9dSYoshinobu Inoue 
48921fb391fSHajimu UMEMOTO 	m->m_pkthdr.rcvif = ifp;
490cfa1ca9dSYoshinobu Inoue 
49110722b85SRobert Watson #ifdef MAC
49230d239bcSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
49310722b85SRobert Watson #endif
49410722b85SRobert Watson 
49516d878ccSChristian S.J. Peron 	if (bpf_peers_present(ifp->if_bpf)) {
49633841545SHajimu UMEMOTO 		u_int32_t af1 = af;
497437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
498cfa1ca9dSYoshinobu Inoue 	}
499cfa1ca9dSYoshinobu Inoue 
50094408d94SBrooks Davis 	if (ng_gif_input_p != NULL) {
50121fb391fSHajimu UMEMOTO 		(*ng_gif_input_p)(ifp, &m, af);
50294408d94SBrooks Davis 		if (m == NULL)
50394408d94SBrooks Davis 			return;
50494408d94SBrooks Davis 	}
50594408d94SBrooks Davis 
506cfa1ca9dSYoshinobu Inoue 	/*
507cfa1ca9dSYoshinobu Inoue 	 * Put the packet to the network layer input queue according to the
508cfa1ca9dSYoshinobu Inoue 	 * specified address family.
509cfa1ca9dSYoshinobu Inoue 	 * Note: older versions of gif_input directly called network layer
510cfa1ca9dSYoshinobu Inoue 	 * input functions, e.g. ip6_input, here.  We changed the policy to
511cfa1ca9dSYoshinobu Inoue 	 * prevent too many recursive calls of such input functions, which
512cfa1ca9dSYoshinobu Inoue 	 * might cause kernel panic.  But the change may introduce another
513cfa1ca9dSYoshinobu Inoue 	 * problem; if the input queue is full, packets are discarded.
51488ff5695SSUZUKI Shinsuke 	 * The kernel stack overflow really happened, and we believed
51588ff5695SSUZUKI Shinsuke 	 * queue-full rarely occurs, so we changed the policy.
516cfa1ca9dSYoshinobu Inoue 	 */
517cfa1ca9dSYoshinobu Inoue 	switch (af) {
518cfa1ca9dSYoshinobu Inoue #ifdef INET
519cfa1ca9dSYoshinobu Inoue 	case AF_INET:
520cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IP;
521cfa1ca9dSYoshinobu Inoue 		break;
522cfa1ca9dSYoshinobu Inoue #endif
523cfa1ca9dSYoshinobu Inoue #ifdef INET6
524cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
525cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IPV6;
526cfa1ca9dSYoshinobu Inoue 		break;
527cfa1ca9dSYoshinobu Inoue #endif
52873ff045cSAndrew Thompson 	case AF_LINK:
52973ff045cSAndrew Thompson 		n = sizeof(struct etherip_header) + sizeof(struct ether_header);
53073ff045cSAndrew Thompson 		if (n > m->m_len) {
53173ff045cSAndrew Thompson 			m = m_pullup(m, n);
53273ff045cSAndrew Thompson 			if (m == NULL) {
53373ff045cSAndrew Thompson 				ifp->if_ierrors++;
53473ff045cSAndrew Thompson 				return;
53573ff045cSAndrew Thompson 			}
53673ff045cSAndrew Thompson 		}
53773ff045cSAndrew Thompson 
53873ff045cSAndrew Thompson 		eip = mtod(m, struct etherip_header *);
53973ff045cSAndrew Thompson  		if (eip->eip_ver !=
54073ff045cSAndrew Thompson 		    (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) {
54173ff045cSAndrew Thompson 			/* discard unknown versions */
54273ff045cSAndrew Thompson 			m_freem(m);
54373ff045cSAndrew Thompson 			return;
54473ff045cSAndrew Thompson 		}
54573ff045cSAndrew Thompson 		m_adj(m, sizeof(struct etherip_header));
54673ff045cSAndrew Thompson 
54773ff045cSAndrew Thompson 		m->m_flags &= ~(M_BCAST|M_MCAST);
54873ff045cSAndrew Thompson 		m->m_pkthdr.rcvif = ifp;
54973ff045cSAndrew Thompson 
55056abdd33SAndrew Thompson 		if (ifp->if_bridge) {
55156abdd33SAndrew Thompson 			oldifp = ifp;
55256abdd33SAndrew Thompson 			eh = mtod(m, struct ether_header *);
55356abdd33SAndrew Thompson 			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
55456abdd33SAndrew Thompson 				if (ETHER_IS_BROADCAST(eh->ether_dhost))
55556abdd33SAndrew Thompson 					m->m_flags |= M_BCAST;
55656abdd33SAndrew Thompson 				else
55756abdd33SAndrew Thompson 					m->m_flags |= M_MCAST;
55856abdd33SAndrew Thompson 				ifp->if_imcasts++;
55956abdd33SAndrew Thompson 			}
56073ff045cSAndrew Thompson 			BRIDGE_INPUT(ifp, m);
56173ff045cSAndrew Thompson 
56256abdd33SAndrew Thompson 			if (m != NULL && ifp != oldifp) {
56356abdd33SAndrew Thompson 				/*
56456abdd33SAndrew Thompson 				 * The bridge gave us back itself or one of the
56556abdd33SAndrew Thompson 				 * members for which the frame is addressed.
56656abdd33SAndrew Thompson 				 */
56756abdd33SAndrew Thompson 				ether_demux(ifp, m);
56856abdd33SAndrew Thompson 				return;
56956abdd33SAndrew Thompson 			}
57056abdd33SAndrew Thompson 		}
57173ff045cSAndrew Thompson 		if (m != NULL)
57273ff045cSAndrew Thompson 			m_freem(m);
57373ff045cSAndrew Thompson 		return;
57473ff045cSAndrew Thompson 
575cfa1ca9dSYoshinobu Inoue 	default:
57694408d94SBrooks Davis 		if (ng_gif_input_orphan_p != NULL)
57721fb391fSHajimu UMEMOTO 			(*ng_gif_input_orphan_p)(ifp, m, af);
57894408d94SBrooks Davis 		else
579cfa1ca9dSYoshinobu Inoue 			m_freem(m);
580cfa1ca9dSYoshinobu Inoue 		return;
581cfa1ca9dSYoshinobu Inoue 	}
582cfa1ca9dSYoshinobu Inoue 
58321fb391fSHajimu UMEMOTO 	ifp->if_ipackets++;
58421fb391fSHajimu UMEMOTO 	ifp->if_ibytes += m->m_pkthdr.len;
5851cafed39SJonathan Lemon 	netisr_dispatch(isr, m);
586cfa1ca9dSYoshinobu Inoue }
587cfa1ca9dSYoshinobu Inoue 
588686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
589cfa1ca9dSYoshinobu Inoue int
590cfa1ca9dSYoshinobu Inoue gif_ioctl(ifp, cmd, data)
591cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
592cfa1ca9dSYoshinobu Inoue 	u_long cmd;
593cfa1ca9dSYoshinobu Inoue 	caddr_t data;
594cfa1ca9dSYoshinobu Inoue {
595fc74a9f9SBrooks Davis 	struct gif_softc *sc  = ifp->if_softc;
596cfa1ca9dSYoshinobu Inoue 	struct ifreq     *ifr = (struct ifreq*)data;
597cfa1ca9dSYoshinobu Inoue 	int error = 0, size;
598686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *dst, *src;
5993bb61ca6SHajimu UMEMOTO #ifdef	SIOCSIFMTU /* xxx */
6003bb61ca6SHajimu UMEMOTO 	u_long mtu;
6013bb61ca6SHajimu UMEMOTO #endif
602cfa1ca9dSYoshinobu Inoue 
603cfa1ca9dSYoshinobu Inoue 	switch (cmd) {
604cfa1ca9dSYoshinobu Inoue 	case SIOCSIFADDR:
6059426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_UP;
606cfa1ca9dSYoshinobu Inoue 		break;
607cfa1ca9dSYoshinobu Inoue 
608cfa1ca9dSYoshinobu Inoue 	case SIOCSIFDSTADDR:
609cfa1ca9dSYoshinobu Inoue 		break;
610cfa1ca9dSYoshinobu Inoue 
611cfa1ca9dSYoshinobu Inoue 	case SIOCADDMULTI:
612cfa1ca9dSYoshinobu Inoue 	case SIOCDELMULTI:
613cfa1ca9dSYoshinobu Inoue 		break;
614cfa1ca9dSYoshinobu Inoue 
615686cdd19SJun-ichiro itojun Hagino #ifdef	SIOCSIFMTU /* xxx */
616cfa1ca9dSYoshinobu Inoue 	case SIOCGIFMTU:
617cfa1ca9dSYoshinobu Inoue 		break;
618686cdd19SJun-ichiro itojun Hagino 
619cfa1ca9dSYoshinobu Inoue 	case SIOCSIFMTU:
620cfa1ca9dSYoshinobu Inoue 		mtu = ifr->ifr_mtu;
6213bb61ca6SHajimu UMEMOTO 		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
622cfa1ca9dSYoshinobu Inoue 			return (EINVAL);
623cfa1ca9dSYoshinobu Inoue 		ifp->if_mtu = mtu;
624cfa1ca9dSYoshinobu Inoue 		break;
625686cdd19SJun-ichiro itojun Hagino #endif /* SIOCSIFMTU */
626cfa1ca9dSYoshinobu Inoue 
6273bb61ca6SHajimu UMEMOTO #ifdef INET
628cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR:
6293bb61ca6SHajimu UMEMOTO #endif
630cfa1ca9dSYoshinobu Inoue #ifdef INET6
631cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR_IN6:
632cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
63333841545SHajimu UMEMOTO 	case SIOCSLIFPHYADDR:
634686cdd19SJun-ichiro itojun Hagino 		switch (cmd) {
63533841545SHajimu UMEMOTO #ifdef INET
636686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR:
637cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
638cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_addr);
639cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
640cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
641cfa1ca9dSYoshinobu Inoue 			break;
64233841545SHajimu UMEMOTO #endif
643cfa1ca9dSYoshinobu Inoue #ifdef INET6
644686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR_IN6:
645cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
646cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_addr);
647cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
648cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
649686cdd19SJun-ichiro itojun Hagino 			break;
650686cdd19SJun-ichiro itojun Hagino #endif
65133841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
65233841545SHajimu UMEMOTO 			src = (struct sockaddr *)
65333841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->addr);
65433841545SHajimu UMEMOTO 			dst = (struct sockaddr *)
65533841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->dstaddr);
6569426aedfSHajimu UMEMOTO 			break;
6576f4ded3aSBrooks Davis 		default:
6589426aedfSHajimu UMEMOTO 			return EINVAL;
65933841545SHajimu UMEMOTO 		}
66033841545SHajimu UMEMOTO 
66133841545SHajimu UMEMOTO 		/* sa_family must be equal */
66233841545SHajimu UMEMOTO 		if (src->sa_family != dst->sa_family)
66333841545SHajimu UMEMOTO 			return EINVAL;
66433841545SHajimu UMEMOTO 
66533841545SHajimu UMEMOTO 		/* validate sa_len */
66633841545SHajimu UMEMOTO 		switch (src->sa_family) {
66733841545SHajimu UMEMOTO #ifdef INET
66833841545SHajimu UMEMOTO 		case AF_INET:
66933841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in))
67033841545SHajimu UMEMOTO 				return EINVAL;
67133841545SHajimu UMEMOTO 			break;
67233841545SHajimu UMEMOTO #endif
67333841545SHajimu UMEMOTO #ifdef INET6
67433841545SHajimu UMEMOTO 		case AF_INET6:
67533841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in6))
67633841545SHajimu UMEMOTO 				return EINVAL;
67733841545SHajimu UMEMOTO 			break;
67833841545SHajimu UMEMOTO #endif
67933841545SHajimu UMEMOTO 		default:
68033841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
68133841545SHajimu UMEMOTO 		}
68233841545SHajimu UMEMOTO 		switch (dst->sa_family) {
68333841545SHajimu UMEMOTO #ifdef INET
68433841545SHajimu UMEMOTO 		case AF_INET:
68533841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in))
68633841545SHajimu UMEMOTO 				return EINVAL;
68733841545SHajimu UMEMOTO 			break;
68833841545SHajimu UMEMOTO #endif
68933841545SHajimu UMEMOTO #ifdef INET6
69033841545SHajimu UMEMOTO 		case AF_INET6:
69133841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in6))
69233841545SHajimu UMEMOTO 				return EINVAL;
69333841545SHajimu UMEMOTO 			break;
69433841545SHajimu UMEMOTO #endif
69533841545SHajimu UMEMOTO 		default:
69633841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
69733841545SHajimu UMEMOTO 		}
69833841545SHajimu UMEMOTO 
69933841545SHajimu UMEMOTO 		/* check sa_family looks sane for the cmd */
70033841545SHajimu UMEMOTO 		switch (cmd) {
70133841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR:
70233841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET)
70333841545SHajimu UMEMOTO 				break;
70433841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
70533841545SHajimu UMEMOTO #ifdef INET6
70633841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR_IN6:
70733841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET6)
70833841545SHajimu UMEMOTO 				break;
70933841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
71033841545SHajimu UMEMOTO #endif /* INET6 */
71133841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
71233841545SHajimu UMEMOTO 			/* checks done in the above */
71333841545SHajimu UMEMOTO 			break;
714686cdd19SJun-ichiro itojun Hagino 		}
715cfa1ca9dSYoshinobu Inoue 
716fc74a9f9SBrooks Davis 		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
717cfa1ca9dSYoshinobu Inoue 		break;
718cfa1ca9dSYoshinobu Inoue 
719686cdd19SJun-ichiro itojun Hagino #ifdef SIOCDIFPHYADDR
720686cdd19SJun-ichiro itojun Hagino 	case SIOCDIFPHYADDR:
721fc74a9f9SBrooks Davis 		gif_delete_tunnel(GIF2IFP(sc));
722686cdd19SJun-ichiro itojun Hagino 		break;
723686cdd19SJun-ichiro itojun Hagino #endif
724686cdd19SJun-ichiro itojun Hagino 
725cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR:
726cfa1ca9dSYoshinobu Inoue #ifdef INET6
727cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR_IN6:
728cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
729cfa1ca9dSYoshinobu Inoue 		if (sc->gif_psrc == NULL) {
730cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
731cfa1ca9dSYoshinobu Inoue 			goto bad;
732cfa1ca9dSYoshinobu Inoue 		}
733cfa1ca9dSYoshinobu Inoue 		src = sc->gif_psrc;
73433841545SHajimu UMEMOTO 		switch (cmd) {
735cfa1ca9dSYoshinobu Inoue #ifdef INET
73633841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR:
737cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
73833841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
739cfa1ca9dSYoshinobu Inoue 			break;
740cfa1ca9dSYoshinobu Inoue #endif /* INET */
741cfa1ca9dSYoshinobu Inoue #ifdef INET6
74233841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR_IN6:
743cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
744cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
74533841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
746cfa1ca9dSYoshinobu Inoue 			break;
747cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
748cfa1ca9dSYoshinobu Inoue 		default:
749cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
750cfa1ca9dSYoshinobu Inoue 			goto bad;
751cfa1ca9dSYoshinobu Inoue 		}
75233841545SHajimu UMEMOTO 		if (src->sa_len > size)
75333841545SHajimu UMEMOTO 			return EINVAL;
75433841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
755a1f7e5f8SHajimu UMEMOTO #ifdef INET6
756a1f7e5f8SHajimu UMEMOTO 		if (dst->sa_family == AF_INET6) {
757a1f7e5f8SHajimu UMEMOTO 			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
758a1f7e5f8SHajimu UMEMOTO 			if (error != 0)
759a1f7e5f8SHajimu UMEMOTO 				return (error);
760a1f7e5f8SHajimu UMEMOTO 		}
761a1f7e5f8SHajimu UMEMOTO #endif
762cfa1ca9dSYoshinobu Inoue 		break;
763cfa1ca9dSYoshinobu Inoue 
764cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR:
765cfa1ca9dSYoshinobu Inoue #ifdef INET6
766cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR_IN6:
767cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
768cfa1ca9dSYoshinobu Inoue 		if (sc->gif_pdst == NULL) {
769cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
770cfa1ca9dSYoshinobu Inoue 			goto bad;
771cfa1ca9dSYoshinobu Inoue 		}
772cfa1ca9dSYoshinobu Inoue 		src = sc->gif_pdst;
77333841545SHajimu UMEMOTO 		switch (cmd) {
774cfa1ca9dSYoshinobu Inoue #ifdef INET
77533841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR:
776cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
77733841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
778cfa1ca9dSYoshinobu Inoue 			break;
779cfa1ca9dSYoshinobu Inoue #endif /* INET */
780cfa1ca9dSYoshinobu Inoue #ifdef INET6
78133841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR_IN6:
782cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
783cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
78433841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
785cfa1ca9dSYoshinobu Inoue 			break;
786cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
787cfa1ca9dSYoshinobu Inoue 		default:
788cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
789cfa1ca9dSYoshinobu Inoue 			goto bad;
790cfa1ca9dSYoshinobu Inoue 		}
79133841545SHajimu UMEMOTO 		if (src->sa_len > size)
79233841545SHajimu UMEMOTO 			return EINVAL;
79333841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
794a1f7e5f8SHajimu UMEMOTO #ifdef INET6
795a1f7e5f8SHajimu UMEMOTO 		if (dst->sa_family == AF_INET6) {
796a1f7e5f8SHajimu UMEMOTO 			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
797a1f7e5f8SHajimu UMEMOTO 			if (error != 0)
798a1f7e5f8SHajimu UMEMOTO 				return (error);
799a1f7e5f8SHajimu UMEMOTO 		}
800a1f7e5f8SHajimu UMEMOTO #endif
80133841545SHajimu UMEMOTO 		break;
80233841545SHajimu UMEMOTO 
80333841545SHajimu UMEMOTO 	case SIOCGLIFPHYADDR:
80433841545SHajimu UMEMOTO 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
80533841545SHajimu UMEMOTO 			error = EADDRNOTAVAIL;
80633841545SHajimu UMEMOTO 			goto bad;
80733841545SHajimu UMEMOTO 		}
80833841545SHajimu UMEMOTO 
80933841545SHajimu UMEMOTO 		/* copy src */
81033841545SHajimu UMEMOTO 		src = sc->gif_psrc;
81133841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
81233841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->addr);
81333841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->addr);
81433841545SHajimu UMEMOTO 		if (src->sa_len > size)
81533841545SHajimu UMEMOTO 			return EINVAL;
81633841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
81733841545SHajimu UMEMOTO 
81833841545SHajimu UMEMOTO 		/* copy dst */
81933841545SHajimu UMEMOTO 		src = sc->gif_pdst;
82033841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
82133841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->dstaddr);
82233841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
82333841545SHajimu UMEMOTO 		if (src->sa_len > size)
82433841545SHajimu UMEMOTO 			return EINVAL;
82533841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
826cfa1ca9dSYoshinobu Inoue 		break;
827cfa1ca9dSYoshinobu Inoue 
828cfa1ca9dSYoshinobu Inoue 	case SIOCSIFFLAGS:
829686cdd19SJun-ichiro itojun Hagino 		/* if_ioctl() takes care of it */
830cfa1ca9dSYoshinobu Inoue 		break;
831cfa1ca9dSYoshinobu Inoue 
832cfa1ca9dSYoshinobu Inoue 	default:
833cfa1ca9dSYoshinobu Inoue 		error = EINVAL;
834cfa1ca9dSYoshinobu Inoue 		break;
835cfa1ca9dSYoshinobu Inoue 	}
836cfa1ca9dSYoshinobu Inoue  bad:
837cfa1ca9dSYoshinobu Inoue 	return error;
838cfa1ca9dSYoshinobu Inoue }
83953dab5feSBrooks Davis 
84017d5cb2dSRobert Watson /*
84117d5cb2dSRobert Watson  * XXXRW: There's a general event-ordering issue here: the code to check
84217d5cb2dSRobert Watson  * if a given tunnel is already present happens before we perform a
84317d5cb2dSRobert Watson  * potentially blocking setup of the tunnel.  This code needs to be
84417d5cb2dSRobert Watson  * re-ordered so that the check and replacement can be atomic using
84517d5cb2dSRobert Watson  * a mutex.
84617d5cb2dSRobert Watson  */
8479426aedfSHajimu UMEMOTO int
8489426aedfSHajimu UMEMOTO gif_set_tunnel(ifp, src, dst)
8499426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
8509426aedfSHajimu UMEMOTO 	struct sockaddr *src;
8519426aedfSHajimu UMEMOTO 	struct sockaddr *dst;
85253dab5feSBrooks Davis {
853fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
8549426aedfSHajimu UMEMOTO 	struct gif_softc *sc2;
8559426aedfSHajimu UMEMOTO 	struct sockaddr *osrc, *odst, *sa;
8569426aedfSHajimu UMEMOTO 	int error = 0;
8579426aedfSHajimu UMEMOTO 
85817d5cb2dSRobert Watson 	mtx_lock(&gif_mtx);
8599426aedfSHajimu UMEMOTO 	LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
8609426aedfSHajimu UMEMOTO 		if (sc2 == sc)
8619426aedfSHajimu UMEMOTO 			continue;
8629426aedfSHajimu UMEMOTO 		if (!sc2->gif_pdst || !sc2->gif_psrc)
8639426aedfSHajimu UMEMOTO 			continue;
8649426aedfSHajimu UMEMOTO 		if (sc2->gif_pdst->sa_family != dst->sa_family ||
8659426aedfSHajimu UMEMOTO 		    sc2->gif_pdst->sa_len != dst->sa_len ||
8669426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_family != src->sa_family ||
8679426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_len != src->sa_len)
8689426aedfSHajimu UMEMOTO 			continue;
8699426aedfSHajimu UMEMOTO 
8709426aedfSHajimu UMEMOTO 		/*
8719426aedfSHajimu UMEMOTO 		 * Disallow parallel tunnels unless instructed
8729426aedfSHajimu UMEMOTO 		 * otherwise.
8739426aedfSHajimu UMEMOTO 		 */
8749426aedfSHajimu UMEMOTO 		if (!parallel_tunnels &&
8759426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
8769426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
8779426aedfSHajimu UMEMOTO 			error = EADDRNOTAVAIL;
87817d5cb2dSRobert Watson 			mtx_unlock(&gif_mtx);
8799426aedfSHajimu UMEMOTO 			goto bad;
8809426aedfSHajimu UMEMOTO 		}
8819426aedfSHajimu UMEMOTO 
8829426aedfSHajimu UMEMOTO 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
8839426aedfSHajimu UMEMOTO 	}
88417d5cb2dSRobert Watson 	mtx_unlock(&gif_mtx);
8859426aedfSHajimu UMEMOTO 
8869426aedfSHajimu UMEMOTO 	/* XXX we can detach from both, but be polite just in case */
8879426aedfSHajimu UMEMOTO 	if (sc->gif_psrc)
8889426aedfSHajimu UMEMOTO 		switch (sc->gif_psrc->sa_family) {
8899426aedfSHajimu UMEMOTO #ifdef INET
8909426aedfSHajimu UMEMOTO 		case AF_INET:
8919426aedfSHajimu UMEMOTO 			(void)in_gif_detach(sc);
8929426aedfSHajimu UMEMOTO 			break;
8939426aedfSHajimu UMEMOTO #endif
8949426aedfSHajimu UMEMOTO #ifdef INET6
8959426aedfSHajimu UMEMOTO 		case AF_INET6:
8969426aedfSHajimu UMEMOTO 			(void)in6_gif_detach(sc);
8979426aedfSHajimu UMEMOTO 			break;
8989426aedfSHajimu UMEMOTO #endif
8999426aedfSHajimu UMEMOTO 		}
9009426aedfSHajimu UMEMOTO 
9019426aedfSHajimu UMEMOTO 	osrc = sc->gif_psrc;
902a163d034SWarner Losh 	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
9039426aedfSHajimu UMEMOTO 	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
9049426aedfSHajimu UMEMOTO 	sc->gif_psrc = sa;
9059426aedfSHajimu UMEMOTO 
9069426aedfSHajimu UMEMOTO 	odst = sc->gif_pdst;
907a163d034SWarner Losh 	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
9089426aedfSHajimu UMEMOTO 	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
9099426aedfSHajimu UMEMOTO 	sc->gif_pdst = sa;
9109426aedfSHajimu UMEMOTO 
9119426aedfSHajimu UMEMOTO 	switch (sc->gif_psrc->sa_family) {
9129426aedfSHajimu UMEMOTO #ifdef INET
9139426aedfSHajimu UMEMOTO 	case AF_INET:
9149426aedfSHajimu UMEMOTO 		error = in_gif_attach(sc);
9159426aedfSHajimu UMEMOTO 		break;
9169426aedfSHajimu UMEMOTO #endif
9179426aedfSHajimu UMEMOTO #ifdef INET6
9189426aedfSHajimu UMEMOTO 	case AF_INET6:
919a1f7e5f8SHajimu UMEMOTO 		/*
920a1f7e5f8SHajimu UMEMOTO 		 * Check validity of the scope zone ID of the addresses, and
921a1f7e5f8SHajimu UMEMOTO 		 * convert it into the kernel internal form if necessary.
922a1f7e5f8SHajimu UMEMOTO 		 */
923a1f7e5f8SHajimu UMEMOTO 		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
924a1f7e5f8SHajimu UMEMOTO 		if (error != 0)
925a1f7e5f8SHajimu UMEMOTO 			break;
926a1f7e5f8SHajimu UMEMOTO 		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
927a1f7e5f8SHajimu UMEMOTO 		if (error != 0)
928a1f7e5f8SHajimu UMEMOTO 			break;
9299426aedfSHajimu UMEMOTO 		error = in6_gif_attach(sc);
9309426aedfSHajimu UMEMOTO 		break;
9319426aedfSHajimu UMEMOTO #endif
9329426aedfSHajimu UMEMOTO 	}
9339426aedfSHajimu UMEMOTO 	if (error) {
9349426aedfSHajimu UMEMOTO 		/* rollback */
9359426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_psrc, M_IFADDR);
9369426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_pdst, M_IFADDR);
9379426aedfSHajimu UMEMOTO 		sc->gif_psrc = osrc;
9389426aedfSHajimu UMEMOTO 		sc->gif_pdst = odst;
9399426aedfSHajimu UMEMOTO 		goto bad;
9409426aedfSHajimu UMEMOTO 	}
9419426aedfSHajimu UMEMOTO 
9429426aedfSHajimu UMEMOTO 	if (osrc)
9439426aedfSHajimu UMEMOTO 		free((caddr_t)osrc, M_IFADDR);
9449426aedfSHajimu UMEMOTO 	if (odst)
9459426aedfSHajimu UMEMOTO 		free((caddr_t)odst, M_IFADDR);
9469426aedfSHajimu UMEMOTO 
9479426aedfSHajimu UMEMOTO  bad:
9489426aedfSHajimu UMEMOTO 	if (sc->gif_psrc && sc->gif_pdst)
94913f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
9509426aedfSHajimu UMEMOTO 	else
95113f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
9529426aedfSHajimu UMEMOTO 
9539426aedfSHajimu UMEMOTO 	return error;
9549426aedfSHajimu UMEMOTO }
9559426aedfSHajimu UMEMOTO 
9569426aedfSHajimu UMEMOTO void
9579426aedfSHajimu UMEMOTO gif_delete_tunnel(ifp)
9589426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
9599426aedfSHajimu UMEMOTO {
960fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
96153dab5feSBrooks Davis 
96253dab5feSBrooks Davis 	if (sc->gif_psrc) {
96353dab5feSBrooks Davis 		free((caddr_t)sc->gif_psrc, M_IFADDR);
96453dab5feSBrooks Davis 		sc->gif_psrc = NULL;
96553dab5feSBrooks Davis 	}
96653dab5feSBrooks Davis 	if (sc->gif_pdst) {
96753dab5feSBrooks Davis 		free((caddr_t)sc->gif_pdst, M_IFADDR);
96853dab5feSBrooks Davis 		sc->gif_pdst = NULL;
96953dab5feSBrooks Davis 	}
9709426aedfSHajimu UMEMOTO 	/* it is safe to detach from both */
9719426aedfSHajimu UMEMOTO #ifdef INET
9729426aedfSHajimu UMEMOTO 	(void)in_gif_detach(sc);
9739426aedfSHajimu UMEMOTO #endif
9749426aedfSHajimu UMEMOTO #ifdef INET6
9759426aedfSHajimu UMEMOTO 	(void)in6_gif_detach(sc);
9769426aedfSHajimu UMEMOTO #endif
97713f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
97853dab5feSBrooks Davis }
979