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