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