]> git.proxmox.com Git - mirror_frr.git/blob - zebra/if_sysctl.c
Merge pull request #261 from opensourcerouting/lib_cleanup
[mirror_frr.git] / zebra / if_sysctl.c
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 "zebra_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
40 void
41 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 {
51 CTL_NET,
52 PF_ROUTE,
53 0,
54 0, /* AF_INET & AF_INET6 */
55 NET_RT_IFLIST,
56 0
57 };
58
59 /* Query buffer size. */
60 if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0)
61 {
62 zlog_warn ("sysctl() error by %s", safe_strerror (errno));
63 return;
64 }
65
66 /* We free this memory at the end of this function. */
67 ref = buf = XMALLOC (MTYPE_TMP, bufsiz);
68
69 /* Fetch interface informations into allocated buffer. */
70 if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0)
71 {
72 zlog_warn("sysctl error by %s", safe_strerror(errno));
73 XFREE(MTYPE_TMP, ref);
74 return;
75 }
76
77 /* Parse both interfaces and addresses. */
78 for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen)
79 {
80 ifm = (struct if_msghdr *) buf;
81 if (ifm->ifm_type == RTM_IFINFO)
82 {
83 ifp = if_lookup_by_index (ifm->ifm_index);
84 if (ifp)
85 ifp->stats = ifm->ifm_data;
86 }
87 }
88
89 /* Free sysctl buffer. */
90 XFREE (MTYPE_TMP, ref);
91
92 return;
93 }
94
95 /* Interface listing up function using sysctl(). */
96 void
97 interface_list (struct zebra_ns *zns)
98 {
99 caddr_t ref, buf, end;
100 size_t bufsiz;
101 struct if_msghdr *ifm;
102
103 #define MIBSIZ 6
104 int mib[MIBSIZ] =
105 {
106 CTL_NET,
107 PF_ROUTE,
108 0,
109 0, /* AF_INET & AF_INET6 */
110 NET_RT_IFLIST,
111 0
112 };
113
114 if (zns->ns_id != NS_DEFAULT)
115 {
116 zlog_warn ("interface_list: ignore NS %u", zns->ns_id);
117 return;
118 }
119
120 /* Query buffer size. */
121 if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0)
122 {
123 zlog_warn("sysctl() error by %s", safe_strerror(errno));
124 return;
125 }
126
127 /* We free this memory at the end of this function. */
128 ref = buf = XMALLOC (MTYPE_TMP, bufsiz);
129
130 /* Fetch interface informations into allocated buffer. */
131 if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0)
132 {
133 zlog_warn("sysctl error by %s", safe_strerror(errno));
134 return;
135 }
136
137 /* Parse both interfaces and addresses. */
138 for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen)
139 {
140 ifm = (struct if_msghdr *) buf;
141
142 switch (ifm->ifm_type)
143 {
144 case RTM_IFINFO:
145 ifm_read (ifm);
146 break;
147 case RTM_NEWADDR:
148 ifam_read ((struct ifa_msghdr *) ifm);
149 break;
150 default:
151 zlog_info ("interfaces_list(): unexpected message type");
152 XFREE (MTYPE_TMP, ref);
153 return;
154 break;
155 }
156 }
157
158 /* Free sysctl buffer. */
159 XFREE (MTYPE_TMP, ref);
160 }