xref: /aosp_15_r20/external/ltp/doc/old/C-Test-Network-API.asciidoc (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard WorkerLTP C Test Network API
2*49cdfc7eSAndroid Build Coastguard Worker======================
3*49cdfc7eSAndroid Build Coastguard Worker
4*49cdfc7eSAndroid Build Coastguard WorkerNOTE: See also
5*49cdfc7eSAndroid Build Coastguard Worker      https://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines[Test Writing Guidelines],
6*49cdfc7eSAndroid Build Coastguard Worker      https://github.com/linux-test-project/ltp/wiki/C-Test-Case-Tutorial[C Test Case Tutorial],
7*49cdfc7eSAndroid Build Coastguard Worker      https://github.com/linux-test-project/ltp/wiki/C-Test-API[C Test API],
8*49cdfc7eSAndroid Build Coastguard Worker      https://github.com/linux-test-project/ltp/wiki/Shell-Test-API[Shell Test API].
9*49cdfc7eSAndroid Build Coastguard Worker
10*49cdfc7eSAndroid Build Coastguard WorkerLTP library includes helper functions for configuring sockets and setting up
11*49cdfc7eSAndroid Build Coastguard Workernetwork devices.
12*49cdfc7eSAndroid Build Coastguard Worker
13*49cdfc7eSAndroid Build Coastguard Worker1 Configuring sockets
14*49cdfc7eSAndroid Build Coastguard Worker---------------------
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker1.1 Safe syscall variants
17*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker+#include "tst_safe_net.h"+
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard WorkerMost common standard syscalls and libc functions for configuring sockets have a
22*49cdfc7eSAndroid Build Coastguard Worker"safe" variant in LTP library which will call +tst_brk()+ if the underlying
23*49cdfc7eSAndroid Build Coastguard Workersystem function fails. See
24*49cdfc7eSAndroid Build Coastguard Workerhttps://github.com/linux-test-project/ltp/wiki/C-Test-API[C Test API]. The
25*49cdfc7eSAndroid Build Coastguard Workersafe function names are in uppercase with the +SAFE_+ prefix (e.g. the safe
26*49cdfc7eSAndroid Build Coastguard Workervariant of +socket()+ is called +SAFE_SOCKET()+). For most safe functions, the
27*49cdfc7eSAndroid Build Coastguard Workerparameters and return type are identical to the standard system function:
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker- +SAFE_SOCKET()+
30*49cdfc7eSAndroid Build Coastguard Worker- +SAFE_SOCKETPAIR()+
31*49cdfc7eSAndroid Build Coastguard Worker- +SAFE_GETSOCKOPT()+
32*49cdfc7eSAndroid Build Coastguard Worker- +SAFE_SETSOCKOPT()+
33*49cdfc7eSAndroid Build Coastguard Worker- +SAFE_BIND()+
34*49cdfc7eSAndroid Build Coastguard Worker- +SAFE_LISTEN()+
35*49cdfc7eSAndroid Build Coastguard Worker- +SAFE_ACCEPT()+
36*49cdfc7eSAndroid Build Coastguard Worker- +SAFE_CONNECT()+
37*49cdfc7eSAndroid Build Coastguard Worker- +SAFE_GETSOCKNAME()+
38*49cdfc7eSAndroid Build Coastguard Worker- +SAFE_GETHOSTNAME()+
39*49cdfc7eSAndroid Build Coastguard Worker- +SAFE_GETADDRINFO()+
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard WorkerA few safe functions have extra parameters for quick return value validation.
42*49cdfc7eSAndroid Build Coastguard WorkerThe ellipsis (+...+) represents the standard parameters of the underlying system
43*49cdfc7eSAndroid Build Coastguard Workerfunction:
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker* +SAFE_SEND(char strict, ...)+
46*49cdfc7eSAndroid Build Coastguard Worker* +SAFE_SENDTO(char strict, ...)+
47*49cdfc7eSAndroid Build Coastguard Worker** If +strict+ is non-zero, the return value must be equal to the data length
48*49cdfc7eSAndroid Build Coastguard Worker   argument. Otherwise the test will fail and exit.
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker* +SAFE_SENDMSG(size_t msg_len, ...)+
51*49cdfc7eSAndroid Build Coastguard Worker* +SAFE_RECV(size_t msg_len, ...)+
52*49cdfc7eSAndroid Build Coastguard Worker* +SAFE_RECVMSG(size_t msg_len, ...)+
53*49cdfc7eSAndroid Build Coastguard Worker** If +msg_len+ is non-zero, the return value must be equal to the +msg_len+
54*49cdfc7eSAndroid Build Coastguard Worker   argument. Otherwise the test will fail and exit.
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard WorkerThere are also some custom functions for simpler configuration and queries:
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker- +int SAFE_SETSOCKOPT_INT(int sockfd, int level, int optname, int value)+ –
59*49cdfc7eSAndroid Build Coastguard Worker  Simple setsockopt() variant for passing integers by value.
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker- +int TST_GETSOCKPORT(int sockfd)+ – Get port number (in host byte order) of a
62*49cdfc7eSAndroid Build Coastguard Worker  bound socket.
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker- +unsigned short TST_GET_UNUSED_PORT(int family, int type)+ – Get a random
65*49cdfc7eSAndroid Build Coastguard Worker  port number (in network byte order) which is currently closed for the given
66*49cdfc7eSAndroid Build Coastguard Worker  socket family and type. Note that another application may open the port while
67*49cdfc7eSAndroid Build Coastguard Worker  the test is still running. The test user is responsible for setting up test
68*49cdfc7eSAndroid Build Coastguard Worker  environment without such interference.
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker1.2 Address conversion functions
71*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker+#include "tst_net.h"+
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard WorkerLTP library also provides helper functions for quick initialization of socket
76*49cdfc7eSAndroid Build Coastguard Workeraddress structures:
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker- +void tst_get_in_addr(const char *ip_str, struct in_addr *ip)+ – Convert
79*49cdfc7eSAndroid Build Coastguard Worker  human-readable IPv4 address string +ip_str+ to binary representation in
80*49cdfc7eSAndroid Build Coastguard Worker  network byte order. The converted value will be stored in the second argument.
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker- +void tst_get_in6_addr(const char *ip_str, struct in6_addr *ip6)+ – Convert
83*49cdfc7eSAndroid Build Coastguard Worker  human-readable IPv6 address string +ip_str+ to binary representation in
84*49cdfc7eSAndroid Build Coastguard Worker  network byte order. The converted value will be stored in the second argument.
85*49cdfc7eSAndroid Build Coastguard Worker
86*49cdfc7eSAndroid Build Coastguard Worker- +socklen_t tst_get_connect_address(int sock, struct sockaddr_storage *addr)+ –
87*49cdfc7eSAndroid Build Coastguard Worker  Find the address which can be used to send data to bound socket +sock+ from
88*49cdfc7eSAndroid Build Coastguard Worker  another socket. The address will be stored in the second argument. This
89*49cdfc7eSAndroid Build Coastguard Worker  function automatically converts wildcard bind address to localhost. Returns
90*49cdfc7eSAndroid Build Coastguard Worker  size of the address in bytes.
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker- +void tst_init_sockaddr_inet(struct sockaddr_in *sa, const char *ip_str,
93*49cdfc7eSAndroid Build Coastguard Worker  uint16_t port)+ – Initialize socket address structure +sa+ using
94*49cdfc7eSAndroid Build Coastguard Worker  human-readable IPv4 address +ip_str+ and port number +port+ in host byte
95*49cdfc7eSAndroid Build Coastguard Worker  order.
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Worker- +void tst_init_sockaddr_inet_bin(struct sockaddr_in *sa, uint32_t ip_val,
98*49cdfc7eSAndroid Build Coastguard Worker  uint16_t port)+ – Initialize socket address structure +sa+ using binary IPv4
99*49cdfc7eSAndroid Build Coastguard Worker  address +ip_val+ and port number +port+, both in host byte order.
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker- +void tst_init_sockaddr_inet6(struct sockaddr_in6 *sa, const char *ip_str,
102*49cdfc7eSAndroid Build Coastguard Worker  uint16_t port)+ – Initialize socket address structure +sa+ using
103*49cdfc7eSAndroid Build Coastguard Worker  human-readable IPv6 address +ip_str+ and port number +port+ in host byte
104*49cdfc7eSAndroid Build Coastguard Worker  order.
105*49cdfc7eSAndroid Build Coastguard Worker
106*49cdfc7eSAndroid Build Coastguard Worker- +void tst_init_sockaddr_inet6_bin(struct sockaddr_in6 *sa, const struct
107*49cdfc7eSAndroid Build Coastguard Worker  in6_addr *ip_val, uint16_t port)+ – Initialize socket address structure +sa+
108*49cdfc7eSAndroid Build Coastguard Worker  using binary IPv6 address +ip_val+ and port number +port+, both in host byte
109*49cdfc7eSAndroid Build Coastguard Worker  order.
110*49cdfc7eSAndroid Build Coastguard Worker
111*49cdfc7eSAndroid Build Coastguard WorkerExample Usage
112*49cdfc7eSAndroid Build Coastguard Worker+++++++++++++
113*49cdfc7eSAndroid Build Coastguard Worker[source,c]
114*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
115*49cdfc7eSAndroid Build Coastguard Worker
116*49cdfc7eSAndroid Build Coastguard Worker#include <sys/socket.h>
117*49cdfc7eSAndroid Build Coastguard Worker#include <netinet/in.h>
118*49cdfc7eSAndroid Build Coastguard Worker
119*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h"
120*49cdfc7eSAndroid Build Coastguard Worker#include "tst_safe_net.h"
121*49cdfc7eSAndroid Build Coastguard Worker#include "tst_net.h"
122*49cdfc7eSAndroid Build Coastguard Worker
123*49cdfc7eSAndroid Build Coastguard Workerstatic int sockfd = -1;
124*49cdfc7eSAndroid Build Coastguard Worker
125*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void)
126*49cdfc7eSAndroid Build Coastguard Worker{
127*49cdfc7eSAndroid Build Coastguard Worker	struct sockaddr_in addr;
128*49cdfc7eSAndroid Build Coastguard Worker
129*49cdfc7eSAndroid Build Coastguard Worker	tst_init_sockaddr_inet_bin(&addr, INADDR_ANY, 0);
130*49cdfc7eSAndroid Build Coastguard Worker	sockfd = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
131*49cdfc7eSAndroid Build Coastguard Worker	SAFE_SETSOCKOPT_INT(sockfd, SOL_SOCKET, SO_SNDBUF, 4096);
132*49cdfc7eSAndroid Build Coastguard Worker	SAFE_BIND(sockfd, (struct sockaddr *)&addr, sizeof(addr));
133*49cdfc7eSAndroid Build Coastguard Worker	SAFE_LISTEN(sockfd, 8);
134*49cdfc7eSAndroid Build Coastguard Worker}
135*49cdfc7eSAndroid Build Coastguard Worker
136*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
137*49cdfc7eSAndroid Build Coastguard Worker
138*49cdfc7eSAndroid Build Coastguard Worker2 Configuring network devices
139*49cdfc7eSAndroid Build Coastguard Worker-----------------------------
140*49cdfc7eSAndroid Build Coastguard Worker
141*49cdfc7eSAndroid Build Coastguard Worker+#include "tst_netdevice.h"+
142*49cdfc7eSAndroid Build Coastguard Worker
143*49cdfc7eSAndroid Build Coastguard WorkerWhen opening a localhost socket isn't enough and the test needs special device
144*49cdfc7eSAndroid Build Coastguard Workeror routing configuration, the netdevice library can create the required network
145*49cdfc7eSAndroid Build Coastguard Workersetup without calling external programs. Internally, the netdevice functions
146*49cdfc7eSAndroid Build Coastguard Workeruse a netlink socket to communicate with the kernel.
147*49cdfc7eSAndroid Build Coastguard Worker
148*49cdfc7eSAndroid Build Coastguard WorkerAll of these functions will call +tst_brk()+ on failure, unless stated
149*49cdfc7eSAndroid Build Coastguard Workerotherwise. Error values described below are returned only during test cleanup
150*49cdfc7eSAndroid Build Coastguard Workerstage.
151*49cdfc7eSAndroid Build Coastguard Worker
152*49cdfc7eSAndroid Build Coastguard Worker2.1 Network device management
153*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154*49cdfc7eSAndroid Build Coastguard Worker
155*49cdfc7eSAndroid Build Coastguard Worker- +int NETDEV_INDEX_BY_NAME(const char *ifname)+ – Returns the device index for
156*49cdfc7eSAndroid Build Coastguard Worker  the given device name, or -1 on error.
157*49cdfc7eSAndroid Build Coastguard Worker
158*49cdfc7eSAndroid Build Coastguard Worker- +int NETDEV_SET_STATE(const char *ifname, int up)+ – Enable or disable a
159*49cdfc7eSAndroid Build Coastguard Worker  network device +ifname+. Returns 0 on success, -1 on error.
160*49cdfc7eSAndroid Build Coastguard Worker
161*49cdfc7eSAndroid Build Coastguard Worker- +int CREATE_VETH_PAIR(const char *ifname1, const char *ifname2)+ – Creates a
162*49cdfc7eSAndroid Build Coastguard Worker  connected pair of virtual network devices with given device names. Returns 1
163*49cdfc7eSAndroid Build Coastguard Worker  on success, 0 on error. Add +"CONFIG_VETH"+ to +test.needs_kconfigs+ if your
164*49cdfc7eSAndroid Build Coastguard Worker  test calls this function.
165*49cdfc7eSAndroid Build Coastguard Worker
166*49cdfc7eSAndroid Build Coastguard Worker- +int NETDEV_ADD_DEVICE(const char *ifname, const char *devtype)+ - Creates
167*49cdfc7eSAndroid Build Coastguard Worker  a new network device named +ifname+ of specified device type. Returns 1 on
168*49cdfc7eSAndroid Build Coastguard Worker  success, 0 on error.
169*49cdfc7eSAndroid Build Coastguard Worker
170*49cdfc7eSAndroid Build Coastguard Worker- +int NETDEV_REMOVE_DEVICE(const char *ifname)+ – Removes network device
171*49cdfc7eSAndroid Build Coastguard Worker  +ifname+. Returns 1 on success, 0 on error.
172*49cdfc7eSAndroid Build Coastguard Worker
173*49cdfc7eSAndroid Build Coastguard Worker2.2 Network address management
174*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
175*49cdfc7eSAndroid Build Coastguard Worker
176*49cdfc7eSAndroid Build Coastguard Worker- +int NETDEV_ADD_ADDRESS(const char \*ifname, unsigned int family, const void
177*49cdfc7eSAndroid Build Coastguard Worker  *address, unsigned int prefix, size_t addrlen, unsigned int flags)+ – Adds
178*49cdfc7eSAndroid Build Coastguard Worker  new address to network device +ifname+. This is a low-level function which
179*49cdfc7eSAndroid Build Coastguard Worker  allows setting any type of address. You must specify the protocol +family+,
180*49cdfc7eSAndroid Build Coastguard Worker  address length in bytes (+addrlen+) and network prefix length (+prefix+). The
181*49cdfc7eSAndroid Build Coastguard Worker  +address+ itself must be in binary representation in network byte order. You
182*49cdfc7eSAndroid Build Coastguard Worker  can also pass rtnetlink flags from the +IFA_F_*+ group. Returns 1 on success,
183*49cdfc7eSAndroid Build Coastguard Worker  0 on error.
184*49cdfc7eSAndroid Build Coastguard Worker
185*49cdfc7eSAndroid Build Coastguard Worker- +int NETDEV_ADD_ADDRESS_INET(const char *ifname, in_addr_t address, unsigned
186*49cdfc7eSAndroid Build Coastguard Worker  int prefix, unsigned int flags)+ – Adds new IPv4 address to network device
187*49cdfc7eSAndroid Build Coastguard Worker  +ifname+. Parameters have the same meaning as in +NETDEV_ADD_ADDRESS()+.
188*49cdfc7eSAndroid Build Coastguard Worker  Returns 1 on success, 0 on error.
189*49cdfc7eSAndroid Build Coastguard Worker
190*49cdfc7eSAndroid Build Coastguard Worker- +int NETDEV_REMOVE_ADDRESS(const char *ifname, unsigned int family, const
191*49cdfc7eSAndroid Build Coastguard Worker  void *address, size_t addrlen)+ – Removes the specified address from network
192*49cdfc7eSAndroid Build Coastguard Worker  device +ifname+. Parameters have the same meaning as in
193*49cdfc7eSAndroid Build Coastguard Worker  +NETDEV_ADD_ADDRESS()+. Returns 1 on success, 0 on error.
194*49cdfc7eSAndroid Build Coastguard Worker
195*49cdfc7eSAndroid Build Coastguard Worker- +int NETDEV_REMOVE_ADDRESS_INET(const char *ifname, in_addr_t address)+ –
196*49cdfc7eSAndroid Build Coastguard Worker  Removes specified IPv4 +address+ (in network byte order) from network device
197*49cdfc7eSAndroid Build Coastguard Worker  +ifname+. Returns 1 on success, 0 on error.
198*49cdfc7eSAndroid Build Coastguard Worker
199*49cdfc7eSAndroid Build Coastguard Worker2.3 Network namespace device assignment
200*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
201*49cdfc7eSAndroid Build Coastguard Worker
202*49cdfc7eSAndroid Build Coastguard WorkerWARNING: Moving a network device to another namespace will erase previous
203*49cdfc7eSAndroid Build Coastguard Worker         configuration. Move the device to the correct namespace first, then
204*49cdfc7eSAndroid Build Coastguard Worker         configure it.
205*49cdfc7eSAndroid Build Coastguard Worker
206*49cdfc7eSAndroid Build Coastguard Worker- +int NETDEV_CHANGE_NS_FD(const char *ifname, int nsfd)+ – Moves network
207*49cdfc7eSAndroid Build Coastguard Worker  device +ifname+ to network namespace designated by open file descriptor
208*49cdfc7eSAndroid Build Coastguard Worker  +nsfd+. Returns 1 on success, 0 on error.
209*49cdfc7eSAndroid Build Coastguard Worker
210*49cdfc7eSAndroid Build Coastguard Worker- +int NETDEV_CHANGE_NS_PID(const char *ifname, pid_t nspid)+ – Moves network
211*49cdfc7eSAndroid Build Coastguard Worker  device +ifname+ to the network namespace currently used by process +nspid+.
212*49cdfc7eSAndroid Build Coastguard Worker  Returns 1 on success, 0 on error.
213*49cdfc7eSAndroid Build Coastguard Worker
214*49cdfc7eSAndroid Build Coastguard Worker2.4 Routing table management
215*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~
216*49cdfc7eSAndroid Build Coastguard Worker
217*49cdfc7eSAndroid Build Coastguard Worker- +int NETDEV_ADD_ROUTE(const char *ifname, unsigned int family, const void
218*49cdfc7eSAndroid Build Coastguard Worker  *srcaddr, unsigned int srcprefix, size_t srclen, const void *dstaddr,
219*49cdfc7eSAndroid Build Coastguard Worker  unsigned int dstprefix, size_t dstlen, const void *gateway, size_t
220*49cdfc7eSAndroid Build Coastguard Worker  gatewaylen)+ – Adds new route to the main routing table. This is a low-level
221*49cdfc7eSAndroid Build Coastguard Worker  function which allows creating routes for any protocol. You must specify the
222*49cdfc7eSAndroid Build Coastguard Worker  protocol +family+ and either network device name +ifname+ or +gateway+
223*49cdfc7eSAndroid Build Coastguard Worker  address. Both packet source address +srcaddr+ and destination address
224*49cdfc7eSAndroid Build Coastguard Worker  +dstaddr+ are optional. You must also specify the corresponding length
225*49cdfc7eSAndroid Build Coastguard Worker  and prefix argument for any address which is not +NULL+. All addresses must
226*49cdfc7eSAndroid Build Coastguard Worker  be in binary representation in network byte order. Returns 1 on success,
227*49cdfc7eSAndroid Build Coastguard Worker  0 on error.
228*49cdfc7eSAndroid Build Coastguard Worker
229*49cdfc7eSAndroid Build Coastguard Worker- +int NETDEV_ADD_ROUTE_INET(const char *ifname, in_addr_t srcaddr, unsigned
230*49cdfc7eSAndroid Build Coastguard Worker  int srcprefix, in_addr_t dstaddr, unsigned int dstprefix, in_addr_t
231*49cdfc7eSAndroid Build Coastguard Worker  gateway)+ – Adds new IPv4 route to the main routing table. Parameters have
232*49cdfc7eSAndroid Build Coastguard Worker  the same meaning as in +NETDEV_ADD_ROUTE()+. If you do not want to set
233*49cdfc7eSAndroid Build Coastguard Worker  explicit +gateway+ address, set it to 0. If the routing rule should ignore
234*49cdfc7eSAndroid Build Coastguard Worker  the source or destination address, set the corresponding prefix argument
235*49cdfc7eSAndroid Build Coastguard Worker  to 0. Returns 1 on success, 0 on error.
236*49cdfc7eSAndroid Build Coastguard Worker
237*49cdfc7eSAndroid Build Coastguard Worker- +int NETDEV_REMOVE_ROUTE(const char *ifname, unsigned int family, const void
238*49cdfc7eSAndroid Build Coastguard Worker  *srcaddr, unsigned int srcprefix, size_t srclen, const void *dstaddr,
239*49cdfc7eSAndroid Build Coastguard Worker  unsigned int dstprefix, size_t dstlen, const void *gateway, size_t
240*49cdfc7eSAndroid Build Coastguard Worker  gatewaylen)+ – Removes a route from the main routing table. Parameters have
241*49cdfc7eSAndroid Build Coastguard Worker  the same meaning as in +NETDEV_ADD_ROUTE()+. Returns 1 on success, 0 on
242*49cdfc7eSAndroid Build Coastguard Worker  error.
243*49cdfc7eSAndroid Build Coastguard Worker
244*49cdfc7eSAndroid Build Coastguard Worker- +int NETDEV_REMOVE_ROUTE_INET(const char *ifname, in_addr_t srcaddr,
245*49cdfc7eSAndroid Build Coastguard Worker  unsigned int srcprefix, in_addr_t dstaddr, unsigned int dstprefix, in_addr_t
246*49cdfc7eSAndroid Build Coastguard Worker  gateway)+ – Removes IPv4 route from the main routing table. Parameters have
247*49cdfc7eSAndroid Build Coastguard Worker  the same meaning as in +NETDEV_ADD_ROUTE_INET()+. Returns 1 on success,
248*49cdfc7eSAndroid Build Coastguard Worker  0 on error.
249*49cdfc7eSAndroid Build Coastguard Worker
250*49cdfc7eSAndroid Build Coastguard WorkerExample Usage
251*49cdfc7eSAndroid Build Coastguard Worker+++++++++++++
252*49cdfc7eSAndroid Build Coastguard Worker[source,c]
253*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
254*49cdfc7eSAndroid Build Coastguard Worker#include <arpa/inet.h>
255*49cdfc7eSAndroid Build Coastguard Worker#include <linux/if_addr.h>
256*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h"
257*49cdfc7eSAndroid Build Coastguard Worker#include "tst_netdevice.h"
258*49cdfc7eSAndroid Build Coastguard Worker
259*49cdfc7eSAndroid Build Coastguard Worker...
260*49cdfc7eSAndroid Build Coastguard Worker
261*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void)
262*49cdfc7eSAndroid Build Coastguard Worker{
263*49cdfc7eSAndroid Build Coastguard Worker	CREATE_VETH_PAIR("ltp_veth1", "ltp_veth2");
264*49cdfc7eSAndroid Build Coastguard Worker	NETDEV_ADD_ADDRESS_INET("ltp_veth2", htonl(DSTADDR), NETMASK,
265*49cdfc7eSAndroid Build Coastguard Worker		IFA_F_NOPREFIXROUTE);
266*49cdfc7eSAndroid Build Coastguard Worker	NETDEV_SET_STATE("ltp_veth2", 1);
267*49cdfc7eSAndroid Build Coastguard Worker	NETDEV_ADD_ROUTE_INET("ltp_veth2", 0, 0, htonl(SRCNET), NETMASK, 0);
268*49cdfc7eSAndroid Build Coastguard Worker
269*49cdfc7eSAndroid Build Coastguard Worker	NETDEV_ADD_ADDRESS_INET("ltp_veth1", htonl(SRCADDR), NETMASK,
270*49cdfc7eSAndroid Build Coastguard Worker		IFA_F_NOPREFIXROUTE);
271*49cdfc7eSAndroid Build Coastguard Worker	NETDEV_SET_STATE("ltp_veth1", 1);
272*49cdfc7eSAndroid Build Coastguard Worker	NETDEV_ADD_ROUTE_INET("ltp_veth1", 0, 0, htonl(DSTNET), NETMASK, 0);
273*49cdfc7eSAndroid Build Coastguard Worker	...
274*49cdfc7eSAndroid Build Coastguard Worker}
275*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
276*49cdfc7eSAndroid Build Coastguard Worker
277*49cdfc7eSAndroid Build Coastguard Worker3 Netlink API
278*49cdfc7eSAndroid Build Coastguard Worker-------------
279*49cdfc7eSAndroid Build Coastguard Worker
280*49cdfc7eSAndroid Build Coastguard Worker+#include "tst_netlink.h"+
281*49cdfc7eSAndroid Build Coastguard Worker
282*49cdfc7eSAndroid Build Coastguard WorkerThe netlink library provides helper functions for constructing and sending
283*49cdfc7eSAndroid Build Coastguard Workerarbitrary messages and parsing kernel responses.
284*49cdfc7eSAndroid Build Coastguard Worker
285*49cdfc7eSAndroid Build Coastguard WorkerAll of the functions below will call +tst_brk()+ on failure, unless stated
286*49cdfc7eSAndroid Build Coastguard Workerotherwise. Error values described below are returned only during test cleanup
287*49cdfc7eSAndroid Build Coastguard Workerstage.
288*49cdfc7eSAndroid Build Coastguard Worker
289*49cdfc7eSAndroid Build Coastguard Worker3.1 Data structures
290*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~
291*49cdfc7eSAndroid Build Coastguard Worker
292*49cdfc7eSAndroid Build Coastguard Worker[source,c]
293*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
294*49cdfc7eSAndroid Build Coastguard Workerstruct tst_netlink_context;
295*49cdfc7eSAndroid Build Coastguard Worker
296*49cdfc7eSAndroid Build Coastguard Workerstruct tst_netlink_attr_list {
297*49cdfc7eSAndroid Build Coastguard Worker	unsigned short type;
298*49cdfc7eSAndroid Build Coastguard Worker	const void *data;
299*49cdfc7eSAndroid Build Coastguard Worker	ssize_t len;
300*49cdfc7eSAndroid Build Coastguard Worker	const struct tst_netlink_attr_list *sublist;
301*49cdfc7eSAndroid Build Coastguard Worker};
302*49cdfc7eSAndroid Build Coastguard Worker
303*49cdfc7eSAndroid Build Coastguard Workerstruct tst_netlink_message {
304*49cdfc7eSAndroid Build Coastguard Worker	struct nlmsghdr *header;
305*49cdfc7eSAndroid Build Coastguard Worker	struct nlmsgerr *err;
306*49cdfc7eSAndroid Build Coastguard Worker	void *payload;
307*49cdfc7eSAndroid Build Coastguard Worker	size_t payload_size;
308*49cdfc7eSAndroid Build Coastguard Worker};
309*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
310*49cdfc7eSAndroid Build Coastguard Worker
311*49cdfc7eSAndroid Build Coastguard Worker+struct tst_netlink_context+ is an opaque netlink socket with buffer for
312*49cdfc7eSAndroid Build Coastguard Workerconstructing and sending arbitrary messages using the functions described
313*49cdfc7eSAndroid Build Coastguard Workerbelow. Create a new context using +NETLINK_CREATE_CONTEXT()+, then free it
314*49cdfc7eSAndroid Build Coastguard Workerusing +NETLINK_DESTROY_CONTEXT()+ when you're done with it.
315*49cdfc7eSAndroid Build Coastguard Worker
316*49cdfc7eSAndroid Build Coastguard Worker+struct tst_netlink_attr_list+ is a helper structure for defining complex
317*49cdfc7eSAndroid Build Coastguard Workerrtnetlink message attribute payloads, including nested attribute lists. Every
318*49cdfc7eSAndroid Build Coastguard Workerlist and sublist defined using this structure is terminated by item with
319*49cdfc7eSAndroid Build Coastguard Workernegative +len+.
320*49cdfc7eSAndroid Build Coastguard Worker
321*49cdfc7eSAndroid Build Coastguard Worker- +type+ is the attribute type that will be stored in +struct nlattr.nla_type+
322*49cdfc7eSAndroid Build Coastguard Worker  or +struct rtattr.rta_type+.
323*49cdfc7eSAndroid Build Coastguard Worker
324*49cdfc7eSAndroid Build Coastguard Worker- +data+ contains arbitrary attribute payload.
325*49cdfc7eSAndroid Build Coastguard Worker
326*49cdfc7eSAndroid Build Coastguard Worker- +len+ contains length of the +data+ attribute in bytes. If +data+ is +NULL+,
327*49cdfc7eSAndroid Build Coastguard Worker  set +len+ to 0. The last item in a list or sublist must have negative length.
328*49cdfc7eSAndroid Build Coastguard Worker
329*49cdfc7eSAndroid Build Coastguard Worker- +sublist+ contains a nested attribute list which will be appended after
330*49cdfc7eSAndroid Build Coastguard Worker  +data+ as part of the attribute payload. +struct nlattr.nla_len+ or
331*49cdfc7eSAndroid Build Coastguard Worker  +struct rtattr.rta_len+ will be calculated automatically with proper
332*49cdfc7eSAndroid Build Coastguard Worker  alignment, do _not_ add the sublist size to the +len+ field. If you do not
333*49cdfc7eSAndroid Build Coastguard Worker  want to add nested attributes, set +sublist+ to +NULL+.
334*49cdfc7eSAndroid Build Coastguard Worker
335*49cdfc7eSAndroid Build Coastguard Worker+struct tst_netlink_message+ is a structure holding partially parsed netlink
336*49cdfc7eSAndroid Build Coastguard Workermessages received from the kernel. +NETLINK_RECV()+ returns an array of these
337*49cdfc7eSAndroid Build Coastguard Workerstructures with the last item having +NULL+ in the +header+ field. Call
338*49cdfc7eSAndroid Build Coastguard Worker+NETLINK_FREE_MESSAGE()+ to free a message list returned by +NETLINK_RECV()+.
339*49cdfc7eSAndroid Build Coastguard Worker
340*49cdfc7eSAndroid Build Coastguard Worker- +header+ is the netlink header structure of the message. +NULL+ in the header
341*49cdfc7eSAndroid Build Coastguard Worker  field terminates a list of messages.
342*49cdfc7eSAndroid Build Coastguard Worker
343*49cdfc7eSAndroid Build Coastguard Worker- +err+ points to the payload of +NLMSG_ERROR+ messages. It is set to +NULL+
344*49cdfc7eSAndroid Build Coastguard Worker  for all other message types.
345*49cdfc7eSAndroid Build Coastguard Worker
346*49cdfc7eSAndroid Build Coastguard Worker- +payload+ is a pointer to message data.
347*49cdfc7eSAndroid Build Coastguard Worker
348*49cdfc7eSAndroid Build Coastguard Worker- +payload_size+ is the length of +payload+ data in bytes.
349*49cdfc7eSAndroid Build Coastguard Worker
350*49cdfc7eSAndroid Build Coastguard Worker3.2 Sending and receiving messages
351*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
352*49cdfc7eSAndroid Build Coastguard Worker
353*49cdfc7eSAndroid Build Coastguard Worker- +struct tst_netlink_context *NETLINK_CREATE_CONTEXT(int protocol)+ – Creates
354*49cdfc7eSAndroid Build Coastguard Worker  a new netlink communication context with given netlink protocol for use
355*49cdfc7eSAndroid Build Coastguard Worker  with the functions described below. Returns +NULL+ on error.
356*49cdfc7eSAndroid Build Coastguard Worker
357*49cdfc7eSAndroid Build Coastguard Worker- +void NETLINK_FREE_MESSAGE(struct tst_netlink_message *msg)+ – Frees
358*49cdfc7eSAndroid Build Coastguard Worker  an array of messages returned by +NETLINK_RECV()+.
359*49cdfc7eSAndroid Build Coastguard Worker
360*49cdfc7eSAndroid Build Coastguard Worker- +void NETLINK_DESTROY_CONTEXT(struct tst_netlink_context *ctx)+ – Closes a
361*49cdfc7eSAndroid Build Coastguard Worker  communication context created by +NETLINK_CREATE_CONTEXT()+.
362*49cdfc7eSAndroid Build Coastguard Worker
363*49cdfc7eSAndroid Build Coastguard Worker- +int NETLINK_SEND(struct tst_netlink_context *ctx)+ – Sends all messages
364*49cdfc7eSAndroid Build Coastguard Worker  waiting in +ctx+ buffer to the kernel. If there are multiple messages
365*49cdfc7eSAndroid Build Coastguard Worker  to send, a new +NLMSG_DONE+ message will be added automatically. Returns
366*49cdfc7eSAndroid Build Coastguard Worker  the number of bytes sent on success. Return 0 or negative value on error.
367*49cdfc7eSAndroid Build Coastguard Worker
368*49cdfc7eSAndroid Build Coastguard Worker- +int NETLINK_SEND_VALIDATE(struct tst_netlink_context *ctx)+ – Sends all
369*49cdfc7eSAndroid Build Coastguard Worker  messages just like +NETLINK_SEND()+, then receives the response from
370*49cdfc7eSAndroid Build Coastguard Worker  the kernel and validates results of requests sent with the +NLM_F_ACK+ flag.
371*49cdfc7eSAndroid Build Coastguard Worker  This function calls +tst_brk()+ as usual if communication fails but it will
372*49cdfc7eSAndroid Build Coastguard Worker  return error status without terminating the test if one of the received
373*49cdfc7eSAndroid Build Coastguard Worker  messages contains error code. See +NETLINK_CHECK_ACKS()+ below for
374*49cdfc7eSAndroid Build Coastguard Worker  explanation of the return value.
375*49cdfc7eSAndroid Build Coastguard Worker
376*49cdfc7eSAndroid Build Coastguard Worker- +int NETLINK_WAIT(struct tst_netlink_context *ctx)+ – Waits until data becomes
377*49cdfc7eSAndroid Build Coastguard Worker  available to read from the netlink socket (timeout: 1 second). Returns 1
378*49cdfc7eSAndroid Build Coastguard Worker  if there is data to read, 0 on timeout or -1 on error.
379*49cdfc7eSAndroid Build Coastguard Worker
380*49cdfc7eSAndroid Build Coastguard Worker- +struct tst_netlink_message *NETLINK_RECV(struct tst_netlink_context *ctx)+ –
381*49cdfc7eSAndroid Build Coastguard Worker  Receives netlink messages from the kernel. The messages are received
382*49cdfc7eSAndroid Build Coastguard Worker  in non-blocking mode so calling +NETLINK_WAIT()+ first is recommended.
383*49cdfc7eSAndroid Build Coastguard Worker  Returns an array of partially parsed messages terminated by an item with
384*49cdfc7eSAndroid Build Coastguard Worker  +NULL+ in the +header+ field. On error or when there are no messages
385*49cdfc7eSAndroid Build Coastguard Worker  to receive, returns +NULL+. Call +NETLINK_FREE_MESSAGE()+ to free
386*49cdfc7eSAndroid Build Coastguard Worker  the returned data.
387*49cdfc7eSAndroid Build Coastguard Worker
388*49cdfc7eSAndroid Build Coastguard Worker- +int NETLINK_CHECK_ACKS(struct tst_netlink_context *ctx,
389*49cdfc7eSAndroid Build Coastguard Worker  struct tst_netlink_message *response)+ – Validate results of requests sent
390*49cdfc7eSAndroid Build Coastguard Worker  with the +NLM_F_ACK+ flag. Do not call +NETLINK_ADD_MESSAGE()+ between
391*49cdfc7eSAndroid Build Coastguard Worker  +NETLINK_SEND()+ and +NETLINK_CHECK_ACKS()+ because it will reset the state
392*49cdfc7eSAndroid Build Coastguard Worker  of +ctx+ and prevent result validation. Returns 1 if all messages sent
393*49cdfc7eSAndroid Build Coastguard Worker  with the +NLM_F_ACK+ flag have a corresponding message in +response+ and
394*49cdfc7eSAndroid Build Coastguard Worker  the error code is 0. If any of the expected response messages is missing,
395*49cdfc7eSAndroid Build Coastguard Worker  this function will call +tst_brk()+ (or return 0 during test cleanup phase).
396*49cdfc7eSAndroid Build Coastguard Worker  If any of the response messages has non-zero error code, this function will
397*49cdfc7eSAndroid Build Coastguard Worker  return 0 and store the first non-zero error code in global variable
398*49cdfc7eSAndroid Build Coastguard Worker  +tst_netlink_errno+ (sign-flipped just like regular libc +errno+).
399*49cdfc7eSAndroid Build Coastguard Worker
400*49cdfc7eSAndroid Build Coastguard Worker3.3 Creating messages
401*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~
402*49cdfc7eSAndroid Build Coastguard Worker
403*49cdfc7eSAndroid Build Coastguard Worker- +int NETLINK_ADD_MESSAGE(struct tst_netlink_context *ctx, const struct
404*49cdfc7eSAndroid Build Coastguard Worker  nlmsghdr *header, const void *payload, size_t payload_size)+ – Adds new
405*49cdfc7eSAndroid Build Coastguard Worker  netlink message to +ctx+ buffer. You need to provide message +header+ and
406*49cdfc7eSAndroid Build Coastguard Worker  optional +payload+. +payload_size+ is the size of +payload+ data in bytes.
407*49cdfc7eSAndroid Build Coastguard Worker  If you don't want to add any payload data, set +payload+ to +NULL+ and
408*49cdfc7eSAndroid Build Coastguard Worker  +payload_size+ to 0. This function will automatically fill the +nlmsg_len+,
409*49cdfc7eSAndroid Build Coastguard Worker  +nlmsg_seq+ and +nlmsg_pid+ fields of the new message header. You don't need
410*49cdfc7eSAndroid Build Coastguard Worker  to set those. It'll also automatically add +NLM_F_MULTI+ flag when needed.
411*49cdfc7eSAndroid Build Coastguard Worker  Returns 1 on success, 0 on error. Note that the first call of
412*49cdfc7eSAndroid Build Coastguard Worker  +NETLINK_ADD_MESSAGE()+ after +NETLINK_SEND()+ will reset the state of +ctx+
413*49cdfc7eSAndroid Build Coastguard Worker  and +NETLINK_CHECK_ACKS()+ will not work correctly until the next
414*49cdfc7eSAndroid Build Coastguard Worker  +NETLINK_SEND()+.
415*49cdfc7eSAndroid Build Coastguard Worker
416*49cdfc7eSAndroid Build Coastguard Worker- +int NETLINK_ADD_ATTR(struct tst_netlink_context *ctx, unsigned short type,
417*49cdfc7eSAndroid Build Coastguard Worker  const void *data, unsigned short len)+ – Adds new +struct nlattr+ attribute
418*49cdfc7eSAndroid Build Coastguard Worker  to the last message in +ctx+ buffer. See +NETLINK_ADD_MESSAGE()+. You need
419*49cdfc7eSAndroid Build Coastguard Worker  to provide attribute +type+ which will be stored in +struct nlattr.nla_type+,
420*49cdfc7eSAndroid Build Coastguard Worker  optional payload +data+ and payload size +len+ in bytes. If you don't want
421*49cdfc7eSAndroid Build Coastguard Worker  to add any payload, set +data+ to +NULL+ and +len+ to 0. Returns 1 on
422*49cdfc7eSAndroid Build Coastguard Worker  success, 0 on error.
423*49cdfc7eSAndroid Build Coastguard Worker
424*49cdfc7eSAndroid Build Coastguard Worker- +int NETLINK_ADD_ATTR_STRING(struct tst_netlink_context *ctx, unsigned short
425*49cdfc7eSAndroid Build Coastguard Worker  type, const char *data)+ – Adds new +struct nlattr+ string attribute to the
426*49cdfc7eSAndroid Build Coastguard Worker  last message in +ctx+ buffer. Parameters and return value are the same as
427*49cdfc7eSAndroid Build Coastguard Worker  for +NETLINK_ADD_ATTR()+, except the payload length is calculated using
428*49cdfc7eSAndroid Build Coastguard Worker  +strlen()+.
429*49cdfc7eSAndroid Build Coastguard Worker
430*49cdfc7eSAndroid Build Coastguard Worker- +int NETLINK_ADD_ATTR_LIST(struct tst_netlink_context *ctx, const struct
431*49cdfc7eSAndroid Build Coastguard Worker  tst_netlink_attr_list *list)+ – Adds a list of +struct nlattr+ attributes
432*49cdfc7eSAndroid Build Coastguard Worker  to the last message in +ctx+ buffer. See description of
433*49cdfc7eSAndroid Build Coastguard Worker  +struct tst_netlink_attr_list+ and +NETLINK_ADD_MESSAGE()+ above. Returns
434*49cdfc7eSAndroid Build Coastguard Worker  the number of added attributes on success (nested attributes are not
435*49cdfc7eSAndroid Build Coastguard Worker  counted), -1 on error.
436*49cdfc7eSAndroid Build Coastguard Worker
437*49cdfc7eSAndroid Build Coastguard Worker- +int RTNL_ADD_ATTR(struct tst_netlink_context *ctx, unsigned short type,
438*49cdfc7eSAndroid Build Coastguard Worker  const void *data, unsigned short len)+ – Adds new +struct rtattr+ attribute
439*49cdfc7eSAndroid Build Coastguard Worker  to the last message in +ctx+ buffer. See +NETLINK_ADD_MESSAGE()+. You need
440*49cdfc7eSAndroid Build Coastguard Worker  to provide attribute +type+ which will be stored in +struct rtattr.rta_type+,
441*49cdfc7eSAndroid Build Coastguard Worker  optional payload +data+ and payload size +len+ in bytes. If you don't want
442*49cdfc7eSAndroid Build Coastguard Worker  to add any payload, set +data+ to +NULL+ and +len+ to 0. Returns 1 on
443*49cdfc7eSAndroid Build Coastguard Worker  success, 0 on error.
444*49cdfc7eSAndroid Build Coastguard Worker
445*49cdfc7eSAndroid Build Coastguard Worker- +int RTNL_ADD_ATTR_STRING(struct tst_netlink_context *ctx, unsigned short
446*49cdfc7eSAndroid Build Coastguard Worker  type, const char *data)+ – Adds new +struct rtattr+ string attribute to the
447*49cdfc7eSAndroid Build Coastguard Worker  last message in +ctx+ buffer. Parameters and return value are the same as
448*49cdfc7eSAndroid Build Coastguard Worker  for +RTNL_ADD_ATTR()+, except the payload length is calculated using
449*49cdfc7eSAndroid Build Coastguard Worker  +strlen()+.
450*49cdfc7eSAndroid Build Coastguard Worker
451*49cdfc7eSAndroid Build Coastguard Worker- +int RTNL_ADD_ATTR_LIST(struct tst_netlink_context *ctx, const struct
452*49cdfc7eSAndroid Build Coastguard Worker  tst_netlink_attr_list *list)+ – Adds a list of +struct rtattr+ attributes
453*49cdfc7eSAndroid Build Coastguard Worker  to the last message in +ctx+ buffer. See description of
454*49cdfc7eSAndroid Build Coastguard Worker  +struct tst_netlink_attr_list+ and +NETLINK_ADD_MESSAGE()+ above. Returns
455*49cdfc7eSAndroid Build Coastguard Worker  the number of added attributes on success (nested attributes are not
456*49cdfc7eSAndroid Build Coastguard Worker  counted), -1 on error.
457*49cdfc7eSAndroid Build Coastguard Worker
458*49cdfc7eSAndroid Build Coastguard WorkerExample Usage
459*49cdfc7eSAndroid Build Coastguard Worker+++++++++++++
460*49cdfc7eSAndroid Build Coastguard Worker[source,c]
461*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
462*49cdfc7eSAndroid Build Coastguard Worker#include <asm/types.h>
463*49cdfc7eSAndroid Build Coastguard Worker#include <linux/netlink.h>
464*49cdfc7eSAndroid Build Coastguard Worker#include <linux/rtnetlink.h>
465*49cdfc7eSAndroid Build Coastguard Worker#include <sys/socket.h>
466*49cdfc7eSAndroid Build Coastguard Worker#include <netinet/in.h>
467*49cdfc7eSAndroid Build Coastguard Worker#include <arpa/inet.h>
468*49cdfc7eSAndroid Build Coastguard Worker
469*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h"
470*49cdfc7eSAndroid Build Coastguard Worker#include "tst_netlink.h"
471*49cdfc7eSAndroid Build Coastguard Worker#include "tst_netdevice.h"
472*49cdfc7eSAndroid Build Coastguard Worker
473*49cdfc7eSAndroid Build Coastguard Worker...
474*49cdfc7eSAndroid Build Coastguard Worker
475*49cdfc7eSAndroid Build Coastguard Workervoid setup(void)
476*49cdfc7eSAndroid Build Coastguard Worker{
477*49cdfc7eSAndroid Build Coastguard Worker	struct tst_netlink_context *ctx;
478*49cdfc7eSAndroid Build Coastguard Worker	int index, ret;
479*49cdfc7eSAndroid Build Coastguard Worker	in_addr_t addr;
480*49cdfc7eSAndroid Build Coastguard Worker
481*49cdfc7eSAndroid Build Coastguard Worker	struct nlmsghdr header = {
482*49cdfc7eSAndroid Build Coastguard Worker		.nlmsg_type = RTM_NEWADDR,
483*49cdfc7eSAndroid Build Coastguard Worker		.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE |
484*49cdfc7eSAndroid Build Coastguard Worker			NLM_F_EXCL
485*49cdfc7eSAndroid Build Coastguard Worker	};
486*49cdfc7eSAndroid Build Coastguard Worker
487*49cdfc7eSAndroid Build Coastguard Worker	struct ifaddrmsg info = {
488*49cdfc7eSAndroid Build Coastguard Worker		.ifa_family = AF_INET,
489*49cdfc7eSAndroid Build Coastguard Worker		.ifa_prefixlen = 24
490*49cdfc7eSAndroid Build Coastguard Worker	};
491*49cdfc7eSAndroid Build Coastguard Worker
492*49cdfc7eSAndroid Build Coastguard Worker	index = NETDEV_INDEX_BY_NAME("ltp_veth1");
493*49cdfc7eSAndroid Build Coastguard Worker	info.ifa_index = index;
494*49cdfc7eSAndroid Build Coastguard Worker
495*49cdfc7eSAndroid Build Coastguard Worker	ctx = NETLINK_CREATE_CONTEXT(NETLINK_ROUTE);
496*49cdfc7eSAndroid Build Coastguard Worker	NETLINK_ADD_MESSAGE(ctx, &header, &info, sizeof(info));
497*49cdfc7eSAndroid Build Coastguard Worker	addr = inet_addr("192.168.123.45");
498*49cdfc7eSAndroid Build Coastguard Worker	RTNL_ADD_ATTR(ctx, IFA_LOCAL, &addr, sizeof(addr));
499*49cdfc7eSAndroid Build Coastguard Worker	ret = NETLINK_SEND_VALIDATE(ctx);
500*49cdfc7eSAndroid Build Coastguard Worker	NETLINK_DESTROY_CONTEXT(ctx);
501*49cdfc7eSAndroid Build Coastguard Worker
502*49cdfc7eSAndroid Build Coastguard Worker	if (!ret) {
503*49cdfc7eSAndroid Build Coastguard Worker		tst_brk(TBROK, "Failed to set ltp_veth1 address");
504*49cdfc7eSAndroid Build Coastguard Worker	}
505*49cdfc7eSAndroid Build Coastguard Worker}
506*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
507