Skip to content

Conversation

@zhhyu7
Copy link
Contributor

@zhhyu7 zhhyu7 commented Dec 17, 2025

Summary

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

Impact

net/pkt: setsockopt

Testing

sim:matter with below test code:

#include <nuttx/config.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <netpacket/packet.h>
#include <string.h>
#include <stdio.h>

int main(int argc, FAR char *argv[])
{
    int fd;
    int res;
    struct packet_mreq mreq;
    char macaddr[ETH_ALEN] = {0x91, 0xe0, 0xf0, 0x00, 0x0e, 0x01};

    fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    if (fd < 0) {
        perror("Failed to open socket");
        return -1;
    }
    mreq.mr_ifindex = 1; // eth0
    mreq.mr_type = PACKET_MR_MULTICAST;
    mreq.mr_alen = ETH_ALEN;
    memcpy(&mreq.mr_address, macaddr, ETH_ALEN);

    res = setsockopt(fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
                    &mreq, sizeof(struct packet_mreq));
    if (res < 0) {
        perror("Couldn't set PACKET_ADD_MEMBERSHIP");
    } else {
        printf("Successfully set PACKET_ADD_MEMBERSHIP\n");
    }

    close(fd);
    return res;
}

NuttX test log

NuttShell (NSH) NuttX-12.11.0
MOTD: username=admin password=Administrator
nsh> ifconfig
eth0	Link encap:Ethernet HWaddr 42:e1:c4:3f:48:dd at RUNNING mtu 1500
	inet addr:10.0.1.2 DRaddr:10.0.0.1 Mask:255.255.255.0
	inet6 addr: fe80::40e1:c4ff:fe3f:48dd/64
	inet6 DRaddr: ::

nsh> hello
Successfully set PACKET_ADD_MEMBERSHIP
nsh> 

@github-actions github-actions bot added Area: Networking Effects networking subsystem Arch: simulator Issues related to the SIMulator Size: S The size of the change in this PR is small labels Dec 17, 2025
Copy link
Contributor

@acassis acassis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhhyu7 please add a Documentation about it at https://nuttx.apache.org/docs/latest/components/net/index.html maybe more specifically here: https://nuttx.apache.org/docs/latest/components/net/netdriver.html or create a new entry to it if you think it doesn't fit in the existing categories.

@zhhyu7
Copy link
Contributor Author

zhhyu7 commented Dec 19, 2025

@zhhyu7 please add a Documentation about it at https://nuttx.apache.org/docs/latest/components/net/index.html maybe more specifically here: https://nuttx.apache.org/docs/latest/components/net/netdriver.html or create a new entry to it if you think it doesn't fit in the existing categories.

OK, After https://github.com/apache/nuttx/pull/17483 PR is merged, I will add the new part based on the modifications of this PR.

@xiaoxiang781216
Copy link
Contributor

@zhhyu7 please add a Documentation about it at https://nuttx.apache.org/docs/latest/components/net/index.html maybe more specifically here: https://nuttx.apache.org/docs/latest/components/net/netdriver.html or create a new entry to it if you think it doesn't fit in the existing categories.

OK, After https://github.com/apache/nuttx/pull/17483 PR is merged, I will add the new part based on the modifications of this PR.

@acassis let's finish the document on the next patch?

@acassis
Copy link
Contributor

acassis commented Dec 20, 2025

@zhhyu7 please add a Documentation about it at https://nuttx.apache.org/docs/latest/components/net/index.html maybe more specifically here: https://nuttx.apache.org/docs/latest/components/net/netdriver.html or create a new entry to it if you think it doesn't fit in the existing categories.

OK, After https://github.com/apache/nuttx/pull/17483 PR is merged, I will add the new part based on the modifications of this PR.

@acassis let's finish the document on the next patch?

@xiaoxiang781216 from previous experience the Documentation never come later, if the PR adds a new feature, it needs to include a Documentation. We all agreed on that in the voting. Similar for testing, to avoid breaking the mainline with features that people forget to test

Copy link
Contributor

@acassis acassis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhhyu7 please add Documentation

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>
Avoid PACKET_ADD_MEMBERSHIP calls returning errors.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
@github-actions github-actions bot added Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium labels Dec 24, 2025
@zhhyu7
Copy link
Contributor Author

zhhyu7 commented Dec 24, 2025

@zhhyu7 please add Documentation

@acassis I added sample code and updated the relevant description information, please review again, thanks.

@zhhyu7 zhhyu7 requested a review from acassis December 24, 2025 09:14
@xiaoxiang781216 xiaoxiang781216 dismissed acassis’s stale review December 25, 2025 01:54

documentation is provided.

@Donny9 Donny9 merged commit ac3a7da into apache:master Dec 25, 2025
55 of 78 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: simulator Issues related to the SIMulator Area: Documentation Improvements or additions to documentation Area: Networking Effects networking subsystem Size: M The size of the change in this PR is medium Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants