2 * nmrp-flash - Netgear Unbrick Utility
3 * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
5 * nmrp-flash 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 * nmrp-flash 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 nmrp-flash. If not, see <http://www.gnu.org/licenses/>.
29 #define NMRP_HDR_LEN 6
30 #define NMRP_OPT_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 2
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
82 struct nmrp_opt opts[2];
87 uint8_t ether_dhost[6];
88 uint8_t ether_shost[6];
97 static void msg_update_len(struct nmrp_msg *msg)
100 msg->len = NMRP_HDR_LEN;
101 for (; i != msg->num_opts; ++i) {
102 msg->len += msg->opts[i].len;
106 static void msg_dump(struct nmrp_msg *msg, int dump_opts)
108 struct nmrp_opt *opt;
109 int remain_len, len, i;
111 fprintf(stderr, "res=0x%04x, code=0x%02x, id=0x%02x, len=%u",
112 msg->reserved, msg->code, msg->id, msg->len);
114 remain_len = msg->len - NMRP_HDR_LEN;
115 fprintf(stderr, "%s\n", remain_len ? "" : " (no opts)");
120 while (remain_len > 0) {
122 fprintf(stderr, " opt type=%u, len=%u", opt->type, len);
123 for (i = 0; i != len - NMRP_OPT_LEN; ++i) {
125 fprintf(stderr, "\n ");
128 fprintf(stderr, "%02x ", ((char*)&opt->val)[i] & 0xff);
130 fprintf(stderr, "\n");
132 opt = (struct nmrp_opt*)(((char*)opt) + len);
137 static void msg_hton(struct nmrp_msg *msg)
141 msg->reserved = htons(msg->reserved);
142 msg->len = htons(msg->len);
144 for (; i != msg->num_opts; ++i) {
145 msg->opts[i].len = htons(msg->opts[i].len);
146 msg->opts[i].type = htons(msg->opts[i].type);
150 static void msg_hdr_ntoh(struct nmrp_msg *msg)
152 msg->reserved = ntohs(msg->reserved);
153 msg->len = ntohs(msg->len);
156 static int msg_ntoh(struct nmrp_msg *msg)
158 struct nmrp_opt *opt = msg->opts;
161 remaining = msg->len - NMRP_HDR_LEN;
163 // FIXME maximum of two options supported, maximum option
165 if (remaining < NMRP_MAX_OPT_NUM * NMRP_MAX_OPT_SIZE) {
166 while (remaining > 0) {
167 if (remaining < NMRP_OPT_LEN) {
171 opt->type = ntohs(opt->type);
172 opt->len = ntohs(opt->len);
174 if (opt->len > NMRP_MAX_OPT_SIZE) {
178 remaining -= opt->len;
187 fprintf(stderr, "Unexpected message format.\n");
192 static int pkt_send(struct ethsock *sock, struct nmrp_pkt *pkt)
194 size_t len = ntohs(pkt->msg.len) + sizeof(pkt->eh);
195 return ethsock_send(sock, pkt, len);
198 static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
202 memset(pkt, 0, sizeof(*pkt));
203 bytes = ethsock_recv(sock, pkt, sizeof(*pkt));
208 } else if (bytes < NMRP_MIN_PKT_LEN) {
209 fprintf(stderr, "Short packet (%d bytes)\n", (int)bytes);
213 msg_hdr_ntoh(&pkt->msg);
214 len = pkt->msg.len + sizeof(pkt->eh);
217 fprintf(stderr, "Short packet (expected %d, got %d).\n",
218 (int)len, (int)bytes);
222 return msg_ntoh(&pkt->msg);
225 static int mac_parse(const char *str, uint8_t *hwaddr)
230 sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x%n",
231 data, data + 1, data + 2, data + 3, data + 4, data + 5, &i);
233 if (i == strlen(str)) {
234 for (i = 0; i != 6; ++i) {
239 hwaddr[i] = data[i] & 0xff;
249 static struct ethsock *gsock = NULL;
251 static void sigh(int sig)
255 ethsock_close(gsock);
261 static const char *spinner = "\\|/-";
263 int nmrp_do(struct nmrpd_args *args)
265 struct nmrp_pkt tx, rx;
266 uint8_t *src, dest[6];
267 struct in_addr ipaddr, ipmask;
269 int i, err, ulreqs, expect;
270 struct ethsock *sock;
271 void (*sigh_orig)(int);
273 if (args->op != NMRP_UPLOAD_FW) {
274 fprintf(stderr, "Operation not implemented.\n");
278 if (!mac_parse(args->mac, dest)) {
279 fprintf(stderr, "Invalid MAC address '%s'.\n", args->mac);
283 if ((ipaddr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
284 fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
288 if ((ipmask.s_addr = inet_addr(args->ipmask)) == INADDR_NONE) {
289 fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
293 if (access(args->filename, R_OK) == -1) {
294 fprintf(stderr, "Error accessing file '%s'.\n", args->filename);
300 sock = ethsock_create(args->intf, ETH_P_NMRP);
306 sigh_orig = signal(SIGINT, sigh);
308 if (ethsock_set_timeout(sock, args->rx_timeout)) {
312 src = ethsock_get_hwaddr(sock);
317 memcpy(tx.eh.ether_shost, src, 6);
318 memcpy(tx.eh.ether_dhost, dest, 6);
319 tx.eh.ether_type = htons(ETH_P_NMRP);
322 tx.msg.code = NMRP_C_ADVERTISE;
325 tx.msg.opts[0].type = NMRP_O_MAGIC_NO;
326 tx.msg.opts[0].len = NMRP_OPT_LEN + 4;
327 tx.msg.opts[0].val.magic[0] = 'N';
328 tx.msg.opts[0].val.magic[1] = 'T';
329 tx.msg.opts[0].val.magic[2] = 'G';
330 tx.msg.opts[0].val.magic[3] = 'R';
332 msg_update_len(&tx.msg);
339 printf("\rAdvertising NMRP server on interface ... %c",
344 if (pkt_send(sock, &tx) < 0) {
349 err = pkt_recv(sock, &rx);
350 if (err == 0 && memcmp(rx.eh.ether_dhost, src, 6) == 0) {
352 } else if (err == 1) {
356 if ((time(NULL) - beg) >= 60) {
357 printf("\nNo response after 60 seconds. Bailing out.\n");
365 expect = NMRP_C_CONF_REQ;
369 if (expect != NMRP_C_NONE && rx.msg.code != expect) {
370 fprintf(stderr, "Received code 0x%02x while waiting for 0x%02x!\n",
371 rx.msg.code, expect);
374 tx.msg.code = NMRP_C_NONE;
382 switch (rx.msg.code) {
383 case NMRP_C_ADVERTISE:
384 printf("Received NMRP advertisement from %s.\n",
385 mac_to_str(rx.eh.ether_shost));
388 case NMRP_C_CONF_REQ:
389 tx.msg.code = NMRP_C_CONF_ACK;
392 tx.msg.opts[0].type = NMRP_O_DEV_IP;
393 tx.msg.opts[0].len = NMRP_OPT_LEN + 2 * 4;
395 memcpy(tx.msg.opts[0].val.ip.addr, &ipaddr, 4);
396 memcpy(tx.msg.opts[0].val.ip.mask, &ipmask, 4);
398 tx.msg.opts[1].type = NMRP_O_FW_UP;
399 tx.msg.opts[1].len = NMRP_OPT_LEN;
401 expect = NMRP_C_TFTP_UL_REQ;
403 printf("Received configuration request from %s.\n",
404 mac_to_str(rx.eh.ether_shost));
406 memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6);
408 printf("Sending configuration: ip %s, mask %s.\n",
409 args->ipaddr, args->ipmask);
412 case NMRP_C_TFTP_UL_REQ:
414 fprintf(stderr, "Device re-requested file upload %d "
415 "times; aborting.\n", ulreqs);
416 tx.msg.code = NMRP_C_CLOSE_REQ;
420 if (!args->tftpcmd) {
421 printf("Uploading %s ... ", args->filename);
423 err = tftp_put(args);
425 printf("Running %s ... ", args->tftpcmd);
427 err = system(args->tftpcmd);
431 printf("OK\nWaiting for remote to respond.\n");
432 ethsock_set_timeout(sock, args->ul_timeout);
433 expect = NMRP_C_CLOSE_REQ;
434 } else if (err == -2) {
435 expect = NMRP_C_TFTP_UL_REQ;
441 case NMRP_C_KEEP_ALIVE_REQ:
442 tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
444 case NMRP_C_CLOSE_REQ:
445 tx.msg.code = NMRP_C_CLOSE_ACK;
447 case NMRP_C_CLOSE_ACK:
451 fprintf(stderr, "Unknown message code 0x%02x!\n",
453 msg_dump(&rx.msg, 0);
456 if (tx.msg.code != NMRP_C_NONE) {
457 msg_update_len(&tx.msg);
460 if (pkt_send(sock, &tx) < 0) {
466 if (rx.msg.code == NMRP_C_CLOSE_REQ) {
467 printf("Remote finished. Closing connection.\n");
471 err = pkt_recv(sock, &rx);
474 fprintf(stderr, "Timeout while waiting for 0x%02x.\n", expect);
479 ethsock_set_timeout(sock, args->rx_timeout);
486 signal(SIGINT, sigh_orig);