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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
|
/* $OpenBSD: ip_ipip.c,v 1.79 2017/05/26 15:56:51 bluhm 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).
*
* The original version of 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.
* Copyright (c) 2001, Angelos D. Keromytis.
*
* Permission to use, copy, and modify this software with or 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 "pf.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/route.h>
#include <net/netisr.h>
#include <net/bpf.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/in_pcb.h>
#include <netinet/ip_var.h>
#include <netinet/ip_ecn.h>
#include <netinet/ip_ipip.h>
#ifdef MROUTING
#include <netinet/ip_mroute.h>
#endif
#include "bpfilter.h"
#if NPF > 0
#include <net/pfvar.h>
#endif
#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.ipip.allow value. Zero means drop them, all else is acceptance.
*/
int ipip_allow = 0;
struct cpumem *ipipcounters;
void
ipip_init(void)
{
ipipcounters = counters_alloc(ipips_ncounters);
}
/*
* Really only a wrapper for ipip_input_gif(), for use with pr_input.
*/
int
ipip_input(struct mbuf **mp, int *offp, int proto, int af)
{
/* If we do not accept IP-in-IP explicitly, drop. */
if (!ipip_allow && ((*mp)->m_flags & (M_AUTH|M_CONF)) == 0) {
DPRINTF(("%s: dropped due to policy\n", __func__));
ipipstat_inc(ipips_pdrops);
m_freem(*mp);
return IPPROTO_DONE;
}
return ipip_input_gif(mp, offp, proto, af, NULL);
}
/*
* ipip_input gets called when we receive an IP{46} 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 ph_ifidx element
* will contain the index of the encX interface associated with the
* tunnel.
*/
int
ipip_input_gif(struct mbuf **mp, int *offp, int proto, int oaf,
struct ifnet *gifp)
{
struct mbuf *m = *mp;
int iphlen = *offp;
struct sockaddr_in *sin;
struct ifnet *ifp;
struct niqueue *ifq = NULL;
struct ip *ipo;
#ifdef INET6
struct sockaddr_in6 *sin6;
struct ip6_hdr *ip6;
#endif
int mode, hlen;
u_int8_t itos, otos;
sa_family_t iaf;
ipipstat_inc(ipips_ipackets);
switch (oaf) {
case AF_INET:
hlen = sizeof(struct ip);
break;
#ifdef INET6
case AF_INET6:
hlen = sizeof(struct ip6_hdr);
break;
#endif
default:
unhandled_af(oaf);
}
/* Bring the IP header in the first mbuf, if not there already */
if (m->m_len < hlen) {
if ((m = *mp = m_pullup(m, hlen)) == NULL) {
DPRINTF(("%s: m_pullup() failed\n", __func__));
ipipstat_inc(ipips_hdrops);
return IPPROTO_DONE;
}
}
/* Keep outer ecn field. */
switch (oaf) {
case AF_INET:
ipo = mtod(m, struct ip *);
otos = ipo->ip_tos;
break;
#ifdef INET6
case AF_INET6:
ip6 = mtod(m, struct ip6_hdr *);
otos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
break;
#endif
}
/* Remove outer IP header */
m_adj(m, iphlen);
/* Sanity check */
if (m->m_pkthdr.len < sizeof(struct ip)) {
ipipstat_inc(ipips_hdrops);
m_freem(m);
return IPPROTO_DONE;
}
switch (proto) {
case IPPROTO_IPV4:
hlen = sizeof(struct ip);
break;
#ifdef INET6
case IPPROTO_IPV6:
hlen = sizeof(struct ip6_hdr);
break;
#endif
default:
ipipstat_inc(ipips_family);
m_freem(m);
return IPPROTO_DONE;
}
/*
* Bring the inner header into the first mbuf, if not there already.
*/
if (m->m_len < hlen) {
if ((m = *mp = m_pullup(m, hlen)) == NULL) {
DPRINTF(("%s: m_pullup() failed\n", __func__));
ipipstat_inc(ipips_hdrops);
return IPPROTO_DONE;
}
}
/*
* 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.
*/
/* Some sanity checks in the inner IP header */
switch (proto) {
case IPPROTO_IPV4:
ipo = mtod(m, struct ip *);
#ifdef INET6
ip6 = NULL;
#endif
itos = ipo->ip_tos;
mode = m->m_flags & (M_AUTH|M_CONF) ?
ECN_ALLOWED_IPSEC : ECN_ALLOWED;
if (!ip_ecn_egress(mode, &otos, &ipo->ip_tos)) {
DPRINTF(("%s: ip_ecn_egress() failed\n", __func__));
ipipstat_inc(ipips_pdrops);
m_freem(m);
return IPPROTO_DONE;
}
/* re-calculate the checksum if ip_tos was changed */
if (itos != ipo->ip_tos) {
hlen = ipo->ip_hl << 2;
if (m->m_pkthdr.len >= hlen) {
ipo->ip_sum = 0;
ipo->ip_sum = in_cksum(m, hlen);
}
}
break;
#ifdef INET6
case IPPROTO_IPV6:
ipo = NULL;
ip6 = mtod(m, struct ip6_hdr *);
itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
if (!ip_ecn_egress(ECN_ALLOWED, &otos, &itos)) {
DPRINTF(("%s: ip_ecn_egress() failed\n", __func__));
ipipstat_inc(ipips_pdrops);
m_freem(m);
return IPPROTO_DONE;
}
ip6->ip6_flow &= ~htonl(0xff << 20);
ip6->ip6_flow |= htonl((u_int32_t) itos << 20);
break;
#endif
default:
ipo = NULL;
#ifdef INET6
ip6 = NULL;
#endif
}
/* Check for local address spoofing. */
ifp = if_get(m->m_pkthdr.ph_ifidx);
if (((ifp == NULL) || !(ifp->if_flags & IFF_LOOPBACK)) &&
ipip_allow != 2) {
struct sockaddr_storage ss;
struct rtentry *rt;
if_put(ifp);
memset(&ss, 0, sizeof(ss));
if (ipo) {
sin = (struct sockaddr_in *)&ss;
sin->sin_family = AF_INET;
sin->sin_len = sizeof(*sin);
sin->sin_addr = ipo->ip_src;
#ifdef INET6
} else if (ip6) {
sin6 = (struct sockaddr_in6 *)&ss;
sin6->sin6_family = AF_INET6;
sin6->sin6_len = sizeof(*sin6);
sin6->sin6_addr = ip6->ip6_src;
#endif /* INET6 */
}
rt = rtalloc(sstosa(&ss), 0, m->m_pkthdr.ph_rtableid);
if ((rt != NULL) && (rt->rt_flags & RTF_LOCAL)) {
ipipstat_inc(ipips_spoof);
m_freem(m);
rtfree(rt);
return IPPROTO_DONE;
}
rtfree(rt);
} else {
if_put(ifp);
}
/* Statistics */
ipipstat_add(ipips_ibytes, m->m_pkthdr.len - iphlen);
/*
* 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 enc interface, which
* will allow a packet filter to distinguish between secure and
* untrusted packets.
*/
switch (proto) {
case IPPROTO_IPV4:
ifq = &ipintrq;
iaf = AF_INET;
break;
#ifdef INET6
case IPPROTO_IPV6:
ifq = &ip6intrq;
iaf = AF_INET6;
break;
#endif
default:
panic("%s: should never reach here", __func__);
}
#if NBPFILTER > 0
if (gifp && gifp->if_bpf)
bpf_mtap_af(gifp->if_bpf, iaf, m, BPF_DIRECTION_IN);
#endif
#if NPF > 0
pf_pkt_addr_changed(m);
#endif
if (niq_enqueue(ifq, m) != 0) {
ipipstat_inc(ipips_qfull);
DPRINTF(("%s: packet dropped because of full queue\n",
__func__));
}
return IPPROTO_DONE;
}
int
ipip_output(struct mbuf *m, struct tdb *tdb, struct mbuf **mp, int dummy,
int dummy2)
{
u_int8_t tp, otos;
u_int8_t itos;
struct ip *ipo;
#ifdef INET6
struct ip6_hdr *ip6, *ip6o;
#endif /* INET6 */
#ifdef ENCDEBUG
char buf[INET6_ADDRSTRLEN];
#endif
/* XXX Deal with empty TDB source/destination addresses. */
m_copydata(m, 0, 1, &tp);
tp = (tp >> 4) & 0xff; /* Get the IP version number. */
switch (tdb->tdb_dst.sa.sa_family) {
case AF_INET:
if (tdb->tdb_src.sa.sa_family != AF_INET ||
tdb->tdb_src.sin.sin_addr.s_addr == INADDR_ANY ||
tdb->tdb_dst.sin.sin_addr.s_addr == INADDR_ANY) {
DPRINTF(("%s: unspecified tunnel endpoind "
"address in SA %s/%08x\n", __func__,
ipsp_address(&tdb->tdb_dst, buf, sizeof(buf)),
ntohl(tdb->tdb_spi)));
ipipstat_inc(ipips_unspec);
m_freem(m);
*mp = NULL;
return EINVAL;
}
M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
if (m == NULL) {
DPRINTF(("%s: M_PREPEND failed\n", __func__));
ipipstat_inc(ipips_hdrops);
*mp = NULL;
return ENOBUFS;
}
ipo = mtod(m, struct ip *);
ipo->ip_v = IPVERSION;
ipo->ip_hl = 5;
ipo->ip_len = htons(m->m_pkthdr.len);
ipo->ip_ttl = ip_defttl;
ipo->ip_sum = 0;
ipo->ip_src = tdb->tdb_src.sin.sin_addr;
ipo->ip_dst = tdb->tdb_dst.sin.sin_addr;
/*
* We do the htons() to prevent snoopers from determining our
* endianness.
*/
ipo->ip_id = htons(ip_randomid());
/* If the inner protocol is IP... */
if (tp == IPVERSION) {
/* Save ECN notification */
m_copydata(m, sizeof(struct ip) +
offsetof(struct ip, ip_tos),
sizeof(u_int8_t), (caddr_t) &itos);
ipo->ip_p = IPPROTO_IPIP;
/*
* We should be keeping tunnel soft-state and
* send back ICMPs if needed.
*/
m_copydata(m, sizeof(struct ip) +
offsetof(struct ip, ip_off),
sizeof(u_int16_t), (caddr_t) &ipo->ip_off);
ipo->ip_off = ntohs(ipo->ip_off);
ipo->ip_off &= ~(IP_DF | IP_MF | IP_OFFMASK);
ipo->ip_off = htons(ipo->ip_off);
}
#ifdef INET6
else if (tp == (IPV6_VERSION >> 4)) {
u_int32_t itos32;
/* Save ECN notification. */
m_copydata(m, sizeof(struct ip) +
offsetof(struct ip6_hdr, ip6_flow),
sizeof(u_int32_t), (caddr_t) &itos32);
itos = ntohl(itos32) >> 20;
ipo->ip_p = IPPROTO_IPV6;
ipo->ip_off = 0;
}
#endif /* INET6 */
else {
m_freem(m);
*mp = NULL;
ipipstat_inc(ipips_family);
return EAFNOSUPPORT;
}
otos = 0;
ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
ipo->ip_tos = otos;
break;
#ifdef INET6
case AF_INET6:
if (IN6_IS_ADDR_UNSPECIFIED(&tdb->tdb_dst.sin6.sin6_addr) ||
tdb->tdb_src.sa.sa_family != AF_INET6 ||
IN6_IS_ADDR_UNSPECIFIED(&tdb->tdb_src.sin6.sin6_addr)) {
DPRINTF(("%s: unspecified tunnel endpoind "
"address in SA %s/%08x\n", __func__,
ipsp_address(&tdb->tdb_dst, buf, sizeof(buf)),
ntohl(tdb->tdb_spi)));
ipipstat_inc(ipips_unspec);
m_freem(m);
*mp = NULL;
return ENOBUFS;
}
/* If the inner protocol is IPv6, clear link local scope */
if (tp == (IPV6_VERSION >> 4)) {
/* scoped address handling */
ip6 = mtod(m, struct ip6_hdr *);
if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
ip6->ip6_src.s6_addr16[1] = 0;
if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
ip6->ip6_dst.s6_addr16[1] = 0;
}
M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
if (m == NULL) {
DPRINTF(("%s: M_PREPEND failed\n", __func__));
ipipstat_inc(ipips_hdrops);
*mp = NULL;
return ENOBUFS;
}
/* Initialize IPv6 header */
ip6o = mtod(m, struct ip6_hdr *);
ip6o->ip6_flow = 0;
ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
ip6o->ip6_vfc |= IPV6_VERSION;
ip6o->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6o));
ip6o->ip6_hlim = ip_defttl;
in6_embedscope(&ip6o->ip6_src, &tdb->tdb_src.sin6, NULL);
in6_embedscope(&ip6o->ip6_dst, &tdb->tdb_dst.sin6, NULL);
if (tp == IPVERSION) {
/* Save ECN notification */
m_copydata(m, sizeof(struct ip6_hdr) +
offsetof(struct ip, ip_tos), sizeof(u_int8_t),
(caddr_t) &itos);
/* This is really IPVERSION. */
ip6o->ip6_nxt = IPPROTO_IPIP;
}
else
if (tp == (IPV6_VERSION >> 4)) {
u_int32_t itos32;
/* Save ECN notification. */
m_copydata(m, sizeof(struct ip6_hdr) +
offsetof(struct ip6_hdr, ip6_flow),
sizeof(u_int32_t), (caddr_t) &itos32);
itos = ntohl(itos32) >> 20;
ip6o->ip6_nxt = IPPROTO_IPV6;
} else {
m_freem(m);
*mp = NULL;
ipipstat_inc(ipips_family);
return EAFNOSUPPORT;
}
otos = 0;
ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
ip6o->ip6_flow |= htonl((u_int32_t) otos << 20);
break;
#endif /* INET6 */
default:
DPRINTF(("%s: unsupported protocol family %d\n", __func__,
tdb->tdb_dst.sa.sa_family));
m_freem(m);
*mp = NULL;
ipipstat_inc(ipips_family);
return EAFNOSUPPORT;
}
ipipstat_inc(ipips_opackets);
*mp = m;
if (tdb->tdb_dst.sa.sa_family == AF_INET) {
if (tdb->tdb_xform->xf_type == XF_IP4)
tdb->tdb_cur_bytes +=
m->m_pkthdr.len - sizeof(struct ip);
ipipstat_add(ipips_obytes, m->m_pkthdr.len - sizeof(struct ip));
}
#ifdef INET6
if (tdb->tdb_dst.sa.sa_family == AF_INET6) {
if (tdb->tdb_xform->xf_type == XF_IP4)
tdb->tdb_cur_bytes +=
m->m_pkthdr.len - sizeof(struct ip6_hdr);
ipipstat_add(ipips_obytes,
m->m_pkthdr.len - sizeof(struct ip6_hdr));
}
#endif /* INET6 */
return 0;
}
#ifdef IPSEC
int
ipe4_attach(void)
{
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;
}
int
ipe4_input(struct mbuf *m, struct tdb *tdb, int hlen, int proto)
{
/* This is a rather serious mistake, so no conditional printing. */
printf("ipe4_input(): should never be called\n");
m_freem(m);
return EINVAL;
}
#endif /* IPSEC */
int
ipip_sysctl_ipipstat(void *oldp, size_t *oldlenp, void *newp)
{
struct ipipstat ipipstat;
CTASSERT(sizeof(ipipstat) == (ipips_ncounters * sizeof(uint64_t)));
memset(&ipipstat, 0, sizeof ipipstat);
counters_read(ipipcounters, (uint64_t *)&ipipstat, ipips_ncounters);
return (sysctl_rdstruct(oldp, oldlenp, newp,
&ipipstat, sizeof(ipipstat)));
}
int
ipip_sysctl(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 IPIPCTL_ALLOW:
return (sysctl_int(oldp, oldlenp, newp, newlen, &ipip_allow));
case IPIPCTL_STATS:
return (ipip_sysctl_ipipstat(oldp, oldlenp, newp));
default:
return (ENOPROTOOPT);
}
/* NOTREACHED */
}
|