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>
57 #include <sys/kernel.h>
58 #include <sys/malloc.h>
59 #include <sys/module.h>
63 #include <sys/protosw.h>
64 #include <sys/socket.h>
65 #include <sys/sockio.h>
66 #include <sys/sysctl.h>
67 #include <sys/systm.h>
69 #include <net/ethernet.h>
71 #include <net/if_clone.h>
72 #include <net/if_types.h>
73 #include <net/route.h>
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_gre.h>
82 #include <netinet/ip_var.h>
83 #include <netinet/ip_encap.h>
85 #error "Huh? if_gre without inet?"
90 #include <net/if_gre.h>
93 * It is not easy to calculate the right value for a GRE MTU.
94 * We leave this task to the admin and use the same default that
102 * gre_mtx protects all global variables in if_gre.c.
103 * XXX: gre_softc data not protected yet.
106 static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
108 struct gre_softc_head gre_softc_list;
110 static int gre_clone_create(struct if_clone *, int, caddr_t);
111 static void gre_clone_destroy(struct ifnet *);
112 static int gre_ioctl(struct ifnet *, u_long, caddr_t);
113 static int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
116 IFC_SIMPLE_DECLARE(gre, 0);
118 static int gre_compute_route(struct gre_softc *sc);
120 static void greattach(void);
123 extern struct domain inetdomain;
124 static const struct protosw in_gre_protosw = {
126 .pr_domain = &inetdomain,
127 .pr_protocol = IPPROTO_GRE,
128 .pr_flags = PR_ATOMIC|PR_ADDR,
129 .pr_input = gre_input,
130 .pr_output = (pr_output_t *)rip_output,
131 .pr_ctlinput = rip_ctlinput,
132 .pr_ctloutput = rip_ctloutput,
133 .pr_usrreqs = &rip_usrreqs
135 static const struct protosw in_mobile_protosw = {
137 .pr_domain = &inetdomain,
138 .pr_protocol = IPPROTO_MOBILE,
139 .pr_flags = PR_ATOMIC|PR_ADDR,
140 .pr_input = gre_mobile_input,
141 .pr_output = (pr_output_t *)rip_output,
142 .pr_ctlinput = rip_ctlinput,
143 .pr_ctloutput = rip_ctloutput,
144 .pr_usrreqs = &rip_usrreqs
148 SYSCTL_DECL(_net_link);
149 SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
150 "Generic Routing Encapsulation");
153 * This macro controls the default upper limitation on nesting of gre tunnels.
154 * Since, setting a large value to this macro with a careless configuration
155 * may introduce system crash, we don't allow any nestings by default.
156 * If you need to configure nested gre tunnels, you can define this macro
157 * in your kernel configuration file. However, if you do so, please be
158 * careful to configure the tunnels so that it won't make a loop.
160 #define MAX_GRE_NEST 1
162 static int max_gre_nesting = MAX_GRE_NEST;
163 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
164 &max_gre_nesting, 0, "Max nested tunnels");
171 mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
172 LIST_INIT(&gre_softc_list);
173 if_clone_attach(&gre_cloner);
177 gre_clone_create(ifc, unit, params)
178 struct if_clone *ifc;
182 struct gre_softc *sc;
184 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
186 GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
187 if (GRE2IFP(sc) == NULL) {
192 GRE2IFP(sc)->if_softc = sc;
193 if_initname(GRE2IFP(sc), ifc->ifc_name, unit);
195 GRE2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen;
196 GRE2IFP(sc)->if_addrlen = 0;
197 GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */
198 GRE2IFP(sc)->if_mtu = GREMTU;
199 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
200 GRE2IFP(sc)->if_output = gre_output;
201 GRE2IFP(sc)->if_ioctl = gre_ioctl;
202 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
203 sc->g_proto = IPPROTO_GRE;
204 GRE2IFP(sc)->if_flags |= IFF_LINK0;
207 sc->gre_fibnum = curthread->td_proc->p_fibnum;
208 sc->wccp_ver = WCCP_V1;
210 if_attach(GRE2IFP(sc));
211 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
213 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
214 mtx_unlock(&gre_mtx);
219 gre_clone_destroy(ifp)
222 struct gre_softc *sc = ifp->if_softc;
225 LIST_REMOVE(sc, sc_list);
226 mtx_unlock(&gre_mtx);
229 if (sc->encap != NULL)
230 encap_detach(sc->encap);
239 * The output routine. Takes a packet and encapsulates it in the protocol
240 * given by sc->g_proto. See also RFC 1701 and RFC 2004
243 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
247 struct gre_softc *sc = ifp->if_softc;
250 u_short gre_ip_id = 0;
251 uint8_t gre_ip_tos = 0;
253 struct mobile_h mob_h;
258 * gre may cause infinite recursion calls when misconfigured.
259 * We'll prevent this by introducing upper limit.
261 if (++(sc->called) > max_gre_nesting) {
262 printf("%s: gre_output: recursively called too many "
263 "times(%d)\n", if_name(GRE2IFP(sc)), sc->called);
265 error = EIO; /* is there better errno? */
269 if (!((ifp->if_flags & IFF_UP) &&
270 (ifp->if_drv_flags & IFF_DRV_RUNNING)) ||
271 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
280 /* BPF writes need to be handled specially. */
281 if (dst->sa_family == AF_UNSPEC) {
282 bcopy(dst->sa_data, &af, sizeof(af));
286 if (bpf_peers_present(ifp->if_bpf)) {
288 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
291 m->m_flags &= ~(M_BCAST|M_MCAST);
293 if (sc->g_proto == IPPROTO_MOBILE) {
294 if (dst->sa_family == AF_INET) {
298 ip = mtod(m, struct ip *);
301 * RFC2004 specifies that fragmented diagrams shouldn't
304 if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
305 _IF_DROP(&ifp->if_snd);
307 error = EINVAL; /* is there better errno? */
310 memset(&mob_h, 0, MOB_H_SIZ_L);
311 mob_h.proto = (ip->ip_p) << 8;
312 mob_h.odst = ip->ip_dst.s_addr;
313 ip->ip_dst.s_addr = sc->g_dst.s_addr;
316 * If the packet comes from our host, we only change
317 * the destination address in the IP header.
318 * Else we also need to save and change the source
320 if (in_hosteq(ip->ip_src, sc->g_src)) {
323 mob_h.proto |= MOB_H_SBIT;
324 mob_h.osrc = ip->ip_src.s_addr;
325 ip->ip_src.s_addr = sc->g_src.s_addr;
328 mob_h.proto = htons(mob_h.proto);
329 mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
331 if ((m->m_data - msiz) < m->m_pktdat) {
333 MGETHDR(m0, M_DONTWAIT, MT_DATA);
335 _IF_DROP(&ifp->if_snd);
341 m->m_data += sizeof(struct ip);
342 m->m_len -= sizeof(struct ip);
343 m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
344 m0->m_len = msiz + sizeof(struct ip);
345 m0->m_data += max_linkhdr;
346 memcpy(mtod(m0, caddr_t), (caddr_t)ip,
349 } else { /* we have some space left in the old one */
352 m->m_pkthdr.len += msiz;
353 bcopy(ip, mtod(m, caddr_t),
356 ip = mtod(m, struct ip *);
357 memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
358 ip->ip_len = ntohs(ip->ip_len) + msiz;
359 } else { /* AF_INET */
360 _IF_DROP(&ifp->if_snd);
365 } else if (sc->g_proto == IPPROTO_GRE) {
366 switch (dst->sa_family) {
368 ip = mtod(m, struct ip *);
369 gre_ip_tos = ip->ip_tos;
370 gre_ip_id = ip->ip_id;
371 if (sc->wccp_ver == WCCP_V2) {
372 extra = sizeof(uint32_t);
373 etype = WCCP_PROTOCOL_TYPE;
375 etype = ETHERTYPE_IP;
380 gre_ip_id = ip_newid();
381 etype = ETHERTYPE_IPV6;
386 etype = ETHERTYPE_ATALK;
390 _IF_DROP(&ifp->if_snd);
392 error = EAFNOSUPPORT;
396 /* Reserve space for GRE header + optional GRE key */
397 int hdrlen = sizeof(struct greip) + extra;
399 hdrlen += sizeof(uint32_t);
400 M_PREPEND(m, hdrlen, M_DONTWAIT);
402 _IF_DROP(&ifp->if_snd);
408 if (m == NULL) { /* mbuf allocation failed */
409 _IF_DROP(&ifp->if_snd);
414 M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */
416 gh = mtod(m, struct greip *);
417 if (sc->g_proto == IPPROTO_GRE) {
418 uint32_t *options = gh->gi_options;
420 memset((void *)gh, 0, sizeof(struct greip) + extra);
421 gh->gi_ptype = htons(etype);
427 gh->gi_flags |= htons(GRE_KP);
428 *(options++) = htonl(sc->key);
432 gh->gi_pr = sc->g_proto;
433 if (sc->g_proto != IPPROTO_MOBILE) {
434 gh->gi_src = sc->g_src;
435 gh->gi_dst = sc->g_dst;
436 ((struct ip*)gh)->ip_v = IPPROTO_IPV4;
437 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
438 ((struct ip*)gh)->ip_ttl = GRE_TTL;
439 ((struct ip*)gh)->ip_tos = gre_ip_tos;
440 ((struct ip*)gh)->ip_id = gre_ip_id;
441 gh->gi_len = m->m_pkthdr.len;
445 ifp->if_obytes += m->m_pkthdr.len;
447 * Send it off and with IP_FORWARD flag to prevent it from
448 * overwriting the ip_id again. ip_id is already set to the
449 * ip_id of the encapsulated packet.
451 error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
452 (struct ip_moptions *)NULL, (struct inpcb *)NULL);
461 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
463 struct ifreq *ifr = (struct ifreq *)data;
464 struct if_laddrreq *lifr = (struct if_laddrreq *)data;
465 struct in_aliasreq *aifr = (struct in_aliasreq *)data;
466 struct gre_softc *sc = ifp->if_softc;
468 struct sockaddr_in si;
469 struct sockaddr *sa = NULL;
471 struct sockaddr_in sp, sm, dp, dm;
480 ifp->if_flags |= IFF_UP;
486 * XXXRW: Isn't this priv_check() redundant to the ifnet
489 if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0)
491 if ((ifr->ifr_flags & IFF_LINK0) != 0)
492 sc->g_proto = IPPROTO_GRE;
494 sc->g_proto = IPPROTO_MOBILE;
495 if ((ifr->ifr_flags & IFF_LINK2) != 0)
496 sc->wccp_ver = WCCP_V2;
498 sc->wccp_ver = WCCP_V1;
502 * XXXRW: Isn't this priv_check() redundant to the ifnet
505 if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0)
507 if (ifr->ifr_mtu < 576) {
511 ifp->if_mtu = ifr->ifr_mtu;
514 ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
518 * XXXRW: Isn't this priv_checkr() redundant to the ifnet
521 if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0)
524 error = EAFNOSUPPORT;
527 switch (ifr->ifr_addr.sa_family) {
537 error = EAFNOSUPPORT;
543 * XXXRW: Isn't this priv_check() redundant to the ifnet
546 if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0)
549 error = EAFNOSUPPORT;
552 switch (ifr->ifr_addr.sa_family) {
562 error = EAFNOSUPPORT;
568 * XXXRW: Isn't this priv_check() redundant to the ifnet
571 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
573 sc->g_proto = ifr->ifr_flags;
574 switch (sc->g_proto) {
576 ifp->if_flags |= IFF_LINK0;
579 ifp->if_flags &= ~IFF_LINK0;
582 error = EPROTONOSUPPORT;
587 ifr->ifr_flags = sc->g_proto;
591 error = priv_check(curthread, PRIV_NET_GRE);
595 * set tunnel endpoints, compute a less specific route
596 * to the remote end and mark if as up
599 if (cmd == GRESADDRS)
600 sc->g_src = (satosin(sa))->sin_addr;
601 if (cmd == GRESADDRD)
602 sc->g_dst = (satosin(sa))->sin_addr;
605 if (sc->encap != NULL) {
606 encap_detach(sc->encap);
610 if ((sc->g_src.s_addr != INADDR_ANY) &&
611 (sc->g_dst.s_addr != INADDR_ANY)) {
612 bzero(&sp, sizeof(sp));
613 bzero(&sm, sizeof(sm));
614 bzero(&dp, sizeof(dp));
615 bzero(&dm, sizeof(dm));
616 sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
617 sizeof(struct sockaddr_in);
618 sp.sin_family = sm.sin_family = dp.sin_family =
619 dm.sin_family = AF_INET;
620 sp.sin_addr = sc->g_src;
621 dp.sin_addr = sc->g_dst;
622 sm.sin_addr.s_addr = dm.sin_addr.s_addr =
625 sc->encap = encap_attach(AF_INET, sc->g_proto,
626 sintosa(&sp), sintosa(&sm), sintosa(&dp),
627 sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
628 &in_gre_protosw : &in_mobile_protosw, sc);
629 if (sc->encap == NULL)
630 printf("%s: unable to attach encap\n",
631 if_name(GRE2IFP(sc)));
633 if (sc->route.ro_rt != 0) /* free old route */
634 RTFREE(sc->route.ro_rt);
635 if (gre_compute_route(sc) == 0)
636 ifp->if_drv_flags |= IFF_DRV_RUNNING;
638 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
642 memset(&si, 0, sizeof(si));
643 si.sin_family = AF_INET;
644 si.sin_len = sizeof(struct sockaddr_in);
645 si.sin_addr.s_addr = sc->g_src.s_addr;
647 error = prison_if(curthread->td_ucred, sa);
653 memset(&si, 0, sizeof(si));
654 si.sin_family = AF_INET;
655 si.sin_len = sizeof(struct sockaddr_in);
656 si.sin_addr.s_addr = sc->g_dst.s_addr;
658 error = prison_if(curthread->td_ucred, sa);
665 * XXXRW: Isn't this priv_check() redundant to the ifnet
668 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
670 if (aifr->ifra_addr.sin_family != AF_INET ||
671 aifr->ifra_dstaddr.sin_family != AF_INET) {
672 error = EAFNOSUPPORT;
675 if (aifr->ifra_addr.sin_len != sizeof(si) ||
676 aifr->ifra_dstaddr.sin_len != sizeof(si)) {
680 sc->g_src = aifr->ifra_addr.sin_addr;
681 sc->g_dst = aifr->ifra_dstaddr.sin_addr;
683 case SIOCSLIFPHYADDR:
685 * XXXRW: Isn't this priv_check() redundant to the ifnet
688 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
690 if (lifr->addr.ss_family != AF_INET ||
691 lifr->dstaddr.ss_family != AF_INET) {
692 error = EAFNOSUPPORT;
695 if (lifr->addr.ss_len != sizeof(si) ||
696 lifr->dstaddr.ss_len != sizeof(si)) {
700 sc->g_src = (satosin(&lifr->addr))->sin_addr;
702 (satosin(&lifr->dstaddr))->sin_addr;
706 * XXXRW: Isn't this priv_check() redundant to the ifnet
709 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
711 sc->g_src.s_addr = INADDR_ANY;
712 sc->g_dst.s_addr = INADDR_ANY;
714 case SIOCGLIFPHYADDR:
715 if (sc->g_src.s_addr == INADDR_ANY ||
716 sc->g_dst.s_addr == INADDR_ANY) {
717 error = EADDRNOTAVAIL;
720 memset(&si, 0, sizeof(si));
721 si.sin_family = AF_INET;
722 si.sin_len = sizeof(struct sockaddr_in);
723 si.sin_addr.s_addr = sc->g_src.s_addr;
724 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
727 memcpy(&lifr->addr, &si, sizeof(si));
728 si.sin_addr.s_addr = sc->g_dst.s_addr;
729 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
732 memcpy(&lifr->dstaddr, &si, sizeof(si));
734 case SIOCGIFPSRCADDR:
736 case SIOCGIFPSRCADDR_IN6:
738 if (sc->g_src.s_addr == INADDR_ANY) {
739 error = EADDRNOTAVAIL;
742 memset(&si, 0, sizeof(si));
743 si.sin_family = AF_INET;
744 si.sin_len = sizeof(struct sockaddr_in);
745 si.sin_addr.s_addr = sc->g_src.s_addr;
746 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
749 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
751 case SIOCGIFPDSTADDR:
753 case SIOCGIFPDSTADDR_IN6:
755 if (sc->g_dst.s_addr == INADDR_ANY) {
756 error = EADDRNOTAVAIL;
759 memset(&si, 0, sizeof(si));
760 si.sin_family = AF_INET;
761 si.sin_len = sizeof(struct sockaddr_in);
762 si.sin_addr.s_addr = sc->g_dst.s_addr;
763 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
766 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
769 error = priv_check(curthread, PRIV_NET_GRE);
772 error = copyin(ifr->ifr_data, &key, sizeof(key));
775 /* adjust MTU for option header */
776 if (key == 0 && sc->key != 0) /* clear */
778 else if (key != 0 && sc->key == 0) /* set */
781 if (ifp->if_mtu + adj < 576) {
789 error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key));
802 * computes a route to our destination that is not the one
803 * which would be taken by ip_output(), as this one will loop back to
804 * us. If the interface is p2p as a--->b, then a routing entry exists
805 * If we now send a packet to b (e.g. ping b), this will come down here
806 * gets src=a, dst=b tacked on and would from ip_output() sent back to
808 * Goal here is to compute a route to b that is less specific than
809 * a-->b. We know that this one exists as in normal operation we have
810 * at least a default route which matches.
813 gre_compute_route(struct gre_softc *sc)
819 memset(ro, 0, sizeof(struct route));
820 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
821 ro->ro_dst.sa_family = AF_INET;
822 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
825 * toggle last bit, so our interface is not found, but a less
826 * specific route. I'd rather like to specify a shorter mask,
827 * but this is not possible. Should work though. XXX
828 * XXX MRT Use a different FIB for the tunnel to solve this problem.
830 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
831 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^=
836 printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
837 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
840 rtalloc_fib(ro, sc->gre_fibnum);
843 * check if this returned a route at all and this route is no
844 * recursion to ourself
846 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
848 if (ro->ro_rt == NULL)
849 printf(" - no route found!\n");
851 printf(" - route loops back to ourself!\n");
853 return EADDRNOTAVAIL;
857 * now change it back - else ip_output will just drop
858 * the route and search one to this interface ...
860 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
861 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
864 printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
865 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
873 * do a checksum of a buffer - much like in_cksum, which operates on
877 gre_in_cksum(u_int16_t *p, u_int len)
880 int nwords = len >> 1;
882 while (nwords-- != 0)
890 u.c[0] = *(u_char *)p;
895 /* end-around-carry */
896 sum = (sum >> 16) + (sum & 0xffff);
902 gremodevent(module_t mod, int type, void *data)
910 if_clone_detach(&gre_cloner);
911 mtx_destroy(&gre_mtx);
919 static moduledata_t gre_mod = {
925 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
926 MODULE_VERSION(if_gre, 1);