Debian Codegraph @

agent/mibgroup/ip-mib/data_access/arp_sysctl.c

raw
1 /*
2 * Interface MIB architecture support
3 */
4 #include <net-snmp/net-snmp-config.h>
5 #include <net-snmp/net-snmp-includes.h>
6
7 #include <net-snmp/agent/net-snmp-agent-includes.h>
8 #include <net-snmp/data_access/arp.h>
9 #include <net-snmp/data_access/interface.h>
10
11 #include <sys/param.h>
12 #include <sys/socket.h>
13 #include <sys/sysctl.h>
14 #include <net/if.h>
15 #include <net/if_dl.h>
16 #include <net/if_types.h>
17 #include <net/route.h>
18 #include <netinet/if_ether.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <errno.h>
22
23 #if !defined(SA_SIZE) && !defined(RT_ROUNDUP)
24 #define RT_ROUNDUP(a) \
25 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
26 #endif
27
28 static int _load_arp_table_from_sysctl(netsnmp_arp_access *);
29 static int _load_ndp_table_from_sysctl(netsnmp_arp_access *);
30
31 netsnmp_arp_access *
32 netsnmp_access_arp_create(u_int init_flags,
33 NetsnmpAccessArpUpdate *update_hook,
34 NetsnmpAccessArpGC *gc_hook,
35 int *cache_timeout, int *cache_flags,
36 char *cache_expired)
37 {
38 netsnmp_arp_access *access;
39
40 access = SNMP_MALLOC_TYPEDEF(netsnmp_arp_access);
41 if (NULL == access) {
42 snmp_log(LOG_ERR, "malloc error in netsnmp_access_arp_create\n");
43 return NULL;
44 }
45
46 access->arch_magic = NULL;
47 access->magic = NULL;
48 access->update_hook = update_hook;
49 access->gc_hook = gc_hook;
50 access->synchronized = 0;
51
52 if (cache_timeout != NULL)
53 *cache_timeout = 5;
54 if (cache_flags != NULL)
55 *cache_flags |= NETSNMP_CACHE_DONT_FREE_BEFORE_LOAD
56 | NETSNMP_CACHE_AUTO_RELOAD;
57 access->cache_expired = cache_expired;
58
59 return access;
60 }
61
62 int netsnmp_access_arp_delete(netsnmp_arp_access *access)
63 {
64 if (NULL == access)
65 return 0;
66
67 netsnmp_access_arp_unload(access);
68 free(access);
69
70 return 0;
71 }
72
73 int netsnmp_access_arp_load(netsnmp_arp_access *access)
74 {
75 int err = 0;
76
77 access->generation++;
78 DEBUGMSGTL(("access:route:container",
79 "route_container_arch_load ipv4\n"));
80 err = _load_arp_table_from_sysctl(access);
81 if (err != 0) {
82 NETSNMP_LOGONCE((LOG_ERR,
83 "netsnmp_access_arp_ipv4 failed %d\n", err));
84 goto out;
85 }
86 access->gc_hook(access);
87 access->synchronized = (err == 0);
88
89 #ifdef NETSNMP_ENABLE_IPV6
90 DEBUGMSGTL(("access:route:container",
91 "route_container_arch_load ipv6\n"));
92 err = _load_ndp_table_from_sysctl(access);
93 if (err != 0) {
94 NETSNMP_LOGONCE((LOG_ERR,
95 "netsnmp_access_arp_ipv6 failed %d\n", err));
96 goto out;
97 }
98 access->gc_hook(access);
99 access->synchronized = (err == 0);
100 #endif
101
102 out:
103 return (err == 0 ? 0 : -1);
104 }
105
106 int netsnmp_access_arp_unload(netsnmp_arp_access *access)
107 {
108 access->synchronized = 0;
109
110 return 0;
111 }
112
113 static int
114 _load_arp_table_from_sysctl(netsnmp_arp_access *access)
115 {
116 netsnmp_arp_entry *entry;
117 struct rt_msghdr *rtm;
118 struct sockaddr_inarp *sin2;
119 struct sockaddr_dl *sdl;
120 char *buf, *lim, *newbuf, *next;
121 int mib[6];
122 size_t needed;
123 int err, st;
124
125 netsnmp_assert(NULL != access);
126
127 mib[0] = CTL_NET;
128 mib[1] = PF_ROUTE;
129 mib[2] = 0;
130 mib[3] = AF_INET;
131 mib[4] = NET_RT_FLAGS;
132 #ifdef RTF_LLINFO
133 mib[5] = RTF_LLINFO;
134 #elif defined(RTF_LLDATA)
135 mib[5] = RTF_LLDATA;
136 #else
137 mib[5] = 0;
138 #endif
139
140 err = 0;
141 buf = newbuf = NULL;
142
143 /* The following logic was adapted from search(..) in usr.sbin/arp/arp.c */
144
145 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
146 err = errno;
147 goto out;
148 }
149
150 /* Empty arp table. */
151 if (needed == 0)
152 goto out;
153
154 for (;;) {
155 newbuf = realloc(buf, needed);
156 if (newbuf == NULL) {
157 err = ENOMEM;
158 goto out;
159 }
160 buf = newbuf;
161 st = sysctl(mib, 6, buf, &needed, NULL, 0);
162 if (st == 0 || errno != ENOMEM)
163 break;
164 else
165 needed += needed / 8; /* XXX: why 8? */
166 }
167 if (st == -1) {
168 err = errno;
169 goto out;
170 }
171
172 lim = buf + needed;
173 for (next = buf; next < lim; next += rtm->rtm_msglen) {
174
175 rtm = (struct rt_msghdr *)next;
176 sin2 = (struct sockaddr_inarp*)(rtm + 1);
177 #ifdef SA_SIZE
178 sdl = (struct sockaddr_dl*)((char *)sin2 + SA_SIZE(sin2));
179 #else
180 sdl = (struct sockaddr_dl*)(void *)(RT_ROUNDUP(sin2->sin_len) + (char *)(void *)sin2);
181 #endif
182
183 entry = netsnmp_access_arp_entry_create();
184 if (NULL == entry) {
185 err = ENOMEM;
186 break;
187 }
188
189 entry->generation = access->generation;
190 entry->if_index = rtm->rtm_index;
191
192 entry->arp_ipaddress_len = 4;
193
194 memcpy(entry->arp_ipaddress, &sin2->sin_addr.s_addr,
195 entry->arp_ipaddress_len);
196
197 /* HW Address */
198 entry->arp_physaddress_len = 6;
199 if (0 < sdl->sdl_alen &&
200 sdl->sdl_alen <= NETSNMP_ACCESS_ARP_PHYSADDR_BUF_SIZE) {
201 memcpy(entry->arp_physaddress, LLADDR(sdl), sdl->sdl_alen);
202 /* Process status */
203 /* XXX: setting this value for all states is wrong. */
204 entry->arp_state = INETNETTOMEDIASTATE_REACHABLE;
205 } else {
206 entry->arp_physaddress[0] = '\0';
207 entry->arp_state = INETNETTOMEDIASTATE_INCOMPLETE;
208 }
209
210 /* Process type */
211 /* XXX: more states should be handled here, probably.. */
212 if (rtm->rtm_rmx.rmx_expire == 0)
213 entry->arp_type = INETNETTOMEDIATYPE_STATIC;
214 else
215 entry->arp_type = INETNETTOMEDIATYPE_DYNAMIC;
216
217 access->update_hook(access, entry);
218
219 }
220
221 out:
222 free(buf);
223 return err;
224 }
225
226 static int
227 _load_ndp_table_from_sysctl(netsnmp_arp_access *access)
228 {
229 #if 1
230 netsnmp_arp_entry *entry;
231 struct rt_msghdr *rtm;
232 struct sockaddr_in6 *sin2;
233 struct sockaddr_dl *sdl;
234 size_t needed;
235 int err, mib[6], st;
236 char *buf, *lim, *newbuf, *next;
237
238 netsnmp_assert(NULL != access);
239
240 mib[0] = CTL_NET;
241 mib[1] = PF_ROUTE;
242 mib[2] = 0;
243 mib[3] = AF_INET6;
244 mib[4] = NET_RT_FLAGS;
245 #ifdef RTF_LLINFO
246 mib[5] = RTF_LLINFO;
247 #elif defined(RTF_LLDATA)
248 mib[5] = RTF_LLDATA;
249 #else
250 mib[5] = 0;
251 #endif
252
253 err = 0;
254 buf = newbuf = NULL;
255
256 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
257 err = errno;
258 goto out;
259 }
260
261 /* Empty arp table. */
262 if (needed == 0)
263 goto out;
264
265 for (;;) {
266 newbuf = realloc(buf, needed);
267 if (newbuf == NULL) {
268 err = ENOMEM;
269 goto out;
270 }
271 buf = newbuf;
272 st = sysctl(mib, 6, buf, &needed, NULL, 0);
273 if (st == 0 || errno != ENOMEM)
274 break;
275 else
276 needed += needed / 8; /* XXX: why 8? */
277 }
278 if (st == -1) {
279 err = errno;
280 goto out;
281 }
282
283 lim = buf + needed;
284 for (next = buf; next < lim; next += rtm->rtm_msglen) {
285
286 rtm = (struct rt_msghdr *)next;
287 sin2 = (struct sockaddr_in6*)(rtm + 1);
288 #ifdef SA_SIZE
289 sdl = (struct sockaddr_dl*)((char *)sin2 + SA_SIZE(sin2));
290 #else
291 sdl = (struct sockaddr_dl*)(void *)(RT_ROUNDUP(sin2->sin6_len) + (char *)(void *)sin2);
292 #endif
293
294 if (!(rtm->rtm_flags & RTF_HOST) ||
295 IN6_IS_ADDR_MULTICAST(&sin2->sin6_addr))
296 continue;
297
298 entry = netsnmp_access_arp_entry_create();
299 if (NULL == entry) {
300 err = ENOMEM;
301 break;
302 }
303
304 entry->generation = access->generation;
305 entry->if_index = rtm->rtm_index;
306
307 entry->arp_ipaddress_len = 16;
308
309 memcpy(entry->arp_ipaddress, &sin2->sin6_addr.s6_addr,
310 entry->arp_ipaddress_len);
311
312 /* HW Address */
313 entry->arp_physaddress_len = sdl->sdl_alen;
314 if (0 < sdl->sdl_alen &&
315 sdl->sdl_alen <= NETSNMP_ACCESS_ARP_PHYSADDR_BUF_SIZE) {
316 memcpy(entry->arp_physaddress, LLADDR(sdl), sdl->sdl_alen);
317 /* Process status */
318 /* XXX: setting this value for all states is wrong. */
319 entry->arp_state = INETNETTOMEDIASTATE_REACHABLE;
320 } else {
321 entry->arp_physaddress[0] = '\0';
322 entry->arp_state = INETNETTOMEDIASTATE_INCOMPLETE;
323 }
324
325 /* Process type */
326 /* XXX: more states should be handled here, probably.. */
327 if (rtm->rtm_rmx.rmx_expire == 0)
328 entry->arp_type = INETNETTOMEDIATYPE_STATIC;
329 else
330 entry->arp_type = INETNETTOMEDIATYPE_DYNAMIC;
331
332 access->update_hook(access, entry);
333
334 }
335
336 out:
337 free(buf);
338 return err;
339 #else
340 return 0;
341 #endif
342 }