#include "nmrpd.h"
#define NMRP_HDR_LEN 6
-#define NMRP_OPT_LEN 4
+#define NMRP_OPT_HDR_LEN 4
#define NMRP_MIN_PKT_LEN (sizeof(struct eth_hdr) + NMRP_HDR_LEN)
#define NMRP_MAX_OPT_SIZE 12
-#define NMRP_MAX_OPT_NUM 2
+#define NMRP_MAX_OPT_NUM 3
#define ETH_P_NMRP 0x0912
#define IP_LEN 4
uint16_t len;
union {
uint8_t magic[4];
+ uint16_t region;
struct {
uint8_t addr[4];
uint8_t mask[4];
uint8_t code;
uint8_t id;
uint16_t len;
- struct nmrp_opt opts[2];
+ struct nmrp_opt opts[NMRP_MAX_OPT_NUM];
uint32_t num_opts;
} PACKED;
struct nmrp_msg msg;
} PACKED;
+static const char *msg_code_str(uint16_t code)
+{
+#define CASE_CODE(x) case NMRP_C_ ## x: return #x
+ static char buf[16];
+
+ switch (code) {
+ CASE_CODE(ADVERTISE);
+ CASE_CODE(CONF_REQ);
+ CASE_CODE(CONF_ACK);
+ CASE_CODE(CLOSE_REQ);
+ CASE_CODE(CLOSE_ACK);
+ CASE_CODE(KEEP_ALIVE_REQ);
+ CASE_CODE(KEEP_ALIVE_ACK);
+ CASE_CODE(TFTP_UL_REQ);
+ default:
+ snprintf(buf, sizeof(buf), "%04x", code);
+ return buf;
+ }
+#undef CASE_CODE
+}
+
static void msg_update_len(struct nmrp_msg *msg)
{
uint32_t i = 0;
while (remain_len > 0) {
len = opt->len;
fprintf(stderr, " opt type=%u, len=%u", opt->type, len);
- for (i = 0; i != len - NMRP_OPT_LEN; ++i) {
+ for (i = 0; i != len - NMRP_OPT_HDR_LEN; ++i) {
if (!(i % 16)) {
fprintf(stderr, "\n ");
}
// size is 12
if (remaining < NMRP_MAX_OPT_NUM * NMRP_MAX_OPT_SIZE) {
while (remaining > 0) {
- if (remaining < NMRP_OPT_LEN) {
+ if (remaining < NMRP_OPT_HDR_LEN) {
break;
}
return 1;
}
+static void *msg_opt_data(struct nmrp_msg *msg, uint16_t type, uint16_t *len)
+{
+ static char buf[128];
+ struct nmrp_opt *opt = msg->opts;
+ int remaining = msg->len - NMRP_HDR_LEN;
+
+ memset(buf, 0, sizeof(buf));
+
+ while (remaining > 0) {
+ if (opt->type == type) {
+ if (opt->len == NMRP_OPT_HDR_LEN) {
+ return NULL;
+ }
+ *len = opt->len - NMRP_OPT_HDR_LEN;
+ memcpy(buf, &opt->val, MIN(*len, sizeof(buf)-1));
+ return buf;
+ }
+
+ remaining -= opt->len;
+ opt = (struct nmrp_opt*)((char*)opt) + opt->len;
+ }
+
+ return NULL;
+}
+
+static void msg_opt_add(struct nmrp_msg *msg, uint16_t type, void *data,
+ uint16_t len)
+{
+ uint32_t i = 0;
+ struct nmrp_opt *opt = msg->opts;
+
+ if (len + NMRP_OPT_HDR_LEN > NMRP_MAX_OPT_SIZE
+ || msg->num_opts == NMRP_MAX_OPT_NUM) {
+ fprintf(stderr, "Invalid option - this is a bug.\n");
+ }
+
+ for (; i != msg->num_opts; ++i) {
+ opt = (struct nmrp_opt*)(((char*)opt) + msg->len);
+ }
+
+ opt->len = NMRP_OPT_HDR_LEN + len;
+ opt->type = type;
+
+ if (len) {
+ memcpy(&opt->val, data, len);
+ }
+
+ ++msg->num_opts;
+
+ return true;
+}
+
static int pkt_send(struct ethsock *sock, struct nmrp_pkt *pkt)
{
size_t len = ntohs(pkt->msg.len) + sizeof(pkt->eh);
return 0;
}
+struct is_valid_ip_arg
+{
+ struct in_addr *ipaddr;
+ struct in_addr *ipmask;
+ int result;
+};
+
+static int is_valid_ip_cb(struct ethsock_ip_callback_args *args)
+{
+#define SUBNET(x) ((x)->ipaddr->s_addr & (x)->ipmask->s_addr)
+ struct is_valid_ip_arg *arg = args->arg;
+ if (SUBNET(args) == SUBNET(arg)) {
+ arg->result = args->ipaddr->s_addr != arg->ipaddr->s_addr;
+ return 0;
+ }
+
+ return 1;
+#undef SUBNET
+}
+
+static int is_valid_ip(struct ethsock *sock, struct in_addr *ipaddr,
+ struct in_addr *ipmask)
+{
+ int status;
+ struct is_valid_ip_arg arg = {
+ .ipaddr = ipaddr,
+ .ipmask = ipmask,
+ .result = 0
+ };
+
+ status = ethsock_for_each_ip(sock, is_valid_ip_cb, &arg);
+ return status < 0 ? status : arg.result;
+}
+
static struct ethsock *gsock = NULL;
static void sigh(int sig)
{
struct nmrp_pkt tx, rx;
uint8_t *src, dest[6];
+ uint16_t len;
+ char *filename;
struct in_addr ipaddr, ipmask;
time_t beg;
- int i, err, ulreqs, expect;
+ int i, status, ulreqs, expect, upload_ok;
struct ethsock *sock;
void (*sigh_orig)(int);
return 1;
}
- if (access(args->filename, R_OK) == -1) {
- fprintf(stderr, "Error accessing file '%s'.\n", args->filename);
+ if (strcmp(args->file_local, "-") && access(args->file_local, R_OK) == -1) {
+ fprintf(stderr, "Error accessing file '%s'.\n", args->file_local);
return 1;
}
- err = 1;
+ if (args->file_remote) {
+ if (!tftp_is_valid_filename(args->file_remote)) {
+ fprintf(stderr, "Invalid remote filename '%s'.\n",
+ args->file_remote);
+ return 1;
+ }
+ }
+
+ status = 1;
sock = ethsock_create(args->intf, ETH_P_NMRP);
if (!sock) {
return 1;
}
+ status = is_valid_ip(sock, &ipaddr, &ipmask);
+ 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;
+ }
+
gsock = sock;
sigh_orig = signal(SIGINT, sigh);
tx.msg.reserved = 0;
tx.msg.code = NMRP_C_ADVERTISE;
tx.msg.id = 0;
- tx.msg.num_opts = 1;
+ tx.msg.num_opts = 0;
+
tx.msg.opts[0].type = NMRP_O_MAGIC_NO;
- tx.msg.opts[0].len = NMRP_OPT_LEN + 4;
+ tx.msg.opts[0].len = NMRP_OPT_HDR_LEN + 4;
tx.msg.opts[0].val.magic[0] = 'N';
tx.msg.opts[0].val.magic[1] = 'T';
tx.msg.opts[0].val.magic[2] = 'G';
msg_hton(&tx.msg);
i = 0;
+ upload_ok = 0;
beg = time(NULL);
while (1) {
- printf("\rAdvertising NMRP server on interface ... %c",
- spinner[i]);
+ printf("\rAdvertising NMRP server on %s ... %c",
+ args->intf, spinner[i]);
fflush(stdout);
i = (i + 1) & 3;
goto out;
}
- err = pkt_recv(sock, &rx);
- if (err == 0 && memcmp(rx.eh.ether_dhost, src, 6) == 0) {
+ status = pkt_recv(sock, &rx);
+ if (status == 0 && memcmp(rx.eh.ether_dhost, src, 6) == 0) {
break;
- } else if (err == 1) {
+ } else if (status == 1) {
printf("ERR\n");
goto out;
} else {
do {
if (expect != NMRP_C_NONE && rx.msg.code != expect) {
- fprintf(stderr, "Received code 0x%02x while waiting for 0x%02x!\n",
- rx.msg.code, expect);
+ fprintf(stderr, "Received %s while waiting for %s!\n",
+ msg_code_str(rx.msg.code), msg_code_str(expect));
}
tx.msg.code = NMRP_C_NONE;
tx.msg.num_opts = 0;
tx.msg.len = 0;
- err = 1;
+ status = 1;
switch (rx.msg.code) {
case NMRP_C_ADVERTISE:
printf("Received NMRP advertisement from %s.\n",
mac_to_str(rx.eh.ether_shost));
- err = 1;
+ status = 1;
goto out;
case NMRP_C_CONF_REQ:
tx.msg.code = NMRP_C_CONF_ACK;
tx.msg.num_opts = 2;
tx.msg.opts[0].type = NMRP_O_DEV_IP;
- tx.msg.opts[0].len = NMRP_OPT_LEN + 2 * 4;
+ tx.msg.opts[0].len = NMRP_OPT_HDR_LEN + 2 * 4;
memcpy(tx.msg.opts[0].val.ip.addr, &ipaddr, 4);
memcpy(tx.msg.opts[0].val.ip.mask, &ipmask, 4);
tx.msg.opts[1].type = NMRP_O_FW_UP;
- tx.msg.opts[1].len = NMRP_OPT_LEN;
+ tx.msg.opts[1].len = NMRP_OPT_HDR_LEN;
+
+#ifdef NMRPFLASH_SET_REGION
+ tx.msg.num_opts = 3;
+
+ tx.msg.opts[2].type = NMRP_O_DEV_REGION;
+ tx.msg.opts[2].len = NMRP_OPT_HDR_LEN + 2;
+ tx.msg.opts[2].val.region = args->region;
+#endif
expect = NMRP_C_TFTP_UL_REQ;
break;
case NMRP_C_TFTP_UL_REQ:
- if (++ulreqs > 5) {
- fprintf(stderr, "Device re-requested file upload %d "
- "times; aborting.\n", ulreqs);
- tx.msg.code = NMRP_C_CLOSE_REQ;
+ if (!upload_ok) {
+ if (++ulreqs > 5) {
+ printf("Bailing out after %d upload requests.\n",
+ ulreqs);
+ tx.msg.code = NMRP_C_CLOSE_REQ;
+ break;
+ }
+ } else {
+ if (verbosity) {
+ printf("Ignoring extra upload request.\n");
+ }
+ ethsock_set_timeout(sock, args->ul_timeout);
+ tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
break;
}
- if (!args->tftpcmd) {
- printf("Uploading %s ... ", args->filename);
+ len = 0;
+ filename = msg_opt_data(&rx.msg, NMRP_O_FILE_NAME, &len);
+ if (filename) {
+ if (!args->file_remote) {
+ args->file_remote = filename;
+ }
+ printf("Received upload request: filename '%.*s'.\n",
+ len, filename);
+ } else if (!args->file_remote) {
+ args->file_remote = args->file_local;
+ printf("Received upload request with empty filename.");
+ }
+
+ status = 0;
+
+ if (args->tftpcmd) {
+ printf("Executing '%s' ... ", args->tftpcmd);
fflush(stdout);
- err = tftp_put(args);
- } else {
- printf("Running %s ... ", args->tftpcmd);
+ status = system(args->tftpcmd);
+ if (!status) {
+ printf("OK\n");
+ } else {
+ printf("ERR\n");
+ }
+ }
+
+ if (!status && args->file_local) {
+ status = is_valid_ip(sock, &ipaddr, &ipmask);
+ 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) {
+ printf("Using remote filename '%s'.\n",
+ args->file_remote);
+ }
+
+ if (!strcmp(args->file_local, "-")) {
+ printf("Uploading from stdin ... ");
+ } else {
+ printf("Uploading %s ... ", leafname(args->file_local));
+ }
fflush(stdout);
- err = system(args->tftpcmd);
+ status = tftp_put(args);
}
- if (!err) {
+ if (!status) {
printf("OK\nWaiting for remote to respond.\n");
+ upload_ok = 1;
ethsock_set_timeout(sock, args->ul_timeout);
- expect = NMRP_C_CLOSE_REQ;
- } else if (err == -2) {
+ tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
+ expect = NMRP_C_NONE;
+ } else if (status == -2) {
expect = NMRP_C_TFTP_UL_REQ;
} else {
goto out;
break;
case NMRP_C_KEEP_ALIVE_REQ:
tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
+ ethsock_set_timeout(sock, 15000);
+ printf("Received keep-alive request.\n");
break;
case NMRP_C_CLOSE_REQ:
tx.msg.code = NMRP_C_CLOSE_ACK;
break;
case NMRP_C_CLOSE_ACK:
- err = 0;
+ status = 0;
goto out;
default:
fprintf(stderr, "Unknown message code 0x%02x!\n",
break;
}
- err = pkt_recv(sock, &rx);
- if (err) {
- if (err == 2) {
- fprintf(stderr, "Timeout while waiting for 0x%02x.\n", expect);
+ status = pkt_recv(sock, &rx);
+ if (status) {
+ if (status == 2) {
+ fprintf(stderr, "Timeout while waiting for %s.\n",
+ msg_code_str(expect));
}
goto out;
}
} while (1);
- err = 0;
+ 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_close(sock);
- return err;
+ return status;
}