Use time_monotonic for timeouts
[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, int dump_opts)
138 {
139         struct nmrp_opt *opt;
140         int remain_len, len, i;
141
142         fprintf(stderr, "res=0x%04x, code=0x%02x, id=0x%02x, len=%u",
143                         msg->reserved, msg->code, msg->id, msg->len);
144
145         remain_len = msg->len - NMRP_HDR_LEN;
146         fprintf(stderr, "%s\n", remain_len ? "" : " (no opts)");
147 }
148
149 static void msg_hton(struct nmrp_msg *msg)
150 {
151         uint32_t i = 0;
152         struct nmrp_opt *opt = msg->opts, *next;
153
154         msg->reserved = htons(msg->reserved);
155         msg->len = htons(msg->len);
156
157         for (; i != msg->num_opts; ++i) {
158                 next = NMRP_OPT_NEXT(opt);
159                 opt->len = htons(opt->len);
160                 opt->type = htons(opt->type);
161                 opt = next;
162         }
163 }
164
165 static void msg_hdr_ntoh(struct nmrp_msg *msg)
166 {
167         msg->reserved = ntohs(msg->reserved);
168         msg->len = ntohs(msg->len);
169 }
170
171 static int msg_ntoh(struct nmrp_msg *msg)
172 {
173         struct nmrp_opt *opt = msg->opts;
174         int remaining;
175
176         remaining = msg->len - NMRP_HDR_LEN;
177
178         // FIXME maximum of two options supported, maximum option
179         // size is 12
180         if (remaining < NMRP_MAX_OPT_NUM * NMRP_MAX_OPT_SIZE) {
181                 while (remaining > 0) {
182                         if (remaining < NMRP_OPT_HDR_LEN) {
183                                 break;
184                         }
185
186                         opt->type = ntohs(opt->type);
187                         opt->len = ntohs(opt->len);
188
189                         if (!opt->len || opt->len > NMRP_MAX_OPT_SIZE) {
190                                 break;
191                         }
192
193                         remaining -= opt->len;
194                         opt = NMRP_OPT_NEXT(opt);
195                 }
196
197                 if (!remaining) {
198                         return 0;
199                 }
200         }
201
202         fprintf(stderr, "Unexpected message format.\n");
203         msg_dump(msg, 0);
204         return 1;
205 }
206
207 static void *msg_opt_data(struct nmrp_msg *msg, uint16_t type, uint16_t *len)
208 {
209         static char buf[128];
210         struct nmrp_opt *opt = msg->opts;
211         int remaining = msg->len - NMRP_HDR_LEN;
212
213         memset(buf, 0, sizeof(buf));
214
215         while (remaining > 0) {
216                 if (opt->type == type) {
217                         if (opt->len == NMRP_OPT_HDR_LEN) {
218                                 return NULL;
219                         }
220                         *len = opt->len - NMRP_OPT_HDR_LEN;
221                         memcpy(buf, &opt->val, MIN(*len, sizeof(buf)-1));
222                         return buf;
223                 }
224
225                 if (!opt->len) {
226                         break;
227                 }
228
229                 remaining -= opt->len;
230                 opt = NMRP_OPT_NEXT(opt);
231         }
232
233         return NULL;
234 }
235
236 static void msg_opt_add(struct nmrp_msg *msg, uint16_t type, void *data,
237                 uint16_t len)
238 {
239         uint32_t i = 0;
240         struct nmrp_opt *opt = msg->opts;
241
242         if (len + NMRP_OPT_HDR_LEN > NMRP_MAX_OPT_SIZE
243                         || msg->num_opts == NMRP_MAX_OPT_NUM) {
244                 fprintf(stderr, "Invalid option - this is a bug.\n");
245         }
246
247         for (; i <= msg->num_opts; ++i) {
248                 opt = NMRP_OPT_NEXT(opt);
249         }
250
251         opt->len = NMRP_OPT_HDR_LEN + len;
252         opt->type = type;
253
254         if (len) {
255                 memcpy(&opt->val, data, len);
256         }
257
258         msg->len += opt->len;
259         ++msg->num_opts;
260 }
261
262 static inline void msg_init(struct nmrp_msg *msg, uint16_t code)
263 {
264         memset(msg, 0, sizeof(*msg));
265         msg->len = NMRP_HDR_LEN;
266         msg->code = code;
267 }
268
269 #ifdef NMRPFLASH_FUZZ
270 #define ethsock_create(a, b) ethsock_create_fake(a, b)
271 #define ethsock_get_hwaddr(a) ethsock_get_hwaddr_fake(a)
272 #define ethsock_recv(a, b, c) ethsock_recv_fake(a, b, c)
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 struct ethsock* ethsock_create_fake(const char *intf, uint16_t protocol)
281 {
282         return (struct ethsock*)1;
283 }
284
285 static uint8_t *ethsock_get_hwaddr_fake(struct ethsock* sock)
286 {
287         static uint8_t hwaddr[6];
288         memset(hwaddr, 0xfa, 6);
289         return hwaddr;
290 }
291
292 static ssize_t ethsock_recv_fake(struct ethsock *sock, void *buf, size_t len)
293 {
294         return read(STDIN_FILENO, buf, len);
295 }
296 #endif
297
298 static int pkt_send(struct ethsock *sock, struct nmrp_pkt *pkt)
299 {
300         size_t len = ntohs(pkt->msg.len) + sizeof(pkt->eh);
301         return ethsock_send(sock, pkt, len);
302 }
303
304 static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
305 {
306         ssize_t bytes, len;
307
308         memset(pkt, 0, sizeof(*pkt));
309         bytes = ethsock_recv(sock, pkt, sizeof(*pkt));
310         if (bytes < 0) {
311                 return 1;
312         } else if (!bytes) {
313                 return 2;
314         } else if (bytes < NMRP_MIN_PKT_LEN) {
315                 fprintf(stderr, "Short packet (%d bytes)\n", (int)bytes);
316                 return 1;
317         }
318
319         msg_hdr_ntoh(&pkt->msg);
320         len = pkt->msg.len + sizeof(pkt->eh);
321
322         if (bytes < len) {
323                 fprintf(stderr, "Short packet (expected %d, got %d).\n",
324                                 (int)len, (int)bytes);
325                 return 1;
326         }
327
328         return msg_ntoh(&pkt->msg);
329 }
330
331 static int mac_parse(const char *str, uint8_t *hwaddr)
332 {
333         int i;
334         unsigned data[6];
335
336         sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x%n",
337                         data, data + 1, data + 2, data + 3, data + 4, data + 5, &i);
338
339         if (i == strlen(str)) {
340                 for (i = 0; i != 6; ++i) {
341                         if (data[i] > 255) {
342                                 break;
343                         }
344
345                         hwaddr[i] = data[i] & 0xff;
346                 }
347
348                 if (i == 6) {
349                         return 1;
350                 }
351         }
352         return 0;
353 }
354
355 struct is_valid_ip_arg
356 {
357         struct in_addr *ipaddr;
358         struct in_addr *ipmask;
359         int result;
360 };
361
362 static int is_valid_ip_cb(struct ethsock_ip_callback_args *args)
363 {
364 #define SUBNET(x) ((x)->ipaddr->s_addr & (x)->ipmask->s_addr)
365         struct is_valid_ip_arg *arg = args->arg;
366         if (SUBNET(args) == SUBNET(arg)) {
367                 arg->result = args->ipaddr->s_addr != arg->ipaddr->s_addr;
368                 return 0;
369         }
370
371         return 1;
372 #undef SUBNET
373 }
374
375 static int is_valid_ip(struct ethsock *sock, struct in_addr *ipaddr,
376                 struct in_addr *ipmask)
377 {
378         int status;
379         struct is_valid_ip_arg arg = {
380                 .ipaddr = ipaddr,
381                 .ipmask = ipmask,
382                 .result = 0
383         };
384
385         status = ethsock_for_each_ip(sock, is_valid_ip_cb, &arg);
386         return status < 0 ? status : arg.result;
387 }
388
389 static struct ethsock *gsock = NULL;
390 static struct ethsock_ip_undo *gundo = NULL;
391 static int garp = 0;
392 static struct in_addr arpip = { 0 };
393 static uint8_t arpmac[6] = { 0 };
394
395 static void sigh(int sig)
396 {
397         printf("\n");
398         if (gsock) {
399                 if (garp) {
400                         ethsock_arp_del(gsock, arpmac, &arpip);
401                 }
402                 ethsock_ip_del(gsock, &gundo);
403                 ethsock_close(gsock);
404                 gsock = NULL;
405         }
406
407         exit(1);
408 }
409
410 static const char *spinner = "\\|/-";
411
412 int nmrp_do(struct nmrpd_args *args)
413 {
414         struct nmrp_pkt tx, rx;
415         uint8_t *src, dest[6];
416         uint16_t len, region;
417         char *filename;
418         time_t beg;
419         int i, status, ulreqs, expect, upload_ok, autoip;
420         struct ethsock *sock;
421         uint32_t intf_addr;
422         void (*sigh_orig)(int);
423         struct {
424                 struct in_addr addr;
425                 struct in_addr mask;
426         } PACKED ipconf;
427
428         if (args->op != NMRP_UPLOAD_FW) {
429                 fprintf(stderr, "Operation not implemented.\n");
430                 return 1;
431         }
432
433         if (!mac_parse(args->mac, dest)) {
434                 fprintf(stderr, "Invalid MAC address '%s'.\n", args->mac);
435                 return 1;
436         }
437
438         if ((ipconf.mask.s_addr = inet_addr(args->ipmask)) == INADDR_NONE) {
439                 fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
440                 return 1;
441         }
442
443         if (!args->ipaddr) {
444                 autoip = true;
445                 /* The MAC of the device that was used to test this utility starts
446                  * with a4:2b:8c, hence 164 (0xa4) and 183 (0x2b + 0x8c)
447                  */
448                 args->ipaddr = "10.164.183.252";
449
450                 if (!args->ipaddr_intf) {
451                         args->ipaddr_intf = "10.164.183.253";
452                 }
453         } else if (args->ipaddr_intf) {
454                 autoip = true;
455         } else {
456                 autoip = false;
457         }
458
459         if ((ipconf.addr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
460                 fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
461                 return 1;
462         }
463
464         if (args->ipaddr_intf && (intf_addr = inet_addr(args->ipaddr_intf)) == INADDR_NONE) {
465                 fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr_intf);
466                 return 1;
467         }
468
469         if (args->file_local && strcmp(args->file_local, "-") && access(args->file_local, R_OK) == -1) {
470                 fprintf(stderr, "Error accessing file '%s'.\n", args->file_local);
471                 return 1;
472         }
473
474         if (args->file_remote) {
475                 if (!tftp_is_valid_filename(args->file_remote)) {
476                         fprintf(stderr, "Invalid remote filename '%s'.\n",
477                                         args->file_remote);
478                         return 1;
479                 }
480         }
481
482         if (args->region) {
483                 region = htons(to_region_code(args->region));
484                 if (!region) {
485                         fprintf(stderr, "Invalid region code '%s'.\n", args->region);
486                         return 1;
487                 }
488         } else {
489                 region = 0;
490         }
491
492         status = 1;
493
494         sock = ethsock_create(args->intf, ETH_P_NMRP);
495         if (!sock) {
496                 return 1;
497         }
498
499         gsock = sock;
500         garp = 0;
501         sigh_orig = signal(SIGINT, sigh);
502
503         if (!autoip) {
504                 status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask);
505                 if (status <= 0) {
506                         if (!status) {
507                                 fprintf(stderr, "Address %s/%s cannot be used on interface %s.\n",
508                                                 args->ipaddr, args->ipmask, args->intf);
509                         }
510                         goto out;
511                 }
512         } else {
513                 if (verbosity) {
514                         printf("Adding %s to interface %s.\n", args->ipaddr_intf, args->intf);
515                 }
516
517                 if (ethsock_ip_add(sock, intf_addr, ipconf.mask.s_addr, &gundo) != 0) {
518                         goto out;
519                 }
520         }
521
522         if (ethsock_set_timeout(sock, args->rx_timeout)) {
523                 goto out;
524         }
525
526         src = ethsock_get_hwaddr(sock);
527         if (!src) {
528                 goto out;
529         }
530
531         memcpy(tx.eh.ether_shost, src, 6);
532         memcpy(tx.eh.ether_dhost, dest, 6);
533         tx.eh.ether_type = htons(ETH_P_NMRP);
534
535         msg_init(&tx.msg, NMRP_C_ADVERTISE);
536         msg_opt_add(&tx.msg, NMRP_O_MAGIC_NO, "NTGR", 4);
537         msg_hton(&tx.msg);
538
539         i = 0;
540         upload_ok = 0;
541         beg = time_monotonic();
542
543         while (1) {
544                 printf("\rAdvertising NMRP server on %s ... %c",
545                                 args->intf, spinner[i]);
546                 fflush(stdout);
547                 i = (i + 1) & 3;
548
549                 if (pkt_send(sock, &tx) < 0) {
550                         perror("sendto");
551                         goto out;
552                 }
553
554                 status = pkt_recv(sock, &rx);
555                 if (status == 0 && memcmp(rx.eh.ether_dhost, src, 6) == 0) {
556                         break;
557                 } else if (status == 1) {
558                         goto out;
559                 } else {
560                         if ((time_monotonic() - beg) >= 60) {
561                                 printf("\nNo response after 60 seconds. Bailing out.\n");
562                                 goto out;
563                         }
564 #ifdef NMRPFLASH_FUZZ
565                         goto out;
566 #endif
567                 }
568         }
569
570         printf("\n");
571
572         expect = NMRP_C_CONF_REQ;
573         ulreqs = 0;
574
575         do {
576                 if (expect != NMRP_C_NONE && rx.msg.code != expect) {
577                         fprintf(stderr, "Received %s while waiting for %s!\n",
578                                         msg_code_str(rx.msg.code), msg_code_str(expect));
579                 }
580
581                 msg_init(&tx.msg, NMRP_C_NONE);
582
583                 status = 1;
584
585                 switch (rx.msg.code) {
586                         case NMRP_C_ADVERTISE:
587                                 printf("Received NMRP advertisement from %s.\n",
588                                                 mac_to_str(rx.eh.ether_shost));
589                                 status = 1;
590                                 goto out;
591                         case NMRP_C_CONF_REQ:
592                                 tx.msg.code = NMRP_C_CONF_ACK;
593
594                                 msg_opt_add(&tx.msg, NMRP_O_DEV_IP, &ipconf, 8);
595                                 msg_opt_add(&tx.msg, NMRP_O_FW_UP, NULL, 0);
596
597 #ifdef NMRPFLASH_SET_REGION
598                                 if (region) {
599                                         msg_opt_add(&tx.msg, NMRP_O_DEV_REGION, &region, 2);
600                                 }
601 #endif
602
603                                 expect = NMRP_C_TFTP_UL_REQ;
604
605                                 printf("Received configuration request from %s.\n",
606                                                 mac_to_str(rx.eh.ether_shost));
607
608                                 memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6);
609
610                                 printf("Sending configuration: ip %s, mask %s.\n",
611                                                 args->ipaddr, args->ipmask);
612
613                                 memcpy(arpmac, rx.eh.ether_shost, 6);
614                                 memcpy(&arpip, &ipconf.addr, sizeof(ipconf.addr));
615
616                                 if (ethsock_arp_add(sock, arpmac, &arpip) != 0) {
617                                         goto out;
618                                 }
619
620                                 garp = 1;
621
622                                 break;
623                         case NMRP_C_TFTP_UL_REQ:
624                                 if (!upload_ok) {
625                                         if (++ulreqs > 5) {
626                                                 printf("Bailing out after %d upload requests.\n",
627                                                                 ulreqs);
628                                                 tx.msg.code = NMRP_C_CLOSE_REQ;
629                                                 break;
630                                         }
631                                 } else {
632                                         if (verbosity) {
633                                                 printf("Ignoring extra upload request.\n");
634                                         }
635                                         ethsock_set_timeout(sock, args->ul_timeout);
636                                         tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
637                                         break;
638                                 }
639
640                                 len = 0;
641                                 filename = msg_opt_data(&rx.msg, NMRP_O_FILE_NAME, &len);
642                                 if (filename) {
643                                         if (!args->file_remote) {
644                                                 args->file_remote = filename;
645                                         }
646                                         printf("Received upload request: filename '%.*s'.\n",
647                                                         len, filename);
648                                 } else if (!args->file_remote) {
649                                         args->file_remote = args->file_local;
650                                         printf("Received upload request with empty filename.\n");
651                                 }
652
653                                 status = 0;
654
655                                 if (args->tftpcmd) {
656                                         printf("Executing '%s' ... \n", args->tftpcmd);
657                                         setenv("IP", inet_ntoa(ipconf.addr), 1);
658                                         setenv("MAC", mac_to_str(rx.eh.ether_shost), 1);
659                                         setenv("NETMASK", inet_ntoa(ipconf.mask), 1);
660                                         status = system(args->tftpcmd);
661                                 }
662
663                                 if (!status && args->file_local) {
664                                         if (!autoip) {
665                                                 status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask);
666                                                 if (status < 0) {
667                                                         goto out;
668                                                 } else if (!status) {
669                                                         printf("IP address of %s has changed. Please assign a "
670                                                                         "static ip to the interface.\n", args->intf);
671                                                         tx.msg.code = NMRP_C_CLOSE_REQ;
672                                                         break;
673                                                 }
674                                         }
675
676                                         if (verbosity) {
677                                                 printf("Using remote filename '%s'.\n",
678                                                                 args->file_remote);
679                                         }
680
681                                         if (!strcmp(args->file_local, "-")) {
682                                                 printf("Uploading from stdin ... ");
683                                         } else {
684                                                 printf("Uploading %s ... ", leafname(args->file_local));
685                                         }
686                                         fflush(stdout);
687                                         status = tftp_put(args);
688                                 }
689
690                                 if (!status) {
691                                         printf("OK\nWaiting for remote to respond.\n");
692                                         upload_ok = 1;
693                                         ethsock_set_timeout(sock, args->ul_timeout);
694                                         tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
695                                         expect = NMRP_C_NONE;
696                                 } else if (status == -2) {
697                                         expect = NMRP_C_TFTP_UL_REQ;
698                                 } else {
699                                         goto out;
700                                 }
701
702                                 break;
703                         case NMRP_C_KEEP_ALIVE_REQ:
704                                 tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
705                                 ethsock_set_timeout(sock, args->ul_timeout);
706                                 printf("Received keep-alive request.\n");
707                                 break;
708                         case NMRP_C_CLOSE_REQ:
709                                 tx.msg.code = NMRP_C_CLOSE_ACK;
710                                 break;
711                         case NMRP_C_CLOSE_ACK:
712                                 status = 0;
713                                 goto out;
714                         default:
715                                 fprintf(stderr, "Unknown message code 0x%02x!\n",
716                                                 rx.msg.code);
717                                 msg_dump(&rx.msg, 0);
718                 }
719
720                 if (tx.msg.code != NMRP_C_NONE) {
721                         msg_hton(&tx.msg);
722
723                         if (pkt_send(sock, &tx) < 0) {
724                                 perror("sendto");
725                                 goto out;
726                         }
727
728                         if (tx.msg.code == NMRP_C_CLOSE_REQ) {
729                                 goto out;
730                         }
731                 }
732
733                 if (rx.msg.code == NMRP_C_CLOSE_REQ) {
734                         printf("Remote finished. Closing connection.\n");
735                         break;
736                 }
737
738                 status = pkt_recv(sock, &rx);
739                 if (status) {
740                         if (status == 2) {
741                                 fprintf(stderr, "Timeout while waiting for %s.\n",
742                                                 msg_code_str(expect));
743                         }
744                         goto out;
745                 }
746
747                 ethsock_set_timeout(sock, args->rx_timeout);
748
749         } while (1);
750
751         status = 0;
752
753         if (ulreqs) {
754                 printf("Reboot your device now.\n");
755         } else {
756                 printf("No upload request received.\n");
757         }
758
759 out:
760         signal(SIGINT, sigh_orig);
761         gsock = NULL;
762         ethsock_arp_del(sock, arpmac, &arpip);
763         ethsock_ip_del(sock, &gundo);
764         ethsock_close(sock);
765         return status;
766 }