]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_gre.c
Add 32-bit compat for ioctls that take struct ifgroupreq.
[FreeBSD/FreeBSD.git] / sys / net / if_gre.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * Copyright (c) 2014 Andrey V. Elsukov <ae@FreeBSD.org>
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  * $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $
35  */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include "opt_inet.h"
41 #include "opt_inet6.h"
42
43 #include <sys/param.h>
44 #include <sys/jail.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/libkern.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/mbuf.h>
51 #include <sys/priv.h>
52 #include <sys/proc.h>
53 #include <sys/protosw.h>
54 #include <sys/rmlock.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sx.h>
58 #include <sys/sysctl.h>
59 #include <sys/syslog.h>
60 #include <sys/systm.h>
61
62 #include <net/ethernet.h>
63 #include <net/if.h>
64 #include <net/if_var.h>
65 #include <net/if_clone.h>
66 #include <net/if_types.h>
67 #include <net/netisr.h>
68 #include <net/vnet.h>
69 #include <net/route.h>
70
71 #include <netinet/in.h>
72 #ifdef INET
73 #include <netinet/in_systm.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_var.h>
77 #endif
78
79 #ifdef INET6
80 #include <netinet/ip6.h>
81 #include <netinet6/in6_var.h>
82 #include <netinet6/ip6_var.h>
83 #include <netinet6/scope6_var.h>
84 #endif
85
86 #include <netinet/ip_encap.h>
87 #include <net/bpf.h>
88 #include <net/if_gre.h>
89
90 #include <machine/in_cksum.h>
91 #include <security/mac/mac_framework.h>
92
93 #define GREMTU                  1476
94 static const char grename[] = "gre";
95 static MALLOC_DEFINE(M_GRE, grename, "Generic Routing Encapsulation");
96 static VNET_DEFINE(struct mtx, gre_mtx);
97 #define V_gre_mtx       VNET(gre_mtx)
98 #define GRE_LIST_LOCK_INIT(x)           mtx_init(&V_gre_mtx, "gre_mtx", NULL, \
99                                             MTX_DEF)
100 #define GRE_LIST_LOCK_DESTROY(x)        mtx_destroy(&V_gre_mtx)
101 #define GRE_LIST_LOCK(x)                mtx_lock(&V_gre_mtx)
102 #define GRE_LIST_UNLOCK(x)              mtx_unlock(&V_gre_mtx)
103
104 static VNET_DEFINE(LIST_HEAD(, gre_softc), gre_softc_list);
105 #define V_gre_softc_list        VNET(gre_softc_list)
106 static struct sx gre_ioctl_sx;
107 SX_SYSINIT(gre_ioctl_sx, &gre_ioctl_sx, "gre_ioctl");
108
109 static int      gre_clone_create(struct if_clone *, int, caddr_t);
110 static void     gre_clone_destroy(struct ifnet *);
111 static VNET_DEFINE(struct if_clone *, gre_cloner);
112 #define V_gre_cloner    VNET(gre_cloner)
113
114 static void     gre_qflush(struct ifnet *);
115 static int      gre_transmit(struct ifnet *, struct mbuf *);
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 void     gre_updatehdr(struct gre_softc *);
121 static int      gre_set_tunnel(struct ifnet *, struct sockaddr *,
122     struct sockaddr *);
123 static void     gre_delete_tunnel(struct ifnet *);
124
125 SYSCTL_DECL(_net_link);
126 static SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
127     "Generic Routing Encapsulation");
128 #ifndef MAX_GRE_NEST
129 /*
130  * This macro controls the default upper limitation on nesting of gre tunnels.
131  * Since, setting a large value to this macro with a careless configuration
132  * may introduce system crash, we don't allow any nestings by default.
133  * If you need to configure nested gre tunnels, you can define this macro
134  * in your kernel configuration file.  However, if you do so, please be
135  * careful to configure the tunnels so that it won't make a loop.
136  */
137 #define MAX_GRE_NEST 1
138 #endif
139
140 static VNET_DEFINE(int, max_gre_nesting) = MAX_GRE_NEST;
141 #define V_max_gre_nesting       VNET(max_gre_nesting)
142 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET,
143     &VNET_NAME(max_gre_nesting), 0, "Max nested tunnels");
144
145 static void
146 vnet_gre_init(const void *unused __unused)
147 {
148         LIST_INIT(&V_gre_softc_list);
149         GRE_LIST_LOCK_INIT();
150         V_gre_cloner = if_clone_simple(grename, gre_clone_create,
151             gre_clone_destroy, 0);
152 }
153 VNET_SYSINIT(vnet_gre_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
154     vnet_gre_init, NULL);
155
156 static void
157 vnet_gre_uninit(const void *unused __unused)
158 {
159
160         if_clone_detach(V_gre_cloner);
161         GRE_LIST_LOCK_DESTROY();
162 }
163 VNET_SYSUNINIT(vnet_gre_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
164     vnet_gre_uninit, NULL);
165
166 static int
167 gre_clone_create(struct if_clone *ifc, int unit, caddr_t params)
168 {
169         struct gre_softc *sc;
170
171         sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
172         sc->gre_fibnum = curthread->td_proc->p_fibnum;
173         GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
174         GRE_LOCK_INIT(sc);
175         GRE2IFP(sc)->if_softc = sc;
176         if_initname(GRE2IFP(sc), grename, unit);
177
178         GRE2IFP(sc)->if_mtu = GREMTU;
179         GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
180         GRE2IFP(sc)->if_output = gre_output;
181         GRE2IFP(sc)->if_ioctl = gre_ioctl;
182         GRE2IFP(sc)->if_transmit = gre_transmit;
183         GRE2IFP(sc)->if_qflush = gre_qflush;
184         GRE2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
185         GRE2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
186         if_attach(GRE2IFP(sc));
187         bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
188         GRE_LIST_LOCK();
189         LIST_INSERT_HEAD(&V_gre_softc_list, sc, gre_list);
190         GRE_LIST_UNLOCK();
191         return (0);
192 }
193
194 static void
195 gre_clone_destroy(struct ifnet *ifp)
196 {
197         struct gre_softc *sc;
198
199         sx_xlock(&gre_ioctl_sx);
200         sc = ifp->if_softc;
201         gre_delete_tunnel(ifp);
202         GRE_LIST_LOCK();
203         LIST_REMOVE(sc, gre_list);
204         GRE_LIST_UNLOCK();
205         bpfdetach(ifp);
206         if_detach(ifp);
207         ifp->if_softc = NULL;
208         sx_xunlock(&gre_ioctl_sx);
209
210         if_free(ifp);
211         GRE_LOCK_DESTROY(sc);
212         free(sc, M_GRE);
213 }
214
215 static int
216 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
217 {
218         GRE_RLOCK_TRACKER;
219         struct ifreq *ifr = (struct ifreq *)data;
220         struct sockaddr *src, *dst;
221         struct gre_softc *sc;
222 #ifdef INET
223         struct sockaddr_in *sin = NULL;
224 #endif
225 #ifdef INET6
226         struct sockaddr_in6 *sin6 = NULL;
227 #endif
228         uint32_t opt;
229         int error;
230
231         switch (cmd) {
232         case SIOCSIFMTU:
233                  /* XXX: */
234                 if (ifr->ifr_mtu < 576)
235                         return (EINVAL);
236                 ifp->if_mtu = ifr->ifr_mtu;
237                 return (0);
238         case SIOCSIFADDR:
239                 ifp->if_flags |= IFF_UP;
240         case SIOCSIFFLAGS:
241         case SIOCADDMULTI:
242         case SIOCDELMULTI:
243                 return (0);
244         case GRESADDRS:
245         case GRESADDRD:
246         case GREGADDRS:
247         case GREGADDRD:
248         case GRESPROTO:
249         case GREGPROTO:
250                 return (EOPNOTSUPP);
251         }
252         src = dst = NULL;
253         sx_xlock(&gre_ioctl_sx);
254         sc = ifp->if_softc;
255         if (sc == NULL) {
256                 error = ENXIO;
257                 goto end;
258         }
259         error = 0;
260         switch (cmd) {
261         case SIOCSIFPHYADDR:
262 #ifdef INET6
263         case SIOCSIFPHYADDR_IN6:
264 #endif
265                 error = EINVAL;
266                 switch (cmd) {
267 #ifdef INET
268                 case SIOCSIFPHYADDR:
269                         src = (struct sockaddr *)
270                                 &(((struct in_aliasreq *)data)->ifra_addr);
271                         dst = (struct sockaddr *)
272                                 &(((struct in_aliasreq *)data)->ifra_dstaddr);
273                         break;
274 #endif
275 #ifdef INET6
276                 case SIOCSIFPHYADDR_IN6:
277                         src = (struct sockaddr *)
278                                 &(((struct in6_aliasreq *)data)->ifra_addr);
279                         dst = (struct sockaddr *)
280                                 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
281                         break;
282 #endif
283                 default:
284                         error = EAFNOSUPPORT;
285                         goto end;
286                 }
287                 /* sa_family must be equal */
288                 if (src->sa_family != dst->sa_family ||
289                     src->sa_len != dst->sa_len)
290                         goto end;
291
292                 /* validate sa_len */
293                 switch (src->sa_family) {
294 #ifdef INET
295                 case AF_INET:
296                         if (src->sa_len != sizeof(struct sockaddr_in))
297                                 goto end;
298                         break;
299 #endif
300 #ifdef INET6
301                 case AF_INET6:
302                         if (src->sa_len != sizeof(struct sockaddr_in6))
303                                 goto end;
304                         break;
305 #endif
306                 default:
307                         error = EAFNOSUPPORT;
308                         goto end;
309                 }
310                 /* check sa_family looks sane for the cmd */
311                 error = EAFNOSUPPORT;
312                 switch (cmd) {
313 #ifdef INET
314                 case SIOCSIFPHYADDR:
315                         if (src->sa_family == AF_INET)
316                                 break;
317                         goto end;
318 #endif
319 #ifdef INET6
320                 case SIOCSIFPHYADDR_IN6:
321                         if (src->sa_family == AF_INET6)
322                                 break;
323                         goto end;
324 #endif
325                 }
326                 error = EADDRNOTAVAIL;
327                 switch (src->sa_family) {
328 #ifdef INET
329                 case AF_INET:
330                         if (satosin(src)->sin_addr.s_addr == INADDR_ANY ||
331                             satosin(dst)->sin_addr.s_addr == INADDR_ANY)
332                                 goto end;
333                         break;
334 #endif
335 #ifdef INET6
336                 case AF_INET6:
337                         if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr)
338                             ||
339                             IN6_IS_ADDR_UNSPECIFIED(&satosin6(dst)->sin6_addr))
340                                 goto end;
341                         /*
342                          * Check validity of the scope zone ID of the
343                          * addresses, and convert it into the kernel
344                          * internal form if necessary.
345                          */
346                         error = sa6_embedscope(satosin6(src), 0);
347                         if (error != 0)
348                                 goto end;
349                         error = sa6_embedscope(satosin6(dst), 0);
350                         if (error != 0)
351                                 goto end;
352 #endif
353                 }
354                 error = gre_set_tunnel(ifp, src, dst);
355                 break;
356         case SIOCDIFPHYADDR:
357                 gre_delete_tunnel(ifp);
358                 break;
359         case SIOCGIFPSRCADDR:
360         case SIOCGIFPDSTADDR:
361 #ifdef INET6
362         case SIOCGIFPSRCADDR_IN6:
363         case SIOCGIFPDSTADDR_IN6:
364 #endif
365                 if (sc->gre_family == 0) {
366                         error = EADDRNOTAVAIL;
367                         break;
368                 }
369                 GRE_RLOCK(sc);
370                 switch (cmd) {
371 #ifdef INET
372                 case SIOCGIFPSRCADDR:
373                 case SIOCGIFPDSTADDR:
374                         if (sc->gre_family != AF_INET) {
375                                 error = EADDRNOTAVAIL;
376                                 break;
377                         }
378                         sin = (struct sockaddr_in *)&ifr->ifr_addr;
379                         memset(sin, 0, sizeof(*sin));
380                         sin->sin_family = AF_INET;
381                         sin->sin_len = sizeof(*sin);
382                         break;
383 #endif
384 #ifdef INET6
385                 case SIOCGIFPSRCADDR_IN6:
386                 case SIOCGIFPDSTADDR_IN6:
387                         if (sc->gre_family != AF_INET6) {
388                                 error = EADDRNOTAVAIL;
389                                 break;
390                         }
391                         sin6 = (struct sockaddr_in6 *)
392                                 &(((struct in6_ifreq *)data)->ifr_addr);
393                         memset(sin6, 0, sizeof(*sin6));
394                         sin6->sin6_family = AF_INET6;
395                         sin6->sin6_len = sizeof(*sin6);
396                         break;
397 #endif
398                 }
399                 if (error == 0) {
400                         switch (cmd) {
401 #ifdef INET
402                         case SIOCGIFPSRCADDR:
403                                 sin->sin_addr = sc->gre_oip.ip_src;
404                                 break;
405                         case SIOCGIFPDSTADDR:
406                                 sin->sin_addr = sc->gre_oip.ip_dst;
407                                 break;
408 #endif
409 #ifdef INET6
410                         case SIOCGIFPSRCADDR_IN6:
411                                 sin6->sin6_addr = sc->gre_oip6.ip6_src;
412                                 break;
413                         case SIOCGIFPDSTADDR_IN6:
414                                 sin6->sin6_addr = sc->gre_oip6.ip6_dst;
415                                 break;
416 #endif
417                         }
418                 }
419                 GRE_RUNLOCK(sc);
420                 if (error != 0)
421                         break;
422                 switch (cmd) {
423 #ifdef INET
424                 case SIOCGIFPSRCADDR:
425                 case SIOCGIFPDSTADDR:
426                         error = prison_if(curthread->td_ucred,
427                             (struct sockaddr *)sin);
428                         if (error != 0)
429                                 memset(sin, 0, sizeof(*sin));
430                         break;
431 #endif
432 #ifdef INET6
433                 case SIOCGIFPSRCADDR_IN6:
434                 case SIOCGIFPDSTADDR_IN6:
435                         error = prison_if(curthread->td_ucred,
436                             (struct sockaddr *)sin6);
437                         if (error == 0)
438                                 error = sa6_recoverscope(sin6);
439                         if (error != 0)
440                                 memset(sin6, 0, sizeof(*sin6));
441 #endif
442                 }
443                 break;
444         case SIOCGTUNFIB:
445                 ifr->ifr_fib = sc->gre_fibnum;
446                 break;
447         case SIOCSTUNFIB:
448                 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
449                         break;
450                 if (ifr->ifr_fib >= rt_numfibs)
451                         error = EINVAL;
452                 else
453                         sc->gre_fibnum = ifr->ifr_fib;
454                 break;
455         case GRESKEY:
456                 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
457                         break;
458                 if ((error = copyin(ifr_data_get_ptr(ifr), &opt,
459                     sizeof(opt))) != 0)
460                         break;
461                 if (sc->gre_key != opt) {
462                         GRE_WLOCK(sc);
463                         sc->gre_key = opt;
464                         gre_updatehdr(sc);
465                         GRE_WUNLOCK(sc);
466                 }
467                 break;
468         case GREGKEY:
469                 error = copyout(&sc->gre_key, ifr_data_get_ptr(ifr),
470                     sizeof(sc->gre_key));
471                 break;
472         case GRESOPTS:
473                 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
474                         break;
475                 if ((error = copyin(ifr_data_get_ptr(ifr), &opt,
476                     sizeof(opt))) != 0)
477                         break;
478                 if (opt & ~GRE_OPTMASK)
479                         error = EINVAL;
480                 else {
481                         if (sc->gre_options != opt) {
482                                 GRE_WLOCK(sc);
483                                 sc->gre_options = opt;
484                                 gre_updatehdr(sc);
485                                 GRE_WUNLOCK(sc);
486                         }
487                 }
488                 break;
489
490         case GREGOPTS:
491                 error = copyout(&sc->gre_options, ifr_data_get_ptr(ifr),
492                     sizeof(sc->gre_options));
493                 break;
494         default:
495                 error = EINVAL;
496                 break;
497         }
498 end:
499         sx_xunlock(&gre_ioctl_sx);
500         return (error);
501 }
502
503 static void
504 gre_updatehdr(struct gre_softc *sc)
505 {
506         struct grehdr *gh = NULL;
507         uint32_t *opts;
508         uint16_t flags;
509
510         GRE_WLOCK_ASSERT(sc);
511         switch (sc->gre_family) {
512 #ifdef INET
513         case AF_INET:
514                 sc->gre_hlen = sizeof(struct greip);
515                 sc->gre_oip.ip_v = IPPROTO_IPV4;
516                 sc->gre_oip.ip_hl = sizeof(struct ip) >> 2;
517                 sc->gre_oip.ip_p = IPPROTO_GRE;
518                 gh = &sc->gre_gihdr->gi_gre;
519                 break;
520 #endif
521 #ifdef INET6
522         case AF_INET6:
523                 sc->gre_hlen = sizeof(struct greip6);
524                 sc->gre_oip6.ip6_vfc = IPV6_VERSION;
525                 sc->gre_oip6.ip6_nxt = IPPROTO_GRE;
526                 gh = &sc->gre_gi6hdr->gi6_gre;
527                 break;
528 #endif
529         default:
530                 return;
531         }
532         flags = 0;
533         opts = gh->gre_opts;
534         if (sc->gre_options & GRE_ENABLE_CSUM) {
535                 flags |= GRE_FLAGS_CP;
536                 sc->gre_hlen += 2 * sizeof(uint16_t);
537                 *opts++ = 0;
538         }
539         if (sc->gre_key != 0) {
540                 flags |= GRE_FLAGS_KP;
541                 sc->gre_hlen += sizeof(uint32_t);
542                 *opts++ = htonl(sc->gre_key);
543         }
544         if (sc->gre_options & GRE_ENABLE_SEQ) {
545                 flags |= GRE_FLAGS_SP;
546                 sc->gre_hlen += sizeof(uint32_t);
547                 *opts++ = 0;
548         } else
549                 sc->gre_oseq = 0;
550         gh->gre_flags = htons(flags);
551 }
552
553 static void
554 gre_detach(struct gre_softc *sc)
555 {
556
557         sx_assert(&gre_ioctl_sx, SA_XLOCKED);
558         if (sc->gre_ecookie != NULL)
559                 encap_detach(sc->gre_ecookie);
560         sc->gre_ecookie = NULL;
561 }
562
563 static int
564 gre_set_tunnel(struct ifnet *ifp, struct sockaddr *src,
565     struct sockaddr *dst)
566 {
567         struct gre_softc *sc, *tsc;
568 #ifdef INET6
569         struct ip6_hdr *ip6;
570 #endif
571 #ifdef INET
572         struct ip *ip;
573 #endif
574         void *hdr;
575         int error;
576
577         sx_assert(&gre_ioctl_sx, SA_XLOCKED);
578         GRE_LIST_LOCK();
579         sc = ifp->if_softc;
580         LIST_FOREACH(tsc, &V_gre_softc_list, gre_list) {
581                 if (tsc == sc || tsc->gre_family != src->sa_family)
582                         continue;
583 #ifdef INET
584                 if (tsc->gre_family == AF_INET &&
585                     tsc->gre_oip.ip_src.s_addr ==
586                     satosin(src)->sin_addr.s_addr &&
587                     tsc->gre_oip.ip_dst.s_addr ==
588                     satosin(dst)->sin_addr.s_addr) {
589                         GRE_LIST_UNLOCK();
590                         return (EADDRNOTAVAIL);
591                 }
592 #endif
593 #ifdef INET6
594                 if (tsc->gre_family == AF_INET6 &&
595                     IN6_ARE_ADDR_EQUAL(&tsc->gre_oip6.ip6_src,
596                     &satosin6(src)->sin6_addr) &&
597                     IN6_ARE_ADDR_EQUAL(&tsc->gre_oip6.ip6_dst,
598                         &satosin6(dst)->sin6_addr)) {
599                         GRE_LIST_UNLOCK();
600                         return (EADDRNOTAVAIL);
601                 }
602 #endif
603         }
604         GRE_LIST_UNLOCK();
605
606         switch (src->sa_family) {
607 #ifdef INET
608         case AF_INET:
609                 hdr = ip = malloc(sizeof(struct greip) +
610                     3 * sizeof(uint32_t), M_GRE, M_WAITOK | M_ZERO);
611                 ip->ip_src = satosin(src)->sin_addr;
612                 ip->ip_dst = satosin(dst)->sin_addr;
613                 break;
614 #endif
615 #ifdef INET6
616         case AF_INET6:
617                 hdr = ip6 = malloc(sizeof(struct greip6) +
618                     3 * sizeof(uint32_t), M_GRE, M_WAITOK | M_ZERO);
619                 ip6->ip6_src = satosin6(src)->sin6_addr;
620                 ip6->ip6_dst = satosin6(dst)->sin6_addr;
621                 break;
622 #endif
623         default:
624                 return (EAFNOSUPPORT);
625         }
626         if (sc->gre_family != 0)
627                 gre_detach(sc);
628         GRE_WLOCK(sc);
629         if (sc->gre_family != 0)
630                 free(sc->gre_hdr, M_GRE);
631         sc->gre_family = src->sa_family;
632         sc->gre_hdr = hdr;
633         sc->gre_oseq = 0;
634         sc->gre_iseq = UINT32_MAX;
635         gre_updatehdr(sc);
636         GRE_WUNLOCK(sc);
637
638         error = 0;
639         switch (src->sa_family) {
640 #ifdef INET
641         case AF_INET:
642                 error = in_gre_attach(sc);
643                 break;
644 #endif
645 #ifdef INET6
646         case AF_INET6:
647                 error = in6_gre_attach(sc);
648                 break;
649 #endif
650         }
651         if (error == 0) {
652                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
653                 if_link_state_change(ifp, LINK_STATE_UP);
654         }
655         return (error);
656 }
657
658 static void
659 gre_delete_tunnel(struct ifnet *ifp)
660 {
661         struct gre_softc *sc = ifp->if_softc;
662         int family;
663
664         GRE_WLOCK(sc);
665         family = sc->gre_family;
666         sc->gre_family = 0;
667         GRE_WUNLOCK(sc);
668         if (family != 0) {
669                 gre_detach(sc);
670                 free(sc->gre_hdr, M_GRE);
671         }
672         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
673         if_link_state_change(ifp, LINK_STATE_DOWN);
674 }
675
676 int
677 gre_input(struct mbuf **mp, int *offp, int proto)
678 {
679         struct gre_softc *sc;
680         struct grehdr *gh;
681         struct ifnet *ifp;
682         struct mbuf *m;
683         uint32_t *opts;
684 #ifdef notyet
685         uint32_t key;
686 #endif
687         uint16_t flags;
688         int hlen, isr, af;
689
690         m = *mp;
691         sc = encap_getarg(m);
692         KASSERT(sc != NULL, ("encap_getarg returned NULL"));
693
694         ifp = GRE2IFP(sc);
695         hlen = *offp + sizeof(struct grehdr) + 4 * sizeof(uint32_t);
696         if (m->m_pkthdr.len < hlen)
697                 goto drop;
698         if (m->m_len < hlen) {
699                 m = m_pullup(m, hlen);
700                 if (m == NULL)
701                         goto drop;
702         }
703         gh = (struct grehdr *)mtodo(m, *offp);
704         flags = ntohs(gh->gre_flags);
705         if (flags & ~GRE_FLAGS_MASK)
706                 goto drop;
707         opts = gh->gre_opts;
708         hlen = 2 * sizeof(uint16_t);
709         if (flags & GRE_FLAGS_CP) {
710                 /* reserved1 field must be zero */
711                 if (((uint16_t *)opts)[1] != 0)
712                         goto drop;
713                 if (in_cksum_skip(m, m->m_pkthdr.len, *offp) != 0)
714                         goto drop;
715                 hlen += 2 * sizeof(uint16_t);
716                 opts++;
717         }
718         if (flags & GRE_FLAGS_KP) {
719 #ifdef notyet
720         /* 
721          * XXX: The current implementation uses the key only for outgoing
722          * packets. But we can check the key value here, or even in the
723          * encapcheck function.
724          */
725                 key = ntohl(*opts);
726 #endif
727                 hlen += sizeof(uint32_t);
728                 opts++;
729     }
730 #ifdef notyet
731         } else
732                 key = 0;
733
734         if (sc->gre_key != 0 && (key != sc->gre_key || key != 0))
735                 goto drop;
736 #endif
737         if (flags & GRE_FLAGS_SP) {
738 #ifdef notyet
739                 seq = ntohl(*opts);
740 #endif
741                 hlen += sizeof(uint32_t);
742         }
743         switch (ntohs(gh->gre_proto)) {
744         case ETHERTYPE_WCCP:
745                 /*
746                  * For WCCP skip an additional 4 bytes if after GRE header
747                  * doesn't follow an IP header.
748                  */
749                 if (flags == 0 && (*(uint8_t *)gh->gre_opts & 0xF0) != 0x40)
750                         hlen += sizeof(uint32_t);
751                 /* FALLTHROUGH */
752         case ETHERTYPE_IP:
753                 isr = NETISR_IP;
754                 af = AF_INET;
755                 break;
756         case ETHERTYPE_IPV6:
757                 isr = NETISR_IPV6;
758                 af = AF_INET6;
759                 break;
760         default:
761                 goto drop;
762         }
763         m_adj(m, *offp + hlen);
764         m_clrprotoflags(m);
765         m->m_pkthdr.rcvif = ifp;
766         M_SETFIB(m, ifp->if_fib);
767 #ifdef MAC
768         mac_ifnet_create_mbuf(ifp, m);
769 #endif
770         BPF_MTAP2(ifp, &af, sizeof(af), m);
771         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
772         if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
773         if ((ifp->if_flags & IFF_MONITOR) != 0)
774                 m_freem(m);
775         else
776                 netisr_dispatch(isr, m);
777         return (IPPROTO_DONE);
778 drop:
779         if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
780         m_freem(m);
781         return (IPPROTO_DONE);
782 }
783
784 #define MTAG_GRE        1307983903
785 static int
786 gre_check_nesting(struct ifnet *ifp, struct mbuf *m)
787 {
788         struct m_tag *mtag;
789         int count;
790
791         count = 1;
792         mtag = NULL;
793         while ((mtag = m_tag_locate(m, MTAG_GRE, 0, mtag)) != NULL) {
794                 if (*(struct ifnet **)(mtag + 1) == ifp) {
795                         log(LOG_NOTICE, "%s: loop detected\n", ifp->if_xname);
796                         return (EIO);
797                 }
798                 count++;
799         }
800         if (count > V_max_gre_nesting) {
801                 log(LOG_NOTICE,
802                     "%s: if_output recursively called too many times(%d)\n",
803                     ifp->if_xname, count);
804                 return (EIO);
805         }
806         mtag = m_tag_alloc(MTAG_GRE, 0, sizeof(struct ifnet *), M_NOWAIT);
807         if (mtag == NULL)
808                 return (ENOMEM);
809         *(struct ifnet **)(mtag + 1) = ifp;
810         m_tag_prepend(m, mtag);
811         return (0);
812 }
813
814 static int
815 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
816    struct route *ro)
817 {
818         uint32_t af;
819         int error;
820
821 #ifdef MAC
822         error = mac_ifnet_check_transmit(ifp, m);
823         if (error != 0)
824                 goto drop;
825 #endif
826         if ((ifp->if_flags & IFF_MONITOR) != 0 ||
827             (ifp->if_flags & IFF_UP) == 0) {
828                 error = ENETDOWN;
829                 goto drop;
830         }
831
832         error = gre_check_nesting(ifp, m);
833         if (error != 0)
834                 goto drop;
835
836         m->m_flags &= ~(M_BCAST|M_MCAST);
837         if (dst->sa_family == AF_UNSPEC)
838                 bcopy(dst->sa_data, &af, sizeof(af));
839         else
840                 af = dst->sa_family;
841         BPF_MTAP2(ifp, &af, sizeof(af), m);
842         m->m_pkthdr.csum_data = af;     /* save af for if_transmit */
843         return (ifp->if_transmit(ifp, m));
844 drop:
845         m_freem(m);
846         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
847         return (error);
848 }
849
850 static void
851 gre_setseqn(struct grehdr *gh, uint32_t seq)
852 {
853         uint32_t *opts;
854         uint16_t flags;
855
856         opts = gh->gre_opts;
857         flags = ntohs(gh->gre_flags);
858         KASSERT((flags & GRE_FLAGS_SP) != 0,
859             ("gre_setseqn called, but GRE_FLAGS_SP isn't set "));
860         if (flags & GRE_FLAGS_CP)
861                 opts++;
862         if (flags & GRE_FLAGS_KP)
863                 opts++;
864         *opts = htonl(seq);
865 }
866
867 static int
868 gre_transmit(struct ifnet *ifp, struct mbuf *m)
869 {
870         GRE_RLOCK_TRACKER;
871         struct gre_softc *sc;
872         struct grehdr *gh;
873         uint32_t iaf, oaf, oseq;
874         int error, hlen, olen, plen;
875         int want_seq, want_csum;
876
877         plen = 0;
878         sc = ifp->if_softc;
879         if (sc == NULL) {
880                 error = ENETDOWN;
881                 m_freem(m);
882                 goto drop;
883         }
884         GRE_RLOCK(sc);
885         if (sc->gre_family == 0) {
886                 GRE_RUNLOCK(sc);
887                 error = ENETDOWN;
888                 m_freem(m);
889                 goto drop;
890         }
891         iaf = m->m_pkthdr.csum_data;
892         oaf = sc->gre_family;
893         hlen = sc->gre_hlen;
894         want_seq = (sc->gre_options & GRE_ENABLE_SEQ) != 0;
895         if (want_seq)
896                 oseq = sc->gre_oseq++; /* XXX */
897         else
898                 oseq = 0;               /* Make compiler happy. */
899         want_csum = (sc->gre_options & GRE_ENABLE_CSUM) != 0;
900         M_SETFIB(m, sc->gre_fibnum);
901         M_PREPEND(m, hlen, M_NOWAIT);
902         if (m == NULL) {
903                 GRE_RUNLOCK(sc);
904                 error = ENOBUFS;
905                 goto drop;
906         }
907         bcopy(sc->gre_hdr, mtod(m, void *), hlen);
908         GRE_RUNLOCK(sc);
909         switch (oaf) {
910 #ifdef INET
911         case AF_INET:
912                 olen = sizeof(struct ip);
913                 break;
914 #endif
915 #ifdef INET6
916         case AF_INET6:
917                 olen = sizeof(struct ip6_hdr);
918                 break;
919 #endif
920         default:
921                 error = ENETDOWN;
922                 goto drop;
923         }
924         gh = (struct grehdr *)mtodo(m, olen);
925         switch (iaf) {
926 #ifdef INET
927         case AF_INET:
928                 gh->gre_proto = htons(ETHERTYPE_IP);
929                 break;
930 #endif
931 #ifdef INET6
932         case AF_INET6:
933                 gh->gre_proto = htons(ETHERTYPE_IPV6);
934                 break;
935 #endif
936         default:
937                 error = ENETDOWN;
938                 goto drop;
939         }
940         if (want_seq)
941                 gre_setseqn(gh, oseq);
942         if (want_csum) {
943                 *(uint16_t *)gh->gre_opts = in_cksum_skip(m,
944                     m->m_pkthdr.len, olen);
945         }
946         plen = m->m_pkthdr.len - hlen;
947         switch (oaf) {
948 #ifdef INET
949         case AF_INET:
950                 error = in_gre_output(m, iaf, hlen);
951                 break;
952 #endif
953 #ifdef INET6
954         case AF_INET6:
955                 error = in6_gre_output(m, iaf, hlen);
956                 break;
957 #endif
958         default:
959                 m_freem(m);
960                 error = ENETDOWN;
961         }
962 drop:
963         if (error)
964                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
965         else {
966                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
967                 if_inc_counter(ifp, IFCOUNTER_OBYTES, plen);
968         }
969         return (error);
970 }
971
972 static void
973 gre_qflush(struct ifnet *ifp __unused)
974 {
975
976 }
977
978 static int
979 gremodevent(module_t mod, int type, void *data)
980 {
981
982         switch (type) {
983         case MOD_LOAD:
984         case MOD_UNLOAD:
985                 break;
986         default:
987                 return (EOPNOTSUPP);
988         }
989         return (0);
990 }
991
992 static moduledata_t gre_mod = {
993         "if_gre",
994         gremodevent,
995         0
996 };
997
998 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
999 MODULE_VERSION(if_gre, 1);