]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/ip_gre.c
Add context pointer and source address to the UDP tunnel callback
[FreeBSD/FreeBSD.git] / sys / netinet / ip_gre.c
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_inet6.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
52 #include <sys/errno.h>
53 #include <sys/time.h>
54 #include <sys/kernel.h>
55 #include <sys/syslog.h>
56 #include <net/bpf.h>
57 #include <net/ethernet.h>
58 #include <net/if.h>
59 #include <net/if_var.h>
60 #include <net/netisr.h>
61 #include <net/route.h>
62 #include <net/raw_cb.h>
63
64 #ifdef INET
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>
72 #else
73 #error "ip_gre requires INET"
74 #endif
75
76 /* Needs IP headers. */
77 #include <net/if_gre.h>
78
79 #include <machine/stdarg.h>
80
81 #if 1
82 void gre_inet_ntoa(struct in_addr in);  /* XXX */
83 #endif
84
85 static struct gre_softc *gre_lookup(struct mbuf *, u_int8_t);
86
87 static struct mbuf *gre_input2(struct mbuf *, int, u_char);
88
89 /*
90  * De-encapsulate a packet and feed it back through ip input (this
91  * routine is called whenever IP gets a packet with proto type
92  * IPPROTO_GRE and a local destination address).
93  * This really is simple
94  */
95 int
96 gre_input(struct mbuf **mp, int *offp, int proto)
97 {
98         struct mbuf *m;
99         int off;
100
101         m = *mp;
102         off = *offp;
103         *mp = NULL;
104
105         m = gre_input2(m, off, proto);
106
107         /*
108          * If no matching tunnel that is up is found. We inject
109          * the mbuf to raw ip socket to see if anyone picks it up.
110          */
111         if (m != NULL) {
112                 *mp = m;
113                 rip_input(mp, offp, proto);
114         }
115         return (IPPROTO_DONE);
116 }
117
118 /*
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.
123  */
124 static struct mbuf *
125 gre_input2(struct mbuf *m ,int hlen, u_char proto)
126 {
127         struct greip *gip;
128         int isr;
129         struct gre_softc *sc;
130         u_int16_t flags;
131         u_int32_t af;
132
133         if ((sc = gre_lookup(m, proto)) == NULL) {
134                 /* No matching tunnel or tunnel is down. */
135                 return (m);
136         }
137
138         if (m->m_len < sizeof(*gip)) {
139                 m = m_pullup(m, sizeof(*gip));
140                 if (m == NULL)
141                         return (NULL);
142         }
143         gip = mtod(m, struct greip *);
144
145         if_inc_counter(GRE2IFP(sc), IFCOUNTER_IPACKETS, 1);
146         if_inc_counter(GRE2IFP(sc), IFCOUNTER_IBYTES, m->m_pkthdr.len);
147
148         switch (proto) {
149         case IPPROTO_GRE:
150                 hlen += sizeof(struct gre_h);
151
152                 /* process GRE flags as packet can be of variable len */
153                 flags = ntohs(gip->gi_flags);
154
155                 /* Checksum & Offset are present */
156                 if ((flags & GRE_CP) | (flags & GRE_RP))
157                         hlen += 4;
158                 /* We don't support routing fields (variable length) */
159                 if (flags & GRE_RP)
160                         return (m);
161                 if (flags & GRE_KP)
162                         hlen += 4;
163                 if (flags & GRE_SP)
164                         hlen += 4;
165
166                 switch (ntohs(gip->gi_ptype)) { /* ethertypes */
167                 case WCCP_PROTOCOL_TYPE:
168                         if (sc->wccp_ver == WCCP_V2)
169                                 hlen += 4;
170                         /* FALLTHROUGH */
171                 case ETHERTYPE_IP:      /* shouldn't need a schednetisr(), */
172                         isr = NETISR_IP;/* as we are in ip_input */
173                         af = AF_INET;
174                         break;
175 #ifdef INET6
176                 case ETHERTYPE_IPV6:
177                         isr = NETISR_IPV6;
178                         af = AF_INET6;
179                         break;
180 #endif
181                 default:
182                         /* Others not yet supported. */
183                         return (m);
184                 }
185                 break;
186         default:
187                 /* Others not yet supported. */
188                 return (m);
189         }
190
191         if (hlen > m->m_pkthdr.len) {
192                 m_freem(m);
193                 return (NULL);
194         }
195         /* Unlike NetBSD, in FreeBSD m_adj() adjusts m->m_pkthdr.len as well */
196         m_adj(m, hlen);
197
198         if (bpf_peers_present(GRE2IFP(sc)->if_bpf)) {
199                 bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m);
200         }
201
202         if ((GRE2IFP(sc)->if_flags & IFF_MONITOR) != 0) {
203                 m_freem(m);
204                 return(NULL);
205         }
206
207         m->m_pkthdr.rcvif = GRE2IFP(sc);
208         m_clrprotoflags(m);
209         netisr_queue(isr, m);
210
211         /* Packet is done, no further processing needed. */
212         return (NULL);
213 }
214
215 /*
216  * input routine for IPPRPOTO_MOBILE
217  * This is a little bit diffrent from the other modes, as the
218  * encapsulating header was not prepended, but instead inserted
219  * between IP header and payload
220  */
221
222 int
223 gre_mobile_input(struct mbuf **mp, int *offp, int proto)
224 {
225         struct ip *ip;
226         struct mobip_h *mip;
227         struct mbuf *m;
228         struct gre_softc *sc;
229         int msiz;
230
231         m = *mp;
232         if ((sc = gre_lookup(m, IPPROTO_MOBILE)) == NULL) {
233                 /* No matching tunnel or tunnel is down. */
234                 m_freem(m);
235                 return (IPPROTO_DONE);
236         }
237
238         if (m->m_len < sizeof(*mip)) {
239                 m = m_pullup(m, sizeof(*mip));
240                 if (m == NULL)
241                         return (IPPROTO_DONE);
242         }
243         ip = mtod(m, struct ip *);
244         mip = mtod(m, struct mobip_h *);
245
246         if_inc_counter(GRE2IFP(sc), IFCOUNTER_IPACKETS, 1);
247         if_inc_counter(GRE2IFP(sc), IFCOUNTER_IBYTES, m->m_pkthdr.len);
248
249         if (ntohs(mip->mh.proto) & MOB_H_SBIT) {
250                 msiz = MOB_H_SIZ_L;
251                 mip->mi.ip_src.s_addr = mip->mh.osrc;
252         } else
253                 msiz = MOB_H_SIZ_S;
254
255         if (m->m_len < (ip->ip_hl << 2) + msiz) {
256                 m = m_pullup(m, (ip->ip_hl << 2) + msiz);
257                 if (m == NULL)
258                         return (IPPROTO_DONE);
259                 ip = mtod(m, struct ip *);
260                 mip = mtod(m, struct mobip_h *);
261         }
262
263         mip->mi.ip_dst.s_addr = mip->mh.odst;
264         mip->mi.ip_p = (ntohs(mip->mh.proto) >> 8);
265
266         if (gre_in_cksum((u_int16_t *)&mip->mh, msiz) != 0) {
267                 m_freem(m);
268                 return (IPPROTO_DONE);
269         }
270
271         bcopy((caddr_t)(ip) + (ip->ip_hl << 2) + msiz, (caddr_t)(ip) +
272             (ip->ip_hl << 2), m->m_len - msiz - (ip->ip_hl << 2));
273         m->m_len -= msiz;
274         m->m_pkthdr.len -= msiz;
275
276         /*
277          * On FreeBSD, rip_input() supplies us with ip->ip_len
278          * decreased by the lengh of IP header, however, ip_input()
279          * expects it to be full size of IP packet, so adjust accordingly.
280          */
281         ip->ip_len = htons(ntohs(ip->ip_len) + sizeof(struct ip) - msiz);
282
283         ip->ip_sum = 0;
284         ip->ip_sum = in_cksum(m, (ip->ip_hl << 2));
285
286         if (bpf_peers_present(GRE2IFP(sc)->if_bpf)) {
287                 u_int32_t af = AF_INET;
288                 bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m);
289         }
290
291         if ((GRE2IFP(sc)->if_flags & IFF_MONITOR) != 0) {
292                 m_freem(m);
293                 return (IPPROTO_DONE);
294         }
295
296         m->m_pkthdr.rcvif = GRE2IFP(sc);
297
298         netisr_queue(NETISR_IP, m);
299         return (IPPROTO_DONE);
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(struct mbuf *m, u_int8_t proto)
314 {
315         struct ip *ip = mtod(m, struct ip *);
316         struct gre_softc *sc;
317
318         GRE_LIST_LOCK();
319         for (sc = LIST_FIRST(&V_gre_softc_list); sc != NULL;
320              sc = LIST_NEXT(sc, sc_list)) {
321                 if ((sc->g_dst.s_addr == ip->ip_src.s_addr) &&
322                     (sc->g_src.s_addr == ip->ip_dst.s_addr) &&
323                     (sc->g_proto == proto) &&
324                     ((GRE2IFP(sc)->if_flags & IFF_UP) != 0)) {
325                         GRE_LIST_UNLOCK();
326                         return (sc);
327                 }
328         }
329         GRE_LIST_UNLOCK();
330
331         return (NULL);
332 }