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