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
|
/*-
* Copyright (c) 1999, 2000 Boris Popov
* All rights reserved.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*
* $FreeBSD$
*/
#include "opt_inet.h"
#include "opt_ipx.h"
#include "opt_ef.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sockio.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/syslog.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <net/ethernet.h>
#include <net/if_llc.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/netisr.h>
#include <net/route.h>
#include <net/bpf.h>
#ifdef INET
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/if_ether.h>
#endif
#ifdef IPX
#include <netipx/ipx.h>
#include <netipx/ipx_if.h>
#endif
/* internal frame types */
#define ETHER_FT_EII 0 /* Ethernet_II - default */
#define ETHER_FT_8023 1 /* 802.3 (Novell) */
#define ETHER_FT_8022 2 /* 802.2 */
#define ETHER_FT_SNAP 3 /* SNAP */
#define EF_NFT 4 /* total number of frame types */
#ifdef EF_DEBUG
#define EFDEBUG(format, args...) printf("%s: "format, __func__ ,## args)
#else
#define EFDEBUG(format, args...)
#endif
#define EFERROR(format, args...) printf("%s: "format, __func__ ,## args)
struct efnet {
struct arpcom ef_ac;
struct ifnet * ef_ifp;
};
struct ef_link {
SLIST_ENTRY(ef_link) el_next;
struct ifnet *el_ifp; /* raw device for this clones */
struct efnet *el_units[EF_NFT]; /* our clones */
};
static SLIST_HEAD(ef_link_head, ef_link) efdev = {NULL};
static int efcount;
extern int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
extern int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
struct sockaddr *dst, short *tp, int *hlen);
/*
static void ef_reset (struct ifnet *);
*/
static int ef_attach(struct efnet *sc);
static int ef_detach(struct efnet *sc);
static void ef_init(void *);
static int ef_ioctl(struct ifnet *, u_long, caddr_t);
static void ef_start(struct ifnet *);
static int ef_input(struct ifnet*, struct ether_header *, struct mbuf *);
static int ef_output(struct ifnet *ifp, struct mbuf **mp,
struct sockaddr *dst, short *tp, int *hlen);
static int ef_load(void);
static int ef_unload(void);
/*
* Install the interface, most of structure initialization done in ef_clone()
*/
static int
ef_attach(struct efnet *sc)
{
struct ifnet *ifp = (struct ifnet*)&sc->ef_ac.ac_if;
struct ifaddr *ifa2;
struct sockaddr_dl *sdl2;
ifp->if_output = ether_output;
ifp->if_start = ef_start;
ifp->if_watchdog = NULL;
ifp->if_init = ef_init;
ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
/*
* Attach the interface
*/
ifa2 = ifaddr_byindex(sc->ef_ifp->if_index);
sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
ether_ifattach(ifp, LLADDR(sdl2));
ifp->if_resolvemulti = 0;
ifp->if_type = IFT_XETHER;
ifp->if_flags |= IFF_RUNNING;
bcopy(LLADDR(sdl2), sc->ef_ac.ac_enaddr, ETHER_ADDR_LEN);
EFDEBUG("%s%d: attached\n", ifp->if_name, ifp->if_unit);
return 1;
}
/*
* This is for _testing_only_, just removes interface from interfaces list
*/
static int
ef_detach(struct efnet *sc)
{
struct ifnet *ifp = (struct ifnet*)&sc->ef_ac.ac_if;
int s;
s = splimp();
if (ifp->if_flags & IFF_UP) {
if_down(ifp);
if (ifp->if_flags & IFF_RUNNING) {
/* find internet addresses and delete routes */
register struct ifaddr *ifa;
TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
rtinit(ifa, (int)RTM_DELETE, 0);
}
}
}
IFNET_WLOCK();
TAILQ_REMOVE(&ifnet, ifp, if_link);
IFNET_WUNLOCK();
splx(s);
return 0;
}
static void
ef_init(void *foo) {
return;
}
static int
ef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
{
/* struct ef_link *sc = (struct ef_link*)ifp->if_softc;*/
struct ifaddr *ifa = (struct ifaddr*)data;
int s, error;
EFDEBUG("IOCTL %ld for %s%d\n", cmd, ifp->if_name, ifp->if_unit);
error = 0;
s = splimp();
switch (cmd) {
case SIOCSIFFLAGS:
error = 0;
break;
case SIOCSIFADDR:
if (ifp->if_unit == ETHER_FT_8023 &&
ifa->ifa_addr->sa_family != AF_IPX) {
error = EAFNOSUPPORT;
break;
}
ifp->if_flags |= IFF_UP;
/* FALL THROUGH */
default:
error = ether_ioctl(ifp, cmd, data);
break;
}
splx(s);
return error;
}
/*
* Currently packet prepared in the ether_output(), but this can be a better
* place.
*/
static void
ef_start(struct ifnet *ifp)
{
struct efnet *sc = (struct efnet*)ifp->if_softc;
struct ifnet *p;
struct mbuf *m;
ifp->if_flags |= IFF_OACTIVE;
p = sc->ef_ifp;
EFDEBUG("\n");
for (;;) {
IF_DEQUEUE(&ifp->if_snd, m);
if (m == 0)
break;
BPF_MTAP(ifp, m);
if (! IF_HANDOFF(&p->if_snd, m, p)) {
ifp->if_oerrors++;
continue;
}
ifp->if_opackets++;
}
ifp->if_flags &= ~IFF_OACTIVE;
return;
}
/*
* Inline functions do not put additional overhead to procedure call or
* parameter passing but simplify the code
*/
static int __inline
ef_inputEII(struct mbuf *m, struct ether_header *eh, u_short ether_type)
{
int isr;
switch(ether_type) {
#ifdef IPX
case ETHERTYPE_IPX:
isr = NETISR_IPX;
break;
#endif
#ifdef INET
case ETHERTYPE_IP:
if (ipflow_fastforward(m))
return (0);
isr = NETISR_IP;
break;
case ETHERTYPE_ARP:
isr = NETISR_ARP;
break;
#endif
default:
return (EPROTONOSUPPORT);
}
netisr_dispatch(isr, m);
return (0);
}
static int __inline
ef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l,
u_short ether_type)
{
int isr;
switch(ether_type) {
#ifdef IPX
case ETHERTYPE_IPX:
m_adj(m, 8);
isr = NETISR_IPX;
break;
#endif
default:
return (EPROTONOSUPPORT);
}
netisr_dispatch(isr, m);
return (0);
}
static int __inline
ef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l,
u_short ether_type)
{
int isr;
switch(ether_type) {
#ifdef IPX
case 0xe0:
m_adj(m, 3);
isr = NETISR_IPX;
break;
#endif
default:
return (EPROTONOSUPPORT);
}
netisr_dispatch(isr, m);
return (0);
}
/*
* Called from ether_input()
*/
static int
ef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
{
u_short ether_type;
int ft = -1;
struct efnet *efp;
struct ifnet *eifp;
struct llc *l;
struct ef_link *efl;
int isr;
ether_type = ntohs(eh->ether_type);
if (ether_type < ETHERMTU) {
l = mtod(m, struct llc*);
if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
/*
* Novell's "802.3" frame
*/
ft = ETHER_FT_8023;
} else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
/*
* 802.2/SNAP
*/
ft = ETHER_FT_SNAP;
ether_type = ntohs(l->llc_un.type_snap.ether_type);
} else if (l->llc_dsap == l->llc_ssap) {
/*
* 802.3/802.2
*/
ft = ETHER_FT_8022;
ether_type = l->llc_ssap;
}
} else
ft = ETHER_FT_EII;
if (ft == -1) {
EFDEBUG("Unrecognised ether_type %x\n", ether_type);
return EPROTONOSUPPORT;
}
/*
* Check if interface configured for the given frame
*/
efp = NULL;
SLIST_FOREACH(efl, &efdev, el_next) {
if (efl->el_ifp == ifp) {
efp = efl->el_units[ft];
break;
}
}
if (efp == NULL) {
EFDEBUG("Can't find if for %d\n", ft);
return EPROTONOSUPPORT;
}
eifp = &efp->ef_ac.ac_if;
if ((eifp->if_flags & IFF_UP) == 0)
return EPROTONOSUPPORT;
eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
m->m_pkthdr.rcvif = eifp;
if (eifp->if_bpf) {
struct mbuf m0;
m0.m_next = m;
m0.m_len = ETHER_HDR_LEN;
m0.m_data = (char *)eh;
BPF_MTAP(eifp, &m0);
}
/*
* Now we ready to adjust mbufs and pass them to protocol intr's
*/
switch(ft) {
case ETHER_FT_EII:
return (ef_inputEII(m, eh, ether_type));
break;
#ifdef IPX
case ETHER_FT_8023: /* only IPX can be here */
isr = NETISR_IPX;
break;
#endif
case ETHER_FT_SNAP:
return (ef_inputSNAP(m, eh, l, ether_type));
break;
case ETHER_FT_8022:
return (ef_input8022(m, eh, l, ether_type));
break;
default:
EFDEBUG("No support for frame %d and proto %04x\n",
ft, ether_type);
return (EPROTONOSUPPORT);
}
netisr_dispatch(isr, m);
return (0);
}
static int
ef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
int *hlen)
{
struct mbuf *m = *mp;
u_char *cp;
short type;
if (ifp->if_type != IFT_XETHER)
return ENETDOWN;
switch (ifp->if_unit) {
case ETHER_FT_EII:
#ifdef IPX
type = htons(ETHERTYPE_IPX);
#else
return EPFNOSUPPORT;
#endif
break;
case ETHER_FT_8023:
type = htons(m->m_pkthdr.len);
break;
case ETHER_FT_8022:
M_PREPEND(m, ETHER_HDR_LEN + 3, M_TRYWAIT);
if (m == NULL) {
*mp = NULL;
return ENOBUFS;
}
/*
* Ensure that ethernet header and next three bytes
* will fit into single mbuf
*/
m = m_pullup(m, ETHER_HDR_LEN + 3);
if (m == NULL) {
*mp = NULL;
return ENOBUFS;
}
m_adj(m, ETHER_HDR_LEN);
type = htons(m->m_pkthdr.len);
cp = mtod(m, u_char *);
*cp++ = 0xE0;
*cp++ = 0xE0;
*cp++ = 0x03;
*hlen += 3;
break;
case ETHER_FT_SNAP:
M_PREPEND(m, 8, M_TRYWAIT);
if (m == NULL) {
*mp = NULL;
return ENOBUFS;
}
type = htons(m->m_pkthdr.len);
cp = mtod(m, u_char *);
bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
*hlen += 8;
break;
default:
return EPFNOSUPPORT;
}
*mp = m;
*tp = type;
return 0;
}
/*
* Create clone from the given interface
*/
static int
ef_clone(struct ef_link *efl, int ft)
{
struct efnet *efp;
struct ifnet *eifp;
struct ifnet *ifp = efl->el_ifp;
char cbuf[IFNAMSIZ], *ifname;
int ifnlen;
efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR,
M_WAITOK | M_ZERO);
if (efp == NULL)
return ENOMEM;
efp->ef_ifp = ifp;
eifp = &efp->ef_ac.ac_if;
ifnlen = 1 + snprintf(cbuf, sizeof(cbuf), "%s%df", ifp->if_name,
ifp->if_unit);
ifname = (char*)malloc(ifnlen, M_IFADDR, M_WAITOK);
eifp->if_name = strcpy(ifname, cbuf);
eifp->if_unit = ft;
eifp->if_softc = efp;
if (ifp->if_ioctl)
eifp->if_ioctl = ef_ioctl;
efl->el_units[ft] = efp;
return 0;
}
static int
ef_load(void)
{
struct ifnet *ifp;
struct efnet *efp;
struct ef_link *efl = NULL;
int error = 0, d;
IFNET_RLOCK();
TAILQ_FOREACH(ifp, &ifnet, if_link) {
if (ifp->if_type != IFT_ETHER) continue;
EFDEBUG("Found interface %s%d\n", ifp->if_name, ifp->if_unit);
efl = (struct ef_link*)malloc(sizeof(struct ef_link),
M_IFADDR, M_WAITOK | M_ZERO);
if (efl == NULL) {
error = ENOMEM;
break;
}
efl->el_ifp = ifp;
#ifdef ETHER_II
error = ef_clone(efl, ETHER_FT_EII);
if (error) break;
#endif
#ifdef ETHER_8023
error = ef_clone(efl, ETHER_FT_8023);
if (error) break;
#endif
#ifdef ETHER_8022
error = ef_clone(efl, ETHER_FT_8022);
if (error) break;
#endif
#ifdef ETHER_SNAP
error = ef_clone(efl, ETHER_FT_SNAP);
if (error) break;
#endif
efcount++;
SLIST_INSERT_HEAD(&efdev, efl, el_next);
}
IFNET_RUNLOCK();
if (error) {
if (efl)
SLIST_INSERT_HEAD(&efdev, efl, el_next);
SLIST_FOREACH(efl, &efdev, el_next) {
for (d = 0; d < EF_NFT; d++)
if (efl->el_units[d])
free(efl->el_units[d], M_IFADDR);
free(efl, M_IFADDR);
}
return error;
}
SLIST_FOREACH(efl, &efdev, el_next) {
for (d = 0; d < EF_NFT; d++) {
efp = efl->el_units[d];
if (efp)
ef_attach(efp);
}
}
ef_inputp = ef_input;
ef_outputp = ef_output;
EFDEBUG("Loaded\n");
return 0;
}
static int
ef_unload(void)
{
struct efnet *efp;
struct ef_link *efl;
int d;
ef_inputp = NULL;
ef_outputp = NULL;
SLIST_FOREACH(efl, &efdev, el_next) {
for (d = 0; d < EF_NFT; d++) {
efp = efl->el_units[d];
if (efp) {
ef_detach(efp);
}
}
}
EFDEBUG("Unloaded\n");
return 0;
}
static int
if_ef_modevent(module_t mod, int type, void *data)
{
switch ((modeventtype_t)type) {
case MOD_LOAD:
return ef_load();
case MOD_UNLOAD:
return ef_unload();
default:
break;
}
return 0;
}
static moduledata_t if_ef_mod = {
"if_ef", if_ef_modevent, NULL
};
DECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);
|