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