forked from jclehner/nmrpflash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmrp.c
833 lines (699 loc) · 18.8 KB
/
nmrp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
/**
* nmrpflash - Netgear Unbrick Utility
* Copyright (C) 2016 Joseph Lehner <[email protected]>
*
* nmrpflash is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nmrpflash is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with nmrpflash. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include "nmrpd.h"
#define NMRP_HDR_LEN 6
#define NMRP_OPT_HDR_LEN 4
#define NMRP_MIN_PKT_LEN (sizeof(struct eth_hdr) + NMRP_HDR_LEN)
#define ETH_P_NMRP 0x0912
#ifndef PACKED
#define PACKED __attribute__((__packed__))
#endif
#ifdef NMRPFLASH_WINDOWS
#define setenv(name, value, overwrite) SetEnvironmentVariable(name, value)
#endif
enum nmrp_code {
NMRP_C_NONE = 0,
NMRP_C_ADVERTISE = 1,
NMRP_C_CONF_REQ = 2,
NMRP_C_CONF_ACK = 3,
NMRP_C_CLOSE_REQ = 4,
NMRP_C_CLOSE_ACK = 5,
NMRP_C_KEEP_ALIVE_REQ = 6,
NMRP_C_KEEP_ALIVE_ACK = 7,
NMRP_C_TFTP_UL_REQ = 16
};
enum nmrp_opt_type {
NMRP_O_MAGIC_NO = 0x0001,
NMRP_O_DEV_IP = 0x0002,
NMRP_O_DEV_REGION = 0x0004,
NMRP_O_FW_UP = 0x0101,
NMRP_O_ST_UP = 0x0102,
NMRP_O_FILE_NAME = 0x0181
};
struct nmrp_opt {
uint16_t type;
uint16_t len;
char val[1];
} PACKED;
struct nmrp_msg {
uint16_t reserved;
uint8_t code;
uint8_t id;
uint16_t len;
char opts[44];
} PACKED;
struct nmrp_pkt {
struct eth_hdr eh;
struct nmrp_msg msg;
} PACKED;
static const char *msg_code_str(uint16_t code)
{
#define MSG_CODE(x) case NMRP_C_ ## x: return #x
static char buf[16];
switch (code) {
MSG_CODE(ADVERTISE);
MSG_CODE(CONF_REQ);
MSG_CODE(CONF_ACK);
MSG_CODE(CLOSE_REQ);
MSG_CODE(CLOSE_ACK);
MSG_CODE(KEEP_ALIVE_REQ);
MSG_CODE(KEEP_ALIVE_ACK);
MSG_CODE(TFTP_UL_REQ);
default:
snprintf(buf, sizeof(buf), "%04x", ntohs(code));
return buf;
}
#undef MSG_CODE
}
static uint16_t to_region_code(const char *region)
{
#define REGION_CODE(r, c) if (!strcasecmp(region, r)) return htons(c)
REGION_CODE("NA", 0x0001);
REGION_CODE("WW", 0x0002);
REGION_CODE("GR", 0x0003);
REGION_CODE("PR", 0x0004);
REGION_CODE("RU", 0x0005);
REGION_CODE("BZ", 0x0006);
REGION_CODE("IN", 0x0007);
REGION_CODE("KO", 0x0008);
REGION_CODE("JP", 0x0009);
#undef REGION_CODE
return 0;
}
static void msg_dump(struct nmrp_msg *msg)
{
int rem;
fprintf(stderr, "res=0x%04x, code=0x%02x, id=0x%02x, len=%u",
ntohs(msg->reserved), msg->code, msg->id, ntohs(msg->len));
rem = ntohs(msg->len) - NMRP_HDR_LEN;
fprintf(stderr, "%s\n", rem ? "" : " (no opts)");
}
static void *msg_opt(struct nmrp_msg *msg, uint16_t type, uint16_t* len)
{
struct nmrp_opt* opt = (struct nmrp_opt*)msg->opts;
size_t rem = ntohs(msg->len) - NMRP_HDR_LEN;
uint16_t olen;
do {
olen = ntohs(opt->len);
if (olen < NMRP_OPT_HDR_LEN || olen > rem) {
break;
}
if (ntohs(opt->type) == type) {
if (len) {
*len = olen;
}
return opt->val;
}
opt = (struct nmrp_opt*)(((char *)opt) + olen);
rem -= olen;
} while (rem);
return NULL;
}
static char *msg_filename(struct nmrp_msg *msg)
{
static char buf[256];
uint16_t len;
char *p = msg_opt(msg, NMRP_O_FILE_NAME, &len);
if (p) {
len = MIN(sizeof(buf) - 1, len);
memcpy(buf, p, len);
buf[len] = '\0';
return buf;
}
return NULL;
}
static inline void msg_init(struct nmrp_msg *msg, uint16_t code)
{
memset(msg, 0, sizeof(*msg));
msg->len = htons(NMRP_HDR_LEN);
msg->code = code;
}
static char *msg_mkopt(struct nmrp_msg *msg, char *p, uint16_t type, const void *val, size_t len)
{
struct nmrp_opt* opt = (struct nmrp_opt*)p;
len &= 0xffff;
msg->len = ntohs(msg->len);
if ((msg->len + len > sizeof(*msg))) {
fprintf(stderr, "Error: invalid option - this is a bug\n");
exit(1);
}
opt->type = htons(type);
opt->len = NMRP_OPT_HDR_LEN + len;
if (val) {
memcpy(opt->val, val, len);
}
msg->len += opt->len;
p += opt->len;
msg->len = htons(msg->len);
opt->len = htons(opt->len);
return p;
}
static void msg_mkadvertise(struct nmrp_msg *msg, const char *magic)
{
msg_init(msg, NMRP_C_ADVERTISE);
msg_mkopt(msg, msg->opts, NMRP_O_MAGIC_NO, magic, strlen(magic));
}
static void msg_mkconfack(struct nmrp_msg *msg, uint32_t ipaddr, uint32_t ipmask, uint16_t region)
{
char *p;
uint32_t ip[2] = { ipaddr, ipmask };
msg_init(msg, NMRP_C_CONF_ACK);
p = msg_mkopt(msg, msg->opts, NMRP_O_DEV_IP, &ip, 8);
p = msg_mkopt(msg, p, NMRP_O_FW_UP, NULL, 0);
#ifdef NMRPFLASH_SET_REGION
if (region) {
p = msg_mkopt(msg, p, NMRP_O_DEV_REGION, ®ion, 2);
}
#endif
}
#ifdef NMRPFLASH_FUZZ
#define NMRP_ADVERTISE_TIMEOUT 0
#define ethsock_create(a, b) ((struct ethsock*)1)
#define ethsock_get_hwaddr(a) ethsock_get_hwaddr_fake(a)
#define ethsock_recv(sock, buf, len) read(STDIN_FILENO, buf, len)
#define ethsock_send(a, b, c) (0)
#define ethsock_set_timeout(a, b) (0)
#define ethsock_arp_add(a, b, c, d) (0)
#define ethsock_arp_del(a, b) (0)
#define ethsock_ip_add(a, b, c, d) (0)
#define ethsock_ip_del(a, b) (0)
#define ethsock_close(a) (0)
#define ethsock_for_each_ip(a, b, c) (1)
#define tftp_put(a) (0)
static uint8_t *ethsock_get_hwaddr_fake(struct ethsock* sock)
{
static uint8_t hwaddr[6] = { 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa };
return hwaddr;
}
#else
#define NMRP_ADVERTISE_TIMEOUT 60
#endif
static int pkt_send(struct ethsock *sock, struct nmrp_pkt *pkt)
{
return ethsock_send(sock, pkt, sizeof(pkt->eh) + ntohs(pkt->msg.len));
}
static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
{
ssize_t bytes, mlen;
memset(pkt, 0, sizeof(*pkt));
bytes = ethsock_recv(sock, pkt, sizeof(*pkt));
if (bytes < 0) {
return 1;
} else if (!bytes) {
return 2;
}
mlen = ntohs(pkt->msg.len);
if (bytes < (mlen + sizeof(pkt->eh))
|| bytes < NMRP_MIN_PKT_LEN
|| mlen < NMRP_HDR_LEN) {
fprintf(stderr, "Short packet (%d raw, %d message)\n",
(int)bytes, (int)mlen);
return 1;
} else if (mlen > sizeof(pkt->msg)) {
printf("Truncating %d byte message.\n", (int)mlen);
pkt->msg.len = htons(sizeof(pkt->msg));
}
return 0;
}
static int mac_parse(const char *str, uint8_t *hwaddr)
{
int i;
unsigned data[6];
sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x%n",
data, data + 1, data + 2, data + 3, data + 4, data + 5, &i);
if (i == strlen(str)) {
for (i = 0; i != 6; ++i) {
if (data[i] > 255) {
break;
}
hwaddr[i] = data[i] & 0xff;
}
if (i == 6) {
return 1;
}
}
return 0;
}
static bool mac_is_broadcast(uint8_t* hwaddr)
{
for (int i = 0; i != 6; ++i) {
if (hwaddr[i] != 0xff) {
return false;
}
}
return true;
}
struct is_valid_ip_arg
{
struct in_addr *ipaddr;
struct in_addr *ipmask;
int result;
};
static int is_valid_ip_cb(struct ethsock_ip_callback_args *args)
{
#define SUBNET(x) ((x)->ipaddr->s_addr & (x)->ipmask->s_addr)
struct is_valid_ip_arg *arg = args->arg;
if (SUBNET(args) == SUBNET(arg)) {
arg->result = args->ipaddr->s_addr != arg->ipaddr->s_addr;
return 0;
}
return 1;
#undef SUBNET
}
static int is_valid_ip(struct ethsock *sock, struct in_addr *ipaddr,
struct in_addr *ipmask)
{
int status;
struct is_valid_ip_arg arg = {
.ipaddr = ipaddr,
.ipmask = ipmask,
.result = 0
};
status = ethsock_for_each_ip(sock, is_valid_ip_cb, &arg);
return status < 0 ? status : arg.result;
}
static void sigh(int sig)
{
g_interrupted = 1;
}
bool nmrp_discard(struct ethsock *sock)
{
// between nmrpflash sending the TFTP WRQ packet, and the router
// responding with ACK(0)/OACK, some devices send extraneous
// CONF_REQ and/or TFTP_UL_REQ packets.
//
// the TFTP code thus calls this function in each loop iteration,
// to discard any late NMRP packets.
//
// without this it might seem as if these packets arrived after
// the TFTP upload completed successfuly, confusing the NMRP code.
unsigned timeout = ethsock_get_timeout(sock);
// don't set this to 0, as this would cause pkt_recv to block!
ethsock_set_timeout(sock, 1);
struct nmrp_pkt rx;
int ret = pkt_recv(sock, &rx);
if (ret == 0) {
if (rx.msg.code != NMRP_C_CONF_REQ && rx.msg.code != NMRP_C_TFTP_UL_REQ) {
printf("Discarding unexpected %s packet\n", msg_code_str(rx.msg.code));
} else if (verbosity > 1) {
printf("Discarding %s packet\n", msg_code_str(rx.msg.code));
}
}
ethsock_set_timeout(sock, timeout);
return ret == 0;
}
static const char *spinner = "\\|/-";
int nmrp_do(struct nmrpd_args *args)
{
struct nmrp_pkt tx, rx;
uint8_t *src, dest[6];
uint16_t region;
char *filename;
time_t beg;
int i, timeout, status, ulreqs, expect, upload_ok, autoip, ka_reqs;
bool was_plugged_in;
ssize_t bytes;
struct ethsock *sock;
struct ethsock_ip_undo *ip_undo = NULL;
struct ethsock_arp_undo *arp_undo = NULL;
uint32_t intf_addr = 0;
void (*sigh_orig)(int);
struct in_addr ipaddr;
struct in_addr ipmask;
if (args->op != NMRP_UPLOAD_FW) {
fprintf(stderr, "Operation not implemented.\n");
return 1;
}
if (!mac_parse(args->mac, dest)) {
fprintf(stderr, "Invalid MAC address '%s'.\n", args->mac);
return 1;
}
ipmask.s_addr = inet_addr(args->ipmask);
if (ipmask.s_addr == INADDR_NONE
|| netmask(bitcount(ipmask.s_addr)) != ipmask.s_addr) {
fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
return 1;
}
if (!args->ipaddr) {
autoip = true;
args->ipaddr = NMRP_DEFAULT_IP_REMOTE;
if (!args->ipaddr_intf) {
args->ipaddr_intf = NMRP_DEFAULT_IP_LOCAL;
}
} else if (args->ipaddr_intf) {
autoip = true;
} else {
autoip = false;
}
if ((ipaddr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
return 1;
}
if (args->ipaddr_intf && (intf_addr = inet_addr(args->ipaddr_intf)) == INADDR_NONE) {
fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr_intf);
return 1;
}
if (args->file_local && strcmp(args->file_local, "-") && access(args->file_local, R_OK) == -1) {
fprintf(stderr, "Error accessing file '%s'.\n", args->file_local);
return 1;
}
if (args->file_remote) {
if (!tftp_is_valid_filename(args->file_remote)) {
fprintf(stderr, "Invalid remote filename '%s'.\n",
args->file_remote);
return 1;
}
}
if (args->region) {
region = to_region_code(args->region);
if (!region) {
fprintf(stderr, "Invalid region code '%s'.\n", args->region);
return 1;
}
} else {
region = 0;
}
status = 1;
sock = ethsock_create(args->intf, ETH_P_NMRP);
if (!sock) {
return 1;
}
args->sock = sock;
sigh_orig = signal(SIGINT, sigh);
was_plugged_in = !ethsock_is_unplugged(sock);
if (!was_plugged_in) {
if (ethsock_is_wifi(sock)) {
fprintf(stderr, "Error: Wi-Fi not connected.\n");
goto out;
}
printf("Waiting for Ethernet connection (Ctrl-C to skip).\n");
bool unplugged = true;
time_t beg = time_monotonic();
while (!g_interrupted && (time_monotonic() - beg) < 20) {
if (!ethsock_is_unplugged(sock)) {
unplugged = false;
break;
}
}
was_plugged_in = !unplugged;
if (unplugged) {
if (!g_interrupted) {
fprintf(stderr, "Error: Ethernet cable is unplugged.\n");
goto out;
} else {
printf("\rSkipped.\n");
g_interrupted = false;
}
}
}
if (ethsock_is_wifi(sock)) {
printf("Warning: using a Wi-Fi interface. Make sure you know what you're doing!\n");
}
if (!autoip) {
status = is_valid_ip(sock, &ipaddr, &ipmask);
if (status <= 0) {
if (!status) {
fprintf(stderr, "Address %s/%s cannot be used on interface %s.\n",
args->ipaddr, args->ipmask, args->intf);
}
goto out;
}
} else {
if (verbosity) {
printf("Adding %s to interface %s.\n", args->ipaddr_intf, args->intf);
}
if (ethsock_ip_add(sock, intf_addr, ipmask.s_addr, &ip_undo) != 0) {
goto out;
}
}
if (ethsock_set_timeout(sock, 20)) {
goto out;
}
src = ethsock_get_hwaddr(sock);
if (!src) {
goto out;
}
memcpy(tx.eh.ether_shost, src, 6);
memcpy(tx.eh.ether_dhost, dest, 6);
tx.eh.ether_type = htons(ETH_P_NMRP);
msg_mkadvertise(&tx.msg, "NTGR");
i = 0;
upload_ok = 0;
timeout = args->blind ? 10 : NMRP_ADVERTISE_TIMEOUT;
beg = time_monotonic();
printf("Advertising NMRP server on %s ... ", args->intf);
while (!g_interrupted) {
i = (i + 1) & 3;
printf("%c ", spinner[i]);
fflush(stdout);
printf("\b\b");
was_plugged_in |= !ethsock_is_unplugged(sock);
if (pkt_send(sock, &tx) < 0) {
goto out;
}
status = pkt_recv(sock, &rx);
if (status == 0) {
if (memcmp(rx.eh.ether_dhost, src, 6) == 0) {
// don't continue in blind mode if we've received a response
args->blind = false;
break;
} else if (verbosity) {
printf("\nIgnoring bogus response: %s -> %s.\n",
mac_to_str(rx.eh.ether_shost),
mac_to_str(rx.eh.ether_dhost));
}
} else if (status == 1) {
goto out;
} else {
/* because we don't want nmrpflash's exit status to be zero */
status = 1;
if ((time_monotonic() - beg) >= timeout) {
printf("\nNo response after %d seconds. ", timeout);
if (!args->blind || !was_plugged_in) {
if (!was_plugged_in) {
printf("Ethernet cable unplugged. ");
}
printf("Bailing out.\n");
goto out;
} else {
// we're blind, so fake a response from the MAC specified by -m
memcpy(rx.eh.ether_shost, dest, 6);
msg_init(&rx.msg, NMRP_C_CONF_REQ);
printf("Continuing blindly.");
break;
}
}
}
}
printf("\n");
memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6);
// a manually specified MAC address (using `-m`) takes precedence over
// the NMRP response packets' MAC.
uint8_t* arp_mac = !mac_is_broadcast(dest) ? dest : rx.eh.ether_shost;
if (ethsock_arp_add(sock, arp_mac, ipaddr.s_addr, &arp_undo) != 0) {
goto out;
}
if (ethsock_set_timeout(sock, args->rx_timeout)) {
goto out;
}
expect = NMRP_C_CONF_REQ;
ulreqs = 0;
ka_reqs = 0;
while (!g_interrupted) {
if (expect != NMRP_C_NONE && rx.msg.code != expect) {
fprintf(stderr, "Received %s while waiting for %s!\n",
msg_code_str(rx.msg.code), msg_code_str(expect));
}
msg_init(&tx.msg, NMRP_C_NONE);
status = 1;
switch (rx.msg.code) {
case NMRP_C_ADVERTISE:
printf("Received NMRP advertisement from %s.\n",
mac_to_str(rx.eh.ether_shost));
status = 1;
goto out;
case NMRP_C_CONF_REQ:
msg_mkconfack(&tx.msg, ipaddr.s_addr, ipmask.s_addr, region);
expect = NMRP_C_TFTP_UL_REQ;
if (!args->blind) {
printf("Received configuration request from %s.\n",
mac_to_str(rx.eh.ether_shost));
}
printf("Sending configuration: %s/%d.\n",
args->ipaddr, bitcount(ipmask.s_addr));
break;
case NMRP_C_TFTP_UL_REQ:
if (!upload_ok) {
if (++ulreqs > 5) {
printf("Bailing out after %d upload requests.\n",
ulreqs);
tx.msg.code = NMRP_C_CLOSE_REQ;
break;
}
} else {
if (verbosity) {
printf("Ignoring extra upload request.\n");
}
ethsock_set_timeout(sock, args->ul_timeout);
break;
}
filename = msg_filename(&rx.msg);
if (filename) {
if (!args->file_remote) {
args->file_remote = filename;
}
printf("Received upload request: filename '%s'.\n", filename);
} else if (!args->file_remote) {
args->file_remote = leafname(args->file_local);
if (!args->blind) {
printf("Received upload request.\n");
}
}
if (args->tftpcmd) {
printf("Executing '%s' ... \n", args->tftpcmd);
setenv("IP", inet_ntoa(ipaddr), 1);
setenv("PORT", lltostr(args->port, 10), 1);
setenv("MAC", mac_to_str(arp_mac), 1);
setenv("NETMASK", inet_ntoa(ipmask), 1);
//setenv("FILENAME", args->file_remote ? args->file_remote : "", 1);
setenv("INTERFACE", args->intf, 1);
status = system(args->tftpcmd);
if (status != 0) {
fprintf(stderr, "Command failed: status %d.\n", status);
goto out;
}
}
if (args->file_local) {
if (!autoip) {
status = is_valid_ip(sock, &ipaddr, &ipmask);
if (status < 0) {
goto out;
} else if (!status) {
printf("IP address of %s has changed. Please assign a "
"static ip to the interface.\n", args->intf);
tx.msg.code = NMRP_C_CLOSE_REQ;
break;
}
}
if (verbosity) {
printf("Using remote filename '%s'.\n",
args->file_remote);
}
if (!strcmp(args->file_local, "-")) {
printf("Uploading from stdin ... ");
} else {
printf("Uploading %s ... ", leafname(args->file_local));
}
fflush(stdout);
bytes = tftp_put(args);
if (bytes > 0) {
printf("OK (%zd b)\n", bytes);
upload_ok = 1;
if (args->blind) {
printf("Not waiting for further responses in blind mode.\n");
goto out;
}
} else if (bytes == -2) {
// return -2 means the TFTP code signalled that the remote
// file has been rejected. this feature is only implemented
// by some bootloaders.
expect = NMRP_C_TFTP_UL_REQ;
} else {
goto out;
}
} else {
// if no `-f <filename>` flag has been supplied, assume
// that the command specified by `-c <command>` handles
// the file upload.
upload_ok = 1;
}
if (upload_ok) {
printf("Waiting for remote to respond.\n");
ethsock_set_timeout(sock, args->ul_timeout);
tx.msg.code = NMRP_C_NONE;
expect = NMRP_C_NONE;
}
break;
case NMRP_C_KEEP_ALIVE_REQ:
tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
ethsock_set_timeout(sock, args->ul_timeout);
printf("\rReceived keep-alive request (%d). ", ++ka_reqs);
break;
case NMRP_C_CLOSE_REQ:
tx.msg.code = NMRP_C_CLOSE_ACK;
break;
case NMRP_C_CLOSE_ACK:
status = 0;
goto out;
default:
fprintf(stderr, "Unknown message code 0x%02x!\n",
rx.msg.code);
msg_dump(&rx.msg);
}
if (tx.msg.code != NMRP_C_NONE) {
if (pkt_send(sock, &tx) != 0 || tx.msg.code == NMRP_C_CLOSE_REQ) {
goto out;
}
}
if (rx.msg.code == NMRP_C_CLOSE_REQ) {
if (ka_reqs) {
printf("\n");
}
printf("Remote finished. Closing connection.\n");
break;
}
status = pkt_recv(sock, &rx);
if (status) {
if (status == 2) {
if (!args->blind) {
fprintf(stderr, "Timeout while waiting for %s.\n",
msg_code_str(expect));
goto out;
}
// fake response
msg_init(&rx.msg, expect);
memcpy(rx.eh.ether_shost, tx.eh.ether_dhost, 6);
} else {
goto out;
}
}
ethsock_set_timeout(sock, args->rx_timeout);
}
if (!g_interrupted) {
status = 0;
if (ulreqs) {
printf("Reboot your device now.\n");
} else {
printf("No upload request received.\n");
}
}
out:
signal(SIGINT, sigh_orig);
ethsock_arp_del(sock, &arp_undo);
ethsock_ip_del(sock, &ip_undo);
ethsock_close(sock);
return status;
}