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 <gaohedong@xiaomi.com>
This commit is contained in:
Zhe Weng 2025-01-15 17:08:01 +08:00 committed by Donny(董九柱)
parent 157ecb091d
commit 4f93ec0a43
2 changed files with 7 additions and 1 deletions

View file

@ -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");

View file

@ -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");