]> granicus.if.org Git - strace/blob - tests/nlattr_packet_diag_msg.c
tests: extend TEST_NETLINK_OBJECT macro
[strace] / tests / nlattr_packet_diag_msg.c
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 <string.h>
33 #include <stdint.h>
34 #include <net/if.h>
35 #include "test_nlattr.h"
36 #include <sys/socket.h>
37 #include <linux/filter.h>
38 #include <linux/packet_diag.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/sock_diag.h>
41
42 #ifdef HAVE_IF_INDEXTONAME
43 # define IFINDEX_LO     (if_nametoindex("lo"))
44 #else
45 # define IFINDEX_LO     1
46 #endif
47
48 static void
49 init_packet_diag_msg(struct nlmsghdr *const nlh, const unsigned int msg_len)
50 {
51         SET_STRUCT(struct nlmsghdr, nlh,
52                 .nlmsg_len = msg_len,
53                 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
54                 .nlmsg_flags = NLM_F_DUMP
55         );
56
57         struct packet_diag_msg *const msg = NLMSG_DATA(nlh);
58         *msg = (struct packet_diag_msg) {
59                 .pdiag_family = AF_PACKET,
60                 .pdiag_type = SOCK_STREAM
61         };
62 }
63
64 static void
65 print_packet_diag_msg(const unsigned int msg_len)
66 {
67         printf("{len=%u, type=SOCK_DIAG_BY_FAMILY"
68                ", flags=NLM_F_DUMP, seq=0, pid=0}"
69                ", {pdiag_family=AF_PACKET"
70                ", pdiag_type=SOCK_STREAM, pdiag_num=0"
71                ", pdiag_ino=0, pdiag_cookie=[0, 0]}",
72                msg_len);
73 }
74
75 static void
76 print_packet_diag_mclist(const struct packet_diag_mclist *const dml)
77 {
78         printf("{pdmc_index=if_nametoindex(\"lo\")");
79         PRINT_FIELD_U(", ", *dml, pdmc_count);
80         PRINT_FIELD_U(", ", *dml, pdmc_type);
81         PRINT_FIELD_U(", ", *dml, pdmc_alen);
82         printf(", pdmc_addr=");
83         print_quoted_hex(dml->pdmc_addr, dml->pdmc_alen);
84         printf("}");
85 }
86
87 static const struct sock_filter filter[] = {
88         BPF_STMT(BPF_LD|BPF_B|BPF_ABS, SKF_AD_OFF+SKF_AD_PKTTYPE),
89         BPF_STMT(BPF_RET|BPF_K, 0x2a)
90 };
91
92 static void
93 print_sock_filter(const struct sock_filter *const f)
94 {
95         if (f == filter)
96                 printf("BPF_STMT(BPF_LD|BPF_B|BPF_ABS"
97                        ", SKF_AD_OFF+SKF_AD_PKTTYPE)");
98         else
99                 printf("BPF_STMT(BPF_RET|BPF_K, 0x2a)");
100 }
101
102 int
103 main(void)
104 {
105         skip_if_unavailable("/proc/self/fd/");
106
107         int fd = create_nl_socket(NETLINK_SOCK_DIAG);
108         const unsigned int hdrlen = sizeof(struct packet_diag_msg);
109         void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
110
111         static char pattern[4096];
112         fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
113
114         static const struct packet_diag_info pinfo = {
115                 .pdi_index = 0xabcddafa,
116                 .pdi_version = 0xbabcdafb,
117                 .pdi_reserve = 0xcfaacdaf,
118                 .pdi_copy_thresh = 0xdabacdaf,
119                 .pdi_tstamp = 0xeafbaadf,
120                 .pdi_flags = PDI_RUNNING
121         };
122         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
123                            init_packet_diag_msg, print_packet_diag_msg,
124                            PACKET_DIAG_INFO, pattern, pinfo,
125                            PRINT_FIELD_U("{", pinfo, pdi_index);
126                            PRINT_FIELD_U(", ", pinfo, pdi_version);
127                            PRINT_FIELD_U(", ", pinfo, pdi_reserve);
128                            PRINT_FIELD_U(", ", pinfo, pdi_copy_thresh);
129                            PRINT_FIELD_U(", ", pinfo, pdi_tstamp);
130                            printf(", pdi_flags=PDI_RUNNING}"));
131
132         const struct packet_diag_mclist dml[] = {
133                 {
134                         .pdmc_index = IFINDEX_LO,
135                         .pdmc_count = 0xabcdaefc,
136                         .pdmc_type = 0xcdaf,
137                         .pdmc_alen = 4,
138                         .pdmc_addr = "1234"
139                 },
140                 {
141                         .pdmc_index = IFINDEX_LO,
142                         .pdmc_count = 0xdaefeafc,
143                         .pdmc_type = 0xadef,
144                         .pdmc_alen = 4,
145                         .pdmc_addr = "5678"
146                 }
147         };
148         TEST_NLATTR_ARRAY(fd, nlh0, hdrlen,
149                           init_packet_diag_msg, print_packet_diag_msg,
150                           PACKET_DIAG_MCLIST, pattern, dml,
151                           print_packet_diag_mclist);
152
153         static const struct packet_diag_ring pdr = {
154                 .pdr_block_size = 0xabcdafed,
155                 .pdr_block_nr = 0xbcadefae,
156                 .pdr_frame_size = 0xcabdfeac,
157                 .pdr_frame_nr = 0xdeaeadef,
158                 .pdr_retire_tmo = 0xedbafeac,
159                 .pdr_sizeof_priv = 0xfeadeacd,
160                 .pdr_features = 0xadebadea
161         };
162         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
163                            init_packet_diag_msg, print_packet_diag_msg,
164                            PACKET_DIAG_RX_RING, pattern, pdr,
165                            PRINT_FIELD_U("{", pdr, pdr_block_size);
166                            PRINT_FIELD_U(", ", pdr, pdr_block_nr);
167                            PRINT_FIELD_U(", ", pdr, pdr_frame_size);
168                            PRINT_FIELD_U(", ", pdr, pdr_frame_nr);
169                            PRINT_FIELD_U(", ", pdr, pdr_retire_tmo);
170                            PRINT_FIELD_U(", ", pdr, pdr_sizeof_priv);
171                            PRINT_FIELD_U(", ", pdr, pdr_features);
172                            printf("}"));
173
174         TEST_NLATTR_ARRAY(fd, nlh0, hdrlen,
175                           init_packet_diag_msg, print_packet_diag_msg,
176                           PACKET_DIAG_FILTER, pattern, filter,
177                           print_sock_filter);
178
179         printf("+++ exited with 0 +++\n");
180         return 0;
181 }