2 * nmrpflash - Netgear Unbrick Utility
3 * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
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.
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.
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/>.
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)
33 #define NMRP_MAX_OPT_SIZE 12
34 #define NMRP_MAX_OPT_NUM 3
36 #define NMRP_OPT_NEXT(x) ((struct nmrp_opt*)(((char*)x) + x->len))
38 #define ETH_P_NMRP 0x0912
40 #define MAX_LOOP_RECV 1024
43 #define PACKED __attribute__((__packed__))
46 #ifdef NMRPFLASH_WINDOWS
47 #define setenv(name, value, overwrite) SetEnvironmentVariable(name, value)
57 NMRP_C_KEEP_ALIVE_REQ = 6,
58 NMRP_C_KEEP_ALIVE_ACK = 7,
59 NMRP_C_TFTP_UL_REQ = 16
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
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 */
100 static const char *msg_code_str(uint16_t code)
102 #define CASE_CODE(x) case NMRP_C_ ## x: return #x
106 CASE_CODE(ADVERTISE);
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);
115 snprintf(buf, sizeof(buf), "%04x", code);
121 static uint16_t to_region_code(const char *region)
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);
137 static void msg_dump(struct nmrp_msg *msg)
141 fprintf(stderr, "res=0x%04x, code=0x%02x, id=0x%02x, len=%u",
142 msg->reserved, msg->code, msg->id, msg->len);
144 remain_len = msg->len - NMRP_HDR_LEN;
145 fprintf(stderr, "%s\n", remain_len ? "" : " (no opts)");
148 static void msg_hton(struct nmrp_msg *msg)
151 struct nmrp_opt *opt = msg->opts, *next;
153 msg->reserved = htons(msg->reserved);
154 msg->len = htons(msg->len);
156 for (; i != msg->num_opts; ++i) {
157 next = NMRP_OPT_NEXT(opt);
158 opt->len = htons(opt->len);
159 opt->type = htons(opt->type);
164 static void msg_hdr_ntoh(struct nmrp_msg *msg)
166 msg->reserved = ntohs(msg->reserved);
167 msg->len = ntohs(msg->len);
170 static int msg_ntoh(struct nmrp_msg *msg)
172 struct nmrp_opt *opt = msg->opts;
175 remaining = msg->len - NMRP_HDR_LEN;
177 // FIXME maximum of two options supported, maximum option
179 if (remaining < NMRP_MAX_OPT_NUM * NMRP_MAX_OPT_SIZE) {
180 while (remaining > 0) {
181 if (remaining < NMRP_OPT_HDR_LEN) {
185 opt->type = ntohs(opt->type);
186 opt->len = ntohs(opt->len);
188 if (!opt->len || opt->len > NMRP_MAX_OPT_SIZE) {
192 remaining -= opt->len;
193 opt = NMRP_OPT_NEXT(opt);
201 fprintf(stderr, "Unexpected message format.\n");
206 static void *msg_opt_data(struct nmrp_msg *msg, uint16_t type, uint16_t *len)
208 static char buf[128];
209 struct nmrp_opt *opt = msg->opts;
210 int remaining = msg->len - NMRP_HDR_LEN;
212 memset(buf, 0, sizeof(buf));
214 while (remaining > 0) {
215 if (opt->type == type) {
216 if (opt->len == NMRP_OPT_HDR_LEN) {
219 *len = opt->len - NMRP_OPT_HDR_LEN;
220 memcpy(buf, &opt->val, MIN(*len, sizeof(buf)-1));
228 remaining -= opt->len;
229 opt = NMRP_OPT_NEXT(opt);
235 static void msg_opt_add(struct nmrp_msg *msg, uint16_t type, void *data,
239 struct nmrp_opt *opt = msg->opts;
241 if (len + NMRP_OPT_HDR_LEN > NMRP_MAX_OPT_SIZE
242 || msg->num_opts == NMRP_MAX_OPT_NUM) {
243 fprintf(stderr, "Invalid option - this is a bug.\n");
246 for (; i <= msg->num_opts; ++i) {
247 opt = NMRP_OPT_NEXT(opt);
250 opt->len = NMRP_OPT_HDR_LEN + len;
254 memcpy(&opt->val, data, len);
257 msg->len += opt->len;
261 static inline void msg_init(struct nmrp_msg *msg, uint16_t code)
263 memset(msg, 0, sizeof(*msg));
264 msg->len = NMRP_HDR_LEN;
268 #ifdef NMRPFLASH_FUZZ
269 #define ethsock_create(a, b) ethsock_create_fake(a, b)
270 #define ethsock_get_hwaddr(a) ethsock_get_hwaddr_fake(a)
271 #define ethsock_recv(a, b, c) ethsock_recv_fake(a, b, c)
272 #define ethsock_send(a, b, c) (0)
273 #define ethsock_set_timeout(a, b) (0)
274 #define ethsock_ip_add(a, b, c, d) (0)
275 #define ethsock_ip_del(a, b) (0)
276 #define ethsock_close(a) (0)
277 #define tftp_put(a) (0)
279 static struct ethsock* ethsock_create_fake(const char *intf, uint16_t protocol)
281 return (struct ethsock*)1;
284 static uint8_t *ethsock_get_hwaddr_fake(struct ethsock* sock)
286 static uint8_t hwaddr[6];
287 memset(hwaddr, 0xfa, 6);
291 static ssize_t ethsock_recv_fake(struct ethsock *sock, void *buf, size_t len)
293 return read(STDIN_FILENO, buf, len);
297 static int pkt_send(struct ethsock *sock, struct nmrp_pkt *pkt)
299 size_t len = ntohs(pkt->msg.len) + sizeof(pkt->eh);
300 return ethsock_send(sock, pkt, len);
303 static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
307 memset(pkt, 0, sizeof(*pkt));
308 bytes = ethsock_recv(sock, pkt, sizeof(*pkt));
313 } else if (bytes < NMRP_MIN_PKT_LEN) {
314 fprintf(stderr, "Short packet (%d bytes)\n", (int)bytes);
318 msg_hdr_ntoh(&pkt->msg);
319 len = pkt->msg.len + sizeof(pkt->eh);
322 fprintf(stderr, "Short packet (expected %d, got %d).\n",
323 (int)len, (int)bytes);
327 return msg_ntoh(&pkt->msg);
330 static int mac_parse(const char *str, uint8_t *hwaddr)
335 sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x%n",
336 data, data + 1, data + 2, data + 3, data + 4, data + 5, &i);
338 if (i == strlen(str)) {
339 for (i = 0; i != 6; ++i) {
344 hwaddr[i] = data[i] & 0xff;
354 struct is_valid_ip_arg
356 struct in_addr *ipaddr;
357 struct in_addr *ipmask;
361 static int is_valid_ip_cb(struct ethsock_ip_callback_args *args)
363 #define SUBNET(x) ((x)->ipaddr->s_addr & (x)->ipmask->s_addr)
364 struct is_valid_ip_arg *arg = args->arg;
365 if (SUBNET(args) == SUBNET(arg)) {
366 arg->result = args->ipaddr->s_addr != arg->ipaddr->s_addr;
374 static int is_valid_ip(struct ethsock *sock, struct in_addr *ipaddr,
375 struct in_addr *ipmask)
378 struct is_valid_ip_arg arg = {
384 status = ethsock_for_each_ip(sock, is_valid_ip_cb, &arg);
385 return status < 0 ? status : arg.result;
388 static struct ethsock *gsock = NULL;
389 static struct ethsock_ip_undo *g_ip_undo = NULL;
390 static struct ethsock_arp_undo *g_arp_undo = NULL;
392 static void sigh(int sig)
396 ethsock_arp_del(gsock, &g_arp_undo);
397 ethsock_ip_del(gsock, &g_ip_undo);
398 ethsock_close(gsock);
405 static const char *spinner = "\\|/-";
407 int nmrp_do(struct nmrpd_args *args)
409 struct nmrp_pkt tx, rx;
410 uint8_t *src, dest[6];
411 uint16_t len, region;
414 int i, status, ulreqs, expect, upload_ok, autoip;
415 struct ethsock *sock;
417 void (*sigh_orig)(int);
423 if (args->op != NMRP_UPLOAD_FW) {
424 fprintf(stderr, "Operation not implemented.\n");
428 if (!mac_parse(args->mac, dest)) {
429 fprintf(stderr, "Invalid MAC address '%s'.\n", args->mac);
433 ipconf.mask.s_addr = inet_addr(args->ipmask);
434 if (ipconf.mask.s_addr == INADDR_NONE
435 || netmask(bitcount(ipconf.mask.s_addr)) != ipconf.mask.s_addr) {
436 fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
442 /* The MAC of the device that was used to test this utility starts
443 * with a4:2b:8c, hence 164 (0xa4) and 183 (0x2b + 0x8c)
445 args->ipaddr = "10.164.183.252";
447 if (!args->ipaddr_intf) {
448 args->ipaddr_intf = "10.164.183.253";
450 } else if (args->ipaddr_intf) {
456 if ((ipconf.addr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
457 fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
461 if (args->ipaddr_intf && (intf_addr = inet_addr(args->ipaddr_intf)) == INADDR_NONE) {
462 fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr_intf);
466 if (args->file_local && strcmp(args->file_local, "-") && access(args->file_local, R_OK) == -1) {
467 fprintf(stderr, "Error accessing file '%s'.\n", args->file_local);
471 if (args->file_remote) {
472 if (!tftp_is_valid_filename(args->file_remote)) {
473 fprintf(stderr, "Invalid remote filename '%s'.\n",
480 region = htons(to_region_code(args->region));
482 fprintf(stderr, "Invalid region code '%s'.\n", args->region);
491 sock = ethsock_create(args->intf, ETH_P_NMRP);
497 sigh_orig = signal(SIGINT, sigh);
500 status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask);
503 fprintf(stderr, "Address %s/%s cannot be used on interface %s.\n",
504 args->ipaddr, args->ipmask, args->intf);
510 printf("Adding %s to interface %s.\n", args->ipaddr_intf, args->intf);
513 if (ethsock_ip_add(sock, intf_addr, ipconf.mask.s_addr, &g_ip_undo) != 0) {
518 if (ethsock_set_timeout(sock, args->rx_timeout)) {
522 src = ethsock_get_hwaddr(sock);
527 memcpy(tx.eh.ether_shost, src, 6);
528 memcpy(tx.eh.ether_dhost, dest, 6);
529 tx.eh.ether_type = htons(ETH_P_NMRP);
531 msg_init(&tx.msg, NMRP_C_ADVERTISE);
532 msg_opt_add(&tx.msg, NMRP_O_MAGIC_NO, "NTGR", 4);
537 beg = time_monotonic();
540 printf("\rAdvertising NMRP server on %s ... %c",
541 args->intf, spinner[i]);
545 if (pkt_send(sock, &tx) < 0) {
550 status = pkt_recv(sock, &rx);
551 if (status == 0 && memcmp(rx.eh.ether_dhost, src, 6) == 0) {
553 } else if (status == 1) {
556 if ((time_monotonic() - beg) >= 60) {
557 printf("\nNo response after 60 seconds. Bailing out.\n");
560 #ifdef NMRPFLASH_FUZZ
568 expect = NMRP_C_CONF_REQ;
572 if (expect != NMRP_C_NONE && rx.msg.code != expect) {
573 fprintf(stderr, "Received %s while waiting for %s!\n",
574 msg_code_str(rx.msg.code), msg_code_str(expect));
577 msg_init(&tx.msg, NMRP_C_NONE);
581 switch (rx.msg.code) {
582 case NMRP_C_ADVERTISE:
583 printf("Received NMRP advertisement from %s.\n",
584 mac_to_str(rx.eh.ether_shost));
587 case NMRP_C_CONF_REQ:
588 tx.msg.code = NMRP_C_CONF_ACK;
590 msg_opt_add(&tx.msg, NMRP_O_DEV_IP, &ipconf, 8);
591 msg_opt_add(&tx.msg, NMRP_O_FW_UP, NULL, 0);
593 #ifdef NMRPFLASH_SET_REGION
595 msg_opt_add(&tx.msg, NMRP_O_DEV_REGION, ®ion, 2);
599 expect = NMRP_C_TFTP_UL_REQ;
601 printf("Received configuration request from %s.\n",
602 mac_to_str(rx.eh.ether_shost));
604 memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6);
606 printf("Sending configuration: %s, netmask %s.\n",
607 args->ipaddr, args->ipmask);
609 if (ethsock_arp_add(sock, rx.eh.ether_shost, ipconf.addr.s_addr, &g_arp_undo) != 0) {
614 case NMRP_C_TFTP_UL_REQ:
617 printf("Bailing out after %d upload requests.\n",
619 tx.msg.code = NMRP_C_CLOSE_REQ;
624 printf("Ignoring extra upload request.\n");
626 ethsock_set_timeout(sock, args->ul_timeout);
627 tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
632 filename = msg_opt_data(&rx.msg, NMRP_O_FILE_NAME, &len);
634 if (!args->file_remote) {
635 args->file_remote = filename;
637 printf("Received upload request: filename '%.*s'.\n",
639 } else if (!args->file_remote) {
640 args->file_remote = args->file_local;
641 printf("Received upload request with empty filename.\n");
647 printf("Executing '%s' ... \n", args->tftpcmd);
648 setenv("IP", inet_ntoa(ipconf.addr), 1);
649 setenv("PORT", lltostr(args->port, 10), 1);
650 setenv("MAC", mac_to_str(rx.eh.ether_shost), 1);
651 setenv("NETMASK", inet_ntoa(ipconf.mask), 1);
652 //setenv("FILENAME", args->file_remote ? args->file_remote : "", 1);
653 status = system(args->tftpcmd);
656 if (!status && args->file_local) {
658 status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask);
661 } else if (!status) {
662 printf("IP address of %s has changed. Please assign a "
663 "static ip to the interface.\n", args->intf);
664 tx.msg.code = NMRP_C_CLOSE_REQ;
670 printf("Using remote filename '%s'.\n",
674 if (!strcmp(args->file_local, "-")) {
675 printf("Uploading from stdin ... ");
677 printf("Uploading %s ... ", leafname(args->file_local));
680 status = tftp_put(args);
684 printf("OK\nWaiting for remote to respond.\n");
686 ethsock_set_timeout(sock, args->ul_timeout);
687 tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
688 expect = NMRP_C_NONE;
689 } else if (status == -2) {
690 expect = NMRP_C_TFTP_UL_REQ;
696 case NMRP_C_KEEP_ALIVE_REQ:
697 tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
698 ethsock_set_timeout(sock, args->ul_timeout);
699 printf("Received keep-alive request.\n");
701 case NMRP_C_CLOSE_REQ:
702 tx.msg.code = NMRP_C_CLOSE_ACK;
704 case NMRP_C_CLOSE_ACK:
708 fprintf(stderr, "Unknown message code 0x%02x!\n",
713 if (tx.msg.code != NMRP_C_NONE) {
716 if (pkt_send(sock, &tx) < 0) {
721 if (tx.msg.code == NMRP_C_CLOSE_REQ) {
726 if (rx.msg.code == NMRP_C_CLOSE_REQ) {
727 printf("Remote finished. Closing connection.\n");
731 status = pkt_recv(sock, &rx);
734 fprintf(stderr, "Timeout while waiting for %s.\n",
735 msg_code_str(expect));
740 ethsock_set_timeout(sock, args->rx_timeout);
747 printf("Reboot your device now.\n");
749 printf("No upload request received.\n");
753 signal(SIGINT, sigh_orig);
755 ethsock_arp_del(sock, &g_arp_undo);
756 ethsock_ip_del(sock, &g_ip_undo);