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