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