forked from the-tcpdump-group/libpcap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcap-dos.c
1550 lines (1322 loc) · 36.4 KB
/
pcap-dos.c
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of DOS-libpcap
* Ported to DOS/DOSX by G. Vanem <gvanem@yahoo.no>
*
* pcap-dos.c: Interface to PKTDRVR, NDIS2 and 32-bit pmode
* network drivers.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <float.h>
#include <fcntl.h>
#include <limits.h> /* for INT_MAX */
#include <io.h>
#if defined(USE_32BIT_DRIVERS)
#include "msdos/pm_drvr/pmdrvr.h"
#include "msdos/pm_drvr/pci.h"
#include "msdos/pm_drvr/bios32.h"
#include "msdos/pm_drvr/module.h"
#include "msdos/pm_drvr/3c501.h"
#include "msdos/pm_drvr/3c503.h"
#include "msdos/pm_drvr/3c509.h"
#include "msdos/pm_drvr/3c59x.h"
#include "msdos/pm_drvr/3c515.h"
#include "msdos/pm_drvr/3c90x.h"
#include "msdos/pm_drvr/3c575_cb.h"
#include "msdos/pm_drvr/ne.h"
#include "msdos/pm_drvr/wd.h"
#include "msdos/pm_drvr/accton.h"
#include "msdos/pm_drvr/cs89x0.h"
#include "msdos/pm_drvr/rtl8139.h"
#include "msdos/pm_drvr/ne2k-pci.h"
#endif
#include "pcap.h"
#include "pcap-dos.h"
#include "pcap-int.h"
#include "msdos/pktdrvr.h"
#ifdef USE_NDIS2
#include "msdos/ndis2.h"
#endif
#include <arpa/inet.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/if_ether.h>
#include <net/if_packe.h>
#include <tcp.h>
#if defined(USE_32BIT_DRIVERS)
#define FLUSHK() do { _printk_safe = 1; _printk_flush(); } while (0)
#define NDIS_NEXT_DEV &rtl8139_dev
static char *rx_pool = NULL;
static void init_32bit (void);
static int pktq_init (struct rx_ringbuf *q, int size, int num, char *pool);
static int pktq_check (struct rx_ringbuf *q);
static int pktq_inc_out (struct rx_ringbuf *q);
static int pktq_in_index (struct rx_ringbuf *q) LOCKED_FUNC;
static void pktq_clear (struct rx_ringbuf *q) LOCKED_FUNC;
static struct rx_elem *pktq_in_elem (struct rx_ringbuf *q) LOCKED_FUNC;
static struct rx_elem *pktq_out_elem (struct rx_ringbuf *q);
#else
#define FLUSHK() ((void)0)
#define NDIS_NEXT_DEV NULL
#endif
/*
* Internal variables/functions in Watt-32
*/
extern WORD _pktdevclass;
extern BOOL _eth_is_init;
extern int _w32_dynamic_host;
extern int _watt_do_exit;
extern int _watt_is_init;
extern int _w32__bootp_on, _w32__dhcp_on, _w32__rarp_on, _w32__do_mask_req;
extern void (*_w32_usr_post_init) (void);
extern void (*_w32_print_hook)();
extern void dbug_write (const char *); /* Watt-32 lib, pcdbug.c */
extern int pkt_get_mtu (void);
static int ref_count = 0;
static u_long mac_count = 0;
static u_long filter_count = 0;
static volatile BOOL exc_occured = 0;
static struct device *handle_to_device [20];
static int pcap_activate_dos (pcap_t *p);
static int pcap_read_dos (pcap_t *p, int cnt, pcap_handler callback,
u_char *data);
static void pcap_cleanup_dos (pcap_t *p);
static int pcap_stats_dos (pcap_t *p, struct pcap_stat *ps);
static int pcap_sendpacket_dos (pcap_t *p, const void *buf, size_t len);
static int pcap_setfilter_dos (pcap_t *p, struct bpf_program *fp);
static int ndis_probe (struct device *dev);
static int pkt_probe (struct device *dev);
static void close_driver (void);
static int init_watt32 (struct pcap *pcap, const char *dev_name, char *err_buf);
static int first_init (const char *name, char *ebuf, int promisc);
static void watt32_recv_hook (u_char *dummy, const struct pcap_pkthdr *pcap,
const u_char *buf);
/*
* These are the device we always support
*/
static struct device ndis_dev = {
"ndis",
"NDIS2 LanManager",
0,
0,0,0,0,0,0,
NDIS_NEXT_DEV, /* NULL or a 32-bit device */
ndis_probe
};
static struct device pkt_dev = {
"pkt",
"Packet-Driver",
0,
0,0,0,0,0,0,
&ndis_dev,
pkt_probe
};
static struct device *get_device (int fd)
{
if (fd <= 0 || fd >= sizeof(handle_to_device)/sizeof(handle_to_device[0]))
return (NULL);
return handle_to_device [fd-1];
}
/*
* Private data for capturing on MS-DOS.
*/
struct pcap_dos {
void (*wait_proc)(void); /* call proc while waiting */
struct pcap_stat stat;
};
pcap_t *pcap_create_interface (const char *device _U_, char *ebuf)
{
pcap_t *p;
p = PCAP_CREATE_COMMON(ebuf, struct pcap_dos);
if (p == NULL)
return (NULL);
p->activate_op = pcap_activate_dos;
return (p);
}
/*
* Open MAC-driver with name 'device_name' for live capture of
* network packets.
*/
static int pcap_activate_dos (pcap_t *pcap)
{
if (pcap->opt.rfmon) {
/*
* No monitor mode on DOS.
*/
return (PCAP_ERROR_RFMON_NOTSUP);
}
/*
* Turn a negative snapshot value (invalid), a snapshot value of
* 0 (unspecified), or a value bigger than the normal maximum
* value, into the maximum allowed value.
*
* If some application really *needs* a bigger snapshot
* length, we should just increase MAXIMUM_SNAPLEN.
*/
if (pcap->snapshot <= 0 || pcap->snapshot > MAXIMUM_SNAPLEN)
pcap->snapshot = MAXIMUM_SNAPLEN;
if (pcap->snapshot < ETH_MIN+8)
pcap->snapshot = ETH_MIN+8;
if (pcap->snapshot > ETH_MAX) /* silently accept and truncate large MTUs */
pcap->snapshot = ETH_MAX;
pcap->linktype = DLT_EN10MB; /* !! */
pcap->cleanup_op = pcap_cleanup_dos;
pcap->read_op = pcap_read_dos;
pcap->stats_op = pcap_stats_dos;
pcap->inject_op = pcap_sendpacket_dos;
pcap->setfilter_op = pcap_setfilter_dos;
pcap->setdirection_op = NULL; /* Not implemented.*/
pcap->fd = ++ref_count;
pcap->bufsize = ETH_MAX+100; /* add some margin */
pcap->buffer = calloc (pcap->bufsize, 1);
if (pcap->fd == 1) /* first time we're called */
{
if (!init_watt32(pcap, pcap->opt.device, pcap->errbuf) ||
!first_init(pcap->opt.device, pcap->errbuf, pcap->opt.promisc))
{
/* XXX - free pcap->buffer? */
return (PCAP_ERROR);
}
atexit (close_driver);
}
else if (stricmp(active_dev->name,pcap->opt.device))
{
snprintf (pcap->errbuf, PCAP_ERRBUF_SIZE,
"Cannot use different devices simultaneously "
"(`%s' vs. `%s')", active_dev->name, pcap->opt.device);
/* XXX - free pcap->buffer? */
return (PCAP_ERROR);
}
handle_to_device [pcap->fd-1] = active_dev;
return (0);
}
/*
* Poll the receiver queue and call the pcap callback-handler
* with the packet.
*/
static int
pcap_read_one (pcap_t *p, pcap_handler callback, u_char *data)
{
struct pcap_dos *pd = p->priv;
struct pcap_pkthdr pcap;
struct timeval now, expiry = { 0,0 };
int rx_len = 0;
if (p->opt.timeout > 0)
{
gettimeofday2 (&now, NULL);
expiry.tv_usec = now.tv_usec + 1000UL * p->opt.timeout;
expiry.tv_sec = now.tv_sec;
while (expiry.tv_usec >= 1000000L)
{
expiry.tv_usec -= 1000000L;
expiry.tv_sec++;
}
}
while (!exc_occured)
{
volatile struct device *dev; /* might be reset by sig_handler */
dev = get_device (p->fd);
if (!dev)
break;
PCAP_ASSERT (dev->copy_rx_buf || dev->peek_rx_buf);
FLUSHK();
/* If driver has a zero-copy receive facility, peek at the queue,
* filter it, do the callback and release the buffer.
*/
if (dev->peek_rx_buf)
{
PCAP_ASSERT (dev->release_rx_buf);
rx_len = (*dev->peek_rx_buf) (&p->buffer);
}
else
{
rx_len = (*dev->copy_rx_buf) (p->buffer, p->snapshot);
}
if (rx_len > 0) /* got a packet */
{
mac_count++;
FLUSHK();
pcap.caplen = min (rx_len, p->snapshot);
pcap.len = rx_len;
if (callback &&
(!p->fcode.bf_insns || pcap_filter(p->fcode.bf_insns, p->buffer, pcap.len, pcap.caplen)))
{
filter_count++;
/* Fix-me!! Should be time of arrival. Not time of
* capture.
*/
gettimeofday2 (&pcap.ts, NULL);
(*callback) (data, &pcap, p->buffer);
}
if (dev->release_rx_buf)
(*dev->release_rx_buf) (p->buffer);
if (pcap_pkt_debug > 0)
{
if (callback == watt32_recv_hook)
dbug_write ("pcap_recv_hook\n");
else dbug_write ("pcap_read_op\n");
}
FLUSHK();
return (1);
}
/* Has "pcap_breakloop()" been called?
*/
if (p->break_loop) {
/*
* Yes - clear the flag that indicates that it
* has, and return -2 to indicate that we were
* told to break out of the loop.
*/
p->break_loop = 0;
return (-2);
}
/* If not to wait for a packet or pcap_cleanup_dos() called from
* e.g. SIGINT handler, exit loop now.
*/
if (p->opt.timeout <= 0 || (volatile int)p->fd <= 0)
break;
gettimeofday2 (&now, NULL);
if (timercmp(&now, &expiry, >))
break;
#ifndef DJGPP
kbhit(); /* a real CPU hog */
#endif
if (pd->wait_proc)
(*pd->wait_proc)(); /* call yield func */
}
if (rx_len < 0) /* receive error */
{
pd->stat.ps_drop++;
#ifdef USE_32BIT_DRIVERS
if (pcap_pkt_debug > 1)
printk ("pkt-err %s\n", pktInfo.error);
#endif
return (-1);
}
return (0);
}
static int
pcap_read_dos (pcap_t *p, int cnt, pcap_handler callback, u_char *data)
{
int rc, num = 0;
/*
* This can conceivably process more than INT_MAX packets,
* which would overflow the packet count, causing it either
* to look like a negative number, and thus cause us to
* return a value that looks like an error, or overflow
* back into positive territory, and thus cause us to
* return a too-low count.
*
* Therefore, if the packet count is unlimited, we clip
* it at INT_MAX; this routine is not expected to
* process packets indefinitely, so that's not an issue.
*/
if (PACKET_COUNT_IS_UNLIMITED(cnt))
cnt = INT_MAX;
while (num <= cnt)
{
if (p->fd <= 0)
return (-1);
rc = pcap_read_one (p, callback, data);
if (rc > 0)
num++;
if (rc < 0)
break;
_w32_os_yield(); /* allow SIGINT generation, yield to Win95/NT */
}
return (num);
}
/*
* Return network statistics
*/
static int pcap_stats_dos (pcap_t *p, struct pcap_stat *ps)
{
struct net_device_stats *stats;
struct pcap_dos *pd;
struct device *dev = p ? get_device(p->fd) : NULL;
if (!dev)
{
strcpy (p->errbuf, "illegal pcap handle");
return (-1);
}
if (!dev->get_stats || (stats = (*dev->get_stats)(dev)) == NULL)
{
strcpy (p->errbuf, "device statistics not available");
return (-1);
}
FLUSHK();
pd = p->priv;
pd->stat.ps_recv = stats->rx_packets;
pd->stat.ps_drop += stats->rx_missed_errors;
pd->stat.ps_ifdrop = stats->rx_dropped + /* queue full */
stats->rx_errors; /* HW errors */
if (ps)
*ps = pd->stat;
return (0);
}
/*
* Return detailed network/device statistics.
* May be called after 'dev->close' is called.
*/
int pcap_stats_ex (pcap_t *p, struct pcap_stat_ex *se)
{
struct device *dev = p ? get_device (p->fd) : NULL;
if (!dev || !dev->get_stats)
{
pcap_strlcpy (p->errbuf, "detailed device statistics not available",
PCAP_ERRBUF_SIZE);
return (-1);
}
if (!strnicmp(dev->name,"pkt",3))
{
pcap_strlcpy (p->errbuf, "pktdrvr doesn't have detailed statistics",
PCAP_ERRBUF_SIZE);
return (-1);
}
memcpy (se, (*dev->get_stats)(dev), sizeof(*se));
return (0);
}
/*
* Simply store the filter-code for the pcap_read_dos() callback
* Some day the filter-code could be handed down to the active
* device (pkt_rx1.s or 32-bit device interrupt handler).
*/
static int pcap_setfilter_dos (pcap_t *p, struct bpf_program *fp)
{
if (!p)
return (-1);
p->fcode = *fp;
return (0);
}
/*
* Return # of packets received in pcap_read_dos()
*/
u_long pcap_mac_packets (void)
{
return (mac_count);
}
/*
* Return # of packets passed through filter in pcap_read_dos()
*/
u_long pcap_filter_packets (void)
{
return (filter_count);
}
/*
* Close pcap device. Not called for offline captures.
*/
static void pcap_cleanup_dos (pcap_t *p)
{
struct pcap_dos *pd;
if (!exc_occured)
{
pd = p->priv;
if (pcap_stats(p,NULL) < 0)
pd->stat.ps_drop = 0;
if (!get_device(p->fd))
return;
handle_to_device [p->fd-1] = NULL;
p->fd = 0;
if (ref_count > 0)
ref_count--;
if (ref_count > 0)
return;
}
close_driver();
/* XXX - call pcap_cleanup_live_common? */
}
/*
* Return the name of the 1st network interface,
* or NULL if none can be found.
*/
char *pcap_lookupdev (char *ebuf)
{
struct device *dev;
#ifdef USE_32BIT_DRIVERS
init_32bit();
#endif
for (dev = (struct device*)dev_base; dev; dev = dev->next)
{
PCAP_ASSERT (dev->probe);
if ((*dev->probe)(dev))
{
FLUSHK();
probed_dev = (struct device*) dev; /* remember last probed device */
return (char*) dev->name;
}
}
if (ebuf)
strcpy (ebuf, "No driver found");
return (NULL);
}
/*
* Gets localnet & netmask from Watt-32.
*/
int pcap_lookupnet (const char *device, bpf_u_int32 *localnet,
bpf_u_int32 *netmask, char *errbuf)
{
DWORD mask, net;
if (!_watt_is_init)
{
strcpy (errbuf, "pcap_open_offline() or pcap_activate() must be "
"called first");
return (-1);
}
mask = _w32_sin_mask;
net = my_ip_addr & mask;
if (net == 0)
{
if (IN_CLASSA(*netmask))
net = IN_CLASSA_NET;
else if (IN_CLASSB(*netmask))
net = IN_CLASSB_NET;
else if (IN_CLASSC(*netmask))
net = IN_CLASSC_NET;
else
{
snprintf (errbuf, PCAP_ERRBUF_SIZE, "inet class for 0x%lx unknown", mask);
return (-1);
}
}
*localnet = htonl (net);
*netmask = htonl (mask);
ARGSUSED (device);
return (0);
}
/*
* Get a list of all interfaces that are present and that we probe okay.
* Returns -1 on error, 0 otherwise.
* The list may be NULL epty if no interfaces were up and could be opened.
*/
int pcap_platform_finddevs (pcap_if_list_t *devlistp, char *errbuf)
{
struct device *dev;
pcap_if_t *curdev;
#if 0 /* Pkt drivers should have no addresses */
struct sockaddr_in sa_ll_1, sa_ll_2;
struct sockaddr *addr, *netmask, *broadaddr, *dstaddr;
#endif
int ret = 0;
int found = 0;
for (dev = (struct device*)dev_base; dev; dev = dev->next)
{
PCAP_ASSERT (dev->probe);
if (!(*dev->probe)(dev))
continue;
PCAP_ASSERT (dev->close); /* set by probe routine */
FLUSHK();
(*dev->close) (dev);
/*
* XXX - find out whether it's up or running? Does that apply here?
* Can we find out if anything's plugged into the adapter, if it's
* a wired device, and set PCAP_IF_CONNECTION_STATUS_CONNECTED
* or PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
*/
if ((curdev = add_dev(devlistp, dev->name, 0,
dev->long_name, errbuf)) == NULL)
{
ret = -1;
break;
}
found = 1;
#if 0 /* Pkt drivers should have no addresses */
memset (&sa_ll_1, 0, sizeof(sa_ll_1));
memset (&sa_ll_2, 0, sizeof(sa_ll_2));
sa_ll_1.sin_family = AF_INET;
sa_ll_2.sin_family = AF_INET;
addr = (struct sockaddr*) &sa_ll_1;
netmask = (struct sockaddr*) &sa_ll_1;
dstaddr = (struct sockaddr*) &sa_ll_1;
broadaddr = (struct sockaddr*) &sa_ll_2;
memset (&sa_ll_2.sin_addr, 0xFF, sizeof(sa_ll_2.sin_addr));
if (add_addr_to_dev(curdev, addr, sizeof(*addr),
netmask, sizeof(*netmask),
broadaddr, sizeof(*broadaddr),
dstaddr, sizeof(*dstaddr), errbuf) < 0)
{
ret = -1;
break;
}
#endif
}
if (ret == 0 && !found)
strcpy (errbuf, "No drivers found");
return (ret);
}
/*
* pcap_assert() is mainly used for debugging
*/
void pcap_assert (const char *what, const char *file, unsigned line)
{
FLUSHK();
fprintf (stderr, "%s (%u): Assertion \"%s\" failed\n",
file, line, what);
close_driver();
_exit (-1);
}
/*
* For pcap_offline_read(): wait and yield between printing packets
* to simulate the pace packets where actually recorded.
*/
void pcap_set_wait (pcap_t *p, void (*yield)(void), int wait)
{
if (p)
{
struct pcap_dos *pd = p->priv;
pd->wait_proc = yield;
p->opt.timeout = wait;
}
}
/*
* Initialise a named network device.
*/
static struct device *
open_driver (const char *dev_name, char *ebuf, int promisc)
{
struct device *dev;
for (dev = (struct device*)dev_base; dev; dev = dev->next)
{
PCAP_ASSERT (dev->name);
if (strcmp (dev_name,dev->name))
continue;
if (!probed_dev) /* user didn't call pcap_lookupdev() first */
{
PCAP_ASSERT (dev->probe);
if (!(*dev->probe)(dev)) /* call the xx_probe() function */
{
snprintf (ebuf, PCAP_ERRBUF_SIZE, "failed to detect device `%s'", dev_name);
return (NULL);
}
probed_dev = dev; /* device is probed okay and may be used */
}
else if (dev != probed_dev)
{
goto not_probed;
}
FLUSHK();
/* Select what traffic to receive
*/
if (promisc)
dev->flags |= (IFF_ALLMULTI | IFF_PROMISC);
else dev->flags &= ~(IFF_ALLMULTI | IFF_PROMISC);
PCAP_ASSERT (dev->open);
if (!(*dev->open)(dev))
{
snprintf (ebuf, PCAP_ERRBUF_SIZE, "failed to activate device `%s'", dev_name);
if (pktInfo.error && !strncmp(dev->name,"pkt",3))
{
strcat (ebuf, ": ");
strcat (ebuf, pktInfo.error);
}
return (NULL);
}
/* Some devices need this to operate in promiscuous mode
*/
if (promisc && dev->set_multicast_list)
(*dev->set_multicast_list) (dev);
active_dev = dev; /* remember our active device */
break;
}
/* 'dev_name' not matched in 'dev_base' list.
*/
if (!dev)
{
snprintf (ebuf, PCAP_ERRBUF_SIZE, "device `%s' not supported", dev_name);
return (NULL);
}
not_probed:
if (!probed_dev)
{
snprintf (ebuf, PCAP_ERRBUF_SIZE, "device `%s' not probed", dev_name);
return (NULL);
}
return (dev);
}
/*
* Deinitialise MAC driver.
* Set receive mode back to default mode.
*/
static void close_driver (void)
{
/* !!todo: loop over all 'handle_to_device[]' ? */
struct device *dev = active_dev;
if (dev && dev->close)
{
(*dev->close) (dev);
FLUSHK();
}
active_dev = NULL;
#ifdef USE_32BIT_DRIVERS
if (rx_pool)
{
k_free (rx_pool);
rx_pool = NULL;
}
if (dev)
pcibios_exit();
#endif
}
#ifdef __DJGPP__
static void setup_signals (void (*handler)(int))
{
signal (SIGSEGV,handler);
signal (SIGILL, handler);
signal (SIGFPE, handler);
}
static void exc_handler (int sig)
{
#ifdef USE_32BIT_DRIVERS
if (active_dev->irq > 0) /* excludes IRQ 0 */
{
disable_irq (active_dev->irq);
irq_eoi_cmd (active_dev->irq);
_printk_safe = 1;
}
#endif
switch (sig)
{
case SIGSEGV:
fputs ("Catching SIGSEGV.\n", stderr);
break;
case SIGILL:
fputs ("Catching SIGILL.\n", stderr);
break;
case SIGFPE:
_fpreset();
fputs ("Catching SIGFPE.\n", stderr);
break;
default:
fprintf (stderr, "Catching signal %d.\n", sig);
}
exc_occured = 1;
close_driver();
}
#endif /* __DJGPP__ */
/*
* Open the pcap device for the first client calling pcap_activate()
*/
static int first_init (const char *name, char *ebuf, int promisc)
{
struct device *dev;
#ifdef USE_32BIT_DRIVERS
rx_pool = k_calloc (RECEIVE_BUF_SIZE, RECEIVE_QUEUE_SIZE);
if (!rx_pool)
{
strcpy (ebuf, "Not enough memory (Rx pool)");
return (0);
}
#endif
#ifdef __DJGPP__
setup_signals (exc_handler);
#endif
#ifdef USE_32BIT_DRIVERS
init_32bit();
#endif
dev = open_driver (name, ebuf, promisc);
if (!dev)
{
#ifdef USE_32BIT_DRIVERS
k_free (rx_pool);
rx_pool = NULL;
#endif
#ifdef __DJGPP__
setup_signals (SIG_DFL);
#endif
return (0);
}
#ifdef USE_32BIT_DRIVERS
/*
* If driver is NOT a 16-bit "pkt/ndis" driver (having a 'copy_rx_buf'
* set in it's probe handler), initialise near-memory ring-buffer for
* the 32-bit device.
*/
if (dev->copy_rx_buf == NULL)
{
dev->get_rx_buf = get_rxbuf;
dev->peek_rx_buf = peek_rxbuf;
dev->release_rx_buf = release_rxbuf;
pktq_init (&dev->queue, RECEIVE_BUF_SIZE, RECEIVE_QUEUE_SIZE, rx_pool);
}
#endif
return (1);
}
#ifdef USE_32BIT_DRIVERS
static void init_32bit (void)
{
static int init_pci = 0;
if (!_printk_file)
_printk_init (64*1024, NULL); /* calls atexit(printk_exit) */
if (!init_pci)
(void)pci_init(); /* init BIOS32+PCI interface */
init_pci = 1;
}
#endif
/*
* Hook functions for using Watt-32 together with pcap
*/
static char rxbuf [ETH_MAX+100]; /* rx-buffer with some margin */
static WORD etype;
static pcap_t pcap_save;
static void watt32_recv_hook (u_char *dummy, const struct pcap_pkthdr *pcap,
const u_char *buf)
{
/* Fix me: assumes Ethernet II only */
struct ether_header *ep = (struct ether_header*) buf;
memcpy (rxbuf, buf, pcap->caplen);
etype = ep->ether_type;
ARGSUSED (dummy);
}
#if (WATTCP_VER >= 0x0224)
/*
* This function is used by Watt-32 to poll for a packet.
* i.e. it's set to bypass _eth_arrived()
*/
static void *pcap_recv_hook (WORD *type)
{
int len = pcap_read_dos (&pcap_save, 1, watt32_recv_hook, NULL);
if (len < 0)
return (NULL);
*type = etype;
return (void*) &rxbuf;
}
/*
* This function is called by Watt-32 (via _eth_xmit_hook).
* If dbug_init() was called, we should trace packets sent.
*/
static int pcap_xmit_hook (const void *buf, unsigned len)
{
int rc = 0;
if (pcap_pkt_debug > 0)
dbug_write ("pcap_xmit_hook: ");
if (active_dev && active_dev->xmit)
if ((*active_dev->xmit) (active_dev, buf, len) > 0)
rc = len;
if (pcap_pkt_debug > 0)
dbug_write (rc ? "ok\n" : "fail\n");
return (rc);
}
#endif
static int pcap_sendpacket_dos (pcap_t *p, const void *buf, size_t len)
{
struct device *dev = p ? get_device(p->fd) : NULL;
if (!dev || !dev->xmit)
return (-1);
return (*dev->xmit) (dev, buf, len);
}
/*
* This function is called by Watt-32 in tcp_post_init().
* We should prevent Watt-32 from using BOOTP/DHCP/RARP etc.
*/
static void (*prev_post_hook) (void);
static void pcap_init_hook (void)
{
_w32__bootp_on = _w32__dhcp_on = _w32__rarp_on = 0;
_w32__do_mask_req = 0;
_w32_dynamic_host = 0;
if (prev_post_hook)
(*prev_post_hook)();
}
/*
* Suppress PRINT message from Watt-32's sock_init()
*/
static void null_print (void) {}
/*
* To use features of Watt-32 (netdb functions and socket etc.)
* we must call sock_init(). But we set various hooks to prevent
* using normal PKTDRVR functions in pcpkt.c. This should hopefully
* make Watt-32 and pcap co-operate.
*/
static int init_watt32 (struct pcap *pcap, const char *dev_name, char *err_buf)
{
char *env;
int rc, MTU, has_ip_addr;
int using_pktdrv = 1;
/* If user called sock_init() first, we need to reinit in
* order to open debug/trace-file properly
*/
if (_watt_is_init)
sock_exit();
env = getenv ("PCAP_TRACE");
if (env && atoi(env) > 0 &&
pcap_pkt_debug < 0) /* if not already set */
{
dbug_init();
pcap_pkt_debug = atoi (env);
}
_watt_do_exit = 0; /* prevent sock_init() calling exit() */
prev_post_hook = _w32_usr_post_init;
_w32_usr_post_init = pcap_init_hook;
_w32_print_hook = null_print;
if (dev_name && strncmp(dev_name,"pkt",3))
using_pktdrv = FALSE;
rc = sock_init();