if_gif.c revision 1.9 1 /* $NetBSD: if_gif.c,v 1.9 2000/04/19 06:30:52 itojun Exp $ */
2 /* $KAME: if_gif.c,v 1.21 2000/04/19 06:20:11 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 /*
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 if (m->m_pkthdr.rcvif)
359 m->m_pkthdr.rcvif = gifp;
360
361 #if NBPFILTER > 0
362 if (gifp->if_bpf) {
363 /*
364 * We need to prepend the address family as
365 * a four byte field. Cons up a dummy header
366 * to pacify bpf. This is safe because bpf
367 * will only read from the mbuf (i.e., it won't
368 * try to free it or keep a pointer a to it).
369 */
370 struct mbuf m0;
371 u_int af = AF_INET6;
372
373 m0.m_next = m;
374 m0.m_len = 4;
375 m0.m_data = (char *)⁡
376
377 #ifdef HAVE_OLD_BPF
378 bpf_mtap(gifp, &m0);
379 #else
380 bpf_mtap(gifp->if_bpf, &m0);
381 #endif
382 }
383 #endif /*NBPFILTER > 0*/
384
385 /*
386 * Put the packet to the network layer input queue according to the
387 * specified address family.
388 * Note: older versions of gif_input directly called network layer
389 * input functions, e.g. ip6_input, here. We changed the policy to
390 * prevent too many recursive calls of such input functions, which
391 * might cause kernel panic. But the change may introduce another
392 * problem; if the input queue is full, packets are discarded.
393 * We believed it rarely occurs and changed the policy. If we find
394 * it occurs more times than we thought, we may change the policy
395 * again.
396 */
397 switch (af) {
398 #ifdef INET
399 case AF_INET:
400 ifq = &ipintrq;
401 isr = NETISR_IP;
402 break;
403 #endif
404 #ifdef INET6
405 case AF_INET6:
406 ifq = &ip6intrq;
407 isr = NETISR_IPV6;
408 break;
409 #endif
410 default:
411 m_freem(m);
412 return;
413 }
414
415 s = splimp();
416 if (IF_QFULL(ifq)) {
417 IF_DROP(ifq); /* update statistics */
418 m_freem(m);
419 splx(s);
420 return;
421 }
422 IF_ENQUEUE(ifq, m);
423 /* we need schednetisr since the address family may change */
424 schednetisr(isr);
425 gifp->if_ipackets++;
426 gifp->if_ibytes += m->m_pkthdr.len;
427 splx(s);
428
429 return;
430 }
431
432 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
433 int
434 gif_ioctl(ifp, cmd, data)
435 struct ifnet *ifp;
436 #if defined(__FreeBSD__) && __FreeBSD__ < 3
437 int cmd;
438 #else
439 u_long cmd;
440 #endif
441 caddr_t data;
442 {
443 struct gif_softc *sc = (struct gif_softc*)ifp;
444 struct ifreq *ifr = (struct ifreq*)data;
445 int error = 0, size;
446 struct sockaddr *dst, *src;
447 struct sockaddr *sa;
448 int i;
449 struct gif_softc *sc2;
450
451 switch (cmd) {
452 case SIOCSIFADDR:
453 break;
454
455 case SIOCSIFDSTADDR:
456 break;
457
458 case SIOCADDMULTI:
459 case SIOCDELMULTI:
460 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
461 switch (ifr->ifr_addr.sa_family) {
462 #ifdef INET
463 case AF_INET: /* IP supports Multicast */
464 break;
465 #endif /* INET */
466 #ifdef INET6
467 case AF_INET6: /* IP6 supports Multicast */
468 break;
469 #endif /* INET6 */
470 default: /* Other protocols doesn't support Multicast */
471 error = EAFNOSUPPORT;
472 break;
473 }
474 #endif /*not FreeBSD3*/
475 break;
476
477 #ifdef SIOCSIFMTU /* xxx */
478 #ifndef __OpenBSD__
479 case SIOCGIFMTU:
480 break;
481 case SIOCSIFMTU:
482 {
483 #ifdef __bsdi__
484 short mtu;
485 mtu = *(short *)ifr->ifr_data;
486 #else
487 u_long mtu;
488 mtu = ifr->ifr_mtu;
489 #endif
490 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
491 return (EINVAL);
492 }
493 ifp->if_mtu = mtu;
494 }
495 break;
496 #endif /* !OpenBSD */
497 #endif /* SIOCSIFMTU */
498
499 case SIOCSIFPHYADDR:
500 #ifdef INET6
501 case SIOCSIFPHYADDR_IN6:
502 #endif /* INET6 */
503 /* can't configure same pair of address onto two gifs */
504 src = (struct sockaddr *)
505 &(((struct in_aliasreq *)data)->ifra_addr);
506 dst = (struct sockaddr *)
507 &(((struct in_aliasreq *)data)->ifra_dstaddr);
508 for (i = 0; i < ngif; i++) {
509 sc2 = gif + i;
510 if (sc2 == sc)
511 continue;
512 if (!sc2->gif_pdst || !sc2->gif_psrc)
513 continue;
514 if (sc2->gif_pdst->sa_family == dst->sa_family &&
515 sc2->gif_pdst->sa_len == dst->sa_family &&
516 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
517 sc2->gif_psrc->sa_family == src->sa_family &&
518 sc2->gif_psrc->sa_len == src->sa_family &&
519 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
520 error = EADDRNOTAVAIL;
521 goto bad;
522 }
523 }
524
525 if (src->sa_family != dst->sa_family ||
526 src->sa_len != dst->sa_len) {
527 error = EINVAL;
528 break;
529 }
530 switch (src->sa_family) {
531 #ifdef INET
532 case AF_INET:
533 size = sizeof(struct sockaddr_in);
534 break;
535 #endif
536 #ifdef INET6
537 case AF_INET6:
538 size = sizeof(struct sockaddr_in6);
539 break;
540 #endif
541 default:
542 error = EAFNOSUPPORT;
543 goto bad;
544 }
545 if (src->sa_len != size) {
546 error = EINVAL;
547 break;
548 }
549
550 if (sc->gif_psrc)
551 free((caddr_t)sc->gif_psrc, M_IFADDR);
552 sa = (struct sockaddr *)malloc(size, M_IFADDR, M_WAITOK);
553 bcopy((caddr_t)src, (caddr_t)sa, size);
554 sc->gif_psrc = sa;
555
556 if (sc->gif_pdst)
557 free((caddr_t)sc->gif_pdst, M_IFADDR);
558 sa = (struct sockaddr *)malloc(size, M_IFADDR, M_WAITOK);
559 bcopy((caddr_t)dst, (caddr_t)sa, size);
560 sc->gif_pdst = sa;
561
562 ifp->if_flags |= IFF_UP;
563 if_up(ifp); /* send up RTM_IFINFO */
564
565 error = 0;
566 break;
567
568 #ifdef SIOCDIFPHYADDR
569 case SIOCDIFPHYADDR:
570 if (sc->gif_psrc) {
571 free((caddr_t)sc->gif_psrc, M_IFADDR);
572 sc->gif_psrc = NULL;
573 }
574 if (sc->gif_pdst) {
575 free((caddr_t)sc->gif_pdst, M_IFADDR);
576 sc->gif_pdst = NULL;
577 }
578 /* change the IFF_UP flag as well? */
579 break;
580 #endif
581
582 case SIOCGIFPSRCADDR:
583 #ifdef INET6
584 case SIOCGIFPSRCADDR_IN6:
585 #endif /* INET6 */
586 if (sc->gif_psrc == NULL) {
587 error = EADDRNOTAVAIL;
588 goto bad;
589 }
590 src = sc->gif_psrc;
591 switch (sc->gif_psrc->sa_family) {
592 #ifdef INET
593 case AF_INET:
594 dst = &ifr->ifr_addr;
595 size = sizeof(struct sockaddr_in);
596 break;
597 #endif /* INET */
598 #ifdef INET6
599 case AF_INET6:
600 dst = (struct sockaddr *)
601 &(((struct in6_ifreq *)data)->ifr_addr);
602 size = sizeof(struct sockaddr_in6);
603 break;
604 #endif /* INET6 */
605 default:
606 error = EADDRNOTAVAIL;
607 goto bad;
608 }
609 bcopy((caddr_t)src, (caddr_t)dst, size);
610 break;
611
612 case SIOCGIFPDSTADDR:
613 #ifdef INET6
614 case SIOCGIFPDSTADDR_IN6:
615 #endif /* INET6 */
616 if (sc->gif_pdst == NULL) {
617 error = EADDRNOTAVAIL;
618 goto bad;
619 }
620 src = sc->gif_pdst;
621 switch (sc->gif_pdst->sa_family) {
622 #ifdef INET
623 case AF_INET:
624 dst = &ifr->ifr_addr;
625 size = sizeof(struct sockaddr_in);
626 break;
627 #endif /* INET */
628 #ifdef INET6
629 case AF_INET6:
630 dst = (struct sockaddr *)
631 &(((struct in6_ifreq *)data)->ifr_addr);
632 size = sizeof(struct sockaddr_in6);
633 break;
634 #endif /* INET6 */
635 default:
636 error = EADDRNOTAVAIL;
637 goto bad;
638 }
639 bcopy((caddr_t)src, (caddr_t)dst, size);
640 break;
641
642 case SIOCSIFFLAGS:
643 /* if_ioctl() takes care of it */
644 break;
645
646 default:
647 error = EINVAL;
648 break;
649 }
650 bad:
651 return error;
652 }
653 #endif /*NGIF > 0*/
654