2019-11-03 22:13:47 +01:00
|
|
|
/****************************************************************************
|
2021-06-11 12:06:42 +02:00
|
|
|
* apps/netutils/netlib/netlib_getarptab.c
|
2019-11-03 22:13:47 +01:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2019-11-03 22:13:47 +01:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-11-03 22:13:47 +01:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2019-11-03 22:13:47 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <stdlib.h>
|
2019-11-07 21:08:03 +01:00
|
|
|
#include <unistd.h>
|
2019-11-03 22:13:47 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <netpacket/netlink.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/arp.h>
|
|
|
|
|
|
|
|
#include "netutils/netlib.h"
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_ARP) && defined(CONFIG_NETLINK_ROUTE)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct netlib_sendto_request_s
|
|
|
|
{
|
|
|
|
struct nlmsghdr hdr;
|
2020-04-09 14:14:55 +02:00
|
|
|
struct rtgenmsg gen;
|
2019-11-03 22:13:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct netlib_recvfrom_response_s
|
|
|
|
{
|
|
|
|
struct nlmsghdr hdr;
|
|
|
|
struct ndmsg msg;
|
|
|
|
struct rtattr attr;
|
|
|
|
uint8_t data[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SIZEOF_NETLIB_RECVFROM_RESPONSE_S(n) \
|
|
|
|
(sizeof(struct netlib_recvfrom_response_s) + (n) - 1)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlib_get_arptable
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Attempt to read the entire ARP table into a buffer.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* arptab - The location to store the copy of the ARP table
|
2019-11-04 19:31:13 +01:00
|
|
|
* nentries - The size of the provided 'arptab' in number of entries each
|
|
|
|
* of size sizeof(struct arp_entry_s)
|
2019-11-03 22:13:47 +01:00
|
|
|
*
|
|
|
|
* Return:
|
2019-11-04 19:31:13 +01:00
|
|
|
* The number of ARP table entries read is returned on success; a negated
|
2019-11-03 23:19:14 +01:00
|
|
|
* errno value is returned on failure.
|
2019-11-03 22:13:47 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-11-08 18:30:32 +01:00
|
|
|
ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab,
|
|
|
|
unsigned int nentries)
|
2019-11-03 22:13:47 +01:00
|
|
|
{
|
|
|
|
FAR struct netlib_recvfrom_response_s *resp;
|
|
|
|
struct netlib_sendto_request_s req;
|
2019-11-04 02:29:20 +01:00
|
|
|
struct sockaddr_nl addr;
|
|
|
|
static unsigned int seqno = 0;
|
|
|
|
unsigned int thiseq;
|
2019-11-03 22:13:47 +01:00
|
|
|
unsigned int allocsize;
|
|
|
|
ssize_t nsent;
|
|
|
|
ssize_t nrecvd;
|
|
|
|
ssize_t paysize;
|
|
|
|
ssize_t maxsize;
|
2019-11-10 00:38:02 +01:00
|
|
|
pid_t pid;
|
2019-11-03 22:13:47 +01:00
|
|
|
int fd;
|
2019-11-03 23:19:14 +01:00
|
|
|
int ret;
|
2019-11-03 22:13:47 +01:00
|
|
|
|
2019-11-04 02:29:20 +01:00
|
|
|
/* Pre-allocate a buffer to hold the response */
|
2019-11-03 22:13:47 +01:00
|
|
|
|
|
|
|
maxsize = CONFIG_NET_ARPTAB_SIZE * sizeof(struct arp_entry_s);
|
|
|
|
allocsize = SIZEOF_NETLIB_RECVFROM_RESPONSE_S(maxsize);
|
|
|
|
resp = (FAR struct netlib_recvfrom_response_s *)malloc(allocsize);
|
|
|
|
if (resp == NULL)
|
|
|
|
{
|
2019-11-04 02:29:20 +01:00
|
|
|
fprintf(stderr, "ERROR: Failed to allocate response buffer\n");
|
2019-11-03 23:19:14 +01:00
|
|
|
ret = -ENOMEM;
|
2019-11-03 22:13:47 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-11-04 19:31:13 +01:00
|
|
|
/* Create a NetLink socket with NETLINK_ROUTE protocol */
|
2019-11-03 22:13:47 +01:00
|
|
|
|
|
|
|
fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr, "ERROR: socket() failed: %d\n", errcode);
|
2019-11-03 23:19:14 +01:00
|
|
|
ret = -errcode;
|
2019-11-03 22:13:47 +01:00
|
|
|
goto errout_with_resp;
|
|
|
|
}
|
|
|
|
|
2019-11-04 02:29:20 +01:00
|
|
|
/* Bind the socket so that we can use send() and receive() */
|
|
|
|
|
2019-11-10 00:38:02 +01:00
|
|
|
pid = getpid();
|
2019-11-04 02:29:20 +01:00
|
|
|
addr.nl_family = AF_NETLINK;
|
2019-11-07 21:08:03 +01:00
|
|
|
addr.nl_pad = 0;
|
2019-11-10 00:38:02 +01:00
|
|
|
addr.nl_pid = pid;
|
2020-04-09 12:37:04 +02:00
|
|
|
addr.nl_groups = 0;
|
2019-11-04 02:29:20 +01:00
|
|
|
|
2019-11-07 21:08:03 +01:00
|
|
|
ret = bind(fd, (FAR const struct sockaddr *)&addr,
|
2020-04-10 14:27:58 +02:00
|
|
|
sizeof(struct sockaddr_nl));
|
2019-11-11 20:40:57 +01:00
|
|
|
if (ret < 0)
|
2019-11-04 02:29:20 +01:00
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr, "ERROR: bind() failed: %d\n", errcode);
|
|
|
|
ret = -errcode;
|
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
|
2019-11-03 22:13:47 +01:00
|
|
|
/* Initialize the request */
|
|
|
|
|
2019-11-04 02:29:20 +01:00
|
|
|
thiseq = ++seqno;
|
|
|
|
|
2019-11-03 22:13:47 +01:00
|
|
|
memset(&req, 0, sizeof(req));
|
2020-04-09 14:14:55 +02:00
|
|
|
req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
|
|
|
|
req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
|
|
|
|
req.hdr.nlmsg_seq = thiseq;
|
|
|
|
req.hdr.nlmsg_type = RTM_GETNEIGH;
|
|
|
|
req.hdr.nlmsg_pid = pid;
|
|
|
|
req.gen.rtgen_family = AF_INET;
|
2019-11-03 22:13:47 +01:00
|
|
|
|
|
|
|
nsent = send(fd, &req, req.hdr.nlmsg_len, 0);
|
|
|
|
if (nsent < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr, "ERROR: send() failed: %d\n", errcode);
|
2019-11-03 23:19:14 +01:00
|
|
|
ret = -errcode;
|
2019-11-03 22:13:47 +01:00
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
|
2019-11-04 16:04:17 +01:00
|
|
|
/* Read the response */
|
2019-11-04 02:29:20 +01:00
|
|
|
|
2019-11-03 22:13:47 +01:00
|
|
|
nrecvd = recv(fd, resp, allocsize, 0);
|
|
|
|
if (nrecvd < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr, "ERROR: recv() failed: %d\n", errcode);
|
2019-11-03 23:19:14 +01:00
|
|
|
ret = -errcode;
|
2019-11-03 22:13:47 +01:00
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify the data and transfer the ARP table data to the caller */
|
|
|
|
|
|
|
|
if (resp->hdr.nlmsg_len < sizeof(struct nlmsghdr) ||
|
|
|
|
resp->hdr.nlmsg_len > nrecvd)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Bad message\n");
|
2019-11-03 23:19:14 +01:00
|
|
|
ret = -EIO;
|
2019-11-03 22:13:47 +01:00
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
|
2020-04-10 14:27:58 +02:00
|
|
|
/* The sequence number in the response should match the sequence
|
|
|
|
* number in the request (since we created the socket, this should
|
|
|
|
* always be true).
|
|
|
|
*/
|
2019-11-04 16:04:17 +01:00
|
|
|
|
2020-04-10 14:27:58 +02:00
|
|
|
if (resp->hdr.nlmsg_seq != thiseq)
|
|
|
|
{
|
2019-11-04 16:04:17 +01:00
|
|
|
fprintf(stderr, "ERROR: Bad sequence number in response\n");
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout_with_socket;
|
2020-04-10 14:27:58 +02:00
|
|
|
}
|
2019-11-04 16:04:17 +01:00
|
|
|
|
2019-11-03 22:13:47 +01:00
|
|
|
/* Copy the ARP table data to the caller's buffer */
|
|
|
|
|
2019-11-10 19:36:34 +01:00
|
|
|
paysize = RTA_PAYLOAD(&resp->attr);
|
2019-11-03 22:13:47 +01:00
|
|
|
if (paysize > maxsize)
|
|
|
|
{
|
|
|
|
paysize = maxsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(arptab, resp->data, paysize);
|
2019-11-03 23:19:14 +01:00
|
|
|
ret = paysize / sizeof(struct arp_entry_s);
|
2019-11-03 22:13:47 +01:00
|
|
|
|
|
|
|
errout_with_socket:
|
|
|
|
close(fd);
|
2019-11-04 02:29:20 +01:00
|
|
|
|
2019-11-03 22:13:47 +01:00
|
|
|
errout_with_resp:
|
|
|
|
free(resp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_ARP && CONFIG_NETLINK_ROUTE */
|