Debian Codegraph @

agent/mibgroup/ip-forward-mib/data_access/route_sysctl.c

raw
1 /*
2 * Interface MIB architecture support
3 *
4 * $Id$
5 */
6 #include <net-snmp/net-snmp-config.h>
7 #include <net-snmp/net-snmp-includes.h>
8 #include "mibII/mibII_common.h"
9
10 #include <net-snmp/agent/net-snmp-agent-includes.h>
11 #include <net-snmp/data_access/interface.h>
12 #include <net-snmp/data_access/route.h>
13 #include <net-snmp/data_access/ipaddress.h>
14 #include <net/if_dl.h>
15
16 #include "ip-forward-mib/data_access/route_sysctl.h"
17 #include "ip-forward-mib/inetCidrRouteTable/inetCidrRouteTable_constants.h"
18 #include "if-mib/data_access/interface_ioctl.h"
19 #include "route_private.h"
20
21 static int _load_ipv4(netsnmp_container*, int*);
22 static int _load_ipv6(netsnmp_container*, int*);
23 static int _create_ipv4(netsnmp_route_entry *);
24 static int _create_ipv6(netsnmp_route_entry *);
25 static int _delete_ipv4(netsnmp_route_entry *);
26 static int _delete_ipv6(netsnmp_route_entry *);
27
28 #if !defined(SA_SIZE) && !defined(RT_ROUNDUP)
29 #define RT_ROUNDUP(a) \
30 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
31 #endif
32 #ifndef SA_SIZE
33 #define SA_SIZE(x) RT_ROUNDUP(((struct sockaddr *)(x))->sa_len)
34 #endif
35
36 /** arch specific load
37 * @internal
38 *
39 * @retval 0 success
40 * @retval -1 no container specified
41 * @retval -2 could not access data source
42 */
43 int
44 netsnmp_access_route_container_arch_load(netsnmp_container* container,
45 u_int load_flags)
46 {
47 int count, err;
48
49 err = 0;
50
51 DEBUGMSGTL(("access:route:container", "route_container_arch_load\n"));
52
53 if (container == NULL) {
54 snmp_log(LOG_ERR, "no container specified/found for access_route\n");
55 return -1;
56 }
57
58 err = _load_ipv4(container, &count);
59 if (err != 0) {
60 NETSNMP_LOGONCE((LOG_ERR, "_load_ipv4 failed %d\n", err));
61 goto out;
62 }
63
64 if (err != 0 || load_flags & NETSNMP_ACCESS_ROUTE_LOAD_IPV4_ONLY)
65 return err;
66
67 err = _load_ipv6(container, &count);
68 if (err != 0) {
69 NETSNMP_LOGONCE((LOG_ERR, "_load_ipv6 failed %d\n", err));
70 goto out;
71 }
72
73 out:
74 return (err == 0 ? 0 : -3);
75 }
76
77 /** arch specific new route entry creation
78 * @internal
79 *
80 * @retval 0 success
81 * @retval -1 invalid entry specified
82 * @retval -2 could not create route entry
83 */
84 int
85 netsnmp_arch_route_create(netsnmp_route_entry *entry)
86 {
87 int err;
88
89 if (NULL == entry)
90 return -1;
91
92 switch (entry->rt_dest_type) {
93 case INETADDRESSTYPE_IPV4:
94 err = _create_ipv4(entry);
95 break;
96 #ifdef NETSNMP_ENABLE_IPV6
97 case INETADDRESSTYPE_IPV6:
98 err = _create_ipv6(entry);
99 break;
100 #endif
101 default:
102 err = 0;
103 }
104
105 return (err == 0 ? 0 : -2);
106 }
107
108 /** arch specific new route entry deletion
109 * @internal
110 *
111 * @retval 0 success
112 * @retval -1 invalid entry specified
113 * @retval -2 could not delete route entry
114 */
115 int
116 netsnmp_arch_route_delete(netsnmp_route_entry *entry)
117 {
118 int err;
119
120 DEBUGMSGTL(("access:route:container",
121 "route_container_arch_delete\n"));
122
123 if (NULL == entry)
124 return -1;
125
126 switch (entry->rt_dest_type) {
127 case INETADDRESSTYPE_IPV4:
128 err = _delete_ipv4(entry);
129 break;
130 #ifdef NETSNMP_ENABLE_IPV6
131 case INETADDRESSTYPE_IPV6:
132 err = _delete_ipv6(entry);
133 break;
134 #endif
135 default:
136 err = 0;
137 }
138
139 return (err == 0 ? 0 : -2);
140 }
141
142 #if defined(freebsd4) || defined(netbsd4) || defined(openbsd) || defined(darwin)
143 static int
144 _type_from_flags(int flags)
145 {
146 if (flags & RTF_UP) {
147 if (flags & RTF_BLACKHOLE)
148 return INETCIDRROUTETYPE_REJECT;
149 #ifdef RTF_LOCAL
150 else if (flags & RTF_LOCAL)
151 return INETCIDRROUTETYPE_LOCAL;
152 #endif
153 else if (flags & RTF_GATEWAY)
154 return INETCIDRROUTETYPE_REMOTE;
155 else
156 return INETCIDRROUTETYPE_OTHER;
157 }
158 return 0;
159 }
160
161
162 static int
163 _load_routing_table_from_sysctl(netsnmp_container* container, int *index,
164 int family)
165 {
166 char *buf, *lim, *next;
167 int mib[6];
168 size_t needed;
169 int err;
170 u_char dest_len, dest_type;
171 struct rt_msghdr *rtm;
172
173 buf = NULL;
174 err = 0;
175
176 if (family == AF_INET) {
177 dest_len = 4;
178 dest_type = INETADDRESSTYPE_IPV4;
179 } else if (family == AF_INET6) {
180 dest_len = 16;
181 dest_type = INETADDRESSTYPE_IPV6;
182 } else {
183 err = EINVAL;
184 goto out;
185 }
186
187 mib[0] = CTL_NET;
188 mib[1] = PF_ROUTE;
189 mib[2] = 0;
190 mib[3] = family;
191 mib[4] = NET_RT_DUMP;
192 mib[5] = 0;
193
194 if (sysctl(mib, (sizeof(mib) / sizeof(mib[0])), NULL, &needed, NULL,
195 0) == -1) {
196 err = errno;
197 goto out;
198 }
199
200 if ((buf = malloc(needed)) == NULL) {
201 err = ENOMEM;
202 goto out;
203 }
204
205 if (sysctl(mib, (sizeof(mib) / sizeof(mib[0])), buf, &needed, NULL,
206 0) == -1) {
207 err = errno;
208 goto out;
209 }
210
211 lim = buf + needed;
212
213 for (next = buf; next < lim; next += rtm->rtm_msglen) {
214 struct sockaddr *if_name = NULL, *if_addr = NULL;
215 struct sockaddr *dest_sa = NULL, *gateway_sa = NULL, *netmask_sa = NULL;
216 netsnmp_route_entry *entry;
217 char *addr_ptr;
218
219 rtm = (struct rt_msghdr*)next;
220
221 /*
222 * Some code in netstat checks for this ("netmasks done" case).
223 * Filter this out (I don't know why it should exist).
224 */
225 if (rtm->rtm_addrs == RTA_DST)
226 continue;
227 #ifdef RTF_CLONED
228 if (rtm->rtm_flags & RTF_CLONED)
229 continue;
230 #endif
231 #ifdef RTF_WASCLONED
232 if (rtm->rtm_flags & RTF_WASCLONED)
233 continue;
234 #endif
235
236 entry = netsnmp_access_route_entry_create();
237 if (entry == NULL)
238 return -3;
239 memset(entry->rt_dest, 0, dest_len);
240 entry->rt_mask = 0;
241 memset(entry->rt_nexthop, 0, dest_len);
242
243 addr_ptr = (char *)(rtm + 1);
244
245 if (rtm->rtm_addrs & RTA_DST) {
246 dest_sa = (struct sockaddr *)addr_ptr;
247 addr_ptr += SA_SIZE(dest_sa);
248 }
249 if (rtm->rtm_addrs & RTA_GATEWAY) {
250 gateway_sa = (struct sockaddr *)addr_ptr;
251 addr_ptr += SA_SIZE(gateway_sa);
252 }
253 if (rtm->rtm_addrs & RTA_NETMASK) {
254 netmask_sa = (struct sockaddr *)addr_ptr;
255 addr_ptr += SA_SIZE(netmask_sa);
256 }
257 if (rtm->rtm_addrs & RTA_IFP) {
258 if_name = (struct sockaddr *)addr_ptr;
259 addr_ptr += SA_SIZE(if_name);
260 }
261 if (rtm->rtm_addrs & RTA_IFA) {
262 if_addr = (struct sockaddr *)addr_ptr;
263 addr_ptr += SA_SIZE(if_addr);
264 }
265
266 entry->if_index = rtm->rtm_index;
267
268 /* arbitrary index */
269 entry->ns_rt_index = ++(*index);
270
271 /* copy dest & next hop */
272 entry->rt_dest_type = dest_type;
273 entry->rt_dest_len = dest_len;
274 if (rtm->rtm_addrs & RTA_DST) {
275 if (family == AF_INET)
276 memcpy(entry->rt_dest,
277 &((struct sockaddr_in*)dest_sa)->sin_addr, dest_len);
278 #ifdef NETSNMP_ENABLE_IPV6
279 if (family == AF_INET6)
280 memcpy(entry->rt_dest,
281 &((struct sockaddr_in6*)dest_sa)->sin6_addr, dest_len);
282 #endif
283 }
284
285 entry->rt_nexthop_type = dest_type;
286 entry->rt_nexthop_len = dest_len;
287 if (rtm->rtm_addrs & RTA_GATEWAY) {
288 if (family == AF_INET)
289 memcpy(entry->rt_nexthop,
290 &((struct sockaddr_in*)gateway_sa)->sin_addr, dest_len);
291 #ifdef NETSNMP_ENABLE_IPV6
292 if (family == AF_INET6)
293 memcpy(entry->rt_nexthop,
294 &((struct sockaddr_in6*)gateway_sa)->sin6_addr, dest_len);
295 #endif
296 }
297 else {
298 if (family == AF_INET)
299 memcpy(entry->rt_nexthop,
300 &((struct sockaddr_in*)if_addr)->sin_addr, dest_len);
301 #ifdef NETSNMP_ENABLE_IPV6
302 if (family == AF_INET6)
303 memcpy(entry->rt_nexthop,
304 &((struct sockaddr_in6*)if_addr)->sin6_addr, dest_len);
305 #endif
306 }
307
308 if (family == AF_INET) {
309 if (netmask_sa) {
310 /* count bits in mask */
311 entry->rt_pfx_len = netsnmp_ipaddress_ipv4_prefix_len(
312 ((struct sockaddr_in *)netmask_sa)->sin_addr.s_addr);
313 memcpy(&entry->rt_mask, &((struct sockaddr_in *)netmask_sa)->sin_addr, 4);
314 }
315 else {
316 entry->rt_pfx_len = 32;
317 }
318 }
319 #ifdef NETSNMP_ENABLE_IPV6
320 if (family == AF_INET6) {
321 if (netmask_sa) {
322 /* count bits in mask */
323 entry->rt_pfx_len = netsnmp_ipaddress_ipv6_prefix_len(
324 ((struct sockaddr_in6 *)netmask_sa)->sin6_addr);
325 memcpy(&entry->rt_mask, &((struct sockaddr_in6 *)netmask_sa)->sin6_addr, 16);
326 }
327 else {
328 entry->rt_pfx_len = 128;
329 }
330 }
331 #endif
332
333 #ifdef USING_IP_FORWARD_MIB_INETCIDRROUTETABLE_INETCIDRROUTETABLE_MODULE
334 /*
335 inetCidrRoutePolicy OBJECT-TYPE
336 SYNTAX OBJECT IDENTIFIER
337 MAX-ACCESS not-accessible
338 STATUS current
339 DESCRIPTION
340 "This object is an opaque object without any defined
341 semantics. Its purpose is to serve as an additional
342 index which may delineate between multiple entries to
343 the same destination. The value { 0 0 } shall be used
344 as the default value for this object."
345 */
346 entry->rt_policy = calloc(3, sizeof(oid));
347 entry->rt_policy[2] = entry->if_index;
348 entry->rt_policy_len = sizeof(oid)*3;
349 #endif
350
351 entry->rt_type = _type_from_flags(rtm->rtm_flags);
352 entry->rt_age = rtm->rtm_rmx.rmx_expire;
353 entry->rt_nexthop_as = 0;
354 /* entry->rt_metric1 = rtm->rtm_rmx.rmx_hopcount; */
355
356 if (rtm->rtm_flags & RTF_DYNAMIC)
357 entry->rt_proto = IANAIPROUTEPROTOCOL_ICMP;
358 else if (rtm->rtm_flags & RTF_STATIC)
359 entry->rt_proto = IANAIPROUTEPROTOCOL_NETMGMT;
360 #ifdef RTF_LOCAL
361 else if (rtm->rtm_flags & RTF_LOCAL)
362 entry->rt_proto = IANAIPROUTEPROTOCOL_LOCAL;
363 #endif
364 else
365 entry->rt_proto = IANAIPROUTEPROTOCOL_OTHER;
366
367 if (CONTAINER_INSERT(container, entry) < 0) {
368 DEBUGMSGTL(("access:route:container",
369 "error with route_entry: insert into container failed.\n"));
370 netsnmp_access_route_entry_free(entry);
371 continue;
372 }
373 }
374
375 out:
376 free(buf);
377
378 return err;
379 }
380
381 /**
382 * netsnmp_access_route_container_arch_load* functions.
383 */
384 static int
385 _load_ipv4(netsnmp_container* container, int *index)
386 {
387
388 DEBUGMSGTL(("access:route:container",
389 "route_container_arch_load ipv4\n"));
390 return _load_routing_table_from_sysctl(container, index, AF_INET);
391 }
392
393 static int
394 _load_ipv6(netsnmp_container* container, int *index)
395 {
396
397 #ifdef NETSNMP_ENABLE_IPV6
398 DEBUGMSGTL(("access:route:container",
399 "route_container_arch_load ipv6\n"));
400 return _load_routing_table_from_sysctl(container, index, AF_INET6);
401 #else
402 return 0;
403 #endif
404 }
405
406 /**
407 * netsnmp_access_route_create* functions.
408 *
409 * TODO: add logic similar to newroute(..) in sbin/route/route.c .
410 */
411 static int
412 _create_ipv4(netsnmp_route_entry *entry)
413 {
414
415 return 0;
416 }
417
418 static int
419 _create_ipv6(netsnmp_route_entry *entry)
420 {
421
422 return 0;
423 }
424
425 /**
426 * netsnmp_access_route_delete* functions.
427 *
428 * TODO: add logic similar to newroute(..) in sbin/route/route.c .
429 */
430 static int
431 _delete_ipv4(netsnmp_route_entry *entry)
432 {
433
434 return 0;
435 }
436
437 static int
438 _delete_ipv6(netsnmp_route_entry *entry)
439 {
440 #ifdef NETSNMP_ENABLE_IPV6
441
442 #endif
443 return 0;
444 }
445 #endif /* defined(freebsd7) || defined(netbsd) || defined(openbsd) */