1 /**
2 * collectd - src/tcpconns.c
3 * Copyright (C) 2007,2008 Florian octo Forster
4 * Copyright (C) 2008 Michael Stapelberg
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Author:
20 * Florian octo Forster <octo at verplant.org>
21 * Michael Stapelberg <michael+git at stapelberg.de>
22 **/
24 /**
25 * Code within `HAVE_LIBKVM_NLIST' blocks is provided under the following
26 * license:
27 *
28 * $collectd: parts of tcpconns.c, 2008/08/08 03:48:30 Michael Stapelberg $
29 * $OpenBSD: inet.c,v 1.100 2007/06/19 05:28:30 ray Exp $
30 * $NetBSD: inet.c,v 1.14 1995/10/03 21:42:37 thorpej Exp $
31 *
32 * Copyright (c) 1983, 1988, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 */
60 #include "collectd.h"
61 #include "common.h"
62 #include "plugin.h"
64 #if defined(__OpenBSD__) || defined(__NetBSD__)
65 #undef HAVE_SYSCTLBYNAME /* force HAVE_LIBKVM_NLIST path */
66 #endif
68 #if !KERNEL_LINUX && !HAVE_SYSCTLBYNAME && !HAVE_LIBKVM_NLIST
69 # error "No applicable input method."
70 #endif
72 #if KERNEL_LINUX
73 /* #endif KERNEL_LINUX */
75 #elif HAVE_SYSCTLBYNAME
76 # include <sys/socketvar.h>
77 # include <sys/sysctl.h>
79 /* Some includes needed for compiling on FreeBSD */
80 #include <sys/time.h>
81 #if HAVE_SYS_TYPES_H
82 # include <sys/types.h>
83 #endif
84 #if HAVE_SYS_SOCKET_H
85 # include <sys/socket.h>
86 #endif
87 #if HAVE_NET_IF_H
88 # include <net/if.h>
89 #endif
91 # include <net/route.h>
92 # include <netinet/in.h>
93 # include <netinet/in_systm.h>
94 # include <netinet/ip.h>
95 # include <netinet/ip6.h>
96 # include <netinet/in_pcb.h>
97 # include <netinet/ip_var.h>
98 # include <netinet/tcp.h>
99 # include <netinet/tcpip.h>
100 # include <netinet/tcp_seq.h>
101 # include <netinet/tcp_var.h>
102 /* #endif HAVE_SYSCTLBYNAME */
104 /* This is for OpenBSD and NetBSD. */
105 #elif HAVE_LIBKVM_NLIST
106 # include <sys/queue.h>
107 # include <sys/socket.h>
108 # include <net/route.h>
109 # include <netinet/in.h>
110 # include <netinet/in_systm.h>
111 # include <netinet/ip.h>
112 # include <netinet/ip_var.h>
113 # include <netinet/in_pcb.h>
114 # include <netinet/tcp.h>
115 # include <netinet/tcp_timer.h>
116 # include <netinet/tcp_var.h>
117 # include <netdb.h>
118 # include <arpa/inet.h>
119 # include <nlist.h>
120 # include <kvm.h>
121 #endif /* HAVE_LIBKVM_NLIST */
123 #if KERNEL_LINUX
124 static const char *tcp_state[] =
125 {
126 "", /* 0 */
127 "ESTABLISHED",
128 "SYN_SENT",
129 "SYN_RECV",
130 "FIN_WAIT1",
131 "FIN_WAIT2",
132 "TIME_WAIT",
133 "CLOSED",
134 "CLOSE_WAIT",
135 "LAST_ACK",
136 "LISTEN", /* 10 */
137 "CLOSING"
138 };
140 # define TCP_STATE_LISTEN 10
141 # define TCP_STATE_MIN 1
142 # define TCP_STATE_MAX 11
143 /* #endif KERNEL_LINUX */
145 #elif HAVE_SYSCTLBYNAME
146 static const char *tcp_state[] =
147 {
148 "CLOSED",
149 "LISTEN",
150 "SYN_SENT",
151 "SYN_RECV",
152 "ESTABLISHED",
153 "CLOSE_WAIT",
154 "FIN_WAIT1",
155 "CLOSING",
156 "LAST_ACK",
157 "FIN_WAIT2",
158 "TIME_WAIT"
159 };
161 # define TCP_STATE_LISTEN 1
162 # define TCP_STATE_MIN 0
163 # define TCP_STATE_MAX 10
164 /* #endif HAVE_SYSCTLBYNAME */
166 #elif HAVE_LIBKVM_NLIST
167 static const char *tcp_state[] =
168 {
169 "CLOSED",
170 "LISTEN",
171 "SYN_SENT",
172 "SYN_RECV",
173 "ESTABLISHED",
174 "CLOSE_WAIT",
175 "FIN_WAIT1",
176 "CLOSING",
177 "LAST_ACK",
178 "FIN_WAIT2",
179 "TIME_WAIT"
180 };
182 static kvm_t *kvmd;
183 static u_long inpcbtable_off = 0;
184 struct inpcbtable *inpcbtable_ptr = NULL;
186 # define TCP_STATE_LISTEN 1
187 # define TCP_STATE_MIN 1
188 # define TCP_STATE_MAX 10
189 #endif /* HAVE_LIBKVM_NLIST */
191 #define PORT_COLLECT_LOCAL 0x01
192 #define PORT_COLLECT_REMOTE 0x02
193 #define PORT_IS_LISTENING 0x04
195 typedef struct port_entry_s
196 {
197 uint16_t port;
198 uint16_t flags;
199 uint32_t count_local[TCP_STATE_MAX + 1];
200 uint32_t count_remote[TCP_STATE_MAX + 1];
201 struct port_entry_s *next;
202 } port_entry_t;
204 static const char *config_keys[] =
205 {
206 "ListeningPorts",
207 "LocalPort",
208 "RemotePort"
209 };
210 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
212 static int port_collect_listening = 0;
213 static port_entry_t *port_list_head = NULL;
215 static void conn_submit_port_entry (port_entry_t *pe)
216 {
217 value_t values[1];
218 value_list_t vl = VALUE_LIST_INIT;
219 int i;
221 vl.values = values;
222 vl.values_len = 1;
223 vl.time = time (NULL);
224 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
225 sstrncpy (vl.plugin, "tcpconns", sizeof (vl.plugin));
226 sstrncpy (vl.type, "tcp_connections", sizeof (vl.type));
228 if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
229 || (pe->flags & PORT_COLLECT_LOCAL))
230 {
231 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
232 "%"PRIu16"-local", pe->port);
234 for (i = 1; i <= TCP_STATE_MAX; i++)
235 {
236 vl.values[0].gauge = pe->count_local[i];
238 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
240 plugin_dispatch_values (&vl);
241 }
242 }
244 if (pe->flags & PORT_COLLECT_REMOTE)
245 {
246 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
247 "%"PRIu16"-remote", pe->port);
249 for (i = 1; i <= TCP_STATE_MAX; i++)
250 {
251 vl.values[0].gauge = pe->count_remote[i];
253 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
255 plugin_dispatch_values (&vl);
256 }
257 }
258 } /* void conn_submit */
260 static void conn_submit_all (void)
261 {
262 port_entry_t *pe;
264 for (pe = port_list_head; pe != NULL; pe = pe->next)
265 conn_submit_port_entry (pe);
266 } /* void conn_submit_all */
268 static port_entry_t *conn_get_port_entry (uint16_t port, int create)
269 {
270 port_entry_t *ret;
272 ret = port_list_head;
273 while (ret != NULL)
274 {
275 if (ret->port == port)
276 break;
277 ret = ret->next;
278 }
280 if ((ret == NULL) && (create != 0))
281 {
282 ret = (port_entry_t *) malloc (sizeof (port_entry_t));
283 if (ret == NULL)
284 return (NULL);
285 memset (ret, '\0', sizeof (port_entry_t));
287 ret->port = port;
288 ret->next = port_list_head;
289 port_list_head = ret;
290 }
292 return (ret);
293 } /* port_entry_t *conn_get_port_entry */
295 /* Removes ports that were added automatically due to the `ListeningPorts'
296 * setting but which are no longer listening. */
297 static void conn_reset_port_entry (void)
298 {
299 port_entry_t *prev = NULL;
300 port_entry_t *pe = port_list_head;
302 while (pe != NULL)
303 {
304 /* If this entry was created while reading the files (ant not when handling
305 * the configuration) remove it now. */
306 if ((pe->flags & (PORT_COLLECT_LOCAL
307 | PORT_COLLECT_REMOTE
308 | PORT_IS_LISTENING)) == 0)
309 {
310 port_entry_t *next = pe->next;
312 DEBUG ("tcpconns plugin: Removing temporary entry "
313 "for listening port %"PRIu16, pe->port);
315 if (prev == NULL)
316 port_list_head = next;
317 else
318 prev->next = next;
320 sfree (pe);
321 pe = next;
323 continue;
324 }
326 memset (pe->count_local, '\0', sizeof (pe->count_local));
327 memset (pe->count_remote, '\0', sizeof (pe->count_remote));
328 pe->flags &= ~PORT_IS_LISTENING;
330 pe = pe->next;
331 }
332 } /* void conn_reset_port_entry */
334 static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t state)
335 {
336 port_entry_t *pe = NULL;
338 if ((state > TCP_STATE_MAX)
339 #if TCP_STATE_MIN > 0
340 || (state < TCP_STATE_MIN)
341 #endif
342 )
343 {
344 NOTICE ("tcpconns plugin: Ignoring connection with "
345 "unknown state 0x%02"PRIx8".", state);
346 return (-1);
347 }
349 /* Listening sockets */
350 if ((state == TCP_STATE_LISTEN) && (port_collect_listening != 0))
351 {
352 pe = conn_get_port_entry (port_local, 1 /* create */);
353 if (pe != NULL)
354 pe->flags |= PORT_IS_LISTENING;
355 }
357 DEBUG ("tcpconns plugin: Connection %"PRIu16" <-> %"PRIu16" (%s)",
358 port_local, port_remote, tcp_state[state]);
360 pe = conn_get_port_entry (port_local, 0 /* no create */);
361 if (pe != NULL)
362 pe->count_local[state]++;
364 pe = conn_get_port_entry (port_remote, 0 /* no create */);
365 if (pe != NULL)
366 pe->count_remote[state]++;
368 return (0);
369 } /* int conn_handle_ports */
371 #if KERNEL_LINUX
372 static int conn_handle_line (char *buffer)
373 {
374 char *fields[32];
375 int fields_len;
377 char *endptr;
379 char *port_local_str;
380 char *port_remote_str;
381 uint16_t port_local;
382 uint16_t port_remote;
384 uint8_t state;
386 int buffer_len = strlen (buffer);
388 while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
389 buffer[--buffer_len] = '\0';
390 if (buffer_len <= 0)
391 return (-1);
393 fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
394 if (fields_len < 12)
395 {
396 DEBUG ("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
397 return (-1);
398 }
400 port_local_str = strchr (fields[1], ':');
401 port_remote_str = strchr (fields[2], ':');
403 if ((port_local_str == NULL) || (port_remote_str == NULL))
404 return (-1);
405 port_local_str++;
406 port_remote_str++;
407 if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
408 return (-1);
410 endptr = NULL;
411 port_local = (uint16_t) strtol (port_local_str, &endptr, 16);
412 if ((endptr == NULL) || (*endptr != '\0'))
413 return (-1);
415 endptr = NULL;
416 port_remote = (uint16_t) strtol (port_remote_str, &endptr, 16);
417 if ((endptr == NULL) || (*endptr != '\0'))
418 return (-1);
420 endptr = NULL;
421 state = (uint8_t) strtol (fields[3], &endptr, 16);
422 if ((endptr == NULL) || (*endptr != '\0'))
423 return (-1);
425 return (conn_handle_ports (port_local, port_remote, state));
426 } /* int conn_handle_line */
428 static int conn_read_file (const char *file)
429 {
430 FILE *fh;
431 char buffer[1024];
433 fh = fopen (file, "r");
434 if (fh == NULL)
435 return (-1);
437 while (fgets (buffer, sizeof (buffer), fh) != NULL)
438 {
439 conn_handle_line (buffer);
440 } /* while (fgets) */
442 fclose (fh);
444 return (0);
445 } /* int conn_read_file */
446 /* #endif KERNEL_LINUX */
448 #elif HAVE_SYSCTLBYNAME
449 /* #endif HAVE_SYSCTLBYNAME */
451 #elif HAVE_LIBKVM_NLIST
452 #endif /* HAVE_LIBKVM_NLIST */
454 static int conn_config (const char *key, const char *value)
455 {
456 if (strcasecmp (key, "ListeningPorts") == 0)
457 {
458 if ((strcasecmp (value, "Yes") == 0)
459 || (strcasecmp (value, "True") == 0)
460 || (strcasecmp (value, "On") == 0))
461 port_collect_listening = 1;
462 else
463 port_collect_listening = 0;
464 }
465 else if ((strcasecmp (key, "LocalPort") == 0)
466 || (strcasecmp (key, "RemotePort") == 0))
467 {
468 port_entry_t *pe;
469 int port = atoi (value);
471 if ((port < 1) || (port > 65535))
472 {
473 ERROR ("tcpconns plugin: Invalid port: %i", port);
474 return (1);
475 }
477 pe = conn_get_port_entry ((uint16_t) port, 1 /* create */);
478 if (pe == NULL)
479 {
480 ERROR ("tcpconns plugin: conn_get_port_entry failed.");
481 return (1);
482 }
484 if (strcasecmp (key, "LocalPort") == 0)
485 pe->flags |= PORT_COLLECT_LOCAL;
486 else
487 pe->flags |= PORT_COLLECT_REMOTE;
488 }
489 else
490 {
491 return (-1);
492 }
494 return (0);
495 } /* int conn_config */
497 #if KERNEL_LINUX
498 static int conn_init (void)
499 {
500 if (port_list_head == NULL)
501 port_collect_listening = 1;
503 return (0);
504 } /* int conn_init */
506 static int conn_read (void)
507 {
508 int errors_num = 0;
510 conn_reset_port_entry ();
512 if (conn_read_file ("/proc/net/tcp") != 0)
513 errors_num++;
514 if (conn_read_file ("/proc/net/tcp6") != 0)
515 errors_num++;
517 if (errors_num < 2)
518 {
519 conn_submit_all ();
520 }
521 else
522 {
523 ERROR ("tcpconns plugin: Neither /proc/net/tcp nor /proc/net/tcp6 "
524 "coult be read.");
525 return (-1);
526 }
528 return (0);
529 } /* int conn_read */
530 /* #endif KERNEL_LINUX */
532 #elif HAVE_SYSCTLBYNAME
533 static int conn_read (void)
534 {
535 int status;
536 char *buffer;
537 size_t buffer_len;;
539 struct xinpgen *in_orig;
540 struct xinpgen *in_ptr;
542 conn_reset_port_entry ();
544 buffer_len = 0;
545 status = sysctlbyname ("net.inet.tcp.pcblist", NULL, &buffer_len, 0, 0);
546 if (status < 0)
547 {
548 ERROR ("tcpconns plugin: sysctlbyname failed.");
549 return (-1);
550 }
552 buffer = (char *) malloc (buffer_len);
553 if (buffer == NULL)
554 {
555 ERROR ("tcpconns plugin: malloc failed.");
556 return (-1);
557 }
559 status = sysctlbyname ("net.inet.tcp.pcblist", buffer, &buffer_len, 0, 0);
560 if (status < 0)
561 {
562 ERROR ("tcpconns plugin: sysctlbyname failed.");
563 sfree (buffer);
564 return (-1);
565 }
567 if (buffer_len <= sizeof (struct xinpgen))
568 {
569 ERROR ("tcpconns plugin: (buffer_len <= sizeof (struct xinpgen))");
570 sfree (buffer);
571 return (-1);
572 }
574 in_orig = (struct xinpgen *) buffer;
575 for (in_ptr = (struct xinpgen *) (((char *) in_orig) + in_orig->xig_len);
576 in_ptr->xig_len > sizeof (struct xinpgen);
577 in_ptr = (struct xinpgen *) (((char *) in_ptr) + in_ptr->xig_len))
578 {
579 struct tcpcb *tp = &((struct xtcpcb *) in_ptr)->xt_tp;
580 struct inpcb *inp = &((struct xtcpcb *) in_ptr)->xt_inp;
581 struct xsocket *so = &((struct xtcpcb *) in_ptr)->xt_socket;
583 /* Ignore non-TCP sockets */
584 if (so->xso_protocol != IPPROTO_TCP)
585 continue;
587 /* Ignore PCBs which were freed during copyout. */
588 if (inp->inp_gencnt > in_orig->xig_gen)
589 continue;
591 if (((inp->inp_vflag & INP_IPV4) == 0)
592 && ((inp->inp_vflag & INP_IPV6) == 0))
593 continue;
595 conn_handle_ports (ntohs (inp->inp_lport), ntohs (inp->inp_fport),
596 tp->t_state);
597 } /* for (in_ptr) */
599 in_orig = NULL;
600 in_ptr = NULL;
601 sfree (buffer);
603 conn_submit_all ();
605 return (0);
606 } /* int conn_read */
607 /* #endif HAVE_SYSCTLBYNAME */
609 #elif HAVE_LIBKVM_NLIST
610 static int kread (u_long addr, void *buf, int size)
611 {
612 int status;
614 status = kvm_read (kvmd, addr, buf, size);
615 if (status != size)
616 {
617 ERROR ("tcpconns plugin: kvm_read failed (got %i, expected %i): %s\n",
618 status, size, kvm_geterr (kvmd));
619 return (-1);
620 }
621 return (0);
622 } /* int kread */
624 static int conn_init (void)
625 {
626 char buf[_POSIX2_LINE_MAX];
627 struct nlist nl[] =
628 {
629 #define N_TCBTABLE 0
630 { "_tcbtable" },
631 { "" }
632 };
633 int status;
635 kvmd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, buf);
636 if (kvmd == NULL)
637 {
638 ERROR ("tcpconns plugin: kvm_openfiles failed: %s", buf);
639 return (-1);
640 }
642 status = kvm_nlist (kvmd, nl);
643 if (status < 0)
644 {
645 ERROR ("tcpconns plugin: kvm_nlist failed with status %i.", status);
646 return (-1);
647 }
649 if (nl[N_TCBTABLE].n_type == 0)
650 {
651 ERROR ("tcpconns plugin: Error looking up kernel's namelist: "
652 "N_TCBTABLE is invalid.");
653 return (-1);
654 }
656 inpcbtable_off = (u_long) nl[N_TCBTABLE].n_value;
657 inpcbtable_ptr = (struct inpcbtable *) nl[N_TCBTABLE].n_value;
659 return (0);
660 } /* int conn_init */
662 static int conn_read (void)
663 {
664 struct inpcbtable table;
665 struct inpcb *head;
666 struct inpcb *next;
667 struct inpcb inpcb;
668 struct tcpcb tcpcb;
669 int status;
671 conn_reset_port_entry ();
673 /* Read the pcbtable from the kernel */
674 status = kread (inpcbtable_off, &table, sizeof (table));
675 if (status != 0)
676 return (-1);
678 /* Get the `head' pcb */
679 head = (struct inpcb *) &(inpcbtable_ptr->inpt_queue);
680 /* Get the first pcb */
681 next = (struct inpcb *)CIRCLEQ_FIRST (&table.inpt_queue);
683 while (next != head)
684 {
685 /* Read the pcb pointed to by `next' into `inpcb' */
686 kread ((u_long) next, &inpcb, sizeof (inpcb));
688 /* Advance `next' */
689 next = (struct inpcb *)CIRCLEQ_NEXT (&inpcb, inp_queue);
691 /* Ignore sockets, that are not connected. */
692 #ifdef __NetBSD__
693 if (inpcb.inp_af == AF_INET6)
694 continue; /* XXX see netbsd/src/usr.bin/netstat/inet6.c */
695 #else
696 if (!(inpcb.inp_flags & INP_IPV6)
697 && (inet_lnaof(inpcb.inp_laddr) == INADDR_ANY))
698 continue;
699 if ((inpcb.inp_flags & INP_IPV6)
700 && IN6_IS_ADDR_UNSPECIFIED (&inpcb.inp_laddr6))
701 continue;
702 #endif
704 kread ((u_long) inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
705 conn_handle_ports (ntohs(inpcb.inp_lport), ntohs(inpcb.inp_fport), tcpcb.t_state);
706 } /* while (next != head) */
708 conn_submit_all ();
710 return (0);
711 }
712 #endif /* HAVE_LIBKVM_NLIST */
714 void module_register (void)
715 {
716 plugin_register_config ("tcpconns", conn_config,
717 config_keys, config_keys_num);
718 #if KERNEL_LINUX
719 plugin_register_init ("tcpconns", conn_init);
720 #elif HAVE_SYSCTLBYNAME
721 /* no initialization */
722 #elif HAVE_LIBKVM_NLIST
723 plugin_register_init ("tcpconns", conn_init);
724 #endif
725 plugin_register_read ("tcpconns", conn_read);
726 } /* void module_register */
728 /*
729 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker :
730 */