#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
-#include <stdint.h>
+#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
+#include "nmrpd.h"
#define NMRP_HDR_LEN 6
#define NMRP_OPT_LEN 4
-#define NMRP_MAX_OPT 6
#define NMRP_MIN_PKT_LEN (sizeof(struct ether_header) + NMRP_HDR_LEN)
#define ETH_P_NMRP 0x0912
#define PACKED __attribute__((__packed__))
#define MAX_LOOP_RECV 1024
-#define IS_OOO_CODE(x) (x == NMRP_C_CLOSE_REQ \
- || x == NMRP_C_KEEP_ALIVE_REQ \
- || x == NMRP_C_TFTP_UL_REQ)
-
-extern int tftp_put(const char *filename, const char *ipaddr, uint16_t port);
-extern int sock_set_rx_timeout(int fd, unsigned msec);
-
enum nmrp_code {
NMRP_C_NONE = 0,
NMRP_C_ADVERTISE = 1,
uint8_t code;
uint8_t id;
uint16_t len;
- struct nmrp_opt opts[6];
+ struct nmrp_opt opts[2];
uint32_t num_opts;
} PACKED;
}
}
+static void msg_dump(struct nmrp_msg *msg, int dump_opts)
+{
+ struct nmrp_opt *opt;
+ int remain_len, len, i;
+
+ 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);
+ for (i = 0; i != len - NMRP_OPT_LEN; ++i) {
+ if (!(i % 16)) {
+ fprintf(stderr, "\n ");
+ }
+
+ fprintf(stderr, "%02x ", ((char*)&opt->val)[i] & 0xff);
+ }
+ fprintf(stderr, "\n");
+ remain_len -= len;
+ opt = (struct nmrp_opt*)(((char*)opt) + len);
+ }
+ }
+}
+
static void msg_hton(struct nmrp_msg *msg)
{
uint32_t i = 0;
while (remaining > 0) {
if (remaining < NMRP_OPT_LEN) {
- fprintf(stderr, "malformed message (rem=%d)\n", remaining);
+ fprintf(stderr, "Malformed message.\n");
+ msg_dump(msg, 0);
return 1;
}
opt->len = ntohs(opt->len);
remaining -= opt->len;
+ ++opt;
}
- return 0;
-}
-
-static void msg_dump(struct nmrp_msg *msg)
-{
- struct nmrp_opt *opt;
- int remain_len, len, i;
-
- printf("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;
- printf("%s\n", remain_len ? "" : " (no opts)");
-
- opt = msg->opts;
-
- while (remain_len > 0) {
- len = opt->len;
- printf(" opt type=%u, len=%u", opt->type, len);
- for (i = 0; i != len - NMRP_OPT_LEN; ++i) {
- if (!(i % 16)) {
- printf("\n ");
- }
-
- printf("%02x ", ((char*)&opt->val)[i] & 0xff);
- }
- printf("\n");
- remain_len -= len;
- opt = (struct nmrp_opt*)(((char*)opt) + len);
+ if (remaining) {
+ fprintf(stderr, "Trailing data in message.\n");
+ msg_dump(msg, 0);
+ return 1;
}
+
+ return 0;
}
-static int intf_get_index_and_addr(int fd, const char *name, int *index,
+static int intf_get_info(int sock, const char *name, int *index,
uint8_t *hwaddr)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
- if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
+ if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) {
perror("ioctl(SIOCGIFINDEX)");
return -1;
}
*index = ifr.ifr_ifindex;
- if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
+ if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
perror("ioctl(SIOCGIFHWADDR)");
return -1;
}
return 0;
}
-static int pkt_send(int fd, struct sockaddr_ll *addr, struct nmrp_pkt *pkt)
+static int pkt_send(int sock, struct sockaddr_ll *addr, struct nmrp_pkt *pkt)
{
size_t len = ntohs(pkt->msg.len) + sizeof(pkt->eh);
- return sendto(fd, pkt, len, 0, (struct sockaddr*)addr, sizeof(*addr));
+ return sendto(sock, pkt, len, 0, (struct sockaddr*)addr, sizeof(*addr));
}
-static int pkt_recv(int fd, struct nmrp_pkt *pkt)
+static int pkt_recv(int sock, struct nmrp_pkt *pkt)
{
struct sockaddr_ll from;
socklen_t addrlen;
ssize_t bytes, len;
memset(pkt, 0, sizeof(*pkt));
- bytes = recvfrom(fd, pkt, NMRP_MIN_PKT_LEN, MSG_PEEK,
+ bytes = recvfrom(sock, pkt, NMRP_MIN_PKT_LEN, MSG_PEEK,
(struct sockaddr*)&from, &addrlen);
if (bytes < 0) {
} else if (ntohs(pkt->eh.ether_type) != ETH_P_NMRP) {
return 3;
} else if (bytes < NMRP_MIN_PKT_LEN) {
- fprintf(stderr, "short packet (%zi bytes)\n", bytes);
+ fprintf(stderr, "Short packet (%zi bytes)\n", bytes);
return 1;
}
msg_hdr_ntoh(&pkt->msg);
len = pkt->msg.len + sizeof(pkt->eh);
- bytes = recvfrom(fd, pkt, len, MSG_DONTWAIT, NULL, NULL);
+ bytes = recvfrom(sock, pkt, len, MSG_DONTWAIT, NULL, NULL);
if (bytes < 0) {
perror("recvfrom(msg)");
return 1;
} else if (bytes != len) {
- fprintf(stderr, "short message (%zi bytes)\n", len);
+ fprintf(stderr, "Unexpected message length (%zi bytes).\n", len);
return 1;
} else {
if (msg_ntoh(&pkt->msg) != 0) {
return 1;
}
- msg_dump(&pkt->msg);
-
return 0;
}
return 1;
}
-static int sock_bind_to_intf(int fd, const char *name)
+static int sock_bind_to_intf(int sock, const char *name)
{
struct ifreq ifr;
strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
- if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0) {
+ if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0) {
perror("setsockopt(SO_BINDTODEVICE)");
return 1;
}
return 0;
}
-//static const char *arg_filename = "EX2700-V1.0.1.8.img";
-static unsigned arg_rx_timeout = 250;
-static unsigned arg_ul_timeout = 60000;
-static const char *arg_filename = "bad.img";
-static const char *arg_ipaddr = "192.168.2.2";
-static const char *arg_ipmask = "255.255.255.0";
-static const char *arg_intf = "enp4s0";
-static uint16_t arg_port = 69;
-#if 0
-static uint8_t target[ETH_ALEN] = { 0xa4, 0x2b, 0x8c, 0x10, 0xc2, 0x96 };
-#else
-static uint8_t target[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
-#endif
+static int mac_parse(const char *str, uint8_t *hwaddr)
+{
+ unsigned i, data[6];
+
+ i = sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
+ data, data + 1, data + 2, data + 3, data + 4, data + 5);
+
+ if (i == 6) {
+ for (i = 0; i != 6; ++i) {
+ if (data[i] > 255) {
+ break;
+ }
+
+ hwaddr[i] = data[i] & 0xff;
+ }
+
+ if (i == 6) {
+ return 1;
+ }
+ }
+
+ fprintf(stderr, "Invalid MAC address.\n");
+ return 0;
+}
static const char *spinner = "\\|/-";
-int main(int argc, char **argv)
+int nmrp_do(struct nmrpd_args *args)
{
struct nmrp_pkt tx, rx;
struct sockaddr_ll addr;
- uint8_t hwaddr[ETH_ALEN];
- int i, fd, err, ulreqs, expect;
+ uint8_t src[ETH_ALEN], dest[ETH_ALEN];
+ struct in_addr ipaddr, ipmask;
+ int i, sock, err, ulreqs, expect;
+
+ if (args->op != NMRP_UPLOAD_FW) {
+ fprintf(stderr, "Operation not implemented.\n");
+ return 1;
+ }
+
+ if (!mac_parse(args->mac, dest)) {
+ fprintf(stderr, "Invalid MAC address %s.\n", args->mac);
+ return 1;
+ }
+
+ if (!inet_aton(args->ipaddr, &ipaddr)) {
+ fprintf(stderr, "Invalid IP address %s.\n", args->ipaddr);
+ return 1;
+ }
+
+ if (!inet_aton(args->ipmask, &ipmask)) {
+ fprintf(stderr, "Invalid subnet mask %s.\n", args->ipmask);
+ return 1;
+ }
+
+ if (access(args->filename, R_OK) == -1) {
+ fprintf(stderr, "Error accessing file %s.\n", args->filename);
+ return 1;
+ }
err = 1;
- fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_NMRP));
- if (fd == -1) {
+ sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_NMRP));
+ if (sock == -1) {
perror("socket");
return 1;
}
- if (intf_get_index_and_addr(fd, arg_intf, &addr.sll_ifindex, hwaddr)) {
+ if (intf_get_info(sock, args->intf, &addr.sll_ifindex, src)) {
return 1;
}
- if (sock_bind_to_intf(fd, arg_intf)) {
+ if (sock_bind_to_intf(sock, args->intf)) {
return 1;
}
- if (sock_set_rx_timeout(fd, arg_rx_timeout)) {
+ if (sock_set_rx_timeout(sock, args->rx_timeout)) {
return 1;
}
- addr.sll_family = PF_PACKET;
- //addr.sll_hatype = ARPHRD_ETHER;
- //addr.sll_pkttype = PACKET_OTHERHOST;
+ addr.sll_family = AF_PACKET;
addr.sll_protocol = htons(ETH_P_NMRP);
addr.sll_halen = ETH_ALEN;
- memcpy(addr.sll_addr, target, ETH_ALEN);
+ memcpy(addr.sll_addr, dest, ETH_ALEN);
- memcpy(tx.eh.ether_shost, hwaddr, ETH_ALEN);
- memcpy(tx.eh.ether_dhost, target, ETH_ALEN);
+ memcpy(tx.eh.ether_shost, src, ETH_ALEN);
+ memcpy(tx.eh.ether_dhost, dest, ETH_ALEN);
tx.eh.ether_type = htons(ETH_P_NMRP);
tx.msg.reserved = 0;
i = 0;
while (1) {
- printf("\rAdvertising NMRP server on %s ... %c", arg_intf, spinner[i]);
+ printf("\rAdvertising NMRP server on %s ... %c", args->intf,
+ spinner[i]);
fflush(stdout);
i = (i + 1) & 3;
- if (pkt_send(fd, &addr, &tx) < 0) {
+ if (pkt_send(sock, &addr, &tx) < 0) {
perror("sendto");
goto out;
}
- err = pkt_recv(fd, &rx);
+ err = pkt_recv(sock, &rx);
if (err == 0) {
break;
} else if (err == 1) {
tx.msg.opts[0].type = NMRP_O_DEV_IP;
tx.msg.opts[0].len = NMRP_OPT_LEN + 2 * IP_LEN;
- inet_aton(arg_ipaddr,
- (struct in_addr*)tx.msg.opts[0].val.ip.addr);
- inet_aton(arg_ipmask,
- (struct in_addr*)tx.msg.opts[0].val.ip.mask);
+ memcpy(tx.msg.opts[0].val.ip.addr, &ipaddr, IP_LEN);
+ memcpy(tx.msg.opts[0].val.ip.mask, &ipmask, IP_LEN);
tx.msg.opts[1].type = NMRP_O_FW_UP;
tx.msg.opts[1].len = NMRP_OPT_LEN;
rx.eh.ether_shost[0], rx.eh.ether_shost[1],
rx.eh.ether_shost[2], rx.eh.ether_shost[3],
rx.eh.ether_shost[4], rx.eh.ether_shost[5]);
- printf("Sending configuration: ip %s, mask %s.\n", arg_ipaddr,
- arg_ipmask);
+
+ memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, ETH_ALEN);
+ memcpy(addr.sll_addr, rx.eh.ether_shost, ETH_ALEN);
+
+ printf("Sending configuration: ip %s, mask %s.\n",
+ args->ipaddr, args->ipmask);
break;
case NMRP_C_TFTP_UL_REQ:
tx.msg.code = NMRP_C_CLOSE_REQ;
break;
}
- printf("Uploading %s ... ", arg_filename);
- fflush(stdout);
- err = tftp_put(arg_filename, arg_ipaddr, arg_port);
+
+ if (!args->tftpcmd) {
+ printf("Uploading %s ... ", args->filename);
+ fflush(stdout);
+ err = tftp_put(args);
+ } else {
+ printf("Running %s ... ", args->tftpcmd);
+ fflush(stdout);
+ err = system(args->tftpcmd);
+ }
+
if (!err) {
printf("OK\nWaiting for router to respond.\n");
- sock_set_rx_timeout(fd, arg_ul_timeout);
+ sock_set_rx_timeout(sock, args->ul_timeout);
expect = NMRP_C_CLOSE_REQ;
- } else if (err != -3) {
+ } else {
+ printf("\n");
goto out;
}
+
break;
case NMRP_C_KEEP_ALIVE_REQ:
tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
err = 0;
goto out;
default:
- fprintf(stderr, "Unhandled message code 0x%02x!\n",
+ fprintf(stderr, "Unknown message code 0x%02x!\n",
rx.msg.code);
+ msg_dump(&rx.msg, 0);
}
if (tx.msg.code != NMRP_C_NONE) {
msg_update_len(&tx.msg);
msg_hton(&tx.msg);
- if (pkt_send(fd, &addr, &tx) < 0) {
+ if (pkt_send(sock, &addr, &tx) < 0) {
perror("sendto");
goto out;
}
break;
}
- err = pkt_recv(fd, &rx);
+ err = pkt_recv(sock, &rx);
if (err) {
if (err == 2) {
fprintf(stderr, "Timeout while waiting for 0x%02x.\n", expect);
goto out;
}
- sock_set_rx_timeout(fd, arg_rx_timeout);
+ sock_set_rx_timeout(sock, args->rx_timeout);
} while (1);
err = 0;
out:
- close(fd);
+ close(sock);
return err;
}