]>
git.proxmox.com Git - mirror_frr.git/blob - zebra/if_sysctl.c
2 * Get interface's address and mask information by sysctl() function.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #if !defined(GNU_LINUX) && !defined(OPEN_BSD)
27 #include "sockunion.h"
29 #include "connected.h"
33 #include "interface.h"
37 #include "zebra/kernel_socket.h"
38 #include "zebra/rib.h"
39 #include "zebra/zebra_errors.h"
41 void ifstat_update_sysctl(void)
43 caddr_t ref
, buf
, end
;
45 struct if_msghdr
*ifm
;
46 struct interface
*ifp
;
50 CTL_NET
, PF_ROUTE
, 0, 0, /* AF_INET & AF_INET6 */
53 /* Query buffer size. */
54 if (sysctl(mib
, MIBSIZ
, NULL
, &bufsiz
, NULL
, 0) < 0) {
55 flog_warn(EC_ZEBRA_SYSCTL_FAILED
, "sysctl() error by %s",
56 safe_strerror(errno
));
60 /* We free this memory at the end of this function. */
61 ref
= buf
= XMALLOC(MTYPE_TMP
, bufsiz
);
63 /* Fetch interface information into allocated buffer. */
64 if (sysctl(mib
, MIBSIZ
, buf
, &bufsiz
, NULL
, 0) < 0) {
65 flog_warn(EC_ZEBRA_SYSCTL_FAILED
, "sysctl error by %s",
66 safe_strerror(errno
));
67 XFREE(MTYPE_TMP
, ref
);
71 /* Parse both interfaces and addresses. */
72 for (end
= buf
+ bufsiz
; buf
< end
; buf
+= ifm
->ifm_msglen
) {
73 ifm
= (struct if_msghdr
*)buf
;
74 if (ifm
->ifm_type
== RTM_IFINFO
) {
75 ifp
= if_lookup_by_index(ifm
->ifm_index
, VRF_DEFAULT
);
77 ifp
->stats
= ifm
->ifm_data
;
81 /* Free sysctl buffer. */
82 XFREE(MTYPE_TMP
, ref
);
87 /* Interface listing up function using sysctl(). */
88 void interface_list(struct zebra_ns
*zns
)
90 caddr_t ref
, buf
, end
;
92 struct if_msghdr
*ifm
;
96 CTL_NET
, PF_ROUTE
, 0, 0, /* AF_INET & AF_INET6 */
99 if (zns
->ns_id
!= NS_DEFAULT
) {
100 zlog_debug("interface_list: ignore NS %u", zns
->ns_id
);
104 /* Query buffer size. */
105 if (sysctl(mib
, MIBSIZ
, NULL
, &bufsiz
, NULL
, 0) < 0) {
106 flog_err_sys(EC_ZEBRA_IFLIST_FAILED
,
107 "Could not enumerate interfaces: %s",
108 safe_strerror(errno
));
112 /* We free this memory at the end of this function. */
113 ref
= buf
= XMALLOC(MTYPE_TMP
, bufsiz
);
115 /* Fetch interface information into allocated buffer. */
116 if (sysctl(mib
, MIBSIZ
, buf
, &bufsiz
, NULL
, 0) < 0) {
117 flog_err_sys(EC_ZEBRA_IFLIST_FAILED
,
118 "Could not enumerate interfaces: %s",
119 safe_strerror(errno
));
123 /* Parse both interfaces and addresses. */
124 for (end
= buf
+ bufsiz
; buf
< end
; buf
+= ifm
->ifm_msglen
) {
125 ifm
= (struct if_msghdr
*)buf
;
127 switch (ifm
->ifm_type
) {
132 ifam_read((struct ifa_msghdr
*)ifm
);
135 zlog_info("interfaces_list(): unexpected message type");
136 XFREE(MTYPE_TMP
, ref
);
142 /* Free sysctl buffer. */
143 XFREE(MTYPE_TMP
, ref
);
146 #endif /* !defined(GNU_LINUX) && !defined(OPEN_BSD) */