]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if_gif.c
xnu-1228.7.58.tar.gz
[apple/xnu.git] / bsd / net / if_gif.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 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.
8f6c56a5 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.
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
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
9bccf70c
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>
1c79356b 85
1c79356b
A
86#include <netinet/in.h>
87#include <netinet/in_systm.h>
1c79356b 88#include <netinet/ip.h>
9bccf70c
A
89#if INET
90#include <netinet/in_var.h>
1c79356b 91#include <netinet/in_gif.h>
9bccf70c 92#include <netinet/ip_var.h>
1c79356b
A
93#endif /* INET */
94
95#if INET6
1c79356b
A
96#include <netinet6/in6_var.h>
97#include <netinet/ip6.h>
98#include <netinet6/ip6_var.h>
99#include <netinet6/in6_gif.h>
100#include <netinet6/ip6protosw.h>
101#endif /* INET6 */
102
103#include <netinet/ip_encap.h>
104#include <net/dlil.h>
105#include <net/if_gif.h>
106
1c79356b
A
107#include <net/net_osdep.h>
108
2d21ac55
A
109#if CONFIG_MACF_NET
110#include <security/mac_framework.h>
111#endif
112
9bccf70c
A
113#define GIFNAME "gif"
114#define GIFDEV "if_gif"
115#define GIF_MAXUNIT 0x7fff /* ifp->if_unit is only 15 bits */
1c79356b 116
9bccf70c
A
117#ifndef __APPLE__
118static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
119#endif
120
121TAILQ_HEAD(gifhead, gif_softc) gifs = TAILQ_HEAD_INITIALIZER(gifs);
122
123#ifdef __APPLE__
91447636 124void gifattach(void);
9bccf70c
A
125static void gif_create_dev(void);
126static int gif_encapcheck(const struct mbuf*, int, int, void*);
2d21ac55
A
127static errno_t gif_output(ifnet_t ifp, mbuf_t m);
128static errno_t gif_input(ifnet_t ifp, protocol_family_t protocol_family,
129 mbuf_t m, char *frame_header);
130static errno_t gif_ioctl(ifnet_t ifp, u_int32_t cmd, void *data);
9bccf70c
A
131
132int ngif = 0; /* number of interfaces */
133#endif
134
135#if INET
136struct protosw in_gif_protosw =
137{ SOCK_RAW, 0, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC|PR_ADDR,
138 in_gif_input, 0, 0, 0,
139 0,
91447636 140 0, 0, 0, 0,
9bccf70c 141 0,
91447636 142 &rip_usrreqs,
2d21ac55 143 0, rip_unlock, 0, {0, 0}, 0, {0}
9bccf70c
A
144};
145#endif
146#if INET6
147struct ip6protosw in6_gif_protosw =
148{ SOCK_RAW, 0, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC|PR_ADDR,
91447636 149 in6_gif_input, 0, 0, 0,
9bccf70c
A
150 0,
151 0, 0, 0, 0,
91447636
A
152 0,
153 &rip6_usrreqs,
2d21ac55 154 0, rip_unlock, 0, {0, 0}, 0, {0}
91447636 155
9bccf70c
A
156};
157#endif
1c79356b 158
9bccf70c
A
159#ifdef __APPLE__
160/*
161 * Theory of operation: initially, one gif interface is created.
162 * Any time a gif interface is configured, if there are no other
163 * unconfigured gif interfaces, a new gif interface is created.
164 * BSD uses the clone mechanism to dynamically create more
165 * gif interfaces.
166 *
167 * We have some extra glue to support DLIL.
168 */
169
170/* GIF interface module support */
2d21ac55
A
171static int gif_demux(
172 ifnet_t ifp,
173 __unused mbuf_t m,
174 __unused char *frame_header,
175 protocol_family_t *protocol_family)
1c79356b 176{
9bccf70c 177 /* Only one protocol may be attached to a gif interface. */
2d21ac55 178 *protocol_family = ((struct gif_softc*)ifnet_softc(ifp))->gif_proto;
9bccf70c
A
179
180 return 0;
1c79356b
A
181}
182
2d21ac55
A
183static errno_t
184gif_add_proto(
185 ifnet_t ifp,
186 protocol_family_t protocol_family,
187 __unused const struct ifnet_demux_desc *demux_array,
188 __unused u_int32_t demux_count)
9bccf70c
A
189{
190 /* Only one protocol may be attached at a time */
2d21ac55 191 struct gif_softc* gif = ifnet_softc(ifp);
1c79356b 192
91447636 193 if (gif->gif_proto != 0)
2d21ac55 194 printf("gif_add_proto: request add_proto for gif%d\n", ifnet_unit(ifp));
1c79356b 195
91447636 196 gif->gif_proto = protocol_family;
1c79356b 197
9bccf70c 198 return 0;
1c79356b
A
199}
200
2d21ac55
A
201static errno_t
202gif_del_proto(
203 ifnet_t ifp,
204 protocol_family_t protocol_family)
9bccf70c 205{
2d21ac55
A
206 if (((struct gif_softc*)ifnet_softc(ifp))->gif_proto == protocol_family)
207 ((struct gif_softc*)ifnet_softc(ifp))->gif_proto = 0;
208
9bccf70c 209 return 0;
1c79356b
A
210}
211
9bccf70c 212/* Glue code to attach inet to a gif interface through DLIL */
2d21ac55 213static errno_t
91447636 214gif_attach_proto_family(
2d21ac55
A
215 ifnet_t ifp,
216 protocol_family_t protocol_family)
1c79356b 217{
2d21ac55
A
218 struct ifnet_attach_proto_param reg;
219 errno_t stat;
9bccf70c 220
91447636 221 bzero(&reg, sizeof(reg));
1c79356b 222 reg.input = gif_input;
1c79356b 223
2d21ac55 224 stat = ifnet_attach_protocol(ifp, protocol_family, &reg);
91447636 225 if (stat && stat != EEXIST) {
2d21ac55
A
226 printf("gif_attach_proto_family can't attach interface fam=%d\n",
227 protocol_family);
9bccf70c 228 }
55e303ae 229
91447636 230 return stat;
55e303ae
A
231}
232
9bccf70c
A
233#endif
234
235/* Function to setup the first gif interface */
2d21ac55 236__private_extern__ void
9bccf70c 237gifattach(void)
1c79356b 238{
2d21ac55 239 errno_t result;
55e303ae 240
9bccf70c
A
241 /* Init the list of interfaces */
242 TAILQ_INIT(&gifs);
1c79356b 243
55e303ae 244 /* Register protocol registration functions */
2d21ac55
A
245 result = proto_register_plumber(PF_INET, APPLE_IF_FAM_GIF,
246 gif_attach_proto_family, NULL);
247 if (result != 0)
248 printf("proto_register_plumber failed for AF_INET error=%d\n", result);
55e303ae 249
2d21ac55
A
250 result = proto_register_plumber(PF_INET6, APPLE_IF_FAM_GIF,
251 gif_attach_proto_family, NULL);
252 if (result != 0)
253 printf("proto_register_plumber failed for AF_INET6 error=%d\n", result);
55e303ae 254
9bccf70c
A
255 /* Create first device */
256 gif_create_dev();
257}
258
2d21ac55
A
259static errno_t
260gif_set_bpf_tap(
261 ifnet_t ifp,
262 bpf_tap_mode mode,
263 bpf_packet_func callback)
264{
265 struct gif_softc *sc = ifnet_softc(ifp);
266
267 sc->tap_mode = mode;
268 sc->tap_callback = callback;
269
270 return 0;
271}
272
9bccf70c
A
273/* Creates another gif device if there are none free */
274static void
275gif_create_dev(void)
276{
2d21ac55
A
277 struct gif_softc *sc;
278 struct ifnet_init_params gif_init;
279 errno_t result = 0;
9bccf70c
A
280
281
282 /* Can't create more than GIF_MAXUNIT */
283 if (ngif >= GIF_MAXUNIT)
284 return;
285
286 /* Check for unused gif interface */
287 TAILQ_FOREACH(sc, &gifs, gif_link) {
288 /* If unused, return, no need to create a new interface */
2d21ac55 289 if ((ifnet_flags(sc->gif_if) & IFF_RUNNING) == 0)
9bccf70c
A
290 return;
291 }
292
293 sc = _MALLOC(sizeof(struct gif_softc), M_DEVBUF, M_WAITOK);
294 if (sc == NULL) {
295 log(LOG_ERR, "gifattach: failed to allocate gif%d\n", ngif);
296 return;
297 }
2d21ac55
A
298
299 bzero(&gif_init, sizeof(gif_init));
300 gif_init.name = GIFNAME;
301 gif_init.unit = ngif;
302 gif_init.type = IFT_GIF;
303 gif_init.family = IFNET_FAMILY_GIF;
304 gif_init.output = gif_output;
305 gif_init.demux = gif_demux;
306 gif_init.add_proto = gif_add_proto;
307 gif_init.del_proto = gif_del_proto;
308 gif_init.softc = sc;
309 gif_init.ioctl = gif_ioctl;
310 gif_init.set_bpf_tap = gif_set_bpf_tap;
9bccf70c
A
311
312 bzero(sc, sizeof(struct gif_softc));
2d21ac55
A
313 result = ifnet_allocate(&gif_init, &sc->gif_if);
314 if (result != 0) {
315 printf("gif_create_dev, ifnet_allocate failed - %d\n", result);
316 _FREE(sc, M_DEVBUF);
317 return;
318 }
9bccf70c 319 sc->encap_cookie4 = sc->encap_cookie6 = NULL;
2d21ac55 320#if INET
9bccf70c
A
321 sc->encap_cookie4 = encap_attach_func(AF_INET, -1,
322 gif_encapcheck, &in_gif_protosw, sc);
323 if (sc->encap_cookie4 == NULL) {
2d21ac55
A
324 printf("%s: unable to attach encap4\n", if_name(sc->gif_if));
325 ifnet_release(sc->gif_if);
9bccf70c
A
326 FREE(sc, M_DEVBUF);
327 return;
328 }
329#endif
2d21ac55 330#if INET6
9bccf70c
A
331 sc->encap_cookie6 = encap_attach_func(AF_INET6, -1,
332 gif_encapcheck, (struct protosw*)&in6_gif_protosw, sc);
333 if (sc->encap_cookie6 == NULL) {
334 if (sc->encap_cookie4) {
335 encap_detach(sc->encap_cookie4);
336 sc->encap_cookie4 = NULL;
337 }
2d21ac55
A
338 printf("%s: unable to attach encap6\n", if_name(sc->gif_if));
339 ifnet_release(sc->gif_if);
9bccf70c
A
340 FREE(sc, M_DEVBUF);
341 return;
342 }
343#endif
91447636 344 sc->gif_called = 0;
2d21ac55
A
345 ifnet_set_mtu(sc->gif_if, GIF_MTU);
346 ifnet_set_flags(sc->gif_if, IFF_POINTOPOINT | IFF_MULTICAST, 0xffff);
1c79356b 347#if 0
9bccf70c
A
348 /* turn off ingress filter */
349 sc->gif_if.if_flags |= IFF_LINK2;
1c79356b 350#endif
2d21ac55
A
351 result = ifnet_attach(sc->gif_if, NULL);
352 if (result != 0) {
353 printf("gif_create_dev - ifnet_attach failed - %d\n", result);
354 ifnet_release(sc->gif_if);
355 FREE(sc, M_DEVBUF);
356 return;
357 }
358#if CONFIG_MACF_NET
359 mac_ifnet_label_init(&sc->gif_if);
360#endif
361 bpfattach(sc->gif_if, DLT_NULL, sizeof(u_int));
9bccf70c
A
362 TAILQ_INSERT_TAIL(&gifs, sc, gif_link);
363 ngif++;
364}
365
366static int
2d21ac55
A
367gif_encapcheck(
368 const struct mbuf *m,
369 int off,
370 int proto,
371 void *arg)
9bccf70c
A
372{
373 struct ip ip;
374 struct gif_softc *sc;
375
376 sc = (struct gif_softc *)arg;
377 if (sc == NULL)
378 return 0;
379
2d21ac55 380 if ((ifnet_flags(sc->gif_if) & IFF_UP) == 0)
9bccf70c
A
381 return 0;
382
383 /* no physical address */
384 if (!sc->gif_psrc || !sc->gif_pdst)
385 return 0;
386
387 switch (proto) {
388#if INET
389 case IPPROTO_IPV4:
390 break;
1c79356b 391#endif
9bccf70c
A
392#if INET6
393 case IPPROTO_IPV6:
394 break;
1c79356b 395#endif
9bccf70c
A
396 default:
397 return 0;
1c79356b 398 }
1c79356b 399
2d21ac55 400 mbuf_copydata(m, 0, sizeof(ip), &ip);
9bccf70c
A
401
402 switch (ip.ip_v) {
403#if INET
404 case 4:
405 if (sc->gif_psrc->sa_family != AF_INET ||
406 sc->gif_pdst->sa_family != AF_INET)
407 return 0;
408 return gif_encapcheck4(m, off, proto, arg);
409#endif
410#if INET6
411 case 6:
412 if (sc->gif_psrc->sa_family != AF_INET6 ||
413 sc->gif_pdst->sa_family != AF_INET6)
414 return 0;
415 return gif_encapcheck6(m, off, proto, arg);
1c79356b 416#endif
9bccf70c
A
417 default:
418 return 0;
419 }
420}
1c79356b 421
2d21ac55
A
422static errno_t
423gif_output(
424 ifnet_t ifp,
425 mbuf_t m)
1c79356b 426{
2d21ac55 427 struct gif_softc *sc = ifnet_softc(ifp);
1c79356b 428 int error = 0;
91447636 429
1c79356b 430 /*
2d21ac55
A
431 max_gif_nesting check used to live here. It doesn't anymore
432 because there is no guaruntee that we won't be called
433 concurrently from more than one thread.
1c79356b 434 */
2d21ac55 435
1c79356b 436 m->m_flags &= ~(M_BCAST|M_MCAST);
2d21ac55 437 if (!(ifnet_flags(ifp) & IFF_UP) ||
1c79356b 438 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
2d21ac55 439 ifnet_touch_lastchange(ifp);
55e303ae 440 m_freem(m); /* free it here not in dlil_output */
1c79356b 441 error = ENETDOWN;
1c79356b
A
442 goto end;
443 }
444
2d21ac55
A
445 bpf_tap_out(ifp, 0, m, &sc->gif_proto, sizeof(sc->gif_proto));
446
9bccf70c
A
447 /* inner AF-specific encapsulation */
448
449 /* XXX should we check if our outer source is legal? */
450
451 /* dispatch to output logic based on outer AF */
1c79356b
A
452 switch (sc->gif_psrc->sa_family) {
453#if INET
454 case AF_INET:
2d21ac55 455 error = in_gif_output(ifp, sc->gif_proto, m, NULL);
1c79356b
A
456 break;
457#endif
458#if INET6
459 case AF_INET6:
2d21ac55 460 error = in6_gif_output(ifp, sc->gif_proto, m, NULL);
1c79356b
A
461 break;
462#endif
463 default:
1c79356b 464 error = ENETDOWN;
9bccf70c 465 goto end;
1c79356b 466 }
1c79356b
A
467
468 end:
55e303ae
A
469 if (error) {
470 /* the mbuf was freed either by in_gif_output or in here */
2d21ac55
A
471 ifnet_stat_increment_out(ifp, 0, 0, 1);
472 }
473 else {
474 ifnet_stat_increment_out(ifp, 1, m->m_pkthdr.len, 0);
55e303ae 475 }
9bccf70c
A
476 if (error == 0)
477 error = EJUSTRETURN; /* if no error, packet got sent already */
478 return error;
1c79356b
A
479}
480
2d21ac55
A
481/*
482 * gif_input is the input handler for IP and IPv6 attached to gif
483 */
484static errno_t
91447636 485gif_input(
2d21ac55
A
486 ifnet_t ifp,
487 protocol_family_t protocol_family,
488 mbuf_t m,
489 __unused char *frame_header)
1c79356b 490{
2d21ac55
A
491 errno_t error;
492 struct gif_softc *sc = ifnet_softc(ifp);
1c79356b 493
2d21ac55 494 bpf_tap_in(ifp, 0, m, &sc->gif_proto, sizeof(sc->gif_proto));
1c79356b
A
495
496 /*
497 * Put the packet to the network layer input queue according to the
498 * specified address family.
499 * Note: older versions of gif_input directly called network layer
500 * input functions, e.g. ip6_input, here. We changed the policy to
501 * prevent too many recursive calls of such input functions, which
502 * might cause kernel panic. But the change may introduce another
503 * problem; if the input queue is full, packets are discarded.
504 * We believed it rarely occurs and changed the policy. If we find
505 * it occurs more times than we thought, we may change the policy
506 * again.
507 */
2d21ac55
A
508 error = proto_input(protocol_family, m);
509 ifnet_stat_increment_in(ifp, 1, m->m_pkthdr.len, 0);
1c79356b 510
9bccf70c 511 return (0);
1c79356b
A
512}
513
514/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
2d21ac55
A
515static errno_t
516gif_ioctl(
517 ifnet_t ifp,
518 u_int32_t cmd,
519 void *data)
1c79356b 520{
2d21ac55 521 struct gif_softc *sc = ifnet_softc(ifp);
1c79356b
A
522 struct ifreq *ifr = (struct ifreq*)data;
523 int error = 0, size;
2d21ac55 524 struct sockaddr *dst = NULL, *src = NULL;
9bccf70c 525 struct sockaddr *sa;
9bccf70c 526 struct ifnet *ifp2;
1c79356b
A
527 struct gif_softc *sc2;
528
529 switch (cmd) {
530 case SIOCSIFADDR:
531 break;
532
533 case SIOCSIFDSTADDR:
534 break;
535
536 case SIOCADDMULTI:
537 case SIOCDELMULTI:
1c79356b
A
538 break;
539
540#ifdef SIOCSIFMTU /* xxx */
1c79356b
A
541 case SIOCGIFMTU:
542 break;
9bccf70c 543
1c79356b
A
544 case SIOCSIFMTU:
545 {
1c79356b
A
546 u_long mtu;
547 mtu = ifr->ifr_mtu;
1c79356b
A
548 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
549 return (EINVAL);
550 }
2d21ac55 551 ifnet_set_mtu(ifp, mtu);
1c79356b
A
552 }
553 break;
1c79356b
A
554#endif /* SIOCSIFMTU */
555
556 case SIOCSIFPHYADDR:
557#if INET6
558 case SIOCSIFPHYADDR_IN6:
559#endif /* INET6 */
9bccf70c
A
560 case SIOCSLIFPHYADDR:
561 switch (cmd) {
562#if INET
563 case SIOCSIFPHYADDR:
564 src = (struct sockaddr *)
565 &(((struct in_aliasreq *)data)->ifra_addr);
566 dst = (struct sockaddr *)
567 &(((struct in_aliasreq *)data)->ifra_dstaddr);
568 break;
569#endif
570#if INET6
571 case SIOCSIFPHYADDR_IN6:
572 src = (struct sockaddr *)
573 &(((struct in6_aliasreq *)data)->ifra_addr);
574 dst = (struct sockaddr *)
575 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
576 break;
577#endif
578 case SIOCSLIFPHYADDR:
579 src = (struct sockaddr *)
580 &(((struct if_laddrreq *)data)->addr);
581 dst = (struct sockaddr *)
582 &(((struct if_laddrreq *)data)->dstaddr);
583 }
584
585 /* sa_family must be equal */
586 if (src->sa_family != dst->sa_family)
587 return EINVAL;
588
589 /* validate sa_len */
590 switch (src->sa_family) {
591#if INET
592 case AF_INET:
593 if (src->sa_len != sizeof(struct sockaddr_in))
594 return EINVAL;
595 break;
596#endif
597#if INET6
598 case AF_INET6:
599 if (src->sa_len != sizeof(struct sockaddr_in6))
600 return EINVAL;
601 break;
602#endif
603 default:
604 return EAFNOSUPPORT;
605 }
606 switch (dst->sa_family) {
607#if INET
608 case AF_INET:
609 if (dst->sa_len != sizeof(struct sockaddr_in))
610 return EINVAL;
611 break;
612#endif
613#if INET6
614 case AF_INET6:
615 if (dst->sa_len != sizeof(struct sockaddr_in6))
616 return EINVAL;
617 break;
618#endif
619 default:
620 return EAFNOSUPPORT;
621 }
622
623 /* check sa_family looks sane for the cmd */
624 switch (cmd) {
625 case SIOCSIFPHYADDR:
626 if (src->sa_family == AF_INET)
627 break;
628 return EAFNOSUPPORT;
629#if INET6
630 case SIOCSIFPHYADDR_IN6:
631 if (src->sa_family == AF_INET6)
632 break;
633 return EAFNOSUPPORT;
634#endif /* INET6 */
635 case SIOCSLIFPHYADDR:
636 /* checks done in the above */
637 break;
638 }
639
91447636
A
640 ifnet_head_lock_shared();
641 TAILQ_FOREACH(ifp2, &ifnet_head, if_link) {
2d21ac55 642 if (strcmp(ifnet_name(ifp2), GIFNAME) != 0)
9bccf70c 643 continue;
2d21ac55 644 sc2 = ifnet_softc(ifp2);
1c79356b
A
645 if (sc2 == sc)
646 continue;
647 if (!sc2->gif_pdst || !sc2->gif_psrc)
648 continue;
9bccf70c
A
649 if (sc2->gif_pdst->sa_family != dst->sa_family ||
650 sc2->gif_pdst->sa_len != dst->sa_len ||
651 sc2->gif_psrc->sa_family != src->sa_family ||
652 sc2->gif_psrc->sa_len != src->sa_len)
653 continue;
654#ifndef XBONEHACK
655 /* can't configure same pair of address onto two gifs */
656 if (bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
1c79356b
A
657 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
658 error = EADDRNOTAVAIL;
91447636 659 ifnet_head_done();
1c79356b
A
660 goto bad;
661 }
9bccf70c 662#endif
1c79356b 663
9bccf70c
A
664 /* can't configure multiple multi-dest interfaces */
665#define multidest(x) \
666 (((struct sockaddr_in *)(x))->sin_addr.s_addr == INADDR_ANY)
1c79356b 667#if INET6
9bccf70c
A
668#define multidest6(x) \
669 (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(x))->sin6_addr))
670#endif
671 if (dst->sa_family == AF_INET &&
672 multidest(dst) && multidest(sc2->gif_pdst)) {
673 error = EADDRNOTAVAIL;
91447636 674 ifnet_head_done();
9bccf70c
A
675 goto bad;
676 }
677#if INET6
678 if (dst->sa_family == AF_INET6 &&
679 multidest6(dst) && multidest6(sc2->gif_pdst)) {
680 error = EADDRNOTAVAIL;
91447636 681 ifnet_head_done();
9bccf70c
A
682 goto bad;
683 }
684#endif
1c79356b 685 }
91447636 686 ifnet_head_done();
9bccf70c
A
687
688 if (sc->gif_psrc)
689 FREE((caddr_t)sc->gif_psrc, M_IFADDR);
690 sa = (struct sockaddr *)_MALLOC(src->sa_len, M_IFADDR, M_WAITOK);
691 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
692 sc->gif_psrc = sa;
693
694 if (sc->gif_pdst)
695 FREE((caddr_t)sc->gif_pdst, M_IFADDR);
696 sa = (struct sockaddr *)_MALLOC(dst->sa_len, M_IFADDR, M_WAITOK);
697 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
698 sc->gif_pdst = sa;
699
2d21ac55
A
700 ifnet_set_flags(ifp, IFF_RUNNING | IFF_UP, IFF_RUNNING | IFF_UP);
701
9bccf70c
A
702#ifdef __APPLE__
703 /* Make sure at least one unused device is still available */
704 gif_create_dev();
705#endif
9bccf70c
A
706
707 error = 0;
1c79356b 708 break;
9bccf70c
A
709
710#ifdef SIOCDIFPHYADDR
711 case SIOCDIFPHYADDR:
712 if (sc->gif_psrc) {
713 FREE((caddr_t)sc->gif_psrc, M_IFADDR);
714 sc->gif_psrc = NULL;
715 }
716 if (sc->gif_pdst) {
717 FREE((caddr_t)sc->gif_pdst, M_IFADDR);
718 sc->gif_pdst = NULL;
719 }
720 /* change the IFF_{UP, RUNNING} flag as well? */
721 break;
722#endif
1c79356b
A
723
724 case SIOCGIFPSRCADDR:
725#if INET6
726 case SIOCGIFPSRCADDR_IN6:
727#endif /* INET6 */
728 if (sc->gif_psrc == NULL) {
729 error = EADDRNOTAVAIL;
730 goto bad;
731 }
732 src = sc->gif_psrc;
9bccf70c 733 switch (cmd) {
1c79356b 734#if INET
9bccf70c 735 case SIOCGIFPSRCADDR:
1c79356b 736 dst = &ifr->ifr_addr;
9bccf70c 737 size = sizeof(ifr->ifr_addr);
1c79356b
A
738 break;
739#endif /* INET */
740#if INET6
9bccf70c 741 case SIOCGIFPSRCADDR_IN6:
1c79356b
A
742 dst = (struct sockaddr *)
743 &(((struct in6_ifreq *)data)->ifr_addr);
9bccf70c 744 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
1c79356b
A
745 break;
746#endif /* INET6 */
747 default:
748 error = EADDRNOTAVAIL;
749 goto bad;
750 }
9bccf70c
A
751 if (src->sa_len > size)
752 return EINVAL;
753 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
1c79356b
A
754 break;
755
756 case SIOCGIFPDSTADDR:
757#if INET6
758 case SIOCGIFPDSTADDR_IN6:
759#endif /* INET6 */
760 if (sc->gif_pdst == NULL) {
761 error = EADDRNOTAVAIL;
762 goto bad;
763 }
764 src = sc->gif_pdst;
9bccf70c 765 switch (cmd) {
1c79356b 766#if INET
9bccf70c 767 case SIOCGIFPDSTADDR:
1c79356b 768 dst = &ifr->ifr_addr;
9bccf70c 769 size = sizeof(ifr->ifr_addr);
1c79356b
A
770 break;
771#endif /* INET */
772#if INET6
9bccf70c 773 case SIOCGIFPDSTADDR_IN6:
1c79356b
A
774 dst = (struct sockaddr *)
775 &(((struct in6_ifreq *)data)->ifr_addr);
9bccf70c 776 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
1c79356b
A
777 break;
778#endif /* INET6 */
779 default:
780 error = EADDRNOTAVAIL;
781 goto bad;
782 }
9bccf70c
A
783 if (src->sa_len > size)
784 return EINVAL;
785 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
1c79356b
A
786 break;
787
9bccf70c
A
788 case SIOCGLIFPHYADDR:
789 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
790 error = EADDRNOTAVAIL;
1c79356b 791 goto bad;
1c79356b 792 }
9bccf70c
A
793
794 /* copy src */
795 src = sc->gif_psrc;
796 dst = (struct sockaddr *)
797 &(((struct if_laddrreq *)data)->addr);
798 size = sizeof(((struct if_laddrreq *)data)->addr);
799 if (src->sa_len > size)
800 return EINVAL;
801 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
802
803 /* copy dst */
804 src = sc->gif_pdst;
805 dst = (struct sockaddr *)
806 &(((struct if_laddrreq *)data)->dstaddr);
807 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
808 if (src->sa_len > size)
809 return EINVAL;
810 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
811 break;
812
813 case SIOCSIFFLAGS:
814 /* if_ioctl() takes care of it */
1c79356b
A
815 break;
816
817 default:
9bccf70c 818 error = EOPNOTSUPP;
1c79356b
A
819 break;
820 }
821 bad:
822 return error;
823}
9bccf70c 824
91447636
A
825#ifndef __APPLE__
826/* This function is not used in our stack */
9bccf70c
A
827void
828gif_delete_tunnel(sc)
829 struct gif_softc *sc;
830{
831 /* XXX: NetBSD protects this function with splsoftnet() */
832
833 if (sc->gif_psrc) {
834 FREE((caddr_t)sc->gif_psrc, M_IFADDR);
835 sc->gif_psrc = NULL;
836 }
837 if (sc->gif_pdst) {
838 FREE((caddr_t)sc->gif_pdst, M_IFADDR);
839 sc->gif_pdst = NULL;
840 }
841 /* change the IFF_UP flag as well? */
842}
91447636 843#endif