genetlink: Test Netlink subsystem of Linux v6.1
Subsystems (5)
cover genetlink: Test Netlink subsystem of Linux v6.1
This series adds comprehensive testing infrastructure for Netlink and Generic Netlink
The implementation includes both kernel module and userspace tests to verify correct Generic Netlink and Netlink behaviors under various conditions.
Yana Bashlykova (15):
genetlink: add sysfs test module for Generic Netlink
genetlink: add TEST_GENL family for netlink testing
genetlink: add PARALLEL_GENL test family
genetlink: add test case for duplicate genl family registration
genetlink: add test case for family with invalid ops
genetlink: add netlink notifier support
genetlink: add THIRD_GENL family
genetlink: verify unregister fails for non-registered family
genetlink: add LARGE_GENL stress test family
selftests: net: genetlink: add packet capture test infrastructure
selftests: net: genetlink: add /proc/net/netlink test
selftests: net: genetlink: add Generic Netlink controller tests
selftests: net: genetlink: add large family ID resolution test
selftests: net: genetlink: add Netlink and Generic Netlink test suite
selftests: net: genetlink: fix expectation for large family resolution
drivers/net/Kconfig | 2 + drivers/net/Makefile | 2 + drivers/net/genetlink/Kconfig | 8 + drivers/net/genetlink/Makefile | 3 + .../net-pf-16-proto-16-family-PARALLEL_GENL.c | 1921 ++++++ tools/testing/selftests/net/Makefile | 6 + tools/testing/selftests/net/genetlink.c | 5152 +++++++++++++++++ 7 files changed, 7094 insertions(+) create mode 100644 drivers/net/genetlink/Kconfig create mode 100644 drivers/net/genetlink/Makefile create mode 100644 drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c create mode 100644 tools/testing/selftests/net/genetlink.c
Signature · 1 line
2.34.1
PATCH 1/15 genetlink: add sysfs test module for Generic Netlink
Add a test module that creates sysfs interfaces for Generic Netlink testing:
- /sys/kernel/genl_test with value/message/info attributes
- /sys/kernel/parallel_genl with message attribute
- /sys/kernel/third_genl with message attribute
Implements basic read/write operations with proper error handling and cleanup. Will be used as foundation for netlink testing infrastructure.
--- drivers/net/Kconfig | 2 + drivers/net/Makefile | 2 + drivers/net/genetlink/Kconfig | 8 + drivers/net/genetlink/Makefile | 3 + .../net-pf-16-proto-16-family-PARALLEL_GENL.c | 288 ++++++++++++++++++ 5 files changed, 303 insertions(+) create mode 100644 drivers/net/genetlink/Kconfig create mode 100644 drivers/net/genetlink/Makefile create mode 100644 drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c
drivers/net/Kconfig +2 -0
@@ -631,4 +631,6 @@ config NETDEV_LEGACY_INIT Drivers that call netdev_boot_setup_check() should select this symbol, everything else no longer needs it. +source "drivers/net/genetlink/Kconfig" + endif # NETDEVICES
drivers/net/Makefile +2 -0
@@ -89,3 +89,5 @@ thunderbolt-net-y += thunderbolt.o obj-$(CONFIG_USB4_NET) += thunderbolt-net.o obj-$(CONFIG_NETDEVSIM) += netdevsim/ obj-$(CONFIG_NET_FAILOVER) += net_failover.o + +obj-$(CONFIG_NETLINK_TEST) += genetlink/
drivers/net/genetlink/Kconfig new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0 + +config NETLINK_TEST + tristate "Test module for netlink communication" + depends on NET + help + This module provides testing interface for netlink communication. + Used by selftests in tools/testing/selftests/net/.
drivers/net/genetlink/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_NETLINK_TEST) += net-pf-16-proto-16-family-PARALLEL_GENL.o
drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c new file +288 -0
288 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 2/15 genetlink: add TEST_GENL family for netlink testing
In reply to Yana BashlykovaImplement basic Generic Netlink family with:
- 4 commands (ECHO, SET/GET_VALUE, NO_ATTRS)
- 1 small command (genl_small_ops)
- Multicast group support
- Attribute validation
- Mutex-protected operations
- Error handling with netlink_ext_ack
--- .../net-pf-16-proto-16-family-PARALLEL_GENL.c | 438 +++++++++++++++++- 1 file changed, 431 insertions(+), 7 deletions(-)
drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c +431 -7
@@ -71,17 +71,16 @@ static ssize_t show_genl_test_message(struct kobject *kobj,static ssize_t store_genl_test_message (struct kobject *kobj,struct kobj_attribute *attr, -const char *buf,size_t count) +const char *buf,size_t count) { -size_t len =min (count,sizeof (sysfs_data.genl_test_message) -1 ); +size_t len =min (count,sizeof (sysfs_data.genl_test_message) -1 ); -strncpy (sysfs_data.genl_test_message, buf, len); - sysfs_data.genl_test_message[len] ='\0' ; -return count; +strncpy (sysfs_data.genl_test_message, buf, len); + sysfs_data.genl_test_message[len] ='\0' ; +return count; } -static ssize_t show_genl_test_value (struct kobject *kobj, -struct kobj_attribute *attr,char *buf) +static ssize_t show_genl_test_value (struct kobject *kobj,struct kobj_attribute *attr,char *buf) {return sprintf (buf,"%d" , sysfs_data.genl_test_value); }
@@ -146,6 +145,424 @@ static struct kobj_attribute my_attr_str_parallel_genl =static struct kobj_attribute my_attr_str_third_genl =__ATTR (message,0664 , show_third_genl_message, store_third_genl_message); +static DEFINE_MUTEX (genl_mutex); + +#define MY_GENL_FAMILY_NAME "TEST_GENL" +#define MY_GENL_VERSION 1 + +#define PATH_GENL_TEST_NUM "/sys/kernel/genl_test/value" +#define PATH_GENL_TEST_MES "/sys/kernel/genl_test/message" +#define PATH_GENL_TEST_DEV "/sys/kernel/genl_test/some_info" + +// TEST_GENL +enum { +MY_GENL_ATTR_UNSPEC , +MY_GENL_ATTR_DATA , +MY_GENL_ATTR_VALUE , +MY_GENL_ATTR_PATH , +MY_GENL_ATTR_NESTED , + __MY_GENL_ATTR_MAX, +}; + +#define MY_GENL_ATTR_MAX (__MY_GENL_ATTR_MAX - 1) + +enum { +MY_GENL_CMD_UNSPEC , +MY_GENL_CMD_ECHO , +MY_GENL_CMD_SET_VALUE , +MY_GENL_CMD_GET_VALUE , +MY_GENL_CMD_EVENT , +MY_GENL_CMD_NO_ATTRS , + __MY_GENL_CMD_MAX, +}; + +#define MY_GENL_CMD_MAX (__MY_GENL_CMD_MAX - 1) + +enum { +MY_GENL_SMALL_CMD_GET , +MY_GENL_SMALL_CMD_ERROR , + __MY_GENL_SMALL_CMD_MAX, +}; + +#define MY_GENL_SMALL_CMD_MAX (__MY_GENL_SMALL_CMD_MAX - 1) + +static const struct nla_policy my_genl_policy[MY_GENL_ATTR_MAX +1 ] = { + [MY_GENL_ATTR_UNSPEC ] = { .type =NLA_UNSPEC }, + [MY_GENL_ATTR_DATA ] = { .type =NLA_STRING }, + [MY_GENL_ATTR_VALUE ] = { .type =NLA_U32 , + .validation_type =NLA_VALIDATE_RANGE , + .min =0 , + .max =100 }, + [MY_GENL_ATTR_PATH ] = { .type =NLA_STRING }, + [MY_GENL_ATTR_NESTED ] = { .type =NLA_NESTED }, +}; + +/* netlink families */ +static struct genl_family my_genl_family; + +enum my_multicast_groups { +MY_MCGRP_GENL , +}; + +static const struct genl_multicast_group genl_mcgrps[] = { + [MY_MCGRP_GENL ] = { .name ="MY_MCGRP_GENL" , }, +}; + +static int my_genl_pre_doit (const struct genl_ops *ops,struct sk_buff *skb, +struct genl_info *info) +{ +mutex_lock (&genl_mutex); +return 0 ; +} + +static void my_genl_post_doit (const struct genl_ops *ops,struct sk_buff *skb, +struct genl_info *info) +{ +mutex_unlock (&genl_mutex); +} + +static void my_genl_mcast_msg (struct sk_buff *mcast_skb,struct genl_info *info) +{ +if (info) { +genl_notify (&my_genl_family, mcast_skb, info,MY_MCGRP_GENL , +GFP_KERNEL ); + }else { +genlmsg_multicast (&my_genl_family, mcast_skb,0 ,MY_MCGRP_GENL , +GFP_KERNEL ); + } +} + +// Functions for Generic Netlink TEST_GENL family +static int my_genl_echo (struct sk_buff *skb,struct genl_info *info) +{ +struct sk_buff *msg; +void *data; +int ret; +char *str; + +if (info->nlhdr->nlmsg_flags &NLM_F_ECHO ) { + msg =genlmsg_new (GENLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + data =genlmsg_put_reply (msg, info, &my_genl_family,0 , +MY_GENL_CMD_ECHO ); +if (!data) + goto error; + + str ="Hello to mcast groups!" ; + +strcpy (sysfs_data.genl_test_message, str); + + ret =nla_put_string (msg,MY_GENL_ATTR_DATA , str); +if (ret <0 ) + goto error; + +genlmsg_end (msg, data); + +my_genl_mcast_msg (msg, info); + } + +return 0 ; + +error: +nlmsg_free (msg); +return -EMSGSIZE ; +} + +static int my_genl_set_value (struct sk_buff *skb,struct genl_info *info) +{ +struct sk_buff *msg; +void *msg_head; +struct nlattr *na_path; +struct nlattr *na_value; +char *sysfs_path; +u32 new_value; +int err; +int code; +struct netlink_ext_ack *extack; +struct nlattr *attr; +struct nlmsghdr *nlh; +char cookie[NETLINK_MAX_COOKIE_LEN ]; + +if (!info->attrs[MY_GENL_ATTR_VALUE ]) { +pr_info ("%s: Missing MY_GENL_ATTR_VALUE\n" , __func__); +return -EINVAL ; + } + + na_value = info->attrs[MY_GENL_ATTR_VALUE ]; + new_value =nla_get_u32 (na_value); + +if (new_value !=0 && new_value !=1 ) { +pr_err ("%s: New value is incorrect\n" , __func__); + goto error; + } + + na_path = info->attrs[MY_GENL_ATTR_PATH ]; +if (!na_path) { +pr_info ("%s: Missing MY_GENL_ATTR_PATH\n" , __func__); +return -EINVAL ; + } + sysfs_path =nla_data (na_path); + + sysfs_data.genl_test_value = new_value; + + msg =genlmsg_new (NLMSG_GOODSIZE ,GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + msg_head =genlmsg_put (msg, info->snd_portid, info->snd_seq, + &my_genl_family,0 ,MY_GENL_CMD_SET_VALUE ); +if (!msg_head) { +nlmsg_free (msg); +return -ENOMEM ; + } + +if (nla_put_u32 (msg,MY_GENL_ATTR_VALUE , new_value)) { +genlmsg_cancel (msg, msg_head); +nlmsg_free (msg); +return -EMSGSIZE ; + } + +genlmsg_end (msg, msg_head); + + err =netlink_unicast (skb->sk, msg, info->snd_portid,0 ); +if (err <0 ) { +pr_err ("%s: Error in netlink_unicast, err=%d\n" , __func__, err); +nlmsg_free (msg); +return err; + } + +return 0 ; + +error: +// sending error ACK + code = -EINVAL ; + + extack =kmalloc (sizeof (*extack),GFP_KERNEL ); +if (!extack) +return -ENOMEM ; + +strcpy (cookie,"000001" ); + extack->_msg ="Incorrect value from userspace" ; + extack->bad_attr = na_value; + extack->policy = my_genl_policy; + extack->cookie_len =strlen (cookie); + extack->miss_type =MY_GENL_ATTR_VALUE ; + extack->miss_nest = attr; + + nlh =nlmsg_hdr (skb); +netlink_ack (skb, nlh, code, extack); +pr_info ("%s: Message with TLV was sent\n" , __func__); +return -EINVAL ; +} + +static int my_genl_get_value (struct sk_buff *skb,struct genl_info *info) +{ +struct sk_buff *msg; +void *msg_head; +struct nlattr *na_path; +char *sysfs_path; +u32 value; +int err; +int code; + + msg =genlmsg_new (NLMSG_GOODSIZE ,GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + msg_head =genlmsg_put (msg, info->snd_portid, info->snd_seq, + &my_genl_family,0 ,MY_GENL_CMD_GET_VALUE ); +if (!msg_head) { +nlmsg_free (msg); +return -ENOMEM ; + } + +if (!info->attrs[MY_GENL_ATTR_PATH ]) { +nlmsg_free (msg); +return -EINVAL ; + } +genl_unlock (); + na_path = info->attrs[MY_GENL_ATTR_PATH ]; + sysfs_path =nla_data (na_path); +genl_lock (); + +if (strcmp (sysfs_path,PATH_GENL_TEST_NUM ) !=0 ) { +pr_err ("%s: Incorrect path: %s\n" , __func__, sysfs_path); + goto error; + } + + value = sysfs_data.genl_test_value; + +if (nla_put_u32 (msg,MY_GENL_ATTR_VALUE , value)) { +genlmsg_cancel (msg, msg_head); +nlmsg_free (msg); +return -EMSGSIZE ; + } + +genlmsg_end (msg, msg_head); + +if (info) { + err =genlmsg_reply (msg, info); +if (err !=0 ) { +pr_err ("%s: Error in genlmsg_reply, err=%d\n" , __func__, err); +nlmsg_free (msg); +return err; + } + } + +return 0 ; + +error: + code = -EINVAL ; +netlink_set_err (skb->sk,0 ,MY_MCGRP_GENL , code); +return -EINVAL ; +} + +static int my_genl_no_attrs (struct sk_buff *skb,struct genl_info *info) +{ +struct sk_buff *msg; +void *msg_head; +int ret; +char *str; + + msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + msg_head =genlmsg_put_reply (msg, info, &my_genl_family,0 , + info->genlhdr->cmd); +if (!msg_head) + goto error; + + str ="Reply from GENL_TEST family function with no attrs" ; + +strcpy (sysfs_data.genl_test_message, str); + +if (nla_put_string (msg,MY_GENL_ATTR_DATA , str)) { +pr_err ("%s: Error with putting value to MY_GENL_ATTR_DATA\n" , __func__); + goto error; + } + +genlmsg_end (msg, msg_head); +return genlmsg_reply (msg, info); + +error: + ret = -EMSGSIZE ; +nlmsg_free (msg); +return ret; +} + +// Generic Netlink operations for TEST_GENL family +static const struct genl_ops my_genl_ops[] = { + { + .cmd =MY_GENL_CMD_ECHO , + .flags =0 , + .policy = my_genl_policy, + .doit = my_genl_echo, + .dumpit =NULL , + }, + { + .cmd =MY_GENL_CMD_SET_VALUE , + .policy = my_genl_policy, + .doit = my_genl_set_value, + .flags =GENL_ADMIN_PERM , + }, + { + .cmd =MY_GENL_CMD_GET_VALUE , + .flags =0 , + .policy = my_genl_policy, + .doit = my_genl_get_value, + .dumpit =NULL , + }, + { + .cmd =MY_GENL_CMD_NO_ATTRS , + .flags =0 , + .policy =NULL , + .doit = my_genl_no_attrs, + .dumpit =NULL , + }, +}; + +static int my_genl_small_cmd_get (struct sk_buff *skb,struct genl_info *info) +{ +struct sk_buff *msg; +void *reply; +int ret; +char *str; + + msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + reply =genlmsg_put_reply (msg, info, &my_genl_family,0 , + info->genlhdr->cmd); +if (!reply) + goto error; + + str ="IT'S ME from kernel" ; + +strcpy (sysfs_data.genl_test_message, str); + +if (nla_put_string (msg,MY_GENL_ATTR_DATA , str)) { +nlmsg_free (msg); +pr_err ("%s: Error with putting value to MY_GENL_ATTR_DATA\n" , __func__); +return -EMSGSIZE ; + } + +genlmsg_end (msg, reply); +return genlmsg_reply (msg, info); + +error: + ret = -EMSGSIZE ; +nlmsg_free (msg); +return ret; +} + +static const struct genl_small_ops my_genl_small_ops[] = { + { + .cmd =MY_GENL_SMALL_CMD_GET , + .doit = my_genl_small_cmd_get, + }, +}; + +// genl_family struct for TEST_GENL family +static struct genl_family my_genl_family = { + .hdrsize =0 , + .name =MY_GENL_FAMILY_NAME , + .version =MY_GENL_VERSION , + .maxattr =MY_GENL_ATTR_MAX , + .netnsok = true, + .pre_doit = my_genl_pre_doit, + .post_doit = my_genl_post_doit, + .ops = my_genl_ops, + .n_ops =ARRAY_SIZE (my_genl_ops), + .small_ops = my_genl_small_ops, + .n_small_ops =ARRAY_SIZE (my_genl_small_ops), + .policy = my_genl_policy, + .mcgrps = genl_mcgrps, + .n_mcgrps =ARRAY_SIZE (genl_mcgrps), +}; + +static int __init init_netlink (void ) +{ +int rc; + +pr_info ("%s: My module. Initializing Netlink\n" , __func__); + + rc =genl_register_family (&my_genl_family); +if (rc) { +pr_err ("%s: Failed to register Generic Netlink family\n" , __func__); + goto failure_1; + } + +return 0 ; + +failure_1: +pr_debug ("%s: My module. Error occurred in %s\n" , __func__, __func__); +return rc; +} +static int __init init_sysfs_third_genl (void ) {int ret;
@@ -253,6 +670,11 @@ static int __init module_netlink_init(void)if (ret) goto err_sysfs; + ret =init_netlink (); +if (ret) + goto err_sysfs; +pr_info ("%s: New families are registered\n" , __func__); +return 0 ; err_sysfs:
@@ -271,6 +693,8 @@ static int __init module_netlink_init(void) static void __exit module_netlink_exit(void) { +genl_unregister_family (&my_genl_family); +sysfs_remove_file (kobj_genl_test, &my_attr_u32_genl_test.attr);sysfs_remove_file (kobj_genl_test, &my_attr_str_genl_test.attr);device_remove_file (dev_genl_test, &dev_attr_info_genl_test);
Signature · 1 line
2.34.1
PATCH 3/15 genetlink: add PARALLEL_GENL test family
In reply to Yana BashlykovaImplement PARALLEL_GENL Generic Netlink family with:
- Parallel operations support (parallel dump)
- 4 commands (SEND, DUMP_INFO, SET/GET_VALUE)
- Command with reject policy
- Advanced attribute types (binary, nested, flags)
- Multicast group support
- Proper error handling
--- .../net-pf-16-proto-16-family-PARALLEL_GENL.c | 547 +++++++++++++++++- 1 file changed, 544 insertions(+), 3 deletions(-)
drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c +544 -3
@@ -71,7 +71,7 @@ static ssize_t show_genl_test_message(struct kobject *kobj,static ssize_t store_genl_test_message (struct kobject *kobj,struct kobj_attribute *attr, -const char *buf,size_t count) +const char *buf,size_t count) {size_t len =min (count,sizeof (sysfs_data.genl_test_message) -1 );
@@ -150,9 +150,12 @@ static DEFINE_MUTEX(genl_mutex);#define MY_GENL_FAMILY_NAME "TEST_GENL"#define MY_GENL_VERSION 1 +#define PARALLEL_GENL_FAMILY_NAME "PARALLEL_GENL" +#define PATH_GENL_TEST_NUM "/sys/kernel/genl_test/value"#define PATH_GENL_TEST_MES "/sys/kernel/genl_test/message"#define PATH_GENL_TEST_DEV "/sys/kernel/genl_test/some_info" +#define PATH_PARALLEL_GENL_MES "/sys/kernel/parallel_genl/message"// TEST_GENL enum {
@@ -200,6 +203,8 @@ static const struct nla_policy my_genl_policy[MY_GENL_ATTR_MAX + 1] = {/* netlink families */ static struct genl_family my_genl_family; +static struct genl_family my_genl_family_parallel; +enum my_multicast_groups {MY_MCGRP_GENL , };
@@ -544,6 +549,534 @@ static struct genl_family my_genl_family = { .n_mcgrps = ARRAY_SIZE(genl_mcgrps), }; +// PARALLEL_GENL family +enum { +PARALLEL_GENL_ATTR_UNSPEC , +PARALLEL_GENL_ATTR_DATA , +PARALLEL_GENL_ATTR_BINARY , +PARALLEL_GENL_ATTR_NAME , +PARALLEL_GENL_ATTR_DESC , +PARALLEL_GENL_ATTR_BITFIELD32 , +PARALLEL_GENL_ATTR_SIGN_NUM , +PARALLEL_GENL_ATTR_ARRAY , +PARALLEL_GENL_ATTR_NESTED , +PARALLEL_GENL_ATTR_FLAG_NONBLOCK , +PARALLEL_GENL_ATTR_FLAG_BLOCK , +PARALLEL_GENL_ATTR_REJECT , +PARALLEL_GENL_ATTR_PATH , + __PARALLEL_GENL_ATTR_MAX, +}; + +#define PARALLEL_GENL_ATTR_MAX (__PARALLEL_GENL_ATTR_MAX - 1) + +enum { +PARALLEL_GENL_CMD_UNSPEC , +PARALLEL_GENL_CMD_SEND , +PARALLEL_GENL_CMD_DUMP_INFO , +PARALLEL_GENL_CMD_SET_VALUE , +PARALLEL_GENL_CMD_GET_VALUE , + __PARALLEL_GENL_CMD_MAX, +}; + +#define PARALLEL_GENL_CMD_MAX (__PARALLEL_GENL_CMD_MAX - 1) + +static const struct nla_policy parallel_genl_policy[PARALLEL_GENL_ATTR_MAX + +1 ] = { + [PARALLEL_GENL_ATTR_UNSPEC ] = { .type =NLA_UNSPEC }, + [PARALLEL_GENL_ATTR_DATA ] = { .type =NLA_STRING }, + [PARALLEL_GENL_ATTR_BINARY ] = { .type =NLA_BINARY }, + [PARALLEL_GENL_ATTR_NAME ] = { .type =NLA_NUL_STRING }, + [PARALLEL_GENL_ATTR_DESC ] = { .type =NLA_NUL_STRING }, + [PARALLEL_GENL_ATTR_BITFIELD32 ] = { .type =NLA_BITFIELD32 }, + [PARALLEL_GENL_ATTR_SIGN_NUM ] = { .type =NLA_S32 , + .validation_type =NLA_VALIDATE_RANGE , + .min =-100 , + .max =100 }, + [PARALLEL_GENL_ATTR_ARRAY ] = { .type =NLA_NESTED_ARRAY }, + [PARALLEL_GENL_ATTR_NESTED ] = { .type =NLA_NESTED }, + [PARALLEL_GENL_ATTR_FLAG_NONBLOCK ] = { .type =NLA_FLAG }, + [PARALLEL_GENL_ATTR_FLAG_BLOCK ] = { .type =NLA_FLAG }, + [PARALLEL_GENL_ATTR_REJECT ] = { .type =NLA_REJECT }, + [PARALLEL_GENL_ATTR_PATH ] = { .type =NLA_STRING }, +}; + +static int parallel_genl_send (struct sk_buff *skb,struct genl_info *info) +{ +struct sk_buff *msg; +void *reply; +int ret; +char *str; + + msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); + +if (!msg) +return -ENOMEM ; + + reply =genlmsg_put_reply (msg, info, &my_genl_family,0 , + info->genlhdr->cmd); +if (!reply) + goto error; + + str ="NEW family - parallel_genl" ; + +strcpy (sysfs_data.parallel_genl_message, str); + +if (nla_put_string (msg,PARALLEL_GENL_ATTR_DATA , str)) { +nlmsg_free (msg); +pr_err ("%s: Error with putting value to PARALLEL_GENL_ATTR_DATA\n" , __func__); +return -EMSGSIZE ; + } + +genlmsg_end (msg, reply); + +if (nla_get_flag (info->attrs[PARALLEL_GENL_ATTR_FLAG_NONBLOCK ])) + goto overrun_nonblock; +if (nla_get_flag (info->attrs[PARALLEL_GENL_ATTR_FLAG_BLOCK ])) + goto overrun_block; + +return genlmsg_reply (msg, info); + +error: + ret = -EMSGSIZE ; +nlmsg_free (msg); +return ret; + +overrun_nonblock: + skb->sk->sk_sndtimeo =1000 ; + ret =netlink_unicast (skb->sk, msg, info->snd_portid,1 ); +if (ret <0 ) +return ret; + +return 0 ; + +overrun_block: + ret =netlink_unicast (skb->sk, msg, info->snd_portid,0 ); +if (ret <0 ) +return ret; + +return 0 ; +} + +static int parallel_genl_set_str_value (struct sk_buff *skb, +struct genl_info *info) +{ +struct sk_buff *msg; +void *msg_head; +struct nlattr *na_path; +struct nlattr *na_value; +char *sysfs_path; +char *new_value =NULL ; +int err; +int data_len; + +if (!info->attrs[PARALLEL_GENL_ATTR_DATA ]) { +pr_info ("%s: Missing PARALLEL_GENL_ATTR_DATA\n" , __func__); +return -EINVAL ; + } + + na_value = info->attrs[PARALLEL_GENL_ATTR_DATA ]; + data_len =nla_len (na_value); + + new_value =kmalloc (data_len +1 ,GFP_KERNEL ); +if (!new_value) +return -ENOMEM ; + +strncpy (new_value,nla_data (na_value), data_len); + new_value[data_len] ='\0' ; + + na_path = info->attrs[PARALLEL_GENL_ATTR_PATH ]; +if (!na_path) { +pr_info ("%s: Missing PARALLEL_GENL_ATTR_PATH\n" , __func__); +return -EINVAL ; + } + sysfs_path =nla_data (na_path); + +strcpy (sysfs_data.parallel_genl_message, new_value); + + msg =genlmsg_new (NLMSG_GOODSIZE ,GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + msg_head =genlmsg_put (msg, info->snd_portid, info->snd_seq, + &my_genl_family_parallel,0 , +PARALLEL_GENL_CMD_SET_VALUE ); +if (!msg_head) { +nlmsg_free (msg); +return -ENOMEM ; + } + +if (nla_put_string (msg,PARALLEL_GENL_ATTR_DATA , new_value)) { +genlmsg_cancel (msg, msg_head); +nlmsg_free (msg); +return -EMSGSIZE ; + } + +genlmsg_end (msg, msg_head); + + err =netlink_unicast (skb->sk, msg, info->snd_portid,0 ); +if (err <0 ) { +pr_err ("%s: Error in netlink_unicast, err=%d\n" , __func__, err); +nlmsg_free (msg); +return err; + } + +kfree (new_value); +return 0 ; +} + +static int parallel_genl_get_str_value (struct sk_buff *skb, +struct genl_info *info) +{ +struct sk_buff *msg; +void *msg_head; +struct nlattr *na_path; +char *sysfs_path; +char *value; +int err; +int code; + + msg =genlmsg_new (NLMSG_GOODSIZE ,GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + msg_head =genlmsg_put (msg, info->snd_portid, info->snd_seq, + &my_genl_family_parallel,0 , +PARALLEL_GENL_CMD_GET_VALUE ); +if (!msg_head) { +nlmsg_free (msg); +return -ENOMEM ; + } + +if (!info->attrs[PARALLEL_GENL_ATTR_PATH ]) { +nlmsg_free (msg); +return -EINVAL ; + } +genl_unlock (); + na_path = info->attrs[PARALLEL_GENL_ATTR_PATH ]; + sysfs_path =nla_data (na_path); +genl_lock (); + +if (strcmp (sysfs_path,PATH_PARALLEL_GENL_MES ) !=0 ) { +pr_err ("%s: Incorrect path: %s\n" , __func__, sysfs_path); + goto error; + } + + value =kmalloc (MAX_DATA_LEN ,GFP_KERNEL ); +if (!value) +return -ENOMEM ; + +strcpy (value, sysfs_data.parallel_genl_message); + +if (nla_put_string (msg,PARALLEL_GENL_ATTR_DATA , value)) { +genlmsg_cancel (msg, msg_head); +nlmsg_free (msg); +kfree (value); +return -EMSGSIZE ; + } + +genlmsg_end (msg, msg_head); + +if (info) { + err =genlmsg_reply (msg, info); +if (err !=0 ) { +pr_err ("%s: Error in genlmsg_reply, err=%d\n" , + __func__, err); +nlmsg_free (msg); +kfree (value); +return err; + } + } +kfree (value); + +return 0 ; + +error: +nlmsg_free (msg); + code = -ENOENT ; +netlink_set_err (skb->sk,0 ,MY_MCGRP_GENL , code); +return -ENOENT ; +} + +struct parallel_data { +char *name; +char *desc; +}; + +struct parallel_data data[] = { + {"TEST_GENL" ,"one" }, + {"PARALLEL_GENL" ,"two" }, + {"THIRD_GENL" ,"three" }, + {"LARGE_GENL" ,"four" }, +}; + +#define DATA_SIZE ARRAY_SIZE(data) + +static int parallel_genl_dump_start (struct netlink_callback *cb) +{ +pr_info ("%s: Dump is started" , __func__); +return 0 ; +} + +static int __parallel_genl_fill_info (struct parallel_data *info, +struct sk_buff *msg) +{ +if (nla_put_string (msg,PARALLEL_GENL_ATTR_NAME , info->name) || +nla_put_string (msg,PARALLEL_GENL_ATTR_DESC , info->desc)) +return -EMSGSIZE ; + +return 0 ; +} + +static int __parallel_genl_dump_element (struct parallel_data *info,u32 portid, +u32 seq,u32 flags,struct sk_buff *skb, +u8 cmd) +{ +void *hdr; + + hdr =genlmsg_put (skb, portid, seq, &my_genl_family_parallel, flags, + cmd); +if (!hdr) +return -ENOMEM ; + +if (__parallel_genl_fill_info (info, skb) <0 ) + goto nla_put_failure; + +genlmsg_end (skb, hdr); +return 0 ; + +nla_put_failure: +genlmsg_cancel (skb, hdr); +return -EMSGSIZE ; +} + +static int parallel_genl_dump_info (struct sk_buff *skb, +struct netlink_callback *cb) +{ +int ret; +int idx = cb->args[0 ]; + +for (;;) { +if (idx >=DATA_SIZE ) +return 0 ; + + ret =__parallel_genl_dump_element (&data[idx], +NETLINK_CB (cb->skb).portid, + cb->nlh->nlmsg_seq, +NLM_F_MULTI , skb, +PARALLEL_GENL_CMD_DUMP_INFO ); + +if (ret) { +pr_err ("%s: __parallel_genl_dump_element failed: %d\n" , __func__, ret); +return ret; + } + + cb->args[0 ]++; + idx++; + } + +return ret; +} + +static int parallel_genl_dump_done (struct netlink_callback *cb) +{ +pr_info ("%s: Dump is done" , __func__); +return 0 ; +} + +// Generic Netlink operations for PARALLEL_GENL family +static const struct genl_ops parallel_genl_ops[] = { + { + .cmd =PARALLEL_GENL_CMD_SEND , + .flags =0 , + .policy = parallel_genl_policy, + .doit = parallel_genl_send, + .dumpit =NULL , + }, + { + .cmd =PARALLEL_GENL_CMD_DUMP_INFO , + .flags =0 , + .policy = parallel_genl_policy, + .start = parallel_genl_dump_start, + .dumpit = parallel_genl_dump_info, + .done = parallel_genl_dump_done, + }, + { + .cmd =PARALLEL_GENL_CMD_SET_VALUE , + .flags =0 , + .policy =NULL , + .doit = parallel_genl_set_str_value, + .dumpit =NULL , + }, + { + .cmd =PARALLEL_GENL_CMD_GET_VALUE , + .flags =GENL_UNS_ADMIN_PERM , + .policy = parallel_genl_policy, + .doit = parallel_genl_get_str_value, + }, +}; + +enum my_multicast_many_groups_two { +MCGRP_TWO_1 , +MCGRP_TWO_2 , +MCGRP_TWO_3 , +MCGRP_TWO_4 , +MCGRP_TWO_5 , +MCGRP_TWO_6 , +MCGRP_TWO_7 , +MCGRP_TWO_8 , +MCGRP_TWO_9 , +MCGRP_TWO_10 , +MCGRP_TWO_11 , +MCGRP_TWO_12 , +MCGRP_TWO_13 , +MCGRP_TWO_14 , +MCGRP_TWO_15 , +MCGRP_TWO_16 , +MCGRP_TWO_17 , +MCGRP_TWO_18 , +MCGRP_TWO_19 , +MCGRP_TWO_20 , +MCGRP_TWO_21 , +MCGRP_TWO_22 , +MCGRP_TWO_23 , +MCGRP_TWO_24 , +MCGRP_TWO_25 , +MCGRP_TWO_26 , +MCGRP_TWO_27 , +MCGRP_TWO_28 , +MCGRP_TWO_29 , +MCGRP_TWO_30 , +MCGRP_TWO_31 , +MCGRP_TWO_32 , +MCGRP_TWO_33 , +MCGRP_TWO_34 , +MCGRP_TWO_35 , +MCGRP_TWO_36 , +MCGRP_TWO_37 , +MCGRP_TWO_38 , +MCGRP_TWO_39 , +MCGRP_TWO_40 , +MCGRP_TWO_41 , +MCGRP_TWO_42 , +MCGRP_TWO_43 , +MCGRP_TWO_44 , +MCGRP_TWO_45 , +MCGRP_TWO_46 , +MCGRP_TWO_47 , +MCGRP_TWO_48 , +MCGRP_TWO_49 , +MCGRP_TWO_50 , +MCGRP_TWO_51 , +MCGRP_TWO_52 , +MCGRP_TWO_53 , +MCGRP_TWO_54 , +MCGRP_TWO_55 , +MCGRP_TWO_56 , +MCGRP_TWO_57 , +MCGRP_TWO_58 , +MCGRP_TWO_59 , +MCGRP_TWO_60 , +MCGRP_TWO_61 , +MCGRP_TWO_62 , +MCGRP_TWO_63 , +MCGRP_TWO_64 , +MCGRP_TWO_65 , +MCGRP_TWO_66 , +MCGRP_TWO_67 , +MCGRP_TWO_68 , +MCGRP_TWO_69 , +}; + +static const struct genl_multicast_group genl_many_mcgrps_two[] = { + [MCGRP_TWO_1 ] = { .name ="MCGRP_TWO_1" , }, + [MCGRP_TWO_2 ] = { .name ="MCGRP_TWO_2" , }, + [MCGRP_TWO_3 ] = { .name ="MCGRP_TWO_3" , }, + [MCGRP_TWO_4 ] = { .name ="MCGRP_TWO_4" , }, + [MCGRP_TWO_5 ] = { .name ="MCGRP_TWO_5" , }, + [MCGRP_TWO_6 ] = { .name ="MCGRP_TWO_6" , }, + [MCGRP_TWO_7 ] = { .name ="MCGRP_TWO_7" , }, + [MCGRP_TWO_8 ] = { .name ="MCGRP_TWO_8" , }, + [MCGRP_TWO_9 ] = { .name ="MCGRP_TWO_9" , }, + [MCGRP_TWO_10 ] = { .name ="MCGRP_TWO_10" , }, + [MCGRP_TWO_11 ] = { .name ="MCGRP_TWO_11" , }, + [MCGRP_TWO_12 ] = { .name ="MCGRP_TWO_12" , }, + [MCGRP_TWO_13 ] = { .name ="MCGRP_TWO_13" , }, + [MCGRP_TWO_14 ] = { .name ="MCGRP_TWO_14" , }, + [MCGRP_TWO_15 ] = { .name ="MCGRP_TWO_15" , }, + [MCGRP_TWO_16 ] = { .name ="MCGRP_TWO_16" , }, + [MCGRP_TWO_17 ] = { .name ="MCGRP_TWO_17" , }, + [MCGRP_TWO_18 ] = { .name ="MCGRP_TWO_18" , }, + [MCGRP_TWO_19 ] = { .name ="MCGRP_TWO_19" , }, + [MCGRP_TWO_20 ] = { .name ="MCGRP_TWO_20" , }, + [MCGRP_TWO_21 ] = { .name ="MCGRP_TWO_21" , }, + [MCGRP_TWO_22 ] = { .name ="MCGRP_TWO_22" , }, + [MCGRP_TWO_23 ] = { .name ="MCGRP_TWO_23" , }, + [MCGRP_TWO_24 ] = { .name ="MCGRP_TWO_24" , }, + [MCGRP_TWO_25 ] = { .name ="MCGRP_TWO_25" , }, + [MCGRP_TWO_26 ] = { .name ="MCGRP_TWO_26" , }, + [MCGRP_TWO_27 ] = { .name ="MCGRP_TWO_27" , }, + [MCGRP_TWO_28 ] = { .name ="MCGRP_TWO_28" , }, + [MCGRP_TWO_29 ] = { .name ="MCGRP_TWO_29" , }, + [MCGRP_TWO_30 ] = { .name ="MCGRP_TWO_30" , }, + [MCGRP_TWO_31 ] = { .name ="MCGRP_TWO_31" , }, + [MCGRP_TWO_32 ] = { .name ="MCGRP_TWO_32" , }, + [MCGRP_TWO_33 ] = { .name ="MCGRP_TWO_33" , }, + [MCGRP_TWO_34 ] = { .name ="MCGRP_TWO_34" , }, + [MCGRP_TWO_35 ] = { .name ="MCGRP_TWO_35" , }, + [MCGRP_TWO_36 ] = { .name ="MCGRP_TWO_36" , }, + [MCGRP_TWO_37 ] = { .name ="MCGRP_TWO_37" , }, + [MCGRP_TWO_38 ] = { .name ="MCGRP_TWO_38" , }, + [MCGRP_TWO_39 ] = { .name ="MCGRP_TWO_39" , }, + [MCGRP_TWO_40 ] = { .name ="MCGRP_TWO_40" , }, + [MCGRP_TWO_41 ] = { .name ="MCGRP_TWO_41" , }, + [MCGRP_TWO_42 ] = { .name ="MCGRP_TWO_42" , }, + [MCGRP_TWO_43 ] = { .name ="MCGRP_TWO_43" , }, + [MCGRP_TWO_44 ] = { .name ="MCGRP_TWO_44" , }, + [MCGRP_TWO_45 ] = { .name ="MCGRP_TWO_45" , }, + [MCGRP_TWO_46 ] = { .name ="MCGRP_TWO_46" , }, + [MCGRP_TWO_47 ] = { .name ="MCGRP_TWO_47" , }, + [MCGRP_TWO_48 ] = { .name ="MCGRP_TWO_48" , }, + [MCGRP_TWO_49 ] = { .name ="MCGRP_TWO_49" , }, + [MCGRP_TWO_50 ] = { .name ="MCGRP_TWO_50" , }, + [MCGRP_TWO_51 ] = { .name ="MCGRP_TWO_51" , }, + [MCGRP_TWO_52 ] = { .name ="MCGRP_TWO_52" , }, + [MCGRP_TWO_53 ] = { .name ="MCGRP_TWO_53" , }, + [MCGRP_TWO_54 ] = { .name ="MCGRP_TWO_54" , }, + [MCGRP_TWO_55 ] = { .name ="MCGRP_TWO_55" , }, + [MCGRP_TWO_56 ] = { .name ="MCGRP_TWO_56" , }, + [MCGRP_TWO_57 ] = { .name ="MCGRP_TWO_57" , }, + [MCGRP_TWO_58 ] = { .name ="MCGRP_TWO_58" , }, + [MCGRP_TWO_59 ] = { .name ="MCGRP_TWO_59" , }, + [MCGRP_TWO_60 ] = { .name ="MCGRP_TWO_60" , }, + [MCGRP_TWO_61 ] = { .name ="MCGRP_TWO_61" , }, + [MCGRP_TWO_62 ] = { .name ="MCGRP_TWO_62" , }, + [MCGRP_TWO_63 ] = { .name ="MCGRP_TWO_63" , }, + [MCGRP_TWO_64 ] = { .name ="MCGRP_TWO_64" , }, + [MCGRP_TWO_65 ] = { .name ="MCGRP_TWO_65" , }, + [MCGRP_TWO_66 ] = { .name ="MCGRP_TWO_66" , }, + [MCGRP_TWO_67 ] = { .name ="MCGRP_TWO_67" , }, + [MCGRP_TWO_68 ] = { .name ="MCGRP_TWO_68" , }, + [MCGRP_TWO_69 ] = { .name ="MCGRP_TWO_69" , }, +}; + +// genl_family struct for PARALLEL_GENL family +static struct genl_family my_genl_family_parallel = { + .hdrsize =0 , + .name =PARALLEL_GENL_FAMILY_NAME , + .version =1 , + .maxattr =PARALLEL_GENL_ATTR_MAX , + .netnsok = true, + .parallel_ops = true, + .ops = parallel_genl_ops, + .n_ops =ARRAY_SIZE (parallel_genl_ops), +/** .policy = parallel_genl_policy, +* needs to delete policy from family to test reject policy +*/ + .resv_start_op =PARALLEL_GENL_CMD_DUMP_INFO +1 , + .mcgrps = genl_many_mcgrps_two, + .n_mcgrps =ARRAY_SIZE (genl_many_mcgrps_two), +}; +static int __init init_netlink (void ) {int rc;
@@ -556,8 +1089,16 @@ static int __init init_netlink(void) goto failure_1; } + rc =genl_register_family (&my_genl_family_parallel); +if (rc) { +pr_err ("%s: Failed to register Generic Netlink family\n" , __func__); + goto failure_2; + } +return 0 ; +failure_2: +genl_unregister_family (&my_genl_family); failure_1:pr_debug ("%s: My module. Error occurred in %s\n" , __func__, __func__);return rc;
@@ -599,8 +1140,7 @@ static int __init init_sysfs_parallel_genl(void)return -ENOMEM ; } - ret =sysfs_create_file (kobj_parallel_genl, - &my_attr_str_parallel_genl.attr); + ret =sysfs_create_file (kobj_parallel_genl, &my_attr_str_parallel_genl.attr);if (ret) {pr_err ("%s: Failed to create sysfs file\n" , __func__); goto err_sysfs;
@@ -694,6 +1234,7 @@ static int __init module_netlink_init(void) static void __exit module_netlink_exit(void) {genl_unregister_family (&my_genl_family); +genl_unregister_family (&my_genl_family_parallel);sysfs_remove_file (kobj_genl_test, &my_attr_u32_genl_test.attr);sysfs_remove_file (kobj_genl_test, &my_attr_str_genl_test.attr);
Signature · 1 line
2.34.1
PATCH 4/15 genetlink: add test case for duplicate genl family registration
In reply to Yana BashlykovaAdd incorrect_genl_family struct with duplicate name to test error handling when registering families with conflicting names.
--- .../net-pf-16-proto-16-family-PARALLEL_GENL.c | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+)
drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c +35 -0
@@ -1077,6 +1077,18 @@ static struct genl_family my_genl_family_parallel = { .n_mcgrps = ARRAY_SIZE(genl_many_mcgrps_two), }; +// genl_family struct with incorrect name +static struct genl_family incorrect_genl_family = { + .hdrsize =0 , + .name =MY_GENL_FAMILY_NAME ,// such family already exists + .version =MY_GENL_VERSION , + .maxattr =MY_GENL_ATTR_MAX , + .netnsok = true, + .ops = my_genl_ops, + .n_ops =ARRAY_SIZE (my_genl_ops), + .policy = my_genl_policy, +}; +static int __init init_netlink (void ) {int rc;
@@ -1104,6 +1116,25 @@ static int __init init_netlink(void)return rc; } +static int __init incorrect_init_netlink (void ) +{ +int rc; + +pr_info ("%s: My module. Initializing incorrect Netlink\n" , __func__); + + rc =genl_register_family (&incorrect_genl_family); +if (rc) { +pr_err ("%s: Failed to register incorrect Generic Netlink family\n" , __func__); + goto failure; + } + +return 0 ; + +failure: +pr_info ("%s: Error was handled correctly\n" , __func__); +return -EINVAL ; +} +static int __init init_sysfs_third_genl (void ) {int ret;
@@ -1215,6 +1246,10 @@ static int __init module_netlink_init(void) goto err_sysfs;pr_info ("%s: New families are registered\n" , __func__); + ret =incorrect_init_netlink (); +if (ret) +pr_err ("%s: Incorrect Generic Netlink family wasn't registered\n" , __func__); +return 0 ; err_sysfs:
Signature · 1 line
2.34.1
PATCH 5/15 genetlink: add test case for family with invalid ops
In reply to Yana BashlykovaAdd test case that verifies error handling when registering a genetlink family with invalid operations where both doit and dumpit callbacks are NULL.
The test registers incorrect_ops_genl_family which contains a command with:
- .doit = NULL
- .dumpit = NULL
and expects the registration to fail with -EINVAL.
This validates proper validation of genetlink operations during family registration.
--- .../net-pf-16-proto-16-family-PARALLEL_GENL.c | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+)
drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c +41 -0
@@ -1089,6 +1089,33 @@ static struct genl_family incorrect_genl_family = { .policy = my_genl_policy, }; +enum { +INCORRECT_OP_WITH_NULL , +}; + +// Generic Netlink operations with incorrect ops +static const struct genl_ops incorrect_ops_with_null[] = { + { + .cmd =INCORRECT_OP_WITH_NULL , + .flags =0 , + .policy = my_genl_policy,// random policy + .doit =NULL ,// doit and dumpit are NULL --> kernel will send -EINVAL + .dumpit =NULL , + }, +}; + +// genl_family struct with incorrect ops +static struct genl_family incorrect_ops_genl_family = { + .hdrsize =0 , + .name ="INCORRECT" , + .version =1 , + .maxattr =1 , + .netnsok = true, + .ops = incorrect_ops_with_null,// ops contain NULL + .n_ops =ARRAY_SIZE (incorrect_ops_with_null), + .policy = my_genl_policy,// random policy +}; +static int __init init_netlink (void ) {int rc;
@@ -1116,6 +1143,16 @@ static int __init init_netlink(void)return rc; } +static int __init incorrect_ops_netlink (void ) +{ +int ret; + + ret =genl_register_family (&incorrect_ops_genl_family); +if (ret != -EINVAL ) +return ret; +return 0 ; +} +static int __init incorrect_init_netlink (void ) {int rc;
@@ -1241,6 +1278,10 @@ static int __init module_netlink_init(void)if (ret) goto err_sysfs; + ret =incorrect_ops_netlink (); +if (ret) + goto err_sysfs; + ret =init_netlink ();if (ret) goto err_sysfs;
Signature · 1 line
2.34.1
PATCH 6/15 genetlink: add netlink notifier support
In reply to Yana BashlykovaAdd basic netlink notifier to monitor genetlink events:
- Simple ntf_genl_event() callback returning NOTIFY_OK
- Register/unregister in module init/exit
- Error handling for registration
Prepares infrastructure for tracking family registration events.
--- .../net-pf-16-proto-16-family-PARALLEL_GENL.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c +19 -0
@@ -1116,6 +1116,16 @@ static struct genl_family incorrect_ops_genl_family = { .policy = my_genl_policy, // random policy }; +static int ntf_genl_event (struct notifier_block *nb,unsigned long state, +void *_notify) +{ +return NOTIFY_OK ; +} + +static struct notifier_block genl_notifier = { + .notifier_call = ntf_genl_event, +}; +static int __init init_netlink (void ) {int rc;
@@ -1291,8 +1301,16 @@ static int __init module_netlink_init(void)if (ret)pr_err ("%s: Incorrect Generic Netlink family wasn't registered\n" , __func__); + ret =netlink_register_notifier (&genl_notifier); +if (ret) + goto err_family; +return 0 ; +// netlink_unregister_notifier(&genl_notifier); +err_family: +genl_unregister_family (&my_genl_family); +genl_unregister_family (&my_genl_family_parallel); err_sysfs:sysfs_remove_file (kobj_genl_test, &my_attr_u32_genl_test.attr);sysfs_remove_file (kobj_genl_test, &my_attr_str_genl_test.attr);
@@ -1309,6 +1327,7 @@ static int __init module_netlink_init(void) static void __exit module_netlink_exit(void) { +netlink_unregister_notifier (&genl_notifier);genl_unregister_family (&my_genl_family);genl_unregister_family (&my_genl_family_parallel);
Signature · 1 line
2.34.1
PATCH 7/15 genetlink: add THIRD_GENL family
In reply to Yana Bashlykova- New family with echo command
- Supports string and flag attributes
- Sysfs interface at /sys/kernel/third_genl/message
--- .../net-pf-16-proto-16-family-PARALLEL_GENL.c | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+)
drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c +103 -0
@@ -152,10 +152,13 @@ static DEFINE_MUTEX(genl_mutex);#define PARALLEL_GENL_FAMILY_NAME "PARALLEL_GENL" +#define THIRD_GENL_FAMILY_NAME "THIRD_GENL" +#define PATH_GENL_TEST_NUM "/sys/kernel/genl_test/value"#define PATH_GENL_TEST_MES "/sys/kernel/genl_test/message"#define PATH_GENL_TEST_DEV "/sys/kernel/genl_test/some_info"#define PATH_PARALLEL_GENL_MES "/sys/kernel/parallel_genl/message" +#define PATH_THIRD_GENL_MES "/sys/kernel/third_genl/message"// TEST_GENL enum {
@@ -205,6 +208,8 @@ static struct genl_family my_genl_family;static struct genl_family my_genl_family_parallel; +static struct genl_family third_genl_family; +enum my_multicast_groups {MY_MCGRP_GENL , };
@@ -1077,6 +1082,94 @@ static struct genl_family my_genl_family_parallel = { .n_mcgrps = ARRAY_SIZE(genl_many_mcgrps_two), }; +// THIRD_GENL +enum { +THIRD_GENL_ATTR_UNSPEC , +THIRD_GENL_ATTR_DATA , +THIRD_GENL_ATTR_FLAG , + __THIRD_GENL_ATTR_MAX, +}; + +#define THIRD_GENL_ATTR_MAX (__THIRD_GENL_ATTR_MAX - 1) + +enum { +THIRD_GENL_CMD_UNSPEC , +THIRD_GENL_CMD_ECHO , + __THIRD_GENL_CMD_MAX, +}; + +#define THIRD_GENL_CMD_MAX (__THIRD_GENL_CMD_MAX - 1) + +static const struct nla_policy third_genl_policy[THIRD_GENL_ATTR_MAX +1 ] = { + [THIRD_GENL_ATTR_UNSPEC ] = { .type =NLA_UNSPEC }, + [THIRD_GENL_ATTR_DATA ] = { .type =NLA_STRING }, + [THIRD_GENL_ATTR_FLAG ] = { .type =NLA_FLAG }, +}; + +// Functions for THIRD_GENL family +static int third_genl_echo (struct sk_buff *skb,struct genl_info *info) +{ +struct sk_buff *msg; +void *data; +int ret; +char *str; + +if (info->nlhdr->nlmsg_flags &NLM_F_ECHO ) { + msg =genlmsg_new (GENLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + data =genlmsg_put_reply (msg, info, &my_genl_family,0 , +THIRD_GENL_CMD_ECHO ); +if (!data) + goto error; + + str ="Hello from THIRD_GENL!" ; +strcpy (sysfs_data.third_genl_message, str); + + ret =nla_put_string (msg,THIRD_GENL_ATTR_DATA , str); +if (ret <0 ) + goto error; + + ret =nla_put_flag (msg,THIRD_GENL_ATTR_FLAG ); +if (ret <0 ) + goto error; + +genlmsg_end (msg, data); + +genlmsg_reply (msg, info); + } + +return 0 ; + +error: +nlmsg_free (msg); +return -EMSGSIZE ; +} + +// Generic Netlink operations for THIRD_GENL family +static const struct genl_ops third_genl_ops[] = { + { + .cmd =THIRD_GENL_CMD_ECHO , + .flags =0 , + .policy = third_genl_policy, + .doit = third_genl_echo, + .dumpit =NULL , + }, +}; + +// genl_family struct for THIRD_GENL family +static struct genl_family third_genl_family = { + .hdrsize =0 , + .name =THIRD_GENL_FAMILY_NAME , + .version =1 , + .maxattr =THIRD_GENL_ATTR_MAX , + .netnsok = true, + .ops = third_genl_ops, + .n_ops =ARRAY_SIZE (third_genl_ops), + .policy = third_genl_policy, +}; +// genl_family struct with incorrect name static struct genl_family incorrect_genl_family = { .hdrsize =0 ,
@@ -1144,8 +1237,16 @@ static int __init init_netlink(void) goto failure_2; } + rc =genl_register_family (&third_genl_family); +if (rc) { +pr_err ("%s: Failed to register Generic Netlink family\n" , __func__); + goto failure_3; + } +return 0 ; +failure_3: +genl_unregister_family (&my_genl_family_parallel); failure_2:genl_unregister_family (&my_genl_family); failure_1:
@@ -1311,6 +1412,7 @@ static int __init module_netlink_init(void) err_family:genl_unregister_family (&my_genl_family);genl_unregister_family (&my_genl_family_parallel); +genl_unregister_family (&third_genl_family); err_sysfs:sysfs_remove_file (kobj_genl_test, &my_attr_u32_genl_test.attr);sysfs_remove_file (kobj_genl_test, &my_attr_str_genl_test.attr);
@@ -1330,6 +1432,7 @@ static void __exit module_netlink_exit(void)netlink_unregister_notifier (&genl_notifier);genl_unregister_family (&my_genl_family);genl_unregister_family (&my_genl_family_parallel); +genl_unregister_family (&third_genl_family);sysfs_remove_file (kobj_genl_test, &my_attr_u32_genl_test.attr);sysfs_remove_file (kobj_genl_test, &my_attr_str_genl_test.attr);
Signature · 1 line
2.34.1
PATCH 8/15 genetlink: verify unregister fails for non-registered family
In reply to Yana BashlykovaTest that genl_unregister_family() returns error
when called on non-registered family.
--- .../net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c +4 -0
@@ -1402,6 +1402,10 @@ static int __init module_netlink_init(void)if (ret)pr_err ("%s: Incorrect Generic Netlink family wasn't registered\n" , __func__); + ret =genl_unregister_family (&incorrect_genl_family); +if (ret) +pr_err ("%s: Incorrect Generic Netlink family wasn't unregistered\n" , __func__); + ret =netlink_register_notifier (&genl_notifier);if (ret) goto err_family;
Signature · 1 line
2.34.1
PATCH 9/15 genetlink: add LARGE_GENL stress test family
In reply to Yana BashlykovaAdded new test family with 190+ multicast groups to verify:
- Handling of large number of multicast groups
- Family registration with many groups
The family serves as a stress test for Generic Netlink scalability.
--- .../net-pf-16-proto-16-family-PARALLEL_GENL.c | 468 +++++++++++++++++- 1 file changed, 467 insertions(+), 1 deletion(-)
drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c +467 -1
@@ -1,4 +1,16 @@// SPDX-License-Identifier: GPL-2.0 +/* +* Generic Netlink Test Module +* +* Implements test families for Generic Netlink functionality: +* - TEST_GENL: Basic commands with mutex protection +* - PARALLEL_GENL: Advanced ops with parallel dump support +* - THIRD_GENL: Simple message handling supporting many multicast groups +* - LARGE_GENL: Stress test with 190+ multicast groups +* +* Includes sysfs interfaces for manual testing and validation +* of error cases and edge conditions. +*/ #include <linux/init.h> #include <linux/kernel.h>
@@ -146,6 +158,7 @@ static struct kobj_attribute my_attr_str_third_genl =__ATTR (message,0664 , show_third_genl_message, store_third_genl_message);static DEFINE_MUTEX (genl_mutex); +static DEFINE_MUTEX (sysfs_mutex);#define MY_GENL_FAMILY_NAME "TEST_GENL"#define MY_GENL_VERSION 1
@@ -154,6 +167,8 @@ static DEFINE_MUTEX(genl_mutex);#define THIRD_GENL_FAMILY_NAME "THIRD_GENL" +#define LARGE_GENL_FAMILY_NAME "LARGE_GENL" +#define PATH_GENL_TEST_NUM "/sys/kernel/genl_test/value"#define PATH_GENL_TEST_MES "/sys/kernel/genl_test/message"#define PATH_GENL_TEST_DEV "/sys/kernel/genl_test/some_info"
@@ -920,6 +935,410 @@ static const struct genl_ops parallel_genl_ops[] = { }, }; +enum my_multicast_many_groups_one { +MCGRP_1 , +MCGRP_2 , +MCGRP_3 , +MCGRP_4 , +MCGRP_5 , +MCGRP_6 , +MCGRP_7 , +MCGRP_8 , +MCGRP_9 , +MCGRP_10 , +MCGRP_11 , +MCGRP_12 , +MCGRP_13 , +MCGRP_14 , +MCGRP_15 , +MCGRP_16 , +MCGRP_17 , +MCGRP_18 , +MCGRP_19 , +MCGRP_20 , +MCGRP_21 , +MCGRP_22 , +MCGRP_23 , +MCGRP_24 , +MCGRP_25 , +MCGRP_26 , +MCGRP_27 , +MCGRP_28 , +MCGRP_29 , +MCGRP_30 , +MCGRP_31 , +MCGRP_32 , +MCGRP_33 , +MCGRP_34 , +MCGRP_35 , +MCGRP_36 , +MCGRP_37 , +MCGRP_38 , +MCGRP_39 , +MCGRP_40 , +MCGRP_41 , +MCGRP_42 , +MCGRP_43 , +MCGRP_44 , +MCGRP_45 , +MCGRP_46 , +MCGRP_47 , +MCGRP_48 , +MCGRP_49 , +MCGRP_50 , +MCGRP_51 , +MCGRP_52 , +MCGRP_53 , +MCGRP_54 , +MCGRP_55 , +MCGRP_56 , +MCGRP_57 , +MCGRP_58 , +MCGRP_59 , +MCGRP_60 , +MCGRP_61 , +MCGRP_62 , +MCGRP_63 , +MCGRP_64 , +MCGRP_65 , +MCGRP_66 , +MCGRP_67 , +MCGRP_68 , +MCGRP_69 , +MCGRP_70 , +MCGRP_71 , +MCGRP_72 , +MCGRP_73 , +MCGRP_74 , +MCGRP_75 , +MCGRP_76 , +MCGRP_77 , +MCGRP_78 , +MCGRP_79 , +MCGRP_80 , +MCGRP_81 , +MCGRP_82 , +MCGRP_83 , +MCGRP_84 , +MCGRP_85 , +MCGRP_86 , +MCGRP_87 , +MCGRP_88 , +MCGRP_89 , +MCGRP_90 , +MCGRP_91 , +MCGRP_92 , +MCGRP_93 , +MCGRP_94 , +MCGRP_95 , +MCGRP_96 , +MCGRP_97 , +MCGRP_98 , +MCGRP_99 , +MCGRP_100 , +MCGRP_101 , +MCGRP_102 , +MCGRP_103 , +MCGRP_104 , +MCGRP_105 , +MCGRP_106 , +MCGRP_107 , +MCGRP_108 , +MCGRP_109 , +MCGRP_110 , +MCGRP_111 , +MCGRP_112 , +MCGRP_113 , +MCGRP_114 , +MCGRP_115 , +MCGRP_116 , +MCGRP_117 , +MCGRP_118 , +MCGRP_119 , +MCGRP_120 , +MCGRP_121 , +MCGRP_122 , +MCGRP_123 , +MCGRP_124 , +MCGRP_125 , +MCGRP_126 , +MCGRP_127 , +MCGRP_128 , +MCGRP_129 , +MCGRP_130 , +MCGRP_131 , +MCGRP_132 , +MCGRP_133 , +MCGRP_134 , +MCGRP_135 , +MCGRP_136 , +MCGRP_137 , +MCGRP_138 , +MCGRP_139 , +MCGRP_140 , +MCGRP_141 , +MCGRP_142 , +MCGRP_143 , +MCGRP_144 , +MCGRP_145 , +MCGRP_146 , +MCGRP_147 , +MCGRP_148 , +MCGRP_149 , +MCGRP_150 , +MCGRP_151 , +MCGRP_152 , +MCGRP_153 , +MCGRP_154 , +MCGRP_155 , +MCGRP_156 , +MCGRP_157 , +MCGRP_158 , +MCGRP_159 , +MCGRP_160 , +MCGRP_161 , +MCGRP_162 , +MCGRP_163 , +MCGRP_164 , +MCGRP_165 , +MCGRP_166 , +MCGRP_167 , +MCGRP_168 , +MCGRP_169 , +MCGRP_170 , +MCGRP_171 , +MCGRP_172 , +MCGRP_173 , +MCGRP_174 , +MCGRP_175 , +MCGRP_176 , +MCGRP_177 , +MCGRP_178 , +MCGRP_179 , +MCGRP_180 , +MCGRP_181 , +MCGRP_182 , +MCGRP_183 , +MCGRP_184 , +MCGRP_185 , +MCGRP_186 , +MCGRP_187 , +MCGRP_188 , +MCGRP_189 , +MCGRP_190 , +MCGRP_191 , +MCGRP_192 , +MCGRP_193 , +MCGRP_194 , +MCGRP_195 , +MCGRP_196 , +MCGRP_197 , +MCGRP_198 , +MCGRP_199 , +}; + +static const struct genl_multicast_group genl_many_mcgrps_one[] = { + [MCGRP_1 ] = { .name ="MCGRP_1" , }, + [MCGRP_2 ] = { .name ="MCGRP_2" , }, + [MCGRP_3 ] = { .name ="MCGRP_3" , }, + [MCGRP_4 ] = { .name ="MCGRP_4" , }, + [MCGRP_5 ] = { .name ="MCGRP_5" , }, + [MCGRP_6 ] = { .name ="MCGRP_6" , }, + [MCGRP_7 ] = { .name ="MCGRP_7" , }, + [MCGRP_8 ] = { .name ="MCGRP_8" , }, + [MCGRP_9 ] = { .name ="MCGRP_9" , }, + [MCGRP_10 ] = { .name ="MCGRP_10" , }, + [MCGRP_11 ] = { .name ="MCGRP_11" , }, + [MCGRP_12 ] = { .name ="MCGRP_12" , }, + [MCGRP_13 ] = { .name ="MCGRP_13" , }, + [MCGRP_14 ] = { .name ="MCGRP_14" , }, + [MCGRP_15 ] = { .name ="MCGRP_15" , }, + [MCGRP_16 ] = { .name ="MCGRP_16" , }, + [MCGRP_17 ] = { .name ="MCGRP_17" , }, + [MCGRP_18 ] = { .name ="MCGRP_18" , }, + [MCGRP_19 ] = { .name ="MCGRP_19" , }, + [MCGRP_20 ] = { .name ="MCGRP_20" , }, + [MCGRP_21 ] = { .name ="MCGRP_21" , }, + [MCGRP_22 ] = { .name ="MCGRP_22" , }, + [MCGRP_23 ] = { .name ="MCGRP_23" , }, + [MCGRP_24 ] = { .name ="MCGRP_24" , }, + [MCGRP_25 ] = { .name ="MCGRP_25" , }, + [MCGRP_26 ] = { .name ="MCGRP_26" , }, + [MCGRP_27 ] = { .name ="MCGRP_27" , }, + [MCGRP_28 ] = { .name ="MCGRP_28" , }, + [MCGRP_29 ] = { .name ="MCGRP_29" , }, + [MCGRP_30 ] = { .name ="MCGRP_30" , }, + [MCGRP_31 ] = { .name ="MCGRP_31" , }, + [MCGRP_32 ] = { .name ="MCGRP_32" , }, + [MCGRP_33 ] = { .name ="MCGRP_33" , }, + [MCGRP_34 ] = { .name ="MCGRP_34" , }, + [MCGRP_35 ] = { .name ="MCGRP_35" , }, + [MCGRP_36 ] = { .name ="MCGRP_36" , }, + [MCGRP_37 ] = { .name ="MCGRP_37" , }, + [MCGRP_38 ] = { .name ="MCGRP_38" , }, + [MCGRP_39 ] = { .name ="MCGRP_39" , }, + [MCGRP_40 ] = { .name ="MCGRP_40" , }, + [MCGRP_41 ] = { .name ="MCGRP_41" , }, + [MCGRP_42 ] = { .name ="MCGRP_42" , }, + [MCGRP_43 ] = { .name ="MCGRP_43" , }, + [MCGRP_44 ] = { .name ="MCGRP_44" , }, + [MCGRP_45 ] = { .name ="MCGRP_45" , }, + [MCGRP_46 ] = { .name ="MCGRP_46" , }, + [MCGRP_47 ] = { .name ="MCGRP_47" , }, + [MCGRP_48 ] = { .name ="MCGRP_48" , }, + [MCGRP_49 ] = { .name ="MCGRP_49" , }, + [MCGRP_50 ] = { .name ="MCGRP_50" , }, + [MCGRP_51 ] = { .name ="MCGRP_51" , }, + [MCGRP_52 ] = { .name ="MCGRP_52" , }, + [MCGRP_53 ] = { .name ="MCGRP_53" , }, + [MCGRP_54 ] = { .name ="MCGRP_54" , }, + [MCGRP_55 ] = { .name ="MCGRP_55" , }, + [MCGRP_56 ] = { .name ="MCGRP_56" , }, + [MCGRP_57 ] = { .name ="MCGRP_57" , }, + [MCGRP_58 ] = { .name ="MCGRP_58" , }, + [MCGRP_59 ] = { .name ="MCGRP_59" , }, + [MCGRP_60 ] = { .name ="MCGRP_60" , }, + [MCGRP_61 ] = { .name ="MCGRP_61" , }, + [MCGRP_62 ] = { .name ="MCGRP_62" , }, + [MCGRP_63 ] = { .name ="MCGRP_63" , }, + [MCGRP_64 ] = { .name ="MCGRP_64" , }, + [MCGRP_65 ] = { .name ="MCGRP_65" , }, + [MCGRP_66 ] = { .name ="MCGRP_66" , }, + [MCGRP_67 ] = { .name ="MCGRP_67" , }, + [MCGRP_68 ] = { .name ="MCGRP_68" , }, + [MCGRP_69 ] = { .name ="MCGRP_69" , }, + [MCGRP_70 ] = { .name ="MCGRP_70" , }, + [MCGRP_71 ] = { .name ="MCGRP_71" , }, + [MCGRP_72 ] = { .name ="MCGRP_72" , }, + [MCGRP_73 ] = { .name ="MCGRP_73" , }, + [MCGRP_74 ] = { .name ="MCGRP_74" , }, + [MCGRP_75 ] = { .name ="MCGRP_75" , }, + [MCGRP_76 ] = { .name ="MCGRP_76" , }, + [MCGRP_77 ] = { .name ="MCGRP_77" , }, + [MCGRP_78 ] = { .name ="MCGRP_78" , }, + [MCGRP_79 ] = { .name ="MCGRP_79" , }, + [MCGRP_80 ] = { .name ="MCGRP_80" , }, + [MCGRP_81 ] = { .name ="MCGRP_81" , }, + [MCGRP_82 ] = { .name ="MCGRP_82" , }, + [MCGRP_83 ] = { .name ="MCGRP_83" , }, + [MCGRP_84 ] = { .name ="MCGRP_84" , }, + [MCGRP_85 ] = { .name ="MCGRP_85" , }, + [MCGRP_86 ] = { .name ="MCGRP_86" , }, + [MCGRP_87 ] = { .name ="MCGRP_87" , }, + [MCGRP_88 ] = { .name ="MCGRP_88" , }, + [MCGRP_89 ] = { .name ="MCGRP_89" , }, + [MCGRP_90 ] = { .name ="MCGRP_90" , }, + [MCGRP_91 ] = { .name ="MCGRP_91" , }, + [MCGRP_92 ] = { .name ="MCGRP_92" , }, + [MCGRP_93 ] = { .name ="MCGRP_93" , }, + [MCGRP_94 ] = { .name ="MCGRP_94" , }, + [MCGRP_95 ] = { .name ="MCGRP_95" , }, + [MCGRP_96 ] = { .name ="MCGRP_96" , }, + [MCGRP_97 ] = { .name ="MCGRP_97" , }, + [MCGRP_98 ] = { .name ="MCGRP_98" , }, + [MCGRP_99 ] = { .name ="MCGRP_99" , }, + [MCGRP_100 ] = { .name ="MCGRP_100" , }, + [MCGRP_101 ] = { .name ="MCGRP_101" , }, + [MCGRP_102 ] = { .name ="MCGRP_102" , }, + [MCGRP_103 ] = { .name ="MCGRP_103" , }, + [MCGRP_104 ] = { .name ="MCGRP_104" , }, + [MCGRP_105 ] = { .name ="MCGRP_105" , }, + [MCGRP_106 ] = { .name ="MCGRP_106" , }, + [MCGRP_107 ] = { .name ="MCGRP_107" , }, + [MCGRP_108 ] = { .name ="MCGRP_108" , }, + [MCGRP_109 ] = { .name ="MCGRP_109" , }, + [MCGRP_110 ] = { .name ="MCGRP_100" , }, + [MCGRP_111 ] = { .name ="MCGRP_111" , }, + [MCGRP_112 ] = { .name ="MCGRP_112" , }, + [MCGRP_113 ] = { .name ="MCGRP_113" , }, + [MCGRP_114 ] = { .name ="MCGRP_114" , }, + [MCGRP_115 ] = { .name ="MCGRP_115" , }, + [MCGRP_116 ] = { .name ="MCGRP_116" , }, + [MCGRP_117 ] = { .name ="MCGRP_117" , }, + [MCGRP_118 ] = { .name ="MCGRP_118" , }, + [MCGRP_119 ] = { .name ="MCGRP_119" , }, + [MCGRP_120 ] = { .name ="MCGRP_120" , }, + [MCGRP_121 ] = { .name ="MCGRP_121" , }, + [MCGRP_122 ] = { .name ="MCGRP_122" , }, + [MCGRP_123 ] = { .name ="MCGRP_123" , }, + [MCGRP_124 ] = { .name ="MCGRP_124" , }, + [MCGRP_125 ] = { .name ="MCGRP_125" , }, + [MCGRP_126 ] = { .name ="MCGRP_126" , }, + [MCGRP_127 ] = { .name ="MCGRP_127" , }, + [MCGRP_128 ] = { .name ="MCGRP_128" , }, + [MCGRP_129 ] = { .name ="MCGRP_129" , }, + [MCGRP_130 ] = { .name ="MCGRP_130" , }, + [MCGRP_131 ] = { .name ="MCGRP_131" , }, + [MCGRP_132 ] = { .name ="MCGRP_132" , }, + [MCGRP_133 ] = { .name ="MCGRP_133" , }, + [MCGRP_134 ] = { .name ="MCGRP_134" , }, + [MCGRP_135 ] = { .name ="MCGRP_135" , }, + [MCGRP_136 ] = { .name ="MCGRP_136" , }, + [MCGRP_137 ] = { .name ="MCGRP_137" , }, + [MCGRP_138 ] = { .name ="MCGRP_138" , }, + [MCGRP_139 ] = { .name ="MCGRP_139" , }, + [MCGRP_140 ] = { .name ="MCGRP_140" , }, + [MCGRP_141 ] = { .name ="MCGRP_141" , }, + [MCGRP_142 ] = { .name ="MCGRP_142" , }, + [MCGRP_143 ] = { .name ="MCGRP_143" , }, + [MCGRP_144 ] = { .name ="MCGRP_144" , }, + [MCGRP_145 ] = { .name ="MCGRP_145" , }, + [MCGRP_146 ] = { .name ="MCGRP_146" , }, + [MCGRP_147 ] = { .name ="MCGRP_147" , }, + [MCGRP_148 ] = { .name ="MCGRP_148" , }, + [MCGRP_149 ] = { .name ="MCGRP_149" , }, + [MCGRP_150 ] = { .name ="MCGRP_150" , }, + [MCGRP_151 ] = { .name ="MCGRP_151" , }, + [MCGRP_152 ] = { .name ="MCGRP_152" , }, + [MCGRP_153 ] = { .name ="MCGRP_153" , }, + [MCGRP_154 ] = { .name ="MCGRP_154" , }, + [MCGRP_155 ] = { .name ="MCGRP_155" , }, + [MCGRP_156 ] = { .name ="MCGRP_156" , }, + [MCGRP_157 ] = { .name ="MCGRP_157" , }, + [MCGRP_158 ] = { .name ="MCGRP_158" , }, + [MCGRP_159 ] = { .name ="MCGRP_159" , }, + [MCGRP_160 ] = { .name ="MCGRP_160" , }, + [MCGRP_161 ] = { .name ="MCGRP_161" , }, + [MCGRP_162 ] = { .name ="MCGRP_162" , }, + [MCGRP_163 ] = { .name ="MCGRP_163" , }, + [MCGRP_164 ] = { .name ="MCGRP_164" , }, + [MCGRP_165 ] = { .name ="MCGRP_165" , }, + [MCGRP_166 ] = { .name ="MCGRP_166" , }, + [MCGRP_167 ] = { .name ="MCGRP_167" , }, + [MCGRP_168 ] = { .name ="MCGRP_168" , }, + [MCGRP_169 ] = { .name ="MCGRP_169" , }, + [MCGRP_170 ] = { .name ="MCGRP_170" , }, + [MCGRP_171 ] = { .name ="MCGRP_171" , }, + [MCGRP_172 ] = { .name ="MCGRP_172" , }, + [MCGRP_173 ] = { .name ="MCGRP_173" , }, + [MCGRP_174 ] = { .name ="MCGRP_174" , }, + [MCGRP_175 ] = { .name ="MCGRP_175" , }, + [MCGRP_176 ] = { .name ="MCGRP_176" , }, + [MCGRP_177 ] = { .name ="MCGRP_177" , }, + [MCGRP_178 ] = { .name ="MCGRP_178" , }, + [MCGRP_179 ] = { .name ="MCGRP_179" , }, + [MCGRP_180 ] = { .name ="MCGRP_180" , }, + [MCGRP_181 ] = { .name ="MCGRP_181" , }, + [MCGRP_182 ] = { .name ="MCGRP_182" , }, + [MCGRP_183 ] = { .name ="MCGRP_183" , }, + [MCGRP_184 ] = { .name ="MCGRP_184" , }, + [MCGRP_185 ] = { .name ="MCGRP_185" , }, + [MCGRP_186 ] = { .name ="MCGRP_186" , }, + [MCGRP_187 ] = { .name ="MCGRP_187" , }, + [MCGRP_188 ] = { .name ="MCGRP_188" , }, + [MCGRP_189 ] = { .name ="MCGRP_189" , }, + [MCGRP_190 ] = { .name ="MCGRP_190" , }, + [MCGRP_191 ] = { .name ="MCGRP_191" , }, + [MCGRP_192 ] = { .name ="MCGRP_192" , }, + [MCGRP_193 ] = { .name ="MCGRP_193" , }, + [MCGRP_194 ] = { .name ="MCGRP_194" , }, + [MCGRP_195 ] = { .name ="MCGRP_195" , }, + [MCGRP_196 ] = { .name ="MCGRP_196" , }, + [MCGRP_197 ] = { .name ="MCGRP_197" , }, + [MCGRP_198 ] = { .name ="MCGRP_198" , }, + [MCGRP_199 ] = { .name ="MCGRP_199" , }, +}; +enum my_multicast_many_groups_two {MCGRP_TWO_1 ,MCGRP_TWO_2 ,
@@ -1170,6 +1589,43 @@ static struct genl_family third_genl_family = { .policy = third_genl_policy, }; +// LARGE_GENL +enum { +LARGE_GENL_CMD_UNSPEC , +LARGE_GENL_CMD_ECHO , + __LARGE_GENL_CMD_MAX, +}; + +#define LARGE_GENL_CMD_MAX (__LARGE_GENL_CMD_MAX - 1) + +static int large_genl_echo (struct sk_buff *skb,struct genl_info *info) +{ +return 0 ; +} + +// Generic Netlink operations for LARGE_GENL family +static const struct genl_ops large_genl_ops[] = { + { + .cmd =LARGE_GENL_CMD_ECHO , + .flags =0 , + .doit = large_genl_echo, + .dumpit =NULL , + }, +}; + +// genl_family struct for LARGE_GENL family +static struct genl_family large_genl_family = { + .hdrsize =0 , + .name =LARGE_GENL_FAMILY_NAME , + .version =1 , + .maxattr =1 , + .netnsok = true, + .ops = large_genl_ops, + .n_ops =ARRAY_SIZE (large_genl_ops), + .mcgrps = genl_many_mcgrps_one, + .n_mcgrps =ARRAY_SIZE (genl_many_mcgrps_one), +}; +// genl_family struct with incorrect name static struct genl_family incorrect_genl_family = { .hdrsize =0 ,
@@ -1237,14 +1693,22 @@ static int __init init_netlink(void) goto failure_2; } - rc =genl_register_family (&third_genl_family); + rc =genl_register_family (&large_genl_family);if (rc) {pr_err ("%s: Failed to register Generic Netlink family\n" , __func__); goto failure_3; } + rc =genl_register_family (&third_genl_family); +if (rc) { +pr_err ("%s: Failed to register Generic Netlink family\n" , __func__); + goto failure_4; + } +return 0 ; +failure_4: +genl_unregister_family (&large_genl_family); failure_3:genl_unregister_family (&my_genl_family_parallel); failure_2:
@@ -1417,6 +1881,7 @@ static int __init module_netlink_init(void)genl_unregister_family (&my_genl_family);genl_unregister_family (&my_genl_family_parallel);genl_unregister_family (&third_genl_family); +genl_unregister_family (&large_genl_family); err_sysfs:sysfs_remove_file (kobj_genl_test, &my_attr_u32_genl_test.attr);sysfs_remove_file (kobj_genl_test, &my_attr_str_genl_test.attr);
@@ -1437,6 +1902,7 @@ static void __exit module_netlink_exit(void)genl_unregister_family (&my_genl_family);genl_unregister_family (&my_genl_family_parallel);genl_unregister_family (&third_genl_family); +genl_unregister_family (&large_genl_family);sysfs_remove_file (kobj_genl_test, &my_attr_u32_genl_test.attr);sysfs_remove_file (kobj_genl_test, &my_attr_str_genl_test.attr);
Signature · 1 line
2.34.1
PATCH 10/15 selftests: net: genetlink: add packet capture test infrastructure
In reply to Yana BashlykovaAdd test cases for monitoring Netlink traffic during test execution
Require CONFIG_NLMON.
--- tools/testing/selftests/net/Makefile | 6 + tools/testing/selftests/net/genetlink.c | 234 ++++++++++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100644 tools/testing/selftests/net/genetlink.c
tools/testing/selftests/net/Makefile +6 -0
@@ -71,6 +71,7 @@ TEST_GEN_FILES += bind_bhash TEST_GEN_PROGS += sk_bind_sendto_listen TEST_GEN_PROGS += sk_connect_zero_addr TEST_PROGS += test_ingress_egress_chaining.sh +TEST_GEN_PROGS += genetlink TEST_FILES := settings
@@ -82,3 +83,8 @@ $(OUTPUT)/reuseport_bpf_numa: LDLIBS += -lnuma $(OUTPUT)/tcp_mmap: LDLIBS += -lpthread $(OUTPUT)/tcp_inq: LDLIBS += -lpthread $(OUTPUT)/bind_bhash: LDLIBS += -lpthread + +$(OUTPUT)/genetlink: LDLIBS += -lnl-3 -lnl-genl-3 +$(OUTPUT)/genetlink: CFLAGS += $(shell pkg-config --cflags libnl-3.0 libnl-genl-3.0) + +EXTRA_CLEAN := $(SCRATCH_DIR) $(OUTPUT)/genetlink.pcap
tools/testing/selftests/net/genetlink.c new file +234 -0
234 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 11/15 selftests: net: genetlink: add /proc/net/netlink test
In reply to Yana BashlykovaAdd test case to verify proper handling of Netlink sockets in procfs:
- Tests /proc/net/netlink file accessibility
- Validates socket count changes on creation/deletion
- Uses kernel's netlink_seq_ops mechanism
Checks that socket entries are correctly added/removed from procfs.
--- tools/testing/selftests/net/genetlink.c | 74 +++++++++++++++++++++++++ 1 file changed, 74 insertions(+)
tools/testing/selftests/net/genetlink.c +74 -0
@@ -81,6 +81,25 @@#define LARGE_GENL_FAMILY_NAME "LARGE_GENL" +struct nl_sock *socket_alloc_and_conn (void ) +{ +struct nl_sock *socket; + + socket =nl_socket_alloc (); +if (!socket) { +fprintf (stderr,"Failed to allocate socket\n" ); +return NULL ; + } + +if (genl_connect (socket)) { +fprintf (stderr, +"Failed to connect to generic netlink through socket\n" ); +nl_socket_free (socket); +return NULL ; + } +return socket; +} +/* * Test cases */
@@ -143,6 +162,61 @@ TEST(capture_start)printf ("Starting Netlink tests...\n" ); } +/** +* TEST(open_netlink_file) - Verifies correct reading of Netlink socket information +* +* Tests the /proc/net/netlink interface by: +* 1. Creating a test Netlink socket +* 2. Reading the proc file before and after socket creation +* 3. Verifying the socket count changes as expected +* +* The test checks that: +* - /proc/net/netlink is accessible +* - Entries are properly added/removed +* - Uses kernel's netlink_seq_ops mechanism +*/ + +TEST (open_netlink_file) +{ +FILE *file; +char line[256 ]; +int cnt =0 ; + +printf ("Running Test: opening and reading /proc/net/netlink file...\n" ); + +struct nl_sock *sock; + + sock =socket_alloc_and_conn (); + + file =fopen ("/proc/net/netlink" ,"r" ); +ASSERT_NE (NULL , file); +if (file ==NULL ) { +perror ("fopen" ); +return ; + } + +while (fgets (line,sizeof (line), file) !=NULL ) + cnt++; + +nl_socket_free (sock); + +fclose (file); + + file =fopen ("/proc/net/netlink" ,"r" ); +ASSERT_NE (NULL , file); +if (file ==NULL ) { +perror ("fopen" ); +return ; + } + +while (fgets (line,sizeof (line), file) !=NULL ) + cnt--; + +EXPECT_EQ (cnt,1 ); + +fclose (file); +} +/** * TEST(capture_end) - Terminates Netlink traffic monitoring session *
Signature · 1 line
2.34.1
PATCH 12/15 selftests: net: genetlink: add Generic Netlink controller tests
In reply to Yana BashlykovaAdd comprehensive test coverage for nlctrl (Generic Netlink controller)
Add my_genl_ctrl_resolve function for resolving family ID
--- tools/testing/selftests/net/genetlink.c | 925 ++++++++++++++++++++++++ 1 file changed, 925 insertions(+)
tools/testing/selftests/net/genetlink.c +925 -0
@@ -81,6 +81,404 @@#define LARGE_GENL_FAMILY_NAME "LARGE_GENL" +/** +* Callback data structures - used to pass data between test cases and message handlers +*/ + +struct callback_data_ctrl { +int family_id; +char *family_name; +int op; +struct expected_policies *expected_policy; +int family_index; +}; + +static int elem; + +static int id_elem; + +struct ctrl_policy { +int id; +uint32_t field; +uint32_t type; +int value; +}; + +struct ctrl_policy expected_parallel_policy[] = { + {1 ,CTRL_ATTR_OP_POLICY ,CTRL_ATTR_POLICY_DO ,0 }, + {2 ,CTRL_ATTR_OP_POLICY ,CTRL_ATTR_POLICY_DUMP ,0 }, + {3 ,CTRL_ATTR_OP_POLICY ,CTRL_ATTR_POLICY_DO ,1 }, + {4 ,CTRL_ATTR_OP_POLICY ,CTRL_ATTR_POLICY_DO ,0 }, + {5 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_TYPE ,11 }, + {6 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_TYPE ,10 }, + {7 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_TYPE ,12 }, + {8 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_TYPE ,12 }, + {9 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_TYPE ,15 }, + {9 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_BITFIELD32_MASK ,0 }, + {10 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_TYPE ,8 }, + {10 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_MIN_VALUE_S ,-100 }, + {10 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_MAX_VALUE_S ,100 }, + {11 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_TYPE ,14 }, + {12 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_TYPE ,13 }, + {13 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_TYPE ,1 }, + {14 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_TYPE ,1 }, + {15 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_TYPE ,11 }, +}; + +struct ctrl_policy expected_genl_cmd_get_value_policy[] = { + {1 ,CTRL_ATTR_OP_POLICY ,CTRL_ATTR_POLICY_DO ,0 }, + {2 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_TYPE ,11 }, + {3 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_TYPE ,4 }, + {3 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_MIN_VALUE_U ,0 }, + {3 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_MAX_VALUE_U ,100 }, + {4 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_TYPE ,11 }, + {5 ,CTRL_ATTR_POLICY ,NL_POLICY_TYPE_ATTR_TYPE ,13 }, +}; + +struct expected_policies { +struct ctrl_policy *policy; +int count; +int matched; +}; + +struct expected_policies parallel_policy = { + .policy = expected_parallel_policy, + .count =sizeof (expected_parallel_policy) / +sizeof (expected_parallel_policy[0 ]), + .matched =0 , +}; + +struct expected_policies genl_cmd_get_value_policy = { + .policy = expected_genl_cmd_get_value_policy, + .count =sizeof (expected_genl_cmd_get_value_policy) / +sizeof (expected_genl_cmd_get_value_policy[0 ]), + .matched =0 , +}; + +int validate_cb_ctrl (struct nl_msg *msg,void *arg) +{ +struct genlmsghdr *gnlh =nlmsg_data (nlmsg_hdr (msg)); +struct nlattr *attrs[CTRL_ATTR_MAX +1 ]; +int ret =0 ; +int family_id =-40 ; +char *family_name =NULL ; + + ret =genlmsg_parse (nlmsg_hdr (msg),0 , attrs,CTRL_ATTR_MAX ,NULL ); +if (ret <0 ) { +printf ("Failed to parse attributes: %d\n" , ret); +return NL_STOP ; + } + +struct callback_data_ctrl *data_ctrl = (struct callback_data_ctrl *)arg; + +switch (gnlh->cmd) { +case CTRL_CMD_NEWFAMILY : +if (attrs[CTRL_ATTR_FAMILY_ID ]) { +if (data_ctrl->family_name) { + family_name =nla_get_string ( + attrs[CTRL_ATTR_FAMILY_NAME ]); +if (!strcmp (family_name, + data_ctrl->family_name)) { + family_id =nla_get_u16 ( + attrs[CTRL_ATTR_FAMILY_ID ]); + data_ctrl->family_id = family_id; + } + } + } +if (attrs[CTRL_ATTR_FAMILY_NAME ]) { +if (data_ctrl->family_id) { +if (!data_ctrl->family_name) { + family_name =nla_get_string ( + attrs[CTRL_ATTR_FAMILY_NAME ]); + data_ctrl->family_name = family_name; + } + } + } + data_ctrl->family_index++; +return NL_OK ; + +case CTRL_CMD_GETPOLICY : +struct ctrl_policy *exp = + &data_ctrl->expected_policy->policy[elem]; +if (attrs[CTRL_ATTR_FAMILY_ID ]) { + family_id =nla_get_u16 (attrs[CTRL_ATTR_FAMILY_ID ]); + data_ctrl->family_id = family_id; + } + +if (attrs[CTRL_ATTR_OP_POLICY ]) { +struct nlattr *nla; +int rem; + +nla_for_each_nested (nla, attrs[CTRL_ATTR_OP_POLICY ], + rem) { +struct nlattr *tb[CTRL_ATTR_POLICY_MAX +1 ] = { +NULL + }; + +int err =nla_parse_nested ( + tb,CTRL_ATTR_POLICY_MAX , nla,NULL ); +if (err <0 ) { +printf ("Failed to parse nested policy attributes: %d\n" , + err); +continue ; + } + +if (tb[CTRL_ATTR_POLICY_DO ]) { +uint32_t do_id =nla_get_u32 ( + tb[CTRL_ATTR_POLICY_DO ]); +if (exp->field ==CTRL_ATTR_OP_POLICY && + exp->type ==CTRL_ATTR_POLICY_DO && + exp->value == do_id) { + data_ctrl->expected_policy + ->matched++; + elem++; +if (elem != id_elem) { + exp = &data_ctrl + ->expected_policy + ->policy[elem]; + } + } + } + +if (tb[CTRL_ATTR_POLICY_DUMP ]) { +uint32_t dump_id =nla_get_u32 ( + tb[CTRL_ATTR_POLICY_DUMP ]); +if (exp->field ==CTRL_ATTR_OP_POLICY && + exp->type == +CTRL_ATTR_POLICY_DUMP && + exp->value == dump_id) { + data_ctrl->expected_policy + ->matched++; + elem++; +if (elem != id_elem) { + exp = &data_ctrl + ->expected_policy + ->policy[elem]; + } + } + } + } + id_elem++; + } + +if (attrs[CTRL_ATTR_POLICY ]) { +struct nlattr *policy_attr; +int rem; + +nla_for_each_nested (policy_attr, + attrs[CTRL_ATTR_POLICY ], rem) { +struct nlattr *tb[NL_POLICY_TYPE_ATTR_MAX + +1 ] = {NULL }; + +int err =nla_parse_nested ( + tb,NL_POLICY_TYPE_ATTR_MAX , +nla_data (policy_attr),NULL ); +if (err <0 ) { +printf ("Failed to parse nested policy attributes: %d\n" , + err); +continue ; + } + +if (tb[NL_POLICY_TYPE_ATTR_TYPE ]) { +uint32_t value1 =nla_get_u32 ( + tb[NL_POLICY_TYPE_ATTR_TYPE ]); +if (exp->field ==CTRL_ATTR_POLICY && + exp->type == +NL_POLICY_TYPE_ATTR_TYPE && + exp->value == value1) { + data_ctrl->expected_policy + ->matched++; + elem++; +if (elem != id_elem) { + exp = &data_ctrl + ->expected_policy + ->policy[elem]; + } + } + } + +if (tb[NL_POLICY_TYPE_ATTR_MIN_VALUE_S ]) { +int64_t value2 =nla_get_s64 ( + tb[NL_POLICY_TYPE_ATTR_MIN_VALUE_S ]); +if (exp->field ==CTRL_ATTR_POLICY && + exp->type == +NL_POLICY_TYPE_ATTR_MIN_VALUE_S && + exp->value == value2) { + data_ctrl->expected_policy + ->matched++; + elem++; +if (elem != id_elem) { + exp = &data_ctrl + ->expected_policy + ->policy[elem]; + } + } + } + +if (tb[NL_POLICY_TYPE_ATTR_MAX_VALUE_S ]) { +int64_t value3 =nla_get_s64 ( + tb[NL_POLICY_TYPE_ATTR_MAX_VALUE_S ]); +if (exp->field ==CTRL_ATTR_POLICY && + exp->type == +NL_POLICY_TYPE_ATTR_MAX_VALUE_S && + exp->value == value3) { + data_ctrl->expected_policy + ->matched++; + elem++; +if (elem != id_elem) { + exp = &data_ctrl + ->expected_policy + ->policy[elem]; + } + } + } + +if (tb[NL_POLICY_TYPE_ATTR_MIN_VALUE_U ]) { +uint64_t value4 =nla_get_u64 ( + tb[NL_POLICY_TYPE_ATTR_MIN_VALUE_U ]); +if (exp->field ==CTRL_ATTR_POLICY && + exp->type == +NL_POLICY_TYPE_ATTR_MIN_VALUE_U && + exp->value == value4) { + data_ctrl->expected_policy + ->matched++; + elem++; +if (elem != id_elem) { + exp = &data_ctrl + ->expected_policy + ->policy[elem]; + } + } + } + +if (tb[NL_POLICY_TYPE_ATTR_MAX_VALUE_U ]) { +uint64_t value5 =nla_get_u64 ( + tb[NL_POLICY_TYPE_ATTR_MAX_VALUE_U ]); +if (exp->field ==CTRL_ATTR_POLICY && + exp->type == +NL_POLICY_TYPE_ATTR_MAX_VALUE_U && + exp->value == value5) { + data_ctrl->expected_policy + ->matched++; + elem++; + } + } +if (tb[NL_POLICY_TYPE_ATTR_MIN_LENGTH ]) { +uint32_t value6 =nla_get_u32 ( + tb[NL_POLICY_TYPE_ATTR_MIN_LENGTH ]); +if (exp->field ==CTRL_ATTR_POLICY && + exp->type == +NL_POLICY_TYPE_ATTR_MIN_LENGTH && + exp->value == value6) { + data_ctrl->expected_policy + ->matched++; + elem++; +if (elem != id_elem) { + exp = &data_ctrl + ->expected_policy + ->policy[elem]; + } + } + } + +if (tb[NL_POLICY_TYPE_ATTR_MAX_LENGTH ]) { +uint32_t value7 =nla_get_u32 ( + tb[NL_POLICY_TYPE_ATTR_MAX_LENGTH ]); +if (exp->field ==CTRL_ATTR_POLICY && + exp->type == +NL_POLICY_TYPE_ATTR_MAX_LENGTH && + exp->value == value7) { + data_ctrl->expected_policy + ->matched++; + elem++; +if (elem != id_elem) { + exp = &data_ctrl + ->expected_policy + ->policy[elem]; + } + } + } +if (tb[NL_POLICY_TYPE_ATTR_POLICY_IDX ]) { +uint32_t value8 =nla_get_u32 ( + tb[NL_POLICY_TYPE_ATTR_POLICY_IDX ]); +if (exp->field ==CTRL_ATTR_POLICY && + exp->type == +NL_POLICY_TYPE_ATTR_POLICY_IDX && + exp->value == value8) { + data_ctrl->expected_policy + ->matched++; + elem++; +if (elem != id_elem) { + exp = &data_ctrl + ->expected_policy + ->policy[elem]; + } + } + } + +if (tb[NL_POLICY_TYPE_ATTR_POLICY_MAXTYPE ]) { +uint32_t value9 =nla_get_u32 ( + tb[NL_POLICY_TYPE_ATTR_POLICY_MAXTYPE ]); +if (exp->field ==CTRL_ATTR_POLICY && + exp->type == +NL_POLICY_TYPE_ATTR_POLICY_MAXTYPE && + exp->value == value9) { + data_ctrl->expected_policy + ->matched++; + elem++; +if (elem != id_elem) { + exp = &data_ctrl + ->expected_policy + ->policy[elem]; + } + } + } +if (tb[NL_POLICY_TYPE_ATTR_BITFIELD32_MASK ]) { +uint32_t value10 =nla_get_u32 ( + tb[NL_POLICY_TYPE_ATTR_BITFIELD32_MASK ]); +if (exp->field ==CTRL_ATTR_POLICY && + exp->type == +NL_POLICY_TYPE_ATTR_BITFIELD32_MASK && + exp->value == value10) { + data_ctrl->expected_policy + ->matched++; + elem++; +if (elem != id_elem) { + exp = &data_ctrl + ->expected_policy + ->policy[elem]; + } + } + } + +if (tb[NL_POLICY_TYPE_ATTR_PAD ]) { +uint64_t value11 =nla_get_u64 ( + tb[NL_POLICY_TYPE_ATTR_PAD ]); +if (exp->field ==CTRL_ATTR_POLICY && + exp->type == +NL_POLICY_TYPE_ATTR_PAD && + exp->value == value11) { + data_ctrl->expected_policy + ->matched++; + elem++; +if (elem != id_elem) { + exp = &data_ctrl + ->expected_policy + ->policy[elem]; + } + } + } + } + id_elem++; + } +return NL_OK ; +default : +printf ("Unknown command: %u\n" , gnlh->cmd); +break ; + } +return NL_OK ; +} +struct nl_sock *socket_alloc_and_conn (void ) {struct nl_sock *socket;
@@ -100,6 +498,100 @@ struct nl_sock *socket_alloc_and_conn(void)return socket; } +int my_genl_ctrl_resolve (char *family_name) +{ +struct nl_sock *ctrl_sock; +int genl_ctrl_family_id; +struct nl_msg *msg; +struct genlmsghdr *user_hdr; +struct nl_cb *cb_ctrl; +int err =-100 ; +struct callback_data_ctrl cb_ctrl_data; + + cb_ctrl_data.family_name = family_name; + + ctrl_sock =socket_alloc_and_conn (); +if (!ctrl_sock) { +fprintf (stderr,"socket for genl_ctrl is NULL\n" ); +return -ENOMEM ; + } + + genl_ctrl_family_id =genl_ctrl_resolve (ctrl_sock,GENL_CTRL ); +if (genl_ctrl_family_id <0 ) { +fprintf (stderr, +"Failed to resolve family id for genl_ctrl: %d\n" , + genl_ctrl_family_id); + err = genl_ctrl_family_id; +return err; + } + + msg =nlmsg_alloc (); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +nl_socket_free (ctrl_sock); +return -ENOMEM ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , + genl_ctrl_family_id,0 , +NLM_F_REQUEST |NLM_F_DUMP ,CTRL_CMD_GETFAMILY , +0 ); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); +nlmsg_free (msg); +nl_socket_free (ctrl_sock); +return -ENOMEM ; + } + +if (nla_put_string (msg,CTRL_ATTR_FAMILY_NAME , family_name) <0 ) { +fprintf (stderr, +"Failed to add CTRL_ATTR_FAMILY_NAME attribute: %s\n" , +strerror (errno)); +nlmsg_free (msg); +nl_socket_free (ctrl_sock); +return -EMSGSIZE ; + } + + cb_ctrl =nl_cb_alloc (NL_CB_DEFAULT ); +if (!cb_ctrl) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (ctrl_sock); +return -ENOMEM ; + } + + err =nl_cb_set (cb_ctrl,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb_ctrl, + &cb_ctrl_data); +if (err <0 ) { +printf ("Error setting callback\n" ); + goto error; + } + + err =nl_send_auto (ctrl_sock, msg); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_recvmsgs (ctrl_sock, cb_ctrl); +if (err <0 ) { +fprintf (stderr,"Failed to receive message: %s\n" , +nl_geterror (err)); + goto error; + } + +nlmsg_free (msg); +nl_cb_put (cb_ctrl); +nl_socket_free (ctrl_sock); +return cb_ctrl_data.family_id; +error: +nlmsg_free (msg); +nl_cb_put (cb_ctrl); +nl_socket_free (ctrl_sock); +return err; +} +/* * Test cases */
@@ -217,6 +709,439 @@ TEST(open_netlink_file)fclose (file); } +/** +* TEST(genl_ctrl_one_family) - Tests resolution of single Generic Netlink family +* +* Validates that: +* 1. Controller correctly resolves family ID for given family name +* 2. Family ID obtained through direct query matches cached resolution +* 3. Callback correctly processes controller response +* +* Test flow: +* 1. Creates control socket +* 2. Sends GETFAMILY request for target family +* 3. Validates response through callback +* 4. Compares with direct resolution result +*/ + +TEST (genl_ctrl_one_family) +{ +struct nl_sock *ctrl_sock; +int genl_ctrl_family_id; +int family_id; +struct nl_msg *msg; +struct genlmsghdr *user_hdr; +struct nl_cb *cb_ctrl; +int err =0 ; +struct callback_data_ctrl cb_ctrl_data; + + cb_ctrl_data.family_id =-30 ; + cb_ctrl_data.family_name =NULL ; + cb_ctrl_data.op =-100 ; + +printf ("Running Test: getting family via genl_ctrl...\n" ); + + ctrl_sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , ctrl_sock); +if (!ctrl_sock) { +fprintf (stderr,"socket for genl_ctrl is NULL\n" ); +return ; + } + + genl_ctrl_family_id =genl_ctrl_resolve (ctrl_sock,GENL_CTRL ); +EXPECT_GT (genl_ctrl_family_id,0 ); +if (genl_ctrl_family_id <0 ) { +fprintf (stderr, +"Failed to resolve family id for genl_ctrl: %d\n" , + genl_ctrl_family_id); + err = genl_ctrl_family_id; +return ; + } + + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +nl_socket_free (ctrl_sock); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , + genl_ctrl_family_id,0 ,NLM_F_REQUEST , +CTRL_CMD_GETFAMILY ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); +nlmsg_free (msg); +nl_socket_free (ctrl_sock); +return ; + } + +if (nla_put_string (msg,CTRL_ATTR_FAMILY_NAME , +PARALLEL_GENL_FAMILY_NAME ) <0 ) { +fprintf (stderr, +"Failed to add CTRL_ATTR_FAMILY_NAME attribute: %s\n" , +strerror (errno)); +nlmsg_free (msg); +nl_socket_free (ctrl_sock); +ASSERT_EQ (0 ,1 ); +return ; + } + cb_ctrl_data.family_name =PARALLEL_GENL_FAMILY_NAME ; + + cb_ctrl =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb_ctrl); +if (!cb_ctrl) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (ctrl_sock); +return ; + } + + err =nl_cb_set (cb_ctrl,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb_ctrl, + &cb_ctrl_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +printf ("Error setting callback\n" ); + goto error; + } + + err =nl_send_auto (ctrl_sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_recvmsgs (ctrl_sock, cb_ctrl); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to receive message: %s\n" , +nl_geterror (err)); + goto error; + } +my_genl_ctrl_resolve (PARALLEL_GENL_FAMILY_NAME ); + family_id =genl_ctrl_resolve (socket_alloc_and_conn (), +PARALLEL_GENL_FAMILY_NAME ); +EXPECT_GT (cb_ctrl_data.family_id,0 ); +EXPECT_GT (family_id,0 ); +EXPECT_EQ (cb_ctrl_data.family_id, family_id); + +error: +nlmsg_free (msg); +nl_cb_put (cb_ctrl); +nl_socket_free (ctrl_sock); +} + +/** +* TEST(genl_ctrl_family) - Tests dumping all registered Generic Netlink families +* +* Verifies that: +* 1. Controller correctly responds to family dump request +* 2. No errors occur during dump operation +* +* Test flow: +* 1. Creates control socket and resolves genl_ctrl family +* 2. Sends GETFAMILY dump request with NLM_F_DUMP flag +* 3. Checks for operation success +*/ + +TEST (genl_ctrl_family) +{ +struct nl_sock *ctrl_sock; +int genl_ctrl_family_id; +struct nl_msg *msg; +struct genlmsghdr *user_hdr; +struct nl_cb *cb_ctrl; +int err =0 ; +struct callback_data_ctrl cb_ctrl_data; + + cb_ctrl_data.family_id =-30 ; + cb_ctrl_data.family_name =NULL ; + cb_ctrl_data.op =-100 ; + cb_ctrl_data.family_index =0 ; + +printf ("Running Test: getting families via genl_ctrl...\n" ); + + ctrl_sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , ctrl_sock); +if (!ctrl_sock) { +fprintf (stderr,"socket for genl_ctrl is NULL\n" ); +return ; + } + + genl_ctrl_family_id =genl_ctrl_resolve (ctrl_sock,GENL_CTRL ); +EXPECT_GT (genl_ctrl_family_id,0 ); +if (genl_ctrl_family_id <0 ) { +fprintf (stderr, +"Failed to resolve family id for genl_ctrl: %d\n" , + genl_ctrl_family_id); + err = genl_ctrl_family_id; +return ; + } + + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +nl_socket_free (ctrl_sock); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , + genl_ctrl_family_id,0 ,NLM_F_DUMP , +CTRL_CMD_GETFAMILY ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); +nlmsg_free (msg); +nl_socket_free (ctrl_sock); +return ; + } + + cb_ctrl =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb_ctrl); +if (!cb_ctrl) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (ctrl_sock); +return ; + } + + err =nl_cb_set (cb_ctrl,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb_ctrl, + &cb_ctrl_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +printf ("Error setting callback\n" ); + goto error; + } + + err =nl_send_auto (ctrl_sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_recvmsgs (ctrl_sock, cb_ctrl); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to receive message: %s\n" , +nl_geterror (err)); + goto error; + } +EXPECT_GE (cb_ctrl_data.family_index,4 ); + +error: +nlmsg_free (msg); +nl_cb_put (cb_ctrl); +nl_socket_free (ctrl_sock); +} + +/** +* TEST(genl_ctrl_policy) - Validates Generic Netlink policy retrieval mechanism +* +* Tests that: +* 1. Policy information can be retrieved by family ID and name +* 2. Operation-specific policies can be retrieved +* 3. Retrieved policies match expected structures +* +* Test sequence: +* 1. Retrieves general policy for PARALLEL_GENL family +* 2. Retrieves operation-specific policy for MY_GENL_CMD_GET_VALUE +* 3. Validates policy contents through callback +*/ + +TEST (genl_ctrl_policy) +{ +struct nl_sock *ctrl_sock; +int genl_ctrl_family_id; +struct nl_msg *msg; +struct genlmsghdr *user_hdr; +struct nl_cb *cb_ctrl; +int err =0 ; +struct callback_data_ctrl cb_ctrl_data; + + cb_ctrl_data.family_id =-30 ; + cb_ctrl_data.family_name =NULL ; + cb_ctrl_data.op =-100 ; + cb_ctrl_data.expected_policy = ¶llel_policy; + cb_ctrl_data.expected_policy->matched =0 ; + +printf ("Running Test: getting policy via genl_ctrl...\n" ); + + ctrl_sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , ctrl_sock); +if (!ctrl_sock) { +fprintf (stderr, +"sockets for genl_ctrl and parallel_genl are NULL\n" ); +return ; + } + + genl_ctrl_family_id =genl_ctrl_resolve (ctrl_sock,GENL_CTRL ); +EXPECT_GT (genl_ctrl_family_id,0 ); +if (genl_ctrl_family_id <0 ) { +fprintf (stderr, +"Failed to resolve family id for genl_ctrl: %d\n" , + genl_ctrl_family_id); +nl_socket_free (ctrl_sock); +return ; + } + +printf ("Start first message with family id and family name\n" ); + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +nl_socket_free (ctrl_sock); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , + genl_ctrl_family_id,0 ,NLM_F_DUMP , +CTRL_CMD_GETPOLICY ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); +nlmsg_free (msg); +nl_socket_free (ctrl_sock); +return ; + } + +if (nla_put_u16 (msg,CTRL_ATTR_FAMILY_ID , +genl_ctrl_resolve (ctrl_sock, +PARALLEL_GENL_FAMILY_NAME )) <0 ) { +fprintf (stderr, +"Failed to add CTRL_ATTR_FAMILY_ID attribute: %s\n" , +strerror (errno)); +nlmsg_free (msg); +nl_socket_free (ctrl_sock); +ASSERT_EQ (0 ,1 ); +return ; + } +if (nla_put_string (msg,CTRL_ATTR_FAMILY_NAME , +PARALLEL_GENL_FAMILY_NAME ) <0 ) { +fprintf (stderr, +"Failed to add CTRL_ATTR_FAMILY_NAME attribute: %s\n" , +strerror (errno)); +nlmsg_free (msg); +nl_socket_free (ctrl_sock); +ASSERT_EQ (0 ,1 ); +return ; + } + + cb_ctrl =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb_ctrl); +if (!cb_ctrl) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (ctrl_sock); +return ; + } + + err =nl_cb_set (cb_ctrl,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb_ctrl, + &cb_ctrl_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Error setting callback: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_send_auto (ctrl_sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_recvmsgs (ctrl_sock, cb_ctrl); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to receive message: %s\n" , +nl_geterror (err)); + goto error; + } +EXPECT_EQ (cb_ctrl_data.expected_policy->matched, + cb_ctrl_data.expected_policy->count); + +printf ("[OK] [1/2]\n" ); + + cb_ctrl_data.expected_policy = &genl_cmd_get_value_policy; + cb_ctrl_data.expected_policy->matched =0 ; + elem =0 ; + id_elem =0 ; + +nlmsg_free (msg); + +printf ("Start second message with family name and ctrl_attr_op\n" ); + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +nl_socket_free (ctrl_sock); +nl_cb_put (cb_ctrl); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , + genl_ctrl_family_id,0 ,NLM_F_DUMP , +CTRL_CMD_GETPOLICY ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); + goto error; + } + +if (nla_put_string (msg,CTRL_ATTR_FAMILY_NAME ,MY_GENL_FAMILY_NAME ) < +0 ) { +fprintf (stderr, +"Failed to add CTRL_ATTR_FAMILY_NAME attribute: %s\n" , +strerror (errno)); +EXPECT_EQ (0 ,1 ); + goto error; + } + +if (nla_put_u32 (msg,CTRL_ATTR_OP ,MY_GENL_CMD_GET_VALUE ) <0 ) { +fprintf (stderr,"Failed to add CTRL_ATTR_OP attribute: %s\n" , +strerror (errno)); +EXPECT_EQ (0 ,1 ); + goto error; + } + + err =nl_send_auto (ctrl_sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_recvmsgs (ctrl_sock, cb_ctrl); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to receive message: %s\n" , +nl_geterror (err)); + goto error; + } + +EXPECT_EQ (cb_ctrl_data.expected_policy->matched, + cb_ctrl_data.expected_policy->count); +printf ("[OK] [2/2]\n" ); + + cb_ctrl_data.expected_policy->matched =0 ; + elem =0 ; + id_elem =0 ; + +error: +nlmsg_free (msg); +nl_cb_put (cb_ctrl); +nl_socket_free (ctrl_sock); +} +/** * TEST(capture_end) - Terminates Netlink traffic monitoring session *
Signature · 1 line
2.34.1
PATCH 13/15 selftests: net: genetlink: add large family ID resolution test
In reply to Yana BashlykovaAdd test case for resolving family IDs of large Generic Netlink families (those with 199+ multicast groups):
1. Tests that standard genl_ctrl_resolve() may fail due to:
- Response size exceeding single message limit
- Implementation expecting single response
2. Verifies custom my_genl_ctrl_resolve() works by:
- Using dump mechanism to collect all responses
- Properly handling multi-message replies
- Correctly identifying target family
The test validates that large families can be reliably resolved despite kernel message size limitations.
--- tools/testing/selftests/net/genetlink.c | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
tools/testing/selftests/net/genetlink.c +37 -0
@@ -1142,6 +1142,43 @@ TEST(genl_ctrl_policy)nl_socket_free (ctrl_sock); } +/** +* TEST(resolve_large_family_id) - Tests resolution of family ID for +* LARGE_GENL Generic Netlink family +* +* Validates special handling required for families with many multicast groups (199+): +* 1. Standard genl_ctrl_resolve() fails due to message size limitations +* 2. Custom my_genl_ctrl_resolve() succeeds by using dump mechanism +* +* Background: +* - Kernel successfully registers large families +* - Standard resolution fails because: +* * Response doesn't fit in single message +* * genl_ctrl_resolve() expects single response +* - Custom solution works by: +* * Using dump request to get all messages +* * Searching for target family in callback +* +* Verification: +* 1. Custom resolver returns valid ID (> 0) +* 2. Standard resolver either fails or succeeds (platform-dependent) +*/ + +TEST (resolve_large_family_id) +{ +int family_id; +int no_family_id; + +/* Test custom resolver */ + family_id =my_genl_ctrl_resolve (LARGE_GENL_FAMILY_NAME ); +EXPECT_TRUE (family_id >0 ); + +/* Test standard resolver (may fail) */ + no_family_id =genl_ctrl_resolve (socket_alloc_and_conn (), +LARGE_GENL_FAMILY_NAME ); +EXPECT_TRUE (no_family_id >0 ); +} +/** * TEST(capture_end) - Terminates Netlink traffic monitoring session *
Signature · 1 line
2.34.1
PATCH 14/15 selftests: net: genetlink: add Netlink and Generic Netlink test suite
In reply to Yana BashlykovaThis commit introduces a comprehensive test suite for Netlink and Generic Netlink functionality. The tests cover:
- Basic socket operations and protocol validation
- Controller family resolution and policy checks
- Multicast group communication
- Error handling and edge cases
- Socket option testing (EXT_ACK, NO_ENOBUFS, etc.)
- Stress testing and race conditions
The test suite uses kselftest harness and includes both positive and negative test cases to validate kernel behavior under various conditions.
--- tools/testing/selftests/net/genetlink.c | 3881 +++++++++++++++++++++++ 1 file changed, 3881 insertions(+)
tools/testing/selftests/net/genetlink.c +3881 -0
@@ -81,10 +81,34 @@#define LARGE_GENL_FAMILY_NAME "LARGE_GENL" +#define SOME_RANDOM_CMD 99 +/** * Callback data structures - used to pass data between test cases and message handlers */ +struct callback_data { +int int_value; +char *message; +char *some_info; +}; + +struct callback_data_parallel_dump { +char *name; +char *desc; +}; + +struct callback_data_parallel_dump data_check[] = { + {"TEST_GENL" ,"one" }, + {"PARALLEL_GENL" ,"two" }, + {"THIRD_GENL" ,"three" }, + {"LARGE_GENL" ,"four" }, +}; + +#define DATA_SIZE (sizeof(data_check) / sizeof(data_check[0])) + +static int elems; +struct callback_data_ctrl {int family_id;char *family_name;
@@ -93,6 +117,221 @@ struct callback_data_ctrl {int family_index; }; +struct callback_data_third { +char *message; +int flag; +}; + +int validate_cb_parallel (struct nl_msg *msg,void *arg) +{ +struct genlmsghdr *gnlh =nlmsg_data (nlmsg_hdr (msg)); +struct nlattr *attrs[PARALLEL_GENL_ATTR_MAX +1 ]; +int ret =0 ; +char *message =NULL ; + + ret =genlmsg_parse (nlmsg_hdr (msg),0 , attrs,PARALLEL_GENL_ATTR_MAX , +NULL ); + +if (ret <0 ) { +printf ("Failed to parse attributes: %d\n" , ret); +return NL_STOP ; + } + +struct callback_data *data = (struct callback_data *)arg; + +switch (gnlh->cmd) { +case PARALLEL_GENL_CMD_SEND : +if (attrs[PARALLEL_GENL_ATTR_DATA ]) { + message = +nla_get_string (attrs[PARALLEL_GENL_ATTR_DATA ]); + data->message =strdup (message); +if (data->message ==NULL ) { +perror ("strdup failed" ); +return NL_SKIP ; + } + }else { +printf ("Attribute not found.\n" ); +return NL_SKIP ; + } +return NL_OK ; +case PARALLEL_GENL_CMD_GET_VALUE : +if (attrs[PARALLEL_GENL_ATTR_DATA ]) { + message = +nla_get_string (attrs[PARALLEL_GENL_ATTR_DATA ]); + data->message =strdup (message); +if (data->message ==NULL ) { +perror ("strdup failed" ); +return NL_SKIP ; + } + } +return NL_OK ; + } +return NL_OK ; +} + +int validate_cb_parallel_dump (struct nl_msg *msg,void *arg) +{ +struct genlmsghdr *gnlh =nlmsg_data (nlmsg_hdr (msg)); +struct nlattr *attrs[PARALLEL_GENL_ATTR_MAX +1 ]; +int ret =0 ; +char *name; +char *desc; + + ret =genlmsg_parse (nlmsg_hdr (msg),0 , attrs,PARALLEL_GENL_ATTR_MAX , +NULL ); + +if (ret <0 ) { +printf ("Failed to parse attributes: %d\n" , ret); +return NL_STOP ; + } + +struct callback_data_parallel_dump *data = + (struct callback_data_parallel_dump *)arg; +switch (gnlh->cmd) { +case PARALLEL_GENL_CMD_DUMP_INFO : +if (attrs[PARALLEL_GENL_ATTR_NAME ]) { + name =nla_get_string (attrs[PARALLEL_GENL_ATTR_NAME ]); + data->name =strdup (name); +if (data->name ==NULL ) { +perror ("strdup failed" ); +free (data); +return NL_SKIP ; + } + +if (strcmp (data->name, data_check[elems].name)) +printf ("[FAILED]\n" ); + } +if (attrs[PARALLEL_GENL_ATTR_DESC ]) { + desc =nla_get_string (attrs[PARALLEL_GENL_ATTR_DESC ]); + data->desc =strdup (desc); +if (data->desc ==NULL ) { +perror ("strdup failed" ); +return NL_SKIP ; + } +if (strcmp (data->desc, data_check[elems].desc)) +printf ("[FAILED]\n" ); + } + elems++; +return NL_OK ; + } +return NL_OK ; +} + +int validate_cb_third (struct nl_msg *msg,void *arg) +{ +struct genlmsghdr *gnlh =nlmsg_data (nlmsg_hdr (msg)); +struct nlattr *attrs[PARALLEL_GENL_ATTR_MAX +1 ]; +int ret =0 ; +int flag =-1 ; +char *message =NULL ; + + ret =genlmsg_parse (nlmsg_hdr (msg),0 , attrs,PARALLEL_GENL_ATTR_MAX , +NULL ); + +if (ret <0 ) { +printf ("Failed to parse attributes: %d\n" , ret); +return NL_STOP ; + } + +struct callback_data_third *data = (struct callback_data_third *)arg; + +switch (gnlh->cmd) { +case THIRD_GENL_CMD_ECHO : +if (attrs[THIRD_GENL_ATTR_DATA ]) { + message =nla_get_string (attrs[THIRD_GENL_ATTR_DATA ]); + data->message =strdup (message); +if (data->message ==NULL ) { +perror ("strdup failed" ); +return NL_SKIP ; + } + } +if (attrs[THIRD_GENL_ATTR_FLAG ]) { + flag =nla_get_flag (attrs[THIRD_GENL_ATTR_FLAG ]); + data->flag = flag; + } +return NL_OK ; + } +return NL_OK ; +} + +int validate_cb (struct nl_msg *msg,void *arg) +{ +struct genlmsghdr *gnlh =nlmsg_data (nlmsg_hdr (msg)); +struct nlattr *attrs[MY_GENL_ATTR_MAX +1 ]; +int ret =0 ; +int int_value =30 ; + + ret =genlmsg_parse (nlmsg_hdr (msg),0 , attrs,MY_GENL_ATTR_MAX ,NULL ); + +if (ret <0 ) { +printf ("Failed to parse attributes: %d\n" , ret); +return NL_STOP ; + } + +struct callback_data *data = (struct callback_data *)arg; + +switch (gnlh->cmd) { +case MY_GENL_CMD_GET_VALUE : +if (attrs[MY_GENL_ATTR_VALUE ]) { +if (nla_len (attrs[MY_GENL_ATTR_VALUE ]) >=sizeof (int )) { + int_value = +nla_get_u32 (attrs[MY_GENL_ATTR_VALUE ]); + data->int_value = int_value; + }else { +fprintf (stderr, +"MY_GENL_ATTR_VALUE has incorrect size" ); +return NL_STOP ; + } + } +return NL_OK ; + +case MY_GENL_CMD_SET_VALUE : +if (attrs[MY_GENL_ATTR_VALUE ]) { +if (nla_len (attrs[MY_GENL_ATTR_VALUE ]) >=sizeof (int )) { + int_value = +nla_get_u32 (attrs[MY_GENL_ATTR_VALUE ]); + data->int_value = int_value; + }else { +fprintf (stderr, +"MY_GENL_ATTR_VALUE has incorrect size" ); +return NL_STOP ; + } + } +return NL_OK ; + +case MY_GENL_CMD_ECHO : +if (attrs[MY_GENL_ATTR_DATA ]) { +char *message = +nla_get_string (attrs[MY_GENL_ATTR_DATA ]); + data->message =strdup (message); +if (data->message ==NULL ) { +perror ("strdup failed" ); +return NL_SKIP ; + } + }else { +printf ("Attribute not found.\n" ); +return NL_SKIP ; + } +return NL_OK ; + +case MY_GENL_SMALL_CMD_GET : +if (attrs[MY_GENL_ATTR_DATA ]) { +char *message = +nla_get_string (attrs[MY_GENL_ATTR_DATA ]); + data->message =strdup (message); +if (data->message ==NULL ) { +perror ("strdup failed" ); +return NL_SKIP ; + } + } +return NL_OK ; +default : +printf ("Unknown command: %u\n" , gnlh->cmd); +break ; + } +return NL_OK ; +} +static int elem;static int id_elem;
@@ -479,6 +718,91 @@ int validate_cb_ctrl(struct nl_msg *msg, void *arg)return NL_OK ; } +#define BUFFER_SIZE 256 + +int read_string_from_sysfs (const char *path,char *buffer,size_t buffer_size) +{ +int fd =open (path,O_RDONLY ); + +if (fd <0 ) { +fprintf (stderr,"Error opening %s for reading sysfs: %s\n" , + path,strerror (errno)); +return -errno; + } + +ssize_t len =read (fd, buffer, buffer_size -1 ); + +if (len <0 ) { +fprintf (stderr,"Error reading to %s: %s\n" , path, +strerror (errno)); +close (fd); +return -errno; + } + + buffer[len] ='\0' ; +close (fd); +return 0 ; +} + +int read_int_from_sysfs (const char *path,int *value) +{ +char buffer[BUFFER_SIZE ]; +int ret; + + ret =read_string_from_sysfs (path, buffer,sizeof (buffer)); + +if (ret !=0 ) +return ret; + +char *endptr; +long val =strtol (buffer, &endptr,10 ); + +if (endptr == buffer) { +fprintf (stderr,"Conversion error: %s\n" ,strerror (errno)); +return -errno; + } + + *value = (int )val; +return 0 ; +} + +int write_string_to_sysfs (const char *path,const char *value) +{ +int fd =open (path,O_WRONLY ); + +if (fd <0 ) { +fprintf (stderr,"Error opening %s for writing sysfs: %s\n" , + path,strerror (errno)); +return -errno; + } + +ssize_t len =write (fd, value,strlen (value)); + +if (len <0 ) { +fprintf (stderr,"Error writing to %s: %s\n" , path, +strerror (errno)); +close (fd); +return -errno; + } + +close (fd); +return 0 ; +} + +int write_int_to_sysfs (const char *path,int value) +{ +char buffer[32 ]; + +int ret =snprintf (buffer,sizeof (buffer),"%d" , value); + +if (ret <0 || ret >=sizeof (buffer)) { +fprintf (stderr,"Conversion error: %s\n" ,strerror (errno)); +return -errno; + } + +return write_string_to_sysfs (path, buffer); +} +struct nl_sock *socket_alloc_and_conn (void ) {struct nl_sock *socket;
@@ -1179,6 +1503,3563 @@ TEST(resolve_large_family_id)EXPECT_TRUE (no_family_id >0 ); } +/** +* TEST(genl_small_cmd_get_value) - Tests small command in Generic Netlink family +* +* Validates: +* 1. Family resolution and socket setup +* 2. Message formatting and transmission +* 3. Response validation +* 4. Sysfs integration check +* +* Test Flow: +* 1. Resolves target Generic Netlink family +* 2. Prepares request with MY_GENL_SMALL_CMD_GET +* 3. Validates response through callback +* 4. Compares result with sysfs reference data +* 5. Cleans up test artifacts +* +* Special Handling: +* - Requires root for sysfs operations +* - Uses custom validation callback +* - Resets sysfs state after test +*/ + +TEST (genl_small_cmd_get_value) +{ +struct nl_sock *sock; +int family_id; +struct nl_msg *msg; +struct genlmsghdr *user_hdr; +struct nl_cb *cb; +int err =0 ; +struct callback_data cb_data; + + cb_data.int_value =-30 ; + cb_data.message =NULL ; + +printf ("Running Test: getting value using small cmd...\n" ); + +// Only root can write to sysfs (needs for testing) +if (geteuid ()) { +SKIP (return,"test requires root" ); +return ; + } + + sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , sock); +if (!sock) { +fprintf (stderr,"socket is NULL\n" ); +return ; + } + + family_id =genl_ctrl_resolve (sock,MY_GENL_FAMILY_NAME ); +EXPECT_GT (family_id,0 ); +if (family_id <0 ) { +fprintf (stderr,"Failed to resolve family id: %d\n" , family_id); +nl_socket_free (sock); + err = family_id; +return ; + } + + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +nl_socket_free (sock); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , family_id,0 , +NLM_F_REQUEST ,MY_GENL_SMALL_CMD_GET ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); +nlmsg_free (msg); +nl_socket_free (sock); +return ; + } + + cb =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb); +if (!cb) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (sock); +return ; + } + +char *str =malloc (BUFFER_SIZE ); + + err =nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb, &cb_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Error setting callback: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_send_auto (sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_recvmsgs (sock, cb); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to receive message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =read_string_from_sysfs (PATH_GENL_TEST_MES , str,BUFFER_SIZE ); +EXPECT_EQ (err,0 ); +if (err) { +fprintf (stderr,"Failed to read from sysfs: %s\n" , +strerror (err)); + goto error; + } + +EXPECT_STREQ (str, cb_data.message); + + err =write_string_to_sysfs (PATH_GENL_TEST_MES ,"default" ); +EXPECT_EQ (err,0 ); +if (err) { +fprintf (stderr,"Failed to write to sysfs: %s\n" , +strerror (err)); + goto error; + } + +error: +free (str); +nlmsg_free (msg); +nl_cb_put (cb); +nl_socket_free (sock); +} + +/** +* TEST(genl_parallel) - Tests message exchange with PARALLEL_GENL Generic Netlink family +* +* Validates end-to-end communication with PARALLEL_GENL family: +* 1. Establishes connection to PARALLEL_GENL family +* 2. Sends PARALLEL_GENL_CMD_SEND command +* 3. Checks results with sysfs reference data +* +* Test Flow: +* 1. Resolves PARALLEL_GENL family ID +* 2. Prepares and sends command message +* 3. Receives and validates response +* 4. Compares with sysfs reference data +* 5. Resets test environment +* +* Requirements: +* - Root privileges (for sysfs access) +* - Loaded parallel_genl kernel module +* - Properly configured sysfs interface +* +* Error Handling: +* - Validates all intermediate steps +* - Cleans up resources on failure +* - Resets sysfs state after test +*/ + +TEST (genl_parallel) +{ +struct nl_sock *sock; +int family_id; +struct nl_msg *msg; +struct genlmsghdr *user_hdr; +struct nl_cb *cb; +int err =0 ; +struct callback_data cb_data; + + cb_data.int_value =-30 ; + cb_data.message =NULL ; + +printf ("Running Test: sending message to parallel_genl...\n" ); + +// Only root can write to sysfs (needs for testing) +if (geteuid ()) { +SKIP (return,"test requires root" ); +return ; + } + + sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , sock); +if (!sock) { +fprintf (stderr,"socket is NULL\n" ); +return ; + } + + family_id =genl_ctrl_resolve (sock,PARALLEL_GENL_FAMILY_NAME ); +EXPECT_GT (family_id,0 ); +if (family_id <0 ) { +fprintf (stderr,"Failed to resolve family id: %d\n" , family_id); +nl_socket_free (sock); + err = family_id; +return ; + } + + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +nl_socket_free (sock); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , family_id,0 , +NLM_F_REQUEST ,PARALLEL_GENL_CMD_SEND ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); +nlmsg_free (msg); +nl_socket_free (sock); +return ; + } + + cb =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb); +if (!cb) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (sock); +return ; + } + +char *str =malloc (BUFFER_SIZE ); + + err =nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb_parallel, + &cb_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Error setting callback: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_send_auto (sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_recvmsgs (sock, cb); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to receive message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =read_string_from_sysfs (PATH_PARALLEL_GENL_MES , str,BUFFER_SIZE ); +EXPECT_EQ (err,0 ); +if (err) { +fprintf (stderr,"Failed to read from sysfs: %s\n" , +strerror (err)); + goto error; + } + +EXPECT_STREQ (str, cb_data.message); + + err =write_string_to_sysfs (PATH_PARALLEL_GENL_MES ,"default" ); +EXPECT_EQ (err,0 ); +if (err) { +fprintf (stderr,"Failed to write to sysfs: %s\n" , +strerror (err)); + goto error; + } + +error: +free (str); +nlmsg_free (msg); +nl_cb_put (cb); +nl_socket_free (sock); +} + +/** +* TEST(genl_parallel_dump) - Tests parallel dump operation in Generic Netlink +* +* Validates the parallel dump functionality of the PARALLEL_GENL family by: +* 1. Sending a dump request (NLM_F_DUMP flag) with PARALLEL_GENL_CMD_DUMP_INFO command +* 2. Processing multiple response messages through callback validation +* 3. Verifying correct handling of parallel data streams +* +* Key Features Tested: +* - Multi-message dump handling capability +* - Parallel data stream processing +* - Callback validation of dump contents +* - Resource management during extended operations +* +* Test Flow: +* 1. Resolve PARALLEL_GENL family ID +* 2. Prepare dump request message: +* - Sets NLM_F_DUMP flag +* - Uses PARALLEL_GENL_CMD_DUMP_INFO command +* 3. Configure callback (validate_cb_parallel_dump) for message processing +* 4. Execute send/receive operations +* 5. Validate results through callback +* +* Error Handling: +* - Verifies all intermediate steps +* - Proper resource cleanup on failure +* - Callback determines final test status +*/ + +TEST (genl_parallel_dump) +{ +struct nl_sock *sock; +int family_id; +struct nl_msg *msg; +struct genlmsghdr *user_hdr; +struct nl_cb *cb; +int err =0 ; + +struct callback_data_parallel_dump cb_data; + +printf ("Running Test: doing parallel dump with genl_parallel_dump...\n" ); + sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , sock); +if (!sock) { +fprintf (stderr,"socket is NULL\n" ); +return ; + } + + family_id =genl_ctrl_resolve (sock,PARALLEL_GENL_FAMILY_NAME ); +EXPECT_GT (family_id,0 ); +if (family_id <0 ) { +fprintf (stderr,"Failed to resolve family id: %d\n" , family_id); +nl_socket_free (sock); + err = family_id; +return ; + } + + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +nl_socket_free (sock); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , family_id,0 , +NLM_F_DUMP ,PARALLEL_GENL_CMD_DUMP_INFO ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); +nlmsg_free (msg); +nl_socket_free (sock); +return ; + } + + cb =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb); +if (!cb) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (sock); +return ; + } + +char *str =malloc (BUFFER_SIZE ); + + err =nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , + validate_cb_parallel_dump, &cb_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Error setting callback: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_send_auto (sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_recvmsgs (sock, cb); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to receive message: %s\n" , +nl_geterror (err)); + goto error; + } + +// callback function determines whether the test is successful + +EXPECT_EQ (elems,DATA_SIZE ); + elems =0 ; + +error: +free (str); +nlmsg_free (msg); +nl_cb_put (cb); +nl_socket_free (sock); +} + +static struct nl_msg *genl_generate_messages (int family_id,int nonblock) +{ +struct nl_msg *msg; +struct genlmsghdr *user_hdr; +char *data; +int err; + + msg =nlmsg_alloc (); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +return NULL ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , family_id,0 , +NLM_F_REQUEST ,PARALLEL_GENL_CMD_SEND ,0 ); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); +nlmsg_free (msg); +return NULL ; + } + +if (nonblock) { +if (nla_put_flag (msg,PARALLEL_GENL_ATTR_FLAG_NONBLOCK ) <0 ) { +fprintf (stderr, +"Failed to add PARALLEL_GENL_ATTR_FLAG_NONBLOCK flag attribute: %s\n" , +strerror (errno)); +nlmsg_free (msg); +return NULL ; + } + }else { +if (nla_put_flag (msg,PARALLEL_GENL_ATTR_FLAG_BLOCK ) <0 ) { +fprintf (stderr, +"Failed to add PARALLEL_GENL_ATTR_FLAG_BLOCK flag attribute: %s\n" , +strerror (errno)); +nlmsg_free (msg); +return NULL ; + } + } + +int data_size =4068 ; + + data =malloc (data_size); +if (!data) { +fprintf (stderr,"Failed to allocate data buffer\n" ); +nlmsg_free (msg); +return NULL ; + } +memset (data,1 , data_size); + + err =nla_put (msg,PARALLEL_GENL_ATTR_BINARY , data_size, data); +if (err <0 ) { +fprintf (stderr, +"Failed to add PARALLEL_GENL_ATTR_BINARY attribute: %s\n" , +strerror (errno)); +free (data); +nlmsg_free (msg); +return NULL ; + } + +return msg; +} + +/** +* TEST(incorrect_genl_parallel_with_flag_nonblock_sock) - Tests buffer overflow in +* non-blocking Generic Netlink socket +* +* Validates kernel behavior under buffer overflow conditions by: +* 1. Creating a non-blocking socket with reduced buffer size (5000 bytes) +* 2. Rapidly sending multiple messages (20x) without waiting for responses +* 3. Verifying proper error handling when buffer capacity is exceeded +* +* Expected Behavior: +* - First receive attempt should fail with NLE_NOMEM (ENOBUFS) +* - Subsequent receives should return NLE_AGAIN (EAGAIN) +* - Number of successfully received messages should be less than sent (kernel drops packets) +* +* Test Methodology: +* 1. Configure non-blocking socket with small buffer +* 2. Flood the socket with back-to-back messages +* 3. Verify error sequence: +* a) Initial ENOBUFS when buffer is full +* b) Subsequent EAGAIN for empty buffer +* 4. Confirm packet loss (recv_count < send_count) +* +* Technical Notes: +* - Requires root for socket configuration +* - Uses custom message generator (genl_generate_messages) +* - Tests kernel's congestion control mechanisms +* - Validates Netlink's non-blocking error handling +*/ + +TEST (incorrect_genl_parallel_with_flag_nonblock_sock) +{ +struct nl_sock *sock; +int family_id; +struct nl_msg *msg; +struct nl_cb *cb; +int err =0 ; +int send_c; +int recv_c; +struct callback_data cb_data; + + cb_data.int_value =-30 ; + cb_data.message =NULL ; + +printf ("Running Test: trying to overflow buffer of nonblock socket...\n" ); + +// Only root can write to sysfs (needs for testing) +if (geteuid ()) { +SKIP (return,"test requires root" ); +return ; + } + + sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , sock); +if (!sock) { +fprintf (stderr,"socket is NULL\n" ); +return ; + } + + family_id =genl_ctrl_resolve (sock,PARALLEL_GENL_FAMILY_NAME ); +EXPECT_GT (family_id,0 ); +if (family_id <0 ) { +fprintf (stderr,"Failed to resolve family id: %d\n" , family_id); +nl_socket_free (sock); + err = family_id; +return ; + } + + err =nl_socket_set_buffer_size (sock,5000 ,5000 ); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to change socket buffer size: %s\n" , +nl_geterror (err)); +nl_socket_free (sock); +return ; + } + + err =nl_socket_set_nonblocking (sock); +EXPECT_EQ (err,0 ); + + cb =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb); +if (!cb) { +fprintf (stderr,"Failed to allocate callback\n" ); +nl_socket_free (sock); +return ; + } + + err =nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb_parallel, + &cb_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Error setting callback: %s\n" , +nl_geterror (err)); +nl_socket_free (sock); +return ; + } + + send_c =20 ; + recv_c =0 ; +for (int i =0 ; i < send_c; i++) { + msg =genl_generate_messages (family_id,1 ); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to create message\n" ); + goto error; + } + err =nl_send_auto (sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } +nlmsg_free (msg); + } + +// the buffer is full + err =nl_recvmsgs (sock, cb); +EXPECT_EQ (err, -NLE_NOMEM ); +EXPECT_EQ (errno,ENOBUFS ); + +for (int i =1 ; i < send_c; i++) { + err =nl_recvmsgs (sock, cb); +if (err <0 ) { +EXPECT_EQ (err, -NLE_AGAIN ); +EXPECT_EQ (errno,EAGAIN ); + } +if (err ==0 ) + recv_c++; + } +EXPECT_LT (recv_c, send_c); + +error: +nl_cb_put (cb); +nl_socket_free (sock); +} + +/** +* TEST(incorrect_genl_parallel_with_flag_block_sock) - Tests buffer overflow behavior in +* blocking Generic Netlink socket +* +* Validates kernel packet handling under buffer overflow conditions with blocking sockets: +* 1. Configures socket with small buffer (1000 bytes) and 1-second timeout +* 2. Sends burst of messages (8x) to trigger overflow +* 3. Verifies correct error sequence and packet loss +* +* Expected Behavior: +* - Initial receive returns NLE_NOMEM (ENOBUFS) when buffer is full +* - Subsequent receives get partial messages (recv_count < send_count) +* - Final receive returns NLE_AGAIN (EAGAIN) when queue is empty +* +* Test Methodology: +* 1. Reduce socket buffer size to force quick overflow +* 2. Set conservative timeout (1s) for blocked receives +* 3. Send message burst without waiting for responses +* 4. Verify: +* a) Initial ENOBUFS error +* b) Partial message delivery +* c) Terminal EAGAIN condition +* +* Technical Notes: +* - Demonstrates kernel's congestion control with blocking sockets +* - Shows Netlink's reliability limits under overload +* - Uses SO_RCVTIMEO to prevent indefinite blocking +* - Requires root for socket configuration +*/ + +TEST (incorrect_genl_parallel_with_flag_block_sock) +{ +struct nl_sock *sock; +int family_id; +struct nl_msg *msg; +struct nl_cb *cb; +int err =0 ; +struct callback_data cb_data; + + cb_data.int_value =-30 ; + cb_data.message =NULL ; + +struct timeval tv = {1 ,0 }; + +int send_c; +int recv_c; + +printf ("Running Test: trying to overflow buffer of block socket...\n" ); + +// Only root can write to sysfs (needs for testing) +if (geteuid ()) { +SKIP (return,"test requires root" ); +return ; + } + + sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , sock); +if (!sock) { +fprintf (stderr,"socket is NULL\n" ); +return ; + } + + family_id =genl_ctrl_resolve (sock,PARALLEL_GENL_FAMILY_NAME ); +EXPECT_GT (family_id,0 ); +if (family_id <0 ) { +fprintf (stderr,"Failed to resolve family id: %d\n" , family_id); +nl_socket_free (sock); + err = family_id; +return ; + } + + err =nl_socket_set_buffer_size (sock,1000 ,1000 ); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to change socket buffer size: %s\n" , +nl_geterror (err)); +nl_socket_free (sock); +return ; + } + + err =setsockopt (nl_socket_get_fd (sock),SOL_SOCKET ,SO_RCVTIMEO , &tv, +sizeof (tv)); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to set timeout for block socket\n" ); +nl_socket_free (sock); +return ; + } + + cb =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb); +if (!cb) { +fprintf (stderr,"Failed to allocate callback\n" ); +nl_socket_free (sock); +return ; + } + + err =nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb_parallel, + &cb_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Error setting callback: %s\n" , +nl_geterror (err)); +nl_socket_free (sock); +return ; + } + + send_c =8 ; +for (int i =0 ; i < send_c; i++) { + msg =genl_generate_messages (family_id,0 ); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to create message\n" ); + goto error; + } + err =nl_send_auto (sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } +nlmsg_free (msg); + } + + recv_c =0 ; + +// the buffer is full + err =nl_recvmsgs (sock, cb); +EXPECT_EQ (err, -NLE_NOMEM ); +EXPECT_EQ (errno,ENOBUFS ); + +for (int i =1 ; i < send_c; i++) { + err =nl_recvmsgs (sock, cb); +if (err ==0 ) + recv_c++; + +if (err <0 ) { +EXPECT_EQ (err, -NLE_AGAIN ); +EXPECT_EQ (errno,EAGAIN ); + } + } + +EXPECT_TRUE (err == -NLE_AGAIN && errno ==EAGAIN ); +EXPECT_LT (recv_c, send_c); + +error: +nl_cb_put (cb); +nl_socket_free (sock); +} + +/** +* TEST(genl_test_get_value) - Tests basic communication with TEST_GENL family +* +* Validates end-to-end message exchange including: +* 1. Family and multicast group resolution +* 2. Message formatting and transmission +* 3. Response validation through callback +* 4. Sysfs integration verification +* +* Test Flow: +* 1. Establish connection to TEST_GENL +* 2. Resolve multicast group MY_MCGRP_GENL +* 3. Prepare GET_VALUE request message: +* - Sets MY_GENL_CMD_GET_VALUE command +* - Includes path attribute (PATH_GENL_TEST_NUM) +* 4. Send request and validate response +* 5. Compare result with sysfs reference data +* 6. Reset test environment +* +* Special Handling: +* - Requires root privileges (sysfs access) +* - Uses custom validation callback +* - Verifies both Netlink and sysfs interfaces +* - Automatic cleanup of test artifacts +*/ + +TEST (genl_test_get_value) +{ +struct nl_sock *nl_sock; +int family_id; +int mcgrp_id; +struct nl_msg *msg; +struct nl_cb *cb; +int err =0 ; +int int_value =-10 ; +struct genlmsghdr *user_hdr; +struct callback_data cb_data; + + cb_data.int_value =20 ; + cb_data.message =NULL ; + +printf ("Running Test: getting value from sysfs via Netlink message...\n" ); + +// Only root can write to sysfs (needs for testing) +if (geteuid ()) { +SKIP (return,"test requires root" ); +return ; + } + + nl_sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , nl_sock); +if (!nl_sock) +return ; + + family_id =genl_ctrl_resolve (nl_sock,MY_GENL_FAMILY_NAME ); +EXPECT_GT (family_id,0 ); +if (family_id <0 ) { +nl_socket_free (nl_sock); + err = family_id; +return ; + } + + mcgrp_id =genl_ctrl_resolve_grp (nl_sock,MY_GENL_FAMILY_NAME , +MY_MCGRP_NAME ); +EXPECT_GE (mcgrp_id,0 ); +if (mcgrp_id <0 ) { +nl_socket_free (nl_sock); + err = mcgrp_id; +return ; + } + + err =nl_socket_add_membership (nl_sock, mcgrp_id); +EXPECT_EQ (err,0 ); +if (err <0 ) { +nl_socket_free (nl_sock); +return ; + } + + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +nl_socket_free (nl_sock); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , family_id,0 , +NLM_F_REQUEST ,MY_GENL_CMD_GET_VALUE ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +nlmsg_free (msg); +nl_socket_free (nl_sock); +return ; + } + +if (nla_put_string (msg,MY_GENL_ATTR_PATH ,PATH_GENL_TEST_NUM ) <0 ) { +nlmsg_free (msg); +nl_socket_free (nl_sock); +ASSERT_EQ (0 ,1 ); +return ; + } + + cb =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb); +if (!cb) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (nl_sock); +return ; + } + + err =nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb, &cb_data); +EXPECT_EQ (err,0 ); +if (err <0 ) + goto error; + + err =nl_send_auto (nl_sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); +nlmsg_free (msg); +nl_cb_put (cb); +return ; + } + + err =nl_recvmsgs (nl_sock, cb); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to receive message: %s\n" , +nl_geterror (err)); +nlmsg_free (msg); +nl_cb_put (cb); +return ; + } + + err =read_int_from_sysfs (PATH_GENL_TEST_NUM , &int_value); +EXPECT_EQ (err,0 ); +if (err) { +fprintf (stderr,"Failed to read from sysfs: %s\n" , +strerror (err)); + goto error; + } + +EXPECT_EQ (int_value, cb_data.int_value); + + err =write_int_to_sysfs (PATH_GENL_TEST_NUM ,-20 ); +EXPECT_EQ (err,0 ); +if (err) { +fprintf (stderr,"Failed to write to sysfs: %s\n" , +strerror (err)); + goto error; + } + +error: +nlmsg_free (msg); +nl_cb_put (cb); +nl_socket_free (nl_sock); +} + +/** +* TEST(genl_test_echo) - Tests multicast echo functionality in TEST_GENL family +* +* Validates the echo request/reply mechanism through multicast groups by: +* 1. Configuring socket with CAP_ACK capability +* 2. Sending echo request (NLM_F_ECHO flag) with MY_GENL_CMD_ECHO command +* 3. Receiving and validating multicast response +* 4. Verifying message content against sysfs reference +* +* Protocol Details: +* - Uses NETLINK_CAP_ACK for reliable multicast delivery +* - Leverages multicast group (MY_MCGRP_GENL) for replies +* - Validates both Netlink and sysfs message content +* +* Test Flow: +* 1. Establish Netlink connection +* 2. Enable CAP_ACK socket option +* 3. Resolve family and multicast group IDs +* 4. Join multicast group +* 5. Prepare and send echo request +* 6. Receive and validate multicast reply +* 7. Compare with sysfs reference data +* 8. Reset test environment +* +* Requirements: +* - Root privileges (for sysfs and socket configuration) +* - Properly configured multicast group in kernel +* - Validated echo handler in kernel module +*/ + +TEST (genl_test_echo) +{ +struct nl_sock *sock; +int family_id; +int mcgrp_id; +struct nl_msg *msg; +struct genlmsghdr *user_hdr; +struct nl_cb *cb; +int err =0 ; +int sock_fd; +int cap_ack =1 ; +int optval; +socklen_t optlen =sizeof (optval); +struct callback_data cb_data; + + cb_data.int_value =-30 ; + cb_data.message =NULL ; + +printf ("Running Test: getting message that was sent to mcast groups...\n" ); + +// Only root can write to sysfs (needs for testing) +if (geteuid ()) { +SKIP (return,"test requires root" ); +return ; + } + + sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , sock); +if (!sock) +return ; + + sock_fd =nl_socket_get_fd (sock); +EXPECT_GE (sock_fd,0 ); +if (sock_fd <0 ) { +nl_socket_free (sock); + err = sock_fd; +return ; + } + + err =setsockopt (sock_fd,SOL_NETLINK ,NETLINK_CAP_ACK , &cap_ack, +sizeof (cap_ack)); +EXPECT_EQ (err,0 ); +if (err <0 ) { +nl_socket_free (sock); +return ; + } + + err =getsockopt (sock_fd,SOL_NETLINK ,NETLINK_CAP_ACK , &optval, + &optlen); +EXPECT_EQ (err,0 ); +if (err <0 ) { +nl_socket_free (sock); +return ; + } + + family_id =genl_ctrl_resolve (sock,MY_GENL_FAMILY_NAME ); +EXPECT_GT (family_id,0 ); +if (family_id <0 ) { +nl_socket_free (sock); + err = family_id; +return ; + } + + mcgrp_id = +genl_ctrl_resolve_grp (sock,MY_GENL_FAMILY_NAME ,MY_MCGRP_NAME ); +EXPECT_GE (mcgrp_id,0 ); +if (mcgrp_id <0 ) { + err = mcgrp_id; +fprintf (stderr,"Failed to resolve multicast group: %s\n" , +nl_geterror (err)); +nl_socket_free (sock); +return ; + } + + err =nl_socket_add_membership (sock, mcgrp_id); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to add membership: %s\n" , +nl_geterror (err)); +nl_socket_free (sock); +return ; + } + + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +nl_socket_free (sock); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , family_id,0 , +NLM_F_ECHO ,MY_GENL_CMD_ECHO ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +nlmsg_free (msg); +nl_socket_free (sock); +return ; + } + + cb =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb); +if (!cb) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (sock); +return ; + } + +char *str =malloc (BUFFER_SIZE ); + + err =nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb, &cb_data); +EXPECT_EQ (err,0 ); +if (err <0 ) + goto error; + + err =nl_send_auto (sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); +free (str); +nlmsg_free (msg); +nl_cb_put (cb); +return ; + } + + err =nl_recvmsgs (sock, cb); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to receive message: %s\n" , +nl_geterror (err)); +free (str); +nlmsg_free (msg); +nl_cb_put (cb); +return ; + } + + err =read_string_from_sysfs (PATH_GENL_TEST_MES , str,BUFFER_SIZE ); +EXPECT_EQ (err,0 ); +if (err) { +fprintf (stderr,"Failed to read from sysfs: %s\n" , +strerror (err)); + goto error; + } + +EXPECT_STREQ (str, cb_data.message); + + err =write_string_to_sysfs (PATH_GENL_TEST_MES ,"default" ); +EXPECT_EQ (err,0 ); +if (err) { +fprintf (stderr,"Failed to write to sysfs: %s\n" , +strerror (err)); + goto error; + } + +error: +free (str); +nlmsg_free (msg); +nl_cb_put (cb); +nl_socket_free (sock); +} + +/** +* TEST(genl_test_set_value) - Tests value setting functionality in TEST_GENL family +* +* Validates the complete SET_VALUE operation workflow: +* 1. Configures socket with STRICT_CHK option for robust message validation +* 2. Sends properly formatted SET_VALUE command with: +* - Target path (PATH_GENL_TEST_NUM) +* - Integer value to set (1) +* 3. Verifies correct processing through: +* - Netlink response validation +* - Sysfs value verification +* +* Key Features Tested: +* - Strict checking option (NETLINK_GET_STRICT_CHK) +* - Multicast group communication +* - Attribute packing and unpacking +* - End-to-end sysfs integration +* +* Test Flow: +* 1. Initialize Netlink connection with strict checking +* 2. Resolve family and multicast group IDs +* 3. Prepare SET_VALUE request with path and value attributes +* 4. Send request and validate response +* 5. Verify sysfs value was updated correctly +* 6. Reset test environment +* +* Special Handling: +* - Requires root privileges (sysfs modification) +* - Uses custom validation callback +* - Automatic cleanup of test artifacts +*/ + +TEST (genl_test_set_value) +{ +struct nl_sock *nl_sock; +int family_id; +int mcgrp_id; +struct nl_msg *msg; +struct nl_cb *cb; +int err =0 ; +int int_value; +int sock_fd; +int up =1 ; +struct genlmsghdr *user_hdr; +int optval; + +socklen_t optlen =sizeof (optval); + +struct callback_data cb_data; + + cb_data.int_value =74 ; + cb_data.message =NULL ; + +printf ("Running Test: sending correct value for sysfs to genl_test...\n" ); + +// Only root can write to sysfs (needs for testing) +if (geteuid ()) { +SKIP (return,"test requires root" ); +return ; + } + + nl_sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , nl_sock); +if (!nl_sock) +return ; + + sock_fd =nl_socket_get_fd (nl_sock); +EXPECT_GE (sock_fd,0 ); +if (sock_fd <0 ) { + err = sock_fd; +nl_socket_free (nl_sock); +return ; + } + + err =setsockopt (sock_fd,SOL_NETLINK ,NETLINK_GET_STRICT_CHK , &up, +sizeof (up)); +EXPECT_EQ (err,0 ); +if (err <0 ) { +nl_socket_free (nl_sock); +return ; + } + + err =getsockopt (sock_fd,SOL_NETLINK ,NETLINK_GET_STRICT_CHK , &optval, + &optlen); +EXPECT_EQ (err,0 ); +if (err <0 ) { +nl_socket_free (nl_sock); +return ; + } + + family_id =genl_ctrl_resolve (nl_sock,MY_GENL_FAMILY_NAME ); +EXPECT_GT (family_id,0 ); +if (family_id <0 ) { +nl_socket_free (nl_sock); + err = family_id; +return ; + } + + mcgrp_id =genl_ctrl_resolve_grp (nl_sock,MY_GENL_FAMILY_NAME , +MY_MCGRP_NAME ); +EXPECT_GE (mcgrp_id,0 ); +if (mcgrp_id <0 ) { +nl_socket_free (nl_sock); + err = mcgrp_id; +return ; + } + + err =nl_socket_add_membership (nl_sock, mcgrp_id); +EXPECT_EQ (err,0 ); +if (err <0 ) { +nl_socket_free (nl_sock); +return ; + } + + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +nl_socket_free (nl_sock); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , family_id,0 , +NLM_F_REQUEST ,MY_GENL_CMD_SET_VALUE ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +nlmsg_free (msg); +nl_socket_free (nl_sock); +return ; + } + +if (nla_put_string (msg,MY_GENL_ATTR_PATH ,PATH_GENL_TEST_NUM ) <0 ) { +nlmsg_free (msg); +nl_socket_free (nl_sock); +ASSERT_EQ (0 ,1 ); +return ; + } + +if (nla_put_u32 (msg,MY_GENL_ATTR_VALUE ,1 ) <0 ) { +nlmsg_free (msg); +nl_socket_free (nl_sock); +ASSERT_EQ (0 ,1 ); +return ; + } + + cb =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb); +if (!cb) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (nl_sock); +return ; + } + + err =nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb, &cb_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Error setting callback: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_send_auto (nl_sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); +nlmsg_free (msg); +nl_cb_put (cb); +return ; + } + + err =nl_recvmsgs (nl_sock, cb); +EXPECT_TRUE (err ==0 || err == -NLE_PERM ); +if (err <0 ) { +if (geteuid () !=0 ) +EXPECT_EQ (err, -NLE_PERM ); + +EXPECT_EQ (err,0 ); +fprintf (stderr,"Failed to receive message: %s\n" , +nl_geterror (err)); +nlmsg_free (msg); +nl_cb_put (cb); +return ; + } + + err =read_int_from_sysfs (PATH_GENL_TEST_NUM , &int_value); +EXPECT_EQ (err,0 ); +if (err) { +fprintf (stderr,"Failed to read from sysfs: %s\n" , +strerror (err)); + goto error; + } + +EXPECT_EQ (int_value, cb_data.int_value); + + err =write_int_to_sysfs (PATH_GENL_TEST_NUM ,-20 ); +EXPECT_EQ (err,0 ); +if (err) { +fprintf (stderr,"Failed to write to sysfs: %s\n" , +strerror (err)); + goto error; + } + +error: +nlmsg_free (msg); +nl_cb_put (cb); +nl_socket_free (nl_sock); +} + +/** +* TEST(incorrect_genl_test_set_value) - Tests error handling in TEST_GENL family +* +* Validates proper error reporting for invalid SET_VALUE operations by: +* 1. Enabling extended ACK (NETLINK_EXT_ACK) for detailed error messages +* 2. Configuring broadcast error reporting (NETLINK_BROADCAST_ERROR) +* 3. Sending intentionally incorrect value (34) to trigger error +* 4. Verifying kernel returns appropriate error codes: +* - NLE_INVAL for root (invalid value) +* - NLE_PERM for non-root (permission denied) +* +* Key Features Tested: +* - Extended ACK error reporting mechanism +* - Broadcast error propagation +* - Kernel validation of input values +* - Permission checking +* +* Test Flow: +* 1. Configure socket with EXT_ACK and BROADCAST_ERROR options +* 2. Resolve family and multicast group IDs +* 3. Prepare SET_VALUE request with invalid value (34) +* 4. Send request and verify error response +* 5. Validate error code matches expectations +* +* Special Handling: +* - Different expected errors for root/non-root +* - Uses kernel's extended ACK reporting +* - Tests both value validation and permission checks +*/ + +TEST (incorrect_genl_test_set_value) +{ +struct nl_sock *nl_sock; +int family_id; +int mcgrp_id; +struct nl_msg *msg; +struct nl_cb *cb; +int err; +int sock_fd; +int ext_ack =1 ; +int broadcast_error =1 ; +struct genlmsghdr *user_hdr; + +int optval; + +socklen_t optlen =sizeof (optval); + +struct callback_data cb_data; + + cb_data.int_value =74 ; + cb_data.message =NULL ; + +printf ("Running Test: sending incorrect value for sysfs to genl_test...\n" ); + + nl_sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , nl_sock); +if (!nl_sock) { +printf ("socket for my_genl is NULL\n" ); +return ; + } + + sock_fd =nl_socket_get_fd (nl_sock); +EXPECT_GE (sock_fd,0 ); +if (sock_fd <0 ) { +fprintf (stderr,"Failed to get socket file descriptor\n" ); + err = sock_fd; +nl_socket_free (nl_sock); +return ; + } + + err =setsockopt (sock_fd,SOL_NETLINK ,NETLINK_EXT_ACK , &ext_ack, +sizeof (ext_ack)); +EXPECT_EQ (err,0 ); +if (err <0 ) { +nl_socket_free (nl_sock); +return ; + } + + err =getsockopt (sock_fd,SOL_NETLINK ,NETLINK_EXT_ACK , &optval, + &optlen); +EXPECT_EQ (err,0 ); +if (err <0 ) { +nl_socket_free (nl_sock); +return ; + } + +EXPECT_EQ (optval,1 ); + + err =setsockopt (sock_fd,SOL_NETLINK ,NETLINK_BROADCAST_ERROR , + &broadcast_error,sizeof (broadcast_error)); +EXPECT_EQ (err,0 ); +if (err <0 ) { +nl_socket_free (nl_sock); +return ; + } + + err =getsockopt (sock_fd,SOL_NETLINK ,NETLINK_BROADCAST_ERROR , &optval, + &optlen); +EXPECT_EQ (err,0 ); +if (err <0 ) { +nl_socket_free (nl_sock); +return ; + } + +EXPECT_EQ (optval,1 ); + + family_id =genl_ctrl_resolve (nl_sock,MY_GENL_FAMILY_NAME ); +EXPECT_GT (family_id,0 ); +if (family_id <0 ) { + err = family_id; +fprintf (stderr,"Failed to resolve family id: %s\n" , +nl_geterror (err)); +nl_socket_free (nl_sock); +return ; + } + + mcgrp_id =genl_ctrl_resolve_grp (nl_sock,MY_GENL_FAMILY_NAME , +MY_MCGRP_NAME ); +EXPECT_GE (mcgrp_id,0 ); +if (mcgrp_id <0 ) { + err = mcgrp_id; +fprintf (stderr,"Failed to resolve multicast group: %s\n" , +nl_geterror (err)); +nl_socket_free (nl_sock); +return ; + } + + err =nl_socket_add_membership (nl_sock, mcgrp_id); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to add membership: %s\n" , +nl_geterror (err)); +nl_socket_free (nl_sock); +return ; + } + + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +nl_socket_free (nl_sock); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , family_id,0 , +NLM_F_REQUEST ,MY_GENL_CMD_SET_VALUE ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); +nlmsg_free (msg); +nl_socket_free (nl_sock); +return ; + } + +if (nla_put_string (msg,MY_GENL_ATTR_PATH ,PATH_GENL_TEST_NUM ) <0 ) { +fprintf (stderr, +"Failed to add MY_GENL_ATTR_PATH attribute: %s\n" , +strerror (errno)); +nlmsg_free (msg); +nl_socket_free (nl_sock); +ASSERT_EQ (0 ,1 ); +return ; + } + +if (nla_put_u32 (msg,MY_GENL_ATTR_VALUE ,34 ) <0 ) { +fprintf (stderr, +"Failed to add MY_GENL_ATTR_VALUE attribute: %s\n" , +strerror (errno)); +nlmsg_free (msg); +nl_socket_free (nl_sock); +ASSERT_EQ (0 ,1 ); +return ; + } + + cb =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb); +if (!cb) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (nl_sock); +return ; + } + + err =nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb, &cb_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +printf ("Error setting callback\n" ); + goto error; + } + + err =nl_send_auto (nl_sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } + +// nl_recvmsgs gets error code from error ACK from kernel + err =nl_recvmsgs (nl_sock, cb); +EXPECT_TRUE (err == -NLE_PERM || err == -NLE_INVAL ); +if (err <0 ) { +if (geteuid () !=0 ) +EXPECT_EQ (err, -NLE_PERM ); + +if (geteuid () ==0 ) +EXPECT_EQ (err, -NLE_INVAL ); + } +EXPECT_LT (err,0 ); + +error: +nlmsg_free (msg); +nl_cb_put (cb); +nl_socket_free (nl_sock); +} + +/** +* TEST(incorrect_family) - Tests handling of non-existent Generic Netlink family +* +* Validates proper error reporting when attempting to resolve: +* 1. A deliberately non-existent Generic Netlink family +* 2. Expected error code (NLE_OBJ_NOTFOUND) +* +* Key Features Tested: +* - Controller behavior for unknown family names +* - Correct error code propagation +* - Graceful handling of resolution failures +* +* Test Methodology: +* 1. Creates standard Netlink socket +* 2. Attempts to resolve "SOME_RANDOM_NAME" family +* 3. Verifies: +* - Return value is negative (error) +* - Specific error code is NLE_OBJ_NOTFOUND +* - Clean resource deallocation +* +* Negative Testing: +* - Demonstrates proper error handling for invalid inputs +* - Verifies kernel API contract for unknown families +*/ + +TEST (incorrect_family) +{ +struct nl_sock *sock; +int family_id; + +printf ("Running Test: trying to find id of incorrect Netlink family...\n" ); + + sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , sock); +if (!sock) { +fprintf (stderr,"socket is NULL\n" ); +return ; + } + + family_id =genl_ctrl_resolve (sock,"SOME_RANDOM_NAME" ); +EXPECT_EQ (family_id, -NLE_OBJ_NOTFOUND ); + +nl_socket_free (sock); +} + +/** +* TEST(incorrect_family_id) - Tests error handling for invalid family ID +* +* Validates proper error reporting when sending messages to: +* 1. A deliberately incorrect family ID (valid ID + 1) +* 2. With otherwise valid command (MY_GENL_SMALL_CMD_GET) +* +* Expected Behavior: +* - Kernel should return NLE_OPNOTSUPP ("Operation not supported") +* - Demonstrates proper error propagation through Netlink stack +* +* Test Methodology: +* 1. Resolves valid family ID for reference +* 2. Creates message with intentionally invalid family ID (+1) +* 3. Verifies: +* - Message transmission succeeds +* - Kernel responds with NLE_OPNOTSUPP +* - Resources are properly cleaned up +* +* Negative Testing: +* - Confirms kernel validates family IDs +* - Tests error handling for invalid destinations +* - Verifies API contract for unsupported operations +*/ + +TEST (incorrect_family_id) +{ +struct nl_sock *sock; +int family_id; +struct nl_msg *msg; +struct genlmsghdr *user_hdr; +struct nl_cb *cb; +int err; +struct callback_data cb_data; + + cb_data.int_value =-30 ; + cb_data.message =NULL ; + +printf ("Running Test: trying to send a message to incorrect id of Netlink family (== nonexistent cmd)...\n" ); + + sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , sock); +if (!sock) { +fprintf (stderr,"socket is NULL\n" ); +return ; + } + + family_id =genl_ctrl_resolve (sock,MY_GENL_FAMILY_NAME ); +EXPECT_GT (family_id,0 ); +if (family_id <0 ) { + err = family_id; +fprintf (stderr,"Failed to resolve family id: %s\n" , +nl_geterror (err)); +nl_socket_free (sock); +return ; + } + + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +nl_socket_free (sock); +return ; + } + +// put incorrect id for Netlink message + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , family_id +1 ,0 , +NLM_F_REQUEST ,MY_GENL_SMALL_CMD_GET ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); +nlmsg_free (msg); +nl_socket_free (sock); +return ; + } + + cb =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb); +if (!cb) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (sock); +return ; + } + + err =nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb, &cb_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Error setting callback: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_send_auto (sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_recvmsgs (sock, cb); +EXPECT_EQ (err, -NLE_OPNOTSUPP ); + +error: +nlmsg_free (msg); +nl_cb_put (cb); +nl_socket_free (sock); +} + +/** +* TEST(incorrect_cmd) - Tests error handling for invalid Generic Netlink commands +* +* Validates proper error reporting when: +* 1. Sending a message with deliberately invalid command (SOME_RANDOM_CMD) +* 2. Using otherwise correct family ID and message format +* +* Expected Behavior: +* - Kernel should return NLE_OPNOTSUPP ("Operation not supported") +* - Demonstrates command validation in Generic Netlink subsystem +* +* Test Methodology: +* 1. Resolves valid family ID +* 2. Creates message with invalid command number +* 3. Verifies: +* - Message transmission succeeds (invalid command is syntactically valid) +* - Kernel responds with NLE_OPNOTSUPP +* - Resources are properly cleaned up +* +* Negative Testing: +* - Confirms kernel validates command numbers +* - Tests error handling for unsupported operations +* - Verifies API contract for invalid commands +*/ + +TEST (incorrect_cmd) +{ +struct nl_sock *sock; +int family_id; +struct nl_msg *msg; +struct genlmsghdr *user_hdr; +struct nl_cb *cb; +int err; +struct callback_data cb_data; + + cb_data.int_value =-30 ; + cb_data.message =NULL ; + +printf ("Running Test: trying to send incorrect == non-existent cmd...\n" ); + + sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , sock); +if (!sock) { +fprintf (stderr,"socket is NULL\n" ); +return ; + } + + family_id =genl_ctrl_resolve (sock,MY_GENL_FAMILY_NAME ); +EXPECT_GT (family_id,0 ); +if (family_id <0 ) { + err = family_id; +fprintf (stderr,"Failed to resolve family id: %s\n" , +nl_geterror (err)); +nl_socket_free (sock); +return ; + } + + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +nl_socket_free (sock); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , family_id,0 , +NLM_F_REQUEST ,SOME_RANDOM_CMD ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); +nlmsg_free (msg); +nl_socket_free (sock); +return ; + } + + cb =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb); +if (!cb) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (sock); +return ; + } + + err =nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb, &cb_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Error setting callback: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_send_auto (sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_recvmsgs (sock, cb); +EXPECT_EQ (err, -NLE_OPNOTSUPP ); + +error: +nlmsg_free (msg); +nl_cb_put (cb); +nl_socket_free (sock); +} + +/** +* TEST(incorrect_ctrl_family_name) - Tests error handling for invalid family names +* in controller requests +* +* Validates that the Generic Netlink controller: +* 1. Properly rejects requests with non-existent family names +* 2. Returns appropriate error code (NLE_INVAL) +* 3. Maintains system stability when receiving invalid parameters +* +* Test Methodology: +* 1. Creates valid connection to genl_ctrl +* 2. Prepares GETFAMILY request with invalid family name +* 3. Verifies: +* - Controller rejects request with NLE_INVAL +* - Error is properly propagated to userspace +* - Resources are correctly cleaned up +* +* Expected Behavior: +* - Kernel should return NLE_INVAL ("Invalid input data or parameter") +* - Controller should maintain stable operation +* - No memory leaks or resource issues +* +* Negative Testing: +* - Confirms input validation in Generic Netlink controller +* - Tests error handling for non-existent entities +* - Verifies API contract for invalid parameters +*/ + +TEST (incorrect_ctrl_family_name) +{ +struct nl_sock *ctrl_sock; +int genl_ctrl_family_id; +struct nl_msg *msg; +struct genlmsghdr *user_hdr; +struct nl_cb *cb_ctrl; +int err; +struct callback_data_ctrl cb_ctrl_data; + + cb_ctrl_data.family_id =-30 ; + cb_ctrl_data.family_name =NULL ; + cb_ctrl_data.op =-100 ; + +printf ("Running Test: sending invalid family name to genl_ctrl...\n" ); + + ctrl_sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , ctrl_sock); +if (!ctrl_sock) { +fprintf (stderr,"socket for genl_ctrl is NULL\n" ); +return ; + } + + genl_ctrl_family_id =genl_ctrl_resolve (ctrl_sock,GENL_CTRL ); +EXPECT_GT (genl_ctrl_family_id,0 ); +if (genl_ctrl_family_id <0 ) { + err = genl_ctrl_family_id; +fprintf (stderr, +"Failed to resolve family id for genl_ctrl: %s\n" , +nl_geterror (err)); +nl_socket_free (ctrl_sock); +return ; + } + + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +nl_socket_free (ctrl_sock); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , + genl_ctrl_family_id,0 ,NLM_F_REQUEST , +CTRL_CMD_GETFAMILY ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); +nlmsg_free (msg); +nl_socket_free (ctrl_sock); +return ; + } + +if (nla_put_string (msg,CTRL_ATTR_FAMILY_NAME ,"SOME_RANDOM_NAME" ) < +0 ) { +fprintf (stderr, +"Failed to add incorrect CTRL_ATTR_FAMILY_NAME attribute: %s\n" , +strerror (errno)); +nlmsg_free (msg); +nl_socket_free (ctrl_sock); +ASSERT_EQ (0 ,1 ); +return ; + } + + cb_ctrl =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb_ctrl); +if (!cb_ctrl) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (ctrl_sock); +return ; + } + + err =nl_cb_set (cb_ctrl,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb_ctrl, + &cb_ctrl_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Error setting callback: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_send_auto (ctrl_sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_recvmsgs (ctrl_sock, cb_ctrl); +EXPECT_EQ (err, -NLE_INVAL ); + +error: +nlmsg_free (msg); +nl_cb_put (cb_ctrl); +nl_socket_free (ctrl_sock); +} + +/** +* TEST(incorrect_genl_parallel_with_flag_nonblock_sock_enobufs) - Tests +* NETLINK_NO_ENOBUFS socket option behavior +* +* Validates the interaction of buffer overflow handling with NETLINK_NO_ENOBUFS by: +* 1. Configuring socket with NETLINK_NO_ENOBUFS option +* 2. Artificially creating buffer overflow conditions +* 3. Verifying the absence of ENOBUFS errors +* 4. Confirming packet loss occurs during overflow +* +* Key Features Tested: +* - NETLINK_NO_ENOBUFS socket option functionality +* - Non-blocking socket behavior under congestion +* - Kernel packet dropping policy +* - Error code propagation +* +* Test Methodology: +* 1. Configure socket with: +* - Small buffer size (2000 bytes) +* - Non-blocking mode +* - NETLINK_NO_ENOBUFS option +* 2. Flood the socket with back-to-back messages (15x) +* 3. Verify: +* - No ENOBUFS errors occur +* - Only EAGAIN is reported when buffer is empty +* - Packet loss occurs (recv_count < send_count) +* +* Expected Behavior: +* - Kernel should suppress ENOBUFS errors when NETLINK_NO_ENOBUFS is set +* - Messages should be silently dropped when buffer is full +* - Application should only see EAGAIN for empty buffer +*/ + +TEST (incorrect_genl_parallel_with_flag_nonblock_sock_enobufs) +{ +struct nl_sock *sock; +int family_id; +struct nl_msg *msg; +struct nl_cb *cb; +int err =0 ; +int send_c; +int recv_c; + +int no_enobufs_val =1 ; +int sock_fd; + +int optval; + +socklen_t optlen =sizeof (optval); + +struct callback_data cb_data; + + cb_data.int_value =-30 ; + cb_data.message =NULL ; + +printf ("Running Test: trying to overflow buffer of nonblock socket with NETLINK_NO_ENOBUFS socket option...\n" ); + +// Only root can write to sysfs (needs for testing) +if (geteuid ()) { +SKIP (return,"test requires root" ); +return ; + } + + sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , sock); +if (!sock) { +fprintf (stderr,"socket is NULL\n" ); +return ; + } + + sock_fd =nl_socket_get_fd (sock); +EXPECT_GE (sock_fd,0 ); +if (sock_fd <0 ) { + err = sock_fd; +fprintf (stderr,"Failed to get socket file descriptor: %s\n" , +nl_geterror (err)); +nl_socket_free (sock); +return ; + } + + err =setsockopt (sock_fd,SOL_NETLINK ,NETLINK_NO_ENOBUFS , + &no_enobufs_val,sizeof (no_enobufs_val)); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to set NETLINK_NO_ENOBUFS: %s\n" , +strerror (errno)); +nl_socket_free (sock); +return ; + } + + err =getsockopt (sock_fd,SOL_NETLINK ,NETLINK_NO_ENOBUFS , &optval, + &optlen); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to get NETLINK_NO_ENOBUFS: %s\n" , +strerror (errno)); +nl_socket_free (sock); +return ; + } + +EXPECT_EQ (optval,1 ); + + family_id =genl_ctrl_resolve (sock,PARALLEL_GENL_FAMILY_NAME ); +EXPECT_GT (family_id,0 ); +if (family_id <0 ) { +fprintf (stderr,"Failed to resolve family id: %d\n" , family_id); +nl_socket_free (sock); +return ; + } + + err =nl_socket_set_buffer_size (sock,2000 ,2000 ); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to change socket buffer size: %s\n" , +nl_geterror (err)); +nl_socket_free (sock); +return ; + } + + err =nl_socket_set_nonblocking (sock); +EXPECT_EQ (err,0 ); + + cb =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb); +if (!cb) { +fprintf (stderr,"Failed to allocate callback\n" ); +nl_socket_free (sock); +return ; + } + + err =nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb_parallel, + &cb_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Error setting callback: %s\n" , +nl_geterror (err)); +nl_socket_free (sock); +return ; + } + + send_c =15 ; + recv_c =0 ; +for (int i =0 ; i < send_c; i++) { + msg =genl_generate_messages (family_id,1 ); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to create message\n" ); + goto error; + } + err =nl_send_auto (sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } +nlmsg_free (msg); + } + +for (int i =0 ; i < send_c; i++) { + err =nl_recvmsgs (sock, cb); +if (err <0 ) { +EXPECT_EQ (err, -NLE_AGAIN ); +EXPECT_EQ (errno,EAGAIN ); + } +if (err ==0 ) + recv_c++; + +EXPECT_NE (errno,ENOBUFS ); + } +EXPECT_LT (recv_c, send_c); + +error: +nl_cb_put (cb); +nl_socket_free (sock); +} + +/** +* TEST(genl_test_get_value_sock_option) - Tests NETLINK_NO_ENOBUFS socket option functionality +* +* Validates the complete workflow of setting and using the NETLINK_NO_ENOBUFS socket option: +* 1. Sets and verifies NETLINK_NO_ENOBUFS socket option +* 2. Performs standard GET_VALUE operation +* 3. Verifies successful message exchange despite option being set +* +* Key Features Tested: +* - NETLINK_NO_ENOBUFS option setting and verification +* - Normal operation with the option enabled +* - Integration with existing GET_VALUE functionality +* - Sysfs value verification +* +* Test Flow: +* 1. Configure socket with NETLINK_NO_ENOBUFS option +* 2. Verify option was correctly set +* 3. Resolve family and multicast group IDs +* 4. Prepare and send GET_VALUE request +* 5. Validate response and sysfs value +* 6. Clean up test environment +* +* Special Considerations: +* - Requires root privileges for sysfs access +* - Tests both socket option functionality and normal operation +* - Maintains backward compatibility with existing behavior +*/ + +TEST (genl_test_get_value_sock_option) +{ +struct nl_sock *nl_sock; +int family_id; +int mcgrp_id; +struct nl_msg *msg; +struct nl_cb *cb; +int err; +int int_value; +int no_enobufs_val =1 ; +int sock_fd; + +int optval; + +socklen_t optlen =sizeof (optval); + +struct genlmsghdr *user_hdr; + +struct callback_data cb_data; + +printf ("Running Test: sending a message with NETLINK_NO_ENOBUFS socket option...\n" ); + +// Only root can write to sysfs (needs for testing) +if (geteuid ()) { +SKIP (return,"test requires root" ); +return ; + } + + nl_sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , nl_sock); +if (!nl_sock) { +printf ("socket for my_genl is NULL\n" ); +return ; + } + + sock_fd =nl_socket_get_fd (nl_sock); +EXPECT_GE (sock_fd,0 ); +if (sock_fd <0 ) { + err = sock_fd; +fprintf (stderr,"Failed to get socket file descriptor: %s\n" , +nl_geterror (err)); +nl_socket_free (nl_sock); +return ; + } + + err =setsockopt (sock_fd,SOL_NETLINK ,NETLINK_NO_ENOBUFS , + &no_enobufs_val,sizeof (no_enobufs_val)); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to set NETLINK_NO_ENOBUFS: %s\n" , +strerror (errno)); + err = -errno; +nl_socket_free (nl_sock); +return ; + } + + err =getsockopt (sock_fd,SOL_NETLINK ,NETLINK_NO_ENOBUFS , &optval, + &optlen); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to get NETLINK_NO_ENOBUFS: %s\n" , +strerror (errno)); + err = -errno; +nl_socket_free (nl_sock); +return ; + } + +EXPECT_EQ (optval,1 ); + + family_id =genl_ctrl_resolve (nl_sock,MY_GENL_FAMILY_NAME ); +EXPECT_GT (family_id,0 ); +if (family_id <0 ) { + err = family_id; +fprintf (stderr,"Failed to resolve family id: %s\n" , +nl_geterror (err)); +nl_socket_free (nl_sock); +return ; + } + + mcgrp_id =genl_ctrl_resolve_grp (nl_sock,MY_GENL_FAMILY_NAME , +MY_MCGRP_NAME ); +EXPECT_GE (mcgrp_id,0 ); +if (mcgrp_id <0 ) { + err = mcgrp_id; +fprintf (stderr,"Failed to resolve multicast group: %s\n" , +nl_geterror (err)); +nl_socket_free (nl_sock); +return ; + } + + err =nl_socket_add_membership (nl_sock, mcgrp_id); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to add membership: %s\n" , +nl_geterror (err)); +nl_socket_free (nl_sock); +return ; + } + + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +nl_socket_free (nl_sock); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , family_id,0 , +NLM_F_REQUEST ,MY_GENL_CMD_GET_VALUE ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); +nlmsg_free (msg); +nl_socket_free (nl_sock); +return ; + } + +if (nla_put_string (msg,MY_GENL_ATTR_PATH ,PATH_GENL_TEST_NUM ) <0 ) { +fprintf (stderr, +"Failed to add MY_GENL_ATTR_PATH attribute: %s\n" , +strerror (errno)); +nlmsg_free (msg); +nl_socket_free (nl_sock); +ASSERT_EQ (0 ,1 ); +return ; + } + + cb =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb); +if (!cb) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (nl_sock); +return ; + } + + err =nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb, &cb_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Error setting callback: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_send_auto (nl_sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_recvmsgs (nl_sock, cb); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to receive message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =read_int_from_sysfs (PATH_GENL_TEST_NUM , &int_value); +EXPECT_EQ (err,0 ); +if (err) { +fprintf (stderr,"Failed to read from sysfs: %s\n" , +strerror (err)); + goto error; + } + +EXPECT_EQ (int_value, cb_data.int_value); + + err =write_int_to_sysfs (PATH_GENL_TEST_NUM ,-20 ); +EXPECT_EQ (err,0 ); +if (err) { +fprintf (stderr,"Failed to write to sysfs: %s\n" , +strerror (err)); + goto error; + } + +error: +nlmsg_free (msg); +nl_cb_put (cb); +nl_socket_free (nl_sock); +} + +/** +* TEST(incorrect_genl_test_ext_ack) - NETLINK_EXT_ACK error handling verification +* +* Validates extended error acknowledgment mechanism by: +* 1. Testing successful setting of NETLINK_EXT_ACK socket option +* 2. Sending deliberately malformed Generic Netlink message: +* - Contains invalid path attribute (MY_GENL_ATTR_PATH="some/random/path") +* 3. Verifying kernel returns expected error (EINVAL/NLE_INVAL) +* 4. Checking proper delivery of extended error information via EXT_ACK +* +* Expected behavior: +* - NETLINK_EXT_ACK option should be successfully set (optval == 1) +* - Kernel should reject malformed message with -NLE_INVAL error +* +* Implementation notes: +* - Uses synthetic invalid path to guarantee error generation +* - Tests complete workflow: option setup -> message send -> error handling +* - Covers both basic error code and extended error information +*/ + +TEST (incorrect_genl_test_ext_ack) +{ +struct nl_sock *nl_sock; +int family_id; +int mcgrp_id; +struct nl_msg *msg; +struct nl_cb *cb; +int err; +int up; +int sock_fd; + +int optval; + +socklen_t optlen =sizeof (optval); + +struct genlmsghdr *user_hdr; +struct callback_data cb_data; + +printf ("Running Test: sending a message with NETLINK_EXT_ACK socket option and incorrect path value...\n" ); + + nl_sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , nl_sock); +if (!nl_sock) { +fprintf (stderr,"socket for my_genl is NULL\n" ); +return ; + } + + sock_fd =nl_socket_get_fd (nl_sock); +EXPECT_GE (sock_fd,0 ); +if (sock_fd <0 ) { + err = sock_fd; +fprintf (stderr,"Failed to get socket file descriptor: %s\n" , +nl_geterror (err)); +nl_socket_free (nl_sock); +return ; + } + + up =1 ; + err =setsockopt (sock_fd,SOL_NETLINK ,NETLINK_EXT_ACK , &up, +sizeof (up)); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to set NETLINK_EXT_ACK: %s" , +strerror (errno)); + err = -errno; +nl_socket_free (nl_sock); +return ; + } + + err =getsockopt (sock_fd,SOL_NETLINK ,NETLINK_EXT_ACK , &optval, + &optlen); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to get NETLINK_EXT_ACK: %s" , +strerror (errno)); + err = -errno; +nl_socket_free (nl_sock); +return ; + } + +EXPECT_EQ (optval,1 ); + + family_id =genl_ctrl_resolve (nl_sock,MY_GENL_FAMILY_NAME ); +EXPECT_GT (family_id,0 ); +if (family_id <0 ) { + err = family_id; +fprintf (stderr,"Failed to resolve family id: %s\n" , +nl_geterror (err)); +nl_socket_free (nl_sock); +return ; + } + + mcgrp_id =genl_ctrl_resolve_grp (nl_sock,MY_GENL_FAMILY_NAME , +MY_MCGRP_NAME ); +EXPECT_GE (mcgrp_id,0 ); +if (mcgrp_id <0 ) { + err = mcgrp_id; +fprintf (stderr,"Failed to resolve multicast group: %s\n" , +nl_geterror (err)); +nl_socket_free (nl_sock); +return ; + } + + err =nl_socket_add_membership (nl_sock, mcgrp_id); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to add membership: %s\n" , +nl_geterror (err)); +nl_socket_free (nl_sock); +return ; + } + + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +nl_socket_free (nl_sock); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , family_id,0 , +NLM_F_REQUEST ,MY_GENL_CMD_GET_VALUE ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); +nlmsg_free (msg); +nl_socket_free (nl_sock); +return ; + } + +if (nla_put_string (msg,MY_GENL_ATTR_PATH ,"some/random/path" ) <0 ) { +fprintf (stderr, +"Failed to add MY_GENL_ATTR_PATH attribute: %s\n" , +strerror (errno)); +nlmsg_free (msg); +nl_socket_free (nl_sock); +ASSERT_EQ (0 ,1 ); +return ; + } + + cb =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb); +if (!cb) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (nl_sock); +return ; + } + + err =nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb, &cb_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Error setting callback: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_send_auto (nl_sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to send message: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_recvmsgs (nl_sock, cb); +EXPECT_EQ (err, -NLE_INVAL ); + +error: +nlmsg_free (msg); +nl_cb_put (cb); +nl_socket_free (nl_sock); +} + +/** +* TEST(genl_test_sock_listen_all_nsid) - Tests NETLINK_LISTEN_ALL_NSID socket option functionality +* +* Validates the behavior of NETLINK_LISTEN_ALL_NSID socket option by: +* 1. Setting the NETLINK_LISTEN_ALL_NSID option on a netlink socket +* 2. Verifying proper socket operation with this option enabled +* 3. Testing permission enforcement (root privileges required) +* 4. Performing end-to-end communication with the option set +* +* Test flow: +* - Creates netlink socket with increased buffer size +* - Sets NETLINK_LISTEN_ALL_NSID option +* - Configures Generic Netlink communication: +* - Resolves family and multicast group IDs +* - Sets up message and callback +* - Performs round-trip message exchange +* - Verifies sysfs interaction matches received data +* +* Special considerations: +* - Requires root privileges (checks and skips if not root) +* - No getsockopt available for NETLINK_LISTEN_ALL_NSID verification +* - Tests both successful operation and permission error cases +* - Includes sysfs value validation for complete functionality check +* +* Expected results: +* - Root user: Successful option setting and normal operation +* - Correct message handling through entire communication chain +* - Proper sysfs value synchronization with received data +* - Non-root: Graceful test skipping +*/ + +TEST (genl_test_sock_listen_all_nsid) +{ +struct nl_sock *nl_sock; +int family_id; +int mcgrp_id; +struct nl_msg *msg; +struct nl_cb *cb; +int err; +int int_value; +int up; +int sock_fd; + +struct genlmsghdr *user_hdr; +struct callback_data cb_data; + +printf ("Running Test: setting NETLINK_LISTEN_ALL_NSID option for socket...\n" ); + +// Only root can listen to different Netlink namespaces +if (geteuid ()) { +SKIP (return,"test requires root" ); +return ; + } + + nl_sock =socket_alloc_and_conn (); +EXPECT_NE (NULL , nl_sock); +if (!nl_sock) { +printf ("socket for my_genl is NULL\n" ); +return ; + } + + err =nl_socket_set_buffer_size (nl_sock,8000 ,8000 ); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to change socket buffer size: %s\n" , +strerror (err)); +nl_socket_free (nl_sock); +return ; + } + + sock_fd =nl_socket_get_fd (nl_sock); +EXPECT_GE (sock_fd,0 ); +if (sock_fd <0 ) { + err = sock_fd; +fprintf (stderr,"Failed to get socket file descriptor: %s\n" , +nl_geterror (err)); +nl_socket_free (nl_sock); +return ; + } + + up =1 ; + err =setsockopt (sock_fd,SOL_NETLINK ,NETLINK_LISTEN_ALL_NSID , &up, +sizeof (up)); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to set NETLINK_LISTEN_ALL_NSID: %s\n" , +strerror (err)); +nl_socket_free (nl_sock); +return ; + } + + family_id =genl_ctrl_resolve (nl_sock,MY_GENL_FAMILY_NAME ); +EXPECT_GT (family_id,0 ); +if (family_id <0 ) { + err = family_id; +fprintf (stderr,"Failed to resolve family id: %s\n" , +nl_geterror (err)); +nl_socket_free (nl_sock); +return ; + } + + mcgrp_id =genl_ctrl_resolve_grp (nl_sock,MY_GENL_FAMILY_NAME , +MY_MCGRP_NAME ); +EXPECT_GE (mcgrp_id,0 ); +if (mcgrp_id <0 ) { + err = mcgrp_id; +fprintf (stderr,"Failed to resolve multicast group: %s\n" , +nl_geterror (err)); +nl_socket_free (nl_sock); +return ; + } + + err =nl_socket_add_membership (nl_sock, mcgrp_id); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Failed to add membership: %s\n" , +nl_geterror (err)); +nl_socket_free (nl_sock); +return ; + } + + msg =nlmsg_alloc (); +EXPECT_NE (NULL , msg); +if (!msg) { +fprintf (stderr,"Failed to allocate message\n" ); +nl_socket_free (nl_sock); +return ; + } + + user_hdr =genlmsg_put (msg,NL_AUTO_PID ,NL_AUTO_SEQ , family_id,0 , +NLM_F_REQUEST ,MY_GENL_CMD_GET_VALUE ,0 ); +EXPECT_NE (NULL , user_hdr); +if (!user_hdr) { +fprintf (stderr,"Failed to genlmsg_put\n" ); +nlmsg_free (msg); +nl_socket_free (nl_sock); +return ; + } + +if (nla_put_string (msg,MY_GENL_ATTR_PATH ,PATH_GENL_TEST_NUM ) <0 ) { +fprintf (stderr, +"Failed to add MY_GENL_ATTR_PATH attribute: %s\n" , +strerror (errno)); +nlmsg_free (msg); +nl_socket_free (nl_sock); +ASSERT_EQ (0 ,1 ); +return ; + } + + cb =nl_cb_alloc (NL_CB_DEFAULT ); +EXPECT_NE (NULL , cb); +if (!cb) { +fprintf (stderr,"Failed to allocate callback\n" ); +nlmsg_free (msg); +nl_socket_free (nl_sock); +return ; + } + + err =nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , validate_cb, &cb_data); +EXPECT_EQ (err,0 ); +if (err <0 ) { +fprintf (stderr,"Error setting callback: %s\n" , +nl_geterror (err)); + goto error; + } + + err =nl_send_auto (nl_sock, msg); +EXPECT_GE (err,0 ); +if (err <0 ) {
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.34.1
Part of the diff is not shown here. View the raw message.
PATCH 15/15 selftests: net: genetlink: fix expectation for large family resolution
In reply to Yana BashlykovaCorrectly expect standard genl_ctrl_resolve() to fail for large families with many multicast groups. While kernel handles large families correctly, libnl's resolution fails due to message fragmentation:
- Responses exceed single message size (~32KB)
- Kernel fragments across multiple messages
- genl_ctrl_resolve() only processes first fragment
- Returns ENOMSG instead of family ID
Custom my_genl_ctrl_resolve() handles fragmentation correctly via dump mechanism. Change EXPECT_TRUE(no_family_id > 0) to EXPECT_TRUE(no_family_id < 0) to match actual behavior.
--- tools/testing/selftests/net/genetlink.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/net/genetlink.c +3 -2
@@ -437,6 +437,7 @@ int validate_cb_ctrl(struct nl_msg *msg, void *arg)return NL_OK ;case CTRL_CMD_GETPOLICY : + ;struct ctrl_policy *exp = &data_ctrl->expected_policy->policy[elem];if (attrs[CTRL_ATTR_FAMILY_ID ]) {
@@ -1485,7 +1486,7 @@ TEST(genl_ctrl_policy)* * Verification: * 1. Custom resolver returns valid ID (> 0) -* 2. Standard resolver either fails or succeeds (platform-dependent) +* 2. Standard resolver fails */ TEST (resolve_large_family_id)
@@ -1500,7 +1501,7 @@ TEST(resolve_large_family_id)/* Test standard resolver (may fail) */ no_family_id =genl_ctrl_resolve (socket_alloc_and_conn (),LARGE_GENL_FAMILY_NAME ); -EXPECT_TRUE (no_family_id >0 ); +EXPECT_TRUE (no_family_id <0 ); }/**
Signature · 1 line
2.34.1
Jakub Kicinski
maintainerQuoted 6 lines · On Fri, 12 Sep 2025 22:53:23 +0300 Yana Bashlykova wrote:
This series adds comprehensive testing infrastructure for Netlink and Generic Netlink
The implementation includes both kernel module and userspace tests to verify correct Generic Netlink and Netlink behaviors under various conditions.
What is the motivation for this work?
The motivation for this work is to improve the test coverage and reliability of the Netlink subsystem, specifically for the core af_netlink.c and genetlink.c components. While the subsystem is critical for kernel-userspace communication, its coverage by the existing selftests is quite limited.
To quantify the improvement, these new selftests achieve the following line coverage (as measured by gcov):
- net/netlink/af_netlink.c: 84.0%
- net/netlink/genetlink.c: 88.8%
Integrating these tests into the upstream suite will provide long-term stability and make it safer to refactor or add new features to the Netlink core in the future.
пт, 12 сент. 2025 г. в 23:17, Jakub Kicinski <[email protected]>:
пт, 12 сент. 2025 г. в 23:17, Jakub Kicinski <[email protected]>:
Quoted 10 lines · On Fri, 12 Sep 2025 22:53:23 +0300 Yana Bashlykova wrote:
Quoted 6 lines
This series adds comprehensive testing infrastructure for Netlink and Generic Netlink
The implementation includes both kernel module and userspace tests to verify correct Generic Netlink and Netlink behaviors under various conditions.
What is the motivation for this work?
Jakub Kicinski
maintainerQuoted 10 lines · On Wed, 15 Oct 2025 17:49:22 +0300 Яна Башлыкова wrote:
The motivation for this work is to improve the test coverage and reliability of the Netlink subsystem, specifically for the core
af_netlink.candgenetlink.ccomponents. While the subsystem is critical for kernel-userspace communication, its coverage by the existing selftests is quite limited.To quantify the improvement, these new selftests achieve the following line coverage (as measured by gcov):
-net/netlink/af_netlink.c: 84.0%
-net/netlink/genetlink.c: 88.8%
For what it's worth syzbot has:
af_netlink.c 91%
genetlink.c 68%Without a line of code added to the kernel. Of course it's not functional testing.
Quoted 3 lines
Integrating these tests into the upstream suite will provide long-term stability and make it safer to refactor or add new features to the Netlink core in the future.
Happy to hear from others if they disagree but what kernel tests get merged into the tree is pretty subjective. Do we have a lot of bugs in genetlink? Are you planning to do major development in this area and want to catch regressions? If the answers to both of those questions is "no" IMHO this 7kLoC is not worth carrying in the tree.