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