forked from jwise/HoRNDIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHoRNDIS.cpp
1221 lines (967 loc) · 32 KB
/
HoRNDIS.cpp
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
/* HoRNDIS.cpp
* Implementation of IOKit-derived classes
* HoRNDIS, a RNDIS driver for Mac OS X
*
* Copyright (c) 2012 Joshua Wise.
*
* IOKit examples from Apple's USBCDCEthernet.cpp; not much of that code remains.
*
* RNDIS logic is from linux/drivers/net/usb/rndis_host.c, which is:
*
* Copyright (c) 2005 David Brownell.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "HoRNDIS.h"
#define MYNAME "HoRNDIS"
#define V_PTR 0
#define V_DEBUG 1
#define V_NOTE 2
#define V_ERROR 3
#define DEBUGLEVEL V_NOTE
#define LOG(verbosity, s, ...) do { if (verbosity >= DEBUGLEVEL) IOLog(MYNAME ": %s: " s "\n", __func__, ##__VA_ARGS__); } while(0)
#define super IOEthernetController
OSDefineMetaClassAndStructors(HoRNDIS, IOEthernetController);
OSDefineMetaClassAndStructors(HoRNDISUSBInterface, HoRNDIS);
bool HoRNDIS::init(OSDictionary *properties) {
int i;
LOG(V_NOTE, "HoRNDIS tethering driver for Mac OS X, by Joshua Wise");
if (super::init(properties) == false) {
LOG(V_ERROR, "initialize super failed");
return false;
}
LOG(V_PTR, "PTR: I am: %p", this);
fNetworkInterface = NULL;
fpNetStats = NULL;
fPacketFilter = RNDIS_DEFAULT_FILTER;
fMediumDict = NULL;
fNetifEnabled = false;
fDataDead = false;
fCommInterface = NULL;
fDataInterface = NULL;
fInPipe = NULL;
fOutPipe = NULL;
outbuf_lock = NULL;
for (i = 0; i < N_OUT_BUFS; i++) {
outbufs[i].mdp = NULL;
outbufs[i].buf = NULL;
outbufs[i].inuse = false;
}
inbuf.mdp = NULL;
inbuf.buf = NULL;
fpDevice = NULL;
xid_lock = IOLockAlloc();
xid = 1;
return true;
}
/***** Driver setup and teardown language *****/
bool HoRNDISUSBInterface::start(IOService *provider) {
IOUSBHostInterface *intf;
intf = OSDynamicCast(IOUSBHostInterface, provider);
if (!intf) {
LOG(V_ERROR, "cast to IOUSBHostInterface failed?");
return false;
}
fpDevice = intf->getDevice();
return HoRNDIS::start(provider);
}
bool HoRNDIS::start(IOService *provider) {
LOG(V_DEBUG, "start");
if(!super::start(provider))
return false;
if (!fpDevice) {
stop(provider);
return false;
}
if (!fWorkLoop) {
fWorkLoop = getWorkLoop();
if (!fWorkLoop) {
LOG(V_ERROR, "start - getWorkLoop failed");
return false;
}
}
if (!openInterfaces())
goto bailout;
if (!rndisInit())
goto bailout;
/* Looks like everything's good... publish the interface! */
if (!createNetworkInterface())
goto bailout;
if (fWorkLoop) {
fWorkLoop->retain();
}
LOG(V_DEBUG, "successful");
return true;
bailout:
fpDevice->close(this);
fpDevice = NULL;
stop(provider);
return false;
}
void HoRNDIS::stop(IOService *provider) {
LOG(V_DEBUG, "stop");
// Release all resources
fNetifEnabled = false;
releaseResources();
if (fNetworkInterface) {
detachInterface(fNetworkInterface, FALSE);
}
super::stop(provider);
return;
}
void HoRNDIS::free() {
LOG(V_DEBUG, "free");
if (fNetworkInterface) {
fNetworkInterface->release();
}
if (fCommInterface) {
fCommInterface->close(this);
fCommInterface->release();
}
if (fDataInterface) {
fDataInterface->close(this);
fDataInterface->release();
}
if (fMediumDict) {
fMediumDict->release();
}
if (fWorkLoop) {
fWorkLoop->release();
}
if (xid_lock) {
IOLockFree(xid_lock);
}
super::free();
}
bool HoRNDIS::openInterfaces() {
StandardUSB::InterfaceDescriptor req;
StandardUSB::EndpointDescriptor epReq;
int rc;
/* open up the RNDIS control interface */
req.bInterfaceClass = 0xE0;
req.bInterfaceSubClass = 0x01;
req.bInterfaceProtocol = 0x03;
req.bAlternateSetting = 0xFFFF;
{
OSIterator* iterator = fpDevice->getChildIterator(gIOServicePlane);
OSObject* candidate = NULL;
while(iterator != NULL && (candidate = iterator->getNextObject()) != NULL) {
IOUSBHostInterface* interfaceCandidate = OSDynamicCast(IOUSBHostInterface, candidate);
if(interfaceCandidate != NULL && interfaceCandidate->getInterfaceDescriptor()->bInterfaceClass == 0xE0) {
fCommInterface = interfaceCandidate;
break;
}
}
OSSafeReleaseNULL(iterator);
}
LOG(V_PTR, "PTR: fCommInterface: %p", fCommInterface);
if (!fCommInterface) {
/* Maybe it's one of those stupid Galaxy S IIs? (issue #5) */
// Actually this should be the class used... I think samsung is actually right here.
req.bInterfaceClass = 0x02;
req.bInterfaceSubClass = 0x02;
req.bInterfaceProtocol = 0xFF;
req.bAlternateSetting = 0xFFFF;
OSIterator* iterator = fpDevice->getChildIterator(gIOServicePlane);
OSObject* candidate = NULL;
while(iterator != NULL && (candidate = iterator->getNextObject()) != NULL) {
IOUSBHostInterface* interfaceCandidate = OSDynamicCast(IOUSBHostInterface, candidate);
if(interfaceCandidate != NULL && interfaceCandidate->getInterfaceDescriptor()->bInterfaceClass == 0x02) {
fCommInterface = interfaceCandidate;
break;
}
}
OSSafeReleaseNULL(iterator);
if (!fCommInterface) /* Okay, I really have no clue. Oh well. */
return false;
}
rc = fCommInterface->open(this);
if (!rc)
goto bailout1;
/* open up the RNDIS data interface */
req.bInterfaceClass = 0x0A;
req.bInterfaceSubClass = 0x00;
req.bInterfaceProtocol = 0x00;
req.bAlternateSetting = 0xFFFF;
{
OSIterator* iterator = fpDevice->getChildIterator(gIOServicePlane);
OSObject* candidate = NULL;
while(iterator != NULL && (candidate = iterator->getNextObject()) != NULL) {
IOUSBHostInterface* interfaceCandidate = OSDynamicCast(IOUSBHostInterface, candidate);
if(interfaceCandidate != NULL && interfaceCandidate->getInterfaceDescriptor()->bInterfaceClass == 0x0A) {
fDataInterface = interfaceCandidate;
break;
}
}
OSSafeReleaseNULL(iterator);
}
if (!fDataInterface)
goto bailout2;
LOG(V_PTR, "PTR: fDataInterface: %p", fDataInterface);
rc = fDataInterface->open(this);
if (!rc)
goto bailout3;
if (fDataInterface->getInterfaceDescriptor()->bNumEndpoints < 2) {
LOG(V_ERROR, "not enough endpoints on data interface?");
goto bailout4;
}
fCommInterface->retain();
fDataInterface->retain();
/* open up the endpoints */
epReq.bDescriptorType = kDescriptorTypeEndpoint;
epReq.bLength = kDescriptorSizeEndpoint;
epReq.bmAttributes = kEndpointDescriptorDirectionIn;
epReq.wMaxPacketSize = 0;
epReq.bInterval = 0;
// Replacement: getInterfaceDescriptor and StandardUSB::getNextAssociatedDescriptorWithType to find an endpoint descriptor,
// then use copyPipe to retrieve the pipe object
{
const EndpointDescriptor *endpointDescriptor = StandardUSB::getNextEndpointDescriptor(fDataInterface->getConfigurationDescriptor(), fDataInterface->getInterfaceDescriptor(), &epReq);
/** THIS FOLLOWING LINE causes a page fault!!! XXX FIXME, KDK(Kernel debug kit) does not provide any more logging info -.- **/
fInPipe = fDataInterface->copyPipe(endpointDescriptor->bEndpointAddress);
}
if (!fInPipe) {
LOG(V_ERROR, "no bulk input pipe");
goto bailout5;
}
LOG(V_PTR, "PTR: fInPipe: %p", fInPipe);
LOG(V_DEBUG, "bulk input pipe %p: max packet size %d, interval %d", fInPipe, epReq.wMaxPacketSize, epReq.bInterval);
epReq.bmAttributes = kEndpointDescriptorDirectionOut;
{
const EndpointDescriptor *endpointDescriptor = StandardUSB::getNextEndpointDescriptor(fDataInterface->getConfigurationDescriptor(), fDataInterface->getInterfaceDescriptor(), &epReq);
// I assume this will have the same issue as the above code one line 299 -.-
fOutPipe = fDataInterface->copyPipe(endpointDescriptor->bEndpointAddress);
}
if (!fOutPipe) {
LOG(V_ERROR, "no bulk output pipe");
goto bailout5;
}
LOG(V_PTR, "PTR: fOutPipe: %p", fOutPipe);
LOG(V_DEBUG, "bulk output pipe %p: max packet size %d, interval %d", fOutPipe, epReq.wMaxPacketSize, epReq.bInterval);
/* Currently, we don't even bother to listen on the interrupt pipe. */
/* And we're done! */
return true;
bailout5:
fCommInterface->release();
fDataInterface->release();
bailout4:
fDataInterface->close(this);
bailout3:
fDataInterface = NULL;
bailout2:
fCommInterface->close(this);
bailout1:
fCommInterface = NULL;
return false;
}
/* Overrides IOEthernetController::createInterface */
IONetworkInterface *HoRNDIS::createInterface() {
IOEthernetInterface *netif = new IOEthernetInterface;
if (!netif)
return NULL;
if (!ifnet_set_mtu(netif->getIfnet(), mtu) && netif->setProperty(kIOMaxTransferUnit, mtu)) {
netif->release();
return NULL;
}
return netif;
}
bool HoRNDIS::createNetworkInterface() {
LOG(V_DEBUG, "attaching and registering interface");
// Allocate Timer event source
fTimerSource = IOTimerEventSource::timerEventSource(this, timerFired);
if (fTimerSource == NULL) {
LOG(V_ERROR, "createNetworkInterface - Allocate Timer event source failed");
return false;
}
if (fWorkLoop) {
if (fWorkLoop->addEventSource(fTimerSource) != kIOReturnSuccess) {
LOG(V_ERROR, "createNetworkInterface - Add Timer event source failed");
return false;
}
}
/* MTU is initialized before we get here, so this is a safe time to do this. */
if (!attachInterface((IONetworkInterface **)&fNetworkInterface, true)) {
LOG(V_ERROR, "attachInterface failed?");
return false;
}
LOG(V_PTR, "fNetworkInterface: %p", fNetworkInterface);
fNetworkInterface->registerService();
return true;
}
/***** Interface enable and disable logic *****/
/* Contains buffer alloc and dealloc, notably. Why do that here? Because that's what Apple did. */
IOReturn HoRNDIS::enable(IONetworkInterface *netif) {
IONetworkMedium *medium;
IOReturn rtn = kIOReturnSuccess;
LOG(V_DEBUG, "enable from tid %p", current_thread());
if (fNetifEnabled) {
LOG(V_ERROR, "already enabled?");
return kIOReturnSuccess;
}
if (!allocateResources())
return kIOReturnNoMemory;
if (!fMediumDict)
if (!createMediumTables()) {
rtn = kIOReturnNoMemory;
goto bailout;
}
setCurrentMedium(IONetworkMedium::medium(kIOMediumEthernetAuto, 480 * 1000000));
/* Kick off the first read. */
inbuf.comp.owner = this;
inbuf.comp.action = OSMemberFunctionCast(IOUSBHostCompletionAction, this, &HoRNDIS::dataReadComplete);
inbuf.comp.parameter = NULL;
rtn = fInPipe->io(inbuf.mdp, static_cast<uint32_t>(inbuf.mdp->getLength()), &inbuf.comp);
if (rtn != kIOReturnSuccess)
goto bailout;
/* Tell the world that the link is up... */
medium = IONetworkMedium::getMediumWithType(fMediumDict, kIOMediumEthernetAuto);
setLinkStatus(kIONetworkLinkActive | kIONetworkLinkValid, medium, 480 * 1000000);
/* ... and then listen for packets! */
getOutputQueue()->setCapacity(TRANSMIT_QUEUE_SIZE);
getOutputQueue()->start();
LOG(V_DEBUG, "txqueue started");
/* Tell the other end to start transmitting. */
if (!rndisSetPacketFilter(fPacketFilter))
goto bailout;
/* Now we can say we're alive. */
fNetifEnabled = true;
LOG(V_DEBUG, "done from tid %p", current_thread());
return kIOReturnSuccess;
bailout:
LOG(V_ERROR, "setting up the pipes failed");
releaseResources();
return rtn;
}
IOReturn HoRNDIS::disable(IONetworkInterface * netif) {
LOG(V_DEBUG, "disable from tid %p", current_thread());
/* Disable the queue (no more outputPacket), and then flush everything in the queue. */
getOutputQueue()->stop();
getOutputQueue()->setCapacity(0);
getOutputQueue()->flush();
/* Other end should stop xmitting, too. */
rndisSetPacketFilter(0);
setLinkStatus(0, 0);
/* Release all resources */
releaseResources();
fNetifEnabled = false;
/* Terminates also close the device in 'disable'. */
if (fTerminate) {
fpDevice->close(this);
fpDevice = NULL;
}
LOG(V_DEBUG, "done from tid %p", current_thread());
return kIOReturnSuccess;
}
bool HoRNDIS::createMediumTables() {
IONetworkMedium *medium;
fMediumDict = OSDictionary::withCapacity(1);
if (fMediumDict == NULL)
return false;
LOG(V_PTR, "PTR: fMediumDict: %p", fMediumDict);
medium = IONetworkMedium::medium(kIOMediumEthernetAuto, 480 * 1000000);
IONetworkMedium::addMedium(fMediumDict, medium);
if (publishMediumDictionary(fMediumDict) != true)
return false;
return true;
}
bool HoRNDIS::allocateResources() {
LOG(V_DEBUG, "allocateResources");
/* Grab a memory descriptor pointer for data-in. */
inbuf.mdp = IOBufferMemoryDescriptor::withCapacity(MAX_BLOCK_SIZE, kIODirectionIn);
if (!inbuf.mdp)
return false;
LOG(V_PTR, "PTR: inbuf.mdp: %p", inbuf.mdp); // That "int i" looks like it was a copy&paste error from the LOG statement in the for loop below
inbuf.mdp->setLength(MAX_BLOCK_SIZE);
inbuf.buf = (void *)inbuf.mdp->getBytesNoCopy();
/* And a handful for data-out... */
LOG(V_DEBUG, "allocating %d buffers", N_OUT_BUFS);
outbuf_lock = IOLockAlloc();
LOG(V_PTR, "PTR: outbuf_lock: %p", outbuf_lock);
for (int i = 0; i < N_OUT_BUFS; i++) {
outbufs[i].mdp = IOBufferMemoryDescriptor::withCapacity(MAX_BLOCK_SIZE, kIODirectionOut);
if (!outbufs[i].mdp) {
LOG(V_ERROR, "allocate output descriptor failed");
return false;
}
LOG(V_PTR, "PTR: outbufs[%d].mdp: %p", i, outbufs[i].mdp);
outbufs[i].mdp->setLength(MAX_BLOCK_SIZE);
outbufs[i].buf = (UInt8*)outbufs[i].mdp->getBytesNoCopy();
outbufs[i].inuse = false;
}
return true;
}
void HoRNDIS::releaseResources() {
int i;
LOG(V_DEBUG, "releaseResources");
for (i = 0; i < N_OUT_BUFS; i++)
if (outbufs[i].mdp) {
outbufs[i].mdp->release();
outbufs[i].mdp = NULL;
}
if (inbuf.mdp) {
inbuf.mdp->release();
inbuf.mdp = NULL;
}
if (outbuf_lock) {
IOLockFree(outbuf_lock);
outbuf_lock = NULL;
}
}
IOOutputQueue* HoRNDIS::createOutputQueue() {
if (!fWorkLoop) {
fWorkLoop = getWorkLoop();
if (!fWorkLoop) {
LOG(V_ERROR, "createOutputQueue - getWorkLoop failed");
return NULL;
}
}
return IOGatedOutputQueue::withTarget(this, fWorkLoop, TRANSMIT_QUEUE_SIZE);
}
bool HoRNDIS::configureInterface(IONetworkInterface *netif) {
IONetworkData *nd;
if (super::configureInterface(netif) == false) {
LOG(V_ERROR, "super failed");
return false;
}
nd = netif->getNetworkData(kIONetworkStatsKey);
if (!nd || !(fpNetStats = (IONetworkStats *)nd->getBuffer())) {
LOG(V_ERROR, "network statistics buffer unavailable?");
return false;
}
LOG(V_PTR, "fpNetStats: %p", fpNetStats);
return true;
}
/***** All-purpose IOKit network routines *****/
IOReturn HoRNDIS::getPacketFilters(const OSSymbol *group, UInt32 *filters) const {
IOReturn rtn = kIOReturnSuccess;
if (group == gIOEthernetWakeOnLANFilterGroup)
*filters = 0;
else if (group == gIONetworkFilterGroup)
*filters = fPacketFilter;
else
rtn = super::getPacketFilters(group, filters);
return rtn;
}
IOReturn HoRNDIS::getMaxPacketSize(UInt32 * maxSize) const {
*maxSize = mtu;
return kIOReturnSuccess;
}
IOReturn HoRNDIS::selectMedium(const IONetworkMedium *medium) {
setSelectedMedium(medium);
return kIOReturnSuccess;
}
IOReturn HoRNDIS::getHardwareAddress(IOEthernetAddress *ea) {
UInt32 i;
void *buf;
unsigned char *bp;
int rlen = -1;
int rv;
buf = IOMalloc(RNDIS_CMD_BUF_SZ);
if (!buf)
return kIOReturnNoMemory;
rv = rndisQuery(buf, OID_802_3_PERMANENT_ADDRESS, 48, (void **) &bp, &rlen);
if (rv < 0) {
LOG(V_ERROR, "getHardwareAddress OID failed?");
IOFree(buf, RNDIS_CMD_BUF_SZ);
return kIOReturnIOError;
}
LOG(V_DEBUG, "MAC Address %02x:%02x:%02x:%02x:%02x:%02x -- rlen %d",
bp[0], bp[1], bp[2], bp[3], bp[4], bp[5],
rlen);
for (i=0; i<6; i++)
ea->bytes[i] = bp[i];
IOFree(buf, RNDIS_CMD_BUF_SZ);
return kIOReturnSuccess;
}
IOReturn HoRNDIS::setPromiscuousMode(bool active) {
if (!fNetifEnabled) {
return kIOReturnSuccess;
}
if (((fPacketFilter & kIOPacketFilterPromiscuous) && active) || (!(fPacketFilter & kIOPacketFilterPromiscuous) && !active)) {
return kIOReturnOutputSuccess;
} else {
if (active) {
fPacketFilter |= kIOPacketFilterPromiscuous;
} else {
fPacketFilter &= ~kIOPacketFilterPromiscuous;
}
}
if (!this->rndisSetPacketFilter(fPacketFilter)) {
return kIOReturnIOError;
}
return kIOReturnSuccess;
}
IOReturn HoRNDIS::message(UInt32 type, IOService *provider, void *argument) {
switch (type) {
case kIOMessageServiceIsTerminated:
LOG(V_NOTE, "kIOMessageServiceIsTerminated");
if (!fNetifEnabled) {
if (fCommInterface) {
fCommInterface->close(this);
fCommInterface->release();
fCommInterface = NULL;
}
if (fDataInterface) {
fDataInterface->close(this);
fDataInterface->release();
fDataInterface = NULL;
}
fpDevice->close(this);
fpDevice = NULL;
}
fTerminate = true;
return kIOReturnSuccess;
case kIOMessageServiceIsSuspended:
LOG(V_NOTE, "kIOMessageServiceIsSuspended");
break;
case kIOMessageServiceIsResumed:
LOG(V_NOTE, "kIOMessageServiceIsResumed");
break;
case kIOMessageServiceIsRequestingClose:
LOG(V_NOTE, "kIOMessageServiceIsRequestingClose");
break;
case kIOMessageServiceWasClosed:
LOG(V_NOTE, "kIOMessageServiceWasClosed");
break;
case kIOMessageServiceBusyStateChange:
LOG(V_NOTE, "kIOMessageServiceBusyStateChange");
break;
// case kIOUSBHostMessagePortHasBeenResumed:
// LOG(V_NOTE, "kIOUSBMessagePortHasBeenResumed");
//
// /* Try to resurrect any dead reads. */
// if (fDataDead) {
// ior = fInPipe->io(inbuf.mdp, static_cast<uint32_t>(inbuf.mdp->getLength()), &inbuf.comp);
// if (ior == kIOReturnSuccess)
// fDataDead = false;
// else
// LOG(V_ERROR, "failed to queue Data pipe read");
// }
//
// break;
case kIOMessageServiceIsAttemptingOpen:
LOG(V_NOTE, "kIOMessageServiceIsAttemptingOpen");
break;
default:
LOG(V_NOTE, "unknown message type %08x", (unsigned int) type);
break;
}
return kIOReturnUnsupported;
}
/***** Packet transmit logic *****/
UInt32 HoRNDIS::outputPacket(mbuf_t packet, void *param) {
mbuf_t m;
size_t pktlen = 0;
IOReturn ior = kIOReturnSuccess;
UInt32 poolIndx;
int i;
LOG(V_DEBUG, "");
/* Count the total size of this packet */
m = packet;
while (m) {
pktlen += mbuf_len(m);
m = mbuf_next(m);
}
LOG(V_DEBUG, "%ld bytes", pktlen);
if (pktlen > (mtu + 14)) {
LOG(V_ERROR, "packet too large (%ld bytes, but I told you you could have %d!)", pktlen, mtu);
fpNetStats->outputErrors++;
return false;
}
/* Find an output buffer in the pool */
IOLockLock(outbuf_lock);
for (i = 0; i < OUT_BUF_MAX_TRIES; i++) {
uint64_t ivl, deadl;
for (poolIndx = 0; poolIndx < N_OUT_BUFS; poolIndx++)
if (!outbufs[poolIndx].inuse) {
outbufs[poolIndx].inuse = true;
break;
}
if (poolIndx != N_OUT_BUFS)
break;
/* "while", not "if". See Symphony X's seminal work on this topic, /Paradise Lost/ (2007). */
nanoseconds_to_absolutetime(OUT_BUF_WAIT_TIME, &ivl);
clock_absolutetime_interval_to_deadline(ivl, &deadl);
LOG(V_NOTE, "waiting for buffer...");
IOLockSleepDeadline(outbuf_lock, outbufs, *(AbsoluteTime *)&deadl, THREAD_INTERRUPTIBLE);
}
IOLockUnlock(outbuf_lock);
if (poolIndx == N_OUT_BUFS) {
LOG(V_ERROR, "timed out waiting for buffer");
return kIOReturnTimeout;
}
/* Start filling in the send buffer */
struct rndis_data_hdr *hdr;
hdr = (struct rndis_data_hdr *)outbufs[poolIndx].buf;
outbufs[poolIndx].inuse = true;
outbufs[poolIndx].mdp->setLength(pktlen + sizeof *hdr);
memset(hdr, 0, sizeof *hdr);
hdr->msg_type = RNDIS_MSG_PACKET;
hdr->msg_len = cpu_to_le32(pktlen + sizeof *hdr);
hdr->data_offset = cpu_to_le32(sizeof(*hdr) - 8);
hdr->data_len = cpu_to_le32(pktlen);
mbuf_copydata(packet, 0, pktlen, hdr + 1);
freePacket(packet);
/* Now, fire it off! */
outbufs[poolIndx].comp.owner = this;
outbufs[poolIndx].comp.parameter = (void *)&poolIndx; // How did this work before? Passing the value as a memory address??? (You forgot the address operator) - winsock
outbufs[poolIndx].comp.action = OSMemberFunctionCast(IOUSBHostCompletionAction, this, &HoRNDIS::dataWriteComplete);
ior = fOutPipe->io(outbufs[poolIndx].mdp, static_cast<uint32_t>(outbufs[poolIndx].mdp->getLength()), &outbufs[poolIndx].comp);
if (ior != kIOReturnSuccess) {
LOG(V_ERROR, "write failed");
if (ior == kUSBHostReturnPipeStalled) {
fOutPipe->clearStall(false);
ior = fOutPipe->io(outbufs[poolIndx].mdp, static_cast<uint32_t>(outbufs[poolIndx].mdp->getLength()), &outbufs[poolIndx].comp);
if (ior != kIOReturnSuccess) {
LOG(V_ERROR, "write really failed");
fpNetStats->outputErrors++;
return ior;
}
}
}
fpNetStats->outputPackets++;
return kIOReturnOutputSuccess;
}
void HoRNDIS::dataWriteComplete(void *obj, void *param, IOReturn rc, UInt32 remaining) {
HoRNDIS *me = (HoRNDIS *)obj;
UInt32 poolIndx = *static_cast<UInt32 *>(param);
LOG(V_DEBUG, "(rc %08x, poolIndx %d)", rc, poolIndx);
/* Free the buffer, and hand it off to anyone who might be waiting for one. */
me->outbufs[poolIndx].inuse = false;
IOLockWakeup(me->outbuf_lock, me->outbufs, true);
if (rc == kIOReturnSuccess)
return;
/* Sigh. Try to clean up. */
LOG(V_ERROR, "I/O error: %08x", rc);
if (rc != kIOReturnAborted) {
rc = me->clearPipeStall(me->fOutPipe);
if (rc != kIOReturnSuccess)
LOG(V_ERROR, "clear stall failed (trying to continue)");
}
}
IOReturn HoRNDIS::clearPipeStall(IOUSBHostPipe *thePipe) {
IOReturn rc;
rc = thePipe->clearStall(true);
LOG(V_ERROR, "pipe stall clear: rv %08x", rc);
return rc;
}
/***** Packet receive logic *****/
void HoRNDIS::dataReadComplete(void *obj, void *param, IOReturn rc, UInt32 remaining) {
HoRNDIS *me = (HoRNDIS *)obj;
IOReturn ior;
if (rc == kIOReturnAborted || rc == kIOReturnNotResponding) {
LOG(V_ERROR, "I/O aborted: device unplugged?");
return;
}
if (rc == kIOReturnSuccess) {
/* Got one? Hand it to the back end. */
LOG(V_DEBUG, "%d bytes", (int)(MAX_BLOCK_SIZE - remaining));
me->receivePacket(me->inbuf.buf, MAX_BLOCK_SIZE - remaining);
} else {
LOG(V_ERROR, "dataReadComplete: I/O error: %08x", rc);
rc = me->clearPipeStall(me->fInPipe);
if (rc != kIOReturnSuccess)
LOG(V_ERROR, "clear stall failed (trying to continue)");
}
/* Queue the next one up. */
ior = me->fInPipe->io(me->inbuf.mdp, static_cast<uint32_t>(me->inbuf.mdp->getLength()), &me->inbuf.comp);
if (ior != kIOReturnSuccess) {
LOG(V_ERROR, "failed to queue read");
if (ior == kUSBHostReturnPipeStalled) {
me->fInPipe->clearStall(false);
ior = me->fInPipe->io(me->inbuf.mdp, static_cast<uint32_t>(me->inbuf.mdp->getLength()), &me->inbuf.comp, NULL);
if (ior != kIOReturnSuccess) {
LOG(V_ERROR, "failed, read dead");
me->fDataDead = true;
}
}
}
}
void HoRNDIS::receivePacket(void *packet, UInt32 size) {
mbuf_t m;
IOReturn rv;
LOG(V_DEBUG, "sz %d", (int)size);
if (size > MAX_BLOCK_SIZE) {
LOG(V_ERROR, "packet size error, packet dropped");
fpNetStats->inputErrors++;
return;
}
while (size) {
struct rndis_data_hdr *hdr = (struct rndis_data_hdr *)packet;
uint32_t msg_len, data_ofs, data_len;
if (size <= sizeof(struct rndis_data_hdr)) {
LOG(V_ERROR, "receivePacket() on too small packet? (size %d)", size);
return;
}
msg_len = le32_to_cpu(hdr->msg_len);
data_ofs = le32_to_cpu(hdr->data_offset);
data_len = le32_to_cpu(hdr->data_len);
if (hdr->msg_type != RNDIS_MSG_PACKET) { /* both are LE, so that's okay */
LOG(V_ERROR, "non-PACKET over data channel? (msg_type %08x)", hdr->msg_type);
return;
}
if (msg_len > size) {
LOG(V_ERROR, "msg_len too big?");
return;
}
if ((data_ofs + data_len + 8) > msg_len) {
LOG(V_ERROR, "data bigger than msg?");
return;
}
m = allocatePacket(data_len);
if (!m) {
LOG(V_ERROR, "allocatePacket for data_len %d failed", data_len);
fpNetStats->inputErrors++;
return;
}
LOG(V_PTR, "PTR: mbuf: %p", m);
rv = mbuf_copyback(m, 0, data_len, (char *)packet + data_ofs + 8, MBUF_WAITOK);
if (rv) {
LOG(V_ERROR, "mbuf_copyback failed, rv %08x", rv);
fpNetStats->inputErrors++;
freePacket(m);
return;
}
fNetworkInterface->inputPacket(m, data_len);
LOG(V_DEBUG, "submitted pkt sz %d", data_len);
fpNetStats->inputPackets++;
size -= msg_len;
packet = (char *)packet + msg_len;
}
}
/****************************************************************************************************/
//
// Method: HoRNDIS::timerFired
//
// Inputs:
//
// Outputs:
//
// Desc: Static member function called when a timer event fires.
//
/****************************************************************************************************/
void HoRNDIS::timerFired(OSObject *owner, IOTimerEventSource *sender)
{
// XTRACE(this, 0, 0, "timerFired");
if (owner) {
HoRNDIS* target = OSDynamicCast(HoRNDIS, owner);
if (target) {
target->timeoutOccurred(sender);
}
}
}/* end timerFired */
/****************************************************************************************************/
//
// Method: HoRNDIS::timeoutOccurred
//
// Inputs:
//
// Outputs:
//
// Desc: Timeout handler, used for stats gathering.
//
/****************************************************************************************************/
void HoRNDIS::timeoutOccurred(IOTimerEventSource * /*timer*/)
{
// Stats gathering should be happening here :/
// Restart the watchdog timer
fTimerSource->setTimeoutMS(WATCHDOG_TIMER_MS);
}/* end timeoutOccurred */
/***** RNDIS command logic *****/
int HoRNDIS::rndisCommand(struct rndis_msg_hdr *buf, int buflen) {
int rc = kIOReturnSuccess;
StandardUSB::DeviceRequest rq;
IOBufferMemoryDescriptor *txdsc = IOBufferMemoryDescriptor::withCapacity(le32_to_cpu(buf->msg_len), kIODirectionOut);
LOG(V_PTR, "PTR: txdsc: %p", txdsc);