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