1 /* $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Heiko W.Rupp <hwr@pilhuhn.de>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
41 * Encapsulate L3 protocols into IP
42 * See RFC 1701 and 1702 for more details.
43 * If_gre is compatible with Cisco GRE tunnels, so you can
44 * have a NetBSD box as the other end of a tunnel interface of a Cisco
45 * router. See gre(4) for more details.
46 * Also supported: IP in IP encaps (proto 55) as of RFC 2004
49 #include "opt_atalk.h"
51 #include "opt_inet6.h"
53 #include <sys/param.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/module.h>
58 #include <sys/protosw.h>
59 #include <sys/socket.h>
60 #include <sys/sockio.h>
61 #include <sys/sysctl.h>
62 #include <sys/systm.h>
64 #include <net/ethernet.h>
66 #include <net/if_types.h>
67 #include <net/route.h>
70 #include <netinet/in.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/in_var.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_gre.h>
75 #include <netinet/ip_var.h>
76 #include <netinet/ip_encap.h>
78 #error "Huh? if_gre without inet?"
83 #include <net/net_osdep.h>
84 #include <net/if_gre.h>
87 * It is not easy to calculate the right value for a GRE MTU.
88 * We leave this task to the admin and use the same default that
96 * gre_mtx protects all global variables in if_gre.c.
97 * XXX: gre_softc data not protected yet.
100 static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
102 struct gre_softc_head gre_softc_list;
104 static int gre_clone_create(struct if_clone *, int);
105 static void gre_clone_destroy(struct ifnet *);
106 static int gre_ioctl(struct ifnet *, u_long, caddr_t);
107 static int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
110 static struct if_clone gre_cloner =
111 IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy, 0, IF_MAXUNIT);
113 static int gre_compute_route(struct gre_softc *sc);
115 static void greattach(void);
118 extern struct domain inetdomain;
119 static const struct protosw in_gre_protosw =
120 { SOCK_RAW, &inetdomain, IPPROTO_GRE, PR_ATOMIC|PR_ADDR,
121 (pr_input_t*)gre_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
126 static const struct protosw in_mobile_protosw =
127 { SOCK_RAW, &inetdomain, IPPROTO_MOBILE, PR_ATOMIC|PR_ADDR,
128 (pr_input_t*)gre_mobile_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
135 SYSCTL_DECL(_net_link);
136 SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
137 "Generic Routing Encapsulation");
140 * This macro controls the default upper limitation on nesting of gre tunnels.
141 * Since, setting a large value to this macro with a careless configuration
142 * may introduce system crash, we don't allow any nestings by default.
143 * If you need to configure nested gre tunnels, you can define this macro
144 * in your kernel configuration file. However, if you do so, please be
145 * careful to configure the tunnels so that it won't make a loop.
147 #define MAX_GRE_NEST 1
149 static int max_gre_nesting = MAX_GRE_NEST;
150 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
151 &max_gre_nesting, 0, "Max nested tunnels");
158 mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
159 LIST_INIT(&gre_softc_list);
160 if_clone_attach(&gre_cloner);
164 gre_clone_create(ifc, unit)
165 struct if_clone *ifc;
168 struct gre_softc *sc;
170 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK);
171 memset(sc, 0, sizeof(struct gre_softc));
173 if_initname(&sc->sc_if, ifc->ifc_name, unit);
174 sc->sc_if.if_softc = sc;
175 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
176 sc->sc_if.if_type = IFT_TUNNEL;
177 sc->sc_if.if_addrlen = 0;
178 sc->sc_if.if_hdrlen = 24; /* IP + GRE */
179 sc->sc_if.if_mtu = GREMTU;
180 sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
181 sc->sc_if.if_output = gre_output;
182 sc->sc_if.if_ioctl = gre_ioctl;
183 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
184 sc->g_proto = IPPROTO_GRE;
185 sc->sc_if.if_flags |= IFF_LINK0;
188 sc->wccp_ver = WCCP_V1;
189 if_attach(&sc->sc_if);
190 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
192 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
193 mtx_unlock(&gre_mtx);
198 gre_destroy(struct gre_softc *sc)
202 if (sc->encap != NULL)
203 encap_detach(sc->encap);
205 bpfdetach(&sc->sc_if);
206 if_detach(&sc->sc_if);
211 gre_clone_destroy(ifp)
214 struct gre_softc *sc = ifp->if_softc;
217 LIST_REMOVE(sc, sc_list);
218 mtx_unlock(&gre_mtx);
223 * The output routine. Takes a packet and encapsulates it in the protocol
224 * given by sc->g_proto. See also RFC 1701 and RFC 2004
227 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
231 struct gre_softc *sc = ifp->if_softc;
235 struct mobile_h mob_h;
238 * gre may cause infinite recursion calls when misconfigured.
239 * We'll prevent this by introducing upper limit.
241 if (++(sc->called) > max_gre_nesting) {
242 printf("%s: gre_output: recursively called too many "
243 "times(%d)\n", if_name(&sc->sc_if), sc->called);
245 error = EIO; /* is there better errno? */
249 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
250 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
260 u_int32_t af = dst->sa_family;
261 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
264 m->m_flags &= ~(M_BCAST|M_MCAST);
266 if (sc->g_proto == IPPROTO_MOBILE) {
267 if (dst->sa_family == AF_INET) {
271 ip = mtod(m, struct ip *);
274 * RFC2004 specifies that fragmented diagrams shouldn't
277 if ((ip->ip_off & IP_MF) != 0) {
278 _IF_DROP(&ifp->if_snd);
280 error = EINVAL; /* is there better errno? */
283 memset(&mob_h, 0, MOB_H_SIZ_L);
284 mob_h.proto = (ip->ip_p) << 8;
285 mob_h.odst = ip->ip_dst.s_addr;
286 ip->ip_dst.s_addr = sc->g_dst.s_addr;
289 * If the packet comes from our host, we only change
290 * the destination address in the IP header.
291 * Else we also need to save and change the source
293 if (in_hosteq(ip->ip_src, sc->g_src)) {
296 mob_h.proto |= MOB_H_SBIT;
297 mob_h.osrc = ip->ip_src.s_addr;
298 ip->ip_src.s_addr = sc->g_src.s_addr;
301 mob_h.proto = htons(mob_h.proto);
302 mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
304 if ((m->m_data - msiz) < m->m_pktdat) {
306 MGETHDR(m0, M_DONTWAIT, MT_HEADER);
308 _IF_DROP(&ifp->if_snd);
314 m->m_data += sizeof(struct ip);
315 m->m_len -= sizeof(struct ip);
316 m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
317 m0->m_len = msiz + sizeof(struct ip);
318 m0->m_data += max_linkhdr;
319 memcpy(mtod(m0, caddr_t), (caddr_t)ip,
322 } else { /* we have some space left in the old one */
325 m->m_pkthdr.len += msiz;
326 bcopy(ip, mtod(m, caddr_t),
329 ip = mtod(m, struct ip *);
330 memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
331 ip->ip_len = ntohs(ip->ip_len) + msiz;
332 } else { /* AF_INET */
333 _IF_DROP(&ifp->if_snd);
338 } else if (sc->g_proto == IPPROTO_GRE) {
339 switch (dst->sa_family) {
341 ip = mtod(m, struct ip *);
342 etype = ETHERTYPE_IP;
346 etype = ETHERTYPE_ATALK;
350 _IF_DROP(&ifp->if_snd);
352 error = EAFNOSUPPORT;
355 M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
357 _IF_DROP(&ifp->if_snd);
363 if (m == NULL) { /* mbuf allocation failed */
364 _IF_DROP(&ifp->if_snd);
369 gh = mtod(m, struct greip *);
370 if (sc->g_proto == IPPROTO_GRE) {
371 /* we don't have any GRE flags for now */
372 memset((void *)gh, 0, sizeof(struct greip));
373 gh->gi_ptype = htons(etype);
376 gh->gi_pr = sc->g_proto;
377 if (sc->g_proto != IPPROTO_MOBILE) {
378 gh->gi_src = sc->g_src;
379 gh->gi_dst = sc->g_dst;
380 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
381 ((struct ip*)gh)->ip_ttl = GRE_TTL;
382 ((struct ip*)gh)->ip_tos = ip->ip_tos;
383 ((struct ip*)gh)->ip_id = ip->ip_id;
384 gh->gi_len = m->m_pkthdr.len;
388 ifp->if_obytes += m->m_pkthdr.len;
390 * Send it off and with IP_FORWARD flag to prevent it from
391 * overwriting the ip_id again. ip_id is already set to the
392 * ip_id of the encapsulated packet.
394 error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
395 (struct ip_moptions *)NULL, (struct inpcb *)NULL);
404 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
406 struct ifreq *ifr = (struct ifreq *)data;
407 struct if_laddrreq *lifr = (struct if_laddrreq *)data;
408 struct in_aliasreq *aifr = (struct in_aliasreq *)data;
409 struct gre_softc *sc = ifp->if_softc;
411 struct sockaddr_in si;
412 struct sockaddr *sa = NULL;
414 struct sockaddr_in sp, sm, dp, dm;
421 ifp->if_flags |= IFF_UP;
426 if ((error = suser(curthread)) != 0)
428 if ((ifr->ifr_flags & IFF_LINK0) != 0)
429 sc->g_proto = IPPROTO_GRE;
431 sc->g_proto = IPPROTO_MOBILE;
432 if ((ifr->ifr_flags & IFF_LINK2) != 0)
433 sc->wccp_ver = WCCP_V2;
435 sc->wccp_ver = WCCP_V1;
438 if ((error = suser(curthread)) != 0)
440 if (ifr->ifr_mtu < 576) {
444 ifp->if_mtu = ifr->ifr_mtu;
447 ifr->ifr_mtu = sc->sc_if.if_mtu;
451 if ((error = suser(curthread)) != 0)
454 error = EAFNOSUPPORT;
457 switch (ifr->ifr_addr.sa_family) {
463 error = EAFNOSUPPORT;
468 if ((error = suser(curthread)) != 0)
470 sc->g_proto = ifr->ifr_flags;
471 switch (sc->g_proto) {
473 ifp->if_flags |= IFF_LINK0;
476 ifp->if_flags &= ~IFF_LINK0;
479 error = EPROTONOSUPPORT;
484 ifr->ifr_flags = sc->g_proto;
488 if ((error = suser(curthread)) != 0)
491 * set tunnel endpoints, compute a less specific route
492 * to the remote end and mark if as up
495 if (cmd == GRESADDRS)
496 sc->g_src = (satosin(sa))->sin_addr;
497 if (cmd == GRESADDRD)
498 sc->g_dst = (satosin(sa))->sin_addr;
501 if (sc->encap != NULL) {
502 encap_detach(sc->encap);
506 if ((sc->g_src.s_addr != INADDR_ANY) &&
507 (sc->g_dst.s_addr != INADDR_ANY)) {
508 bzero(&sp, sizeof(sp));
509 bzero(&sm, sizeof(sm));
510 bzero(&dp, sizeof(dp));
511 bzero(&dm, sizeof(dm));
512 sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
513 sizeof(struct sockaddr_in);
514 sp.sin_family = sm.sin_family = dp.sin_family =
515 dm.sin_family = AF_INET;
516 sp.sin_addr = sc->g_src;
517 dp.sin_addr = sc->g_dst;
518 sm.sin_addr.s_addr = dm.sin_addr.s_addr =
521 sc->encap = encap_attach(AF_INET, sc->g_proto,
522 sintosa(&sp), sintosa(&sm), sintosa(&dp),
523 sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
524 &in_gre_protosw : &in_mobile_protosw, sc);
525 if (sc->encap == NULL)
526 printf("%s: unable to attach encap\n",
527 if_name(&sc->sc_if));
529 if (sc->route.ro_rt != 0) /* free old route */
530 RTFREE(sc->route.ro_rt);
531 if (gre_compute_route(sc) == 0)
532 ifp->if_flags |= IFF_RUNNING;
534 ifp->if_flags &= ~IFF_RUNNING;
538 memset(&si, 0, sizeof(si));
539 si.sin_family = AF_INET;
540 si.sin_len = sizeof(struct sockaddr_in);
541 si.sin_addr.s_addr = sc->g_src.s_addr;
546 memset(&si, 0, sizeof(si));
547 si.sin_family = AF_INET;
548 si.sin_len = sizeof(struct sockaddr_in);
549 si.sin_addr.s_addr = sc->g_dst.s_addr;
554 if ((error = suser(curthread)) != 0)
556 if (aifr->ifra_addr.sin_family != AF_INET ||
557 aifr->ifra_dstaddr.sin_family != AF_INET) {
558 error = EAFNOSUPPORT;
561 if (aifr->ifra_addr.sin_len != sizeof(si) ||
562 aifr->ifra_dstaddr.sin_len != sizeof(si)) {
566 sc->g_src = aifr->ifra_addr.sin_addr;
567 sc->g_dst = aifr->ifra_dstaddr.sin_addr;
569 case SIOCSLIFPHYADDR:
570 if ((error = suser(curthread)) != 0)
572 if (lifr->addr.ss_family != AF_INET ||
573 lifr->dstaddr.ss_family != AF_INET) {
574 error = EAFNOSUPPORT;
577 if (lifr->addr.ss_len != sizeof(si) ||
578 lifr->dstaddr.ss_len != sizeof(si)) {
582 sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
584 (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
587 if ((error = suser(curthread)) != 0)
589 sc->g_src.s_addr = INADDR_ANY;
590 sc->g_dst.s_addr = INADDR_ANY;
592 case SIOCGLIFPHYADDR:
593 if (sc->g_src.s_addr == INADDR_ANY ||
594 sc->g_dst.s_addr == INADDR_ANY) {
595 error = EADDRNOTAVAIL;
598 memset(&si, 0, sizeof(si));
599 si.sin_family = AF_INET;
600 si.sin_len = sizeof(struct sockaddr_in);
601 si.sin_addr.s_addr = sc->g_src.s_addr;
602 memcpy(&lifr->addr, &si, sizeof(si));
603 si.sin_addr.s_addr = sc->g_dst.s_addr;
604 memcpy(&lifr->dstaddr, &si, sizeof(si));
606 case SIOCGIFPSRCADDR:
608 case SIOCGIFPSRCADDR_IN6:
610 if (sc->g_src.s_addr == INADDR_ANY) {
611 error = EADDRNOTAVAIL;
614 memset(&si, 0, sizeof(si));
615 si.sin_family = AF_INET;
616 si.sin_len = sizeof(struct sockaddr_in);
617 si.sin_addr.s_addr = sc->g_src.s_addr;
618 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
620 case SIOCGIFPDSTADDR:
622 case SIOCGIFPDSTADDR_IN6:
624 if (sc->g_dst.s_addr == INADDR_ANY) {
625 error = EADDRNOTAVAIL;
628 memset(&si, 0, sizeof(si));
629 si.sin_family = AF_INET;
630 si.sin_len = sizeof(struct sockaddr_in);
631 si.sin_addr.s_addr = sc->g_dst.s_addr;
632 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
644 * computes a route to our destination that is not the one
645 * which would be taken by ip_output(), as this one will loop back to
646 * us. If the interface is p2p as a--->b, then a routing entry exists
647 * If we now send a packet to b (e.g. ping b), this will come down here
648 * gets src=a, dst=b tacked on and would from ip_output() sent back to
650 * Goal here is to compute a route to b that is less specific than
651 * a-->b. We know that this one exists as in normal operation we have
652 * at least a default route which matches.
655 gre_compute_route(struct gre_softc *sc)
662 memset(ro, 0, sizeof(struct route));
663 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
664 ro->ro_dst.sa_family = AF_INET;
665 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
668 * toggle last bit, so our interface is not found, but a less
669 * specific route. I'd rather like to specify a shorter mask,
670 * but this is not possible. Should work though. XXX
671 * there is a simpler way ...
673 if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
674 a = ntohl(sc->g_dst.s_addr);
679 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
684 printf("%s: searching for a route to %s", if_name(&sc->sc_if),
685 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
691 * check if this returned a route at all and this route is no
692 * recursion to ourself
694 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
696 if (ro->ro_rt == NULL)
697 printf(" - no route found!\n");
699 printf(" - route loops back to ourself!\n");
701 return EADDRNOTAVAIL;
705 * now change it back - else ip_output will just drop
706 * the route and search one to this interface ...
708 if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
709 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
712 printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
713 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
721 * do a checksum of a buffer - much like in_cksum, which operates on
725 gre_in_cksum(u_int16_t *p, u_int len)
728 int nwords = len >> 1;
730 while (nwords-- != 0)
738 u.c[0] = *(u_char *)p;
743 /* end-around-carry */
744 sum = (sum >> 16) + (sum & 0xffff);
750 gremodevent(module_t mod, int type, void *data)
752 struct gre_softc *sc;
759 if_clone_detach(&gre_cloner);
762 while ((sc = LIST_FIRST(&gre_softc_list)) != NULL) {
763 LIST_REMOVE(sc, sc_list);
764 mtx_unlock(&gre_mtx);
768 mtx_unlock(&gre_mtx);
769 mtx_destroy(&gre_mtx);
775 static moduledata_t gre_mod = {
781 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
782 MODULE_VERSION(if_gre, 1);