net/packet: add PACKET_<ADD|DROP>_MEMBERSHIP support

Some third-party network libraries use PACKET_ADD_MEMBERSHIP to add MAC
addresses to devices, and this patch can add support for this.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
This commit is contained in:
zhanghongyu
2025-06-09 11:10:58 +08:00
committed by Donny(董九柱)
parent f03e4774a4
commit 1fd47357f7
5 changed files with 90 additions and 4 deletions
+21 -1
View File
@@ -17,8 +17,10 @@ Configuration Options
Dynamic memory allocations for packet connections.
``CONFIG_NET_PKT_MAX_CONNS``
Maximum number of packet connections.
``NET_PKT_WRITE_BUFFERS``
``CONFIG_NET_PKT_WRITE_BUFFERS``
Use write buffers for packet sockets, support SOCK_NONBLOCK mode.
``CONFIG_NET_PKTPROTO_OPTIONS``
Enable setting protocol options on packet sockets.
Usage
=====
@@ -58,3 +60,21 @@ Usage
(struct sockaddr *)&addr, sizeof(addr));
close(sd); /* Close the socket */
.. code-block:: c
int sd;
struct packet_mreq mreq;
char macaddr[ETH_ALEN] = {0x91, 0xe0, 0xf0, 0x00, 0x0e, 0x01};
sd = socket(AF_PACKET, SOCK_RAW, 0);
mreq.mr_ifindex = if_nametoindex("eth0");
mreq.mr_type = PACKET_MR_MULTICAST;
mreq.mr_alen = ETH_ALEN;
memcpy(&mreq.mr_address, macaddr, ETH_ALEN);
setsockopt(sd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq,
sizeof(struct packet_mreq));
close(sd);
+15
View File
@@ -44,6 +44,13 @@
#define PACKET_LOOPBACK 5
#define PACKET_FASTROUTE 6
/* Packet socket options */
#define PACKET_ADD_MEMBERSHIP 1 /* Add a multicast address to the interface */
#define PACKET_DROP_MEMBERSHIP 2 /* Drop a multicast address from the interface */
#define PACKET_MR_MULTICAST 0 /* Multicast address */
/****************************************************************************
* Public Types
****************************************************************************/
@@ -59,4 +66,12 @@ struct sockaddr_ll
unsigned char sll_addr[8];
};
struct packet_mreq
{
int mr_ifindex;
unsigned short mr_type;
unsigned short mr_alen;
unsigned char mr_address[8];
};
#endif /* __INCLUDE_NETPACKET_PACKET_H */
+1 -1
View File
@@ -94,7 +94,7 @@ int pkt_getsockopt(FAR struct socket *psock, int level, int option,
switch (option)
{
#if CONFIG_NET_SEND_BUFSIZE > 0
#if defined(CONFIG_NET_PKT_WRITE_BUFFERS) && CONFIG_NET_SEND_BUFSIZE > 0
case SO_SNDBUF:
{
FAR struct pkt_conn_s *conn;
+52 -1
View File
@@ -32,9 +32,11 @@
#include <assert.h>
#include <debug.h>
#include <netpacket/packet.h>
#include <nuttx/net/net.h>
#include <nuttx/net/pkt.h>
#include "netdev/netdev.h"
#include "socket/socket.h"
#include "utils/utils.h"
#include "pkt/pkt.h"
@@ -90,7 +92,7 @@ int pkt_setsockopt(FAR struct socket *psock, int level, int option,
switch (option)
{
#if CONFIG_NET_SEND_BUFSIZE > 0
#if defined(CONFIG_NET_PKT_WRITE_BUFFERS) && CONFIG_NET_SEND_BUFSIZE > 0
case SO_SNDBUF:
{
FAR struct pkt_conn_s *conn;
@@ -121,6 +123,55 @@ int pkt_setsockopt(FAR struct socket *psock, int level, int option,
}
#endif
#ifdef CONFIG_NET_MCASTGROUP
case PACKET_ADD_MEMBERSHIP:
case PACKET_DROP_MEMBERSHIP:
{
FAR const struct packet_mreq *mreq;
FAR struct net_driver_s *dev;
if (value == NULL || value_len < sizeof(struct packet_mreq))
{
return -EINVAL;
}
mreq = (FAR const struct packet_mreq *)value;
dev = netdev_findbyindex(mreq->mr_ifindex);
if (dev == NULL)
{
return -ENODEV;
}
if (mreq->mr_type == PACKET_MR_MULTICAST)
{
if (option == PACKET_ADD_MEMBERSHIP && dev->d_addmac != NULL)
{
/* Add the multicast MAC address to the device */
ret = dev->d_addmac(dev, mreq->mr_address);
}
else if (option == PACKET_DROP_MEMBERSHIP &&
dev->d_rmmac != NULL)
{
/* Drop the multicast MAC address from the device */
ret = dev->d_rmmac(dev, mreq->mr_address);
}
else
{
nerr("ERROR: Device does not support add MAC address\n");
ret = -ENOSYS;
}
}
else
{
nerr("ERROR: Invalid mr_type: %d\n", mreq->mr_type);
return -ENOSYS;
}
}
break;
#endif
default:
nerr("ERROR: Unrecognized PKT option: %d\n", option);
ret = -ENOPROTOOPT;
+1 -1
View File
@@ -61,7 +61,7 @@ config NET_CANPROTO_OPTIONS
Enable or disable support for CAN protocol level socket option
config NET_PKTPROTO_OPTIONS
bool
bool "PACKET proto socket options"
default n
---help---
Enable or disable support for PKT protocol level socket option