Debian Codegraph @

agent/mibgroup/if-mib/data_access/interface_ioctl.c

raw
1 /*
2 * Interface MIB architecture support
3 *
4 * $Id$
5 */
6 #include <net-snmp/net-snmp-config.h>
7 #include <net-snmp/net-snmp-features.h>
8 #include <net-snmp/net-snmp-includes.h>
9 #include "mibII/mibII_common.h"
10 #include "if-mib/ifTable/ifTable_constants.h"
11
12 #include <net-snmp/agent/net-snmp-agent-includes.h>
13 #include <net-snmp/data_access/interface.h>
14 #include <net-snmp/data_access/ipaddress.h>
15 #include "if-mib/data_access/interface.h"
16
17 netsnmp_feature_child_of(interface_ioctl_flags_set, interface_all);
18
19 #ifdef HAVE_NET_IF_H
20 #include <net/if.h>
21 #endif
22 #ifdef HAVE_NET_IF_ARP_H
23 #include <net/if_arp.h>
24 #endif
25 #ifdef HAVE_SYS_IOCTL_H
26 #include <sys/ioctl.h>
27 #endif
28
29 #include "interface_ioctl.h"
30 #include "ip-mib/data_access/ipaddress_ioctl.h"
31
32 /**
33 * ioctl wrapper
34 *
35 * @param fd : socket fd to use w/ioctl, or -1 to open/close one
36 * @param which
37 * @param ifrq
38 * param ifentry : ifentry to update
39 * @param name
40 *
41 * @retval 0 : success
42 * @retval -1 : invalid parameters
43 * @retval -2 : couldn't create socket
44 * @retval -3 : ioctl call failed
45 */
46 static int
47 _ioctl_get(int fd, int which, struct ifreq *ifrq, const char* name)
48 {
49 int ourfd = -1, rc = 0;
50
51 DEBUGMSGTL(("verbose:access:interface:ioctl",
52 "ioctl %d for %s\n", which, name));
53
54 /*
55 * sanity checks
56 */
57 if(NULL == name) {
58 snmp_log(LOG_ERR, "invalid ifentry\n");
59 return -1;
60 }
61
62 /*
63 * create socket for ioctls
64 */
65 if(fd < 0) {
66 fd = ourfd = socket(AF_INET, SOCK_DGRAM, 0);
67 if(ourfd < 0) {
68 snmp_log(LOG_ERR,"couldn't create socket\n");
69 return -2;
70 }
71 }
72
73 strlcpy(ifrq->ifr_name, name, sizeof(ifrq->ifr_name));
74 rc = ioctl(fd, which, ifrq);
75 if (rc < 0)
76 rc = -3;
77
78 if(ourfd >= 0)
79 close(ourfd);
80
81 return rc;
82 }
83
84 int netsnmp_convert_arphrd_type(int arphrd_type)
85 {
86 /*
87 * arphrd defines vary greatly. ETHER seems to be the only common one
88 */
89 #ifdef ARPHRD_ETHER
90 switch (arphrd_type) {
91 case ARPHRD_ETHER:
92 return IANAIFTYPE_ETHERNETCSMACD;
93 #if defined(ARPHRD_TUNNEL) || defined(ARPHRD_IPGRE) || defined(ARPHRD_SIT)
94 #ifdef ARPHRD_TUNNEL
95 case ARPHRD_TUNNEL:
96 case ARPHRD_TUNNEL6:
97 #endif
98 #ifdef ARPHRD_IPGRE
99 case ARPHRD_IPGRE:
100 #endif
101 #ifdef ARPHRD_SIT
102 case ARPHRD_SIT:
103 #endif
104 return IANAIFTYPE_TUNNEL;
105 #endif
106 #ifdef ARPHRD_INFINIBAND
107 case ARPHRD_INFINIBAND:
108 return IANAIFTYPE_INFINIBAND;
109 #endif
110 #ifdef ARPHRD_SLIP
111 case ARPHRD_SLIP:
112 case ARPHRD_CSLIP:
113 case ARPHRD_SLIP6:
114 case ARPHRD_CSLIP6:
115 return IANAIFTYPE_SLIP;
116 #endif
117 #ifdef ARPHRD_PPP
118 case ARPHRD_PPP:
119 return IANAIFTYPE_PPP;
120 #endif
121 #ifdef ARPHRD_LOOPBACK
122 case ARPHRD_LOOPBACK:
123 return IANAIFTYPE_SOFTWARELOOPBACK;
124 #endif
125 #ifdef ARPHRD_FDDI
126 case ARPHRD_FDDI:
127 return IANAIFTYPE_FDDI;
128 #endif
129 #ifdef ARPHRD_ARCNET
130 case ARPHRD_ARCNET:
131 return IANAIFTYPE_ARCNET;
132 #endif
133 #ifdef ARPHRD_LOCALTLK
134 case ARPHRD_LOCALTLK:
135 return IANAIFTYPE_LOCALTALK;
136 #endif
137 #ifdef ARPHRD_HIPPI
138 case ARPHRD_HIPPI:
139 return IANAIFTYPE_HIPPI;
140 #endif
141 #ifdef ARPHRD_ATM
142 case ARPHRD_ATM:
143 return IANAIFTYPE_ATM;
144 #endif
145 /*
146 * XXX: more if_arp.h:ARPHRD_xxx to IANAifType mappings...
147 */
148 default:
149 DEBUGMSGTL(("access:interface:ioctl", "unknown entry type %d\n",
150 arphrd_type));
151 return IANAIFTYPE_OTHER;
152 } /* switch */
153 #endif /* ARPHRD_LOOPBACK */
154 }
155
156 #ifdef SIOCGIFHWADDR
157 /**
158 * interface entry physaddr ioctl wrapper
159 *
160 * @param fd : socket fd to use w/ioctl, or -1 to open/close one
161 * @param ifentry : ifentry to update
162 *
163 * @retval 0 : success
164 * @retval -1 : invalid parameters
165 * @retval -2 : couldn't create socket
166 * @retval -3 : ioctl call failed
167 * @retval -4 : malloc error
168 */
169 int
170 netsnmp_access_interface_ioctl_physaddr_get(int fd,
171 netsnmp_interface_entry *ifentry)
172 {
173 struct ifreq ifrq;
174 int rc = 0;
175
176 DEBUGMSGTL(("access:interface:ioctl", "physaddr_get\n"));
177
178 if((NULL != ifentry->paddr) &&
179 (ifentry->paddr_len != IFHWADDRLEN)) {
180 SNMP_FREE(ifentry->paddr);
181 }
182 if(NULL == ifentry->paddr)
183 ifentry->paddr = (char*)malloc(IFHWADDRLEN);
184
185 if(NULL == ifentry->paddr) {
186 rc = -4;
187 } else {
188
189 /*
190 * NOTE: this ioctl does not guarantee 6 bytes of a physaddr.
191 * In particular, a 'sit0' interface only appears to get back
192 * 4 bytes of sa_data. Uncomment this memset, and suddenly
193 * the sit interface will be 0:0:0:0:?:? where ? is whatever was
194 * in the memory before. Not sure if this memset should be done
195 * for every ioctl, as the rest seem to work ok...
196 */
197 memset(ifrq.ifr_hwaddr.sa_data, (0), IFHWADDRLEN);
198 ifentry->paddr_len = IFHWADDRLEN;
199 rc = _ioctl_get(fd, SIOCGIFHWADDR, &ifrq, ifentry->name);
200 if (rc < 0) {
201 memset(ifentry->paddr, (0), IFHWADDRLEN);
202 rc = -3;
203 } else {
204 memcpy(ifentry->paddr, ifrq.ifr_hwaddr.sa_data, IFHWADDRLEN);
205 ifentry->type =
206 netsnmp_convert_arphrd_type(ifrq.ifr_hwaddr.sa_family);
207 }
208 }
209
210 return rc;
211 }
212 #endif /* SIOCGIFHWADDR */
213
214 void
215 netsnmp_process_link_flags(netsnmp_interface_entry *ifentry,
216 unsigned int os_flags)
217 {
218 ifentry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_IF_FLAGS;
219 ifentry->os_flags = os_flags;
220
221 /*
222 * ifOperStatus description:
223 * If ifAdminStatus is down(2) then ifOperStatus should be down(2).
224 */
225 if (ifentry->os_flags & IFF_UP) {
226 ifentry->admin_status = IFADMINSTATUS_UP;
227 if (ifentry->os_flags & IFF_RUNNING)
228 ifentry->oper_status = IFOPERSTATUS_UP;
229 else
230 ifentry->oper_status = IFOPERSTATUS_DOWN;
231 } else {
232 ifentry->admin_status = IFADMINSTATUS_DOWN;
233 ifentry->oper_status = IFOPERSTATUS_DOWN;
234 }
235
236 /*
237 * ifConnectorPresent description:
238 * This object has the value 'true(1)' if the interface sublayer has a
239 * physical connector and the value 'false(2)' otherwise."
240 * So, at very least, false(2) should be returned for loopback devices.
241 */
242 if (ifentry->os_flags & IFF_LOOPBACK) {
243 ifentry->connector_present = 0;
244 } else {
245 ifentry->connector_present = 1;
246 }
247 }
248
249 #ifdef SIOCGIFFLAGS
250 /**
251 * interface entry flags ioctl wrapper
252 *
253 * @param fd : socket fd to use w/ioctl, or -1 to open/close one
254 * @param ifentry : ifentry to update
255 *
256 * @retval 0 : success
257 * @retval -1 : invalid parameters
258 * @retval -2 : couldn't create socket
259 * @retval -3 : ioctl call failed
260 */
261 int
262 netsnmp_access_interface_ioctl_flags_get(int fd,
263 netsnmp_interface_entry *ifentry)
264 {
265 struct ifreq ifrq;
266 int rc = 0;
267
268 DEBUGMSGTL(("access:interface:ioctl", "flags_get\n"));
269
270 rc = _ioctl_get(fd, SIOCGIFFLAGS, &ifrq, ifentry->name);
271 if (rc < 0) {
272 ifentry->ns_flags &= ~NETSNMP_INTERFACE_FLAGS_HAS_IF_FLAGS;
273 return rc;
274 } else {
275 netsnmp_process_link_flags(ifentry, ifrq.ifr_flags);
276 }
277
278 return rc;
279 }
280
281 #ifndef NETSNMP_FEATURE_REMOVE_INTERFACE_IOCTL_FLAGS_SET
282 /**
283 * interface entry flags ioctl wrapper
284 *
285 * @param fd : socket fd to use w/ioctl, or -1 to open/close one
286 * @param ifentry : ifentry to update
287 *
288 * @retval 0 : success
289 * @retval -1 : invalid parameters
290 * @retval -2 : couldn't create socket
291 * @retval -3 : ioctl get call failed
292 * @retval -4 : ioctl set call failed
293 */
294 int
295 netsnmp_access_interface_ioctl_flags_set(int fd,
296 netsnmp_interface_entry *ifentry,
297 unsigned int flags, int and_complement)
298 {
299 struct ifreq ifrq;
300 int ourfd = -1, rc = 0;
301
302 DEBUGMSGTL(("access:interface:ioctl", "flags_set\n"));
303
304 /*
305 * sanity checks
306 */
307 if((NULL == ifentry) || (NULL == ifentry->name)) {
308 snmp_log(LOG_ERR, "invalid ifentry\n");
309 return -1;
310 }
311
312 /*
313 * create socket for ioctls
314 */
315 if(fd < 0) {
316 fd = ourfd = socket(AF_INET, SOCK_DGRAM, 0);
317 if(ourfd < 0) {
318 snmp_log(LOG_ERR,"couldn't create socket\n");
319 return -2;
320 }
321 }
322
323 strlcpy(ifrq.ifr_name, ifentry->name, sizeof(ifrq.ifr_name));
324 rc = ioctl(fd, SIOCGIFFLAGS, &ifrq);
325 if(rc < 0) {
326 snmp_log(LOG_ERR,"error getting flags\n");
327 close(fd);
328 return -3;
329 }
330 if(0 == and_complement)
331 ifrq.ifr_flags |= flags;
332 else
333 ifrq.ifr_flags &= ~flags;
334 rc = ioctl(fd, SIOCSIFFLAGS, &ifrq);
335 if(rc < 0) {
336 close(fd);
337 snmp_log(LOG_ERR,"error setting flags\n");
338 ifentry->os_flags = 0;
339 return -4;
340 }
341
342 if(ourfd >= 0)
343 close(ourfd);
344
345 ifentry->os_flags = ifrq.ifr_flags;
346
347 return 0;
348 }
349 #endif /* NETSNMP_FEATURE_REMOVE_INTERFACE_IOCTL_FLAGS_SET */
350 #endif /* SIOCGIFFLAGS */
351
352 #ifdef SIOCGIFMTU
353 /**
354 * interface entry mtu ioctl wrapper
355 *
356 * @param fd : socket fd to use w/ioctl, or -1 to open/close one
357 * @param ifentry : ifentry to update
358 *
359 * @retval 0 : success
360 * @retval -1 : invalid parameters
361 * @retval -2 : couldn't create socket
362 * @retval -3 : ioctl call failed
363 */
364 int
365 netsnmp_access_interface_ioctl_mtu_get(int fd,
366 netsnmp_interface_entry *ifentry)
367 {
368 struct ifreq ifrq;
369 int rc = 0;
370
371 DEBUGMSGTL(("access:interface:ioctl", "mtu_get\n"));
372
373 rc = _ioctl_get(fd, SIOCGIFMTU, &ifrq, ifentry->name);
374 if (rc < 0) {
375 ifentry->mtu = 0;
376 return rc;
377 } else {
378 ifentry->mtu = ifrq.ifr_mtu;
379 }
380
381 return rc;
382 }
383 #endif /* SIOCGIFMTU */
384
385 /**
386 * interface entry ifIndex ioctl wrapper
387 *
388 * @param fd : socket fd to use w/ioctl, or -1 to open/close one
389 * @param name : ifentry to update
390 *
391 * @retval 0 : not found
392 * @retval !0 : ifIndex
393 */
394 oid
395 netsnmp_access_interface_ioctl_ifindex_get(int fd, const char *name)
396 {
397 #ifndef SIOCGIFINDEX
398 return 0;
399 #else
400 struct ifreq ifrq;
401 int rc = 0;
402
403 DEBUGMSGTL(("access:interface:ioctl", "ifindex_get\n"));
404
405 rc = _ioctl_get(fd, SIOCGIFINDEX, &ifrq, name);
406 if (rc < 0) {
407 DEBUGMSGTL(("access:interface:ioctl",
408 "ifindex_get error on interface '%s'\n", name));
409 return 0;
410 }
411
412 #if defined(freebsd3) /* ? Should use HAVE_STRUCT_IFREQ_IFR_INDEX */
413 return ifrq.ifr_index;
414 #else
415 return ifrq.ifr_ifindex;
416 #endif
417 #endif /* SIOCGIFINDEX */
418 }
419
420 /**
421 * check an interface for ipv4 addresses
422 *
423 * @param sd : open socket descriptor
424 * @param if_name : optional name. takes precedent over if_index.
425 * @param if_index: optional if index. only used if no if_name specified
426 * @param flags :
427 * @param pifc : ifconf structures returned by the SIOCGIFCONF ioctl
428 *
429 * @retval < 0 : error
430 * @retval 0 : no ip v4 addresses
431 * @retval 1 : 1 or more ip v4 addresses
432 */
433 int
434 netsnmp_access_interface_ioctl_has_ipv4(int sd, const char *if_name,
435 int if_index, u_int *flags,
436 const struct ifconf *pifc)
437 {
438 int i, interfaces = 0;
439 struct ifreq *ifrp;
440
441 /*
442 * one or the other
443 */
444 if (NULL == flags || NULL == pifc ||
445 ((0 == if_index) && (NULL == if_name))) {
446 return -1;
447 }
448
449 *flags &= ~NETSNMP_INTERFACE_FLAGS_HAS_IPV4;
450
451 ifrp = pifc->ifc_req;
452 interfaces = pifc->ifc_len / sizeof(struct ifreq);
453 for(i=0; i < interfaces; ++i, ++ifrp) {
454
455 DEBUGMSGTL(("access:ipaddress:container",
456 " interface %d, %s\n", i, ifrp->ifr_name));
457
458 /*
459 * search for matching if_name or if_index
460 */
461 if (NULL != if_name) {
462 if (strncmp(if_name, ifrp->ifr_name, sizeof(ifrp->ifr_name)) != 0)
463 continue;
464 }
465 else {
466 /*
467 * I think that Linux and Solaris both use ':' in the
468 * interface name for aliases.
469 */
470 char *ptr = strchr(ifrp->ifr_name, ':');
471 if (NULL != ptr)
472 *ptr = 0;
473
474 if (if_index != (int)netsnmp_access_interface_ioctl_ifindex_get(sd, ifrp->ifr_name))
475 continue;
476 }
477
478 /*
479 * check and set v4 or v6 flag, and break if we've found both
480 */
481 if (AF_INET == ifrp->ifr_addr.sa_family) {
482 *flags |= NETSNMP_INTERFACE_FLAGS_HAS_IPV4;
483 break;
484 }
485 }
486
487 return 0;
488 }