2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017 The strace developers.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
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>
42 #ifdef HAVE_IF_INDEXTONAME
43 # define IFINDEX_LO (if_nametoindex("lo"))
49 init_packet_diag_msg(struct nlmsghdr *const nlh, const unsigned int msg_len)
51 SET_STRUCT(struct nlmsghdr, nlh,
53 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
54 .nlmsg_flags = NLM_F_DUMP
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
65 print_packet_diag_msg(const unsigned int msg_len)
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]}",
76 print_packet_diag_mclist(const struct packet_diag_mclist *const dml)
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);
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)
93 print_sock_filter(const struct sock_filter *const f)
96 printf("BPF_STMT(BPF_LD|BPF_B|BPF_ABS"
97 ", SKF_AD_OFF+SKF_AD_PKTTYPE)");
99 printf("BPF_STMT(BPF_RET|BPF_K, 0x2a)");
105 skip_if_unavailable("/proc/self/fd/");
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));
111 static char pattern[4096];
112 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
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
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}"));
132 const struct packet_diag_mclist dml[] = {
134 .pdmc_index = IFINDEX_LO,
135 .pdmc_count = 0xabcdaefc,
141 .pdmc_index = IFINDEX_LO,
142 .pdmc_count = 0xdaefeafc,
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);
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
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);
174 TEST_NLATTR_ARRAY(fd, nlh0, hdrlen,
175 init_packet_diag_msg, print_packet_diag_msg,
176 PACKET_DIAG_FILTER, pattern, filter,
179 printf("+++ exited with 0 +++\n");