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