tools/testing/selftests/net/tcp_ao/lib/netlink.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/tcp_ao/lib/netlink.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/net/tcp_ao/lib/netlink.c- Extension
.c- Size
- 10374 bytes
- Lines
- 414
- 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.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/netlink.hlinux/random.hlinux/rtnetlink.hlinux/veth.hnet/if.hstdint.hstring.hsys/socket.haolib.h
Detected Declarations
struct nlmsgerrorfunction netlink_sockfunction netlink_check_answerfunction rtattr_packfunction rtattr_endfunction veth_pack_peerbfunction __add_vethfunction add_vethfunction __ip_addr_addfunction ip_addr_addfunction __ip_route_addfunction ip_route_add_vrffunction ip_route_addfunction __link_set_upfunction link_set_upfunction __add_vrffunction add_vrf
Annotated Snippet
struct nlmsgerror {
struct nlmsghdr hdr;
int error;
struct nlmsghdr orig_msg;
} answer;
if (recv(sock, &answer, sizeof(answer), 0) < 0) {
test_print("recv()");
return -1;
} else if (answer.hdr.nlmsg_type != NLMSG_ERROR) {
test_print("expected NLMSG_ERROR, got %d",
(int)answer.hdr.nlmsg_type);
return -1;
} else if (answer.error) {
if (!quite) {
test_print("NLMSG_ERROR: %d: %s",
answer.error, strerror(-answer.error));
}
return answer.error;
}
return 0;
}
static inline struct rtattr *rtattr_hdr(struct nlmsghdr *nh)
{
return (struct rtattr *)((char *)(nh) + RTA_ALIGN((nh)->nlmsg_len));
}
static int rtattr_pack(struct nlmsghdr *nh, size_t req_sz,
unsigned short rta_type, const void *payload, size_t size)
{
/* NLMSG_ALIGNTO == RTA_ALIGNTO, nlmsg_len already aligned */
struct rtattr *attr = rtattr_hdr(nh);
size_t nl_size = RTA_ALIGN(nh->nlmsg_len) + RTA_LENGTH(size);
if (req_sz < nl_size) {
test_print("req buf is too small: %zu < %zu", req_sz, nl_size);
return -1;
}
nh->nlmsg_len = nl_size;
attr->rta_len = RTA_LENGTH(size);
attr->rta_type = rta_type;
memcpy(RTA_DATA(attr), payload, size);
return 0;
}
static struct rtattr *_rtattr_begin(struct nlmsghdr *nh, size_t req_sz,
unsigned short rta_type, const void *payload, size_t size)
{
struct rtattr *ret = rtattr_hdr(nh);
if (rtattr_pack(nh, req_sz, rta_type, payload, size))
return 0;
return ret;
}
static inline struct rtattr *rtattr_begin(struct nlmsghdr *nh, size_t req_sz,
unsigned short rta_type)
{
return _rtattr_begin(nh, req_sz, rta_type, 0, 0);
}
static inline void rtattr_end(struct nlmsghdr *nh, struct rtattr *attr)
{
char *nlmsg_end = (char *)nh + nh->nlmsg_len;
attr->rta_len = nlmsg_end - (char *)attr;
}
static int veth_pack_peerb(struct nlmsghdr *nh, size_t req_sz,
const char *peer, int ns)
{
struct ifinfomsg pi;
struct rtattr *peer_attr;
memset(&pi, 0, sizeof(pi));
pi.ifi_family = AF_UNSPEC;
pi.ifi_change = 0xFFFFFFFF;
peer_attr = _rtattr_begin(nh, req_sz, VETH_INFO_PEER, &pi, sizeof(pi));
if (!peer_attr)
return -1;
if (rtattr_pack(nh, req_sz, IFLA_IFNAME, peer, strlen(peer)))
return -1;
Annotation
- Immediate include surface: `linux/netlink.h`, `linux/random.h`, `linux/rtnetlink.h`, `linux/veth.h`, `net/if.h`, `stdint.h`, `string.h`, `sys/socket.h`.
- Detected declarations: `struct nlmsgerror`, `function netlink_sock`, `function netlink_check_answer`, `function rtattr_pack`, `function rtattr_end`, `function veth_pack_peerb`, `function __add_veth`, `function add_veth`, `function __ip_addr_add`, `function ip_addr_add`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.