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/>.
21 #include <netinet/if_ether.h>
22 //#include <linux/if_packet.h>
23 #include <arpa/inet.h>
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
37 #define NMRP_HDR_LEN 6
38 #define NMRP_OPT_LEN 4
39 #define NMRP_MIN_PKT_LEN (sizeof(struct ether_header) + NMRP_HDR_LEN)
41 #define MAX_OPT_SIZE 12
44 #define ETH_P_NMRP 0x0912
46 #define PACKED __attribute__((__packed__))
47 #define MAX_LOOP_RECV 1024
56 NMRP_C_KEEP_ALIVE_REQ = 6,
57 NMRP_C_KEEP_ALIVE_ACK = 7,
58 NMRP_C_TFTP_UL_REQ = 16
62 NMRP_O_MAGIC_NO = 0x0001,
63 NMRP_O_DEV_IP = 0x0002,
64 NMRP_O_DEV_REGION = 0x0004,
65 NMRP_O_FW_UP = 0x0101,
66 NMRP_O_ST_UP = 0x0102,
67 NMRP_O_FILE_NAME = 0x0181
87 struct nmrp_opt opts[2];
92 struct ether_header eh;
96 static void msg_update_len(struct nmrp_msg *msg)
99 msg->len = NMRP_HDR_LEN;
100 for (; i != msg->num_opts; ++i) {
101 msg->len += msg->opts[i].len;
105 static void msg_dump(struct nmrp_msg *msg, int dump_opts)
107 struct nmrp_opt *opt;
108 int remain_len, len, i;
110 fprintf(stderr, "res=0x%04x, code=0x%02x, id=0x%02x, len=%u",
111 msg->reserved, msg->code, msg->id, msg->len);
113 remain_len = msg->len - NMRP_HDR_LEN;
114 fprintf(stderr, "%s\n", remain_len ? "" : " (no opts)");
119 while (remain_len > 0) {
121 fprintf(stderr, " opt type=%u, len=%u", opt->type, len);
122 for (i = 0; i != len - NMRP_OPT_LEN; ++i) {
124 fprintf(stderr, "\n ");
127 fprintf(stderr, "%02x ", ((char*)&opt->val)[i] & 0xff);
129 fprintf(stderr, "\n");
131 opt = (struct nmrp_opt*)(((char*)opt) + len);
136 static void msg_hton(struct nmrp_msg *msg)
140 msg->reserved = htons(msg->reserved);
141 msg->len = htons(msg->len);
143 for (; i != msg->num_opts; ++i) {
144 msg->opts[i].len = htons(msg->opts[i].len);
145 msg->opts[i].type = htons(msg->opts[i].type);
149 static void msg_hdr_ntoh(struct nmrp_msg *msg)
151 msg->reserved = ntohs(msg->reserved);
152 msg->len = ntohs(msg->len);
155 static int msg_ntoh(struct nmrp_msg *msg)
157 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 < MAX_OPT_NUM * 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 > 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 (%zi bytes)\n", bytes);
213 msg_hdr_ntoh(&pkt->msg);
214 len = pkt->msg.len + sizeof(pkt->eh);
217 fprintf(stderr, "Unexpected message length (%zi bytes).\n", len);
221 return msg_ntoh(&pkt->msg);
224 static int mac_parse(const char *str, uint8_t *hwaddr)
229 sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x%n",
230 data, data + 1, data + 2, data + 3, data + 4, data + 5, &i);
232 if (i == strlen(str)) {
233 for (i = 0; i != 6; ++i) {
238 hwaddr[i] = data[i] & 0xff;
248 static struct ethsock *gsock = NULL;
250 static void sigh(int sig)
254 ethsock_close(gsock);
260 static const char *spinner = "\\|/-";
262 int nmrp_do(struct nmrpd_args *args)
264 struct nmrp_pkt tx, rx;
265 uint8_t *src, dest[6];
266 struct in_addr ipaddr, ipmask;
268 int i, err, ulreqs, expect;
269 struct ethsock *sock;
272 if (args->op != NMRP_UPLOAD_FW) {
273 fprintf(stderr, "Operation not implemented.\n");
277 if (!mac_parse(args->mac, dest)) {
278 fprintf(stderr, "Invalid MAC address '%s'.\n", args->mac);
282 if (!inet_aton(args->ipaddr, &ipaddr)) {
283 fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
287 if (!inet_aton(args->ipmask, &ipmask)) {
288 fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
292 if (access(args->filename, R_OK) == -1) {
293 fprintf(stderr, "Error accessing file '%s'.\n", args->filename);
299 sock = ethsock_create(args->intf, ETH_P_NMRP);
305 sigh_orig = signal(SIGINT, sigh);
307 if (ethsock_set_timeout(sock, args->rx_timeout)) {
311 src = ethsock_get_hwaddr(sock);
316 memcpy(tx.eh.ether_shost, src, 6);
317 memcpy(tx.eh.ether_dhost, dest, 6);
318 tx.eh.ether_type = htons(ETH_P_NMRP);
321 tx.msg.code = NMRP_C_ADVERTISE;
324 tx.msg.opts[0].type = NMRP_O_MAGIC_NO;
325 tx.msg.opts[0].len = NMRP_OPT_LEN + 4;
326 tx.msg.opts[0].val.magic[0] = 'N';
327 tx.msg.opts[0].val.magic[1] = 'T';
328 tx.msg.opts[0].val.magic[2] = 'G';
329 tx.msg.opts[0].val.magic[3] = 'R';
331 msg_update_len(&tx.msg);
338 printf("\rAdvertising NMRP server on %s ... %c", args->intf,
343 if (pkt_send(sock, &tx) < 0) {
348 err = pkt_recv(sock, &rx);
349 if (err == 0 && memcmp(rx.eh.ether_dhost, src, 6) == 0) {
351 } else if (err == 1) {
355 if ((time(NULL) - beg) >= 60) {
356 printf("\nNo response after 60 seconds. Bailing out.\n");
364 expect = NMRP_C_CONF_REQ;
368 if (expect != NMRP_C_NONE && rx.msg.code != expect) {
369 fprintf(stderr, "Received code 0x%02x while waiting for 0x%02x!\n",
370 rx.msg.code, expect);
373 tx.msg.code = NMRP_C_NONE;
381 switch (rx.msg.code) {
382 case NMRP_C_ADVERTISE:
383 printf("Received NMRP advertisement from "
384 "%02x:%02x:%02x:%02x:%02x:%02x.\n",
385 rx.eh.ether_shost[0], rx.eh.ether_shost[1],
386 rx.eh.ether_shost[2], rx.eh.ether_shost[3],
387 rx.eh.ether_shost[4], rx.eh.ether_shost[5]);
390 case NMRP_C_CONF_REQ:
391 tx.msg.code = NMRP_C_CONF_ACK;
394 tx.msg.opts[0].type = NMRP_O_DEV_IP;
395 tx.msg.opts[0].len = NMRP_OPT_LEN + 2 * 4;
397 memcpy(tx.msg.opts[0].val.ip.addr, &ipaddr, 4);
398 memcpy(tx.msg.opts[0].val.ip.mask, &ipmask, 4);
400 tx.msg.opts[1].type = NMRP_O_FW_UP;
401 tx.msg.opts[1].len = NMRP_OPT_LEN;
403 expect = NMRP_C_TFTP_UL_REQ;
405 printf("Received configuration request from "
406 "%02x:%02x:%02x:%02x:%02x:%02x.\n",
407 rx.eh.ether_shost[0], rx.eh.ether_shost[1],
408 rx.eh.ether_shost[2], rx.eh.ether_shost[3],
409 rx.eh.ether_shost[4], rx.eh.ether_shost[5]);
411 memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6);
413 printf("Sending configuration: ip %s, mask %s.\n",
414 args->ipaddr, args->ipmask);
417 case NMRP_C_TFTP_UL_REQ:
419 fprintf(stderr, "Device re-requested file upload %d "
420 "times; aborting.\n", ulreqs);
421 tx.msg.code = NMRP_C_CLOSE_REQ;
425 if (!args->tftpcmd) {
426 printf("Uploading %s ... ", args->filename);
428 err = tftp_put(args);
430 printf("Running %s ... ", args->tftpcmd);
432 err = system(args->tftpcmd);
436 printf("OK\nWaiting for remote to respond.\n");
437 ethsock_set_timeout(sock, args->ul_timeout);
438 expect = NMRP_C_CLOSE_REQ;
445 case NMRP_C_KEEP_ALIVE_REQ:
446 tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
448 case NMRP_C_CLOSE_REQ:
449 tx.msg.code = NMRP_C_CLOSE_ACK;
451 case NMRP_C_CLOSE_ACK:
455 fprintf(stderr, "Unknown message code 0x%02x!\n",
457 msg_dump(&rx.msg, 0);
460 if (tx.msg.code != NMRP_C_NONE) {
461 msg_update_len(&tx.msg);
464 if (pkt_send(sock, &tx) < 0) {
470 if (rx.msg.code == NMRP_C_CLOSE_REQ) {
471 printf("Remote finished. Closing connection.\n");
475 err = pkt_recv(sock, &rx);
478 fprintf(stderr, "Timeout while waiting for 0x%02x.\n", expect);
483 ethsock_set_timeout(sock, args->rx_timeout);
490 signal(SIGINT, sigh_orig);