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