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
|
/* $OpenBSD: if_enc.c,v 1.20 2000/02/07 06:09:08 itojun 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.
*
* Copyright (C) 1995, 1996, 1997, 1998 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.
*/
/*
* Encapsulation interface driver.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/proc.h>
#include <machine/cpu.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/netisr.h>
#include <net/route.h>
#include <net/bpf.h>
#include <netinet/ip_ipsp.h>
#include <net/if_enc.h>
#ifdef INET
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/ip_var.h>
#include <netinet/ip.h>
#endif
#ifdef INET6
#ifndef INET
#include <netinet/in.h>
#endif
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#endif /* INET6 */
#ifdef ISO
extern struct ifqueue clnlintrq;
#endif
#ifdef NS
extern struct ifqueue nsintrq;
#endif
#include "bpfilter.h"
#include "enc.h"
#ifdef ENCDEBUG
#define DPRINTF(x) do { if (encdebug) printf x ; } while (0)
#else
#define DPRINTF(x)
#endif
#ifndef offsetof
#define offsetof(s, e) ((int)&((s *)0)->e)
#endif
struct enc_softc encif[NENC];
void encattach __P((int));
int encoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
struct rtentry *));
int encioctl __P((struct ifnet *, u_long, caddr_t));
void encrtrequest __P((int, struct rtentry *, struct sockaddr *));
void encstart __P((struct ifnet *));
extern int ifqmaxlen;
void
encattach(int nenc)
{
struct ifnet *ifp;
int i;
bzero(encif, sizeof(encif));
for (i = 0; i < NENC; i++)
{
ifp = &encif[i].sc_if;
sprintf(ifp->if_xname, "enc%d", i);
ifp->if_softc = &encif[i];
ifp->if_mtu = ENCMTU;
ifp->if_ioctl = encioctl;
ifp->if_output = encoutput;
ifp->if_start = encstart;
ifp->if_type = IFT_ENC;
ifp->if_snd.ifq_maxlen = ifqmaxlen;
ifp->if_hdrlen = ENC_HDRLEN;
if_attach(ifp);
#if NBPFILTER > 0
bpfattach(&encif[i].sc_if.if_bpf, ifp, DLT_ENC, ENC_HDRLEN);
#endif
}
}
/*
* Start output on the enc interface.
*/
void
encstart(ifp)
struct ifnet *ifp;
{
struct mbuf *m;
int s;
#ifndef IPSEC
for (;;)
{
s = splimp();
IF_DROP(&ifp->if_snd);
IF_DEQUEUE(&ifp->if_snd, m);
splx(s);
if (m == NULL)
return;
else
m_freem(m);
}
#else /* IPSEC */
struct enc_softc *enc = ifp->if_softc;
int err = 0, protoflag;
struct mbuf *mp;
struct tdb *tdb;
/* If the interface is not setup, flush the queue */
if ((enc->sc_spi == 0) && (enc->sc_sproto == 0) &&
((enc->sc_dst.sa.sa_family == AF_INET) ||
(enc->sc_dst.sa.sa_family == AF_INET6)))
{
DPRINTF(("%s: not initialized with SA\n", ifp->if_xname));
for (;;)
{
s = splimp();
IF_DROP(&ifp->if_snd);
IF_DEQUEUE(&ifp->if_snd, m);
splx(s);
if (m == NULL)
return;
else
m_freem(m);
}
/* Unreachable */
}
/* Find what type of processing we need to do */
tdb = gettdb(enc->sc_spi, &(enc->sc_dst), enc->sc_sproto);
if (tdb == NULL)
{
DPRINTF(("%s: SA non-existant\n", ifp->if_xname));
/* Flush the queue */
for (;;)
{
s = splimp();
IF_DROP(&ifp->if_snd);
IF_DEQUEUE(&ifp->if_snd, m);
splx(s);
if (m == NULL)
return;
else
m_freem(m);
}
}
/* See if we need to notify a key mgmt. daemon to setup SAs */
if (ntohl(enc->sc_spi) == SPI_LOCAL_USE)
{
/*
* XXX Can't do this for now, as there's no way for
* XXX key mgmt. to specify link-layer properties
* XXX (e.g., encrypt everything on this interface)
*/
#ifdef notyet
if (tdb->tdb_satype != SADB_X_SATYPE_BYPASS)
pfkeyv2_acquire(tdb, 0); /* No point checking for errors */
#endif
/* Flush the queue */
for (;;)
{
s = splimp();
IF_DROP(&ifp->if_snd);
IF_DEQUEUE(&ifp->if_snd, m);
splx(s);
if (m == NULL)
return;
else
m_freem(m);
}
/* Unreachable */
}
/* IPsec-process all packets in the queue */
for (;;)
{
/* Get a packet from the queue */
s = splimp();
IF_DEQUEUE(&ifp->if_snd, m);
splx(s);
if (m == NULL) /* Empty queue */
return;
/* First, we encapsulate in etherip */
err = etherip_output(m, tdb, &mp, 0, 0); /* Last 2 args not used */
if ((mp == NULL) || err)
{
/* Just skip this frame */
IF_DROP(&ifp->if_snd);
if (mp)
m_freem(mp);
continue;
}
else
{
m = mp;
mp = NULL;
}
protoflag = tdb->tdb_dst.sa.sa_family;
/* IPsec packet processing -- skip encapsulation */
err = ipsp_process_packet(m, &mp, tdb, &protoflag, 1);
if ((mp == NULL) || err)
{
IF_DROP(&ifp->if_snd);
if (mp)
m_freem(mp);
continue;
}
else
{
m = mp;
mp = NULL;
}
#ifdef INET
/* Send the packet on its way, no point checking for errors here */
if (protoflag == AF_INET)
ip_output(m, NULL, NULL, IP_ENCAPSULATED | IP_RAWOUTPUT, NULL, NULL);
#endif /* INET */
#ifdef INET6
/* Send the packet on its way, no point checking for errors here */
if (protoflag == AF_INET6)
ip6_output(m, NULL, NULL, IP_ENCAPSULATED | IP_RAWOUTPUT,
NULL, NULL);
#endif /* INET6 */
/* XXX Should find a way to avoid bridging-loops, some mbuf flag ? */
}
#endif /* IPSEC */
}
/*
* Shamelessly stolen from looutput()
*/
int
encoutput(ifp, m, dst, rt)
struct ifnet *ifp;
register struct mbuf *m;
struct sockaddr *dst;
register struct rtentry *rt;
{
register struct ifqueue *ifq = 0;
int s, isr;
if ((m->m_flags & M_PKTHDR) == 0)
panic("encoutput(): no HDR");
ifp->if_lastchange = time;
m->m_pkthdr.rcvif = ifp;
if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE))
{
m_freem(m);
return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
}
ifp->if_opackets++;
ifp->if_obytes += m->m_pkthdr.len;
switch (dst->sa_family)
{
#ifdef INET
case AF_INET:
ifq = &ipintrq;
isr = NETISR_IP;
break;
#endif
#ifdef INET6
case AF_INET6:
ifq = &ip6intrq;
isr = NETISR_IPV6;
break;
#endif
#ifdef NS
case AF_NS:
ifq = &nsintrq;
isr = NETISR_NS;
break;
#endif
#ifdef ISO
case AF_ISO:
ifq = &clnlintrq;
isr = NETISR_ISO;
break;
#endif
default:
m_freem(m);
return (EAFNOSUPPORT);
}
s = splimp();
if (IF_QFULL(ifq))
{
IF_DROP(ifq);
m_freem(m);
splx(s);
return (ENOBUFS);
}
IF_ENQUEUE(ifq, m);
schednetisr(isr);
/* Statistics */
ifp->if_ipackets++;
ifp->if_ibytes += m->m_pkthdr.len;
splx(s);
return (0);
}
/* ARGSUSED */
void
encrtrequest(cmd, rt, sa)
int cmd;
struct rtentry *rt;
struct sockaddr *sa;
{
if (rt)
rt->rt_rmx.rmx_mtu = ENCMTU;
}
/* ARGSUSED */
int
encioctl(ifp, cmd, data)
register struct ifnet *ifp;
u_long cmd;
caddr_t data;
{
#ifdef IPSEC
struct enc_softc *enc = (struct enc_softc *) ifp->if_softc;
struct ifsa *ifsa = (struct ifsa *) data;
struct proc *prc = curproc; /* XXX */
struct tdb *tdb;
int s, error;
/*
* enc0 does not allow binding of SAs, as it's used for all non-bound
* SAs.
*/
if (ifp->if_softc == &encif[0])
return EOPNOTSUPP;
switch (cmd)
{
case SIOCSIFADDR:
return EOPNOTSUPP;
case SIOCGENCSA:
ifsa->sa_spi = enc->sc_spi;
ifsa->sa_proto = enc->sc_sproto;
bcopy(&enc->sc_dst, &ifsa->sa_dst, enc->sc_dst.sa.sa_len);
break;
case SIOCSENCCLEARSA:
/* Check for superuser */
if ((error = suser(prc->p_ucred, &prc->p_acflag)) != 0)
break;
if (ifsa->sa_proto == 0)
{
/* Clear SA if requested */
if (enc->sc_sproto != 0)
{
s = spltdb();
tdb = gettdb(enc->sc_spi, &enc->sc_dst, enc->sc_sproto);
if (tdb != NULL)
tdb->tdb_interface = 0;
splx(s);
}
bzero(&enc->sc_dst, sizeof(union sockaddr_union));
enc->sc_spi = 0;
enc->sc_sproto = 0;
break;
}
s = spltdb();
tdb = gettdb(ifsa->sa_spi, &ifsa->sa_dst, ifsa->sa_proto);
if (tdb == NULL)
{
splx(s);
error = ENOENT;
break;
}
tdb->tdb_interface = 0;
splx(s);
break;
case SIOCSENCSRCSA:
/* Check for superuser */
if ((error = suser(prc->p_ucred, &prc->p_acflag)) != 0)
break;
if (ifsa->sa_proto == 0)
{
error = ENOENT;
break;
}
s = spltdb();
tdb = gettdb(ifsa->sa_spi, &ifsa->sa_dst, ifsa->sa_proto);
if (tdb == NULL)
{
splx(s);
error = ENOENT;
break;
}
/* Is it already bound ? */
if (tdb->tdb_interface)
{
splx(s);
error = EEXIST;
break;
}
tdb->tdb_interface = (caddr_t) ifp;
splx(s);
break;
case SIOCSENCDSTSA:
/* Check for superuser */
if ((error = suser(prc->p_ucred, &prc->p_acflag)) != 0)
break;
/* Check for pre-existing TDB */
if (enc->sc_sproto != 0)
{
error = EEXIST;
break;
}
s = spltdb();
if (ifsa->sa_proto != 0)
{
tdb = gettdb(ifsa->sa_spi, &ifsa->sa_dst, ifsa->sa_proto);
if (tdb == NULL)
{
splx(s);
error = ENOENT;
break;
}
}
else
{
/* Clear SA if requested */
if (enc->sc_sproto != 0)
{
tdb = gettdb(enc->sc_spi, &enc->sc_dst, enc->sc_sproto);
if (tdb != NULL)
tdb->tdb_interface = 0;
}
bzero(&enc->sc_dst, sizeof(enc->sc_dst));
enc->sc_spi = 0;
enc->sc_sproto = 0;
splx(s);
break;
}
#ifdef INET
if ((ifsa->sa_dst.sa.sa_family == AF_INET) &&
(ifsa->sa_dst.sa.sa_len != sizeof(struct sockaddr_in)))
{
splx(s);
error = EINVAL;
break;
}
#endif /* INET */
#ifdef INET6
if ((ifsa->sa_dst.sa.sa_family == AF_INET6) &&
(ifsa->sa_dst.sa.sa_len != sizeof(struct sockaddr_in6)))
{
splx(s);
error = EINVAL;
break;
}
#endif /* INET6 */
bcopy(&ifsa->sa_dst, &enc->sc_dst, ifsa->sa_dst.sa.sa_len);
enc->sc_spi = ifsa->sa_spi;
enc->sc_sproto = ifsa->sa_proto;
tdb->tdb_interface = (caddr_t) ifp;
splx(s);
break;
default:
error = EINVAL;
break;
}
return (error);
#else /* IPSEC */
return EOPNOTSUPP;
#endif /* IPSEC */
}
|