diff --git a/samples/net/sockets/packet/Kconfig b/samples/net/sockets/packet/Kconfig index df901331469..0b9192a6a9e 100644 --- a/samples/net/sockets/packet/Kconfig +++ b/samples/net/sockets/packet/Kconfig @@ -13,4 +13,19 @@ config NET_SAMPLE_SEND_WAIT_TIME If set to 0, then the packets are sent as fast as possible, which will stress test the network stack. +config NET_SAMPLE_ENABLE_PACKET_DGRAM + bool "Use AF_PACKET with SOCK_DGRAM" + depends on NET_SOCKETS_PACKET_DGRAM + default y + help + This will strip Ethernet header from received packets + and insert Ethernet header to sent packets. + +config NET_SAMPLE_DESTINATION_ADDR + string "Destination Ethernet MAC address" + depends on NET_SOCKETS_PACKET_DGRAM + default "00:11:22:33:44:55" + help + Where to send the Ethernet frames. + source "Kconfig.zephyr" diff --git a/samples/net/sockets/packet/src/packet.c b/samples/net/sockets/packet/src/packet.c index bcce4b4e59f..0fd7d0c8b6b 100644 --- a/samples/net/sockets/packet/src/packet.c +++ b/samples/net/sockets/packet/src/packet.c @@ -73,12 +73,18 @@ static void quit(void) static int start_socket(int *sock) { - struct sockaddr_ll dst; + struct sockaddr_ll dst = { 0 }; int ret; - *sock = socket(AF_PACKET, SOCK_RAW, ETH_P_ALL); + *sock = socket(AF_PACKET, + IS_ENABLED(CONFIG_NET_SAMPLE_ENABLE_PACKET_DGRAM) ? + SOCK_DGRAM : SOCK_RAW, + ETH_P_ALL); if (*sock < 0) { - LOG_ERR("Failed to create RAW socket : %d", errno); + LOG_ERR("Failed to create %s socket : %d", + IS_ENABLED(CONFIG_NET_SAMPLE_ENABLE_PACKET_DGRAM) ? + "DGRAM" : "RAW", + errno); return -errno; } @@ -140,12 +146,28 @@ static void recv_packet(void) static int send_packet_socket(struct packet_data *packet) { - struct sockaddr_ll dst; + struct sockaddr_ll dst = { 0 }; size_t send = 100U; int ret; dst.sll_ifindex = net_if_get_by_iface(net_if_get_default()); + if (IS_ENABLED(CONFIG_NET_SAMPLE_ENABLE_PACKET_DGRAM)) { + dst.sll_halen = sizeof(struct net_eth_addr); + + /* FIXME: assume IP data atm */ + dst.sll_protocol = htons(ETH_P_IP); + + ret = net_bytes_from_str( + dst.sll_addr, + dst.sll_halen, + CONFIG_NET_SAMPLE_DESTINATION_ADDR); + if (ret < 0) { + LOG_ERR("Invalid MAC address '%s'", + CONFIG_NET_SAMPLE_DESTINATION_ADDR); + } + } + do { /* Sending dummy data */ ret = sendto(packet->send_sock, lorem_ipsum, send, 0,