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