| /* main.c - Application main entry point */ |
| |
| /* |
| * Copyright (c) 2021 Intel Corporation |
| * |
| * SPDX-License-Identifier: Apache-2.0 |
| */ |
| |
| #include <zephyr/logging/log.h> |
| LOG_MODULE_REGISTER(net_test, CONFIG_NET_IPV4_LOG_LEVEL); |
| |
| #include <zephyr/types.h> |
| #include <stdbool.h> |
| #include <stddef.h> |
| #include <string.h> |
| #include <errno.h> |
| #include <zephyr/linker/sections.h> |
| |
| #include <zephyr/ztest.h> |
| |
| #include <zephyr/net/net_if.h> |
| #include <zephyr/net/net_pkt.h> |
| #include <zephyr/net/net_ip.h> |
| #include <zephyr/net/net_core.h> |
| #include <zephyr/net/ethernet.h> |
| #include <zephyr/net/dummy.h> |
| #include <zephyr/net/net_mgmt.h> |
| #include <zephyr/net/net_event.h> |
| #include <zephyr/net/igmp.h> |
| #include <zephyr/net/socket.h> |
| |
| #include <zephyr/random/random.h> |
| |
| #include "ipv4.h" |
| |
| #define THREAD_SLEEP 50 /* ms */ |
| |
| #define NET_LOG_ENABLED 1 |
| #include "net_private.h" |
| |
| #if defined(CONFIG_NET_IPV4_LOG_LEVEL_DBG) |
| #define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__) |
| #else |
| #define DBG(fmt, ...) |
| #endif |
| |
| static struct in_addr my_addr = { { { 192, 0, 2, 1 } } }; |
| static struct in_addr mcast_addr = { { { 224, 0, 2, 63 } } }; |
| static struct in_addr any_addr = INADDR_ANY_INIT; |
| |
| static struct net_if *net_iface; |
| static bool is_group_joined; |
| static bool is_group_left; |
| static bool is_join_msg_ok; |
| static bool is_leave_msg_ok; |
| static bool is_query_received; |
| static bool is_report_sent; |
| static bool ignore_already; |
| K_SEM_DEFINE(wait_data, 0, UINT_MAX); |
| |
| #define WAIT_TIME 500 |
| #define WAIT_TIME_LONG MSEC_PER_SEC |
| #define MY_PORT 1969 |
| #define PEER_PORT 13856 |
| |
| struct net_test_igmp { |
| uint8_t mac_addr[sizeof(struct net_eth_addr)]; |
| struct net_linkaddr ll_addr; |
| }; |
| |
| int net_test_dev_init(const struct device *dev) |
| { |
| return 0; |
| } |
| |
| static uint8_t *net_test_get_mac(const struct device *dev) |
| { |
| struct net_test_igmp *context = dev->data; |
| |
| if (context->mac_addr[2] == 0x00) { |
| /* 00-00-5E-00-53-xx Documentation RFC 7042 */ |
| context->mac_addr[0] = 0x00; |
| context->mac_addr[1] = 0x00; |
| context->mac_addr[2] = 0x5E; |
| context->mac_addr[3] = 0x00; |
| context->mac_addr[4] = 0x53; |
| context->mac_addr[5] = sys_rand32_get(); |
| } |
| |
| return context->mac_addr; |
| } |
| |
| static void net_test_iface_init(struct net_if *iface) |
| { |
| uint8_t *mac = net_test_get_mac(net_if_get_device(iface)); |
| |
| net_if_set_link_addr(iface, mac, sizeof(struct net_eth_addr), |
| NET_LINK_ETHERNET); |
| } |
| |
| static struct net_ipv4_igmp_v2_query *get_igmp_hdr(struct net_pkt *pkt) |
| { |
| net_pkt_cursor_init(pkt); |
| |
| net_pkt_skip(pkt, net_pkt_ip_hdr_len(pkt) + |
| net_pkt_ipv4_opts_len(pkt)); |
| |
| return (struct net_ipv4_igmp_v2_query *)net_pkt_cursor_get_pos(pkt); |
| } |
| |
| static int tester_send(const struct device *dev, struct net_pkt *pkt) |
| { |
| struct net_ipv4_igmp_v2_query *igmp; |
| |
| if (!pkt->buffer) { |
| TC_ERROR("No data to send!\n"); |
| return -ENODATA; |
| } |
| |
| igmp = get_igmp_hdr(pkt); |
| |
| if (igmp->type == NET_IPV4_IGMP_QUERY) { |
| NET_DBG("Received query...."); |
| is_query_received = true; |
| k_sem_give(&wait_data); |
| } else if (igmp->type == NET_IPV4_IGMP_REPORT_V2) { |
| NET_DBG("Received v2 report...."); |
| is_join_msg_ok = true; |
| is_report_sent = true; |
| k_sem_give(&wait_data); |
| } else if (igmp->type == NET_IPV4_IGMP_LEAVE) { |
| NET_DBG("Received leave...."); |
| is_leave_msg_ok = true; |
| k_sem_give(&wait_data); |
| } |
| |
| return 0; |
| } |
| |
| struct net_test_igmp net_test_data; |
| |
| static struct dummy_api net_test_if_api = { |
| .iface_api.init = net_test_iface_init, |
| .send = tester_send, |
| }; |
| |
| #define _ETH_L2_LAYER DUMMY_L2 |
| #define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2) |
| |
| NET_DEVICE_INIT(net_test_igmp, "net_test_igmp", |
| net_test_dev_init, NULL, &net_test_data, NULL, |
| CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, |
| &net_test_if_api, _ETH_L2_LAYER, _ETH_L2_CTX_TYPE, |
| 127); |
| |
| static void group_joined(struct net_mgmt_event_callback *cb, |
| uint32_t nm_event, struct net_if *iface) |
| { |
| if (nm_event != NET_EVENT_IPV4_MCAST_JOIN) { |
| /* Spurious callback. */ |
| return; |
| } |
| |
| is_group_joined = true; |
| |
| k_sem_give(&wait_data); |
| } |
| |
| static void group_left(struct net_mgmt_event_callback *cb, |
| uint32_t nm_event, struct net_if *iface) |
| { |
| if (nm_event != NET_EVENT_IPV4_MCAST_LEAVE) { |
| /* Spurious callback. */ |
| return; |
| } |
| |
| is_group_left = true; |
| |
| k_sem_give(&wait_data); |
| } |
| |
| static struct mgmt_events { |
| uint32_t event; |
| net_mgmt_event_handler_t handler; |
| struct net_mgmt_event_callback cb; |
| } mgmt_events[] = { |
| { .event = NET_EVENT_IPV4_MCAST_JOIN, .handler = group_joined }, |
| { .event = NET_EVENT_IPV4_MCAST_LEAVE, .handler = group_left }, |
| { 0 } |
| }; |
| |
| static void setup_mgmt_events(void) |
| { |
| int i; |
| |
| for (i = 0; mgmt_events[i].event; i++) { |
| net_mgmt_init_event_callback(&mgmt_events[i].cb, |
| mgmt_events[i].handler, |
| mgmt_events[i].event); |
| |
| net_mgmt_add_event_callback(&mgmt_events[i].cb); |
| } |
| } |
| |
| static void *igmp_setup(void) |
| { |
| struct net_if_addr *ifaddr; |
| |
| setup_mgmt_events(); |
| |
| net_iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY)); |
| |
| zassert_not_null(net_iface, "Interface is NULL"); |
| |
| ifaddr = net_if_ipv4_addr_add(net_iface, &my_addr, NET_ADDR_MANUAL, 0); |
| |
| zassert_not_null(ifaddr, "Cannot add IPv4 address"); |
| |
| return NULL; |
| } |
| |
| static void igmp_teardown(void *dummy) |
| { |
| ARG_UNUSED(dummy); |
| |
| int i; |
| |
| for (i = 0; mgmt_events[i].event; i++) { |
| net_mgmt_del_event_callback(&mgmt_events[i].cb); |
| } |
| |
| net_iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY)); |
| |
| net_if_ipv4_addr_rm(net_iface, &my_addr); |
| } |
| |
| static void join_group(void) |
| { |
| int ret; |
| |
| ret = net_ipv4_igmp_join(net_iface, &mcast_addr, NULL); |
| |
| if (ignore_already) { |
| zassert_true(ret == 0 || ret == -EALREADY, |
| "Cannot join IPv4 multicast group"); |
| } else { |
| zassert_equal(ret, 0, "Cannot join IPv4 multicast group"); |
| } |
| |
| /* Let the network stack to proceed */ |
| k_msleep(THREAD_SLEEP); |
| } |
| |
| static void leave_group(void) |
| { |
| int ret; |
| |
| ret = net_ipv4_igmp_leave(net_iface, &mcast_addr); |
| |
| zassert_equal(ret, 0, "Cannot leave IPv4 multicast group"); |
| |
| if (IS_ENABLED(CONFIG_NET_TC_THREAD_PREEMPTIVE)) { |
| /* Let the network stack to proceed */ |
| k_msleep(THREAD_SLEEP); |
| } else { |
| k_yield(); |
| } |
| } |
| |
| static void catch_join_group(void) |
| { |
| is_group_joined = false; |
| |
| ignore_already = false; |
| |
| join_group(); |
| |
| if (k_sem_take(&wait_data, K_MSEC(WAIT_TIME))) { |
| zassert_true(0, "Timeout while waiting join event"); |
| } |
| |
| if (!is_group_joined) { |
| zassert_true(0, "Did not catch join event"); |
| } |
| |
| is_group_joined = false; |
| } |
| |
| static void catch_leave_group(void) |
| { |
| is_group_joined = false; |
| |
| leave_group(); |
| |
| if (k_sem_take(&wait_data, K_MSEC(WAIT_TIME))) { |
| zassert_true(0, "Timeout while waiting leave event"); |
| } |
| |
| if (!is_group_left) { |
| zassert_true(0, "Did not catch leave event"); |
| } |
| |
| is_group_left = false; |
| } |
| |
| static void verify_join_group(void) |
| { |
| is_join_msg_ok = false; |
| |
| ignore_already = false; |
| |
| join_group(); |
| |
| if (k_sem_take(&wait_data, K_MSEC(WAIT_TIME))) { |
| zassert_true(0, "Timeout while waiting join event"); |
| } |
| |
| if (!is_join_msg_ok) { |
| zassert_true(0, "Join msg invalid"); |
| } |
| |
| is_join_msg_ok = false; |
| } |
| |
| static void verify_leave_group(void) |
| { |
| is_leave_msg_ok = false; |
| |
| leave_group(); |
| |
| if (k_sem_take(&wait_data, K_MSEC(WAIT_TIME))) { |
| zassert_true(0, "Timeout while waiting leave event"); |
| } |
| |
| if (!is_leave_msg_ok) { |
| zassert_true(0, "Leave msg invalid"); |
| } |
| |
| is_leave_msg_ok = false; |
| } |
| |
| ZTEST(net_igmp, test_igmp_catch_join) |
| { |
| join_group(); |
| leave_group(); |
| } |
| |
| ZTEST(net_igmp, test_igmp_catch_catch_join) |
| { |
| catch_join_group(); |
| catch_leave_group(); |
| } |
| |
| ZTEST(net_igmp, test_igmp_verify_catch_join) |
| { |
| verify_join_group(); |
| verify_leave_group(); |
| } |
| |
| static void socket_group_with_address(struct in_addr *local_addr, bool do_join) |
| { |
| struct ip_mreqn mreqn = { 0 }; |
| int option; |
| int ret, fd; |
| |
| if (do_join) { |
| option = IP_ADD_MEMBERSHIP; |
| } else { |
| option = IP_DROP_MEMBERSHIP; |
| } |
| |
| fd = zsock_socket(AF_INET, SOCK_DGRAM, 0); |
| zassert_true(fd >= 0, "Cannot get socket (%d)", -errno); |
| |
| ret = zsock_setsockopt(fd, IPPROTO_IP, option, |
| NULL, sizeof(mreqn)); |
| zassert_true(ret == -1 && errno == EINVAL, |
| "Incorrect return value (%d)", -errno); |
| |
| ret = zsock_setsockopt(fd, IPPROTO_IP, option, |
| (void *)&mreqn, 1); |
| zassert_true(ret == -1 && errno == EINVAL, |
| "Incorrect return value (%d)", -errno); |
| |
| /* First try with empty mreqn */ |
| ret = zsock_setsockopt(fd, IPPROTO_IP, option, |
| (void *)&mreqn, sizeof(mreqn)); |
| zassert_true(ret == -1 && errno == EINVAL, |
| "Incorrect return value (%d)", -errno); |
| |
| memcpy(&mreqn.imr_address, local_addr, sizeof(mreqn.imr_address)); |
| memcpy(&mreqn.imr_multiaddr, &mcast_addr, sizeof(mreqn.imr_multiaddr)); |
| |
| ret = zsock_setsockopt(fd, IPPROTO_IP, option, |
| (void *)&mreqn, sizeof(mreqn)); |
| |
| if (do_join) { |
| if (ignore_already) { |
| zassert_true(ret == 0 || ret == -EALREADY, |
| "Cannot join IPv4 multicast group (%d)", |
| -errno); |
| } else { |
| zassert_equal(ret, 0, |
| "Cannot join IPv4 multicast group (%d) " |
| "with local addr %s", |
| -errno, net_sprint_ipv4_addr(local_addr)); |
| } |
| } else { |
| zassert_equal(ret, 0, "Cannot leave IPv4 multicast group (%d)", |
| -errno); |
| |
| if (IS_ENABLED(CONFIG_NET_TC_THREAD_PREEMPTIVE)) { |
| /* Let the network stack to proceed */ |
| k_msleep(THREAD_SLEEP); |
| } else { |
| k_yield(); |
| } |
| } |
| |
| zsock_close(fd); |
| |
| /* Let the network stack to proceed */ |
| k_msleep(THREAD_SLEEP); |
| } |
| |
| static void socket_group_with_index(struct in_addr *local_addr, bool do_join) |
| { |
| struct ip_mreqn mreqn = { 0 }; |
| int option; |
| int ret, fd; |
| |
| if (do_join) { |
| option = IP_ADD_MEMBERSHIP; |
| } else { |
| option = IP_DROP_MEMBERSHIP; |
| } |
| |
| fd = zsock_socket(AF_INET, SOCK_DGRAM, 0); |
| zassert_true(fd >= 0, "Cannot get socket (%d)", -errno); |
| |
| mreqn.imr_ifindex = net_if_ipv4_addr_lookup_by_index(local_addr); |
| memcpy(&mreqn.imr_multiaddr, &mcast_addr, sizeof(mreqn.imr_multiaddr)); |
| |
| ret = zsock_setsockopt(fd, IPPROTO_IP, option, |
| (void *)&mreqn, sizeof(mreqn)); |
| |
| if (do_join) { |
| if (ignore_already) { |
| zassert_true(ret == 0 || ret == -EALREADY, |
| "Cannot join IPv4 multicast group (%d)", |
| -errno); |
| } else { |
| zassert_equal(ret, 0, |
| "Cannot join IPv4 multicast group (%d)", |
| -errno); |
| } |
| } else { |
| zassert_equal(ret, 0, "Cannot leave IPv4 multicast group (%d)", |
| -errno); |
| |
| if (IS_ENABLED(CONFIG_NET_TC_THREAD_PREEMPTIVE)) { |
| /* Let the network stack to proceed */ |
| k_msleep(THREAD_SLEEP); |
| } else { |
| k_yield(); |
| } |
| } |
| |
| zsock_close(fd); |
| |
| /* Let the network stack to proceed */ |
| k_msleep(THREAD_SLEEP); |
| } |
| |
| static void socket_join_group_with_address(struct in_addr *addr) |
| { |
| socket_group_with_address(addr, true); |
| } |
| |
| static void socket_leave_group_with_address(struct in_addr *addr) |
| { |
| socket_group_with_address(addr, false); |
| } |
| |
| static void socket_join_group_with_index(struct in_addr *addr) |
| { |
| socket_group_with_index(addr, true); |
| } |
| |
| static void socket_leave_group_with_index(struct in_addr *addr) |
| { |
| socket_group_with_index(addr, false); |
| } |
| |
| ZTEST_USER(net_igmp, test_socket_catch_join_with_address) |
| { |
| socket_join_group_with_address(&any_addr); |
| socket_leave_group_with_address(&any_addr); |
| socket_join_group_with_address(&my_addr); |
| socket_leave_group_with_address(&my_addr); |
| } |
| |
| ZTEST_USER(net_igmp, test_socket_catch_join_with_index) |
| { |
| socket_join_group_with_index(&any_addr); |
| socket_leave_group_with_index(&any_addr); |
| socket_join_group_with_index(&my_addr); |
| socket_leave_group_with_index(&my_addr); |
| } |
| |
| ZTEST_SUITE(net_igmp, NULL, igmp_setup, NULL, NULL, igmp_teardown); |