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