if_gre.c revision 1.59.10.2 1 /* $NetBSD: if_gre.c,v 1.59.10.2 2006/03/10 15:05:22 elad Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Heiko W.Rupp <hwr (at) pilhuhn.de>
9 *
10 * IPv6-over-GRE contributed by Gert Doering <gert (at) greenie.muc.de>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 /*
42 * Encapsulate L3 protocols into IP
43 * See RFC 1701 and 1702 for more details.
44 * If_gre is compatible with Cisco GRE tunnels, so you can
45 * have a NetBSD box as the other end of a tunnel interface of a Cisco
46 * router. See gre(4) for more details.
47 * Also supported: IP in IP encaps (proto 55) as of RFC 2004
48 */
49
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.59.10.2 2006/03/10 15:05:22 elad Exp $");
52
53 #include "opt_inet.h"
54 #include "opt_ns.h"
55 #include "bpfilter.h"
56
57 #ifdef INET
58 #include <sys/param.h>
59 #include <sys/malloc.h>
60 #include <sys/mbuf.h>
61 #include <sys/proc.h>
62 #include <sys/protosw.h>
63 #include <sys/socket.h>
64 #include <sys/ioctl.h>
65 #include <sys/queue.h>
66 #if __NetBSD__
67 #include <sys/systm.h>
68 #endif
69
70 #include <machine/cpu.h>
71
72 #include <net/ethertypes.h>
73 #include <net/if.h>
74 #include <net/if_types.h>
75 #include <net/netisr.h>
76 #include <net/route.h>
77
78 #ifdef INET
79 #include <netinet/in.h>
80 #include <netinet/in_systm.h>
81 #include <netinet/in_var.h>
82 #include <netinet/ip.h>
83 #include <netinet/ip_var.h>
84 #else
85 #error "Huh? if_gre without inet?"
86 #endif
87
88 #ifdef NS
89 #include <netns/ns.h>
90 #include <netns/ns_if.h>
91 #endif
92
93 #ifdef NETATALK
94 #include <netatalk/at.h>
95 #include <netatalk/at_var.h>
96 #include <netatalk/at_extern.h>
97 #endif
98
99 #if NBPFILTER > 0
100 #include <sys/time.h>
101 #include <net/bpf.h>
102 #endif
103
104 #include <net/if_gre.h>
105
106 /*
107 * It is not easy to calculate the right value for a GRE MTU.
108 * We leave this task to the admin and use the same default that
109 * other vendors use.
110 */
111 #define GREMTU 1476
112
113 struct gre_softc_head gre_softc_list;
114 int ip_gre_ttl = GRE_TTL;
115
116 static int gre_clone_create(struct if_clone *, int);
117 static int gre_clone_destroy(struct ifnet *);
118
119 static struct if_clone gre_cloner =
120 IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
121
122 static int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
123 struct rtentry *);
124 static int gre_ioctl(struct ifnet *, u_long, caddr_t);
125
126 static int gre_compute_route(struct gre_softc *sc);
127
128 static int
129 gre_clone_create(struct if_clone *ifc, int unit)
130 {
131 struct gre_softc *sc;
132
133 sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK);
134 memset(sc, 0, sizeof(struct gre_softc));
135
136 snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d",
137 ifc->ifc_name, unit);
138 sc->sc_if.if_softc = sc;
139 sc->sc_if.if_type = IFT_TUNNEL;
140 sc->sc_if.if_addrlen = 0;
141 sc->sc_if.if_hdrlen = 24; /* IP + GRE */
142 sc->sc_if.if_dlt = DLT_NULL;
143 sc->sc_if.if_mtu = GREMTU;
144 sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
145 sc->sc_if.if_output = gre_output;
146 sc->sc_if.if_ioctl = gre_ioctl;
147 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
148 sc->g_proto = IPPROTO_GRE;
149 sc->sc_if.if_flags |= IFF_LINK0;
150 if_attach(&sc->sc_if);
151 if_alloc_sadl(&sc->sc_if);
152 #if NBPFILTER > 0
153 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
154 #endif
155 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
156 return (0);
157 }
158
159 static int
160 gre_clone_destroy(struct ifnet *ifp)
161 {
162 struct gre_softc *sc = ifp->if_softc;
163
164 LIST_REMOVE(sc, sc_list);
165 #if NBPFILTER > 0
166 bpfdetach(ifp);
167 #endif
168 if_detach(ifp);
169 free(sc, M_DEVBUF);
170
171 return (0);
172 }
173
174 /*
175 * The output routine. Takes a packet and encapsulates it in the protocol
176 * given by sc->g_proto. See also RFC 1701 and RFC 2004
177 */
178 static int
179 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
180 struct rtentry *rt)
181 {
182 int error = 0;
183 struct gre_softc *sc = ifp->if_softc;
184 struct greip *gh;
185 struct ip *ip;
186 u_int8_t ip_tos = 0;
187 u_int16_t etype = 0;
188 struct mobile_h mob_h;
189
190 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
191 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
192 m_freem(m);
193 error = ENETDOWN;
194 goto end;
195 }
196
197 gh = NULL;
198 ip = NULL;
199
200 #if NBPFILTER >0
201 if (ifp->if_bpf)
202 bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
203 #endif
204
205 m->m_flags &= ~(M_BCAST|M_MCAST);
206
207 if (sc->g_proto == IPPROTO_MOBILE) {
208 if (dst->sa_family == AF_INET) {
209 struct mbuf *m0;
210 int msiz;
211
212 ip = mtod(m, struct ip *);
213
214 memset(&mob_h, 0, MOB_H_SIZ_L);
215 mob_h.proto = (ip->ip_p) << 8;
216 mob_h.odst = ip->ip_dst.s_addr;
217 ip->ip_dst.s_addr = sc->g_dst.s_addr;
218
219 /*
220 * If the packet comes from our host, we only change
221 * the destination address in the IP header.
222 * Else we also need to save and change the source
223 */
224 if (in_hosteq(ip->ip_src, sc->g_src)) {
225 msiz = MOB_H_SIZ_S;
226 } else {
227 mob_h.proto |= MOB_H_SBIT;
228 mob_h.osrc = ip->ip_src.s_addr;
229 ip->ip_src.s_addr = sc->g_src.s_addr;
230 msiz = MOB_H_SIZ_L;
231 }
232 HTONS(mob_h.proto);
233 mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
234
235 if ((m->m_data - msiz) < m->m_pktdat) {
236 /* need new mbuf */
237 MGETHDR(m0, M_DONTWAIT, MT_HEADER);
238 if (m0 == NULL) {
239 IF_DROP(&ifp->if_snd);
240 m_freem(m);
241 error = ENOBUFS;
242 goto end;
243 }
244 m0->m_next = m;
245 m->m_data += sizeof(struct ip);
246 m->m_len -= sizeof(struct ip);
247 m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
248 m0->m_len = msiz + sizeof(struct ip);
249 m0->m_data += max_linkhdr;
250 memcpy(mtod(m0, caddr_t), (caddr_t)ip,
251 sizeof(struct ip));
252 m = m0;
253 } else { /* we have some space left in the old one */
254 m->m_data -= msiz;
255 m->m_len += msiz;
256 m->m_pkthdr.len += msiz;
257 memmove(mtod(m, caddr_t), ip,
258 sizeof(struct ip));
259 }
260 ip = mtod(m, struct ip *);
261 memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
262 ip->ip_len = htons(ntohs(ip->ip_len) + msiz);
263 } else { /* AF_INET */
264 IF_DROP(&ifp->if_snd);
265 m_freem(m);
266 error = EINVAL;
267 goto end;
268 }
269 } else if (sc->g_proto == IPPROTO_GRE) {
270 #ifdef GRE_DEBUG
271 printf( "start gre_output/GRE, dst->sa_family=%d\n",
272 dst->sa_family );
273 #endif
274 switch (dst->sa_family) {
275 case AF_INET:
276 ip = mtod(m, struct ip *);
277 ip_tos = ip->ip_tos;
278 etype = ETHERTYPE_IP;
279 break;
280 #ifdef NETATALK
281 case AF_APPLETALK:
282 etype = ETHERTYPE_ATALK;
283 break;
284 #endif
285 #ifdef NS
286 case AF_NS:
287 etype = ETHERTYPE_NS;
288 break;
289 #endif
290 #ifdef INET6
291 case AF_INET6:
292 etype = ETHERTYPE_IPV6;
293 break;
294 #endif
295 default:
296 IF_DROP(&ifp->if_snd);
297 m_freem(m);
298 error = EAFNOSUPPORT;
299 goto end;
300 }
301 M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
302 } else {
303 IF_DROP(&ifp->if_snd);
304 m_freem(m);
305 error = EINVAL;
306 goto end;
307 }
308
309 if (m == NULL) { /* impossible */
310 IF_DROP(&ifp->if_snd);
311 error = ENOBUFS;
312 goto end;
313 }
314
315 gh = mtod(m, struct greip *);
316 if (sc->g_proto == IPPROTO_GRE) {
317 /* we don't have any GRE flags for now */
318
319 memset((void *)&gh->gi_g, 0, sizeof(struct gre_h));
320 gh->gi_ptype = htons(etype);
321 }
322
323 gh->gi_pr = sc->g_proto;
324 if (sc->g_proto != IPPROTO_MOBILE) {
325 gh->gi_src = sc->g_src;
326 gh->gi_dst = sc->g_dst;
327 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
328 ((struct ip*)gh)->ip_ttl = ip_gre_ttl;
329 ((struct ip*)gh)->ip_tos = ip_tos;
330 gh->gi_len = htons(m->m_pkthdr.len);
331 }
332
333 ifp->if_opackets++;
334 ifp->if_obytes += m->m_pkthdr.len;
335 /* send it off */
336 error = ip_output(m, NULL, &sc->route, 0,
337 (struct ip_moptions *)NULL, (struct socket *)NULL);
338 end:
339 if (error)
340 ifp->if_oerrors++;
341 return (error);
342 }
343
344 static int
345 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
346 {
347 struct proc *p = curproc; /* XXX */
348 struct ifreq *ifr = (struct ifreq *)data;
349 struct if_laddrreq *lifr = (struct if_laddrreq *)data;
350 struct gre_softc *sc = ifp->if_softc;
351 int s;
352 struct sockaddr_in si;
353 struct sockaddr *sa = NULL;
354 int error;
355
356 error = 0;
357
358 s = splnet();
359 switch (cmd) {
360 case SIOCSIFADDR:
361 ifp->if_flags |= IFF_UP;
362 break;
363 case SIOCSIFDSTADDR:
364 break;
365 case SIOCSIFFLAGS:
366 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag)) != 0)
367 break;
368 if ((ifr->ifr_flags & IFF_LINK0) != 0)
369 sc->g_proto = IPPROTO_GRE;
370 else
371 sc->g_proto = IPPROTO_MOBILE;
372 break;
373 case SIOCSIFMTU:
374 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag)) != 0)
375 break;
376 if (ifr->ifr_mtu < 576) {
377 error = EINVAL;
378 break;
379 }
380 ifp->if_mtu = ifr->ifr_mtu;
381 break;
382 case SIOCGIFMTU:
383 ifr->ifr_mtu = sc->sc_if.if_mtu;
384 break;
385 case SIOCADDMULTI:
386 case SIOCDELMULTI:
387 if (ifr == 0) {
388 error = EAFNOSUPPORT;
389 break;
390 }
391 switch (ifr->ifr_addr.sa_family) {
392 #ifdef INET
393 case AF_INET:
394 break;
395 #endif
396 #ifdef INET6
397 case AF_INET6:
398 break;
399 #endif
400 default:
401 error = EAFNOSUPPORT;
402 break;
403 }
404 break;
405 case GRESPROTO:
406 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag)) != 0)
407 break;
408 sc->g_proto = ifr->ifr_flags;
409 switch (sc->g_proto) {
410 case IPPROTO_GRE:
411 ifp->if_flags |= IFF_LINK0;
412 break;
413 case IPPROTO_MOBILE:
414 ifp->if_flags &= ~IFF_LINK0;
415 break;
416 default:
417 error = EPROTONOSUPPORT;
418 break;
419 }
420 break;
421 case GREGPROTO:
422 ifr->ifr_flags = sc->g_proto;
423 break;
424 case GRESADDRS:
425 case GRESADDRD:
426 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag)) != 0)
427 break;
428 /*
429 * set tunnel endpoints, compute a less specific route
430 * to the remote end and mark if as up
431 */
432 sa = &ifr->ifr_addr;
433 if (cmd == GRESADDRS)
434 sc->g_src = (satosin(sa))->sin_addr;
435 if (cmd == GRESADDRD)
436 sc->g_dst = (satosin(sa))->sin_addr;
437 recompute:
438 if ((sc->g_src.s_addr != INADDR_ANY) &&
439 (sc->g_dst.s_addr != INADDR_ANY)) {
440 if (sc->route.ro_rt != 0) /* free old route */
441 RTFREE(sc->route.ro_rt);
442 if (gre_compute_route(sc) == 0)
443 ifp->if_flags |= IFF_RUNNING;
444 else
445 ifp->if_flags &= ~IFF_RUNNING;
446 }
447 break;
448 case GREGADDRS:
449 memset(&si, 0, sizeof(si));
450 si.sin_family = AF_INET;
451 si.sin_len = sizeof(struct sockaddr_in);
452 si.sin_addr.s_addr = sc->g_src.s_addr;
453 sa = sintosa(&si);
454 ifr->ifr_addr = *sa;
455 break;
456 case GREGADDRD:
457 memset(&si, 0, sizeof(si));
458 si.sin_family = AF_INET;
459 si.sin_len = sizeof(struct sockaddr_in);
460 si.sin_addr.s_addr = sc->g_dst.s_addr;
461 sa = sintosa(&si);
462 ifr->ifr_addr = *sa;
463 break;
464 case SIOCSLIFPHYADDR:
465 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag)) != 0)
466 break;
467 if (lifr->addr.ss_family != AF_INET ||
468 lifr->dstaddr.ss_family != AF_INET) {
469 error = EAFNOSUPPORT;
470 break;
471 }
472 if (lifr->addr.ss_len != sizeof(si) ||
473 lifr->dstaddr.ss_len != sizeof(si)) {
474 error = EINVAL;
475 break;
476 }
477 sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
478 sc->g_dst =
479 (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
480 goto recompute;
481 case SIOCDIFPHYADDR:
482 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag)) != 0)
483 break;
484 sc->g_src.s_addr = INADDR_ANY;
485 sc->g_dst.s_addr = INADDR_ANY;
486 break;
487 case SIOCGLIFPHYADDR:
488 if (sc->g_src.s_addr == INADDR_ANY ||
489 sc->g_dst.s_addr == INADDR_ANY) {
490 error = EADDRNOTAVAIL;
491 break;
492 }
493 memset(&si, 0, sizeof(si));
494 si.sin_family = AF_INET;
495 si.sin_len = sizeof(struct sockaddr_in);
496 si.sin_addr.s_addr = sc->g_src.s_addr;
497 memcpy(&lifr->addr, &si, sizeof(si));
498 si.sin_addr.s_addr = sc->g_dst.s_addr;
499 memcpy(&lifr->dstaddr, &si, sizeof(si));
500 break;
501 default:
502 error = EINVAL;
503 break;
504 }
505
506 splx(s);
507 return (error);
508 }
509
510 /*
511 * computes a route to our destination that is not the one
512 * which would be taken by ip_output(), as this one will loop back to
513 * us. If the interface is p2p as a--->b, then a routing entry exists
514 * If we now send a packet to b (e.g. ping b), this will come down here
515 * gets src=a, dst=b tacked on and would from ip_output() sent back to
516 * if_gre.
517 * Goal here is to compute a route to b that is less specific than
518 * a-->b. We know that this one exists as in normal operation we have
519 * at least a default route which matches.
520 */
521 static int
522 gre_compute_route(struct gre_softc *sc)
523 {
524 struct route *ro;
525 u_int32_t a, b, c;
526
527 ro = &sc->route;
528
529 memset(ro, 0, sizeof(struct route));
530 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
531 ro->ro_dst.sa_family = AF_INET;
532 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
533
534 /*
535 * toggle last bit, so our interface is not found, but a less
536 * specific route. I'd rather like to specify a shorter mask,
537 * but this is not possible. Should work though. XXX
538 * there is a simpler way ...
539 */
540 if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
541 a = ntohl(sc->g_dst.s_addr);
542 b = a & 0x01;
543 c = a & 0xfffffffe;
544 b = b ^ 0x01;
545 a = b | c;
546 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
547 = htonl(a);
548 }
549
550 #ifdef DIAGNOSTIC
551 printf("%s: searching for a route to %s", sc->sc_if.if_xname,
552 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
553 #endif
554
555 rtalloc(ro);
556
557 /*
558 * check if this returned a route at all and this route is no
559 * recursion to ourself
560 */
561 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
562 #ifdef DIAGNOSTIC
563 if (ro->ro_rt == NULL)
564 printf(" - no route found!\n");
565 else
566 printf(" - route loops back to ourself!\n");
567 #endif
568 return EADDRNOTAVAIL;
569 }
570
571 /*
572 * now change it back - else ip_output will just drop
573 * the route and search one to this interface ...
574 */
575 if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
576 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
577
578 #ifdef DIAGNOSTIC
579 printf(", choosing %s with gateway %s", ro->ro_rt->rt_ifp->if_xname,
580 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
581 printf("\n");
582 #endif
583
584 return 0;
585 }
586
587 /*
588 * do a checksum of a buffer - much like in_cksum, which operates on
589 * mbufs.
590 */
591 u_int16_t
592 gre_in_cksum(u_int16_t *p, u_int len)
593 {
594 u_int32_t sum = 0;
595 int nwords = len >> 1;
596
597 while (nwords-- != 0)
598 sum += *p++;
599
600 if (len & 1) {
601 union {
602 u_short w;
603 u_char c[2];
604 } u;
605 u.c[0] = *(u_char *)p;
606 u.c[1] = 0;
607 sum += u.w;
608 }
609
610 /* end-around-carry */
611 sum = (sum >> 16) + (sum & 0xffff);
612 sum += (sum >> 16);
613 return (~sum);
614 }
615 #endif
616
617 void greattach(int);
618
619 /* ARGSUSED */
620 void
621 greattach(int count)
622 {
623 #ifdef INET
624 LIST_INIT(&gre_softc_list);
625 if_clone_attach(&gre_cloner);
626 #endif
627 }
628