]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/net/if_gre.c
MFC r222247:
[FreeBSD/stable/8.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  * 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.
28  *
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.
40  */
41
42 /*
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
49  */
50
51 #include "opt_atalk.h"
52 #include "opt_inet.h"
53 #include "opt_inet6.h"
54
55 #include <sys/param.h>
56 #include <sys/jail.h>
57 #include <sys/kernel.h>
58 #include <sys/malloc.h>
59 #include <sys/module.h>
60 #include <sys/mbuf.h>
61 #include <sys/priv.h>
62 #include <sys/proc.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>
68
69 #include <net/ethernet.h>
70 #include <net/if.h>
71 #include <net/if_clone.h>
72 #include <net/if_types.h>
73 #include <net/route.h>
74 #include <net/vnet.h>
75
76 #ifdef INET
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>
84 #else
85 #error "Huh? if_gre without inet?"
86 #endif
87
88 #include <net/bpf.h>
89
90 #include <net/if_gre.h>
91
92 /*
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
95  * other vendors use.
96  */
97 #define GREMTU  1476
98
99 #define GRENAME "gre"
100
101 /*
102  * gre_mtx protects all global variables in if_gre.c.
103  * XXX: gre_softc data not protected yet.
104  */
105 struct mtx gre_mtx;
106 static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
107
108 struct gre_softc_head gre_softc_list;
109
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 *,
114                     struct route *ro);
115
116 IFC_SIMPLE_DECLARE(gre, 0);
117
118 static int gre_compute_route(struct gre_softc *sc);
119
120 static void     greattach(void);
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 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 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");
165
166 /* ARGSUSED */
167 static void
168 greattach(void)
169 {
170
171         mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
172         LIST_INIT(&gre_softc_list);
173         if_clone_attach(&gre_cloner);
174 }
175
176 static int
177 gre_clone_create(ifc, unit, params)
178         struct if_clone *ifc;
179         int unit;
180         caddr_t params;
181 {
182         struct gre_softc *sc;
183
184         sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
185
186         GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
187         if (GRE2IFP(sc) == NULL) {
188                 free(sc, M_GRE);
189                 return (ENOSPC);
190         }
191
192         GRE2IFP(sc)->if_softc = sc;
193         if_initname(GRE2IFP(sc), ifc->ifc_name, unit);
194
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;
205         sc->encap = NULL;
206         sc->called = 0;
207         sc->gre_fibnum = curthread->td_proc->p_fibnum;
208         sc->wccp_ver = WCCP_V1;
209         sc->key = 0;
210         if_attach(GRE2IFP(sc));
211         bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
212         mtx_lock(&gre_mtx);
213         LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
214         mtx_unlock(&gre_mtx);
215         return (0);
216 }
217
218 static void
219 gre_clone_destroy(ifp)
220         struct ifnet *ifp;
221 {
222         struct gre_softc *sc = ifp->if_softc;
223
224         mtx_lock(&gre_mtx);
225         LIST_REMOVE(sc, sc_list);
226         mtx_unlock(&gre_mtx);
227
228 #ifdef INET
229         if (sc->encap != NULL)
230                 encap_detach(sc->encap);
231 #endif
232         bpfdetach(ifp);
233         if_detach(ifp);
234         if_free(ifp);
235         free(sc, M_GRE);
236 }
237
238 /*
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
241  */
242 static int
243 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
244            struct route *ro)
245 {
246         int error = 0;
247         struct gre_softc *sc = ifp->if_softc;
248         struct greip *gh;
249         struct ip *ip;
250         u_short gre_ip_id = 0;
251         uint8_t gre_ip_tos = 0;
252         u_int16_t etype = 0;
253         struct mobile_h mob_h;
254         u_int32_t af;
255         int extra = 0;
256
257         /*
258          * gre may cause infinite recursion calls when misconfigured.
259          * We'll prevent this by introducing upper limit.
260          */
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);
264                 m_freem(m);
265                 error = EIO;    /* is there better errno? */
266                 goto end;
267         }
268
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) {
272                 m_freem(m);
273                 error = ENETDOWN;
274                 goto end;
275         }
276
277         gh = NULL;
278         ip = NULL;
279
280         /* BPF writes need to be handled specially. */
281         if (dst->sa_family == AF_UNSPEC) {
282                 bcopy(dst->sa_data, &af, sizeof(af));
283                 dst->sa_family = af;
284         }
285
286         if (bpf_peers_present(ifp->if_bpf)) {
287                 af = dst->sa_family;
288                 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
289         }
290
291         m->m_flags &= ~(M_BCAST|M_MCAST);
292
293         if (sc->g_proto == IPPROTO_MOBILE) {
294                 if (dst->sa_family == AF_INET) {
295                         struct mbuf *m0;
296                         int msiz;
297
298                         ip = mtod(m, struct ip *);
299
300                         /*
301                          * RFC2004 specifies that fragmented diagrams shouldn't
302                          * be encapsulated.
303                          */
304                         if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
305                                 _IF_DROP(&ifp->if_snd);
306                                 m_freem(m);
307                                 error = EINVAL;    /* is there better errno? */
308                                 goto end;
309                         }
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;
314
315                         /*
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
319                          */
320                         if (in_hosteq(ip->ip_src, sc->g_src)) {
321                                 msiz = MOB_H_SIZ_S;
322                         } else {
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;
326                                 msiz = MOB_H_SIZ_L;
327                         }
328                         mob_h.proto = htons(mob_h.proto);
329                         mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
330
331                         if ((m->m_data - msiz) < m->m_pktdat) {
332                                 /* need new mbuf */
333                                 MGETHDR(m0, M_DONTWAIT, MT_DATA);
334                                 if (m0 == NULL) {
335                                         _IF_DROP(&ifp->if_snd);
336                                         m_freem(m);
337                                         error = ENOBUFS;
338                                         goto end;
339                                 }
340                                 m0->m_next = m;
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,
347                                        sizeof(struct ip));
348                                 m = m0;
349                         } else {  /* we have some space left in the old one */
350                                 m->m_data -= msiz;
351                                 m->m_len += msiz;
352                                 m->m_pkthdr.len += msiz;
353                                 bcopy(ip, mtod(m, caddr_t),
354                                         sizeof(struct ip));
355                         }
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);
361                         m_freem(m);
362                         error = EINVAL;
363                         goto end;
364                 }
365         } else if (sc->g_proto == IPPROTO_GRE) {
366                 switch (dst->sa_family) {
367                 case AF_INET:
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;
374                         } else {
375                                 etype = ETHERTYPE_IP;
376                         }
377                         break;
378 #ifdef INET6
379                 case AF_INET6:
380                         gre_ip_id = ip_newid();
381                         etype = ETHERTYPE_IPV6;
382                         break;
383 #endif
384 #ifdef NETATALK
385                 case AF_APPLETALK:
386                         etype = ETHERTYPE_ATALK;
387                         break;
388 #endif
389                 default:
390                         _IF_DROP(&ifp->if_snd);
391                         m_freem(m);
392                         error = EAFNOSUPPORT;
393                         goto end;
394                 }
395                         
396                 /* Reserve space for GRE header + optional GRE key */
397                 int hdrlen = sizeof(struct greip) + extra;
398                 if (sc->key)
399                         hdrlen += sizeof(uint32_t);
400                 M_PREPEND(m, hdrlen, M_DONTWAIT);
401         } else {
402                 _IF_DROP(&ifp->if_snd);
403                 m_freem(m);
404                 error = EINVAL;
405                 goto end;
406         }
407
408         if (m == NULL) {        /* mbuf allocation failed */
409                 _IF_DROP(&ifp->if_snd);
410                 error = ENOBUFS;
411                 goto end;
412         }
413
414         M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */
415
416         gh = mtod(m, struct greip *);
417         if (sc->g_proto == IPPROTO_GRE) {
418                 uint32_t *options = gh->gi_options;
419
420                 memset((void *)gh, 0, sizeof(struct greip) + extra);
421                 gh->gi_ptype = htons(etype);
422                 gh->gi_flags = 0;
423
424                 /* Add key option */
425                 if (sc->key)
426                 {
427                         gh->gi_flags |= htons(GRE_KP);
428                         *(options++) = htonl(sc->key);
429                 }
430         }
431
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;
442         }
443
444         ifp->if_opackets++;
445         ifp->if_obytes += m->m_pkthdr.len;
446         /*
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.
450          */
451         error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
452             (struct ip_moptions *)NULL, (struct inpcb *)NULL);
453   end:
454         sc->called = 0;
455         if (error)
456                 ifp->if_oerrors++;
457         return (error);
458 }
459
460 static int
461 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
462 {
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;
467         int s;
468         struct sockaddr_in si;
469         struct sockaddr *sa = NULL;
470         int error, adj;
471         struct sockaddr_in sp, sm, dp, dm;
472         uint32_t key;
473
474         error = 0;
475         adj = 0;
476
477         s = splnet();
478         switch (cmd) {
479         case SIOCSIFADDR:
480                 ifp->if_flags |= IFF_UP;
481                 break;
482         case SIOCSIFDSTADDR:
483                 break;
484         case SIOCSIFFLAGS:
485                 /*
486                  * XXXRW: Isn't this priv_check() redundant to the ifnet
487                  * layer check?
488                  */
489                 if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0)
490                         break;
491                 if ((ifr->ifr_flags & IFF_LINK0) != 0)
492                         sc->g_proto = IPPROTO_GRE;
493                 else
494                         sc->g_proto = IPPROTO_MOBILE;
495                 if ((ifr->ifr_flags & IFF_LINK2) != 0)
496                         sc->wccp_ver = WCCP_V2;
497                 else
498                         sc->wccp_ver = WCCP_V1;
499                 goto recompute;
500         case SIOCSIFMTU:
501                 /*
502                  * XXXRW: Isn't this priv_check() redundant to the ifnet
503                  * layer check?
504                  */
505                 if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0)
506                         break;
507                 if (ifr->ifr_mtu < 576) {
508                         error = EINVAL;
509                         break;
510                 }
511                 ifp->if_mtu = ifr->ifr_mtu;
512                 break;
513         case SIOCGIFMTU:
514                 ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
515                 break;
516         case SIOCADDMULTI:
517                 /*
518                  * XXXRW: Isn't this priv_checkr() redundant to the ifnet
519                  * layer check?
520                  */
521                 if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0)
522                         break;
523                 if (ifr == 0) {
524                         error = EAFNOSUPPORT;
525                         break;
526                 }
527                 switch (ifr->ifr_addr.sa_family) {
528 #ifdef INET
529                 case AF_INET:
530                         break;
531 #endif
532 #ifdef INET6
533                 case AF_INET6:
534                         break;
535 #endif
536                 default:
537                         error = EAFNOSUPPORT;
538                         break;
539                 }
540                 break;
541         case SIOCDELMULTI:
542                 /*
543                  * XXXRW: Isn't this priv_check() redundant to the ifnet
544                  * layer check?
545                  */
546                 if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0)
547                         break;
548                 if (ifr == 0) {
549                         error = EAFNOSUPPORT;
550                         break;
551                 }
552                 switch (ifr->ifr_addr.sa_family) {
553 #ifdef INET
554                 case AF_INET:
555                         break;
556 #endif
557 #ifdef INET6
558                 case AF_INET6:
559                         break;
560 #endif
561                 default:
562                         error = EAFNOSUPPORT;
563                         break;
564                 }
565                 break;
566         case GRESPROTO:
567                 /*
568                  * XXXRW: Isn't this priv_check() redundant to the ifnet
569                  * layer check?
570                  */
571                 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
572                         break;
573                 sc->g_proto = ifr->ifr_flags;
574                 switch (sc->g_proto) {
575                 case IPPROTO_GRE:
576                         ifp->if_flags |= IFF_LINK0;
577                         break;
578                 case IPPROTO_MOBILE:
579                         ifp->if_flags &= ~IFF_LINK0;
580                         break;
581                 default:
582                         error = EPROTONOSUPPORT;
583                         break;
584                 }
585                 goto recompute;
586         case GREGPROTO:
587                 ifr->ifr_flags = sc->g_proto;
588                 break;
589         case GRESADDRS:
590         case GRESADDRD:
591                 error = priv_check(curthread, PRIV_NET_GRE);
592                 if (error)
593                         return (error);
594                 /*
595                  * set tunnel endpoints, compute a less specific route
596                  * to the remote end and mark if as up
597                  */
598                 sa = &ifr->ifr_addr;
599                 if (cmd == GRESADDRS)
600                         sc->g_src = (satosin(sa))->sin_addr;
601                 if (cmd == GRESADDRD)
602                         sc->g_dst = (satosin(sa))->sin_addr;
603         recompute:
604 #ifdef INET
605                 if (sc->encap != NULL) {
606                         encap_detach(sc->encap);
607                         sc->encap = NULL;
608                 }
609 #endif
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 =
623                             INADDR_BROADCAST;
624 #ifdef INET
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)));
632 #endif
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;
637                         else
638                                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
639                 }
640                 break;
641         case GREGADDRS:
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;
646                 sa = sintosa(&si);
647                 error = prison_if(curthread->td_ucred, sa);
648                 if (error != 0)
649                         break;
650                 ifr->ifr_addr = *sa;
651                 break;
652         case GREGADDRD:
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;
657                 sa = sintosa(&si);
658                 error = prison_if(curthread->td_ucred, sa);
659                 if (error != 0)
660                         break;
661                 ifr->ifr_addr = *sa;
662                 break;
663         case SIOCSIFPHYADDR:
664                 /*
665                  * XXXRW: Isn't this priv_check() redundant to the ifnet
666                  * layer check?
667                  */
668                 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
669                         break;
670                 if (aifr->ifra_addr.sin_family != AF_INET ||
671                     aifr->ifra_dstaddr.sin_family != AF_INET) {
672                         error = EAFNOSUPPORT;
673                         break;
674                 }
675                 if (aifr->ifra_addr.sin_len != sizeof(si) ||
676                     aifr->ifra_dstaddr.sin_len != sizeof(si)) {
677                         error = EINVAL;
678                         break;
679                 }
680                 sc->g_src = aifr->ifra_addr.sin_addr;
681                 sc->g_dst = aifr->ifra_dstaddr.sin_addr;
682                 goto recompute;
683         case SIOCSLIFPHYADDR:
684                 /*
685                  * XXXRW: Isn't this priv_check() redundant to the ifnet
686                  * layer check?
687                  */
688                 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
689                         break;
690                 if (lifr->addr.ss_family != AF_INET ||
691                     lifr->dstaddr.ss_family != AF_INET) {
692                         error = EAFNOSUPPORT;
693                         break;
694                 }
695                 if (lifr->addr.ss_len != sizeof(si) ||
696                     lifr->dstaddr.ss_len != sizeof(si)) {
697                         error = EINVAL;
698                         break;
699                 }
700                 sc->g_src = (satosin(&lifr->addr))->sin_addr;
701                 sc->g_dst =
702                     (satosin(&lifr->dstaddr))->sin_addr;
703                 goto recompute;
704         case SIOCDIFPHYADDR:
705                 /*
706                  * XXXRW: Isn't this priv_check() redundant to the ifnet
707                  * layer check?
708                  */
709                 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
710                         break;
711                 sc->g_src.s_addr = INADDR_ANY;
712                 sc->g_dst.s_addr = INADDR_ANY;
713                 goto recompute;
714         case SIOCGLIFPHYADDR:
715                 if (sc->g_src.s_addr == INADDR_ANY ||
716                     sc->g_dst.s_addr == INADDR_ANY) {
717                         error = EADDRNOTAVAIL;
718                         break;
719                 }
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);
725                 if (error != 0)
726                         break;
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);
730                 if (error != 0)
731                         break;
732                 memcpy(&lifr->dstaddr, &si, sizeof(si));
733                 break;
734         case SIOCGIFPSRCADDR:
735 #ifdef INET6
736         case SIOCGIFPSRCADDR_IN6:
737 #endif
738                 if (sc->g_src.s_addr == INADDR_ANY) {
739                         error = EADDRNOTAVAIL;
740                         break;
741                 }
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);
747                 if (error != 0)
748                         break;
749                 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
750                 break;
751         case SIOCGIFPDSTADDR:
752 #ifdef INET6
753         case SIOCGIFPDSTADDR_IN6:
754 #endif
755                 if (sc->g_dst.s_addr == INADDR_ANY) {
756                         error = EADDRNOTAVAIL;
757                         break;
758                 }
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);
764                 if (error != 0)
765                         break;
766                 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
767                 break;
768         case GRESKEY:
769                 error = priv_check(curthread, PRIV_NET_GRE);
770                 if (error)
771                         break;
772                 error = copyin(ifr->ifr_data, &key, sizeof(key));
773                 if (error)
774                         break;
775                 /* adjust MTU for option header */
776                 if (key == 0 && sc->key != 0)           /* clear */
777                         adj += sizeof(key);
778                 else if (key != 0 && sc->key == 0)      /* set */
779                         adj -= sizeof(key);
780
781                 if (ifp->if_mtu + adj < 576) {
782                         error = EINVAL;
783                         break;
784                 }
785                 ifp->if_mtu += adj;
786                 sc->key = key;
787                 break;
788         case GREGKEY:
789                 error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key));
790                 break;
791
792         default:
793                 error = EINVAL;
794                 break;
795         }
796
797         splx(s);
798         return (error);
799 }
800
801 /*
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
807  * if_gre.
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.
811  */
812 static int
813 gre_compute_route(struct gre_softc *sc)
814 {
815         struct route *ro;
816
817         ro = &sc->route;
818
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);
823
824         /*
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.
829          */
830         if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
831                 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^=
832                     htonl(0x01);
833         }
834
835 #ifdef DIAGNOSTIC
836         printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
837             inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
838 #endif
839
840         rtalloc_fib(ro, sc->gre_fibnum);
841
842         /*
843          * check if this returned a route at all and this route is no
844          * recursion to ourself
845          */
846         if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
847 #ifdef DIAGNOSTIC
848                 if (ro->ro_rt == NULL)
849                         printf(" - no route found!\n");
850                 else
851                         printf(" - route loops back to ourself!\n");
852 #endif
853                 return EADDRNOTAVAIL;
854         }
855
856         /*
857          * now change it back - else ip_output will just drop
858          * the route and search one to this interface ...
859          */
860         if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
861                 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
862
863 #ifdef DIAGNOSTIC
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));
866         printf("\n");
867 #endif
868
869         return 0;
870 }
871
872 /*
873  * do a checksum of a buffer - much like in_cksum, which operates on
874  * mbufs.
875  */
876 u_int16_t
877 gre_in_cksum(u_int16_t *p, u_int len)
878 {
879         u_int32_t sum = 0;
880         int nwords = len >> 1;
881
882         while (nwords-- != 0)
883                 sum += *p++;
884
885         if (len & 1) {
886                 union {
887                         u_short w;
888                         u_char c[2];
889                 } u;
890                 u.c[0] = *(u_char *)p;
891                 u.c[1] = 0;
892                 sum += u.w;
893         }
894
895         /* end-around-carry */
896         sum = (sum >> 16) + (sum & 0xffff);
897         sum += (sum >> 16);
898         return (~sum);
899 }
900
901 static int
902 gremodevent(module_t mod, int type, void *data)
903 {
904
905         switch (type) {
906         case MOD_LOAD:
907                 greattach();
908                 break;
909         case MOD_UNLOAD:
910                 if_clone_detach(&gre_cloner);
911                 mtx_destroy(&gre_mtx);
912                 break;
913         default:
914                 return EOPNOTSUPP;
915         }
916         return 0;
917 }
918
919 static moduledata_t gre_mod = {
920         "if_gre",
921         gremodevent,
922         0
923 };
924
925 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
926 MODULE_VERSION(if_gre, 1);