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