if_gif.c revision 236955
1/*	$FreeBSD: head/sys/net/if_gif.c 236955 2012-06-12 12:44:17Z rrs $	*/
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
91#define 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 *);
109
110IFC_SIMPLE_DECLARE(gif, 0);
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), ifc->ifc_name, unit);
175
176	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
177	sc->gif_options = GIF_ACCEPT_REVETHIP;
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		if_clone_attach(&gif_cloner);
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	if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
363		/* Already active */
364		ifp->if_drv_flags |= IFF_GIF_WANTED;
365		GIF_UNLOCK(sc);
366		return;
367	}
368	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
369	GIF_UNLOCK(sc);
370keep_going:
371	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
372
373		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
374		if (m == 0)
375			break;
376
377#ifdef ALTQ
378		/* Take out those altq bytes we add in gif_output  */
379#ifdef INET
380		if (sc->gif_psrc->sa_family == AF_INET)
381			m->m_pkthdr.len -= GIF_HDR_LEN;
382#endif
383#ifdef INET6
384		if (sc->gif_psrc->sa_family == AF_INET6)
385		    m->m_pkthdr.len -= GIF_HDR_LEN6;
386#endif
387#endif
388		/* Now pull back the af in packet that
389		 * was saved in the address location.
390		 */
391		af = m->m_pkthdr.csum_data;
392
393		if (ifp->if_bridge)
394			af = AF_LINK;
395
396		BPF_MTAP2(ifp, &af, sizeof(af), m);
397		ifp->if_opackets++;
398
399/*              Done by IFQ_HANDOFF */
400/* 		ifp->if_obytes += m->m_pkthdr.len;*/
401		/* override to IPPROTO_ETHERIP for bridged traffic */
402
403		M_SETFIB(m, sc->gif_fibnum);
404		/* inner AF-specific encapsulation */
405		/* XXX should we check if our outer source is legal? */
406		/* dispatch to output logic based on outer AF */
407		switch (sc->gif_psrc->sa_family) {
408#ifdef INET
409		case AF_INET:
410			error = in_gif_output(ifp, af, m);
411			break;
412#endif
413#ifdef INET6
414		case AF_INET6:
415			error = in6_gif_output(ifp, af, m);
416			break;
417#endif
418		default:
419			m_freem(m);
420			error = ENETDOWN;
421		}
422		if (error)
423			ifp->if_oerrors++;
424
425	}
426	GIF_LOCK(sc);
427	if (ifp->if_drv_flags & IFF_GIF_WANTED) {
428		/* Someone did a start while
429		 * we were unlocked and processing
430		 * lets clear the flag and try again.
431		 */
432		ifp->if_drv_flags &= ~IFF_GIF_WANTED;
433		GIF_UNLOCK(sc);
434		goto keep_going;
435	}
436	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
437	GIF_UNLOCK(sc);
438	return;
439}
440
441int
442gif_output(ifp, m, dst, ro)
443	struct ifnet *ifp;
444	struct mbuf *m;
445	struct sockaddr *dst;
446	struct route *ro;
447{
448	struct gif_softc *sc = ifp->if_softc;
449	struct m_tag *mtag;
450	int error = 0;
451	int gif_called;
452	uint32_t af;
453#ifdef MAC
454	error = mac_ifnet_check_transmit(ifp, m);
455	if (error) {
456		m_freem(m);
457		goto end;
458	}
459#endif
460
461	/*
462	 * gif may cause infinite recursion calls when misconfigured.
463	 * We'll prevent this by detecting loops.
464	 *
465	 * High nesting level may cause stack exhaustion.
466	 * We'll prevent this by introducing upper limit.
467	 */
468	gif_called = 1;
469	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
470	while (mtag != NULL) {
471		if (*(struct ifnet **)(mtag + 1) == ifp) {
472			log(LOG_NOTICE,
473			    "gif_output: loop detected on %s\n",
474			    (*(struct ifnet **)(mtag + 1))->if_xname);
475			m_freem(m);
476			error = EIO;	/* is there better errno? */
477			goto end;
478		}
479		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
480		gif_called++;
481	}
482	if (gif_called > V_max_gif_nesting) {
483		log(LOG_NOTICE,
484		    "gif_output: recursively called too many times(%d)\n",
485		    gif_called);
486		m_freem(m);
487		error = EIO;	/* is there better errno? */
488		goto end;
489	}
490	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
491	    M_NOWAIT);
492	if (mtag == NULL) {
493		m_freem(m);
494		error = ENOMEM;
495		goto end;
496	}
497	*(struct ifnet **)(mtag + 1) = ifp;
498	m_tag_prepend(m, mtag);
499
500	m->m_flags &= ~(M_BCAST|M_MCAST);
501	/* BPF writes need to be handled specially. */
502	if (dst->sa_family == AF_UNSPEC) {
503		bcopy(dst->sa_data, &af, sizeof(af));
504		dst->sa_family = af;
505	}
506	af = dst->sa_family;
507	/* Now save the af in the inbound pkt csum
508	 * data, this is a cheat since really
509	 * gif tunnels don't do offload.
510	 */
511	m->m_pkthdr.csum_data = af;
512	if (!(ifp->if_flags & IFF_UP) ||
513	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
514		m_freem(m);
515		error = ENETDOWN;
516		goto end;
517	}
518#ifdef ALTQ
519	/* Make altq aware of the bytes we will add
520	 * when we actually send it.
521	 */
522#ifdef INET
523	if (sc->gif_psrc->sa_family == AF_INET)
524		m->m_pkthdr.len += GIF_HDR_LEN;
525#endif
526#ifdef INET6
527	if (sc->gif_psrc->sa_family == AF_INET6)
528		m->m_pkthdr.len += GIF_HDR_LEN6;
529#endif
530#endif
531	/*
532	 * Queue message on interface, update output statistics if
533	 * successful, and start output if interface not yet active.
534	 */
535	IFQ_HANDOFF(ifp, m, error);
536  end:
537	if (error)
538		ifp->if_oerrors++;
539	return (error);
540}
541
542void
543gif_input(m, af, ifp)
544	struct mbuf *m;
545	int af;
546	struct ifnet *ifp;
547{
548	int isr, n;
549	struct gif_softc *sc;
550	struct etherip_header *eip;
551	struct ether_header *eh;
552	struct ifnet *oldifp;
553
554	if (ifp == NULL) {
555		/* just in case */
556		m_freem(m);
557		return;
558	}
559	sc = ifp->if_softc;
560	m->m_pkthdr.rcvif = ifp;
561
562#ifdef MAC
563	mac_ifnet_create_mbuf(ifp, m);
564#endif
565
566	if (bpf_peers_present(ifp->if_bpf)) {
567		u_int32_t af1 = af;
568		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
569	}
570
571	if (ng_gif_input_p != NULL) {
572		(*ng_gif_input_p)(ifp, &m, af);
573		if (m == NULL)
574			return;
575	}
576
577	/*
578	 * Put the packet to the network layer input queue according to the
579	 * specified address family.
580	 * Note: older versions of gif_input directly called network layer
581	 * input functions, e.g. ip6_input, here.  We changed the policy to
582	 * prevent too many recursive calls of such input functions, which
583	 * might cause kernel panic.  But the change may introduce another
584	 * problem; if the input queue is full, packets are discarded.
585	 * The kernel stack overflow really happened, and we believed
586	 * queue-full rarely occurs, so we changed the policy.
587	 */
588	switch (af) {
589#ifdef INET
590	case AF_INET:
591		isr = NETISR_IP;
592		break;
593#endif
594#ifdef INET6
595	case AF_INET6:
596		isr = NETISR_IPV6;
597		break;
598#endif
599	case AF_LINK:
600		n = sizeof(struct etherip_header) + sizeof(struct ether_header);
601		if (n > m->m_len) {
602			m = m_pullup(m, n);
603			if (m == NULL) {
604				ifp->if_ierrors++;
605				return;
606			}
607		}
608
609		eip = mtod(m, struct etherip_header *);
610		/*
611		 * GIF_ACCEPT_REVETHIP (enabled by default) intentionally
612		 * accepts an EtherIP packet with revered version field in
613		 * the header.  This is a knob for backward compatibility
614		 * with FreeBSD 7.2R or prior.
615		 */
616		if (sc->gif_options & GIF_ACCEPT_REVETHIP) {
617			if (eip->eip_resvl != ETHERIP_VERSION
618			    && eip->eip_ver != ETHERIP_VERSION) {
619				/* discard unknown versions */
620				m_freem(m);
621				return;
622			}
623		} else {
624			if (eip->eip_ver != ETHERIP_VERSION) {
625				/* discard unknown versions */
626				m_freem(m);
627				return;
628			}
629		}
630		m_adj(m, sizeof(struct etherip_header));
631
632		m->m_flags &= ~(M_BCAST|M_MCAST);
633		m->m_pkthdr.rcvif = ifp;
634
635		if (ifp->if_bridge) {
636			oldifp = ifp;
637			eh = mtod(m, struct ether_header *);
638			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
639				if (ETHER_IS_BROADCAST(eh->ether_dhost))
640					m->m_flags |= M_BCAST;
641				else
642					m->m_flags |= M_MCAST;
643				ifp->if_imcasts++;
644			}
645			BRIDGE_INPUT(ifp, m);
646
647			if (m != NULL && ifp != oldifp) {
648				/*
649				 * The bridge gave us back itself or one of the
650				 * members for which the frame is addressed.
651				 */
652				ether_demux(ifp, m);
653				return;
654			}
655		}
656		if (m != NULL)
657			m_freem(m);
658		return;
659
660	default:
661		if (ng_gif_input_orphan_p != NULL)
662			(*ng_gif_input_orphan_p)(ifp, m, af);
663		else
664			m_freem(m);
665		return;
666	}
667
668	ifp->if_ipackets++;
669	ifp->if_ibytes += m->m_pkthdr.len;
670	M_SETFIB(m, ifp->if_fib);
671	netisr_dispatch(isr, m);
672}
673
674/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
675int
676gif_ioctl(ifp, cmd, data)
677	struct ifnet *ifp;
678	u_long cmd;
679	caddr_t data;
680{
681	struct gif_softc *sc  = ifp->if_softc;
682	struct ifreq     *ifr = (struct ifreq*)data;
683	int error = 0, size;
684	u_int	options;
685	struct sockaddr *dst, *src;
686#ifdef	SIOCSIFMTU /* xxx */
687	u_long mtu;
688#endif
689
690	switch (cmd) {
691	case SIOCSIFADDR:
692		ifp->if_flags |= IFF_UP;
693		break;
694
695	case SIOCSIFDSTADDR:
696		break;
697
698	case SIOCADDMULTI:
699	case SIOCDELMULTI:
700		break;
701
702#ifdef	SIOCSIFMTU /* xxx */
703	case SIOCGIFMTU:
704		break;
705
706	case SIOCSIFMTU:
707		mtu = ifr->ifr_mtu;
708		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
709			return (EINVAL);
710		ifp->if_mtu = mtu;
711		break;
712#endif /* SIOCSIFMTU */
713
714#ifdef INET
715	case SIOCSIFPHYADDR:
716#endif
717#ifdef INET6
718	case SIOCSIFPHYADDR_IN6:
719#endif /* INET6 */
720	case SIOCSLIFPHYADDR:
721		switch (cmd) {
722#ifdef INET
723		case SIOCSIFPHYADDR:
724			src = (struct sockaddr *)
725				&(((struct in_aliasreq *)data)->ifra_addr);
726			dst = (struct sockaddr *)
727				&(((struct in_aliasreq *)data)->ifra_dstaddr);
728			break;
729#endif
730#ifdef INET6
731		case SIOCSIFPHYADDR_IN6:
732			src = (struct sockaddr *)
733				&(((struct in6_aliasreq *)data)->ifra_addr);
734			dst = (struct sockaddr *)
735				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
736			break;
737#endif
738		case SIOCSLIFPHYADDR:
739			src = (struct sockaddr *)
740				&(((struct if_laddrreq *)data)->addr);
741			dst = (struct sockaddr *)
742				&(((struct if_laddrreq *)data)->dstaddr);
743			break;
744		default:
745			return EINVAL;
746		}
747
748		/* sa_family must be equal */
749		if (src->sa_family != dst->sa_family)
750			return EINVAL;
751
752		/* validate sa_len */
753		switch (src->sa_family) {
754#ifdef INET
755		case AF_INET:
756			if (src->sa_len != sizeof(struct sockaddr_in))
757				return EINVAL;
758			break;
759#endif
760#ifdef INET6
761		case AF_INET6:
762			if (src->sa_len != sizeof(struct sockaddr_in6))
763				return EINVAL;
764			break;
765#endif
766		default:
767			return EAFNOSUPPORT;
768		}
769		switch (dst->sa_family) {
770#ifdef INET
771		case AF_INET:
772			if (dst->sa_len != sizeof(struct sockaddr_in))
773				return EINVAL;
774			break;
775#endif
776#ifdef INET6
777		case AF_INET6:
778			if (dst->sa_len != sizeof(struct sockaddr_in6))
779				return EINVAL;
780			break;
781#endif
782		default:
783			return EAFNOSUPPORT;
784		}
785
786		/* check sa_family looks sane for the cmd */
787		switch (cmd) {
788		case SIOCSIFPHYADDR:
789			if (src->sa_family == AF_INET)
790				break;
791			return EAFNOSUPPORT;
792#ifdef INET6
793		case SIOCSIFPHYADDR_IN6:
794			if (src->sa_family == AF_INET6)
795				break;
796			return EAFNOSUPPORT;
797#endif /* INET6 */
798		case SIOCSLIFPHYADDR:
799			/* checks done in the above */
800			break;
801		}
802
803		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
804		break;
805
806#ifdef SIOCDIFPHYADDR
807	case SIOCDIFPHYADDR:
808		gif_delete_tunnel(GIF2IFP(sc));
809		break;
810#endif
811
812	case SIOCGIFPSRCADDR:
813#ifdef INET6
814	case SIOCGIFPSRCADDR_IN6:
815#endif /* INET6 */
816		if (sc->gif_psrc == NULL) {
817			error = EADDRNOTAVAIL;
818			goto bad;
819		}
820		src = sc->gif_psrc;
821		switch (cmd) {
822#ifdef INET
823		case SIOCGIFPSRCADDR:
824			dst = &ifr->ifr_addr;
825			size = sizeof(ifr->ifr_addr);
826			break;
827#endif /* INET */
828#ifdef INET6
829		case SIOCGIFPSRCADDR_IN6:
830			dst = (struct sockaddr *)
831				&(((struct in6_ifreq *)data)->ifr_addr);
832			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
833			break;
834#endif /* INET6 */
835		default:
836			error = EADDRNOTAVAIL;
837			goto bad;
838		}
839		if (src->sa_len > size)
840			return EINVAL;
841		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
842#ifdef INET6
843		if (dst->sa_family == AF_INET6) {
844			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
845			if (error != 0)
846				return (error);
847		}
848#endif
849		break;
850
851	case SIOCGIFPDSTADDR:
852#ifdef INET6
853	case SIOCGIFPDSTADDR_IN6:
854#endif /* INET6 */
855		if (sc->gif_pdst == NULL) {
856			error = EADDRNOTAVAIL;
857			goto bad;
858		}
859		src = sc->gif_pdst;
860		switch (cmd) {
861#ifdef INET
862		case SIOCGIFPDSTADDR:
863			dst = &ifr->ifr_addr;
864			size = sizeof(ifr->ifr_addr);
865			break;
866#endif /* INET */
867#ifdef INET6
868		case SIOCGIFPDSTADDR_IN6:
869			dst = (struct sockaddr *)
870				&(((struct in6_ifreq *)data)->ifr_addr);
871			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
872			break;
873#endif /* INET6 */
874		default:
875			error = EADDRNOTAVAIL;
876			goto bad;
877		}
878		if (src->sa_len > size)
879			return EINVAL;
880		error = prison_if(curthread->td_ucred, src);
881		if (error != 0)
882			return (error);
883		error = prison_if(curthread->td_ucred, dst);
884		if (error != 0)
885			return (error);
886		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
887#ifdef INET6
888		if (dst->sa_family == AF_INET6) {
889			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
890			if (error != 0)
891				return (error);
892		}
893#endif
894		break;
895
896	case SIOCGLIFPHYADDR:
897		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
898			error = EADDRNOTAVAIL;
899			goto bad;
900		}
901
902		/* copy src */
903		src = sc->gif_psrc;
904		dst = (struct sockaddr *)
905			&(((struct if_laddrreq *)data)->addr);
906		size = sizeof(((struct if_laddrreq *)data)->addr);
907		if (src->sa_len > size)
908			return EINVAL;
909		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
910
911		/* copy dst */
912		src = sc->gif_pdst;
913		dst = (struct sockaddr *)
914			&(((struct if_laddrreq *)data)->dstaddr);
915		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
916		if (src->sa_len > size)
917			return EINVAL;
918		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
919		break;
920
921	case SIOCSIFFLAGS:
922		/* if_ioctl() takes care of it */
923		break;
924
925	case GIFGOPTS:
926		options = sc->gif_options;
927		error = copyout(&options, ifr->ifr_data,
928				sizeof(options));
929		break;
930
931	case GIFSOPTS:
932		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
933			break;
934		error = copyin(ifr->ifr_data, &options, sizeof(options));
935		if (error)
936			break;
937		if (options & ~GIF_OPTMASK)
938			error = EINVAL;
939		else
940			sc->gif_options = options;
941		break;
942
943	default:
944		error = EINVAL;
945		break;
946	}
947 bad:
948	return error;
949}
950
951/*
952 * XXXRW: There's a general event-ordering issue here: the code to check
953 * if a given tunnel is already present happens before we perform a
954 * potentially blocking setup of the tunnel.  This code needs to be
955 * re-ordered so that the check and replacement can be atomic using
956 * a mutex.
957 */
958int
959gif_set_tunnel(ifp, src, dst)
960	struct ifnet *ifp;
961	struct sockaddr *src;
962	struct sockaddr *dst;
963{
964	struct gif_softc *sc = ifp->if_softc;
965	struct gif_softc *sc2;
966	struct sockaddr *osrc, *odst, *sa;
967	int error = 0;
968
969	mtx_lock(&gif_mtx);
970	LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) {
971		if (sc2 == sc)
972			continue;
973		if (!sc2->gif_pdst || !sc2->gif_psrc)
974			continue;
975		if (sc2->gif_pdst->sa_family != dst->sa_family ||
976		    sc2->gif_pdst->sa_len != dst->sa_len ||
977		    sc2->gif_psrc->sa_family != src->sa_family ||
978		    sc2->gif_psrc->sa_len != src->sa_len)
979			continue;
980
981		/*
982		 * Disallow parallel tunnels unless instructed
983		 * otherwise.
984		 */
985		if (!V_parallel_tunnels &&
986		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
987		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
988			error = EADDRNOTAVAIL;
989			mtx_unlock(&gif_mtx);
990			goto bad;
991		}
992
993		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
994	}
995	mtx_unlock(&gif_mtx);
996
997	/* XXX we can detach from both, but be polite just in case */
998	if (sc->gif_psrc)
999		switch (sc->gif_psrc->sa_family) {
1000#ifdef INET
1001		case AF_INET:
1002			(void)in_gif_detach(sc);
1003			break;
1004#endif
1005#ifdef INET6
1006		case AF_INET6:
1007			(void)in6_gif_detach(sc);
1008			break;
1009#endif
1010		}
1011
1012	osrc = sc->gif_psrc;
1013	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
1014	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
1015	sc->gif_psrc = sa;
1016
1017	odst = sc->gif_pdst;
1018	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
1019	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
1020	sc->gif_pdst = sa;
1021
1022	switch (sc->gif_psrc->sa_family) {
1023#ifdef INET
1024	case AF_INET:
1025		error = in_gif_attach(sc);
1026		break;
1027#endif
1028#ifdef INET6
1029	case AF_INET6:
1030		/*
1031		 * Check validity of the scope zone ID of the addresses, and
1032		 * convert it into the kernel internal form if necessary.
1033		 */
1034		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
1035		if (error != 0)
1036			break;
1037		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
1038		if (error != 0)
1039			break;
1040		error = in6_gif_attach(sc);
1041		break;
1042#endif
1043	}
1044	if (error) {
1045		/* rollback */
1046		free((caddr_t)sc->gif_psrc, M_IFADDR);
1047		free((caddr_t)sc->gif_pdst, M_IFADDR);
1048		sc->gif_psrc = osrc;
1049		sc->gif_pdst = odst;
1050		goto bad;
1051	}
1052
1053	if (osrc)
1054		free((caddr_t)osrc, M_IFADDR);
1055	if (odst)
1056		free((caddr_t)odst, M_IFADDR);
1057
1058 bad:
1059	if (sc->gif_psrc && sc->gif_pdst)
1060		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1061	else
1062		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1063
1064	return error;
1065}
1066
1067void
1068gif_delete_tunnel(ifp)
1069	struct ifnet *ifp;
1070{
1071	struct gif_softc *sc = ifp->if_softc;
1072
1073	if (sc->gif_psrc) {
1074		free((caddr_t)sc->gif_psrc, M_IFADDR);
1075		sc->gif_psrc = NULL;
1076	}
1077	if (sc->gif_pdst) {
1078		free((caddr_t)sc->gif_pdst, M_IFADDR);
1079		sc->gif_pdst = NULL;
1080	}
1081	/* it is safe to detach from both */
1082#ifdef INET
1083	(void)in_gif_detach(sc);
1084#endif
1085#ifdef INET6
1086	(void)in6_gif_detach(sc);
1087#endif
1088	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1089}
1090