xref: /freebsd/sys/net/if_gif.c (revision 39beb93c)
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 #include "opt_mac.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.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/proc.h>
50 #include <sys/protosw.h>
51 #include <sys/conf.h>
52 #include <sys/vimage.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 
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #ifdef	INET
66 #include <netinet/in_var.h>
67 #include <netinet/in_gif.h>
68 #include <netinet/ip_var.h>
69 #endif	/* INET */
70 
71 #ifdef INET6
72 #ifndef INET
73 #include <netinet/in.h>
74 #endif
75 #include <netinet6/in6_var.h>
76 #include <netinet/ip6.h>
77 #include <netinet6/ip6_var.h>
78 #include <netinet6/scope6_var.h>
79 #include <netinet6/in6_gif.h>
80 #include <netinet6/ip6protosw.h>
81 #endif /* INET6 */
82 
83 #include <netinet/ip_encap.h>
84 #include <net/ethernet.h>
85 #include <net/if_bridgevar.h>
86 #include <net/if_gif.h>
87 
88 #include <security/mac/mac_framework.h>
89 
90 #define GIFNAME		"gif"
91 
92 /*
93  * gif_mtx protects the global gif_softc_list.
94  */
95 static struct mtx gif_mtx;
96 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
97 
98 #ifndef VIMAGE
99 #ifndef VIMAGE_GLOBALS
100 struct vnet_gif vnet_gif_0;
101 #endif
102 #endif
103 
104 #ifdef VIMAGE_GLOBALS
105 static LIST_HEAD(, gif_softc) gif_softc_list;
106 static int max_gif_nesting;
107 static int parallel_tunnels;
108 #ifdef INET
109 int ip_gif_ttl;
110 #endif
111 #ifdef INET6
112 int ip6_gif_hlim;
113 #endif
114 #endif
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 void	gif_start(struct ifnet *);
122 static int	gif_clone_create(struct if_clone *, int, caddr_t);
123 static void	gif_clone_destroy(struct ifnet *);
124 
125 IFC_SIMPLE_DECLARE(gif, 0);
126 
127 static int gifmodevent(module_t, int, void *);
128 
129 SYSCTL_DECL(_net_link);
130 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
131     "Generic Tunnel Interface");
132 #ifndef MAX_GIF_NEST
133 /*
134  * This macro controls the default upper limitation on nesting of gif tunnels.
135  * Since, setting a large value to this macro with a careless configuration
136  * may introduce system crash, we don't allow any nestings by default.
137  * If you need to configure nested gif tunnels, you can define this macro
138  * in your kernel configuration file.  However, if you do so, please be
139  * careful to configure the tunnels so that it won't make a loop.
140  */
141 #define MAX_GIF_NEST 1
142 #endif
143 SYSCTL_V_INT(V_NET, vnet_gif, _net_link_gif, OID_AUTO, max_nesting,
144     CTLFLAG_RW, max_gif_nesting, 0, "Max nested tunnels");
145 
146 #ifdef INET6
147 SYSCTL_DECL(_net_inet6_ip6);
148 SYSCTL_V_INT(V_NET, vnet_gif, _net_inet6_ip6, IPV6CTL_GIF_HLIM,
149     gifhlim, CTLFLAG_RW, ip6_gif_hlim, 0, "");
150 #endif
151 
152 /*
153  * By default, we disallow creation of multiple tunnels between the same
154  * pair of addresses.  Some applications require this functionality so
155  * we allow control over this check here.
156  */
157 SYSCTL_V_INT(V_NET, vnet_gif, _net_link_gif, OID_AUTO, parallel_tunnels,
158     CTLFLAG_RW, parallel_tunnels, 0, "Allow parallel tunnels?");
159 
160 /* copy from src/sys/net/if_ethersubr.c */
161 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
162 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
163 #ifndef ETHER_IS_BROADCAST
164 #define ETHER_IS_BROADCAST(addr) \
165 	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
166 #endif
167 
168 static int
169 gif_clone_create(ifc, unit, params)
170 	struct if_clone *ifc;
171 	int unit;
172 	caddr_t params;
173 {
174 	INIT_VNET_GIF(curvnet);
175 	struct gif_softc *sc;
176 
177 	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
178 	sc->gif_fibnum = curthread->td_proc->p_fibnum;
179 	GIF2IFP(sc) = if_alloc(IFT_GIF);
180 	if (GIF2IFP(sc) == NULL) {
181 		free(sc, M_GIF);
182 		return (ENOSPC);
183 	}
184 
185 	GIF_LOCK_INIT(sc);
186 
187 	GIF2IFP(sc)->if_softc = sc;
188 	if_initname(GIF2IFP(sc), ifc->ifc_name, unit);
189 
190 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
191 
192 	GIF2IFP(sc)->if_addrlen = 0;
193 	GIF2IFP(sc)->if_mtu    = GIF_MTU;
194 	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
195 #if 0
196 	/* turn off ingress filter */
197 	GIF2IFP(sc)->if_flags  |= IFF_LINK2;
198 #endif
199 	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
200 	GIF2IFP(sc)->if_start  = gif_start;
201 	GIF2IFP(sc)->if_output = gif_output;
202 	GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
203 	if_attach(GIF2IFP(sc));
204 	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
205 	if (ng_gif_attach_p != NULL)
206 		(*ng_gif_attach_p)(GIF2IFP(sc));
207 
208 	mtx_lock(&gif_mtx);
209 	LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list);
210 	mtx_unlock(&gif_mtx);
211 
212 	return (0);
213 }
214 
215 static void
216 gif_clone_destroy(ifp)
217 	struct ifnet *ifp;
218 {
219 #if defined(INET) || defined(INET6)
220 	int err;
221 #endif
222 	struct gif_softc *sc = ifp->if_softc;
223 
224 	mtx_lock(&gif_mtx);
225 	LIST_REMOVE(sc, gif_list);
226 	mtx_unlock(&gif_mtx);
227 
228 	gif_delete_tunnel(ifp);
229 #ifdef INET6
230 	if (sc->encap_cookie6 != NULL) {
231 		err = encap_detach(sc->encap_cookie6);
232 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
233 	}
234 #endif
235 #ifdef INET
236 	if (sc->encap_cookie4 != NULL) {
237 		err = encap_detach(sc->encap_cookie4);
238 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
239 	}
240 #endif
241 
242 	if (ng_gif_detach_p != NULL)
243 		(*ng_gif_detach_p)(ifp);
244 	bpfdetach(ifp);
245 	if_detach(ifp);
246 	if_free(ifp);
247 
248 	GIF_LOCK_DESTROY(sc);
249 
250 	free(sc, M_GIF);
251 }
252 
253 static int
254 gifmodevent(mod, type, data)
255 	module_t mod;
256 	int type;
257 	void *data;
258 {
259 
260 	switch (type) {
261 	case MOD_LOAD:
262 		mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
263 
264 		LIST_INIT(&V_gif_softc_list);
265 		V_max_gif_nesting = MAX_GIF_NEST;
266 #ifdef XBONEHACK
267 		V_parallel_tunnels = 1;
268 #else
269 		V_parallel_tunnels = 0;
270 #endif
271 #ifdef INET
272 		V_ip_gif_ttl = GIF_TTL;
273 #endif
274 #ifdef INET6
275 		V_ip6_gif_hlim = GIF_HLIM;
276 #endif
277 		if_clone_attach(&gif_cloner);
278 
279 		break;
280 	case MOD_UNLOAD:
281 		if_clone_detach(&gif_cloner);
282 		mtx_destroy(&gif_mtx);
283 #ifdef INET6
284 		V_ip6_gif_hlim = 0;
285 #endif
286 		break;
287 	default:
288 		return EOPNOTSUPP;
289 	}
290 	return 0;
291 }
292 
293 static moduledata_t gif_mod = {
294 	"if_gif",
295 	gifmodevent,
296 	0
297 };
298 
299 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
300 MODULE_VERSION(if_gif, 1);
301 
302 int
303 gif_encapcheck(m, off, proto, arg)
304 	const struct mbuf *m;
305 	int off;
306 	int proto;
307 	void *arg;
308 {
309 	struct ip ip;
310 	struct gif_softc *sc;
311 
312 	sc = (struct gif_softc *)arg;
313 	if (sc == NULL)
314 		return 0;
315 
316 	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
317 		return 0;
318 
319 	/* no physical address */
320 	if (!sc->gif_psrc || !sc->gif_pdst)
321 		return 0;
322 
323 	switch (proto) {
324 #ifdef INET
325 	case IPPROTO_IPV4:
326 		break;
327 #endif
328 #ifdef INET6
329 	case IPPROTO_IPV6:
330 		break;
331 #endif
332 	case IPPROTO_ETHERIP:
333 		break;
334 
335 	default:
336 		return 0;
337 	}
338 
339 	/* Bail on short packets */
340 	if (m->m_pkthdr.len < sizeof(ip))
341 		return 0;
342 
343 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
344 
345 	switch (ip.ip_v) {
346 #ifdef INET
347 	case 4:
348 		if (sc->gif_psrc->sa_family != AF_INET ||
349 		    sc->gif_pdst->sa_family != AF_INET)
350 			return 0;
351 		return gif_encapcheck4(m, off, proto, arg);
352 #endif
353 #ifdef INET6
354 	case 6:
355 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
356 			return 0;
357 		if (sc->gif_psrc->sa_family != AF_INET6 ||
358 		    sc->gif_pdst->sa_family != AF_INET6)
359 			return 0;
360 		return gif_encapcheck6(m, off, proto, arg);
361 #endif
362 	default:
363 		return 0;
364 	}
365 }
366 
367 static void
368 gif_start(struct ifnet *ifp)
369 {
370 	struct gif_softc *sc;
371 	struct mbuf *m;
372 
373 	sc = ifp->if_softc;
374 
375 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
376 	for (;;) {
377 		IFQ_DEQUEUE(&ifp->if_snd, m);
378 		if (m == 0)
379 			break;
380 
381 		gif_output(ifp, m, sc->gif_pdst, NULL);
382 
383 	}
384 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
385 
386 	return;
387 }
388 
389 int
390 gif_output(ifp, m, dst, rt)
391 	struct ifnet *ifp;
392 	struct mbuf *m;
393 	struct sockaddr *dst;
394 	struct rtentry *rt;	/* added in net2 */
395 {
396 	INIT_VNET_GIF(ifp->if_vnet);
397 	struct gif_softc *sc = ifp->if_softc;
398 	struct m_tag *mtag;
399 	int error = 0;
400 	int gif_called;
401 	u_int32_t af;
402 
403 #ifdef MAC
404 	error = mac_ifnet_check_transmit(ifp, m);
405 	if (error) {
406 		m_freem(m);
407 		goto end;
408 	}
409 #endif
410 
411 	/*
412 	 * gif may cause infinite recursion calls when misconfigured.
413 	 * We'll prevent this by detecting loops.
414 	 *
415 	 * High nesting level may cause stack exhaustion.
416 	 * We'll prevent this by introducing upper limit.
417 	 */
418 	gif_called = 1;
419 	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
420 	while (mtag != NULL) {
421 		if (*(struct ifnet **)(mtag + 1) == ifp) {
422 			log(LOG_NOTICE,
423 			    "gif_output: loop detected on %s\n",
424 			    (*(struct ifnet **)(mtag + 1))->if_xname);
425 			m_freem(m);
426 			error = EIO;	/* is there better errno? */
427 			goto end;
428 		}
429 		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
430 		gif_called++;
431 	}
432 	if (gif_called > V_max_gif_nesting) {
433 		log(LOG_NOTICE,
434 		    "gif_output: recursively called too many times(%d)\n",
435 		    gif_called);
436 		m_freem(m);
437 		error = EIO;	/* is there better errno? */
438 		goto end;
439 	}
440 	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
441 	    M_NOWAIT);
442 	if (mtag == NULL) {
443 		m_freem(m);
444 		error = ENOMEM;
445 		goto end;
446 	}
447 	*(struct ifnet **)(mtag + 1) = ifp;
448 	m_tag_prepend(m, mtag);
449 
450 	m->m_flags &= ~(M_BCAST|M_MCAST);
451 
452 	GIF_LOCK(sc);
453 
454 	if (!(ifp->if_flags & IFF_UP) ||
455 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
456 		GIF_UNLOCK(sc);
457 		m_freem(m);
458 		error = ENETDOWN;
459 		goto end;
460 	}
461 
462 	/* BPF writes need to be handled specially. */
463 	if (dst->sa_family == AF_UNSPEC) {
464 		bcopy(dst->sa_data, &af, sizeof(af));
465 		dst->sa_family = af;
466 	}
467 
468 	af = dst->sa_family;
469 	BPF_MTAP2(ifp, &af, sizeof(af), m);
470 	ifp->if_opackets++;
471 	ifp->if_obytes += m->m_pkthdr.len;
472 
473 	/* override to IPPROTO_ETHERIP for bridged traffic */
474 	if (ifp->if_bridge)
475 		af = AF_LINK;
476 
477 	M_SETFIB(m, sc->gif_fibnum);
478 	/* inner AF-specific encapsulation */
479 
480 	/* XXX should we check if our outer source is legal? */
481 
482 	/* dispatch to output logic based on outer AF */
483 	switch (sc->gif_psrc->sa_family) {
484 #ifdef INET
485 	case AF_INET:
486 		error = in_gif_output(ifp, af, m);
487 		break;
488 #endif
489 #ifdef INET6
490 	case AF_INET6:
491 		error = in6_gif_output(ifp, af, m);
492 		break;
493 #endif
494 	default:
495 		m_freem(m);
496 		error = ENETDOWN;
497 	}
498 
499 	GIF_UNLOCK(sc);
500   end:
501 	if (error)
502 		ifp->if_oerrors++;
503 	return (error);
504 }
505 
506 void
507 gif_input(m, af, ifp)
508 	struct mbuf *m;
509 	int af;
510 	struct ifnet *ifp;
511 {
512 	int isr, n;
513 	struct etherip_header *eip;
514 	struct ether_header *eh;
515 	struct ifnet *oldifp;
516 
517 	if (ifp == NULL) {
518 		/* just in case */
519 		m_freem(m);
520 		return;
521 	}
522 
523 	m->m_pkthdr.rcvif = ifp;
524 
525 #ifdef MAC
526 	mac_ifnet_create_mbuf(ifp, m);
527 #endif
528 
529 	if (bpf_peers_present(ifp->if_bpf)) {
530 		u_int32_t af1 = af;
531 		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
532 	}
533 
534 	if (ng_gif_input_p != NULL) {
535 		(*ng_gif_input_p)(ifp, &m, af);
536 		if (m == NULL)
537 			return;
538 	}
539 
540 	/*
541 	 * Put the packet to the network layer input queue according to the
542 	 * specified address family.
543 	 * Note: older versions of gif_input directly called network layer
544 	 * input functions, e.g. ip6_input, here.  We changed the policy to
545 	 * prevent too many recursive calls of such input functions, which
546 	 * might cause kernel panic.  But the change may introduce another
547 	 * problem; if the input queue is full, packets are discarded.
548 	 * The kernel stack overflow really happened, and we believed
549 	 * queue-full rarely occurs, so we changed the policy.
550 	 */
551 	switch (af) {
552 #ifdef INET
553 	case AF_INET:
554 		isr = NETISR_IP;
555 		break;
556 #endif
557 #ifdef INET6
558 	case AF_INET6:
559 		isr = NETISR_IPV6;
560 		break;
561 #endif
562 	case AF_LINK:
563 		n = sizeof(struct etherip_header) + sizeof(struct ether_header);
564 		if (n > m->m_len) {
565 			m = m_pullup(m, n);
566 			if (m == NULL) {
567 				ifp->if_ierrors++;
568 				return;
569 			}
570 		}
571 
572 		eip = mtod(m, struct etherip_header *);
573  		if (eip->eip_ver !=
574 		    (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) {
575 			/* discard unknown versions */
576 			m_freem(m);
577 			return;
578 		}
579 		m_adj(m, sizeof(struct etherip_header));
580 
581 		m->m_flags &= ~(M_BCAST|M_MCAST);
582 		m->m_pkthdr.rcvif = ifp;
583 
584 		if (ifp->if_bridge) {
585 			oldifp = ifp;
586 			eh = mtod(m, struct ether_header *);
587 			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
588 				if (ETHER_IS_BROADCAST(eh->ether_dhost))
589 					m->m_flags |= M_BCAST;
590 				else
591 					m->m_flags |= M_MCAST;
592 				ifp->if_imcasts++;
593 			}
594 			BRIDGE_INPUT(ifp, m);
595 
596 			if (m != NULL && ifp != oldifp) {
597 				/*
598 				 * The bridge gave us back itself or one of the
599 				 * members for which the frame is addressed.
600 				 */
601 				ether_demux(ifp, m);
602 				return;
603 			}
604 		}
605 		if (m != NULL)
606 			m_freem(m);
607 		return;
608 
609 	default:
610 		if (ng_gif_input_orphan_p != NULL)
611 			(*ng_gif_input_orphan_p)(ifp, m, af);
612 		else
613 			m_freem(m);
614 		return;
615 	}
616 
617 	ifp->if_ipackets++;
618 	ifp->if_ibytes += m->m_pkthdr.len;
619 	netisr_dispatch(isr, m);
620 }
621 
622 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
623 int
624 gif_ioctl(ifp, cmd, data)
625 	struct ifnet *ifp;
626 	u_long cmd;
627 	caddr_t data;
628 {
629 	struct gif_softc *sc  = ifp->if_softc;
630 	struct ifreq     *ifr = (struct ifreq*)data;
631 	int error = 0, size;
632 	struct sockaddr *dst, *src;
633 #ifdef	SIOCSIFMTU /* xxx */
634 	u_long mtu;
635 #endif
636 
637 	switch (cmd) {
638 	case SIOCSIFADDR:
639 		ifp->if_flags |= IFF_UP;
640 		break;
641 
642 	case SIOCSIFDSTADDR:
643 		break;
644 
645 	case SIOCADDMULTI:
646 	case SIOCDELMULTI:
647 		break;
648 
649 #ifdef	SIOCSIFMTU /* xxx */
650 	case SIOCGIFMTU:
651 		break;
652 
653 	case SIOCSIFMTU:
654 		mtu = ifr->ifr_mtu;
655 		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
656 			return (EINVAL);
657 		ifp->if_mtu = mtu;
658 		break;
659 #endif /* SIOCSIFMTU */
660 
661 #ifdef INET
662 	case SIOCSIFPHYADDR:
663 #endif
664 #ifdef INET6
665 	case SIOCSIFPHYADDR_IN6:
666 #endif /* INET6 */
667 	case SIOCSLIFPHYADDR:
668 		switch (cmd) {
669 #ifdef INET
670 		case SIOCSIFPHYADDR:
671 			src = (struct sockaddr *)
672 				&(((struct in_aliasreq *)data)->ifra_addr);
673 			dst = (struct sockaddr *)
674 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
675 			break;
676 #endif
677 #ifdef INET6
678 		case SIOCSIFPHYADDR_IN6:
679 			src = (struct sockaddr *)
680 				&(((struct in6_aliasreq *)data)->ifra_addr);
681 			dst = (struct sockaddr *)
682 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
683 			break;
684 #endif
685 		case SIOCSLIFPHYADDR:
686 			src = (struct sockaddr *)
687 				&(((struct if_laddrreq *)data)->addr);
688 			dst = (struct sockaddr *)
689 				&(((struct if_laddrreq *)data)->dstaddr);
690 			break;
691 		default:
692 			return EINVAL;
693 		}
694 
695 		/* sa_family must be equal */
696 		if (src->sa_family != dst->sa_family)
697 			return EINVAL;
698 
699 		/* validate sa_len */
700 		switch (src->sa_family) {
701 #ifdef INET
702 		case AF_INET:
703 			if (src->sa_len != sizeof(struct sockaddr_in))
704 				return EINVAL;
705 			break;
706 #endif
707 #ifdef INET6
708 		case AF_INET6:
709 			if (src->sa_len != sizeof(struct sockaddr_in6))
710 				return EINVAL;
711 			break;
712 #endif
713 		default:
714 			return EAFNOSUPPORT;
715 		}
716 		switch (dst->sa_family) {
717 #ifdef INET
718 		case AF_INET:
719 			if (dst->sa_len != sizeof(struct sockaddr_in))
720 				return EINVAL;
721 			break;
722 #endif
723 #ifdef INET6
724 		case AF_INET6:
725 			if (dst->sa_len != sizeof(struct sockaddr_in6))
726 				return EINVAL;
727 			break;
728 #endif
729 		default:
730 			return EAFNOSUPPORT;
731 		}
732 
733 		/* check sa_family looks sane for the cmd */
734 		switch (cmd) {
735 		case SIOCSIFPHYADDR:
736 			if (src->sa_family == AF_INET)
737 				break;
738 			return EAFNOSUPPORT;
739 #ifdef INET6
740 		case SIOCSIFPHYADDR_IN6:
741 			if (src->sa_family == AF_INET6)
742 				break;
743 			return EAFNOSUPPORT;
744 #endif /* INET6 */
745 		case SIOCSLIFPHYADDR:
746 			/* checks done in the above */
747 			break;
748 		}
749 
750 		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
751 		break;
752 
753 #ifdef SIOCDIFPHYADDR
754 	case SIOCDIFPHYADDR:
755 		gif_delete_tunnel(GIF2IFP(sc));
756 		break;
757 #endif
758 
759 	case SIOCGIFPSRCADDR:
760 #ifdef INET6
761 	case SIOCGIFPSRCADDR_IN6:
762 #endif /* INET6 */
763 		if (sc->gif_psrc == NULL) {
764 			error = EADDRNOTAVAIL;
765 			goto bad;
766 		}
767 		src = sc->gif_psrc;
768 		switch (cmd) {
769 #ifdef INET
770 		case SIOCGIFPSRCADDR:
771 			dst = &ifr->ifr_addr;
772 			size = sizeof(ifr->ifr_addr);
773 			break;
774 #endif /* INET */
775 #ifdef INET6
776 		case SIOCGIFPSRCADDR_IN6:
777 			dst = (struct sockaddr *)
778 				&(((struct in6_ifreq *)data)->ifr_addr);
779 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
780 			break;
781 #endif /* INET6 */
782 		default:
783 			error = EADDRNOTAVAIL;
784 			goto bad;
785 		}
786 		if (src->sa_len > size)
787 			return EINVAL;
788 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
789 #ifdef INET6
790 		if (dst->sa_family == AF_INET6) {
791 			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
792 			if (error != 0)
793 				return (error);
794 		}
795 #endif
796 		break;
797 
798 	case SIOCGIFPDSTADDR:
799 #ifdef INET6
800 	case SIOCGIFPDSTADDR_IN6:
801 #endif /* INET6 */
802 		if (sc->gif_pdst == NULL) {
803 			error = EADDRNOTAVAIL;
804 			goto bad;
805 		}
806 		src = sc->gif_pdst;
807 		switch (cmd) {
808 #ifdef INET
809 		case SIOCGIFPDSTADDR:
810 			dst = &ifr->ifr_addr;
811 			size = sizeof(ifr->ifr_addr);
812 			break;
813 #endif /* INET */
814 #ifdef INET6
815 		case SIOCGIFPDSTADDR_IN6:
816 			dst = (struct sockaddr *)
817 				&(((struct in6_ifreq *)data)->ifr_addr);
818 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
819 			break;
820 #endif /* INET6 */
821 		default:
822 			error = EADDRNOTAVAIL;
823 			goto bad;
824 		}
825 		if (src->sa_len > size)
826 			return EINVAL;
827 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
828 #ifdef INET6
829 		if (dst->sa_family == AF_INET6) {
830 			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
831 			if (error != 0)
832 				return (error);
833 		}
834 #endif
835 		break;
836 
837 	case SIOCGLIFPHYADDR:
838 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
839 			error = EADDRNOTAVAIL;
840 			goto bad;
841 		}
842 
843 		/* copy src */
844 		src = sc->gif_psrc;
845 		dst = (struct sockaddr *)
846 			&(((struct if_laddrreq *)data)->addr);
847 		size = sizeof(((struct if_laddrreq *)data)->addr);
848 		if (src->sa_len > size)
849 			return EINVAL;
850 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
851 
852 		/* copy dst */
853 		src = sc->gif_pdst;
854 		dst = (struct sockaddr *)
855 			&(((struct if_laddrreq *)data)->dstaddr);
856 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
857 		if (src->sa_len > size)
858 			return EINVAL;
859 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
860 		break;
861 
862 	case SIOCSIFFLAGS:
863 		/* if_ioctl() takes care of it */
864 		break;
865 
866 	default:
867 		error = EINVAL;
868 		break;
869 	}
870  bad:
871 	return error;
872 }
873 
874 /*
875  * XXXRW: There's a general event-ordering issue here: the code to check
876  * if a given tunnel is already present happens before we perform a
877  * potentially blocking setup of the tunnel.  This code needs to be
878  * re-ordered so that the check and replacement can be atomic using
879  * a mutex.
880  */
881 int
882 gif_set_tunnel(ifp, src, dst)
883 	struct ifnet *ifp;
884 	struct sockaddr *src;
885 	struct sockaddr *dst;
886 {
887 	INIT_VNET_GIF(ifp->if_vnet);
888 	struct gif_softc *sc = ifp->if_softc;
889 	struct gif_softc *sc2;
890 	struct sockaddr *osrc, *odst, *sa;
891 	int error = 0;
892 
893 	mtx_lock(&gif_mtx);
894 	LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) {
895 		if (sc2 == sc)
896 			continue;
897 		if (!sc2->gif_pdst || !sc2->gif_psrc)
898 			continue;
899 		if (sc2->gif_pdst->sa_family != dst->sa_family ||
900 		    sc2->gif_pdst->sa_len != dst->sa_len ||
901 		    sc2->gif_psrc->sa_family != src->sa_family ||
902 		    sc2->gif_psrc->sa_len != src->sa_len)
903 			continue;
904 
905 		/*
906 		 * Disallow parallel tunnels unless instructed
907 		 * otherwise.
908 		 */
909 		if (!V_parallel_tunnels &&
910 		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
911 		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
912 			error = EADDRNOTAVAIL;
913 			mtx_unlock(&gif_mtx);
914 			goto bad;
915 		}
916 
917 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
918 	}
919 	mtx_unlock(&gif_mtx);
920 
921 	/* XXX we can detach from both, but be polite just in case */
922 	if (sc->gif_psrc)
923 		switch (sc->gif_psrc->sa_family) {
924 #ifdef INET
925 		case AF_INET:
926 			(void)in_gif_detach(sc);
927 			break;
928 #endif
929 #ifdef INET6
930 		case AF_INET6:
931 			(void)in6_gif_detach(sc);
932 			break;
933 #endif
934 		}
935 
936 	osrc = sc->gif_psrc;
937 	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
938 	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
939 	sc->gif_psrc = sa;
940 
941 	odst = sc->gif_pdst;
942 	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
943 	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
944 	sc->gif_pdst = sa;
945 
946 	switch (sc->gif_psrc->sa_family) {
947 #ifdef INET
948 	case AF_INET:
949 		error = in_gif_attach(sc);
950 		break;
951 #endif
952 #ifdef INET6
953 	case AF_INET6:
954 		/*
955 		 * Check validity of the scope zone ID of the addresses, and
956 		 * convert it into the kernel internal form if necessary.
957 		 */
958 		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
959 		if (error != 0)
960 			break;
961 		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
962 		if (error != 0)
963 			break;
964 		error = in6_gif_attach(sc);
965 		break;
966 #endif
967 	}
968 	if (error) {
969 		/* rollback */
970 		free((caddr_t)sc->gif_psrc, M_IFADDR);
971 		free((caddr_t)sc->gif_pdst, M_IFADDR);
972 		sc->gif_psrc = osrc;
973 		sc->gif_pdst = odst;
974 		goto bad;
975 	}
976 
977 	if (osrc)
978 		free((caddr_t)osrc, M_IFADDR);
979 	if (odst)
980 		free((caddr_t)odst, M_IFADDR);
981 
982  bad:
983 	if (sc->gif_psrc && sc->gif_pdst)
984 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
985 	else
986 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
987 
988 	return error;
989 }
990 
991 void
992 gif_delete_tunnel(ifp)
993 	struct ifnet *ifp;
994 {
995 	struct gif_softc *sc = ifp->if_softc;
996 
997 	if (sc->gif_psrc) {
998 		free((caddr_t)sc->gif_psrc, M_IFADDR);
999 		sc->gif_psrc = NULL;
1000 	}
1001 	if (sc->gif_pdst) {
1002 		free((caddr_t)sc->gif_pdst, M_IFADDR);
1003 		sc->gif_pdst = NULL;
1004 	}
1005 	/* it is safe to detach from both */
1006 #ifdef INET
1007 	(void)in_gif_detach(sc);
1008 #endif
1009 #ifdef INET6
1010 	(void)in6_gif_detach(sc);
1011 #endif
1012 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1013 }
1014