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__))
53 NMRP_C_KEEP_ALIVE_REQ = 6,
54 NMRP_C_KEEP_ALIVE_ACK = 7,
55 NMRP_C_TFTP_UL_REQ = 16
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
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 */
96 static const char *msg_code_str(uint16_t code)
98 #define CASE_CODE(x) case NMRP_C_ ## x: return #x
102 CASE_CODE(ADVERTISE);
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);
111 snprintf(buf, sizeof(buf), "%04x", code);
117 static uint16_t to_region_code(const char *region)
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);
133 static void msg_dump(struct nmrp_msg *msg, int dump_opts)
135 struct nmrp_opt *opt;
136 int remain_len, len, i;
138 fprintf(stderr, "res=0x%04x, code=0x%02x, id=0x%02x, len=%u",
139 msg->reserved, msg->code, msg->id, msg->len);
141 remain_len = msg->len - NMRP_HDR_LEN;
142 fprintf(stderr, "%s\n", remain_len ? "" : " (no opts)");
147 while (remain_len > 0) {
149 fprintf(stderr, " opt type=%u, len=%u", opt->type, len);
151 for (i = 0; i != len - NMRP_OPT_HDR_LEN; ++i) {
153 fprintf(stderr, "\n ");
156 fprintf(stderr, "%02x ", ((char*)&opt->val)[i] & 0xff);
158 fprintf(stderr, "\n");
161 opt = NMRP_OPT_NEXT(opt);
166 static void msg_hton(struct nmrp_msg *msg)
169 struct nmrp_opt *opt = msg->opts, *next;
171 msg->reserved = htons(msg->reserved);
172 msg->len = htons(msg->len);
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);
182 static void msg_hdr_ntoh(struct nmrp_msg *msg)
184 msg->reserved = ntohs(msg->reserved);
185 msg->len = ntohs(msg->len);
188 static int msg_ntoh(struct nmrp_msg *msg)
190 struct nmrp_opt *opt = msg->opts;
193 remaining = msg->len - NMRP_HDR_LEN;
195 // FIXME maximum of two options supported, maximum option
197 if (remaining < NMRP_MAX_OPT_NUM * NMRP_MAX_OPT_SIZE) {
198 while (remaining > 0) {
199 if (remaining < NMRP_OPT_HDR_LEN) {
203 opt->type = ntohs(opt->type);
204 opt->len = ntohs(opt->len);
206 if (opt->len > NMRP_MAX_OPT_SIZE) {
210 remaining -= opt->len;
211 opt = NMRP_OPT_NEXT(opt);
219 fprintf(stderr, "Unexpected message format.\n");
224 static void *msg_opt_data(struct nmrp_msg *msg, uint16_t type, uint16_t *len)
226 static char buf[128];
227 struct nmrp_opt *opt = msg->opts;
228 int remaining = msg->len - NMRP_HDR_LEN;
230 memset(buf, 0, sizeof(buf));
232 while (remaining > 0) {
233 if (opt->type == type) {
234 if (opt->len == NMRP_OPT_HDR_LEN) {
237 *len = opt->len - NMRP_OPT_HDR_LEN;
238 memcpy(buf, &opt->val, MIN(*len, sizeof(buf)-1));
242 remaining -= opt->len;
243 opt = NMRP_OPT_NEXT(opt);
249 static void msg_opt_add(struct nmrp_msg *msg, uint16_t type, void *data,
253 struct nmrp_opt *opt = msg->opts;
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");
260 for (; i <= msg->num_opts; ++i) {
261 opt = NMRP_OPT_NEXT(opt);
264 opt->len = NMRP_OPT_HDR_LEN + len;
268 memcpy(&opt->val, data, len);
271 msg->len += opt->len;
275 static inline void msg_init(struct nmrp_msg *msg, uint16_t code)
277 memset(msg, 0, sizeof(*msg));
278 msg->len = NMRP_HDR_LEN;
282 static int pkt_send(struct ethsock *sock, struct nmrp_pkt *pkt)
284 size_t len = ntohs(pkt->msg.len) + sizeof(pkt->eh);
285 return ethsock_send(sock, pkt, len);
288 static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
292 memset(pkt, 0, sizeof(*pkt));
293 bytes = ethsock_recv(sock, pkt, sizeof(*pkt));
298 } else if (bytes < NMRP_MIN_PKT_LEN) {
299 fprintf(stderr, "Short packet (%d bytes)\n", (int)bytes);
303 msg_hdr_ntoh(&pkt->msg);
304 len = pkt->msg.len + sizeof(pkt->eh);
307 fprintf(stderr, "Short packet (expected %d, got %d).\n",
308 (int)len, (int)bytes);
312 return msg_ntoh(&pkt->msg);
315 static int mac_parse(const char *str, uint8_t *hwaddr)
320 sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x%n",
321 data, data + 1, data + 2, data + 3, data + 4, data + 5, &i);
323 if (i == strlen(str)) {
324 for (i = 0; i != 6; ++i) {
329 hwaddr[i] = data[i] & 0xff;
339 struct is_valid_ip_arg
341 struct in_addr *ipaddr;
342 struct in_addr *ipmask;
346 static int is_valid_ip_cb(struct ethsock_ip_callback_args *args)
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;
359 static int is_valid_ip(struct ethsock *sock, struct in_addr *ipaddr,
360 struct in_addr *ipmask)
363 struct is_valid_ip_arg arg = {
369 status = ethsock_for_each_ip(sock, is_valid_ip_cb, &arg);
370 return status < 0 ? status : arg.result;
373 static struct ethsock *gsock = NULL;
375 static struct in_addr arpip = { 0 };
376 static uint8_t arpmac[6] = { 0 };
378 static void sigh(int sig)
383 ethsock_arp_del(gsock, arpmac, &arpip);
385 ethsock_close(gsock);
392 static const char *spinner = "\\|/-";
394 int nmrp_do(struct nmrpd_args *args)
396 struct nmrp_pkt tx, rx;
397 uint8_t *src, dest[6];
398 uint16_t len, region;
401 int i, status, ulreqs, expect, upload_ok, autoip;
402 struct ethsock *sock;
403 void (*sigh_orig)(int);
409 if (args->op != NMRP_UPLOAD_FW) {
410 fprintf(stderr, "Operation not implemented.\n");
414 if (!mac_parse(args->mac, dest)) {
415 fprintf(stderr, "Invalid MAC address '%s'.\n", args->mac);
419 if ((ipconf.addr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
421 //fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
427 if ((ipconf.mask.s_addr = inet_addr(args->ipmask)) == INADDR_NONE) {
428 fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
432 if (args->file_local && strcmp(args->file_local, "-") && access(args->file_local, R_OK) == -1) {
433 fprintf(stderr, "Error accessing file '%s'.\n", args->file_local);
437 if (args->file_remote) {
438 if (!tftp_is_valid_filename(args->file_remote)) {
439 fprintf(stderr, "Invalid remote filename '%s'.\n",
446 region = htons(to_region_code(args->region));
448 fprintf(stderr, "Invalid region code '%s'.\n", args->region);
458 if (arp_find_free_ip(args->intf, &ipconf.addr.s_addr) != 0) {
463 sock = ethsock_create(args->intf, ETH_P_NMRP);
470 sigh_orig = signal(SIGINT, sigh);
472 status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask);
475 fprintf(stderr, "Address %s/%s cannot be used on interface %s.\n",
476 args->ipaddr, args->ipmask, args->intf);
481 if (ethsock_set_timeout(sock, args->rx_timeout)) {
485 src = ethsock_get_hwaddr(sock);
490 memcpy(tx.eh.ether_shost, src, 6);
491 memcpy(tx.eh.ether_dhost, dest, 6);
492 tx.eh.ether_type = htons(ETH_P_NMRP);
494 msg_init(&tx.msg, NMRP_C_ADVERTISE);
495 msg_opt_add(&tx.msg, NMRP_O_MAGIC_NO, "NTGR", 4);
503 printf("\rAdvertising NMRP server on %s ... %c",
504 args->intf, spinner[i]);
508 if (pkt_send(sock, &tx) < 0) {
513 status = pkt_recv(sock, &rx);
514 if (status == 0 && memcmp(rx.eh.ether_dhost, src, 6) == 0) {
516 } else if (status == 1) {
519 if ((time(NULL) - beg) >= 60) {
520 printf("\nNo response after 60 seconds. Bailing out.\n");
528 expect = NMRP_C_CONF_REQ;
532 if (expect != NMRP_C_NONE && rx.msg.code != expect) {
533 fprintf(stderr, "Received %s while waiting for %s!\n",
534 msg_code_str(rx.msg.code), msg_code_str(expect));
537 msg_init(&tx.msg, NMRP_C_NONE);
541 switch (rx.msg.code) {
542 case NMRP_C_ADVERTISE:
543 printf("Received NMRP advertisement from %s.\n",
544 mac_to_str(rx.eh.ether_shost));
547 case NMRP_C_CONF_REQ:
548 tx.msg.code = NMRP_C_CONF_ACK;
550 msg_opt_add(&tx.msg, NMRP_O_DEV_IP, &ipconf, 8);
551 msg_opt_add(&tx.msg, NMRP_O_FW_UP, NULL, 0);
553 #ifdef NMRPFLASH_SET_REGION
555 msg_opt_add(&tx.msg, NMRP_O_DEV_REGION, ®ion, 2);
559 expect = NMRP_C_TFTP_UL_REQ;
561 printf("Received configuration request from %s.\n",
562 mac_to_str(rx.eh.ether_shost));
564 memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6);
566 printf("Sending configuration: ip %s, mask %s.\n",
567 args->ipaddr, args->ipmask);
569 memcpy(arpmac, rx.eh.ether_shost, 6);
570 memcpy(&arpip, &ipconf.addr, sizeof(ipconf.addr));
572 if (ethsock_arp_add(sock, arpmac, &arpip) != 0) {
579 case NMRP_C_TFTP_UL_REQ:
582 printf("Bailing out after %d upload requests.\n",
584 tx.msg.code = NMRP_C_CLOSE_REQ;
589 printf("Ignoring extra upload request.\n");
591 ethsock_set_timeout(sock, args->ul_timeout);
592 tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
597 filename = msg_opt_data(&rx.msg, NMRP_O_FILE_NAME, &len);
599 if (!args->file_remote) {
600 args->file_remote = filename;
602 printf("Received upload request: filename '%.*s'.\n",
604 } else if (!args->file_remote) {
605 args->file_remote = args->file_local;
606 printf("Received upload request with empty filename.\n");
612 printf("Executing '%s' ... ", args->tftpcmd);
614 status = system(args->tftpcmd);
618 if (!status && args->file_local) {
619 status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask);
622 } else if (!status) {
623 printf("IP address of %s has changed. Please assign a "
624 "static ip to the interface.\n", args->intf);
625 tx.msg.code = NMRP_C_CLOSE_REQ;
630 printf("Using remote filename '%s'.\n",
634 if (!strcmp(args->file_local, "-")) {
635 printf("Uploading from stdin ... ");
637 printf("Uploading %s ... ", leafname(args->file_local));
640 status = tftp_put(args);
644 printf("OK\nWaiting for remote to respond.\n");
646 ethsock_set_timeout(sock, args->ul_timeout);
647 tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
648 expect = NMRP_C_NONE;
649 } else if (status == -2) {
650 expect = NMRP_C_TFTP_UL_REQ;
656 case NMRP_C_KEEP_ALIVE_REQ:
657 tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
658 ethsock_set_timeout(sock, args->ul_timeout);
659 printf("Received keep-alive request.\n");
661 case NMRP_C_CLOSE_REQ:
662 tx.msg.code = NMRP_C_CLOSE_ACK;
664 case NMRP_C_CLOSE_ACK:
668 fprintf(stderr, "Unknown message code 0x%02x!\n",
670 msg_dump(&rx.msg, 0);
673 if (tx.msg.code != NMRP_C_NONE) {
676 if (pkt_send(sock, &tx) < 0) {
681 if (tx.msg.code == NMRP_C_CLOSE_REQ) {
686 if (rx.msg.code == NMRP_C_CLOSE_REQ) {
687 printf("Remote finished. Closing connection.\n");
691 status = pkt_recv(sock, &rx);
694 fprintf(stderr, "Timeout while waiting for %s.\n",
695 msg_code_str(expect));
700 ethsock_set_timeout(sock, args->rx_timeout);
707 printf("Reboot your device now.\n");
709 printf("No upload request received.\n");
713 signal(SIGINT, sigh_orig);
715 ethsock_arp_del(sock, arpmac, &arpip);