]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/net/if_gre.c
MFC r271918 (by hrs):
[FreeBSD/stable/10.git] / sys / net / if_gre.c
1 /*      $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
2 /*       $FreeBSD$ */
3
4 /*-
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Heiko W.Rupp <hwr@pilhuhn.de>
10  *
11  * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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  *
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.
33  */
34
35 /*
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
42  */
43
44 #include "opt_atalk.h"
45 #include "opt_inet.h"
46 #include "opt_inet6.h"
47
48 #include <sys/param.h>
49 #include <sys/jail.h>
50 #include <sys/kernel.h>
51 #include <sys/libkern.h>
52 #include <sys/malloc.h>
53 #include <sys/module.h>
54 #include <sys/mbuf.h>
55 #include <sys/priv.h>
56 #include <sys/proc.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>
62
63 #include <net/ethernet.h>
64 #include <net/if.h>
65 #include <net/if_clone.h>
66 #include <net/if_types.h>
67 #include <net/route.h>
68 #include <net/vnet.h>
69
70 #ifdef INET
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>
78 #else
79 #error "Huh? if_gre without inet?"
80 #endif
81
82 #include <net/bpf.h>
83
84 #include <net/if_gre.h>
85
86 /*
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
89  * other vendors use.
90  */
91 #define GREMTU  1476
92
93 #define MTAG_COOKIE_GRE         1307983903
94 #define MTAG_GRE_NESTING        1
95 struct mtag_gre_nesting {
96         uint16_t        count;
97         uint16_t        max;
98         struct ifnet    *ifp[];
99 };
100
101 /*
102  * gre_mtx protects all global variables in if_gre.c.
103  * XXX: gre_softc data not protected yet.
104  */
105 VNET_DEFINE(struct mtx, gre_mtx);
106 VNET_DEFINE(struct gre_softc_head, gre_softc_list);
107
108 static const char grename[] = "gre";
109 static MALLOC_DEFINE(M_GRE, grename, "Generic Routing Encapsulation");
110
111 static int      gre_clone_create(struct if_clone *, int, caddr_t);
112 static void     gre_clone_destroy(struct ifnet *);
113 static VNET_DEFINE(struct if_clone *, gre_cloner);
114 #define V_gre_cloner    VNET(gre_cloner)
115
116 static int      gre_ioctl(struct ifnet *, u_long, caddr_t);
117 static int      gre_output(struct ifnet *, struct mbuf *,
118                     const struct sockaddr *, struct route *);
119
120 static int gre_compute_route(struct gre_softc *sc);
121
122 #ifdef INET
123 extern struct domain inetdomain;
124 static const struct protosw in_gre_protosw = {
125         .pr_type =              SOCK_RAW,
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
134 };
135 static const struct protosw in_mobile_protosw = {
136         .pr_type =              SOCK_RAW,
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
145 };
146 #endif
147
148 SYSCTL_DECL(_net_link);
149 static SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
150     "Generic Routing Encapsulation");
151 #ifndef MAX_GRE_NEST
152 /*
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.
159  */
160 #define MAX_GRE_NEST 1
161 #endif
162 static VNET_DEFINE(int, max_gre_nesting) = MAX_GRE_NEST;
163 #define V_max_gre_nesting       VNET(max_gre_nesting)
164 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET,
165     &VNET_NAME(max_gre_nesting), 0, "Max nested tunnels");
166
167 static void
168 vnet_gre_init(const void *unused __unused)
169 {
170         LIST_INIT(&V_gre_softc_list);
171         GRE_LIST_LOCK_INIT();
172         V_gre_cloner = if_clone_simple(grename, gre_clone_create,
173             gre_clone_destroy, 0);
174 }
175 VNET_SYSINIT(vnet_gre_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
176     vnet_gre_init, NULL);
177
178 static void
179 vnet_gre_uninit(const void *unused __unused)
180 {
181
182         if_clone_detach(V_gre_cloner);
183         GRE_LIST_LOCK_DESTROY();
184 }
185 VNET_SYSUNINIT(vnet_gre_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
186     vnet_gre_uninit, NULL);
187
188 static int
189 gre_clone_create(struct if_clone *ifc, int unit, caddr_t params)
190 {
191         struct gre_softc *sc;
192
193         sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
194
195         GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
196         if (GRE2IFP(sc) == NULL) {
197                 free(sc, M_GRE);
198                 return (ENOSPC);
199         }
200
201         GRE2IFP(sc)->if_softc = sc;
202         if_initname(GRE2IFP(sc), grename, unit);
203
204         GRE2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen;
205         GRE2IFP(sc)->if_addrlen = 0;
206         GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */
207         GRE2IFP(sc)->if_mtu = GREMTU;
208         GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
209         GRE2IFP(sc)->if_output = gre_output;
210         GRE2IFP(sc)->if_ioctl = gre_ioctl;
211         sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
212         sc->g_proto = IPPROTO_GRE;
213         GRE2IFP(sc)->if_flags |= IFF_LINK0;
214         sc->encap = NULL;
215         sc->gre_fibnum = curthread->td_proc->p_fibnum;
216         sc->wccp_ver = WCCP_V1;
217         sc->key = 0;
218         if_attach(GRE2IFP(sc));
219         bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
220         GRE_LIST_LOCK();
221         LIST_INSERT_HEAD(&V_gre_softc_list, sc, sc_list);
222         GRE_LIST_UNLOCK();
223         return (0);
224 }
225
226 static void
227 gre_clone_destroy(struct ifnet *ifp)
228 {
229         struct gre_softc *sc = ifp->if_softc;
230
231         GRE_LIST_LOCK();
232         LIST_REMOVE(sc, sc_list);
233         GRE_LIST_UNLOCK();
234
235 #ifdef INET
236         if (sc->encap != NULL)
237                 encap_detach(sc->encap);
238 #endif
239         bpfdetach(ifp);
240         if_detach(ifp);
241         if_free(ifp);
242         free(sc, M_GRE);
243 }
244
245 /*
246  * The output routine. Takes a packet and encapsulates it in the protocol
247  * given by sc->g_proto. See also RFC 1701 and RFC 2004
248  */
249 static int
250 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
251            struct route *ro)
252 {
253         int error = 0;
254         struct gre_softc *sc = ifp->if_softc;
255         struct greip *gh;
256         struct ip *ip;
257         struct m_tag *mtag;
258         struct mtag_gre_nesting *gt;
259         size_t len;
260         u_short gre_ip_id = 0;
261         uint8_t gre_ip_tos = 0;
262         u_int16_t etype = 0;
263         struct mobile_h mob_h;
264         u_int32_t af;
265         int extra = 0, max;
266
267         /*
268          * gre may cause infinite recursion calls when misconfigured.  High
269          * nesting level may cause stack exhaustion.  We'll prevent this by
270          * detecting loops and by introducing upper limit.
271          */
272         mtag = m_tag_locate(m, MTAG_COOKIE_GRE, MTAG_GRE_NESTING, NULL);
273         if (mtag != NULL) {
274                 struct ifnet **ifp2;
275
276                 gt = (struct mtag_gre_nesting *)(mtag + 1);
277                 gt->count++;
278                 if (gt->count > min(gt->max, V_max_gre_nesting)) {
279                         printf("%s: hit maximum recursion limit %u on %s\n",
280                                 __func__, gt->count - 1, ifp->if_xname);
281                         m_freem(m);
282                         error = EIO;    /* is there better errno? */
283                         goto end;
284                 }
285
286                 ifp2 = gt->ifp;
287                 for (max = gt->count - 1; max > 0; max--) {
288                         if (*ifp2 == ifp)
289                                 break;
290                         ifp2++;
291                 }
292                 if (*ifp2 == ifp) {
293                         printf("%s: detected loop with nexting %u on %s\n",
294                                 __func__, gt->count-1, ifp->if_xname);
295                         m_freem(m);
296                         error = EIO;    /* is there better errno? */
297                         goto end;
298                 }
299                 *ifp2 = ifp;
300
301         } else {
302                 /*
303                  * Given that people should NOT increase max_gre_nesting beyond
304                  * their real needs, we allocate once per packet rather than
305                  * allocating an mtag once per passing through gre.
306                  *
307                  * Note: the sysctl does not actually check for saneness, so we
308                  * limit the maximum numbers of possible recursions here.
309                  */
310                 max = imin(V_max_gre_nesting, 256);
311                 /* If someone sets the sysctl <= 0, we want at least 1. */
312                 max = imax(max, 1);
313                 len = sizeof(struct mtag_gre_nesting) +
314                     max * sizeof(struct ifnet *);
315                 mtag = m_tag_alloc(MTAG_COOKIE_GRE, MTAG_GRE_NESTING, len,
316                     M_NOWAIT);
317                 if (mtag == NULL) {
318                         m_freem(m);
319                         error = ENOMEM;
320                         goto end;
321                 }
322                 gt = (struct mtag_gre_nesting *)(mtag + 1);
323                 bzero(gt, len);
324                 gt->count = 1;
325                 gt->max = max;
326                 *gt->ifp = ifp;
327                 m_tag_prepend(m, mtag);
328         }
329
330         if (!((ifp->if_flags & IFF_UP) &&
331             (ifp->if_drv_flags & IFF_DRV_RUNNING)) ||
332             sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
333                 m_freem(m);
334                 error = ENETDOWN;
335                 goto end;
336         }
337
338         gh = NULL;
339         ip = NULL;
340
341         /* BPF writes need to be handled specially. */
342         if (dst->sa_family == AF_UNSPEC)
343                 bcopy(dst->sa_data, &af, sizeof(af));
344         else
345                 af = dst->sa_family;
346
347         if (bpf_peers_present(ifp->if_bpf))
348                 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
349
350         if ((ifp->if_flags & IFF_MONITOR) != 0) {
351                 m_freem(m);
352                 error = ENETDOWN;
353                 goto end;
354         }
355
356         m->m_flags &= ~(M_BCAST|M_MCAST);
357
358         if (sc->g_proto == IPPROTO_MOBILE) {
359                 if (af == AF_INET) {
360                         struct mbuf *m0;
361                         int msiz;
362
363                         ip = mtod(m, struct ip *);
364
365                         /*
366                          * RFC2004 specifies that fragmented diagrams shouldn't
367                          * be encapsulated.
368                          */
369                         if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
370                                 _IF_DROP(&ifp->if_snd);
371                                 m_freem(m);
372                                 error = EINVAL;    /* is there better errno? */
373                                 goto end;
374                         }
375                         memset(&mob_h, 0, MOB_H_SIZ_L);
376                         mob_h.proto = (ip->ip_p) << 8;
377                         mob_h.odst = ip->ip_dst.s_addr;
378                         ip->ip_dst.s_addr = sc->g_dst.s_addr;
379
380                         /*
381                          * If the packet comes from our host, we only change
382                          * the destination address in the IP header.
383                          * Else we also need to save and change the source
384                          */
385                         if (in_hosteq(ip->ip_src, sc->g_src)) {
386                                 msiz = MOB_H_SIZ_S;
387                         } else {
388                                 mob_h.proto |= MOB_H_SBIT;
389                                 mob_h.osrc = ip->ip_src.s_addr;
390                                 ip->ip_src.s_addr = sc->g_src.s_addr;
391                                 msiz = MOB_H_SIZ_L;
392                         }
393                         mob_h.proto = htons(mob_h.proto);
394                         mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
395
396                         if ((m->m_data - msiz) < m->m_pktdat) {
397                                 m0 = m_gethdr(M_NOWAIT, MT_DATA);
398                                 if (m0 == NULL) {
399                                         _IF_DROP(&ifp->if_snd);
400                                         m_freem(m);
401                                         error = ENOBUFS;
402                                         goto end;
403                                 }
404                                 m0->m_next = m;
405                                 m->m_data += sizeof(struct ip);
406                                 m->m_len -= sizeof(struct ip);
407                                 m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
408                                 m0->m_len = msiz + sizeof(struct ip);
409                                 m0->m_data += max_linkhdr;
410                                 memcpy(mtod(m0, caddr_t), (caddr_t)ip,
411                                        sizeof(struct ip));
412                                 m = m0;
413                         } else {  /* we have some space left in the old one */
414                                 m->m_data -= msiz;
415                                 m->m_len += msiz;
416                                 m->m_pkthdr.len += msiz;
417                                 bcopy(ip, mtod(m, caddr_t),
418                                         sizeof(struct ip));
419                         }
420                         ip = mtod(m, struct ip *);
421                         memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
422                         ip->ip_len = htons(ntohs(ip->ip_len) + msiz);
423                 } else {  /* AF_INET */
424                         _IF_DROP(&ifp->if_snd);
425                         m_freem(m);
426                         error = EINVAL;
427                         goto end;
428                 }
429         } else if (sc->g_proto == IPPROTO_GRE) {
430                 switch (af) {
431                 case AF_INET:
432                         ip = mtod(m, struct ip *);
433                         gre_ip_tos = ip->ip_tos;
434                         gre_ip_id = ip->ip_id;
435                         if (sc->wccp_ver == WCCP_V2) {
436                                 extra = sizeof(uint32_t);
437                                 etype =  WCCP_PROTOCOL_TYPE;
438                         } else {
439                                 etype = ETHERTYPE_IP;
440                         }
441                         break;
442 #ifdef INET6
443                 case AF_INET6:
444                         gre_ip_id = ip_newid();
445                         etype = ETHERTYPE_IPV6;
446                         break;
447 #endif
448 #ifdef NETATALK
449                 case AF_APPLETALK:
450                         etype = ETHERTYPE_ATALK;
451                         break;
452 #endif
453                 default:
454                         _IF_DROP(&ifp->if_snd);
455                         m_freem(m);
456                         error = EAFNOSUPPORT;
457                         goto end;
458                 }
459                         
460                 /* Reserve space for GRE header + optional GRE key */
461                 int hdrlen = sizeof(struct greip) + extra;
462                 if (sc->key)
463                         hdrlen += sizeof(uint32_t);
464                 M_PREPEND(m, hdrlen, M_NOWAIT);
465         } else {
466                 _IF_DROP(&ifp->if_snd);
467                 m_freem(m);
468                 error = EINVAL;
469                 goto end;
470         }
471
472         if (m == NULL) {        /* mbuf allocation failed */
473                 _IF_DROP(&ifp->if_snd);
474                 error = ENOBUFS;
475                 goto end;
476         }
477
478         M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */
479
480         gh = mtod(m, struct greip *);
481         if (sc->g_proto == IPPROTO_GRE) {
482                 uint32_t *options = gh->gi_options;
483
484                 memset((void *)gh, 0, sizeof(struct greip) + extra);
485                 gh->gi_ptype = htons(etype);
486                 gh->gi_flags = 0;
487
488                 /* Add key option */
489                 if (sc->key)
490                 {
491                         gh->gi_flags |= htons(GRE_KP);
492                         *(options++) = htonl(sc->key);
493                 }
494         }
495
496         gh->gi_pr = sc->g_proto;
497         if (sc->g_proto != IPPROTO_MOBILE) {
498                 gh->gi_src = sc->g_src;
499                 gh->gi_dst = sc->g_dst;
500                 ((struct ip*)gh)->ip_v = IPPROTO_IPV4;
501                 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
502                 ((struct ip*)gh)->ip_ttl = GRE_TTL;
503                 ((struct ip*)gh)->ip_tos = gre_ip_tos;
504                 ((struct ip*)gh)->ip_id = gre_ip_id;
505                 gh->gi_len = htons(m->m_pkthdr.len);
506         }
507
508         ifp->if_opackets++;
509         ifp->if_obytes += m->m_pkthdr.len;
510         /*
511          * Send it off and with IP_FORWARD flag to prevent it from
512          * overwriting the ip_id again.  ip_id is already set to the
513          * ip_id of the encapsulated packet.
514          */
515         error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
516             (struct ip_moptions *)NULL, (struct inpcb *)NULL);
517   end:
518         if (error)
519                 ifp->if_oerrors++;
520         return (error);
521 }
522
523 static int
524 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
525 {
526         struct ifreq *ifr = (struct ifreq *)data;
527         struct if_laddrreq *lifr = (struct if_laddrreq *)data;
528         struct in_aliasreq *aifr = (struct in_aliasreq *)data;
529         struct gre_softc *sc = ifp->if_softc;
530         struct sockaddr_in si;
531         struct sockaddr *sa = NULL;
532         int error, adj;
533         struct sockaddr_in sp, sm, dp, dm;
534         uint32_t key;
535
536         error = 0;
537         adj = 0;
538
539         switch (cmd) {
540         case SIOCSIFADDR:
541                 ifp->if_flags |= IFF_UP;
542                 break;
543         case SIOCSIFFLAGS:
544                 /*
545                  * XXXRW: Isn't this priv_check() redundant to the ifnet
546                  * layer check?
547                  */
548                 if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0)
549                         break;
550                 if ((ifr->ifr_flags & IFF_LINK0) != 0)
551                         sc->g_proto = IPPROTO_GRE;
552                 else
553                         sc->g_proto = IPPROTO_MOBILE;
554                 if ((ifr->ifr_flags & IFF_LINK2) != 0)
555                         sc->wccp_ver = WCCP_V2;
556                 else
557                         sc->wccp_ver = WCCP_V1;
558                 goto recompute;
559         case SIOCSIFMTU:
560                 /*
561                  * XXXRW: Isn't this priv_check() redundant to the ifnet
562                  * layer check?
563                  */
564                 if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0)
565                         break;
566                 if (ifr->ifr_mtu < 576) {
567                         error = EINVAL;
568                         break;
569                 }
570                 ifp->if_mtu = ifr->ifr_mtu;
571                 break;
572         case SIOCGIFMTU:
573                 ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
574                 break;
575         case SIOCADDMULTI:
576                 /*
577                  * XXXRW: Isn't this priv_checkr() redundant to the ifnet
578                  * layer check?
579                  */
580                 if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0)
581                         break;
582                 if (ifr == 0) {
583                         error = EAFNOSUPPORT;
584                         break;
585                 }
586                 switch (ifr->ifr_addr.sa_family) {
587 #ifdef INET
588                 case AF_INET:
589                         break;
590 #endif
591 #ifdef INET6
592                 case AF_INET6:
593                         break;
594 #endif
595                 default:
596                         error = EAFNOSUPPORT;
597                         break;
598                 }
599                 break;
600         case SIOCDELMULTI:
601                 /*
602                  * XXXRW: Isn't this priv_check() redundant to the ifnet
603                  * layer check?
604                  */
605                 if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0)
606                         break;
607                 if (ifr == 0) {
608                         error = EAFNOSUPPORT;
609                         break;
610                 }
611                 switch (ifr->ifr_addr.sa_family) {
612 #ifdef INET
613                 case AF_INET:
614                         break;
615 #endif
616 #ifdef INET6
617                 case AF_INET6:
618                         break;
619 #endif
620                 default:
621                         error = EAFNOSUPPORT;
622                         break;
623                 }
624                 break;
625         case GRESPROTO:
626                 /*
627                  * XXXRW: Isn't this priv_check() redundant to the ifnet
628                  * layer check?
629                  */
630                 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
631                         break;
632                 sc->g_proto = ifr->ifr_flags;
633                 switch (sc->g_proto) {
634                 case IPPROTO_GRE:
635                         ifp->if_flags |= IFF_LINK0;
636                         break;
637                 case IPPROTO_MOBILE:
638                         ifp->if_flags &= ~IFF_LINK0;
639                         break;
640                 default:
641                         error = EPROTONOSUPPORT;
642                         break;
643                 }
644                 goto recompute;
645         case GREGPROTO:
646                 ifr->ifr_flags = sc->g_proto;
647                 break;
648         case GRESADDRS:
649         case GRESADDRD:
650                 error = priv_check(curthread, PRIV_NET_GRE);
651                 if (error)
652                         return (error);
653                 /*
654                  * set tunnel endpoints, compute a less specific route
655                  * to the remote end and mark if as up
656                  */
657                 sa = &ifr->ifr_addr;
658                 if (cmd == GRESADDRS)
659                         sc->g_src = (satosin(sa))->sin_addr;
660                 if (cmd == GRESADDRD)
661                         sc->g_dst = (satosin(sa))->sin_addr;
662         recompute:
663 #ifdef INET
664                 if (sc->encap != NULL) {
665                         encap_detach(sc->encap);
666                         sc->encap = NULL;
667                 }
668 #endif
669                 if ((sc->g_src.s_addr != INADDR_ANY) &&
670                     (sc->g_dst.s_addr != INADDR_ANY)) {
671                         bzero(&sp, sizeof(sp));
672                         bzero(&sm, sizeof(sm));
673                         bzero(&dp, sizeof(dp));
674                         bzero(&dm, sizeof(dm));
675                         sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
676                             sizeof(struct sockaddr_in);
677                         sp.sin_family = sm.sin_family = dp.sin_family =
678                             dm.sin_family = AF_INET;
679                         sp.sin_addr = sc->g_src;
680                         dp.sin_addr = sc->g_dst;
681                         sm.sin_addr.s_addr = dm.sin_addr.s_addr =
682                             INADDR_BROADCAST;
683 #ifdef INET
684                         sc->encap = encap_attach(AF_INET, sc->g_proto,
685                             sintosa(&sp), sintosa(&sm), sintosa(&dp),
686                             sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
687                                 &in_gre_protosw : &in_mobile_protosw, sc);
688                         if (sc->encap == NULL)
689                                 printf("%s: unable to attach encap\n",
690                                     if_name(GRE2IFP(sc)));
691 #endif
692                         if (sc->route.ro_rt != 0) /* free old route */
693                                 RTFREE(sc->route.ro_rt);
694                         if (gre_compute_route(sc) == 0)
695                                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
696                         else
697                                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
698                 }
699                 break;
700         case GREGADDRS:
701                 memset(&si, 0, sizeof(si));
702                 si.sin_family = AF_INET;
703                 si.sin_len = sizeof(struct sockaddr_in);
704                 si.sin_addr.s_addr = sc->g_src.s_addr;
705                 sa = sintosa(&si);
706                 error = prison_if(curthread->td_ucred, sa);
707                 if (error != 0)
708                         break;
709                 ifr->ifr_addr = *sa;
710                 break;
711         case GREGADDRD:
712                 memset(&si, 0, sizeof(si));
713                 si.sin_family = AF_INET;
714                 si.sin_len = sizeof(struct sockaddr_in);
715                 si.sin_addr.s_addr = sc->g_dst.s_addr;
716                 sa = sintosa(&si);
717                 error = prison_if(curthread->td_ucred, sa);
718                 if (error != 0)
719                         break;
720                 ifr->ifr_addr = *sa;
721                 break;
722         case SIOCSIFPHYADDR:
723                 /*
724                  * XXXRW: Isn't this priv_check() redundant to the ifnet
725                  * layer check?
726                  */
727                 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
728                         break;
729                 if (aifr->ifra_addr.sin_family != AF_INET ||
730                     aifr->ifra_dstaddr.sin_family != AF_INET) {
731                         error = EAFNOSUPPORT;
732                         break;
733                 }
734                 if (aifr->ifra_addr.sin_len != sizeof(si) ||
735                     aifr->ifra_dstaddr.sin_len != sizeof(si)) {
736                         error = EINVAL;
737                         break;
738                 }
739                 sc->g_src = aifr->ifra_addr.sin_addr;
740                 sc->g_dst = aifr->ifra_dstaddr.sin_addr;
741                 goto recompute;
742         case SIOCSLIFPHYADDR:
743                 /*
744                  * XXXRW: Isn't this priv_check() redundant to the ifnet
745                  * layer check?
746                  */
747                 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
748                         break;
749                 if (lifr->addr.ss_family != AF_INET ||
750                     lifr->dstaddr.ss_family != AF_INET) {
751                         error = EAFNOSUPPORT;
752                         break;
753                 }
754                 if (lifr->addr.ss_len != sizeof(si) ||
755                     lifr->dstaddr.ss_len != sizeof(si)) {
756                         error = EINVAL;
757                         break;
758                 }
759                 sc->g_src = (satosin(&lifr->addr))->sin_addr;
760                 sc->g_dst =
761                     (satosin(&lifr->dstaddr))->sin_addr;
762                 goto recompute;
763         case SIOCDIFPHYADDR:
764                 /*
765                  * XXXRW: Isn't this priv_check() redundant to the ifnet
766                  * layer check?
767                  */
768                 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
769                         break;
770                 sc->g_src.s_addr = INADDR_ANY;
771                 sc->g_dst.s_addr = INADDR_ANY;
772                 goto recompute;
773         case SIOCGLIFPHYADDR:
774                 if (sc->g_src.s_addr == INADDR_ANY ||
775                     sc->g_dst.s_addr == INADDR_ANY) {
776                         error = EADDRNOTAVAIL;
777                         break;
778                 }
779                 memset(&si, 0, sizeof(si));
780                 si.sin_family = AF_INET;
781                 si.sin_len = sizeof(struct sockaddr_in);
782                 si.sin_addr.s_addr = sc->g_src.s_addr;
783                 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
784                 if (error != 0)
785                         break;
786                 memcpy(&lifr->addr, &si, sizeof(si));
787                 si.sin_addr.s_addr = sc->g_dst.s_addr;
788                 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
789                 if (error != 0)
790                         break;
791                 memcpy(&lifr->dstaddr, &si, sizeof(si));
792                 break;
793         case SIOCGIFPSRCADDR:
794 #ifdef INET6
795         case SIOCGIFPSRCADDR_IN6:
796 #endif
797                 if (sc->g_src.s_addr == INADDR_ANY) {
798                         error = EADDRNOTAVAIL;
799                         break;
800                 }
801                 memset(&si, 0, sizeof(si));
802                 si.sin_family = AF_INET;
803                 si.sin_len = sizeof(struct sockaddr_in);
804                 si.sin_addr.s_addr = sc->g_src.s_addr;
805                 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
806                 if (error != 0)
807                         break;
808                 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
809                 break;
810         case SIOCGIFPDSTADDR:
811 #ifdef INET6
812         case SIOCGIFPDSTADDR_IN6:
813 #endif
814                 if (sc->g_dst.s_addr == INADDR_ANY) {
815                         error = EADDRNOTAVAIL;
816                         break;
817                 }
818                 memset(&si, 0, sizeof(si));
819                 si.sin_family = AF_INET;
820                 si.sin_len = sizeof(struct sockaddr_in);
821                 si.sin_addr.s_addr = sc->g_dst.s_addr;
822                 error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
823                 if (error != 0)
824                         break;
825                 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
826                 break;
827         case GRESKEY:
828                 error = priv_check(curthread, PRIV_NET_GRE);
829                 if (error)
830                         break;
831                 error = copyin(ifr->ifr_data, &key, sizeof(key));
832                 if (error)
833                         break;
834                 /* adjust MTU for option header */
835                 if (key == 0 && sc->key != 0)           /* clear */
836                         adj += sizeof(key);
837                 else if (key != 0 && sc->key == 0)      /* set */
838                         adj -= sizeof(key);
839
840                 if (ifp->if_mtu + adj < 576) {
841                         error = EINVAL;
842                         break;
843                 }
844                 ifp->if_mtu += adj;
845                 sc->key = key;
846                 break;
847         case GREGKEY:
848                 error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key));
849                 break;
850
851         default:
852                 error = EINVAL;
853                 break;
854         }
855
856         return (error);
857 }
858
859 /*
860  * computes a route to our destination that is not the one
861  * which would be taken by ip_output(), as this one will loop back to
862  * us. If the interface is p2p as  a--->b, then a routing entry exists
863  * If we now send a packet to b (e.g. ping b), this will come down here
864  * gets src=a, dst=b tacked on and would from ip_output() sent back to
865  * if_gre.
866  * Goal here is to compute a route to b that is less specific than
867  * a-->b. We know that this one exists as in normal operation we have
868  * at least a default route which matches.
869  */
870 static int
871 gre_compute_route(struct gre_softc *sc)
872 {
873         struct route *ro;
874
875         ro = &sc->route;
876
877         memset(ro, 0, sizeof(struct route));
878         ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
879         ro->ro_dst.sa_family = AF_INET;
880         ro->ro_dst.sa_len = sizeof(ro->ro_dst);
881
882         /*
883          * toggle last bit, so our interface is not found, but a less
884          * specific route. I'd rather like to specify a shorter mask,
885          * but this is not possible. Should work though. XXX
886          * XXX MRT Use a different FIB for the tunnel to solve this problem.
887          */
888         if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
889                 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^=
890                     htonl(0x01);
891         }
892
893 #ifdef DIAGNOSTIC
894         printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
895             inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
896 #endif
897
898         rtalloc_fib(ro, sc->gre_fibnum);
899
900         /*
901          * check if this returned a route at all and this route is no
902          * recursion to ourself
903          */
904         if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
905 #ifdef DIAGNOSTIC
906                 if (ro->ro_rt == NULL)
907                         printf(" - no route found!\n");
908                 else
909                         printf(" - route loops back to ourself!\n");
910 #endif
911                 return EADDRNOTAVAIL;
912         }
913
914         /*
915          * now change it back - else ip_output will just drop
916          * the route and search one to this interface ...
917          */
918         if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
919                 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
920
921 #ifdef DIAGNOSTIC
922         printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
923             inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
924         printf("\n");
925 #endif
926
927         return 0;
928 }
929
930 /*
931  * do a checksum of a buffer - much like in_cksum, which operates on
932  * mbufs.
933  */
934 u_int16_t
935 gre_in_cksum(u_int16_t *p, u_int len)
936 {
937         u_int32_t sum = 0;
938         int nwords = len >> 1;
939
940         while (nwords-- != 0)
941                 sum += *p++;
942
943         if (len & 1) {
944                 union {
945                         u_short w;
946                         u_char c[2];
947                 } u;
948                 u.c[0] = *(u_char *)p;
949                 u.c[1] = 0;
950                 sum += u.w;
951         }
952
953         /* end-around-carry */
954         sum = (sum >> 16) + (sum & 0xffff);
955         sum += (sum >> 16);
956         return (~sum);
957 }
958
959 static int
960 gremodevent(module_t mod, int type, void *data)
961 {
962
963         switch (type) {
964         case MOD_LOAD:
965         case MOD_UNLOAD:
966                 break;
967         default:
968                 return (EOPNOTSUPP);
969         }
970         return (0);
971 }
972
973 static moduledata_t gre_mod = {
974         "if_gre",
975         gremodevent,
976         0
977 };
978
979 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
980 MODULE_VERSION(if_gre, 1);