xref: /freebsd/sys/net/if_gif.c (revision bd81e07d2761cf1c13063eb49a5c0cb4a6951318)
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	$KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/jail.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/module.h>
46 #include <sys/rmlock.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/sx.h>
50 #include <sys/errno.h>
51 #include <sys/time.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
54 #include <sys/priv.h>
55 #include <sys/proc.h>
56 #include <sys/protosw.h>
57 #include <sys/conf.h>
58 #include <machine/cpu.h>
59 
60 #include <net/if.h>
61 #include <net/if_var.h>
62 #include <net/if_clone.h>
63 #include <net/if_types.h>
64 #include <net/netisr.h>
65 #include <net/route.h>
66 #include <net/bpf.h>
67 #include <net/vnet.h>
68 
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip_ecn.h>
73 #ifdef	INET
74 #include <netinet/in_var.h>
75 #include <netinet/ip_var.h>
76 #endif	/* INET */
77 
78 #ifdef INET6
79 #ifndef INET
80 #include <netinet/in.h>
81 #endif
82 #include <netinet6/in6_var.h>
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_ecn.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet6/scope6_var.h>
87 #include <netinet6/ip6protosw.h>
88 #endif /* INET6 */
89 
90 #include <netinet/ip_encap.h>
91 #include <net/ethernet.h>
92 #include <net/if_bridgevar.h>
93 #include <net/if_gif.h>
94 
95 #include <security/mac/mac_framework.h>
96 
97 static const char gifname[] = "gif";
98 
99 /*
100  * gif_mtx protects a per-vnet gif_softc_list.
101  */
102 static VNET_DEFINE(struct mtx, gif_mtx);
103 #define	V_gif_mtx		VNET(gif_mtx)
104 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
105 static VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list);
106 #define	V_gif_softc_list	VNET(gif_softc_list)
107 static struct sx gif_ioctl_sx;
108 SX_SYSINIT(gif_ioctl_sx, &gif_ioctl_sx, "gif_ioctl");
109 
110 #define	GIF_LIST_LOCK_INIT(x)		mtx_init(&V_gif_mtx, "gif_mtx", \
111 					    NULL, MTX_DEF)
112 #define	GIF_LIST_LOCK_DESTROY(x)	mtx_destroy(&V_gif_mtx)
113 #define	GIF_LIST_LOCK(x)		mtx_lock(&V_gif_mtx)
114 #define	GIF_LIST_UNLOCK(x)		mtx_unlock(&V_gif_mtx)
115 
116 void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
117 void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
118 void	(*ng_gif_attach_p)(struct ifnet *ifp);
119 void	(*ng_gif_detach_p)(struct ifnet *ifp);
120 
121 static int	gif_check_nesting(struct ifnet *, struct mbuf *);
122 static int	gif_set_tunnel(struct ifnet *, struct sockaddr *,
123     struct sockaddr *);
124 static void	gif_delete_tunnel(struct ifnet *);
125 static int	gif_ioctl(struct ifnet *, u_long, caddr_t);
126 static int	gif_transmit(struct ifnet *, struct mbuf *);
127 static void	gif_qflush(struct ifnet *);
128 static int	gif_clone_create(struct if_clone *, int, caddr_t);
129 static void	gif_clone_destroy(struct ifnet *);
130 static VNET_DEFINE(struct if_clone *, gif_cloner);
131 #define	V_gif_cloner	VNET(gif_cloner)
132 
133 static int gifmodevent(module_t, int, void *);
134 
135 SYSCTL_DECL(_net_link);
136 static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
137     "Generic Tunnel Interface");
138 #ifndef MAX_GIF_NEST
139 /*
140  * This macro controls the default upper limitation on nesting of gif tunnels.
141  * Since, setting a large value to this macro with a careless configuration
142  * may introduce system crash, we don't allow any nestings by default.
143  * If you need to configure nested gif tunnels, you can define this macro
144  * in your kernel configuration file.  However, if you do so, please be
145  * careful to configure the tunnels so that it won't make a loop.
146  */
147 #define MAX_GIF_NEST 1
148 #endif
149 static VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST;
150 #define	V_max_gif_nesting	VNET(max_gif_nesting)
151 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW,
152     &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels");
153 
154 /*
155  * By default, we disallow creation of multiple tunnels between the same
156  * pair of addresses.  Some applications require this functionality so
157  * we allow control over this check here.
158  */
159 #ifdef XBONEHACK
160 static VNET_DEFINE(int, parallel_tunnels) = 1;
161 #else
162 static VNET_DEFINE(int, parallel_tunnels) = 0;
163 #endif
164 #define	V_parallel_tunnels	VNET(parallel_tunnels)
165 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels,
166     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(parallel_tunnels), 0,
167     "Allow parallel tunnels?");
168 
169 /* copy from src/sys/net/if_ethersubr.c */
170 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
171 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
172 #ifndef ETHER_IS_BROADCAST
173 #define ETHER_IS_BROADCAST(addr) \
174 	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
175 #endif
176 
177 static int
178 gif_clone_create(struct if_clone *ifc, int unit, caddr_t params)
179 {
180 	struct gif_softc *sc;
181 
182 	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
183 	sc->gif_fibnum = curthread->td_proc->p_fibnum;
184 	GIF2IFP(sc) = if_alloc(IFT_GIF);
185 	GIF_LOCK_INIT(sc);
186 	GIF2IFP(sc)->if_softc = sc;
187 	if_initname(GIF2IFP(sc), gifname, unit);
188 
189 	GIF2IFP(sc)->if_addrlen = 0;
190 	GIF2IFP(sc)->if_mtu    = GIF_MTU;
191 	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
192 #if 0
193 	/* turn off ingress filter */
194 	GIF2IFP(sc)->if_flags  |= IFF_LINK2;
195 #endif
196 	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
197 	GIF2IFP(sc)->if_transmit  = gif_transmit;
198 	GIF2IFP(sc)->if_qflush  = gif_qflush;
199 	GIF2IFP(sc)->if_output = gif_output;
200 	if_attach(GIF2IFP(sc));
201 	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
202 	if (ng_gif_attach_p != NULL)
203 		(*ng_gif_attach_p)(GIF2IFP(sc));
204 
205 	GIF_LIST_LOCK();
206 	LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list);
207 	GIF_LIST_UNLOCK();
208 	return (0);
209 }
210 
211 static void
212 gif_clone_destroy(struct ifnet *ifp)
213 {
214 	struct gif_softc *sc;
215 
216 	sx_xlock(&gif_ioctl_sx);
217 	sc = ifp->if_softc;
218 	gif_delete_tunnel(ifp);
219 	GIF_LIST_LOCK();
220 	LIST_REMOVE(sc, gif_list);
221 	GIF_LIST_UNLOCK();
222 	if (ng_gif_detach_p != NULL)
223 		(*ng_gif_detach_p)(ifp);
224 	bpfdetach(ifp);
225 	if_detach(ifp);
226 	ifp->if_softc = NULL;
227 	sx_xunlock(&gif_ioctl_sx);
228 
229 	if_free(ifp);
230 	GIF_LOCK_DESTROY(sc);
231 	free(sc, M_GIF);
232 }
233 
234 static void
235 vnet_gif_init(const void *unused __unused)
236 {
237 
238 	LIST_INIT(&V_gif_softc_list);
239 	GIF_LIST_LOCK_INIT();
240 	V_gif_cloner = if_clone_simple(gifname, gif_clone_create,
241 	    gif_clone_destroy, 0);
242 }
243 VNET_SYSINIT(vnet_gif_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
244     vnet_gif_init, NULL);
245 
246 static void
247 vnet_gif_uninit(const void *unused __unused)
248 {
249 
250 	if_clone_detach(V_gif_cloner);
251 	GIF_LIST_LOCK_DESTROY();
252 }
253 VNET_SYSUNINIT(vnet_gif_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
254     vnet_gif_uninit, NULL);
255 
256 static int
257 gifmodevent(module_t mod, int type, void *data)
258 {
259 
260 	switch (type) {
261 	case MOD_LOAD:
262 	case MOD_UNLOAD:
263 		break;
264 	default:
265 		return (EOPNOTSUPP);
266 	}
267 	return (0);
268 }
269 
270 static moduledata_t gif_mod = {
271 	"if_gif",
272 	gifmodevent,
273 	0
274 };
275 
276 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
277 MODULE_VERSION(if_gif, 1);
278 
279 int
280 gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
281 {
282 	GIF_RLOCK_TRACKER;
283 	const struct ip *ip;
284 	struct gif_softc *sc;
285 	int ret;
286 
287 	sc = (struct gif_softc *)arg;
288 	if (sc == NULL || (GIF2IFP(sc)->if_flags & IFF_UP) == 0)
289 		return (0);
290 
291 	ret = 0;
292 	GIF_RLOCK(sc);
293 
294 	/* no physical address */
295 	if (sc->gif_family == 0)
296 		goto done;
297 
298 	switch (proto) {
299 #ifdef INET
300 	case IPPROTO_IPV4:
301 #endif
302 #ifdef INET6
303 	case IPPROTO_IPV6:
304 #endif
305 	case IPPROTO_ETHERIP:
306 		break;
307 	default:
308 		goto done;
309 	}
310 
311 	/* Bail on short packets */
312 	M_ASSERTPKTHDR(m);
313 	if (m->m_pkthdr.len < sizeof(struct ip))
314 		goto done;
315 
316 	ip = mtod(m, const struct ip *);
317 	switch (ip->ip_v) {
318 #ifdef INET
319 	case 4:
320 		if (sc->gif_family != AF_INET)
321 			goto done;
322 		ret = in_gif_encapcheck(m, off, proto, arg);
323 		break;
324 #endif
325 #ifdef INET6
326 	case 6:
327 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
328 			goto done;
329 		if (sc->gif_family != AF_INET6)
330 			goto done;
331 		ret = in6_gif_encapcheck(m, off, proto, arg);
332 		break;
333 #endif
334 	}
335 done:
336 	GIF_RUNLOCK(sc);
337 	return (ret);
338 }
339 
340 static int
341 gif_transmit(struct ifnet *ifp, struct mbuf *m)
342 {
343 	struct gif_softc *sc;
344 	struct etherip_header *eth;
345 #ifdef INET
346 	struct ip *ip;
347 #endif
348 #ifdef INET6
349 	struct ip6_hdr *ip6;
350 	uint32_t t;
351 #endif
352 	uint32_t af;
353 	uint8_t proto, ecn;
354 	int error;
355 
356 #ifdef MAC
357 	error = mac_ifnet_check_transmit(ifp, m);
358 	if (error) {
359 		m_freem(m);
360 		goto err;
361 	}
362 #endif
363 	error = ENETDOWN;
364 	sc = ifp->if_softc;
365 	if ((ifp->if_flags & IFF_MONITOR) != 0 ||
366 	    (ifp->if_flags & IFF_UP) == 0 ||
367 	    sc->gif_family == 0 ||
368 	    (error = gif_check_nesting(ifp, m)) != 0) {
369 		m_freem(m);
370 		goto err;
371 	}
372 	/* Now pull back the af that we stashed in the csum_data. */
373 	if (ifp->if_bridge)
374 		af = AF_LINK;
375 	else
376 		af = m->m_pkthdr.csum_data;
377 	m->m_flags &= ~(M_BCAST|M_MCAST);
378 	M_SETFIB(m, sc->gif_fibnum);
379 	BPF_MTAP2(ifp, &af, sizeof(af), m);
380 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
381 	if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
382 	/* inner AF-specific encapsulation */
383 	ecn = 0;
384 	switch (af) {
385 #ifdef INET
386 	case AF_INET:
387 		proto = IPPROTO_IPV4;
388 		if (m->m_len < sizeof(struct ip))
389 			m = m_pullup(m, sizeof(struct ip));
390 		if (m == NULL) {
391 			error = ENOBUFS;
392 			goto err;
393 		}
394 		ip = mtod(m, struct ip *);
395 		ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
396 		    ECN_NOCARE, &ecn, &ip->ip_tos);
397 		break;
398 #endif
399 #ifdef INET6
400 	case AF_INET6:
401 		proto = IPPROTO_IPV6;
402 		if (m->m_len < sizeof(struct ip6_hdr))
403 			m = m_pullup(m, sizeof(struct ip6_hdr));
404 		if (m == NULL) {
405 			error = ENOBUFS;
406 			goto err;
407 		}
408 		t = 0;
409 		ip6 = mtod(m, struct ip6_hdr *);
410 		ip6_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
411 		    ECN_NOCARE, &t, &ip6->ip6_flow);
412 		ecn = (ntohl(t) >> 20) & 0xff;
413 		break;
414 #endif
415 	case AF_LINK:
416 		proto = IPPROTO_ETHERIP;
417 		M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT);
418 		if (m == NULL) {
419 			error = ENOBUFS;
420 			goto err;
421 		}
422 		eth = mtod(m, struct etherip_header *);
423 		eth->eip_resvh = 0;
424 		if ((sc->gif_options & GIF_SEND_REVETHIP) != 0) {
425 			eth->eip_ver = 0;
426 			eth->eip_resvl = ETHERIP_VERSION;
427 		} else {
428 			eth->eip_ver = ETHERIP_VERSION;
429 			eth->eip_resvl = 0;
430 		}
431 		break;
432 	default:
433 		error = EAFNOSUPPORT;
434 		m_freem(m);
435 		goto err;
436 	}
437 	/* XXX should we check if our outer source is legal? */
438 	/* dispatch to output logic based on outer AF */
439 	switch (sc->gif_family) {
440 #ifdef INET
441 	case AF_INET:
442 		error = in_gif_output(ifp, m, proto, ecn);
443 		break;
444 #endif
445 #ifdef INET6
446 	case AF_INET6:
447 		error = in6_gif_output(ifp, m, proto, ecn);
448 		break;
449 #endif
450 	default:
451 		m_freem(m);
452 	}
453 err:
454 	if (error)
455 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
456 	return (error);
457 }
458 
459 static void
460 gif_qflush(struct ifnet *ifp __unused)
461 {
462 
463 }
464 
465 #define	MTAG_GIF	1080679712
466 static int
467 gif_check_nesting(struct ifnet *ifp, struct mbuf *m)
468 {
469 	struct m_tag *mtag;
470 	int count;
471 
472 	/*
473 	 * gif may cause infinite recursion calls when misconfigured.
474 	 * We'll prevent this by detecting loops.
475 	 *
476 	 * High nesting level may cause stack exhaustion.
477 	 * We'll prevent this by introducing upper limit.
478 	 */
479 	count = 1;
480 	mtag = NULL;
481 	while ((mtag = m_tag_locate(m, MTAG_GIF, 0, mtag)) != NULL) {
482 		if (*(struct ifnet **)(mtag + 1) == ifp) {
483 			log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
484 			return (EIO);
485 		}
486 		count++;
487 	}
488 	if (count > V_max_gif_nesting) {
489 		log(LOG_NOTICE,
490 		    "%s: if_output recursively called too many times(%d)\n",
491 		    if_name(ifp), count);
492 		return (EIO);
493 	}
494 	mtag = m_tag_alloc(MTAG_GIF, 0, sizeof(struct ifnet *), M_NOWAIT);
495 	if (mtag == NULL)
496 		return (ENOMEM);
497 	*(struct ifnet **)(mtag + 1) = ifp;
498 	m_tag_prepend(m, mtag);
499 	return (0);
500 }
501 
502 int
503 gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
504 	struct route *ro)
505 {
506 	uint32_t af;
507 
508 	if (dst->sa_family == AF_UNSPEC)
509 		bcopy(dst->sa_data, &af, sizeof(af));
510 	else
511 		af = dst->sa_family;
512 	/*
513 	 * Now save the af in the inbound pkt csum data, this is a cheat since
514 	 * we are using the inbound csum_data field to carry the af over to
515 	 * the gif_transmit() routine, avoiding using yet another mtag.
516 	 */
517 	m->m_pkthdr.csum_data = af;
518 	return (ifp->if_transmit(ifp, m));
519 }
520 
521 void
522 gif_input(struct mbuf *m, struct ifnet *ifp, int proto, uint8_t ecn)
523 {
524 	struct etherip_header *eip;
525 #ifdef INET
526 	struct ip *ip;
527 #endif
528 #ifdef INET6
529 	struct ip6_hdr *ip6;
530 	uint32_t t;
531 #endif
532 	struct gif_softc *sc;
533 	struct ether_header *eh;
534 	struct ifnet *oldifp;
535 	uint32_t gif_options;
536 	int isr, n, af;
537 
538 	if (ifp == NULL) {
539 		/* just in case */
540 		m_freem(m);
541 		return;
542 	}
543 	sc = ifp->if_softc;
544 	gif_options = sc->gif_options;
545 	m->m_pkthdr.rcvif = ifp;
546 	m_clrprotoflags(m);
547 	switch (proto) {
548 #ifdef INET
549 	case IPPROTO_IPV4:
550 		af = AF_INET;
551 		if (m->m_len < sizeof(struct ip))
552 			m = m_pullup(m, sizeof(struct ip));
553 		if (m == NULL)
554 			goto drop;
555 		ip = mtod(m, struct ip *);
556 		if (ip_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
557 		    ECN_NOCARE, &ecn, &ip->ip_tos) == 0) {
558 			m_freem(m);
559 			goto drop;
560 		}
561 		break;
562 #endif
563 #ifdef INET6
564 	case IPPROTO_IPV6:
565 		af = AF_INET6;
566 		if (m->m_len < sizeof(struct ip6_hdr))
567 			m = m_pullup(m, sizeof(struct ip6_hdr));
568 		if (m == NULL)
569 			goto drop;
570 		t = htonl((uint32_t)ecn << 20);
571 		ip6 = mtod(m, struct ip6_hdr *);
572 		if (ip6_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
573 		    ECN_NOCARE, &t, &ip6->ip6_flow) == 0) {
574 			m_freem(m);
575 			goto drop;
576 		}
577 		break;
578 #endif
579 	case IPPROTO_ETHERIP:
580 		af = AF_LINK;
581 		break;
582 	default:
583 		m_freem(m);
584 		goto drop;
585 	}
586 
587 #ifdef MAC
588 	mac_ifnet_create_mbuf(ifp, m);
589 #endif
590 
591 	if (bpf_peers_present(ifp->if_bpf)) {
592 		uint32_t af1 = af;
593 		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
594 	}
595 
596 	if ((ifp->if_flags & IFF_MONITOR) != 0) {
597 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
598 		if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
599 		m_freem(m);
600 		return;
601 	}
602 
603 	if (ng_gif_input_p != NULL) {
604 		(*ng_gif_input_p)(ifp, &m, af);
605 		if (m == NULL)
606 			goto drop;
607 	}
608 
609 	/*
610 	 * Put the packet to the network layer input queue according to the
611 	 * specified address family.
612 	 * Note: older versions of gif_input directly called network layer
613 	 * input functions, e.g. ip6_input, here.  We changed the policy to
614 	 * prevent too many recursive calls of such input functions, which
615 	 * might cause kernel panic.  But the change may introduce another
616 	 * problem; if the input queue is full, packets are discarded.
617 	 * The kernel stack overflow really happened, and we believed
618 	 * queue-full rarely occurs, so we changed the policy.
619 	 */
620 	switch (af) {
621 #ifdef INET
622 	case AF_INET:
623 		isr = NETISR_IP;
624 		break;
625 #endif
626 #ifdef INET6
627 	case AF_INET6:
628 		isr = NETISR_IPV6;
629 		break;
630 #endif
631 	case AF_LINK:
632 		n = sizeof(struct etherip_header) + sizeof(struct ether_header);
633 		if (n > m->m_len)
634 			m = m_pullup(m, n);
635 		if (m == NULL)
636 			goto drop;
637 		eip = mtod(m, struct etherip_header *);
638 		/*
639 		 * GIF_ACCEPT_REVETHIP (enabled by default) intentionally
640 		 * accepts an EtherIP packet with revered version field in
641 		 * the header.  This is a knob for backward compatibility
642 		 * with FreeBSD 7.2R or prior.
643 		 */
644 		if (eip->eip_ver != ETHERIP_VERSION) {
645 			if ((gif_options & GIF_ACCEPT_REVETHIP) == 0 ||
646 			    eip->eip_resvl != ETHERIP_VERSION) {
647 				/* discard unknown versions */
648 				m_freem(m);
649 				goto drop;
650 			}
651 		}
652 		m_adj(m, sizeof(struct etherip_header));
653 
654 		m->m_flags &= ~(M_BCAST|M_MCAST);
655 		m->m_pkthdr.rcvif = ifp;
656 
657 		if (ifp->if_bridge) {
658 			oldifp = ifp;
659 			eh = mtod(m, struct ether_header *);
660 			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
661 				if (ETHER_IS_BROADCAST(eh->ether_dhost))
662 					m->m_flags |= M_BCAST;
663 				else
664 					m->m_flags |= M_MCAST;
665 				if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
666 			}
667 			BRIDGE_INPUT(ifp, m);
668 
669 			if (m != NULL && ifp != oldifp) {
670 				/*
671 				 * The bridge gave us back itself or one of the
672 				 * members for which the frame is addressed.
673 				 */
674 				ether_demux(ifp, m);
675 				return;
676 			}
677 		}
678 		if (m != NULL)
679 			m_freem(m);
680 		return;
681 
682 	default:
683 		if (ng_gif_input_orphan_p != NULL)
684 			(*ng_gif_input_orphan_p)(ifp, m, af);
685 		else
686 			m_freem(m);
687 		return;
688 	}
689 
690 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
691 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
692 	M_SETFIB(m, ifp->if_fib);
693 	netisr_dispatch(isr, m);
694 	return;
695 drop:
696 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
697 }
698 
699 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
700 int
701 gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
702 {
703 	GIF_RLOCK_TRACKER;
704 	struct ifreq *ifr = (struct ifreq*)data;
705 	struct sockaddr *dst, *src;
706 	struct gif_softc *sc;
707 #ifdef INET
708 	struct sockaddr_in *sin = NULL;
709 #endif
710 #ifdef INET6
711 	struct sockaddr_in6 *sin6 = NULL;
712 #endif
713 	u_int options;
714 	int error;
715 
716 	switch (cmd) {
717 	case SIOCSIFADDR:
718 		ifp->if_flags |= IFF_UP;
719 	case SIOCADDMULTI:
720 	case SIOCDELMULTI:
721 	case SIOCGIFMTU:
722 	case SIOCSIFFLAGS:
723 		return (0);
724 	case SIOCSIFMTU:
725 		if (ifr->ifr_mtu < GIF_MTU_MIN ||
726 		    ifr->ifr_mtu > GIF_MTU_MAX)
727 			return (EINVAL);
728 		else
729 			ifp->if_mtu = ifr->ifr_mtu;
730 		return (0);
731 	}
732 	sx_xlock(&gif_ioctl_sx);
733 	sc = ifp->if_softc;
734 	if (sc == NULL) {
735 		error = ENXIO;
736 		goto bad;
737 	}
738 	error = 0;
739 	switch (cmd) {
740 	case SIOCSIFPHYADDR:
741 #ifdef INET6
742 	case SIOCSIFPHYADDR_IN6:
743 #endif
744 		error = EINVAL;
745 		switch (cmd) {
746 #ifdef INET
747 		case SIOCSIFPHYADDR:
748 			src = (struct sockaddr *)
749 				&(((struct in_aliasreq *)data)->ifra_addr);
750 			dst = (struct sockaddr *)
751 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
752 			break;
753 #endif
754 #ifdef INET6
755 		case SIOCSIFPHYADDR_IN6:
756 			src = (struct sockaddr *)
757 				&(((struct in6_aliasreq *)data)->ifra_addr);
758 			dst = (struct sockaddr *)
759 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
760 			break;
761 #endif
762 		default:
763 			goto bad;
764 		}
765 		/* sa_family must be equal */
766 		if (src->sa_family != dst->sa_family ||
767 		    src->sa_len != dst->sa_len)
768 			goto bad;
769 
770 		/* validate sa_len */
771 		switch (src->sa_family) {
772 #ifdef INET
773 		case AF_INET:
774 			if (src->sa_len != sizeof(struct sockaddr_in))
775 				goto bad;
776 			break;
777 #endif
778 #ifdef INET6
779 		case AF_INET6:
780 			if (src->sa_len != sizeof(struct sockaddr_in6))
781 				goto bad;
782 			break;
783 #endif
784 		default:
785 			error = EAFNOSUPPORT;
786 			goto bad;
787 		}
788 		/* check sa_family looks sane for the cmd */
789 		error = EAFNOSUPPORT;
790 		switch (cmd) {
791 #ifdef INET
792 		case SIOCSIFPHYADDR:
793 			if (src->sa_family == AF_INET)
794 				break;
795 			goto bad;
796 #endif
797 #ifdef INET6
798 		case SIOCSIFPHYADDR_IN6:
799 			if (src->sa_family == AF_INET6)
800 				break;
801 			goto bad;
802 #endif
803 		}
804 		error = EADDRNOTAVAIL;
805 		switch (src->sa_family) {
806 #ifdef INET
807 		case AF_INET:
808 			if (satosin(src)->sin_addr.s_addr == INADDR_ANY ||
809 			    satosin(dst)->sin_addr.s_addr == INADDR_ANY)
810 				goto bad;
811 			break;
812 #endif
813 #ifdef INET6
814 		case AF_INET6:
815 			if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr)
816 			    ||
817 			    IN6_IS_ADDR_UNSPECIFIED(&satosin6(dst)->sin6_addr))
818 				goto bad;
819 			/*
820 			 * Check validity of the scope zone ID of the
821 			 * addresses, and convert it into the kernel
822 			 * internal form if necessary.
823 			 */
824 			error = sa6_embedscope(satosin6(src), 0);
825 			if (error != 0)
826 				goto bad;
827 			error = sa6_embedscope(satosin6(dst), 0);
828 			if (error != 0)
829 				goto bad;
830 #endif
831 		};
832 		error = gif_set_tunnel(ifp, src, dst);
833 		break;
834 	case SIOCDIFPHYADDR:
835 		gif_delete_tunnel(ifp);
836 		break;
837 	case SIOCGIFPSRCADDR:
838 	case SIOCGIFPDSTADDR:
839 #ifdef INET6
840 	case SIOCGIFPSRCADDR_IN6:
841 	case SIOCGIFPDSTADDR_IN6:
842 #endif
843 		if (sc->gif_family == 0) {
844 			error = EADDRNOTAVAIL;
845 			break;
846 		}
847 		GIF_RLOCK(sc);
848 		switch (cmd) {
849 #ifdef INET
850 		case SIOCGIFPSRCADDR:
851 		case SIOCGIFPDSTADDR:
852 			if (sc->gif_family != AF_INET) {
853 				error = EADDRNOTAVAIL;
854 				break;
855 			}
856 			sin = (struct sockaddr_in *)&ifr->ifr_addr;
857 			memset(sin, 0, sizeof(*sin));
858 			sin->sin_family = AF_INET;
859 			sin->sin_len = sizeof(*sin);
860 			break;
861 #endif
862 #ifdef INET6
863 		case SIOCGIFPSRCADDR_IN6:
864 		case SIOCGIFPDSTADDR_IN6:
865 			if (sc->gif_family != AF_INET6) {
866 				error = EADDRNOTAVAIL;
867 				break;
868 			}
869 			sin6 = (struct sockaddr_in6 *)
870 				&(((struct in6_ifreq *)data)->ifr_addr);
871 			memset(sin6, 0, sizeof(*sin6));
872 			sin6->sin6_family = AF_INET6;
873 			sin6->sin6_len = sizeof(*sin6);
874 			break;
875 #endif
876 		default:
877 			error = EAFNOSUPPORT;
878 		}
879 		if (error == 0) {
880 			switch (cmd) {
881 #ifdef INET
882 			case SIOCGIFPSRCADDR:
883 				sin->sin_addr = sc->gif_iphdr->ip_src;
884 				break;
885 			case SIOCGIFPDSTADDR:
886 				sin->sin_addr = sc->gif_iphdr->ip_dst;
887 				break;
888 #endif
889 #ifdef INET6
890 			case SIOCGIFPSRCADDR_IN6:
891 				sin6->sin6_addr = sc->gif_ip6hdr->ip6_src;
892 				break;
893 			case SIOCGIFPDSTADDR_IN6:
894 				sin6->sin6_addr = sc->gif_ip6hdr->ip6_dst;
895 				break;
896 #endif
897 			}
898 		}
899 		GIF_RUNLOCK(sc);
900 		if (error != 0)
901 			break;
902 		switch (cmd) {
903 #ifdef INET
904 		case SIOCGIFPSRCADDR:
905 		case SIOCGIFPDSTADDR:
906 			error = prison_if(curthread->td_ucred,
907 			    (struct sockaddr *)sin);
908 			if (error != 0)
909 				memset(sin, 0, sizeof(*sin));
910 			break;
911 #endif
912 #ifdef INET6
913 		case SIOCGIFPSRCADDR_IN6:
914 		case SIOCGIFPDSTADDR_IN6:
915 			error = prison_if(curthread->td_ucred,
916 			    (struct sockaddr *)sin6);
917 			if (error == 0)
918 				error = sa6_recoverscope(sin6);
919 			if (error != 0)
920 				memset(sin6, 0, sizeof(*sin6));
921 #endif
922 		}
923 		break;
924 	case SIOCGTUNFIB:
925 		ifr->ifr_fib = sc->gif_fibnum;
926 		break;
927 	case SIOCSTUNFIB:
928 		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
929 			break;
930 		if (ifr->ifr_fib >= rt_numfibs)
931 			error = EINVAL;
932 		else
933 			sc->gif_fibnum = ifr->ifr_fib;
934 		break;
935 	case GIFGOPTS:
936 		options = sc->gif_options;
937 		error = copyout(&options, ifr->ifr_data, sizeof(options));
938 		break;
939 	case GIFSOPTS:
940 		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
941 			break;
942 		error = copyin(ifr->ifr_data, &options, sizeof(options));
943 		if (error)
944 			break;
945 		if (options & ~GIF_OPTMASK)
946 			error = EINVAL;
947 		else
948 			sc->gif_options = options;
949 		break;
950 	default:
951 		error = EINVAL;
952 		break;
953 	}
954 bad:
955 	sx_xunlock(&gif_ioctl_sx);
956 	return (error);
957 }
958 
959 static void
960 gif_detach(struct gif_softc *sc)
961 {
962 
963 	sx_assert(&gif_ioctl_sx, SA_XLOCKED);
964 	if (sc->gif_ecookie != NULL)
965 		encap_detach(sc->gif_ecookie);
966 	sc->gif_ecookie = NULL;
967 }
968 
969 static int
970 gif_attach(struct gif_softc *sc, int af)
971 {
972 
973 	sx_assert(&gif_ioctl_sx, SA_XLOCKED);
974 	switch (af) {
975 #ifdef INET
976 	case AF_INET:
977 		return (in_gif_attach(sc));
978 #endif
979 #ifdef INET6
980 	case AF_INET6:
981 		return (in6_gif_attach(sc));
982 #endif
983 	}
984 	return (EAFNOSUPPORT);
985 }
986 
987 static int
988 gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
989 {
990 	struct gif_softc *sc = ifp->if_softc;
991 	struct gif_softc *tsc;
992 #ifdef INET
993 	struct ip *ip;
994 #endif
995 #ifdef INET6
996 	struct ip6_hdr *ip6;
997 #endif
998 	void *hdr;
999 	int error = 0;
1000 
1001 	if (sc == NULL)
1002 		return (ENXIO);
1003 	/* Disallow parallel tunnels unless instructed otherwise. */
1004 	if (V_parallel_tunnels == 0) {
1005 		GIF_LIST_LOCK();
1006 		LIST_FOREACH(tsc, &V_gif_softc_list, gif_list) {
1007 			if (tsc == sc || tsc->gif_family != src->sa_family)
1008 				continue;
1009 #ifdef INET
1010 			if (tsc->gif_family == AF_INET &&
1011 			    tsc->gif_iphdr->ip_src.s_addr ==
1012 			    satosin(src)->sin_addr.s_addr &&
1013 			    tsc->gif_iphdr->ip_dst.s_addr ==
1014 			    satosin(dst)->sin_addr.s_addr) {
1015 				error = EADDRNOTAVAIL;
1016 				GIF_LIST_UNLOCK();
1017 				goto bad;
1018 			}
1019 #endif
1020 #ifdef INET6
1021 			if (tsc->gif_family == AF_INET6 &&
1022 			    IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_src,
1023 			    &satosin6(src)->sin6_addr) &&
1024 			    IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_dst,
1025 			    &satosin6(dst)->sin6_addr)) {
1026 				error = EADDRNOTAVAIL;
1027 				GIF_LIST_UNLOCK();
1028 				goto bad;
1029 			}
1030 #endif
1031 		}
1032 		GIF_LIST_UNLOCK();
1033 	}
1034 	switch (src->sa_family) {
1035 #ifdef INET
1036 	case AF_INET:
1037 		hdr = ip = malloc(sizeof(struct ip), M_GIF,
1038 		    M_WAITOK | M_ZERO);
1039 		ip->ip_src.s_addr = satosin(src)->sin_addr.s_addr;
1040 		ip->ip_dst.s_addr = satosin(dst)->sin_addr.s_addr;
1041 		break;
1042 #endif
1043 #ifdef INET6
1044 	case AF_INET6:
1045 		hdr = ip6 = malloc(sizeof(struct ip6_hdr), M_GIF,
1046 		    M_WAITOK | M_ZERO);
1047 		ip6->ip6_src = satosin6(src)->sin6_addr;
1048 		ip6->ip6_dst = satosin6(dst)->sin6_addr;
1049 		ip6->ip6_vfc = IPV6_VERSION;
1050 		break;
1051 #endif
1052 	default:
1053 		return (EAFNOSUPPORT);
1054 	};
1055 
1056 	if (sc->gif_family != src->sa_family)
1057 		gif_detach(sc);
1058 	if (sc->gif_family == 0 ||
1059 	    sc->gif_family != src->sa_family)
1060 		error = gif_attach(sc, src->sa_family);
1061 
1062 	GIF_WLOCK(sc);
1063 	if (sc->gif_family != 0)
1064 		free(sc->gif_hdr, M_GIF);
1065 	sc->gif_family = src->sa_family;
1066 	sc->gif_hdr = hdr;
1067 	GIF_WUNLOCK(sc);
1068 #if defined(INET) || defined(INET6)
1069 bad:
1070 #endif
1071 	if (error == 0 && sc->gif_family != 0)
1072 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1073 	else
1074 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1075 	return (error);
1076 }
1077 
1078 static void
1079 gif_delete_tunnel(struct ifnet *ifp)
1080 {
1081 	struct gif_softc *sc = ifp->if_softc;
1082 	int family;
1083 
1084 	if (sc == NULL)
1085 		return;
1086 
1087 	GIF_WLOCK(sc);
1088 	family = sc->gif_family;
1089 	sc->gif_family = 0;
1090 	GIF_WUNLOCK(sc);
1091 	if (family != 0) {
1092 		gif_detach(sc);
1093 		free(sc->gif_hdr, M_GIF);
1094 	}
1095 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1096 }
1097