Add BSD bridge interface detection
[oweals/nmrpflash.git] / nmrp.c
1 /**
2  * nmrpflash - Netgear Unbrick Utility
3  * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
4  *
5  * nmrpflash 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  * nmrpflash 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 nmrpflash.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19
20 #include <signal.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <time.h>
27 #include "nmrpd.h"
28
29 #define NMRP_HDR_LEN 6
30 #define NMRP_OPT_HDR_LEN 4
31 #define NMRP_MIN_PKT_LEN (sizeof(struct eth_hdr) +  NMRP_HDR_LEN)
32
33 #define NMRP_MAX_OPT_SIZE 12
34 #define NMRP_MAX_OPT_NUM 3
35
36 #define NMRP_OPT_NEXT(x) ((struct nmrp_opt*)(((char*)x) + x->len))
37
38 #define ETH_P_NMRP 0x0912
39 #define IP_LEN 4
40 #define MAX_LOOP_RECV 1024
41
42 #ifndef PACKED
43 #define PACKED __attribute__((__packed__))
44 #endif
45
46 #ifdef NMRPFLASH_WINDOWS
47 #define setenv(name, value, overwrite) SetEnvironmentVariable(name, value)
48 #endif
49
50 enum nmrp_code {
51         NMRP_C_NONE = 0,
52         NMRP_C_ADVERTISE = 1,
53         NMRP_C_CONF_REQ = 2,
54         NMRP_C_CONF_ACK = 3,
55         NMRP_C_CLOSE_REQ = 4,
56         NMRP_C_CLOSE_ACK = 5,
57         NMRP_C_KEEP_ALIVE_REQ = 6,
58         NMRP_C_KEEP_ALIVE_ACK = 7,
59         NMRP_C_TFTP_UL_REQ = 16
60 };
61
62 enum nmrp_opt_type {
63         NMRP_O_MAGIC_NO = 0x0001,
64         NMRP_O_DEV_IP = 0x0002,
65         NMRP_O_DEV_REGION = 0x0004,
66         NMRP_O_FW_UP = 0x0101,
67         NMRP_O_ST_UP = 0x0102,
68         NMRP_O_FILE_NAME = 0x0181
69 };
70
71 struct nmrp_opt {
72         uint16_t type;
73         uint16_t len;
74         union {
75                 uint8_t magic[4];
76                 uint16_t region;
77                 struct {
78                         uint8_t addr[4];
79                         uint8_t mask[4];
80                 } ip;
81         } val;
82 } PACKED;
83
84 struct nmrp_msg {
85         uint16_t reserved;
86         uint8_t code;
87         uint8_t id;
88         uint16_t len;
89         /* only opts[0] is valid! think of this as a char* */
90         struct nmrp_opt opts[NMRP_MAX_OPT_NUM];
91         /* this is NOT part of the transmitted packet */
92         uint32_t num_opts;
93 } PACKED;
94
95 struct nmrp_pkt {
96         struct eth_hdr eh;
97         struct nmrp_msg msg;
98 } PACKED;
99
100 static const char *msg_code_str(uint16_t code)
101 {
102 #define CASE_CODE(x) case NMRP_C_ ## x: return #x
103         static char buf[16];
104
105         switch (code) {
106                 CASE_CODE(ADVERTISE);
107                 CASE_CODE(CONF_REQ);
108                 CASE_CODE(CONF_ACK);
109                 CASE_CODE(CLOSE_REQ);
110                 CASE_CODE(CLOSE_ACK);
111                 CASE_CODE(KEEP_ALIVE_REQ);
112                 CASE_CODE(KEEP_ALIVE_ACK);
113                 CASE_CODE(TFTP_UL_REQ);
114                 default:
115                         snprintf(buf, sizeof(buf), "%04x", code);
116                         return buf;
117         }
118 #undef CASE_CODE
119 }
120
121 static uint16_t to_region_code(const char *region)
122 {
123 #define REGION_CODE(r, c) if (!strcasecmp(region, r)) return c
124         REGION_CODE("NA", 0x0001);
125         REGION_CODE("WW", 0x0002);
126         REGION_CODE("GR", 0x0003);
127         REGION_CODE("PR", 0x0004);
128         REGION_CODE("RU", 0x0005);
129         REGION_CODE("BZ", 0x0006);
130         REGION_CODE("IN", 0x0007);
131         REGION_CODE("KO", 0x0008);
132         REGION_CODE("JP", 0x0009);
133 #undef REGION_CODE
134         return 0;
135 }
136
137 static void msg_dump(struct nmrp_msg *msg)
138 {
139         int remain_len;
140
141         fprintf(stderr, "res=0x%04x, code=0x%02x, id=0x%02x, len=%u",
142                         msg->reserved, msg->code, msg->id, msg->len);
143
144         remain_len = msg->len - NMRP_HDR_LEN;
145         fprintf(stderr, "%s\n", remain_len ? "" : " (no opts)");
146 }
147
148 static void msg_hton(struct nmrp_msg *msg)
149 {
150         uint32_t i = 0;
151         struct nmrp_opt *opt = msg->opts, *next;
152
153         msg->reserved = htons(msg->reserved);
154         msg->len = htons(msg->len);
155
156         for (; i != msg->num_opts; ++i) {
157                 next = NMRP_OPT_NEXT(opt);
158                 opt->len = htons(opt->len);
159                 opt->type = htons(opt->type);
160                 opt = next;
161         }
162 }
163
164 static void msg_hdr_ntoh(struct nmrp_msg *msg)
165 {
166         msg->reserved = ntohs(msg->reserved);
167         msg->len = ntohs(msg->len);
168 }
169
170 static int msg_ntoh(struct nmrp_msg *msg)
171 {
172         struct nmrp_opt *opt = msg->opts;
173         int remaining;
174
175         remaining = msg->len - NMRP_HDR_LEN;
176
177         // FIXME maximum of two options supported, maximum option
178         // size is 12
179         if (remaining < NMRP_MAX_OPT_NUM * NMRP_MAX_OPT_SIZE) {
180                 while (remaining > 0) {
181                         if (remaining < NMRP_OPT_HDR_LEN) {
182                                 break;
183                         }
184
185                         opt->type = ntohs(opt->type);
186                         opt->len = ntohs(opt->len);
187
188                         if (!opt->len || opt->len > NMRP_MAX_OPT_SIZE) {
189                                 break;
190                         }
191
192                         remaining -= opt->len;
193                         opt = NMRP_OPT_NEXT(opt);
194                 }
195
196                 if (!remaining) {
197                         return 0;
198                 }
199         }
200
201         fprintf(stderr, "Unexpected message format.\n");
202         msg_dump(msg);
203         return 1;
204 }
205
206 static void *msg_opt_data(struct nmrp_msg *msg, uint16_t type, uint16_t *len)
207 {
208         static char buf[128];
209         struct nmrp_opt *opt = msg->opts;
210         int remaining = msg->len - NMRP_HDR_LEN;
211
212         memset(buf, 0, sizeof(buf));
213
214         while (remaining > 0) {
215                 if (opt->type == type) {
216                         if (opt->len == NMRP_OPT_HDR_LEN) {
217                                 return NULL;
218                         }
219                         *len = opt->len - NMRP_OPT_HDR_LEN;
220                         memcpy(buf, &opt->val, MIN(*len, sizeof(buf)-1));
221                         return buf;
222                 }
223
224                 if (!opt->len) {
225                         break;
226                 }
227
228                 remaining -= opt->len;
229                 opt = NMRP_OPT_NEXT(opt);
230         }
231
232         return NULL;
233 }
234
235 static void msg_opt_add(struct nmrp_msg *msg, uint16_t type, void *data,
236                 uint16_t len)
237 {
238         uint32_t i = 0;
239         struct nmrp_opt *opt = msg->opts;
240
241         if (len + NMRP_OPT_HDR_LEN > NMRP_MAX_OPT_SIZE
242                         || msg->num_opts == NMRP_MAX_OPT_NUM) {
243                 fprintf(stderr, "Invalid option - this is a bug.\n");
244         }
245
246         for (; i <= msg->num_opts; ++i) {
247                 opt = NMRP_OPT_NEXT(opt);
248         }
249
250         opt->len = NMRP_OPT_HDR_LEN + len;
251         opt->type = type;
252
253         if (len) {
254                 memcpy(&opt->val, data, len);
255         }
256
257         msg->len += opt->len;
258         ++msg->num_opts;
259 }
260
261 static inline void msg_init(struct nmrp_msg *msg, uint16_t code)
262 {
263         memset(msg, 0, sizeof(*msg));
264         msg->len = NMRP_HDR_LEN;
265         msg->code = code;
266 }
267
268 #ifdef NMRPFLASH_FUZZ
269 #define NMRP_INITIAL_TIMEOUT 0
270 #define ethsock_create(a, b) ((struct ethsock*)1)
271 #define ethsock_get_hwaddr(a) ethsock_get_hwaddr_fake(a)
272 #define ethsock_recv(sock, buf, len) read(STDIN_FILENO, buf, len)
273 #define ethsock_send(a, b, c) (0)
274 #define ethsock_set_timeout(a, b) (0)
275 #define ethsock_ip_add(a, b, c, d) (0)
276 #define ethsock_ip_del(a, b) (0)
277 #define ethsock_close(a) (0)
278 #define tftp_put(a) (0)
279
280 static uint8_t *ethsock_get_hwaddr_fake(struct ethsock* sock)
281 {
282         static uint8_t hwaddr[6] = { 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa };
283         return hwaddr;
284 }
285 #else
286 #define NMRP_INITIAL_TIMEOUT 60
287 #endif
288
289 static int pkt_send(struct ethsock *sock, struct nmrp_pkt *pkt)
290 {
291         size_t len = ntohs(pkt->msg.len) + sizeof(pkt->eh);
292         return ethsock_send(sock, pkt, len);
293 }
294
295 static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
296 {
297         ssize_t bytes, len;
298
299         memset(pkt, 0, sizeof(*pkt));
300         bytes = ethsock_recv(sock, pkt, sizeof(*pkt));
301         if (bytes < 0) {
302                 return 1;
303         } else if (!bytes) {
304                 return 2;
305         } else if (bytes < NMRP_MIN_PKT_LEN) {
306                 fprintf(stderr, "Short packet (%d bytes)\n", (int)bytes);
307                 return 1;
308         }
309
310         msg_hdr_ntoh(&pkt->msg);
311         len = pkt->msg.len + sizeof(pkt->eh);
312
313         if (bytes < len) {
314                 fprintf(stderr, "Short packet (expected %d, got %d).\n",
315                                 (int)len, (int)bytes);
316                 return 1;
317         }
318
319         return msg_ntoh(&pkt->msg);
320 }
321
322 static int mac_parse(const char *str, uint8_t *hwaddr)
323 {
324         int i;
325         unsigned data[6];
326
327         sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x%n",
328                         data, data + 1, data + 2, data + 3, data + 4, data + 5, &i);
329
330         if (i == strlen(str)) {
331                 for (i = 0; i != 6; ++i) {
332                         if (data[i] > 255) {
333                                 break;
334                         }
335
336                         hwaddr[i] = data[i] & 0xff;
337                 }
338
339                 if (i == 6) {
340                         return 1;
341                 }
342         }
343         return 0;
344 }
345
346 struct is_valid_ip_arg
347 {
348         struct in_addr *ipaddr;
349         struct in_addr *ipmask;
350         int result;
351 };
352
353 static int is_valid_ip_cb(struct ethsock_ip_callback_args *args)
354 {
355 #define SUBNET(x) ((x)->ipaddr->s_addr & (x)->ipmask->s_addr)
356         struct is_valid_ip_arg *arg = args->arg;
357         if (SUBNET(args) == SUBNET(arg)) {
358                 arg->result = args->ipaddr->s_addr != arg->ipaddr->s_addr;
359                 return 0;
360         }
361
362         return 1;
363 #undef SUBNET
364 }
365
366 static int is_valid_ip(struct ethsock *sock, struct in_addr *ipaddr,
367                 struct in_addr *ipmask)
368 {
369         int status;
370         struct is_valid_ip_arg arg = {
371                 .ipaddr = ipaddr,
372                 .ipmask = ipmask,
373                 .result = 0
374         };
375
376         status = ethsock_for_each_ip(sock, is_valid_ip_cb, &arg);
377         return status < 0 ? status : arg.result;
378 }
379
380 static void sigh(int sig)
381 {
382         g_interrupted = 1;
383 }
384
385 static const char *spinner = "\\|/-";
386
387 int nmrp_do(struct nmrpd_args *args)
388 {
389         struct nmrp_pkt tx, rx;
390         uint8_t *src, dest[6];
391         uint16_t len, region;
392         char *filename;
393         time_t beg;
394         int i, status, ulreqs, expect, upload_ok, autoip;
395         struct ethsock *sock;
396         struct ethsock_ip_undo *ip_undo = NULL;
397         struct ethsock_arp_undo *arp_undo = NULL;
398         uint32_t intf_addr;
399         void (*sigh_orig)(int);
400         struct {
401                 struct in_addr addr;
402                 struct in_addr mask;
403         } PACKED ipconf;
404
405         if (args->op != NMRP_UPLOAD_FW) {
406                 fprintf(stderr, "Operation not implemented.\n");
407                 return 1;
408         }
409
410         if (!mac_parse(args->mac, dest)) {
411                 fprintf(stderr, "Invalid MAC address '%s'.\n", args->mac);
412                 return 1;
413         }
414
415         ipconf.mask.s_addr = inet_addr(args->ipmask);
416         if (ipconf.mask.s_addr == INADDR_NONE
417                         || netmask(bitcount(ipconf.mask.s_addr)) != ipconf.mask.s_addr) {
418                 fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
419                 return 1;
420         }
421
422         if (!args->ipaddr) {
423                 autoip = true;
424                 /* The MAC of the device that was used to test this utility starts
425                  * with a4:2b:8c, hence 164 (0xa4) and 183 (0x2b + 0x8c)
426                  */
427                 args->ipaddr = "10.164.183.252";
428
429                 if (!args->ipaddr_intf) {
430                         args->ipaddr_intf = "10.164.183.253";
431                 }
432         } else if (args->ipaddr_intf) {
433                 autoip = true;
434         } else {
435                 autoip = false;
436         }
437
438         if ((ipconf.addr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
439                 fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
440                 return 1;
441         }
442
443         if (args->ipaddr_intf && (intf_addr = inet_addr(args->ipaddr_intf)) == INADDR_NONE) {
444                 fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr_intf);
445                 return 1;
446         }
447
448         if (args->file_local && strcmp(args->file_local, "-") && access(args->file_local, R_OK) == -1) {
449                 fprintf(stderr, "Error accessing file '%s'.\n", args->file_local);
450                 return 1;
451         }
452
453         if (args->file_remote) {
454                 if (!tftp_is_valid_filename(args->file_remote)) {
455                         fprintf(stderr, "Invalid remote filename '%s'.\n",
456                                         args->file_remote);
457                         return 1;
458                 }
459         }
460
461         if (args->region) {
462                 region = htons(to_region_code(args->region));
463                 if (!region) {
464                         fprintf(stderr, "Invalid region code '%s'.\n", args->region);
465                         return 1;
466                 }
467         } else {
468                 region = 0;
469         }
470
471         status = 1;
472
473         sock = ethsock_create(args->intf, ETH_P_NMRP);
474         if (!sock) {
475                 return 1;
476         }
477
478         sigh_orig = signal(SIGINT, sigh);
479
480         if (!autoip) {
481                 status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask);
482                 if (status <= 0) {
483                         if (!status) {
484                                 fprintf(stderr, "Address %s/%s cannot be used on interface %s.\n",
485                                                 args->ipaddr, args->ipmask, args->intf);
486                         }
487                         goto out;
488                 }
489         } else {
490                 if (verbosity) {
491                         printf("Adding %s to interface %s.\n", args->ipaddr_intf, args->intf);
492                 }
493
494                 if (ethsock_ip_add(sock, intf_addr, ipconf.mask.s_addr, &ip_undo) != 0) {
495                         goto out;
496                 }
497         }
498
499         if (ethsock_set_timeout(sock, args->rx_timeout)) {
500                 goto out;
501         }
502
503         src = ethsock_get_hwaddr(sock);
504         if (!src) {
505                 goto out;
506         }
507
508         memcpy(tx.eh.ether_shost, src, 6);
509         memcpy(tx.eh.ether_dhost, dest, 6);
510         tx.eh.ether_type = htons(ETH_P_NMRP);
511
512         msg_init(&tx.msg, NMRP_C_ADVERTISE);
513         msg_opt_add(&tx.msg, NMRP_O_MAGIC_NO, "NTGR", 4);
514         msg_hton(&tx.msg);
515
516         i = 0;
517         upload_ok = 0;
518         beg = time_monotonic();
519
520         while (!g_interrupted) {
521                 printf("\rAdvertising NMRP server on %s ... %c",
522                                 args->intf, spinner[i]);
523                 fflush(stdout);
524                 i = (i + 1) & 3;
525
526                 if (pkt_send(sock, &tx) < 0) {
527                         xperror("sendto");
528                         goto out;
529                 }
530
531                 status = pkt_recv(sock, &rx);
532                 if (status == 0 && memcmp(rx.eh.ether_dhost, src, 6) == 0) {
533                         break;
534                 } else if (status == 1) {
535                         goto out;
536                 } else {
537                         /* because we don't want nmrpflash's exit status to be zero */
538                         status = 1;
539                         if ((time_monotonic() - beg) >= NMRP_INITIAL_TIMEOUT) {
540                                 printf("\nNo response after 60 seconds. Bailing out.\n");
541                                 goto out;
542                         }
543                 }
544         }
545
546         printf("\n");
547
548         expect = NMRP_C_CONF_REQ;
549         ulreqs = 0;
550
551         while (!g_interrupted) {
552                 if (expect != NMRP_C_NONE && rx.msg.code != expect) {
553                         fprintf(stderr, "Received %s while waiting for %s!\n",
554                                         msg_code_str(rx.msg.code), msg_code_str(expect));
555                 }
556
557                 msg_init(&tx.msg, NMRP_C_NONE);
558
559                 status = 1;
560
561                 switch (rx.msg.code) {
562                         case NMRP_C_ADVERTISE:
563                                 printf("Received NMRP advertisement from %s.\n",
564                                                 mac_to_str(rx.eh.ether_shost));
565                                 status = 1;
566                                 goto out;
567                         case NMRP_C_CONF_REQ:
568                                 tx.msg.code = NMRP_C_CONF_ACK;
569
570                                 msg_opt_add(&tx.msg, NMRP_O_DEV_IP, &ipconf, 8);
571                                 msg_opt_add(&tx.msg, NMRP_O_FW_UP, NULL, 0);
572
573 #ifdef NMRPFLASH_SET_REGION
574                                 if (region) {
575                                         msg_opt_add(&tx.msg, NMRP_O_DEV_REGION, &region, 2);
576                                 }
577 #endif
578
579                                 expect = NMRP_C_TFTP_UL_REQ;
580
581                                 printf("Received configuration request from %s.\n",
582                                                 mac_to_str(rx.eh.ether_shost));
583
584                                 memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6);
585
586                                 printf("Sending configuration: %s, netmask %s.\n",
587                                                 args->ipaddr, args->ipmask);
588
589                                 if (ethsock_arp_add(sock, rx.eh.ether_shost, ipconf.addr.s_addr, &arp_undo) != 0) {
590                                         goto out;
591                                 }
592
593                                 break;
594                         case NMRP_C_TFTP_UL_REQ:
595                                 if (!upload_ok) {
596                                         if (++ulreqs > 5) {
597                                                 printf("Bailing out after %d upload requests.\n",
598                                                                 ulreqs);
599                                                 tx.msg.code = NMRP_C_CLOSE_REQ;
600                                                 break;
601                                         }
602                                 } else {
603                                         if (verbosity) {
604                                                 printf("Ignoring extra upload request.\n");
605                                         }
606                                         ethsock_set_timeout(sock, args->ul_timeout);
607                                         tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
608                                         break;
609                                 }
610
611                                 len = 0;
612                                 filename = msg_opt_data(&rx.msg, NMRP_O_FILE_NAME, &len);
613                                 if (filename) {
614                                         if (!args->file_remote) {
615                                                 args->file_remote = filename;
616                                         }
617                                         printf("Received upload request: filename '%.*s'.\n",
618                                                         len, filename);
619                                 } else if (!args->file_remote) {
620                                         args->file_remote = args->file_local;
621                                         printf("Received upload request with empty filename.\n");
622                                 }
623
624                                 status = 0;
625
626                                 if (args->tftpcmd) {
627                                         printf("Executing '%s' ... \n", args->tftpcmd);
628                                         setenv("IP", inet_ntoa(ipconf.addr), 1);
629                                         setenv("PORT", lltostr(args->port, 10), 1);
630                                         setenv("MAC", mac_to_str(rx.eh.ether_shost), 1);
631                                         setenv("NETMASK", inet_ntoa(ipconf.mask), 1);
632                                         //setenv("FILENAME", args->file_remote ? args->file_remote : "", 1);
633                                         status = system(args->tftpcmd);
634                                 }
635
636                                 if (!status && args->file_local) {
637                                         if (!autoip) {
638                                                 status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask);
639                                                 if (status < 0) {
640                                                         goto out;
641                                                 } else if (!status) {
642                                                         printf("IP address of %s has changed. Please assign a "
643                                                                         "static ip to the interface.\n", args->intf);
644                                                         tx.msg.code = NMRP_C_CLOSE_REQ;
645                                                         break;
646                                                 }
647                                         }
648
649                                         if (verbosity) {
650                                                 printf("Using remote filename '%s'.\n",
651                                                                 args->file_remote);
652                                         }
653
654                                         if (!strcmp(args->file_local, "-")) {
655                                                 printf("Uploading from stdin ... ");
656                                         } else {
657                                                 printf("Uploading %s ... ", leafname(args->file_local));
658                                         }
659                                         fflush(stdout);
660                                         if (!(status = tftp_put(args))) {
661                                                 printf("OK\n");
662                                         }
663
664                                 }
665
666                                 if (!status) {
667                                         printf("Waiting for remote to respond.\n");
668                                         upload_ok = 1;
669                                         ethsock_set_timeout(sock, args->ul_timeout);
670                                         tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
671                                         expect = NMRP_C_NONE;
672                                 } else if (status == -2) {
673                                         expect = NMRP_C_TFTP_UL_REQ;
674                                 } else {
675                                         goto out;
676                                 }
677
678                                 break;
679                         case NMRP_C_KEEP_ALIVE_REQ:
680                                 tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
681                                 ethsock_set_timeout(sock, args->ul_timeout);
682                                 printf("Received keep-alive request.\n");
683                                 break;
684                         case NMRP_C_CLOSE_REQ:
685                                 tx.msg.code = NMRP_C_CLOSE_ACK;
686                                 break;
687                         case NMRP_C_CLOSE_ACK:
688                                 status = 0;
689                                 goto out;
690                         default:
691                                 fprintf(stderr, "Unknown message code 0x%02x!\n",
692                                                 rx.msg.code);
693                                 msg_dump(&rx.msg);
694                 }
695
696                 if (tx.msg.code != NMRP_C_NONE) {
697                         msg_hton(&tx.msg);
698
699                         if (pkt_send(sock, &tx) < 0) {
700                                 xperror("sendto");
701                                 goto out;
702                         }
703
704                         if (tx.msg.code == NMRP_C_CLOSE_REQ) {
705                                 goto out;
706                         }
707                 }
708
709                 if (rx.msg.code == NMRP_C_CLOSE_REQ) {
710                         printf("Remote finished. Closing connection.\n");
711                         break;
712                 }
713
714                 status = pkt_recv(sock, &rx);
715                 if (status) {
716                         if (status == 2) {
717                                 fprintf(stderr, "Timeout while waiting for %s.\n",
718                                                 msg_code_str(expect));
719                         }
720                         goto out;
721                 }
722
723                 ethsock_set_timeout(sock, args->rx_timeout);
724
725         }
726
727         if (!g_interrupted) {
728                 status = 0;
729                 if (ulreqs) {
730                         printf("Reboot your device now.\n");
731                 } else {
732                         printf("No upload request received.\n");
733                 }
734         }
735
736 out:
737         signal(SIGINT, sigh_orig);
738         ethsock_arp_del(sock, &arp_undo);
739         ethsock_ip_del(sock, &ip_undo);
740         ethsock_close(sock);
741         return status;
742 }