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