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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
|
/*
* Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
/** @file
*
* Remote Network Driver Interface Specification
*
*/
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/iobuf.h>
#include <ipxe/netdevice.h>
#include <ipxe/ethernet.h>
#include <ipxe/device.h>
#include <ipxe/rndis.h>
/**
* Allocate I/O buffer
*
* @v len Length
* @ret iobuf I/O buffer, or NULL
*/
static struct io_buffer * rndis_alloc_iob ( size_t len ) {
struct rndis_header *header;
struct io_buffer *iobuf;
/* Allocate I/O buffer and reserve space */
iobuf = alloc_iob ( sizeof ( *header ) + len );
if ( iobuf )
iob_reserve ( iobuf, sizeof ( *header ) );
return iobuf;
}
/**
* Wait for completion
*
* @v rndis RNDIS device
* @v wait_id Request ID
* @ret rc Return status code
*/
static int rndis_wait ( struct rndis_device *rndis, unsigned int wait_id ) {
unsigned int i;
/* Record query ID */
rndis->wait_id = wait_id;
/* Wait for operation to complete */
for ( i = 0 ; i < RNDIS_MAX_WAIT_MS ; i++ ) {
/* Check for completion */
if ( ! rndis->wait_id )
return rndis->wait_rc;
/* Poll RNDIS device */
rndis->op->poll ( rndis );
/* Delay for 1ms */
mdelay ( 1 );
}
DBGC ( rndis, "RNDIS %s timed out waiting for ID %#08x\n",
rndis->name, wait_id );
return -ETIMEDOUT;
}
/**
* Transmit message
*
* @v rndis RNDIS device
* @v iobuf I/O buffer
* @v type Message type
* @ret rc Return status code
*/
static int rndis_tx_message ( struct rndis_device *rndis,
struct io_buffer *iobuf, unsigned int type ) {
struct rndis_header *header;
int rc;
/* Prepend RNDIS header */
header = iob_push ( iobuf, sizeof ( *header ) );
header->type = cpu_to_le32 ( type );
header->len = cpu_to_le32 ( iob_len ( iobuf ) );
/* Transmit message */
if ( ( rc = rndis->op->transmit ( rndis, iobuf ) ) != 0 ) {
DBGC ( rndis, "RNDIS %s could not transmit: %s\n",
rndis->name, strerror ( rc ) );
return rc;
}
return 0;
}
/**
* Complete message transmission
*
* @v rndis RNDIS device
* @v iobuf I/O buffer
* @v rc Packet status code
*/
void rndis_tx_complete_err ( struct rndis_device *rndis,
struct io_buffer *iobuf, int rc ) {
struct net_device *netdev = rndis->netdev;
struct rndis_header *header;
size_t len = iob_len ( iobuf );
/* Sanity check */
if ( len < sizeof ( *header ) ) {
DBGC ( rndis, "RNDIS %s completed underlength transmission:\n",
rndis->name );
DBGC_HDA ( rndis, 0, iobuf->data, len );
netdev_tx_err ( netdev, NULL, -EINVAL );
return;
}
header = iobuf->data;
/* Complete buffer */
if ( header->type == cpu_to_le32 ( RNDIS_PACKET_MSG ) ) {
netdev_tx_complete_err ( netdev, iobuf, rc );
} else {
free_iob ( iobuf );
}
}
/**
* Transmit data packet
*
* @v rndis RNDIS device
* @v iobuf I/O buffer
* @ret rc Return status code
*/
static int rndis_tx_data ( struct rndis_device *rndis,
struct io_buffer *iobuf ) {
struct rndis_packet_message *msg;
size_t len = iob_len ( iobuf );
int rc;
/* Prepend packet message header */
msg = iob_push ( iobuf, sizeof ( *msg ) );
memset ( msg, 0, sizeof ( *msg ) );
msg->data.offset = cpu_to_le32 ( sizeof ( *msg ) );
msg->data.len = cpu_to_le32 ( len );
/* Transmit message */
if ( ( rc = rndis_tx_message ( rndis, iobuf, RNDIS_PACKET_MSG ) ) != 0 )
return rc;
return 0;
}
/**
* Defer transmitted packet
*
* @v rndis RNDIS device
* @v iobuf I/O buffer
* @ret rc Return status code
*
* As with netdev_tx_defer(), the caller must ensure that space in the
* transmit descriptor ring is freed up before calling
* rndis_tx_complete().
*
* Unlike netdev_tx_defer(), this call may fail.
*/
int rndis_tx_defer ( struct rndis_device *rndis, struct io_buffer *iobuf ) {
struct net_device *netdev = rndis->netdev;
struct rndis_header *header;
struct rndis_packet_message *msg;
/* Fail unless this was a packet message. Only packet
* messages correspond to I/O buffers in the network device's
* TX queue; other messages cannot be deferred in this way.
*/
assert ( iob_len ( iobuf ) >= sizeof ( *header ) );
header = iobuf->data;
if ( header->type != cpu_to_le32 ( RNDIS_PACKET_MSG ) )
return -ENOTSUP;
/* Strip RNDIS header and packet message header, to return
* this packet to the state in which we received it.
*/
iob_pull ( iobuf, ( sizeof ( *header ) + sizeof ( *msg ) ) );
/* Defer packet */
netdev_tx_defer ( netdev, iobuf );
return 0;
}
/**
* Receive data packet
*
* @v rndis RNDIS device
* @v iobuf I/O buffer
*/
static void rndis_rx_data ( struct rndis_device *rndis,
struct io_buffer *iobuf ) {
struct net_device *netdev = rndis->netdev;
struct rndis_packet_message *msg;
size_t len = iob_len ( iobuf );
size_t data_offset;
size_t data_len;
int rc;
/* Sanity check */
if ( len < sizeof ( *msg ) ) {
DBGC ( rndis, "RNDIS %s received underlength data packet:\n",
rndis->name );
DBGC_HDA ( rndis, 0, iobuf->data, len );
rc = -EINVAL;
goto err_len;
}
msg = iobuf->data;
/* Locate and sanity check data buffer */
data_offset = le32_to_cpu ( msg->data.offset );
data_len = le32_to_cpu ( msg->data.len );
if ( ( data_offset > len ) || ( data_len > ( len - data_offset ) ) ) {
DBGC ( rndis, "RNDIS %s data packet data exceeds packet:\n",
rndis->name );
DBGC_HDA ( rndis, 0, iobuf->data, len );
rc = -EINVAL;
goto err_data;
}
/* Strip non-data portions */
iob_pull ( iobuf, data_offset );
iob_unput ( iobuf, ( iob_len ( iobuf ) - data_len ) );
/* Hand off to network stack */
netdev_rx ( netdev, iob_disown ( iobuf ) );
return;
err_data:
err_len:
/* Report error to network stack */
netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
}
/**
* Transmit initialisation message
*
* @v rndis RNDIS device
* @v id Request ID
* @ret rc Return status code
*/
static int rndis_tx_initialise ( struct rndis_device *rndis, unsigned int id ) {
struct io_buffer *iobuf;
struct rndis_initialise_message *msg;
int rc;
/* Allocate I/O buffer */
iobuf = rndis_alloc_iob ( sizeof ( *msg ) );
if ( ! iobuf ) {
rc = -ENOMEM;
goto err_alloc;
}
/* Construct message */
msg = iob_put ( iobuf, sizeof ( *msg ) );
memset ( msg, 0, sizeof ( *msg ) );
msg->id = id; /* Non-endian */
msg->major = cpu_to_le32 ( RNDIS_VERSION_MAJOR );
msg->minor = cpu_to_le32 ( RNDIS_VERSION_MINOR );
msg->mtu = cpu_to_le32 ( RNDIS_MTU );
/* Transmit message */
if ( ( rc = rndis_tx_message ( rndis, iobuf,
RNDIS_INITIALISE_MSG ) ) != 0 )
goto err_tx;
return 0;
err_tx:
free_iob ( iobuf );
err_alloc:
return rc;
}
/**
* Receive initialisation completion
*
* @v rndis RNDIS device
* @v iobuf I/O buffer
*/
static void rndis_rx_initialise ( struct rndis_device *rndis,
struct io_buffer *iobuf ) {
struct rndis_initialise_completion *cmplt;
size_t len = iob_len ( iobuf );
unsigned int id;
int rc;
/* Sanity check */
if ( len < sizeof ( *cmplt ) ) {
DBGC ( rndis, "RNDIS %s received underlength initialisation "
"completion:\n", rndis->name );
DBGC_HDA ( rndis, 0, iobuf->data, len );
rc = -EINVAL;
goto err_len;
}
cmplt = iobuf->data;
/* Extract request ID */
id = cmplt->id; /* Non-endian */
/* Check status */
if ( cmplt->status ) {
DBGC ( rndis, "RNDIS %s received initialisation completion "
"failure %#08x\n", rndis->name,
le32_to_cpu ( cmplt->status ) );
rc = -EIO;
goto err_status;
}
/* Success */
rc = 0;
err_status:
/* Record completion result if applicable */
if ( id == rndis->wait_id ) {
rndis->wait_id = 0;
rndis->wait_rc = rc;
}
err_len:
free_iob ( iobuf );
}
/**
* Initialise RNDIS
*
* @v rndis RNDIS device
* @ret rc Return status code
*/
static int rndis_initialise ( struct rndis_device *rndis ) {
int rc;
/* Transmit initialisation message */
if ( ( rc = rndis_tx_initialise ( rndis, RNDIS_INIT_ID ) ) != 0 )
return rc;
/* Wait for response */
if ( ( rc = rndis_wait ( rndis, RNDIS_INIT_ID ) ) != 0 )
return rc;
return 0;
}
/**
* Transmit halt message
*
* @v rndis RNDIS device
* @ret rc Return status code
*/
static int rndis_tx_halt ( struct rndis_device *rndis ) {
struct io_buffer *iobuf;
struct rndis_halt_message *msg;
int rc;
/* Allocate I/O buffer */
iobuf = rndis_alloc_iob ( sizeof ( *msg ) );
if ( ! iobuf ) {
rc = -ENOMEM;
goto err_alloc;
}
/* Construct message */
msg = iob_put ( iobuf, sizeof ( *msg ) );
memset ( msg, 0, sizeof ( *msg ) );
/* Transmit message */
if ( ( rc = rndis_tx_message ( rndis, iobuf, RNDIS_HALT_MSG ) ) != 0 )
goto err_tx;
return 0;
err_tx:
free_iob ( iobuf );
err_alloc:
return rc;
}
/**
* Halt RNDIS
*
* @v rndis RNDIS device
* @ret rc Return status code
*/
static int rndis_halt ( struct rndis_device *rndis ) {
int rc;
/* Transmit halt message */
if ( ( rc = rndis_tx_halt ( rndis ) ) != 0 )
return rc;
return 0;
}
/**
* Transmit OID message
*
* @v rndis RNDIS device
* @v oid Object ID
* @v data New OID value (or NULL to query current value)
* @v len Length of new OID value
* @ret rc Return status code
*/
static int rndis_tx_oid ( struct rndis_device *rndis, unsigned int oid,
const void *data, size_t len ) {
struct io_buffer *iobuf;
struct rndis_oid_message *msg;
unsigned int type;
int rc;
/* Allocate I/O buffer */
iobuf = rndis_alloc_iob ( sizeof ( *msg ) + len );
if ( ! iobuf ) {
rc = -ENOMEM;
goto err_alloc;
}
/* Construct message. We use the OID as the request ID. */
msg = iob_put ( iobuf, sizeof ( *msg ) );
memset ( msg, 0, sizeof ( *msg ) );
msg->id = oid; /* Non-endian */
msg->oid = cpu_to_le32 ( oid );
msg->offset = cpu_to_le32 ( sizeof ( *msg ) );
msg->len = cpu_to_le32 ( len );
memcpy ( iob_put ( iobuf, len ), data, len );
/* Transmit message */
type = ( data ? RNDIS_SET_MSG : RNDIS_QUERY_MSG );
if ( ( rc = rndis_tx_message ( rndis, iobuf, type ) ) != 0 )
goto err_tx;
return 0;
err_tx:
free_iob ( iobuf );
err_alloc:
return rc;
}
/**
* Receive query OID completion
*
* @v rndis RNDIS device
* @v iobuf I/O buffer
*/
static void rndis_rx_query_oid ( struct rndis_device *rndis,
struct io_buffer *iobuf ) {
struct net_device *netdev = rndis->netdev;
struct rndis_query_completion *cmplt;
size_t len = iob_len ( iobuf );
size_t info_offset;
size_t info_len;
unsigned int id;
void *info;
uint32_t *link_status;
int rc;
/* Sanity check */
if ( len < sizeof ( *cmplt ) ) {
DBGC ( rndis, "RNDIS %s received underlength query "
"completion:\n", rndis->name );
DBGC_HDA ( rndis, 0, iobuf->data, len );
rc = -EINVAL;
goto err_len;
}
cmplt = iobuf->data;
/* Extract request ID */
id = cmplt->id; /* Non-endian */
/* Check status */
if ( cmplt->status ) {
DBGC ( rndis, "RNDIS %s received query completion failure "
"%#08x\n", rndis->name, le32_to_cpu ( cmplt->status ) );
DBGC_HDA ( rndis, 0, iobuf->data, len );
rc = -EIO;
goto err_status;
}
/* Locate and sanity check information buffer */
info_offset = le32_to_cpu ( cmplt->offset );
info_len = le32_to_cpu ( cmplt->len );
if ( ( info_offset > len ) || ( info_len > ( len - info_offset ) ) ) {
DBGC ( rndis, "RNDIS %s query completion information exceeds "
"packet:\n", rndis->name );
DBGC_HDA ( rndis, 0, iobuf->data, len );
rc = -EINVAL;
goto err_info;
}
info = ( ( ( void * ) cmplt ) + info_offset );
/* Handle OID */
switch ( id ) {
case RNDIS_OID_802_3_PERMANENT_ADDRESS:
if ( info_len > sizeof ( netdev->hw_addr ) )
info_len = sizeof ( netdev->hw_addr );
memcpy ( netdev->hw_addr, info, info_len );
break;
case RNDIS_OID_802_3_CURRENT_ADDRESS:
if ( info_len > sizeof ( netdev->ll_addr ) )
info_len = sizeof ( netdev->ll_addr );
memcpy ( netdev->ll_addr, info, info_len );
break;
case RNDIS_OID_GEN_MEDIA_CONNECT_STATUS:
if ( info_len != sizeof ( *link_status ) ) {
DBGC ( rndis, "RNDIS %s invalid link status:\n",
rndis->name );
DBGC_HDA ( rndis, 0, iobuf->data, len );
rc = -EPROTO;
goto err_link_status;
}
link_status = info;
if ( *link_status == 0 ) {
DBGC ( rndis, "RNDIS %s link is up\n", rndis->name );
netdev_link_up ( netdev );
} else {
DBGC ( rndis, "RNDIS %s link is down: %#08x\n",
rndis->name, le32_to_cpu ( *link_status ) );
netdev_link_down ( netdev );
}
break;
default:
DBGC ( rndis, "RNDIS %s unexpected query completion ID %#08x\n",
rndis->name, id );
DBGC_HDA ( rndis, 0, iobuf->data, len );
rc = -EPROTO;
goto err_id;
}
/* Success */
rc = 0;
err_id:
err_link_status:
err_info:
err_status:
/* Record completion result if applicable */
if ( id == rndis->wait_id ) {
rndis->wait_id = 0;
rndis->wait_rc = rc;
}
err_len:
/* Free I/O buffer */
free_iob ( iobuf );
}
/**
* Receive set OID completion
*
* @v rndis RNDIS device
* @v iobuf I/O buffer
*/
static void rndis_rx_set_oid ( struct rndis_device *rndis,
struct io_buffer *iobuf ) {
struct rndis_set_completion *cmplt;
size_t len = iob_len ( iobuf );
unsigned int id;
int rc;
/* Sanity check */
if ( len < sizeof ( *cmplt ) ) {
DBGC ( rndis, "RNDIS %s received underlength set completion:\n",
rndis->name );
DBGC_HDA ( rndis, 0, iobuf->data, len );
rc = -EINVAL;
goto err_len;
}
cmplt = iobuf->data;
/* Extract request ID */
id = cmplt->id; /* Non-endian */
/* Check status */
if ( cmplt->status ) {
DBGC ( rndis, "RNDIS %s received set completion failure "
"%#08x\n", rndis->name, le32_to_cpu ( cmplt->status ) );
DBGC_HDA ( rndis, 0, iobuf->data, len );
rc = -EIO;
goto err_status;
}
/* Success */
rc = 0;
err_status:
/* Record completion result if applicable */
if ( id == rndis->wait_id ) {
rndis->wait_id = 0;
rndis->wait_rc = rc;
}
err_len:
/* Free I/O buffer */
free_iob ( iobuf );
}
/**
* Query or set OID
*
* @v rndis RNDIS device
* @v oid Object ID
* @v data New OID value (or NULL to query current value)
* @v len Length of new OID value
* @ret rc Return status code
*/
static int rndis_oid ( struct rndis_device *rndis, unsigned int oid,
const void *data, size_t len ) {
int rc;
/* Transmit query */
if ( ( rc = rndis_tx_oid ( rndis, oid, data, len ) ) != 0 )
return rc;
/* Wait for response */
if ( ( rc = rndis_wait ( rndis, oid ) ) != 0 )
return rc;
return 0;
}
/**
* Receive indicate status message
*
* @v rndis RNDIS device
* @v iobuf I/O buffer
*/
static void rndis_rx_status ( struct rndis_device *rndis,
struct io_buffer *iobuf ) {
struct net_device *netdev = rndis->netdev;
struct rndis_indicate_status_message *msg;
size_t len = iob_len ( iobuf );
unsigned int status;
int rc;
/* Sanity check */
if ( len < sizeof ( *msg ) ) {
DBGC ( rndis, "RNDIS %s received underlength status message:\n",
rndis->name );
DBGC_HDA ( rndis, 0, iobuf->data, len );
rc = -EINVAL;
goto err_len;
}
msg = iobuf->data;
/* Extract status */
status = le32_to_cpu ( msg->status );
/* Handle status */
switch ( msg->status ) {
case RNDIS_STATUS_MEDIA_CONNECT:
DBGC ( rndis, "RNDIS %s link is up\n", rndis->name );
netdev_link_up ( netdev );
break;
case RNDIS_STATUS_MEDIA_DISCONNECT:
DBGC ( rndis, "RNDIS %s link is down\n", rndis->name );
netdev_link_down ( netdev );
break;
case RNDIS_STATUS_WTF_WORLD:
/* Ignore */
break;
default:
DBGC ( rndis, "RNDIS %s unexpected status %#08x:\n",
rndis->name, status );
DBGC_HDA ( rndis, 0, iobuf->data, len );
rc = -ENOTSUP;
goto err_status;
}
/* Free I/O buffer */
free_iob ( iobuf );
return;
err_status:
err_len:
/* Report error via network device statistics */
netdev_rx_err ( netdev, iobuf, rc );
}
/**
* Receive RNDIS message
*
* @v rndis RNDIS device
* @v iobuf I/O buffer
* @v type Message type
*/
static void rndis_rx_message ( struct rndis_device *rndis,
struct io_buffer *iobuf, unsigned int type ) {
struct net_device *netdev = rndis->netdev;
int rc;
/* Handle packet */
switch ( type ) {
case RNDIS_PACKET_MSG:
rndis_rx_data ( rndis, iob_disown ( iobuf ) );
break;
case RNDIS_INITIALISE_CMPLT:
rndis_rx_initialise ( rndis, iob_disown ( iobuf ) );
break;
case RNDIS_QUERY_CMPLT:
rndis_rx_query_oid ( rndis, iob_disown ( iobuf ) );
break;
case RNDIS_SET_CMPLT:
rndis_rx_set_oid ( rndis, iob_disown ( iobuf ) );
break;
case RNDIS_INDICATE_STATUS_MSG:
rndis_rx_status ( rndis, iob_disown ( iobuf ) );
break;
default:
DBGC ( rndis, "RNDIS %s received unexpected type %#08x\n",
rndis->name, type );
DBGC_HDA ( rndis, 0, iobuf->data, iob_len ( iobuf ) );
rc = -EPROTO;
goto err_type;
}
return;
err_type:
/* Report error via network device statistics */
netdev_rx_err ( netdev, iobuf, rc );
}
/**
* Receive packet from underlying transport layer
*
* @v rndis RNDIS device
* @v iobuf I/O buffer
*/
void rndis_rx ( struct rndis_device *rndis, struct io_buffer *iobuf ) {
struct net_device *netdev = rndis->netdev;
struct rndis_header *header;
unsigned int type;
int rc;
/* Sanity check */
if ( iob_len ( iobuf ) < sizeof ( *header ) ) {
DBGC ( rndis, "RNDIS %s received underlength packet:\n",
rndis->name );
DBGC_HDA ( rndis, 0, iobuf->data, iob_len ( iobuf ) );
rc = -EINVAL;
goto drop;
}
header = iobuf->data;
/* Parse and strip header */
type = le32_to_cpu ( header->type );
iob_pull ( iobuf, sizeof ( *header ) );
/* Handle message */
rndis_rx_message ( rndis, iob_disown ( iobuf ), type );
return;
drop:
/* Record error */
netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
}
/**
* Discard packet from underlying transport layer
*
* @v rndis RNDIS device
* @v iobuf I/O buffer
* @v rc Packet status code
*/
void rndis_rx_err ( struct rndis_device *rndis, struct io_buffer *iobuf,
int rc ) {
struct net_device *netdev = rndis->netdev;
/* Record error */
netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
}
/**
* Set receive filter
*
* @v rndis RNDIS device
* @v filter Receive filter
* @ret rc Return status code
*/
static int rndis_filter ( struct rndis_device *rndis, unsigned int filter ) {
uint32_t value = cpu_to_le32 ( filter );
int rc;
/* Set receive filter */
if ( ( rc = rndis_oid ( rndis, RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
&value, sizeof ( value ) ) ) != 0 ) {
DBGC ( rndis, "RNDIS %s could not set receive filter to %#08x: "
"%s\n", rndis->name, filter, strerror ( rc ) );
return rc;
}
return 0;
}
/**
* Open network device
*
* @v netdev Network device
* @ret rc Return status code
*/
static int rndis_open ( struct net_device *netdev ) {
struct rndis_device *rndis = netdev->priv;
int rc;
/* Open RNDIS device */
if ( ( rc = rndis->op->open ( rndis ) ) != 0 ) {
DBGC ( rndis, "RNDIS %s could not open: %s\n",
rndis->name, strerror ( rc ) );
goto err_open;
}
/* Initialise RNDIS */
if ( ( rc = rndis_initialise ( rndis ) ) != 0 )
goto err_initialise;
/* Set receive filter */
if ( ( rc = rndis_filter ( rndis, ( RNDIS_FILTER_UNICAST |
RNDIS_FILTER_MULTICAST |
RNDIS_FILTER_ALL_MULTICAST |
RNDIS_FILTER_BROADCAST |
RNDIS_FILTER_PROMISCUOUS ) ) ) != 0)
goto err_set_filter;
/* Update link status */
if ( ( rc = rndis_oid ( rndis, RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
NULL, 0 ) ) != 0 )
goto err_query_link;
return 0;
err_query_link:
err_set_filter:
rndis_halt ( rndis );
err_initialise:
rndis->op->close ( rndis );
err_open:
return rc;
}
/**
* Close network device
*
* @v netdev Network device
*/
static void rndis_close ( struct net_device *netdev ) {
struct rndis_device *rndis = netdev->priv;
/* Clear receive filter */
rndis_filter ( rndis, 0 );
/* Halt RNDIS device */
rndis_halt ( rndis );
/* Close RNDIS device */
rndis->op->close ( rndis );
}
/**
* Transmit packet
*
* @v netdev Network device
* @v iobuf I/O buffer
* @ret rc Return status code
*/
static int rndis_transmit ( struct net_device *netdev,
struct io_buffer *iobuf ) {
struct rndis_device *rndis = netdev->priv;
/* Transmit data packet */
return rndis_tx_data ( rndis, iobuf );
}
/**
* Poll for completed and received packets
*
* @v netdev Network device
*/
static void rndis_poll ( struct net_device *netdev ) {
struct rndis_device *rndis = netdev->priv;
/* Poll RNDIS device */
rndis->op->poll ( rndis );
}
/** Network device operations */
static struct net_device_operations rndis_operations = {
.open = rndis_open,
.close = rndis_close,
.transmit = rndis_transmit,
.poll = rndis_poll,
};
/**
* Allocate RNDIS device
*
* @v priv_len Length of private data
* @ret rndis RNDIS device, or NULL on allocation failure
*/
struct rndis_device * alloc_rndis ( size_t priv_len ) {
struct net_device *netdev;
struct rndis_device *rndis;
/* Allocate and initialise structure */
netdev = alloc_etherdev ( sizeof ( *rndis ) + priv_len );
if ( ! netdev )
return NULL;
netdev_init ( netdev, &rndis_operations );
rndis = netdev->priv;
rndis->netdev = netdev;
rndis->priv = ( ( ( void * ) rndis ) + sizeof ( *rndis ) );
return rndis;
}
/**
* Register RNDIS device
*
* @v rndis RNDIS device
* @ret rc Return status code
*
* Note that this routine will open and use the RNDIS device in order
* to query the MAC address. The device must be immediately ready for
* use prior to registration.
*/
int register_rndis ( struct rndis_device *rndis ) {
struct net_device *netdev = rndis->netdev;
int rc;
/* Assign device name (for debugging) */
rndis->name = netdev->dev->name;
/* Register network device */
if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
DBGC ( rndis, "RNDIS %s could not register: %s\n",
rndis->name, strerror ( rc ) );
goto err_register;
}
/* Open RNDIS device to read MAC addresses */
if ( ( rc = rndis->op->open ( rndis ) ) != 0 ) {
DBGC ( rndis, "RNDIS %s could not open: %s\n",
rndis->name, strerror ( rc ) );
goto err_open;
}
/* Initialise RNDIS */
if ( ( rc = rndis_initialise ( rndis ) ) != 0 )
goto err_initialise;
/* Query permanent MAC address */
if ( ( rc = rndis_oid ( rndis, RNDIS_OID_802_3_PERMANENT_ADDRESS,
NULL, 0 ) ) != 0 )
goto err_query_permanent;
/* Query current MAC address */
if ( ( rc = rndis_oid ( rndis, RNDIS_OID_802_3_CURRENT_ADDRESS,
NULL, 0 ) ) != 0 )
goto err_query_current;
/* Get link status */
if ( ( rc = rndis_oid ( rndis, RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
NULL, 0 ) ) != 0 )
goto err_query_link;
/* Halt RNDIS device */
rndis_halt ( rndis );
/* Close RNDIS device */
rndis->op->close ( rndis );
return 0;
err_query_link:
err_query_current:
err_query_permanent:
rndis_halt ( rndis );
err_initialise:
rndis->op->close ( rndis );
err_open:
unregister_netdev ( netdev );
err_register:
return rc;
}
/**
* Unregister RNDIS device
*
* @v rndis RNDIS device
*/
void unregister_rndis ( struct rndis_device *rndis ) {
struct net_device *netdev = rndis->netdev;
/* Unregister network device */
unregister_netdev ( netdev );
}
/**
* Free RNDIS device
*
* @v rndis RNDIS device
*/
void free_rndis ( struct rndis_device *rndis ) {
struct net_device *netdev = rndis->netdev;
/* Free network device */
netdev_nullify ( netdev );
netdev_put ( netdev );
}
|