tools/testing/selftests/net/netlink-dumps.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/netlink-dumps.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/net/netlink-dumps.c
Extension
.c
Size
5729 bytes
Lines
264
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct ext_ack {
	int err;

	__u32 attr_offs;
	__u32 miss_type;
	__u32 miss_nest;
	const char *str;
};

enum get_ea_ret {
	ERROR = -1,
	NO_CTRL = 0,
	FOUND_DONE,
	FOUND_ERR,
	FOUND_EXTACK,
};

static enum get_ea_ret
nl_get_extack(char *buf, size_t n, struct ext_ack *ea)
{
	enum get_ea_ret ret = NO_CTRL;
	const struct nlmsghdr *nlh;
	const struct nlattr *attr;
	ssize_t rem;

	for (rem = n; rem > 0; NLMSG_NEXT(nlh, rem)) {
		nlh = (struct nlmsghdr *)&buf[n - rem];
		if (!NLMSG_OK(nlh, rem))
			return ERROR;

		if (nlh->nlmsg_type == NLMSG_ERROR)
			ret = FOUND_ERR;
		else if (nlh->nlmsg_type == NLMSG_DONE)
			ret = FOUND_DONE;
		else
			continue;

		ea->err = -*(int *)NLMSG_DATA(nlh);

		if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
			return ret;

		ynl_attr_for_each(attr, nlh, sizeof(int)) {
			switch (ynl_attr_type(attr)) {
			case NLMSGERR_ATTR_OFFS:
				ea->attr_offs = ynl_attr_get_u32(attr);
				break;
			case NLMSGERR_ATTR_MISS_TYPE:
				ea->miss_type = ynl_attr_get_u32(attr);
				break;
			case NLMSGERR_ATTR_MISS_NEST:
				ea->miss_nest = ynl_attr_get_u32(attr);
				break;
			case NLMSGERR_ATTR_MSG:
				ea->str = ynl_attr_get_str(attr);
				break;
			}
		}

		return FOUND_EXTACK;
	}

	return ret;
}

static const struct {
	struct nlmsghdr nlhdr;
	struct ndmsg ndm;
	struct nlattr ahdr;
	__u32 val;
} dump_neigh_bad = {
	.nlhdr = {
		.nlmsg_len	= sizeof(dump_neigh_bad),
		.nlmsg_type	= RTM_GETNEIGH,
		.nlmsg_flags	= NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
		.nlmsg_seq	= 1,
	},
	.ndm = {
		.ndm_family	= 123,
	},
	.ahdr = {
		.nla_len	= 4 + 4,
		.nla_type	= NDA_FLAGS_EXT,
	},
	.val = -1, // should fail MASK validation
};

TEST(dump_extack)
{
	int netlink_sock;

Annotation

Implementation Notes