blob: ffa6927d617ec63560cb3e5caad70985bf299063 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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"
David Lamparter388f8852015-03-03 08:55:54 +010032#include "interface.h"
paul718e3742002-12-13 20:15:29 +000033
paulec1a4282005-11-24 15:15:17 +000034#include "zebra/rt.h"
35#include "zebra/kernel_socket.h"
36
Paul Jakmaf28b0e52006-08-06 15:57:59 +000037void
38ifstat_update_sysctl (void)
paul718e3742002-12-13 20:15:29 +000039{
40 caddr_t ref, buf, end;
41 size_t bufsiz;
42 struct if_msghdr *ifm;
43 struct interface *ifp;
44
45#define MIBSIZ 6
46 int mib[MIBSIZ] =
47 {
48 CTL_NET,
49 PF_ROUTE,
50 0,
51 0, /* AF_INET & AF_INET6 */
52 NET_RT_IFLIST,
53 0
54 };
55
56 /* Query buffer size. */
57 if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0)
58 {
ajs6099b3b2004-11-20 02:06:59 +000059 zlog_warn ("sysctl() error by %s", safe_strerror (errno));
Paul Jakmaf28b0e52006-08-06 15:57:59 +000060 return;
paul718e3742002-12-13 20:15:29 +000061 }
62
63 /* We free this memory at the end of this function. */
64 ref = buf = XMALLOC (MTYPE_TMP, bufsiz);
65
66 /* Fetch interface informations into allocated buffer. */
67 if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0)
68 {
ajs6099b3b2004-11-20 02:06:59 +000069 zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno));
Paul Jakmaf28b0e52006-08-06 15:57:59 +000070 return;
paul718e3742002-12-13 20:15:29 +000071 }
72
73 /* Parse both interfaces and addresses. */
74 for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen)
75 {
76 ifm = (struct if_msghdr *) buf;
77 if (ifm->ifm_type == RTM_IFINFO)
78 {
79 ifp = if_lookup_by_index (ifm->ifm_index);
80 if (ifp)
81 ifp->stats = ifm->ifm_data;
82 }
83 }
84
85 /* Free sysctl buffer. */
86 XFREE (MTYPE_TMP, ref);
87
Paul Jakmaf28b0e52006-08-06 15:57:59 +000088 return;
paul718e3742002-12-13 20:15:29 +000089}
90
91/* Interface listing up function using sysctl(). */
92void
93interface_list ()
94{
95 caddr_t ref, buf, end;
96 size_t bufsiz;
97 struct if_msghdr *ifm;
paul718e3742002-12-13 20:15:29 +000098
99#define MIBSIZ 6
100 int mib[MIBSIZ] =
101 {
102 CTL_NET,
103 PF_ROUTE,
104 0,
105 0, /* AF_INET & AF_INET6 */
106 NET_RT_IFLIST,
107 0
108 };
109
110 /* Query buffer size. */
111 if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0)
112 {
ajs6099b3b2004-11-20 02:06:59 +0000113 zlog (NULL, LOG_WARNING, "sysctl() error by %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000114 return;
115 }
116
117 /* We free this memory at the end of this function. */
118 ref = buf = XMALLOC (MTYPE_TMP, bufsiz);
119
120 /* Fetch interface informations into allocated buffer. */
121 if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0)
122 {
ajs6099b3b2004-11-20 02:06:59 +0000123 zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000124 return;
125 }
126
127 /* Parse both interfaces and addresses. */
128 for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen)
129 {
130 ifm = (struct if_msghdr *) buf;
131
132 switch (ifm->ifm_type)
133 {
134 case RTM_IFINFO:
135 ifm_read (ifm);
136 break;
137 case RTM_NEWADDR:
138 ifam_read ((struct ifa_msghdr *) ifm);
139 break;
140 default:
141 zlog_info ("interfaces_list(): unexpected message type");
142 XFREE (MTYPE_TMP, ref);
143 return;
144 break;
145 }
146 }
147
148 /* Free sysctl buffer. */
149 XFREE (MTYPE_TMP, ref);
150}