FreeBSD/Linux Kernel Cross Reference
sys/net/if_gif.c
1 /* $FreeBSD: releng/6.0/sys/net/if_gif.c 149443 2005-08-25 05:01:24Z rwatson $ */
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/in6_gif.h>
78 #include <netinet6/ip6protosw.h>
79 #endif /* INET6 */
80
81 #include <netinet/ip_encap.h>
82 #include <net/if_gif.h>
83
84 #include <net/net_osdep.h>
85
86 #define GIFNAME "gif"
87
88 /*
89 * gif_mtx protects the global gif_softc_list.
90 * XXX: Per-softc locking is still required.
91 */
92 static struct mtx gif_mtx;
93 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
94 static LIST_HEAD(, gif_softc) gif_softc_list;
95
96 void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
97 void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
98 void (*ng_gif_attach_p)(struct ifnet *ifp);
99 void (*ng_gif_detach_p)(struct ifnet *ifp);
100
101 static int gif_clone_create(struct if_clone *, int);
102 static void gif_clone_destroy(struct ifnet *);
103
104 IFC_SIMPLE_DECLARE(gif, 0);
105
106 static int gifmodevent(module_t, int, void *);
107
108 SYSCTL_DECL(_net_link);
109 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
110 "Generic Tunnel Interface");
111 #ifndef MAX_GIF_NEST
112 /*
113 * This macro controls the default upper limitation on nesting of gif tunnels.
114 * Since, setting a large value to this macro with a careless configuration
115 * may introduce system crash, we don't allow any nestings by default.
116 * If you need to configure nested gif tunnels, you can define this macro
117 * in your kernel configuration file. However, if you do so, please be
118 * careful to configure the tunnels so that it won't make a loop.
119 */
120 #define MAX_GIF_NEST 1
121 #endif
122 static int max_gif_nesting = MAX_GIF_NEST;
123 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
124 &max_gif_nesting, 0, "Max nested tunnels");
125
126 /*
127 * By default, we disallow creation of multiple tunnels between the same
128 * pair of addresses. Some applications require this functionality so
129 * we allow control over this check here.
130 */
131 #ifdef XBONEHACK
132 static int parallel_tunnels = 1;
133 #else
134 static int parallel_tunnels = 0;
135 #endif
136 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
137 ¶llel_tunnels, 0, "Allow parallel tunnels?");
138
139 static int
140 gif_clone_create(ifc, unit)
141 struct if_clone *ifc;
142 int unit;
143 {
144 struct gif_softc *sc;
145
146 sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
147 GIF2IFP(sc) = if_alloc(IFT_GIF);
148 if (GIF2IFP(sc) == NULL) {
149 free(sc, M_GIF);
150 return (ENOSPC);
151 }
152
153 GIF2IFP(sc)->if_softc = sc;
154 if_initname(GIF2IFP(sc), ifc->ifc_name, unit);
155
156 gifattach0(sc);
157
158 mtx_lock(&gif_mtx);
159 LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
160 mtx_unlock(&gif_mtx);
161 return (0);
162 }
163
164 void
165 gifattach0(sc)
166 struct gif_softc *sc;
167 {
168
169 sc->encap_cookie4 = sc->encap_cookie6 = NULL;
170
171 GIF2IFP(sc)->if_addrlen = 0;
172 GIF2IFP(sc)->if_mtu = GIF_MTU;
173 GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
174 #if 0
175 /* turn off ingress filter */
176 GIF2IFP(sc)->if_flags |= IFF_LINK2;
177 #endif
178 GIF2IFP(sc)->if_ioctl = gif_ioctl;
179 GIF2IFP(sc)->if_output = gif_output;
180 GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
181 if_attach(GIF2IFP(sc));
182 bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
183 if (ng_gif_attach_p != NULL)
184 (*ng_gif_attach_p)(GIF2IFP(sc));
185 }
186
187 static void
188 gif_destroy(struct gif_softc *sc)
189 {
190 struct ifnet *ifp = GIF2IFP(sc);
191 int err;
192
193 gif_delete_tunnel(ifp);
194 #ifdef INET6
195 if (sc->encap_cookie6 != NULL) {
196 err = encap_detach(sc->encap_cookie6);
197 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
198 }
199 #endif
200 #ifdef INET
201 if (sc->encap_cookie4 != NULL) {
202 err = encap_detach(sc->encap_cookie4);
203 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
204 }
205 #endif
206
207 if (ng_gif_detach_p != NULL)
208 (*ng_gif_detach_p)(ifp);
209 bpfdetach(ifp);
210 if_detach(ifp);
211 if_free(ifp);
212
213 free(sc, M_GIF);
214 }
215
216 static void
217 gif_clone_destroy(ifp)
218 struct ifnet *ifp;
219 {
220 struct gif_softc *sc = ifp->if_softc;
221
222 mtx_lock(&gif_mtx);
223 LIST_REMOVE(sc, gif_list);
224 mtx_unlock(&gif_mtx);
225 gif_destroy(sc);
226 }
227
228 static int
229 gifmodevent(mod, type, data)
230 module_t mod;
231 int type;
232 void *data;
233 {
234 struct gif_softc *sc;
235
236 switch (type) {
237 case MOD_LOAD:
238 mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
239 LIST_INIT(&gif_softc_list);
240 if_clone_attach(&gif_cloner);
241
242 #ifdef INET6
243 ip6_gif_hlim = GIF_HLIM;
244 #endif
245
246 break;
247 case MOD_UNLOAD:
248 if_clone_detach(&gif_cloner);
249
250 mtx_lock(&gif_mtx);
251 while ((sc = LIST_FIRST(&gif_softc_list)) != NULL) {
252 LIST_REMOVE(sc, gif_list);
253 mtx_unlock(&gif_mtx);
254 gif_destroy(sc);
255 mtx_lock(&gif_mtx);
256 }
257 mtx_unlock(&gif_mtx);
258 mtx_destroy(&gif_mtx);
259 #ifdef INET6
260 ip6_gif_hlim = 0;
261 #endif
262 break;
263 default:
264 return EOPNOTSUPP;
265 }
266 return 0;
267 }
268
269 static moduledata_t gif_mod = {
270 "if_gif",
271 gifmodevent,
272 0
273 };
274
275 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
276 MODULE_VERSION(if_gif, 1);
277
278 int
279 gif_encapcheck(m, off, proto, arg)
280 const struct mbuf *m;
281 int off;
282 int proto;
283 void *arg;
284 {
285 struct ip ip;
286 struct gif_softc *sc;
287
288 sc = (struct gif_softc *)arg;
289 if (sc == NULL)
290 return 0;
291
292 if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
293 return 0;
294
295 /* no physical address */
296 if (!sc->gif_psrc || !sc->gif_pdst)
297 return 0;
298
299 switch (proto) {
300 #ifdef INET
301 case IPPROTO_IPV4:
302 break;
303 #endif
304 #ifdef INET6
305 case IPPROTO_IPV6:
306 break;
307 #endif
308 default:
309 return 0;
310 }
311
312 /* Bail on short packets */
313 if (m->m_pkthdr.len < sizeof(ip))
314 return 0;
315
316 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
317
318 switch (ip.ip_v) {
319 #ifdef INET
320 case 4:
321 if (sc->gif_psrc->sa_family != AF_INET ||
322 sc->gif_pdst->sa_family != AF_INET)
323 return 0;
324 return gif_encapcheck4(m, off, proto, arg);
325 #endif
326 #ifdef INET6
327 case 6:
328 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
329 return 0;
330 if (sc->gif_psrc->sa_family != AF_INET6 ||
331 sc->gif_pdst->sa_family != AF_INET6)
332 return 0;
333 return gif_encapcheck6(m, off, proto, arg);
334 #endif
335 default:
336 return 0;
337 }
338 }
339
340 int
341 gif_output(ifp, m, dst, rt)
342 struct ifnet *ifp;
343 struct mbuf *m;
344 struct sockaddr *dst;
345 struct rtentry *rt; /* added in net2 */
346 {
347 struct gif_softc *sc = ifp->if_softc;
348 struct m_tag *mtag;
349 int error = 0;
350 int gif_called;
351 u_int32_t af;
352
353 #ifdef MAC
354 error = mac_check_ifnet_transmit(ifp, m);
355 if (error) {
356 m_freem(m);
357 goto end;
358 }
359 #endif
360
361 /*
362 * gif may cause infinite recursion calls when misconfigured.
363 * We'll prevent this by detecting loops.
364 *
365 * High nesting level may cause stack exhaustion.
366 * We'll prevent this by introducing upper limit.
367 */
368 gif_called = 1;
369 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
370 while (mtag != NULL) {
371 if (*(struct ifnet **)(mtag + 1) == ifp) {
372 log(LOG_NOTICE,
373 "gif_output: loop detected on %s\n",
374 (*(struct ifnet **)(mtag + 1))->if_xname);
375 m_freem(m);
376 error = EIO; /* is there better errno? */
377 goto end;
378 }
379 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
380 gif_called++;
381 }
382 if (gif_called > max_gif_nesting) {
383 log(LOG_NOTICE,
384 "gif_output: recursively called too many times(%d)\n",
385 gif_called);
386 m_freem(m);
387 error = EIO; /* is there better errno? */
388 goto end;
389 }
390 mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
391 M_NOWAIT);
392 if (mtag == NULL) {
393 m_freem(m);
394 error = ENOMEM;
395 goto end;
396 }
397 *(struct ifnet **)(mtag + 1) = ifp;
398 m_tag_prepend(m, mtag);
399
400 m->m_flags &= ~(M_BCAST|M_MCAST);
401 if (!(ifp->if_flags & IFF_UP) ||
402 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
403 m_freem(m);
404 error = ENETDOWN;
405 goto end;
406 }
407
408 /* BPF writes need to be handled specially. */
409 if (dst->sa_family == AF_UNSPEC) {
410 bcopy(dst->sa_data, &af, sizeof(af));
411 dst->sa_family = af;
412 }
413
414 if (ifp->if_bpf) {
415 af = dst->sa_family;
416 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
417 }
418 ifp->if_opackets++;
419 ifp->if_obytes += m->m_pkthdr.len;
420
421 /* inner AF-specific encapsulation */
422
423 /* XXX should we check if our outer source is legal? */
424
425 /* dispatch to output logic based on outer AF */
426 switch (sc->gif_psrc->sa_family) {
427 #ifdef INET
428 case AF_INET:
429 error = in_gif_output(ifp, dst->sa_family, m);
430 break;
431 #endif
432 #ifdef INET6
433 case AF_INET6:
434 error = in6_gif_output(ifp, dst->sa_family, m);
435 break;
436 #endif
437 default:
438 m_freem(m);
439 error = ENETDOWN;
440 goto end;
441 }
442
443 end:
444 if (error)
445 ifp->if_oerrors++;
446 return error;
447 }
448
449 void
450 gif_input(m, af, ifp)
451 struct mbuf *m;
452 int af;
453 struct ifnet *ifp;
454 {
455 int isr;
456
457 if (ifp == NULL) {
458 /* just in case */
459 m_freem(m);
460 return;
461 }
462
463 m->m_pkthdr.rcvif = ifp;
464
465 #ifdef MAC
466 mac_create_mbuf_from_ifnet(ifp, m);
467 #endif
468
469 if (ifp->if_bpf) {
470 u_int32_t af1 = af;
471 bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
472 }
473
474 if (ng_gif_input_p != NULL) {
475 (*ng_gif_input_p)(ifp, &m, af);
476 if (m == NULL)
477 return;
478 }
479
480 /*
481 * Put the packet to the network layer input queue according to the
482 * specified address family.
483 * Note: older versions of gif_input directly called network layer
484 * input functions, e.g. ip6_input, here. We changed the policy to
485 * prevent too many recursive calls of such input functions, which
486 * might cause kernel panic. But the change may introduce another
487 * problem; if the input queue is full, packets are discarded.
488 * The kernel stack overflow really happened, and we believed
489 * queue-full rarely occurs, so we changed the policy.
490 */
491 switch (af) {
492 #ifdef INET
493 case AF_INET:
494 isr = NETISR_IP;
495 break;
496 #endif
497 #ifdef INET6
498 case AF_INET6:
499 isr = NETISR_IPV6;
500 break;
501 #endif
502 default:
503 if (ng_gif_input_orphan_p != NULL)
504 (*ng_gif_input_orphan_p)(ifp, m, af);
505 else
506 m_freem(m);
507 return;
508 }
509
510 ifp->if_ipackets++;
511 ifp->if_ibytes += m->m_pkthdr.len;
512 netisr_dispatch(isr, m);
513 }
514
515 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
516 int
517 gif_ioctl(ifp, cmd, data)
518 struct ifnet *ifp;
519 u_long cmd;
520 caddr_t data;
521 {
522 struct gif_softc *sc = ifp->if_softc;
523 struct ifreq *ifr = (struct ifreq*)data;
524 int error = 0, size;
525 struct sockaddr *dst, *src;
526 #ifdef SIOCSIFMTU /* xxx */
527 u_long mtu;
528 #endif
529
530 switch (cmd) {
531 case SIOCSIFADDR:
532 ifp->if_flags |= IFF_UP;
533 break;
534
535 case SIOCSIFDSTADDR:
536 break;
537
538 case SIOCADDMULTI:
539 case SIOCDELMULTI:
540 break;
541
542 #ifdef SIOCSIFMTU /* xxx */
543 case SIOCGIFMTU:
544 break;
545
546 case SIOCSIFMTU:
547 mtu = ifr->ifr_mtu;
548 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
549 return (EINVAL);
550 ifp->if_mtu = mtu;
551 break;
552 #endif /* SIOCSIFMTU */
553
554 #ifdef INET
555 case SIOCSIFPHYADDR:
556 #endif
557 #ifdef INET6
558 case SIOCSIFPHYADDR_IN6:
559 #endif /* INET6 */
560 case SIOCSLIFPHYADDR:
561 switch (cmd) {
562 #ifdef 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 #ifdef 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 break;
584 default:
585 return EINVAL;
586 }
587
588 /* sa_family must be equal */
589 if (src->sa_family != dst->sa_family)
590 return EINVAL;
591
592 /* validate sa_len */
593 switch (src->sa_family) {
594 #ifdef INET
595 case AF_INET:
596 if (src->sa_len != sizeof(struct sockaddr_in))
597 return EINVAL;
598 break;
599 #endif
600 #ifdef INET6
601 case AF_INET6:
602 if (src->sa_len != sizeof(struct sockaddr_in6))
603 return EINVAL;
604 break;
605 #endif
606 default:
607 return EAFNOSUPPORT;
608 }
609 switch (dst->sa_family) {
610 #ifdef INET
611 case AF_INET:
612 if (dst->sa_len != sizeof(struct sockaddr_in))
613 return EINVAL;
614 break;
615 #endif
616 #ifdef INET6
617 case AF_INET6:
618 if (dst->sa_len != sizeof(struct sockaddr_in6))
619 return EINVAL;
620 break;
621 #endif
622 default:
623 return EAFNOSUPPORT;
624 }
625
626 /* check sa_family looks sane for the cmd */
627 switch (cmd) {
628 case SIOCSIFPHYADDR:
629 if (src->sa_family == AF_INET)
630 break;
631 return EAFNOSUPPORT;
632 #ifdef INET6
633 case SIOCSIFPHYADDR_IN6:
634 if (src->sa_family == AF_INET6)
635 break;
636 return EAFNOSUPPORT;
637 #endif /* INET6 */
638 case SIOCSLIFPHYADDR:
639 /* checks done in the above */
640 break;
641 }
642
643 error = gif_set_tunnel(GIF2IFP(sc), src, dst);
644 break;
645
646 #ifdef SIOCDIFPHYADDR
647 case SIOCDIFPHYADDR:
648 gif_delete_tunnel(GIF2IFP(sc));
649 break;
650 #endif
651
652 case SIOCGIFPSRCADDR:
653 #ifdef INET6
654 case SIOCGIFPSRCADDR_IN6:
655 #endif /* INET6 */
656 if (sc->gif_psrc == NULL) {
657 error = EADDRNOTAVAIL;
658 goto bad;
659 }
660 src = sc->gif_psrc;
661 switch (cmd) {
662 #ifdef INET
663 case SIOCGIFPSRCADDR:
664 dst = &ifr->ifr_addr;
665 size = sizeof(ifr->ifr_addr);
666 break;
667 #endif /* INET */
668 #ifdef INET6
669 case SIOCGIFPSRCADDR_IN6:
670 dst = (struct sockaddr *)
671 &(((struct in6_ifreq *)data)->ifr_addr);
672 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
673 break;
674 #endif /* INET6 */
675 default:
676 error = EADDRNOTAVAIL;
677 goto bad;
678 }
679 if (src->sa_len > size)
680 return EINVAL;
681 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
682 break;
683
684 case SIOCGIFPDSTADDR:
685 #ifdef INET6
686 case SIOCGIFPDSTADDR_IN6:
687 #endif /* INET6 */
688 if (sc->gif_pdst == NULL) {
689 error = EADDRNOTAVAIL;
690 goto bad;
691 }
692 src = sc->gif_pdst;
693 switch (cmd) {
694 #ifdef INET
695 case SIOCGIFPDSTADDR:
696 dst = &ifr->ifr_addr;
697 size = sizeof(ifr->ifr_addr);
698 break;
699 #endif /* INET */
700 #ifdef INET6
701 case SIOCGIFPDSTADDR_IN6:
702 dst = (struct sockaddr *)
703 &(((struct in6_ifreq *)data)->ifr_addr);
704 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
705 break;
706 #endif /* INET6 */
707 default:
708 error = EADDRNOTAVAIL;
709 goto bad;
710 }
711 if (src->sa_len > size)
712 return EINVAL;
713 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
714 break;
715
716 case SIOCGLIFPHYADDR:
717 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
718 error = EADDRNOTAVAIL;
719 goto bad;
720 }
721
722 /* copy src */
723 src = sc->gif_psrc;
724 dst = (struct sockaddr *)
725 &(((struct if_laddrreq *)data)->addr);
726 size = sizeof(((struct if_laddrreq *)data)->addr);
727 if (src->sa_len > size)
728 return EINVAL;
729 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
730
731 /* copy dst */
732 src = sc->gif_pdst;
733 dst = (struct sockaddr *)
734 &(((struct if_laddrreq *)data)->dstaddr);
735 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
736 if (src->sa_len > size)
737 return EINVAL;
738 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
739 break;
740
741 case SIOCSIFFLAGS:
742 /* if_ioctl() takes care of it */
743 break;
744
745 default:
746 error = EINVAL;
747 break;
748 }
749 bad:
750 return error;
751 }
752
753 /*
754 * XXXRW: There's a general event-ordering issue here: the code to check
755 * if a given tunnel is already present happens before we perform a
756 * potentially blocking setup of the tunnel. This code needs to be
757 * re-ordered so that the check and replacement can be atomic using
758 * a mutex.
759 */
760 int
761 gif_set_tunnel(ifp, src, dst)
762 struct ifnet *ifp;
763 struct sockaddr *src;
764 struct sockaddr *dst;
765 {
766 struct gif_softc *sc = ifp->if_softc;
767 struct gif_softc *sc2;
768 struct sockaddr *osrc, *odst, *sa;
769 int s;
770 int error = 0;
771
772 s = splnet();
773
774 mtx_lock(&gif_mtx);
775 LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
776 if (sc2 == sc)
777 continue;
778 if (!sc2->gif_pdst || !sc2->gif_psrc)
779 continue;
780 if (sc2->gif_pdst->sa_family != dst->sa_family ||
781 sc2->gif_pdst->sa_len != dst->sa_len ||
782 sc2->gif_psrc->sa_family != src->sa_family ||
783 sc2->gif_psrc->sa_len != src->sa_len)
784 continue;
785
786 /*
787 * Disallow parallel tunnels unless instructed
788 * otherwise.
789 */
790 if (!parallel_tunnels &&
791 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
792 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
793 error = EADDRNOTAVAIL;
794 mtx_unlock(&gif_mtx);
795 goto bad;
796 }
797
798 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
799 }
800 mtx_unlock(&gif_mtx);
801
802 /* XXX we can detach from both, but be polite just in case */
803 if (sc->gif_psrc)
804 switch (sc->gif_psrc->sa_family) {
805 #ifdef INET
806 case AF_INET:
807 (void)in_gif_detach(sc);
808 break;
809 #endif
810 #ifdef INET6
811 case AF_INET6:
812 (void)in6_gif_detach(sc);
813 break;
814 #endif
815 }
816
817 osrc = sc->gif_psrc;
818 sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
819 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
820 sc->gif_psrc = sa;
821
822 odst = sc->gif_pdst;
823 sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
824 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
825 sc->gif_pdst = sa;
826
827 switch (sc->gif_psrc->sa_family) {
828 #ifdef INET
829 case AF_INET:
830 error = in_gif_attach(sc);
831 break;
832 #endif
833 #ifdef INET6
834 case AF_INET6:
835 error = in6_gif_attach(sc);
836 break;
837 #endif
838 }
839 if (error) {
840 /* rollback */
841 free((caddr_t)sc->gif_psrc, M_IFADDR);
842 free((caddr_t)sc->gif_pdst, M_IFADDR);
843 sc->gif_psrc = osrc;
844 sc->gif_pdst = odst;
845 goto bad;
846 }
847
848 if (osrc)
849 free((caddr_t)osrc, M_IFADDR);
850 if (odst)
851 free((caddr_t)odst, M_IFADDR);
852
853 if (sc->gif_psrc && sc->gif_pdst)
854 ifp->if_drv_flags |= IFF_DRV_RUNNING;
855 else
856 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
857 splx(s);
858
859 return 0;
860
861 bad:
862 if (sc->gif_psrc && sc->gif_pdst)
863 ifp->if_drv_flags |= IFF_DRV_RUNNING;
864 else
865 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
866 splx(s);
867
868 return error;
869 }
870
871 void
872 gif_delete_tunnel(ifp)
873 struct ifnet *ifp;
874 {
875 struct gif_softc *sc = ifp->if_softc;
876 int s;
877
878 s = splnet();
879
880 if (sc->gif_psrc) {
881 free((caddr_t)sc->gif_psrc, M_IFADDR);
882 sc->gif_psrc = NULL;
883 }
884 if (sc->gif_pdst) {
885 free((caddr_t)sc->gif_pdst, M_IFADDR);
886 sc->gif_pdst = NULL;
887 }
888 /* it is safe to detach from both */
889 #ifdef INET
890 (void)in_gif_detach(sc);
891 #endif
892 #ifdef INET6
893 (void)in6_gif_detach(sc);
894 #endif
895
896 if (sc->gif_psrc && sc->gif_pdst)
897 ifp->if_drv_flags |= IFF_DRV_RUNNING;
898 else
899 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
900 splx(s);
901 }
Cache object: 2d7421ad2e3579bef3972b9c086a7d8d
|