if_gre.c revision 1.54.4.1 1 /* $NetBSD: if_gre.c,v 1.54.4.1 2005/04/29 11:29:31 kent 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.54.4.1 2005/04/29 11:29:31 kent 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 int gre_clone_create __P((struct if_clone *, int));
117 int gre_clone_destroy __P((struct ifnet *));
118
119 struct if_clone gre_cloner =
120 IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
121
122 int gre_compute_route(struct gre_softc *sc);
123
124 int
125 gre_clone_create(ifc, unit)
126 struct if_clone *ifc;
127 int unit;
128 {
129 struct gre_softc *sc;
130
131 sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK);
132 memset(sc, 0, sizeof(struct gre_softc));
133
134 snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d",
135 ifc->ifc_name, unit);
136 sc->sc_if.if_softc = sc;
137 sc->sc_if.if_type = IFT_TUNNEL;
138 sc->sc_if.if_addrlen = 0;
139 sc->sc_if.if_hdrlen = 24; /* IP + GRE */
140 sc->sc_if.if_dlt = DLT_NULL;
141 sc->sc_if.if_mtu = GREMTU;
142 sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
143 sc->sc_if.if_output = gre_output;
144 sc->sc_if.if_ioctl = gre_ioctl;
145 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
146 sc->g_proto = IPPROTO_GRE;
147 sc->sc_if.if_flags |= IFF_LINK0;
148 if_attach(&sc->sc_if);
149 if_alloc_sadl(&sc->sc_if);
150 #if NBPFILTER > 0
151 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
152 #endif
153 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
154 return (0);
155 }
156
157 int
158 gre_clone_destroy(ifp)
159 struct ifnet *ifp;
160 {
161 struct gre_softc *sc = ifp->if_softc;
162
163 LIST_REMOVE(sc, sc_list);
164 #if NBPFILTER > 0
165 bpfdetach(ifp);
166 #endif
167 if_detach(ifp);
168 free(sc, M_DEVBUF);
169
170 return (0);
171 }
172
173 /*
174 * The output routine. Takes a packet and encapsulates it in the protocol
175 * given by sc->g_proto. See also RFC 1701 and RFC 2004
176 */
177 int
178 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
179 struct rtentry *rt)
180 {
181 int error = 0;
182 struct gre_softc *sc = ifp->if_softc;
183 struct greip *gh;
184 struct ip *ip;
185 u_int8_t ip_tos = 0;
186 u_int16_t etype = 0;
187 struct mobile_h mob_h;
188
189 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
190 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
191 m_freem(m);
192 error = ENETDOWN;
193 goto end;
194 }
195
196 gh = NULL;
197 ip = NULL;
198
199 #if NBPFILTER >0
200 if (ifp->if_bpf)
201 bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
202 #endif
203
204 m->m_flags &= ~(M_BCAST|M_MCAST);
205
206 if (sc->g_proto == IPPROTO_MOBILE) {
207 if (dst->sa_family == AF_INET) {
208 struct mbuf *m0;
209 int msiz;
210
211 ip = mtod(m, struct ip *);
212
213 memset(&mob_h, 0, MOB_H_SIZ_L);
214 mob_h.proto = (ip->ip_p) << 8;
215 mob_h.odst = ip->ip_dst.s_addr;
216 ip->ip_dst.s_addr = sc->g_dst.s_addr;
217
218 /*
219 * If the packet comes from our host, we only change
220 * the destination address in the IP header.
221 * Else we also need to save and change the source
222 */
223 if (in_hosteq(ip->ip_src, sc->g_src)) {
224 msiz = MOB_H_SIZ_S;
225 } else {
226 mob_h.proto |= MOB_H_SBIT;
227 mob_h.osrc = ip->ip_src.s_addr;
228 ip->ip_src.s_addr = sc->g_src.s_addr;
229 msiz = MOB_H_SIZ_L;
230 }
231 HTONS(mob_h.proto);
232 mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
233
234 if ((m->m_data - msiz) < m->m_pktdat) {
235 /* need new mbuf */
236 MGETHDR(m0, M_DONTWAIT, MT_HEADER);
237 if (m0 == NULL) {
238 IF_DROP(&ifp->if_snd);
239 m_freem(m);
240 error = ENOBUFS;
241 goto end;
242 }
243 m0->m_next = m;
244 m->m_data += sizeof(struct ip);
245 m->m_len -= sizeof(struct ip);
246 m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
247 m0->m_len = msiz + sizeof(struct ip);
248 m0->m_data += max_linkhdr;
249 memcpy(mtod(m0, caddr_t), (caddr_t)ip,
250 sizeof(struct ip));
251 m = m0;
252 } else { /* we have some space left in the old one */
253 m->m_data -= msiz;
254 m->m_len += msiz;
255 m->m_pkthdr.len += msiz;
256 memmove(mtod(m, caddr_t), ip,
257 sizeof(struct ip));
258 }
259 ip = mtod(m, struct ip *);
260 memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
261 ip->ip_len = htons(ntohs(ip->ip_len) + msiz);
262 } else { /* AF_INET */
263 IF_DROP(&ifp->if_snd);
264 m_freem(m);
265 error = EINVAL;
266 goto end;
267 }
268 } else if (sc->g_proto == IPPROTO_GRE) {
269 #ifdef GRE_DEBUG
270 printf( "start gre_output/GRE, dst->sa_family=%d\n",
271 dst->sa_family );
272 #endif
273 switch (dst->sa_family) {
274 case AF_INET:
275 ip = mtod(m, struct ip *);
276 ip_tos = ip->ip_tos;
277 etype = ETHERTYPE_IP;
278 break;
279 #ifdef NETATALK
280 case AF_APPLETALK:
281 etype = ETHERTYPE_ATALK;
282 break;
283 #endif
284 #ifdef NS
285 case AF_NS:
286 etype = ETHERTYPE_NS;
287 break;
288 #endif
289 #ifdef INET6
290 case AF_INET6:
291 etype = ETHERTYPE_IPV6;
292 break;
293 #endif
294 default:
295 IF_DROP(&ifp->if_snd);
296 m_freem(m);
297 error = EAFNOSUPPORT;
298 goto end;
299 }
300 M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
301 } else {
302 IF_DROP(&ifp->if_snd);
303 m_freem(m);
304 error = EINVAL;
305 goto end;
306 }
307
308 if (m == NULL) { /* impossible */
309 IF_DROP(&ifp->if_snd);
310 error = ENOBUFS;
311 goto end;
312 }
313
314 gh = mtod(m, struct greip *);
315 if (sc->g_proto == IPPROTO_GRE) {
316 /* we don't have any GRE flags for now */
317
318 memset((void *)&gh->gi_g, 0, sizeof(struct gre_h));
319 gh->gi_ptype = htons(etype);
320 }
321
322 gh->gi_pr = sc->g_proto;
323 if (sc->g_proto != IPPROTO_MOBILE) {
324 gh->gi_src = sc->g_src;
325 gh->gi_dst = sc->g_dst;
326 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
327 ((struct ip*)gh)->ip_ttl = ip_gre_ttl;
328 ((struct ip*)gh)->ip_tos = ip_tos;
329 gh->gi_len = htons(m->m_pkthdr.len);
330 }
331
332 ifp->if_opackets++;
333 ifp->if_obytes += m->m_pkthdr.len;
334 /* send it off */
335 error = ip_output(m, NULL, &sc->route, 0,
336 (struct ip_moptions *)NULL, (struct socket *)NULL);
337 end:
338 if (error)
339 ifp->if_oerrors++;
340 return (error);
341 }
342
343 int
344 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
345 {
346 struct proc *p = curproc; /* XXX */
347 struct ifreq *ifr = (struct ifreq *)data;
348 struct if_laddrreq *lifr = (struct if_laddrreq *)data;
349 struct gre_softc *sc = ifp->if_softc;
350 int s;
351 struct sockaddr_in si;
352 struct sockaddr *sa = NULL;
353 int error;
354
355 error = 0;
356
357 s = splnet();
358 switch (cmd) {
359 case SIOCSIFADDR:
360 ifp->if_flags |= IFF_UP;
361 break;
362 case SIOCSIFDSTADDR:
363 break;
364 case SIOCSIFFLAGS:
365 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
366 break;
367 if ((ifr->ifr_flags & IFF_LINK0) != 0)
368 sc->g_proto = IPPROTO_GRE;
369 else
370 sc->g_proto = IPPROTO_MOBILE;
371 break;
372 case SIOCSIFMTU:
373 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
374 break;
375 if (ifr->ifr_mtu < 576) {
376 error = EINVAL;
377 break;
378 }
379 ifp->if_mtu = ifr->ifr_mtu;
380 break;
381 case SIOCGIFMTU:
382 ifr->ifr_mtu = sc->sc_if.if_mtu;
383 break;
384 case SIOCADDMULTI:
385 case SIOCDELMULTI:
386 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
387 break;
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 = suser(p->p_ucred, &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 = suser(p->p_ucred, &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 = suser(p->p_ucred, &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 = suser(p->p_ucred, &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 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 __P((int));
619
620 /* ARGSUSED */
621 void
622 greattach(count)
623 int count;
624 {
625 #ifdef INET
626 LIST_INIT(&gre_softc_list);
627 if_clone_attach(&gre_cloner);
628 #endif
629 }
630