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.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
36 * Encapsulate L3 protocols into IP
37 * See RFC 2784 (successor of RFC 1701 and 1702) for more details.
38 * If_gre is compatible with Cisco GRE tunnels, so you can
39 * have a NetBSD box as the other end of a tunnel interface of a Cisco
40 * router. See gre(4) for more details.
41 * Also supported: IP in IP encaps (proto 55) as of RFC 2004
44 #include "opt_atalk.h"
46 #include "opt_inet6.h"
48 #include <sys/param.h>
50 #include <sys/kernel.h>
51 #include <sys/libkern.h>
52 #include <sys/malloc.h>
53 #include <sys/module.h>
57 #include <sys/protosw.h>
58 #include <sys/socket.h>
59 #include <sys/sockio.h>
60 #include <sys/sysctl.h>
61 #include <sys/systm.h>
63 #include <net/ethernet.h>
65 #include <net/if_clone.h>
66 #include <net/if_types.h>
67 #include <net/route.h>
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/in_var.h>
74 #include <netinet/ip.h>
75 #include <netinet/ip_gre.h>
76 #include <netinet/ip_var.h>
77 #include <netinet/ip_encap.h>
79 #error "Huh? if_gre without inet?"
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
95 #define MTAG_COOKIE_GRE 1307983903
96 #define MTAG_GRE_NESTING 1
97 struct mtag_gre_nesting {
104 * gre_mtx protects all global variables in if_gre.c.
105 * XXX: gre_softc data not protected yet.
108 static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
110 struct gre_softc_head gre_softc_list;
112 static int gre_clone_create(struct if_clone *, int, caddr_t);
113 static void gre_clone_destroy(struct ifnet *);
114 static int gre_ioctl(struct ifnet *, u_long, caddr_t);
115 static int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
118 IFC_SIMPLE_DECLARE(gre, 0);
120 static int gre_compute_route(struct gre_softc *sc);
122 static void greattach(void);
125 extern struct domain inetdomain;
126 static const struct protosw in_gre_protosw = {
128 .pr_domain = &inetdomain,
129 .pr_protocol = IPPROTO_GRE,
130 .pr_flags = PR_ATOMIC|PR_ADDR,
131 .pr_input = gre_input,
132 .pr_output = (pr_output_t *)rip_output,
133 .pr_ctlinput = rip_ctlinput,
134 .pr_ctloutput = rip_ctloutput,
135 .pr_usrreqs = &rip_usrreqs
137 static const struct protosw in_mobile_protosw = {
139 .pr_domain = &inetdomain,
140 .pr_protocol = IPPROTO_MOBILE,
141 .pr_flags = PR_ATOMIC|PR_ADDR,
142 .pr_input = gre_mobile_input,
143 .pr_output = (pr_output_t *)rip_output,
144 .pr_ctlinput = rip_ctlinput,
145 .pr_ctloutput = rip_ctloutput,
146 .pr_usrreqs = &rip_usrreqs
150 SYSCTL_DECL(_net_link);
151 static SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
152 "Generic Routing Encapsulation");
155 * This macro controls the default upper limitation on nesting of gre tunnels.
156 * Since, setting a large value to this macro with a careless configuration
157 * may introduce system crash, we don't allow any nestings by default.
158 * If you need to configure nested gre tunnels, you can define this macro
159 * in your kernel configuration file. However, if you do so, please be
160 * careful to configure the tunnels so that it won't make a loop.
162 #define MAX_GRE_NEST 1
164 static int max_gre_nesting = MAX_GRE_NEST;
165 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
166 &max_gre_nesting, 0, "Max nested tunnels");
173 mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
174 LIST_INIT(&gre_softc_list);
175 if_clone_attach(&gre_cloner);
179 gre_clone_create(ifc, unit, params)
180 struct if_clone *ifc;
184 struct gre_softc *sc;
186 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
188 GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
189 if (GRE2IFP(sc) == NULL) {
194 GRE2IFP(sc)->if_softc = sc;
195 if_initname(GRE2IFP(sc), ifc->ifc_name, unit);
197 GRE2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen;
198 GRE2IFP(sc)->if_addrlen = 0;
199 GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */
200 GRE2IFP(sc)->if_mtu = GREMTU;
201 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
202 GRE2IFP(sc)->if_output = gre_output;
203 GRE2IFP(sc)->if_ioctl = gre_ioctl;
204 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
205 sc->g_proto = IPPROTO_GRE;
206 GRE2IFP(sc)->if_flags |= IFF_LINK0;
208 sc->gre_fibnum = curthread->td_proc->p_fibnum;
209 sc->wccp_ver = WCCP_V1;
211 if_attach(GRE2IFP(sc));
212 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
214 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
215 mtx_unlock(&gre_mtx);
220 gre_clone_destroy(ifp)
223 struct gre_softc *sc = ifp->if_softc;
226 LIST_REMOVE(sc, sc_list);
227 mtx_unlock(&gre_mtx);
230 if (sc->encap != NULL)
231 encap_detach(sc->encap);
240 * The output routine. Takes a packet and encapsulates it in the protocol
241 * given by sc->g_proto. See also RFC 1701 and RFC 2004
244 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
248 struct gre_softc *sc = ifp->if_softc;
252 struct mtag_gre_nesting *gt;
254 u_short gre_ip_id = 0;
255 uint8_t gre_ip_tos = 0;
257 struct mobile_h mob_h;
262 * gre may cause infinite recursion calls when misconfigured. High
263 * nesting level may cause stack exhaustion. We'll prevent this by
264 * detecting loops and by introducing upper limit.
266 mtag = m_tag_locate(m, MTAG_COOKIE_GRE, MTAG_GRE_NESTING, NULL);
270 gt = (struct mtag_gre_nesting *)(mtag + 1);
272 if (gt->count > min(gt->max,max_gre_nesting)) {
273 printf("%s: hit maximum recursion limit %u on %s\n",
274 __func__, gt->count - 1, ifp->if_xname);
276 error = EIO; /* is there better errno? */
281 for (max = gt->count - 1; max > 0; max--) {
287 printf("%s: detected loop with nexting %u on %s\n",
288 __func__, gt->count-1, ifp->if_xname);
290 error = EIO; /* is there better errno? */
297 * Given that people should NOT increase max_gre_nesting beyond
298 * their real needs, we allocate once per packet rather than
299 * allocating an mtag once per passing through gre.
301 * Note: the sysctl does not actually check for saneness, so we
302 * limit the maximum numbers of possible recursions here.
304 max = imin(max_gre_nesting, 256);
305 /* If someone sets the sysctl <= 0, we want at least 1. */
307 len = sizeof(struct mtag_gre_nesting) +
308 max * sizeof(struct ifnet *);
309 mtag = m_tag_alloc(MTAG_COOKIE_GRE, MTAG_GRE_NESTING, len,
316 gt = (struct mtag_gre_nesting *)(mtag + 1);
321 m_tag_prepend(m, mtag);
324 if (!((ifp->if_flags & IFF_UP) &&
325 (ifp->if_drv_flags & IFF_DRV_RUNNING)) ||
326 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
335 /* BPF writes need to be handled specially. */
336 if (dst->sa_family == AF_UNSPEC) {
337 bcopy(dst->sa_data, &af, sizeof(af));
341 if (bpf_peers_present(ifp->if_bpf)) {
343 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
346 if ((ifp->if_flags & IFF_MONITOR) != 0) {
352 m->m_flags &= ~(M_BCAST|M_MCAST);
354 if (sc->g_proto == IPPROTO_MOBILE) {
355 if (dst->sa_family == AF_INET) {
359 ip = mtod(m, struct ip *);
362 * RFC2004 specifies that fragmented diagrams shouldn't
365 if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
366 _IF_DROP(&ifp->if_snd);
368 error = EINVAL; /* is there better errno? */
371 memset(&mob_h, 0, MOB_H_SIZ_L);
372 mob_h.proto = (ip->ip_p) << 8;
373 mob_h.odst = ip->ip_dst.s_addr;
374 ip->ip_dst.s_addr = sc->g_dst.s_addr;
377 * If the packet comes from our host, we only change
378 * the destination address in the IP header.
379 * Else we also need to save and change the source
381 if (in_hosteq(ip->ip_src, sc->g_src)) {
384 mob_h.proto |= MOB_H_SBIT;
385 mob_h.osrc = ip->ip_src.s_addr;
386 ip->ip_src.s_addr = sc->g_src.s_addr;
389 mob_h.proto = htons(mob_h.proto);
390 mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
392 if ((m->m_data - msiz) < m->m_pktdat) {
394 MGETHDR(m0, M_DONTWAIT, MT_DATA);
396 _IF_DROP(&ifp->if_snd);
402 m->m_data += sizeof(struct ip);
403 m->m_len -= sizeof(struct ip);
404 m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
405 m0->m_len = msiz + sizeof(struct ip);
406 m0->m_data += max_linkhdr;
407 memcpy(mtod(m0, caddr_t), (caddr_t)ip,
410 } else { /* we have some space left in the old one */
413 m->m_pkthdr.len += msiz;
414 bcopy(ip, mtod(m, caddr_t),
417 ip = mtod(m, struct ip *);
418 memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
419 ip->ip_len = ntohs(ip->ip_len) + msiz;
420 } else { /* AF_INET */
421 _IF_DROP(&ifp->if_snd);
426 } else if (sc->g_proto == IPPROTO_GRE) {
427 switch (dst->sa_family) {
429 ip = mtod(m, struct ip *);
430 gre_ip_tos = ip->ip_tos;
431 gre_ip_id = ip->ip_id;
432 if (sc->wccp_ver == WCCP_V2) {
433 extra = sizeof(uint32_t);
434 etype = WCCP_PROTOCOL_TYPE;
436 etype = ETHERTYPE_IP;
441 gre_ip_id = ip_newid();
442 etype = ETHERTYPE_IPV6;
447 etype = ETHERTYPE_ATALK;
451 _IF_DROP(&ifp->if_snd);
453 error = EAFNOSUPPORT;
457 /* Reserve space for GRE header + optional GRE key */
458 int hdrlen = sizeof(struct greip) + extra;
460 hdrlen += sizeof(uint32_t);
461 M_PREPEND(m, hdrlen, M_DONTWAIT);
463 _IF_DROP(&ifp->if_snd);
469 if (m == NULL) { /* mbuf allocation failed */
470 _IF_DROP(&ifp->if_snd);
475 M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */
477 gh = mtod(m, struct greip *);
478 if (sc->g_proto == IPPROTO_GRE) {
479 uint32_t *options = gh->gi_options;
481 memset((void *)gh, 0, sizeof(struct greip) + extra);
482 gh->gi_ptype = htons(etype);
488 gh->gi_flags |= htons(GRE_KP);
489 *(options++) = htonl(sc->key);
493 gh->gi_pr = sc->g_proto;
494 if (sc->g_proto != IPPROTO_MOBILE) {
495 gh->gi_src = sc->g_src;
496 gh->gi_dst = sc->g_dst;
497 ((struct ip*)gh)->ip_v = IPPROTO_IPV4;
498 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
499 ((struct ip*)gh)->ip_ttl = GRE_TTL;
500 ((struct ip*)gh)->ip_tos = gre_ip_tos;
501 ((struct ip*)gh)->ip_id = gre_ip_id;
502 gh->gi_len = m->m_pkthdr.len;
506 ifp->if_obytes += m->m_pkthdr.len;
508 * Send it off and with IP_FORWARD flag to prevent it from
509 * overwriting the ip_id again. ip_id is already set to the
510 * ip_id of the encapsulated packet.
512 error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
513 (struct ip_moptions *)NULL, (struct inpcb *)NULL);
521 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
523 struct ifreq *ifr = (struct ifreq *)data;
524 struct if_laddrreq *lifr = (struct if_laddrreq *)data;
525 struct in_aliasreq *aifr = (struct in_aliasreq *)data;
526 struct gre_softc *sc = ifp->if_softc;
528 struct sockaddr_in si;
529 struct sockaddr *sa = NULL;
531 struct sockaddr_in sp, sm, dp, dm;
540 ifp->if_flags |= IFF_UP;
546 * XXXRW: Isn't this priv_check() redundant to the ifnet
549 if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0)
551 if ((ifr->ifr_flags & IFF_LINK0) != 0)
552 sc->g_proto = IPPROTO_GRE;
554 sc->g_proto = IPPROTO_MOBILE;
555 if ((ifr->ifr_flags & IFF_LINK2) != 0)
556 sc->wccp_ver = WCCP_V2;
558 sc->wccp_ver = WCCP_V1;
562 * XXXRW: Isn't this priv_check() redundant to the ifnet
565 if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0)
567 if (ifr->ifr_mtu < 576) {
571 ifp->if_mtu = ifr->ifr_mtu;
574 ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
578 * XXXRW: Isn't this priv_checkr() redundant to the ifnet
581 if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0)
584 error = EAFNOSUPPORT;
587 switch (ifr->ifr_addr.sa_family) {
597 error = EAFNOSUPPORT;
603 * XXXRW: Isn't this priv_check() redundant to the ifnet
606 if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0)
609 error = EAFNOSUPPORT;
612 switch (ifr->ifr_addr.sa_family) {
622 error = EAFNOSUPPORT;
628 * XXXRW: Isn't this priv_check() redundant to the ifnet
631 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
633 sc->g_proto = ifr->ifr_flags;
634 switch (sc->g_proto) {
636 ifp->if_flags |= IFF_LINK0;
639 ifp->if_flags &= ~IFF_LINK0;
642 error = EPROTONOSUPPORT;
647 ifr->ifr_flags = sc->g_proto;
651 error = priv_check(curthread, PRIV_NET_GRE);
655 * set tunnel endpoints, compute a less specific route
656 * to the remote end and mark if as up
659 if (cmd == GRESADDRS)
660 sc->g_src = (satosin(sa))->sin_addr;
661 if (cmd == GRESADDRD)
662 sc->g_dst = (satosin(sa))->sin_addr;
665 if (sc->encap != NULL) {
666 encap_detach(sc->encap);
670 if ((sc->g_src.s_addr != INADDR_ANY) &&
671 (sc->g_dst.s_addr != INADDR_ANY)) {
672 bzero(&sp, sizeof(sp));
673 bzero(&sm, sizeof(sm));
674 bzero(&dp, sizeof(dp));
675 bzero(&dm, sizeof(dm));
676 sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
677 sizeof(struct sockaddr_in);
678 sp.sin_family = sm.sin_family = dp.sin_family =
679 dm.sin_family = AF_INET;
680 sp.sin_addr = sc->g_src;
681 dp.sin_addr = sc->g_dst;
682 sm.sin_addr.s_addr = dm.sin_addr.s_addr =
685 sc->encap = encap_attach(AF_INET, sc->g_proto,
686 sintosa(&sp), sintosa(&sm), sintosa(&dp),
687 sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
688 &in_gre_protosw : &in_mobile_protosw, sc);
689 if (sc->encap == NULL)
690 printf("%s: unable to attach encap\n",
691 if_name(GRE2IFP(sc)));
693 if (sc->route.ro_rt != 0) /* free old route */
694 RTFREE(sc->route.ro_rt);
695 if (gre_compute_route(sc) == 0)
696 ifp->if_drv_flags |= IFF_DRV_RUNNING;
698 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
702 memset(&si, 0, sizeof(si));
703 si.sin_family = AF_INET;
704 si.sin_len = sizeof(struct sockaddr_in);
705 si.sin_addr.s_addr = sc->g_src.s_addr;
707 error = prison_if(curthread->td_ucred, sa);
713 memset(&si, 0, sizeof(si));
714 si.sin_family = AF_INET;
715 si.sin_len = sizeof(struct sockaddr_in);
716 si.sin_addr.s_addr = sc->g_dst.s_addr;
718 error = prison_if(curthread->td_ucred, sa);
725 * XXXRW: Isn't this priv_check() redundant to the ifnet
728 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
730 if (aifr->ifra_addr.sin_family != AF_INET ||
731 aifr->ifra_dstaddr.sin_family != AF_INET) {
732 error = EAFNOSUPPORT;
735 if (aifr->ifra_addr.sin_len != sizeof(si) ||
736 aifr->ifra_dstaddr.sin_len != sizeof(si)) {
740 sc->g_src = aifr->ifra_addr.sin_addr;
741 sc->g_dst = aifr->ifra_dstaddr.sin_addr;
743 case SIOCSLIFPHYADDR:
745 * XXXRW: Isn't this priv_check() redundant to the ifnet
748 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
750 if (lifr->addr.ss_family != AF_INET ||
751 lifr->dstaddr.ss_family != AF_INET) {
752 error = EAFNOSUPPORT;
755 if (lifr->addr.ss_len != sizeof(si) ||
756 lifr->dstaddr.ss_len != sizeof(si)) {
760 sc->g_src = (satosin(&lifr->addr))->sin_addr;
762 (satosin(&lifr->dstaddr))->sin_addr;
766 * XXXRW: Isn't this priv_check() redundant to the ifnet
769 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
771 sc->g_src.s_addr = INADDR_ANY;
772 sc->g_dst.s_addr = INADDR_ANY;
774 case SIOCGLIFPHYADDR:
775 if (sc->g_src.s_addr == INADDR_ANY ||
776 sc->g_dst.s_addr == INADDR_ANY) {
777 error = EADDRNOTAVAIL;
780 memset(&si, 0, sizeof(si));
781 si.sin_family = AF_INET;
782 si.sin_len = sizeof(struct sockaddr_in);
783 si.sin_addr.s_addr = sc->g_src.s_addr;
784 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
787 memcpy(&lifr->addr, &si, sizeof(si));
788 si.sin_addr.s_addr = sc->g_dst.s_addr;
789 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
792 memcpy(&lifr->dstaddr, &si, sizeof(si));
794 case SIOCGIFPSRCADDR:
796 case SIOCGIFPSRCADDR_IN6:
798 if (sc->g_src.s_addr == INADDR_ANY) {
799 error = EADDRNOTAVAIL;
802 memset(&si, 0, sizeof(si));
803 si.sin_family = AF_INET;
804 si.sin_len = sizeof(struct sockaddr_in);
805 si.sin_addr.s_addr = sc->g_src.s_addr;
806 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
809 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
811 case SIOCGIFPDSTADDR:
813 case SIOCGIFPDSTADDR_IN6:
815 if (sc->g_dst.s_addr == INADDR_ANY) {
816 error = EADDRNOTAVAIL;
819 memset(&si, 0, sizeof(si));
820 si.sin_family = AF_INET;
821 si.sin_len = sizeof(struct sockaddr_in);
822 si.sin_addr.s_addr = sc->g_dst.s_addr;
823 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
826 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
829 error = priv_check(curthread, PRIV_NET_GRE);
832 error = copyin(ifr->ifr_data, &key, sizeof(key));
835 /* adjust MTU for option header */
836 if (key == 0 && sc->key != 0) /* clear */
838 else if (key != 0 && sc->key == 0) /* set */
841 if (ifp->if_mtu + adj < 576) {
849 error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key));
862 * computes a route to our destination that is not the one
863 * which would be taken by ip_output(), as this one will loop back to
864 * us. If the interface is p2p as a--->b, then a routing entry exists
865 * If we now send a packet to b (e.g. ping b), this will come down here
866 * gets src=a, dst=b tacked on and would from ip_output() sent back to
868 * Goal here is to compute a route to b that is less specific than
869 * a-->b. We know that this one exists as in normal operation we have
870 * at least a default route which matches.
873 gre_compute_route(struct gre_softc *sc)
879 memset(ro, 0, sizeof(struct route));
880 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
881 ro->ro_dst.sa_family = AF_INET;
882 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
885 * toggle last bit, so our interface is not found, but a less
886 * specific route. I'd rather like to specify a shorter mask,
887 * but this is not possible. Should work though. XXX
888 * XXX MRT Use a different FIB for the tunnel to solve this problem.
890 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
891 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^=
896 printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
897 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
900 rtalloc_fib(ro, sc->gre_fibnum);
903 * check if this returned a route at all and this route is no
904 * recursion to ourself
906 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
908 if (ro->ro_rt == NULL)
909 printf(" - no route found!\n");
911 printf(" - route loops back to ourself!\n");
913 return EADDRNOTAVAIL;
917 * now change it back - else ip_output will just drop
918 * the route and search one to this interface ...
920 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
921 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
924 printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
925 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
933 * do a checksum of a buffer - much like in_cksum, which operates on
937 gre_in_cksum(u_int16_t *p, u_int len)
940 int nwords = len >> 1;
942 while (nwords-- != 0)
950 u.c[0] = *(u_char *)p;
955 /* end-around-carry */
956 sum = (sum >> 16) + (sum & 0xffff);
962 gremodevent(module_t mod, int type, void *data)
970 if_clone_detach(&gre_cloner);
971 mtx_destroy(&gre_mtx);
979 static moduledata_t gre_mod = {
985 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
986 MODULE_VERSION(if_gre, 1);