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