if_gre.c revision 1.17.2.5 1 /* $NetBSD: if_gre.c,v 1.17.2.5 2002/02/28 04:15:00 nathanw 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.17.2.5 2002/02/28 04:15:00 nathanw 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/lwp.h>
59 #include <sys/proc.h>
60 #include <sys/protosw.h>
61 #include <sys/socket.h>
62 #include <sys/ioctl.h>
63 #include <sys/queue.h>
64 #if __NetBSD__
65 #include <sys/systm.h>
66 #endif
67
68 #include <machine/cpu.h>
69
70 #include <net/ethertypes.h>
71 #include <net/if.h>
72 #include <net/if_types.h>
73 #include <net/netisr.h>
74 #include <net/route.h>
75
76 #ifdef INET
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_var.h>
82 #else
83 #error "Huh? if_gre without inet?"
84 #endif
85
86 #ifdef NS
87 #include <netns/ns.h>
88 #include <netns/ns_if.h>
89 #endif
90
91 #ifdef NETATALK
92 #include <netatalk/at.h>
93 #include <netatalk/at_var.h>
94 #include <netatalk/at_extern.h>
95 #endif
96
97 #if NBPFILTER > 0
98 #include <sys/time.h>
99 #include <net/bpf.h>
100 #endif
101
102 #include <net/if_gre.h>
103
104 /*
105 * XXX this is below the standard MTU of
106 * 1500 Bytes, allowing for headers,
107 * but we should possibly do path mtu discovery
108 * before changing if state to up to find the
109 * correct value
110 */
111 #define GREMTU 1450
112 #define LINK_MASK (IFF_LINK0|IFF_LINK1|IFF_LINK2)
113
114 struct gre_softc_head gre_softc_list;
115 int ip_gre_ttl = GRE_TTL;
116
117 int gre_clone_create __P((struct if_clone *, int));
118 void gre_clone_destroy __P((struct ifnet *));
119
120 struct if_clone gre_cloner =
121 IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
122
123 int gre_compute_route(struct gre_softc *sc);
124
125 void greattach __P((int));
126
127 /* ARGSUSED */
128 void
129 greattach(count)
130 int count;
131 {
132
133 LIST_INIT(&gre_softc_list);
134 if_clone_attach(&gre_cloner);
135 }
136
137 int
138 gre_clone_create(ifc, unit)
139 struct if_clone *ifc;
140 int unit;
141 {
142 struct gre_softc *sc;
143
144 sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK);
145 memset(sc, 0, sizeof(struct gre_softc));
146
147 sprintf(sc->sc_if.if_xname, "%s%d", ifc->ifc_name, unit);
148 sc->sc_if.if_softc = sc;
149 sc->sc_if.if_type = IFT_OTHER;
150 sc->sc_if.if_addrlen = 4;
151 sc->sc_if.if_hdrlen = 24; /* IP + GRE */
152 sc->sc_if.if_dlt = DLT_NULL;
153 sc->sc_if.if_mtu = GREMTU;
154 sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
155 sc->sc_if.if_output = gre_output;
156 sc->sc_if.if_ioctl = gre_ioctl;
157 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
158 sc->g_proto = IPPROTO_GRE;
159 if_attach(&sc->sc_if);
160 if_alloc_sadl(&sc->sc_if);
161 #if NBPFILTER > 0
162 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
163 #endif
164 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
165 return (0);
166 }
167
168 void
169 gre_clone_destroy(ifp)
170 struct ifnet *ifp;
171 {
172 struct gre_softc *sc = ifp->if_softc;
173
174 LIST_REMOVE(sc, sc_list);
175 #if NBPFILTER > 0
176 bpfdetach(ifp);
177 #endif
178 if_detach(ifp);
179 free(sc, M_DEVBUF);
180 }
181
182 /*
183 * The output routine. Takes a packet and encapsulates it in the protocol
184 * given by sc->g_proto. See also RFC 1701 and RFC 2004
185 */
186 int
187 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
188 struct rtentry *rt)
189 {
190 int error = 0;
191 struct gre_softc *sc = ifp->if_softc;
192 struct greip *gh;
193 struct ip *inp;
194 u_char osrc;
195 u_short etype = 0;
196 struct mobile_h mob_h;
197
198 if ((ifp->if_flags & IFF_UP) == 0)
199 return ENETDOWN;
200
201 gh = NULL;
202 inp = NULL;
203 osrc = 0;
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 inp = mtod(m, struct ip *);
227
228 memset(&mob_h, 0, MOB_H_SIZ_L);
229 mob_h.proto = (inp->ip_p) << 8;
230 mob_h.odst = inp->ip_dst.s_addr;
231 inp->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(inp->ip_src, sc->g_src)) {
239 msiz = MOB_H_SIZ_S;
240 } else {
241 mob_h.proto |= MOB_H_SBIT;
242 mob_h.osrc = inp->ip_src.s_addr;
243 inp->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_short *)&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 return (ENOBUFS);
256 }
257 m0->m_next = m;
258 m->m_data += sizeof(struct ip);
259 m->m_len -= sizeof(struct ip);
260 m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
261 m0->m_len = msiz + sizeof(struct ip);
262 m0->m_data += max_linkhdr;
263 memcpy(mtod(m0, caddr_t), (caddr_t)inp,
264 sizeof(struct ip));
265 m = m0;
266 } else { /* we have some space left in the old one */
267 m->m_data -= msiz;
268 m->m_len += msiz;
269 m->m_pkthdr.len += msiz;
270 memmove(mtod(m, caddr_t), inp,
271 sizeof(struct ip));
272 }
273 inp=mtod(m, struct ip *);
274 memcpy((caddr_t)(inp + 1), &mob_h, (unsigned)msiz);
275 NTOHS(inp->ip_len);
276 inp->ip_len += msiz;
277 } else { /* AF_INET */
278 IF_DROP(&ifp->if_snd);
279 m_freem(m);
280 return (EINVAL);
281 }
282 } else if (sc->g_proto == IPPROTO_GRE) {
283 switch (dst->sa_family) {
284 case AF_INET:
285 inp = mtod(m, struct ip *);
286 etype = ETHERTYPE_IP;
287 break;
288 #ifdef NETATALK
289 case AF_APPLETALK:
290 etype = ETHERTYPE_ATALK;
291 break;
292 #endif
293 #ifdef NS
294 case AF_NS:
295 etype = ETHERTYPE_NS;
296 break;
297 #endif
298 default:
299 IF_DROP(&ifp->if_snd);
300 m_freem(m);
301 return (EAFNOSUPPORT);
302 }
303 M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
304 } else {
305 error = EINVAL;
306 IF_DROP(&ifp->if_snd);
307 m_freem(m);
308 return (error);
309 }
310
311 if (m == NULL) {
312 IF_DROP(&ifp->if_snd);
313 return (ENOBUFS);
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 = inp->ip_tos;
331 gh->gi_len = 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, NULL);
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->l_proc; /* XXX */
347 struct ifaddr *ifa = (struct ifaddr *)data;
348 struct ifreq *ifr = (struct ifreq *)data;
349 struct in_ifaddr *ia = (struct in_ifaddr *)data;
350 struct gre_softc *sc = ifp->if_softc;
351 int s;
352 struct sockaddr_in si;
353 struct sockaddr *sa = NULL;
354 int error;
355
356 error = 0;
357
358 s = splnet();
359 switch (cmd) {
360 case SIOCSIFADDR:
361 case SIOCSIFDSTADDR:
362 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
363 break;
364 /*
365 * set tunnel endpoints in case that we "only"
366 * have ip over ip encapsulation. This allows to
367 * set tunnel endpoints with ifconfig.
368 */
369 if (ifa->ifa_addr->sa_family == AF_INET) {
370 sa = ifa->ifa_addr;
371 sc->g_src = (satosin(sa))->sin_addr;
372 sc->g_dst = ia->ia_dstaddr.sin_addr;
373 if ((sc->g_src.s_addr != INADDR_ANY) &&
374 (sc->g_dst.s_addr != INADDR_ANY)) {
375 if (sc->route.ro_rt != 0) /* free old route */
376 RTFREE(sc->route.ro_rt);
377 if (gre_compute_route(sc) == 0)
378 ifp->if_flags |= IFF_UP;
379 }
380 }
381 break;
382 case SIOCSIFFLAGS:
383 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
384 break;
385 if ((sc->g_dst.s_addr == INADDR_ANY) ||
386 (sc->g_src.s_addr == INADDR_ANY))
387 ifp->if_flags &= ~IFF_UP;
388
389 switch (ifr->ifr_flags & LINK_MASK) {
390 case IFF_LINK0:
391 sc->g_proto = IPPROTO_GRE;
392 ifp->if_flags |= IFF_LINK0;
393 ifp->if_flags &= ~(IFF_LINK1|IFF_LINK2);
394 break;
395 case IFF_LINK2:
396 sc->g_proto = IPPROTO_MOBILE;
397 ifp->if_flags |= IFF_LINK2;
398 ifp->if_flags &= ~(IFF_LINK0|IFF_LINK1);
399 break;
400 }
401 break;
402 case SIOCSIFMTU:
403 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
404 break;
405 if (ifr->ifr_mtu > GREMTU || ifr->ifr_mtu < 576) {
406 error = EINVAL;
407 break;
408 }
409 ifp->if_mtu = ifr->ifr_mtu;
410 break;
411 case SIOCGIFMTU:
412 ifr->ifr_mtu = sc->sc_if.if_mtu;
413 break;
414 case SIOCADDMULTI:
415 case SIOCDELMULTI:
416 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
417 break;
418 if (ifr == 0) {
419 error = EAFNOSUPPORT;
420 break;
421 }
422 switch (ifr->ifr_addr.sa_family) {
423 #ifdef INET
424 case AF_INET:
425 break;
426 #endif
427 default:
428 error = EAFNOSUPPORT;
429 break;
430 }
431 break;
432 case GRESPROTO:
433 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
434 break;
435 sc->g_proto = ifr->ifr_flags;
436 switch (sc->g_proto) {
437 case IPPROTO_GRE :
438 ifp->if_flags |= IFF_LINK0;
439 ifp->if_flags &= ~(IFF_LINK1|IFF_LINK2);
440 break;
441 case IPPROTO_MOBILE :
442 ifp->if_flags |= IFF_LINK2;
443 ifp->if_flags &= ~(IFF_LINK1|IFF_LINK2);
444 break;
445 default:
446 ifp->if_flags &= ~(IFF_LINK0|IFF_LINK1|IFF_LINK2);
447 }
448 break;
449 case GREGPROTO:
450 ifr->ifr_flags = sc->g_proto;
451 break;
452 case GRESADDRS:
453 case GRESADDRD:
454 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
455 break;
456 /*
457 * set tunnel endpoints, compute a less specific route
458 * to the remote end and mark if as up
459 */
460 sa = &ifr->ifr_addr;
461 if (cmd == GRESADDRS )
462 sc->g_src = (satosin(sa))->sin_addr;
463 if (cmd == GRESADDRD )
464 sc->g_dst = (satosin(sa))->sin_addr;
465 if ((sc->g_src.s_addr != INADDR_ANY) &&
466 (sc->g_dst.s_addr != INADDR_ANY)) {
467 if (sc->route.ro_rt != 0) /* free old route */
468 RTFREE(sc->route.ro_rt);
469 if (gre_compute_route(sc) == 0)
470 ifp->if_flags |= IFF_UP;
471 }
472 break;
473 case GREGADDRS:
474 si.sin_addr.s_addr = sc->g_src.s_addr;
475 sa = sintosa(&si);
476 ifr->ifr_addr = *sa;
477 break;
478 case GREGADDRD:
479 si.sin_addr.s_addr = sc->g_dst.s_addr;
480 sa = sintosa(&si);
481 ifr->ifr_addr = *sa;
482 break;
483 default:
484 error = EINVAL;
485 }
486
487 splx(s);
488 return (error);
489 }
490
491 /*
492 * computes a route to our destination that is not the one
493 * which would be taken by ip_output(), as this one will loop back to
494 * us. If the interface is p2p as a--->b, then a routing entry exists
495 * If we now send a packet to b (e.g. ping b), this will come down here
496 * gets src=a, dst=b tacked on and would from ip_ouput() sent back to
497 * if_gre.
498 * Goal here is to compute a route to b that is less specific than
499 * a-->b. We know that this one exists as in normal operation we have
500 * at least a default route which matches.
501 */
502 int
503 gre_compute_route(struct gre_softc *sc)
504 {
505 struct route *ro;
506 u_int32_t a, b, c;
507
508 ro = &sc->route;
509
510 memset(ro, 0, sizeof(struct route));
511 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
512 ro->ro_dst.sa_family = AF_INET;
513 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
514
515 /*
516 * toggle last bit, so our interface is not found, but a less
517 * specific route. I'd rather like to specify a shorter mask,
518 * but this is not possible. Should work though. XXX
519 * there is a simpler way ...
520 */
521 if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
522 a = ntohl(sc->g_dst.s_addr);
523 b = a & 0x01;
524 c = a & 0xfffffffe;
525 b = b ^ 0x01;
526 a = b | c;
527 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
528 = htonl(a);
529 }
530
531 #ifdef DIAGNOSTIC
532 printf("%s: searching a route to %s", sc->sc_if.if_xname,
533 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
534 #endif
535
536 rtalloc(ro);
537
538 /*
539 * check if this returned a route at all and this route is no
540 * recursion to ourself
541 */
542 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
543 #ifdef DIAGNOSTIC
544 if (ro->ro_rt == NULL)
545 printf(" - no route found!\n");
546 else
547 printf(" - route loops back to ourself!\n");
548 #endif
549 return EADDRNOTAVAIL;
550 }
551
552 /*
553 * now change it back - else ip_output will just drop
554 * the route and search one to this interface ...
555 */
556 if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
557 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
558
559 #ifdef DIAGNOSTIC
560 printf(", choosing %s with gateway %s", ro->ro_rt->rt_ifp->if_xname,
561 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
562 printf("\n");
563 #endif
564
565 return 0;
566 }
567
568 /*
569 * do a checksum of a buffer - much like in_cksum, which operates on
570 * mbufs.
571 */
572 u_short
573 gre_in_cksum(u_short *p, u_int len)
574 {
575 u_int sum = 0;
576 int nwords = len >> 1;
577
578 while (nwords-- != 0)
579 sum += *p++;
580
581 if (len & 1) {
582 union {
583 u_short w;
584 u_char c[2];
585 } u;
586 u.c[0] = *(u_char *)p;
587 u.c[1] = 0;
588 sum += u.w;
589 }
590
591 /* end-around-carry */
592 sum = (sum >> 16) + (sum & 0xffff);
593 sum += (sum >> 16);
594 return (~sum);
595 }
596