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