1 /* $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Heiko W.Rupp <hwr@pilhuhn.de>
10 * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
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
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 #include "opt_atalk.h"
45 #include "opt_inet6.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/protosw.h>
53 #include <sys/errno.h>
55 #include <sys/kernel.h>
56 #include <sys/syslog.h>
58 #include <net/ethernet.h>
60 #include <net/netisr.h>
61 #include <net/route.h>
62 #include <net/raw_cb.h>
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/ip.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/ip_gre.h>
71 #include <machine/in_cksum.h>
73 #error ip_gre input without IP?
77 #include <netatalk/at.h>
78 #include <netatalk/at_var.h>
79 #include <netatalk/at_extern.h>
82 /* Needs IP headers. */
83 #include <net/if_gre.h>
85 #include <machine/stdarg.h>
88 void gre_inet_ntoa(struct in_addr in); /* XXX */
91 static struct gre_softc *gre_lookup(struct mbuf *, u_int8_t);
93 static struct mbuf *gre_input2(struct mbuf *, int, u_char);
96 * De-encapsulate a packet and feed it back through ip input (this
97 * routine is called whenever IP gets a packet with proto type
98 * IPPROTO_GRE and a local destination address).
99 * This really is simple
102 gre_input(struct mbuf *m, int off)
106 proto = (mtod(m, struct ip *))->ip_p;
108 m = gre_input2(m, off, proto);
111 * If no matching tunnel that is up is found. We inject
112 * the mbuf to raw ip socket to see if anyone picks it up.
119 * Decapsulate. Does the real work and is called from gre_input()
120 * (above). Returns an mbuf back if packet is not yet processed,
121 * and NULL if it needs no further processing. proto is the protocol
122 * number of the "calling" foo_input() routine.
125 gre_input2(struct mbuf *m ,int hlen, u_char proto)
129 struct gre_softc *sc;
133 if ((sc = gre_lookup(m, proto)) == NULL) {
134 /* No matching tunnel or tunnel is down. */
138 if (m->m_len < sizeof(*gip)) {
139 m = m_pullup(m, sizeof(*gip));
143 gip = mtod(m, struct greip *);
145 GRE2IFP(sc)->if_ipackets++;
146 GRE2IFP(sc)->if_ibytes += m->m_pkthdr.len;
150 hlen += sizeof(struct gre_h);
152 /* process GRE flags as packet can be of variable len */
153 flags = ntohs(gip->gi_flags);
155 /* Checksum & Offset are present */
156 if ((flags & GRE_CP) | (flags & GRE_RP))
158 /* We don't support routing fields (variable length) */
166 switch (ntohs(gip->gi_ptype)) { /* ethertypes */
167 case WCCP_PROTOCOL_TYPE:
168 if (sc->wccp_ver == WCCP_V2)
171 case ETHERTYPE_IP: /* shouldn't need a schednetisr(), */
172 isr = NETISR_IP;/* as we are in ip_input */
182 case ETHERTYPE_ATALK:
188 /* Others not yet supported. */
193 /* Others not yet supported. */
197 if (hlen > m->m_pkthdr.len) {
201 /* Unlike NetBSD, in FreeBSD m_adj() adjusts m->m_pkthdr.len as well */
204 if (bpf_peers_present(GRE2IFP(sc)->if_bpf)) {
205 bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m);
208 m->m_pkthdr.rcvif = GRE2IFP(sc);
210 netisr_queue(isr, m);
212 /* Packet is done, no further processing needed. */
217 * input routine for IPPRPOTO_MOBILE
218 * This is a little bit diffrent from the other modes, as the
219 * encapsulating header was not prepended, but instead inserted
220 * between IP header and payload
224 gre_mobile_input(struct mbuf *m, int hlen)
228 struct gre_softc *sc;
231 if ((sc = gre_lookup(m, IPPROTO_MOBILE)) == NULL) {
232 /* No matching tunnel or tunnel is down. */
237 if (m->m_len < sizeof(*mip)) {
238 m = m_pullup(m, sizeof(*mip));
242 ip = mtod(m, struct ip *);
243 mip = mtod(m, struct mobip_h *);
245 GRE2IFP(sc)->if_ipackets++;
246 GRE2IFP(sc)->if_ibytes += m->m_pkthdr.len;
248 if (ntohs(mip->mh.proto) & MOB_H_SBIT) {
250 mip->mi.ip_src.s_addr = mip->mh.osrc;
254 if (m->m_len < (ip->ip_hl << 2) + msiz) {
255 m = m_pullup(m, (ip->ip_hl << 2) + msiz);
258 ip = mtod(m, struct ip *);
259 mip = mtod(m, struct mobip_h *);
262 mip->mi.ip_dst.s_addr = mip->mh.odst;
263 mip->mi.ip_p = (ntohs(mip->mh.proto) >> 8);
265 if (gre_in_cksum((u_int16_t *)&mip->mh, msiz) != 0) {
270 bcopy((caddr_t)(ip) + (ip->ip_hl << 2) + msiz, (caddr_t)(ip) +
271 (ip->ip_hl << 2), m->m_len - msiz - (ip->ip_hl << 2));
273 m->m_pkthdr.len -= msiz;
276 * On FreeBSD, rip_input() supplies us with ip->ip_len
277 * already converted into host byteorder and also decreases
278 * it by the lengh of IP header, however, ip_input() expects
279 * that this field is in the original format (network byteorder
280 * and full size of IP packet), so that adjust accordingly.
282 ip->ip_len = htons(ip->ip_len + sizeof(struct ip) - msiz);
285 ip->ip_sum = in_cksum(m, (ip->ip_hl << 2));
287 if (bpf_peers_present(GRE2IFP(sc)->if_bpf)) {
288 u_int32_t af = AF_INET;
289 bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m);
292 m->m_pkthdr.rcvif = GRE2IFP(sc);
294 netisr_queue(NETISR_IP, m);
298 * Find the gre interface associated with our src/dst/proto set.
300 * XXXRW: Need some sort of drain/refcount mechanism so that the softc
301 * reference remains valid after it's returned from gre_lookup(). Right
302 * now, I'm thinking it should be reference-counted with a gre_dropref()
303 * when the caller is done with the softc. This is complicated by how
304 * to handle destroying the gre softc; probably using a gre_drain() in
305 * in_gre.c during destroy.
307 static struct gre_softc *
308 gre_lookup(struct mbuf *m, u_int8_t proto)
310 struct ip *ip = mtod(m, struct ip *);
311 struct gre_softc *sc;
314 for (sc = LIST_FIRST(&gre_softc_list); sc != NULL;
315 sc = LIST_NEXT(sc, sc_list)) {
316 if ((sc->g_dst.s_addr == ip->ip_src.s_addr) &&
317 (sc->g_src.s_addr == ip->ip_dst.s_addr) &&
318 (sc->g_proto == proto) &&
319 ((GRE2IFP(sc)->if_flags & IFF_UP) != 0)) {
320 mtx_unlock(&gre_mtx);
324 mtx_unlock(&gre_mtx);