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