]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if_gif.c
xnu-4570.31.3.tar.gz
[apple/xnu.git] / bsd / net / if_gif.c
CommitLineData
1c79356b 1/*
5ba3f43e 2 * Copyright (c) 2000-2017 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39236c6e 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
39236c6e 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39236c6e 17 *
2d21ac55
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
39236c6e 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
39236c6e
A
28/* $FreeBSD: src/sys/net/if_gif.c,v 1.4.2.6 2001/07/24 19:10:18 brooks Exp $ */
29/* $KAME: if_gif.c,v 1.47 2001/05/01 05:28:42 itojun Exp $ */
1c79356b
A
30
31/*
32 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the project nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 */
2d21ac55
A
59/*
60 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
61 * support for mandatory and extensible security protections. This notice
62 * is included in support of clause 2.2 (b) of the Apple Public License,
63 * Version 2.0.
64 */
1c79356b 65
1c79356b
A
66#include <sys/param.h>
67#include <sys/systm.h>
68#include <sys/kernel.h>
69#include <sys/malloc.h>
70#include <sys/mbuf.h>
71#include <sys/socket.h>
72#include <sys/sockio.h>
73#include <sys/errno.h>
74#include <sys/time.h>
75#include <sys/syslog.h>
9bccf70c 76#include <sys/protosw.h>
1c79356b
A
77#include <kern/cpu_number.h>
78
79#include <net/if.h>
80#include <net/if_types.h>
1c79356b
A
81#include <net/route.h>
82#include <net/bpf.h>
2d21ac55
A
83#include <net/kpi_protocol.h>
84#include <net/kpi_interface.h>
39236c6e 85#include <net/init.h>
1c79356b 86
1c79356b
A
87#include <netinet/in.h>
88#include <netinet/in_systm.h>
1c79356b 89#include <netinet/ip.h>
9bccf70c
A
90#if INET
91#include <netinet/in_var.h>
1c79356b 92#include <netinet/in_gif.h>
9bccf70c 93#include <netinet/ip_var.h>
1c79356b
A
94#endif /* INET */
95
96#if INET6
1c79356b
A
97#include <netinet6/in6_var.h>
98#include <netinet/ip6.h>
99#include <netinet6/ip6_var.h>
100#include <netinet6/in6_gif.h>
101#include <netinet6/ip6protosw.h>
102#endif /* INET6 */
103
104#include <netinet/ip_encap.h>
105#include <net/dlil.h>
106#include <net/if_gif.h>
107
1c79356b
A
108#include <net/net_osdep.h>
109
2d21ac55
A
110#if CONFIG_MACF_NET
111#include <security/mac_framework.h>
112#endif
113
39236c6e
A
114#define GIFNAME "gif"
115#define GIFDEV "if_gif"
116#define GIF_MAXUNIT 0x7fff /* ifp->if_unit is only 15 bits */
1c79356b 117
39236c6e
A
118/* gif lock variables */
119static lck_grp_t *gif_mtx_grp;
120static lck_grp_attr_t *gif_mtx_grp_attr;
121static lck_attr_t *gif_mtx_attr;
122decl_lck_mtx_data(static, gif_mtx_data);
123static lck_mtx_t *gif_mtx = &gif_mtx_data;
9bccf70c
A
124
125TAILQ_HEAD(gifhead, gif_softc) gifs = TAILQ_HEAD_INITIALIZER(gifs);
126
39236c6e 127static int gif_encapcheck(const struct mbuf *, int, int, void *);
2d21ac55
A
128static errno_t gif_output(ifnet_t ifp, mbuf_t m);
129static errno_t gif_input(ifnet_t ifp, protocol_family_t protocol_family,
39236c6e 130 mbuf_t m, char *frame_header);
b0d623f7 131static errno_t gif_ioctl(ifnet_t ifp, u_long cmd, void *data);
9bccf70c 132
39236c6e 133static int ngif = 0; /* number of interfaces */
9bccf70c
A
134
135#if INET
39236c6e
A
136static struct protosw in_gif_protosw =
137{
138 .pr_type = SOCK_RAW,
139 .pr_protocol = 0, /* IPPROTO_IPV[46] */
140 .pr_flags = PR_ATOMIC|PR_ADDR,
141 .pr_input = in_gif_input,
142 .pr_usrreqs = &rip_usrreqs,
143 .pr_unlock = rip_unlock,
9bccf70c
A
144};
145#endif
146#if INET6
39236c6e
A
147static struct ip6protosw in6_gif_protosw =
148{
149 .pr_type = SOCK_RAW,
150 .pr_protocol = 0, /* IPPROTO_IPV[46] */
151 .pr_flags = PR_ATOMIC|PR_ADDR,
152 .pr_input = in6_gif_input,
153 .pr_usrreqs = &rip6_usrreqs,
154 .pr_unlock = rip_unlock,
9bccf70c
A
155};
156#endif
1c79356b 157
6d2010ae
A
158static if_clone_t gif_cloner = NULL;
159static int gif_clone_create(struct if_clone *, uint32_t, void *);
160static int gif_clone_destroy(struct ifnet *);
161static void gif_delete_tunnel(struct gif_softc *);
39236c6e 162static void gif_detach(struct ifnet *);
6d2010ae 163
9bccf70c
A
164/*
165 * Theory of operation: initially, one gif interface is created.
166 * Any time a gif interface is configured, if there are no other
167 * unconfigured gif interfaces, a new gif interface is created.
168 * BSD uses the clone mechanism to dynamically create more
169 * gif interfaces.
170 *
171 * We have some extra glue to support DLIL.
172 */
173
174/* GIF interface module support */
39236c6e
A
175static int
176gif_demux(
177 ifnet_t ifp,
178 __unused mbuf_t m,
179 __unused char *frame_header,
180 protocol_family_t *protocol_family)
1c79356b 181{
39236c6e
A
182 struct gif_softc *sc = ifnet_softc(ifp);
183
184 GIF_LOCK(sc);
9bccf70c 185 /* Only one protocol may be attached to a gif interface. */
39236c6e
A
186 *protocol_family = sc->gif_proto;
187 GIF_UNLOCK(sc);
188
189 return (0);
1c79356b
A
190}
191
2d21ac55
A
192static errno_t
193gif_add_proto(
39236c6e
A
194 ifnet_t ifp,
195 protocol_family_t protocol_family,
196 __unused const struct ifnet_demux_desc *demux_array,
197 __unused u_int32_t demux_count)
9bccf70c
A
198{
199 /* Only one protocol may be attached at a time */
39236c6e 200 struct gif_softc *sc = ifnet_softc(ifp);
1c79356b 201
39236c6e
A
202 GIF_LOCK(sc);
203 if (sc->gif_proto != 0)
204 printf("gif_add_proto: request add_proto for gif%d\n",
205 ifnet_unit(ifp));
1c79356b 206
39236c6e
A
207 sc->gif_proto = protocol_family;
208 GIF_UNLOCK(sc);
1c79356b 209
39236c6e 210 return (0);
1c79356b
A
211}
212
2d21ac55
A
213static errno_t
214gif_del_proto(
39236c6e
A
215 ifnet_t ifp,
216 protocol_family_t protocol_family)
9bccf70c 217{
39236c6e
A
218 struct gif_softc *sc = ifnet_softc(ifp);
219
220 GIF_LOCK(sc);
221 if (sc->gif_proto == protocol_family)
222 sc->gif_proto = 0;
223 GIF_UNLOCK(sc);
224
225 return (0);
1c79356b
A
226}
227
9bccf70c 228/* Glue code to attach inet to a gif interface through DLIL */
2d21ac55 229static errno_t
91447636 230gif_attach_proto_family(
39236c6e
A
231 ifnet_t ifp,
232 protocol_family_t protocol_family)
1c79356b 233{
39236c6e
A
234 struct ifnet_attach_proto_param reg;
235 errno_t stat;
9bccf70c 236
39236c6e
A
237 bzero(&reg, sizeof (reg));
238 reg.input = gif_input;
1c79356b 239
39236c6e
A
240 stat = ifnet_attach_protocol(ifp, protocol_family, &reg);
241 if (stat && stat != EEXIST) {
242 printf("gif_attach_proto_family can't attach interface \
243 fam=%d\n", protocol_family);
244 }
55e303ae 245
39236c6e 246 return (stat);
55e303ae
A
247}
248
9bccf70c 249/* Function to setup the first gif interface */
39236c6e
A
250void
251gif_init(void)
1c79356b 252{
2d21ac55 253 errno_t result;
6d2010ae 254 struct ifnet_clone_params ifnet_clone_params;
39236c6e 255 struct if_clone *ifc = NULL;
55e303ae 256
39236c6e 257 /* Initialize the list of interfaces */
9bccf70c 258 TAILQ_INIT(&gifs);
1c79356b 259
39236c6e
A
260 /* Initialize the gif global lock */
261 gif_mtx_grp_attr = lck_grp_attr_alloc_init();
262 gif_mtx_grp = lck_grp_alloc_init("gif", gif_mtx_grp_attr);
263 gif_mtx_attr = lck_attr_alloc_init();
264 lck_mtx_init(gif_mtx, gif_mtx_grp, gif_mtx_attr);
265
55e303ae 266 /* Register protocol registration functions */
2d21ac55 267 result = proto_register_plumber(PF_INET, APPLE_IF_FAM_GIF,
39236c6e 268 gif_attach_proto_family, NULL);
2d21ac55 269 if (result != 0)
39236c6e
A
270 printf("proto_register_plumber failed for AF_INET error=%d\n",
271 result);
272
2d21ac55 273 result = proto_register_plumber(PF_INET6, APPLE_IF_FAM_GIF,
39236c6e 274 gif_attach_proto_family, NULL);
2d21ac55 275 if (result != 0)
39236c6e
A
276 printf("proto_register_plumber failed for AF_INET6 error=%d\n",
277 result);
55e303ae 278
6d2010ae
A
279 ifnet_clone_params.ifc_name = "gif";
280 ifnet_clone_params.ifc_create = gif_clone_create;
281 ifnet_clone_params.ifc_destroy = gif_clone_destroy;
282
283 result = ifnet_clone_attach(&ifnet_clone_params, &gif_cloner);
284 if (result != 0)
285 printf("gifattach: ifnet_clone_attach failed %d\n", result);
286
9bccf70c 287 /* Create first device */
6d2010ae
A
288 ifc = if_clone_lookup("gif", NULL);
289 gif_clone_create(ifc, 0, NULL);
9bccf70c
A
290}
291
2d21ac55
A
292static errno_t
293gif_set_bpf_tap(
39236c6e
A
294 ifnet_t ifp,
295 bpf_tap_mode mode,
296 bpf_packet_func callback)
2d21ac55 297{
39236c6e
A
298 struct gif_softc *sc = ifnet_softc(ifp);
299
300 GIF_LOCK(sc);
2d21ac55
A
301 sc->tap_mode = mode;
302 sc->tap_callback = callback;
39236c6e
A
303 GIF_UNLOCK(sc);
304
305 return (0);
2d21ac55
A
306}
307
39236c6e
A
308static void
309gif_detach(struct ifnet *ifp)
310{
311 struct gif_softc *sc = ifp->if_softc;
312 lck_mtx_destroy(&sc->gif_lock, gif_mtx_grp);
313 _FREE(ifp->if_softc, M_DEVBUF);
314 ifp->if_softc = NULL;
315 (void) ifnet_release(ifp);
316}
6d2010ae
A
317
318static int
319gif_clone_create(struct if_clone *ifc, uint32_t unit, __unused void *params)
9bccf70c 320{
39236c6e 321 struct gif_softc *sc = NULL;
5ba3f43e 322 struct ifnet_init_eparams gif_init_params;
39236c6e
A
323 errno_t error = 0;
324
325 lck_mtx_lock(gif_mtx);
6d2010ae 326
9bccf70c 327 /* Can't create more than GIF_MAXUNIT */
39236c6e
A
328 if (ngif >= GIF_MAXUNIT) {
329 error = ENXIO;
330 goto done;
331 }
9bccf70c 332
3e170ce0 333 sc = _MALLOC(sizeof (struct gif_softc), M_DEVBUF, M_WAITOK | M_ZERO);
9bccf70c 334 if (sc == NULL) {
39236c6e
A
335 log(LOG_ERR, "gif_clone_create: failed to allocate gif%d\n",
336 unit);
337 error = ENOBUFS;
338 goto done;
9bccf70c 339 }
6d2010ae
A
340
341 /* use the interface name as the unique id for ifp recycle */
39236c6e
A
342 snprintf(sc->gif_ifname, sizeof (sc->gif_ifname), "%s%d",
343 ifc->ifc_name, unit);
344
345 lck_mtx_init(&sc->gif_lock, gif_mtx_grp, gif_mtx_attr);
346
347 bzero(&gif_init_params, sizeof (gif_init_params));
5ba3f43e
A
348 gif_init_params.ver = IFNET_INIT_CURRENT_VERSION;
349 gif_init_params.len = sizeof (gif_init_params);
350 gif_init_params.flags = IFNET_INIT_LEGACY;
39236c6e
A
351 gif_init_params.uniqueid = sc->gif_ifname;
352 gif_init_params.uniqueid_len = strlen(sc->gif_ifname);
353 gif_init_params.name = GIFNAME;
354 gif_init_params.unit = unit;
355 gif_init_params.type = IFT_GIF;
356 gif_init_params.family = IFNET_FAMILY_GIF;
357 gif_init_params.output = gif_output;
358 gif_init_params.demux = gif_demux;
359 gif_init_params.add_proto = gif_add_proto;
360 gif_init_params.del_proto = gif_del_proto;
361 gif_init_params.softc = sc;
362 gif_init_params.ioctl = gif_ioctl;
363 gif_init_params.set_bpf_tap = gif_set_bpf_tap;
364 gif_init_params.detach = gif_detach;
365
5ba3f43e 366 error = ifnet_allocate_extended(&gif_init_params, &sc->gif_if);
39236c6e
A
367 if (error != 0) {
368 printf("gif_clone_create, ifnet_allocate failed - %d\n", error);
2d21ac55 369 _FREE(sc, M_DEVBUF);
39236c6e
A
370 error = ENOBUFS;
371 goto done;
2d21ac55 372 }
6d2010ae 373
9bccf70c 374 sc->encap_cookie4 = sc->encap_cookie6 = NULL;
2d21ac55 375#if INET
9bccf70c 376 sc->encap_cookie4 = encap_attach_func(AF_INET, -1,
6d2010ae 377 gif_encapcheck, &in_gif_protosw, sc);
9bccf70c 378 if (sc->encap_cookie4 == NULL) {
2d21ac55
A
379 printf("%s: unable to attach encap4\n", if_name(sc->gif_if));
380 ifnet_release(sc->gif_if);
9bccf70c 381 FREE(sc, M_DEVBUF);
39236c6e
A
382 error = ENOBUFS;
383 goto done;
9bccf70c
A
384 }
385#endif
2d21ac55 386#if INET6
9bccf70c 387 sc->encap_cookie6 = encap_attach_func(AF_INET6, -1,
39236c6e 388 gif_encapcheck, (struct protosw *)&in6_gif_protosw, sc);
9bccf70c
A
389 if (sc->encap_cookie6 == NULL) {
390 if (sc->encap_cookie4) {
391 encap_detach(sc->encap_cookie4);
392 sc->encap_cookie4 = NULL;
393 }
2d21ac55
A
394 printf("%s: unable to attach encap6\n", if_name(sc->gif_if));
395 ifnet_release(sc->gif_if);
9bccf70c 396 FREE(sc, M_DEVBUF);
39236c6e
A
397 error = ENOBUFS;
398 goto done;
9bccf70c
A
399 }
400#endif
91447636 401 sc->gif_called = 0;
2d21ac55
A
402 ifnet_set_mtu(sc->gif_if, GIF_MTU);
403 ifnet_set_flags(sc->gif_if, IFF_POINTOPOINT | IFF_MULTICAST, 0xffff);
1c79356b 404#if 0
9bccf70c
A
405 /* turn off ingress filter */
406 sc->gif_if.if_flags |= IFF_LINK2;
1c79356b 407#endif
39236c6e
A
408 error = ifnet_attach(sc->gif_if, NULL);
409 if (error != 0) {
410 printf("gif_clone_create - ifnet_attach failed - %d\n", error);
2d21ac55 411 ifnet_release(sc->gif_if);
6d2010ae
A
412 if (sc->encap_cookie4) {
413 encap_detach(sc->encap_cookie4);
414 sc->encap_cookie4 = NULL;
415 }
416 if (sc->encap_cookie6) {
417 encap_detach(sc->encap_cookie6);
418 sc->encap_cookie6 = NULL;
419 }
2d21ac55 420 FREE(sc, M_DEVBUF);
39236c6e 421 goto done;
2d21ac55
A
422 }
423#if CONFIG_MACF_NET
424 mac_ifnet_label_init(&sc->gif_if);
425#endif
39236c6e 426 bpfattach(sc->gif_if, DLT_NULL, sizeof (u_int));
9bccf70c
A
427 TAILQ_INSERT_TAIL(&gifs, sc, gif_link);
428 ngif++;
39236c6e
A
429done:
430 lck_mtx_unlock(gif_mtx);
431
432 return (error);
6d2010ae
A
433}
434
435static int
436gif_clone_destroy(struct ifnet *ifp)
437{
438#if defined(INET) || defined(INET6)
39236c6e 439 int error = 0;
6d2010ae
A
440#endif
441 struct gif_softc *sc = ifp->if_softc;
442
39236c6e 443 lck_mtx_lock(gif_mtx);
6d2010ae 444 TAILQ_REMOVE(&gifs, sc, gif_link);
39236c6e 445 ngif--;
6d2010ae 446
39236c6e 447 GIF_LOCK(sc);
6d2010ae
A
448 gif_delete_tunnel(sc);
449#ifdef INET6
450 if (sc->encap_cookie6 != NULL) {
39236c6e
A
451 error = encap_detach(sc->encap_cookie6);
452 KASSERT(error == 0, ("gif_clone_destroy: Unexpected \
453 error detaching encap_cookie6"));
6d2010ae
A
454 }
455#endif
456#ifdef INET
457 if (sc->encap_cookie4 != NULL) {
39236c6e
A
458 error = encap_detach(sc->encap_cookie4);
459 KASSERT(error == 0, ("gif_clone_destroy: Unexpected \
460 error detaching encap_cookie4"));
6d2010ae
A
461 }
462#endif
39236c6e
A
463 error = ifnet_set_flags(ifp, 0, IFF_UP);
464 if (error != 0) {
465 printf("gif_clone_destroy: ifnet_set_flags failed %d\n", error);
6d2010ae
A
466 }
467
39236c6e
A
468 error = ifnet_detach(ifp);
469 if (error != 0)
470 panic("gif_clone_destroy: ifnet_detach(%p) failed %d\n", ifp,
471 error);
472
473 GIF_UNLOCK(sc);
474 lck_mtx_unlock(gif_mtx);
475
476 return (0);
9bccf70c
A
477}
478
479static int
2d21ac55
A
480gif_encapcheck(
481 const struct mbuf *m,
482 int off,
483 int proto,
484 void *arg)
9bccf70c 485{
39236c6e 486 int error = 0;
9bccf70c
A
487 struct ip ip;
488 struct gif_softc *sc;
489
490 sc = (struct gif_softc *)arg;
491 if (sc == NULL)
39236c6e 492 return (error);
9bccf70c 493
39236c6e 494 GIF_LOCK(sc);
2d21ac55 495 if ((ifnet_flags(sc->gif_if) & IFF_UP) == 0)
39236c6e 496 goto done;
9bccf70c
A
497
498 /* no physical address */
499 if (!sc->gif_psrc || !sc->gif_pdst)
39236c6e 500 goto done;
9bccf70c
A
501
502 switch (proto) {
503#if INET
504 case IPPROTO_IPV4:
505 break;
1c79356b 506#endif
9bccf70c
A
507#if INET6
508 case IPPROTO_IPV6:
509 break;
1c79356b 510#endif
9bccf70c 511 default:
39236c6e 512 goto done;
1c79356b 513 }
1c79356b 514
39236c6e 515 mbuf_copydata((struct mbuf *)(size_t)m, 0, sizeof (ip), &ip);
9bccf70c
A
516
517 switch (ip.ip_v) {
518#if INET
519 case 4:
520 if (sc->gif_psrc->sa_family != AF_INET ||
521 sc->gif_pdst->sa_family != AF_INET)
39236c6e
A
522 goto done;
523 error = gif_encapcheck4(m, off, proto, arg);
9bccf70c
A
524#endif
525#if INET6
526 case 6:
527 if (sc->gif_psrc->sa_family != AF_INET6 ||
528 sc->gif_pdst->sa_family != AF_INET6)
39236c6e
A
529 goto done;
530 error = gif_encapcheck6(m, off, proto, arg);
1c79356b 531#endif
9bccf70c 532 default:
39236c6e 533 goto done;
9bccf70c 534 }
39236c6e
A
535done:
536 GIF_UNLOCK(sc);
537 return (error);
9bccf70c 538}
1c79356b 539
2d21ac55
A
540static errno_t
541gif_output(
39236c6e
A
542 ifnet_t ifp,
543 mbuf_t m)
1c79356b 544{
2d21ac55 545 struct gif_softc *sc = ifnet_softc(ifp);
39236c6e
A
546 struct sockaddr *gif_psrc;
547 struct sockaddr *gif_pdst;
1c79356b 548 int error = 0;
39236c6e
A
549
550 GIF_LOCK(sc);
551 gif_psrc = sc->gif_psrc;
552 gif_pdst = sc->gif_pdst;
553 GIF_UNLOCK(sc);
554
1c79356b 555 /*
39236c6e
A
556 * max_gif_nesting check used to live here. It doesn't anymore
557 * because there is no guaruntee that we won't be called
558 * concurrently from more than one thread.
1c79356b 559 */
1c79356b 560 m->m_flags &= ~(M_BCAST|M_MCAST);
2d21ac55 561 if (!(ifnet_flags(ifp) & IFF_UP) ||
39236c6e 562 gif_psrc == NULL || gif_pdst == NULL) {
2d21ac55 563 ifnet_touch_lastchange(ifp);
55e303ae 564 m_freem(m); /* free it here not in dlil_output */
1c79356b 565 error = ENETDOWN;
1c79356b
A
566 goto end;
567 }
568
39236c6e
A
569 bpf_tap_out(ifp, 0, m, &sc->gif_proto, sizeof (sc->gif_proto));
570
571 GIF_LOCK(sc);
572
9bccf70c
A
573 /* inner AF-specific encapsulation */
574
575 /* XXX should we check if our outer source is legal? */
576
577 /* dispatch to output logic based on outer AF */
1c79356b
A
578 switch (sc->gif_psrc->sa_family) {
579#if INET
580 case AF_INET:
2d21ac55 581 error = in_gif_output(ifp, sc->gif_proto, m, NULL);
1c79356b
A
582 break;
583#endif
584#if INET6
585 case AF_INET6:
2d21ac55 586 error = in6_gif_output(ifp, sc->gif_proto, m, NULL);
1c79356b
A
587 break;
588#endif
589 default:
1c79356b 590 error = ENETDOWN;
15129b1c 591 break;
1c79356b 592 }
1c79356b 593
39236c6e 594 GIF_UNLOCK(sc);
15129b1c 595end:
55e303ae
A
596 if (error) {
597 /* the mbuf was freed either by in_gif_output or in here */
2d21ac55 598 ifnet_stat_increment_out(ifp, 0, 0, 1);
39236c6e 599 } else {
2d21ac55 600 ifnet_stat_increment_out(ifp, 1, m->m_pkthdr.len, 0);
55e303ae 601 }
39236c6e 602 if (error == 0)
9bccf70c 603 error = EJUSTRETURN; /* if no error, packet got sent already */
39236c6e 604 return (error);
1c79356b
A
605}
606
2d21ac55
A
607/*
608 * gif_input is the input handler for IP and IPv6 attached to gif
609 */
610static errno_t
91447636 611gif_input(
39236c6e
A
612 ifnet_t ifp,
613 protocol_family_t protocol_family,
614 mbuf_t m,
615 __unused char *frame_header)
1c79356b 616{
2d21ac55 617 struct gif_softc *sc = ifnet_softc(ifp);
39236c6e
A
618
619 bpf_tap_in(ifp, 0, m, &sc->gif_proto, sizeof (sc->gif_proto));
1c79356b
A
620
621 /*
622 * Put the packet to the network layer input queue according to the
623 * specified address family.
624 * Note: older versions of gif_input directly called network layer
625 * input functions, e.g. ip6_input, here. We changed the policy to
626 * prevent too many recursive calls of such input functions, which
627 * might cause kernel panic. But the change may introduce another
628 * problem; if the input queue is full, packets are discarded.
629 * We believed it rarely occurs and changed the policy. If we find
630 * it occurs more times than we thought, we may change the policy
631 * again.
632 */
6d2010ae
A
633 if (proto_input(protocol_family, m) != 0) {
634 ifnet_stat_increment_in(ifp, 0, 0, 1);
635 m_freem(m);
636 } else
637 ifnet_stat_increment_in(ifp, 1, m->m_pkthdr.len, 0);
1c79356b 638
9bccf70c 639 return (0);
1c79356b
A
640}
641
642/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
2d21ac55
A
643static errno_t
644gif_ioctl(
645 ifnet_t ifp,
b0d623f7 646 u_long cmd,
2d21ac55 647 void *data)
1c79356b 648{
2d21ac55 649 struct gif_softc *sc = ifnet_softc(ifp);
39236c6e 650 struct ifreq *ifr = (struct ifreq *)data;
1c79356b 651 int error = 0, size;
2d21ac55 652 struct sockaddr *dst = NULL, *src = NULL;
9bccf70c 653 struct sockaddr *sa;
9bccf70c 654 struct ifnet *ifp2;
1c79356b 655 struct gif_softc *sc2;
b0d623f7 656
1c79356b
A
657 switch (cmd) {
658 case SIOCSIFADDR:
659 break;
b0d623f7 660
1c79356b
A
661 case SIOCSIFDSTADDR:
662 break;
663
664 case SIOCADDMULTI:
665 case SIOCDELMULTI:
1c79356b
A
666 break;
667
668#ifdef SIOCSIFMTU /* xxx */
1c79356b
A
669 case SIOCGIFMTU:
670 break;
9bccf70c 671
1c79356b
A
672 case SIOCSIFMTU:
673 {
b0d623f7 674 u_int32_t mtu;
1c79356b 675 mtu = ifr->ifr_mtu;
1c79356b
A
676 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
677 return (EINVAL);
678 }
2d21ac55 679 ifnet_set_mtu(ifp, mtu);
1c79356b
A
680 }
681 break;
1c79356b
A
682#endif /* SIOCSIFMTU */
683
684 case SIOCSIFPHYADDR:
685#if INET6
b0d623f7
A
686 case SIOCSIFPHYADDR_IN6_32:
687 case SIOCSIFPHYADDR_IN6_64:
1c79356b 688#endif /* INET6 */
9bccf70c
A
689 switch (cmd) {
690#if INET
691 case SIOCSIFPHYADDR:
692 src = (struct sockaddr *)
693 &(((struct in_aliasreq *)data)->ifra_addr);
694 dst = (struct sockaddr *)
695 &(((struct in_aliasreq *)data)->ifra_dstaddr);
696 break;
697#endif
698#if INET6
b0d623f7
A
699 case SIOCSIFPHYADDR_IN6_32: {
700 struct in6_aliasreq_32 *ifra_32 =
701 (struct in6_aliasreq_32 *)data;
702
703 src = (struct sockaddr *)&ifra_32->ifra_addr;
704 dst = (struct sockaddr *)&ifra_32->ifra_dstaddr;
9bccf70c 705 break;
b0d623f7
A
706 }
707
708 case SIOCSIFPHYADDR_IN6_64: {
709 struct in6_aliasreq_64 *ifra_64 =
710 (struct in6_aliasreq_64 *)data;
711
712 src = (struct sockaddr *)&ifra_64->ifra_addr;
713 dst = (struct sockaddr *)&ifra_64->ifra_dstaddr;
714 break;
715 }
9bccf70c 716#endif
9bccf70c
A
717 }
718
719 /* sa_family must be equal */
720 if (src->sa_family != dst->sa_family)
39236c6e 721 return (EINVAL);
9bccf70c
A
722
723 /* validate sa_len */
724 switch (src->sa_family) {
725#if INET
726 case AF_INET:
39236c6e
A
727 if (src->sa_len != sizeof (struct sockaddr_in))
728 return (EINVAL);
9bccf70c
A
729 break;
730#endif
731#if INET6
732 case AF_INET6:
39236c6e
A
733 if (src->sa_len != sizeof (struct sockaddr_in6))
734 return (EINVAL);
9bccf70c
A
735 break;
736#endif
737 default:
39236c6e 738 return (EAFNOSUPPORT);
9bccf70c
A
739 }
740 switch (dst->sa_family) {
741#if INET
742 case AF_INET:
39236c6e
A
743 if (dst->sa_len != sizeof (struct sockaddr_in))
744 return (EINVAL);
9bccf70c
A
745 break;
746#endif
747#if INET6
748 case AF_INET6:
39236c6e
A
749 if (dst->sa_len != sizeof (struct sockaddr_in6))
750 return (EINVAL);
9bccf70c
A
751 break;
752#endif
753 default:
39236c6e 754 return (EAFNOSUPPORT);
9bccf70c
A
755 }
756
757 /* check sa_family looks sane for the cmd */
758 switch (cmd) {
759 case SIOCSIFPHYADDR:
760 if (src->sa_family == AF_INET)
761 break;
39236c6e 762 return (EAFNOSUPPORT);
9bccf70c 763#if INET6
b0d623f7
A
764 case SIOCSIFPHYADDR_IN6_32:
765 case SIOCSIFPHYADDR_IN6_64:
9bccf70c
A
766 if (src->sa_family == AF_INET6)
767 break;
39236c6e 768 return (EAFNOSUPPORT);
9bccf70c 769#endif /* INET6 */
9bccf70c
A
770 }
771
39236c6e
A
772#define GIF_ORDERED_LOCK(sc, sc2) \
773 if (sc < sc2) { \
774 GIF_LOCK(sc); \
775 GIF_LOCK(sc2); \
776 } else { \
777 GIF_LOCK(sc2); \
778 GIF_LOCK(sc); \
779 }
780
781#define GIF_ORDERED_UNLOCK(sc, sc2) \
782 if (sc > sc2) { \
783 GIF_UNLOCK(sc); \
784 GIF_UNLOCK(sc2); \
785 } else { \
786 GIF_UNLOCK(sc2); \
787 GIF_UNLOCK(sc); \
788 }
789
91447636
A
790 ifnet_head_lock_shared();
791 TAILQ_FOREACH(ifp2, &ifnet_head, if_link) {
2d21ac55 792 if (strcmp(ifnet_name(ifp2), GIFNAME) != 0)
9bccf70c 793 continue;
2d21ac55 794 sc2 = ifnet_softc(ifp2);
1c79356b
A
795 if (sc2 == sc)
796 continue;
39236c6e
A
797 /* lock sc and sc2 in increasing order of ifnet index */
798 GIF_ORDERED_LOCK(sc, sc2);
799 if (!sc2->gif_pdst || !sc2->gif_psrc) {
800 GIF_ORDERED_UNLOCK(sc, sc2);
1c79356b 801 continue;
39236c6e 802 }
9bccf70c
A
803 if (sc2->gif_pdst->sa_family != dst->sa_family ||
804 sc2->gif_pdst->sa_len != dst->sa_len ||
805 sc2->gif_psrc->sa_family != src->sa_family ||
39236c6e
A
806 sc2->gif_psrc->sa_len != src->sa_len) {
807 GIF_ORDERED_UNLOCK(sc, sc2);
9bccf70c 808 continue;
39236c6e 809 }
9bccf70c
A
810#ifndef XBONEHACK
811 /* can't configure same pair of address onto two gifs */
812 if (bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
1c79356b 813 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
39236c6e 814 GIF_ORDERED_UNLOCK(sc, sc2);
1c79356b 815 error = EADDRNOTAVAIL;
91447636 816 ifnet_head_done();
1c79356b
A
817 goto bad;
818 }
9bccf70c 819#endif
1c79356b 820
9bccf70c 821 /* can't configure multiple multi-dest interfaces */
39236c6e 822#define multidest(x) \
316670eb 823 (((struct sockaddr_in *)(void *)(x))->sin_addr.s_addr == INADDR_ANY)
1c79356b 824#if INET6
39236c6e
A
825#define multidest6(x) \
826 (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *) \
827 (void *)(x))->sin6_addr))
9bccf70c
A
828#endif
829 if (dst->sa_family == AF_INET &&
830 multidest(dst) && multidest(sc2->gif_pdst)) {
39236c6e 831 GIF_ORDERED_UNLOCK(sc, sc2);
9bccf70c 832 error = EADDRNOTAVAIL;
91447636 833 ifnet_head_done();
9bccf70c
A
834 goto bad;
835 }
836#if INET6
837 if (dst->sa_family == AF_INET6 &&
838 multidest6(dst) && multidest6(sc2->gif_pdst)) {
39236c6e 839 GIF_ORDERED_UNLOCK(sc, sc2);
9bccf70c 840 error = EADDRNOTAVAIL;
91447636 841 ifnet_head_done();
9bccf70c
A
842 goto bad;
843 }
844#endif
39236c6e 845 GIF_ORDERED_UNLOCK(sc, sc2);
1c79356b 846 }
91447636 847 ifnet_head_done();
9bccf70c 848
39236c6e 849 GIF_LOCK(sc);
9bccf70c
A
850 if (sc->gif_psrc)
851 FREE((caddr_t)sc->gif_psrc, M_IFADDR);
39236c6e
A
852 sa = (struct sockaddr *)_MALLOC(src->sa_len, M_IFADDR,
853 M_WAITOK);
854 if (sa == NULL) {
855 GIF_UNLOCK(sc);
856 return (ENOBUFS);
857 }
9bccf70c
A
858 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
859 sc->gif_psrc = sa;
860
861 if (sc->gif_pdst)
862 FREE((caddr_t)sc->gif_pdst, M_IFADDR);
39236c6e
A
863 sa = (struct sockaddr *)_MALLOC(dst->sa_len, M_IFADDR,
864 M_WAITOK);
865 if (sa == NULL) {
866 GIF_UNLOCK(sc);
867 return (ENOBUFS);
868 }
9bccf70c
A
869 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
870 sc->gif_pdst = sa;
39236c6e
A
871 GIF_UNLOCK(sc);
872
873 ifnet_set_flags(ifp, IFF_RUNNING | IFF_UP, IFF_RUNNING |
874 IFF_UP);
9bccf70c 875
9bccf70c 876 error = 0;
1c79356b 877 break;
9bccf70c
A
878
879#ifdef SIOCDIFPHYADDR
880 case SIOCDIFPHYADDR:
39236c6e 881 GIF_LOCK(sc);
9bccf70c
A
882 if (sc->gif_psrc) {
883 FREE((caddr_t)sc->gif_psrc, M_IFADDR);
884 sc->gif_psrc = NULL;
885 }
886 if (sc->gif_pdst) {
887 FREE((caddr_t)sc->gif_pdst, M_IFADDR);
888 sc->gif_pdst = NULL;
889 }
39236c6e 890 GIF_UNLOCK(sc);
9bccf70c
A
891 /* change the IFF_{UP, RUNNING} flag as well? */
892 break;
893#endif
39236c6e 894
1c79356b
A
895 case SIOCGIFPSRCADDR:
896#if INET6
897 case SIOCGIFPSRCADDR_IN6:
898#endif /* INET6 */
39236c6e 899 GIF_LOCK(sc);
1c79356b 900 if (sc->gif_psrc == NULL) {
39236c6e 901 GIF_UNLOCK(sc);
1c79356b
A
902 error = EADDRNOTAVAIL;
903 goto bad;
904 }
905 src = sc->gif_psrc;
9bccf70c 906 switch (cmd) {
1c79356b 907#if INET
9bccf70c 908 case SIOCGIFPSRCADDR:
1c79356b 909 dst = &ifr->ifr_addr;
39236c6e 910 size = sizeof (ifr->ifr_addr);
1c79356b
A
911 break;
912#endif /* INET */
913#if INET6
9bccf70c 914 case SIOCGIFPSRCADDR_IN6:
1c79356b
A
915 dst = (struct sockaddr *)
916 &(((struct in6_ifreq *)data)->ifr_addr);
39236c6e 917 size = sizeof (((struct in6_ifreq *)data)->ifr_addr);
1c79356b
A
918 break;
919#endif /* INET6 */
920 default:
39236c6e 921 GIF_UNLOCK(sc);
1c79356b
A
922 error = EADDRNOTAVAIL;
923 goto bad;
924 }
39236c6e
A
925 if (src->sa_len > size) {
926 GIF_UNLOCK(sc);
927 return (EINVAL);
928 }
9bccf70c 929 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
39236c6e 930 GIF_UNLOCK(sc);
1c79356b 931 break;
39236c6e 932
1c79356b
A
933 case SIOCGIFPDSTADDR:
934#if INET6
935 case SIOCGIFPDSTADDR_IN6:
936#endif /* INET6 */
39236c6e 937 GIF_LOCK(sc);
1c79356b 938 if (sc->gif_pdst == NULL) {
39236c6e 939 GIF_UNLOCK(sc);
1c79356b
A
940 error = EADDRNOTAVAIL;
941 goto bad;
942 }
943 src = sc->gif_pdst;
9bccf70c 944 switch (cmd) {
1c79356b 945#if INET
9bccf70c 946 case SIOCGIFPDSTADDR:
1c79356b 947 dst = &ifr->ifr_addr;
39236c6e 948 size = sizeof (ifr->ifr_addr);
1c79356b
A
949 break;
950#endif /* INET */
951#if INET6
9bccf70c 952 case SIOCGIFPDSTADDR_IN6:
1c79356b
A
953 dst = (struct sockaddr *)
954 &(((struct in6_ifreq *)data)->ifr_addr);
39236c6e 955 size = sizeof (((struct in6_ifreq *)data)->ifr_addr);
1c79356b
A
956 break;
957#endif /* INET6 */
958 default:
959 error = EADDRNOTAVAIL;
39236c6e 960 GIF_UNLOCK(sc);
1c79356b
A
961 goto bad;
962 }
39236c6e
A
963 if (src->sa_len > size) {
964 GIF_UNLOCK(sc);
965 return (EINVAL);
966 }
9bccf70c 967 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
39236c6e 968 GIF_UNLOCK(sc);
1c79356b
A
969 break;
970
9bccf70c
A
971 case SIOCSIFFLAGS:
972 /* if_ioctl() takes care of it */
1c79356b
A
973 break;
974
975 default:
9bccf70c 976 error = EOPNOTSUPP;
1c79356b
A
977 break;
978 }
39236c6e
A
979bad:
980 return (error);
1c79356b 981}
9bccf70c 982
39236c6e
A
983static void
984gif_delete_tunnel(struct gif_softc *sc)
9bccf70c 985{
39236c6e 986 GIF_LOCK_ASSERT(sc);
9bccf70c
A
987 if (sc->gif_psrc) {
988 FREE((caddr_t)sc->gif_psrc, M_IFADDR);
989 sc->gif_psrc = NULL;
990 }
991 if (sc->gif_pdst) {
992 FREE((caddr_t)sc->gif_pdst, M_IFADDR);
993 sc->gif_pdst = NULL;
994 }
39236c6e 995 ROUTE_RELEASE(&sc->gif_ro);
9bccf70c
A
996 /* change the IFF_UP flag as well? */
997}