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 ETH_P_NMRP 0x0912
38 #define MAX_LOOP_RECV 1024
41 #define PACKED __attribute__((__packed__))
51 NMRP_C_KEEP_ALIVE_REQ = 6,
52 NMRP_C_KEEP_ALIVE_ACK = 7,
53 NMRP_C_TFTP_UL_REQ = 16
57 NMRP_O_MAGIC_NO = 0x0001,
58 NMRP_O_DEV_IP = 0x0002,
59 NMRP_O_DEV_REGION = 0x0004,
60 NMRP_O_FW_UP = 0x0101,
61 NMRP_O_ST_UP = 0x0102,
62 NMRP_O_FILE_NAME = 0x0181
83 struct nmrp_opt opts[NMRP_MAX_OPT_NUM];
88 uint8_t ether_dhost[6];
89 uint8_t ether_shost[6];
98 static const char *msg_code_str(uint16_t code)
100 #define CASE_CODE(x) case NMRP_C_ ## x: return #x
104 CASE_CODE(ADVERTISE);
107 CASE_CODE(CLOSE_REQ);
108 CASE_CODE(CLOSE_ACK);
109 CASE_CODE(KEEP_ALIVE_REQ);
110 CASE_CODE(KEEP_ALIVE_ACK);
111 CASE_CODE(TFTP_UL_REQ);
113 snprintf(buf, sizeof(buf), "%04x", code);
119 static void msg_update_len(struct nmrp_msg *msg)
122 msg->len = NMRP_HDR_LEN;
123 for (; i != msg->num_opts; ++i) {
124 msg->len += msg->opts[i].len;
128 static void msg_dump(struct nmrp_msg *msg, int dump_opts)
130 struct nmrp_opt *opt;
131 int remain_len, len, i;
133 fprintf(stderr, "res=0x%04x, code=0x%02x, id=0x%02x, len=%u",
134 msg->reserved, msg->code, msg->id, msg->len);
136 remain_len = msg->len - NMRP_HDR_LEN;
137 fprintf(stderr, "%s\n", remain_len ? "" : " (no opts)");
142 while (remain_len > 0) {
144 fprintf(stderr, " opt type=%u, len=%u", opt->type, len);
145 for (i = 0; i != len - NMRP_OPT_HDR_LEN; ++i) {
147 fprintf(stderr, "\n ");
150 fprintf(stderr, "%02x ", ((char*)&opt->val)[i] & 0xff);
152 fprintf(stderr, "\n");
154 opt = (struct nmrp_opt*)(((char*)opt) + len);
159 static void msg_hton(struct nmrp_msg *msg)
163 msg->reserved = htons(msg->reserved);
164 msg->len = htons(msg->len);
166 for (; i != msg->num_opts; ++i) {
167 msg->opts[i].len = htons(msg->opts[i].len);
168 msg->opts[i].type = htons(msg->opts[i].type);
172 static void msg_hdr_ntoh(struct nmrp_msg *msg)
174 msg->reserved = ntohs(msg->reserved);
175 msg->len = ntohs(msg->len);
178 static int msg_ntoh(struct nmrp_msg *msg)
180 struct nmrp_opt *opt = msg->opts;
183 remaining = msg->len - NMRP_HDR_LEN;
185 // FIXME maximum of two options supported, maximum option
187 if (remaining < NMRP_MAX_OPT_NUM * NMRP_MAX_OPT_SIZE) {
188 while (remaining > 0) {
189 if (remaining < NMRP_OPT_HDR_LEN) {
193 opt->type = ntohs(opt->type);
194 opt->len = ntohs(opt->len);
196 if (opt->len > NMRP_MAX_OPT_SIZE) {
200 remaining -= opt->len;
209 fprintf(stderr, "Unexpected message format.\n");
214 static void *msg_opt_data(struct nmrp_msg *msg, uint16_t type, uint16_t *len)
216 static char buf[128];
217 struct nmrp_opt *opt = msg->opts;
218 int remaining = msg->len - NMRP_HDR_LEN;
220 memset(buf, 0, sizeof(buf));
222 while (remaining > 0) {
223 if (opt->type == type) {
224 if (opt->len == NMRP_OPT_HDR_LEN) {
227 *len = opt->len - NMRP_OPT_HDR_LEN;
228 memcpy(buf, &opt->val, MIN(*len, sizeof(buf)-1));
232 remaining -= opt->len;
233 opt = (struct nmrp_opt*)((char*)opt) + opt->len;
239 static void msg_opt_add(struct nmrp_msg *msg, uint16_t type, void *data,
243 struct nmrp_opt *opt = msg->opts;
245 if (len + NMRP_OPT_HDR_LEN > NMRP_MAX_OPT_SIZE
246 || msg->num_opts == NMRP_MAX_OPT_NUM) {
247 fprintf(stderr, "Invalid option - this is a bug.\n");
250 for (; i != msg->num_opts; ++i) {
251 opt = (struct nmrp_opt*)(((char*)opt) + msg->len);
254 opt->len = NMRP_OPT_HDR_LEN + len;
258 memcpy(&opt->val, data, len);
266 static int pkt_send(struct ethsock *sock, struct nmrp_pkt *pkt)
268 size_t len = ntohs(pkt->msg.len) + sizeof(pkt->eh);
269 return ethsock_send(sock, pkt, len);
272 static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
276 memset(pkt, 0, sizeof(*pkt));
277 bytes = ethsock_recv(sock, pkt, sizeof(*pkt));
282 } else if (bytes < NMRP_MIN_PKT_LEN) {
283 fprintf(stderr, "Short packet (%d bytes)\n", (int)bytes);
287 msg_hdr_ntoh(&pkt->msg);
288 len = pkt->msg.len + sizeof(pkt->eh);
291 fprintf(stderr, "Short packet (expected %d, got %d).\n",
292 (int)len, (int)bytes);
296 return msg_ntoh(&pkt->msg);
299 static int mac_parse(const char *str, uint8_t *hwaddr)
304 sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x%n",
305 data, data + 1, data + 2, data + 3, data + 4, data + 5, &i);
307 if (i == strlen(str)) {
308 for (i = 0; i != 6; ++i) {
313 hwaddr[i] = data[i] & 0xff;
323 struct is_valid_ip_arg
325 struct in_addr *ipaddr;
326 struct in_addr *ipmask;
330 static int is_valid_ip_cb(struct ethsock_ip_callback_args *args)
332 #define SUBNET(x) ((x)->ipaddr->s_addr & (x)->ipmask->s_addr)
333 struct is_valid_ip_arg *arg = args->arg;
334 if (SUBNET(args) == SUBNET(arg)) {
335 arg->result = args->ipaddr->s_addr != arg->ipaddr->s_addr;
343 static int is_valid_ip(struct ethsock *sock, struct in_addr *ipaddr,
344 struct in_addr *ipmask)
347 struct is_valid_ip_arg arg = {
353 status = ethsock_for_each_ip(sock, is_valid_ip_cb, &arg);
354 return status < 0 ? status : arg.result;
357 static struct ethsock *gsock = NULL;
359 static void sigh(int sig)
363 ethsock_close(gsock);
369 static const char *spinner = "\\|/-";
371 int nmrp_do(struct nmrpd_args *args)
373 struct nmrp_pkt tx, rx;
374 uint8_t *src, dest[6];
377 struct in_addr ipaddr, ipmask;
379 int i, status, ulreqs, expect, upload_ok;
380 struct ethsock *sock;
381 void (*sigh_orig)(int);
383 if (args->op != NMRP_UPLOAD_FW) {
384 fprintf(stderr, "Operation not implemented.\n");
388 if (!mac_parse(args->mac, dest)) {
389 fprintf(stderr, "Invalid MAC address '%s'.\n", args->mac);
393 if ((ipaddr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
394 fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
398 if ((ipmask.s_addr = inet_addr(args->ipmask)) == INADDR_NONE) {
399 fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
403 if (strcmp(args->file_local, "-") && access(args->file_local, R_OK) == -1) {
404 fprintf(stderr, "Error accessing file '%s'.\n", args->file_local);
408 if (args->file_remote) {
409 if (!tftp_is_valid_filename(args->file_remote)) {
410 fprintf(stderr, "Invalid remote filename '%s'.\n",
418 sock = ethsock_create(args->intf, ETH_P_NMRP);
423 status = is_valid_ip(sock, &ipaddr, &ipmask);
426 fprintf(stderr, "Address %s/%s cannot be used on interface %s.\n",
427 args->ipaddr, args->ipmask, args->intf);
433 sigh_orig = signal(SIGINT, sigh);
435 if (ethsock_set_timeout(sock, args->rx_timeout)) {
439 src = ethsock_get_hwaddr(sock);
444 memcpy(tx.eh.ether_shost, src, 6);
445 memcpy(tx.eh.ether_dhost, dest, 6);
446 tx.eh.ether_type = htons(ETH_P_NMRP);
449 tx.msg.code = NMRP_C_ADVERTISE;
453 tx.msg.opts[0].type = NMRP_O_MAGIC_NO;
454 tx.msg.opts[0].len = NMRP_OPT_HDR_LEN + 4;
455 tx.msg.opts[0].val.magic[0] = 'N';
456 tx.msg.opts[0].val.magic[1] = 'T';
457 tx.msg.opts[0].val.magic[2] = 'G';
458 tx.msg.opts[0].val.magic[3] = 'R';
460 msg_update_len(&tx.msg);
468 printf("\rAdvertising NMRP server on %s ... %c",
469 args->intf, spinner[i]);
473 if (pkt_send(sock, &tx) < 0) {
478 status = pkt_recv(sock, &rx);
479 if (status == 0 && memcmp(rx.eh.ether_dhost, src, 6) == 0) {
481 } else if (status == 1) {
485 if ((time(NULL) - beg) >= 60) {
486 printf("\nNo response after 60 seconds. Bailing out.\n");
494 expect = NMRP_C_CONF_REQ;
498 if (expect != NMRP_C_NONE && rx.msg.code != expect) {
499 fprintf(stderr, "Received %s while waiting for %s!\n",
500 msg_code_str(rx.msg.code), msg_code_str(expect));
503 tx.msg.code = NMRP_C_NONE;
511 switch (rx.msg.code) {
512 case NMRP_C_ADVERTISE:
513 printf("Received NMRP advertisement from %s.\n",
514 mac_to_str(rx.eh.ether_shost));
517 case NMRP_C_CONF_REQ:
518 tx.msg.code = NMRP_C_CONF_ACK;
521 tx.msg.opts[0].type = NMRP_O_DEV_IP;
522 tx.msg.opts[0].len = NMRP_OPT_HDR_LEN + 2 * 4;
524 memcpy(tx.msg.opts[0].val.ip.addr, &ipaddr, 4);
525 memcpy(tx.msg.opts[0].val.ip.mask, &ipmask, 4);
527 tx.msg.opts[1].type = NMRP_O_FW_UP;
528 tx.msg.opts[1].len = NMRP_OPT_HDR_LEN;
530 #ifdef NMRPFLASH_SET_REGION
533 tx.msg.opts[2].type = NMRP_O_DEV_REGION;
534 tx.msg.opts[2].len = NMRP_OPT_HDR_LEN + 2;
535 tx.msg.opts[2].val.region = args->region;
538 expect = NMRP_C_TFTP_UL_REQ;
540 printf("Received configuration request from %s.\n",
541 mac_to_str(rx.eh.ether_shost));
543 memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6);
545 printf("Sending configuration: ip %s, mask %s.\n",
546 args->ipaddr, args->ipmask);
549 case NMRP_C_TFTP_UL_REQ:
552 printf("Bailing out after %d upload requests.\n",
554 tx.msg.code = NMRP_C_CLOSE_REQ;
559 printf("Ignoring extra upload request.\n");
561 ethsock_set_timeout(sock, args->ul_timeout);
562 tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
567 filename = msg_opt_data(&rx.msg, NMRP_O_FILE_NAME, &len);
569 if (!args->file_remote) {
570 args->file_remote = filename;
572 printf("Received upload request: filename '%.*s'.\n",
574 } else if (!args->file_remote) {
575 args->file_remote = args->file_local;
576 printf("Received upload request with empty filename.");
582 printf("Executing '%s' ... ", args->tftpcmd);
584 status = system(args->tftpcmd);
592 if (!status && args->file_local) {
593 status = is_valid_ip(sock, &ipaddr, &ipmask);
596 } else if (!status) {
597 printf("IP address of %s has changed. Please assign a "
598 "static ip to the interface.\n", args->intf);
599 tx.msg.code = NMRP_C_CLOSE_REQ;
604 printf("Using remote filename '%s'.\n",
608 if (!strcmp(args->file_local, "-")) {
609 printf("Uploading from stdin ... ");
611 printf("Uploading %s ... ", leafname(args->file_local));
614 status = tftp_put(args);
618 printf("OK\nWaiting for remote to respond.\n");
620 ethsock_set_timeout(sock, args->ul_timeout);
621 tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
622 expect = NMRP_C_NONE;
623 } else if (status == -2) {
624 expect = NMRP_C_TFTP_UL_REQ;
630 case NMRP_C_KEEP_ALIVE_REQ:
631 tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
632 ethsock_set_timeout(sock, 15000);
633 printf("Received keep-alive request.\n");
635 case NMRP_C_CLOSE_REQ:
636 tx.msg.code = NMRP_C_CLOSE_ACK;
638 case NMRP_C_CLOSE_ACK:
642 fprintf(stderr, "Unknown message code 0x%02x!\n",
644 msg_dump(&rx.msg, 0);
647 if (tx.msg.code != NMRP_C_NONE) {
648 msg_update_len(&tx.msg);
651 if (pkt_send(sock, &tx) < 0) {
657 if (rx.msg.code == NMRP_C_CLOSE_REQ) {
658 printf("Remote finished. Closing connection.\n");
662 status = pkt_recv(sock, &rx);
665 fprintf(stderr, "Timeout while waiting for %s.\n",
666 msg_code_str(expect));
671 ethsock_set_timeout(sock, args->rx_timeout);
678 printf("Reboot your device now.\n");
680 printf("No upload request received.\n");
684 signal(SIGINT, sigh_orig);