FreeBSD/Linux Kernel Cross Reference
sys/net/if_gif.c
1 /*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/jail.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/module.h>
46 #include <sys/rmlock.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/sx.h>
50 #include <sys/errno.h>
51 #include <sys/time.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
54 #include <sys/priv.h>
55 #include <sys/proc.h>
56 #include <sys/protosw.h>
57 #include <sys/conf.h>
58 #include <machine/cpu.h>
59
60 #include <net/if.h>
61 #include <net/if_var.h>
62 #include <net/if_clone.h>
63 #include <net/if_types.h>
64 #include <net/netisr.h>
65 #include <net/route.h>
66 #include <net/bpf.h>
67 #include <net/vnet.h>
68
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip_ecn.h>
73 #ifdef INET
74 #include <netinet/in_var.h>
75 #include <netinet/ip_var.h>
76 #endif /* INET */
77
78 #ifdef INET6
79 #ifndef INET
80 #include <netinet/in.h>
81 #endif
82 #include <netinet6/in6_var.h>
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_ecn.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet6/scope6_var.h>
87 #include <netinet6/ip6protosw.h>
88 #endif /* INET6 */
89
90 #include <netinet/ip_encap.h>
91 #include <net/ethernet.h>
92 #include <net/if_bridgevar.h>
93 #include <net/if_gif.h>
94
95 #include <security/mac/mac_framework.h>
96
97 static const char gifname[] = "gif";
98
99 /*
100 * gif_mtx protects a per-vnet gif_softc_list.
101 */
102 static VNET_DEFINE(struct mtx, gif_mtx);
103 #define V_gif_mtx VNET(gif_mtx)
104 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
105 static VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list);
106 #define V_gif_softc_list VNET(gif_softc_list)
107 static struct sx gif_ioctl_sx;
108 SX_SYSINIT(gif_ioctl_sx, &gif_ioctl_sx, "gif_ioctl");
109
110 #define GIF_LIST_LOCK_INIT(x) mtx_init(&V_gif_mtx, "gif_mtx", \
111 NULL, MTX_DEF)
112 #define GIF_LIST_LOCK_DESTROY(x) mtx_destroy(&V_gif_mtx)
113 #define GIF_LIST_LOCK(x) mtx_lock(&V_gif_mtx)
114 #define GIF_LIST_UNLOCK(x) mtx_unlock(&V_gif_mtx)
115
116 void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
117 void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
118 void (*ng_gif_attach_p)(struct ifnet *ifp);
119 void (*ng_gif_detach_p)(struct ifnet *ifp);
120
121 static int gif_check_nesting(struct ifnet *, struct mbuf *);
122 static int gif_set_tunnel(struct ifnet *, struct sockaddr *,
123 struct sockaddr *);
124 static void gif_delete_tunnel(struct ifnet *);
125 static int gif_ioctl(struct ifnet *, u_long, caddr_t);
126 static int gif_transmit(struct ifnet *, struct mbuf *);
127 static void gif_qflush(struct ifnet *);
128 static int gif_clone_create(struct if_clone *, int, caddr_t);
129 static void gif_clone_destroy(struct ifnet *);
130 static VNET_DEFINE(struct if_clone *, gif_cloner);
131 #define V_gif_cloner VNET(gif_cloner)
132
133 static int gifmodevent(module_t, int, void *);
134
135 SYSCTL_DECL(_net_link);
136 static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
137 "Generic Tunnel Interface");
138 #ifndef MAX_GIF_NEST
139 /*
140 * This macro controls the default upper limitation on nesting of gif tunnels.
141 * Since, setting a large value to this macro with a careless configuration
142 * may introduce system crash, we don't allow any nestings by default.
143 * If you need to configure nested gif tunnels, you can define this macro
144 * in your kernel configuration file. However, if you do so, please be
145 * careful to configure the tunnels so that it won't make a loop.
146 */
147 #define MAX_GIF_NEST 1
148 #endif
149 static VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST;
150 #define V_max_gif_nesting VNET(max_gif_nesting)
151 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW,
152 &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels");
153
154 /*
155 * By default, we disallow creation of multiple tunnels between the same
156 * pair of addresses. Some applications require this functionality so
157 * we allow control over this check here.
158 */
159 #ifdef XBONEHACK
160 static VNET_DEFINE(int, parallel_tunnels) = 1;
161 #else
162 static VNET_DEFINE(int, parallel_tunnels) = 0;
163 #endif
164 #define V_parallel_tunnels VNET(parallel_tunnels)
165 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels,
166 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(parallel_tunnels), 0,
167 "Allow parallel tunnels?");
168
169 static int
170 gif_clone_create(struct if_clone *ifc, int unit, caddr_t params)
171 {
172 struct gif_softc *sc;
173
174 sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
175 sc->gif_fibnum = curthread->td_proc->p_fibnum;
176 GIF2IFP(sc) = if_alloc(IFT_GIF);
177 GIF_LOCK_INIT(sc);
178 GIF2IFP(sc)->if_softc = sc;
179 if_initname(GIF2IFP(sc), gifname, unit);
180
181 GIF2IFP(sc)->if_addrlen = 0;
182 GIF2IFP(sc)->if_mtu = GIF_MTU;
183 GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
184 #if 0
185 /* turn off ingress filter */
186 GIF2IFP(sc)->if_flags |= IFF_LINK2;
187 #endif
188 GIF2IFP(sc)->if_ioctl = gif_ioctl;
189 GIF2IFP(sc)->if_transmit = gif_transmit;
190 GIF2IFP(sc)->if_qflush = gif_qflush;
191 GIF2IFP(sc)->if_output = gif_output;
192 GIF2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
193 GIF2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
194 if_attach(GIF2IFP(sc));
195 bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
196 if (ng_gif_attach_p != NULL)
197 (*ng_gif_attach_p)(GIF2IFP(sc));
198
199 GIF_LIST_LOCK();
200 LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list);
201 GIF_LIST_UNLOCK();
202 return (0);
203 }
204
205 static void
206 gif_clone_destroy(struct ifnet *ifp)
207 {
208 struct gif_softc *sc;
209
210 sx_xlock(&gif_ioctl_sx);
211 sc = ifp->if_softc;
212 gif_delete_tunnel(ifp);
213 GIF_LIST_LOCK();
214 LIST_REMOVE(sc, gif_list);
215 GIF_LIST_UNLOCK();
216 if (ng_gif_detach_p != NULL)
217 (*ng_gif_detach_p)(ifp);
218 bpfdetach(ifp);
219 if_detach(ifp);
220 ifp->if_softc = NULL;
221 sx_xunlock(&gif_ioctl_sx);
222
223 if_free(ifp);
224 GIF_LOCK_DESTROY(sc);
225 free(sc, M_GIF);
226 }
227
228 static void
229 vnet_gif_init(const void *unused __unused)
230 {
231
232 LIST_INIT(&V_gif_softc_list);
233 GIF_LIST_LOCK_INIT();
234 V_gif_cloner = if_clone_simple(gifname, gif_clone_create,
235 gif_clone_destroy, 0);
236 }
237 VNET_SYSINIT(vnet_gif_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
238 vnet_gif_init, NULL);
239
240 static void
241 vnet_gif_uninit(const void *unused __unused)
242 {
243
244 if_clone_detach(V_gif_cloner);
245 GIF_LIST_LOCK_DESTROY();
246 }
247 VNET_SYSUNINIT(vnet_gif_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
248 vnet_gif_uninit, NULL);
249
250 static int
251 gifmodevent(module_t mod, int type, void *data)
252 {
253
254 switch (type) {
255 case MOD_LOAD:
256 case MOD_UNLOAD:
257 break;
258 default:
259 return (EOPNOTSUPP);
260 }
261 return (0);
262 }
263
264 static moduledata_t gif_mod = {
265 "if_gif",
266 gifmodevent,
267 0
268 };
269
270 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
271 MODULE_VERSION(if_gif, 1);
272
273 int
274 gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
275 {
276 GIF_RLOCK_TRACKER;
277 const struct ip *ip;
278 struct gif_softc *sc;
279 int ret;
280
281 sc = (struct gif_softc *)arg;
282 if (sc == NULL || (GIF2IFP(sc)->if_flags & IFF_UP) == 0)
283 return (0);
284
285 ret = 0;
286 GIF_RLOCK(sc);
287
288 /* no physical address */
289 if (sc->gif_family == 0)
290 goto done;
291
292 switch (proto) {
293 #ifdef INET
294 case IPPROTO_IPV4:
295 #endif
296 #ifdef INET6
297 case IPPROTO_IPV6:
298 #endif
299 case IPPROTO_ETHERIP:
300 break;
301 default:
302 goto done;
303 }
304
305 /* Bail on short packets */
306 M_ASSERTPKTHDR(m);
307 if (m->m_pkthdr.len < sizeof(struct ip))
308 goto done;
309
310 ip = mtod(m, const struct ip *);
311 switch (ip->ip_v) {
312 #ifdef INET
313 case 4:
314 if (sc->gif_family != AF_INET)
315 goto done;
316 ret = in_gif_encapcheck(m, off, proto, arg);
317 break;
318 #endif
319 #ifdef INET6
320 case 6:
321 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
322 goto done;
323 if (sc->gif_family != AF_INET6)
324 goto done;
325 ret = in6_gif_encapcheck(m, off, proto, arg);
326 break;
327 #endif
328 }
329 done:
330 GIF_RUNLOCK(sc);
331 return (ret);
332 }
333
334 static int
335 gif_transmit(struct ifnet *ifp, struct mbuf *m)
336 {
337 struct gif_softc *sc;
338 struct etherip_header *eth;
339 #ifdef INET
340 struct ip *ip;
341 #endif
342 #ifdef INET6
343 struct ip6_hdr *ip6;
344 uint32_t t;
345 #endif
346 uint32_t af;
347 uint8_t proto, ecn;
348 int error;
349
350 #ifdef MAC
351 error = mac_ifnet_check_transmit(ifp, m);
352 if (error) {
353 m_freem(m);
354 goto err;
355 }
356 #endif
357 error = ENETDOWN;
358 sc = ifp->if_softc;
359 if ((ifp->if_flags & IFF_MONITOR) != 0 ||
360 (ifp->if_flags & IFF_UP) == 0 ||
361 sc->gif_family == 0 ||
362 (error = gif_check_nesting(ifp, m)) != 0) {
363 m_freem(m);
364 goto err;
365 }
366 /* Now pull back the af that we stashed in the csum_data. */
367 if (ifp->if_bridge)
368 af = AF_LINK;
369 else
370 af = m->m_pkthdr.csum_data;
371 m->m_flags &= ~(M_BCAST|M_MCAST);
372 M_SETFIB(m, sc->gif_fibnum);
373 BPF_MTAP2(ifp, &af, sizeof(af), m);
374 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
375 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
376 /* inner AF-specific encapsulation */
377 ecn = 0;
378 switch (af) {
379 #ifdef INET
380 case AF_INET:
381 proto = IPPROTO_IPV4;
382 if (m->m_len < sizeof(struct ip))
383 m = m_pullup(m, sizeof(struct ip));
384 if (m == NULL) {
385 error = ENOBUFS;
386 goto err;
387 }
388 ip = mtod(m, struct ip *);
389 ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
390 ECN_NOCARE, &ecn, &ip->ip_tos);
391 break;
392 #endif
393 #ifdef INET6
394 case AF_INET6:
395 proto = IPPROTO_IPV6;
396 if (m->m_len < sizeof(struct ip6_hdr))
397 m = m_pullup(m, sizeof(struct ip6_hdr));
398 if (m == NULL) {
399 error = ENOBUFS;
400 goto err;
401 }
402 t = 0;
403 ip6 = mtod(m, struct ip6_hdr *);
404 ip6_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
405 ECN_NOCARE, &t, &ip6->ip6_flow);
406 ecn = (ntohl(t) >> 20) & 0xff;
407 break;
408 #endif
409 case AF_LINK:
410 proto = IPPROTO_ETHERIP;
411 M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT);
412 if (m == NULL) {
413 error = ENOBUFS;
414 goto err;
415 }
416 eth = mtod(m, struct etherip_header *);
417 eth->eip_resvh = 0;
418 eth->eip_ver = ETHERIP_VERSION;
419 eth->eip_resvl = 0;
420 break;
421 default:
422 error = EAFNOSUPPORT;
423 m_freem(m);
424 goto err;
425 }
426 /* XXX should we check if our outer source is legal? */
427 /* dispatch to output logic based on outer AF */
428 switch (sc->gif_family) {
429 #ifdef INET
430 case AF_INET:
431 error = in_gif_output(ifp, m, proto, ecn);
432 break;
433 #endif
434 #ifdef INET6
435 case AF_INET6:
436 error = in6_gif_output(ifp, m, proto, ecn);
437 break;
438 #endif
439 default:
440 m_freem(m);
441 }
442 err:
443 if (error)
444 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
445 return (error);
446 }
447
448 static void
449 gif_qflush(struct ifnet *ifp __unused)
450 {
451
452 }
453
454 #define MTAG_GIF 1080679712
455 static int
456 gif_check_nesting(struct ifnet *ifp, struct mbuf *m)
457 {
458 struct m_tag *mtag;
459 int count;
460
461 /*
462 * gif may cause infinite recursion calls when misconfigured.
463 * We'll prevent this by detecting loops.
464 *
465 * High nesting level may cause stack exhaustion.
466 * We'll prevent this by introducing upper limit.
467 */
468 count = 1;
469 mtag = NULL;
470 while ((mtag = m_tag_locate(m, MTAG_GIF, 0, mtag)) != NULL) {
471 if (*(struct ifnet **)(mtag + 1) == ifp) {
472 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
473 return (EIO);
474 }
475 count++;
476 }
477 if (count > V_max_gif_nesting) {
478 log(LOG_NOTICE,
479 "%s: if_output recursively called too many times(%d)\n",
480 if_name(ifp), count);
481 return (EIO);
482 }
483 mtag = m_tag_alloc(MTAG_GIF, 0, sizeof(struct ifnet *), M_NOWAIT);
484 if (mtag == NULL)
485 return (ENOMEM);
486 *(struct ifnet **)(mtag + 1) = ifp;
487 m_tag_prepend(m, mtag);
488 return (0);
489 }
490
491 int
492 gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
493 struct route *ro)
494 {
495 uint32_t af;
496
497 if (dst->sa_family == AF_UNSPEC)
498 bcopy(dst->sa_data, &af, sizeof(af));
499 else
500 af = dst->sa_family;
501 /*
502 * Now save the af in the inbound pkt csum data, this is a cheat since
503 * we are using the inbound csum_data field to carry the af over to
504 * the gif_transmit() routine, avoiding using yet another mtag.
505 */
506 m->m_pkthdr.csum_data = af;
507 return (ifp->if_transmit(ifp, m));
508 }
509
510 void
511 gif_input(struct mbuf *m, struct ifnet *ifp, int proto, uint8_t ecn)
512 {
513 struct etherip_header *eip;
514 #ifdef INET
515 struct ip *ip;
516 #endif
517 #ifdef INET6
518 struct ip6_hdr *ip6;
519 uint32_t t;
520 #endif
521 struct gif_softc *sc;
522 struct ether_header *eh;
523 struct ifnet *oldifp;
524 int isr, n, af;
525
526 if (ifp == NULL) {
527 /* just in case */
528 m_freem(m);
529 return;
530 }
531 sc = ifp->if_softc;
532 m->m_pkthdr.rcvif = ifp;
533 m_clrprotoflags(m);
534 switch (proto) {
535 #ifdef INET
536 case IPPROTO_IPV4:
537 af = AF_INET;
538 if (m->m_len < sizeof(struct ip))
539 m = m_pullup(m, sizeof(struct ip));
540 if (m == NULL)
541 goto drop;
542 ip = mtod(m, struct ip *);
543 if (ip_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
544 ECN_NOCARE, &ecn, &ip->ip_tos) == 0) {
545 m_freem(m);
546 goto drop;
547 }
548 break;
549 #endif
550 #ifdef INET6
551 case IPPROTO_IPV6:
552 af = AF_INET6;
553 if (m->m_len < sizeof(struct ip6_hdr))
554 m = m_pullup(m, sizeof(struct ip6_hdr));
555 if (m == NULL)
556 goto drop;
557 t = htonl((uint32_t)ecn << 20);
558 ip6 = mtod(m, struct ip6_hdr *);
559 if (ip6_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
560 ECN_NOCARE, &t, &ip6->ip6_flow) == 0) {
561 m_freem(m);
562 goto drop;
563 }
564 break;
565 #endif
566 case IPPROTO_ETHERIP:
567 af = AF_LINK;
568 break;
569 default:
570 m_freem(m);
571 goto drop;
572 }
573
574 #ifdef MAC
575 mac_ifnet_create_mbuf(ifp, m);
576 #endif
577
578 if (bpf_peers_present(ifp->if_bpf)) {
579 uint32_t af1 = af;
580 bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
581 }
582
583 if ((ifp->if_flags & IFF_MONITOR) != 0) {
584 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
585 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
586 m_freem(m);
587 return;
588 }
589
590 if (ng_gif_input_p != NULL) {
591 (*ng_gif_input_p)(ifp, &m, af);
592 if (m == NULL)
593 goto drop;
594 }
595
596 /*
597 * Put the packet to the network layer input queue according to the
598 * specified address family.
599 * Note: older versions of gif_input directly called network layer
600 * input functions, e.g. ip6_input, here. We changed the policy to
601 * prevent too many recursive calls of such input functions, which
602 * might cause kernel panic. But the change may introduce another
603 * problem; if the input queue is full, packets are discarded.
604 * The kernel stack overflow really happened, and we believed
605 * queue-full rarely occurs, so we changed the policy.
606 */
607 switch (af) {
608 #ifdef INET
609 case AF_INET:
610 isr = NETISR_IP;
611 break;
612 #endif
613 #ifdef INET6
614 case AF_INET6:
615 isr = NETISR_IPV6;
616 break;
617 #endif
618 case AF_LINK:
619 n = sizeof(struct etherip_header) + sizeof(struct ether_header);
620 if (n > m->m_len)
621 m = m_pullup(m, n);
622 if (m == NULL)
623 goto drop;
624 eip = mtod(m, struct etherip_header *);
625 if (eip->eip_ver != ETHERIP_VERSION) {
626 /* discard unknown versions */
627 m_freem(m);
628 goto drop;
629 }
630 m_adj(m, sizeof(struct etherip_header));
631
632 m->m_flags &= ~(M_BCAST|M_MCAST);
633 m->m_pkthdr.rcvif = ifp;
634
635 if (ifp->if_bridge) {
636 oldifp = ifp;
637 eh = mtod(m, struct ether_header *);
638 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
639 if (ETHER_IS_BROADCAST(eh->ether_dhost))
640 m->m_flags |= M_BCAST;
641 else
642 m->m_flags |= M_MCAST;
643 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
644 }
645 BRIDGE_INPUT(ifp, m);
646
647 if (m != NULL && ifp != oldifp) {
648 /*
649 * The bridge gave us back itself or one of the
650 * members for which the frame is addressed.
651 */
652 ether_demux(ifp, m);
653 return;
654 }
655 }
656 if (m != NULL)
657 m_freem(m);
658 return;
659
660 default:
661 if (ng_gif_input_orphan_p != NULL)
662 (*ng_gif_input_orphan_p)(ifp, m, af);
663 else
664 m_freem(m);
665 return;
666 }
667
668 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
669 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
670 M_SETFIB(m, ifp->if_fib);
671 netisr_dispatch(isr, m);
672 return;
673 drop:
674 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
675 }
676
677 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
678 int
679 gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
680 {
681 GIF_RLOCK_TRACKER;
682 struct ifreq *ifr = (struct ifreq*)data;
683 struct sockaddr *dst, *src;
684 struct gif_softc *sc;
685 #ifdef INET
686 struct sockaddr_in *sin = NULL;
687 #endif
688 #ifdef INET6
689 struct sockaddr_in6 *sin6 = NULL;
690 #endif
691 u_int options;
692 int error;
693
694 switch (cmd) {
695 case SIOCSIFADDR:
696 ifp->if_flags |= IFF_UP;
697 case SIOCADDMULTI:
698 case SIOCDELMULTI:
699 case SIOCGIFMTU:
700 case SIOCSIFFLAGS:
701 return (0);
702 case SIOCSIFMTU:
703 if (ifr->ifr_mtu < GIF_MTU_MIN ||
704 ifr->ifr_mtu > GIF_MTU_MAX)
705 return (EINVAL);
706 else
707 ifp->if_mtu = ifr->ifr_mtu;
708 return (0);
709 }
710 sx_xlock(&gif_ioctl_sx);
711 sc = ifp->if_softc;
712 if (sc == NULL) {
713 error = ENXIO;
714 goto bad;
715 }
716 error = 0;
717 switch (cmd) {
718 case SIOCSIFPHYADDR:
719 #ifdef INET6
720 case SIOCSIFPHYADDR_IN6:
721 #endif
722 error = EINVAL;
723 switch (cmd) {
724 #ifdef INET
725 case SIOCSIFPHYADDR:
726 src = (struct sockaddr *)
727 &(((struct in_aliasreq *)data)->ifra_addr);
728 dst = (struct sockaddr *)
729 &(((struct in_aliasreq *)data)->ifra_dstaddr);
730 break;
731 #endif
732 #ifdef INET6
733 case SIOCSIFPHYADDR_IN6:
734 src = (struct sockaddr *)
735 &(((struct in6_aliasreq *)data)->ifra_addr);
736 dst = (struct sockaddr *)
737 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
738 break;
739 #endif
740 default:
741 goto bad;
742 }
743 /* sa_family must be equal */
744 if (src->sa_family != dst->sa_family ||
745 src->sa_len != dst->sa_len)
746 goto bad;
747
748 /* validate sa_len */
749 /* check sa_family looks sane for the cmd */
750 switch (src->sa_family) {
751 #ifdef INET
752 case AF_INET:
753 if (src->sa_len != sizeof(struct sockaddr_in))
754 goto bad;
755 if (cmd != SIOCSIFPHYADDR) {
756 error = EAFNOSUPPORT;
757 goto bad;
758 }
759 if (satosin(src)->sin_addr.s_addr == INADDR_ANY ||
760 satosin(dst)->sin_addr.s_addr == INADDR_ANY) {
761 error = EADDRNOTAVAIL;
762 goto bad;
763 }
764 break;
765 #endif
766 #ifdef INET6
767 case AF_INET6:
768 if (src->sa_len != sizeof(struct sockaddr_in6))
769 goto bad;
770 if (cmd != SIOCSIFPHYADDR_IN6) {
771 error = EAFNOSUPPORT;
772 goto bad;
773 }
774 error = EADDRNOTAVAIL;
775 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr)
776 ||
777 IN6_IS_ADDR_UNSPECIFIED(&satosin6(dst)->sin6_addr))
778 goto bad;
779 /*
780 * Check validity of the scope zone ID of the
781 * addresses, and convert it into the kernel
782 * internal form if necessary.
783 */
784 error = sa6_embedscope(satosin6(src), 0);
785 if (error != 0)
786 goto bad;
787 error = sa6_embedscope(satosin6(dst), 0);
788 if (error != 0)
789 goto bad;
790 break;
791 #endif
792 default:
793 error = EAFNOSUPPORT;
794 goto bad;
795 }
796 error = gif_set_tunnel(ifp, src, dst);
797 break;
798 case SIOCDIFPHYADDR:
799 gif_delete_tunnel(ifp);
800 break;
801 case SIOCGIFPSRCADDR:
802 case SIOCGIFPDSTADDR:
803 #ifdef INET6
804 case SIOCGIFPSRCADDR_IN6:
805 case SIOCGIFPDSTADDR_IN6:
806 #endif
807 if (sc->gif_family == 0) {
808 error = EADDRNOTAVAIL;
809 break;
810 }
811 GIF_RLOCK(sc);
812 switch (cmd) {
813 #ifdef INET
814 case SIOCGIFPSRCADDR:
815 case SIOCGIFPDSTADDR:
816 if (sc->gif_family != AF_INET) {
817 error = EADDRNOTAVAIL;
818 break;
819 }
820 sin = (struct sockaddr_in *)&ifr->ifr_addr;
821 memset(sin, 0, sizeof(*sin));
822 sin->sin_family = AF_INET;
823 sin->sin_len = sizeof(*sin);
824 break;
825 #endif
826 #ifdef INET6
827 case SIOCGIFPSRCADDR_IN6:
828 case SIOCGIFPDSTADDR_IN6:
829 if (sc->gif_family != AF_INET6) {
830 error = EADDRNOTAVAIL;
831 break;
832 }
833 sin6 = (struct sockaddr_in6 *)
834 &(((struct in6_ifreq *)data)->ifr_addr);
835 memset(sin6, 0, sizeof(*sin6));
836 sin6->sin6_family = AF_INET6;
837 sin6->sin6_len = sizeof(*sin6);
838 break;
839 #endif
840 default:
841 error = EAFNOSUPPORT;
842 }
843 if (error == 0) {
844 switch (cmd) {
845 #ifdef INET
846 case SIOCGIFPSRCADDR:
847 sin->sin_addr = sc->gif_iphdr->ip_src;
848 break;
849 case SIOCGIFPDSTADDR:
850 sin->sin_addr = sc->gif_iphdr->ip_dst;
851 break;
852 #endif
853 #ifdef INET6
854 case SIOCGIFPSRCADDR_IN6:
855 sin6->sin6_addr = sc->gif_ip6hdr->ip6_src;
856 break;
857 case SIOCGIFPDSTADDR_IN6:
858 sin6->sin6_addr = sc->gif_ip6hdr->ip6_dst;
859 break;
860 #endif
861 }
862 }
863 GIF_RUNLOCK(sc);
864 if (error != 0)
865 break;
866 switch (cmd) {
867 #ifdef INET
868 case SIOCGIFPSRCADDR:
869 case SIOCGIFPDSTADDR:
870 error = prison_if(curthread->td_ucred,
871 (struct sockaddr *)sin);
872 if (error != 0)
873 memset(sin, 0, sizeof(*sin));
874 break;
875 #endif
876 #ifdef INET6
877 case SIOCGIFPSRCADDR_IN6:
878 case SIOCGIFPDSTADDR_IN6:
879 error = prison_if(curthread->td_ucred,
880 (struct sockaddr *)sin6);
881 if (error == 0)
882 error = sa6_recoverscope(sin6);
883 if (error != 0)
884 memset(sin6, 0, sizeof(*sin6));
885 #endif
886 }
887 break;
888 case SIOCGTUNFIB:
889 ifr->ifr_fib = sc->gif_fibnum;
890 break;
891 case SIOCSTUNFIB:
892 if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
893 break;
894 if (ifr->ifr_fib >= rt_numfibs)
895 error = EINVAL;
896 else
897 sc->gif_fibnum = ifr->ifr_fib;
898 break;
899 case GIFGOPTS:
900 options = sc->gif_options;
901 error = copyout(&options, ifr_data_get_ptr(ifr),
902 sizeof(options));
903 break;
904 case GIFSOPTS:
905 if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
906 break;
907 error = copyin(ifr_data_get_ptr(ifr), &options,
908 sizeof(options));
909 if (error)
910 break;
911 if (options & ~GIF_OPTMASK)
912 error = EINVAL;
913 else
914 sc->gif_options = options;
915 break;
916 default:
917 error = EINVAL;
918 break;
919 }
920 bad:
921 sx_xunlock(&gif_ioctl_sx);
922 return (error);
923 }
924
925 static void
926 gif_detach(struct gif_softc *sc)
927 {
928
929 sx_assert(&gif_ioctl_sx, SA_XLOCKED);
930 if (sc->gif_ecookie != NULL)
931 encap_detach(sc->gif_ecookie);
932 sc->gif_ecookie = NULL;
933 }
934
935 static int
936 gif_attach(struct gif_softc *sc, int af)
937 {
938
939 sx_assert(&gif_ioctl_sx, SA_XLOCKED);
940 switch (af) {
941 #ifdef INET
942 case AF_INET:
943 return (in_gif_attach(sc));
944 #endif
945 #ifdef INET6
946 case AF_INET6:
947 return (in6_gif_attach(sc));
948 #endif
949 }
950 return (EAFNOSUPPORT);
951 }
952
953 static int
954 gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
955 {
956 struct gif_softc *sc = ifp->if_softc;
957 struct gif_softc *tsc;
958 #ifdef INET
959 struct ip *ip;
960 #endif
961 #ifdef INET6
962 struct ip6_hdr *ip6;
963 #endif
964 void *hdr;
965 int error = 0;
966
967 if (sc == NULL)
968 return (ENXIO);
969 /* Disallow parallel tunnels unless instructed otherwise. */
970 if (V_parallel_tunnels == 0) {
971 GIF_LIST_LOCK();
972 LIST_FOREACH(tsc, &V_gif_softc_list, gif_list) {
973 if (tsc == sc || tsc->gif_family != src->sa_family)
974 continue;
975 #ifdef INET
976 if (tsc->gif_family == AF_INET &&
977 tsc->gif_iphdr->ip_src.s_addr ==
978 satosin(src)->sin_addr.s_addr &&
979 tsc->gif_iphdr->ip_dst.s_addr ==
980 satosin(dst)->sin_addr.s_addr) {
981 error = EADDRNOTAVAIL;
982 GIF_LIST_UNLOCK();
983 goto bad;
984 }
985 #endif
986 #ifdef INET6
987 if (tsc->gif_family == AF_INET6 &&
988 IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_src,
989 &satosin6(src)->sin6_addr) &&
990 IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_dst,
991 &satosin6(dst)->sin6_addr)) {
992 error = EADDRNOTAVAIL;
993 GIF_LIST_UNLOCK();
994 goto bad;
995 }
996 #endif
997 }
998 GIF_LIST_UNLOCK();
999 }
1000 switch (src->sa_family) {
1001 #ifdef INET
1002 case AF_INET:
1003 hdr = ip = malloc(sizeof(struct ip), M_GIF,
1004 M_WAITOK | M_ZERO);
1005 ip->ip_src.s_addr = satosin(src)->sin_addr.s_addr;
1006 ip->ip_dst.s_addr = satosin(dst)->sin_addr.s_addr;
1007 break;
1008 #endif
1009 #ifdef INET6
1010 case AF_INET6:
1011 hdr = ip6 = malloc(sizeof(struct ip6_hdr), M_GIF,
1012 M_WAITOK | M_ZERO);
1013 ip6->ip6_src = satosin6(src)->sin6_addr;
1014 ip6->ip6_dst = satosin6(dst)->sin6_addr;
1015 ip6->ip6_vfc = IPV6_VERSION;
1016 break;
1017 #endif
1018 default:
1019 return (EAFNOSUPPORT);
1020 }
1021
1022 if (sc->gif_family != src->sa_family)
1023 gif_detach(sc);
1024 if (sc->gif_family == 0 ||
1025 sc->gif_family != src->sa_family)
1026 error = gif_attach(sc, src->sa_family);
1027
1028 GIF_WLOCK(sc);
1029 if (sc->gif_family != 0)
1030 free(sc->gif_hdr, M_GIF);
1031 sc->gif_family = src->sa_family;
1032 sc->gif_hdr = hdr;
1033 GIF_WUNLOCK(sc);
1034 #if defined(INET) || defined(INET6)
1035 bad:
1036 #endif
1037 if (error == 0 && sc->gif_family != 0) {
1038 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1039 if_link_state_change(ifp, LINK_STATE_UP);
1040 } else {
1041 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1042 if_link_state_change(ifp, LINK_STATE_DOWN);
1043 }
1044 return (error);
1045 }
1046
1047 static void
1048 gif_delete_tunnel(struct ifnet *ifp)
1049 {
1050 struct gif_softc *sc = ifp->if_softc;
1051 int family;
1052
1053 if (sc == NULL)
1054 return;
1055
1056 GIF_WLOCK(sc);
1057 family = sc->gif_family;
1058 sc->gif_family = 0;
1059 GIF_WUNLOCK(sc);
1060 if (family != 0) {
1061 gif_detach(sc);
1062 free(sc->gif_hdr, M_GIF);
1063 }
1064 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1065 if_link_state_change(ifp, LINK_STATE_DOWN);
1066 }
Cache object: accf84db7217e41321d8a4b7b001451e
|