FreeBSD/Linux Kernel Cross Reference
sys/net/if_gif.c
1 /* $FreeBSD: releng/6.3/sys/net/if_gif.c 173886 2007-11-24 19:45:58Z cvs2svn $ */
2 /* $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ */
3
4 /*-
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_mac.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/mac.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/module.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/errno.h>
47 #include <sys/time.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
50 #include <sys/protosw.h>
51 #include <sys/conf.h>
52 #include <machine/cpu.h>
53
54 #include <net/if.h>
55 #include <net/if_clone.h>
56 #include <net/if_types.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
59 #include <net/bpf.h>
60
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #ifdef INET
65 #include <netinet/in_var.h>
66 #include <netinet/in_gif.h>
67 #include <netinet/ip_var.h>
68 #endif /* INET */
69
70 #ifdef INET6
71 #ifndef INET
72 #include <netinet/in.h>
73 #endif
74 #include <netinet6/in6_var.h>
75 #include <netinet/ip6.h>
76 #include <netinet6/ip6_var.h>
77 #include <netinet6/scope6_var.h>
78 #include <netinet6/in6_gif.h>
79 #include <netinet6/ip6protosw.h>
80 #endif /* INET6 */
81
82 #include <netinet/ip_encap.h>
83 #include <net/ethernet.h>
84 #include <net/if_bridgevar.h>
85 #include <net/if_gif.h>
86
87 #include <net/net_osdep.h>
88
89 #define GIFNAME "gif"
90
91 /*
92 * gif_mtx protects the global gif_softc_list.
93 */
94 static struct mtx gif_mtx;
95 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
96 static LIST_HEAD(, gif_softc) gif_softc_list;
97
98 void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
99 void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
100 void (*ng_gif_attach_p)(struct ifnet *ifp);
101 void (*ng_gif_detach_p)(struct ifnet *ifp);
102
103 static void gif_start(struct ifnet *);
104 static int gif_clone_create(struct if_clone *, int);
105 static void gif_clone_destroy(struct ifnet *);
106
107 IFC_SIMPLE_DECLARE(gif, 0);
108
109 static int gifmodevent(module_t, int, void *);
110
111 SYSCTL_DECL(_net_link);
112 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
113 "Generic Tunnel Interface");
114 #ifndef MAX_GIF_NEST
115 /*
116 * This macro controls the default upper limitation on nesting of gif tunnels.
117 * Since, setting a large value to this macro with a careless configuration
118 * may introduce system crash, we don't allow any nestings by default.
119 * If you need to configure nested gif tunnels, you can define this macro
120 * in your kernel configuration file. However, if you do so, please be
121 * careful to configure the tunnels so that it won't make a loop.
122 */
123 #define MAX_GIF_NEST 1
124 #endif
125 static int max_gif_nesting = MAX_GIF_NEST;
126 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
127 &max_gif_nesting, 0, "Max nested tunnels");
128
129 /*
130 * By default, we disallow creation of multiple tunnels between the same
131 * pair of addresses. Some applications require this functionality so
132 * we allow control over this check here.
133 */
134 #ifdef XBONEHACK
135 static int parallel_tunnels = 1;
136 #else
137 static int parallel_tunnels = 0;
138 #endif
139 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
140 ¶llel_tunnels, 0, "Allow parallel tunnels?");
141
142 static int
143 gif_clone_create(ifc, unit)
144 struct if_clone *ifc;
145 int unit;
146 {
147 struct gif_softc *sc;
148
149 sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
150 GIF2IFP(sc) = if_alloc(IFT_GIF);
151 if (GIF2IFP(sc) == NULL) {
152 free(sc, M_GIF);
153 return (ENOSPC);
154 }
155
156 GIF_LOCK_INIT(sc);
157
158 GIF2IFP(sc)->if_softc = sc;
159 if_initname(GIF2IFP(sc), ifc->ifc_name, unit);
160
161 sc->encap_cookie4 = sc->encap_cookie6 = NULL;
162
163 GIF2IFP(sc)->if_addrlen = 0;
164 GIF2IFP(sc)->if_mtu = GIF_MTU;
165 GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
166 #if 0
167 /* turn off ingress filter */
168 GIF2IFP(sc)->if_flags |= IFF_LINK2;
169 #endif
170 GIF2IFP(sc)->if_ioctl = gif_ioctl;
171 GIF2IFP(sc)->if_start = gif_start;
172 GIF2IFP(sc)->if_output = gif_output;
173 GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
174 if_attach(GIF2IFP(sc));
175 bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
176 if (ng_gif_attach_p != NULL)
177 (*ng_gif_attach_p)(GIF2IFP(sc));
178
179 mtx_lock(&gif_mtx);
180 LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
181 mtx_unlock(&gif_mtx);
182
183 return (0);
184 }
185
186 static void
187 gif_destroy(struct gif_softc *sc)
188 {
189 struct ifnet *ifp = GIF2IFP(sc);
190 int err;
191
192 gif_delete_tunnel(ifp);
193 #ifdef INET6
194 if (sc->encap_cookie6 != NULL) {
195 err = encap_detach(sc->encap_cookie6);
196 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
197 }
198 #endif
199 #ifdef INET
200 if (sc->encap_cookie4 != NULL) {
201 err = encap_detach(sc->encap_cookie4);
202 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
203 }
204 #endif
205
206 if (ng_gif_detach_p != NULL)
207 (*ng_gif_detach_p)(ifp);
208 bpfdetach(ifp);
209 if_detach(ifp);
210 if_free(ifp);
211
212 GIF_LOCK_DESTROY(sc);
213
214 free(sc, M_GIF);
215 }
216
217 static void
218 gif_clone_destroy(ifp)
219 struct ifnet *ifp;
220 {
221 struct gif_softc *sc = ifp->if_softc;
222
223 mtx_lock(&gif_mtx);
224 LIST_REMOVE(sc, gif_list);
225 mtx_unlock(&gif_mtx);
226 gif_destroy(sc);
227 }
228
229 static int
230 gifmodevent(mod, type, data)
231 module_t mod;
232 int type;
233 void *data;
234 {
235 struct gif_softc *sc;
236
237 switch (type) {
238 case MOD_LOAD:
239 mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
240 LIST_INIT(&gif_softc_list);
241 if_clone_attach(&gif_cloner);
242
243 #ifdef INET6
244 ip6_gif_hlim = GIF_HLIM;
245 #endif
246
247 break;
248 case MOD_UNLOAD:
249 if_clone_detach(&gif_cloner);
250
251 mtx_lock(&gif_mtx);
252 while ((sc = LIST_FIRST(&gif_softc_list)) != NULL) {
253 LIST_REMOVE(sc, gif_list);
254 mtx_unlock(&gif_mtx);
255 gif_destroy(sc);
256 mtx_lock(&gif_mtx);
257 }
258 mtx_unlock(&gif_mtx);
259 mtx_destroy(&gif_mtx);
260 #ifdef INET6
261 ip6_gif_hlim = 0;
262 #endif
263 break;
264 default:
265 return EOPNOTSUPP;
266 }
267 return 0;
268 }
269
270 static moduledata_t gif_mod = {
271 "if_gif",
272 gifmodevent,
273 0
274 };
275
276 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
277 MODULE_VERSION(if_gif, 1);
278
279 int
280 gif_encapcheck(m, off, proto, arg)
281 const struct mbuf *m;
282 int off;
283 int proto;
284 void *arg;
285 {
286 struct ip ip;
287 struct gif_softc *sc;
288
289 sc = (struct gif_softc *)arg;
290 if (sc == NULL)
291 return 0;
292
293 if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
294 return 0;
295
296 /* no physical address */
297 if (!sc->gif_psrc || !sc->gif_pdst)
298 return 0;
299
300 switch (proto) {
301 #ifdef INET
302 case IPPROTO_IPV4:
303 break;
304 #endif
305 #ifdef INET6
306 case IPPROTO_IPV6:
307 break;
308 #endif
309 case IPPROTO_ETHERIP:
310 break;
311
312 default:
313 return 0;
314 }
315
316 /* Bail on short packets */
317 if (m->m_pkthdr.len < sizeof(ip))
318 return 0;
319
320 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
321
322 switch (ip.ip_v) {
323 #ifdef INET
324 case 4:
325 if (sc->gif_psrc->sa_family != AF_INET ||
326 sc->gif_pdst->sa_family != AF_INET)
327 return 0;
328 return gif_encapcheck4(m, off, proto, arg);
329 #endif
330 #ifdef INET6
331 case 6:
332 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
333 return 0;
334 if (sc->gif_psrc->sa_family != AF_INET6 ||
335 sc->gif_pdst->sa_family != AF_INET6)
336 return 0;
337 return gif_encapcheck6(m, off, proto, arg);
338 #endif
339 default:
340 return 0;
341 }
342 }
343
344 static void
345 gif_start(struct ifnet *ifp)
346 {
347 struct gif_softc *sc;
348 struct mbuf *m;
349
350 sc = ifp->if_softc;
351
352 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
353 for (;;) {
354 IFQ_DEQUEUE(&ifp->if_snd, m);
355 if (m == 0)
356 break;
357
358 gif_output(ifp, m, sc->gif_pdst, NULL);
359
360 }
361 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
362
363 return;
364 }
365
366 int
367 gif_output(ifp, m, dst, rt)
368 struct ifnet *ifp;
369 struct mbuf *m;
370 struct sockaddr *dst;
371 struct rtentry *rt; /* added in net2 */
372 {
373 struct gif_softc *sc = ifp->if_softc;
374 struct m_tag *mtag;
375 int error = 0;
376 int gif_called;
377 u_int32_t af;
378
379 #ifdef MAC
380 error = mac_check_ifnet_transmit(ifp, m);
381 if (error) {
382 m_freem(m);
383 goto end;
384 }
385 #endif
386
387 /*
388 * gif may cause infinite recursion calls when misconfigured.
389 * We'll prevent this by detecting loops.
390 *
391 * High nesting level may cause stack exhaustion.
392 * We'll prevent this by introducing upper limit.
393 */
394 gif_called = 1;
395 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
396 while (mtag != NULL) {
397 if (*(struct ifnet **)(mtag + 1) == ifp) {
398 log(LOG_NOTICE,
399 "gif_output: loop detected on %s\n",
400 (*(struct ifnet **)(mtag + 1))->if_xname);
401 m_freem(m);
402 error = EIO; /* is there better errno? */
403 goto end;
404 }
405 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
406 gif_called++;
407 }
408 if (gif_called > max_gif_nesting) {
409 log(LOG_NOTICE,
410 "gif_output: recursively called too many times(%d)\n",
411 gif_called);
412 m_freem(m);
413 error = EIO; /* is there better errno? */
414 goto end;
415 }
416 mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
417 M_NOWAIT);
418 if (mtag == NULL) {
419 m_freem(m);
420 error = ENOMEM;
421 goto end;
422 }
423 *(struct ifnet **)(mtag + 1) = ifp;
424 m_tag_prepend(m, mtag);
425
426 m->m_flags &= ~(M_BCAST|M_MCAST);
427
428 GIF_LOCK(sc);
429
430 if (!(ifp->if_flags & IFF_UP) ||
431 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
432 GIF_UNLOCK(sc);
433 m_freem(m);
434 error = ENETDOWN;
435 goto end;
436 }
437
438 /* BPF writes need to be handled specially. */
439 if (dst->sa_family == AF_UNSPEC) {
440 bcopy(dst->sa_data, &af, sizeof(af));
441 dst->sa_family = af;
442 }
443
444 af = dst->sa_family;
445 BPF_MTAP2(ifp, &af, sizeof(af), m);
446 ifp->if_opackets++;
447 ifp->if_obytes += m->m_pkthdr.len;
448
449 /* override to IPPROTO_ETHERIP for bridged traffic */
450 if (ifp->if_bridge)
451 af = AF_LINK;
452
453 /* inner AF-specific encapsulation */
454
455 /* XXX should we check if our outer source is legal? */
456
457 /* dispatch to output logic based on outer AF */
458 switch (sc->gif_psrc->sa_family) {
459 #ifdef INET
460 case AF_INET:
461 error = in_gif_output(ifp, af, m);
462 break;
463 #endif
464 #ifdef INET6
465 case AF_INET6:
466 error = in6_gif_output(ifp, af, m);
467 break;
468 #endif
469 default:
470 m_freem(m);
471 error = ENETDOWN;
472 }
473
474 GIF_UNLOCK(sc);
475 end:
476 if (error)
477 ifp->if_oerrors++;
478 return (error);
479 }
480
481 void
482 gif_input(m, af, ifp)
483 struct mbuf *m;
484 int af;
485 struct ifnet *ifp;
486 {
487 int isr, n;
488 struct etherip_header *eip;
489
490 if (ifp == NULL) {
491 /* just in case */
492 m_freem(m);
493 return;
494 }
495
496 m->m_pkthdr.rcvif = ifp;
497
498 #ifdef MAC
499 mac_create_mbuf_from_ifnet(ifp, m);
500 #endif
501
502 if (bpf_peers_present(ifp->if_bpf)) {
503 u_int32_t af1 = af;
504 bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
505 }
506
507 if (ng_gif_input_p != NULL) {
508 (*ng_gif_input_p)(ifp, &m, af);
509 if (m == NULL)
510 return;
511 }
512
513 /*
514 * Put the packet to the network layer input queue according to the
515 * specified address family.
516 * Note: older versions of gif_input directly called network layer
517 * input functions, e.g. ip6_input, here. We changed the policy to
518 * prevent too many recursive calls of such input functions, which
519 * might cause kernel panic. But the change may introduce another
520 * problem; if the input queue is full, packets are discarded.
521 * The kernel stack overflow really happened, and we believed
522 * queue-full rarely occurs, so we changed the policy.
523 */
524 switch (af) {
525 #ifdef INET
526 case AF_INET:
527 isr = NETISR_IP;
528 break;
529 #endif
530 #ifdef INET6
531 case AF_INET6:
532 isr = NETISR_IPV6;
533 break;
534 #endif
535 case AF_LINK:
536 n = sizeof(struct etherip_header) + sizeof(struct ether_header);
537 if (n > m->m_len) {
538 m = m_pullup(m, n);
539 if (m == NULL) {
540 ifp->if_ierrors++;
541 return;
542 }
543 }
544
545 eip = mtod(m, struct etherip_header *);
546 if (eip->eip_ver !=
547 (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) {
548 /* discard unknown versions */
549 m_freem(m);
550 return;
551 }
552 m_adj(m, sizeof(struct etherip_header));
553
554 m->m_flags &= ~(M_BCAST|M_MCAST);
555 m->m_pkthdr.rcvif = ifp;
556
557 if (ifp->if_bridge)
558 BRIDGE_INPUT(ifp, m);
559
560 if (m != NULL)
561 m_freem(m);
562 return;
563
564 default:
565 if (ng_gif_input_orphan_p != NULL)
566 (*ng_gif_input_orphan_p)(ifp, m, af);
567 else
568 m_freem(m);
569 return;
570 }
571
572 ifp->if_ipackets++;
573 ifp->if_ibytes += m->m_pkthdr.len;
574 netisr_dispatch(isr, m);
575 }
576
577 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
578 int
579 gif_ioctl(ifp, cmd, data)
580 struct ifnet *ifp;
581 u_long cmd;
582 caddr_t data;
583 {
584 struct gif_softc *sc = ifp->if_softc;
585 struct ifreq *ifr = (struct ifreq*)data;
586 int error = 0, size;
587 struct sockaddr *dst, *src;
588 #ifdef SIOCSIFMTU /* xxx */
589 u_long mtu;
590 #endif
591
592 switch (cmd) {
593 case SIOCSIFADDR:
594 ifp->if_flags |= IFF_UP;
595 break;
596
597 case SIOCSIFDSTADDR:
598 break;
599
600 case SIOCADDMULTI:
601 case SIOCDELMULTI:
602 break;
603
604 #ifdef SIOCSIFMTU /* xxx */
605 case SIOCGIFMTU:
606 break;
607
608 case SIOCSIFMTU:
609 mtu = ifr->ifr_mtu;
610 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
611 return (EINVAL);
612 ifp->if_mtu = mtu;
613 break;
614 #endif /* SIOCSIFMTU */
615
616 #ifdef INET
617 case SIOCSIFPHYADDR:
618 #endif
619 #ifdef INET6
620 case SIOCSIFPHYADDR_IN6:
621 #endif /* INET6 */
622 case SIOCSLIFPHYADDR:
623 switch (cmd) {
624 #ifdef INET
625 case SIOCSIFPHYADDR:
626 src = (struct sockaddr *)
627 &(((struct in_aliasreq *)data)->ifra_addr);
628 dst = (struct sockaddr *)
629 &(((struct in_aliasreq *)data)->ifra_dstaddr);
630 break;
631 #endif
632 #ifdef INET6
633 case SIOCSIFPHYADDR_IN6:
634 src = (struct sockaddr *)
635 &(((struct in6_aliasreq *)data)->ifra_addr);
636 dst = (struct sockaddr *)
637 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
638 break;
639 #endif
640 case SIOCSLIFPHYADDR:
641 src = (struct sockaddr *)
642 &(((struct if_laddrreq *)data)->addr);
643 dst = (struct sockaddr *)
644 &(((struct if_laddrreq *)data)->dstaddr);
645 break;
646 default:
647 return EINVAL;
648 }
649
650 /* sa_family must be equal */
651 if (src->sa_family != dst->sa_family)
652 return EINVAL;
653
654 /* validate sa_len */
655 switch (src->sa_family) {
656 #ifdef INET
657 case AF_INET:
658 if (src->sa_len != sizeof(struct sockaddr_in))
659 return EINVAL;
660 break;
661 #endif
662 #ifdef INET6
663 case AF_INET6:
664 if (src->sa_len != sizeof(struct sockaddr_in6))
665 return EINVAL;
666 break;
667 #endif
668 default:
669 return EAFNOSUPPORT;
670 }
671 switch (dst->sa_family) {
672 #ifdef INET
673 case AF_INET:
674 if (dst->sa_len != sizeof(struct sockaddr_in))
675 return EINVAL;
676 break;
677 #endif
678 #ifdef INET6
679 case AF_INET6:
680 if (dst->sa_len != sizeof(struct sockaddr_in6))
681 return EINVAL;
682 break;
683 #endif
684 default:
685 return EAFNOSUPPORT;
686 }
687
688 /* check sa_family looks sane for the cmd */
689 switch (cmd) {
690 case SIOCSIFPHYADDR:
691 if (src->sa_family == AF_INET)
692 break;
693 return EAFNOSUPPORT;
694 #ifdef INET6
695 case SIOCSIFPHYADDR_IN6:
696 if (src->sa_family == AF_INET6)
697 break;
698 return EAFNOSUPPORT;
699 #endif /* INET6 */
700 case SIOCSLIFPHYADDR:
701 /* checks done in the above */
702 break;
703 }
704
705 error = gif_set_tunnel(GIF2IFP(sc), src, dst);
706 break;
707
708 #ifdef SIOCDIFPHYADDR
709 case SIOCDIFPHYADDR:
710 gif_delete_tunnel(GIF2IFP(sc));
711 break;
712 #endif
713
714 case SIOCGIFPSRCADDR:
715 #ifdef INET6
716 case SIOCGIFPSRCADDR_IN6:
717 #endif /* INET6 */
718 if (sc->gif_psrc == NULL) {
719 error = EADDRNOTAVAIL;
720 goto bad;
721 }
722 src = sc->gif_psrc;
723 switch (cmd) {
724 #ifdef INET
725 case SIOCGIFPSRCADDR:
726 dst = &ifr->ifr_addr;
727 size = sizeof(ifr->ifr_addr);
728 break;
729 #endif /* INET */
730 #ifdef INET6
731 case SIOCGIFPSRCADDR_IN6:
732 dst = (struct sockaddr *)
733 &(((struct in6_ifreq *)data)->ifr_addr);
734 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
735 break;
736 #endif /* INET6 */
737 default:
738 error = EADDRNOTAVAIL;
739 goto bad;
740 }
741 if (src->sa_len > size)
742 return EINVAL;
743 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
744 #ifdef INET6
745 if (dst->sa_family == AF_INET6) {
746 error = sa6_recoverscope((struct sockaddr_in6 *)dst);
747 if (error != 0)
748 return (error);
749 }
750 #endif
751 break;
752
753 case SIOCGIFPDSTADDR:
754 #ifdef INET6
755 case SIOCGIFPDSTADDR_IN6:
756 #endif /* INET6 */
757 if (sc->gif_pdst == NULL) {
758 error = EADDRNOTAVAIL;
759 goto bad;
760 }
761 src = sc->gif_pdst;
762 switch (cmd) {
763 #ifdef INET
764 case SIOCGIFPDSTADDR:
765 dst = &ifr->ifr_addr;
766 size = sizeof(ifr->ifr_addr);
767 break;
768 #endif /* INET */
769 #ifdef INET6
770 case SIOCGIFPDSTADDR_IN6:
771 dst = (struct sockaddr *)
772 &(((struct in6_ifreq *)data)->ifr_addr);
773 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
774 break;
775 #endif /* INET6 */
776 default:
777 error = EADDRNOTAVAIL;
778 goto bad;
779 }
780 if (src->sa_len > size)
781 return EINVAL;
782 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
783 #ifdef INET6
784 if (dst->sa_family == AF_INET6) {
785 error = sa6_recoverscope((struct sockaddr_in6 *)dst);
786 if (error != 0)
787 return (error);
788 }
789 #endif
790 break;
791
792 case SIOCGLIFPHYADDR:
793 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
794 error = EADDRNOTAVAIL;
795 goto bad;
796 }
797
798 /* copy src */
799 src = sc->gif_psrc;
800 dst = (struct sockaddr *)
801 &(((struct if_laddrreq *)data)->addr);
802 size = sizeof(((struct if_laddrreq *)data)->addr);
803 if (src->sa_len > size)
804 return EINVAL;
805 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
806
807 /* copy dst */
808 src = sc->gif_pdst;
809 dst = (struct sockaddr *)
810 &(((struct if_laddrreq *)data)->dstaddr);
811 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
812 if (src->sa_len > size)
813 return EINVAL;
814 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
815 break;
816
817 case SIOCSIFFLAGS:
818 /* if_ioctl() takes care of it */
819 break;
820
821 default:
822 error = EINVAL;
823 break;
824 }
825 bad:
826 return error;
827 }
828
829 /*
830 * XXXRW: There's a general event-ordering issue here: the code to check
831 * if a given tunnel is already present happens before we perform a
832 * potentially blocking setup of the tunnel. This code needs to be
833 * re-ordered so that the check and replacement can be atomic using
834 * a mutex.
835 */
836 int
837 gif_set_tunnel(ifp, src, dst)
838 struct ifnet *ifp;
839 struct sockaddr *src;
840 struct sockaddr *dst;
841 {
842 struct gif_softc *sc = ifp->if_softc;
843 struct gif_softc *sc2;
844 struct sockaddr *osrc, *odst, *sa;
845 int error = 0;
846
847 mtx_lock(&gif_mtx);
848 LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
849 if (sc2 == sc)
850 continue;
851 if (!sc2->gif_pdst || !sc2->gif_psrc)
852 continue;
853 if (sc2->gif_pdst->sa_family != dst->sa_family ||
854 sc2->gif_pdst->sa_len != dst->sa_len ||
855 sc2->gif_psrc->sa_family != src->sa_family ||
856 sc2->gif_psrc->sa_len != src->sa_len)
857 continue;
858
859 /*
860 * Disallow parallel tunnels unless instructed
861 * otherwise.
862 */
863 if (!parallel_tunnels &&
864 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
865 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
866 error = EADDRNOTAVAIL;
867 mtx_unlock(&gif_mtx);
868 goto bad;
869 }
870
871 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
872 }
873 mtx_unlock(&gif_mtx);
874
875 /* XXX we can detach from both, but be polite just in case */
876 if (sc->gif_psrc)
877 switch (sc->gif_psrc->sa_family) {
878 #ifdef INET
879 case AF_INET:
880 (void)in_gif_detach(sc);
881 break;
882 #endif
883 #ifdef INET6
884 case AF_INET6:
885 (void)in6_gif_detach(sc);
886 break;
887 #endif
888 }
889
890 osrc = sc->gif_psrc;
891 sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
892 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
893 sc->gif_psrc = sa;
894
895 odst = sc->gif_pdst;
896 sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
897 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
898 sc->gif_pdst = sa;
899
900 switch (sc->gif_psrc->sa_family) {
901 #ifdef INET
902 case AF_INET:
903 error = in_gif_attach(sc);
904 break;
905 #endif
906 #ifdef INET6
907 case AF_INET6:
908 /*
909 * Check validity of the scope zone ID of the addresses, and
910 * convert it into the kernel internal form if necessary.
911 */
912 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
913 if (error != 0)
914 break;
915 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
916 if (error != 0)
917 break;
918 error = in6_gif_attach(sc);
919 break;
920 #endif
921 }
922 if (error) {
923 /* rollback */
924 free((caddr_t)sc->gif_psrc, M_IFADDR);
925 free((caddr_t)sc->gif_pdst, M_IFADDR);
926 sc->gif_psrc = osrc;
927 sc->gif_pdst = odst;
928 goto bad;
929 }
930
931 if (osrc)
932 free((caddr_t)osrc, M_IFADDR);
933 if (odst)
934 free((caddr_t)odst, M_IFADDR);
935
936 bad:
937 if (sc->gif_psrc && sc->gif_pdst)
938 ifp->if_drv_flags |= IFF_DRV_RUNNING;
939 else
940 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
941
942 return error;
943 }
944
945 void
946 gif_delete_tunnel(ifp)
947 struct ifnet *ifp;
948 {
949 struct gif_softc *sc = ifp->if_softc;
950
951 if (sc->gif_psrc) {
952 free((caddr_t)sc->gif_psrc, M_IFADDR);
953 sc->gif_psrc = NULL;
954 }
955 if (sc->gif_pdst) {
956 free((caddr_t)sc->gif_pdst, M_IFADDR);
957 sc->gif_pdst = NULL;
958 }
959 /* it is safe to detach from both */
960 #ifdef INET
961 (void)in_gif_detach(sc);
962 #endif
963 #ifdef INET6
964 (void)in6_gif_detach(sc);
965 #endif
966 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
967 }
Cache object: c8bcda85ea861a3937bd5cd74ae73907
|