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