• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3  * Copyright (c) 2017-2018 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 <inttypes.h>
33 #include "test_nlattr.h"
34 #include <linux/if.h>
35 #include <linux/if_arp.h>
36 #ifdef HAVE_LINUX_IF_LINK_H
37 # include <linux/if_link.h>
38 #endif
39 #include <linux/rtnetlink.h>
40 
41 #if !HAVE_DECL_IFLA_PROTINFO
42 enum { IFLA_PROTINFO = 12 };
43 #endif
44 
45 #define IFLA_BRPORT_PRIORITY 2
46 #define IFLA_BRPORT_MESSAGE_AGE_TIMER 21
47 
48 #define IFLA_ATTR IFLA_PROTINFO
49 #include "nlattr_ifla.h"
50 
51 int
main(void)52 main(void)
53 {
54 	skip_if_unavailable("/proc/self/fd/");
55 
56 	const uint16_t u16 = 0xabcd;
57 	const uint64_t u64 = 0xabcdedeeefeafeab;
58 	const int fd = create_nl_socket(NETLINK_ROUTE);
59 	void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
60 				   NLA_HDRLEN * 2 + sizeof(u64));
61 
62 	static char pattern[4096];
63 	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
64 
65 	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
66 				  init_ifinfomsg, print_ifinfomsg,
67 				  IFLA_BRPORT_PRIORITY, pattern, u16,
68 				  printf("%u", u16));
69 
70 	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
71 				  init_ifinfomsg, print_ifinfomsg,
72 				  IFLA_BRPORT_MESSAGE_AGE_TIMER, pattern, u64,
73 				  printf("%" PRIu64, u64));
74 
75 #ifdef HAVE_STRUCT_IFLA_BRIDGE_ID
76 	static const struct ifla_bridge_id id = {
77 		.prio = { 0xab, 0xcd },
78 		.addr = { 0xab, 0xcd, 0xef, 0xac, 0xbc, 0xcd }
79 	};
80 	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
81 				  init_ifinfomsg, print_ifinfomsg,
82 				  IFLA_BRPORT_ROOT_ID, pattern, id,
83 				  printf("{prio=[%u, %u]"
84 					 ", addr=%02x:%02x:%02x:%02x:%02x:%02x}",
85 					 id.prio[0], id.prio[1],
86 					 id.addr[0], id.addr[1], id.addr[2],
87 					 id.addr[3], id.addr[4], id.addr[5]));
88 #endif
89 
90 	puts("+++ exited with 0 +++");
91 	return 0;
92 }
93