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