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