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