Debian Codegraph @

agent/mibgroup/sctp-mib/sctpTables_linux.c

raw
1 #include <net-snmp/net-snmp-config.h>
2 #include <net-snmp/net-snmp-includes.h>
3 #include <net-snmp/agent/net-snmp-agent-includes.h>
4
5 #include "sctpAssocTable.h"
6 #include "sctpAssocLocalAddrTable.h"
7 #include "sctpAssocRemAddrTable.h"
8 #include "sctpTables_common.h"
9
10 #include "mibgroup/util_funcs/get_pid_from_inode.h"
11
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <arpa/inet.h>
15
16 /*
17 * Linux provides sctp statistics in /proc/net/sctp/assoc and
18 * /proc/net/sctp/remaddr. The 'assoc' file covers sctpAssocTable and
19 * sctpAssocLocalAddrTable, the later one contains sctpAssocRemAddrTable.
20 *
21 * Linux does *not* provide *StartTime timestamps. This implementation tries
22 * to guess these timestamps.
23 */
24
25
26 #define PROC_PREFIX "/proc"
27 #define ASSOC_FILE PROC_PREFIX "/net/sctp/assocs"
28 #define REMADDR_FILE PROC_PREFIX "/net/sctp/remaddr"
29
30 /*
31 * Convert string with ipv4 or ipv6 address to provided buffer.
32 */
33 static int
34 convert_address(char *token, char *addr_buffer, u_long * addr_type,
35 u_long * addr_len)
36 {
37 int family;
38 int ret;
39
40 if (strchr(token, ':') != NULL) {
41 family = AF_INET6;
42 *addr_type = INETADDRESSTYPE_IPV6;
43 *addr_len = 16;
44 } else {
45 family = AF_INET;
46 *addr_type = INETADDRESSTYPE_IPV4;
47 *addr_len = 4;
48 }
49 ret = inet_pton(family, token, addr_buffer);
50
51 if (ret <= 0)
52 return SNMP_ERR_GENERR;
53 return SNMP_ERR_NOERROR;
54 }
55
56
57 /*
58 * Parse local address part from assoc file. It assumes that strtok will return
59 * these addresses. The addresses are separated by space and the list ends
60 * with "<->".
61 */
62 static int
63 parse_assoc_local_addresses(sctpAssocTable_entry * entry,
64 sctpTables_containers * containers)
65 {
66 char *token;
67 int ret;
68 /*
69 * parse all local addresses
70 */
71
72 while ((token = strtok(NULL, " "))) {
73 sctpAssocLocalAddrTable_entry *localAddr;
74 char *ip = token;
75
76 if (token[0] == '<')
77 break; /* local addresses finished */
78
79 if (token[0] == '*')
80 ip = token + 1;
81
82 localAddr = sctpAssocLocalAddrTable_entry_create();
83 if (localAddr == NULL)
84 return SNMP_ERR_GENERR;
85
86 localAddr->sctpAssocId = entry->sctpAssocId;
87 ret =
88 convert_address(ip, localAddr->sctpAssocLocalAddr,
89 &localAddr->sctpAssocLocalAddrType,
90 &localAddr->sctpAssocLocalAddr_len);
91 if (ret != SNMP_ERR_NOERROR) {
92 sctpAssocLocalAddrTable_entry_free(localAddr);
93 return SNMP_ERR_GENERR;
94 }
95
96 ret =
97 sctpAssocLocalAddrTable_add_or_update(containers->
98 sctpAssocLocalAddrTable,
99 localAddr);
100 if (ret != SNMP_ERR_NOERROR)
101 return SNMP_ERR_GENERR;
102 }
103 return SNMP_ERR_NOERROR;
104 }
105
106 /*
107 * Parse primary remote address part from assoc file. It assumes that strtok will return
108 * all remote addresses. The addresses are separated by space and the list ends with \t .
109 */
110 static int
111 parse_assoc_remote_addresses(sctpAssocTable_entry * entry)
112 {
113 char *token;
114 int ret = SNMP_ERR_GENERR;
115
116 while ((token = strtok(NULL, " ")) && (token[0] != '\t')) {
117 if (token[0] == '*') {
118 /*
119 * that's the primary address
120 */
121 ret =
122 convert_address(token + 1, entry->sctpAssocRemPrimAddr,
123 &entry->sctpAssocRemPrimAddrType,
124 &entry->sctpAssocRemPrimAddr_len);
125 }
126 }
127 return ret;
128 }
129
130 static int
131 parse_assoc_line(char *line, sctpTables_containers * containers)
132 {
133 unsigned long long inode;
134 char *token;
135 int ret;
136 sctpAssocTable_entry *entry;
137
138 entry = sctpAssocTable_entry_create();
139 if (entry == NULL)
140 return SNMP_ERR_GENERR;
141
142 token = strtok(line, " "); /* ASSOC, ignore */
143 token = strtok(NULL, " "); /* SOCK, ignore */
144 token = strtok(NULL, " "); /* STY, ignore */
145 token = strtok(NULL, " "); /* SST, ignore */
146 token = strtok(NULL, " "); /* ST */
147 if (token == NULL) {
148 ret = SNMP_ERR_GENERR;
149 goto error;
150 }
151 entry->sctpAssocState = strtol(token, NULL, 10);
152
153 token = strtok(NULL, " "); /* HBKT, ignore */
154 token = strtok(NULL, " "); /* ASSOC-ID, store */
155 if (token == NULL) {
156 ret = SNMP_ERR_GENERR;
157 goto error;
158 }
159 entry->sctpAssocId = strtol(token, NULL, 10);
160
161 token = strtok(NULL, " "); /* TX_QUEUE, ignore */
162 token = strtok(NULL, " "); /* RX_QUEUE, ignore */
163 token = strtok(NULL, " "); /* UID, ignore */
164
165 token = strtok(NULL, " "); /* INODE */
166 if (token == NULL) {
167 ret = SNMP_ERR_GENERR;
168 goto error;
169 }
170 inode = strtoull(token, NULL, 10);
171 entry->sctpAssocPrimProcess = netsnmp_get_pid_from_inode(inode);
172
173 token = strtok(NULL, " "); /* LPORT */
174 if (token == NULL) {
175 ret = SNMP_ERR_GENERR;
176 goto error;
177 }
178 entry->sctpAssocLocalPort = strtol(token, NULL, 10);
179
180 token = strtok(NULL, " "); /* RPORT */
181 if (token == NULL) {
182 ret = SNMP_ERR_GENERR;
183 goto error;
184 }
185 entry->sctpAssocRemPort = strtol(token, NULL, 10);
186
187 ret = parse_assoc_local_addresses(entry, containers);
188 if (ret != SNMP_ERR_NOERROR)
189 goto error;
190
191 ret = parse_assoc_remote_addresses(entry);
192 if (ret != SNMP_ERR_NOERROR)
193 goto error;
194
195 token = strtok(NULL, " "); /* HBINT */
196 if (token == NULL) {
197 ret = SNMP_ERR_GENERR;
198 goto error;
199 }
200 entry->sctpAssocHeartBeatInterval = strtol(token, NULL, 10);
201
202 token = strtok(NULL, " "); /* INS */
203 if (token == NULL) {
204 ret = SNMP_ERR_GENERR;
205 goto error;
206 }
207 entry->sctpAssocInStreams = strtol(token, NULL, 10);
208
209 token = strtok(NULL, " "); /* OUTS */
210 if (token == NULL) {
211 ret = SNMP_ERR_GENERR;
212 goto error;
213 }
214 entry->sctpAssocOutStreams = strtol(token, NULL, 10);
215
216 token = strtok(NULL, " "); /* MAXRT */
217 if (token == NULL) {
218 ret = SNMP_ERR_GENERR;
219 goto error;
220 }
221 entry->sctpAssocMaxRetr = strtol(token, NULL, 10);
222
223 token = strtok(NULL, " "); /* T1X */
224 if (token == NULL) {
225 ret = SNMP_ERR_GENERR;
226 goto error;
227 }
228 entry->sctpAssocT1expireds = strtol(token, NULL, 10);
229
230 token = strtok(NULL, " "); /* T2X */
231 if (token == NULL) {
232 ret = SNMP_ERR_GENERR;
233 goto error;
234 }
235 entry->sctpAssocT2expireds = strtol(token, NULL, 10);
236
237 token = strtok(NULL, " "); /* RXTC */
238 if (token == NULL) {
239 ret = SNMP_ERR_GENERR;
240 goto error;
241 }
242 entry->sctpAssocRtxChunks = strtol(token, NULL, 10);
243
244 entry->sctpAssocRemHostName[0] = 0;
245 entry->sctpAssocRemHostName_len = 0;
246 entry->sctpAssocDiscontinuityTime = 0;
247
248 ret = sctpAssocTable_add_or_update(containers->sctpAssocTable, entry);
249 if (ret != SNMP_ERR_NOERROR) {
250 DEBUGMSGTL(("sctp:tables:load:assoc",
251 "error adding/updating the entry in container\n"));
252 return ret;
253 }
254
255 return SNMP_ERR_NOERROR;
256
257 error:
258 if (entry != NULL)
259 sctpAssocTable_entry_free(entry);
260 return ret;
261 }
262
263
264 /*
265 * Load assocTable and localAddrTable from /proc/net/sctp/assoc. Mark all added
266 * or updated entries as valid (so the missing, i.e. invalid, can be deleted).
267 */
268 static int
269 load_assoc(sctpTables_containers * containers)
270 {
271 FILE *f;
272 char line[1024];
273 int ret = SNMP_ERR_NOERROR;
274
275 DEBUGMSGTL(("sctp:tables:load:assoc", "arch load(linux)\n"));
276
277 f = fopen(ASSOC_FILE, "r");
278 if (f == NULL) {
279 DEBUGMSGTL(("sctp:tables:load:assoc",
280 "arch load failed: can't open" ASSOC_FILE "\n"));
281 return SNMP_ERR_GENERR;
282 }
283
284 netsnmp_get_pid_from_inode_init();
285
286 /*
287 * ignore the header.
288 */
289 NETSNMP_IGNORE_RESULT(fgets(line, sizeof(line), f));
290
291 while (fgets(line, sizeof(line), f) != NULL) {
292 DEBUGMSGTL(("sctp:tables:load:assoc", "processing line: %s\n",
293 line));
294
295 ret = parse_assoc_line(line, containers);
296 if (ret != SNMP_ERR_NOERROR) {
297 DEBUGMSGTL(("sctp:tables:load:assoc",
298 "error parsing the line\n"));
299 }
300 }
301 fclose(f);
302
303 return SNMP_ERR_NOERROR;
304 }
305
306
307 static int
308 parse_remaddr_line(char *line, sctpTables_containers * containers)
309 {
310 char *token;
311 int ret;
312 sctpAssocRemAddrTable_entry *entry;
313
314 entry = sctpAssocRemAddrTable_entry_create();
315 if (entry == NULL)
316 return SNMP_ERR_GENERR;
317
318 token = strtok(line, " "); /* rem. address */
319 ret =
320 convert_address(token, entry->sctpAssocRemAddr,
321 &entry->sctpAssocRemAddrType,
322 &entry->sctpAssocRemAddr_len);
323 if (ret < 0) {
324 ret = SNMP_ERR_GENERR;
325 goto error;
326 }
327
328 token = strtok(NULL, " "); /* assoc id */
329 if (token == NULL) {
330 ret = SNMP_ERR_GENERR;
331 goto error;
332 }
333 entry->sctpAssocId = strtol(token, NULL, 10);
334
335 token = strtok(NULL, " "); /* hb act */
336 if (token == NULL) {
337 ret = SNMP_ERR_GENERR;
338 goto error;
339 }
340 if (token[0] == '1')
341 entry->sctpAssocRemAddrHBActive = TRUTHVALUE_TRUE;
342 else
343 entry->sctpAssocRemAddrHBActive = TRUTHVALUE_FALSE;
344
345 token = strtok(NULL, " "); /* rto */
346 if (token == NULL) {
347 ret = SNMP_ERR_GENERR;
348 goto error;
349 }
350 entry->sctpAssocRemAddrRTO = strtol(token, NULL, 10);
351
352 token = strtok(NULL, " "); /* max path rtx */
353 if (token == NULL) {
354 ret = SNMP_ERR_GENERR;
355 goto error;
356 }
357 entry->sctpAssocRemAddrMaxPathRtx = strtol(token, NULL, 10);
358
359 token = strtok(NULL, " "); /* rem addr rtx */
360 if (token == NULL) {
361 ret = SNMP_ERR_GENERR;
362 goto error;
363 }
364 entry->sctpAssocRemAddrRtx = strtol(token, NULL, 10);
365
366 entry->sctpAssocRemAddrStartTime = 0;
367 entry->sctpAssocRemAddrActive = TRUTHVALUE_TRUE;
368
369 ret =
370 sctpAssocRemAddrTable_add_or_update(containers->
371 sctpAssocRemAddrTable, entry);
372 if (ret != SNMP_ERR_NOERROR) {
373 DEBUGMSGTL(("sctp:load:remaddr",
374 "error adding/updating the entry in container\n"));
375 return ret;
376 }
377
378 return SNMP_ERR_NOERROR;
379
380 error:
381 if (entry != NULL)
382 sctpAssocRemAddrTable_entry_free(entry);
383 return ret;
384 }
385
386 /*
387 * Load sctpAssocRemAddrTable from /proc/net/sctp/remaddr. Mark all added
388 * or updated entries as valid (so the missing, i.e. invalid, can be deleted).
389 */
390 static int
391 load_remaddr(sctpTables_containers * containers)
392 {
393 FILE *f;
394 char line[1024];
395 int ret = SNMP_ERR_NOERROR;
396
397 DEBUGMSGTL(("sctp:load:remaddr", "arch load(linux)\n"));
398
399 f = fopen(REMADDR_FILE, "r");
400 if (f == NULL) {
401 DEBUGMSGTL(("sctp:load:remaddr",
402 "arch load failed: can't open" REMADDR_FILE "\n"));
403 return SNMP_ERR_GENERR;
404 }
405
406 /*
407 * ignore the header.
408 */
409 NETSNMP_IGNORE_RESULT(fgets(line, sizeof(line), f));
410
411 while (fgets(line, sizeof(line), f) != NULL) {
412 DEBUGMSGTL(("sctp:load:remaddr", "processing line: %s\n", line));
413
414 ret = parse_remaddr_line(line, containers);
415 if (ret != SNMP_ERR_NOERROR) {
416 DEBUGMSGTL(("sctp:load:remaddr", "error parsing the line\n"));
417 }
418 }
419 fclose(f);
420
421 return SNMP_ERR_NOERROR;
422 }
423
424
425 int
426 sctpTables_arch_load(sctpTables_containers * containers, u_long * flags)
427 {
428 int ret = SNMP_ERR_NOERROR;
429
430 *flags |= SCTP_TABLES_LOAD_FLAG_DELETE_INVALID;
431 *flags |= SCTP_TABLES_LOAD_FLAG_AUTO_LOOKUP;
432
433 ret = load_assoc(containers);
434 if (ret != SNMP_ERR_NOERROR)
435 return ret;
436
437 ret = load_remaddr(containers);
438 if (ret != SNMP_ERR_NOERROR)
439 return ret;
440
441 return ret;
442 }