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