]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_gif.c
d21d77d40290562957fc85b3dc5754f39d23d391
[apple/xnu.git] / bsd / net / if_gif.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* $FreeBSD: src/sys/net/if_gif.c,v 1.4.2.6 2001/07/24 19:10:18 brooks Exp $ */
24 /* $KAME: if_gif.c,v 1.47 2001/05/01 05:28:42 itojun Exp $ */
25
26 /*
27 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
28 * All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. Neither the name of the project nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 */
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/malloc.h>
59 #include <sys/mbuf.h>
60 #include <sys/socket.h>
61 #include <sys/sockio.h>
62 #include <sys/errno.h>
63 #include <sys/time.h>
64 #include <sys/syslog.h>
65 #include <sys/protosw.h>
66 #include <kern/cpu_number.h>
67
68 #include <net/if.h>
69 #include <net/if_types.h>
70 #include <net/route.h>
71 #include <net/bpf.h>
72
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #if INET
77 #include <netinet/in_var.h>
78 #include <netinet/in_gif.h>
79 #include <netinet/ip_var.h>
80 #endif /* INET */
81
82 #if INET6
83 #include <netinet6/in6_var.h>
84 #include <netinet/ip6.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet6/in6_gif.h>
87 #include <netinet6/ip6protosw.h>
88 #endif /* INET6 */
89
90 #include <netinet/ip_encap.h>
91 #include <net/dlil.h>
92 #include <net/if_gif.h>
93
94 #include <net/net_osdep.h>
95
96 #define GIFNAME "gif"
97 #define GIFDEV "if_gif"
98 #define GIF_MAXUNIT 0x7fff /* ifp->if_unit is only 15 bits */
99
100 #ifndef __APPLE__
101 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
102 #endif
103
104 TAILQ_HEAD(gifhead, gif_softc) gifs = TAILQ_HEAD_INITIALIZER(gifs);
105
106 #ifdef __APPLE__
107 void gifattach(void);
108 int gif_pre_output(struct ifnet *ifp, u_long protocol_family, struct mbuf **m0,
109 const struct sockaddr *dst, caddr_t rt, char *frame, char *address);
110 static void gif_create_dev(void);
111 static int gif_encapcheck(const struct mbuf*, int, int, void*);
112
113
114 int ngif = 0; /* number of interfaces */
115 #endif
116
117 #if INET
118 struct protosw in_gif_protosw =
119 { SOCK_RAW, 0, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC|PR_ADDR,
120 in_gif_input, 0, 0, 0,
121 0,
122 0, 0, 0, 0,
123 0,
124 &rip_usrreqs,
125 0, rip_unlock, 0
126 };
127 #endif
128 #if INET6
129 struct ip6protosw in6_gif_protosw =
130 { SOCK_RAW, 0, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC|PR_ADDR,
131 in6_gif_input, 0, 0, 0,
132 0,
133 0, 0, 0, 0,
134 0,
135 &rip6_usrreqs,
136 0, rip_unlock, 0,
137
138 };
139 #endif
140
141 #ifndef MAX_GIF_NEST
142 /*
143 * This macro controls the upper limitation on nesting of gif tunnels.
144 * Since, setting a large value to this macro with a careless configuration
145 * may introduce system crash, we don't allow any nestings by default.
146 * If you need to configure nested gif tunnels, you can define this macro
147 * in your kernel configuration file. However, if you do so, please be
148 * careful to configure the tunnels so that it won't make a loop.
149 */
150 #define MAX_GIF_NEST 1
151 #endif
152 static int max_gif_nesting = MAX_GIF_NEST;
153
154
155
156 #ifdef __APPLE__
157 /*
158 * Theory of operation: initially, one gif interface is created.
159 * Any time a gif interface is configured, if there are no other
160 * unconfigured gif interfaces, a new gif interface is created.
161 * BSD uses the clone mechanism to dynamically create more
162 * gif interfaces.
163 *
164 * We have some extra glue to support DLIL.
165 */
166
167 /* GIF interface module support */
168 int gif_demux(
169 struct ifnet *ifp,
170 struct mbuf *m,
171 char *frame_header,
172 u_long *protocol_family)
173 {
174 struct gif_softc* gif = (struct gif_softc*)ifp->if_softc;
175
176 /* Only one protocol may be attached to a gif interface. */
177 *protocol_family = gif->gif_proto;
178
179 return 0;
180 }
181
182 static
183 int gif_add_proto(struct ifnet *ifp, u_long protocol_family, struct ddesc_head_str *desc_head)
184 {
185 /* Only one protocol may be attached at a time */
186 struct gif_softc* gif = (struct gif_softc*)ifp->if_softc;
187
188 if (gif->gif_proto != 0)
189 printf("gif_add_proto: request add_proto for gif%d\n", gif->gif_if.if_unit);
190
191 gif->gif_proto = protocol_family;
192
193 return 0;
194 }
195
196 static
197 int gif_del_proto(struct ifnet *ifp, u_long protocol_family)
198 {
199 if (((struct gif_softc*)ifp)->gif_proto == protocol_family)
200 ((struct gif_softc*)ifp)->gif_proto = 0;
201 else
202 return ENOENT;
203
204 return 0;
205 }
206
207 /* Glue code to attach inet to a gif interface through DLIL */
208 int
209 gif_attach_proto_family(
210 struct ifnet *ifp,
211 u_long protocol_family)
212 {
213 struct dlil_proto_reg_str reg;
214 int stat;
215
216 bzero(&reg, sizeof(reg));
217 TAILQ_INIT(&reg.demux_desc_head);
218 reg.interface_family = ifp->if_family;
219 reg.unit_number = ifp->if_unit;
220 reg.input = gif_input;
221 reg.pre_output = gif_pre_output;
222 reg.protocol_family = protocol_family;
223
224 stat = dlil_attach_protocol(&reg);
225 if (stat && stat != EEXIST) {
226 panic("gif_attach_proto_family can't attach interface fam=%d\n", protocol_family);
227 }
228
229 return stat;
230 }
231
232 #endif
233
234 /* Function to setup the first gif interface */
235 void
236 gifattach(void)
237 {
238 int error;
239
240 /* Init the list of interfaces */
241 TAILQ_INIT(&gifs);
242
243 /* Register protocol registration functions */
244 if ( error = dlil_reg_proto_module(AF_INET, APPLE_IF_FAM_GIF, gif_attach_proto_family, NULL) != 0)
245 printf("dlil_reg_proto_module failed for AF_INET error=%d\n", error);
246
247 if ( error = dlil_reg_proto_module(AF_INET6, APPLE_IF_FAM_GIF, gif_attach_proto_family, NULL) != 0)
248 printf("dlil_reg_proto_module failed for AF_INET6 error=%d\n", error);
249
250 /* Create first device */
251 gif_create_dev();
252 }
253
254 /* Creates another gif device if there are none free */
255 static void
256 gif_create_dev(void)
257 {
258 struct gif_softc *sc;
259
260
261 /* Can't create more than GIF_MAXUNIT */
262 if (ngif >= GIF_MAXUNIT)
263 return;
264
265 /* Check for unused gif interface */
266 TAILQ_FOREACH(sc, &gifs, gif_link) {
267 /* If unused, return, no need to create a new interface */
268 if ((sc->gif_if.if_flags & IFF_RUNNING) == 0)
269 return;
270 }
271
272 sc = _MALLOC(sizeof(struct gif_softc), M_DEVBUF, M_WAITOK);
273 if (sc == NULL) {
274 log(LOG_ERR, "gifattach: failed to allocate gif%d\n", ngif);
275 return;
276 }
277
278 bzero(sc, sizeof(struct gif_softc));
279 sc->gif_if.if_softc = sc;
280 sc->gif_if.if_name = GIFNAME;
281 sc->gif_if.if_unit = ngif;
282
283 sc->encap_cookie4 = sc->encap_cookie6 = NULL;
284 #ifdef INET
285 sc->encap_cookie4 = encap_attach_func(AF_INET, -1,
286 gif_encapcheck, &in_gif_protosw, sc);
287 if (sc->encap_cookie4 == NULL) {
288 printf("%s: unable to attach encap4\n", if_name(&sc->gif_if));
289 FREE(sc, M_DEVBUF);
290 return;
291 }
292 #endif
293 #ifdef INET6
294 sc->encap_cookie6 = encap_attach_func(AF_INET6, -1,
295 gif_encapcheck, (struct protosw*)&in6_gif_protosw, sc);
296 if (sc->encap_cookie6 == NULL) {
297 if (sc->encap_cookie4) {
298 encap_detach(sc->encap_cookie4);
299 sc->encap_cookie4 = NULL;
300 }
301 printf("%s: unable to attach encap6\n", if_name(&sc->gif_if));
302 FREE(sc, M_DEVBUF);
303 return;
304 }
305 #endif
306
307 sc->gif_called = 0;
308 sc->gif_if.if_family= APPLE_IF_FAM_GIF;
309 sc->gif_if.if_mtu = GIF_MTU;
310 sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
311 #if 0
312 /* turn off ingress filter */
313 sc->gif_if.if_flags |= IFF_LINK2;
314 #endif
315 sc->gif_if.if_demux = gif_demux;
316 sc->gif_if.if_ioctl = gif_ioctl;
317 sc->gif_if.if_output = NULL; /* pre_output returns error or EJUSTRETURN */
318 sc->gif_if.if_type = IFT_GIF;
319 sc->gif_if.if_add_proto = gif_add_proto;
320 sc->gif_if.if_del_proto = gif_del_proto;
321 dlil_if_attach(&sc->gif_if);
322 bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
323 TAILQ_INSERT_TAIL(&gifs, sc, gif_link);
324 ngif++;
325 }
326
327 static int
328 gif_encapcheck(m, off, proto, arg)
329 const struct mbuf *m;
330 int off;
331 int proto;
332 void *arg;
333 {
334 struct ip ip;
335 struct gif_softc *sc;
336
337 sc = (struct gif_softc *)arg;
338 if (sc == NULL)
339 return 0;
340
341 if ((sc->gif_if.if_flags & IFF_UP) == 0)
342 return 0;
343
344 /* no physical address */
345 if (!sc->gif_psrc || !sc->gif_pdst)
346 return 0;
347
348 switch (proto) {
349 #if INET
350 case IPPROTO_IPV4:
351 break;
352 #endif
353 #if INET6
354 case IPPROTO_IPV6:
355 break;
356 #endif
357 default:
358 return 0;
359 }
360
361 /* LINTED const cast */
362 m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
363
364 switch (ip.ip_v) {
365 #if INET
366 case 4:
367 if (sc->gif_psrc->sa_family != AF_INET ||
368 sc->gif_pdst->sa_family != AF_INET)
369 return 0;
370 return gif_encapcheck4(m, off, proto, arg);
371 #endif
372 #if INET6
373 case 6:
374 if (sc->gif_psrc->sa_family != AF_INET6 ||
375 sc->gif_pdst->sa_family != AF_INET6)
376 return 0;
377 return gif_encapcheck6(m, off, proto, arg);
378 #endif
379 default:
380 return 0;
381 }
382 }
383
384 int
385 gif_pre_output(
386 struct ifnet *ifp,
387 u_long protocol_family,
388 struct mbuf **m0,
389 const struct sockaddr *dst,
390 caddr_t rt,
391 char *frame,
392 char *address)
393 {
394 struct gif_softc *sc = (struct gif_softc*)ifp;
395 register struct mbuf * m = *m0;
396 int error = 0;
397
398 /*
399 * gif may cause infinite recursion calls when misconfigured.
400 * We'll prevent this by introducing upper limit.
401 * XXX: this mechanism may introduce another problem about
402 * mutual exclusion of the variable CALLED, especially if we
403 * use kernel thread.
404 */
405 if (++sc->gif_called > max_gif_nesting) {
406 log(LOG_NOTICE,
407 "gif_output: recursively called too many times(%d)\n",
408 sc->gif_called);
409 m_freem(m); /* free it here not in dlil_output*/
410 error = EIO; /* is there better errno? */
411 goto end;
412 }
413
414 ifnet_touch_lastchange(ifp);
415 m->m_flags &= ~(M_BCAST|M_MCAST);
416 if (!(ifp->if_flags & IFF_UP) ||
417 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
418 m_freem(m); /* free it here not in dlil_output */
419 error = ENETDOWN;
420 goto end;
421 }
422
423 if (ifp->if_bpf) {
424 /*
425 * We need to prepend the address family as
426 * a four byte field. Cons up a dummy header
427 * to pacify bpf. This is safe because bpf
428 * will only read from the mbuf (i.e., it won't
429 * try to free it or keep a pointer a to it).
430 */
431 struct mbuf m0;
432 u_int32_t protocol_family = dst->sa_family;
433
434 m0.m_next = m;
435 m0.m_len = 4;
436 m0.m_data = (char *)&protocol_family;
437
438 bpf_mtap(ifp, &m0);
439 }
440 ifp->if_opackets++;
441 ifp->if_obytes += m->m_pkthdr.len;
442
443 /* inner AF-specific encapsulation */
444
445 /* XXX should we check if our outer source is legal? */
446
447 /* dispatch to output logic based on outer AF */
448 switch (sc->gif_psrc->sa_family) {
449 #if INET
450 case AF_INET:
451 error = in_gif_output(ifp, dst->sa_family, m, (struct rtentry*)rt);
452 break;
453 #endif
454 #if INET6
455 case AF_INET6:
456 error = in6_gif_output(ifp, dst->sa_family, m, (struct rtentry*)rt);
457 break;
458 #endif
459 default:
460 error = ENETDOWN;
461 goto end;
462 }
463
464 end:
465 sc->gif_called = 0; /* reset recursion counter */
466 if (error) {
467 /* the mbuf was freed either by in_gif_output or in here */
468 *m0 = NULL; /* avoid getting dlil_output freeing it */
469 ifp->if_oerrors++;
470 }
471 if (error == 0)
472 error = EJUSTRETURN; /* if no error, packet got sent already */
473 return error;
474 }
475
476 int
477 gif_input(
478 struct mbuf *m,
479 char* frame_header,
480 struct ifnet* gifp,
481 u_long protocol_family,
482 int sync_ok)
483 {
484
485 if (gifp == NULL) {
486 /* just in case */
487 m_freem(m);
488 return;
489 }
490
491 if (m->m_pkthdr.rcvif)
492 m->m_pkthdr.rcvif = gifp;
493
494 if (gifp->if_bpf) {
495 /*
496 * We need to prepend the address family as
497 * a four byte field. Cons up a dummy header
498 * to pacify bpf. This is safe because bpf
499 * will only read from the mbuf (i.e., it won't
500 * try to free it or keep a pointer a to it).
501 */
502 struct mbuf m0;
503 u_int32_t protocol_family1 = protocol_family;
504
505 m0.m_next = m;
506 m0.m_len = 4;
507 m0.m_data = (char *)&protocol_family1;
508
509 bpf_mtap(gifp, &m0);
510 }
511
512 /*
513 * Put the packet to the network layer input queue according to the
514 * specified address family.
515 * Note: older versions of gif_input directly called network layer
516 * input functions, e.g. ip6_input, here. We changed the policy to
517 * prevent too many recursive calls of such input functions, which
518 * might cause kernel panic. But the change may introduce another
519 * problem; if the input queue is full, packets are discarded.
520 * We believed it rarely occurs and changed the policy. If we find
521 * it occurs more times than we thought, we may change the policy
522 * again.
523 */
524 proto_input(protocol_family, m);
525 gifp->if_ipackets++;
526 gifp->if_ibytes += m->m_pkthdr.len;
527
528 return (0);
529 }
530
531 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
532 int
533 gif_ioctl(ifp, cmd, data)
534 struct ifnet *ifp;
535 u_long cmd;
536 void* data;
537 {
538 struct gif_softc *sc = (struct gif_softc*)ifp;
539 struct ifreq *ifr = (struct ifreq*)data;
540 int error = 0, size;
541 struct sockaddr *dst, *src;
542 struct sockaddr *sa;
543 int s;
544 struct ifnet *ifp2;
545 struct gif_softc *sc2;
546
547 switch (cmd) {
548 case SIOCSIFADDR:
549 break;
550
551 case SIOCSIFDSTADDR:
552 break;
553
554 case SIOCADDMULTI:
555 case SIOCDELMULTI:
556 break;
557
558 #ifdef SIOCSIFMTU /* xxx */
559 case SIOCGIFMTU:
560 break;
561
562 case SIOCSIFMTU:
563 {
564 u_long mtu;
565 mtu = ifr->ifr_mtu;
566 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
567 return (EINVAL);
568 }
569 ifp->if_mtu = mtu;
570 }
571 break;
572 #endif /* SIOCSIFMTU */
573
574 case SIOCSIFPHYADDR:
575 #if INET6
576 case SIOCSIFPHYADDR_IN6:
577 #endif /* INET6 */
578 case SIOCSLIFPHYADDR:
579 switch (cmd) {
580 #if INET
581 case SIOCSIFPHYADDR:
582 src = (struct sockaddr *)
583 &(((struct in_aliasreq *)data)->ifra_addr);
584 dst = (struct sockaddr *)
585 &(((struct in_aliasreq *)data)->ifra_dstaddr);
586 break;
587 #endif
588 #if INET6
589 case SIOCSIFPHYADDR_IN6:
590 src = (struct sockaddr *)
591 &(((struct in6_aliasreq *)data)->ifra_addr);
592 dst = (struct sockaddr *)
593 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
594 break;
595 #endif
596 case SIOCSLIFPHYADDR:
597 src = (struct sockaddr *)
598 &(((struct if_laddrreq *)data)->addr);
599 dst = (struct sockaddr *)
600 &(((struct if_laddrreq *)data)->dstaddr);
601 }
602
603 /* sa_family must be equal */
604 if (src->sa_family != dst->sa_family)
605 return EINVAL;
606
607 /* validate sa_len */
608 switch (src->sa_family) {
609 #if INET
610 case AF_INET:
611 if (src->sa_len != sizeof(struct sockaddr_in))
612 return EINVAL;
613 break;
614 #endif
615 #if INET6
616 case AF_INET6:
617 if (src->sa_len != sizeof(struct sockaddr_in6))
618 return EINVAL;
619 break;
620 #endif
621 default:
622 return EAFNOSUPPORT;
623 }
624 switch (dst->sa_family) {
625 #if INET
626 case AF_INET:
627 if (dst->sa_len != sizeof(struct sockaddr_in))
628 return EINVAL;
629 break;
630 #endif
631 #if INET6
632 case AF_INET6:
633 if (dst->sa_len != sizeof(struct sockaddr_in6))
634 return EINVAL;
635 break;
636 #endif
637 default:
638 return EAFNOSUPPORT;
639 }
640
641 /* check sa_family looks sane for the cmd */
642 switch (cmd) {
643 case SIOCSIFPHYADDR:
644 if (src->sa_family == AF_INET)
645 break;
646 return EAFNOSUPPORT;
647 #if INET6
648 case SIOCSIFPHYADDR_IN6:
649 if (src->sa_family == AF_INET6)
650 break;
651 return EAFNOSUPPORT;
652 #endif /* INET6 */
653 case SIOCSLIFPHYADDR:
654 /* checks done in the above */
655 break;
656 }
657
658 ifnet_head_lock_shared();
659 TAILQ_FOREACH(ifp2, &ifnet_head, if_link) {
660 if (strcmp(ifp2->if_name, GIFNAME) != 0)
661 continue;
662 sc2 = ifp2->if_softc;
663 if (sc2 == sc)
664 continue;
665 if (!sc2->gif_pdst || !sc2->gif_psrc)
666 continue;
667 if (sc2->gif_pdst->sa_family != dst->sa_family ||
668 sc2->gif_pdst->sa_len != dst->sa_len ||
669 sc2->gif_psrc->sa_family != src->sa_family ||
670 sc2->gif_psrc->sa_len != src->sa_len)
671 continue;
672 #ifndef XBONEHACK
673 /* can't configure same pair of address onto two gifs */
674 if (bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
675 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
676 error = EADDRNOTAVAIL;
677 ifnet_head_done();
678 goto bad;
679 }
680 #endif
681
682 /* can't configure multiple multi-dest interfaces */
683 #define multidest(x) \
684 (((struct sockaddr_in *)(x))->sin_addr.s_addr == INADDR_ANY)
685 #if INET6
686 #define multidest6(x) \
687 (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(x))->sin6_addr))
688 #endif
689 if (dst->sa_family == AF_INET &&
690 multidest(dst) && multidest(sc2->gif_pdst)) {
691 error = EADDRNOTAVAIL;
692 ifnet_head_done();
693 goto bad;
694 }
695 #if INET6
696 if (dst->sa_family == AF_INET6 &&
697 multidest6(dst) && multidest6(sc2->gif_pdst)) {
698 error = EADDRNOTAVAIL;
699 ifnet_head_done();
700 goto bad;
701 }
702 #endif
703 }
704 ifnet_head_done();
705
706 if (sc->gif_psrc)
707 FREE((caddr_t)sc->gif_psrc, M_IFADDR);
708 sa = (struct sockaddr *)_MALLOC(src->sa_len, M_IFADDR, M_WAITOK);
709 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
710 sc->gif_psrc = sa;
711
712 if (sc->gif_pdst)
713 FREE((caddr_t)sc->gif_pdst, M_IFADDR);
714 sa = (struct sockaddr *)_MALLOC(dst->sa_len, M_IFADDR, M_WAITOK);
715 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
716 sc->gif_pdst = sa;
717
718 ifp->if_flags |= IFF_RUNNING;
719
720 s = splimp();
721 if_up(ifp); /* mark interface UP and send up RTM_IFINFO */
722 #ifdef __APPLE__
723 /* Make sure at least one unused device is still available */
724 gif_create_dev();
725 #endif
726 splx(s);
727
728 error = 0;
729 break;
730
731 #ifdef SIOCDIFPHYADDR
732 case SIOCDIFPHYADDR:
733 if (sc->gif_psrc) {
734 FREE((caddr_t)sc->gif_psrc, M_IFADDR);
735 sc->gif_psrc = NULL;
736 }
737 if (sc->gif_pdst) {
738 FREE((caddr_t)sc->gif_pdst, M_IFADDR);
739 sc->gif_pdst = NULL;
740 }
741 /* change the IFF_{UP, RUNNING} flag as well? */
742 break;
743 #endif
744
745 case SIOCGIFPSRCADDR:
746 #if INET6
747 case SIOCGIFPSRCADDR_IN6:
748 #endif /* INET6 */
749 if (sc->gif_psrc == NULL) {
750 error = EADDRNOTAVAIL;
751 goto bad;
752 }
753 src = sc->gif_psrc;
754 switch (cmd) {
755 #if INET
756 case SIOCGIFPSRCADDR:
757 dst = &ifr->ifr_addr;
758 size = sizeof(ifr->ifr_addr);
759 break;
760 #endif /* INET */
761 #if INET6
762 case SIOCGIFPSRCADDR_IN6:
763 dst = (struct sockaddr *)
764 &(((struct in6_ifreq *)data)->ifr_addr);
765 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
766 break;
767 #endif /* INET6 */
768 default:
769 error = EADDRNOTAVAIL;
770 goto bad;
771 }
772 if (src->sa_len > size)
773 return EINVAL;
774 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
775 break;
776
777 case SIOCGIFPDSTADDR:
778 #if INET6
779 case SIOCGIFPDSTADDR_IN6:
780 #endif /* INET6 */
781 if (sc->gif_pdst == NULL) {
782 error = EADDRNOTAVAIL;
783 goto bad;
784 }
785 src = sc->gif_pdst;
786 switch (cmd) {
787 #if INET
788 case SIOCGIFPDSTADDR:
789 dst = &ifr->ifr_addr;
790 size = sizeof(ifr->ifr_addr);
791 break;
792 #endif /* INET */
793 #if INET6
794 case SIOCGIFPDSTADDR_IN6:
795 dst = (struct sockaddr *)
796 &(((struct in6_ifreq *)data)->ifr_addr);
797 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
798 break;
799 #endif /* INET6 */
800 default:
801 error = EADDRNOTAVAIL;
802 goto bad;
803 }
804 if (src->sa_len > size)
805 return EINVAL;
806 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
807 break;
808
809 case SIOCGLIFPHYADDR:
810 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
811 error = EADDRNOTAVAIL;
812 goto bad;
813 }
814
815 /* copy src */
816 src = sc->gif_psrc;
817 dst = (struct sockaddr *)
818 &(((struct if_laddrreq *)data)->addr);
819 size = sizeof(((struct if_laddrreq *)data)->addr);
820 if (src->sa_len > size)
821 return EINVAL;
822 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
823
824 /* copy dst */
825 src = sc->gif_pdst;
826 dst = (struct sockaddr *)
827 &(((struct if_laddrreq *)data)->dstaddr);
828 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
829 if (src->sa_len > size)
830 return EINVAL;
831 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
832 break;
833
834 case SIOCSIFFLAGS:
835 /* if_ioctl() takes care of it */
836 break;
837
838 default:
839 error = EOPNOTSUPP;
840 break;
841 }
842 bad:
843 return error;
844 }
845
846 #ifndef __APPLE__
847 /* This function is not used in our stack */
848 void
849 gif_delete_tunnel(sc)
850 struct gif_softc *sc;
851 {
852 /* XXX: NetBSD protects this function with splsoftnet() */
853
854 if (sc->gif_psrc) {
855 FREE((caddr_t)sc->gif_psrc, M_IFADDR);
856 sc->gif_psrc = NULL;
857 }
858 if (sc->gif_pdst) {
859 FREE((caddr_t)sc->gif_pdst, M_IFADDR);
860 sc->gif_pdst = NULL;
861 }
862 /* change the IFF_UP flag as well? */
863 }
864 #endif