xref: /netbsd-src/sys/net/if_gif.c (revision 7f21db1c0118155e0dd40b75182e30c589d9f63e)
1 /*	$NetBSD: if_gif.c,v 1.77 2010/01/19 22:08:00 pooka Exp $	*/
2 /*	$KAME: if_gif.c,v 1.76 2001/08/20 02:01:02 kjc 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 <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: if_gif.c,v 1.77 2010/01/19 22:08:00 pooka Exp $");
35 
36 #include "opt_inet.h"
37 #include "opt_iso.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/errno.h>
46 #include <sys/ioctl.h>
47 #include <sys/time.h>
48 #include <sys/syslog.h>
49 #include <sys/proc.h>
50 #include <sys/protosw.h>
51 #include <sys/kauth.h>
52 #include <sys/cpu.h>
53 #include <sys/intr.h>
54 
55 #include <net/if.h>
56 #include <net/if_types.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
59 #include <net/bpf.h>
60 
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #ifdef	INET
65 #include <netinet/in_var.h>
66 #endif	/* INET */
67 #include <netinet/in_gif.h>
68 
69 #ifdef INET6
70 #ifndef INET
71 #include <netinet/in.h>
72 #endif
73 #include <netinet6/in6_var.h>
74 #include <netinet/ip6.h>
75 #include <netinet6/ip6_var.h>
76 #include <netinet6/in6_gif.h>
77 #include <netinet6/ip6protosw.h>
78 #endif /* INET6 */
79 
80 #ifdef ISO
81 #include <netiso/iso.h>
82 #include <netiso/iso_var.h>
83 #endif
84 
85 #include <netinet/ip_encap.h>
86 #include <net/if_gif.h>
87 
88 
89 #include <net/net_osdep.h>
90 
91 void	gifattach(int);
92 static void	gifintr(void *);
93 #ifdef ISO
94 static struct mbuf *gif_eon_encap(struct mbuf *);
95 static struct mbuf *gif_eon_decap(struct ifnet *, struct mbuf *);
96 #endif
97 
98 /*
99  * gif global variable definitions
100  */
101 LIST_HEAD(, gif_softc) gif_softc_list;	/* XXX should be static */
102 
103 static int	gif_clone_create(struct if_clone *, int);
104 static int	gif_clone_destroy(struct ifnet *);
105 
106 static struct if_clone gif_cloner =
107     IF_CLONE_INITIALIZER("gif", gif_clone_create, gif_clone_destroy);
108 
109 #ifndef MAX_GIF_NEST
110 /*
111  * This macro controls the upper limitation on nesting of gif tunnels.
112  * Since, setting a large value to this macro with a careless configuration
113  * may introduce system crash, we don't allow any nestings by default.
114  * If you need to configure nested gif tunnels, you can define this macro
115  * in your kernel configuration file.  However, if you do so, please be
116  * careful to configure the tunnels so that it won't make a loop.
117  */
118 #define MAX_GIF_NEST 1
119 #endif
120 static int max_gif_nesting = MAX_GIF_NEST;
121 
122 /* ARGSUSED */
123 void
124 gifattach(int count)
125 {
126 
127 	LIST_INIT(&gif_softc_list);
128 	if_clone_attach(&gif_cloner);
129 }
130 
131 static int
132 gif_clone_create(struct if_clone *ifc, int unit)
133 {
134 	struct gif_softc *sc;
135 
136 	sc = malloc(sizeof(struct gif_softc), M_DEVBUF, M_WAITOK|M_ZERO);
137 
138 	if_initname(&sc->gif_if, ifc->ifc_name, unit);
139 
140 	gifattach0(sc);
141 
142 	LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
143 	return (0);
144 }
145 
146 void
147 gifattach0(struct gif_softc *sc)
148 {
149 
150 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
151 
152 	sc->gif_if.if_addrlen = 0;
153 	sc->gif_if.if_mtu    = GIF_MTU;
154 	sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
155 	sc->gif_if.if_ioctl  = gif_ioctl;
156 	sc->gif_if.if_output = gif_output;
157 	sc->gif_if.if_type   = IFT_GIF;
158 	sc->gif_if.if_dlt    = DLT_NULL;
159 	sc->gif_if.if_softc  = sc;
160 	IFQ_SET_READY(&sc->gif_if.if_snd);
161 	if_attach(&sc->gif_if);
162 	if_alloc_sadl(&sc->gif_if);
163 	bpf_ops->bpf_attach(&sc->gif_if, DLT_NULL,
164 	    sizeof(u_int), &sc->gif_if.if_bpf);
165 }
166 
167 static int
168 gif_clone_destroy(struct ifnet *ifp)
169 {
170 	struct gif_softc *sc = (void *) ifp;
171 
172 	gif_delete_tunnel(&sc->gif_if);
173 	LIST_REMOVE(sc, gif_list);
174 #ifdef INET6
175 	encap_detach(sc->encap_cookie6);
176 #endif
177 #ifdef INET
178 	encap_detach(sc->encap_cookie4);
179 #endif
180 
181 	bpf_ops->bpf_detach(ifp);
182 	if_detach(ifp);
183 	rtcache_free(&sc->gif_ro);
184 
185 	free(sc, M_DEVBUF);
186 
187 	return (0);
188 }
189 
190 #ifdef GIF_ENCAPCHECK
191 int
192 gif_encapcheck(struct mbuf *m, int off, int proto, void *arg)
193 {
194 	struct ip ip;
195 	struct gif_softc *sc;
196 
197 	sc = arg;
198 	if (sc == NULL)
199 		return 0;
200 
201 	if ((sc->gif_if.if_flags & IFF_UP) == 0)
202 		return 0;
203 
204 	/* no physical address */
205 	if (!sc->gif_psrc || !sc->gif_pdst)
206 		return 0;
207 
208 	switch (proto) {
209 #ifdef INET
210 	case IPPROTO_IPV4:
211 		break;
212 #endif
213 #ifdef INET6
214 	case IPPROTO_IPV6:
215 		break;
216 #endif
217 #ifdef ISO
218 	case IPPROTO_EON:
219 		break;
220 #endif
221 	default:
222 		return 0;
223 	}
224 
225 	/* Bail on short packets */
226 	KASSERT(m->m_flags & M_PKTHDR);
227 	if (m->m_pkthdr.len < sizeof(ip))
228 		return 0;
229 
230 	m_copydata(m, 0, sizeof(ip), &ip);
231 
232 	switch (ip.ip_v) {
233 #ifdef INET
234 	case 4:
235 		if (sc->gif_psrc->sa_family != AF_INET ||
236 		    sc->gif_pdst->sa_family != AF_INET)
237 			return 0;
238 		return gif_encapcheck4(m, off, proto, arg);
239 #endif
240 #ifdef INET6
241 	case 6:
242 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
243 			return 0;
244 		if (sc->gif_psrc->sa_family != AF_INET6 ||
245 		    sc->gif_pdst->sa_family != AF_INET6)
246 			return 0;
247 		return gif_encapcheck6(m, off, proto, arg);
248 #endif
249 	default:
250 		return 0;
251 	}
252 }
253 #endif
254 
255 int
256 gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
257     struct rtentry *rt)
258 {
259 	struct gif_softc *sc = ifp->if_softc;
260 	int error = 0;
261 	static int called = 0;	/* XXX: MUTEX */
262 	ALTQ_DECL(struct altq_pktattr pktattr;)
263 	int s;
264 
265 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
266 
267 	/*
268 	 * gif may cause infinite recursion calls when misconfigured.
269 	 * We'll prevent this by introducing upper limit.
270 	 * XXX: this mechanism may introduce another problem about
271 	 *      mutual exclusion of the variable CALLED, especially if we
272 	 *      use kernel thread.
273 	 */
274 	if (++called > max_gif_nesting) {
275 		log(LOG_NOTICE,
276 		    "gif_output: recursively called too many times(%d)\n",
277 		    called);
278 		m_freem(m);
279 		error = EIO;	/* is there better errno? */
280 		goto end;
281 	}
282 
283 	m->m_flags &= ~(M_BCAST|M_MCAST);
284 	if (!(ifp->if_flags & IFF_UP) ||
285 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
286 		m_freem(m);
287 		error = ENETDOWN;
288 		goto end;
289 	}
290 
291 	/* inner AF-specific encapsulation */
292 	switch (dst->sa_family) {
293 #ifdef ISO
294 	case AF_ISO:
295 		m = gif_eon_encap(m);
296 		if (!m) {
297 			error = ENOBUFS;
298 			goto end;
299 		}
300 		break;
301 #endif
302 	default:
303 		break;
304 	}
305 
306 	/* XXX should we check if our outer source is legal? */
307 
308 	/* use DLT_NULL encapsulation here to pass inner af type */
309 	M_PREPEND(m, sizeof(int), M_DONTWAIT);
310 	if (!m) {
311 		error = ENOBUFS;
312 		goto end;
313 	}
314 	*mtod(m, int *) = dst->sa_family;
315 
316 	s = splnet();
317 	IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
318 	if (error) {
319 		splx(s);
320 		goto end;
321 	}
322 	splx(s);
323 
324 	softint_schedule(sc->gif_si);
325 	error = 0;
326 
327   end:
328 	called = 0;		/* reset recursion counter */
329 	if (error)
330 		ifp->if_oerrors++;
331 	return error;
332 }
333 
334 static void
335 gifintr(void *arg)
336 {
337 	struct gif_softc *sc;
338 	struct ifnet *ifp;
339 	struct mbuf *m;
340 	int family;
341 	int len;
342 	int s;
343 	int error;
344 
345 	sc = arg;
346 	ifp = &sc->gif_if;
347 
348 	/* output processing */
349 	while (1) {
350 		s = splnet();
351 		IFQ_DEQUEUE(&sc->gif_if.if_snd, m);
352 		splx(s);
353 		if (m == NULL)
354 			break;
355 
356 		/* grab and chop off inner af type */
357 		if (sizeof(int) > m->m_len) {
358 			m = m_pullup(m, sizeof(int));
359 			if (!m) {
360 				ifp->if_oerrors++;
361 				continue;
362 			}
363 		}
364 		family = *mtod(m, int *);
365 		if (ifp->if_bpf)
366 			bpf_ops->bpf_mtap(ifp->if_bpf, m);
367 		m_adj(m, sizeof(int));
368 
369 		len = m->m_pkthdr.len;
370 
371 		/* dispatch to output logic based on outer AF */
372 		switch (sc->gif_psrc->sa_family) {
373 #ifdef INET
374 		case AF_INET:
375 			error = in_gif_output(ifp, family, m);
376 			break;
377 #endif
378 #ifdef INET6
379 		case AF_INET6:
380 			error = in6_gif_output(ifp, family, m);
381 			break;
382 #endif
383 		default:
384 			m_freem(m);
385 			error = ENETDOWN;
386 			break;
387 		}
388 
389 		if (error)
390 			ifp->if_oerrors++;
391 		else {
392 			ifp->if_opackets++;
393 			ifp->if_obytes += len;
394 		}
395 	}
396 }
397 
398 void
399 gif_input(struct mbuf *m, int af, struct ifnet *ifp)
400 {
401 	int s, isr;
402 	struct ifqueue *ifq = NULL;
403 
404 	if (ifp == NULL) {
405 		/* just in case */
406 		m_freem(m);
407 		return;
408 	}
409 
410 	m->m_pkthdr.rcvif = ifp;
411 
412 	if (ifp->if_bpf)
413 		bpf_ops->bpf_mtap_af(ifp->if_bpf, af, m);
414 
415 	/*
416 	 * Put the packet to the network layer input queue according to the
417 	 * specified address family.
418 	 * Note: older versions of gif_input directly called network layer
419 	 * input functions, e.g. ip6_input, here.  We changed the policy to
420 	 * prevent too many recursive calls of such input functions, which
421 	 * might cause kernel panic.  But the change may introduce another
422 	 * problem; if the input queue is full, packets are discarded.
423 	 * The kernel stack overflow really happened, and we believed
424 	 * queue-full rarely occurs, so we changed the policy.
425 	 */
426 	switch (af) {
427 #ifdef INET
428 	case AF_INET:
429 		ifq = &ipintrq;
430 		isr = NETISR_IP;
431 		break;
432 #endif
433 #ifdef INET6
434 	case AF_INET6:
435 		ifq = &ip6intrq;
436 		isr = NETISR_IPV6;
437 		break;
438 #endif
439 #ifdef ISO
440 	case AF_ISO:
441 		m = gif_eon_decap(ifp, m);
442 		if (!m)
443 			return;
444 		ifq = &clnlintrq;
445 		isr = NETISR_ISO;
446 		break;
447 #endif
448 	default:
449 		m_freem(m);
450 		return;
451 	}
452 
453 	s = splnet();
454 	if (IF_QFULL(ifq)) {
455 		IF_DROP(ifq);	/* update statistics */
456 		m_freem(m);
457 		splx(s);
458 		return;
459 	}
460 	ifp->if_ipackets++;
461 	ifp->if_ibytes += m->m_pkthdr.len;
462 	IF_ENQUEUE(ifq, m);
463 	/* we need schednetisr since the address family may change */
464 	schednetisr(isr);
465 	splx(s);
466 }
467 
468 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
469 int
470 gif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
471 {
472 	struct lwp *l = curlwp;	/* XXX */
473 	struct gif_softc *sc  = ifp->if_softc;
474 	struct ifreq     *ifr = (struct ifreq*)data;
475 	int error = 0, size;
476 	struct sockaddr *dst, *src;
477 
478 	switch (cmd) {
479 	case SIOCSIFMTU:
480 	case SIOCSLIFPHYADDR:
481 #ifdef SIOCDIFPHYADDR
482 	case SIOCDIFPHYADDR:
483 #endif
484 		if ((error = kauth_authorize_network(l->l_cred,
485 		    KAUTH_NETWORK_INTERFACE,
486 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
487 		    NULL)) != 0)
488 			return (error);
489 		/* FALLTHROUGH */
490 	default:
491 		break;
492 	}
493 
494 	switch (cmd) {
495 	case SIOCINITIFADDR:
496 		ifp->if_flags |= IFF_UP;
497 		break;
498 
499 	case SIOCSIFDSTADDR:
500 		break;
501 
502 	case SIOCADDMULTI:
503 	case SIOCDELMULTI:
504 		switch (ifr->ifr_addr.sa_family) {
505 #ifdef INET
506 		case AF_INET:	/* IP supports Multicast */
507 			break;
508 #endif /* INET */
509 #ifdef INET6
510 		case AF_INET6:	/* IP6 supports Multicast */
511 			break;
512 #endif /* INET6 */
513 		default:  /* Other protocols doesn't support Multicast */
514 			error = EAFNOSUPPORT;
515 			break;
516 		}
517 		break;
518 
519 	case SIOCGIFMTU:
520 		break;
521 
522 	case SIOCSIFMTU:
523 		if (ifr->ifr_mtu < GIF_MTU_MIN || ifr->ifr_mtu > GIF_MTU_MAX)
524 			return EINVAL;
525 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
526 			error = 0;
527 		break;
528 
529 #ifdef INET
530 	case SIOCSIFPHYADDR:
531 #endif
532 #ifdef INET6
533 	case SIOCSIFPHYADDR_IN6:
534 #endif /* INET6 */
535 	case SIOCSLIFPHYADDR:
536 		switch (cmd) {
537 #ifdef INET
538 		case SIOCSIFPHYADDR:
539 			src = (struct sockaddr *)
540 				&(((struct in_aliasreq *)data)->ifra_addr);
541 			dst = (struct sockaddr *)
542 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
543 			break;
544 #endif
545 #ifdef INET6
546 		case SIOCSIFPHYADDR_IN6:
547 			src = (struct sockaddr *)
548 				&(((struct in6_aliasreq *)data)->ifra_addr);
549 			dst = (struct sockaddr *)
550 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
551 			break;
552 #endif
553 		case SIOCSLIFPHYADDR:
554 			src = (struct sockaddr *)
555 				&(((struct if_laddrreq *)data)->addr);
556 			dst = (struct sockaddr *)
557 				&(((struct if_laddrreq *)data)->dstaddr);
558 			break;
559 		default:
560 			return EINVAL;
561 		}
562 
563 		/* sa_family must be equal */
564 		if (src->sa_family != dst->sa_family)
565 			return EINVAL;
566 
567 		/* validate sa_len */
568 		switch (src->sa_family) {
569 #ifdef INET
570 		case AF_INET:
571 			if (src->sa_len != sizeof(struct sockaddr_in))
572 				return EINVAL;
573 			break;
574 #endif
575 #ifdef INET6
576 		case AF_INET6:
577 			if (src->sa_len != sizeof(struct sockaddr_in6))
578 				return EINVAL;
579 			break;
580 #endif
581 		default:
582 			return EAFNOSUPPORT;
583 		}
584 		switch (dst->sa_family) {
585 #ifdef INET
586 		case AF_INET:
587 			if (dst->sa_len != sizeof(struct sockaddr_in))
588 				return EINVAL;
589 			break;
590 #endif
591 #ifdef INET6
592 		case AF_INET6:
593 			if (dst->sa_len != sizeof(struct sockaddr_in6))
594 				return EINVAL;
595 			break;
596 #endif
597 		default:
598 			return EAFNOSUPPORT;
599 		}
600 
601 		/* check sa_family looks sane for the cmd */
602 		switch (cmd) {
603 		case SIOCSIFPHYADDR:
604 			if (src->sa_family == AF_INET)
605 				break;
606 			return EAFNOSUPPORT;
607 #ifdef INET6
608 		case SIOCSIFPHYADDR_IN6:
609 			if (src->sa_family == AF_INET6)
610 				break;
611 			return EAFNOSUPPORT;
612 #endif /* INET6 */
613 		case SIOCSLIFPHYADDR:
614 			/* checks done in the above */
615 			break;
616 		}
617 
618 		error = gif_set_tunnel(&sc->gif_if, src, dst);
619 		break;
620 
621 #ifdef SIOCDIFPHYADDR
622 	case SIOCDIFPHYADDR:
623 		gif_delete_tunnel(&sc->gif_if);
624 		break;
625 #endif
626 
627 	case SIOCGIFPSRCADDR:
628 #ifdef INET6
629 	case SIOCGIFPSRCADDR_IN6:
630 #endif /* INET6 */
631 		if (sc->gif_psrc == NULL) {
632 			error = EADDRNOTAVAIL;
633 			goto bad;
634 		}
635 		src = sc->gif_psrc;
636 		switch (cmd) {
637 #ifdef INET
638 		case SIOCGIFPSRCADDR:
639 			dst = &ifr->ifr_addr;
640 			size = sizeof(ifr->ifr_addr);
641 			break;
642 #endif /* INET */
643 #ifdef INET6
644 		case SIOCGIFPSRCADDR_IN6:
645 			dst = (struct sockaddr *)
646 				&(((struct in6_ifreq *)data)->ifr_addr);
647 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
648 			break;
649 #endif /* INET6 */
650 		default:
651 			error = EADDRNOTAVAIL;
652 			goto bad;
653 		}
654 		if (src->sa_len > size)
655 			return EINVAL;
656 		memcpy(dst, src, src->sa_len);
657 		break;
658 
659 	case SIOCGIFPDSTADDR:
660 #ifdef INET6
661 	case SIOCGIFPDSTADDR_IN6:
662 #endif /* INET6 */
663 		if (sc->gif_pdst == NULL) {
664 			error = EADDRNOTAVAIL;
665 			goto bad;
666 		}
667 		src = sc->gif_pdst;
668 		switch (cmd) {
669 #ifdef INET
670 		case SIOCGIFPDSTADDR:
671 			dst = &ifr->ifr_addr;
672 			size = sizeof(ifr->ifr_addr);
673 			break;
674 #endif /* INET */
675 #ifdef INET6
676 		case SIOCGIFPDSTADDR_IN6:
677 			dst = (struct sockaddr *)
678 				&(((struct in6_ifreq *)data)->ifr_addr);
679 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
680 			break;
681 #endif /* INET6 */
682 		default:
683 			error = EADDRNOTAVAIL;
684 			goto bad;
685 		}
686 		if (src->sa_len > size)
687 			return EINVAL;
688 		memcpy(dst, src, src->sa_len);
689 		break;
690 
691 	case SIOCGLIFPHYADDR:
692 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
693 			error = EADDRNOTAVAIL;
694 			goto bad;
695 		}
696 
697 		/* copy src */
698 		src = sc->gif_psrc;
699 		dst = (struct sockaddr *)
700 			&(((struct if_laddrreq *)data)->addr);
701 		size = sizeof(((struct if_laddrreq *)data)->addr);
702 		if (src->sa_len > size)
703 			return EINVAL;
704 		memcpy(dst, src, src->sa_len);
705 
706 		/* copy dst */
707 		src = sc->gif_pdst;
708 		dst = (struct sockaddr *)
709 			&(((struct if_laddrreq *)data)->dstaddr);
710 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
711 		if (src->sa_len > size)
712 			return EINVAL;
713 		memcpy(dst, src, src->sa_len);
714 		break;
715 
716 	default:
717 		return ifioctl_common(ifp, cmd, data);
718 	}
719  bad:
720 	return error;
721 }
722 
723 int
724 gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
725 {
726 	struct gif_softc *sc = ifp->if_softc;
727 	struct gif_softc *sc2;
728 	struct sockaddr *osrc, *odst;
729 	int s;
730 	int error;
731 
732 	s = splsoftnet();
733 
734 	LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
735 		if (sc2 == sc)
736 			continue;
737 		if (!sc2->gif_pdst || !sc2->gif_psrc)
738 			continue;
739 		/* can't configure same pair of address onto two gifs */
740 		if (sockaddr_cmp(sc2->gif_pdst, dst) == 0 &&
741 		    sockaddr_cmp(sc2->gif_psrc, src) == 0) {
742 			error = EADDRNOTAVAIL;
743 			goto bad;
744 		}
745 
746 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
747 	}
748 
749 	if (sc->gif_si) {
750 		softint_disestablish(sc->gif_si);
751 		sc->gif_si = NULL;
752 	}
753 
754 	/* XXX we can detach from both, but be polite just in case */
755 	if (sc->gif_psrc)
756 		switch (sc->gif_psrc->sa_family) {
757 #ifdef INET
758 		case AF_INET:
759 			(void)in_gif_detach(sc);
760 			break;
761 #endif
762 #ifdef INET6
763 		case AF_INET6:
764 			(void)in6_gif_detach(sc);
765 			break;
766 #endif
767 		}
768 
769 	sc->gif_si = softint_establish(SOFTINT_NET, gifintr, sc);
770 	if (sc->gif_si == NULL) {
771 		error = ENOMEM;
772 		goto bad;
773 	}
774 
775 	osrc = sc->gif_psrc;
776 	sc->gif_psrc = sockaddr_dup(src, M_WAITOK);
777 
778 	odst = sc->gif_pdst;
779 	sc->gif_pdst = sockaddr_dup(dst, M_WAITOK);
780 
781 	switch (sc->gif_psrc->sa_family) {
782 #ifdef INET
783 	case AF_INET:
784 		error = in_gif_attach(sc);
785 		break;
786 #endif
787 #ifdef INET6
788 	case AF_INET6:
789 		error = in6_gif_attach(sc);
790 		break;
791 #endif
792 	default:
793 		error = EINVAL;
794 		break;
795 	}
796 	if (error) {
797 		/* rollback */
798 		sockaddr_free(sc->gif_psrc);
799 		sockaddr_free(sc->gif_pdst);
800 		sc->gif_psrc = osrc;
801 		sc->gif_pdst = odst;
802 		goto bad;
803 	}
804 
805 	if (osrc)
806 		sockaddr_free(osrc);
807 	if (odst)
808 		sockaddr_free(odst);
809 
810 	if (sc->gif_psrc && sc->gif_pdst)
811 		ifp->if_flags |= IFF_RUNNING;
812 	else
813 		ifp->if_flags &= ~IFF_RUNNING;
814 	splx(s);
815 
816 	return 0;
817 
818  bad:
819 	if (sc->gif_si) {
820 		softint_disestablish(sc->gif_si);
821 		sc->gif_si = NULL;
822 	}
823 	if (sc->gif_psrc && sc->gif_pdst)
824 		ifp->if_flags |= IFF_RUNNING;
825 	else
826 		ifp->if_flags &= ~IFF_RUNNING;
827 	splx(s);
828 
829 	return error;
830 }
831 
832 void
833 gif_delete_tunnel(struct ifnet *ifp)
834 {
835 	struct gif_softc *sc = ifp->if_softc;
836 	int s;
837 
838 	s = splsoftnet();
839 
840 	if (sc->gif_si) {
841 		softint_disestablish(sc->gif_si);
842 		sc->gif_si = NULL;
843 	}
844 	if (sc->gif_psrc) {
845 		sockaddr_free(sc->gif_psrc);
846 		sc->gif_psrc = NULL;
847 	}
848 	if (sc->gif_pdst) {
849 		sockaddr_free(sc->gif_pdst);
850 		sc->gif_pdst = NULL;
851 	}
852 	/* it is safe to detach from both */
853 #ifdef INET
854 	(void)in_gif_detach(sc);
855 #endif
856 #ifdef INET6
857 	(void)in6_gif_detach(sc);
858 #endif
859 
860 	if (sc->gif_psrc && sc->gif_pdst)
861 		ifp->if_flags |= IFF_RUNNING;
862 	else
863 		ifp->if_flags &= ~IFF_RUNNING;
864 	splx(s);
865 }
866 
867 #ifdef ISO
868 struct eonhdr {
869 	uint8_t version;
870 	uint8_t class;
871 	uint16_t cksum;
872 };
873 
874 /*
875  * prepend EON header to ISO PDU
876  */
877 static struct mbuf *
878 gif_eon_encap(struct mbuf *m)
879 {
880 	struct eonhdr *ehdr;
881 
882 	M_PREPEND(m, sizeof(*ehdr), M_DONTWAIT);
883 	if (m && m->m_len < sizeof(*ehdr))
884 		m = m_pullup(m, sizeof(*ehdr));
885 	if (m == NULL)
886 		return NULL;
887 	ehdr = mtod(m, struct eonhdr *);
888 	ehdr->version = 1;
889 	ehdr->class = 0;		/* always unicast */
890 #if 0
891 	/* calculate the checksum of the eonhdr */
892 	{
893 		struct mbuf mhead;
894 		memset(&mhead, 0, sizeof(mhead));
895 		ehdr->cksum = 0;
896 		mhead.m_data = (void *)ehdr;
897 		mhead.m_len = sizeof(*ehdr);
898 		mhead.m_next = 0;
899 		iso_gen_csum(&mhead, offsetof(struct eonhdr, cksum),
900 		    mhead.m_len);
901 	}
902 #else
903 	/* since the data is always constant we'll just plug the value in */
904 	ehdr->cksum = htons(0xfc02);
905 #endif
906 	return m;
907 }
908 
909 /*
910  * remove EON header and check checksum
911  */
912 static struct mbuf *
913 gif_eon_decap(struct ifnet *ifp, struct mbuf *m)
914 {
915 	struct eonhdr *ehdr;
916 
917 	if (m->m_len < sizeof(*ehdr) &&
918 	    (m = m_pullup(m, sizeof(*ehdr))) == NULL) {
919 		ifp->if_ierrors++;
920 		return NULL;
921 	}
922 	if (iso_check_csum(m, sizeof(struct eonhdr))) {
923 		m_freem(m);
924 		return NULL;
925 	}
926 	m_adj(m, sizeof(*ehdr));
927 	return m;
928 }
929 #endif /*ISO*/
930