]>
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 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with GNU Zebra; see the file COPYING. If not, write to the Free | |
19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
20 | * 02111-1307, USA. | |
21 | */ | |
22 | ||
23 | #include <zebra.h> | |
24 | ||
25 | #include "if.h" | |
26 | #include "sockunion.h" | |
27 | #include "prefix.h" | |
28 | #include "connected.h" | |
29 | #include "memory.h" | |
30 | #include "ioctl.h" | |
31 | #include "log.h" | |
8f7d9fc0 FL |
32 | <<<<<<< HEAD |
33 | ======= | |
34 | #include "interface.h" | |
35 | #include "vrf.h" | |
36 | >>>>>>> 3c27b5f... zebra, lib/memtypes.c: the netlink sockets work per VRF | |
718e3744 | 37 | |
ec1a4283 | 38 | #include "zebra/rt.h" |
39 | #include "zebra/kernel_socket.h" | |
8f7d9fc0 | 40 | #include "zebra/rib.h" |
ec1a4283 | 41 | |
f28b0e57 PJ |
42 | void |
43 | ifstat_update_sysctl (void) | |
718e3744 | 44 | { |
45 | caddr_t ref, buf, end; | |
46 | size_t bufsiz; | |
47 | struct if_msghdr *ifm; | |
48 | struct interface *ifp; | |
49 | ||
50 | #define MIBSIZ 6 | |
51 | int mib[MIBSIZ] = | |
52 | { | |
53 | CTL_NET, | |
54 | PF_ROUTE, | |
55 | 0, | |
56 | 0, /* AF_INET & AF_INET6 */ | |
57 | NET_RT_IFLIST, | |
58 | 0 | |
59 | }; | |
60 | ||
61 | /* Query buffer size. */ | |
62 | if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0) | |
63 | { | |
6099b3b5 | 64 | zlog_warn ("sysctl() error by %s", safe_strerror (errno)); |
f28b0e57 | 65 | return; |
718e3744 | 66 | } |
67 | ||
68 | /* We free this memory at the end of this function. */ | |
69 | ref = buf = XMALLOC (MTYPE_TMP, bufsiz); | |
70 | ||
71 | /* Fetch interface informations into allocated buffer. */ | |
72 | if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0) | |
73 | { | |
6099b3b5 | 74 | zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno)); |
495f0b13 | 75 | XFREE(MTYPE_TMP, ref); |
f28b0e57 | 76 | return; |
718e3744 | 77 | } |
78 | ||
79 | /* Parse both interfaces and addresses. */ | |
80 | for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen) | |
81 | { | |
82 | ifm = (struct if_msghdr *) buf; | |
83 | if (ifm->ifm_type == RTM_IFINFO) | |
84 | { | |
85 | ifp = if_lookup_by_index (ifm->ifm_index); | |
86 | if (ifp) | |
87 | ifp->stats = ifm->ifm_data; | |
88 | } | |
89 | } | |
90 | ||
91 | /* Free sysctl buffer. */ | |
92 | XFREE (MTYPE_TMP, ref); | |
93 | ||
f28b0e57 | 94 | return; |
718e3744 | 95 | } |
96 | ||
97 | /* Interface listing up function using sysctl(). */ | |
98 | void | |
8f7d9fc0 | 99 | interface_list (struct zebra_vrf *zvrf) |
718e3744 | 100 | { |
101 | caddr_t ref, buf, end; | |
102 | size_t bufsiz; | |
103 | struct if_msghdr *ifm; | |
718e3744 | 104 | |
105 | #define MIBSIZ 6 | |
106 | int mib[MIBSIZ] = | |
107 | { | |
108 | CTL_NET, | |
109 | PF_ROUTE, | |
110 | 0, | |
111 | 0, /* AF_INET & AF_INET6 */ | |
112 | NET_RT_IFLIST, | |
113 | 0 | |
114 | }; | |
115 | ||
8f7d9fc0 FL |
116 | if (zvrf->vrf_id != VRF_DEFAULT) |
117 | { | |
118 | zlog_warn ("interface_list: ignore VRF %u", zvrf->vrf_id); | |
119 | return; | |
120 | } | |
121 | ||
718e3744 | 122 | /* Query buffer size. */ |
123 | if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0) | |
124 | { | |
6099b3b5 | 125 | zlog (NULL, LOG_WARNING, "sysctl() error by %s", safe_strerror (errno)); |
718e3744 | 126 | return; |
127 | } | |
128 | ||
129 | /* We free this memory at the end of this function. */ | |
130 | ref = buf = XMALLOC (MTYPE_TMP, bufsiz); | |
131 | ||
132 | /* Fetch interface informations into allocated buffer. */ | |
133 | if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0) | |
134 | { | |
6099b3b5 | 135 | zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno)); |
718e3744 | 136 | return; |
137 | } | |
138 | ||
139 | /* Parse both interfaces and addresses. */ | |
140 | for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen) | |
141 | { | |
142 | ifm = (struct if_msghdr *) buf; | |
143 | ||
144 | switch (ifm->ifm_type) | |
145 | { | |
146 | case RTM_IFINFO: | |
147 | ifm_read (ifm); | |
148 | break; | |
149 | case RTM_NEWADDR: | |
150 | ifam_read ((struct ifa_msghdr *) ifm); | |
151 | break; | |
152 | default: | |
153 | zlog_info ("interfaces_list(): unexpected message type"); | |
154 | XFREE (MTYPE_TMP, ref); | |
155 | return; | |
156 | break; | |
157 | } | |
158 | } | |
159 | ||
160 | /* Free sysctl buffer. */ | |
161 | XFREE (MTYPE_TMP, ref); | |
162 | } |