summaryrefslogtreecommitdiff
path: root/sys/netinet/ip_gre.c
blob: e2470f921083015f7331de17dc75705a11d4d087 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*	$NetBSD: ip_gre.c,v 1.1 1998/09/13 20:27:48 hwr Exp $ */

/*
 * (c) 1998 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Heiko W.Rupp <hwr@pilhuhn.de>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *    
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * deencapsulate GRE packets and send them on
 * output half is in net/ip_gre.[ch]
 *
 */


#include "gre.h"
#if NGRE > 0

#include "opt_inet.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/protosw.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/ioctl.h>
#include <sys/syslog.h>
#include <net/ethertypes.h>
#include <net/if.h>
#include <net/netisr.h>
#include <net/route.h>
#include <net/raw_cb.h>

#ifdef INET
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#else
Huh? ip_gre input without IP?
#endif

#ifdef NS
#include <netns/ns.h>
#include <netns/ns_if.h>
#endif

#ifdef NETATALK
#include <netatalk/at.h>
#include <netatalk/at_var.h>
#include <netatalk/at_extern.h>
#endif

#include <machine/stdarg.h>

#include "ip_gre.h"
#include <net/if_gre.h>

extern struct gre_softc gre_softc[NGRE];

/*
 * De-encapsulate a packet and feed it back through ip input (this
 * routine is called whenever IP gets a packet with proto type
 * IPPROTO_GRE and a local destination address).
 * This really is simple
 */
void
#if __STDC__
gre_input(struct mbuf *m, ...)
#else
gre_input(m, va_alist)
        struct mbuf *m;
        va_dcl
#endif
{
        register int hlen,ret;
	va_list ap;

        va_start(ap, m);
        hlen = va_arg(ap, int);
        va_end(ap);

	ret=gre_input2(m,hlen,IPPROTO_GRE);
	/* 
 	 * ret == 0 : packet not processed, but input from here
	 * means no matching tunnel that is up is found,
	 * so we can just free the mbuf and return
	 */
	if (ret==0)
		m_freem(m);
}

/*
 * decapsulate.
 * Does the real work and is called from gre_input() (above)
 * and also from ipip_input()  (ip_mroute.c)
 * returns 0 if packet is not yet processed 
 * and 1 if it needs no further processing
 * proto is the protocol number of the "calling" foo_input()
 * routine.
 */

int
gre_input2(struct mbuf *m ,int hlen,u_char proto)
{
        register struct ip *ip = mtod(m, struct ip *);
	register struct greip *gip = mtod(m, struct greip *);
        register int s,i;
        register struct ifqueue *ifq;
	struct gre_softc *sc;
	u_short flags;

	i=0;	
	do {
		sc=&gre_softc[i];
		if((sc->g_dst.s_addr == ip->ip_src.s_addr) &&
		    (sc->g_src.s_addr == ip->ip_dst.s_addr))
			break;
		i++;
	} while(i<NGRE);
		  
	if ((i<NGRE) && ((sc->sc_if.if_flags & IFF_UP) == IFF_UP)) {	
		/* found matching tunnel that is up */
		sc->sc_if.if_ipackets++;  
		sc->sc_if.if_ibytes+=m->m_pkthdr.len;
	} else {   /* no matching tunnel or match, but tunnel down */
		return 0;
	}


	switch (proto) {
	case IPPROTO_GRE:
		hlen += sizeof (struct gre_h);

		/* process GRE flags as packet can be of variable len */
		flags = ntohs(gip->gi_flags);

		/* Checksum & Offset are present */
		if ((flags & GRE_CP) | (flags & GRE_RP))
			hlen += 4;
		/* We don't support routing fields (variable length) */
		if (flags & GRE_RP)
			return(0);
		if (flags & GRE_KP)
			hlen += 4;
		if (flags & GRE_SP)
			hlen +=4;

		switch (ntohs(gip->gi_ptype)) { /* ethertypes */
		case ETHERTYPE_IP: /* shouldn't need a schednetisr(), as */
			ifq = &ipintrq;          /* we are in ip_input */
			break;
#ifdef NS
		case ETHERTYPE_NS:
			ifq = &nsintrq;
			schednetisr(NETISR_NS);
			break;
#endif
#ifdef NETATALK
		case ETHERTYPE_AT:
			ifq = &atintrq1;
			schednetisr(NETISR_ATALK);
			break;
#endif
		case ETHERTYPE_IPV6:
			/* FALLTHROUGH */
		default:	   /* others not yet supported */
			return(0);
		}
		break;
	case IPPROTO_IPIP:
		ifq = &ipintrq;
		break;
	default:
		/* others not yet supported */
		return(0);
	}
		

	m->m_data += hlen; 
	m->m_len -= hlen;
	m->m_pkthdr.len -= hlen;

	s = splimp();		/* possible */
	if (IF_QFULL(ifq)) {
		IF_DROP(ifq);
		m_freem(m);
	} else {
		IF_ENQUEUE(ifq,m);
	}
	splx(s);

	return(1);	/* packet is done, no further processing needed */
}



#ifndef MROUTING

/*
 * Well if MROUTING is not defined, the ipip_input function in 
 * ip_mroute.c gets never called, so IPIP packets coming over the
 * tunnel can't be processed. (Not exactly true. They go to raw ip
 * that also can't cope ).
 */

void     
#if __STDC__
gre_ipip_input(struct mbuf *m, ...)
#else    
ipip_input(m, va_alist)
        struct mbuf *m;
        va_dcl
#endif
{
        register int hlen;
	va_list ap;

        va_start(ap, m);
        hlen = va_arg(ap, int);
        va_end(ap);

	/*
	 * return == 0 means, that the packet is not preocessed, but
	 * as we are the only ones to decapsulate IPIP, and are not able,
	 * then we can just dump it.
	 */
	if(gre_input2(m,hlen,IPPROTO_IPIP)==0)
		m_freem(m);

}
#endif /* ifndef MROUTING */

#endif /* if NGRE > 0 */