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