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