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