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