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