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