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