This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwasp_downloader.c
287 lines (248 loc) · 7 KB
/
wasp_downloader.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
/*
* Simple WASP overlay configuration downloader for AVM FRITZ!Box devices
*
* The protocol was found by sniffing ethernet traffic between the two SoCs,
* so some things might be wrong or incomplete.
*
* Important: if the switch is configured for VLAN tagging, the eth0.1 interface
* has to be used, not eth0!
*
* (c) 2020 Andreas Böhler
* GPLv2, see LICENSE
*/
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/ether.h>
#include <unistd.h>
#include <poll.h>
#define ETHER_TYPE 0x88bd
#define BUF_SIZE 1056
#define COUNTER_INCR 4
#define MAX_PAYLOAD_SIZE 1028
#define CHUNK_SIZE 1024
#define WASP_HEADER_LEN 14
#define PACKET_START 0x1200
#define CMD_FIRMWARE_DATA 0x0104
#define CMD_START_FIRMWARE 0xd400
#define RESP_DISCOVER 0x0000
#define RESP_CONFIG 0x1000
#define RESP_OK 0x0100
#define RESP_STARTING 0x0200
#define RESP_ERROR 0x0300
#define PACKET_SIZE 46
typedef enum {
STATE_DISCOVERING,
STATE_TRANSFERRING,
STATE_FINISHED,
STATE_FAIL
} t_state;
static int m_socket_initialized = 0;
static t_state m_state = STATE_DISCOVERING;
static int m_packet_counter = 0;
typedef struct __attribute__((packed)) {
union {
uint8_t data[MAX_PAYLOAD_SIZE + WASP_HEADER_LEN];
struct __attribute__((packed)) {
uint16_t packet_start;
uint8_t pad_one[5];
uint16_t command;
uint16_t response;
uint16_t counter;
uint8_t pad_two;
uint8_t payload[MAX_PAYLOAD_SIZE];
};
};
} t_wasp_packet;
static int send_packet(uint8_t *dest, t_wasp_packet *packet, int payloadlen, char *devname) {
char sendbuf[BUF_SIZE];
static int sockfd;
static struct ifreq if_idx;
static struct ifreq if_mac;
struct ether_header *eh = (struct ether_header *) sendbuf;
int tx_len = 0;
struct sockaddr_ll socket_address;
if(!m_socket_initialized) {
if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) {
perror("socket");
return 1;
}
/* Get the index of the interface to send on */
memset(&if_idx, 0, sizeof(struct ifreq));
strncpy(if_idx.ifr_name, devname, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFINDEX, &if_idx) < 0) {
perror("SIOCGIFINDEX");
return 1;
}
/* Get the MAC address of the interface to send on */
memset(&if_mac, 0, sizeof(struct ifreq));
strncpy(if_mac.ifr_name, devname, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFHWADDR, &if_mac) < 0) {
perror("SIOCGIFHWADDR");
return 1;
}
m_socket_initialized = 1;
}
memset(sendbuf, 0, BUF_SIZE);
eh->ether_shost[0] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[0];
eh->ether_shost[1] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[1];
eh->ether_shost[2] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[2];
eh->ether_shost[3] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[3];
eh->ether_shost[4] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[4];
eh->ether_shost[5] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[5];
for(int i=0; i<6; i++) {
eh->ether_dhost[i] = dest[i];
}
/* Ethertype field */
eh->ether_type = ETHER_TYPE;
tx_len += sizeof(struct ether_header);
for(int i=0; i<WASP_HEADER_LEN; i++) {
sendbuf[tx_len++] = packet->data[i];
}
for(int i=0; i<payloadlen; i++) {
sendbuf[tx_len++] = packet->data[WASP_HEADER_LEN + i];
}
/* Index of the network device */
socket_address.sll_ifindex = if_idx.ifr_ifindex;
/* Address length*/
socket_address.sll_halen = ETH_ALEN;
/* Destination MAC */
for(int i=0; i<6; i++) {
socket_address.sll_addr[i] = dest[i];
}
/* Send packet */
if (sendto(sockfd, sendbuf, tx_len, 0, (struct sockaddr*)&socket_address, sizeof(struct sockaddr_ll)) < 0) {
printf("Send failed\n");
return 1;
}
return 0;
}
int main(int argc, char *argv[]) {
int sockfd;
int sockopt;
uint8_t buf[BUF_SIZE];
uint8_t dest[6];
struct ifreq ifopts; /* set promiscuous mode */
t_wasp_packet *packet = (t_wasp_packet *) (buf + sizeof(struct ether_header));
t_wasp_packet s_packet;
struct pollfd fd;
int ret;
int done = 0;
ssize_t numbytes;
FILE *fp = NULL;
printf("AVM WASP Config downloader.\n");
if(argc < 3) {
printf("Usage: %s eth0 /tmp/config.tar.gz\n", argv[0]);
return 1;
}
printf("Using file: %s\n", argv[2]);
printf("Using Dev : %s\n", argv[1]);
/* Header structures */
struct ether_header *eh = (struct ether_header *) buf;
//struct iphdr *iph = (struct iphdr *) (buf + sizeof(struct ether_header));
//struct udphdr *udph = (struct udphdr *) (buf + sizeof(struct iphdr) + sizeof(struct ether_header));
/* Open PF_PACKET socket, listening for EtherType ETHER_TYPE */
if ((sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETHER_TYPE))) == -1) {
perror("listener: socket");
return -1;
}
strncpy(ifopts.ifr_name, argv[1], IFNAMSIZ-1);
ioctl(sockfd, SIOCGIFFLAGS, &ifopts);
ifopts.ifr_flags |= IFF_PROMISC;
ioctl(sockfd, SIOCSIFFLAGS, &ifopts);
/* Allow the socket to be reused - incase connection is closed prematurely */
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof sockopt) == -1) {
perror("setsockopt");
close(sockfd);
exit(EXIT_FAILURE);
}
/* Bind to device */
if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, argv[1], IFNAMSIZ-1) == -1) {
perror("SO_BINDTODEVICE");
close(sockfd);
exit(EXIT_FAILURE);
}
fd.fd = sockfd; // your socket handler
fd.events = POLLIN;
while(!done) {
memset(&s_packet, 9, sizeof(s_packet));
s_packet.packet_start = PACKET_START;
switch(m_state) {
case STATE_DISCOVERING:
s_packet.response = RESP_CONFIG;
for(int i=0; i<6; i++) {
dest[i] = 0xff;
}
break;
case STATE_TRANSFERRING:
s_packet.response = RESP_OK;
break;
case STATE_FINISHED:
s_packet.response = RESP_STARTING;
done = 1;
break;
case STATE_FAIL:
s_packet.response = RESP_ERROR;
done = 1;
break;
default:
break;
}
send_packet(dest, &s_packet, PACKET_SIZE, argv[1]);
if(done)
continue;
ret = poll(&fd, 1, 1000); // 1 second for timeout
switch (ret) {
case -1:
// Error
done = 1;
continue;
break;
case 0:
// Timeout
continue;
break;
default:
numbytes = recv(sockfd,buf,BUF_SIZE, 0); // get your data
break;
}
if(eh->ether_type != ETHER_TYPE) {
printf("Invalid packet type.\n");
continue;
}
if(((packet->packet_start == PACKET_START) && (packet->command == CMD_FIRMWARE_DATA)) ||
((packet->packet_start == PACKET_START) && (packet->response == CMD_START_FIRMWARE))) {
for(int i=0; i<6; i++) {
dest[i] = eh->ether_shost[i];
}
m_packet_counter = packet->counter;
if(fp == NULL) {
fp = fopen(argv[2], "w");
if(fp == NULL) {
printf("Error writing file.\n");
m_state = STATE_FAIL;
continue;
}
}
fwrite(packet->payload, numbytes - sizeof(struct ether_header) - WASP_HEADER_LEN, 1, fp);
if(packet->response == CMD_START_FIRMWARE) {
m_state = STATE_FINISHED;
fclose(fp);
fp = NULL;
} else {
m_state = STATE_TRANSFERRING;
}
}
}
if(fp)
fclose(fp);
close(sockfd);
return 0;
}