if_gre.c revision 1.61 1 /* $NetBSD: if_gre.c,v 1.61 2006/07/23 22:06:12 ad 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.61 2006/07/23 22:06:12 ad 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 lwp *l = curlwp; /* 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 switch (cmd) {
358 case SIOCSIFFLAGS:
359 case SIOCSIFMTU:
360 case GRESPROTO:
361 case GRESADDRD:
362 case GRESADDRS:
363 case SIOCSLIFPHYADDR:
364 case SIOCDIFPHYADDR:
365 if ((error = kauth_authorize_generic(l->l_cred,
366 KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
367 return (error);
368 break;
369 default:
370 error = 0;
371 break;
372 }
373
374 s = splnet();
375 switch (cmd) {
376 case SIOCSIFADDR:
377 ifp->if_flags |= IFF_UP;
378 break;
379 case SIOCSIFDSTADDR:
380 break;
381 case SIOCSIFFLAGS:
382 if ((ifr->ifr_flags & IFF_LINK0) != 0)
383 sc->g_proto = IPPROTO_GRE;
384 else
385 sc->g_proto = IPPROTO_MOBILE;
386 break;
387 case SIOCSIFMTU:
388 if (ifr->ifr_mtu < 576) {
389 error = EINVAL;
390 break;
391 }
392 ifp->if_mtu = ifr->ifr_mtu;
393 break;
394 case SIOCGIFMTU:
395 ifr->ifr_mtu = sc->sc_if.if_mtu;
396 break;
397 case SIOCADDMULTI:
398 case SIOCDELMULTI:
399 if (ifr == 0) {
400 error = EAFNOSUPPORT;
401 break;
402 }
403 switch (ifr->ifr_addr.sa_family) {
404 #ifdef INET
405 case AF_INET:
406 break;
407 #endif
408 #ifdef INET6
409 case AF_INET6:
410 break;
411 #endif
412 default:
413 error = EAFNOSUPPORT;
414 break;
415 }
416 break;
417 case GRESPROTO:
418 sc->g_proto = ifr->ifr_flags;
419 switch (sc->g_proto) {
420 case IPPROTO_GRE:
421 ifp->if_flags |= IFF_LINK0;
422 break;
423 case IPPROTO_MOBILE:
424 ifp->if_flags &= ~IFF_LINK0;
425 break;
426 default:
427 error = EPROTONOSUPPORT;
428 break;
429 }
430 break;
431 case GREGPROTO:
432 ifr->ifr_flags = sc->g_proto;
433 break;
434 case GRESADDRS:
435 case GRESADDRD:
436 /*
437 * set tunnel endpoints, compute a less specific route
438 * to the remote end and mark if as up
439 */
440 sa = &ifr->ifr_addr;
441 if (cmd == GRESADDRS)
442 sc->g_src = (satosin(sa))->sin_addr;
443 if (cmd == GRESADDRD)
444 sc->g_dst = (satosin(sa))->sin_addr;
445 recompute:
446 if ((sc->g_src.s_addr != INADDR_ANY) &&
447 (sc->g_dst.s_addr != INADDR_ANY)) {
448 if (sc->route.ro_rt != 0) /* free old route */
449 RTFREE(sc->route.ro_rt);
450 if (gre_compute_route(sc) == 0)
451 ifp->if_flags |= IFF_RUNNING;
452 else
453 ifp->if_flags &= ~IFF_RUNNING;
454 }
455 break;
456 case GREGADDRS:
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_src.s_addr;
461 sa = sintosa(&si);
462 ifr->ifr_addr = *sa;
463 break;
464 case GREGADDRD:
465 memset(&si, 0, sizeof(si));
466 si.sin_family = AF_INET;
467 si.sin_len = sizeof(struct sockaddr_in);
468 si.sin_addr.s_addr = sc->g_dst.s_addr;
469 sa = sintosa(&si);
470 ifr->ifr_addr = *sa;
471 break;
472 case SIOCSLIFPHYADDR:
473 if (lifr->addr.ss_family != AF_INET ||
474 lifr->dstaddr.ss_family != AF_INET) {
475 error = EAFNOSUPPORT;
476 break;
477 }
478 if (lifr->addr.ss_len != sizeof(si) ||
479 lifr->dstaddr.ss_len != sizeof(si)) {
480 error = EINVAL;
481 break;
482 }
483 sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
484 sc->g_dst =
485 (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
486 goto recompute;
487 case SIOCDIFPHYADDR:
488 sc->g_src.s_addr = INADDR_ANY;
489 sc->g_dst.s_addr = INADDR_ANY;
490 break;
491 case SIOCGLIFPHYADDR:
492 if (sc->g_src.s_addr == INADDR_ANY ||
493 sc->g_dst.s_addr == INADDR_ANY) {
494 error = EADDRNOTAVAIL;
495 break;
496 }
497 memset(&si, 0, sizeof(si));
498 si.sin_family = AF_INET;
499 si.sin_len = sizeof(struct sockaddr_in);
500 si.sin_addr.s_addr = sc->g_src.s_addr;
501 memcpy(&lifr->addr, &si, sizeof(si));
502 si.sin_addr.s_addr = sc->g_dst.s_addr;
503 memcpy(&lifr->dstaddr, &si, sizeof(si));
504 break;
505 default:
506 error = EINVAL;
507 break;
508 }
509
510 splx(s);
511 return (error);
512 }
513
514 /*
515 * computes a route to our destination that is not the one
516 * which would be taken by ip_output(), as this one will loop back to
517 * us. If the interface is p2p as a--->b, then a routing entry exists
518 * If we now send a packet to b (e.g. ping b), this will come down here
519 * gets src=a, dst=b tacked on and would from ip_output() sent back to
520 * if_gre.
521 * Goal here is to compute a route to b that is less specific than
522 * a-->b. We know that this one exists as in normal operation we have
523 * at least a default route which matches.
524 */
525 static int
526 gre_compute_route(struct gre_softc *sc)
527 {
528 struct route *ro;
529 u_int32_t a, b, c;
530
531 ro = &sc->route;
532
533 memset(ro, 0, sizeof(struct route));
534 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
535 ro->ro_dst.sa_family = AF_INET;
536 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
537
538 /*
539 * toggle last bit, so our interface is not found, but a less
540 * specific route. I'd rather like to specify a shorter mask,
541 * but this is not possible. Should work though. XXX
542 * there is a simpler way ...
543 */
544 if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
545 a = ntohl(sc->g_dst.s_addr);
546 b = a & 0x01;
547 c = a & 0xfffffffe;
548 b = b ^ 0x01;
549 a = b | c;
550 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
551 = htonl(a);
552 }
553
554 #ifdef DIAGNOSTIC
555 printf("%s: searching for a route to %s", sc->sc_if.if_xname,
556 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
557 #endif
558
559 rtalloc(ro);
560
561 /*
562 * check if this returned a route at all and this route is no
563 * recursion to ourself
564 */
565 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
566 #ifdef DIAGNOSTIC
567 if (ro->ro_rt == NULL)
568 printf(" - no route found!\n");
569 else
570 printf(" - route loops back to ourself!\n");
571 #endif
572 return EADDRNOTAVAIL;
573 }
574
575 /*
576 * now change it back - else ip_output will just drop
577 * the route and search one to this interface ...
578 */
579 if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
580 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
581
582 #ifdef DIAGNOSTIC
583 printf(", choosing %s with gateway %s", ro->ro_rt->rt_ifp->if_xname,
584 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
585 printf("\n");
586 #endif
587
588 return 0;
589 }
590
591 /*
592 * do a checksum of a buffer - much like in_cksum, which operates on
593 * mbufs.
594 */
595 u_int16_t
596 gre_in_cksum(u_int16_t *p, u_int len)
597 {
598 u_int32_t sum = 0;
599 int nwords = len >> 1;
600
601 while (nwords-- != 0)
602 sum += *p++;
603
604 if (len & 1) {
605 union {
606 u_short w;
607 u_char c[2];
608 } u;
609 u.c[0] = *(u_char *)p;
610 u.c[1] = 0;
611 sum += u.w;
612 }
613
614 /* end-around-carry */
615 sum = (sum >> 16) + (sum & 0xffff);
616 sum += (sum >> 16);
617 return (~sum);
618 }
619 #endif
620
621 void greattach(int);
622
623 /* ARGSUSED */
624 void
625 greattach(int count)
626 {
627 #ifdef INET
628 LIST_INIT(&gre_softc_list);
629 if_clone_attach(&gre_cloner);
630 #endif
631 }
632