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 && !KERNEL_AIX
69 # error "No applicable input method."
70 #endif
72 #if KERNEL_LINUX
73 # include <asm/types.h>
74 /* sys/socket.h is necessary to compile when using netlink on older systems. */
75 # include <sys/socket.h>
76 # include <linux/netlink.h>
77 # include <linux/inet_diag.h>
78 # include <sys/socket.h>
79 # include <arpa/inet.h>
80 /* #endif KERNEL_LINUX */
82 #elif HAVE_SYSCTLBYNAME
83 # include <sys/socketvar.h>
84 # include <sys/sysctl.h>
86 /* Some includes needed for compiling on FreeBSD */
87 #include <sys/time.h>
88 #if HAVE_SYS_TYPES_H
89 # include <sys/types.h>
90 #endif
91 #if HAVE_SYS_SOCKET_H
92 # include <sys/socket.h>
93 #endif
94 #if HAVE_NET_IF_H
95 # include <net/if.h>
96 #endif
98 # include <net/route.h>
99 # include <netinet/in.h>
100 # include <netinet/in_systm.h>
101 # include <netinet/ip.h>
102 # include <netinet/ip6.h>
103 # include <netinet/in_pcb.h>
104 # include <netinet/ip_var.h>
105 # include <netinet/tcp.h>
106 # include <netinet/tcpip.h>
107 # include <netinet/tcp_seq.h>
108 # include <netinet/tcp_var.h>
109 /* #endif HAVE_SYSCTLBYNAME */
111 /* This is for OpenBSD and NetBSD. */
112 #elif HAVE_LIBKVM_NLIST
113 # include <sys/queue.h>
114 # include <sys/socket.h>
115 # include <net/route.h>
116 # include <netinet/in.h>
117 # include <netinet/in_systm.h>
118 # include <netinet/ip.h>
119 # include <netinet/ip_var.h>
120 # include <netinet/in_pcb.h>
121 # include <netinet/tcp.h>
122 # include <netinet/tcp_timer.h>
123 # include <netinet/tcp_var.h>
124 # include <netdb.h>
125 # include <arpa/inet.h>
126 # if !defined(HAVE_BSD_NLIST_H) || !HAVE_BSD_NLIST_H
127 # include <nlist.h>
128 # else /* HAVE_BSD_NLIST_H */
129 # include <bsd/nlist.h>
130 # endif
131 # include <kvm.h>
132 /* #endif HAVE_LIBKVM_NLIST */
134 #elif KERNEL_AIX
135 # include <arpa/inet.h>
136 # include <sys/socketvar.h>
137 #endif /* KERNEL_AIX */
139 #if KERNEL_LINUX
140 struct nlreq {
141 struct nlmsghdr nlh;
142 struct inet_diag_req r;
143 };
145 static const char *tcp_state[] =
146 {
147 "", /* 0 */
148 "ESTABLISHED",
149 "SYN_SENT",
150 "SYN_RECV",
151 "FIN_WAIT1",
152 "FIN_WAIT2",
153 "TIME_WAIT",
154 "CLOSED",
155 "CLOSE_WAIT",
156 "LAST_ACK",
157 "LISTEN", /* 10 */
158 "CLOSING"
159 };
161 # define TCP_STATE_LISTEN 10
162 # define TCP_STATE_MIN 1
163 # define TCP_STATE_MAX 11
164 /* #endif KERNEL_LINUX */
166 #elif HAVE_SYSCTLBYNAME
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 # define TCP_STATE_LISTEN 1
183 # define TCP_STATE_MIN 0
184 # define TCP_STATE_MAX 10
185 /* #endif HAVE_SYSCTLBYNAME */
187 #elif HAVE_LIBKVM_NLIST
188 static const char *tcp_state[] =
189 {
190 "CLOSED",
191 "LISTEN",
192 "SYN_SENT",
193 "SYN_RECV",
194 "ESTABLISHED",
195 "CLOSE_WAIT",
196 "FIN_WAIT1",
197 "CLOSING",
198 "LAST_ACK",
199 "FIN_WAIT2",
200 "TIME_WAIT"
201 };
203 static kvm_t *kvmd;
204 static u_long inpcbtable_off = 0;
205 struct inpcbtable *inpcbtable_ptr = NULL;
207 # define TCP_STATE_LISTEN 1
208 # define TCP_STATE_MIN 1
209 # define TCP_STATE_MAX 10
210 /* #endif HAVE_LIBKVM_NLIST */
212 #elif KERNEL_AIX
213 static const char *tcp_state[] =
214 {
215 "CLOSED",
216 "LISTEN",
217 "SYN_SENT",
218 "SYN_RCVD",
219 "ESTABLISHED",
220 "CLOSE_WAIT",
221 "FIN_WAIT_1",
222 "CLOSING",
223 "LAST_ACK",
224 "FIN_WAIT_2",
225 "TIME_WAIT"
226 };
228 # define TCP_STATE_LISTEN 1
229 # define TCP_STATE_MIN 0
230 # define TCP_STATE_MAX 10
232 struct netinfo_conn {
233 uint32_t unknow1[2];
234 uint16_t dstport;
235 uint16_t unknow2;
236 struct in6_addr dstaddr;
237 uint16_t srcport;
238 uint16_t unknow3;
239 struct in6_addr srcaddr;
240 uint32_t unknow4[36];
241 uint16_t tcp_state;
242 uint16_t unknow5[7];
243 };
245 struct netinfo_header {
246 unsigned int proto;
247 unsigned int size;
248 };
250 # define NETINFO_TCP 3
251 extern int netinfo (int proto, void *data, int *size, int n);
252 #endif /* KERNEL_AIX */
254 #define PORT_COLLECT_LOCAL 0x01
255 #define PORT_COLLECT_REMOTE 0x02
256 #define PORT_IS_LISTENING 0x04
258 typedef struct port_entry_s
259 {
260 uint16_t port;
261 uint16_t flags;
262 uint32_t count_local[TCP_STATE_MAX + 1];
263 uint32_t count_remote[TCP_STATE_MAX + 1];
264 struct port_entry_s *next;
265 } port_entry_t;
267 static const char *config_keys[] =
268 {
269 "ListeningPorts",
270 "LocalPort",
271 "RemotePort"
272 };
273 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
275 static int port_collect_listening = 0;
276 static port_entry_t *port_list_head = NULL;
278 static void conn_submit_port_entry (port_entry_t *pe)
279 {
280 value_t values[1];
281 value_list_t vl = VALUE_LIST_INIT;
282 int i;
284 vl.values = values;
285 vl.values_len = 1;
286 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
287 sstrncpy (vl.plugin, "tcpconns", sizeof (vl.plugin));
288 sstrncpy (vl.type, "tcp_connections", sizeof (vl.type));
290 if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
291 || (pe->flags & PORT_COLLECT_LOCAL))
292 {
293 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
294 "%"PRIu16"-local", pe->port);
296 for (i = 1; i <= TCP_STATE_MAX; i++)
297 {
298 vl.values[0].gauge = pe->count_local[i];
300 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
302 plugin_dispatch_values (&vl);
303 }
304 }
306 if (pe->flags & PORT_COLLECT_REMOTE)
307 {
308 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
309 "%"PRIu16"-remote", pe->port);
311 for (i = 1; i <= TCP_STATE_MAX; i++)
312 {
313 vl.values[0].gauge = pe->count_remote[i];
315 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
317 plugin_dispatch_values (&vl);
318 }
319 }
320 } /* void conn_submit */
322 static void conn_submit_all (void)
323 {
324 port_entry_t *pe;
326 for (pe = port_list_head; pe != NULL; pe = pe->next)
327 conn_submit_port_entry (pe);
328 } /* void conn_submit_all */
330 static port_entry_t *conn_get_port_entry (uint16_t port, int create)
331 {
332 port_entry_t *ret;
334 ret = port_list_head;
335 while (ret != NULL)
336 {
337 if (ret->port == port)
338 break;
339 ret = ret->next;
340 }
342 if ((ret == NULL) && (create != 0))
343 {
344 ret = (port_entry_t *) malloc (sizeof (port_entry_t));
345 if (ret == NULL)
346 return (NULL);
347 memset (ret, '\0', sizeof (port_entry_t));
349 ret->port = port;
350 ret->next = port_list_head;
351 port_list_head = ret;
352 }
354 return (ret);
355 } /* port_entry_t *conn_get_port_entry */
357 /* Removes ports that were added automatically due to the `ListeningPorts'
358 * setting but which are no longer listening. */
359 static void conn_reset_port_entry (void)
360 {
361 port_entry_t *prev = NULL;
362 port_entry_t *pe = port_list_head;
364 while (pe != NULL)
365 {
366 /* If this entry was created while reading the files (ant not when handling
367 * the configuration) remove it now. */
368 if ((pe->flags & (PORT_COLLECT_LOCAL
369 | PORT_COLLECT_REMOTE
370 | PORT_IS_LISTENING)) == 0)
371 {
372 port_entry_t *next = pe->next;
374 DEBUG ("tcpconns plugin: Removing temporary entry "
375 "for listening port %"PRIu16, pe->port);
377 if (prev == NULL)
378 port_list_head = next;
379 else
380 prev->next = next;
382 sfree (pe);
383 pe = next;
385 continue;
386 }
388 memset (pe->count_local, '\0', sizeof (pe->count_local));
389 memset (pe->count_remote, '\0', sizeof (pe->count_remote));
390 pe->flags &= ~PORT_IS_LISTENING;
392 pe = pe->next;
393 }
394 } /* void conn_reset_port_entry */
396 static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t state)
397 {
398 port_entry_t *pe = NULL;
400 if ((state > TCP_STATE_MAX)
401 #if TCP_STATE_MIN > 0
402 || (state < TCP_STATE_MIN)
403 #endif
404 )
405 {
406 NOTICE ("tcpconns plugin: Ignoring connection with "
407 "unknown state 0x%02"PRIx8".", state);
408 return (-1);
409 }
411 /* Listening sockets */
412 if ((state == TCP_STATE_LISTEN) && (port_collect_listening != 0))
413 {
414 pe = conn_get_port_entry (port_local, 1 /* create */);
415 if (pe != NULL)
416 pe->flags |= PORT_IS_LISTENING;
417 }
419 DEBUG ("tcpconns plugin: Connection %"PRIu16" <-> %"PRIu16" (%s)",
420 port_local, port_remote, tcp_state[state]);
422 pe = conn_get_port_entry (port_local, 0 /* no create */);
423 if (pe != NULL)
424 pe->count_local[state]++;
426 pe = conn_get_port_entry (port_remote, 0 /* no create */);
427 if (pe != NULL)
428 pe->count_remote[state]++;
430 return (0);
431 } /* int conn_handle_ports */
433 #if KERNEL_LINUX
434 static int conn_read_netlink (void)
435 {
436 int fd;
437 struct sockaddr_nl nladdr;
438 struct nlreq req;
439 struct msghdr msg;
440 struct iovec iov;
441 struct inet_diag_msg *r;
442 char buf[8192];
443 static uint32_t sequence_number = 0;
445 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG);
446 if (fd < 0)
447 return (1);
449 memset(&nladdr, 0, sizeof(nladdr));
450 nladdr.nl_family = AF_NETLINK;
452 memset(&req, 0, sizeof(req));
453 req.nlh.nlmsg_len = sizeof(req);
454 req.nlh.nlmsg_type = TCPDIAG_GETSOCK;
455 /* NLM_F_ROOT: return the complete table instead of a single entry.
456 * NLM_F_MATCH: return all entries matching criteria (not implemented)
457 * NLM_F_REQUEST: must be set on all request messages */
458 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
459 req.nlh.nlmsg_pid = 0;
460 /* The sequence_number is used to track our messages. Since netlink is not
461 * reliable, we don't want to end up with a corrupt or incomplete old
462 * message in case the system is/was out of memory. */
463 req.nlh.nlmsg_seq = ++sequence_number;
464 req.r.idiag_family = AF_INET;
465 req.r.idiag_states = 0xfff;
466 req.r.idiag_ext = 0;
468 memset(&iov, 0, sizeof(iov));
469 iov.iov_base = &req;
470 iov.iov_len = sizeof(req);
472 memset(&msg, 0, sizeof(msg));
473 msg.msg_name = (void*)&nladdr;
474 msg.msg_namelen = sizeof(nladdr);
475 msg.msg_iov = &iov;
476 msg.msg_iovlen = 1;
478 if (sendmsg (fd, &msg, 0) < 0)
479 {
480 close (fd);
481 return (1);
482 }
484 iov.iov_base = buf;
485 iov.iov_len = sizeof(buf);
487 while (1)
488 {
489 int status;
490 struct nlmsghdr *h;
492 memset(&msg, 0, sizeof(msg));
493 msg.msg_name = (void*)&nladdr;
494 msg.msg_namelen = sizeof(nladdr);
495 msg.msg_iov = &iov;
496 msg.msg_iovlen = 1;
498 status = recvmsg(fd, (void *) &msg, /* flags = */ 0);
499 if (status < 0)
500 {
501 if (errno == EINTR)
502 continue;
503 close (fd);
504 return (1);
505 }
507 if (status == 0)
508 {
509 close (fd);
510 return (0);
511 }
513 h = (struct nlmsghdr*)buf;
514 while (NLMSG_OK(h, status))
515 {
516 if (h->nlmsg_seq == sequence_number)
517 {
518 if (h->nlmsg_type == NLMSG_DONE)
519 {
520 close (fd);
521 return (0);
522 }
523 else if (h->nlmsg_type == NLMSG_ERROR)
524 {
525 close (fd);
526 return (1);
527 }
529 r = NLMSG_DATA(h);
531 /* This code does not (need to) distinguish between IPv4 and IPv6. */
532 conn_handle_ports (ntohs(r->id.idiag_sport),
533 ntohs(r->id.idiag_dport),
534 r->idiag_state);
535 }
536 h = NLMSG_NEXT(h, status);
537 }
538 }
540 /* Not reached because the while() loop above handles the exit condition. */
541 return (0);
542 } /* int conn_read_netlink */
544 static int conn_handle_line (char *buffer)
545 {
546 char *fields[32];
547 int fields_len;
549 char *endptr;
551 char *port_local_str;
552 char *port_remote_str;
553 uint16_t port_local;
554 uint16_t port_remote;
556 uint8_t state;
558 int buffer_len = strlen (buffer);
560 while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
561 buffer[--buffer_len] = '\0';
562 if (buffer_len <= 0)
563 return (-1);
565 fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
566 if (fields_len < 12)
567 {
568 DEBUG ("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
569 return (-1);
570 }
572 port_local_str = strchr (fields[1], ':');
573 port_remote_str = strchr (fields[2], ':');
575 if ((port_local_str == NULL) || (port_remote_str == NULL))
576 return (-1);
577 port_local_str++;
578 port_remote_str++;
579 if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
580 return (-1);
582 endptr = NULL;
583 port_local = (uint16_t) strtol (port_local_str, &endptr, 16);
584 if ((endptr == NULL) || (*endptr != '\0'))
585 return (-1);
587 endptr = NULL;
588 port_remote = (uint16_t) strtol (port_remote_str, &endptr, 16);
589 if ((endptr == NULL) || (*endptr != '\0'))
590 return (-1);
592 endptr = NULL;
593 state = (uint8_t) strtol (fields[3], &endptr, 16);
594 if ((endptr == NULL) || (*endptr != '\0'))
595 return (-1);
597 return (conn_handle_ports (port_local, port_remote, state));
598 } /* int conn_handle_line */
600 static int conn_read_file (const char *file)
601 {
602 FILE *fh;
603 char buffer[1024];
605 fh = fopen (file, "r");
606 if (fh == NULL)
607 return (-1);
609 while (fgets (buffer, sizeof (buffer), fh) != NULL)
610 {
611 conn_handle_line (buffer);
612 } /* while (fgets) */
614 fclose (fh);
616 return (0);
617 } /* int conn_read_file */
618 /* #endif KERNEL_LINUX */
620 #elif HAVE_SYSCTLBYNAME
621 /* #endif HAVE_SYSCTLBYNAME */
623 #elif HAVE_LIBKVM_NLIST
624 #endif /* HAVE_LIBKVM_NLIST */
626 static int conn_config (const char *key, const char *value)
627 {
628 if (strcasecmp (key, "ListeningPorts") == 0)
629 {
630 if (IS_TRUE (value))
631 port_collect_listening = 1;
632 else
633 port_collect_listening = 0;
634 }
635 else if ((strcasecmp (key, "LocalPort") == 0)
636 || (strcasecmp (key, "RemotePort") == 0))
637 {
638 port_entry_t *pe;
639 int port = atoi (value);
641 if ((port < 1) || (port > 65535))
642 {
643 ERROR ("tcpconns plugin: Invalid port: %i", port);
644 return (1);
645 }
647 pe = conn_get_port_entry ((uint16_t) port, 1 /* create */);
648 if (pe == NULL)
649 {
650 ERROR ("tcpconns plugin: conn_get_port_entry failed.");
651 return (1);
652 }
654 if (strcasecmp (key, "LocalPort") == 0)
655 pe->flags |= PORT_COLLECT_LOCAL;
656 else
657 pe->flags |= PORT_COLLECT_REMOTE;
658 }
659 else
660 {
661 return (-1);
662 }
664 return (0);
665 } /* int conn_config */
667 #if KERNEL_LINUX
668 static int conn_init (void)
669 {
670 if (port_list_head == NULL)
671 port_collect_listening = 1;
673 return (0);
674 } /* int conn_init */
676 static int conn_read (void)
677 {
678 int errors_num = 0;
680 conn_reset_port_entry ();
682 /* Try to use netlink for getting this data, it is _much_ faster on systems
683 * with a large amount of connections. */
684 if (conn_read_netlink () != 0)
685 {
686 if (conn_read_file ("/proc/net/tcp") != 0)
687 errors_num++;
688 if (conn_read_file ("/proc/net/tcp6") != 0)
689 errors_num++;
690 }
692 if (errors_num < 2)
693 {
694 conn_submit_all ();
695 }
696 else
697 {
698 ERROR ("tcpconns plugin: Neither /proc/net/tcp nor /proc/net/tcp6 "
699 "could be read.");
700 return (-1);
701 }
703 return (0);
704 } /* int conn_read */
705 /* #endif KERNEL_LINUX */
707 #elif HAVE_SYSCTLBYNAME
708 static int conn_read (void)
709 {
710 int status;
711 char *buffer;
712 size_t buffer_len;;
714 struct xinpgen *in_orig;
715 struct xinpgen *in_ptr;
717 conn_reset_port_entry ();
719 buffer_len = 0;
720 status = sysctlbyname ("net.inet.tcp.pcblist", NULL, &buffer_len, 0, 0);
721 if (status < 0)
722 {
723 ERROR ("tcpconns plugin: sysctlbyname failed.");
724 return (-1);
725 }
727 buffer = (char *) malloc (buffer_len);
728 if (buffer == NULL)
729 {
730 ERROR ("tcpconns plugin: malloc failed.");
731 return (-1);
732 }
734 status = sysctlbyname ("net.inet.tcp.pcblist", buffer, &buffer_len, 0, 0);
735 if (status < 0)
736 {
737 ERROR ("tcpconns plugin: sysctlbyname failed.");
738 sfree (buffer);
739 return (-1);
740 }
742 if (buffer_len <= sizeof (struct xinpgen))
743 {
744 ERROR ("tcpconns plugin: (buffer_len <= sizeof (struct xinpgen))");
745 sfree (buffer);
746 return (-1);
747 }
749 in_orig = (struct xinpgen *) buffer;
750 for (in_ptr = (struct xinpgen *) (((char *) in_orig) + in_orig->xig_len);
751 in_ptr->xig_len > sizeof (struct xinpgen);
752 in_ptr = (struct xinpgen *) (((char *) in_ptr) + in_ptr->xig_len))
753 {
754 struct tcpcb *tp = &((struct xtcpcb *) in_ptr)->xt_tp;
755 struct inpcb *inp = &((struct xtcpcb *) in_ptr)->xt_inp;
756 struct xsocket *so = &((struct xtcpcb *) in_ptr)->xt_socket;
758 /* Ignore non-TCP sockets */
759 if (so->xso_protocol != IPPROTO_TCP)
760 continue;
762 /* Ignore PCBs which were freed during copyout. */
763 if (inp->inp_gencnt > in_orig->xig_gen)
764 continue;
766 if (((inp->inp_vflag & INP_IPV4) == 0)
767 && ((inp->inp_vflag & INP_IPV6) == 0))
768 continue;
770 conn_handle_ports (ntohs (inp->inp_lport), ntohs (inp->inp_fport),
771 tp->t_state);
772 } /* for (in_ptr) */
774 in_orig = NULL;
775 in_ptr = NULL;
776 sfree (buffer);
778 conn_submit_all ();
780 return (0);
781 } /* int conn_read */
782 /* #endif HAVE_SYSCTLBYNAME */
784 #elif HAVE_LIBKVM_NLIST
785 static int kread (u_long addr, void *buf, int size)
786 {
787 int status;
789 status = kvm_read (kvmd, addr, buf, size);
790 if (status != size)
791 {
792 ERROR ("tcpconns plugin: kvm_read failed (got %i, expected %i): %s\n",
793 status, size, kvm_geterr (kvmd));
794 return (-1);
795 }
796 return (0);
797 } /* int kread */
799 static int conn_init (void)
800 {
801 char buf[_POSIX2_LINE_MAX];
802 struct nlist nl[] =
803 {
804 #define N_TCBTABLE 0
805 { "_tcbtable" },
806 { "" }
807 };
808 int status;
810 kvmd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, buf);
811 if (kvmd == NULL)
812 {
813 ERROR ("tcpconns plugin: kvm_openfiles failed: %s", buf);
814 return (-1);
815 }
817 status = kvm_nlist (kvmd, nl);
818 if (status < 0)
819 {
820 ERROR ("tcpconns plugin: kvm_nlist failed with status %i.", status);
821 return (-1);
822 }
824 if (nl[N_TCBTABLE].n_type == 0)
825 {
826 ERROR ("tcpconns plugin: Error looking up kernel's namelist: "
827 "N_TCBTABLE is invalid.");
828 return (-1);
829 }
831 inpcbtable_off = (u_long) nl[N_TCBTABLE].n_value;
832 inpcbtable_ptr = (struct inpcbtable *) nl[N_TCBTABLE].n_value;
834 return (0);
835 } /* int conn_init */
837 static int conn_read (void)
838 {
839 struct inpcbtable table;
840 struct inpcb *head;
841 struct inpcb *next;
842 struct inpcb inpcb;
843 struct tcpcb tcpcb;
844 int status;
846 conn_reset_port_entry ();
848 /* Read the pcbtable from the kernel */
849 status = kread (inpcbtable_off, &table, sizeof (table));
850 if (status != 0)
851 return (-1);
853 /* Get the `head' pcb */
854 head = (struct inpcb *) &(inpcbtable_ptr->inpt_queue);
855 /* Get the first pcb */
856 next = (struct inpcb *)CIRCLEQ_FIRST (&table.inpt_queue);
858 while (next != head)
859 {
860 /* Read the pcb pointed to by `next' into `inpcb' */
861 kread ((u_long) next, &inpcb, sizeof (inpcb));
863 /* Advance `next' */
864 next = (struct inpcb *)CIRCLEQ_NEXT (&inpcb, inp_queue);
866 /* Ignore sockets, that are not connected. */
867 #ifdef __NetBSD__
868 if (inpcb.inp_af == AF_INET6)
869 continue; /* XXX see netbsd/src/usr.bin/netstat/inet6.c */
870 #else
871 if (!(inpcb.inp_flags & INP_IPV6)
872 && (inet_lnaof(inpcb.inp_laddr) == INADDR_ANY))
873 continue;
874 if ((inpcb.inp_flags & INP_IPV6)
875 && IN6_IS_ADDR_UNSPECIFIED (&inpcb.inp_laddr6))
876 continue;
877 #endif
879 kread ((u_long) inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
880 conn_handle_ports (ntohs(inpcb.inp_lport), ntohs(inpcb.inp_fport), tcpcb.t_state);
881 } /* while (next != head) */
883 conn_submit_all ();
885 return (0);
886 }
887 /* #endif HAVE_LIBKVM_NLIST */
889 #elif KERNEL_AIX
891 static int conn_read (void)
892 {
893 int size;
894 int i;
895 int nconn;
896 void *data;
897 struct netinfo_header *header;
898 struct netinfo_conn *conn;
900 conn_reset_port_entry ();
902 size = netinfo(NETINFO_TCP, 0, 0, 0);
903 if (size < 0)
904 {
905 ERROR ("tcpconns plugin: netinfo failed return: %i", size);
906 return (-1);
907 }
909 if (size == 0)
910 return (0);
912 if ((size - sizeof (struct netinfo_header)) % sizeof (struct netinfo_conn))
913 {
914 ERROR ("tcpconns plugin: invalid buffer size");
915 return (-1);
916 }
918 data = malloc(size);
919 if (data == NULL)
920 {
921 ERROR ("tcpconns plugin: malloc failed");
922 return (-1);
923 }
925 if (netinfo(NETINFO_TCP, data, &size, 0) < 0)
926 {
927 ERROR ("tcpconns plugin: netinfo failed");
928 free(data);
929 return (-1);
930 }
932 header = (struct netinfo_header *)data;
933 nconn = header->size;
934 conn = (struct netinfo_conn *)(data + sizeof(struct netinfo_header));
936 for (i=0; i < nconn; conn++, i++)
937 {
938 conn_handle_ports (conn->srcport, conn->dstport, conn->tcp_state);
939 }
941 free(data);
943 conn_submit_all ();
945 return (0);
946 }
947 #endif /* KERNEL_AIX */
949 void module_register (void)
950 {
951 plugin_register_config ("tcpconns", conn_config,
952 config_keys, config_keys_num);
953 #if KERNEL_LINUX
954 plugin_register_init ("tcpconns", conn_init);
955 #elif HAVE_SYSCTLBYNAME
956 /* no initialization */
957 #elif HAVE_LIBKVM_NLIST
958 plugin_register_init ("tcpconns", conn_init);
959 #elif KERNEL_AIX
960 /* no initialization */
961 #endif
962 plugin_register_read ("tcpconns", conn_read);
963 } /* void module_register */
965 /*
966 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker :
967 */