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/pkt_sched.h>
34 #include <linux/rtnetlink.h>
35
36 #ifndef TCA_STAB
37 # define TCA_STAB 8
38 #endif
39 #ifndef TCA_STAB_DATA
40 # define TCA_STAB_DATA 2
41 #endif
42
43 const unsigned int hdrlen = sizeof(struct tcmsg);
44
45 static void
init_tcmsg(struct nlmsghdr * const nlh,const unsigned int msg_len)46 init_tcmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
47 {
48 SET_STRUCT(struct nlmsghdr, nlh,
49 .nlmsg_len = msg_len,
50 .nlmsg_type = RTM_GETQDISC,
51 .nlmsg_flags = NLM_F_DUMP
52 );
53
54 struct tcmsg *const msg = NLMSG_DATA(nlh);
55 SET_STRUCT(struct tcmsg, msg,
56 .tcm_family = AF_UNIX,
57 .tcm_ifindex = ifindex_lo()
58 );
59
60 struct nlattr *const nla = NLMSG_ATTR(nlh, sizeof(*msg));
61 SET_STRUCT(struct nlattr, nla,
62 .nla_len = msg_len - NLMSG_SPACE(hdrlen),
63 .nla_type = TCA_STAB
64 );
65 }
66
67 static void
print_tcmsg(const unsigned int msg_len)68 print_tcmsg(const unsigned int msg_len)
69 {
70 printf("{len=%u, type=RTM_GETQDISC, flags=NLM_F_DUMP"
71 ", seq=0, pid=0}, {tcm_family=AF_UNIX"
72 ", tcm_ifindex=" IFINDEX_LO_STR
73 ", tcm_handle=0, tcm_parent=0, tcm_info=0}"
74 ", {{nla_len=%u, nla_type=TCA_STAB}",
75 msg_len, msg_len - NLMSG_SPACE(hdrlen));
76 }
77
78 static void
print_uint16(const uint16_t * p)79 print_uint16(const uint16_t *p)
80 {
81 printf("%u", *p);
82 }
83
84 int
main(void)85 main(void)
86 {
87 skip_if_unavailable("/proc/self/fd/");
88
89 const int fd = create_nl_socket(NETLINK_ROUTE);
90 void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
91
92 static char pattern[4096];
93 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
94
95 #ifdef HAVE_STRUCT_TC_SIZESPEC
96 static const struct tc_sizespec s = {
97 .cell_log = 0xab,
98 .size_log = 0xcd,
99 .cell_align = 0xefab,
100 .overhead = 0xcdadeefa,
101 .linklayer = 0xefbaafeb,
102 .mpu = 0xfebfaefb,
103 .mtu = 0xacdbefab,
104 .tsize = 0xbdeaabed
105 };
106 TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
107 init_tcmsg, print_tcmsg,
108 TCA_STAB_BASE, pattern, s,
109 PRINT_FIELD_U("{", s, cell_log);
110 PRINT_FIELD_U(", ", s, size_log);
111 PRINT_FIELD_D(", ", s, cell_align);
112 PRINT_FIELD_D(", ", s, overhead);
113 PRINT_FIELD_U(", ", s, linklayer);
114 PRINT_FIELD_U(", ", s, mpu);
115 PRINT_FIELD_U(", ", s, mtu);
116 PRINT_FIELD_U(", ", s, tsize);
117 printf("}"));
118 #endif
119
120 uint16_t data[2] = { 0xacbd, 0xefba };
121 TEST_NESTED_NLATTR_ARRAY(fd, nlh0, hdrlen,
122 init_tcmsg, print_tcmsg,
123 TCA_STAB_DATA, pattern, data, print_uint16);
124
125 puts("+++ exited with 0 +++");
126 return 0;
127 }
128