FreeBSD/Linux Kernel Cross Reference
sys/netinet/ip_gre.c
1 /* $NetBSD: ip_gre.c,v 1.33.2.1 2005/03/30 21:35:13 tron Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Heiko W.Rupp <hwr@pilhuhn.de>
9 *
10 * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 /*
42 * deencapsulate tunneled packets and send them on
43 * output half is in net/if_gre.[ch]
44 * This currently handles IPPROTO_GRE, IPPROTO_MOBILE
45 */
46
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: ip_gre.c,v 1.33.2.1 2005/03/30 21:35:13 tron Exp $");
49
50 #include "gre.h"
51 #if NGRE > 0
52
53 #include "opt_inet.h"
54 #include "opt_ns.h"
55 #include "opt_atalk.h"
56 #include "bpfilter.h"
57
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/mbuf.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/protosw.h>
64 #include <sys/errno.h>
65 #include <sys/time.h>
66 #include <sys/kernel.h>
67 #include <sys/ioctl.h>
68 #include <sys/syslog.h>
69 #include <net/bpf.h>
70 #include <net/ethertypes.h>
71 #include <net/if.h>
72 #include <net/netisr.h>
73 #include <net/route.h>
74 #include <net/raw_cb.h>
75
76 #ifdef INET
77 #include <netinet/in.h>
78 #include <netinet/in_var.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_var.h>
82 #include <netinet/ip_gre.h>
83 #else
84 #error ip_gre input without IP?
85 #endif
86
87 #ifdef NS
88 #include <netns/ns.h>
89 #include <netns/ns_if.h>
90 #endif
91
92 #ifdef NETATALK
93 #include <netatalk/at.h>
94 #include <netatalk/at_var.h>
95 #include <netatalk/at_extern.h>
96 #endif
97
98 /* Needs IP headers. */
99 #include <net/if_gre.h>
100
101 #include <machine/stdarg.h>
102
103 #if 1
104 void gre_inet_ntoa(struct in_addr in); /* XXX */
105 #endif
106
107 struct gre_softc *gre_lookup(struct mbuf *, u_int8_t);
108
109 int gre_input2(struct mbuf *, int, u_char);
110
111 /*
112 * De-encapsulate a packet and feed it back through ip input (this
113 * routine is called whenever IP gets a packet with proto type
114 * IPPROTO_GRE and a local destination address).
115 * This really is simple
116 */
117 void
118 gre_input(struct mbuf *m, ...)
119 {
120 int off, ret, proto;
121 va_list ap;
122
123 va_start(ap, m);
124 off = va_arg(ap, int);
125 proto = va_arg(ap, int);
126 va_end(ap);
127
128 ret = gre_input2(m, off, proto);
129 /*
130 * ret == 0 : packet not processed, meaning that
131 * no matching tunnel that is up is found.
132 * we inject it to raw ip socket to see if anyone picks it up.
133 */
134 if (ret == 0)
135 rip_input(m, off, proto);
136 }
137
138 /*
139 * decapsulate.
140 * Does the real work and is called from gre_input() (above)
141 * returns 0 if packet is not yet processed
142 * and 1 if it needs no further processing
143 * proto is the protocol number of the "calling" foo_input()
144 * routine.
145 */
146 int
147 gre_input2(struct mbuf *m, int hlen, u_char proto)
148 {
149 struct greip *gip;
150 int s, isr;
151 struct ifqueue *ifq;
152 struct gre_softc *sc;
153 u_int16_t flags;
154
155 if ((sc = gre_lookup(m, proto)) == NULL) {
156 /* No matching tunnel or tunnel is down. */
157 return (0);
158 }
159
160 if (m->m_len < sizeof(*gip)) {
161 m = m_pullup(m, sizeof(*gip));
162 if (m == NULL)
163 return (ENOBUFS);
164 }
165 gip = mtod(m, struct greip *);
166
167 sc->sc_if.if_ipackets++;
168 sc->sc_if.if_ibytes += m->m_pkthdr.len;
169
170 switch (proto) {
171 case IPPROTO_GRE:
172 hlen += sizeof(struct gre_h);
173
174 /* process GRE flags as packet can be of variable len */
175 flags = ntohs(gip->gi_flags);
176
177 /* Checksum & Offset are present */
178 if ((flags & GRE_CP) | (flags & GRE_RP))
179 hlen += 4;
180 /* We don't support routing fields (variable length) */
181 if (flags & GRE_RP)
182 return (0);
183 if (flags & GRE_KP)
184 hlen += 4;
185 if (flags & GRE_SP)
186 hlen += 4;
187
188 switch (ntohs(gip->gi_ptype)) { /* ethertypes */
189 case ETHERTYPE_IP: /* shouldn't need a schednetisr(), as */
190 ifq = &ipintrq; /* we are in ip_input */
191 isr = NETISR_IP;
192 break;
193 #ifdef NS
194 case ETHERTYPE_NS:
195 ifq = &nsintrq;
196 isr = NETISR_NS;
197 break;
198 #endif
199 #ifdef NETATALK
200 case ETHERTYPE_ATALK:
201 ifq = &atintrq1;
202 isr = NETISR_ATALK;
203 break;
204 #endif
205 #ifdef INET6
206 case ETHERTYPE_IPV6:
207 #ifdef GRE_DEBUG
208 printf( "ip_gre.c/gre_input2: IPv6 packet\n" );
209 #endif
210 ifq = &ip6intrq;
211 isr = NETISR_IPV6;
212 break;
213 #endif
214 default: /* others not yet supported */
215 printf( "ip_gre.c/gre_input2: unhandled ethertype 0x%04x\n", (int) ntohs(gip->gi_ptype) );
216 return (0);
217 }
218 break;
219 default:
220 /* others not yet supported */
221 return (0);
222 }
223
224 if (hlen > m->m_pkthdr.len) {
225 m_freem(m);
226 return (EINVAL);
227 }
228 m_adj(m, hlen);
229
230 #if NBPFILTER > 0
231 if (sc->sc_if.if_bpf) {
232 struct mbuf m0;
233 u_int32_t af = AF_INET;
234
235 m0.m_flags = 0;
236 m0.m_next = m;
237 m0.m_len = 4;
238 m0.m_data = (char *)⁡
239
240 bpf_mtap(sc->sc_if.if_bpf, &m0);
241 }
242 #endif /*NBPFILTER > 0*/
243
244 m->m_pkthdr.rcvif = &sc->sc_if;
245
246 s = splnet(); /* possible */
247 if (IF_QFULL(ifq)) {
248 IF_DROP(ifq);
249 m_freem(m);
250 } else {
251 IF_ENQUEUE(ifq, m);
252 }
253 /* we need schednetisr since the address family may change */
254 schednetisr(isr);
255 splx(s);
256
257 return (1); /* packet is done, no further processing needed */
258 }
259
260 /*
261 * input routine for IPPRPOTO_MOBILE
262 * This is a little bit diffrent from the other modes, as the
263 * encapsulating header was not prepended, but instead inserted
264 * between IP header and payload
265 */
266 void
267 gre_mobile_input(struct mbuf *m, ...)
268 {
269 struct ip *ip;
270 struct mobip_h *mip;
271 struct ifqueue *ifq;
272 struct gre_softc *sc;
273 int hlen, s;
274 va_list ap;
275 int msiz;
276
277 va_start(ap, m);
278 hlen = va_arg(ap, int);
279 va_end(ap);
280
281 if ((sc = gre_lookup(m, IPPROTO_MOBILE)) == NULL) {
282 /* No matching tunnel or tunnel is down. */
283 m_freem(m);
284 return;
285 }
286
287 if (m->m_len < sizeof(*mip)) {
288 m = m_pullup(m, sizeof(*mip));
289 if (m == NULL)
290 return;
291 }
292 ip = mtod(m, struct ip *);
293 mip = mtod(m, struct mobip_h *);
294
295 sc->sc_if.if_ipackets++;
296 sc->sc_if.if_ibytes += m->m_pkthdr.len;
297
298 if (ntohs(mip->mh.proto) & MOB_H_SBIT) {
299 msiz = MOB_H_SIZ_L;
300 mip->mi.ip_src.s_addr = mip->mh.osrc;
301 } else
302 msiz = MOB_H_SIZ_S;
303
304 if (m->m_len < (ip->ip_hl << 2) + msiz) {
305 m = m_pullup(m, (ip->ip_hl << 2) + msiz);
306 if (m == NULL)
307 return;
308 ip = mtod(m, struct ip *);
309 mip = mtod(m, struct mobip_h *);
310 }
311
312 mip->mi.ip_dst.s_addr = mip->mh.odst;
313 mip->mi.ip_p = (ntohs(mip->mh.proto) >> 8);
314
315 if (gre_in_cksum((u_int16_t *)&mip->mh, msiz) != 0) {
316 m_freem(m);
317 return;
318 }
319
320 memmove(ip + (ip->ip_hl << 2), ip + (ip->ip_hl << 2) + msiz,
321 m->m_len - msiz - (ip->ip_hl << 2));
322 m->m_len -= msiz;
323 ip->ip_len = htons(ntohs(ip->ip_len) - msiz);
324 m->m_pkthdr.len -= msiz;
325
326 ip->ip_sum = 0;
327 ip->ip_sum = in_cksum(m, (ip->ip_hl << 2));
328
329 #if NBPFILTER > 0
330 if (sc->sc_if.if_bpf) {
331 struct mbuf m0;
332 u_int af = AF_INET;
333
334 m0.m_next = m;
335 m0.m_len = 4;
336 m0.m_data = (char *)⁡
337
338 bpf_mtap(sc->sc_if.if_bpf, &m0);
339 }
340 #endif /*NBPFILTER > 0*/
341
342 ifq = &ipintrq;
343 s = splnet(); /* possible */
344 if (IF_QFULL(ifq)) {
345 IF_DROP(ifq);
346 m_freem(m);
347 } else {
348 IF_ENQUEUE(ifq, m);
349 }
350 splx(s);
351 }
352
353 /*
354 * Find the gre interface associated with our src/dst/proto set.
355 */
356 struct gre_softc *
357 gre_lookup(struct mbuf *m, u_int8_t proto)
358 {
359 struct ip *ip = mtod(m, struct ip *);
360 struct gre_softc *sc;
361
362 for (sc = LIST_FIRST(&gre_softc_list); sc != NULL;
363 sc = LIST_NEXT(sc, sc_list)) {
364 if ((sc->g_dst.s_addr == ip->ip_src.s_addr) &&
365 (sc->g_src.s_addr == ip->ip_dst.s_addr) &&
366 (sc->g_proto == proto) &&
367 ((sc->sc_if.if_flags & IFF_UP) != 0))
368 return (sc);
369 }
370
371 return (NULL);
372 }
373
374 #endif /* if NGRE > 0 */
Cache object: 49ae588b7679c2fa6102e6cba6b3a50b
|