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