if_gif.c revision 257943
1/*	$FreeBSD: head/sys/net/if_gif.c 257943 2013-11-11 05:39:42Z glebius $	*/
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_var.h>
57#include <net/if_clone.h>
58#include <net/if_types.h>
59#include <net/netisr.h>
60#include <net/route.h>
61#include <net/bpf.h>
62#include <net/vnet.h>
63
64#include <netinet/in.h>
65#include <netinet/in_systm.h>
66#include <netinet/ip.h>
67#ifdef	INET
68#include <netinet/in_var.h>
69#include <netinet/in_gif.h>
70#include <netinet/ip_var.h>
71#endif	/* INET */
72
73#ifdef INET6
74#ifndef INET
75#include <netinet/in.h>
76#endif
77#include <netinet6/in6_var.h>
78#include <netinet/ip6.h>
79#include <netinet6/ip6_var.h>
80#include <netinet6/scope6_var.h>
81#include <netinet6/in6_gif.h>
82#include <netinet6/ip6protosw.h>
83#endif /* INET6 */
84
85#include <netinet/ip_encap.h>
86#include <net/ethernet.h>
87#include <net/if_bridgevar.h>
88#include <net/if_gif.h>
89
90#include <security/mac/mac_framework.h>
91
92static const char gifname[] = "gif";
93
94/*
95 * gif_mtx protects the global gif_softc_list.
96 */
97static struct mtx gif_mtx;
98static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
99static VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list);
100#define	V_gif_softc_list	VNET(gif_softc_list)
101
102void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
103void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
104void	(*ng_gif_attach_p)(struct ifnet *ifp);
105void	(*ng_gif_detach_p)(struct ifnet *ifp);
106
107static void	gif_start(struct ifnet *);
108static int	gif_clone_create(struct if_clone *, int, caddr_t);
109static void	gif_clone_destroy(struct ifnet *);
110static struct if_clone *gif_cloner;
111
112static int gifmodevent(module_t, int, void *);
113
114SYSCTL_DECL(_net_link);
115static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
116    "Generic Tunnel Interface");
117#ifndef MAX_GIF_NEST
118/*
119 * This macro controls the default upper limitation on nesting of gif tunnels.
120 * Since, setting a large value to this macro with a careless configuration
121 * may introduce system crash, we don't allow any nestings by default.
122 * If you need to configure nested gif tunnels, you can define this macro
123 * in your kernel configuration file.  However, if you do so, please be
124 * careful to configure the tunnels so that it won't make a loop.
125 */
126#define MAX_GIF_NEST 1
127#endif
128static VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST;
129#define	V_max_gif_nesting	VNET(max_gif_nesting)
130SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
131    &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels");
132
133/*
134 * By default, we disallow creation of multiple tunnels between the same
135 * pair of addresses.  Some applications require this functionality so
136 * we allow control over this check here.
137 */
138#ifdef XBONEHACK
139static VNET_DEFINE(int, parallel_tunnels) = 1;
140#else
141static VNET_DEFINE(int, parallel_tunnels) = 0;
142#endif
143#define	V_parallel_tunnels	VNET(parallel_tunnels)
144SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
145    &VNET_NAME(parallel_tunnels), 0, "Allow parallel tunnels?");
146
147/* copy from src/sys/net/if_ethersubr.c */
148static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
149			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
150#ifndef ETHER_IS_BROADCAST
151#define ETHER_IS_BROADCAST(addr) \
152	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
153#endif
154
155static int
156gif_clone_create(ifc, unit, params)
157	struct if_clone *ifc;
158	int unit;
159	caddr_t params;
160{
161	struct gif_softc *sc;
162
163	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
164	sc->gif_fibnum = curthread->td_proc->p_fibnum;
165	GIF2IFP(sc) = if_alloc(IFT_GIF);
166	if (GIF2IFP(sc) == NULL) {
167		free(sc, M_GIF);
168		return (ENOSPC);
169	}
170
171	GIF_LOCK_INIT(sc);
172
173	GIF2IFP(sc)->if_softc = sc;
174	if_initname(GIF2IFP(sc), gifname, unit);
175
176	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
177	sc->gif_options = 0;
178
179	GIF2IFP(sc)->if_addrlen = 0;
180	GIF2IFP(sc)->if_mtu    = GIF_MTU;
181	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
182#if 0
183	/* turn off ingress filter */
184	GIF2IFP(sc)->if_flags  |= IFF_LINK2;
185#endif
186	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
187	GIF2IFP(sc)->if_start  = gif_start;
188	GIF2IFP(sc)->if_output = gif_output;
189	GIF2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen;
190	if_attach(GIF2IFP(sc));
191	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
192	if (ng_gif_attach_p != NULL)
193		(*ng_gif_attach_p)(GIF2IFP(sc));
194
195	mtx_lock(&gif_mtx);
196	LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list);
197	mtx_unlock(&gif_mtx);
198
199	return (0);
200}
201
202static void
203gif_clone_destroy(ifp)
204	struct ifnet *ifp;
205{
206#if defined(INET) || defined(INET6)
207	int err;
208#endif
209	struct gif_softc *sc = ifp->if_softc;
210
211	mtx_lock(&gif_mtx);
212	LIST_REMOVE(sc, gif_list);
213	mtx_unlock(&gif_mtx);
214
215	gif_delete_tunnel(ifp);
216#ifdef INET6
217	if (sc->encap_cookie6 != NULL) {
218		err = encap_detach(sc->encap_cookie6);
219		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
220	}
221#endif
222#ifdef INET
223	if (sc->encap_cookie4 != NULL) {
224		err = encap_detach(sc->encap_cookie4);
225		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
226	}
227#endif
228
229	if (ng_gif_detach_p != NULL)
230		(*ng_gif_detach_p)(ifp);
231	bpfdetach(ifp);
232	if_detach(ifp);
233	if_free(ifp);
234
235	GIF_LOCK_DESTROY(sc);
236
237	free(sc, M_GIF);
238}
239
240static void
241vnet_gif_init(const void *unused __unused)
242{
243
244	LIST_INIT(&V_gif_softc_list);
245}
246VNET_SYSINIT(vnet_gif_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, vnet_gif_init,
247    NULL);
248
249static int
250gifmodevent(mod, type, data)
251	module_t mod;
252	int type;
253	void *data;
254{
255
256	switch (type) {
257	case MOD_LOAD:
258		mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
259		gif_cloner = if_clone_simple(gifname, gif_clone_create,
260		    gif_clone_destroy, 0);
261		break;
262
263	case MOD_UNLOAD:
264		if_clone_detach(gif_cloner);
265		mtx_destroy(&gif_mtx);
266		break;
267	default:
268		return EOPNOTSUPP;
269	}
270	return 0;
271}
272
273static moduledata_t gif_mod = {
274	"if_gif",
275	gifmodevent,
276	0
277};
278
279DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
280MODULE_VERSION(if_gif, 1);
281
282int
283gif_encapcheck(m, off, proto, arg)
284	const struct mbuf *m;
285	int off;
286	int proto;
287	void *arg;
288{
289	struct ip ip;
290	struct gif_softc *sc;
291
292	sc = (struct gif_softc *)arg;
293	if (sc == NULL)
294		return 0;
295
296	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
297		return 0;
298
299	/* no physical address */
300	if (!sc->gif_psrc || !sc->gif_pdst)
301		return 0;
302
303	switch (proto) {
304#ifdef INET
305	case IPPROTO_IPV4:
306		break;
307#endif
308#ifdef INET6
309	case IPPROTO_IPV6:
310		break;
311#endif
312	case IPPROTO_ETHERIP:
313		break;
314
315	default:
316		return 0;
317	}
318
319	/* Bail on short packets */
320	if (m->m_pkthdr.len < sizeof(ip))
321		return 0;
322
323	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
324
325	switch (ip.ip_v) {
326#ifdef INET
327	case 4:
328		if (sc->gif_psrc->sa_family != AF_INET ||
329		    sc->gif_pdst->sa_family != AF_INET)
330			return 0;
331		return gif_encapcheck4(m, off, proto, arg);
332#endif
333#ifdef INET6
334	case 6:
335		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
336			return 0;
337		if (sc->gif_psrc->sa_family != AF_INET6 ||
338		    sc->gif_pdst->sa_family != AF_INET6)
339			return 0;
340		return gif_encapcheck6(m, off, proto, arg);
341#endif
342	default:
343		return 0;
344	}
345}
346#ifdef INET
347#define GIF_HDR_LEN (ETHER_HDR_LEN + sizeof (struct ip))
348#endif
349#ifdef INET6
350#define GIF_HDR_LEN6 (ETHER_HDR_LEN + sizeof (struct ip6_hdr))
351#endif
352
353static void
354gif_start(struct ifnet *ifp)
355{
356	struct gif_softc *sc;
357	struct mbuf *m;
358	uint32_t af;
359	int error = 0;
360
361	sc = ifp->if_softc;
362	GIF_LOCK(sc);
363	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
364	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
365
366		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
367		if (m == 0)
368			break;
369
370#ifdef ALTQ
371		/* Take out those altq bytes we add in gif_output  */
372#ifdef INET
373		if (sc->gif_psrc->sa_family == AF_INET)
374			m->m_pkthdr.len -= GIF_HDR_LEN;
375#endif
376#ifdef INET6
377		if (sc->gif_psrc->sa_family == AF_INET6)
378		    m->m_pkthdr.len -= GIF_HDR_LEN6;
379#endif
380#endif
381		/*
382		 * Now pull back the af that we
383		 * stashed in the csum_data.
384		 */
385		af = m->m_pkthdr.csum_data;
386
387		if (ifp->if_bridge)
388			af = AF_LINK;
389
390		BPF_MTAP2(ifp, &af, sizeof(af), m);
391		ifp->if_opackets++;
392
393/*              Done by IFQ_HANDOFF */
394/* 		ifp->if_obytes += m->m_pkthdr.len;*/
395		/* override to IPPROTO_ETHERIP for bridged traffic */
396
397		M_SETFIB(m, sc->gif_fibnum);
398		/* inner AF-specific encapsulation */
399		/* XXX should we check if our outer source is legal? */
400		/* dispatch to output logic based on outer AF */
401		switch (sc->gif_psrc->sa_family) {
402#ifdef INET
403		case AF_INET:
404			error = in_gif_output(ifp, af, m);
405			break;
406#endif
407#ifdef INET6
408		case AF_INET6:
409			error = in6_gif_output(ifp, af, m);
410			break;
411#endif
412		default:
413			m_freem(m);
414			error = ENETDOWN;
415		}
416		if (error)
417			ifp->if_oerrors++;
418
419	}
420	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
421	GIF_UNLOCK(sc);
422	return;
423}
424
425int
426gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
427	struct route *ro)
428{
429	struct gif_softc *sc = ifp->if_softc;
430	struct m_tag *mtag;
431	int error = 0;
432	int gif_called;
433	uint32_t af;
434#ifdef MAC
435	error = mac_ifnet_check_transmit(ifp, m);
436	if (error) {
437		m_freem(m);
438		goto end;
439	}
440#endif
441	if ((ifp->if_flags & IFF_MONITOR) != 0) {
442		error = ENETDOWN;
443		m_freem(m);
444		goto end;
445	}
446
447	/*
448	 * gif may cause infinite recursion calls when misconfigured.
449	 * We'll prevent this by detecting loops.
450	 *
451	 * High nesting level may cause stack exhaustion.
452	 * We'll prevent this by introducing upper limit.
453	 */
454	gif_called = 1;
455	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
456	while (mtag != NULL) {
457		if (*(struct ifnet **)(mtag + 1) == ifp) {
458			log(LOG_NOTICE,
459			    "gif_output: loop detected on %s\n",
460			    (*(struct ifnet **)(mtag + 1))->if_xname);
461			m_freem(m);
462			error = EIO;	/* is there better errno? */
463			goto end;
464		}
465		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
466		gif_called++;
467	}
468	if (gif_called > V_max_gif_nesting) {
469		log(LOG_NOTICE,
470		    "gif_output: recursively called too many times(%d)\n",
471		    gif_called);
472		m_freem(m);
473		error = EIO;	/* is there better errno? */
474		goto end;
475	}
476	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
477	    M_NOWAIT);
478	if (mtag == NULL) {
479		m_freem(m);
480		error = ENOMEM;
481		goto end;
482	}
483	*(struct ifnet **)(mtag + 1) = ifp;
484	m_tag_prepend(m, mtag);
485
486	m->m_flags &= ~(M_BCAST|M_MCAST);
487	/* BPF writes need to be handled specially. */
488	if (dst->sa_family == AF_UNSPEC)
489		bcopy(dst->sa_data, &af, sizeof(af));
490	else
491		af = dst->sa_family;
492	/*
493	 * Now save the af in the inbound pkt csum
494	 * data, this is a cheat since we are using
495	 * the inbound csum_data field to carry the
496	 * af over to the gif_start() routine, avoiding
497	 * using yet another mtag.
498	 */
499	m->m_pkthdr.csum_data = af;
500	if (!(ifp->if_flags & IFF_UP) ||
501	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
502		m_freem(m);
503		error = ENETDOWN;
504		goto end;
505	}
506#ifdef ALTQ
507	/*
508	 * Make altq aware of the bytes we will add
509	 * when we actually send it.
510	 */
511#ifdef INET
512	if (sc->gif_psrc->sa_family == AF_INET)
513		m->m_pkthdr.len += GIF_HDR_LEN;
514#endif
515#ifdef INET6
516	if (sc->gif_psrc->sa_family == AF_INET6)
517		m->m_pkthdr.len += GIF_HDR_LEN6;
518#endif
519#endif
520	/*
521	 * Queue message on interface, update output statistics if
522	 * successful, and start output if interface not yet active.
523	 */
524	IFQ_HANDOFF(ifp, m, error);
525  end:
526	if (error)
527		ifp->if_oerrors++;
528	return (error);
529}
530
531void
532gif_input(m, af, ifp)
533	struct mbuf *m;
534	int af;
535	struct ifnet *ifp;
536{
537	int isr, n;
538	struct gif_softc *sc;
539	struct etherip_header *eip;
540	struct ether_header *eh;
541	struct ifnet *oldifp;
542
543	if (ifp == NULL) {
544		/* just in case */
545		m_freem(m);
546		return;
547	}
548	sc = ifp->if_softc;
549	m->m_pkthdr.rcvif = ifp;
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		switch (cmd) {
714#ifdef INET
715		case SIOCSIFPHYADDR:
716			src = (struct sockaddr *)
717				&(((struct in_aliasreq *)data)->ifra_addr);
718			dst = (struct sockaddr *)
719				&(((struct in_aliasreq *)data)->ifra_dstaddr);
720			break;
721#endif
722#ifdef INET6
723		case SIOCSIFPHYADDR_IN6:
724			src = (struct sockaddr *)
725				&(((struct in6_aliasreq *)data)->ifra_addr);
726			dst = (struct sockaddr *)
727				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
728			break;
729#endif
730		default:
731			return EINVAL;
732		}
733
734		/* sa_family must be equal */
735		if (src->sa_family != dst->sa_family)
736			return EINVAL;
737
738		/* validate sa_len */
739		switch (src->sa_family) {
740#ifdef INET
741		case AF_INET:
742			if (src->sa_len != sizeof(struct sockaddr_in))
743				return EINVAL;
744			break;
745#endif
746#ifdef INET6
747		case AF_INET6:
748			if (src->sa_len != sizeof(struct sockaddr_in6))
749				return EINVAL;
750			break;
751#endif
752		default:
753			return EAFNOSUPPORT;
754		}
755		switch (dst->sa_family) {
756#ifdef INET
757		case AF_INET:
758			if (dst->sa_len != sizeof(struct sockaddr_in))
759				return EINVAL;
760			break;
761#endif
762#ifdef INET6
763		case AF_INET6:
764			if (dst->sa_len != sizeof(struct sockaddr_in6))
765				return EINVAL;
766			break;
767#endif
768		default:
769			return EAFNOSUPPORT;
770		}
771
772		/* check sa_family looks sane for the cmd */
773		switch (cmd) {
774		case SIOCSIFPHYADDR:
775			if (src->sa_family == AF_INET)
776				break;
777			return EAFNOSUPPORT;
778#ifdef INET6
779		case SIOCSIFPHYADDR_IN6:
780			if (src->sa_family == AF_INET6)
781				break;
782			return EAFNOSUPPORT;
783#endif /* INET6 */
784		}
785
786		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
787		break;
788
789#ifdef SIOCDIFPHYADDR
790	case SIOCDIFPHYADDR:
791		gif_delete_tunnel(GIF2IFP(sc));
792		break;
793#endif
794
795	case SIOCGIFPSRCADDR:
796#ifdef INET6
797	case SIOCGIFPSRCADDR_IN6:
798#endif /* INET6 */
799		if (sc->gif_psrc == NULL) {
800			error = EADDRNOTAVAIL;
801			goto bad;
802		}
803		src = sc->gif_psrc;
804		switch (cmd) {
805#ifdef INET
806		case SIOCGIFPSRCADDR:
807			dst = &ifr->ifr_addr;
808			size = sizeof(ifr->ifr_addr);
809			break;
810#endif /* INET */
811#ifdef INET6
812		case SIOCGIFPSRCADDR_IN6:
813			dst = (struct sockaddr *)
814				&(((struct in6_ifreq *)data)->ifr_addr);
815			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
816			break;
817#endif /* INET6 */
818		default:
819			error = EADDRNOTAVAIL;
820			goto bad;
821		}
822		if (src->sa_len > size)
823			return EINVAL;
824		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
825#ifdef INET6
826		if (dst->sa_family == AF_INET6) {
827			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
828			if (error != 0)
829				return (error);
830		}
831#endif
832		break;
833
834	case SIOCGIFPDSTADDR:
835#ifdef INET6
836	case SIOCGIFPDSTADDR_IN6:
837#endif /* INET6 */
838		if (sc->gif_pdst == NULL) {
839			error = EADDRNOTAVAIL;
840			goto bad;
841		}
842		src = sc->gif_pdst;
843		switch (cmd) {
844#ifdef INET
845		case SIOCGIFPDSTADDR:
846			dst = &ifr->ifr_addr;
847			size = sizeof(ifr->ifr_addr);
848			break;
849#endif /* INET */
850#ifdef INET6
851		case SIOCGIFPDSTADDR_IN6:
852			dst = (struct sockaddr *)
853				&(((struct in6_ifreq *)data)->ifr_addr);
854			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
855			break;
856#endif /* INET6 */
857		default:
858			error = EADDRNOTAVAIL;
859			goto bad;
860		}
861		if (src->sa_len > size)
862			return EINVAL;
863		error = prison_if(curthread->td_ucred, src);
864		if (error != 0)
865			return (error);
866		error = prison_if(curthread->td_ucred, dst);
867		if (error != 0)
868			return (error);
869		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
870#ifdef INET6
871		if (dst->sa_family == AF_INET6) {
872			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
873			if (error != 0)
874				return (error);
875		}
876#endif
877		break;
878
879	case SIOCSIFFLAGS:
880		/* if_ioctl() takes care of it */
881		break;
882
883	case GIFGOPTS:
884		options = sc->gif_options;
885		error = copyout(&options, ifr->ifr_data,
886				sizeof(options));
887		break;
888
889	case GIFSOPTS:
890		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
891			break;
892		error = copyin(ifr->ifr_data, &options, sizeof(options));
893		if (error)
894			break;
895		if (options & ~GIF_OPTMASK)
896			error = EINVAL;
897		else
898			sc->gif_options = options;
899		break;
900
901	default:
902		error = EINVAL;
903		break;
904	}
905 bad:
906	return error;
907}
908
909/*
910 * XXXRW: There's a general event-ordering issue here: the code to check
911 * if a given tunnel is already present happens before we perform a
912 * potentially blocking setup of the tunnel.  This code needs to be
913 * re-ordered so that the check and replacement can be atomic using
914 * a mutex.
915 */
916int
917gif_set_tunnel(ifp, src, dst)
918	struct ifnet *ifp;
919	struct sockaddr *src;
920	struct sockaddr *dst;
921{
922	struct gif_softc *sc = ifp->if_softc;
923	struct gif_softc *sc2;
924	struct sockaddr *osrc, *odst, *sa;
925	int error = 0;
926
927	mtx_lock(&gif_mtx);
928	LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) {
929		if (sc2 == sc)
930			continue;
931		if (!sc2->gif_pdst || !sc2->gif_psrc)
932			continue;
933		if (sc2->gif_pdst->sa_family != dst->sa_family ||
934		    sc2->gif_pdst->sa_len != dst->sa_len ||
935		    sc2->gif_psrc->sa_family != src->sa_family ||
936		    sc2->gif_psrc->sa_len != src->sa_len)
937			continue;
938
939		/*
940		 * Disallow parallel tunnels unless instructed
941		 * otherwise.
942		 */
943		if (!V_parallel_tunnels &&
944		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
945		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
946			error = EADDRNOTAVAIL;
947			mtx_unlock(&gif_mtx);
948			goto bad;
949		}
950
951		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
952	}
953	mtx_unlock(&gif_mtx);
954
955	/* XXX we can detach from both, but be polite just in case */
956	if (sc->gif_psrc)
957		switch (sc->gif_psrc->sa_family) {
958#ifdef INET
959		case AF_INET:
960			(void)in_gif_detach(sc);
961			break;
962#endif
963#ifdef INET6
964		case AF_INET6:
965			(void)in6_gif_detach(sc);
966			break;
967#endif
968		}
969
970	osrc = sc->gif_psrc;
971	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
972	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
973	sc->gif_psrc = sa;
974
975	odst = sc->gif_pdst;
976	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
977	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
978	sc->gif_pdst = sa;
979
980	switch (sc->gif_psrc->sa_family) {
981#ifdef INET
982	case AF_INET:
983		error = in_gif_attach(sc);
984		break;
985#endif
986#ifdef INET6
987	case AF_INET6:
988		/*
989		 * Check validity of the scope zone ID of the addresses, and
990		 * convert it into the kernel internal form if necessary.
991		 */
992		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
993		if (error != 0)
994			break;
995		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
996		if (error != 0)
997			break;
998		error = in6_gif_attach(sc);
999		break;
1000#endif
1001	}
1002	if (error) {
1003		/* rollback */
1004		free((caddr_t)sc->gif_psrc, M_IFADDR);
1005		free((caddr_t)sc->gif_pdst, M_IFADDR);
1006		sc->gif_psrc = osrc;
1007		sc->gif_pdst = odst;
1008		goto bad;
1009	}
1010
1011	if (osrc)
1012		free((caddr_t)osrc, M_IFADDR);
1013	if (odst)
1014		free((caddr_t)odst, M_IFADDR);
1015
1016 bad:
1017	if (sc->gif_psrc && sc->gif_pdst)
1018		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1019	else
1020		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1021
1022	return error;
1023}
1024
1025void
1026gif_delete_tunnel(ifp)
1027	struct ifnet *ifp;
1028{
1029	struct gif_softc *sc = ifp->if_softc;
1030
1031	if (sc->gif_psrc) {
1032		free((caddr_t)sc->gif_psrc, M_IFADDR);
1033		sc->gif_psrc = NULL;
1034	}
1035	if (sc->gif_pdst) {
1036		free((caddr_t)sc->gif_pdst, M_IFADDR);
1037		sc->gif_pdst = NULL;
1038	}
1039	/* it is safe to detach from both */
1040#ifdef INET
1041	(void)in_gif_detach(sc);
1042#endif
1043#ifdef INET6
1044	(void)in6_gif_detach(sc);
1045#endif
1046	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1047}
1048