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 * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
43 * Encapsulate L3 protocols into IP
44 * See RFC 2784 (successor of RFC 1701 and 1702) for more details.
45 * If_gre is compatible with Cisco GRE tunnels, so you can
46 * have a NetBSD box as the other end of a tunnel interface of a Cisco
47 * router. See gre(4) for more details.
48 * Also supported: IP in IP encaps (proto 55) as of RFC 2004
51 #include "opt_atalk.h"
53 #include "opt_inet6.h"
55 #include <sys/param.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
58 #include <sys/module.h>
60 #include <sys/protosw.h>
61 #include <sys/socket.h>
62 #include <sys/sockio.h>
63 #include <sys/sysctl.h>
64 #include <sys/systm.h>
66 #include <net/ethernet.h>
68 #include <net/if_clone.h>
69 #include <net/if_types.h>
70 #include <net/route.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/in_var.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_gre.h>
78 #include <netinet/ip_var.h>
79 #include <netinet/ip_encap.h>
81 #error "Huh? if_gre without inet?"
86 #include <net/net_osdep.h>
87 #include <net/if_gre.h>
90 * It is not easy to calculate the right value for a GRE MTU.
91 * We leave this task to the admin and use the same default that
99 * gre_mtx protects all global variables in if_gre.c.
100 * XXX: gre_softc data not protected yet.
103 static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
105 struct gre_softc_head gre_softc_list;
107 static int gre_clone_create(struct if_clone *, int);
108 static void gre_clone_destroy(struct ifnet *);
109 static int gre_ioctl(struct ifnet *, u_long, caddr_t);
110 static int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
113 IFC_SIMPLE_DECLARE(gre, 0);
115 static int gre_compute_route(struct gre_softc *sc);
117 static void greattach(void);
120 extern struct domain inetdomain;
121 static const struct protosw in_gre_protosw =
122 { SOCK_RAW, &inetdomain, IPPROTO_GRE, PR_ATOMIC|PR_ADDR,
123 (pr_input_t*)gre_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
128 static const struct protosw in_mobile_protosw =
129 { SOCK_RAW, &inetdomain, IPPROTO_MOBILE, PR_ATOMIC|PR_ADDR,
130 (pr_input_t*)gre_mobile_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
137 SYSCTL_DECL(_net_link);
138 SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
139 "Generic Routing Encapsulation");
142 * This macro controls the default upper limitation on nesting of gre tunnels.
143 * Since, setting a large value to this macro with a careless configuration
144 * may introduce system crash, we don't allow any nestings by default.
145 * If you need to configure nested gre tunnels, you can define this macro
146 * in your kernel configuration file. However, if you do so, please be
147 * careful to configure the tunnels so that it won't make a loop.
149 #define MAX_GRE_NEST 1
151 static int max_gre_nesting = MAX_GRE_NEST;
152 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
153 &max_gre_nesting, 0, "Max nested tunnels");
160 mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
161 LIST_INIT(&gre_softc_list);
162 if_clone_attach(&gre_cloner);
166 gre_clone_create(ifc, unit)
167 struct if_clone *ifc;
170 struct gre_softc *sc;
172 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
174 GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
175 if (GRE2IFP(sc) == NULL) {
180 GRE2IFP(sc)->if_softc = sc;
181 if_initname(GRE2IFP(sc), ifc->ifc_name, unit);
183 GRE2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
184 GRE2IFP(sc)->if_addrlen = 0;
185 GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */
186 GRE2IFP(sc)->if_mtu = GREMTU;
187 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
188 GRE2IFP(sc)->if_output = gre_output;
189 GRE2IFP(sc)->if_ioctl = gre_ioctl;
190 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
191 sc->g_proto = IPPROTO_GRE;
192 GRE2IFP(sc)->if_flags |= IFF_LINK0;
195 sc->wccp_ver = WCCP_V1;
196 if_attach(GRE2IFP(sc));
197 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
199 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
200 mtx_unlock(&gre_mtx);
205 gre_destroy(struct gre_softc *sc)
209 if (sc->encap != NULL)
210 encap_detach(sc->encap);
212 bpfdetach(GRE2IFP(sc));
213 if_detach(GRE2IFP(sc));
214 if_free(GRE2IFP(sc));
219 gre_clone_destroy(ifp)
222 struct gre_softc *sc = ifp->if_softc;
225 LIST_REMOVE(sc, sc_list);
226 mtx_unlock(&gre_mtx);
231 * The output routine. Takes a packet and encapsulates it in the protocol
232 * given by sc->g_proto. See also RFC 1701 and RFC 2004
235 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
239 struct gre_softc *sc = ifp->if_softc;
245 struct mobile_h mob_h;
249 * gre may cause infinite recursion calls when misconfigured.
250 * We'll prevent this by introducing upper limit.
252 if (++(sc->called) > max_gre_nesting) {
253 printf("%s: gre_output: recursively called too many "
254 "times(%d)\n", if_name(GRE2IFP(sc)), sc->called);
256 error = EIO; /* is there better errno? */
260 if (!((ifp->if_flags & IFF_UP) &&
261 (ifp->if_drv_flags & IFF_DRV_RUNNING)) ||
262 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
271 /* BPF writes need to be handled specially. */
272 if (dst->sa_family == AF_UNSPEC) {
273 bcopy(dst->sa_data, &af, sizeof(af));
279 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
282 m->m_flags &= ~(M_BCAST|M_MCAST);
284 if (sc->g_proto == IPPROTO_MOBILE) {
285 if (dst->sa_family == AF_INET) {
289 ip = mtod(m, struct ip *);
292 * RFC2004 specifies that fragmented diagrams shouldn't
295 if ((ip->ip_off & IP_MF) != 0) {
296 _IF_DROP(&ifp->if_snd);
298 error = EINVAL; /* is there better errno? */
301 memset(&mob_h, 0, MOB_H_SIZ_L);
302 mob_h.proto = (ip->ip_p) << 8;
303 mob_h.odst = ip->ip_dst.s_addr;
304 ip->ip_dst.s_addr = sc->g_dst.s_addr;
307 * If the packet comes from our host, we only change
308 * the destination address in the IP header.
309 * Else we also need to save and change the source
311 if (in_hosteq(ip->ip_src, sc->g_src)) {
314 mob_h.proto |= MOB_H_SBIT;
315 mob_h.osrc = ip->ip_src.s_addr;
316 ip->ip_src.s_addr = sc->g_src.s_addr;
319 mob_h.proto = htons(mob_h.proto);
320 mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
322 if ((m->m_data - msiz) < m->m_pktdat) {
324 MGETHDR(m0, M_DONTWAIT, MT_HEADER);
326 _IF_DROP(&ifp->if_snd);
332 m->m_data += sizeof(struct ip);
333 m->m_len -= sizeof(struct ip);
334 m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
335 m0->m_len = msiz + sizeof(struct ip);
336 m0->m_data += max_linkhdr;
337 memcpy(mtod(m0, caddr_t), (caddr_t)ip,
340 } else { /* we have some space left in the old one */
343 m->m_pkthdr.len += msiz;
344 bcopy(ip, mtod(m, caddr_t),
347 ip = mtod(m, struct ip *);
348 memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
349 ip->ip_len = ntohs(ip->ip_len) + msiz;
350 } else { /* AF_INET */
351 _IF_DROP(&ifp->if_snd);
356 } else if (sc->g_proto == IPPROTO_GRE) {
357 switch (dst->sa_family) {
359 ip = mtod(m, struct ip *);
362 etype = ETHERTYPE_IP;
367 etype = ETHERTYPE_IPV6;
372 etype = ETHERTYPE_ATALK;
376 _IF_DROP(&ifp->if_snd);
378 error = EAFNOSUPPORT;
381 M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
383 _IF_DROP(&ifp->if_snd);
389 if (m == NULL) { /* mbuf allocation failed */
390 _IF_DROP(&ifp->if_snd);
395 gh = mtod(m, struct greip *);
396 if (sc->g_proto == IPPROTO_GRE) {
397 /* we don't have any GRE flags for now */
398 memset((void *)gh, 0, sizeof(struct greip));
399 gh->gi_ptype = htons(etype);
402 gh->gi_pr = sc->g_proto;
403 if (sc->g_proto != IPPROTO_MOBILE) {
404 gh->gi_src = sc->g_src;
405 gh->gi_dst = sc->g_dst;
406 ((struct ip*)gh)->ip_v = IPPROTO_IPV4;
407 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
408 ((struct ip*)gh)->ip_ttl = GRE_TTL;
409 ((struct ip*)gh)->ip_tos = ip_tos;
410 ((struct ip*)gh)->ip_id = ip_id;
411 gh->gi_len = m->m_pkthdr.len;
415 ifp->if_obytes += m->m_pkthdr.len;
417 * Send it off and with IP_FORWARD flag to prevent it from
418 * overwriting the ip_id again. ip_id is already set to the
419 * ip_id of the encapsulated packet.
421 error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
422 (struct ip_moptions *)NULL, (struct inpcb *)NULL);
431 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
433 struct ifreq *ifr = (struct ifreq *)data;
434 struct if_laddrreq *lifr = (struct if_laddrreq *)data;
435 struct in_aliasreq *aifr = (struct in_aliasreq *)data;
436 struct gre_softc *sc = ifp->if_softc;
438 struct sockaddr_in si;
439 struct sockaddr *sa = NULL;
441 struct sockaddr_in sp, sm, dp, dm;
448 ifp->if_flags |= IFF_UP;
453 if ((error = suser(curthread)) != 0)
455 if ((ifr->ifr_flags & IFF_LINK0) != 0)
456 sc->g_proto = IPPROTO_GRE;
458 sc->g_proto = IPPROTO_MOBILE;
459 if ((ifr->ifr_flags & IFF_LINK2) != 0)
460 sc->wccp_ver = WCCP_V2;
462 sc->wccp_ver = WCCP_V1;
465 if ((error = suser(curthread)) != 0)
467 if (ifr->ifr_mtu < 576) {
471 ifp->if_mtu = ifr->ifr_mtu;
474 ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
478 if ((error = suser(curthread)) != 0)
481 error = EAFNOSUPPORT;
484 switch (ifr->ifr_addr.sa_family) {
494 error = EAFNOSUPPORT;
499 if ((error = suser(curthread)) != 0)
501 sc->g_proto = ifr->ifr_flags;
502 switch (sc->g_proto) {
504 ifp->if_flags |= IFF_LINK0;
507 ifp->if_flags &= ~IFF_LINK0;
510 error = EPROTONOSUPPORT;
515 ifr->ifr_flags = sc->g_proto;
519 if ((error = suser(curthread)) != 0)
522 * set tunnel endpoints, compute a less specific route
523 * to the remote end and mark if as up
526 if (cmd == GRESADDRS)
527 sc->g_src = (satosin(sa))->sin_addr;
528 if (cmd == GRESADDRD)
529 sc->g_dst = (satosin(sa))->sin_addr;
532 if (sc->encap != NULL) {
533 encap_detach(sc->encap);
537 if ((sc->g_src.s_addr != INADDR_ANY) &&
538 (sc->g_dst.s_addr != INADDR_ANY)) {
539 bzero(&sp, sizeof(sp));
540 bzero(&sm, sizeof(sm));
541 bzero(&dp, sizeof(dp));
542 bzero(&dm, sizeof(dm));
543 sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
544 sizeof(struct sockaddr_in);
545 sp.sin_family = sm.sin_family = dp.sin_family =
546 dm.sin_family = AF_INET;
547 sp.sin_addr = sc->g_src;
548 dp.sin_addr = sc->g_dst;
549 sm.sin_addr.s_addr = dm.sin_addr.s_addr =
552 sc->encap = encap_attach(AF_INET, sc->g_proto,
553 sintosa(&sp), sintosa(&sm), sintosa(&dp),
554 sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
555 &in_gre_protosw : &in_mobile_protosw, sc);
556 if (sc->encap == NULL)
557 printf("%s: unable to attach encap\n",
558 if_name(GRE2IFP(sc)));
560 if (sc->route.ro_rt != 0) /* free old route */
561 RTFREE(sc->route.ro_rt);
562 if (gre_compute_route(sc) == 0)
563 ifp->if_drv_flags |= IFF_DRV_RUNNING;
565 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
569 memset(&si, 0, sizeof(si));
570 si.sin_family = AF_INET;
571 si.sin_len = sizeof(struct sockaddr_in);
572 si.sin_addr.s_addr = sc->g_src.s_addr;
577 memset(&si, 0, sizeof(si));
578 si.sin_family = AF_INET;
579 si.sin_len = sizeof(struct sockaddr_in);
580 si.sin_addr.s_addr = sc->g_dst.s_addr;
585 if ((error = suser(curthread)) != 0)
587 if (aifr->ifra_addr.sin_family != AF_INET ||
588 aifr->ifra_dstaddr.sin_family != AF_INET) {
589 error = EAFNOSUPPORT;
592 if (aifr->ifra_addr.sin_len != sizeof(si) ||
593 aifr->ifra_dstaddr.sin_len != sizeof(si)) {
597 sc->g_src = aifr->ifra_addr.sin_addr;
598 sc->g_dst = aifr->ifra_dstaddr.sin_addr;
600 case SIOCSLIFPHYADDR:
601 if ((error = suser(curthread)) != 0)
603 if (lifr->addr.ss_family != AF_INET ||
604 lifr->dstaddr.ss_family != AF_INET) {
605 error = EAFNOSUPPORT;
608 if (lifr->addr.ss_len != sizeof(si) ||
609 lifr->dstaddr.ss_len != sizeof(si)) {
613 sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
615 (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
618 if ((error = suser(curthread)) != 0)
620 sc->g_src.s_addr = INADDR_ANY;
621 sc->g_dst.s_addr = INADDR_ANY;
623 case SIOCGLIFPHYADDR:
624 if (sc->g_src.s_addr == INADDR_ANY ||
625 sc->g_dst.s_addr == INADDR_ANY) {
626 error = EADDRNOTAVAIL;
629 memset(&si, 0, sizeof(si));
630 si.sin_family = AF_INET;
631 si.sin_len = sizeof(struct sockaddr_in);
632 si.sin_addr.s_addr = sc->g_src.s_addr;
633 memcpy(&lifr->addr, &si, sizeof(si));
634 si.sin_addr.s_addr = sc->g_dst.s_addr;
635 memcpy(&lifr->dstaddr, &si, sizeof(si));
637 case SIOCGIFPSRCADDR:
639 case SIOCGIFPSRCADDR_IN6:
641 if (sc->g_src.s_addr == INADDR_ANY) {
642 error = EADDRNOTAVAIL;
645 memset(&si, 0, sizeof(si));
646 si.sin_family = AF_INET;
647 si.sin_len = sizeof(struct sockaddr_in);
648 si.sin_addr.s_addr = sc->g_src.s_addr;
649 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
651 case SIOCGIFPDSTADDR:
653 case SIOCGIFPDSTADDR_IN6:
655 if (sc->g_dst.s_addr == INADDR_ANY) {
656 error = EADDRNOTAVAIL;
659 memset(&si, 0, sizeof(si));
660 si.sin_family = AF_INET;
661 si.sin_len = sizeof(struct sockaddr_in);
662 si.sin_addr.s_addr = sc->g_dst.s_addr;
663 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
675 * computes a route to our destination that is not the one
676 * which would be taken by ip_output(), as this one will loop back to
677 * us. If the interface is p2p as a--->b, then a routing entry exists
678 * If we now send a packet to b (e.g. ping b), this will come down here
679 * gets src=a, dst=b tacked on and would from ip_output() sent back to
681 * Goal here is to compute a route to b that is less specific than
682 * a-->b. We know that this one exists as in normal operation we have
683 * at least a default route which matches.
686 gre_compute_route(struct gre_softc *sc)
693 memset(ro, 0, sizeof(struct route));
694 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
695 ro->ro_dst.sa_family = AF_INET;
696 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
699 * toggle last bit, so our interface is not found, but a less
700 * specific route. I'd rather like to specify a shorter mask,
701 * but this is not possible. Should work though. XXX
702 * there is a simpler way ...
704 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
705 a = ntohl(sc->g_dst.s_addr);
710 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
715 printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
716 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
722 * check if this returned a route at all and this route is no
723 * recursion to ourself
725 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
727 if (ro->ro_rt == NULL)
728 printf(" - no route found!\n");
730 printf(" - route loops back to ourself!\n");
732 return EADDRNOTAVAIL;
736 * now change it back - else ip_output will just drop
737 * the route and search one to this interface ...
739 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
740 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
743 printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
744 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
752 * do a checksum of a buffer - much like in_cksum, which operates on
756 gre_in_cksum(u_int16_t *p, u_int len)
759 int nwords = len >> 1;
761 while (nwords-- != 0)
769 u.c[0] = *(u_char *)p;
774 /* end-around-carry */
775 sum = (sum >> 16) + (sum & 0xffff);
781 gremodevent(module_t mod, int type, void *data)
783 struct gre_softc *sc;
790 if_clone_detach(&gre_cloner);
793 while ((sc = LIST_FIRST(&gre_softc_list)) != NULL) {
794 LIST_REMOVE(sc, sc_list);
795 mtx_unlock(&gre_mtx);
799 mtx_unlock(&gre_mtx);
800 mtx_destroy(&gre_mtx);
808 static moduledata_t gre_mod = {
814 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
815 MODULE_VERSION(if_gre, 1);