libnl: /data/src/libnl3/lib/idiag/idiag.c Source File

libnl 3.10.0

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