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