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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/* $OpenBSD: ip_ip4.c,v 1.31 1999/05/16 22:31:40 niklas Exp $ */
/*
* The authors of this code are John Ioannidis (ji@tla.org),
* Angelos D. Keromytis (kermit@csd.uch.gr) and
* Niels Provos (provos@physnet.uni-hamburg.de).
*
* This code was written by John Ioannidis for BSD/OS in Athens, Greece,
* in November 1995.
*
* Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
* by Angelos D. Keromytis.
*
* Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
* and Niels Provos.
*
* Additional features in 1999 by Angelos D. Keromytis.
*
* Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
* Angelos D. Keromytis and Niels Provos.
*
* Permission to use, copy, and modify this software without fee
* is hereby granted, provided that this entire notice is included in
* all copies of any software which is or includes a copy or
* modification of this software.
* You may use this code under the GNU public license if you so wish. Please
* contribute changes back to the authors under this freer than GPL license
* so that we may further the use of strong encryption without limitations to
* all.
*
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
* MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
* PURPOSE.
*/
/*
* IP-inside-IP processing
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/domain.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <machine/cpu.h>
#include <net/if.h>
#include <net/route.h>
#include <net/netisr.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/in_pcb.h>
#include <netinet/in_var.h>
#include <netinet/ip_var.h>
#include <netinet/ip_icmp.h>
#ifdef MROUTING
#include <netinet/ip_mroute.h>
#endif
#include <sys/socketvar.h>
#include <net/raw_cb.h>
#include <netinet/ip_ipsp.h>
#include <netinet/ip_ip4.h>
#include <dev/rndvar.h>
#ifdef ENCDEBUG
#define DPRINTF(x) if (encdebug) printf x
#else
#define DPRINTF(x)
#endif
/*
* We can control the acceptance of IP4 packets by altering the sysctl
* net.inet.ip4.allow value. Zero means drop them, all else is acceptance.
*/
int ip4_allow = 0;
struct ip4stat ip4stat;
/*
* ip4_input gets called when we receive an encapsulated packet,
* either because we got it at a real interface, or because AH or ESP
* were being used in tunnel mode (in which case the rcvif element will
* contain the address of the encapX interface associated with the tunnel.
*/
void
#if __STDC__
ip4_input(struct mbuf *m, ...)
#else
ip4_input(m, va_alist)
struct mbuf *m;
va_dcl
#endif
{
register struct ifnet *ifp;
register struct ifaddr *ifa;
register struct sockaddr_in *sin;
int iphlen;
struct ip *ipo, *ipi;
struct ifqueue *ifq = NULL;
int s;
va_list ap;
va_start(ap, m);
iphlen = va_arg(ap, int);
va_end(ap);
ip4stat.ip4s_ipackets++;
#ifdef MROUTING
ipo = mtod(m, struct ip *);
if (IN_MULTICAST(((struct ip *)((char *)ipo + iphlen))->ip_dst.s_addr))
{
ipip_input (m, iphlen);
return;
}
#endif MROUTING
/* If we do not accept IP4 explicitly, drop. */
if (!ip4_allow && (m->m_flags & (M_AUTH|M_CONF)) == 0)
{
DPRINTF(("ip4_input(): dropped due to policy\n"));
ip4stat.ip4s_pdrops++;
m_freem(m);
return;
}
/*
* Strip IP options, if any.
*/
if (iphlen > sizeof(struct ip))
{
ip_stripoptions(m, (struct mbuf *) 0);
iphlen = sizeof(struct ip);
}
/*
* Make sure next IP header is in the first mbuf.
*
* Careful here! we are receiving the packet from ipintr;
* this means that the ip_len field has been adjusted to
* not count the ip header, and is also in host order.
*/
ipo = mtod(m, struct ip *);
if (m->m_len < iphlen + sizeof(struct ip))
{
if ((m = m_pullup(m, iphlen + sizeof(struct ip))) == 0)
{
DPRINTF(("ip4_input(): m_pullup() failed\n"));
ip4stat.ip4s_hdrops++;
m_freem(m);
return;
}
ipo = mtod(m, struct ip *);
}
ipi = (struct ip *) ((caddr_t) ipo + iphlen);
/*
* RFC 1853 specifies that the inner TTL should not be touched on
* decapsulation. There's no reason this comment should be here, but
* this is as good as any a position.
*/
if (ipi->ip_v != IPVERSION)
{
DPRINTF(("ip4_input(): wrong version %d on packet from %s to %s (%s->%s)\n", ipi->ip_v, inet_ntoa4(ipo->ip_src), inet_ntoa4(ipo->ip_dst), inet_ntoa4(ipi->ip_src), inet_ntoa4(ipi->ip_dst)));
ip4stat.ip4s_notip4++;
m_freem(m);
return;
}
/*
* If we do not accept IP4 other than part of ESP & AH, we should
* not accept a packet with double ip4 headers neither.
*/
if (!ip4_allow && ipi->ip_p == IPPROTO_IPIP)
{
DPRINTF(("ip4_input(): dropped due to policy\n"));
ip4stat.ip4s_pdrops++;
m_freem(m);
return;
}
/*
* Check remote packets for local address spoofing.
*/
if (m->m_pkthdr.rcvif == NULL ||
!(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK))
{
for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
for (ifa = ifp->if_addrlist.tqh_first;
ifa != 0;
ifa = ifa->ifa_list.tqe_next)
{
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
sin = (struct sockaddr_in *) ifa->ifa_addr;
if (sin->sin_addr.s_addr == ipi->ip_src.s_addr)
{
DPRINTF(("ip_input(): possible local address spoofing detected on packet from %s to %s (%s->%s)\n", inet_ntoa4(ipo->ip_src), inet_ntoa4(ipo->ip_dst), inet_ntoa4(ipi->ip_src), inet_ntoa4(ipi->ip_dst)));
ip4stat.ip4s_spoof++;
m_freem(m);
return;
}
}
}
/* Statistics */
ip4stat.ip4s_ibytes += ntohs(ipi->ip_len);
/*
* Interface pointer is already in first mbuf; chop off the
* `outer' header and reschedule.
*/
m->m_len -= iphlen;
m->m_pkthdr.len -= iphlen;
m->m_data += iphlen;
/* tdbi is only set in esp or ah, if next protocol is udp or tcp */
if (m->m_flags & (M_CONF|M_AUTH))
m->m_pkthdr.tdbi = NULL;
/*
* Interface pointer stays the same; if no IPsec processing has
* been done (or will be done), this will point to a normal
* interface. Otherwise, it'll point to an encap interface, which
* will allow a packet filter to distinguish between secure and
* untrusted packets.
*/
ifq = &ipintrq;
s = splimp(); /* isn't it already? */
if (IF_QFULL(ifq))
{
IF_DROP(ifq);
m_freem(m);
ip4stat.ip4s_qfull++;
splx(s);
DPRINTF(("ip4_input(): packet dropped because of full queue\n"));
return;
}
IF_ENQUEUE(ifq, m);
schednetisr(NETISR_IP);
splx(s);
return;
}
#ifdef IPSEC
int
ipe4_output(struct mbuf *m, struct sockaddr_encap *gw, struct tdb *tdb,
struct mbuf **mp)
{
struct ip *ipo, *ipi;
ushort ilen;
/* Check that the source address, if present, is from AF_INET */
if ((tdb->tdb_src.sa.sa_family != 0) &&
(tdb->tdb_src.sa.sa_family != AF_INET))
{
DPRINTF(("ipe4_output(): IP in protocol-family <%d> attempted, aborting", tdb->tdb_src.sa.sa_family));
m_freem(m);
return EINVAL;
}
/* Check that the destination address is AF_INET */
if (tdb->tdb_dst.sa.sa_family != AF_INET)
{
DPRINTF(("ipe4_output(): IP in protocol-family <%d> attempted, aborting", tdb->tdb_dst.sa.sa_family));
m_freem(m);
return EINVAL;
}
ip4stat.ip4s_opackets++;
ipi = mtod(m, struct ip *);
ilen = ntohs(ipi->ip_len);
M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
if (m == 0)
{
DPRINTF(("ipe4_output(): M_PREPEND failed\n"));
return ENOBUFS;
}
ipo = mtod(m, struct ip *);
ipo->ip_v = IPVERSION;
ipo->ip_hl = 5;
ipo->ip_tos = ipi->ip_tos;
ipo->ip_len = htons(ilen + sizeof(struct ip));
ipo->ip_ttl = ip_defttl;
ipo->ip_p = IPPROTO_IPIP;
ipo->ip_id = ip_randomid();
HTONS(ipo->ip_id);
/*
* XXX We should be keeping tunnel soft-state and send back ICMPs
* if needed
*/
ipo->ip_off = ipi->ip_off & ~(IP_DF | IP_MF | IP_OFFMASK);
ipo->ip_sum = 0;
ipo->ip_src = tdb->tdb_src.sin.sin_addr;
ipo->ip_dst = tdb->tdb_dst.sin.sin_addr;
*mp = m;
/* Update the counters */
if (tdb->tdb_xform->xf_type == XF_IP4)
tdb->tdb_cur_bytes += ntohs(ipo->ip_len) - (ipo->ip_hl << 2);
ip4stat.ip4s_obytes += ntohs(ipo->ip_len) - (ipo->ip_hl << 2);
return 0;
}
int
ipe4_attach()
{
return 0;
}
int
ipe4_init(struct tdb *tdbp, struct xformsw *xsp, struct ipsecinit *ii)
{
tdbp->tdb_xform = xsp;
return 0;
}
int
ipe4_zeroize(struct tdb *tdbp)
{
return 0;
}
void
ipe4_input(struct mbuf *m, ...)
{
/* This is a rather serious mistake, so no conditional printing */
printf("ipe4_input(): should never be called\n");
if (m)
m_freem(m);
}
#endif /* IPSEC */
int
ip4_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
int *name;
u_int namelen;
void *oldp;
size_t *oldlenp;
void *newp;
size_t newlen;
{
/* All sysctl names at this level are terminal. */
if (namelen != 1)
return (ENOTDIR);
switch (name[0]) {
case IP4CTL_ALLOW:
return (sysctl_int(oldp, oldlenp, newp, newlen, &ip4_allow));
default:
return (ENOPROTOOPT);
}
/* NOTREACHED */
}
|