if_stf.c (46d0f824bec21e582b8b0df2fc6f124eff6aac18) if_stf.c (6d8fdfa9d5e7d4871c5039b0131829f9cbefeee9)
1/* $FreeBSD$ */
2/* $KAME: if_stf.c,v 1.73 2001/12/03 11:08:30 keiichi Exp $ */
3
4/*-
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Copyright (C) 2000 WIDE Project.
8 * All rights reserved.

--- 71 unchanged lines hidden (view full) ---

80#include <sys/systm.h>
81#include <sys/socket.h>
82#include <sys/sockio.h>
83#include <sys/mbuf.h>
84#include <sys/errno.h>
85#include <sys/kernel.h>
86#include <sys/lock.h>
87#include <sys/module.h>
1/* $FreeBSD$ */
2/* $KAME: if_stf.c,v 1.73 2001/12/03 11:08:30 keiichi Exp $ */
3
4/*-
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Copyright (C) 2000 WIDE Project.
8 * All rights reserved.

--- 71 unchanged lines hidden (view full) ---

80#include <sys/systm.h>
81#include <sys/socket.h>
82#include <sys/sockio.h>
83#include <sys/mbuf.h>
84#include <sys/errno.h>
85#include <sys/kernel.h>
86#include <sys/lock.h>
87#include <sys/module.h>
88#include <sys/protosw.h>
89#include <sys/proc.h>
90#include <sys/queue.h>
91#include <sys/rmlock.h>
92#include <sys/sysctl.h>
93#include <machine/cpu.h>
94
95#include <sys/malloc.h>
96

--- 49 unchanged lines hidden (view full) ---

146};
147#define STF2IFP(sc) ((sc)->sc_ifp)
148
149static const char stfname[] = "stf";
150
151static MALLOC_DEFINE(M_STF, stfname, "6to4 Tunnel Interface");
152static const int ip_stf_ttl = 40;
153
88#include <sys/proc.h>
89#include <sys/queue.h>
90#include <sys/rmlock.h>
91#include <sys/sysctl.h>
92#include <machine/cpu.h>
93
94#include <sys/malloc.h>
95

--- 49 unchanged lines hidden (view full) ---

145};
146#define STF2IFP(sc) ((sc)->sc_ifp)
147
148static const char stfname[] = "stf";
149
150static MALLOC_DEFINE(M_STF, stfname, "6to4 Tunnel Interface");
151static const int ip_stf_ttl = 40;
152
154extern struct domain inetdomain;
155static int in_stf_input(struct mbuf **, int *, int);
156static struct protosw in_stf_protosw = {
157 .pr_type = SOCK_RAW,
158 .pr_domain = &inetdomain,
159 .pr_protocol = IPPROTO_IPV6,
160 .pr_flags = PR_ATOMIC|PR_ADDR,
161 .pr_input = in_stf_input,
162 .pr_output = rip_output,
163 .pr_ctloutput = rip_ctloutput,
164 .pr_usrreqs = &rip_usrreqs
165};
166
153static int in_stf_input(struct mbuf *, int, int, void *);
167static char *stfnames[] = {"stf0", "stf", "6to4", NULL};
168
169static int stfmodevent(module_t, int, void *);
170static int stf_encapcheck(const struct mbuf *, int, int, void *);
171static int stf_getsrcifa6(struct ifnet *, struct in6_addr *, struct in6_addr *);
172static int stf_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
173 struct route *);
174static int isrfc1918addr(struct in_addr *);
175static int stf_checkaddr4(struct stf_softc *, struct in_addr *,
176 struct ifnet *);
177static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
178 struct ifnet *);
179static int stf_ioctl(struct ifnet *, u_long, caddr_t);
180
181static int stf_clone_match(struct if_clone *, const char *);
182static int stf_clone_create(struct if_clone *, char *, size_t, caddr_t);
183static int stf_clone_destroy(struct if_clone *, struct ifnet *);
184static struct if_clone *stf_cloner;
185
154static char *stfnames[] = {"stf0", "stf", "6to4", NULL};
155
156static int stfmodevent(module_t, int, void *);
157static int stf_encapcheck(const struct mbuf *, int, int, void *);
158static int stf_getsrcifa6(struct ifnet *, struct in6_addr *, struct in6_addr *);
159static int stf_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
160 struct route *);
161static int isrfc1918addr(struct in_addr *);
162static int stf_checkaddr4(struct stf_softc *, struct in_addr *,
163 struct ifnet *);
164static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
165 struct ifnet *);
166static int stf_ioctl(struct ifnet *, u_long, caddr_t);
167
168static int stf_clone_match(struct if_clone *, const char *);
169static int stf_clone_create(struct if_clone *, char *, size_t, caddr_t);
170static int stf_clone_destroy(struct if_clone *, struct ifnet *);
171static struct if_clone *stf_cloner;
172
173static const struct encap_config ipv4_encap_cfg = {
174 .proto = IPPROTO_IPV6,
175 .min_length = sizeof(struct ip),
176 .exact_match = (sizeof(in_addr_t) << 3) + 8,
177 .check = stf_encapcheck,
178 .input = in_stf_input
179};
180
186static int
187stf_clone_match(struct if_clone *ifc, const char *name)
188{
189 int i;
190
191 for(i = 0; stfnames[i] != NULL; i++) {
192 if (strcmp(stfnames[i], name) == 0)
193 return (1);

--- 51 unchanged lines hidden (view full) ---

245 */
246 panic("if_clone_create(): interface name too long");
247 }
248 }
249 strlcpy(ifp->if_xname, name, IFNAMSIZ);
250 ifp->if_dname = stfname;
251 ifp->if_dunit = IF_DUNIT_NONE;
252
181static int
182stf_clone_match(struct if_clone *ifc, const char *name)
183{
184 int i;
185
186 for(i = 0; stfnames[i] != NULL; i++) {
187 if (strcmp(stfnames[i], name) == 0)
188 return (1);

--- 51 unchanged lines hidden (view full) ---

240 */
241 panic("if_clone_create(): interface name too long");
242 }
243 }
244 strlcpy(ifp->if_xname, name, IFNAMSIZ);
245 ifp->if_dname = stfname;
246 ifp->if_dunit = IF_DUNIT_NONE;
247
253 sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6,
254 stf_encapcheck, &in_stf_protosw, sc);
248 sc->encap_cookie = ip_encap_attach(&ipv4_encap_cfg, sc, M_WAITOK);
255 if (sc->encap_cookie == NULL) {
256 if_printf(ifp, "attach failed\n");
257 free(sc, M_STF);
258 ifc_free_unit(ifc, unit);
259 return (ENOMEM);
260 }
261
262 ifp->if_mtu = IPV6_MMTU;

