]>
Commit | Line | Data |
---|---|---|
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 | * | |
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 | |
20 | */ | |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #if !defined(GNU_LINUX) && !defined(OPEN_BSD) | |
25 | ||
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" | |
33 | #include "interface.h" | |
34 | #include "vrf.h" | |
35 | ||
36 | #include "zebra/rt.h" | |
37 | #include "zebra/kernel_socket.h" | |
38 | #include "zebra/rib.h" | |
39 | #include "zebra/zebra_errors.h" | |
40 | ||
41 | void ifstat_update_sysctl(void) | |
42 | { | |
43 | caddr_t ref, buf, end; | |
44 | size_t bufsiz; | |
45 | struct if_msghdr *ifm; | |
46 | struct interface *ifp; | |
47 | ||
48 | #define MIBSIZ 6 | |
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) { | |
55 | flog_warn(EC_ZEBRA_SYSCTL_FAILED, "sysctl() error by %s", | |
56 | safe_strerror(errno)); | |
57 | return; | |
58 | } | |
59 | ||
60 | /* We free this memory at the end of this function. */ | |
61 | ref = buf = XMALLOC(MTYPE_TMP, bufsiz); | |
62 | ||
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); | |
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; | |
85 | } | |
86 | ||
87 | /* Interface listing up function using sysctl(). */ | |
88 | void interface_list(struct zebra_ns *zns) | |
89 | { | |
90 | caddr_t ref, buf, end; | |
91 | size_t bufsiz; | |
92 | struct if_msghdr *ifm; | |
93 | ||
94 | #define MIBSIZ 6 | |
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) { | |
100 | zlog_debug("%s: ignore NS %u", __func__, zns->ns_id); | |
101 | return; | |
102 | } | |
103 | ||
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)); | |
109 | return; | |
110 | } | |
111 | ||
112 | /* We free this memory at the end of this function. */ | |
113 | ref = buf = XMALLOC(MTYPE_TMP, bufsiz); | |
114 | ||
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)); | |
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("%s: unexpected message type", __func__); | |
136 | XFREE(MTYPE_TMP, ref); | |
137 | return; | |
138 | break; | |
139 | } | |
140 | } | |
141 | ||
142 | /* Free sysctl buffer. */ | |
143 | XFREE(MTYPE_TMP, ref); | |
144 | } | |
145 | ||
146 | #endif /* !defined(GNU_LINUX) && !defined(OPEN_BSD) */ |