]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
718e3744 | 2 | /* |
3 | * Get interface's address and mask information by sysctl() function. | |
4 | * Copyright (C) 1997, 98 Kunihiro Ishiguro | |
718e3744 | 5 | */ |
6 | ||
7 | #include <zebra.h> | |
8 | ||
cae8bc96 | 9 | #if !defined(GNU_LINUX) && !defined(OPEN_BSD) |
ddfeb486 | 10 | |
718e3744 | 11 | #include "if.h" |
12 | #include "sockunion.h" | |
13 | #include "prefix.h" | |
14 | #include "connected.h" | |
15 | #include "memory.h" | |
16 | #include "ioctl.h" | |
17 | #include "log.h" | |
8f7d9fc0 FL |
18 | #include "interface.h" |
19 | #include "vrf.h" | |
718e3744 | 20 | |
ec1a4283 | 21 | #include "zebra/rt.h" |
22 | #include "zebra/kernel_socket.h" | |
8f7d9fc0 | 23 | #include "zebra/rib.h" |
364fed6b | 24 | #include "zebra/zebra_errors.h" |
ec1a4283 | 25 | |
d62a17ae | 26 | void ifstat_update_sysctl(void) |
718e3744 | 27 | { |
d62a17ae | 28 | caddr_t ref, buf, end; |
29 | size_t bufsiz; | |
30 | struct if_msghdr *ifm; | |
31 | struct interface *ifp; | |
718e3744 | 32 | |
33 | #define MIBSIZ 6 | |
d62a17ae | 34 | int mib[MIBSIZ] = { |
35 | CTL_NET, PF_ROUTE, 0, 0, /* AF_INET & AF_INET6 */ | |
36 | NET_RT_IFLIST, 0}; | |
37 | ||
38 | /* Query buffer size. */ | |
39 | if (sysctl(mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0) { | |
e914ccbe | 40 | flog_warn(EC_ZEBRA_SYSCTL_FAILED, "sysctl() error by %s", |
9df414fe | 41 | safe_strerror(errno)); |
d62a17ae | 42 | return; |
718e3744 | 43 | } |
718e3744 | 44 | |
d62a17ae | 45 | /* We free this memory at the end of this function. */ |
46 | ref = buf = XMALLOC(MTYPE_TMP, bufsiz); | |
718e3744 | 47 | |
0437e105 | 48 | /* Fetch interface information into allocated buffer. */ |
d62a17ae | 49 | if (sysctl(mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0) { |
e914ccbe | 50 | flog_warn(EC_ZEBRA_SYSCTL_FAILED, "sysctl error by %s", |
9df414fe | 51 | safe_strerror(errno)); |
d62a17ae | 52 | XFREE(MTYPE_TMP, ref); |
53 | return; | |
54 | } | |
55 | ||
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); | |
61 | if (ifp) | |
62 | ifp->stats = ifm->ifm_data; | |
63 | } | |
64 | } | |
65 | ||
66 | /* Free sysctl buffer. */ | |
67 | XFREE(MTYPE_TMP, ref); | |
68 | ||
69 | return; | |
718e3744 | 70 | } |
71 | ||
72 | /* Interface listing up function using sysctl(). */ | |
d62a17ae | 73 | void interface_list(struct zebra_ns *zns) |
718e3744 | 74 | { |
d62a17ae | 75 | caddr_t ref, buf, end; |
76 | size_t bufsiz; | |
77 | struct if_msghdr *ifm; | |
718e3744 | 78 | |
79 | #define MIBSIZ 6 | |
d62a17ae | 80 | int mib[MIBSIZ] = { |
81 | CTL_NET, PF_ROUTE, 0, 0, /* AF_INET & AF_INET6 */ | |
82 | NET_RT_IFLIST, 0}; | |
83 | ||
84 | if (zns->ns_id != NS_DEFAULT) { | |
6751c0f3 | 85 | zlog_debug("%s: ignore NS %u", __func__, zns->ns_id); |
d62a17ae | 86 | return; |
87 | } | |
88 | ||
89 | /* Query buffer size. */ | |
90 | if (sysctl(mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0) { | |
e914ccbe | 91 | flog_err_sys(EC_ZEBRA_IFLIST_FAILED, |
9df414fe QY |
92 | "Could not enumerate interfaces: %s", |
93 | safe_strerror(errno)); | |
d62a17ae | 94 | return; |
95 | } | |
96 | ||
97 | /* We free this memory at the end of this function. */ | |
98 | ref = buf = XMALLOC(MTYPE_TMP, bufsiz); | |
99 | ||
0437e105 | 100 | /* Fetch interface information into allocated buffer. */ |
d62a17ae | 101 | if (sysctl(mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0) { |
e914ccbe | 102 | flog_err_sys(EC_ZEBRA_IFLIST_FAILED, |
9df414fe QY |
103 | "Could not enumerate interfaces: %s", |
104 | safe_strerror(errno)); | |
d62a17ae | 105 | return; |
106 | } | |
107 | ||
108 | /* Parse both interfaces and addresses. */ | |
109 | for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen) { | |
110 | ifm = (struct if_msghdr *)buf; | |
111 | ||
112 | switch (ifm->ifm_type) { | |
113 | case RTM_IFINFO: | |
114 | ifm_read(ifm); | |
115 | break; | |
116 | case RTM_NEWADDR: | |
117 | ifam_read((struct ifa_msghdr *)ifm); | |
118 | break; | |
119 | default: | |
6751c0f3 | 120 | zlog_info("%s: unexpected message type", __func__); |
d62a17ae | 121 | XFREE(MTYPE_TMP, ref); |
122 | return; | |
123 | break; | |
124 | } | |
718e3744 | 125 | } |
718e3744 | 126 | |
d62a17ae | 127 | /* Free sysctl buffer. */ |
128 | XFREE(MTYPE_TMP, ref); | |
718e3744 | 129 | } |
ddfeb486 | 130 | |
cae8bc96 | 131 | #endif /* !defined(GNU_LINUX) && !defined(OPEN_BSD) */ |