FreeBSD/Linux Kernel Cross Reference
sys/netinet/ip_gre.c
1 /* $NetBSD: ip_gre.c,v 1.44 2006/11/16 22:54:14 dyoung 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.44 2006/11/16 22:54:14 dyoung Exp $");
49
50 #include "gre.h"
51 #if NGRE > 0
52
53 #include "opt_inet.h"
54 #include "opt_atalk.h"
55 #include "bpfilter.h"
56
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/mbuf.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/protosw.h>
63 #include <sys/errno.h>
64 #include <sys/time.h>
65 #include <sys/kernel.h>
66 #include <sys/ioctl.h>
67 #include <sys/syslog.h>
68 #include <net/bpf.h>
69 #include <net/ethertypes.h>
70 #include <net/if.h>
71 #include <net/netisr.h>
72 #include <net/route.h>
73 #include <net/raw_cb.h>
74
75 #ifdef INET
76 #include <netinet/in.h>
77 #include <netinet/in_var.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/ip.h>
80 #include <netinet/ip_var.h>
81 #include <netinet/ip_gre.h>
82 #else
83 #error ip_gre input without IP?
84 #endif
85
86
87 #ifdef NETATALK
88 #include <netatalk/at.h>
89 #include <netatalk/at_var.h>
90 #include <netatalk/at_extern.h>
91 #endif
92
93 /* Needs IP headers. */
94 #include <net/if_gre.h>
95
96 #include <machine/stdarg.h>
97
98 #if 1
99 void gre_inet_ntoa(struct in_addr in); /* XXX */
100 #endif
101
102 struct gre_softc *gre_lookup(struct mbuf *, u_int8_t);
103
104 int gre_input2(struct mbuf *, int, u_char);
105
106 /*
107 * De-encapsulate a packet and feed it back through ip input (this
108 * routine is called whenever IP gets a packet with proto type
109 * IPPROTO_GRE and a local destination address).
110 * This really is simple
111 */
112 void
113 gre_input(struct mbuf *m, ...)
114 {
115 int off, ret, proto;
116 va_list ap;
117
118 va_start(ap, m);
119 off = va_arg(ap, int);
120 proto = va_arg(ap, int);
121 va_end(ap);
122
123 ret = gre_input2(m, off, proto);
124 /*
125 * ret == 0 : packet not processed, meaning that
126 * no matching tunnel that is up is found.
127 * we inject it to raw ip socket to see if anyone picks it up.
128 */
129 if (ret == 0)
130 rip_input(m, off, proto);
131 }
132
133 /*
134 * decapsulate.
135 * Does the real work and is called from gre_input() (above)
136 * returns 0 if packet is not yet processed
137 * and 1 if it needs no further processing
138 * proto is the protocol number of the "calling" foo_input()
139 * routine.
140 */
141 int
142 gre_input2(struct mbuf *m, int hlen, u_char proto)
143 {
144 const struct greip *gip;
145 struct gre_softc *sc;
146
147 if ((sc = gre_lookup(m, proto)) == NULL) {
148 /* No matching tunnel or tunnel is down. */
149 return 0;
150 }
151
152 if (m->m_len < sizeof(*gip)) {
153 m = m_pullup(m, sizeof(*gip));
154 if (m == NULL)
155 return ENOBUFS;
156 }
157 gip = mtod(m, const struct greip *);
158
159 return gre_input3(sc, m, hlen, proto, &gip->gi_g);
160 }
161
162 /*
163 * input routine for IPPRPOTO_MOBILE
164 * This is a little bit different from the other modes, as the
165 * encapsulating header was not prepended, but instead inserted
166 * between IP header and payload
167 */
168 void
169 gre_mobile_input(struct mbuf *m, ...)
170 {
171 struct ip *ip;
172 struct mobip_h *mip;
173 struct ifqueue *ifq;
174 struct gre_softc *sc;
175 uint8_t *hdr;
176 int hlen, s;
177 va_list ap;
178 int msiz;
179
180 va_start(ap, m);
181 hlen = va_arg(ap, int);
182 va_end(ap);
183
184 if ((sc = gre_lookup(m, IPPROTO_MOBILE)) == NULL) {
185 /* No matching tunnel or tunnel is down. */
186 m_freem(m);
187 return;
188 }
189
190 if (M_UNWRITABLE(m, sizeof(*mip))) {
191 m = m_pullup(m, sizeof(*mip));
192 if (m == NULL)
193 return;
194 }
195 ip = mtod(m, struct ip *);
196 /* XXX what if there are IP options? */
197 mip = mtod(m, struct mobip_h *);
198
199 sc->sc_if.if_ipackets++;
200 sc->sc_if.if_ibytes += m->m_pkthdr.len;
201
202 if (ntohs(mip->mh.proto) & MOB_H_SBIT) {
203 msiz = MOB_H_SIZ_L;
204 mip->mi.ip_src.s_addr = mip->mh.osrc;
205 } else
206 msiz = MOB_H_SIZ_S;
207
208 if (M_UNWRITABLE(m, hlen + msiz)) {
209 m = m_pullup(m, hlen + msiz);
210 if (m == NULL)
211 return;
212 ip = mtod(m, struct ip *);
213 mip = mtod(m, struct mobip_h *);
214 }
215
216 mip->mi.ip_dst.s_addr = mip->mh.odst;
217 mip->mi.ip_p = (ntohs(mip->mh.proto) >> 8);
218
219 if (gre_in_cksum((u_int16_t *)&mip->mh, msiz) != 0) {
220 m_freem(m);
221 return;
222 }
223
224 hdr = mtod(m, uint8_t *);
225 memmove(hdr + hlen, hdr + hlen + msiz, m->m_len - msiz - hlen);
226 m->m_len -= msiz;
227 ip->ip_len = htons(ntohs(ip->ip_len) - msiz);
228 m->m_pkthdr.len -= msiz;
229
230 ip->ip_sum = 0;
231 ip->ip_sum = in_cksum(m, hlen);
232
233 #if NBPFILTER > 0
234 if (sc->sc_if.if_bpf != NULL)
235 bpf_mtap_af(sc->sc_if.if_bpf, AF_INET, m);
236 #endif /*NBPFILTER > 0*/
237
238 ifq = &ipintrq;
239 s = splnet(); /* possible */
240 if (IF_QFULL(ifq)) {
241 IF_DROP(ifq);
242 m_freem(m);
243 } else
244 IF_ENQUEUE(ifq, m);
245 splx(s);
246 }
247
248 /*
249 * Find the gre interface associated with our src/dst/proto set.
250 */
251 struct gre_softc *
252 gre_lookup(struct mbuf *m, u_int8_t proto)
253 {
254 const struct ip *ip = mtod(m, const struct ip *);
255 struct gre_softc *sc;
256
257 LIST_FOREACH(sc, &gre_softc_list, sc_list) {
258 if (sc->g_dst.s_addr == ip->ip_src.s_addr &&
259 sc->g_src.s_addr == ip->ip_dst.s_addr &&
260 sc->sc_proto == proto &&
261 (sc->sc_if.if_flags & IFF_UP) != 0)
262 return sc;
263 }
264
265 return NULL;
266 }
267
268 #endif /* if NGRE > 0 */
Cache object: e38da5e63f693e6d204118104bfb602a
|