1/*
2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017 The strace developers.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "tests.h"
30
31#include <stdio.h>
32#include "test_nlattr.h"
33#include <linux/if.h>
34#include <linux/if_arp.h>
35#ifdef HAVE_LINUX_IF_LINK_H
36# include <linux/if_link.h>
37#endif
38#include <linux/rtnetlink.h>
39
40#ifndef IFLA_XDP
41# define IFLA_XDP 43
42#endif
43#ifndef IFLA_XDP_FD
44# define IFLA_XDP_FD 1
45#endif
46
47const unsigned int hdrlen = sizeof(struct ifinfomsg);
48
49static void
50init_ifinfomsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
51{
52	SET_STRUCT(struct nlmsghdr, nlh,
53		.nlmsg_len = msg_len,
54		.nlmsg_type = RTM_GETLINK,
55		.nlmsg_flags = NLM_F_DUMP
56	);
57
58	struct ifinfomsg *const msg = NLMSG_DATA(nlh);
59	SET_STRUCT(struct ifinfomsg, msg,
60		.ifi_family = AF_UNIX,
61		.ifi_type = ARPHRD_LOOPBACK,
62		.ifi_index = ifindex_lo(),
63		.ifi_flags = IFF_UP,
64	);
65
66	struct nlattr *const nla = NLMSG_ATTR(nlh, sizeof(*msg));
67	SET_STRUCT(struct nlattr, nla,
68		.nla_len = msg_len - NLMSG_SPACE(hdrlen),
69		.nla_type = IFLA_XDP
70	);
71}
72
73static void
74print_ifinfomsg(const unsigned int msg_len)
75{
76	printf("{len=%u, type=RTM_GETLINK, flags=NLM_F_DUMP"
77	       ", seq=0, pid=0}, {ifi_family=AF_UNIX"
78	       ", ifi_type=ARPHRD_LOOPBACK"
79	       ", ifi_index=" IFINDEX_LO_STR
80	       ", ifi_flags=IFF_UP, ifi_change=0}"
81	       ", {{nla_len=%u, nla_type=IFLA_XDP}",
82	       msg_len, msg_len - NLMSG_SPACE(hdrlen));
83}
84
85int
86main(void)
87{
88	skip_if_unavailable("/proc/self/fd/");
89
90	const int fd = create_nl_socket(NETLINK_ROUTE);
91	void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
92
93	static char pattern[4096];
94	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
95
96	const int32_t num = 0xabacdbcd;
97	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
98				  init_ifinfomsg, print_ifinfomsg,
99				  IFLA_XDP_FD, pattern, num,
100				  printf("%d", num));
101
102#ifdef XDP_FLAGS_UPDATE_IF_NOEXIST
103	const uint32_t flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
104	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
105				  init_ifinfomsg, print_ifinfomsg,
106				  IFLA_XDP_FLAGS, pattern, flags,
107				  printf("XDP_FLAGS_UPDATE_IF_NOEXIST"));
108#endif
109
110	puts("+++ exited with 0 +++");
111	return 0;
112}
113