This is an automated email from the ASF dual-hosted git repository.

jiuzhudong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 4f93ec0a4 pkt: Set `sll_protocol` for raw socket to `ETH_P_ALL`
4f93ec0a4 is described below

commit 4f93ec0a4335d574eeecfd295584fbfb17056e5b
Author: Zhe Weng <[email protected]>
AuthorDate: Wed Jan 15 17:08:01 2025 +0800

    pkt: Set `sll_protocol` for raw socket to `ETH_P_ALL`
    
    Ref: https://man7.org/linux/man-pages/man7/packet.7.html
    
    We should either set protocal when creating socket or binding, otherwise
    we cannot capture any packet.
    
    Signed-off-by: gaohedong <[email protected]>
---
 examples/netpkt/netpkt_ethercat.c | 5 ++++-
 system/tcpdump/tcpdump.c          | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/examples/netpkt/netpkt_ethercat.c 
b/examples/netpkt/netpkt_ethercat.c
index 8b8ab298a..6ed6ae8d9 100644
--- a/examples/netpkt/netpkt_ethercat.c
+++ b/examples/netpkt/netpkt_ethercat.c
@@ -30,6 +30,8 @@
 #include <time.h>
 
 #include <net/if.h>
+#include <netinet/if_ether.h>
+#include <netinet/in.h>
 #include <netinet/ip.h>
 #include <netpacket/packet.h>
 #include <sys/socket.h>
@@ -66,7 +68,7 @@ int main(int argc, FAR const char *argv[])
   int num_packets = 0;
   int len;
   int ifindex;
-  int sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW);
+  int sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
 
   if (sockfd == -1)
     {
@@ -96,6 +98,7 @@ int main(int argc, FAR const char *argv[])
 
   addr.sll_family = AF_PACKET;
   addr.sll_ifindex = ifindex;
+  addr.sll_protocol = htons(ETH_P_ALL);
   if (bind(sockfd, (FAR const struct sockaddr *)&addr, sizeof(addr)) < 0)
     {
       perror("ERROR: binding socket failed");
diff --git a/system/tcpdump/tcpdump.c b/system/tcpdump/tcpdump.c
index 0220ae8bd..5f80ef510 100644
--- a/system/tcpdump/tcpdump.c
+++ b/system/tcpdump/tcpdump.c
@@ -30,6 +30,8 @@
 #include <fcntl.h>
 #include <net/if.h>
 #include <net/if_arp.h>
+#include <netinet/if_ether.h>
+#include <netinet/in.h>
 #include <netpacket/packet.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -201,6 +203,7 @@ static int socket_open(int ifindex)
 
   addr.sll_family = AF_PACKET;
   addr.sll_ifindex = ifindex;
+  addr.sll_protocol = htons(ETH_P_ALL);
   if (bind(sd, (FAR const struct sockaddr *)&addr, sizeof(addr)) < 0)
     {
       perror("ERROR: binding socket failed");

Reply via email to