if_gif.c revision 273859
1/*	$FreeBSD: stable/10/sys/net/if_gif.c 273859 2014-10-30 13:53:57Z ae $	*/
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
91static const char gifname[] = "gif";
92
93/*
94 * gif_mtx protects the global gif_softc_list.
95 */
96static struct mtx gif_mtx;
97static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
98static VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list);
99#define	V_gif_softc_list	VNET(gif_softc_list)
100
101void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
102void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
103void	(*ng_gif_attach_p)(struct ifnet *ifp);
104void	(*ng_gif_detach_p)(struct ifnet *ifp);
105
106static void	gif_start(struct ifnet *);
107static int	gif_clone_create(struct if_clone *, int, caddr_t);
108static void	gif_clone_destroy(struct ifnet *);
109static struct if_clone *gif_cloner;
110
111static int gifmodevent(module_t, int, void *);
112
113SYSCTL_DECL(_net_link);
114static 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
127static VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST;
128#define	V_max_gif_nesting	VNET(max_gif_nesting)
129SYSCTL_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
138static VNET_DEFINE(int, parallel_tunnels) = 1;
139#else
140static VNET_DEFINE(int, parallel_tunnels) = 0;
141#endif
142#define	V_parallel_tunnels	VNET(parallel_tunnels)
143SYSCTL_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 */
147static 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
154static int
155gif_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
201static void
202gif_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
239static void
240vnet_gif_init(const void *unused __unused)
241{
242
243	LIST_INIT(&V_gif_softc_list);
244}
245VNET_SYSINIT(vnet_gif_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, vnet_gif_init,
246    NULL);
247
248static int
249gifmodevent(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
272static moduledata_t gif_mod = {
273	"if_gif",
274	gifmodevent,
275	0
276};
277
278DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
279MODULE_VERSION(if_gif, 1);
280
281int
282gif_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
352static void
353gif_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
424int
425gif_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
530void
531gif_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	m_clrprotoflags(m);
550
551#ifdef MAC
552	mac_ifnet_create_mbuf(ifp, m);
553#endif
554
555	if (bpf_peers_present(ifp->if_bpf)) {
556		u_int32_t af1 = af;
557		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
558	}
559
560	if ((ifp->if_flags & IFF_MONITOR) != 0) {
561		ifp->if_ipackets++;
562		ifp->if_ibytes += m->m_pkthdr.len;
563		m_freem(m);
564		return;
565	}
566
567	if (ng_gif_input_p != NULL) {
568		(*ng_gif_input_p)(ifp, &m, af);
569		if (m == NULL)
570			return;
571	}
572
573	/*
574	 * Put the packet to the network layer input queue according to the
575	 * specified address family.
576	 * Note: older versions of gif_input directly called network layer
577	 * input functions, e.g. ip6_input, here.  We changed the policy to
578	 * prevent too many recursive calls of such input functions, which
579	 * might cause kernel panic.  But the change may introduce another
580	 * problem; if the input queue is full, packets are discarded.
581	 * The kernel stack overflow really happened, and we believed
582	 * queue-full rarely occurs, so we changed the policy.
583	 */
584	switch (af) {
585#ifdef INET
586	case AF_INET:
587		isr = NETISR_IP;
588		break;
589#endif
590#ifdef INET6
591	case AF_INET6:
592		isr = NETISR_IPV6;
593		break;
594#endif
595	case AF_LINK:
596		n = sizeof(struct etherip_header) + sizeof(struct ether_header);
597		if (n > m->m_len) {
598			m = m_pullup(m, n);
599			if (m == NULL) {
600				ifp->if_ierrors++;
601				return;
602			}
603		}
604
605		eip = mtod(m, struct etherip_header *);
606		/*
607		 * GIF_ACCEPT_REVETHIP (enabled by default) intentionally
608		 * accepts an EtherIP packet with revered version field in
609		 * the header.  This is a knob for backward compatibility
610		 * with FreeBSD 7.2R or prior.
611		 */
612		if (sc->gif_options & GIF_ACCEPT_REVETHIP) {
613			if (eip->eip_resvl != ETHERIP_VERSION
614			    && eip->eip_ver != ETHERIP_VERSION) {
615				/* discard unknown versions */
616				m_freem(m);
617				return;
618			}
619		} else {
620			if (eip->eip_ver != ETHERIP_VERSION) {
621				/* discard unknown versions */
622				m_freem(m);
623				return;
624			}
625		}
626		m_adj(m, sizeof(struct etherip_header));
627
628		m->m_flags &= ~(M_BCAST|M_MCAST);
629		m->m_pkthdr.rcvif = ifp;
630
631		if (ifp->if_bridge) {
632			oldifp = ifp;
633			eh = mtod(m, struct ether_header *);
634			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
635				if (ETHER_IS_BROADCAST(eh->ether_dhost))
636					m->m_flags |= M_BCAST;
637				else
638					m->m_flags |= M_MCAST;
639				ifp->if_imcasts++;
640			}
641			BRIDGE_INPUT(ifp, m);
642
643			if (m != NULL && ifp != oldifp) {
644				/*
645				 * The bridge gave us back itself or one of the
646				 * members for which the frame is addressed.
647				 */
648				ether_demux(ifp, m);
649				return;
650			}
651		}
652		if (m != NULL)
653			m_freem(m);
654		return;
655
656	default:
657		if (ng_gif_input_orphan_p != NULL)
658			(*ng_gif_input_orphan_p)(ifp, m, af);
659		else
660			m_freem(m);
661		return;
662	}
663
664	ifp->if_ipackets++;
665	ifp->if_ibytes += m->m_pkthdr.len;
666	M_SETFIB(m, ifp->if_fib);
667	netisr_dispatch(isr, m);
668}
669
670/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
671int
672gif_ioctl(ifp, cmd, data)
673	struct ifnet *ifp;
674	u_long cmd;
675	caddr_t data;
676{
677	struct gif_softc *sc  = ifp->if_softc;
678	struct ifreq     *ifr = (struct ifreq*)data;
679	int error = 0, size;
680	u_int	options;
681	struct sockaddr *dst, *src;
682#ifdef	SIOCSIFMTU /* xxx */
683	u_long mtu;
684#endif
685
686	switch (cmd) {
687	case SIOCSIFADDR:
688		ifp->if_flags |= IFF_UP;
689		break;
690
691	case SIOCADDMULTI:
692	case SIOCDELMULTI:
693		break;
694
695#ifdef	SIOCSIFMTU /* xxx */
696	case SIOCGIFMTU:
697		break;
698
699	case SIOCSIFMTU:
700		mtu = ifr->ifr_mtu;
701		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
702			return (EINVAL);
703		ifp->if_mtu = mtu;
704		break;
705#endif /* SIOCSIFMTU */
706
707#ifdef INET
708	case SIOCSIFPHYADDR:
709#endif
710#ifdef INET6
711	case SIOCSIFPHYADDR_IN6:
712#endif /* INET6 */
713	case SIOCSLIFPHYADDR:
714		switch (cmd) {
715#ifdef INET
716		case SIOCSIFPHYADDR:
717			src = (struct sockaddr *)
718				&(((struct in_aliasreq *)data)->ifra_addr);
719			dst = (struct sockaddr *)
720				&(((struct in_aliasreq *)data)->ifra_dstaddr);
721			break;
722#endif
723#ifdef INET6
724		case SIOCSIFPHYADDR_IN6:
725			src = (struct sockaddr *)
726				&(((struct in6_aliasreq *)data)->ifra_addr);
727			dst = (struct sockaddr *)
728				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
729			break;
730#endif
731		case SIOCSLIFPHYADDR:
732			src = (struct sockaddr *)
733				&(((struct if_laddrreq *)data)->addr);
734			dst = (struct sockaddr *)
735				&(((struct if_laddrreq *)data)->dstaddr);
736			break;
737		default:
738			return EINVAL;
739		}
740
741		/* sa_family must be equal */
742		if (src->sa_family != dst->sa_family)
743			return EINVAL;
744
745		/* validate sa_len */
746		switch (src->sa_family) {
747#ifdef INET
748		case AF_INET:
749			if (src->sa_len != sizeof(struct sockaddr_in))
750				return EINVAL;
751			break;
752#endif
753#ifdef INET6
754		case AF_INET6:
755			if (src->sa_len != sizeof(struct sockaddr_in6))
756				return EINVAL;
757			break;
758#endif
759		default:
760			return EAFNOSUPPORT;
761		}
762		switch (dst->sa_family) {
763#ifdef INET
764		case AF_INET:
765			if (dst->sa_len != sizeof(struct sockaddr_in))
766				return EINVAL;
767			break;
768#endif
769#ifdef INET6
770		case AF_INET6:
771			if (dst->sa_len != sizeof(struct sockaddr_in6))
772				return EINVAL;
773			break;
774#endif
775		default:
776			return EAFNOSUPPORT;
777		}
778
779		/* check sa_family looks sane for the cmd */
780		switch (cmd) {
781		case SIOCSIFPHYADDR:
782			if (src->sa_family == AF_INET)
783				break;
784			return EAFNOSUPPORT;
785#ifdef INET6
786		case SIOCSIFPHYADDR_IN6:
787			if (src->sa_family == AF_INET6)
788				break;
789			return EAFNOSUPPORT;
790#endif /* INET6 */
791		case SIOCSLIFPHYADDR:
792			/* checks done in the above */
793			break;
794		}
795
796		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
797		break;
798
799#ifdef SIOCDIFPHYADDR
800	case SIOCDIFPHYADDR:
801		gif_delete_tunnel(GIF2IFP(sc));
802		break;
803#endif
804
805	case SIOCGIFPSRCADDR:
806#ifdef INET6
807	case SIOCGIFPSRCADDR_IN6:
808#endif /* INET6 */
809		if (sc->gif_psrc == NULL) {
810			error = EADDRNOTAVAIL;
811			goto bad;
812		}
813		src = sc->gif_psrc;
814		switch (cmd) {
815#ifdef INET
816		case SIOCGIFPSRCADDR:
817			dst = &ifr->ifr_addr;
818			size = sizeof(ifr->ifr_addr);
819			break;
820#endif /* INET */
821#ifdef INET6
822		case SIOCGIFPSRCADDR_IN6:
823			dst = (struct sockaddr *)
824				&(((struct in6_ifreq *)data)->ifr_addr);
825			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
826			break;
827#endif /* INET6 */
828		default:
829			error = EADDRNOTAVAIL;
830			goto bad;
831		}
832		if (src->sa_len > size)
833			return EINVAL;
834		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
835#ifdef INET6
836		if (dst->sa_family == AF_INET6) {
837			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
838			if (error != 0)
839				return (error);
840		}
841#endif
842		break;
843
844	case SIOCGIFPDSTADDR:
845#ifdef INET6
846	case SIOCGIFPDSTADDR_IN6:
847#endif /* INET6 */
848		if (sc->gif_pdst == NULL) {
849			error = EADDRNOTAVAIL;
850			goto bad;
851		}
852		src = sc->gif_pdst;
853		switch (cmd) {
854#ifdef INET
855		case SIOCGIFPDSTADDR:
856			dst = &ifr->ifr_addr;
857			size = sizeof(ifr->ifr_addr);
858			break;
859#endif /* INET */
860#ifdef INET6
861		case SIOCGIFPDSTADDR_IN6:
862			dst = (struct sockaddr *)
863				&(((struct in6_ifreq *)data)->ifr_addr);
864			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
865			break;
866#endif /* INET6 */
867		default:
868			error = EADDRNOTAVAIL;
869			goto bad;
870		}
871		if (src->sa_len > size)
872			return EINVAL;
873		error = prison_if(curthread->td_ucred, src);
874		if (error != 0)
875			return (error);
876		error = prison_if(curthread->td_ucred, dst);
877		if (error != 0)
878			return (error);
879		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
880#ifdef INET6
881		if (dst->sa_family == AF_INET6) {
882			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
883			if (error != 0)
884				return (error);
885		}
886#endif
887		break;
888
889	case SIOCGLIFPHYADDR:
890		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
891			error = EADDRNOTAVAIL;
892			goto bad;
893		}
894
895		/* copy src */
896		src = sc->gif_psrc;
897		dst = (struct sockaddr *)
898			&(((struct if_laddrreq *)data)->addr);
899		size = sizeof(((struct if_laddrreq *)data)->addr);
900		if (src->sa_len > size)
901			return EINVAL;
902		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
903
904		/* copy dst */
905		src = sc->gif_pdst;
906		dst = (struct sockaddr *)
907			&(((struct if_laddrreq *)data)->dstaddr);
908		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
909		if (src->sa_len > size)
910			return EINVAL;
911		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
912		break;
913
914	case SIOCSIFFLAGS:
915		/* if_ioctl() takes care of it */
916		break;
917
918	case GIFGOPTS:
919		options = sc->gif_options;
920		error = copyout(&options, ifr->ifr_data,
921				sizeof(options));
922		break;
923
924	case GIFSOPTS:
925		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
926			break;
927		error = copyin(ifr->ifr_data, &options, sizeof(options));
928		if (error)
929			break;
930		if (options & ~GIF_OPTMASK)
931			error = EINVAL;
932		else
933			sc->gif_options = options;
934		break;
935
936	default:
937		error = EINVAL;
938		break;
939	}
940 bad:
941	return error;
942}
943
944/*
945 * XXXRW: There's a general event-ordering issue here: the code to check
946 * if a given tunnel is already present happens before we perform a
947 * potentially blocking setup of the tunnel.  This code needs to be
948 * re-ordered so that the check and replacement can be atomic using
949 * a mutex.
950 */
951int
952gif_set_tunnel(ifp, src, dst)
953	struct ifnet *ifp;
954	struct sockaddr *src;
955	struct sockaddr *dst;
956{
957	struct gif_softc *sc = ifp->if_softc;
958	struct gif_softc *sc2;
959	struct sockaddr *osrc, *odst, *sa;
960	int error = 0;
961
962	mtx_lock(&gif_mtx);
963	LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) {
964		if (sc2 == sc)
965			continue;
966		if (!sc2->gif_pdst || !sc2->gif_psrc)
967			continue;
968		if (sc2->gif_pdst->sa_family != dst->sa_family ||
969		    sc2->gif_pdst->sa_len != dst->sa_len ||
970		    sc2->gif_psrc->sa_family != src->sa_family ||
971		    sc2->gif_psrc->sa_len != src->sa_len)
972			continue;
973
974		/*
975		 * Disallow parallel tunnels unless instructed
976		 * otherwise.
977		 */
978		if (!V_parallel_tunnels &&
979		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
980		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
981			error = EADDRNOTAVAIL;
982			mtx_unlock(&gif_mtx);
983			goto bad;
984		}
985
986		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
987	}
988	mtx_unlock(&gif_mtx);
989
990	/* XXX we can detach from both, but be polite just in case */
991	if (sc->gif_psrc)
992		switch (sc->gif_psrc->sa_family) {
993#ifdef INET
994		case AF_INET:
995			(void)in_gif_detach(sc);
996			break;
997#endif
998#ifdef INET6
999		case AF_INET6:
1000			(void)in6_gif_detach(sc);
1001			break;
1002#endif
1003		}
1004
1005	osrc = sc->gif_psrc;
1006	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
1007	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
1008	sc->gif_psrc = sa;
1009
1010	odst = sc->gif_pdst;
1011	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
1012	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
1013	sc->gif_pdst = sa;
1014
1015	switch (sc->gif_psrc->sa_family) {
1016#ifdef INET
1017	case AF_INET:
1018		error = in_gif_attach(sc);
1019		break;
1020#endif
1021#ifdef INET6
1022	case AF_INET6:
1023		/*
1024		 * Check validity of the scope zone ID of the addresses, and
1025		 * convert it into the kernel internal form if necessary.
1026		 */
1027		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
1028		if (error != 0)
1029			break;
1030		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
1031		if (error != 0)
1032			break;
1033		error = in6_gif_attach(sc);
1034		break;
1035#endif
1036	}
1037	if (error) {
1038		/* rollback */
1039		free((caddr_t)sc->gif_psrc, M_IFADDR);
1040		free((caddr_t)sc->gif_pdst, M_IFADDR);
1041		sc->gif_psrc = osrc;
1042		sc->gif_pdst = odst;
1043		goto bad;
1044	}
1045
1046	if (osrc)
1047		free((caddr_t)osrc, M_IFADDR);
1048	if (odst)
1049		free((caddr_t)odst, M_IFADDR);
1050
1051 bad:
1052	if (sc->gif_psrc && sc->gif_pdst)
1053		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1054	else
1055		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1056
1057	return error;
1058}
1059
1060void
1061gif_delete_tunnel(ifp)
1062	struct ifnet *ifp;
1063{
1064	struct gif_softc *sc = ifp->if_softc;
1065
1066	if (sc->gif_psrc) {
1067		free((caddr_t)sc->gif_psrc, M_IFADDR);
1068		sc->gif_psrc = NULL;
1069	}
1070	if (sc->gif_pdst) {
1071		free((caddr_t)sc->gif_pdst, M_IFADDR);
1072		sc->gif_pdst = NULL;
1073	}
1074	/* it is safe to detach from both */
1075#ifdef INET
1076	(void)in_gif_detach(sc);
1077#endif
1078#ifdef INET6
1079	(void)in6_gif_detach(sc);
1080#endif
1081	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1082}
1083