Debian Codegraph @

agent/mibgroup/mibII/ipv6.c

raw
1 /*
2 * IP MIB group implementation - ipv6.c
3 *
4 */
5
6 #include <net-snmp/net-snmp-config.h>
7 #include <net-snmp/net-snmp-features.h>
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #ifdef HAVE_SYS_IOCTL_H
11 #include <sys/ioctl.h>
12 #endif
13 #if defined(NETSNMP_IFNET_NEEDS_KERNEL) && !defined(_KERNEL)
14 #define _KERNEL 1
15 #define _I_DEFINED_KERNEL
16 #endif
17 #ifdef NETSNMP_IFNET_NEEDS_KERNEL_STRUCTURES
18 #define _KERNEL_STRUCTURES
19 #endif
20 #include <sys/param.h>
21 #ifdef _I_DEFINED_KERNEL
22 #undef _KERNEL
23 #endif
24 #ifdef HAVE_SYS_SOCKETVAR_H
25 # include <sys/socketvar.h>
26 #endif
27
28 #ifdef STDC_HEADERS
29 #include <string.h>
30 #include <stdlib.h>
31 #else
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35 #endif
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #ifdef HAVE_NETINET_IN_H
40 #include <netinet/in.h>
41 #endif
42 #ifdef HAVE_SYS_SYSCTL_H
43 #ifdef _I_DEFINED_KERNEL
44 #undef _KERNEL
45 #endif
46 #include <sys/sysctl.h>
47 #ifdef _I_DEFINED_KERNEL
48 #define _KERNEL 1
49 #endif
50 #endif
51 #ifdef HAVE_SYS_SYSMP_H
52 #include <sys/sysmp.h>
53 #endif
54 #ifdef HAVE_SYS_TCPIPSTATS_H
55 #include <sys/tcpipstats.h>
56 #endif
57 #ifdef _I_DEFINED_KERNEL
58 #undef _KERNEL
59 #endif
60 #include <net/if.h>
61 #ifdef _I_DEFINED_KERNEL
62 #define _KERNEL 1
63 #endif
64 #ifdef HAVE_NET_IF_VAR_H
65 #include <net/if_var.h>
66 #endif
67 #ifdef HAVE_NET_IF_DL_H
68 #include <net/if_dl.h>
69 #endif
70 #ifdef HAVE_NET_IF_MIB_H
71 #include <net/if_mib.h>
72 #endif
73 #ifdef _I_DEFINED_KERNEL
74 #undef _KERNEL
75 #endif
76 #include <netinet/in_systm.h>
77 #ifdef HAVE_SYS_HASHING_H
78 #include <sys/hashing.h>
79 #endif
80 #ifdef HAVE_SYS_SOCKIO_H
81 #include <sys/sockio.h>
82 #endif
83 #ifdef HAVE_NETINET6_IN6_VAR_H
84 #include <netinet6/in6_var.h>
85 #endif
86 #include <netinet/ip6.h>
87 #ifdef HAVE_NETINET_IP_VAR_H
88 # include <netinet/ip_var.h>
89 #endif
90 #ifdef HAVE_NETINET_ICMP6_H
91 # include <netinet/icmp6.h>
92 #endif
93 #ifdef HAVE_NETINET6_IP6_VAR_H
94 # include <sys/queue.h>
95 # include <netinet6/ip6_var.h>
96 #endif
97 #include <net/route.h>
98 #ifdef HAVE_NETINET_IP_H
99 # include <netinet/ip.h>
100 #endif
101 #ifdef HAVE_NETINET_IN_PCB_H
102 # include <netinet/in_pcb.h>
103 #endif
104 #ifdef HAVE_NETINET6_IN6_PCB_H
105 # include <netinet6/in6_pcb.h>
106 #endif
107 #ifdef HAVE_NETINET6_TCP6_H
108 # define TCP6
109 #endif
110 #ifndef TCP6
111 # ifdef HAVE_NETINET_TCP_H
112 # include <netinet/tcp.h>
113 # endif
114 # ifdef HAVE_NETINET_TCP_TIMER_H
115 # include <netinet/tcp_timer.h>
116 # endif
117 # ifdef HAVE_SYS_CALLOUT_H
118 # include <sys/callout.h>
119 # endif
120 # ifdef HAVE_SYS_OSD_H
121 # include <sys/osd.h>
122 # endif
123 # ifdef HAVE_NETINET_TCP_VAR_H
124 # include <netinet/tcp_var.h>
125 # endif
126 # ifdef HAVE_NETINET_TCP_FSM_H
127 # include <netinet/tcp_fsm.h>
128 # endif
129 #endif
130 #ifdef HAVE_NETINET6_TCP6_H
131 # include <netinet6/tcp6.h>
132 #endif
133 #ifdef HAVE_NETINET6_TCP6_TIMER_H
134 #include <netinet6/tcp6_timer.h>
135 #endif
136 #ifdef HAVE_NETINET6_TCP6_VAR_H
137 #include <netinet6/tcp6_var.h>
138 #endif
139 #ifdef HAVE_NETINET6_TCP6_FSM_H
140 #include <netinet6/tcp6_fsm.h>
141 #endif
142 #ifdef HAVE_INET_MIB2_H
143 #include <inet/mib2.h>
144 #endif
145 #ifdef HAVE_SYSLOG_H
146 #include <syslog.h>
147 #endif
148
149 #ifdef HAVE_KVM_GETFILES
150 #if defined(HAVE_KVM_GETFILE2) || !defined(openbsd5)
151 #undef HAVE_KVM_GETFILES
152 #endif
153 #endif
154
155 #ifdef HAVE_KVM_GETFILES
156 #include <kvm.h>
157 #include <sys/sysctl.h>
158 #define _KERNEL
159 #include <sys/file.h>
160 #undef _KERNEL
161 #endif
162
163 #ifdef MIB_IPCOUNTER_SYMBOL
164 #include <sys/mib.h>
165 #include <netinet/mib_kern.h>
166 #endif /* MIB_IPCOUNTER_SYMBOL */
167
168 #include <net-snmp/net-snmp-includes.h>
169 #include <net-snmp/agent/net-snmp-agent-includes.h>
170 #include <net-snmp/agent/auto_nlist.h>
171
172 #include "kernel.h"
173 #include "ipv6.h"
174 #include "interfaces.h"
175
176 #ifdef freebsd3
177 #undef in6pcb
178 #endif
179
180 netsnmp_feature_require(linux_read_ip6_stat);
181
182 #if defined(netbsd1) && !defined(openbsd4) && __NetBSD_Version__ < 999010400
183 #define inp_lport in6p_lport
184 #define inp_fport in6p_fport
185 #define inp_ppcb in6p_ppcb
186 #endif
187
188 static int header_ipv6
189 (register struct variable *, oid *, size_t *, int, size_t *,
190 WriteMethod **);
191 static int header_ipv6_scan
192 (register struct variable *, oid *, size_t *, int, size_t *,
193 WriteMethod **, int, int);
194 static int if_initialize (void);
195 static int if_maxifindex (void);
196 static char *if_getname (int);
197 #ifdef notused
198 static int if_getindex (const char *);
199 #endif
200
201 struct variable3 ipv6_variables[] = {
202 {IPV6FORWARDING, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
203 var_ipv6, 1, {1}},
204 {IPV6DEFAULTHOPLIMIT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
205 var_ipv6, 1, {2}},
206 {IPV6INTERFACES, ASN_GAUGE, NETSNMP_OLDAPI_RONLY,
207 var_ipv6, 1, {3}},
208 {IPV6IFTBLLASTCHG, ASN_TIMETICKS, NETSNMP_OLDAPI_RONLY,
209 var_ipv6, 1, {4}},
210
211 {IPV6IFDESCR, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
212 var_ifv6Entry, 3, {5, 1, 2}},
213 {IPV6IFLOWLAYER, ASN_OBJECT_ID, NETSNMP_OLDAPI_RONLY,
214 var_ifv6Entry, 3, {5, 1, 3}},
215 {IPV6IFEFFECTMTU, ASN_UNSIGNED, NETSNMP_OLDAPI_RONLY,
216 var_ifv6Entry, 3, {5, 1, 4}},
217 {IPV6IFREASMMAXSIZE, ASN_UNSIGNED, NETSNMP_OLDAPI_RONLY,
218 var_ifv6Entry, 3, {5, 1, 5}},
219
220 {IPV6IFTOKEN, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
221 var_ifv6Entry, 3, {5, 1, 6}},
222 {IPV6IFTOKENLEN, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
223 var_ifv6Entry, 3, {5, 1, 7}},
224 {IPV6IFPHYSADDRESS, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
225 var_ifv6Entry, 3, {5, 1, 8}},
226 {IPV6IFADMSTATUS, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
227 var_ifv6Entry, 3, {5, 1, 9}},
228 {IPV6IFOPERSTATUS, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
229 var_ifv6Entry, 3, {5, 1, 10}},
230 {IPV6IFLASTCHANGE, ASN_TIMETICKS, NETSNMP_OLDAPI_RONLY,
231 var_ifv6Entry, 3, {5, 1, 11}},
232
233 {IPV6IFSTATSINRCVS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
234 var_ifv6Entry, 3, {6, 1, 1}},
235 {IPV6IFSTATSINHDRERRS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
236 var_ifv6Entry, 3, {6, 1, 2}},
237 {IPV6IFSTATSTOOBIGERRS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
238 var_ifv6Entry, 3, {6, 1, 3}},
239 {IPV6IFSTATSINNOROUTES, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
240 var_ifv6Entry, 3, {6, 1, 4}},
241 {IPV6IFSTATSINADDRERRS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
242 var_ifv6Entry, 3, {6, 1, 5}},
243 {IPV6IFSTATSINUNKNOWPROTS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
244 var_ifv6Entry, 3, {6, 1, 6}},
245 {IPV6IFSTATSINTRUNCATPKTS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
246 var_ifv6Entry, 3, {6, 1, 7}},
247 {IPV6IFSTATSINDISCARDS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
248 var_ifv6Entry, 3, {6, 1, 8}},
249 {IPV6IFSTATSINDELIVERS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
250 var_ifv6Entry, 3, {6, 1, 9}},
251 {IPV6IFSTATSOUTFORWDATAS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
252 var_ifv6Entry, 3, {6, 1, 10}},
253 {IPV6IFSTATSOUTREQS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
254 var_ifv6Entry, 3, {6, 1, 11}},
255 {IPV6IFSTATSOUTDISCARDS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
256 var_ifv6Entry, 3, {6, 1, 12}},
257 {IPV6IFSTATSOUTFRAGOKS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
258 var_ifv6Entry, 3, {6, 1, 13}},
259 {IPV6IFSTATSOUTFRAGFAILS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
260 var_ifv6Entry, 3, {6, 1, 14}},
261 {IPV6IFSTATSOUTFRAGCREATS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
262 var_ifv6Entry, 3, {6, 1, 15}},
263 {IPV6IFSTATSOUTREASMREQS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
264 var_ifv6Entry, 3, {6, 1, 16}},
265 {IPV6IFSTATSOUTREASMOKS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
266 var_ifv6Entry, 3, {6, 1, 17}},
267 {IPV6IFSTATSOUTREASMFAILS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
268 var_ifv6Entry, 3, {6, 1, 18}},
269 {IPV6IFSTATSINMCASTPKTS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
270 var_ifv6Entry, 3, {6, 1, 19}},
271 {IPV6IFSTATSOUTMCASTPKTS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
272 var_ifv6Entry, 3, {6, 1, 20}},
273
274 #if 0
275 {IPV6ADDRPREFIXONLINKFLG, INTEGER, NETSNMP_OLDAPI_RONLY,
276 var_ipv6AddrEntry, 3, {7, 1, 3}},
277 {IPV6ADDRPREFIXAUTONOMOUSFLAG, INTEGER, NETSNMP_OLDAPI_RONLY,
278 var_ipv6AddrEntry, 3, {7, 1, 4}},
279 {IPV6ADDRPREFIXADVPREFERLIFE, UNSIGNED32, NETSNMP_OLDAPI_RONLY,
280 var_ipv6AddrEntry, 3, {7, 1, 5}},
281 {IPV6ADDRPREFIXVALIDLIFE, UNSIGNED32, NETSNMP_OLDAPI_RONLY,
282 var_ipv6AddrEntry, 3, {7, 1, 6}},
283
284 {IPV6ADDRPFXLEN, INTEGER, NETSNMP_OLDAPI_RONLY,
285 var_ipv6AddrEntry, 3, {8, 1, 2}},
286 {IPV6ADDRTYPE, INTEGER, NETSNMP_OLDAPI_RONLY,
287 var_ipv6AddrEntry, 3, {8, 1, 3}},
288 {IPV6ADDRANYCASTFLAG, INTEGER, NETSNMP_OLDAPI_RONLY,
289 var_ipv6AddrEntry, 3, {8, 1, 4}},
290 {IPV6ADDRSTATUS, INTEGER, NETSNMP_OLDAPI_RONLY,
291 var_ipv6AddrEntry, 3, {8, 1, 5}},
292
293 {IPV6ROUTEIFINDEX, IpV6IFINDEX, NETSNMP_OLDAPI_RONLY,
294 var_ipv6RouteEntry, 3, {11, 1, 4}},
295 {IPV6ROUTENEXTHOP, IpV6ADDRESS, NETSNMP_OLDAPI_RONLY,
296 var_ipv6RouteEntry, 3, {11, 1, 5}},
297 {IPV6ROUTETYPE, INTEGER, NETSNMP_OLDAPI_RONLY,
298 var_ipv6RouteEntry, 3, {11, 1, 6}},
299 {IPV6ROUTEPROTOCOL, INTEGER, NETSNMP_OLDAPI_RONLY,
300 var_ipv6RouteEntry, 3, {11, 1, 7}},
301 {IPV6ROUTEPOLICY, UNSIGNED32, NETSNMP_OLDAPI_RONLY,
302 var_ipv6RouteEntry, 3, {11, 1, 8}},
303 {IPV6ROUTEAGE, UNSIGNED32, NETSNMP_OLDAPI_RONLY,
304 var_ipv6RouteEntry, 3, {11, 1, 9}},
305 {IPV6ROUTENEXTHOPRDI, UNSIGNED32, NETSNMP_OLDAPI_RONLY,
306 var_ipv6RouteEntry, 3, {11, 1, 10}},
307 {IPV6ROUTEMETRIC, UNSIGNED32, NETSNMP_OLDAPI_RONLY,
308 var_ipv6RouteEntry, 3, {11, 1, 11}},
309 {IPV6ROUTEWEIGHT, UNSIGNED32, NETSNMP_OLDAPI_RONLY,
310 var_ipv6RouteEntry, 3, {11, 1, 12}},
311 {IPV6ROUTEINFO, OBJID, NETSNMP_OLDAPI_RONLY,
312 var_ipv6RouteEntry, 3, {11, 1, 13}},
313 {IPV6ROUTEVALID, INTEGER, NETSNMP_OLDAPI_RONLY,
314 var_ipv6RouteEntry, 3, {11, 1, 14}},
315
316 {IPV6NETTOMEDIAPHYADDR, STRING, NETSNMP_OLDAPI_RONLY,
317 var_ndpEntry, 3, {12, 1, 2}},
318 {IPV6NETTOMEDIATYPE, INTEGER, NETSNMP_OLDAPI_RONLY,
319 var_ndpEntry, 3, {12, 1, 3}},
320 {IPV6NETTOMEDIASTATE, INTEGER, NETSNMP_OLDAPI_RONLY,
321 var_ndpEntry, 3, {12, 1, 4}},
322 {IPV6NETTOMEDIALASTUPDATE, TIMETICKS, NETSNMP_OLDAPI_RONLY,
323 var_ndpEntry, 3, {12, 1, 5}},
324 {IPV6NETTOMEDIAVALID, INTEGER, NETSNMP_OLDAPI_RONLY,
325 var_ndpEntry, 3, {12, 1, 6}},
326 #endif
327 };
328 oid ipv6_variables_oid[] = { SNMP_OID_MIB2, 55, 1 };
329 #if 0
330 config_load_mib(MIB .55 .1, 8, ipv6_variables)
331 config_add_mib(IPV6 - TC);
332 config_add_mib(IPV6 - MIB);
333 #endif
334 struct variable3 ipv6icmp_variables[] = {
335 {IPV6IFICMPINMSG, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
336 var_icmpv6Entry, 3, {1, 1, 1}},
337 {IPV6IFICMPINERRORS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
338 var_icmpv6Entry, 3, {1, 1, 2}},
339 {IPV6IFICMPINDSTUNRCHS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
340 var_icmpv6Entry, 3, {1, 1, 3}},
341 {IPV6IFICMPINADMPROHS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
342 var_icmpv6Entry, 3, {1, 1, 4}},
343 {IPV6IFICMPINTIMEXCDS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
344 var_icmpv6Entry, 3, {1, 1, 5}},
345 {IPV6IFICMPINPARMPROBS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
346 var_icmpv6Entry, 3, {1, 1, 6}},
347 {IPV6IFICMPINPKTTOOBIGS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
348 var_icmpv6Entry, 3, {1, 1, 7}},
349 {IPV6IFICMPINECHOS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
350 var_icmpv6Entry, 3, {1, 1, 8}},
351 {IPV6IFICMPINECHOREPS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
352 var_icmpv6Entry, 3, {1, 1, 9}},
353 {IPV6IFICMPINRTRSLICITS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
354 var_icmpv6Entry, 3, {1, 1, 10}},
355 {IPV6IFICMPINRTRADVS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
356 var_icmpv6Entry, 3, {1, 1, 11}},
357 {IPV6IFICMPINNBRSLICITS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
358 var_icmpv6Entry, 3, {1, 1, 12}},
359 {IPV6IFICMPINNBRADVS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
360 var_icmpv6Entry, 3, {1, 1, 13}},
361 {IPV6IFICMPINREDIRECTS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
362 var_icmpv6Entry, 3, {1, 1, 14}},
363 {IPV6IFICMPINGRPMEQERYS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
364 var_icmpv6Entry, 3, {1, 1, 15}},
365 {IPV6IFICMPINGRPMERSPS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
366 var_icmpv6Entry, 3, {1, 1, 16}},
367 {IPV6IFICMPINGRPMEREDCS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
368 var_icmpv6Entry, 3, {1, 1, 17}},
369 {IPV6IFICMPOUTMSG, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
370 var_icmpv6Entry, 3, {1, 1, 18}},
371 {IPV6IFICMPOUTERRORS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
372 var_icmpv6Entry, 3, {1, 1, 19}},
373 {IPV6IFICMPOUTDSTUNRCHS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
374 var_icmpv6Entry, 3, {1, 1, 20}},
375 {IPV6IFICMPOUTADMPROHS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
376 var_icmpv6Entry, 3, {1, 1, 21}},
377 {IPV6IFICMPOUTTIMEXCDS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
378 var_icmpv6Entry, 3, {1, 1, 22}},
379 {IPV6IFICMPOUTPARMPROBS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
380 var_icmpv6Entry, 3, {1, 1, 23}},
381 {IPV6IFICMPOUTPKTTOOBIGS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
382 var_icmpv6Entry, 3, {1, 1, 24}},
383 {IPV6IFICMPOUTECHOS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
384 var_icmpv6Entry, 3, {1, 1, 25}},
385 {IPV6IFICMPOUTECHOREPS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
386 var_icmpv6Entry, 3, {1, 1, 26}},
387 {IPV6IFICMPOUTRTRSLICITS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
388 var_icmpv6Entry, 3, {1, 1, 27}},
389 {IPV6IFICMPOUTRTRADVS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
390 var_icmpv6Entry, 3, {1, 1, 28}},
391 {IPV6IFICMPOUTNBRSLICITS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
392 var_icmpv6Entry, 3, {1, 1, 29}},
393 {IPV6IFICMPOUTNBRADVS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
394 var_icmpv6Entry, 3, {1, 1, 30}},
395 {IPV6IFICMPOUTREDIRECTS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
396 var_icmpv6Entry, 3, {1, 1, 31}},
397 {IPV6IFICMPOUTGRPMEQERYS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
398 var_icmpv6Entry, 3, {1, 1, 32}},
399 {IPV6IFICMPOUTGRPMERSPS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
400 var_icmpv6Entry, 3, {1, 1, 33}},
401 {IPV6IFICMPOUTGRPMEREDCS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
402 var_icmpv6Entry, 3, {1, 1, 34}}
403 };
404 oid ipv6icmp_variables_oid[] = { 1, 3, 6, 1, 2, 1, 56, 1 };
405 #if 0
406 config_load_mib(MIB .56 .1, 8, ipv6icmp_variables)
407 config_add_mib(IPV6 - ICMP - MIB);
408 #endif
409 struct variable2 ipv6udp_variables[] = {
410 {IPV6UDPIFINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
411 var_udp6, 2, {1, 3}}
412 };
413 oid ipv6udp_variables_oid[] = { 1, 3, 6, 1, 2, 1, 7, 6 };
414 #if 0
415 config_load_mib(1.3 .6 .1 .3 .87 .1, 7, ipv6udp_variables)
416 config_add_mib(IPV6 - UDP - MIB);
417 #endif
418 struct variable2 ipv6tcp_variables[] = {
419 {IPV6TCPCONNSTATE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
420 var_tcp6, 2, {1, 6}},
421 };
422 oid ipv6tcp_variables_oid[] = { 1, 3, 6, 1, 2, 1, 6, 16 };
423 #if 0
424 config_load_mib(1.3 .6 .1 .3 .86 .1, 7, ipv6tcp_variables)
425 config_add_mib(IPV6 - TCP - MIB);
426 #endif
427
428 void
429 init_ipv6(void)
430 {
431 /*
432 * register ourselves with the agent to handle our mib tree
433 */
434 REGISTER_MIB("mibII/ipv6", ipv6_variables, variable3,
435 ipv6_variables_oid);
436 REGISTER_MIB("mibII/icmpv6", ipv6icmp_variables, variable3,
437 ipv6icmp_variables_oid);
438 REGISTER_MIB("mibII/ipv6udp", ipv6udp_variables, variable2,
439 ipv6udp_variables_oid);
440 REGISTER_MIB("mibII/ipv6tcp", ipv6tcp_variables, variable2,
441 ipv6tcp_variables_oid);
442 }
443
444 static int
445 header_ipv6(register struct variable *vp,
446 /*
447 * IN - pointer to variable entry that points here
448 */
449 oid * name, /* IN/OUT - input name requested, output name found */
450 size_t * length, /* IN/OUT - length of input and output oid's */
451 int exact, /* IN - TRUE if an exact match was requested */
452 size_t * var_len, /* OUT - length of variable or 0 if function returned */
453 WriteMethod ** write_method)
454 {
455 oid newname[MAX_OID_LEN];
456 int result;
457
458 DEBUGMSGTL(("mibII/ipv6", "header_ipv6: "));
459 DEBUGMSGOID(("mibII/ipv6", name, *length));
460 DEBUGMSG(("mibII/ipv6", " %d\n", exact));
461
462 memcpy((char *) newname, (char *) vp->name,
463 (int) vp->namelen * sizeof(oid));
464 newname[(int) vp->namelen] = 0;
465 result =
466 snmp_oid_compare(name, *length, newname, (int) vp->namelen + 1);
467 if ((exact && (result != 0)) || (!exact && (result >= 0)))
468 return (MATCH_FAILED);
469 memcpy((char *) name, (char *) newname,
470 ((int) vp->namelen + 1) * sizeof(oid));
471 *length = vp->namelen + 1;
472
473 *write_method = (WriteMethod*)0;
474 *var_len = sizeof(long); /* default to 'long' results */
475 return (MATCH_SUCCEEDED);
476 }
477
478 static int
479 header_ipv6_scan(register struct variable *vp,
480 /*
481 * IN - pointer to variable entry that points here
482 */
483 oid * name, /* IN/OUT - input name requested, output name found */
484 size_t * length, /* IN/OUT - length of input and output oid's */
485 int exact, /* IN - TRUE if an exact match was requested */
486 size_t * var_len, /* OUT - length of variable or 0 if function returned */
487 WriteMethod ** write_method, int from, int to)
488 {
489 oid newname[MAX_OID_LEN];
490 int result;
491 int i;
492
493 DEBUGMSGTL(("mibII/ipv6", "header_ipv6_scan: "));
494 DEBUGMSGOID(("mibII/ipv6", name, *length));
495 DEBUGMSG(("mibII/ipv6", " %d\n", exact));
496
497 memcpy((char *) newname, (char *) vp->name,
498 (int) vp->namelen * sizeof(oid));
499 for (i = from; i <= to; i++) {
500 newname[(int) vp->namelen] = i;
501 result =
502 snmp_oid_compare(name, *length, newname,
503 (int) vp->namelen + 1);
504 if (((exact && result == 0) || (!exact && result < 0))
505 && if_getname(i))
506 break;
507 }
508 if (to < i)
509 return (MATCH_FAILED);
510 memcpy((char *) name, (char *) newname,
511 ((int) vp->namelen + 1) * sizeof(oid));
512 *length = vp->namelen + 1;
513 *write_method = (WriteMethod*)0;
514 *var_len = sizeof(long); /* default to 'long' results */
515 return (MATCH_SUCCEEDED);
516 }
517
518 static struct if_nameindex *ifnames = NULL;
519
520 #ifdef linux
521 static void linux_if_freenameindex(struct if_nameindex *);
522 static struct if_nameindex *linux_if_nameindex(void);
523 #endif
524
525 static int
526 if_initialize(void)
527 {
528 #ifndef HAVE_IF_NAMEINDEX
529 return -1;
530 #else
531 #ifndef linux
532 if (ifnames)
533 if_freenameindex(ifnames);
534 ifnames = if_nameindex();
535 #else
536 if (ifnames)
537 linux_if_freenameindex(ifnames);
538 ifnames = linux_if_nameindex();
539 #endif
540 if (!ifnames) {
541 ERROR_MSG("if_nameindex() failed");
542 return -1;
543 }
544 return 0;
545 #endif
546 }
547
548 static int
549 if_maxifindex(void)
550 {
551 #ifndef HAVE_IF_NAMEINDEX
552 return -1;
553 #else
554 struct if_nameindex *p;
555 int max = 0;
556
557 if (!ifnames) {
558 if (if_initialize() < 0)
559 return -1;
560 }
561 for (p = ifnames; p && p->if_index; p++) {
562 if (max < p->if_index)
563 max = p->if_index;
564 }
565 return max;
566 #endif
567 }
568
569 static int
570 if_countifindex(void)
571 {
572 #ifndef HAVE_IF_NAMEINDEX
573 return -1;
574 #else
575 struct if_nameindex *p;
576 int count = 0;
577
578 if (!ifnames) {
579 if (if_initialize() < 0)
580 return -1;
581 }
582 for (p = ifnames; p && p->if_index; p++) {
583 count++;
584 }
585 return count;
586 #endif
587 }
588
589 static char *
590 if_getname(int idx)
591 {
592 #ifndef HAVE_IF_NAMEINDEX
593 return NULL;
594 #else
595 struct if_nameindex *p;
596
597 if (!ifnames) {
598 if (if_initialize() < 0)
599 return NULL;
600 }
601 for (p = ifnames; p && p->if_index; p++) {
602 if (p->if_index == idx)
603 return p->if_name;
604 }
605 return NULL;
606 #endif
607 }
608
609 #ifdef notused
610 static int
611 if_getindex(const char *name)
612 {
613 #ifndef HAVE_IF_NAMEINDEX
614 return -1;
615 #else
616 struct if_nameindex *p;
617
618 if (!ifnames) {
619 if (if_initialize() < 0)
620 return -1;
621 }
622 for (p = ifnames; p && p->if_index; p++) {
623 if (strcmp(name, p->if_name) == 0)
624 return p->if_index;
625 }
626 return -1;
627 #endif
628 }
629 #endif /* notused */
630
631 /*------------------------------------------------------------*/
632 #ifndef linux
633
634 #if defined(__OpenBSD__) || defined(freebsd3)
635
636 /*
637 * It is not possible to use struct ifnet anymore on OpenBSD, get
638 * interface flags and L2 address through getifaddrs(3).
639 */
640
641 #include <ifaddrs.h>
642
643 static int
644 if_getifflags(int ifindex, int *ifflags)
645 {
646 const char *ifname;
647 struct ifaddrs *ifa0, *ifa;
648 int ret = -1;
649
650 ifname = if_getname(ifindex);
651 if (ifname == NULL)
652 return ret;
653
654 if (getifaddrs(&ifa0) != -1) {
655 for (ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next) {
656 if (strcmp(ifa->ifa_name, ifname) == 0) {
657 *ifflags = ifa->ifa_flags;
658 ret = 0;
659 break;
660 }
661 }
662 freeifaddrs(ifa0);
663 }
664
665 return ret;
666 }
667
668 #else
669
670 /*
671 * KAME dependent part
672 */
673 static int
674 if_getifnet(int idx, struct ifnet *result)
675 {
676 caddr_t q;
677 struct ifnet tmp;
678
679 if (!auto_nlist("ifnet", (char *) &q, sizeof(q)))
680 return -1;
681 while (q) {
682 if (!NETSNMP_KLOOKUP(q, (char *) &tmp, sizeof(tmp))) {
683 DEBUGMSGTL(("mibII/ipv6:if_getifnet", "klookup failed\n"));
684 return -1;
685 }
686 if (idx == tmp.if_index) {
687 memcpy(result, &tmp, sizeof(tmp));
688 return 0;
689 }
690 #if defined(freebsd3) || defined(darwin)
691 q = (caddr_t) TAILQ_NEXT(&tmp, if_link);
692 #else
693 # if defined(__NetBSD__) || defined(__OpenBSD__)
694 q = (caddr_t) TAILQ_NEXT(&tmp, if_list);
695 # else
696 q = (caddr_t) tmp.if_next;
697 # endif
698 #endif
699 }
700 return -1;
701 }
702
703 #endif /* !__OpenBSD__ */
704
705 #ifdef TRUST_IFLASTCHANGE /*untrustable value returned... */
706 #ifdef HAVE_NET_IF_MIB_H
707 #if defined(HAVE_SYS_SYSCTL_H) && defined(CTL_NET)
708 static int
709 if_getifmibdata(int idx, struct ifmibdata *result)
710 {
711 int mib[6] = {
712 CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, 0, IFDATA_GENERAL
713 };
714 size_t len;
715 struct ifmibdata tmp;
716
717 mib[4] = idx;
718 len = sizeof(struct ifmibdata);
719 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), &tmp, &len, 0, 0) < 0)
720 return -1;
721 memcpy(result, &tmp, sizeof(tmp));
722 return 0;
723 }
724 #endif
725 #endif /*HAVE_NET_IF_MIB_H */
726 #endif /* TRUST_IFLASTCHANGE */
727
728 #ifdef __KAME__
729 #define IPV6_FORWARDING_SYMBOL "ip6_forwarding"
730 #define IPV6_DEFHLIM_SYMBOL "ip6_defhlim"
731 #endif
732
733 u_char *
734 var_ipv6(register struct variable * vp,
735 oid * name,
736 size_t * length,
737 int exact, size_t * var_len, WriteMethod ** write_method)
738 {
739 int i;
740
741 if (header_ipv6(vp, name, length, exact, var_len, write_method)
742 == MATCH_FAILED) {
743 return NULL;
744 }
745 #if defined(HAVE_SYS_SYSCTL_H) && defined(CTL_NET)
746 /*
747 * try with sysctl routines
748 */
749 {
750 int name[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
751 const char *namestr = NULL;
752 int result;
753 size_t resultsiz;
754
755 resultsiz = sizeof(result);
756 switch (vp->magic) {
757 case IPV6FORWARDING:
758 name[3] = IPV6CTL_FORWARDING;
759 namestr = "IPV6CTL_FORWARDING";
760 if (sysctl
761 (name, sizeof(name) / sizeof(name[0]), &result, &resultsiz,
762 0, 0) < 0) {
763 DEBUGMSGTL(("mibII/ipv6",
764 "sysctl(CTL_NET, PF_INET6, IPPROTO_IPV6, %s)\n",
765 namestr));
766 break;
767 } else {
768 if (result)
769 long_return = 1; /* GATEWAY */
770 else
771 long_return = 2; /* HOST */
772 return (u_char *) & long_return;
773 }
774 break;
775 case IPV6DEFAULTHOPLIMIT:
776 name[3] = IPV6CTL_DEFHLIM;
777 namestr = "IPV6CTL_DEFHLIM";
778 if (sysctl
779 (name, sizeof(name) / sizeof(name[0]), &result, &resultsiz,
780 0, 0) < 0) {
781 DEBUGMSGTL(("mibII/ipv6",
782 "sysctl(CTL_NET, PF_INET6, IPPROTO_IPV6, %s)\n",
783 namestr));
784 break;
785 } else {
786 long_return = result;
787 return (u_char *) & long_return;
788 }
789 }
790 }
791 #endif /* not (HAVE_SYS_SYSCTL_H && CTL_NET) */
792
793 /*
794 * try looking into the kernel variable
795 */
796 switch (vp->magic) {
797 #ifdef IPV6_FORWARDING_SYMBOL
798 case IPV6FORWARDING:
799 if (auto_nlist(IPV6_FORWARDING_SYMBOL, (char *) &i, sizeof(i))) {
800 if (i)
801 long_return = 1;
802 /*GATEWAY*/
803 else
804 long_return = 2;
805 /*HOST*/ return (u_char *) & long_return;
806 }
807 break;
808 #endif
809 #ifdef IPV6_DEFHLIM_SYMBOL
810 case IPV6DEFAULTHOPLIMIT:
811 if (auto_nlist(IPV6_DEFHLIM_SYMBOL, (char *) &i, sizeof(i))) {
812 long_return = i;
813 return (u_char *) & long_return;
814 }
815 break;
816 #endif
817 case IPV6INTERFACES:
818 #ifdef HAVE_IF_NAMEINDEX
819 /*
820 * not really the right answer... we must count IPv6 capable
821 * interfaces only.
822 */
823 long_return = if_countifindex();
824 if (long_return < 0)
825 break;
826 return (u_char *) & long_return;
827 #endif
828 break;
829 #if 0
830 case IPV6IFTBLLASTCHG:
831 long_return = 0;
832 /*XXX*/ return (u_char *) & long_return;
833 #endif
834 default:
835 break;
836 }
837 ERROR_MSG("");
838 return NULL;
839 }
840
841 u_char *
842 var_ifv6Entry(register struct variable * vp,
843 oid * name,
844 size_t * length,
845 int exact, size_t * var_len, WriteMethod ** write_method)
846 {
847 #ifndef HAVE_IF_NAMEINDEX
848 return NULL;
849 #else
850 int interface;
851 int max;
852 char *p;
853
854 /* Reload list of interfaces */
855 if (if_initialize() < 0)
856 return NULL;
857
858 max = if_maxifindex();
859 if (max < 0)
860 return NULL;
861
862 if (header_ipv6_scan
863 (vp, name, length, exact, var_len, write_method, 1, max)
864 == MATCH_FAILED) {
865 return NULL;
866 }
867 interface = name[*length - 1];
868 DEBUGMSGTL(("mibII/ipv6", "interface: %d(%s)\n",
869 interface, if_getname(interface)));
870 if (interface > max)
871 return NULL;
872
873 switch (vp->magic) {
874 case IPV6IFDESCR:
875 p = if_getname(interface);
876 if (p) {
877 *var_len = strlen(p);
878 return (u_char *)p;
879 }
880 break;
881 case IPV6IFLOWLAYER:
882 /*
883 * should check if type, this is a hard one...
884 */
885 *var_len = nullOidLen;
886 return (u_char *) nullOid;
887 case IPV6IFEFFECTMTU:
888 {
889 #if defined(SIOCGIFMTU) && !defined(__OpenBSD__)
890 struct ifreq ifr;
891 int s;
892
893 memset(&ifr, 0, sizeof(ifr));
894 ifr.ifr_addr.sa_family = AF_INET6;
895 strlcpy(ifr.ifr_name, if_getname(interface), sizeof(ifr.ifr_name));
896 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
897 break;
898 if (ioctl(s, SIOCGIFMTU, (caddr_t) & ifr) < 0) {
899 close(s);
900 break;
901 }
902 long_return = ifr.ifr_mtu;
903 close(s);
904 return (u_char *) & long_return;
905 #else
906 break;
907 #endif
908 }
909 #if 0 /*not yet */
910 case IPV6IFREASMMAXSIZE:
911 /*
912 * I dunno what the spec means for this MIB
913 */
914 case IPV6IFTOKEN:
915 case IPV6IFTOKENLEN:
916 #endif
917 case IPV6IFPHYSADDRESS:
918 {
919 #if defined(__OpenBSD__) || defined(freebsd3)
920 struct ifaddrs *ifa0, *ifa;
921 static struct sockaddr_dl sdl;
922 char ifnam[IF_NAMESIZE];
923
924 if (if_indextoname(interface, ifnam) == NULL) {
925 *var_len = 0;
926 return NULL;
927 }
928
929 if (getifaddrs(&ifa0) != -1) {
930 for (ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next) {
931
932 if (strcmp(ifnam, ifa->ifa_name) != 0)
933 continue;
934
935 if (ifa->ifa_addr == NULL)
936 continue;
937
938 memcpy(&sdl, ifa->ifa_addr, sizeof(sdl));
939 if (sdl.sdl_family != AF_LINK)
940 continue;
941
942 freeifaddrs(ifa0);
943 *var_len = sdl.sdl_alen;
944 return (u_char *) (sdl.sdl_data + sdl.sdl_nlen);
945 }
946 }
947 freeifaddrs(ifa0);
948 return NULL;
949 #else
950 struct ifnet ifnet;
951 struct ifaddr ifaddr;
952 #if defined(__DragonFly__) && __DragonFly_version >= 197700
953 struct ifaddr_container ifac;
954 struct ifaddrhead head;
955 #endif
956 static struct sockaddr_dl sdl;
957 caddr_t ifa;
958
959 if (if_getifnet(interface, &ifnet) < 0)
960 break;
961 #if defined(freebsd3) || defined(darwin)
962 # if defined(__DragonFly__) && __DragonFly_version >= 197700
963 /*
964 * Locate ifaddr head on CPU0
965 */
966 if (!NETSNMP_KLOOKUP(ifnet.if_addrheads, (char *)&head, sizeof(head))) {
967 DEBUGMSGTL(("mibII/ipv6:var_ipv6", "klookup head failed\n"));
968 break;
969 }
970 if (TAILQ_FIRST(&head) != NULL) {
971 if (!NETSNMP_KLOOKUP(TAILQ_FIRST(&head), (char *) &ifac, sizeof(ifac))) {
972 DEBUGMSGTL(("mibII/ipv6:var_ipv6", "klookup ifac failed\n"));
973 break;
974 }
975 ifa = (caddr_t)ifac.ifa;
976 } else {
977 ifa = NULL;
978 }
979 # else
980 ifa = (caddr_t) TAILQ_FIRST(&ifnet.if_addrhead);
981 # endif
982 #else
983 # if defined(__NetBSD__) || defined(__OpenBSD__)
984 ifa = (caddr_t) TAILQ_FIRST(&ifnet.if_addrlist);
985 # else
986 ifa = (caddr_t) ifnet.if_addrlist;
987 # endif
988 #endif
989 while (ifa) {
990 if (!NETSNMP_KLOOKUP(ifa, (char *) &ifaddr, sizeof(ifaddr))) {
991 DEBUGMSGTL(("mibII/ipv6:var_ipv6", "klookup failed\n"));
992 break;
993 }
994 if (!NETSNMP_KLOOKUP(ifaddr.ifa_addr,
995 (char *) &sdl, sizeof(sdl))) {
996 DEBUGMSGTL(("mibII/ipv6:var_ipv6", "klookup failed\n"));
997 break;
998 }
999 if (sdl.sdl_family == AF_LINK) {
1000 if (sizeof(sdl.sdl_data) < sdl.sdl_nlen + sdl.sdl_alen) {
1001 ERROR_MSG("sdl_alen too long for interface\n");
1002 break;
1003 }
1004 *var_len = sdl.sdl_alen;
1005 return (u_char *) (sdl.sdl_data + sdl.sdl_nlen);
1006 }
1007 #if defined(freebsd3) || defined(darwin)
1008 # if defined(__DragonFly__) && __DragonFly_version >= 197700
1009 if (TAILQ_NEXT(&ifac, ifa_link) == NULL) {
1010 ifa = NULL;
1011 } else {
1012 if (!NETSNMP_KLOOKUP(TAILQ_NEXT(&ifac, ifa_link), (char *)&ifac, sizeof(ifac))) {
1013 DEBUGMSGTL(("mibII/ipv6:var_ipv6", "klookup ifac next failed\n"));
1014 break;
1015 }
1016 ifa = (caddr_t)ifac.ifa;
1017 }
1018 # else
1019 ifa = (caddr_t) TAILQ_NEXT(&ifaddr, ifa_link);
1020 # endif
1021 #else
1022 # if defined(__NetBSD__) || defined(__OpenBSD__)
1023 ifa = (caddr_t) TAILQ_NEXT(&ifaddr, ifa_list);
1024 # else
1025 ifa = (caddr_t) ifaddr.ifa_next;
1026 # endif
1027 #endif
1028 }
1029
1030 /*
1031 * no physical address found
1032 */
1033 *var_len = 0;
1034 return NULL;
1035 #endif /* !__OpenBSD__ */
1036 }
1037 case IPV6IFADMSTATUS:
1038 {
1039 #if defined(__OpenBSD__) || defined(freebsd3)
1040 int if_flags;
1041 if (if_getifflags(interface, &if_flags) < 0)
1042 break;
1043 long_return = (if_flags & IFF_RUNNING) ? 1 : 2;
1044 #else
1045 struct ifnet ifnet;
1046
1047 if (if_getifnet(interface, &ifnet) < 0)
1048 break;
1049 long_return = (ifnet.if_flags & IFF_RUNNING) ? 1 : 2;
1050 #endif
1051 return (u_char *) & long_return;
1052 }
1053 case IPV6IFOPERSTATUS:
1054 {
1055 #if defined(__OpenBSD__) || defined(freebsd3)
1056 int if_flags;
1057 if (if_getifflags(interface, &if_flags) < 0)
1058 break;
1059 long_return = (if_flags & IFF_UP) ? 1 : 2;
1060 #else
1061 struct ifnet ifnet;
1062
1063 if (if_getifnet(interface, &ifnet) < 0)
1064 break;
1065 long_return = (ifnet.if_flags & IFF_UP) ? 1 : 2;
1066 #endif
1067 return (u_char *) & long_return;
1068 }
1069 #ifdef TRUST_IFLASTCHANGE /*untrustable value returned... */
1070 case IPV6IFLASTCHANGE:
1071 {
1072 struct timeval lastchange;
1073 struct timeval now;
1074 int gotanswer;
1075
1076 gotanswer = 0;
1077 lastchange.tv_sec = lastchange.tv_usec = 0;
1078 #ifdef HAVE_NET_IF_MIB_H
1079 if (!gotanswer) {
1080 struct ifmibdata ifmd;
1081
1082 if (if_getifmibdata(interface, &ifmd) < 0);
1083 else {
1084 lastchange = ifmd.ifmd_data.ifi_lastchange;
1085 gotanswer++;
1086 }
1087 }
1088 #endif
1089 #ifdef HAVE_STRUCT_IFNET_IF_LASTCHANGE_TV_SEC
1090 if (!gotanswer) {
1091 struct ifnet ifnet;
1092
1093 if (if_getifnet(interface, &ifnet) < 0);
1094 else {
1095 lastchange = ifnet.if_lastchange;
1096 gotanswer++;
1097 }
1098 }
1099 #endif
1100 DEBUGMSGTL(("mibII/ipv6", "lastchange = { %d.%06d }\n",
1101 lastchange.tv_sec, lastchange.tv_usec));
1102 if (lastchange.tv_sec == 0 && lastchange.tv_usec == 0)
1103 long_return = 0;
1104 else {
1105 gettimeofday(&now, (struct timezone *) NULL);
1106 long_return =
1107 (u_long) ((now.tv_sec - lastchange.tv_sec) * 100);
1108 long_return +=
1109 (u_long) ((now.tv_usec - lastchange.tv_usec) / 10000);
1110 }
1111 return (u_char *) & long_return;
1112 }
1113 #endif /* TRUST_IFLASTCHANGE */
1114
1115 #ifdef SIOCGIFSTAT_IN6
1116 case IPV6IFSTATSINRCVS:
1117 case IPV6IFSTATSINHDRERRS:
1118 case IPV6IFSTATSTOOBIGERRS:
1119 case IPV6IFSTATSINNOROUTES:
1120 case IPV6IFSTATSINADDRERRS:
1121 case IPV6IFSTATSINUNKNOWPROTS:
1122 case IPV6IFSTATSINTRUNCATPKTS:
1123 case IPV6IFSTATSINDISCARDS:
1124 case IPV6IFSTATSINDELIVERS:
1125 case IPV6IFSTATSOUTFORWDATAS:
1126 case IPV6IFSTATSOUTREQS:
1127 case IPV6IFSTATSOUTDISCARDS:
1128 case IPV6IFSTATSOUTFRAGOKS:
1129 case IPV6IFSTATSOUTFRAGFAILS:
1130 case IPV6IFSTATSOUTFRAGCREATS:
1131 case IPV6IFSTATSOUTREASMREQS:
1132 case IPV6IFSTATSOUTREASMOKS:
1133 case IPV6IFSTATSOUTREASMFAILS:
1134 case IPV6IFSTATSINMCASTPKTS:
1135 case IPV6IFSTATSOUTMCASTPKTS:
1136 {
1137 struct in6_ifstat *ifs6;
1138 struct in6_ifreq ifr;
1139 int s;
1140
1141 memset(&ifr, 0, sizeof(ifr));
1142 strlcpy(ifr.ifr_name, if_getname(interface), sizeof(ifr.ifr_name));
1143 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1144 break;
1145 if (ioctl(s, SIOCGIFSTAT_IN6, (caddr_t) & ifr) < 0) {
1146 close(s);
1147 break;
1148 }
1149 close(s);
1150 ifs6 = &ifr.ifr_ifru.ifru_stat;
1151 switch (vp->magic) {
1152 case IPV6IFSTATSINRCVS:
1153 long_return = ifs6->ifs6_in_receive;
1154 break;
1155 case IPV6IFSTATSINHDRERRS:
1156 long_return = ifs6->ifs6_in_hdrerr;
1157 break;
1158 case IPV6IFSTATSTOOBIGERRS:
1159 long_return = ifs6->ifs6_in_toobig;
1160 break;
1161 case IPV6IFSTATSINNOROUTES:
1162 long_return = ifs6->ifs6_in_noroute;
1163 break;
1164 case IPV6IFSTATSINADDRERRS:
1165 long_return = ifs6->ifs6_in_addrerr;
1166 break;
1167 case IPV6IFSTATSINUNKNOWPROTS:
1168 long_return = ifs6->ifs6_in_protounknown;
1169 break;
1170 case IPV6IFSTATSINTRUNCATPKTS:
1171 long_return = ifs6->ifs6_in_truncated;
1172 break;
1173 case IPV6IFSTATSINDISCARDS:
1174 long_return = ifs6->ifs6_in_discard;
1175 break;
1176 case IPV6IFSTATSINDELIVERS:
1177 long_return = ifs6->ifs6_in_deliver;
1178 break;
1179 case IPV6IFSTATSOUTFORWDATAS:
1180 long_return = ifs6->ifs6_out_forward;
1181 break;
1182 case IPV6IFSTATSOUTREQS:
1183 long_return = ifs6->ifs6_out_request;
1184 break;
1185 case IPV6IFSTATSOUTDISCARDS:
1186 long_return = ifs6->ifs6_out_discard;
1187 break;
1188 case IPV6IFSTATSOUTFRAGOKS:
1189 long_return = ifs6->ifs6_out_fragok;
1190 break;
1191 case IPV6IFSTATSOUTFRAGFAILS:
1192 long_return = ifs6->ifs6_out_fragfail;
1193 break;
1194 case IPV6IFSTATSOUTFRAGCREATS:
1195 long_return = ifs6->ifs6_out_fragcreat;
1196 break;
1197 case IPV6IFSTATSOUTREASMREQS:
1198 long_return = ifs6->ifs6_reass_reqd;
1199 break;
1200 case IPV6IFSTATSOUTREASMOKS:
1201 long_return = ifs6->ifs6_reass_ok;
1202 break;
1203 case IPV6IFSTATSOUTREASMFAILS:
1204 long_return = ifs6->ifs6_reass_fail;
1205 break;
1206 case IPV6IFSTATSINMCASTPKTS:
1207 long_return = ifs6->ifs6_in_mcast;
1208 break;
1209 case IPV6IFSTATSOUTMCASTPKTS:
1210 long_return = ifs6->ifs6_out_mcast;
1211 break;
1212 default:
1213 return NULL;
1214 }
1215 return (u_char *) & long_return;
1216 }
1217 #endif
1218 default:
1219 break;
1220 }
1221 return NULL;
1222 #endif
1223 }
1224
1225 u_char *
1226 var_icmpv6Entry(register struct variable * vp,
1227 oid * name,
1228 size_t * length,
1229 int exact, size_t * var_len, WriteMethod ** write_method)
1230 {
1231 #ifndef HAVE_IF_NAMEINDEX
1232 return NULL;
1233 #else
1234 int interface;
1235 int max;
1236
1237 /* Reload list of interfaces */
1238 if (if_initialize() < 0)
1239 return NULL;
1240
1241 max = if_maxifindex();
1242 if (max < 0)
1243 return NULL;
1244
1245 if (header_ipv6_scan
1246 (vp, name, length, exact, var_len, write_method, 1, max)
1247 == MATCH_FAILED) {
1248 return NULL;
1249 }
1250 interface = name[*length - 1];
1251 DEBUGMSGTL(("mibII/ipv6", "interface: %d(%s)\n",
1252 interface, if_getname(interface)));
1253 if (interface >= max)
1254 return NULL;
1255
1256 switch (vp->magic) {
1257 #ifdef SIOCGIFSTAT_ICMP6
1258 case IPV6IFICMPINMSG:
1259 case IPV6IFICMPINERRORS:
1260 case IPV6IFICMPINDSTUNRCHS:
1261 case IPV6IFICMPINADMPROHS:
1262 case IPV6IFICMPINTIMEXCDS:
1263 case IPV6IFICMPINPARMPROBS:
1264 case IPV6IFICMPINPKTTOOBIGS:
1265 case IPV6IFICMPINECHOS:
1266 case IPV6IFICMPINECHOREPS:
1267 case IPV6IFICMPINRTRSLICITS:
1268 case IPV6IFICMPINRTRADVS:
1269 case IPV6IFICMPINNBRSLICITS:
1270 case IPV6IFICMPINNBRADVS:
1271 case IPV6IFICMPINREDIRECTS:
1272 case IPV6IFICMPINGRPMEQERYS:
1273 case IPV6IFICMPINGRPMERSPS:
1274 case IPV6IFICMPINGRPMEREDCS:
1275 case IPV6IFICMPOUTMSG:
1276 case IPV6IFICMPOUTERRORS:
1277 case IPV6IFICMPOUTDSTUNRCHS:
1278 case IPV6IFICMPOUTADMPROHS:
1279 case IPV6IFICMPOUTTIMEXCDS:
1280 case IPV6IFICMPOUTPARMPROBS:
1281 case IPV6IFICMPOUTPKTTOOBIGS:
1282 case IPV6IFICMPOUTECHOS:
1283 case IPV6IFICMPOUTECHOREPS:
1284 case IPV6IFICMPOUTRTRSLICITS:
1285 case IPV6IFICMPOUTRTRADVS:
1286 case IPV6IFICMPOUTNBRSLICITS:
1287 case IPV6IFICMPOUTNBRADVS:
1288 case IPV6IFICMPOUTREDIRECTS:
1289 case IPV6IFICMPOUTGRPMEQERYS:
1290 case IPV6IFICMPOUTGRPMERSPS:
1291 case IPV6IFICMPOUTGRPMEREDCS:
1292 {
1293 struct icmp6_ifstat *ifs6;
1294 struct in6_ifreq ifr;
1295 int s;
1296
1297 memset(&ifr, 0, sizeof(ifr));
1298 strlcpy(ifr.ifr_name, if_getname(interface), sizeof(ifr.ifr_name));
1299 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1300 break;
1301 if (ioctl(s, SIOCGIFSTAT_ICMP6, (caddr_t) & ifr) < 0) {
1302 close(s);
1303 break;
1304 }
1305 close(s);
1306 ifs6 = &ifr.ifr_ifru.ifru_icmp6stat;
1307 switch (vp->magic) {
1308 case IPV6IFICMPINMSG:
1309 long_return = ifs6->ifs6_in_msg;
1310 break;
1311 case IPV6IFICMPINERRORS:
1312 long_return = ifs6->ifs6_in_error;
1313 break;
1314 case IPV6IFICMPINDSTUNRCHS:
1315 long_return = ifs6->ifs6_in_dstunreach;
1316 break;
1317 case IPV6IFICMPINADMPROHS:
1318 long_return = ifs6->ifs6_in_adminprohib;
1319 break;
1320 case IPV6IFICMPINTIMEXCDS:
1321 long_return = ifs6->ifs6_in_timeexceed;
1322 break;
1323 case IPV6IFICMPINPARMPROBS:
1324 long_return = ifs6->ifs6_in_paramprob;
1325 break;
1326 case IPV6IFICMPINPKTTOOBIGS:
1327 long_return = ifs6->ifs6_in_pkttoobig;
1328 break;
1329 case IPV6IFICMPINECHOS:
1330 long_return = ifs6->ifs6_in_echo;
1331 break;
1332 case IPV6IFICMPINECHOREPS:
1333 long_return = ifs6->ifs6_in_echoreply;
1334 break;
1335 case IPV6IFICMPINRTRSLICITS:
1336 long_return = ifs6->ifs6_in_routersolicit;
1337 break;
1338 case IPV6IFICMPINRTRADVS:
1339 long_return = ifs6->ifs6_in_routeradvert;
1340 break;
1341 case IPV6IFICMPINNBRSLICITS:
1342 long_return = ifs6->ifs6_in_neighborsolicit;
1343 break;
1344 case IPV6IFICMPINNBRADVS:
1345 long_return = ifs6->ifs6_in_neighboradvert;
1346 break;
1347 case IPV6IFICMPINREDIRECTS:
1348 long_return = ifs6->ifs6_in_redirect;
1349 break;
1350 case IPV6IFICMPINGRPMEQERYS:
1351 long_return = ifs6->ifs6_in_mldquery;
1352 break;
1353 case IPV6IFICMPINGRPMERSPS:
1354 long_return = ifs6->ifs6_in_mldreport;
1355 break;
1356 case IPV6IFICMPINGRPMEREDCS:
1357 long_return = ifs6->ifs6_in_mlddone;
1358 break;
1359 case IPV6IFICMPOUTMSG:
1360 long_return = ifs6->ifs6_out_msg;
1361 break;
1362 case IPV6IFICMPOUTERRORS:
1363 long_return = ifs6->ifs6_out_error;
1364 break;
1365 case IPV6IFICMPOUTDSTUNRCHS:
1366 long_return = ifs6->ifs6_out_dstunreach;
1367 break;
1368 case IPV6IFICMPOUTADMPROHS:
1369 long_return = ifs6->ifs6_out_adminprohib;
1370 break;
1371 case IPV6IFICMPOUTTIMEXCDS:
1372 long_return = ifs6->ifs6_out_timeexceed;
1373 break;
1374 case IPV6IFICMPOUTPARMPROBS:
1375 long_return = ifs6->ifs6_out_paramprob;
1376 break;
1377 case IPV6IFICMPOUTPKTTOOBIGS:
1378 long_return = ifs6->ifs6_out_pkttoobig;
1379 break;
1380 case IPV6IFICMPOUTECHOS:
1381 long_return = ifs6->ifs6_out_echo;
1382 break;
1383 case IPV6IFICMPOUTECHOREPS:
1384 long_return = ifs6->ifs6_out_echoreply;
1385 break;
1386 case IPV6IFICMPOUTRTRSLICITS:
1387 long_return = ifs6->ifs6_out_routersolicit;
1388 break;
1389 case IPV6IFICMPOUTRTRADVS:
1390 long_return = ifs6->ifs6_out_routeradvert;
1391 break;
1392 case IPV6IFICMPOUTNBRSLICITS:
1393 long_return = ifs6->ifs6_out_neighborsolicit;
1394 break;
1395 case IPV6IFICMPOUTNBRADVS:
1396 long_return = ifs6->ifs6_out_neighboradvert;
1397 break;
1398 case IPV6IFICMPOUTREDIRECTS:
1399 long_return = ifs6->ifs6_out_redirect;
1400 break;
1401 case IPV6IFICMPOUTGRPMEQERYS:
1402 long_return = ifs6->ifs6_out_mldquery;
1403 break;
1404 case IPV6IFICMPOUTGRPMERSPS:
1405 long_return = ifs6->ifs6_out_mldreport;
1406 break;
1407 case IPV6IFICMPOUTGRPMEREDCS:
1408 long_return = ifs6->ifs6_out_mlddone;
1409 break;
1410 default:
1411 return NULL;
1412 }
1413 return (u_char *) & long_return;
1414 }
1415 #endif
1416 default:
1417 break;
1418 }
1419 return NULL;
1420 #endif
1421 }
1422
1423 #ifdef HAVE_KVM_GETFILES
1424
1425 u_char *
1426 var_udp6(register struct variable * vp,
1427 oid * name,
1428 size_t * length,
1429 int exact, size_t * var_len, WriteMethod ** write_method)
1430 {
1431 oid newname[MAX_OID_LEN];
1432 oid savname[MAX_OID_LEN];
1433 int result, count, found, savnameLen;
1434 int p, i, j;
1435 u_char *sa, *savsa;
1436 struct kinfo_file *udp;
1437
1438 udp = kvm_getfiles(kd, KERN_FILE_BYFILE, DTYPE_SOCKET, sizeof(struct kinfo_file), &count);
1439 found = savnameLen = 0;
1440 memcpy(newname, vp->name, (int) vp->namelen * sizeof(oid));
1441 for (p = 0; p < count; p++) {
1442 if (udp[p].so_protocol != IPPROTO_UDP || udp[p].so_family != AF_INET6)
1443 continue;
1444 j = vp->namelen;
1445 sa = (u_char *)&udp[p].inp_laddru[0];
1446 for (i = 0; i < sizeof(struct in6_addr); i++)
1447 newname[j++] = sa[i];
1448 newname[j++] = ntohs(udp[p].inp_lport);
1449 if (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)sa))
1450 newname[j++] = ntohs(sa[2]);
1451 else
1452 newname[j++] = 0;
1453 DEBUGMSGTL(("mibII/ipv6", "var_udp6 new: %d %d ",
1454 (int) vp->namelen, j));
1455 DEBUGMSGOID(("mibII/ipv6", newname, j));
1456 DEBUGMSG(("mibII/ipv6", " %d\n", exact));
1457
1458 result = snmp_oid_compare(name, *length, newname, j);
1459 if (exact && result == 0) {
1460 savnameLen = j;
1461 memcpy(savname, newname, j * sizeof(oid));
1462 savsa = sa;
1463 found++;
1464 break;
1465 } else if (!exact && result < 0) {
1466 /*
1467 * take the least greater one
1468 */
1469 if (savnameLen == 0 || snmp_oid_compare(savname, savnameLen, newname, j) > 0) {
1470 savnameLen = j;
1471 savsa = sa;
1472 memcpy(savname, newname, j * sizeof(oid));
1473 found++;
1474 }
1475 }
1476 }
1477 DEBUGMSGTL(("mibII/ipv6", "found=%d\n", found));
1478 if (!found)
1479 return NULL;
1480 *length = savnameLen;
1481 memcpy((char *) name, (char *) savname, *length * sizeof(oid));
1482 *write_method = 0;
1483 *var_len = sizeof(long); /* default to 'long' results */
1484
1485 /*
1486 * DEBUGMSGTL(("mibII/ipv6", "var_udp6 found: "));
1487 * DEBUGMSGOID(("mibII/ipv6", name, *length));
1488 * DEBUGMSG(("mibII/ipv6", " %d\n", exact));
1489 */
1490 DEBUGMSGTL(("mibII/ipv6", "magic=%d\n", vp->magic));
1491 switch (vp->magic) {
1492 case IPV6UDPIFINDEX:
1493 if (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)savsa))
1494 long_return = ntohs(savsa[2]);
1495 else
1496 long_return = 0;
1497 return (u_char *) &long_return;
1498 default:
1499 break;
1500 }
1501 ERROR_MSG("");
1502
1503 return NULL;
1504 }
1505
1506 #else
1507
1508 u_char *
1509 var_udp6(register struct variable * vp,
1510 oid * name,
1511 size_t * length,
1512 int exact, size_t * var_len, WriteMethod ** write_method)
1513 {
1514 oid newname[MAX_OID_LEN];
1515 oid savname[MAX_OID_LEN];
1516 int result;
1517 int i, j;
1518 caddr_t p;
1519 #if defined(openbsd4) || defined(dragonfly)
1520 static struct inpcb in6pcb, savpcb;
1521 #elif defined(freebsd3)
1522 static struct xinpcb in6pcb, savpcb;
1523 #else
1524 static struct in6pcb in6pcb, savpcb;
1525 #endif
1526 int found, savnameLen;
1527 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106250000 || defined(openbsd4) /*1.6Y*/
1528 struct inpcbtable udbtable;
1529 caddr_t first;
1530 #elif defined(dragonfly)
1531 char *sysctl_buf;
1532 struct xinpcb *xig;
1533 size_t sysctl_len;
1534 #elif defined(freebsd3) || defined(darwin)
1535 char *sysctl_buf;
1536 struct xinpgen *xig, *oxig;
1537 #endif
1538
1539 DEBUGMSGTL(("mibII/ipv6", "var_udp6: "));
1540 DEBUGMSGOID(("mibII/ipv6", name, *length));
1541 DEBUGMSG(("mibII/ipv6", " %d\n", exact));
1542
1543 #if defined(__NetBSD__) && __NetBSD_Version__ >= 700000001
1544 if (!auto_nlist("udbtable", (char *) &udbtable, sizeof(udbtable)))
1545 return NULL;
1546 first = p = (caddr_t)udbtable.inpt_queue.tqh_first;
1547 #elif defined(__NetBSD__) && __NetBSD_Version__ >= 106250000 || defined(openbsd4) /*1.6Y*/
1548 if (!auto_nlist("udbtable", (char *) &udbtable, sizeof(udbtable)))
1549 return NULL;
1550 #if defined(openbsd5)
1551 first = p = (caddr_t)TAILQ_FIRST(&udbtable.inpt_queue);
1552 #else
1553 first = p = (caddr_t)udbtable.inpt_queue.cqh_first;
1554 #endif
1555 #elif defined(dragonfly)
1556 {
1557 const char *udblist = "net.inet.udp.pcblist";
1558 const char *pp = udblist;
1559
1560 if (sysctlbyname(udblist, 0, &sysctl_len, 0, 0) < 0)
1561 return NULL;
1562 if ((sysctl_buf = malloc(sysctl_len)) == NULL)
1563 return NULL;
1564 udblist = pp;
1565 if (sysctlbyname(udblist, sysctl_buf, &sysctl_len, 0, 0) < 0) {
1566 free(sysctl_buf);
1567 return NULL;
1568 }
1569 xig = (struct xinpcb *) sysctl_buf;
1570 if (xig->xi_len != sizeof(*xig)) {
1571 free(sysctl_buf);
1572 return NULL;
1573 }
1574 p = (caddr_t) ((char *) xig); /* silence compiler warning */
1575 }
1576 #elif !defined(freebsd3) && !defined(darwin)
1577 {
1578 static struct in6pcb udb6;
1579
1580 if (!auto_nlist("udb6", (char *) &udb6, sizeof(udb6)))
1581 return NULL;
1582 p = (caddr_t) udb6.in6p_next;
1583 }
1584 #else
1585 {
1586 const char *udblist = "net.inet.udp.pcblist";
1587 const char *pp = udblist;
1588 size_t len;
1589
1590 if (sysctlbyname(udblist, 0, &len, 0, 0) < 0)
1591 return NULL;
1592 if ((sysctl_buf = malloc(len)) == NULL)
1593 return NULL;
1594 udblist = pp;
1595 if (sysctlbyname(udblist, sysctl_buf, &len, 0, 0) < 0) {
1596 free(sysctl_buf);
1597 return NULL;
1598 }
1599 oxig = (struct xinpgen *) sysctl_buf;
1600 xig = (struct xinpgen *) ((char *) oxig + oxig->xig_len);
1601 p = (caddr_t) ((char *) xig); /* silence compiler warning */
1602 }
1603 #endif
1604 found = savnameLen = 0;
1605 memcpy((char *) newname, (char *) vp->name,
1606 (int) vp->namelen * sizeof(oid));
1607 DEBUGMSGTL(("mibII/ipv6", "start: p=%p\n", p));
1608 while (
1609 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106250000 || defined(openbsd4) /*1.6Y*/
1610 p
1611 #elif !defined(freebsd3) && !defined(darwin)
1612 p && (u_long) p != auto_nlist_value("udb6")
1613 #elif defined(dragonfly)
1614 (char *)xig + xig->xi_len <= sysctl_buf + sysctl_len && xig->xi_len != 0
1615 #else
1616 xig->xig_len > sizeof(struct xinpgen)
1617 #endif
1618 ) {
1619 DEBUGMSGTL(("mibII/ipv6", "looping: p=%p\n", p));
1620
1621 #if defined(freebsd3) &&!defined(dragonfly)
1622 in6pcb = *(struct xinpcb *) xig;
1623 #elif defined(darwin)
1624 in6pcb = ((struct xinpcb *) xig)->xi_inp;
1625 #else
1626 if (!NETSNMP_KLOOKUP(p, (char *) &in6pcb, sizeof(in6pcb))) {
1627 DEBUGMSGTL(("mibII/ipv6", "klookup fail for udb6 at %p\n",
1628 p));
1629 found = 0;
1630 break;
1631 }
1632 #endif
1633 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106250000 /*1.6Y*/
1634 # if __NetBSD_Version__ < 999010400
1635 if (in6pcb.in6p_af != AF_INET6)
1636 goto skip;
1637 # else
1638 if (in6pcb.in6p_pcb.inp_af != AF_INET6)
1639 goto skip;
1640 # endif
1641 #elif defined(INP_ISIPV6)
1642 if (!INP_ISIPV6(&in6pcb))
1643 goto skip;
1644 #elif defined(freebsd3) || defined(darwin)
1645 if (0 == (in6pcb.inp_vflag & INP_IPV6))
1646 goto skip;
1647 #elif defined(openbsd4)
1648 if (!(in6pcb.inp_flags & INP_IPV6))
1649 goto skip;
1650 #endif
1651
1652 j = (int) vp->namelen;
1653 #if defined(openbsd4)
1654 for (i = 0; i < sizeof(struct in6_addr); i++)
1655 newname[j++] = in6pcb.inp_laddr6.s6_addr[i];
1656 newname[j++] = ntohs(in6pcb.inp_lport);
1657 if (IN6_IS_ADDR_LINKLOCAL(&in6pcb.inp_laddr6))
1658 newname[j++] =
1659 ntohs(*(uint16_t *) &in6pcb.inp_laddr6.s6_addr[2]);
1660 else
1661 newname[j++] = 0;
1662 #elif defined(__NetBSD__) && __NetBSD_Version__ >= 999010400
1663 for (i = 0; i < sizeof(struct in6_addr); i++)
1664 newname[j++] = in6pcb.in6p_ip6.ip6_src.s6_addr[i];
1665 newname[j++] = ntohs(in6pcb.in6p_pcb.inp_lport);
1666 if (IN6_IS_ADDR_LINKLOCAL(&in6pcb.in6p_ip6.ip6_src))
1667 newname[j++] =
1668 ntohs(*(uint16_t *) &in6pcb.in6p_ip6.ip6_src.s6_addr[2]);
1669 else
1670 newname[j++] = 0;
1671 #else
1672 for (i = 0; i < sizeof(struct in6_addr); i++)
1673 newname[j++] = in6pcb.in6p_laddr.s6_addr[i];
1674 newname[j++] = ntohs(in6pcb.inp_lport);
1675 if (IN6_IS_ADDR_LINKLOCAL(&in6pcb.in6p_laddr))
1676 newname[j++] =
1677 ntohs(*(uint16_t *) &in6pcb.in6p_laddr.s6_addr[2]);
1678 else
1679 newname[j++] = 0;
1680 #endif
1681 /*XXX*/
1682 DEBUGMSGTL(("mibII/ipv6", "var_udp6 new: %d %d ",
1683 (int) vp->namelen, j));
1684 DEBUGMSGOID(("mibII/ipv6", newname, j));
1685 DEBUGMSG(("mibII/ipv6", " %d\n", exact));
1686
1687 result = snmp_oid_compare(name, *length, newname, j);
1688 if (exact && (result == 0)) {
1689 memcpy(&savpcb, &in6pcb, sizeof(savpcb));
1690 savnameLen = j;
1691 memcpy(savname, newname, j * sizeof(oid));
1692 found++;
1693 break;
1694 } else if (!exact && (result < 0)) {
1695 /*
1696 * take the least greater one
1697 */
1698 if ((savnameLen == 0) ||
1699 (snmp_oid_compare(savname, savnameLen, newname, j) > 0)) {
1700 memcpy(&savpcb, &in6pcb, sizeof(savpcb));
1701 savnameLen = j;
1702 memcpy(savname, newname, j * sizeof(oid));
1703 found++;
1704 }
1705 }
1706
1707 skip:
1708 #ifdef openbsd5
1709 p = (caddr_t)TAILQ_NEXT(&in6pcb, inp_queue);
1710 if (p == NULL) break;
1711 #elif defined(openbsd4)
1712 p = (caddr_t)in6pcb.inp_queue.cqe_next;
1713 if (p == first) break;
1714 #elif defined(__NetBSD__) && __NetBSD_Version__ >= 999010400
1715 p = (caddr_t)in6pcb.in6p_pcb.inp_queue.tqe_next;
1716 if (p == first) break;
1717 #elif defined(__NetBSD__) && __NetBSD_Version__ >= 700000001
1718 p = (caddr_t)in6pcb.in6p_queue.tqe_next;
1719 if (p == first) break;
1720 #elif defined(__NetBSD__) && __NetBSD_Version__ >= 106250000 /*1.6Y*/
1721 p = (caddr_t)in6pcb.in6p_queue.cqe_next;
1722 if (p == first) break;
1723 #elif defined(__DragonFly__)
1724 xig = (struct xinpcb *) ((char *) xig + xig->xi_len);
1725 #elif !defined(freebsd3) && !defined(darwin)
1726 p = (caddr_t)in6pcb.in6p_next;
1727 #else
1728 xig = (struct xinpgen *) ((char *) xig + xig->xig_len);
1729 #endif
1730 }
1731 #if defined(freebsd3) || defined(darwin)
1732 free(sysctl_buf);
1733 #endif
1734 DEBUGMSGTL(("mibII/ipv6", "found=%d\n", found));
1735 if (!found)
1736 return NULL;
1737 *length = savnameLen;
1738 memcpy((char *) name, (char *) savname, *length * sizeof(oid));
1739 memcpy(&in6pcb, &savpcb, sizeof(savpcb));
1740 *write_method = 0;
1741 *var_len = sizeof(long); /* default to 'long' results */
1742
1743 /*
1744 DEBUGMSGTL(("mibII/ipv6", "var_udp6 found: "));
1745 DEBUGMSGOID(("mibII/ipv6", name, *length));
1746 DEBUGMSG(("mibII/ipv6", " %d\n", exact));
1747 */
1748 DEBUGMSGTL(("mibII/ipv6", "magic=%d\n", vp->magic));
1749 switch (vp->magic) {
1750 case IPV6UDPIFINDEX:
1751 #if defined(openbsd4)
1752 if (IN6_IS_ADDR_LINKLOCAL(&in6pcb.inp_laddr6))
1753 long_return =
1754 ntohs(*(uint16_t *) & in6pcb.inp_laddr6.s6_addr[2]);
1755 else
1756 long_return = 0;
1757 #elif defined(__NetBSD__) && __NetBSD_Version__ >= 999010400
1758 if (IN6_IS_ADDR_LINKLOCAL(&in6pcb.in6p_ip6.ip6_src))
1759 long_return =
1760 ntohs(*(uint16_t *) & in6pcb.in6p_ip6.ip6_src.s6_addr[2]);
1761 else
1762 long_return = 0;
1763 #else
1764 if (IN6_IS_ADDR_LINKLOCAL(&in6pcb.in6p_laddr))
1765 long_return =
1766 ntohs(*(uint16_t *) & in6pcb.in6p_laddr.s6_addr[2]);
1767 else
1768 long_return = 0;
1769 #endif
1770 return (u_char *) & long_return;
1771 default:
1772 break;
1773 }
1774 ERROR_MSG("");
1775 return NULL;
1776 }
1777 #endif /* KVM_GETFILES */
1778
1779 #ifdef TCP6
1780 u_char *
1781 var_tcp6(register struct variable * vp,
1782 oid * name,
1783 size_t * length,
1784 int exact, size_t * var_len, WriteMethod ** write_method)
1785 {
1786 oid newname[MAX_OID_LEN];
1787 oid savname[MAX_OID_LEN];
1788 int result;
1789 int i, j;
1790 caddr_t p;
1791 static struct in6pcb in6pcb, savpcb;
1792 struct tcp6cb tcp6cb;
1793 int found, savnameLen;
1794 static int tcp6statemap[TCP6_NSTATES];
1795 static int initialized = 0;
1796 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106250000 /*1.6Y*/
1797 struct inpcbtable tcbtable;
1798 caddr_t first;
1799 #elif defined(freebsd3) || defined(darwin)
1800 char *sysctl_buf;
1801 struct xinpgen *xig, *oxig;
1802 #endif
1803
1804 if (!initialized) {
1805 tcp6statemap[TCP6S_CLOSED] = 1;
1806 tcp6statemap[TCP6S_LISTEN] = 2;
1807 tcp6statemap[TCP6S_SYN_SENT] = 3;
1808 tcp6statemap[TCP6S_SYN_RECEIVED] = 4;
1809 tcp6statemap[TCP6S_ESTABLISHED] = 5;
1810 tcp6statemap[TCP6S_CLOSE_WAIT] = 8;
1811 tcp6statemap[TCP6S_FIN_WAIT_1] = 6;
1812 tcp6statemap[TCP6S_CLOSING] = 10;
1813 tcp6statemap[TCP6S_LAST_ACK] = 9;
1814 tcp6statemap[TCP6S_FIN_WAIT_2] = 7;
1815 tcp6statemap[TCP6S_TIME_WAIT] = 11;
1816 initialized++;
1817 }
1818
1819 DEBUGMSGTL(("mibII/ipv6", "var_tcp6: "));
1820 DEBUGMSGOID(("mibII/ipv6", name, *length));
1821 DEBUGMSG(("mibII/ipv6", " %d\n", exact));
1822
1823 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106250000 /*1.6Y*/
1824 if (!auto_nlist("tcbtable", (char *) &tcbtable, sizeof(tcbtable)))
1825 return NULL;
1826 first = p = (caddr_t)tcbtable.inpt_queue.cqh_first;
1827 #elif !defined(freebsd3) && !defined(darwin)
1828 {
1829 static struct in6pcb tcb6;
1830
1831 if (!auto_nlist("tcb6", (char *) &tcb6, sizeof(tcb6)))
1832 return NULL;
1833 p = (caddr_t) tcb6.in6p_next;
1834 }
1835 #else
1836 {
1837 const char *tcblist = "net.inet.tcp.pcblist";
1838 const char *pp = tcblist;
1839 size_t len;
1840
1841 if (sysctlbyname(tcblist, 0, &len, 0, 0) < 0)
1842 return NULL;
1843 if ((sysctl_buf = malloc(len)) == NULL)
1844 return NULL;
1845 tcblist = pp;
1846 if (sysctlbyname(tcblist, sysctl_buf, &len, 0, 0) < 0) {
1847 free(sysctl_buf);
1848 return NULL;
1849 }
1850 oxig = (struct xinpgen *) sysctl_buf;
1851 xig = (struct xinpgen *) ((char *) oxig + oxig->xig_len);
1852 p = (caddr_t) ((char *) xig); /* silence compiler warning */
1853 }
1854 #endif
1855 found = savnameLen = 0;
1856 memcpy((char *) newname, (char *) vp->name,
1857 (int) vp->namelen * sizeof(oid));
1858 DEBUGMSGTL(("mibII/ipv6", "start: p=%x\n", p));
1859 while (
1860 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106250000 /*1.6Y*/
1861 p && p != first
1862 #elif !defined(freebsd3) && !defined(darwin)
1863 p && (u_long) p != auto_nlist_value("tcb6")
1864 #else
1865 xig->xig_len > sizeof(struct xinpgen)
1866 #endif
1867 ) {
1868 DEBUGMSGTL(("mibII/ipv6", "looping: p=%x\n", p));
1869
1870 #if !defined(freebsd3) && !defined(darwin)
1871 if (!NETSNMP_KLOOKUP(p, (char *) &in6pcb, sizeof(in6pcb))) {
1872 DEBUGMSGTL(("mibII/ipv6", "klookup fail for tcb6 at %x\n",
1873 p));
1874 found = 0;
1875 break;
1876 }
1877 #else
1878 in6pcb = ((struct xinpcb *) xig)->xi_inp;
1879 #endif
1880 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106250000 /*1.6Y*/
1881 if (in6pcb.in6p_af != AF_INET6)
1882 goto skip;
1883 #elif defined(INP_ISIPV6)
1884 if (!INP_ISIPV6(&in6pcb))
1885 goto skip;
1886 #elif defined(freebsd3) || defined(darwin)
1887 if (0 == (in6pcb.inp_vflag & INP_IPV6))
1888 goto skip;
1889 #endif
1890 if (!NETSNMP_KLOOKUP(in6pcb.in6p_ppcb, (char *) &tcp6cb, sizeof(tcp6cb))) {
1891 DEBUGMSGTL(("mibII/ipv6", "klookup fail for tcb6.tcp6cb at %x\n",
1892 in6pcb.in6p_ppcb));
1893 found = 0;
1894 break;
1895 }
1896 j = (int) vp->namelen;
1897 for (i = 0; i < sizeof(struct in6_addr); i++)
1898 newname[j++] = in6pcb.in6p_laddr.s6_addr[i];
1899 newname[j++] = ntohs(in6pcb.inp_lport);
1900 for (i = 0; i < sizeof(struct in6_addr); i++)
1901 newname[j++] = in6pcb.in6p_faddr.s6_addr[i];
1902 newname[j++] = ntohs(in6pcb.inp_fport);
1903 if (IN6_IS_ADDR_LINKLOCAL(&in6pcb.in6p_laddr))
1904 newname[j++] =
1905 ntohs(*(uint16_t *) & in6pcb.in6p_laddr.s6_addr[2]);
1906 else
1907 newname[j++] = 0;
1908
1909 DEBUGMSGTL(("mibII/ipv6", "var_tcp6 new: %d %d ",
1910 (int) vp->namelen, j));
1911 DEBUGMSGOID(("mibII/ipv6", newname, j));
1912 DEBUGMSG(("mibII/ipv6", " %d\n", exact));
1913
1914 #if 1 /* this is very odd but sometimes happen, and cause infinite loop */
1915 if (ntohs(in6pcb.inp_lport) == 0)
1916 goto skip;
1917 #endif
1918
1919 result = snmp_oid_compare(name, *length, newname, j);
1920 if (exact && (result == 0)) {
1921 memcpy(&savpcb, &in6pcb, sizeof(savpcb));
1922 savnameLen = j;
1923 memcpy(savname, newname, j * sizeof(oid));
1924 found++;
1925 break;
1926 } else if (!exact && (result < 0)) {
1927 /*
1928 * take the least greater one
1929 */
1930 if ((savnameLen == 0) ||
1931 (snmp_oid_compare(savname, savnameLen, newname, j) > 0)) {
1932 memcpy(&savpcb, &in6pcb, sizeof(savpcb));
1933 savnameLen = j;
1934 memcpy(savname, newname, j * sizeof(oid));
1935 found++;
1936 }
1937 }
1938
1939 skip:
1940 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106250000 /*1.6Y*/
1941 p = (caddr_t)in6pcb.in6p_queue.cqe_next;
1942 #elif !defined(freebsd3) && !defined(darwin)
1943 p = (caddr_t)in6pcb.in6p_next;
1944 #else
1945 xig = (struct xinpgen *) ((char *) xig + xig->xig_len);
1946 #endif
1947 }
1948 #if defined(freebsd3) || defined(darwin)
1949 free(sysctl_buf);
1950 #endif
1951 DEBUGMSGTL(("mibII/ipv6", "found=%d\n", found));
1952 if (!found)
1953 return NULL;
1954 *length = savnameLen;
1955 memcpy((char *) name, (char *) savname, *length * sizeof(oid));
1956 memcpy(&in6pcb, &savpcb, sizeof(savpcb));
1957 *write_method = 0;
1958 *var_len = sizeof(long); /* default to 'long' results */
1959
1960 /*
1961 DEBUGMSGTL(("mibII/ipv6", "var_tcp6 found: "));
1962 DEBUGMSGOID(("mibII/ipv6", name, *length));
1963 DEBUGMSG(("mibII/ipv6", " %d\n", exact));
1964 */
1965 DEBUGMSGTL(("mibII/ipv6", "magic=%d\n", vp->magic));
1966 switch (vp->magic) {
1967 case IPV6TCPCONNSTATE:
1968 long_return = tcp6statemap[in6pcb.t_state];
1969 return (u_char *) & long_return;
1970 default:
1971 break;
1972 }
1973 ERROR_MSG("");
1974 return NULL;
1975 }
1976
1977 #elif defined(HAVE_KVM_GETFILES)
1978
1979 u_char *
1980 var_tcp6(register struct variable * vp,
1981 oid * name,
1982 size_t * length,
1983 int exact, size_t * var_len, WriteMethod ** write_method)
1984 {
1985 oid newname[MAX_OID_LEN];
1986 oid savname[MAX_OID_LEN];
1987 int result, count, found, savnameLen, savstate;
1988 int p, i, j;
1989 u_char *lsa, *fsa;
1990 struct kinfo_file *tcp;
1991 static int tcp6statemap[16];
1992 static int initialized = 0;
1993
1994 if (!initialized) {
1995 tcp6statemap[TCPS_CLOSED] = 1;
1996 tcp6statemap[TCPS_LISTEN] = 2;
1997 tcp6statemap[TCPS_SYN_SENT] = 3;
1998 tcp6statemap[TCPS_SYN_RECEIVED] = 4;
1999 tcp6statemap[TCPS_ESTABLISHED] = 5;
2000 tcp6statemap[TCPS_CLOSE_WAIT] = 8;
2001 tcp6statemap[TCPS_FIN_WAIT_1] = 6;
2002 tcp6statemap[TCPS_CLOSING] = 10;
2003 tcp6statemap[TCPS_LAST_ACK] = 9;
2004 tcp6statemap[TCPS_FIN_WAIT_2] = 7;
2005 tcp6statemap[TCPS_TIME_WAIT] = 11;
2006 initialized++;
2007 }
2008
2009 tcp = kvm_getfiles(kd, KERN_FILE_BYFILE, DTYPE_SOCKET, sizeof(struct kinfo_file), &count);
2010 found = savnameLen = 0;
2011 memcpy(newname, vp->name, (int) vp->namelen * sizeof(oid));
2012 for (p = 0; p < count; p++) {
2013 if (tcp[p].so_protocol != IPPROTO_TCP || tcp[p].so_family != AF_INET6)
2014 continue;
2015 j = vp->namelen;
2016 lsa = (u_char *)&tcp[p].inp_laddru[0];
2017 for (i = 0; i < sizeof(struct in6_addr); i++)
2018 newname[j++] = lsa[i];
2019 newname[j++] = ntohs(tcp[p].inp_lport);
2020 fsa = (u_char *)&tcp[p].inp_faddru[0];
2021 for (i = 0; i < sizeof(struct in6_addr); i++)
2022 newname[j++] = fsa[i];
2023 newname[j++] = ntohs(tcp[p].inp_fport);
2024 if (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)lsa))
2025 newname[j++] = ntohs(lsa[2]);
2026 else
2027 newname[j++] = 0;
2028 DEBUGMSGTL(("mibII/ipv6", "var_udp6 new: %d %d ",
2029 (int) vp->namelen, j));
2030 DEBUGMSGOID(("mibII/ipv6", newname, j));
2031 DEBUGMSG(("mibII/ipv6", " %d\n", exact));
2032
2033 result = snmp_oid_compare(name, *length, newname, j);
2034 if (exact && result == 0) {
2035 savnameLen = j;
2036 memcpy(savname, newname, j * sizeof(oid));
2037 savstate = tcp[p].t_state;
2038 found++;
2039 break;
2040 } else if (!exact && result < 0) {
2041 /*
2042 * take the least greater one
2043 */
2044 if (savnameLen == 0 || snmp_oid_compare(savname, savnameLen, newname, j) > 0) {
2045 savnameLen = j;
2046 memcpy(savname, newname, j * sizeof(oid));
2047 savstate = tcp[p].t_state;
2048 found++;
2049 }
2050 }
2051 }
2052 DEBUGMSGTL(("mibII/ipv6", "found=%d\n", found));
2053 if (!found)
2054 return NULL;
2055 *length = savnameLen;
2056 memcpy((char *) name, (char *) savname, *length * sizeof(oid));
2057 *write_method = 0;
2058 *var_len = sizeof(long); /* default to 'long' results */
2059
2060 /*
2061 * DEBUGMSGTL(("mibII/ipv6", "var_udp6 found: "));
2062 * DEBUGMSGOID(("mibII/ipv6", name, *length));
2063 * DEBUGMSG(("mibII/ipv6", " %d\n", exact));
2064 */
2065 DEBUGMSGTL(("mibII/ipv6", "magic=%d\n", vp->magic));
2066 switch (vp->magic) {
2067 case IPV6TCPCONNSTATE:
2068 long_return = tcp6statemap[savstate & 0x0F];
2069 return (u_char *) &long_return;
2070 default:
2071 break;
2072 }
2073 ERROR_MSG("");
2074 return NULL;
2075 }
2076
2077 #else /* ! TCP6 */
2078
2079 static int mapTcpState( int val)
2080 {
2081 static int tcpstatemap[16 /*TCP_NSTATES*/];
2082 static int initialized = 0;
2083
2084 if (!initialized) {
2085 memset(tcpstatemap, 0, sizeof(tcpstatemap));
2086
2087 tcpstatemap[TCPS_CLOSED] = 1;
2088 tcpstatemap[TCPS_LISTEN] = 2;
2089 tcpstatemap[TCPS_SYN_SENT] = 3;
2090 tcpstatemap[TCPS_SYN_RECEIVED] = 4;
2091 tcpstatemap[TCPS_ESTABLISHED] = 5;
2092 tcpstatemap[TCPS_CLOSE_WAIT] = 8;
2093 tcpstatemap[TCPS_FIN_WAIT_1] = 6;
2094 tcpstatemap[TCPS_CLOSING] = 10;
2095 tcpstatemap[TCPS_LAST_ACK] = 9;
2096 tcpstatemap[TCPS_FIN_WAIT_2] = 7;
2097 tcpstatemap[TCPS_TIME_WAIT] = 11;
2098 initialized++;
2099 }
2100 /* XXX GIGO 0 is an invalid state */
2101 return (tcpstatemap[0x0f & val]);
2102 }
2103
2104 u_char *
2105 var_tcp6(register struct variable * vp,
2106 oid * name,
2107 size_t * length,
2108 int exact, size_t * var_len, WriteMethod ** write_method)
2109 {
2110 oid newname[MAX_OID_LEN];
2111 oid savname[MAX_OID_LEN];
2112 int result;
2113 int i, j;
2114 caddr_t p;
2115 #if defined(openbsd4) || defined(dragonfly)
2116 static struct inpcb in6pcb, savpcb;
2117 #elif defined(freebsd3)
2118 static struct xinpcb in6pcb;
2119 static int savstate;
2120 #else
2121 static struct in6pcb in6pcb, savpcb;
2122 #endif
2123 #if !defined(freebsd3) || defined(dragonfly)
2124 struct tcpcb tcpcb;
2125 #endif
2126 int state;
2127 int found, savnameLen;
2128 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106250000 || defined(openbsd4) /*1.6Y*/
2129 struct inpcbtable tcbtable;
2130 caddr_t first;
2131 #elif defined(dragonfly)
2132 char *sysctl_buf;
2133 size_t sysctl_len;
2134 struct xtcpcb *xtp;
2135 #elif defined(freebsd3) || defined(darwin)
2136 char *sysctl_buf;
2137 struct xinpgen *xig, *oxig;
2138 #endif
2139
2140 DEBUGMSGTL(("mibII/ipv6", "var_tcp6: "));
2141 DEBUGMSGOID(("mibII/ipv6", name, *length));
2142 DEBUGMSG(("mibII/ipv6", " %d\n", exact));
2143
2144 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106250000 || defined(openbsd4) /*1.6Y*/
2145 if (!auto_nlist("tcbtable", (char *) &tcbtable, sizeof(tcbtable)))
2146 return NULL;
2147 #ifdef openbsd5
2148 first = p = (caddr_t)TAILQ_FIRST(&tcbtable.inpt_queue);
2149 #elif defined(__NetBSD__) && __NetBSD_Version__ >= 700000001
2150 first = p = (caddr_t)tcbtable.inpt_queue.tqh_first;
2151 #else
2152 first = p = (caddr_t)tcbtable.inpt_queue.cqh_first;
2153 #endif
2154 #elif defined(dragonfly)
2155 {
2156 const char *tcblist = "net.inet.tcp.pcblist";
2157 const char *pp = tcblist;
2158
2159 if (sysctlbyname(tcblist, 0, &sysctl_len, 0, 0) < 0)
2160 return NULL;
2161 if ((sysctl_buf = malloc(sysctl_len)) == NULL)
2162 return NULL;
2163 tcblist = pp;
2164 if (sysctlbyname(tcblist, sysctl_buf, &sysctl_len, 0, 0) < 0) {
2165 free(sysctl_buf);
2166 return NULL;
2167 }
2168 xtp = (struct xtcpcb *) sysctl_buf;
2169 if (xtp->xt_len != sizeof(*xtp)) {
2170 free(sysctl_buf);
2171 return NULL;
2172 }
2173 p = (caddr_t) ((char *) xtp); /* silence compiler warning */
2174 }
2175 #elif !defined(freebsd3) && !defined(darwin)
2176 {
2177 static struct in6pcb tcb6;
2178
2179 if (!auto_nlist("tcb6", (char *) &tcb6, sizeof(tcb6)))
2180 return NULL;
2181 p = (caddr_t) tcb6.in6p_next;
2182 }
2183 #else
2184 {
2185 const char *tcblist = "net.inet.tcp.pcblist";
2186 const char *pp = tcblist;
2187 size_t len;
2188
2189 if (sysctlbyname(tcblist, 0, &len, 0, 0) < 0)
2190 return NULL;
2191 if ((sysctl_buf = malloc(len)) == NULL)
2192 return NULL;
2193 tcblist = pp;
2194 if (sysctlbyname(tcblist, sysctl_buf, &len, 0, 0) < 0) {
2195 free(sysctl_buf);
2196 return NULL;
2197 }
2198 oxig = (struct xinpgen *) sysctl_buf;
2199 xig = (struct xinpgen *) ((char *) oxig + oxig->xig_len);
2200 p = (caddr_t) ((char *) xig); /* silence compiler warning */
2201 }
2202 #endif
2203 found = savnameLen = 0;
2204 memcpy((char *) newname, (char *) vp->name,
2205 (int) vp->namelen * sizeof(oid));
2206 DEBUGMSGTL(("mibII/ipv6", "start: p=%p\n", p));
2207 while (
2208 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106250000 || defined(openbsd4) /*1.6Y*/
2209 p
2210 #elif !defined(freebsd3) && !defined(darwin)
2211 p && (u_long) p != auto_nlist_value("tcb6")
2212 #elif defined(dragonfly)
2213 (char *)xtp + xtp->xt_len < sysctl_buf + sysctl_len
2214 #else
2215 xig->xig_len > sizeof(struct xinpgen)
2216 #endif
2217 ) {
2218 DEBUGMSGTL(("mibII/ipv6", "looping: p=%p\n", p));
2219
2220 #if defined(dragonfly)
2221 in6pcb = xtp->xt_inp;
2222 #elif defined(freebsd3)
2223 in6pcb = ((struct xtcpcb *) xig)->xt_inp;
2224 #elif defined(darwin)
2225 in6pcb = ((struct xinpcb *) xig)->xi_inp;
2226 #else
2227 if (!NETSNMP_KLOOKUP(p, (char *) &in6pcb, sizeof(in6pcb))) {
2228 DEBUGMSGTL(("mibII/ipv6", "klookup fail for tcb6 at %p\n",
2229 p));
2230 found = 0;
2231 break;
2232 }
2233 #endif
2234 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106250000 /*1.6Y*/
2235 # if __NetBSD_Version__ < 999010400
2236 if (in6pcb.in6p_af != AF_INET6)
2237 goto skip;
2238 # else
2239 if (in6pcb.in6p_pcb.inp_af != AF_INET6)
2240 goto skip;
2241 # endif
2242 #elif defined(INP_ISIPV6)
2243 if (!INP_ISIPV6(&in6pcb))
2244 goto skip;
2245 #elif defined(freebsd3) || defined(darwin)
2246 if (0 == (in6pcb.inp_vflag & INP_IPV6))
2247 goto skip;
2248 #elif defined(openbsd4)
2249 if (0 == (in6pcb.inp_flags & INP_IPV6))
2250 goto skip;
2251 #endif
2252 j = (int) vp->namelen;
2253 #if defined(openbsd4)
2254 for (i = 0; i < sizeof(struct in6_addr); i++)
2255 newname[j++] = in6pcb.inp_laddr6.s6_addr[i];
2256 newname[j++] = ntohs(in6pcb.inp_lport);
2257 for (i = 0; i < sizeof(struct in6_addr); i++)
2258 newname[j++] = in6pcb.inp_faddr6.s6_addr[i];
2259 newname[j++] = ntohs(in6pcb.inp_fport);
2260 if (IN6_IS_ADDR_LINKLOCAL(&in6pcb.inp_laddr6))
2261 newname[j++] =
2262 ntohs(*(uint16_t *) &in6pcb.inp_laddr6.s6_addr[2]);
2263 else
2264 newname[j++] = 0;
2265 #elif defined(__NetBSD__) && __NetBSD_Version__ >= 999010400
2266 for (i = 0; i < sizeof(struct in6_addr); i++)
2267 newname[j++] = in6pcb.in6p_ip6.ip6_src.s6_addr[i];
2268 newname[j++] = ntohs(in6pcb.in6p_pcb.inp_lport);
2269 for (i = 0; i < sizeof(struct in6_addr); i++)
2270 newname[j++] = in6pcb.in6p_ip6.ip6_dst.s6_addr[i];
2271 newname[j++] = ntohs(in6pcb.in6p_pcb.inp_fport);
2272 if (IN6_IS_ADDR_LINKLOCAL(&in6pcb.in6p_ip6.ip6_src))
2273 newname[j++] =
2274 ntohs(*(uint16_t *) &in6pcb.in6p_ip6.ip6_src.s6_addr[2]);
2275 else
2276 newname[j++] = 0;
2277 #else
2278 for (i = 0; i < sizeof(struct in6_addr); i++)
2279 newname[j++] = in6pcb.in6p_laddr.s6_addr[i];
2280 newname[j++] = ntohs(in6pcb.inp_lport);
2281 for (i = 0; i < sizeof(struct in6_addr); i++)
2282 newname[j++] = in6pcb.in6p_faddr.s6_addr[i];
2283 newname[j++] = ntohs(in6pcb.inp_fport);
2284 if (IN6_IS_ADDR_LINKLOCAL(&in6pcb.in6p_laddr))
2285 newname[j++] =
2286 ntohs(*(uint16_t *) &in6pcb.in6p_laddr.s6_addr[2]);
2287 else
2288 newname[j++] = 0;
2289 #endif
2290
2291 DEBUGMSGTL(("mibII/ipv6", "var_tcp6 new: %d %d ",
2292 (int) vp->namelen, j));
2293 DEBUGMSGOID(("mibII/ipv6", newname, j));
2294 DEBUGMSG(("mibII/ipv6", " %d\n", exact));
2295
2296 #if 1 /* this is very odd but sometimes happen, and cause infinite loop */
2297 #if defined(__NetBSD__) && __NetBSD_Version__ >= 999010400
2298 if (ntohs(in6pcb.in6p_pcb.inp_lport) == 0)
2299 #else
2300 if (ntohs(in6pcb.inp_lport) == 0)
2301 #endif
2302 goto skip;
2303 #endif
2304 result = snmp_oid_compare(name, *length, newname, j);
2305 if (exact && (result == 0)) {
2306 #if defined(freebsd3) && !defined(dragonfly)
2307 savstate = ((struct xtcpcb *) xig)->t_state;
2308 #else
2309 memcpy(&savpcb, &in6pcb, sizeof(savpcb));
2310 #endif
2311 savnameLen = j;
2312 memcpy(savname, newname, j * sizeof(oid));
2313 found++;
2314 break;
2315 } else if (!exact && (result < 0)) {
2316 /*
2317 * take the least greater one
2318 */
2319 if ((savnameLen == 0) ||
2320 (snmp_oid_compare(savname, savnameLen, newname, j) > 0)) {
2321 #if defined(freebsd3) && !defined(dragonfly)
2322 savstate = ((struct xtcpcb *) xig)->t_state;
2323 #else
2324 memcpy(&savpcb, &in6pcb, sizeof(savpcb));
2325 #endif
2326 savnameLen = j;
2327 memcpy(savname, newname, j * sizeof(oid));
2328 found++;
2329 }
2330 }
2331
2332 skip:
2333 #ifdef openbsd5
2334 p = (caddr_t)TAILQ_NEXT(&in6pcb, inp_queue);
2335 if (p == NULL) break;
2336 #elif defined(openbsd4)
2337 p = (caddr_t)in6pcb.inp_queue.cqe_next;
2338 if (p == first) break;
2339 #elif defined(__NetBSD__) && __NetBSD_Version__ >= 999010400
2340 p = (caddr_t)in6pcb.in6p_pcb.inp_queue.tqe_next;
2341 if (p == first) break;
2342 #elif defined(__NetBSD__) && __NetBSD_Version__ >= 700000001
2343 p = (caddr_t)in6pcb.in6p_queue.tqe_next;
2344 if (p == first) break;
2345 #elif defined(__NetBSD__) && __NetBSD_Version__ >= 106250000 || defined(openbsd4) /*1.6Y*/
2346 p = (caddr_t)in6pcb.in6p_queue.cqe_next;
2347 if (p == first) break;
2348 #elif defined(dragonfly)
2349 xtp = (struct xtcpcb *) ((char *)xtp + xtp->xt_len);
2350 #elif !defined(freebsd3) && !defined(darwin)
2351 p = (caddr_t) in6pcb.in6p_next;
2352 #else
2353 xig = (struct xinpgen *) ((char *) xig + xig->xig_len);
2354 #endif
2355 }
2356 #if defined(freebsd3) || defined(darwin)
2357 free(sysctl_buf);
2358 #endif
2359 DEBUGMSGTL(("mibII/ipv6", "found=%d\n", found));
2360 if (!found)
2361 return NULL;
2362 *length = savnameLen;
2363 memcpy((char *) name, (char *) savname, *length * sizeof(oid));
2364 #if defined(freebsd3) && !defined(dragonfly)
2365 state = savstate;
2366 #elif defined(__NetBSD__) && __NetBSD_Version__ >= 999010400
2367 memcpy(&in6pcb, &savpcb, sizeof(savpcb));
2368 if (!NETSNMP_KLOOKUP(in6pcb.in6p_pcb.inp_ppcb, (char *) &tcpcb, sizeof(tcpcb))) {
2369 DEBUGMSGTL(("mibII/ipv6", "klookup fail for tcb6.tcpcb at %p\n",
2370 in6pcb.in6p_pcb.inp_ppcb));
2371 found = 0;
2372 return NULL;
2373 }
2374 state = (int)tcpcb.t_state;
2375 #else
2376 memcpy(&in6pcb, &savpcb, sizeof(savpcb));
2377 if (!NETSNMP_KLOOKUP(in6pcb.inp_ppcb, (char *) &tcpcb, sizeof(tcpcb))) {
2378 DEBUGMSGTL(("mibII/ipv6", "klookup fail for tcb6.tcpcb at %p\n",
2379 in6pcb.inp_ppcb));
2380 found = 0;
2381 return NULL;
2382 }
2383 state = (int)tcpcb.t_state;
2384 #endif
2385 *write_method = 0;
2386 *var_len = sizeof(long); /* default to 'long' results */
2387
2388 /*
2389 DEBUGMSGTL(("mibII/ipv6", "var_tcp6 found: "));
2390 DEBUGMSGOID(("mibII/ipv6", name, *length));
2391 DEBUGMSG(("mibII/ipv6", " %d\n", exact));
2392 */
2393 DEBUGMSGTL(("mibII/ipv6", "magic=%d\n", vp->magic));
2394 switch (vp->magic) {
2395 case IPV6TCPCONNSTATE:
2396 long_return = mapTcpState(state);
2397 return (u_char *) & long_return;
2398 default:
2399 break;
2400 }
2401 ERROR_MSG("");
2402 return NULL;
2403 }
2404
2405 #endif /*TCP6 */
2406
2407 #else /* !linux / linux */
2408
2409 /*
2410 * Linux dependent part
2411 */
2412 static unsigned long
2413 linux_read_ip6_stat_ulong(const char *file)
2414 {
2415 FILE *f;
2416 unsigned long value;
2417 f = fopen(file, "r");
2418 if (!f)
2419 return 0;
2420 if (fscanf(f, "%lu", &value) != 1) {
2421 fclose(f);
2422 return 0;
2423 }
2424 fclose(f);
2425 return value;
2426 }
2427
2428 static void
2429 linux_read_ip6_stat(struct ip6_mib *ip6stat)
2430 {
2431 if (!ip6stat)
2432 return;
2433 memset(ip6stat, 0, sizeof(*ip6stat));
2434 ip6stat->Ipv6Forwarding =
2435 linux_read_ip6_stat_ulong
2436 ("/proc/sys/net/ipv6/conf/all/forwarding");
2437 ip6stat->Ipv6DefaultHopLimit =
2438 linux_read_ip6_stat_ulong
2439 ("/proc/sys/net/ipv6/conf/default/hop_limit");
2440 }
2441
2442 u_char *
2443 var_ipv6(register struct variable *vp,
2444 oid * name,
2445 size_t * length,
2446 int exact, size_t * var_len, WriteMethod ** write_method)
2447 {
2448 static struct ip6_mib ip6stat;
2449
2450 if (header_ipv6(vp, name, length, exact, var_len, write_method)
2451 == MATCH_FAILED) {
2452 return NULL;
2453 }
2454 linux_read_ip6_stat(&ip6stat);
2455
2456 switch (vp->magic) {
2457 case IPV6DEFAULTHOPLIMIT:
2458 return (u_char *) & ip6stat.Ipv6DefaultHopLimit;
2459 case IPV6FORWARDING:
2460 long_return = (ip6stat.Ipv6Forwarding) ? 1 : 2;
2461 return (u_char *) & long_return;
2462 case IPV6INTERFACES:
2463 #ifdef HAVE_IF_NAMEINDEX
2464 long_return = if_countifindex();
2465 if (long_return < 0)
2466 break;
2467 return (u_char *) & long_return;
2468 #endif
2469 break;
2470 default:
2471 DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_ipv6\n",
2472 vp->magic));
2473 }
2474 return NULL;
2475 }
2476
2477 u_char *
2478 var_ifv6Entry(register struct variable * vp,
2479 oid * name,
2480 size_t * length,
2481 int exact, size_t * var_len, WriteMethod ** write_method)
2482 {
2483 #ifndef HAVE_IF_NAMEINDEX
2484 return NULL;
2485 #else
2486 int interface;
2487 int max;
2488 char *p;
2489 struct ifreq ifr;
2490 int s;
2491
2492 /* Reload list of interfaces */
2493 if (if_initialize() < 0)
2494 return NULL;
2495
2496 max = if_maxifindex();
2497 if (max < 0)
2498 return NULL;
2499
2500 if (header_ipv6_scan
2501 (vp, name, length, exact, var_len, write_method, 1, max)
2502 == MATCH_FAILED) {
2503 return NULL;
2504 }
2505 interface = name[*length - 1];
2506 DEBUGMSGTL(("mibII/ipv6", "interface: %d(%s)\n",
2507 interface, if_getname(interface)));
2508 if (interface > max)
2509 return NULL;
2510
2511 switch (vp->magic) {
2512 case IPV6IFDESCR:
2513 p = if_getname(interface);
2514 if (p) {
2515 *var_len = strlen(p);
2516 return (u_char *) p;
2517 }
2518 break;
2519 case IPV6IFLOWLAYER:
2520 /*
2521 * should check if type, this is a hard one...
2522 */
2523 *var_len = nullOidLen;
2524 return (u_char *) nullOid;
2525 case IPV6IFEFFECTMTU:
2526 {
2527 p = if_getname(interface);
2528 if (!p)
2529 break;
2530 memset(&ifr, 0, sizeof(ifr));
2531 ifr.ifr_addr.sa_family = AF_INET6;
2532 strlcpy(ifr.ifr_name, p, sizeof(ifr.ifr_name));
2533 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
2534 break;
2535 if (ioctl(s, SIOCGIFMTU, (caddr_t) & ifr) < 0) {
2536 close(s);
2537 break;
2538 }
2539 long_return = ifr.ifr_mtu;
2540 close(s);
2541 return (u_char *) & long_return;
2542 }
2543 case IPV6IFPHYSADDRESS:
2544 {
2545 static struct ifreq buf;
2546 int ok = 0;
2547 p = if_getname(interface);
2548 if (!p)
2549 break;
2550 memset(&ifr, 0, sizeof(ifr));
2551 ifr.ifr_addr.sa_family = AF_INET6;
2552 strlcpy(ifr.ifr_name, p, sizeof(ifr.ifr_name));
2553 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
2554 break;
2555 if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
2556 memset(buf.ifr_hwaddr.sa_data, 0,
2557 sizeof(buf.ifr_hwaddr.sa_data));
2558 *var_len = 0;
2559 } else {
2560 memcpy(buf.ifr_hwaddr.sa_data, ifr.ifr_hwaddr.sa_data, 6);
2561 *var_len = (buf.ifr_hwaddr.sa_data[0] |
2562 buf.ifr_hwaddr.sa_data[1] |
2563 buf.ifr_hwaddr.sa_data[2] |
2564 buf.ifr_hwaddr.sa_data[3] |
2565 buf.ifr_hwaddr.sa_data[4] |
2566 buf.ifr_hwaddr.sa_data[5]) ? 6 : 0;
2567 ok = 1;
2568 }
2569 close(s);
2570 return (ok ? ((u_char *) & buf.ifr_hwaddr.sa_data) : NULL);
2571 }
2572 case IPV6IFADMSTATUS:
2573 case IPV6IFOPERSTATUS:
2574 {
2575 int flag = 0;
2576 p = if_getname(interface);
2577 if (!p)
2578 break;
2579 memset(&ifr, 0, sizeof(ifr));
2580 ifr.ifr_addr.sa_family = AF_INET6;
2581 strlcpy(ifr.ifr_name, p, sizeof(ifr.ifr_name));
2582 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
2583 break;
2584 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
2585 close(s);
2586 break;
2587 }
2588 close(s);
2589 switch (vp->magic) {
2590 case IPV6IFADMSTATUS:
2591 flag = IFF_RUNNING;
2592 break;
2593 case IPV6IFOPERSTATUS:
2594 flag = IFF_UP;
2595 break;
2596 }
2597 long_return = (ifr.ifr_flags & flag) ? 1 : 2;
2598 return (u_char *) & long_return;
2599 }
2600 }
2601 return NULL;
2602 #endif
2603 }
2604
2605 u_char *
2606 var_icmpv6Entry(register struct variable * vp,
2607 oid * name,
2608 size_t * length,
2609 int exact, size_t * var_len, WriteMethod ** write_method)
2610 {
2611 return NULL;
2612 }
2613
2614 u_char *
2615 var_udp6(register struct variable * vp,
2616 oid * name,
2617 size_t * length,
2618 int exact, size_t * var_len, WriteMethod ** write_method)
2619 {
2620 return NULL;
2621 }
2622
2623 u_char *
2624 var_tcp6(register struct variable * vp,
2625 oid * name,
2626 size_t * length,
2627 int exact, size_t * var_len, WriteMethod ** write_method)
2628 {
2629 return NULL;
2630 }
2631
2632 /*
2633 * misc functions (against broken kernels )
2634 */
2635 void
2636 linux_if_freenameindex(struct if_nameindex *ifndx)
2637 {
2638 int i;
2639 if (!ifndx)
2640 return;
2641 for (i = 0; ifndx[i].if_index; i++) {
2642 free(ifndx[i].if_name);
2643 }
2644 free(ifndx);
2645 ifndx = NULL;
2646 }
2647
2648 #define linux_freeinternalnameindex(ifni, max) { \
2649 int i; \
2650 for (i=1; i<=max; i++){ \
2651 if (ifni[i].if_name) free(ifni[i].if_name); \
2652 } \
2653 free(ifni); \
2654 }
2655
2656 #define LINUX_PROC_NET_IFINET6 "/proc/net/if_inet6"
2657 struct if_nameindex *
2658 linux_if_nameindex(void)
2659 {
2660 FILE *f;
2661 unsigned long if_index;
2662 char if_name[256];
2663 struct if_nameindex *ifndx = NULL, *iflist = NULL, *tmp;
2664 int i, j;
2665 int maxidx, if_count = 0;
2666 static int last_if_count;
2667
2668 f = fopen(LINUX_PROC_NET_IFINET6, "r");
2669 if (f) {
2670 if_count = 0;
2671 maxidx = -1;
2672 while (!feof(f)) {
2673 if (fscanf(f, "%*s %lx %*x %*x %*x %255s",
2674 &if_index, if_name) != 2)
2675 continue;
2676 if (if_index == 0)
2677 continue;
2678 if_name[sizeof(if_name) - 1] = '\0';
2679 /*
2680 * Large if_index values can cause the multiplication in the
2681 * realloc() statement to overflow. Hence check if_index.
2682 */
2683 if (if_index > 65536)
2684 break;
2685 if (maxidx < 0 || maxidx < if_index) {
2686 if (last_if_count < if_index)
2687 last_if_count = if_index;
2688 tmp =
2689 realloc(iflist,
2690 (sizeof(struct if_nameindex)) * (last_if_count +
2691 2));
2692 if (!tmp) {
2693 linux_freeinternalnameindex(iflist, if_index);
2694 if_count = 0;
2695 iflist = NULL;
2696 break;
2697 }
2698 iflist = tmp;
2699 for (i = maxidx + 1; i <= if_index; i++)
2700 memset(&iflist[i], 0, sizeof(struct if_nameindex));
2701 memset(&iflist[if_index + 1], 0,
2702 sizeof(struct if_nameindex));
2703 maxidx = if_index;
2704 }
2705 if (iflist[if_index].if_index == 0) {
2706 if_count++;
2707 iflist[if_index].if_index = if_index;
2708 iflist[if_index].if_name = strdup(if_name);
2709 if (!iflist[if_index].if_name) {
2710 linux_freeinternalnameindex(iflist, if_index);
2711 if_count = 0;
2712 iflist = NULL;
2713 break;
2714 }
2715 }
2716 }
2717 fclose(f);
2718 if (if_count > 0) {
2719 ifndx = malloc(sizeof(struct if_nameindex) * (if_count + 1));
2720 j = 0;
2721 for (i = 1; i <= maxidx; i++) {
2722 if (iflist[i].if_index > 0 && *iflist[i].if_name) {
2723 memcpy(&ifndx[j++], &iflist[i],
2724 sizeof(struct if_nameindex));
2725 }
2726 }
2727 ifndx[j].if_index = 0;
2728 ifndx[j].if_name = NULL;
2729 }
2730 free(iflist);
2731 }
2732 return (ifndx);
2733 }
2734
2735 #endif /* linux */