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