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_udp.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/udp.h>
38 #include <netinet/ip_var.h>
39 #include <netinet/udp_var.h>
46 static uint64_t udp_tick;
47 static struct udpstat udpstat;
48 static struct xinpgen *xinpgen;
49 static size_t xinpgen_len;
50 static u_int udp_total;
53 static struct udp_index *udpoids;
56 udp_compare(const void *p1, const void *p2)
58 const struct udp_index *t1 = p1;
59 const struct udp_index *t2 = p2;
61 return (asn_compare_oid(&t1->index, &t2->index));
70 struct udp_index *oid;
73 len = sizeof(udpstat);
74 if (sysctlbyname("net.inet.udp.stats", &udpstat, &len, NULL, 0) == -1) {
75 syslog(LOG_ERR, "net.inet.udp.stats: %m");
78 if (len != sizeof(udpstat)) {
79 syslog(LOG_ERR, "net.inet.udp.stats: wrong size");
83 udp_tick = get_ticks();
86 if (sysctlbyname("net.inet.udp.pcblist", NULL, &len, NULL, 0) == -1) {
87 syslog(LOG_ERR, "net.inet.udp.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.udp.pcblist", xinpgen, &len, NULL, 0) == -1) {
99 syslog(LOG_ERR, "net.inet.udp.pcblist: %m");
104 for (ptr = (struct xinpgen *)(void *)((char *)xinpgen + xinpgen->xig_len);
105 ptr->xig_len > sizeof(struct xinpgen);
106 ptr = (struct xinpgen *)(void *)((char *)ptr + ptr->xig_len)) {
107 inp = (struct xinpcb *)ptr;
108 if (inp->xi_inp.inp_gencnt > xinpgen->xig_gen ||
109 (inp->xi_inp.inp_vflag & INP_IPV4) == 0)
115 if (oidnum < udp_total) {
116 oid = realloc(udpoids, udp_total * sizeof(udpoids[0]));
127 for (ptr = (struct xinpgen *)(void *)((char *)xinpgen + xinpgen->xig_len);
128 ptr->xig_len > sizeof(struct xinpgen);
129 ptr = (struct xinpgen *)(void *)((char *)ptr + ptr->xig_len)) {
130 inp = (struct xinpcb *)ptr;
131 if (inp->xi_inp.inp_gencnt > xinpgen->xig_gen ||
132 (inp->xi_inp.inp_vflag & INP_IPV4) == 0)
136 inaddr = ntohl(inp->xi_inp.inp_laddr.s_addr);
137 oid->index.subs[0] = (inaddr >> 24) & 0xff;
138 oid->index.subs[1] = (inaddr >> 16) & 0xff;
139 oid->index.subs[2] = (inaddr >> 8) & 0xff;
140 oid->index.subs[3] = (inaddr >> 0) & 0xff;
141 oid->index.subs[4] = ntohs(inp->xi_inp.inp_lport);
145 qsort(udpoids, udp_total, sizeof(udpoids[0]), udp_compare);
151 op_udp(struct snmp_context *ctx __unused, struct snmp_value *value,
152 u_int sub, u_int iidx __unused, enum snmp_op op)
156 case SNMP_OP_GETNEXT:
163 return (SNMP_ERR_NOT_WRITEABLE);
165 case SNMP_OP_ROLLBACK:
170 if (udp_tick < this_tick)
171 if (fetch_udp() == -1)
172 return (SNMP_ERR_GENERR);
174 switch (value->var.subs[sub - 1]) {
176 case LEAF_udpInDatagrams:
177 value->v.uint32 = udpstat.udps_ipackets;
180 case LEAF_udpNoPorts:
181 value->v.uint32 = udpstat.udps_noport +
182 udpstat.udps_noportbcast +
183 udpstat.udps_noportmcast;
186 case LEAF_udpInErrors:
187 value->v.uint32 = udpstat.udps_hdrops +
188 udpstat.udps_badsum +
189 udpstat.udps_badlen +
190 udpstat.udps_fullsock;
193 case LEAF_udpOutDatagrams:
194 value->v.uint32 = udpstat.udps_opackets;
197 return (SNMP_ERR_NOERROR);
201 op_udptable(struct snmp_context *ctx __unused, struct snmp_value *value,
202 u_int sub, u_int iidx __unused, enum snmp_op op)
206 if (udp_tick < this_tick)
207 if (fetch_udp() == -1)
208 return (SNMP_ERR_GENERR);
212 case SNMP_OP_GETNEXT:
213 for (i = 0; i < udp_total; i++)
214 if (index_compare(&value->var, sub, &udpoids[i].index) < 0)
217 return (SNMP_ERR_NOSUCHNAME);
218 index_append(&value->var, sub, &udpoids[i].index);
222 for (i = 0; i < udp_total; i++)
223 if (index_compare(&value->var, sub, &udpoids[i].index) == 0)
226 return (SNMP_ERR_NOSUCHNAME);
230 return (SNMP_ERR_NOT_WRITEABLE);
232 case SNMP_OP_ROLLBACK:
238 switch (value->var.subs[sub - 1]) {
240 case LEAF_udpLocalAddress:
241 value->v.ipaddress[0] = udpoids[i].index.subs[0];
242 value->v.ipaddress[1] = udpoids[i].index.subs[1];
243 value->v.ipaddress[2] = udpoids[i].index.subs[2];
244 value->v.ipaddress[3] = udpoids[i].index.subs[3];
247 case LEAF_udpLocalPort:
248 value->v.integer = udpoids[i].index.subs[4];
252 return (SNMP_ERR_NOERROR);