1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
3 | /* $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $ */ |
---|
4 | |
---|
5 | /*- |
---|
6 | * Copyright (c) 1998 The NetBSD Foundation, Inc. |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * This code is derived from software contributed to The NetBSD Foundation |
---|
10 | * by Heiko W.Rupp <hwr@pilhuhn.de> |
---|
11 | * |
---|
12 | * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de> |
---|
13 | * |
---|
14 | * Redistribution and use in source and binary forms, with or without |
---|
15 | * modification, are permitted provided that the following conditions |
---|
16 | * are met: |
---|
17 | * 1. Redistributions of source code must retain the above copyright |
---|
18 | * notice, this list of conditions and the following disclaimer. |
---|
19 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer in the |
---|
21 | * documentation and/or other materials provided with the distribution. |
---|
22 | * |
---|
23 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
---|
24 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
---|
25 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
---|
26 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
---|
27 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
33 | * POSSIBILITY OF SUCH DAMAGE. |
---|
34 | */ |
---|
35 | |
---|
36 | /* |
---|
37 | * deencapsulate tunneled packets and send them on |
---|
38 | * output half is in net/if_gre.[ch] |
---|
39 | * This currently handles IPPROTO_GRE, IPPROTO_MOBILE |
---|
40 | */ |
---|
41 | |
---|
42 | #include <sys/cdefs.h> |
---|
43 | __FBSDID("$FreeBSD$"); |
---|
44 | |
---|
45 | #include <rtems/bsd/local/opt_inet.h> |
---|
46 | #include <rtems/bsd/local/opt_atalk.h> |
---|
47 | #include <rtems/bsd/local/opt_inet6.h> |
---|
48 | |
---|
49 | #include <rtems/bsd/sys/param.h> |
---|
50 | #include <sys/systm.h> |
---|
51 | #include <sys/mbuf.h> |
---|
52 | #include <sys/socket.h> |
---|
53 | #include <sys/socketvar.h> |
---|
54 | #include <sys/protosw.h> |
---|
55 | #include <rtems/bsd/sys/errno.h> |
---|
56 | #include <rtems/bsd/sys/time.h> |
---|
57 | #include <sys/kernel.h> |
---|
58 | #include <sys/syslog.h> |
---|
59 | #include <net/bpf.h> |
---|
60 | #include <net/ethernet.h> |
---|
61 | #include <net/if.h> |
---|
62 | #include <net/netisr.h> |
---|
63 | #include <net/route.h> |
---|
64 | #include <net/raw_cb.h> |
---|
65 | |
---|
66 | #ifdef INET |
---|
67 | #include <netinet/in.h> |
---|
68 | #include <netinet/in_var.h> |
---|
69 | #include <netinet/in_systm.h> |
---|
70 | #include <netinet/ip.h> |
---|
71 | #include <netinet/ip_var.h> |
---|
72 | #include <netinet/ip_gre.h> |
---|
73 | #include <machine/in_cksum.h> |
---|
74 | #else |
---|
75 | #error ip_gre input without IP? |
---|
76 | #endif |
---|
77 | |
---|
78 | #ifdef NETATALK |
---|
79 | #include <netatalk/at.h> |
---|
80 | #include <netatalk/at_var.h> |
---|
81 | #include <netatalk/at_extern.h> |
---|
82 | #endif |
---|
83 | |
---|
84 | /* Needs IP headers. */ |
---|
85 | #include <net/if_gre.h> |
---|
86 | |
---|
87 | #include <machine/stdarg.h> |
---|
88 | |
---|
89 | #if 1 |
---|
90 | void gre_inet_ntoa(struct in_addr in); /* XXX */ |
---|
91 | #endif |
---|
92 | |
---|
93 | static struct gre_softc *gre_lookup(struct mbuf *, u_int8_t); |
---|
94 | |
---|
95 | static struct mbuf *gre_input2(struct mbuf *, int, u_char); |
---|
96 | |
---|
97 | /* |
---|
98 | * De-encapsulate a packet and feed it back through ip input (this |
---|
99 | * routine is called whenever IP gets a packet with proto type |
---|
100 | * IPPROTO_GRE and a local destination address). |
---|
101 | * This really is simple |
---|
102 | */ |
---|
103 | void |
---|
104 | gre_input(struct mbuf *m, int off) |
---|
105 | { |
---|
106 | int proto; |
---|
107 | |
---|
108 | proto = (mtod(m, struct ip *))->ip_p; |
---|
109 | |
---|
110 | m = gre_input2(m, off, proto); |
---|
111 | |
---|
112 | /* |
---|
113 | * If no matching tunnel that is up is found. We inject |
---|
114 | * the mbuf to raw ip socket to see if anyone picks it up. |
---|
115 | */ |
---|
116 | if (m != NULL) |
---|
117 | rip_input(m, off); |
---|
118 | } |
---|
119 | |
---|
120 | /* |
---|
121 | * Decapsulate. Does the real work and is called from gre_input() |
---|
122 | * (above). Returns an mbuf back if packet is not yet processed, |
---|
123 | * and NULL if it needs no further processing. proto is the protocol |
---|
124 | * number of the "calling" foo_input() routine. |
---|
125 | */ |
---|
126 | static struct mbuf * |
---|
127 | gre_input2(struct mbuf *m ,int hlen, u_char proto) |
---|
128 | { |
---|
129 | struct greip *gip; |
---|
130 | int isr; |
---|
131 | struct gre_softc *sc; |
---|
132 | u_int16_t flags; |
---|
133 | u_int32_t af; |
---|
134 | |
---|
135 | if ((sc = gre_lookup(m, proto)) == NULL) { |
---|
136 | /* No matching tunnel or tunnel is down. */ |
---|
137 | return (m); |
---|
138 | } |
---|
139 | |
---|
140 | if (m->m_len < sizeof(*gip)) { |
---|
141 | m = m_pullup(m, sizeof(*gip)); |
---|
142 | if (m == NULL) |
---|
143 | return (NULL); |
---|
144 | } |
---|
145 | gip = mtod(m, struct greip *); |
---|
146 | |
---|
147 | GRE2IFP(sc)->if_ipackets++; |
---|
148 | GRE2IFP(sc)->if_ibytes += m->m_pkthdr.len; |
---|
149 | |
---|
150 | switch (proto) { |
---|
151 | case IPPROTO_GRE: |
---|
152 | hlen += sizeof(struct gre_h); |
---|
153 | |
---|
154 | /* process GRE flags as packet can be of variable len */ |
---|
155 | flags = ntohs(gip->gi_flags); |
---|
156 | |
---|
157 | /* Checksum & Offset are present */ |
---|
158 | if ((flags & GRE_CP) | (flags & GRE_RP)) |
---|
159 | hlen += 4; |
---|
160 | /* We don't support routing fields (variable length) */ |
---|
161 | if (flags & GRE_RP) |
---|
162 | return (m); |
---|
163 | if (flags & GRE_KP) |
---|
164 | hlen += 4; |
---|
165 | if (flags & GRE_SP) |
---|
166 | hlen += 4; |
---|
167 | |
---|
168 | switch (ntohs(gip->gi_ptype)) { /* ethertypes */ |
---|
169 | case WCCP_PROTOCOL_TYPE: |
---|
170 | if (sc->wccp_ver == WCCP_V2) |
---|
171 | hlen += 4; |
---|
172 | /* FALLTHROUGH */ |
---|
173 | case ETHERTYPE_IP: /* shouldn't need a schednetisr(), */ |
---|
174 | isr = NETISR_IP;/* as we are in ip_input */ |
---|
175 | af = AF_INET; |
---|
176 | break; |
---|
177 | #ifdef INET6 |
---|
178 | case ETHERTYPE_IPV6: |
---|
179 | isr = NETISR_IPV6; |
---|
180 | af = AF_INET6; |
---|
181 | break; |
---|
182 | #endif |
---|
183 | #ifdef NETATALK |
---|
184 | case ETHERTYPE_ATALK: |
---|
185 | isr = NETISR_ATALK1; |
---|
186 | af = AF_APPLETALK; |
---|
187 | break; |
---|
188 | #endif |
---|
189 | default: |
---|
190 | /* Others not yet supported. */ |
---|
191 | return (m); |
---|
192 | } |
---|
193 | break; |
---|
194 | default: |
---|
195 | /* Others not yet supported. */ |
---|
196 | return (m); |
---|
197 | } |
---|
198 | |
---|
199 | if (hlen > m->m_pkthdr.len) { |
---|
200 | m_freem(m); |
---|
201 | return (NULL); |
---|
202 | } |
---|
203 | /* Unlike NetBSD, in FreeBSD m_adj() adjusts m->m_pkthdr.len as well */ |
---|
204 | m_adj(m, hlen); |
---|
205 | |
---|
206 | if (bpf_peers_present(GRE2IFP(sc)->if_bpf)) { |
---|
207 | bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m); |
---|
208 | } |
---|
209 | |
---|
210 | if ((GRE2IFP(sc)->if_flags & IFF_MONITOR) != 0) { |
---|
211 | m_freem(m); |
---|
212 | return(NULL); |
---|
213 | } |
---|
214 | |
---|
215 | m->m_pkthdr.rcvif = GRE2IFP(sc); |
---|
216 | |
---|
217 | netisr_queue(isr, m); |
---|
218 | |
---|
219 | /* Packet is done, no further processing needed. */ |
---|
220 | return (NULL); |
---|
221 | } |
---|
222 | |
---|
223 | /* |
---|
224 | * input routine for IPPRPOTO_MOBILE |
---|
225 | * This is a little bit diffrent from the other modes, as the |
---|
226 | * encapsulating header was not prepended, but instead inserted |
---|
227 | * between IP header and payload |
---|
228 | */ |
---|
229 | |
---|
230 | void |
---|
231 | gre_mobile_input(struct mbuf *m, int hlen) |
---|
232 | { |
---|
233 | struct ip *ip; |
---|
234 | struct mobip_h *mip; |
---|
235 | struct gre_softc *sc; |
---|
236 | int msiz; |
---|
237 | |
---|
238 | if ((sc = gre_lookup(m, IPPROTO_MOBILE)) == NULL) { |
---|
239 | /* No matching tunnel or tunnel is down. */ |
---|
240 | m_freem(m); |
---|
241 | return; |
---|
242 | } |
---|
243 | |
---|
244 | if (m->m_len < sizeof(*mip)) { |
---|
245 | m = m_pullup(m, sizeof(*mip)); |
---|
246 | if (m == NULL) |
---|
247 | return; |
---|
248 | } |
---|
249 | ip = mtod(m, struct ip *); |
---|
250 | mip = mtod(m, struct mobip_h *); |
---|
251 | |
---|
252 | GRE2IFP(sc)->if_ipackets++; |
---|
253 | GRE2IFP(sc)->if_ibytes += m->m_pkthdr.len; |
---|
254 | |
---|
255 | if (ntohs(mip->mh.proto) & MOB_H_SBIT) { |
---|
256 | msiz = MOB_H_SIZ_L; |
---|
257 | mip->mi.ip_src.s_addr = mip->mh.osrc; |
---|
258 | } else |
---|
259 | msiz = MOB_H_SIZ_S; |
---|
260 | |
---|
261 | if (m->m_len < (ip->ip_hl << 2) + msiz) { |
---|
262 | m = m_pullup(m, (ip->ip_hl << 2) + msiz); |
---|
263 | if (m == NULL) |
---|
264 | return; |
---|
265 | ip = mtod(m, struct ip *); |
---|
266 | mip = mtod(m, struct mobip_h *); |
---|
267 | } |
---|
268 | |
---|
269 | mip->mi.ip_dst.s_addr = mip->mh.odst; |
---|
270 | mip->mi.ip_p = (ntohs(mip->mh.proto) >> 8); |
---|
271 | |
---|
272 | if (gre_in_cksum((u_int16_t *)&mip->mh, msiz) != 0) { |
---|
273 | m_freem(m); |
---|
274 | return; |
---|
275 | } |
---|
276 | |
---|
277 | bcopy((caddr_t)(ip) + (ip->ip_hl << 2) + msiz, (caddr_t)(ip) + |
---|
278 | (ip->ip_hl << 2), m->m_len - msiz - (ip->ip_hl << 2)); |
---|
279 | m->m_len -= msiz; |
---|
280 | m->m_pkthdr.len -= msiz; |
---|
281 | |
---|
282 | /* |
---|
283 | * On FreeBSD, rip_input() supplies us with ip->ip_len |
---|
284 | * already converted into host byteorder and also decreases |
---|
285 | * it by the lengh of IP header, however, ip_input() expects |
---|
286 | * that this field is in the original format (network byteorder |
---|
287 | * and full size of IP packet), so that adjust accordingly. |
---|
288 | */ |
---|
289 | ip->ip_len = htons(ip->ip_len + sizeof(struct ip) - msiz); |
---|
290 | |
---|
291 | ip->ip_sum = 0; |
---|
292 | ip->ip_sum = in_cksum(m, (ip->ip_hl << 2)); |
---|
293 | |
---|
294 | if (bpf_peers_present(GRE2IFP(sc)->if_bpf)) { |
---|
295 | u_int32_t af = AF_INET; |
---|
296 | bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m); |
---|
297 | } |
---|
298 | |
---|
299 | if ((GRE2IFP(sc)->if_flags & IFF_MONITOR) != 0) { |
---|
300 | m_freem(m); |
---|
301 | return; |
---|
302 | } |
---|
303 | |
---|
304 | m->m_pkthdr.rcvif = GRE2IFP(sc); |
---|
305 | |
---|
306 | netisr_queue(NETISR_IP, m); |
---|
307 | } |
---|
308 | |
---|
309 | /* |
---|
310 | * Find the gre interface associated with our src/dst/proto set. |
---|
311 | * |
---|
312 | * XXXRW: Need some sort of drain/refcount mechanism so that the softc |
---|
313 | * reference remains valid after it's returned from gre_lookup(). Right |
---|
314 | * now, I'm thinking it should be reference-counted with a gre_dropref() |
---|
315 | * when the caller is done with the softc. This is complicated by how |
---|
316 | * to handle destroying the gre softc; probably using a gre_drain() in |
---|
317 | * in_gre.c during destroy. |
---|
318 | */ |
---|
319 | static struct gre_softc * |
---|
320 | gre_lookup(struct mbuf *m, u_int8_t proto) |
---|
321 | { |
---|
322 | struct ip *ip = mtod(m, struct ip *); |
---|
323 | struct gre_softc *sc; |
---|
324 | |
---|
325 | mtx_lock(&gre_mtx); |
---|
326 | for (sc = LIST_FIRST(&gre_softc_list); sc != NULL; |
---|
327 | sc = LIST_NEXT(sc, sc_list)) { |
---|
328 | if ((sc->g_dst.s_addr == ip->ip_src.s_addr) && |
---|
329 | (sc->g_src.s_addr == ip->ip_dst.s_addr) && |
---|
330 | (sc->g_proto == proto) && |
---|
331 | ((GRE2IFP(sc)->if_flags & IFF_UP) != 0)) { |
---|
332 | mtx_unlock(&gre_mtx); |
---|
333 | return (sc); |
---|
334 | } |
---|
335 | } |
---|
336 | mtx_unlock(&gre_mtx); |
---|
337 | |
---|
338 | return (NULL); |
---|
339 | } |
---|