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