net/netlink/: Misc bugfixes from initial testing using NSH 'arp -t' command.

This commit is contained in:
Gregory Nutt
2019-11-03 19:27:58 -06:00
parent 3439720739
commit 7bd045130c
3 changed files with 61 additions and 41 deletions
+3
View File
@@ -439,6 +439,8 @@ void arp_hdr_update(FAR uint16_t *pipaddr, FAR uint8_t *ethaddr);
#ifdef CONFIG_NETLINK_ROUTE #ifdef CONFIG_NETLINK_ROUTE
unsigned int arp_snapshot(FAR struct arp_entry_s *snapshot, unsigned int arp_snapshot(FAR struct arp_entry_s *snapshot,
unsigned int nentries); unsigned int nentries);
#else
# define arp_snapshot(s,n) (0)
#endif #endif
/**************************************************************************** /****************************************************************************
@@ -476,6 +478,7 @@ void arp_dump(FAR struct arp_hdr_s *arp);
# define arp_delete(i) # define arp_delete(i)
# define arp_update(i,m); # define arp_update(i,m);
# define arp_hdr_update(i,m); # define arp_hdr_update(i,m);
# define arp_snapshot(s,n) (0)
# define arp_dump(arp) # define arp_dump(arp)
#endif /* CONFIG_NET_ARP */ #endif /* CONFIG_NET_ARP */
+48 -35
View File
@@ -86,7 +86,6 @@ struct nlroute_sendto_request_s
struct nlroute_recvfrom_response_s struct nlroute_recvfrom_response_s
{ {
FAR struct netlink_reqdata_s *flink;
struct nlmsghdr hdr; struct nlmsghdr hdr;
struct ndmsg msg; struct ndmsg msg;
struct rtattr attr; struct rtattr attr;
@@ -96,6 +95,15 @@ struct nlroute_recvfrom_response_s
#define SIZEOF_NLROUTE_RECVFROM_RESPONSE_S(n) \ #define SIZEOF_NLROUTE_RECVFROM_RESPONSE_S(n) \
(sizeof(struct nlroute_recvfrom_response_s) + (n) - 1) (sizeof(struct nlroute_recvfrom_response_s) + (n) - 1)
struct nlroute_recvfrom_rsplist_s
{
FAR struct netlink_reqdata_s *flink;
struct nlroute_recvfrom_response_s payload;
};
#define SIZEOF_NLROUTE_RECVFROM_RSPLIST_S(n) \
(sizeof(struct nlroute_recvfrom_rsplist_s) + (n) - 1)
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
@@ -131,11 +139,11 @@ ssize_t netlink_route_sendto(FAR struct socket *psock,
if (req->hdr.nlmsg_type == RTM_GETNEIGH && req->msg.ndm_family == AF_INET) if (req->hdr.nlmsg_type == RTM_GETNEIGH && req->msg.ndm_family == AF_INET)
{ {
FAR struct nlroute_recvfrom_response_s *entry; FAR struct nlroute_recvfrom_rsplist_s *entry;
unsigned int ncopied; unsigned int ncopied;
size_t tabsize; size_t tabsize;
size_t paysize; size_t rspsize;
size_t entsize; size_t allocsize;
/* Preallocate memory to hold the maximum sized ARP table /* Preallocate memory to hold the maximum sized ARP table
* REVISIT: This is probably excessively large and could cause false * REVISIT: This is probably excessively large and could cause false
@@ -143,11 +151,11 @@ ssize_t netlink_route_sendto(FAR struct socket *psock,
* the number of valid entries in the ARP table. * the number of valid entries in the ARP table.
*/ */
tabsize = CONFIG_NET_ARPTAB_SIZE * sizeof( struct arp_entry_s); tabsize = CONFIG_NET_ARPTAB_SIZE * sizeof( struct arp_entry_s);
paysize = SIZEOF_NLROUTE_RECVFROM_RESPONSE_S(tabsize); rspsize = SIZEOF_NLROUTE_RECVFROM_RESPONSE_S(tabsize);
entsize = SIZEOF_NETLINK_RESPONSE_S(paysize); allocsize = SIZEOF_NLROUTE_RECVFROM_RSPLIST_S(tabsize);
entry = (FAR struct nlroute_recvfrom_response_s *)kmm_malloc(entsize); entry = (FAR struct nlroute_recvfrom_rsplist_s *)kmm_malloc(allocsize);
if (entry == NULL) if (entry == NULL)
{ {
return -ENOMEM; return -ENOMEM;
@@ -155,18 +163,18 @@ ssize_t netlink_route_sendto(FAR struct socket *psock,
/* Populate the entry */ /* Populate the entry */
memcpy(&entry->hdr, &req->hdr, sizeof(struct nlmsghdr)); memcpy(&entry->payload.hdr, &req->hdr, sizeof(struct nlmsghdr));
entry->hdr.nlmsg_len = paysize; entry->payload.hdr.nlmsg_len = rspsize;
memcpy(&entry->msg, &req->msg, sizeof(struct ndmsg)); memcpy(&entry->payload.msg, &req->msg, sizeof(struct ndmsg));
entry->attr.rta_len = tabsize; entry->payload.attr.rta_len = tabsize;
entry->attr.rta_type = 0; entry->payload.attr.rta_type = 0;
/* Lock the network so that the ARP table will be stable, then copy /* Lock the network so that the ARP table will be stable, then copy
* the ARP table into the allocated memory. * the ARP table into the allocated memory.
*/ */
net_lock(); net_lock();
ncopied = arp_snapshot((FAR struct arp_entry_s *)entry->data, ncopied = arp_snapshot((FAR struct arp_entry_s *)entry->payload.data,
CONFIG_NET_ARPTAB_SIZE); CONFIG_NET_ARPTAB_SIZE);
net_unlock(); net_unlock();
@@ -176,28 +184,28 @@ ssize_t netlink_route_sendto(FAR struct socket *psock,
if (ncopied < CONFIG_NET_ARPTAB_SIZE) if (ncopied < CONFIG_NET_ARPTAB_SIZE)
{ {
FAR struct nlroute_recvfrom_response_s *newentry; FAR struct nlroute_recvfrom_rsplist_s *newentry;
tabsize = ncopied * sizeof( struct arp_entry_s); tabsize = ncopied * sizeof( struct arp_entry_s);
paysize = SIZEOF_NLROUTE_RECVFROM_RESPONSE_S(tabsize); rspsize = SIZEOF_NLROUTE_RECVFROM_RESPONSE_S(tabsize);
entsize = SIZEOF_NETLINK_RESPONSE_S(paysize); allocsize = SIZEOF_NLROUTE_RECVFROM_RSPLIST_S(tabsize);
newentry = (FAR struct nlroute_recvfrom_response_s *) newentry = (FAR struct nlroute_recvfrom_rsplist_s *)
kmm_realloc(entry, entsize); kmm_realloc(entry, allocsize);
if (newentry != NULL) if (newentry != NULL)
{ {
entry = newentry; entry = newentry;
} }
entry->hdr.nlmsg_len = paysize; entry->payload.hdr.nlmsg_len = rspsize;
entry->attr.rta_len = tabsize; entry->payload.attr.rta_len = tabsize;
} }
/* Finally, add the data to the list of pending responses */ /* Finally, add the data to the list of pending responses */
netlink_add_response(psock, (FAR struct netlink_response_s *)entry); netlink_add_response(psock, (FAR struct netlink_response_s *)entry);
return OK; return len;
} }
#else #else
@@ -222,41 +230,46 @@ ssize_t netlink_route_recvfrom(FAR struct socket *psock,
size_t len, int flags, size_t len, int flags,
FAR struct sockaddr_nl *from) FAR struct sockaddr_nl *from)
{ {
FAR struct nlroute_recvfrom_response_s *resp; FAR struct nlroute_recvfrom_rsplist_s *entry;
ssize_t ret;
DEBUGASSERT(psock != NULL && nlmsg != NULL && DEBUGASSERT(psock != NULL && nlmsg != NULL &&
nlmsg->nlmsg_len >= sizeof(struct nlmsghdr) && nlmsg->nlmsg_len >= sizeof(struct nlmsghdr) &&
len >= sizeof(struct nlmsghdr) && len >= sizeof(struct nlmsghdr));
from != NULL);
/* Find the response to this message */ /* Find the response to this message */
net_lock(); net_lock();
resp = (FAR struct nlroute_recvfrom_response_s *) entry = (FAR struct nlroute_recvfrom_rsplist_s *)
netlink_get_response(psock, nlmsg); netlink_get_response(psock, nlmsg);
net_unlock(); net_unlock();
if (resp == NULL) if (entry == NULL)
{ {
return -ENOENT; return -ENOENT;
} }
if (len < resp->hdr.nlmsg_len) if (len < entry->payload.hdr.nlmsg_len)
{ {
kmm_free(resp); kmm_free(entry);
return -EMSGSIZE; return -EMSGSIZE;
} }
memcpy(nlmsg, resp, resp->hdr.nlmsg_len); memcpy(nlmsg, &entry->payload, entry->payload.hdr.nlmsg_len);
/* Return address. REVISIT... this is just a guess. */ /* Return address. REVISIT... this is just a guess. */
from->nl_family = resp->msg.ndm_family; if (from != NULL)
from->nl_pad = 0; {
from->nl_pid = resp->hdr.nlmsg_pid; from->nl_family = entry->payload.msg.ndm_family;
from->nl_groups = resp->hdr.nlmsg_type; from->nl_pad = 0;
from->nl_pid = entry->payload.hdr.nlmsg_pid;
from->nl_groups = entry->payload.hdr.nlmsg_type;
}
return resp->hdr.nlmsg_len; ret = entry->payload.hdr.nlmsg_len;
kmm_free(entry);
return ret;
} }
#endif /* CONFIG_NETLINK_ROUTE */ #endif /* CONFIG_NETLINK_ROUTE */
+10 -6
View File
@@ -593,15 +593,14 @@ static ssize_t netlink_sendto(FAR struct socket *psock, FAR const void *buf,
int ret; int ret;
DEBUGASSERT(psock != NULL && psock->s_conn != NULL && buf != NULL && DEBUGASSERT(psock != NULL && psock->s_conn != NULL && buf != NULL &&
to != NULL && tolen >= sizeof(struct nlmsghdr)); to != NULL && tolen >= sizeof(struct sockaddr_nl));
conn = (FAR struct netlink_conn_s *)psock->s_conn; conn = (FAR struct netlink_conn_s *)psock->s_conn;
/* Get a reference to the netlink message */ /* Get a reference to the netlink message */
nlmsg = (FAR struct nlmsghdr *)buf; nlmsg = (FAR struct nlmsghdr *)buf;
DEBUGASSERT(nlmsg->nlmsg_len >= sizeof(struct nlmsghdr) && DEBUGASSERT(nlmsg->nlmsg_len >= sizeof(struct nlmsghdr));
tolen >= nlmsg->nlmsg_len);
switch (conn->protocol) switch (conn->protocol)
{ {
@@ -652,8 +651,9 @@ static ssize_t netlink_recvfrom(FAR struct socket *psock, FAR void *buf,
FAR struct nlmsghdr *nlmsg; FAR struct nlmsghdr *nlmsg;
int ret; int ret;
DEBUGASSERT(psock != NULL && psock->s_conn != NULL && buf != NULL && DEBUGASSERT(psock != NULL && psock->s_conn != NULL && buf != NULL);
from != NULL && *fromlen >= sizeof(struct nlmsghdr)); DEBUGASSERT(from == NULL ||
(fromlen != NULL && *fromlen >= sizeof(struct sockaddr_nl)));
conn = (FAR struct netlink_conn_s *)psock->s_conn; conn = (FAR struct netlink_conn_s *)psock->s_conn;
@@ -667,7 +667,11 @@ static ssize_t netlink_recvfrom(FAR struct socket *psock, FAR void *buf,
case NETLINK_ROUTE: case NETLINK_ROUTE:
ret = netlink_route_recvfrom(psock, nlmsg, len, flags, ret = netlink_route_recvfrom(psock, nlmsg, len, flags,
(FAR struct sockaddr_nl *)from); (FAR struct sockaddr_nl *)from);
*fromlen = sizeof(struct sockaddr_nl); if (ret >= 0 && fromlen != NULL)
{
*fromlen = sizeof(struct sockaddr_nl);
}
break; break;
#endif #endif