Ensure that NMRP packets are at least 64 bytes
[oweals/nmrpflash.git] / nmrp.c
diff --git a/nmrp.c b/nmrp.c
index 327bf816bc0d2d956dbe67cb472496721f164a04..8cf82d6ed560b9da1812ce21b5d941fd6eecf30c 100644 (file)
--- a/nmrp.c
+++ b/nmrp.c
 #define IP_LEN 4
 #define MAX_LOOP_RECV 1024
 
+#ifndef MAX
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+#endif
+
 #ifndef PACKED
 #define PACKED __attribute__((__packed__))
 #endif
 
+#ifdef NMRPFLASH_WINDOWS
+#define setenv(name, value, overwrite) SetEnvironmentVariable(name, value)
+#endif
+
 enum nmrp_code {
        NMRP_C_NONE = 0,
        NMRP_C_ADVERTISE = 1,
@@ -82,16 +90,13 @@ struct nmrp_msg {
        uint8_t code;
        uint8_t id;
        uint16_t len;
+       /* only opts[0] is valid! think of this as a char* */
        struct nmrp_opt opts[NMRP_MAX_OPT_NUM];
+       uint8_t padding[8];
+       /* this is NOT part of the transmitted packet */
        uint32_t num_opts;
 } PACKED;
 
-struct eth_hdr {
-       uint8_t ether_dhost[6];
-       uint8_t ether_shost[6];
-       uint16_t ether_type;
-} PACKED;
-
 struct nmrp_pkt {
        struct eth_hdr eh;
        struct nmrp_msg msg;
@@ -134,37 +139,15 @@ static uint16_t to_region_code(const char *region)
        return 0;
 }
 
-static void msg_dump(struct nmrp_msg *msg, int dump_opts)
+static void msg_dump(struct nmrp_msg *msg)
 {
-       struct nmrp_opt *opt;
-       int remain_len, len, i;
+       int remain_len;
 
        fprintf(stderr, "res=0x%04x, code=0x%02x, id=0x%02x, len=%u",
                        msg->reserved, msg->code, msg->id, msg->len);
 
        remain_len = msg->len - NMRP_HDR_LEN;
        fprintf(stderr, "%s\n", remain_len ? "" : " (no opts)");
-
-       if (dump_opts) {
-               opt = msg->opts;
-
-               while (remain_len > 0) {
-                       len = opt->len;
-                       fprintf(stderr, "  opt type=%u, len=%u", opt->type, len);
-                       if (len) {
-                               for (i = 0; i != len - NMRP_OPT_HDR_LEN; ++i) {
-                                       if (!(i % 16)) {
-                                               fprintf(stderr, "\n  ");
-                                       }
-
-                                       fprintf(stderr, "%02x ", ((char*)&opt->val)[i] & 0xff);
-                               }
-                               fprintf(stderr, "\n");
-                       }
-                       remain_len -= len;
-                       opt = NMRP_OPT_NEXT(opt);
-               }
-       }
 }
 
 static void msg_hton(struct nmrp_msg *msg)
@@ -207,7 +190,7 @@ static int msg_ntoh(struct nmrp_msg *msg)
                        opt->type = ntohs(opt->type);
                        opt->len = ntohs(opt->len);
 
-                       if (opt->len > NMRP_MAX_OPT_SIZE) {
+                       if (!opt->len || opt->len > NMRP_MAX_OPT_SIZE) {
                                break;
                        }
 
@@ -221,7 +204,7 @@ static int msg_ntoh(struct nmrp_msg *msg)
        }
 
        fprintf(stderr, "Unexpected message format.\n");
-       msg_dump(msg, 0);
+       msg_dump(msg);
        return 1;
 }
 
@@ -243,6 +226,10 @@ static void *msg_opt_data(struct nmrp_msg *msg, uint16_t type, uint16_t *len)
                        return buf;
                }
 
+               if (!opt->len) {
+                       break;
+               }
+
                remaining -= opt->len;
                opt = NMRP_OPT_NEXT(opt);
        }
