if_gre.c revision 1.22 1 /* $NetBSD: if_gre.c,v 1.22 2001/11/12 23:49:39 lukem 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.22 2001/11/12 23:49:39 lukem Exp $");
50
51 #include "gre.h"
52 #if NGRE > 0
53
54 #include "opt_inet.h"
55 #include "opt_ns.h"
56 #include "bpfilter.h"
57
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 * XXX this is below the standard MTU of
108 * 1500 Bytes, allowing for headers,
109 * but we should possibly do path mtu discovery
110 * before changing if state to up to find the
111 * correct value
112 */
113 #define GREMTU 1450
114 #define LINK_MASK (IFF_LINK0|IFF_LINK1|IFF_LINK2)
115
116 struct gre_softc_head gre_softc_list;
117
118 int gre_clone_create __P((struct if_clone *, int));
119 void gre_clone_destroy __P((struct ifnet *));
120
121 struct if_clone gre_cloner =
122 IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
123
124 void gre_compute_route(struct gre_softc *sc);
125
126 void greattach __P((int));
127
128 /* ARGSUSED */
129 void
130 greattach(count)
131 int count;
132 {
133
134 LIST_INIT(&gre_softc_list);
135 if_clone_attach(&gre_cloner);
136 }
137
138 int
139 gre_clone_create(ifc, unit)
140 struct if_clone *ifc;
141 int unit;
142 {
143 struct gre_softc *sc;
144
145 sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK);
146 memset(sc, 0, sizeof(struct gre_softc));
147
148 sprintf(sc->sc_if.if_xname, "%s%d", ifc->ifc_name, unit);
149 sc->sc_if.if_softc = sc;
150 sc->sc_if.if_type = IFT_OTHER;
151 sc->sc_if.if_addrlen = 4;
152 sc->sc_if.if_hdrlen = 24; /* IP + GRE */
153 sc->sc_if.if_dlt = DLT_NULL;
154 sc->sc_if.if_mtu = GREMTU;
155 sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
156 sc->sc_if.if_output = gre_output;
157 sc->sc_if.if_ioctl = gre_ioctl;
158 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
159 sc->g_proto = IPPROTO_GRE;
160 if_attach(&sc->sc_if);
161 if_alloc_sadl(&sc->sc_if);
162 #if NBPFILTER > 0
163 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
164 #endif
165 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
166 return (0);
167 }
168
169 void
170 gre_clone_destroy(ifp)
171 struct ifnet *ifp;
172 {
173 struct gre_softc *sc = ifp->if_softc;
174
175 LIST_REMOVE(sc, sc_list);
176 #if NBPFILTER > 0
177 bpfdetach(ifp);
178 #endif
179 if_detach(ifp);
180 free(sc, M_DEVBUF);
181 }
182
183 /*
184 * The output routine. Takes a packet and encapsulates it in the protocol
185 * given by sc->g_proto. See also RFC 1701 and RFC 2004
186 */
187 int
188 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
189 struct rtentry *rt)
190 {
191 int error = 0;
192 struct gre_softc *sc = ifp->if_softc;
193 struct greip *gh;
194 struct ip *inp;
195 u_char ttl, osrc;
196 u_short etype = 0;
197 struct mobile_h mob_h;
198
199 gh = NULL;
200 inp = NULL;
201 osrc = 0;
202
203 #if NBPFILTER >0
204 if (ifp->if_bpf) {
205 /* see comment of other if_foo.c files */
206 struct mbuf m0;
207 u_int32_t af = dst->sa_family;
208
209 m0.m_next = m;
210 m0.m_len = 4;
211 m0.m_data = (char *)⁡
212
213 bpf_mtap(ifp->if_bpf, &m0);
214 }
215 #endif
216
217 ttl = 255;
218
219 if (sc->g_proto == IPPROTO_MOBILE) {
220 if (dst->sa_family == AF_INET) {
221 struct mbuf *m0;
222 int msiz;
223
224 inp = mtod(m, struct ip *);
225
226 memset(&mob_h, 0, MOB_H_SIZ_L);
227 mob_h.proto = (inp->ip_p) << 8;
228 mob_h.odst = inp->ip_dst.s_addr;
229 inp->ip_dst.s_addr = sc->g_dst.s_addr;
230
231 /*
232 * If the packet comes from our host, we only change
233 * the destination address in the IP header.
234 * Else we also need to save and change the source
235 */
236 if (in_hosteq(inp->ip_src, sc->g_src)) {
237 msiz = MOB_H_SIZ_S;
238 } else {
239 mob_h.proto |= MOB_H_SBIT;
240 mob_h.osrc = inp->ip_src.s_addr;
241 inp->ip_src.s_addr = sc->g_src.s_addr;
242 msiz = MOB_H_SIZ_L;
243 }
244 HTONS(mob_h.proto);
245 mob_h.hcrc = gre_in_cksum((u_short *)&mob_h, msiz);
246
247 if ((m->m_data - msiz) < m->m_pktdat) {
248 /* need new mbuf */
249 MGETHDR(m0, M_DONTWAIT, MT_HEADER);
250 if (m0 == NULL) {
251 IF_DROP(&ifp->if_snd);
252 m_freem(m);
253 return (ENOBUFS);
254 }
255 m0->m_next = m;
256 m->m_data += sizeof(struct ip);
257 m->m_len -= sizeof(struct ip);
258 m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
259 m0->m_len = msiz + sizeof(struct ip);
260 m0->m_data += max_linkhdr;
261 memcpy(mtod(m0, caddr_t), (caddr_t)inp,
262 sizeof(struct ip));
263 m = m0;
264 } else { /* we have some space left in the old one */
265 m->m_data -= msiz;
266 m->m_len += msiz;
267 m->m_pkthdr.len += msiz;
268 memmove(mtod(m, caddr_t), inp,
269 sizeof(struct ip));
270 }
271 inp=mtod(m, struct ip *);
272 memcpy((caddr_t)(inp + 1), &mob_h, (unsigned)msiz);
273 NTOHS(inp->ip_len);
274 inp->ip_len += msiz;
275 } else { /* AF_INET */
276 IF_DROP(&ifp->if_snd);
277 m_freem(m);
278 return (EINVAL);
279 }
280 } else if (sc->g_proto == IPPROTO_GRE) {
281 switch (dst->sa_family) {
282 case AF_INET:
283 inp = mtod(m, struct ip *);
284 ttl = inp->ip_ttl;
285 etype = ETHERTYPE_IP;
286 break;
287 #ifdef NETATALK
288 case AF_APPLETALK:
289 etype = ETHERTYPE_ATALK;
290 break;
291 #endif
292 #ifdef NS
293 case AF_NS:
294 etype = ETHERTYPE_NS;
295 break;
296 #endif
297 default:
298 IF_DROP(&ifp->if_snd);
299 m_freem(m);
300 return (EAFNOSUPPORT);
301 }
302 M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
303 } else {
304 error = EINVAL;
305 IF_DROP(&ifp->if_snd);
306 m_freem(m);
307 return (error);
308 }
309
310 if (m == NULL) {
311 IF_DROP(&ifp->if_snd);
312 return (ENOBUFS);
313 }
314
315 gh = mtod(m, struct greip *);
316 if (sc->g_proto == IPPROTO_GRE) {
317 /* we don't have any GRE flags for now */
318
319 memset((void *)&gh->gi_g, 0, sizeof(struct gre_h));
320 gh->gi_ptype = htons(etype);
321 }
322
323 gh->gi_pr = sc->g_proto;
324 if (sc->g_proto != IPPROTO_MOBILE) {
325 gh->gi_src = sc->g_src;
326 gh->gi_dst = sc->g_dst;
327 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
328 ((struct ip*)gh)->ip_ttl = ttl;
329 ((struct ip*)gh)->ip_tos = inp->ip_tos;
330 gh->gi_len = m->m_pkthdr.len;
331 }
332
333 ifp->if_opackets++;
334 ifp->if_obytes += m->m_pkthdr.len;
335 /* send it off */
336 error = ip_output(m, NULL, &sc->route, 0, NULL);
337 if (error)
338 ifp->if_oerrors++;
339 return (error);
340 }
341
342 int
343 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
344 {
345 struct proc *p = curproc; /* XXX */
346 struct ifaddr *ifa = (struct ifaddr *)data;
347 struct ifreq *ifr = (struct ifreq *)data;
348 struct in_ifaddr *ia = (struct in_ifaddr *)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 case SIOCSIFDSTADDR:
361 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
362 break;
363 /*
364 * set tunnel endpoints in case that we "only"
365 * have ip over ip encapsulation. This allows to
366 * set tunnel endpoints with ifconfig.
367 */
368 if (ifa->ifa_addr->sa_family == AF_INET) {
369 sa = ifa->ifa_addr;
370 sc->g_src = (satosin(sa))->sin_addr;
371 sc->g_dst = ia->ia_dstaddr.sin_addr;
372 if ((sc->g_src.s_addr != INADDR_ANY) &&
373 (sc->g_dst.s_addr != INADDR_ANY)) {
374 if (sc->route.ro_rt != 0) /* free old route */
375 RTFREE(sc->route.ro_rt);
376 gre_compute_route(sc);
377 ifp->if_flags |= IFF_UP;
378 }
379 }
380 break;
381 case SIOCSIFFLAGS:
382 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
383 break;
384 if ((sc->g_dst.s_addr == INADDR_ANY) ||
385 (sc->g_src.s_addr == INADDR_ANY))
386 ifp->if_flags &= ~IFF_UP;
387
388 switch (ifr->ifr_flags & LINK_MASK) {
389 case IFF_LINK0:
390 sc->g_proto = IPPROTO_GRE;
391 ifp->if_flags |= IFF_LINK0;
392 ifp->if_flags &= ~(IFF_LINK1|IFF_LINK2);
393 break;
394 case IFF_LINK2:
395 sc->g_proto = IPPROTO_MOBILE;
396 ifp->if_flags |= IFF_LINK2;
397 ifp->if_flags &= ~(IFF_LINK0|IFF_LINK1);
398 break;
399 }
400 break;
401 case SIOCSIFMTU:
402 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
403 break;
404 if (ifr->ifr_mtu > GREMTU || ifr->ifr_mtu < 576) {
405 error = EINVAL;
406 break;
407 }
408 ifp->if_mtu = ifr->ifr_mtu;
409 break;
410 case SIOCGIFMTU:
411 ifr->ifr_mtu = sc->sc_if.if_mtu;
412 break;
413 case SIOCADDMULTI:
414 case SIOCDELMULTI:
415 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
416 break;
417 if (ifr == 0) {
418 error = EAFNOSUPPORT;
419 break;
420 }
421 switch (ifr->ifr_addr.sa_family) {
422 #ifdef INET
423 case AF_INET:
424 break;
425 #endif
426 default:
427 error = EAFNOSUPPORT;
428 break;
429 }
430 break;
431 case GRESPROTO:
432 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
433 break;
434 sc->g_proto = ifr->ifr_flags;
435 switch (sc->g_proto) {
436 case IPPROTO_GRE :
437 ifp->if_flags |= IFF_LINK0;
438 ifp->if_flags &= ~(IFF_LINK1|IFF_LINK2);
439 break;
440 case IPPROTO_MOBILE :
441 ifp->if_flags |= IFF_LINK2;
442 ifp->if_flags &= ~(IFF_LINK1|IFF_LINK2);
443 break;
444 default:
445 ifp->if_flags &= ~(IFF_LINK0|IFF_LINK1|IFF_LINK2);
446 }
447 break;
448 case GREGPROTO:
449 ifr->ifr_flags = sc->g_proto;
450 break;
451 case GRESADDRS:
452 case GRESADDRD:
453 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
454 break;
455 /*
456 * set tunnel endpoints, compute a less specific route
457 * to the remote end and mark if as up
458 */
459 sa = &ifr->ifr_addr;
460 if (cmd == GRESADDRS )
461 sc->g_src = (satosin(sa))->sin_addr;
462 if (cmd == GRESADDRD )
463 sc->g_dst = (satosin(sa))->sin_addr;
464 if ((sc->g_src.s_addr != INADDR_ANY) &&
465 (sc->g_dst.s_addr != INADDR_ANY)) {
466 if (sc->route.ro_rt != 0) /* free old route */
467 RTFREE(sc->route.ro_rt);
468 gre_compute_route(sc);
469 ifp->if_flags |= IFF_UP;
470 }
471 break;
472 case GREGADDRS:
473 si.sin_addr.s_addr = sc->g_src.s_addr;
474 sa = sintosa(&si);
475 ifr->ifr_addr = *sa;
476 break;
477 case GREGADDRD:
478 si.sin_addr.s_addr = sc->g_dst.s_addr;
479 sa = sintosa(&si);
480 ifr->ifr_addr = *sa;
481 break;
482 default:
483 error = EINVAL;
484 }
485
486 splx(s);
487 return (error);
488 }
489
490 /*
491 * computes a route to our destination that is not the one
492 * which would be taken by ip_output(), as this one will loop back to
493 * us. If the interface is p2p as a--->b, then a routing entry exists
494 * If we now send a packet to b (e.g. ping b), this will come down here
495 * gets src=a, dst=b tacked on and would from ip_ouput() sent back to
496 * if_gre.
497 * Goal here is to compute a route to b that is less specific than
498 * a-->b. We know that this one exists as in normal operation we have
499 * at least a default route which matches.
500 */
501 void
502 gre_compute_route(struct gre_softc *sc)
503 {
504 struct route *ro;
505 u_int32_t a, b, c;
506
507 ro = &sc->route;
508
509 memset(ro, 0, sizeof(struct route));
510 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
511 ro->ro_dst.sa_family = AF_INET;
512 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
513
514 /*
515 * toggle last bit, so our interface is not found, but a less
516 * specific route. I'd rather like to specify a shorter mask,
517 * but this is not possible. Should work though. XXX
518 * there is a simpler way ...
519 */
520 if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
521 a = ntohl(sc->g_dst.s_addr);
522 b = a & 0x01;
523 c = a & 0xfffffffe;
524 b = b ^ 0x01;
525 a = b | c;
526 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
527 = htonl(a);
528 }
529
530 #ifdef DIAGNOSTIC
531 printf("%s: searching a route to %s", sc->sc_if.if_xname,
532 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
533 #endif
534
535 rtalloc(ro);
536
537 /*
538 * now change it back - else ip_output will just drop
539 * the route and search one to this interface ...
540 */
541 if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
542 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
543
544 #ifdef DIAGNOSTIC
545 printf(", choosing %s with gateway %s", ro->ro_rt->rt_ifp->if_xname,
546 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
547 printf("\n");
548 #endif
549 }
550
551 /*
552 * do a checksum of a buffer - much like in_cksum, which operates on
553 * mbufs.
554 */
555 u_short
556 gre_in_cksum(u_short *p, u_int len)
557 {
558 u_int sum = 0;
559 int nwords = len >> 1;
560
561 while (nwords-- != 0)
562 sum += *p++;
563
564 if (len & 1) {
565 union {
566 u_short w;
567 u_char c[2];
568 } u;
569 u.c[0] = *(u_char *)p;
570 u.c[1] = 0;
571 sum += u.w;
572 }
573
574 /* end-around-carry */
575 sum = (sum >> 16) + (sum & 0xffff);
576 sum += (sum >> 16);
577 return (~sum);
578 }
579 #endif
580