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>
36 #define NMRP_HDR_LEN 6
37 #define NMRP_OPT_LEN 4
38 #define NMRP_MIN_PKT_LEN (sizeof(struct ether_header) + NMRP_HDR_LEN)
40 #define MAX_OPT_SIZE 12
43 #define ETH_P_NMRP 0x0912
45 #define PACKED __attribute__((__packed__))
46 #define MAX_LOOP_RECV 1024
55 NMRP_C_KEEP_ALIVE_REQ = 6,
56 NMRP_C_KEEP_ALIVE_ACK = 7,
57 NMRP_C_TFTP_UL_REQ = 16
61 NMRP_O_MAGIC_NO = 0x0001,
62 NMRP_O_DEV_IP = 0x0002,
63 NMRP_O_DEV_REGION = 0x0004,
64 NMRP_O_FW_UP = 0x0101,
65 NMRP_O_ST_UP = 0x0102,
66 NMRP_O_FILE_NAME = 0x0181
86 struct nmrp_opt opts[2];
91 struct ether_header eh;
95 static void msg_update_len(struct nmrp_msg *msg)
98 msg->len = NMRP_HDR_LEN;
99 for (; i != msg->num_opts; ++i) {
100 msg->len += msg->opts[i].len;
104 static void msg_dump(struct nmrp_msg *msg, int dump_opts)
106 struct nmrp_opt *opt;
107 int remain_len, len, i;
109 fprintf(stderr, "res=0x%04x, code=0x%02x, id=0x%02x, len=%u",
110 msg->reserved, msg->code, msg->id, msg->len);
112 remain_len = msg->len - NMRP_HDR_LEN;
113 fprintf(stderr, "%s\n", remain_len ? "" : " (no opts)");
118 while (remain_len > 0) {
120 fprintf(stderr, " opt type=%u, len=%u", opt->type, len);
121 for (i = 0; i != len - NMRP_OPT_LEN; ++i) {
123 fprintf(stderr, "\n ");
126 fprintf(stderr, "%02x ", ((char*)&opt->val)[i] & 0xff);
128 fprintf(stderr, "\n");
130 opt = (struct nmrp_opt*)(((char*)opt) + len);
135 static void msg_hton(struct nmrp_msg *msg)
139 msg->reserved = htons(msg->reserved);
140 msg->len = htons(msg->len);
142 for (; i != msg->num_opts; ++i) {
143 msg->opts[i].len = htons(msg->opts[i].len);
144 msg->opts[i].type = htons(msg->opts[i].type);
148 static void msg_hdr_ntoh(struct nmrp_msg *msg)
150 msg->reserved = ntohs(msg->reserved);
151 msg->len = ntohs(msg->len);
154 static int msg_ntoh(struct nmrp_msg *msg)
156 struct nmrp_opt *opt = msg->opts;
160 remaining = msg->len - NMRP_HDR_LEN;
162 // FIXME maximum of two options supported, maximum option
164 if (remaining < MAX_OPT_NUM * MAX_OPT_SIZE) {
165 while (remaining > 0) {
166 if (remaining < NMRP_OPT_LEN) {
170 opt->type = ntohs(opt->type);
171 opt->len = ntohs(opt->len);
173 if (opt->len > MAX_OPT_SIZE) {
177 remaining -= opt->len;
186 fprintf(stderr, "Unexpected message format.\n");
191 static int pkt_send(struct ethsock *sock, struct nmrp_pkt *pkt)
193 size_t len = ntohs(pkt->msg.len) + sizeof(pkt->eh);
194 return ethsock_send(sock, pkt, len);
197 static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
201 memset(pkt, 0, sizeof(*pkt));
202 bytes = ethsock_recv(sock, pkt, sizeof(*pkt));
207 } else if (bytes < NMRP_MIN_PKT_LEN) {
208 fprintf(stderr, "Short packet (%zi bytes)\n", bytes);
212 msg_hdr_ntoh(&pkt->msg);
213 len = pkt->msg.len + sizeof(pkt->eh);
216 fprintf(stderr, "Unexpected message length (%zi bytes).\n", len);
220 return msg_ntoh(&pkt->msg);
223 static int mac_parse(const char *str, uint8_t *hwaddr)
228 sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x%n",
229 data, data + 1, data + 2, data + 3, data + 4, data + 5, &i);
231 if (i == strlen(str)) {
232 for (i = 0; i != 6; ++i) {
237 hwaddr[i] = data[i] & 0xff;
247 static const char *spinner = "\\|/-";
249 int nmrp_do(struct nmrpd_args *args)
251 struct nmrp_pkt tx, rx;
252 uint8_t *src, dest[6];
253 struct in_addr ipaddr, ipmask;
255 int i, err, ulreqs, expect;
256 struct ethsock *sock;
258 if (args->op != NMRP_UPLOAD_FW) {
259 fprintf(stderr, "Operation not implemented.\n");
263 if (!mac_parse(args->mac, dest)) {
264 fprintf(stderr, "Invalid MAC address '%s'.\n", args->mac);
268 if (!inet_aton(args->ipaddr, &ipaddr)) {
269 fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
273 if (!inet_aton(args->ipmask, &ipmask)) {
274 fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
278 if (access(args->filename, R_OK) == -1) {
279 fprintf(stderr, "Error accessing file '%s'.\n", args->filename);
285 sock = ethsock_create(args->intf, ETH_P_NMRP);
290 if (ethsock_set_timeout(sock, args->rx_timeout)) {
294 src = ethsock_get_hwaddr(sock);
299 memcpy(tx.eh.ether_shost, src, 6);
300 memcpy(tx.eh.ether_dhost, dest, 6);
301 tx.eh.ether_type = htons(ETH_P_NMRP);
304 tx.msg.code = NMRP_C_ADVERTISE;
307 tx.msg.opts[0].type = NMRP_O_MAGIC_NO;
308 tx.msg.opts[0].len = NMRP_OPT_LEN + 4;
309 tx.msg.opts[0].val.magic[0] = 'N';
310 tx.msg.opts[0].val.magic[1] = 'T';
311 tx.msg.opts[0].val.magic[2] = 'G';
312 tx.msg.opts[0].val.magic[3] = 'R';
314 msg_update_len(&tx.msg);
321 printf("\rAdvertising NMRP server on %s ... %c", args->intf,
326 if (pkt_send(sock, &tx) < 0) {
331 err = pkt_recv(sock, &rx);
332 if (err == 0 && memcmp(rx.eh.ether_dhost, src, 6) == 0) {
334 } else if (err == 1) {
338 if ((time(NULL) - beg) >= 60) {
339 printf("\nNo response after 60 seconds. Bailing out.\n");
347 expect = NMRP_C_CONF_REQ;
351 if (expect != NMRP_C_NONE && rx.msg.code != expect) {
352 fprintf(stderr, "Received code 0x%02x while waiting for 0x%02x!\n",
353 rx.msg.code, expect);
356 tx.msg.code = NMRP_C_NONE;
364 switch (rx.msg.code) {
365 case NMRP_C_ADVERTISE:
366 printf("Received NMRP advertisement from "
367 "%02x:%02x:%02x:%02x:%02x:%02x.\n",
368 rx.eh.ether_shost[0], rx.eh.ether_shost[1],
369 rx.eh.ether_shost[2], rx.eh.ether_shost[3],
370 rx.eh.ether_shost[4], rx.eh.ether_shost[5]);
373 case NMRP_C_CONF_REQ:
374 tx.msg.code = NMRP_C_CONF_ACK;
377 tx.msg.opts[0].type = NMRP_O_DEV_IP;
378 tx.msg.opts[0].len = NMRP_OPT_LEN + 2 * 4;
380 memcpy(tx.msg.opts[0].val.ip.addr, &ipaddr, 4);
381 memcpy(tx.msg.opts[0].val.ip.mask, &ipmask, 4);
383 tx.msg.opts[1].type = NMRP_O_FW_UP;
384 tx.msg.opts[1].len = NMRP_OPT_LEN;
386 expect = NMRP_C_TFTP_UL_REQ;
388 printf("Received configuration request from "
389 "%02x:%02x:%02x:%02x:%02x:%02x.\n",
390 rx.eh.ether_shost[0], rx.eh.ether_shost[1],
391 rx.eh.ether_shost[2], rx.eh.ether_shost[3],
392 rx.eh.ether_shost[4], rx.eh.ether_shost[5]);
394 memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6);
396 printf("Sending configuration: ip %s, mask %s.\n",
397 args->ipaddr, args->ipmask);
400 case NMRP_C_TFTP_UL_REQ:
402 fprintf(stderr, "Device re-requested file upload %d "
403 "times; aborting.\n", ulreqs);
404 tx.msg.code = NMRP_C_CLOSE_REQ;
408 if (!args->tftpcmd) {
409 printf("Uploading %s ... ", args->filename);
411 err = tftp_put(args);
413 printf("Running %s ... ", args->tftpcmd);
415 err = system(args->tftpcmd);
419 printf("OK\nWaiting for remote to respond.\n");
420 ethsock_set_timeout(sock, args->ul_timeout);
421 expect = NMRP_C_CLOSE_REQ;
428 case NMRP_C_KEEP_ALIVE_REQ:
429 tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
431 case NMRP_C_CLOSE_REQ:
432 tx.msg.code = NMRP_C_CLOSE_ACK;
434 case NMRP_C_CLOSE_ACK:
438 fprintf(stderr, "Unknown message code 0x%02x!\n",
440 msg_dump(&rx.msg, 0);
443 if (tx.msg.code != NMRP_C_NONE) {
444 msg_update_len(&tx.msg);
447 if (pkt_send(sock, &tx) < 0) {
453 if (rx.msg.code == NMRP_C_CLOSE_REQ) {
454 printf("Remote finished. Closing connection.\n");
458 err = pkt_recv(sock, &rx);
461 fprintf(stderr, "Timeout while waiting for 0x%02x.\n", expect);
466 ethsock_set_timeout(sock, args->rx_timeout);