]>
git.proxmox.com Git - mirror_frr.git/blob - zebra/if_sysctl.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Get interface's address and mask information by sysctl() function.
4 * Copyright (C) 1997, 98 Kunihiro Ishiguro
9 #if !defined(GNU_LINUX) && !defined(OPEN_BSD)
12 #include "sockunion.h"
14 #include "connected.h"
18 #include "interface.h"
22 #include "zebra/kernel_socket.h"
23 #include "zebra/rib.h"
24 #include "zebra/zebra_errors.h"
26 void ifstat_update_sysctl(void)
28 caddr_t ref
, buf
, end
;
30 struct if_msghdr
*ifm
;
31 struct interface
*ifp
;
35 CTL_NET
, PF_ROUTE
, 0, 0, /* AF_INET & AF_INET6 */
38 /* Query buffer size. */
39 if (sysctl(mib
, MIBSIZ
, NULL
, &bufsiz
, NULL
, 0) < 0) {
40 flog_warn(EC_ZEBRA_SYSCTL_FAILED
, "sysctl() error by %s",
41 safe_strerror(errno
));
45 /* We free this memory at the end of this function. */
46 ref
= buf
= XMALLOC(MTYPE_TMP
, bufsiz
);
48 /* Fetch interface information into allocated buffer. */
49 if (sysctl(mib
, MIBSIZ
, buf
, &bufsiz
, NULL
, 0) < 0) {
50 flog_warn(EC_ZEBRA_SYSCTL_FAILED
, "sysctl error by %s",
51 safe_strerror(errno
));
52 XFREE(MTYPE_TMP
, ref
);
56 /* Parse both interfaces and addresses. */
57 for (end
= buf
+ bufsiz
; buf
< end
; buf
+= ifm
->ifm_msglen
) {
58 ifm
= (struct if_msghdr
*)buf
;
59 if (ifm
->ifm_type
== RTM_IFINFO
) {
60 ifp
= if_lookup_by_index(ifm
->ifm_index
, VRF_DEFAULT
);
62 ifp
->stats
= ifm
->ifm_data
;
66 /* Free sysctl buffer. */
67 XFREE(MTYPE_TMP
, ref
);
72 /* Interface listing up function using sysctl(). */
73 void interface_list(struct zebra_ns
*zns
)
75 caddr_t ref
, buf
, end
;
77 struct if_msghdr
*ifm
;
81 CTL_NET
, PF_ROUTE
, 0, 0, /* AF_INET & AF_INET6 */
84 if (zns
->ns_id
!= NS_DEFAULT
) {
85 zlog_debug("%s: ignore NS %u", __func__
, zns
->ns_id
);
89 /* Query buffer size. */
90 if (sysctl(mib
, MIBSIZ
, NULL
, &bufsiz
, NULL
, 0) < 0) {
91 flog_err_sys(EC_ZEBRA_IFLIST_FAILED
,
92 "Could not enumerate interfaces: %s",
93 safe_strerror(errno
));
97 /* We free this memory at the end of this function. */
98 ref
= buf
= XMALLOC(MTYPE_TMP
, bufsiz
);
100 /* Fetch interface information into allocated buffer. */
101 if (sysctl(mib
, MIBSIZ
, buf
, &bufsiz
, NULL
, 0) < 0) {
102 flog_err_sys(EC_ZEBRA_IFLIST_FAILED
,
103 "Could not enumerate interfaces: %s",
104 safe_strerror(errno
));
108 /* Parse both interfaces and addresses. */
109 for (end
= buf
+ bufsiz
; buf
< end
; buf
+= ifm
->ifm_msglen
) {
110 ifm
= (struct if_msghdr
*)buf
;
112 switch (ifm
->ifm_type
) {
117 ifam_read((struct ifa_msghdr
*)ifm
);
120 zlog_info("%s: unexpected message type", __func__
);
121 XFREE(MTYPE_TMP
, ref
);
127 /* Free sysctl buffer. */
128 XFREE(MTYPE_TMP
, ref
);
131 #endif /* !defined(GNU_LINUX) && !defined(OPEN_BSD) */