List only ethernet interfaces
[oweals/nmrpflash.git] / nmrp.c
1 /**
2  * nmrp-flash - Netgear Unbrick Utility
3  * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
4  *
5  * nmrp-flash is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * nmrp-flash is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with nmrp-flash.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19
20 #define _BSD_SOURCE
21 #include <netinet/if_ether.h>
22 //#include <linux/if_packet.h>
23 #include <arpa/inet.h>
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
26 #include <net/if.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <time.h>
33 #include "ethsock.h"
34 #include "nmrpd.h"
35
36 #define NMRP_HDR_LEN 6
37 #define NMRP_OPT_LEN 4
38 #define NMRP_MIN_PKT_LEN (sizeof(struct ether_header) +  NMRP_HDR_LEN)
39
40 #define MAX_OPT_SIZE 12
41 #define MAX_OPT_NUM 2
42
43 #define ETH_P_NMRP 0x0912
44 #define IP_LEN 4
45 #define PACKED __attribute__((__packed__))
46 #define MAX_LOOP_RECV 1024
47
48 enum nmrp_code {
49         NMRP_C_NONE = 0,
50         NMRP_C_ADVERTISE = 1,
51         NMRP_C_CONF_REQ = 2,
52         NMRP_C_CONF_ACK = 3,
53         NMRP_C_CLOSE_REQ = 4,
54         NMRP_C_CLOSE_ACK = 5,
55         NMRP_C_KEEP_ALIVE_REQ = 6,
56         NMRP_C_KEEP_ALIVE_ACK = 7,
57         NMRP_C_TFTP_UL_REQ = 16
58 };
59
60 enum nmrp_opt_type {
61         NMRP_O_MAGIC_NO = 0x0001,
62         NMRP_O_DEV_IP = 0x0002,
63         NMRP_O_DEV_REGION = 0x0004,
64         NMRP_O_FW_UP = 0x0101,
65         NMRP_O_ST_UP = 0x0102,
66         NMRP_O_FILE_NAME = 0x0181
67 };
68
69 struct nmrp_opt {
70         uint16_t type;
71         uint16_t len;
72         union {
73                 uint8_t magic[4];
74                 struct {
75                         uint8_t addr[4];
76                         uint8_t mask[4];
77                 } ip;
78         } val;
79 } PACKED;
80
81 struct nmrp_msg {
82         uint16_t reserved;
83         uint8_t code;
84         uint8_t id;
85         uint16_t len;
86         struct nmrp_opt opts[2];
87         uint32_t num_opts;
88 } PACKED;
89
90 struct nmrp_pkt {
91         struct ether_header eh;
92         struct nmrp_msg msg;
93 } PACKED;
94
95 static void msg_update_len(struct nmrp_msg *msg)
96 {
97         uint32_t i = 0;
98         msg->len = NMRP_HDR_LEN;
99         for (; i != msg->num_opts; ++i) {
100                 msg->len += msg->opts[i].len;
101         }
102 }
103
104 static void msg_dump(struct nmrp_msg *msg, int dump_opts)
105 {
106         struct nmrp_opt *opt;
107         int remain_len, len, i;
108
109         fprintf(stderr, "res=0x%04x, code=0x%02x, id=0x%02x, len=%u",
110                         msg->reserved, msg->code, msg->id, msg->len);
111
112         remain_len = msg->len - NMRP_HDR_LEN;
113         fprintf(stderr, "%s\n", remain_len ? "" : " (no opts)");
114
115         if (dump_opts) {
116                 opt = msg->opts;
117
118                 while (remain_len > 0) {
119                         len = opt->len;
120                         fprintf(stderr, "  opt type=%u, len=%u", opt->type, len);
121                         for (i = 0; i != len - NMRP_OPT_LEN; ++i) {
122                                 if (!(i % 16)) {
123                                         fprintf(stderr, "\n  ");
124                                 }
125
126                                 fprintf(stderr, "%02x ", ((char*)&opt->val)[i] & 0xff);
127                         }
128                         fprintf(stderr, "\n");
129                         remain_len -= len;
130                         opt = (struct nmrp_opt*)(((char*)opt) + len);
131                 }
132         }
133 }
134
135 static void msg_hton(struct nmrp_msg *msg)
136 {
137         uint32_t i = 0;
138
139         msg->reserved = htons(msg->reserved);
140         msg->len = htons(msg->len);
141
142         for (; i != msg->num_opts; ++i) {
143                 msg->opts[i].len = htons(msg->opts[i].len);
144                 msg->opts[i].type = htons(msg->opts[i].type);
145         }
146 }
147
148 static void msg_hdr_ntoh(struct nmrp_msg *msg)
149 {
150         msg->reserved = ntohs(msg->reserved);
151         msg->len = ntohs(msg->len);
152 }
153
154 static int msg_ntoh(struct nmrp_msg *msg)
155 {
156         struct nmrp_opt *opt = msg->opts;
157         int remaining;
158
159         msg_hdr_ntoh(msg);
160         remaining = msg->len - NMRP_HDR_LEN;
161
162         // FIXME maximum of two options supported, maximum option
163         // size is 12
164         if (remaining < MAX_OPT_NUM * MAX_OPT_SIZE) {
165                 while (remaining > 0) {
166                         if (remaining < NMRP_OPT_LEN) {
167                                 break;
168                         }
169
170                         opt->type = ntohs(opt->type);
171                         opt->len = ntohs(opt->len);
172
173                         if (opt->len > MAX_OPT_SIZE) {
174                                 break;
175                         }
176
177                         remaining -= opt->len;
178                         ++opt;
179                 }
180
181                 if (!remaining) {
182                         return 0;
183                 }
184         }
185
186         fprintf(stderr, "Unexpected message format.\n");
187         msg_dump(msg, 0);
188         return 1;
189 }
190
191 static int pkt_send(struct ethsock *sock, struct nmrp_pkt *pkt)
192 {
193         size_t len = ntohs(pkt->msg.len) + sizeof(pkt->eh);
194         return ethsock_send(sock, pkt, len);
195 }
196
197 static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
198 {
199         ssize_t bytes, len;
200
201         memset(pkt, 0, sizeof(*pkt));
202         bytes = ethsock_recv(sock, pkt, sizeof(*pkt));
203         if (bytes < 0) {
204                 return 1;
205         } else if (!bytes) {
206                 return 2;
207         } else if (bytes < NMRP_MIN_PKT_LEN) {
208                 fprintf(stderr, "Short packet (%zi bytes)\n", bytes);
209                 return 1;
210         }
211
212         msg_hdr_ntoh(&pkt->msg);
213         len = pkt->msg.len + sizeof(pkt->eh);
214
215         if (bytes != len) {
216                 fprintf(stderr, "Unexpected message length (%zi bytes).\n", len);
217                 return 1;
218         }
219
220         return msg_ntoh(&pkt->msg);
221 }
222
223 static int mac_parse(const char *str, uint8_t *hwaddr)
224 {
225         int i;
226         unsigned data[6];
227
228         sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x%n",
229                         data, data + 1, data + 2, data + 3, data + 4, data + 5, &i);
230
231         if (i == strlen(str)) {
232                 for (i = 0; i != 6; ++i) {
233                         if (data[i] > 255) {
234                                 break;
235                         }
236
237                         hwaddr[i] = data[i] & 0xff;
238                 }
239
240                 if (i == 6) {
241                         return 1;
242                 }
243         }
244         return 0;
245 }
246
247 static const char *spinner = "\\|/-";
248
249 int nmrp_do(struct nmrpd_args *args)
250 {
251         struct nmrp_pkt tx, rx;
252         uint8_t *src, dest[6];
253         struct in_addr ipaddr, ipmask;
254         time_t beg;
255         int i, err, ulreqs, expect;
256         struct ethsock *sock;
257
258         if (args->op != NMRP_UPLOAD_FW) {
259                 fprintf(stderr, "Operation not implemented.\n");
260                 return 1;
261         }
262
263         if (!mac_parse(args->mac, dest)) {
264                 fprintf(stderr, "Invalid MAC address '%s'.\n", args->mac);
265                 return 1;
266         }
267
268         if (!inet_aton(args->ipaddr, &ipaddr)) {
269                 fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
270                 return 1;
271         }
272
273         if (!inet_aton(args->ipmask, &ipmask)) {
274                 fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
275                 return 1;
276         }
277
278         if (access(args->filename, R_OK) == -1) {
279                 fprintf(stderr, "Error accessing file '%s'.\n", args->filename);
280                 return 1;
281         }
282
283         err = 1;
284
285         sock = ethsock_create(args->intf, ETH_P_NMRP);
286         if (!sock) {
287                 return 1;
288         }
289
290         if (ethsock_set_timeout(sock, args->rx_timeout)) {
291                 return 1;
292         }
293
294         src = ethsock_get_hwaddr(sock);
295         if (!src) {
296                 return 1;
297         }
298
299         memcpy(tx.eh.ether_shost, src, 6);
300         memcpy(tx.eh.ether_dhost, dest, 6);
301         tx.eh.ether_type = htons(ETH_P_NMRP);
302
303         tx.msg.reserved = 0;
304         tx.msg.code = NMRP_C_ADVERTISE;
305         tx.msg.id = 0;
306         tx.msg.num_opts = 1;
307         tx.msg.opts[0].type = NMRP_O_MAGIC_NO;
308         tx.msg.opts[0].len = NMRP_OPT_LEN + 4;
309         tx.msg.opts[0].val.magic[0] = 'N';
310         tx.msg.opts[0].val.magic[1] = 'T';
311         tx.msg.opts[0].val.magic[2] = 'G';
312         tx.msg.opts[0].val.magic[3] = 'R';
313
314         msg_update_len(&tx.msg);
315         msg_hton(&tx.msg);
316
317         i = 0;
318         beg = time(NULL);
319
320         while (1) {
321                 printf("\rAdvertising NMRP server on %s ... %c", args->intf,
322                                 spinner[i]);
323                 fflush(stdout);
324                 i = (i + 1) & 3;
325
326                 if (pkt_send(sock, &tx) < 0) {
327                         perror("sendto");
328                         goto out;
329                 }
330
331                 err = pkt_recv(sock, &rx);
332                 if (err == 0 && memcmp(rx.eh.ether_dhost, src, 6) == 0) {
333                         break;
334                 } else if (err == 1) {
335                         printf("ERR\n");
336                         goto out;
337                 } else {
338                         if ((time(NULL) - beg) >= 60) {
339                                 printf("\nNo response after 60 seconds. Bailing out.\n");
340                                 goto out;
341                         }
342                 }
343         }
344
345         printf("\n");
346
347         expect = NMRP_C_CONF_REQ;
348         ulreqs = 0;
349
350         do {
351                 if (expect != NMRP_C_NONE && rx.msg.code != expect) {
352                         fprintf(stderr, "Received code 0x%02x while waiting for 0x%02x!\n", 
353                                         rx.msg.code, expect);
354                 }
355
356                 tx.msg.code = NMRP_C_NONE;
357                 tx.msg.reserved = 0;
358                 tx.msg.id = 0;
359                 tx.msg.num_opts = 0;
360                 tx.msg.len = 0;
361
362                 err = 1;
363
364                 switch (rx.msg.code) {
365                         case NMRP_C_ADVERTISE:
366                                 printf("Received NMRP advertisement from "
367                                                 "%02x:%02x:%02x:%02x:%02x:%02x.\n",
368                                                 rx.eh.ether_shost[0], rx.eh.ether_shost[1],
369                                                 rx.eh.ether_shost[2], rx.eh.ether_shost[3],
370                                                 rx.eh.ether_shost[4], rx.eh.ether_shost[5]);
371                                 err = 1;
372                                 goto out;
373                         case NMRP_C_CONF_REQ:
374                                 tx.msg.code = NMRP_C_CONF_ACK;
375                                 tx.msg.num_opts = 2;
376
377                                 tx.msg.opts[0].type = NMRP_O_DEV_IP;
378                                 tx.msg.opts[0].len = NMRP_OPT_LEN + 2 * 4;
379
380                                 memcpy(tx.msg.opts[0].val.ip.addr, &ipaddr, 4);
381                                 memcpy(tx.msg.opts[0].val.ip.mask, &ipmask, 4);
382
383                                 tx.msg.opts[1].type = NMRP_O_FW_UP;
384                                 tx.msg.opts[1].len = NMRP_OPT_LEN;
385
386                                 expect = NMRP_C_TFTP_UL_REQ;
387
388                                 printf("Received configuration request from "
389                                                 "%02x:%02x:%02x:%02x:%02x:%02x.\n",
390                                                 rx.eh.ether_shost[0], rx.eh.ether_shost[1],
391                                                 rx.eh.ether_shost[2], rx.eh.ether_shost[3],
392                                                 rx.eh.ether_shost[4], rx.eh.ether_shost[5]);
393
394                                 memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6);
395
396                                 printf("Sending configuration: ip %s, mask %s.\n",
397                                                 args->ipaddr, args->ipmask);
398
399                                 break;
400                         case NMRP_C_TFTP_UL_REQ:
401                                 if (++ulreqs > 5) {
402                                         fprintf(stderr, "Device re-requested file upload %d "
403                                                         "times; aborting.\n", ulreqs);
404                                         tx.msg.code = NMRP_C_CLOSE_REQ;
405                                         break;
406                                 }
407
408                                 if (!args->tftpcmd) {
409                                         printf("Uploading %s ... ", args->filename);
410                                         fflush(stdout);
411                                         err = tftp_put(args);
412                                 } else {
413                                         printf("Running %s ... ", args->tftpcmd);
414                                         fflush(stdout);
415                                         err = system(args->tftpcmd);
416                                 }
417
418                                 if (!err) {
419                                         printf("OK\nWaiting for remote to respond.\n");
420                                         ethsock_set_timeout(sock, args->ul_timeout);
421                                         expect = NMRP_C_CLOSE_REQ;
422                                 } else {
423                                         printf("\n");
424                                         goto out;
425                                 }
426
427                                 break;
428                         case NMRP_C_KEEP_ALIVE_REQ:
429                                 tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
430                                 break;
431                         case NMRP_C_CLOSE_REQ:
432                                 tx.msg.code = NMRP_C_CLOSE_ACK;
433                                 break;
434                         case NMRP_C_CLOSE_ACK:
435                                 err = 0;
436                                 goto out;
437                         default:
438                                 fprintf(stderr, "Unknown message code 0x%02x!\n",
439                                                 rx.msg.code);
440                                 msg_dump(&rx.msg, 0);
441                 }
442
443                 if (tx.msg.code != NMRP_C_NONE) {
444                         msg_update_len(&tx.msg);
445                         msg_hton(&tx.msg);
446
447                         if (pkt_send(sock, &tx) < 0) {
448                                 perror("sendto");
449                                 goto out;
450                         }
451                 }
452
453                 if (rx.msg.code == NMRP_C_CLOSE_REQ) {
454                         printf("Remote finished. Closing connection.\n");
455                         break;
456                 }
457
458                 err = pkt_recv(sock, &rx);
459                 if (err) {
460                         if (err == 2) {
461                                 fprintf(stderr, "Timeout while waiting for 0x%02x.\n", expect);
462                         }
463                         goto out;
464                 }
465
466                 ethsock_set_timeout(sock, args->rx_timeout);
467
468         } while (1);
469
470         err = 0;
471
472 out:
473         ethsock_close(sock);
474         return err;
475 }