2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Begemot: bsnmp/snmp_mibII/mibII_tcp.c,v 1.7 2005/05/23 09:03:42 brandt_h Exp $
34 #include "mibII_oid.h"
35 #include <sys/socketvar.h>
36 #include <netinet/in_pcb.h>
37 #include <netinet/tcp.h>
38 #include <netinet/tcp_var.h>
39 #include <netinet/tcp_timer.h>
40 #include <netinet/tcp_fsm.h>
47 static uint64_t tcp_tick;
48 static struct tcpstat tcpstat;
49 static struct xinpgen *xinpgen;
50 static size_t xinpgen_len;
51 static u_int tcp_count;
52 static u_int tcp_total;
55 static struct tcp_index *tcpoids;
58 tcp_compare(const void *p1, const void *p2)
60 const struct tcp_index *t1 = p1;
61 const struct tcp_index *t2 = p2;
63 return (asn_compare_oid(&t1->index, &t2->index));
72 struct tcp_index *oid;
75 len = sizeof(tcpstat);
76 if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len, NULL, 0) == -1) {
77 syslog(LOG_ERR, "net.inet.tcp.stats: %m");
80 if (len != sizeof(tcpstat)) {
81 syslog(LOG_ERR, "net.inet.tcp.stats: wrong size");
86 if (sysctlbyname("net.inet.tcp.pcblist", NULL, &len, NULL, 0) == -1) {
87 syslog(LOG_ERR, "net.inet.tcp.pcblist: %m");
90 if (len > xinpgen_len) {
91 if ((ptr = realloc(xinpgen, len)) == NULL) {
92 syslog(LOG_ERR, "%zu: %m", len);
98 if (sysctlbyname("net.inet.tcp.pcblist", xinpgen, &len, NULL, 0) == -1) {
99 syslog(LOG_ERR, "net.inet.tcp.pcblist: %m");
103 tcp_tick = get_ticks();
107 for (ptr = (struct xinpgen *)(void *)((char *)xinpgen + xinpgen->xig_len);
108 ptr->xig_len > sizeof(struct xinpgen);
109 ptr = (struct xinpgen *)(void *)((char *)ptr + ptr->xig_len)) {
110 tp = (struct xtcpcb *)ptr;
111 if (tp->xt_inp.inp_gencnt > xinpgen->xig_gen ||
112 (tp->xt_inp.inp_vflag & (INP_IPV4|INP_IPV6)) == 0)
115 if (tp->xt_inp.inp_vflag & INP_IPV4)
118 if (tp->xt_tp.t_state == TCPS_ESTABLISHED ||
119 tp->xt_tp.t_state == TCPS_CLOSE_WAIT)
123 if (oidnum < tcp_total) {
124 oid = realloc(tcpoids, tcp_total * sizeof(tcpoids[0]));
135 for (ptr = (struct xinpgen *)(void *)((char *)xinpgen + xinpgen->xig_len);
136 ptr->xig_len > sizeof(struct xinpgen);
137 ptr = (struct xinpgen *)(void *)((char *)ptr + ptr->xig_len)) {
138 tp = (struct xtcpcb *)ptr;
139 if (tp->xt_inp.inp_gencnt > xinpgen->xig_gen ||
140 (tp->xt_inp.inp_vflag & INP_IPV4) == 0)
144 inaddr = ntohl(tp->xt_inp.inp_laddr.s_addr);
145 oid->index.subs[0] = (inaddr >> 24) & 0xff;
146 oid->index.subs[1] = (inaddr >> 16) & 0xff;
147 oid->index.subs[2] = (inaddr >> 8) & 0xff;
148 oid->index.subs[3] = (inaddr >> 0) & 0xff;
149 oid->index.subs[4] = ntohs(tp->xt_inp.inp_lport);
150 inaddr = ntohl(tp->xt_inp.inp_faddr.s_addr);
151 oid->index.subs[5] = (inaddr >> 24) & 0xff;
152 oid->index.subs[6] = (inaddr >> 16) & 0xff;
153 oid->index.subs[7] = (inaddr >> 8) & 0xff;
154 oid->index.subs[8] = (inaddr >> 0) & 0xff;
155 oid->index.subs[9] = ntohs(tp->xt_inp.inp_fport);
159 qsort(tcpoids, tcp_total, sizeof(tcpoids[0]), tcp_compare);
168 op_tcp(struct snmp_context *ctx __unused, struct snmp_value *value,
169 u_int sub, u_int iidx __unused, enum snmp_op op)
173 case SNMP_OP_GETNEXT:
180 return (SNMP_ERR_NOT_WRITEABLE);
182 case SNMP_OP_ROLLBACK:
187 if (tcp_tick < this_tick)
188 if (fetch_tcp() == -1)
189 return (SNMP_ERR_GENERR);
191 switch (value->var.subs[sub - 1]) {
193 case LEAF_tcpRtoAlgorithm:
194 value->v.integer = 4; /* Van Jacobson */
197 #define hz clockinfo.hz
200 value->v.integer = 1000 * TCPTV_MIN / hz;
204 value->v.integer = 1000 * TCPTV_REXMTMAX / hz;
208 case LEAF_tcpMaxConn:
209 value->v.integer = -1;
212 case LEAF_tcpActiveOpens:
213 value->v.uint32 = tcpstat.tcps_connattempt;
216 case LEAF_tcpPassiveOpens:
217 value->v.uint32 = tcpstat.tcps_accepts;
220 case LEAF_tcpAttemptFails:
221 value->v.uint32 = tcpstat.tcps_conndrops;
224 case LEAF_tcpEstabResets:
225 value->v.uint32 = tcpstat.tcps_drops;
228 case LEAF_tcpCurrEstab:
229 value->v.uint32 = tcp_count;
233 value->v.uint32 = tcpstat.tcps_rcvtotal;
236 case LEAF_tcpOutSegs:
237 value->v.uint32 = tcpstat.tcps_sndtotal -
238 tcpstat.tcps_sndrexmitpack;
241 case LEAF_tcpRetransSegs:
242 value->v.uint32 = tcpstat.tcps_sndrexmitpack;
246 value->v.uint32 = tcpstat.tcps_rcvbadsum +
247 tcpstat.tcps_rcvbadoff +
248 tcpstat.tcps_rcvshort;
251 return (SNMP_ERR_NOERROR);
255 op_tcpconn(struct snmp_context *ctx __unused, struct snmp_value *value,
256 u_int sub, u_int iidx __unused, enum snmp_op op)
260 if (tcp_tick < this_tick)
261 if (fetch_tcp() == -1)
262 return (SNMP_ERR_GENERR);
266 case SNMP_OP_GETNEXT:
267 for (i = 0; i < tcp_total; i++)
268 if (index_compare(&value->var, sub, &tcpoids[i].index) < 0)
271 return (SNMP_ERR_NOSUCHNAME);
272 index_append(&value->var, sub, &tcpoids[i].index);
276 for (i = 0; i < tcp_total; i++)
277 if (index_compare(&value->var, sub, &tcpoids[i].index) == 0)
280 return (SNMP_ERR_NOSUCHNAME);
284 return (SNMP_ERR_NOT_WRITEABLE);
286 case SNMP_OP_ROLLBACK:
292 switch (value->var.subs[sub - 1]) {
294 case LEAF_tcpConnState:
295 switch (tcpoids[i].tp->xt_tp.t_state) {
298 value->v.integer = 1;
301 value->v.integer = 2;
304 value->v.integer = 3;
306 case TCPS_SYN_RECEIVED:
307 value->v.integer = 4;
309 case TCPS_ESTABLISHED:
310 value->v.integer = 5;
312 case TCPS_CLOSE_WAIT:
313 value->v.integer = 8;
315 case TCPS_FIN_WAIT_1:
316 value->v.integer = 6;
319 value->v.integer = 10;
322 value->v.integer = 9;
324 case TCPS_FIN_WAIT_2:
325 value->v.integer = 7;
328 value->v.integer = 11;
331 value->v.integer = 0;
336 case LEAF_tcpConnLocalAddress:
337 value->v.ipaddress[0] = tcpoids[i].index.subs[0];
338 value->v.ipaddress[1] = tcpoids[i].index.subs[1];
339 value->v.ipaddress[2] = tcpoids[i].index.subs[2];
340 value->v.ipaddress[3] = tcpoids[i].index.subs[3];
343 case LEAF_tcpConnLocalPort:
344 value->v.integer = tcpoids[i].index.subs[4];
347 case LEAF_tcpConnRemAddress:
348 value->v.ipaddress[0] = tcpoids[i].index.subs[5];
349 value->v.ipaddress[1] = tcpoids[i].index.subs[6];
350 value->v.ipaddress[2] = tcpoids[i].index.subs[7];
351 value->v.ipaddress[3] = tcpoids[i].index.subs[8];
354 case LEAF_tcpConnRemPort:
355 value->v.integer = tcpoids[i].index.subs[9];
358 return (SNMP_ERR_NOERROR);