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: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/netinet6/in6_gif.c 276215 2014-12-25 21:32:37Z ae $");
34
35#include "opt_inet.h"
36#include "opt_inet6.h"
37
38#include <sys/param.h>
39#include <sys/lock.h>
40#include <sys/rmlock.h>
41#include <sys/systm.h>
42#include <sys/socket.h>
43#include <sys/sockio.h>
44#include <sys/mbuf.h>
45#include <sys/errno.h>
46#include <sys/kernel.h>
47#include <sys/queue.h>
48#include <sys/syslog.h>
49#include <sys/sysctl.h>
50#include <sys/protosw.h>
51#include <sys/malloc.h>
52
53#include <net/if.h>
54#include <net/if_var.h>
55#include <net/route.h>
56#include <net/vnet.h>
57
58#include <netinet/in.h>
59#include <netinet/in_systm.h>
60#ifdef INET
61#include <netinet/ip.h>
62#endif
63#include <netinet/ip_encap.h>
64#ifdef INET6
65#include <netinet/ip6.h>
66#include <netinet6/ip6_var.h>
67#include <netinet6/in6_var.h>
68#endif
69#include <netinet/ip_ecn.h>
70#ifdef INET6
71#include <netinet6/ip6_ecn.h>
72#endif
73
74#include <net/if_gif.h>
75
76#define GIF_HLIM 30
77static VNET_DEFINE(int, ip6_gif_hlim) = GIF_HLIM;
78#define V_ip6_gif_hlim VNET(ip6_gif_hlim)
79
80SYSCTL_DECL(_net_inet6_ip6);
81SYSCTL_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM, gifhlim, CTLFLAG_VNET | CTLFLAG_RW,
82 &VNET_NAME(ip6_gif_hlim), 0, "");
83
84static int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
85 struct ifnet *);
86static int in6_gif_input(struct mbuf **, int *, int);
87
88extern struct domain inet6domain;
89static struct protosw in6_gif_protosw = {
90 .pr_type = SOCK_RAW,
91 .pr_domain = &inet6domain,
92 .pr_protocol = 0, /* IPPROTO_IPV[46] */
93 .pr_flags = PR_ATOMIC|PR_ADDR,
94 .pr_input = in6_gif_input,
95 .pr_output = rip6_output,
96 .pr_ctloutput = rip6_ctloutput,
97 .pr_usrreqs = &rip6_usrreqs
98};
99
100int
101in6_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
102{
103 GIF_RLOCK_TRACKER;
104 struct gif_softc *sc = ifp->if_softc;
105 struct ip6_hdr *ip6;
106 int len;
107
108 /* prepend new IP header */
109 len = sizeof(struct ip6_hdr);
110#ifndef __NO_STRICT_ALIGNMENT
111 if (proto == IPPROTO_ETHERIP)
112 len += ETHERIP_ALIGN;
113#endif
114 M_PREPEND(m, len, M_NOWAIT);
115 if (m == NULL)
116 return (ENOBUFS);
117#ifndef __NO_STRICT_ALIGNMENT
118 if (proto == IPPROTO_ETHERIP) {
119 len = mtod(m, vm_offset_t) & 3;
120 KASSERT(len == 0 || len == ETHERIP_ALIGN,
121 ("in6_gif_output: unexpected misalignment"));
122 m->m_data += len;
123 m->m_len -= ETHERIP_ALIGN;
124 }
125#endif
126
127 ip6 = mtod(m, struct ip6_hdr *);
128 GIF_RLOCK(sc);
129 if (sc->gif_family != AF_INET6) {
130 m_freem(m);
131 GIF_RUNLOCK(sc);
132 return (ENETDOWN);
133 }
134 bcopy(sc->gif_ip6hdr, ip6, sizeof(struct ip6_hdr));
135 GIF_RUNLOCK(sc);
136
137 ip6->ip6_flow |= htonl((uint32_t)ecn << 20);
138 ip6->ip6_nxt = proto;
139 ip6->ip6_hlim = V_ip6_gif_hlim;
140 /*
141 * force fragmentation to minimum MTU, to avoid path MTU discovery.
142 * it is too painful to ask for resend of inner packet, to achieve
143 * path MTU discovery for encapsulated packets.
144 */
145 return (ip6_output(m, 0, NULL, IPV6_MINMTU, 0, NULL, NULL));
146}
147
148static int
149in6_gif_input(struct mbuf **mp, int *offp, int proto)
150{
151 struct mbuf *m = *mp;
152 struct ifnet *gifp;
153 struct gif_softc *sc;
154 struct ip6_hdr *ip6;
155 uint8_t ecn;
156
157 sc = encap_getarg(m);
158 if (sc == NULL) {
159 m_freem(m);
160 IP6STAT_INC(ip6s_nogif);
161 return (IPPROTO_DONE);
162 }
163 gifp = GIF2IFP(sc);
164 if ((gifp->if_flags & IFF_UP) != 0) {
165 ip6 = mtod(m, struct ip6_hdr *);
166 ecn = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
167 m_adj(m, *offp);
168 gif_input(m, gifp, proto, ecn);
169 } else {
170 m_freem(m);
171 IP6STAT_INC(ip6s_nogif);
172 }
173 return (IPPROTO_DONE);
174}
175
176/*
177 * validate outer address.
178 */
179static int
180gif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
181 struct ifnet *ifp)
182{
183
184 GIF_RLOCK_ASSERT(sc);
185 /*
186 * Check for address match. Note that the check is for an incoming
187 * packet. We should compare the *source* address in our configuration
188 * and the *destination* address of the packet, and vice versa.
189 */
190 if (!IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src, &ip6->ip6_dst) ||
191 !IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst, &ip6->ip6_src))
192 return (0);
193
194 /* martian filters on outer source - done in ip6_input */
195
196 /* ingress filters on outer source */
197 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
198 struct sockaddr_in6 sin6;
199 struct rtentry *rt;
200
201 bzero(&sin6, sizeof(sin6));
202 sin6.sin6_family = AF_INET6;
203 sin6.sin6_len = sizeof(struct sockaddr_in6);
204 sin6.sin6_addr = ip6->ip6_src;
205 sin6.sin6_scope_id = 0; /* XXX */
206
207 rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, 0UL,
208 sc->gif_fibnum);
209 if (!rt || rt->rt_ifp != ifp) {
210 if (rt)
211 RTFREE_LOCKED(rt);
212 return (0);
213 }
214 RTFREE_LOCKED(rt);
215 }
216
217 return (128 * 2);
218}
219
220/*
221 * we know that we are in IFF_UP, outer address available, and outer family
222 * matched the physical addr family. see gif_encapcheck().
223 */
224int
225in6_gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
226{
227 struct ip6_hdr ip6;
228 struct gif_softc *sc;
229 struct ifnet *ifp;
230
231 /* sanity check done in caller */
232 sc = (struct gif_softc *)arg;
233 GIF_RLOCK_ASSERT(sc);
234
235 m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
236 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
237 return (gif_validate6(&ip6, sc, ifp));
238}
239
240int
241in6_gif_attach(struct gif_softc *sc)
242{
243
244 KASSERT(sc->gif_ecookie == NULL, ("gif_ecookie isn't NULL"));
245 sc->gif_ecookie = encap_attach_func(AF_INET6, -1, gif_encapcheck,
246 (void *)&in6_gif_protosw, sc);
247 if (sc->gif_ecookie == NULL)
248 return (EEXIST);
249 return (0);
250}
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: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/netinet6/in6_gif.c 276215 2014-12-25 21:32:37Z ae $");
34
35#include "opt_inet.h"
36#include "opt_inet6.h"
37
38#include <sys/param.h>
39#include <sys/lock.h>
40#include <sys/rmlock.h>
41#include <sys/systm.h>
42#include <sys/socket.h>
43#include <sys/sockio.h>
44#include <sys/mbuf.h>
45#include <sys/errno.h>
46#include <sys/kernel.h>
47#include <sys/queue.h>
48#include <sys/syslog.h>
49#include <sys/sysctl.h>
50#include <sys/protosw.h>
51#include <sys/malloc.h>
52
53#include <net/if.h>
54#include <net/if_var.h>
55#include <net/route.h>
56#include <net/vnet.h>
57
58#include <netinet/in.h>
59#include <netinet/in_systm.h>
60#ifdef INET
61#include <netinet/ip.h>
62#endif
63#include <netinet/ip_encap.h>
64#ifdef INET6
65#include <netinet/ip6.h>
66#include <netinet6/ip6_var.h>
67#include <netinet6/in6_var.h>
68#endif
69#include <netinet/ip_ecn.h>
70#ifdef INET6
71#include <netinet6/ip6_ecn.h>
72#endif
73
74#include <net/if_gif.h>
75
76#define GIF_HLIM 30
77static VNET_DEFINE(int, ip6_gif_hlim) = GIF_HLIM;
78#define V_ip6_gif_hlim VNET(ip6_gif_hlim)
79
80SYSCTL_DECL(_net_inet6_ip6);
81SYSCTL_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM, gifhlim, CTLFLAG_VNET | CTLFLAG_RW,
82 &VNET_NAME(ip6_gif_hlim), 0, "");
83
84static int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
85 struct ifnet *);
86static int in6_gif_input(struct mbuf **, int *, int);
87
88extern struct domain inet6domain;
89static struct protosw in6_gif_protosw = {
90 .pr_type = SOCK_RAW,
91 .pr_domain = &inet6domain,
92 .pr_protocol = 0, /* IPPROTO_IPV[46] */
93 .pr_flags = PR_ATOMIC|PR_ADDR,
94 .pr_input = in6_gif_input,
95 .pr_output = rip6_output,
96 .pr_ctloutput = rip6_ctloutput,
97 .pr_usrreqs = &rip6_usrreqs
98};
99
100int
101in6_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
102{
103 GIF_RLOCK_TRACKER;
104 struct gif_softc *sc = ifp->if_softc;
105 struct ip6_hdr *ip6;
106 int len;
107
108 /* prepend new IP header */
109 len = sizeof(struct ip6_hdr);
110#ifndef __NO_STRICT_ALIGNMENT
111 if (proto == IPPROTO_ETHERIP)
112 len += ETHERIP_ALIGN;
113#endif
114 M_PREPEND(m, len, M_NOWAIT);
115 if (m == NULL)
116 return (ENOBUFS);
117#ifndef __NO_STRICT_ALIGNMENT
118 if (proto == IPPROTO_ETHERIP) {
119 len = mtod(m, vm_offset_t) & 3;
120 KASSERT(len == 0 || len == ETHERIP_ALIGN,
121 ("in6_gif_output: unexpected misalignment"));
122 m->m_data += len;
123 m->m_len -= ETHERIP_ALIGN;
124 }
125#endif
126
127 ip6 = mtod(m, struct ip6_hdr *);
128 GIF_RLOCK(sc);
129 if (sc->gif_family != AF_INET6) {
130 m_freem(m);
131 GIF_RUNLOCK(sc);
132 return (ENETDOWN);
133 }
134 bcopy(sc->gif_ip6hdr, ip6, sizeof(struct ip6_hdr));
135 GIF_RUNLOCK(sc);
136
137 ip6->ip6_flow |= htonl((uint32_t)ecn << 20);
138 ip6->ip6_nxt = proto;
139 ip6->ip6_hlim = V_ip6_gif_hlim;
140 /*
141 * force fragmentation to minimum MTU, to avoid path MTU discovery.
142 * it is too painful to ask for resend of inner packet, to achieve
143 * path MTU discovery for encapsulated packets.
144 */
145 return (ip6_output(m, 0, NULL, IPV6_MINMTU, 0, NULL, NULL));
146}
147
148static int
149in6_gif_input(struct mbuf **mp, int *offp, int proto)
150{
151 struct mbuf *m = *mp;
152 struct ifnet *gifp;
153 struct gif_softc *sc;
154 struct ip6_hdr *ip6;
155 uint8_t ecn;
156
157 sc = encap_getarg(m);
158 if (sc == NULL) {
159 m_freem(m);
160 IP6STAT_INC(ip6s_nogif);
161 return (IPPROTO_DONE);
162 }
163 gifp = GIF2IFP(sc);
164 if ((gifp->if_flags & IFF_UP) != 0) {
165 ip6 = mtod(m, struct ip6_hdr *);
166 ecn = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
167 m_adj(m, *offp);
168 gif_input(m, gifp, proto, ecn);
169 } else {
170 m_freem(m);
171 IP6STAT_INC(ip6s_nogif);
172 }
173 return (IPPROTO_DONE);
174}
175
176/*
177 * validate outer address.
178 */
179static int
180gif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
181 struct ifnet *ifp)
182{
183
184 GIF_RLOCK_ASSERT(sc);
185 /*
186 * Check for address match. Note that the check is for an incoming
187 * packet. We should compare the *source* address in our configuration
188 * and the *destination* address of the packet, and vice versa.
189 */
190 if (!IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src, &ip6->ip6_dst) ||
191 !IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst, &ip6->ip6_src))
192 return (0);
193
194 /* martian filters on outer source - done in ip6_input */
195
196 /* ingress filters on outer source */
197 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
198 struct sockaddr_in6 sin6;
199 struct rtentry *rt;
200
201 bzero(&sin6, sizeof(sin6));
202 sin6.sin6_family = AF_INET6;
203 sin6.sin6_len = sizeof(struct sockaddr_in6);
204 sin6.sin6_addr = ip6->ip6_src;
205 sin6.sin6_scope_id = 0; /* XXX */
206
207 rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, 0UL,
208 sc->gif_fibnum);
209 if (!rt || rt->rt_ifp != ifp) {
210 if (rt)
211 RTFREE_LOCKED(rt);
212 return (0);
213 }
214 RTFREE_LOCKED(rt);
215 }
216
217 return (128 * 2);
218}
219
220/*
221 * we know that we are in IFF_UP, outer address available, and outer family
222 * matched the physical addr family. see gif_encapcheck().
223 */
224int
225in6_gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
226{
227 struct ip6_hdr ip6;
228 struct gif_softc *sc;
229 struct ifnet *ifp;
230
231 /* sanity check done in caller */
232 sc = (struct gif_softc *)arg;
233 GIF_RLOCK_ASSERT(sc);
234
235 m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
236 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
237 return (gif_validate6(&ip6, sc, ifp));
238}
239
240int
241in6_gif_attach(struct gif_softc *sc)
242{
243
244 KASSERT(sc->gif_ecookie == NULL, ("gif_ecookie isn't NULL"));
245 sc->gif_ecookie = encap_attach_func(AF_INET6, -1, gif_encapcheck,
246 (void *)&in6_gif_protosw, sc);
247 if (sc->gif_ecookie == NULL)
248 return (EEXIST);
249 return (0);
250}