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