]>
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 | ||
24 | #include "if.h" | |
25 | #include "sockunion.h" | |
26 | #include "prefix.h" | |
27 | #include "connected.h" | |
28 | #include "memory.h" | |
4a1ab8e4 | 29 | #include "zebra_memory.h" |
718e3744 | 30 | #include "ioctl.h" |
31 | #include "log.h" | |
8f7d9fc0 FL |
32 | #include "interface.h" |
33 | #include "vrf.h" | |
718e3744 | 34 | |
ec1a4283 | 35 | #include "zebra/rt.h" |
36 | #include "zebra/kernel_socket.h" | |
8f7d9fc0 | 37 | #include "zebra/rib.h" |
ec1a4283 | 38 | |
d62a17ae | 39 | void ifstat_update_sysctl(void) |
718e3744 | 40 | { |
d62a17ae | 41 | caddr_t ref, buf, end; |
42 | size_t bufsiz; | |
43 | struct if_msghdr *ifm; | |
44 | struct interface *ifp; | |
718e3744 | 45 | |
46 | #define MIBSIZ 6 | |
d62a17ae | 47 | int mib[MIBSIZ] = { |
48 | CTL_NET, PF_ROUTE, 0, 0, /* AF_INET & AF_INET6 */ | |
49 | NET_RT_IFLIST, 0}; | |
50 | ||
51 | /* Query buffer size. */ | |
52 | if (sysctl(mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0) { | |
53 | zlog_warn("sysctl() error by %s", safe_strerror(errno)); | |
54 | return; | |
718e3744 | 55 | } |
718e3744 | 56 | |
d62a17ae | 57 | /* We free this memory at the end of this function. */ |
58 | ref = buf = XMALLOC(MTYPE_TMP, bufsiz); | |
718e3744 | 59 | |
d62a17ae | 60 | /* Fetch interface informations into allocated buffer. */ |
61 | if (sysctl(mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0) { | |
62 | zlog_warn("sysctl error by %s", safe_strerror(errno)); | |
63 | XFREE(MTYPE_TMP, ref); | |
64 | return; | |
65 | } | |
66 | ||
67 | /* Parse both interfaces and addresses. */ | |
68 | for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen) { | |
69 | ifm = (struct if_msghdr *)buf; | |
70 | if (ifm->ifm_type == RTM_IFINFO) { | |
71 | ifp = if_lookup_by_index(ifm->ifm_index, VRF_DEFAULT); | |
72 | if (ifp) | |
73 | ifp->stats = ifm->ifm_data; | |
74 | } | |
75 | } | |
76 | ||
77 | /* Free sysctl buffer. */ | |
78 | XFREE(MTYPE_TMP, ref); | |
79 | ||
80 | return; | |
718e3744 | 81 | } |
82 | ||
83 | /* Interface listing up function using sysctl(). */ | |
d62a17ae | 84 | void interface_list(struct zebra_ns *zns) |
718e3744 | 85 | { |
d62a17ae | 86 | caddr_t ref, buf, end; |
87 | size_t bufsiz; | |
88 | struct if_msghdr *ifm; | |
718e3744 | 89 | |
90 | #define MIBSIZ 6 | |
d62a17ae | 91 | int mib[MIBSIZ] = { |
92 | CTL_NET, PF_ROUTE, 0, 0, /* AF_INET & AF_INET6 */ | |
93 | NET_RT_IFLIST, 0}; | |
94 | ||
95 | if (zns->ns_id != NS_DEFAULT) { | |
96 | zlog_warn("interface_list: ignore NS %u", zns->ns_id); | |
97 | return; | |
98 | } | |
99 | ||
100 | /* Query buffer size. */ | |
101 | if (sysctl(mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0) { | |
102 | zlog_warn("sysctl() error by %s", safe_strerror(errno)); | |
103 | return; | |
104 | } | |
105 | ||
106 | /* We free this memory at the end of this function. */ | |
107 | ref = buf = XMALLOC(MTYPE_TMP, bufsiz); | |
108 | ||
109 | /* Fetch interface informations into allocated buffer. */ | |
110 | if (sysctl(mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0) { | |
111 | zlog_warn("sysctl error by %s", safe_strerror(errno)); | |
112 | return; | |
113 | } | |
114 | ||
115 | /* Parse both interfaces and addresses. */ | |
116 | for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen) { | |
117 | ifm = (struct if_msghdr *)buf; | |
118 | ||
119 | switch (ifm->ifm_type) { | |
120 | case RTM_IFINFO: | |
121 | ifm_read(ifm); | |
122 | break; | |
123 | case RTM_NEWADDR: | |
124 | ifam_read((struct ifa_msghdr *)ifm); | |
125 | break; | |
126 | default: | |
127 | zlog_info("interfaces_list(): unexpected message type"); | |
128 | XFREE(MTYPE_TMP, ref); | |
129 | return; | |
130 | break; | |
131 | } | |
718e3744 | 132 | } |
718e3744 | 133 | |
d62a17ae | 134 | /* Free sysctl buffer. */ |
135 | XFREE(MTYPE_TMP, ref); | |
718e3744 | 136 | } |