Debian Codegraph @

agent/mibgroup/mibII/udp.c

raw
1 /*
2 * UDP MIB group implementation - udp.c
3 *
4 */
5
6 #include <net-snmp/net-snmp-config.h>
7 #include "mibII_common.h"
8
9 #ifdef HAVE_NETINET_UDP_H
10 #include <netinet/udp.h>
11 #endif
12 #ifdef HAVE_NETINET_UDP_VAR_H
13 #include <netinet/udp_var.h>
14 #endif
15
16 #include <net-snmp/net-snmp-includes.h>
17 #include <net-snmp/agent/net-snmp-agent-includes.h>
18 #include <net-snmp/agent/auto_nlist.h>
19 #include <net-snmp/agent/sysORTable.h>
20
21 #include "util_funcs/MIB_STATS_CACHE_TIMEOUT.h"
22
23 #ifdef linux
24 #include "tcp.h"
25 #endif
26 #include "udp.h"
27 #include "udpTable.h"
28
29 #ifdef NETSNMP_CAN_USE_SYSCTL
30 #include <sys/sysctl.h>
31 #endif
32
33 #ifndef MIB_STATS_CACHE_TIMEOUT
34 #define MIB_STATS_CACHE_TIMEOUT 5
35 #endif
36 #ifndef UDP_STATS_CACHE_TIMEOUT
37 #define UDP_STATS_CACHE_TIMEOUT MIB_STATS_CACHE_TIMEOUT
38 #endif
39
40 #if defined(HAVE_LIBPERFSTAT_H) && (defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)) && !defined(FIRST_PROTOCOL)
41 #ifdef HAVE_SYS_PROTOSW_H
42 #include <sys/protosw.h>
43 #endif
44 #include <libperfstat.h>
45 #ifdef FIRST_PROTOCOL
46 perfstat_protocol_t ps_proto;
47 perfstat_id_t ps_name;
48 #define _USE_PERFSTAT_PROTOCOL 1
49 #endif
50 #endif
51
52 /*********************
53 *
54 * Kernel & interface information,
55 * and internal forward declarations
56 *
57 *********************/
58
59
60 /*********************
61 *
62 * Initialisation & common implementation functions
63 *
64 *********************/
65
66 /*
67 * Define the OID pointer to the top of the mib tree that we're
68 * registering underneath, and the OID for the MIB module
69 */
70 oid udp_oid[] = { SNMP_OID_MIB2, 7 };
71 oid udp_module_oid[] = { SNMP_OID_MIB2, 50 };
72
73 void
74 init_udp(void)
75 {
76 netsnmp_handler_registration *reginfo;
77 int rc;
78
79 /*
80 * register ourselves with the agent as a group of scalars...
81 */
82 DEBUGMSGTL(("mibII/udpScalar", "Initialising UDP scalar group\n"));
83 reginfo = netsnmp_create_handler_registration("udp", udp_handler,
84 udp_oid, OID_LENGTH(udp_oid), HANDLER_CAN_RONLY);
85 rc = netsnmp_register_scalar_group(reginfo, UDPINDATAGRAMS, UDPOUTDATAGRAMS);
86 if (rc != SNMPERR_SUCCESS)
87 return;
88
89 /*
90 * .... with a local cache
91 * (except for HP-UX 11, which extracts objects individually)
92 */
93 #ifndef hpux11
94 netsnmp_inject_handler( reginfo,
95 netsnmp_get_cache_handler(UDP_STATS_CACHE_TIMEOUT,
96 udp_load, udp_free,
97 udp_oid, OID_LENGTH(udp_oid)));
98 #endif
99
100 REGISTER_SYSOR_ENTRY(udp_module_oid,
101 "The MIB module for managing UDP implementations");
102
103 #if !defined(_USE_PERFSTAT_PROTOCOL)
104 #ifdef UDPSTAT_SYMBOL
105 auto_nlist(UDPSTAT_SYMBOL, 0, 0);
106 #endif
107 #ifdef UDB_SYMBOL
108 auto_nlist(UDB_SYMBOL, 0, 0);
109 #endif
110 #ifdef solaris2
111 init_kernel_sunos5();
112 #endif
113 #endif
114 }
115
116
117 /*********************
118 *
119 * System specific implementation functions
120 *
121 *********************/
122
123 #ifdef hpux11
124 #define UDP_STAT_STRUCTURE int
125 #endif
126
127 #ifdef linux
128 #define UDP_STAT_STRUCTURE struct udp_mib
129 #define USES_SNMP_DESIGNED_UDPSTAT
130 #undef UDPSTAT_SYMBOL
131 #endif
132
133 #ifdef solaris2
134 #define UDP_STAT_STRUCTURE mib2_udp_t
135 #define USES_SNMP_DESIGNED_UDPSTAT
136 #endif
137
138 #ifdef NETBSD_STATS_VIA_SYSCTL
139 #define UDP_STAT_STRUCTURE struct udp_mib
140 #define USES_SNMP_DESIGNED_UDPSTAT
141 #undef UDP_NSTATS
142 #endif
143
144 #ifdef HAVE_IPHLPAPI_H
145 #include <iphlpapi.h>
146 #define UDP_STAT_STRUCTURE MIB_UDPSTATS
147 #endif
148
149 #ifdef HAVE_SYS_TCPIPSTATS_H
150 #define UDP_STAT_STRUCTURE struct kna
151 #define USES_TRADITIONAL_UDPSTAT
152 #endif
153
154 #ifdef UDP_NSTATS
155 typedef struct udpstat {
156 uint64_t st[UDP_NSTATS];
157 };
158 #define UDP_STAT_STRUCTURE struct udpstat
159 #endif
160
161 #if !defined(UDP_STAT_STRUCTURE)
162 #define UDP_STAT_STRUCTURE struct udpstat
163 #define USES_TRADITIONAL_UDPSTAT
164 #endif
165
166 UDP_STAT_STRUCTURE udpstat;
167
168
169 /*********************
170 *
171 * System independent handler (mostly)
172 *
173 *********************/
174
175
176
177 int
178 udp_handler(netsnmp_mib_handler *handler,
179 netsnmp_handler_registration *reginfo,
180 netsnmp_agent_request_info *reqinfo,
181 netsnmp_request_info *requests)
182 {
183 netsnmp_request_info *request;
184 netsnmp_variable_list *requestvb;
185 long ret_value = -1;
186 oid subid;
187 int type = ASN_COUNTER;
188
189 /*
190 * The cached data should already have been loaded by the
191 * cache handler, higher up the handler chain.
192 * But just to be safe, check this and load it manually if necessary
193 */
194 #if defined(_USE_PERFSTAT_PROTOCOL)
195 udp_load(NULL, NULL);
196 #endif
197
198
199 /*
200 *
201 *
202 */
203 DEBUGMSGTL(("mibII/udpScalar", "Handler - mode %s\n",
204 se_find_label_in_slist("agent_mode", reqinfo->mode)));
205 switch (reqinfo->mode) {
206 case MODE_GET:
207 for (request=requests; request; request=request->next) {
208 requestvb = request->requestvb;
209 subid = requestvb->name[OID_LENGTH(udp_oid)]; /* XXX */
210 DEBUGMSGTL(( "mibII/udpScalar", "oid: "));
211 DEBUGMSGOID(("mibII/udpScalar", requestvb->name,
212 requestvb->name_length));
213 DEBUGMSG(( "mibII/udpScalar", "\n"));
214
215 switch (subid) {
216 #ifdef USES_SNMP_DESIGNED_UDPSTAT
217 case UDPINDATAGRAMS:
218 ret_value = udpstat.udpInDatagrams;
219 break;
220 case UDPNOPORTS:
221 #ifdef solaris2
222 ret_value = udp_load(NULL, (void *)UDPNOPORTS);
223 if (ret_value == -1) {
224 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
225 continue;
226 }
227 break;
228 #else
229 ret_value = udpstat.udpNoPorts;
230 break;
231 #endif
232 case UDPOUTDATAGRAMS:
233 ret_value = udpstat.udpOutDatagrams;
234 break;
235 case UDPINERRORS:
236 ret_value = udpstat.udpInErrors;
237 break;
238 #elif defined(USES_TRADITIONAL_UDPSTAT) && !defined(_USE_PERFSTAT_PROTOCOL)
239 #ifdef HAVE_SYS_TCPIPSTATS_H
240 /*
241 * This actually reads statistics for *all* the groups together,
242 * so we need to isolate the UDP-specific bits.
243 */
244 #define udpstat udpstat.udpstat
245 #endif
246 case UDPINDATAGRAMS:
247 #ifdef HAVE_STRUCT_UDPSTAT_UDPS_IPACKETS
248 ret_value = udpstat.udps_ipackets;
249 break;
250 #else
251 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
252 continue;
253 #endif
254
255 case UDPNOPORTS:
256 #ifdef HAVE_STRUCT_UDPSTAT_UDPS_NOPORT
257 ret_value = udpstat.udps_noport;
258 break;
259 #else
260 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
261 continue;
262 #endif
263
264 case UDPOUTDATAGRAMS:
265 #ifdef HAVE_STRUCT_UDPSTAT_UDPS_OPACKETS
266 ret_value = udpstat.udps_opackets;
267 break;
268 #else
269 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
270 continue;
271 #endif
272
273 case UDPINERRORS:
274 ret_value = udpstat.udps_hdrops + udpstat.udps_badsum +
275 #ifdef HAVE_STRUCT_UDPSTAT_UDPS_DISCARD
276 udpstat.udps_discard +
277 #endif
278 #ifdef HAVE_STRUCT_UDPSTAT_UDPS_FULLSOCK
279 udpstat.udps_fullsock +
280 #endif
281 udpstat.udps_badlen;
282 break;
283 #ifdef HAVE_SYS_TCPIPSTATS_H
284 #undef udpstat
285 #endif
286 #elif defined(hpux11)
287 case UDPINDATAGRAMS:
288 case UDPNOPORTS:
289 case UDPOUTDATAGRAMS:
290 case UDPINERRORS:
291 /*
292 * This is a bit of a hack, to shoehorn the HP-UX 11
293 * single-object retrieval approach into the caching
294 * architecture.
295 */
296 if (udp_load(NULL, (void*)subid) == -1 ) {
297 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
298 continue;
299 }
300 ret_value = udpstat;
301 break;
302 #elif defined(WIN32)
303 case UDPINDATAGRAMS:
304 ret_value = udpstat.dwInDatagrams;
305 break;
306 case UDPNOPORTS:
307 ret_value = udpstat.dwNoPorts;
308 break;
309 case UDPOUTDATAGRAMS:
310 ret_value = udpstat.dwOutDatagrams;
311 break;
312 case UDPINERRORS:
313 ret_value = udpstat.dwInErrors;
314 break;
315 #elif defined(_USE_PERFSTAT_PROTOCOL)
316 case UDPINDATAGRAMS:
317 ret_value = ps_proto.u.udp.ipackets;
318 break;
319 case UDPNOPORTS:
320 ret_value = ps_proto.u.udp.no_socket;
321 break;
322 case UDPOUTDATAGRAMS:
323 ret_value = ps_proto.u.udp.opackets;
324 break;
325 case UDPINERRORS:
326 ret_value = ps_proto.u.udp.ierrors;
327 break;
328 #endif /* USES_SNMP_DESIGNED_UDPSTAT */
329
330 }
331 snmp_set_var_typed_value(request->requestvb, (u_char)type,
332 (u_char *)&ret_value, sizeof(ret_value));
333 }
334 break;
335
336 case MODE_GETNEXT:
337 case MODE_GETBULK:
338 #ifndef NETSNMP_NO_WRITE_SUPPORT
339 case MODE_SET_RESERVE1:
340 case MODE_SET_RESERVE2:
341 case MODE_SET_ACTION:
342 case MODE_SET_COMMIT:
343 case MODE_SET_FREE:
344 case MODE_SET_UNDO:
345 snmp_log(LOG_WARNING, "mibII/udp: Unsupported mode (%d)\n",
346 reqinfo->mode);
347 break;
348 #endif /* !NETSNMP_NO_WRITE_SUPPORT */
349 default:
350 snmp_log(LOG_WARNING, "mibII/udp: Unrecognised mode (%d)\n",
351 reqinfo->mode);
352 break;
353 }
354
355 return SNMP_ERR_NOERROR;
356 }
357
358
359 /*********************
360 *
361 * Internal implementation functions
362 *
363 *********************/
364
365 #ifdef hpux11
366 int
367 udp_load(netsnmp_cache *cache, void *vmagic)
368 {
369 int fd;
370 struct nmparms p;
371 unsigned int ulen;
372 int ret;
373 int magic = (int) vmagic;
374
375 if ((fd = open_mib("/dev/ip", O_RDONLY, 0, NM_ASYNC_OFF)) < 0) {
376 DEBUGMSGTL(("mibII/udpScalar", "Failed to load UDP object %d (hpux11)\n", magic));
377 return (-1); /* error */
378 }
379
380 switch (magic) {
381 case UDPINDATAGRAMS:
382 p.objid = ID_udpInDatagrams;
383 break;
384 case UDPNOPORTS:
385 p.objid = ID_udpNoPorts;
386 break;
387 case UDPOUTDATAGRAMS:
388 p.objid = ID_udpOutDatagrams;
389 break;
390 case UDPINERRORS:
391 p.objid = ID_udpInErrors;
392 break;
393 default:
394 udpstat = 0;
395 close_mib(fd);
396 return -1;
397 }
398
399 p.buffer = (void *)&udpstat;
400 ulen = sizeof(UDP_STAT_STRUCTURE);
401 p.len = &ulen;
402 ret = get_mib_info(fd, &p);
403 close_mib(fd);
404
405 DEBUGMSGTL(("mibII/udpScalar", "%s UDP object %d (hpux11)\n",
406 (ret < 0 ? "Failed to load" : "Loaded"), magic));
407 return (ret); /* 0: ok, < 0: error */
408 }
409 #elif defined(linux)
410 int
411 udp_load(netsnmp_cache *cache, void *vmagic)
412 {
413 long ret_value = -1;
414
415 ret_value = linux_read_udp_stat(&udpstat);
416
417 if ( ret_value < 0 ) {
418 DEBUGMSGTL(("mibII/udpScalar", "Failed to load UDP scalar Group (linux)\n"));
419 } else {
420 DEBUGMSGTL(("mibII/udpScalar", "Loaded UDP scalar Group (linux)\n"));
421 }
422 return ret_value;
423 }
424 #elif defined(solaris2)
425 int
426 udp_load(netsnmp_cache *cache, void *vmagic)
427 {
428 long ret_value = -1;
429 int magic = (int)vmagic;
430 mib2_ip_t ipstat;
431
432 /*
433 * udpNoPorts is actually implemented as part of the MIB_IP group
434 * so we need to retrieve this independently
435 */
436 if (magic == UDPNOPORTS) {
437 if (getMibstat
438 (MIB_IP, &ipstat, sizeof(mib2_ip_t), GET_FIRST,
439 &Get_everything, NULL) < 0) {
440 DEBUGMSGTL(("mibII/udpScalar", "Failed to load UDP object %d (solaris)\n", magic));
441 return -1;
442 } else {
443 DEBUGMSGTL(("mibII/udpScalar", "Loaded UDP object %d (solaris)\n", magic));
444 return ipstat.udpNoPorts;
445 }
446 }
447
448 /*
449 * Otherwise, retrieve the whole of the MIB_UDP group (and cache it)
450 */
451 ret_value = getMibstat(MIB_UDP, &udpstat, sizeof(mib2_udp_t),
452 GET_FIRST, &Get_everything, NULL);
453
454 if ( ret_value < 0 ) {
455 DEBUGMSGTL(("mibII/udpScalar", "Failed to load UDP scalar Group (solaris)\n"));
456 } else {
457 DEBUGMSGTL(("mibII/udpScalar", "Loaded UDP scalar Group (solaris)\n"));
458 }
459 return ret_value;
460 }
461 #elif defined(NETBSD_STATS_VIA_SYSCTL)
462 int
463 udp_load(netsnmp_cache *cache, void *vmagic)
464 {
465 long ret_value = -1;
466
467 ret_value = netbsd_read_udp_stat(&udpstat);
468
469 if ( ret_value < 0 ) {
470 DEBUGMSGTL(("mibII/udpScalar", "Failed to load UDP scalar Group (netbsd)\n"));
471 } else {
472 DEBUGMSGTL(("mibII/udpScalar", "Loaded UDP scalar Group (netbsd)\n"));
473 }
474 return ret_value;
475 }
476 #elif defined(WIN32)
477 int
478 udp_load(netsnmp_cache *cache, void *vmagic)
479 {
480 long ret_value = -1;
481
482 ret_value = GetUdpStatistics(&udpstat);
483
484 if ( ret_value < 0 ) {
485 DEBUGMSGTL(("mibII/udpScalar", "Failed to load UDP scalar Group (win32)\n"));
486 } else {
487 DEBUGMSGTL(("mibII/udpScalar", "Loaded UDP scalar Group (win32)\n"));
488 }
489 return ret_value;
490 }
491 #elif defined(_USE_PERFSTAT_PROTOCOL)
492 int
493 udp_load(netsnmp_cache *cache, void *vmagic)
494 {
495 long ret_value = -1;
496
497 strcpy(ps_name.name, "udp");
498 ret_value = perfstat_protocol(&ps_name, &ps_proto, sizeof(ps_proto), 1);
499
500 if ( ret_value < 0 ) {
501 DEBUGMSGTL(("mibII/udpScalar", "Failed to load UDP scalar Group (AIX)\n"));
502 } else {
503 ret_value = 0;
504 DEBUGMSGTL(("mibII/udpScalar", "Loaded UDP scalar Group (AIX)\n"));
505 }
506 return ret_value;
507 }
508 #elif (defined(NETSNMP_CAN_USE_SYSCTL) && defined(UDPCTL_STATS))
509 int
510 udp_load(netsnmp_cache *cache, void *vmagic)
511 {
512 int sname[4] = { CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS };
513 size_t len = sizeof(udpstat);
514 long ret_value = -1;
515
516 ret_value = sysctl(sname, 4, &udpstat, &len, 0, 0);
517
518 if ( ret_value < 0 ) {
519 DEBUGMSGTL(("mibII/udpScalar", "Failed to load UDP scalar Group (sysctl)\n"));
520 } else {
521 DEBUGMSGTL(("mibII/udpScalar", "Loaded UDP scalar Group (sysctl)\n"));
522 }
523 return ret_value;
524 }
525 #elif defined(HAVE_SYS_TCPIPSTATS_H)
526 int
527 udp_load(netsnmp_cache *cache, void *vmagic)
528 {
529 long ret_value = -1;
530
531 ret_value = sysmp(MP_SAGET, MPSA_TCPIPSTATS, &udpstat, sizeof(udpstat));
532
533 if ( ret_value < 0 ) {
534 DEBUGMSGTL(("mibII/udpScalar", "Failed to load UDP scalar Group (tcpipstats)\n"));
535 } else {
536 DEBUGMSGTL(("mibII/udpScalar", "Loaded UDP scalar Group (tcpipstats)\n"));
537 }
538 return ret_value;
539 }
540 #elif defined(UDPSTAT_SYMBOL)
541 int
542 udp_load(netsnmp_cache *cache, void *vmagic)
543 {
544 long ret_value = -1;
545
546 if (auto_nlist(UDPSTAT_SYMBOL, (char *)&udpstat, sizeof(udpstat)))
547 ret_value = 0;
548
549 if ( ret_value < 0 ) {
550 DEBUGMSGTL(("mibII/udpScalar", "Failed to load UDP scalar Group (udpstat)\n"));
551 } else {
552 DEBUGMSGTL(("mibII/udpScalar", "Loaded UDP scalar Group (udpstat)\n"));
553 }
554 return ret_value;
555 }
556 #else /* UDPSTAT_SYMBOL */
557 int
558 udp_load(netsnmp_cache *cache, void *vmagic)
559 {
560 long ret_value = -1;
561
562 DEBUGMSGTL(("mibII/udpScalar", "Failed to load UDP scalar Group (null)\n"));
563 return ret_value;
564 }
565 #endif /* hpux11 */
566
567
568 void
569 udp_free(netsnmp_cache *cache, void *magic)
570 {
571 #if defined(_USE_PERFSTAT_PROTOCOL)
572 memset(&ps_proto, 0, sizeof(ps_proto));
573 #else
574 memset(&udpstat, 0, sizeof(udpstat));
575 #endif
576 }