0bcdf89ef8f7cc7e535f428cd855703733007684
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 static const char *tcp_state[] =
141 {
142 "", /* 0 */
143 "ESTABLISHED",
144 "SYN_SENT",
145 "SYN_RECV",
146 "FIN_WAIT1",
147 "FIN_WAIT2",
148 "TIME_WAIT",
149 "CLOSED",
150 "CLOSE_WAIT",
151 "LAST_ACK",
152 "LISTEN", /* 10 */
153 "CLOSING"
154 };
156 # define TCP_STATE_LISTEN 10
157 # define TCP_STATE_MIN 1
158 # define TCP_STATE_MAX 11
159 /* #endif KERNEL_LINUX */
161 #elif HAVE_SYSCTLBYNAME
162 static const char *tcp_state[] =
163 {
164 "CLOSED",
165 "LISTEN",
166 "SYN_SENT",
167 "SYN_RECV",
168 "ESTABLISHED",
169 "CLOSE_WAIT",
170 "FIN_WAIT1",
171 "CLOSING",
172 "LAST_ACK",
173 "FIN_WAIT2",
174 "TIME_WAIT"
175 };
177 # define TCP_STATE_LISTEN 1
178 # define TCP_STATE_MIN 0
179 # define TCP_STATE_MAX 10
180 /* #endif HAVE_SYSCTLBYNAME */
182 #elif HAVE_LIBKVM_NLIST
183 static const char *tcp_state[] =
184 {
185 "CLOSED",
186 "LISTEN",
187 "SYN_SENT",
188 "SYN_RECV",
189 "ESTABLISHED",
190 "CLOSE_WAIT",
191 "FIN_WAIT1",
192 "CLOSING",
193 "LAST_ACK",
194 "FIN_WAIT2",
195 "TIME_WAIT"
196 };
198 static kvm_t *kvmd;
199 static u_long inpcbtable_off = 0;
200 struct inpcbtable *inpcbtable_ptr = NULL;
202 # define TCP_STATE_LISTEN 1
203 # define TCP_STATE_MIN 1
204 # define TCP_STATE_MAX 10
205 /* #endif HAVE_LIBKVM_NLIST */
207 #elif KERNEL_AIX
208 static const char *tcp_state[] =
209 {
210 "CLOSED",
211 "LISTEN",
212 "SYN_SENT",
213 "SYN_RCVD",
214 "ESTABLISHED",
215 "CLOSE_WAIT",
216 "FIN_WAIT_1",
217 "CLOSING",
218 "LAST_ACK",
219 "FIN_WAIT_2",
220 "TIME_WAIT"
221 };
223 # define TCP_STATE_LISTEN 1
224 # define TCP_STATE_MIN 0
225 # define TCP_STATE_MAX 10
227 struct netinfo_conn {
228 uint32_t unknow1[2];
229 uint16_t dstport;
230 uint16_t unknow2;
231 struct in6_addr dstaddr;
232 uint16_t srcport;
233 uint16_t unknow3;
234 struct in6_addr srcaddr;
235 uint32_t unknow4[36];
236 uint16_t tcp_state;
237 uint16_t unknow5[7];
238 };
240 struct netinfo_header {
241 unsigned int proto;
242 unsigned int size;
243 };
245 # define NETINFO_TCP 3
246 extern int netinfo (int proto, void *data, int *size, int n);
247 #endif /* KERNEL_AIX */
249 #define PORT_COLLECT_LOCAL 0x01
250 #define PORT_COLLECT_REMOTE 0x02
251 #define PORT_IS_LISTENING 0x04
253 typedef struct port_entry_s
254 {
255 uint16_t port;
256 uint16_t flags;
257 uint32_t count_local[TCP_STATE_MAX + 1];
258 uint32_t count_remote[TCP_STATE_MAX + 1];
259 struct port_entry_s *next;
260 } port_entry_t;
262 static const char *config_keys[] =
263 {
264 "ListeningPorts",
265 "LocalPort",
266 "RemotePort"
267 };
268 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
270 static int port_collect_listening = 0;
271 static port_entry_t *port_list_head = NULL;
273 static void conn_submit_port_entry (port_entry_t *pe)
274 {
275 value_t values[1];
276 value_list_t vl = VALUE_LIST_INIT;
277 int i;
279 vl.values = values;
280 vl.values_len = 1;
281 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
282 sstrncpy (vl.plugin, "tcpconns", sizeof (vl.plugin));
283 sstrncpy (vl.type, "tcp_connections", sizeof (vl.type));
285 if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
286 || (pe->flags & PORT_COLLECT_LOCAL))
287 {
288 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
289 "%"PRIu16"-local", pe->port);
291 for (i = 1; i <= TCP_STATE_MAX; i++)
292 {
293 vl.values[0].gauge = pe->count_local[i];
295 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
297 plugin_dispatch_values (&vl);
298 }
299 }
301 if (pe->flags & PORT_COLLECT_REMOTE)
302 {
303 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
304 "%"PRIu16"-remote", pe->port);
306 for (i = 1; i <= TCP_STATE_MAX; i++)
307 {
308 vl.values[0].gauge = pe->count_remote[i];
310 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
312 plugin_dispatch_values (&vl);
313 }
314 }
315 } /* void conn_submit */
317 static void conn_submit_all (void)
318 {
319 port_entry_t *pe;
321 for (pe = port_list_head; pe != NULL; pe = pe->next)
322 conn_submit_port_entry (pe);
323 } /* void conn_submit_all */
325 static port_entry_t *conn_get_port_entry (uint16_t port, int create)
326 {
327 port_entry_t *ret;
329 ret = port_list_head;
330 while (ret != NULL)
331 {
332 if (ret->port == port)
333 break;
334 ret = ret->next;
335 }
337 if ((ret == NULL) && (create != 0))
338 {
339 ret = (port_entry_t *) malloc (sizeof (port_entry_t));
340 if (ret == NULL)
341 return (NULL);
342 memset (ret, '\0', sizeof (port_entry_t));
344 ret->port = port;
345 ret->next = port_list_head;
346 port_list_head = ret;
347 }
349 return (ret);
350 } /* port_entry_t *conn_get_port_entry */
352 /* Removes ports that were added automatically due to the `ListeningPorts'
353 * setting but which are no longer listening. */
354 static void conn_reset_port_entry (void)
355 {
356 port_entry_t *prev = NULL;
357 port_entry_t *pe = port_list_head;
359 while (pe != NULL)
360 {
361 /* If this entry was created while reading the files (ant not when handling
362 * the configuration) remove it now. */
363 if ((pe->flags & (PORT_COLLECT_LOCAL
364 | PORT_COLLECT_REMOTE
365 | PORT_IS_LISTENING)) == 0)
366 {
367 port_entry_t *next = pe->next;
369 DEBUG ("tcpconns plugin: Removing temporary entry "
370 "for listening port %"PRIu16, pe->port);
372 if (prev == NULL)
373 port_list_head = next;
374 else
375 prev->next = next;
377 sfree (pe);
378 pe = next;
380 continue;
381 }
383 memset (pe->count_local, '\0', sizeof (pe->count_local));
384 memset (pe->count_remote, '\0', sizeof (pe->count_remote));
385 pe->flags &= ~PORT_IS_LISTENING;
387 pe = pe->next;
388 }
389 } /* void conn_reset_port_entry */
391 static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t state)
392 {
393 port_entry_t *pe = NULL;
395 if ((state > TCP_STATE_MAX)
396 #if TCP_STATE_MIN > 0
397 || (state < TCP_STATE_MIN)
398 #endif
399 )
400 {
401 NOTICE ("tcpconns plugin: Ignoring connection with "
402 "unknown state 0x%02"PRIx8".", state);
403 return (-1);
404 }
406 /* Listening sockets */
407 if ((state == TCP_STATE_LISTEN) && (port_collect_listening != 0))
408 {
409 pe = conn_get_port_entry (port_local, 1 /* create */);
410 if (pe != NULL)
411 pe->flags |= PORT_IS_LISTENING;
412 }
414 DEBUG ("tcpconns plugin: Connection %"PRIu16" <-> %"PRIu16" (%s)",
415 port_local, port_remote, tcp_state[state]);
417 pe = conn_get_port_entry (port_local, 0 /* no create */);
418 if (pe != NULL)
419 pe->count_local[state]++;
421 pe = conn_get_port_entry (port_remote, 0 /* no create */);
422 if (pe != NULL)
423 pe->count_remote[state]++;
425 return (0);
426 } /* int conn_handle_ports */
428 #if KERNEL_LINUX
429 static int conn_read_netlink (void)
430 {
431 int fd;
432 struct sockaddr_nl nladdr;
433 struct {
434 struct nlmsghdr nlh;
435 struct inet_diag_req r;
436 } req;
437 struct msghdr msg;
438 struct iovec iov;
439 struct inet_diag_msg *r;
440 char buf[8192];
441 static uint32_t sequence_number = 0;
443 if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0)
444 return (0);
446 memset(&nladdr, 0, sizeof(nladdr));
447 nladdr.nl_family = AF_NETLINK;
449 req.nlh.nlmsg_len = sizeof(req);
450 req.nlh.nlmsg_type = TCPDIAG_GETSOCK;
451 /* NLM_F_ROOT: return the complete table instead of a single entry. */
452 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
453 req.nlh.nlmsg_pid = 0;
454 /* The sequence_number is used to track our messages. Since netlink is not
455 * reliable, we don't want to end up with a corrupt or incomplete old
456 * message in case the system is/was out of memory. */
457 req.nlh.nlmsg_seq = ++sequence_number;
458 memset(&req.r, 0, sizeof(req.r));
459 req.r.idiag_family = AF_INET;
460 req.r.idiag_states = 0xfff;
461 req.r.idiag_ext = 0;
463 iov.iov_base = &req;
464 iov.iov_len = sizeof(req);
466 msg = (struct msghdr) {
467 .msg_name = (void*)&nladdr,
468 .msg_namelen = sizeof(nladdr),
469 .msg_iov = &iov,
470 .msg_iovlen = 1,
471 };
473 if (sendmsg (fd, &msg, 0) < 0)
474 {
475 close (fd);
476 return (0);
477 }
479 iov.iov_base = buf;
480 iov.iov_len = sizeof(buf);
482 while (1)
483 {
484 int status;
485 struct nlmsghdr *h;
487 msg = (struct msghdr) {
488 (void*)&nladdr, sizeof(nladdr),
489 &iov, 1,
490 NULL, 0,
491 0
492 };
494 status = recvmsg(fd, &msg, 0);
495 if (status < 0)
496 {
497 if (errno == EINTR)
498 continue;
499 close (fd);
500 return (0);
501 }
503 if (status == 0)
504 {
505 close (fd);
506 return (1);
507 }
509 h = (struct nlmsghdr*)buf;
510 while (NLMSG_OK(h, status))
511 {
512 if (h->nlmsg_seq == sequence_number)
513 {
514 if (h->nlmsg_type == NLMSG_DONE)
515 {
516 close (fd);
517 return (1);
518 }
519 else if (h->nlmsg_type == NLMSG_ERROR)
520 {
521 close (fd);
522 return (0);
523 }
525 r = NLMSG_DATA(h);
527 /* This code does not (need to) distinguish between IPv4 and IPv6. */
528 conn_handle_ports (ntohs(r->id.idiag_sport),
529 ntohs(r->id.idiag_dport),
530 r->idiag_state);
531 }
532 h = NLMSG_NEXT(h, status);
533 }
534 }
536 return (1);
537 } /* int conn_read_netlink */
539 static int conn_handle_line (char *buffer)
540 {
541 char *fields[32];
542 int fields_len;
544 char *endptr;
546 char *port_local_str;
547 char *port_remote_str;
548 uint16_t port_local;
549 uint16_t port_remote;
551 uint8_t state;
553 int buffer_len = strlen (buffer);
555 while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
556 buffer[--buffer_len] = '\0';
557 if (buffer_len <= 0)
558 return (-1);
560 fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
561 if (fields_len < 12)
562 {
563 DEBUG ("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
564 return (-1);
565 }
567 port_local_str = strchr (fields[1], ':');
568 port_remote_str = strchr (fields[2], ':');
570 if ((port_local_str == NULL) || (port_remote_str == NULL))
571 return (-1);
572 port_local_str++;
573 port_remote_str++;
574 if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
575 return (-1);
577 endptr = NULL;
578 port_local = (uint16_t) strtol (port_local_str, &endptr, 16);
579 if ((endptr == NULL) || (*endptr != '\0'))
580 return (-1);
582 endptr = NULL;
583 port_remote = (uint16_t) strtol (port_remote_str, &endptr, 16);
584 if ((endptr == NULL) || (*endptr != '\0'))
585 return (-1);
587 endptr = NULL;
588 state = (uint8_t) strtol (fields[3], &endptr, 16);
589 if ((endptr == NULL) || (*endptr != '\0'))
590 return (-1);
592 return (conn_handle_ports (port_local, port_remote, state));
593 } /* int conn_handle_line */
595 static int conn_read_file (const char *file)
596 {
597 FILE *fh;
598 char buffer[1024];
600 fh = fopen (file, "r");
601 if (fh == NULL)
602 return (-1);
604 while (fgets (buffer, sizeof (buffer), fh) != NULL)
605 {
606 conn_handle_line (buffer);
607 } /* while (fgets) */
609 fclose (fh);
611 return (0);
612 } /* int conn_read_file */
613 /* #endif KERNEL_LINUX */
615 #elif HAVE_SYSCTLBYNAME
616 /* #endif HAVE_SYSCTLBYNAME */
618 #elif HAVE_LIBKVM_NLIST
619 #endif /* HAVE_LIBKVM_NLIST */
621 static int conn_config (const char *key, const char *value)
622 {
623 if (strcasecmp (key, "ListeningPorts") == 0)
624 {
625 if (IS_TRUE (value))
626 port_collect_listening = 1;
627 else
628 port_collect_listening = 0;
629 }
630 else if ((strcasecmp (key, "LocalPort") == 0)
631 || (strcasecmp (key, "RemotePort") == 0))
632 {
633 port_entry_t *pe;
634 int port = atoi (value);
636 if ((port < 1) || (port > 65535))
637 {
638 ERROR ("tcpconns plugin: Invalid port: %i", port);
639 return (1);
640 }
642 pe = conn_get_port_entry ((uint16_t) port, 1 /* create */);
643 if (pe == NULL)
644 {
645 ERROR ("tcpconns plugin: conn_get_port_entry failed.");
646 return (1);
647 }
649 if (strcasecmp (key, "LocalPort") == 0)
650 pe->flags |= PORT_COLLECT_LOCAL;
651 else
652 pe->flags |= PORT_COLLECT_REMOTE;
653 }
654 else
655 {
656 return (-1);
657 }
659 return (0);
660 } /* int conn_config */
662 #if KERNEL_LINUX
663 static int conn_init (void)
664 {
665 if (port_list_head == NULL)
666 port_collect_listening = 1;
668 return (0);
669 } /* int conn_init */
671 static int conn_read (void)
672 {
673 int errors_num = 0;
675 conn_reset_port_entry ();
677 /* Try to use netlink for getting this data, it is _much_ faster on systems
678 * with a large amount of connections. */
679 if (!conn_read_netlink ())
680 {
681 if (conn_read_file ("/proc/net/tcp") != 0)
682 errors_num++;
683 if (conn_read_file ("/proc/net/tcp6") != 0)
684 errors_num++;
685 }
687 if (errors_num < 2)
688 {
689 conn_submit_all ();
690 }
691 else
692 {
693 ERROR ("tcpconns plugin: Neither /proc/net/tcp nor /proc/net/tcp6 "
694 "could be read.");
695 return (-1);
696 }
698 return (0);
699 } /* int conn_read */
700 /* #endif KERNEL_LINUX */
702 #elif HAVE_SYSCTLBYNAME
703 static int conn_read (void)
704 {
705 int status;
706 char *buffer;
707 size_t buffer_len;;
709 struct xinpgen *in_orig;
710 struct xinpgen *in_ptr;
712 conn_reset_port_entry ();
714 buffer_len = 0;
715 status = sysctlbyname ("net.inet.tcp.pcblist", NULL, &buffer_len, 0, 0);
716 if (status < 0)
717 {
718 ERROR ("tcpconns plugin: sysctlbyname failed.");
719 return (-1);
720 }
722 buffer = (char *) malloc (buffer_len);
723 if (buffer == NULL)
724 {
725 ERROR ("tcpconns plugin: malloc failed.");
726 return (-1);
727 }
729 status = sysctlbyname ("net.inet.tcp.pcblist", buffer, &buffer_len, 0, 0);
730 if (status < 0)
731 {
732 ERROR ("tcpconns plugin: sysctlbyname failed.");
733 sfree (buffer);
734 return (-1);
735 }
737 if (buffer_len <= sizeof (struct xinpgen))
738 {
739 ERROR ("tcpconns plugin: (buffer_len <= sizeof (struct xinpgen))");
740 sfree (buffer);
741 return (-1);
742 }
744 in_orig = (struct xinpgen *) buffer;
745 for (in_ptr = (struct xinpgen *) (((char *) in_orig) + in_orig->xig_len);
746 in_ptr->xig_len > sizeof (struct xinpgen);
747 in_ptr = (struct xinpgen *) (((char *) in_ptr) + in_ptr->xig_len))
748 {
749 struct tcpcb *tp = &((struct xtcpcb *) in_ptr)->xt_tp;
750 struct inpcb *inp = &((struct xtcpcb *) in_ptr)->xt_inp;
751 struct xsocket *so = &((struct xtcpcb *) in_ptr)->xt_socket;
753 /* Ignore non-TCP sockets */
754 if (so->xso_protocol != IPPROTO_TCP)
755 continue;
757 /* Ignore PCBs which were freed during copyout. */
758 if (inp->inp_gencnt > in_orig->xig_gen)
759 continue;
761 if (((inp->inp_vflag & INP_IPV4) == 0)
762 && ((inp->inp_vflag & INP_IPV6) == 0))
763 continue;
765 conn_handle_ports (ntohs (inp->inp_lport), ntohs (inp->inp_fport),
766 tp->t_state);
767 } /* for (in_ptr) */
769 in_orig = NULL;
770 in_ptr = NULL;
771 sfree (buffer);
773 conn_submit_all ();
775 return (0);
776 } /* int conn_read */
777 /* #endif HAVE_SYSCTLBYNAME */
779 #elif HAVE_LIBKVM_NLIST
780 static int kread (u_long addr, void *buf, int size)
781 {
782 int status;
784 status = kvm_read (kvmd, addr, buf, size);
785 if (status != size)
786 {
787 ERROR ("tcpconns plugin: kvm_read failed (got %i, expected %i): %s\n",
788 status, size, kvm_geterr (kvmd));
789 return (-1);
790 }
791 return (0);
792 } /* int kread */
794 static int conn_init (void)
795 {
796 char buf[_POSIX2_LINE_MAX];
797 struct nlist nl[] =
798 {
799 #define N_TCBTABLE 0
800 { "_tcbtable" },
801 { "" }
802 };
803 int status;
805 kvmd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, buf);
806 if (kvmd == NULL)
807 {
808 ERROR ("tcpconns plugin: kvm_openfiles failed: %s", buf);
809 return (-1);
810 }
812 status = kvm_nlist (kvmd, nl);
813 if (status < 0)
814 {
815 ERROR ("tcpconns plugin: kvm_nlist failed with status %i.", status);
816 return (-1);
817 }
819 if (nl[N_TCBTABLE].n_type == 0)
820 {
821 ERROR ("tcpconns plugin: Error looking up kernel's namelist: "
822 "N_TCBTABLE is invalid.");
823 return (-1);
824 }
826 inpcbtable_off = (u_long) nl[N_TCBTABLE].n_value;
827 inpcbtable_ptr = (struct inpcbtable *) nl[N_TCBTABLE].n_value;
829 return (0);
830 } /* int conn_init */
832 static int conn_read (void)
833 {
834 struct inpcbtable table;
835 struct inpcb *head;
836 struct inpcb *next;
837 struct inpcb inpcb;
838 struct tcpcb tcpcb;
839 int status;
841 conn_reset_port_entry ();
843 /* Read the pcbtable from the kernel */
844 status = kread (inpcbtable_off, &table, sizeof (table));
845 if (status != 0)
846 return (-1);
848 /* Get the `head' pcb */
849 head = (struct inpcb *) &(inpcbtable_ptr->inpt_queue);
850 /* Get the first pcb */
851 next = (struct inpcb *)CIRCLEQ_FIRST (&table.inpt_queue);
853 while (next != head)
854 {
855 /* Read the pcb pointed to by `next' into `inpcb' */
856 kread ((u_long) next, &inpcb, sizeof (inpcb));
858 /* Advance `next' */
859 next = (struct inpcb *)CIRCLEQ_NEXT (&inpcb, inp_queue);
861 /* Ignore sockets, that are not connected. */
862 #ifdef __NetBSD__
863 if (inpcb.inp_af == AF_INET6)
864 continue; /* XXX see netbsd/src/usr.bin/netstat/inet6.c */
865 #else
866 if (!(inpcb.inp_flags & INP_IPV6)
867 && (inet_lnaof(inpcb.inp_laddr) == INADDR_ANY))
868 continue;
869 if ((inpcb.inp_flags & INP_IPV6)
870 && IN6_IS_ADDR_UNSPECIFIED (&inpcb.inp_laddr6))
871 continue;
872 #endif
874 kread ((u_long) inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
875 conn_handle_ports (ntohs(inpcb.inp_lport), ntohs(inpcb.inp_fport), tcpcb.t_state);
876 } /* while (next != head) */
878 conn_submit_all ();
880 return (0);
881 }
882 /* #endif HAVE_LIBKVM_NLIST */
884 #elif KERNEL_AIX
886 static int conn_read (void)
887 {
888 int size;
889 int i;
890 int nconn;
891 void *data;
892 struct netinfo_header *header;
893 struct netinfo_conn *conn;
895 conn_reset_port_entry ();
897 size = netinfo(NETINFO_TCP, 0, 0, 0);
898 if (size < 0)
899 {
900 ERROR ("tcpconns plugin: netinfo failed return: %i", size);
901 return (-1);
902 }
904 if (size == 0)
905 return (0);
907 if ((size - sizeof (struct netinfo_header)) % sizeof (struct netinfo_conn))
908 {
909 ERROR ("tcpconns plugin: invalid buffer size");
910 return (-1);
911 }
913 data = malloc(size);
914 if (data == NULL)
915 {
916 ERROR ("tcpconns plugin: malloc failed");
917 return (-1);
918 }
920 if (netinfo(NETINFO_TCP, data, &size, 0) < 0)
921 {
922 ERROR ("tcpconns plugin: netinfo failed");
923 free(data);
924 return (-1);
925 }
927 header = (struct netinfo_header *)data;
928 nconn = header->size;
929 conn = (struct netinfo_conn *)(data + sizeof(struct netinfo_header));
931 for (i=0; i < nconn; conn++, i++)
932 {
933 conn_handle_ports (conn->srcport, conn->dstport, conn->tcp_state);
934 }
936 free(data);
938 conn_submit_all ();
940 return (0);
941 }
942 #endif /* KERNEL_AIX */
944 void module_register (void)
945 {
946 plugin_register_config ("tcpconns", conn_config,
947 config_keys, config_keys_num);
948 #if KERNEL_LINUX
949 plugin_register_init ("tcpconns", conn_init);
950 #elif HAVE_SYSCTLBYNAME
951 /* no initialization */
952 #elif HAVE_LIBKVM_NLIST
953 plugin_register_init ("tcpconns", conn_init);
954 #elif KERNEL_AIX
955 /* no initialization */
956 #endif
957 plugin_register_read ("tcpconns", conn_read);
958 } /* void module_register */
960 /*
961 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker :
962 */