xref: /freebsd/sys/net/if_gif.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * Copyright (c) 2018 Andrey V. Elsukov <ae@FreeBSD.org>
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  *	$KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $
33  */
34 
35 #include <sys/cdefs.h>
36 #include "opt_inet.h"
37 #include "opt_inet6.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/module.h>
46 #include <sys/rmlock.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/sx.h>
50 #include <sys/errno.h>
51 #include <sys/time.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
54 #include <sys/priv.h>
55 #include <sys/proc.h>
56 #include <sys/conf.h>
57 #include <machine/cpu.h>
58 
59 #include <net/if.h>
60 #include <net/if_var.h>
61 #include <net/if_private.h>
62 #include <net/if_clone.h>
63 #include <net/if_types.h>
64 #include <net/netisr.h>
65 #include <net/route.h>
66 #include <net/bpf.h>
67 #include <net/vnet.h>
68 
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip_ecn.h>
73 #ifdef	INET
74 #include <netinet/in_var.h>
75 #include <netinet/ip_var.h>
76 #endif	/* INET */
77 
78 #ifdef INET6
79 #ifndef INET
80 #include <netinet/in.h>
81 #endif
82 #include <netinet6/in6_var.h>
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_ecn.h>
85 #include <netinet6/ip6_var.h>
86 #endif /* INET6 */
87 
88 #include <netinet/ip_encap.h>
89 #include <net/ethernet.h>
90 #include <net/if_bridgevar.h>
91 #include <net/if_gif.h>
92 
93 #include <security/mac/mac_framework.h>
94 
95 static const char gifname[] = "gif";
96 
97 MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
98 static struct sx gif_ioctl_sx;
99 SX_SYSINIT(gif_ioctl_sx, &gif_ioctl_sx, "gif_ioctl");
100 
101 void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
102 void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
103 void	(*ng_gif_attach_p)(struct ifnet *ifp);
104 void	(*ng_gif_detach_p)(struct ifnet *ifp);
105 
106 #ifdef VIMAGE
107 static void	gif_reassign(struct ifnet *, struct vnet *, char *);
108 #endif
109 static void	gif_delete_tunnel(struct gif_softc *);
110 static int	gif_ioctl(struct ifnet *, u_long, caddr_t);
111 static int	gif_transmit(struct ifnet *, struct mbuf *);
112 static void	gif_qflush(struct ifnet *);
113 static int	gif_clone_create(struct if_clone *, int, caddr_t);
114 static void	gif_clone_destroy(struct ifnet *);
115 VNET_DEFINE_STATIC(struct if_clone *, gif_cloner);
116 #define	V_gif_cloner	VNET(gif_cloner)
117 
118 SYSCTL_DECL(_net_link);
119 static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
120     "Generic Tunnel Interface");
121 #ifndef MAX_GIF_NEST
122 /*
123  * This macro controls the default upper limitation on nesting of gif tunnels.
124  * Since, setting a large value to this macro with a careless configuration
125  * may introduce system crash, we don't allow any nestings by default.
126  * If you need to configure nested gif tunnels, you can define this macro
127  * in your kernel configuration file.  However, if you do so, please be
128  * careful to configure the tunnels so that it won't make a loop.
129  */
130 #define MAX_GIF_NEST 1
131 #endif
132 VNET_DEFINE_STATIC(int, max_gif_nesting) = MAX_GIF_NEST;
133 #define	V_max_gif_nesting	VNET(max_gif_nesting)
134 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW,
135     &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels");
136 
137 static int
138 gif_clone_create(struct if_clone *ifc, int unit, caddr_t params)
139 {
140 	struct gif_softc *sc;
141 
142 	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
143 	sc->gif_fibnum = curthread->td_proc->p_fibnum;
144 	GIF2IFP(sc) = if_alloc(IFT_GIF);
145 	GIF2IFP(sc)->if_softc = sc;
146 	if_initname(GIF2IFP(sc), gifname, unit);
147 
148 	GIF2IFP(sc)->if_addrlen = 0;
149 	GIF2IFP(sc)->if_mtu    = GIF_MTU;
150 	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
151 	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
152 	GIF2IFP(sc)->if_transmit = gif_transmit;
153 	GIF2IFP(sc)->if_qflush = gif_qflush;
154 	GIF2IFP(sc)->if_output = gif_output;
155 #ifdef VIMAGE
156 	GIF2IFP(sc)->if_reassign = gif_reassign;
157 #endif
158 	GIF2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
159 	GIF2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
160 	if_attach(GIF2IFP(sc));
161 	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
162 	if (ng_gif_attach_p != NULL)
163 		(*ng_gif_attach_p)(GIF2IFP(sc));
164 
165 	return (0);
166 }
167 
168 #ifdef VIMAGE
169 static void
170 gif_reassign(struct ifnet *ifp, struct vnet *new_vnet __unused,
171     char *unused __unused)
172 {
173 	struct gif_softc *sc;
174 
175 	sx_xlock(&gif_ioctl_sx);
176 	sc = ifp->if_softc;
177 	if (sc != NULL)
178 		gif_delete_tunnel(sc);
179 	sx_xunlock(&gif_ioctl_sx);
180 }
181 #endif /* VIMAGE */
182 
183 static void
184 gif_clone_destroy(struct ifnet *ifp)
185 {
186 	struct gif_softc *sc;
187 
188 	sx_xlock(&gif_ioctl_sx);
189 	sc = ifp->if_softc;
190 	gif_delete_tunnel(sc);
191 	if (ng_gif_detach_p != NULL)
192 		(*ng_gif_detach_p)(ifp);
193 	bpfdetach(ifp);
194 	if_detach(ifp);
195 	ifp->if_softc = NULL;
196 	sx_xunlock(&gif_ioctl_sx);
197 
198 	GIF_WAIT();
199 	if_free(ifp);
200 	free(sc, M_GIF);
201 }
202 
203 static void
204 vnet_gif_init(const void *unused __unused)
205 {
206 
207 	V_gif_cloner = if_clone_simple(gifname, gif_clone_create,
208 	    gif_clone_destroy, 0);
209 #ifdef INET
210 	in_gif_init();
211 #endif
212 #ifdef INET6
213 	in6_gif_init();
214 #endif
215 }
216 VNET_SYSINIT(vnet_gif_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
217     vnet_gif_init, NULL);
218 
219 static void
220 vnet_gif_uninit(const void *unused __unused)
221 {
222 
223 	if_clone_detach(V_gif_cloner);
224 #ifdef INET
225 	in_gif_uninit();
226 #endif
227 #ifdef INET6
228 	in6_gif_uninit();
229 #endif
230 }
231 VNET_SYSUNINIT(vnet_gif_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
232     vnet_gif_uninit, NULL);
233 
234 static int
235 gifmodevent(module_t mod, int type, void *data)
236 {
237 
238 	switch (type) {
239 	case MOD_LOAD:
240 	case MOD_UNLOAD:
241 		break;
242 	default:
243 		return (EOPNOTSUPP);
244 	}
245 	return (0);
246 }
247 
248 static moduledata_t gif_mod = {
249 	"if_gif",
250 	gifmodevent,
251 	0
252 };
253 
254 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
255 MODULE_VERSION(if_gif, 1);
256 
257 struct gif_list *
258 gif_hashinit(void)
259 {
260 	struct gif_list *hash;
261 	int i;
262 
263 	hash = malloc(sizeof(struct gif_list) * GIF_HASH_SIZE,
264 	    M_GIF, M_WAITOK);
265 	for (i = 0; i < GIF_HASH_SIZE; i++)
266 		CK_LIST_INIT(&hash[i]);
267 
268 	return (hash);
269 }
270 
271 void
272 gif_hashdestroy(struct gif_list *hash)
273 {
274 
275 	free(hash, M_GIF);
276 }
277 
278 #define	MTAG_GIF	1080679712
279 static int
280 gif_transmit(struct ifnet *ifp, struct mbuf *m)
281 {
282 	struct gif_softc *sc;
283 	struct etherip_header *eth;
284 #ifdef INET
285 	struct ip *ip;
286 #endif
287 #ifdef INET6
288 	struct ip6_hdr *ip6;
289 	uint32_t t;
290 #endif
291 	uint32_t af;
292 	uint8_t proto, ecn;
293 	int error;
294 
295 	NET_EPOCH_ASSERT();
296 #ifdef MAC
297 	error = mac_ifnet_check_transmit(ifp, m);
298 	if (error) {
299 		m_freem(m);
300 		goto err;
301 	}
302 #endif
303 	error = ENETDOWN;
304 	sc = ifp->if_softc;
305 	if ((ifp->if_flags & IFF_MONITOR) != 0 ||
306 	    (ifp->if_flags & IFF_UP) == 0 ||
307 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
308 	    sc->gif_family == 0 ||
309 	    (error = if_tunnel_check_nesting(ifp, m, MTAG_GIF,
310 		V_max_gif_nesting)) != 0) {
311 		m_freem(m);
312 		goto err;
313 	}
314 	/* Now pull back the af that we stashed in the csum_data. */
315 	if (ifp->if_bridge)
316 		af = AF_LINK;
317 	else
318 		af = m->m_pkthdr.csum_data;
319 	m->m_flags &= ~(M_BCAST|M_MCAST);
320 	M_SETFIB(m, sc->gif_fibnum);
321 	BPF_MTAP2(ifp, &af, sizeof(af), m);
322 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
323 	if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
324 	/* inner AF-specific encapsulation */
325 	ecn = 0;
326 	switch (af) {
327 #ifdef INET
328 	case AF_INET:
329 		proto = IPPROTO_IPV4;
330 		if (m->m_len < sizeof(struct ip))
331 			m = m_pullup(m, sizeof(struct ip));
332 		if (m == NULL) {
333 			error = ENOBUFS;
334 			goto err;
335 		}
336 		ip = mtod(m, struct ip *);
337 		ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
338 		    ECN_NOCARE, &ecn, &ip->ip_tos);
339 		break;
340 #endif
341 #ifdef INET6
342 	case AF_INET6:
343 		proto = IPPROTO_IPV6;
344 		if (m->m_len < sizeof(struct ip6_hdr))
345 			m = m_pullup(m, sizeof(struct ip6_hdr));
346 		if (m == NULL) {
347 			error = ENOBUFS;
348 			goto err;
349 		}
350 		t = 0;
351 		ip6 = mtod(m, struct ip6_hdr *);
352 		ip6_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
353 		    ECN_NOCARE, &t, &ip6->ip6_flow);
354 		ecn = (ntohl(t) >> 20) & 0xff;
355 		break;
356 #endif
357 	case AF_LINK:
358 		proto = IPPROTO_ETHERIP;
359 		M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT);
360 		if (m == NULL) {
361 			error = ENOBUFS;
362 			goto err;
363 		}
364 		eth = mtod(m, struct etherip_header *);
365 		eth->eip_resvh = 0;
366 		eth->eip_ver = ETHERIP_VERSION;
367 		eth->eip_resvl = 0;
368 		break;
369 	default:
370 		error = EAFNOSUPPORT;
371 		m_freem(m);
372 		goto err;
373 	}
374 	/* XXX should we check if our outer source is legal? */
375 	/* dispatch to output logic based on outer AF */
376 	switch (sc->gif_family) {
377 #ifdef INET
378 	case AF_INET:
379 		error = in_gif_output(ifp, m, proto, ecn);
380 		break;
381 #endif
382 #ifdef INET6
383 	case AF_INET6:
384 		error = in6_gif_output(ifp, m, proto, ecn);
385 		break;
386 #endif
387 	default:
388 		m_freem(m);
389 	}
390 err:
391 	if (error)
392 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
393 	return (error);
394 }
395 
396 static void
397 gif_qflush(struct ifnet *ifp __unused)
398 {
399 
400 }
401 
402 int
403 gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
404 	struct route *ro)
405 {
406 	uint32_t af;
407 
408 	KASSERT(ifp->if_bridge == NULL,
409 	    ("%s: unexpectedly called with bridge attached", __func__));
410 
411 	if (dst->sa_family == AF_UNSPEC)
412 		memcpy(&af, dst->sa_data, sizeof(af));
413 	else
414 		af = RO_GET_FAMILY(ro, dst);
415 	/*
416 	 * Now save the af in the inbound pkt csum data, this is a cheat since
417 	 * we are using the inbound csum_data field to carry the af over to
418 	 * the gif_transmit() routine, avoiding using yet another mtag.
419 	 */
420 	m->m_pkthdr.csum_data = af;
421 	return (ifp->if_transmit(ifp, m));
422 }
423 
424 void
425 gif_input(struct mbuf *m, struct ifnet *ifp, int proto, uint8_t ecn)
426 {
427 	struct etherip_header *eip;
428 #ifdef INET
429 	struct ip *ip;
430 #endif
431 #ifdef INET6
432 	struct ip6_hdr *ip6;
433 	uint32_t t;
434 #endif
435 	struct ether_header *eh;
436 	struct ifnet *oldifp;
437 	int isr, n, af;
438 
439 	NET_EPOCH_ASSERT();
440 
441 	if (ifp == NULL) {
442 		/* just in case */
443 		m_freem(m);
444 		return;
445 	}
446 	m->m_pkthdr.rcvif = ifp;
447 	m_clrprotoflags(m);
448 	switch (proto) {
449 #ifdef INET
450 	case IPPROTO_IPV4:
451 		af = AF_INET;
452 		if (m->m_len < sizeof(struct ip))
453 			m = m_pullup(m, sizeof(struct ip));
454 		if (m == NULL)
455 			goto drop;
456 		ip = mtod(m, struct ip *);
457 		if (ip_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
458 		    ECN_NOCARE, &ecn, &ip->ip_tos) == 0) {
459 			m_freem(m);
460 			goto drop;
461 		}
462 		break;
463 #endif
464 #ifdef INET6
465 	case IPPROTO_IPV6:
466 		af = AF_INET6;
467 		if (m->m_len < sizeof(struct ip6_hdr))
468 			m = m_pullup(m, sizeof(struct ip6_hdr));
469 		if (m == NULL)
470 			goto drop;
471 		t = htonl((uint32_t)ecn << 20);
472 		ip6 = mtod(m, struct ip6_hdr *);
473 		if (ip6_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
474 		    ECN_NOCARE, &t, &ip6->ip6_flow) == 0) {
475 			m_freem(m);
476 			goto drop;
477 		}
478 		break;
479 #endif
480 	case IPPROTO_ETHERIP:
481 		af = AF_LINK;
482 		break;
483 	default:
484 		m_freem(m);
485 		goto drop;
486 	}
487 
488 #ifdef MAC
489 	mac_ifnet_create_mbuf(ifp, m);
490 #endif
491 
492 	if (bpf_peers_present(ifp->if_bpf)) {
493 		uint32_t af1 = af;
494 		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
495 	}
496 
497 	if ((ifp->if_flags & IFF_MONITOR) != 0) {
498 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
499 		if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
500 		m_freem(m);
501 		return;
502 	}
503 
504 	if (ng_gif_input_p != NULL) {
505 		(*ng_gif_input_p)(ifp, &m, af);
506 		if (m == NULL)
507 			goto drop;
508 	}
509 
510 	/*
511 	 * Put the packet to the network layer input queue according to the
512 	 * specified address family.
513 	 * Note: older versions of gif_input directly called network layer
514 	 * input functions, e.g. ip6_input, here.  We changed the policy to
515 	 * prevent too many recursive calls of such input functions, which
516 	 * might cause kernel panic.  But the change may introduce another
517 	 * problem; if the input queue is full, packets are discarded.
518 	 * The kernel stack overflow really happened, and we believed
519 	 * queue-full rarely occurs, so we changed the policy.
520 	 */
521 	switch (af) {
522 #ifdef INET
523 	case AF_INET:
524 		isr = NETISR_IP;
525 		break;
526 #endif
527 #ifdef INET6
528 	case AF_INET6:
529 		isr = NETISR_IPV6;
530 		break;
531 #endif
532 	case AF_LINK:
533 		n = sizeof(struct etherip_header) +
534 		    sizeof(struct ether_header);
535 		if (n > m->m_len)
536 			m = m_pullup(m, n);
537 		if (m == NULL)
538 			goto drop;
539 		eip = mtod(m, struct etherip_header *);
540 		if (eip->eip_ver != ETHERIP_VERSION) {
541 			/* discard unknown versions */
542 			m_freem(m);
543 			goto drop;
544 		}
545 
546 		m_adj_decap(m, sizeof(struct etherip_header));
547 
548 		m->m_flags &= ~(M_BCAST|M_MCAST);
549 		m->m_pkthdr.rcvif = ifp;
550 
551 		if (ifp->if_bridge) {
552 			oldifp = ifp;
553 			eh = mtod(m, struct ether_header *);
554 			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
555 				if (ETHER_IS_BROADCAST(eh->ether_dhost))
556 					m->m_flags |= M_BCAST;
557 				else
558 					m->m_flags |= M_MCAST;
559 				if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
560 			}
561 			BRIDGE_INPUT(ifp, m);
562 
563 			if (m != NULL && ifp != oldifp) {
564 				/*
565 				 * The bridge gave us back itself or one of the
566 				 * members for which the frame is addressed.
567 				 */
568 				ether_demux(ifp, m);
569 				return;
570 			}
571 		}
572 		if (m != NULL)
573 			m_freem(m);
574 		return;
575 
576 	default:
577 		if (ng_gif_input_orphan_p != NULL)
578 			(*ng_gif_input_orphan_p)(ifp, m, af);
579 		else
580 			m_freem(m);
581 		return;
582 	}
583 
584 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
585 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
586 	M_SETFIB(m, ifp->if_fib);
587 	netisr_dispatch(isr, m);
588 	return;
589 drop:
590 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
591 }
592 
593 static int
594 gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
595 {
596 	struct ifreq *ifr = (struct ifreq*)data;
597 	struct gif_softc *sc;
598 	u_int options;
599 	int error;
600 
601 	switch (cmd) {
602 	case SIOCSIFADDR:
603 		ifp->if_flags |= IFF_UP;
604 	case SIOCADDMULTI:
605 	case SIOCDELMULTI:
606 	case SIOCGIFMTU:
607 	case SIOCSIFFLAGS:
608 		return (0);
609 	case SIOCSIFMTU:
610 		if (ifr->ifr_mtu < GIF_MTU_MIN ||
611 		    ifr->ifr_mtu > GIF_MTU_MAX)
612 			return (EINVAL);
613 		else
614 			ifp->if_mtu = ifr->ifr_mtu;
615 		return (0);
616 	}
617 	sx_xlock(&gif_ioctl_sx);
618 	sc = ifp->if_softc;
619 	if (sc == NULL) {
620 		error = ENXIO;
621 		goto bad;
622 	}
623 	error = 0;
624 	switch (cmd) {
625 	case SIOCDIFPHYADDR:
626 		if (sc->gif_family == 0)
627 			break;
628 		gif_delete_tunnel(sc);
629 		break;
630 #ifdef INET
631 	case SIOCSIFPHYADDR:
632 	case SIOCGIFPSRCADDR:
633 	case SIOCGIFPDSTADDR:
634 		error = in_gif_ioctl(sc, cmd, data);
635 		break;
636 #endif
637 #ifdef INET6
638 	case SIOCSIFPHYADDR_IN6:
639 	case SIOCGIFPSRCADDR_IN6:
640 	case SIOCGIFPDSTADDR_IN6:
641 		error = in6_gif_ioctl(sc, cmd, data);
642 		break;
643 #endif
644 	case SIOCGTUNFIB:
645 		ifr->ifr_fib = sc->gif_fibnum;
646 		break;
647 	case SIOCSTUNFIB:
648 		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
649 			break;
650 		if (ifr->ifr_fib >= rt_numfibs)
651 			error = EINVAL;
652 		else
653 			sc->gif_fibnum = ifr->ifr_fib;
654 		break;
655 	case GIFGOPTS:
656 		options = sc->gif_options;
657 		error = copyout(&options, ifr_data_get_ptr(ifr),
658 		    sizeof(options));
659 		break;
660 	case GIFSOPTS:
661 		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
662 			break;
663 		error = copyin(ifr_data_get_ptr(ifr), &options,
664 		    sizeof(options));
665 		if (error)
666 			break;
667 		if (options & ~GIF_OPTMASK) {
668 			error = EINVAL;
669 			break;
670 		}
671 		if (sc->gif_options != options) {
672 			switch (sc->gif_family) {
673 #ifdef INET
674 			case AF_INET:
675 				error = in_gif_setopts(sc, options);
676 				break;
677 #endif
678 #ifdef INET6
679 			case AF_INET6:
680 				error = in6_gif_setopts(sc, options);
681 				break;
682 #endif
683 			default:
684 				/* No need to invoke AF-handler */
685 				sc->gif_options = options;
686 			}
687 		}
688 		break;
689 	default:
690 		error = EINVAL;
691 		break;
692 	}
693 	if (error == 0 && sc->gif_family != 0) {
694 		if (
695 #ifdef INET
696 		    cmd == SIOCSIFPHYADDR ||
697 #endif
698 #ifdef INET6
699 		    cmd == SIOCSIFPHYADDR_IN6 ||
700 #endif
701 		    0) {
702 			if_link_state_change(ifp, LINK_STATE_UP);
703 		}
704 	}
705 bad:
706 	sx_xunlock(&gif_ioctl_sx);
707 	return (error);
708 }
709 
710 static void
711 gif_delete_tunnel(struct gif_softc *sc)
712 {
713 
714 	sx_assert(&gif_ioctl_sx, SA_XLOCKED);
715 	if (sc->gif_family != 0) {
716 		CK_LIST_REMOVE(sc, srchash);
717 		CK_LIST_REMOVE(sc, chain);
718 		/* Wait until it become safe to free gif_hdr */
719 		GIF_WAIT();
720 		free(sc->gif_hdr, M_GIF);
721 	}
722 	sc->gif_family = 0;
723 	GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
724 	if_link_state_change(GIF2IFP(sc), LINK_STATE_DOWN);
725 }
726