@@ -283,10 +270,31 @@ static inline void msg_init(struct nmrp_msg *msg, uint16_t code)
        msg->code = code;
 }
 
+#ifdef NMRPFLASH_FUZZ
+#define NMRP_INITIAL_TIMEOUT 0
+#define ethsock_create(a, b) ((struct ethsock*)1)
+#define ethsock_get_hwaddr(a) ethsock_get_hwaddr_fake(a)
+#define ethsock_recv(sock, buf, len) read(STDIN_FILENO, buf, len)
+#define ethsock_send(a, b, c) (0)
+#define ethsock_set_timeout(a, b) (0)
+#define ethsock_ip_add(a, b, c, d) (0)
+#define ethsock_ip_del(a, b) (0)
+#define ethsock_close(a) (0)
+#define tftp_put(a) (0)
+
+static uint8_t *ethsock_get_hwaddr_fake(struct ethsock* sock)
+{
+       static uint8_t hwaddr[6] = { 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa };
+       return hwaddr;
+}
+#else
+#define NMRP_INITIAL_TIMEOUT 60
+#endif
+
 static int pkt_send(struct ethsock *sock, struct nmrp_pkt *pkt)
 {
        size_t len = ntohs(pkt->msg.len) + sizeof(pkt->eh);
-       return ethsock_send(sock, pkt, len);
+       return ethsock_send(sock, pkt, MAX(len, 64));
 }
 
 static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
@@ -374,16 +382,9 @@ static int is_valid_ip(struct ethsock *sock, struct in_addr *ipaddr,
        return status < 0 ? status : arg.result;
 }
 
-static struct ethsock *gsock = NULL;
-
 static void sigh(int sig)
 {
-       printf("\n");
-       if (gsock) {
-               ethsock_close(gsock);
-       }
-
-       exit(1);
+       g_interrupted = 1;
 }
 
 static const char *spinner = "\\|/-";
@@ -395,8 +396,11 @@ int nmrp_do(struct nmrpd_args *args)
        uint16_t len, region;
        char *filename;
        time_t beg;
-       int i, status, ulreqs, expect, upload_ok;
+       int i, status, ulreqs, expect, upload_ok, autoip;
        struct ethsock *sock;
+       struct ethsock_ip_undo *ip_undo = NULL;
+       struct ethsock_arp_undo *arp_undo = NULL;
+       uint32_t intf_addr;
        void (*sigh_orig)(int);
        struct {
                struct in_addr addr;
@@ -413,17 +417,40 @@ int nmrp_do(struct nmrpd_args *args)
                return 1;
        }
 
