libnl 3.5.0

idiag.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * lib/idiag/idiag.c Inet Diag Netlink
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation version 2.1
8 * of the License.
9 *
10 * Copyright (c) 2013 Sassano Systems LLC <joe@sassanosystems.com>
11 */
12
13 /**
14 * @defgroup idiag Inet Diag library (libnl-idiag)
15 * @brief
16 * @{
17 */
18
19 #include <netlink-private/netlink.h>
20 #include <netlink/netlink.h>
21 #include <netlink/cache.h>
22 #include <netlink/idiag/idiagnl.h>
23 #include <linux/inet_diag.h>
24
25 /**
26 * @name Socket Creation
27 * @{
28 */
29
30 /**
31 * Create and connect idiag netlink socket.
32 * @arg sk Netlink socket.
33 *
34 * Creates a NETLINK_INET_DIAG socket, binds the socket, and issues a connection
35 * attemp.
36 *
37 * @see nl_connect()
38 *
39 * @return 0 on success or a negative error code.
40 */
41 int idiagnl_connect(struct nl_sock *sk)
42 {
43 return nl_connect(sk, NETLINK_INET_DIAG);
44 }
45
46 /** @} */
47
48 /**
49 * @name Sending
50 * @{
51 */
52
53 /**
54 * Send trivial idiag netlink message
55 * @arg sk Netlink socket.
56 * @arg flags Message flags
57 * @arg family Address family
58 * @arg states Socket states to query
59 * @arg ext Inet Diag attribute extensions to query. Note that this only supports
60 * 8 bit arguments. Flags outside uint8_t range are silently ignored.
61 *
62 * @return 0 on success or a negative error code. Due to a bug, this function
63 * returns the number of bytes sent. Treat any non-negative number as success.
64 */
65 int idiagnl_send_simple(struct nl_sock *sk, int flags, uint8_t family,
66 uint16_t states, uint16_t ext)
67 {
68 struct inet_diag_req req;
69 memset(&req, 0, sizeof(req));
70
71 flags |= NLM_F_ROOT;
72
73 req.idiag_family = family;
74 req.idiag_states = states;
75 req.idiag_ext = ext;
76
77 return nl_send_simple(sk, TCPDIAG_GETSOCK, flags, &req, sizeof(req));
78 }
79
80 /** @} */
81
82 /**
83 * @name Inet Diag flag and attribute conversions
84 * @{
85 */
86
87 static const struct trans_tbl idiag_states[] = {
88 __ADD(TCP_ESTABLISHED, established),
89 __ADD(TCP_SYN_SENT, syn_sent),
90 __ADD(TCP_SYN_RECV, syn_recv),
91 __ADD(TCP_FIN_WAIT1, fin_wait),
92 __ADD(TCP_FIN_WAIT2, fin_wait2),
93 __ADD(TCP_TIME_WAIT, time_wait),
94 __ADD(TCP_CLOSE, close),
95 __ADD(TCP_CLOSE_WAIT, close_wait),
96 __ADD(TCP_LAST_ACK, last_ack),
97 __ADD(TCP_LISTEN, listen),
98 __ADD(TCP_CLOSING, closing),
99 };
100
101 /**
102 * Convert inet diag socket states to strings.
103 * @arg state inetdiag socket state (e.g., TCP_ESTABLISHED)
104 * @arg buf output buffer which will hold string result
105 * @arg len length in bytes of the output buffer
106 *
107 * @return string representation of the inetdiag socket state or an empty
108 * string.
109 */
110 char * idiagnl_state2str(int state, char *buf, size_t len)
111 {
112 return __type2str(state, buf, len, idiag_states,
113 ARRAY_SIZE(idiag_states));
114 }
115
116 /**
117 * Convert inet diag socket state string to int.
118 * @arg name inetdiag socket state string
119 *
120 * @return the int representation of the socket state strign or a negative error
121 * code.
122 */
123 int idiagnl_str2state(const char *name)
124 {
125 return __str2type(name, idiag_states, ARRAY_SIZE(idiag_states));
126 }
127
128 static const struct trans_tbl idiag_timers[] = {
129 __ADD(IDIAGNL_TIMER_OFF, off),
130 __ADD(IDIAGNL_TIMER_ON, on),
131 __ADD(IDIAGNL_TIMER_KEEPALIVE, keepalive),
132 __ADD(IDIAGNL_TIMER_TIMEWAIT, timewait),
133 __ADD(IDIAGNL_TIMER_PERSIST, persist),
134 __ADD(IDIAGNL_TIMER_UNKNOWN, unknown),
135 };
136
137 /**
138 * Convert inet diag timer types to strings.
139 * @arg timer inetdiag timer (e.g., IDIAGNL_TIMER_ON)
140 * @arg buf output buffer which will hold string result
141 * @arg len length in bytes of the output buffer
142 *
143 * @return string representation of the inetdiag timer type or an empty string.
144 */
145 char * idiagnl_timer2str(int timer, char *buf, size_t len)
146 {
147 return __type2str(timer, buf, len, idiag_timers,
148 ARRAY_SIZE(idiag_timers));
149 }
150
151 /**
152 * Convert inet diag timer string to int.
153 * @arg name inetdiag timer string
154 *
155 * @return the int representation of the timer string or a negative error code.
156 */
157 int idiagnl_str2timer(const char *name)
158 {
159 return __str2type(name, idiag_timers, ARRAY_SIZE(idiag_timers));
160 }
161
162 static const struct trans_tbl idiag_attrs[] = {
163 __ADD(INET_DIAG_NONE, none),
164 __ADD(INET_DIAG_MEMINFO, meminfo),
165 __ADD(INET_DIAG_INFO, info),
166 __ADD(INET_DIAG_VEGASINFO, vegasinfo),
167 __ADD(INET_DIAG_CONG, congestion),
168 __ADD(INET_DIAG_TOS, tos),
169 __ADD(INET_DIAG_TCLASS, tclass),
170 __ADD(INET_DIAG_SKMEMINFO, skmeminfo),
171 __ADD(INET_DIAG_SHUTDOWN, shutdown),
172 };
173
174 /**
175 * Convert inet diag extension type to a string.
176 * @arg attrs inet diag extension type (e.g. INET_DIAG_MEMINFO)
177 * @arg buf output buffer which will hold string result
178 * @arg len length in bytes of the output buffer
179 *
180 * @return string representation of inet diag extension type or an empty string.
181 * @deprecated: don't use this function. It is not very useful and should
182 * never have been exposed as public API.
183 */
184 char *idiagnl_attrs2str(int attrs, char *buf, size_t len)
185 {
186 return __type2str(attrs, buf, len, idiag_attrs, ARRAY_SIZE(idiag_attrs));
187 }
188
189 static const struct trans_tbl idiag_exts[] = {
190 __ADD((1 << (INET_DIAG_MEMINFO - 1)), meminfo),
191 __ADD((1 << (INET_DIAG_INFO - 1)), info),
192 __ADD((1 << (INET_DIAG_VEGASINFO - 1)), vegasinfo),
193 __ADD((1 << (INET_DIAG_CONG - 1)), congestion),
194 __ADD((1 << (INET_DIAG_TOS - 1)), tos),
195 __ADD((1 << (INET_DIAG_TCLASS - 1)), tclass),
196 __ADD((1 << (INET_DIAG_SKMEMINFO - 1)), skmeminfo),
197 __ADD((1 << (INET_DIAG_SHUTDOWN - 1)), shutdown),
198 };
199
200 /**
201 * Convert inet diag extension flags to a string.
202 * @arg attrs inet diag extension flags (e.g.
203 * ( (1<<(INET_DIAG_MEMINFO-1)) | (1<<(INET_DIAG_CONG-1)) | (1<<(INET_DIAG_TOS-1)) ) )
204 * @arg buf Output buffer to hold string representation
205 * @arg len length in bytes of the output buffer
206 */
207 char *idiagnl_exts2str(uint8_t attrs, char *buf, size_t len)
208 {
209 return __flags2str(attrs, buf, len, idiag_exts, ARRAY_SIZE(idiag_exts));
210 }
211
212 static const struct trans_tbl idiagnl_tcpstates[] = {
213 __ADD(TCP_CA_Open, open),
214 __ADD(TCP_CA_Disorder, disorder),
215 __ADD(TCP_CA_CWR, cwr),
216 __ADD(TCP_CA_Recovery, recovery),
217 __ADD(TCP_CA_Loss, loss),
218 };
219
220 /**
221 * Convert inetdiag tcp states to strings.
222 * @arg state TCP state (e.g., TCP_CA_Open)
223 * @arg buf output buffer which will hold string result
224 * @arg len length in bytes of the output buffer
225 */
226 char *idiagnl_tcpstate2str(uint8_t state, char *buf, size_t len)
227 {
228 return __type2str(state, buf, len, idiagnl_tcpstates,
229 ARRAY_SIZE(idiagnl_tcpstates));
230 }
231
232 static const struct trans_tbl idiagnl_tcpopt_attrs[] = {
233 __ADD(TCPI_OPT_TIMESTAMPS, timestamps),
234 __ADD(TCPI_OPT_SACK, sACK),
235 __ADD(TCPI_OPT_WSCALE, wscale),
236 __ADD(TCPI_OPT_ECN, ecn),
237 };
238
239 /**
240 * Convert TCP option attributes to string
241 * @arg attrs TCP option attributes to convert (e.g., TCPI_OPT_SACK |
242 * TCPI_OPT_WSCALE)
243 * @arg buf Output buffer for string
244 * @arg len Length in bytes of output buffer
245 *
246 * @return buffer with string representation or empty string
247 */
248 char *idiagnl_tcpopts2str(uint8_t attrs, char *buf, size_t len)
249 {
250 return __flags2str(attrs, buf, len, idiagnl_tcpopt_attrs,
251 ARRAY_SIZE(idiagnl_tcpopt_attrs));
252 }
253
254 /**
255 * Convert shutdown state to string.
256 * @arg shutdown Shutdown state (e.g., idiag_msg->shutdown)
257 * @arg buf Ouput buffer to hold string representation
258 * @arg len Length in bytes of output buffer
259 *
260 * @return string representation of shutdown state or NULL
261 */
262 char * idiagnl_shutdown2str(uint8_t shutdown, char *buf, size_t len)
263 {
264 if (shutdown == 0) {
265 snprintf(buf, len, " ");
266 return buf;
267 } else if (shutdown == 1) {
268 snprintf(buf, len, "receive shutdown");
269 return buf;
270 } else if (shutdown == 2) {
271 snprintf(buf, len, "send shutdown");
272 return buf;
273 }
274
275 return NULL;
276 }
277
278 /** @} */
279 /** @} */
idiagnl_exts2str
char * idiagnl_exts2str(uint8_t attrs, char *buf, size_t len)
Convert inet diag extension flags to a string.
Definition: idiag.c:207
nl_connect
int nl_connect(struct nl_sock *sk, int protocol)
Create file descriptor and bind socket.
Definition: nl.c:104
idiagnl_str2state
int idiagnl_str2state(const char *name)
Convert inet diag socket state string to int.
Definition: idiag.c:123
idiagnl_send_simple
int idiagnl_send_simple(struct nl_sock *sk, int flags, uint8_t family, uint16_t states, uint16_t ext)
Send trivial idiag netlink message.
Definition: idiag.c:65
idiagnl_shutdown2str
char * idiagnl_shutdown2str(uint8_t shutdown, char *buf, size_t len)
Convert shutdown state to string.
Definition: idiag.c:262
idiagnl_attrs2str
char * idiagnl_attrs2str(int attrs, char *buf, size_t len)
Convert inet diag extension type to a string.
Definition: idiag.c:184
idiagnl_str2timer
int idiagnl_str2timer(const char *name)
Convert inet diag timer string to int.
Definition: idiag.c:157
idiagnl_tcpopts2str
char * idiagnl_tcpopts2str(uint8_t attrs, char *buf, size_t len)
Convert TCP option attributes to string.
Definition: idiag.c:248
idiagnl_connect
int idiagnl_connect(struct nl_sock *sk)
Create and connect idiag netlink socket.
Definition: idiag.c:41
idiagnl_tcpstate2str
char * idiagnl_tcpstate2str(uint8_t state, char *buf, size_t len)
Convert inetdiag tcp states to strings.
Definition: idiag.c:226
nl_send_simple
int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf, size_t size)
Construct and transmit a Netlink message.
Definition: nl.c:581
idiagnl_timer2str
char * idiagnl_timer2str(int timer, char *buf, size_t len)
Convert inet diag timer types to strings.
Definition: idiag.c:145
idiagnl_state2str
char * idiagnl_state2str(int state, char *buf, size_t len)
Convert inet diag socket states to strings.
Definition: idiag.c:110