|
| 1 | +package rndis |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "fmt" |
| 6 | +) |
| 7 | + |
| 8 | +// CommonHeader is the 8-byte prefix for all RNDIS Control Messages. |
| 9 | +type CommonHeader struct { |
| 10 | + MessageType uint32 |
| 11 | + MessageLength uint32 |
| 12 | +} |
| 13 | + |
| 14 | +// RemoteNdisInitializeMsg is sent by Host -> Device to start the handshake. |
| 15 | +type RemoteNdisInitializeMsg struct { |
| 16 | + MessageType uint32 // MsgInit (0x02) |
| 17 | + MessageLength uint32 // 24 bytes |
| 18 | + RequestID uint32 |
| 19 | + MajorVersion uint32 // 1 |
| 20 | + MinorVersion uint32 // 0 |
| 21 | + MaxTransferSz uint32 // Recommended: 16384 (16KB) |
| 22 | +} |
| 23 | + |
| 24 | +func (m *RemoteNdisInitializeMsg) Marshal() []byte { |
| 25 | + b := make([]byte, 24) |
| 26 | + binary.LittleEndian.PutUint32(b[0:4], MsgInit) |
| 27 | + binary.LittleEndian.PutUint32(b[4:8], 24) |
| 28 | + binary.LittleEndian.PutUint32(b[8:12], m.RequestID) |
| 29 | + binary.LittleEndian.PutUint32(b[12:16], 1) // Major |
| 30 | + binary.LittleEndian.PutUint32(b[16:20], 0) // Minor |
| 31 | + binary.LittleEndian.PutUint32(b[20:24], m.MaxTransferSz) |
| 32 | + return b |
| 33 | +} |
| 34 | + |
| 35 | +// RemoteNdisInitializeCmplt is sent by Device -> Host. |
| 36 | +type RemoteNdisInitializeCmplt struct { |
| 37 | + RequestID uint32 |
| 38 | + Status uint32 |
| 39 | + MajorVersion uint32 |
| 40 | + MinorVersion uint32 |
| 41 | + MaxTransfer uint32 |
| 42 | + MaxPackets uint32 |
| 43 | + PacketFormat uint32 // RNDIS_PACKET_FORMAT_ETH |
| 44 | +} |
| 45 | + |
| 46 | +func UnmarshalInitializeCmplt(data []byte) (*RemoteNdisInitializeCmplt, error) { |
| 47 | + if len(data) < 52 { |
| 48 | + return nil, fmt.Errorf("init_cmplt: message too short (%d)", len(data)) |
| 49 | + } |
| 50 | + // Verify it's a Completion for Init |
| 51 | + msgType := binary.LittleEndian.Uint32(data[0:4]) |
| 52 | + if msgType != MsgInitCmplt { |
| 53 | + return nil, fmt.Errorf("init_cmplt: wrong message type 0x%08X", msgType) |
| 54 | + } |
| 55 | + |
| 56 | + return &RemoteNdisInitializeCmplt{ |
| 57 | + RequestID: binary.LittleEndian.Uint32(data[8:12]), |
| 58 | + Status: binary.LittleEndian.Uint32(data[12:16]), |
| 59 | + MajorVersion: binary.LittleEndian.Uint32(data[16:20]), |
| 60 | + MinorVersion: binary.LittleEndian.Uint32(data[20:24]), |
| 61 | + MaxTransfer: binary.LittleEndian.Uint32(data[32:36]), |
| 62 | + MaxPackets: binary.LittleEndian.Uint32(data[36:40]), |
| 63 | + PacketFormat: binary.LittleEndian.Uint32(data[44:48]), |
| 64 | + }, nil |
| 65 | +} |
| 66 | + |
| 67 | +// RemoteNdisQueryMsg is sent by Host -> Device to query OIDs (e.g., getting MAC address). |
| 68 | +type RemoteNdisQueryMsg struct { |
| 69 | + RequestID uint32 |
| 70 | + OID uint32 |
| 71 | + InformationBufferLength uint32 // for simple queries, usually 0 |
| 72 | +} |
| 73 | + |
| 74 | +func (m *RemoteNdisQueryMsg) Marshal() []byte { |
| 75 | + b := make([]byte, 28) |
| 76 | + binary.LittleEndian.PutUint32(b[0:4], MsgQuery) |
| 77 | + binary.LittleEndian.PutUint32(b[4:8], 28) |
| 78 | + binary.LittleEndian.PutUint32(b[8:12], m.RequestID) |
| 79 | + binary.LittleEndian.PutUint32(b[12:16], m.OID) |
| 80 | + binary.LittleEndian.PutUint32(b[16:20], 0) // Info Buffer Length |
| 81 | + binary.LittleEndian.PutUint32(b[20:24], 20) // Info Buffer Offset (offset from RequestID field, which is 8 index + 12 = 20) |
| 82 | + binary.LittleEndian.PutUint32(b[24:28], 0) // Device Context |
| 83 | + return b |
| 84 | +} |
| 85 | + |
| 86 | +// RemoteNdisQueryCmplt is sent by Device -> Host. |
| 87 | +type RemoteNdisQueryCmplt struct { |
| 88 | + RequestID uint32 |
| 89 | + Status uint32 |
| 90 | + Payload []byte |
| 91 | +} |
| 92 | + |
| 93 | +func UnmarshalQueryCmplt(data []byte) (*RemoteNdisQueryCmplt, error) { |
| 94 | + if len(data) < 24 { |
| 95 | + return nil, fmt.Errorf("query_cmplt: message too short (%d)", len(data)) |
| 96 | + } |
| 97 | + msgType := binary.LittleEndian.Uint32(data[0:4]) |
| 98 | + if msgType != MsgQueryCmplt { |
| 99 | + return nil, fmt.Errorf("query_cmplt: wrong message type 0x%08X", msgType) |
| 100 | + } |
| 101 | + |
| 102 | + infoLen := binary.LittleEndian.Uint32(data[16:20]) |
| 103 | + infoOff := binary.LittleEndian.Uint32(data[20:24]) // Offset starts at byte 8 (RequestID) |
| 104 | + |
| 105 | + start := int(8 + infoOff) |
| 106 | + end := start + int(infoLen) |
| 107 | + |
| 108 | + if end > len(data) { |
| 109 | + return nil, fmt.Errorf("query_cmplt: payload out of bounds") |
| 110 | + } |
| 111 | + |
| 112 | + return &RemoteNdisQueryCmplt{ |
| 113 | + RequestID: binary.LittleEndian.Uint32(data[8:12]), |
| 114 | + Status: binary.LittleEndian.Uint32(data[12:16]), |
| 115 | + Payload: data[start:end], |
| 116 | + }, nil |
| 117 | +} |
| 118 | + |
| 119 | +// RemoteNdisSetMsg is sent by Host -> Device to set OIDs (e.g., enable packet processing). |
| 120 | +type RemoteNdisSetMsg struct { |
| 121 | + RequestID uint32 |
| 122 | + OID uint32 |
| 123 | + Value uint32 |
| 124 | +} |
| 125 | + |
| 126 | +func (m *RemoteNdisSetMsg) Marshal() []byte { |
| 127 | + b := make([]byte, 32) |
| 128 | + binary.LittleEndian.PutUint32(b[0:4], MsgSet) |
| 129 | + binary.LittleEndian.PutUint32(b[4:8], 32) |
| 130 | + binary.LittleEndian.PutUint32(b[8:12], m.RequestID) |
| 131 | + binary.LittleEndian.PutUint32(b[12:16], m.OID) |
| 132 | + binary.LittleEndian.PutUint32(b[16:20], 4) // Value size (uint32) |
| 133 | + binary.LittleEndian.PutUint32(b[20:24], 20) // Value offset (from byte 8) |
| 134 | + binary.LittleEndian.PutUint32(b[24:28], 0) // Device Context |
| 135 | + binary.LittleEndian.PutUint32(b[28:32], m.Value) |
| 136 | + return b |
| 137 | +} |
| 138 | + |
| 139 | +func UnmarshalSetCmplt(data []byte) (uint32, uint32, error) { // returns RequestID, Status |
| 140 | + if len(data) < 16 { |
| 141 | + return 0, 0, fmt.Errorf("set_cmplt: too short") |
| 142 | + } |
| 143 | + msgType := binary.LittleEndian.Uint32(data[0:4]) |
| 144 | + if msgType != MsgSetCmplt { |
| 145 | + return 0, 0, fmt.Errorf("set_cmplt: wrong msg type") |
| 146 | + } |
| 147 | + return binary.LittleEndian.Uint32(data[8:12]), binary.LittleEndian.Uint32(data[12:16]), nil |
| 148 | +} |
0 commit comments