+       ipconf.mask.s_addr = inet_addr(args->ipmask);
+       if (ipconf.mask.s_addr == INADDR_NONE
+                       || netmask(bitcount(ipconf.mask.s_addr)) != ipconf.mask.s_addr) {
+               fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
+               return 1;
+       }
+
+       if (!args->ipaddr) {
+               autoip = true;
+               /* The MAC of the device that was used to test this utility starts
+                * with a4:2b:8c, hence 164 (0xa4) and 183 (0x2b + 0x8c)
+                */
+               args->ipaddr = "10.164.183.252";
+
+               if (!args->ipaddr_intf) {
+                       args->ipaddr_intf = "10.164.183.253";
+               }
+       } else if (args->ipaddr_intf) {
+               autoip = true;
+       } else {
+               autoip = false;
+       }
+
        if ((ipconf.addr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
                fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
                return 1;
        }
 
-       if ((ipconf.mask.s_addr = inet_addr(args->ipmask)) == INADDR_NONE) {
-               fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
+       if (args->ipaddr_intf && (intf_addr = inet_addr(args->ipaddr_intf)) == INADDR_NONE) {
+               fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr_intf);
                return 1;
        }
 
-       if (strcmp(args->file_local, "-") && access(args->file_local, R_OK) == -1) {
+       if (args->file_local && strcmp(args->file_local, "-") && access(args->file_local, R_OK) == -1) {
                fprintf(stderr, "Error accessing file '%s'.\n", args->file_local);
                return 1;
        }
@@ -453,17 +480,26 @@ int nmrp_do(struct nmrpd_args *args)
                return 1;
        }
 
-       status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask);
-       if (status <= 0) {
-               if (!status) {
-                       fprintf(stderr, "Address %s/%s cannot be used on interface %s.\n",
-                                       args->ipaddr, args->ipmask, args->intf);
+       sigh_orig = signal(SIGINT, sigh);
+
+       if (!autoip) {
+               status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask);
+               if (status <= 0) {
+                       if (!status) {
+                               fprintf(stderr, "Address %s/%s cannot be used on interface %s.\n",
+                                               args->ipaddr, args->ipmask, args->intf);
+                       }
+                       goto out;
+               }
+       } else {
+               if (verbosity) {
+                       printf("Adding %s to interface %s.\n", args->ipaddr_intf, args->intf);
                }
-               goto out;
-       }
 
-       gsock = sock;
-       sigh_orig = signal(SIGINT, sigh);
+               if (ethsock_ip_add(sock, intf_addr, ipconf.mask.s_addr, &ip_undo) != 0) {
+                       goto out;
+               }
+       }
 
        if (ethsock_set_timeout(sock, args->rx_timeout)) {
                goto out;
@@ -484,16 +520,16 @@ int nmrp_do(struct nmrpd_args *args)
 
        i = 0;
        upload_ok = 0;
-       beg = time(NULL);
+       beg = time_monotonic();
 
-       while (1) {
+       while (!g_interrupted) {
                printf("\rAdvertising NMRP server on %s ... %c",
                                args->intf, spinner[i]);
                fflush(stdout);
                i = (i + 1) & 3;
 
                if (pkt_send(sock, &tx) < 0) {
-                       perror("sendto");
+                       xperror("sendto");
                        goto out;
                }
 
@@ -501,10 +537,11 @@ int nmrp_do(struct nmrpd_args *args)
                if (status == 0 && memcmp(rx.eh.ether_dhost, src, 6) == 0) {
                        break;
                } else if (status == 1) {
-                       printf("ERR\n");
                        goto out;
                } else {
-                       if ((time(NULL) - beg) >= 60) {
+                       /* because we don't want nmrpflash's exit status to be zero */
+                       status = 1;
+                       if ((time_monotonic() - beg) >= NMRP_INITIAL_TIMEOUT) {
                                printf("\nNo response after 60 seconds. Bailing out.\n");
                                goto out;
                        }
@@ -516,7 +553,7 @@ int nmrp_do(struct nmrpd_args *args)
        expect = NMRP_C_CONF_REQ;
        ulreqs = 0;
 
-       do {
+       while (!g_interrupted) {
                if (expect != NMRP_C_NONE && rx.msg.code != expect) {
                        fprintf(stderr, "Received %s while waiting for %s!\n",
                                        msg_code_str(rx.msg.code), msg_code_str(expect));
@@ -551,9 +588,13 @@ int nmrp_do(struct nmrpd_args *args)
 
                                memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6);
 
-                               printf("Sending configuration: ip %s, mask %s.\n",
+                               printf("Sending configuration: %s, netmask %s.\n",
                                                args->ipaddr, args->ipmask);
 
+                               if (ethsock_arp_add(sock, rx.eh.ether_shost, ipconf.addr.s_addr, &arp_undo) != 0) {
+                                       goto out;
+                               }
+
                                break;
                        case NMRP_C_TFTP_UL_REQ:
                                if (!upload_ok) {
@@ -588,25 +629,26 @@ int nmrp_do(struct nmrpd_args *args)
                                status = 0;
 
                                if (args->tftpcmd) {
-                                       printf("Executing '%s' ... ", args->tftpcmd);
-                                       fflush(stdout);
+                                       printf("Executing '%s' ... \n", args->tftpcmd);
+                                       setenv("IP", inet_ntoa(ipconf.addr), 1);
+                                       setenv("PORT", lltostr(args->port, 10), 1);
+                                       setenv("MAC", mac_to_str(rx.eh.ether_shost), 1);
+                                       setenv("NETMASK", inet_ntoa(ipconf.mask), 1);
+                                       //setenv("FILENAME", args->file_remote ? args->file_remote : "", 1);
                                        status = system(args->tftpcmd);
-                                       if (!status) {
-                                               printf("OK\n");
-                                       } else {
-                                               printf("ERR\n");
-                                       }
                                }
 
                                if (!status && args->file_local) {
-                                       status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask);
-                                       if (status < 0) {
-                                               goto out;
-                                       } else if (!status) {
-                                               printf("IP address of %s has changed. Please assign a "
-                                                               "static ip to the interface.\n", args->intf);
-                                               tx.msg.code = NMRP_C_CLOSE_REQ;
-                                               break;
+                                       if (!autoip) {
+                                               status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask);
+                                               if (status < 0) {
+                                                       goto out;
+                                               } else if (!status) {
+                                                       printf("IP address of %s has changed. Please assign a "
+                                                                       "static ip to the interface.\n", args->intf);
+                                                       tx.msg.code = NMRP_C_CLOSE_REQ;
+                                                       break;
+                                               }
                                        }
 
                                        if (verbosity) {
@@ -620,11 +662,14 @@ int nmrp_do(struct nmrpd_args *args)
                                                printf("Uploading %s ... ", leafname(args->file_local));
                                        }
                                        fflush(stdout);
-                                       status = tftp_put(args);
+                                       if (!(status = tftp_put(args))) {
+                                               printf("OK\n");
+                                       }
+
                                }
 
                                if (!status) {
-                                       printf("OK\nWaiting for remote to respond.\n");
+                                       printf("Waiting for remote to respond.\n");
                                        upload_ok = 1;
                                        ethsock_set_timeout(sock, args->ul_timeout);
                                        tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
@@ -638,7 +683,7 @@ int nmrp_do(struct nmrpd_args *args)
                                break;
                        case NMRP_C_KEEP_ALIVE_REQ:
                                tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
-                               ethsock_set_timeout(sock, 15000);
+                               ethsock_set_timeout(sock, args->ul_timeout);
                                printf("Received keep-alive request.\n");
                                break;
                        case NMRP_C_CLOSE_REQ:
@@ -650,14 +695,18 @@ int nmrp_do(struct nmrpd_args *args)
                        default:
                                fprintf(stderr, "Unknown message code 0x%02x!\n",
                                                rx.msg.code);
-                               msg_dump(&rx.msg, 0);
+                               msg_dump(&rx.msg);
                }
 
                if (tx.msg.code != NMRP_C_NONE) {
                        msg_hton(&tx.msg);
 
                        if (pkt_send(sock, &tx) < 0) {
-                               perror("sendto");
+                               xperror("sendto");
+                               goto out;
+                       }
+
+                       if (tx.msg.code == NMRP_C_CLOSE_REQ) {
                                goto out;
                        }
                }
@@ -678,19 +727,21 @@ int nmrp_do(struct nmrpd_args *args)
 
                ethsock_set_timeout(sock, args->rx_timeout);
 
-       } while (1);
-
-       status = 0;
+       }
 
-       if (ulreqs) {
-               printf("Reboot your device now.\n");
-       } else {
-               printf("No upload request received.\n");
+       if (!g_interrupted) {
+               status = 0;
+               if (ulreqs) {
+                       printf("Reboot your device now.\n");
+               } else {
+                       printf("No upload request received.\n");
+               }
        }
 
 out:
        signal(SIGINT, sigh_orig);
-       gsock = NULL;
+       ethsock_arp_del(sock, &arp_undo);
+       ethsock_ip_del(sock, &ip_undo);
        ethsock_close(sock);
        return status;
 }