]> git.proxmox.com Git - mirror_frr.git/blob - zebra/if_sysctl.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / zebra / if_sysctl.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Get interface's address and mask information by sysctl() function.
4 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 */
6
7 #include <zebra.h>
8
9 #if !defined(GNU_LINUX) && !defined(OPEN_BSD)
10
11 #include "if.h"
12 #include "sockunion.h"
13 #include "prefix.h"
14 #include "connected.h"
15 #include "memory.h"
16 #include "ioctl.h"
17 #include "log.h"
18 #include "interface.h"
19 #include "vrf.h"
20
21 #include "zebra/rt.h"
22 #include "zebra/kernel_socket.h"
23 #include "zebra/rib.h"
24 #include "zebra/zebra_errors.h"
25
26 void ifstat_update_sysctl(void)
27 {
28 caddr_t ref, buf, end;
29 size_t bufsiz;
30 struct if_msghdr *ifm;
31 struct interface *ifp;
32
33 #define MIBSIZ 6
34 int mib[MIBSIZ] = {
35 CTL_NET, PF_ROUTE, 0, 0, /* AF_INET & AF_INET6 */
36 NET_RT_IFLIST, 0};
37
38 /* Query buffer size. */
39 if (sysctl(mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0) {
40 flog_warn(EC_ZEBRA_SYSCTL_FAILED, "sysctl() error by %s",
41 safe_strerror(errno));
42 return;
43 }
44
45 /* We free this memory at the end of this function. */
46 ref = buf = XMALLOC(MTYPE_TMP, bufsiz);
47
48 /* Fetch interface information into allocated buffer. */
49 if (sysctl(mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0) {
50 flog_warn(EC_ZEBRA_SYSCTL_FAILED, "sysctl error by %s",
51 safe_strerror(errno));
52 XFREE(MTYPE_TMP, ref);
53 return;
54 }
55
56 /* Parse both interfaces and addresses. */
57 for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen) {
58 ifm = (struct if_msghdr *)buf;
59 if (ifm->ifm_type == RTM_IFINFO) {
60 ifp = if_lookup_by_index(ifm->ifm_index, VRF_DEFAULT);
61 if (ifp)
62 ifp->stats = ifm->ifm_data;
63 }
64 }
65
66 /* Free sysctl buffer. */
67 XFREE(MTYPE_TMP, ref);
68
69 return;
70 }
71
72 /* Interface listing up function using sysctl(). */
73 void interface_list(struct zebra_ns *zns)
74 {
75 caddr_t ref, buf, end;
76 size_t bufsiz;
77 struct if_msghdr *ifm;
78
79 #define MIBSIZ 6
80 int mib[MIBSIZ] = {
81 CTL_NET, PF_ROUTE, 0, 0, /* AF_INET & AF_INET6 */
82 NET_RT_IFLIST, 0};
83
84 if (zns->ns_id != NS_DEFAULT) {
85 zlog_debug("%s: ignore NS %u", __func__, zns->ns_id);
86 return;
87 }
88
89 /* Query buffer size. */
90 if (sysctl(mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0) {
91 flog_err_sys(EC_ZEBRA_IFLIST_FAILED,
92 "Could not enumerate interfaces: %s",
93 safe_strerror(errno));
94 return;
95 }
96
97 /* We free this memory at the end of this function. */
98 ref = buf = XMALLOC(MTYPE_TMP, bufsiz);
99
100 /* Fetch interface information into allocated buffer. */
101 if (sysctl(mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0) {
102 flog_err_sys(EC_ZEBRA_IFLIST_FAILED,
103 "Could not enumerate interfaces: %s",
104 safe_strerror(errno));
105 return;
106 }
107
108 /* Parse both interfaces and addresses. */
109 for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen) {
110 ifm = (struct if_msghdr *)buf;
111
112 switch (ifm->ifm_type) {
113 case RTM_IFINFO:
114 ifm_read(ifm);
115 break;
116 case RTM_NEWADDR:
117 ifam_read((struct ifa_msghdr *)ifm);
118 break;
119 default:
120 zlog_info("%s: unexpected message type", __func__);
121 XFREE(MTYPE_TMP, ref);
122 return;
123 break;
124 }
125 }
126
127 /* Free sysctl buffer. */
128 XFREE(MTYPE_TMP, ref);
129 }
130
131 #endif /* !defined(GNU_LINUX) && !defined(OPEN_BSD) */