--- 6 unchanged lines hidden (view full) ---

269}
270
271static int
272stf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
273{
274 struct stf_softc *sc = ifp->if_softc;
275 int err __unused;
276
249 if (sc->encap_cookie == NULL) {
250 if_printf(ifp, "attach failed\n");
251 free(sc, M_STF);
252 ifc_free_unit(ifc, unit);
253 return (ENOMEM);
254 }
255
256 ifp->if_mtu = IPV6_MMTU;

--- 6 unchanged lines hidden (view full) ---

263}
264
265static int
266stf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
267{
268 struct stf_softc *sc = ifp->if_softc;
269 int err __unused;
270
277 err = encap_detach(sc->encap_cookie);
271 err = ip_encap_detach(sc->encap_cookie);
278 KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
279 bpfdetach(ifp);
280 if_detach(ifp);
281 if_free(ifp);
282
283 free(sc, M_STF);
284 ifc_free_unit(ifc, STFUNIT);
285

--- 317 unchanged lines hidden (view full) ---

603 */
604 if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
605 return -1;
606
607 return 0;
608}
609
610static int
272 KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
273 bpfdetach(ifp);
274 if_detach(ifp);
275 if_free(ifp);
276
277 free(sc, M_STF);
278 ifc_free_unit(ifc, STFUNIT);
279

--- 317 unchanged lines hidden (view full) ---

597 */
598 if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
599 return -1;
600
601 return 0;
602}
603
604static int
611in_stf_input(struct mbuf **mp, int *offp, int proto)
605in_stf_input(struct mbuf *m, int off, int proto, void *arg)
612{
606{
613 struct stf_softc *sc;
607 struct stf_softc *sc = arg;
614 struct ip *ip;
615 struct ip6_hdr *ip6;
608 struct ip *ip;
609 struct ip6_hdr *ip6;
616 struct mbuf *m;
617 u_int8_t otos, itos;
618 struct ifnet *ifp;
610 u_int8_t otos, itos;
611 struct ifnet *ifp;
619 int off;
620
612
621 m = *mp;
622 off = *offp;
623
624 if (proto != IPPROTO_IPV6) {
625 m_freem(m);
626 return (IPPROTO_DONE);
627 }
628
629 ip = mtod(m, struct ip *);
613 if (proto != IPPROTO_IPV6) {
614 m_freem(m);
615 return (IPPROTO_DONE);
616 }
617
618 ip = mtod(m, struct ip *);
630
631 sc = (struct stf_softc *)encap_getarg(m);
632
633 if (sc == NULL || (STF2IFP(sc)->if_flags & IFF_UP) == 0) {
634 m_freem(m);
635 return (IPPROTO_DONE);
636 }
637
638 ifp = STF2IFP(sc);
639
640#ifdef MAC

--- 34 unchanged lines hidden (view full) ---

675 if ((ifp->if_flags & IFF_LINK1) != 0)
676 ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
677 else
678 ip_ecn_egress(ECN_NOCARE, &otos, &itos);
679 ip6->ip6_flow &= ~htonl(0xff << 20);
680 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
681
682 m->m_pkthdr.rcvif = ifp;
619 if (sc == NULL || (STF2IFP(sc)->if_flags & IFF_UP) == 0) {
620 m_freem(m);
621 return (IPPROTO_DONE);
622 }
623
624 ifp = STF2IFP(sc);
625
626#ifdef MAC

--- 34 unchanged lines hidden (view full) ---

661 if ((ifp->if_flags & IFF_LINK1) != 0)
662 ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
663 else
664 ip_ecn_egress(ECN_NOCARE, &otos, &itos);
665 ip6->ip6_flow &= ~htonl(0xff << 20);
666 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
667
668 m->m_pkthdr.rcvif = ifp;
683
669
684 if (bpf_peers_present(ifp->if_bpf)) {
685 /*
686 * We need to prepend the address family as
687 * a four byte field. Cons up a dummy header
688 * to pacify bpf. This is safe because bpf
689 * will only read from the mbuf (i.e., it won't
690 * try to free it or keep a pointer a to it).
691 */

--- 76 unchanged lines hidden ---
670 if (bpf_peers_present(ifp->if_bpf)) {
671 /*
672 * We need to prepend the address family as
673 * a four byte field. Cons up a dummy header
674 * to pacify bpf. This is safe because bpf
675 * will only read from the mbuf (i.e., it won't
676 * try to free it or keep a pointer a to it).
677 */

--- 76 unchanged lines hidden ---