Debian Codegraph @

src/OVAL/probes/unix/routingtable_probe.c

raw symbols SCIP
1 /**
2 * @file routingtable_probe.c
3 * @brief routingtable probe
4 * @author "Daniel Kopecek <[email protected]>"
5 */
6
7 /*
8 * Copyright 2009-2012 Red Hat Inc., Durham, North Carolina.
9 * All Rights Reserved.
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 * Authors:
26 * Daniel Kopecek <[email protected]>
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #if defined(OS_APPLE)
38 #include <machine/endian.h>
39 #elif defined(OS_FREEBSD)
40 #include <sys/endian.h>
41 #include <sys/socket.h>
42 #else
43 #include <endian.h>
44 #endif
45 #include <netinet/in.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip6.h>
48 #include <net/route.h>
49 #include <arpa/inet.h>
50
51 #include "probe-api.h"
52 #include "probe/entcmp.h"
53 #include "util.h"
54 #include "debug_priv.h"
55 #include "SEAP/generic/strto.h"
56 #include "routingtable_probe.h"
57
58 #ifndef RT_FLAGS_MAX
59 #define RT_FLAGS_MAX 10
60 #endif
61
62 #ifndef IF_NAME_MAXLEN
63 #define IF_NAME_MAXLEN 128
64 #endif
65
66 #define RT_INFO_DELIMITERS " \t"
67
68 #ifndef htobe32
69 #ifdef OS_APPLE
70 #include <libkern/OSByteOrder.h>
71 #define __bswap_16 OSSwapInt16
72 #define __bswap_32 OSSwapInt32
73 #define __bswap_64 OSSwapInt64
74 #else
75 #include <byteswap.h>
76 #endif
77 # if BYTE_ORDER == LITTLE_ENDIAN
78 # define htobe32(x) __bswap_32 (x)
79 # else
80 # define htobe32(x) (x)
81 # endif
82 #endif
83
84 struct route_info {
85 SEXP_t *ip_dst_ent;
86 char ip_dst[INET6_ADDRSTRLEN+1];
87 char ip_gw[INET6_ADDRSTRLEN+1];
88 char *rt_flags[RT_FLAGS_MAX+1];
89 char if_name[IF_NAME_MAXLEN+1];
90 int ip_version;
91 };
92
93 static int hexstring2bin(const char *hexstr, size_t hexlen, uint8_t *binbuf, size_t binlen)
94 {
95 register size_t i, l;
96
97 if (hexlen % 2 != 0) {
98 return -1;
99 }
100 if (binlen * 2 < hexlen) {
101 return -1;
102 }
103
104 for (l = 0, i = 0; i < hexlen; ++i,++i,++l) {
105 binbuf[l] = strto_uint8_hex(hexstr + i, 2, NULL);
106 if (errno != 0)
107 return -1;
108 }
109
110 return 0;
111 }
112
113 static int collect_item(struct route_info *rt, probe_ctx *ctx)
114 {
115 SEXP_t *item, *rt_dst;
116 oval_datatype_t addr_type;
117
118 rt_dst = SEXP_string_new(rt->ip_dst, strlen(rt->ip_dst));
119
120 if (probe_entobj_cmp(rt->ip_dst_ent, rt_dst) != OVAL_RESULT_TRUE) {
121 SEXP_free(rt_dst);
122 return 0;
123 }
124
125 addr_type = rt->ip_version == 4 ? OVAL_DATATYPE_IPV4ADDR : OVAL_DATATYPE_IPV6ADDR;
126
127 /* create the item */
128 item = probe_item_create(OVAL_UNIX_ROUTINGTABLE, NULL,
129 "destination", addr_type, rt->ip_dst,
130 "gateway", addr_type, rt->ip_gw,
131 "flags", OVAL_DATATYPE_STRING_M, rt->rt_flags,
132 "interface_name", OVAL_DATATYPE_STRING, rt->if_name,
133 NULL);
134
135 SEXP_free(rt_dst);
136 return probe_item_collect(ctx, item) == 2 ? 1 : 0;
137 }
138
139 static int proc_ip4_to_string(const char *proc_ip, size_t proc_iplen, char *strbuf, size_t strbuflen)
140 {
141 uint8_t bb[4];
142 uint32_t *addr = (uint32_t *)bb;
143 struct in_addr ip4;
144
145 if (strbuf == NULL) {
146 return -1;
147 }
148 if (proc_ip == NULL || proc_iplen <= 0) {
149 return -1;
150 }
151
152 if (hexstring2bin(proc_ip, proc_iplen, bb, sizeof bb) != 0)
153 return -1;
154
155 ip4.s_addr = htobe32(*addr);
156
157 if (inet_ntop(AF_INET, &ip4, strbuf, strbuflen) == NULL)
158 return -1;
159 return 0;
160 }
161
162 static int proc_ip6_to_string(const char *proc_ip, size_t proc_iplen, char *strbuf, size_t strbuflen)
163 {
164 struct in6_addr ip6;
165
166 if (strbuf == NULL) {
167 return -1;
168 }
169 if (proc_ip == NULL || proc_iplen <= 0) {
170 return -1;
171 }
172
173 if (hexstring2bin(proc_ip, proc_iplen, (uint8_t *)&ip6, sizeof ip6) != 0)
174 return -1;
175 if (inet_ntop(AF_INET6, &ip6, strbuf, strbuflen) == NULL)
176 return -1;
177 return 0;
178 }
179
180 static int process_line_ip4(char *line, struct route_info *rt)
181 {
182 char *token[8];
183 char *save;
184 uint16_t rt_flags;
185 register int i;
186
187 if (line == NULL || rt == NULL) {
188 return -1;
189 }
190
191 save = NULL;
192 token[0] = strtok_r(line, RT_INFO_DELIMITERS, &save);
193
194 for (i = 1; i < 4; ++i) {
195 token[i] = strtok_r(NULL, RT_INFO_DELIMITERS, &save);
196 if (token[i] == NULL)
197 return -1;
198 }
199 /* Tokens
200 * 0 - interface name
201 * 1 - destination
202 * 2 - gateway
203 * 3 - flags
204 * 7 - netmask
205 */
206 #define TOK_dst token[1]
207 #define TOK_gw token[2]
208 #define TOK_flags token[3]
209 #define TOK_ifname token[0]
210
211 dD("name=%s, dst=%s, gw=%s, flags=%s", TOK_ifname, TOK_dst, TOK_gw, TOK_flags);
212
213 if (proc_ip4_to_string(TOK_dst, strlen(TOK_dst), rt->ip_dst, sizeof rt->ip_dst) != 0 ||
214 proc_ip4_to_string(TOK_gw, strlen(TOK_gw), rt->ip_gw, sizeof rt->ip_gw) != 0)
215 return -1;
216
217 strncpy(rt->if_name, TOK_ifname, IF_NAME_MAXLEN); /* interface name */
218 rt_flags = strto_uint16_hex(TOK_flags, strlen(TOK_flags), NULL);
219
220 if (errno != 0)
221 return -1;
222
223 #define RT_COND_ADD_FLAG(flag, value) if (rt_flags & (flag)) rt->rt_flags[i++] = (value)
224
225 i = 0;
226 RT_COND_ADD_FLAG(RTF_UP, "UP");
227 RT_COND_ADD_FLAG(RTF_GATEWAY, "GATEWAY");
228 RT_COND_ADD_FLAG(RTF_HOST, "HOST");
229 #if !defined(OS_APPLE) && !defined(OS_FREEBSD)
230 RT_COND_ADD_FLAG(RTF_REINSTATE, "REINSTATE");
231 #endif
232 RT_COND_ADD_FLAG(RTF_DYNAMIC, "DYNAMIC");
233 RT_COND_ADD_FLAG(RTF_MODIFIED, "MODIFIED");
234 RT_COND_ADD_FLAG(RTF_REJECT, "REJECT");
235 rt->rt_flags[i] = NULL;
236
237 #undef TOK_dst
238 #undef TOK_gw
239 #undef TOK_flags
240 #undef TOK_ifname
241 rt->ip_version = 4;
242 return 0;
243 }
244
245 static int process_line_ip6(char *line, struct route_info *rt)
246 {
247 char *token[10];
248 char *save;
249 uint32_t rt_flags;
250 register int i;
251
252 if (line == NULL || rt == NULL) {
253 return -1;
254 }
255
256 save = NULL;
257 token[0] = strtok_r(line, RT_INFO_DELIMITERS, &save);
258
259 for (i = 1; i < 10; ++i) {
260 token[i] = strtok_r(NULL, RT_INFO_DELIMITERS, &save);
261 if (token[i] == NULL)
262 return -1;
263 }
264
265 /* Tokens
266 * 0 - destination
267 * 4 - gateway
268 * 8 - flags
269 * 9 - interface name
270 */
271 #define TOK_dst token[0]
272 #define TOK_gw token[4]
273 #define TOK_flags token[8]
274 #define TOK_ifname token[9]
275
276 dD("name=%s, dst=%s, gw=%s, flags=%s", TOK_ifname, TOK_dst, TOK_gw, TOK_flags);
277
278 if (proc_ip6_to_string(TOK_dst, strlen(TOK_dst), rt->ip_dst, sizeof rt->ip_dst) != 0 ||
279 proc_ip6_to_string(TOK_gw, strlen(TOK_gw), rt->ip_gw, sizeof rt->ip_gw) != 0)
280 return -1;
281
282 strncpy(rt->if_name, TOK_ifname, IF_NAME_MAXLEN); /* interface name */
283 rt_flags = strto_uint32_hex(TOK_flags, strlen(TOK_flags), NULL);
284 if (errno != 0)
285 return -1;
286
287 i = 0;
288 RT_COND_ADD_FLAG(RTF_UP, "UP");
289 RT_COND_ADD_FLAG(RTF_GATEWAY, "GATEWAY");
290 RT_COND_ADD_FLAG(RTF_HOST, "HOST");
291 RT_COND_ADD_FLAG(RTF_DYNAMIC, "DYNAMIC");
292 RT_COND_ADD_FLAG(RTF_MODIFIED, "MODIFIED");
293 RT_COND_ADD_FLAG(RTF_REJECT, "REJECT");
294 #if !defined(OS_APPLE) && !defined(OS_FREEBSD)
295 RT_COND_ADD_FLAG(RTF_REINSTATE, "REINSTATE");
296 RT_COND_ADD_FLAG(RTF_ADDRCONF, "ADDRCONF");
297 RT_COND_ADD_FLAG(RTF_CACHE, "CACHE");
298 #endif
299 rt->rt_flags[i] = NULL;
300
301 #undef TOK_dst
302 #undef TOK_gw
303 #undef TOK_flags
304 #undef TOK_ifname
305 rt->ip_version = 6;
306 return 0;
307 }
308
309 int routingtable_probe_main(probe_ctx *ctx, void *arg)
310 {
311 SEXP_t *probe_in, *dst_ent;
312 FILE *fp;
313 char *line_buf;
314 size_t line_len;
315 struct route_info rt;
316 int probe_ret = 0;
317
318 probe_in = probe_ctx_getobject(ctx);
319 dst_ent = probe_obj_getent(probe_in, "destination", 1);
320
321 if (dst_ent == NULL)
322 return (PROBE_ENOENT);
323
324 rt.ip_dst_ent = dst_ent;
325 line_len = 0;
326 line_buf = NULL;
327 fp = NULL;
328
329 switch(probe_ent_getdatatype(dst_ent)) {
330 case OVAL_DATATYPE_IPV4ADDR:
331 fp = fopen("/proc/net/route", "r");
332 /* Skip the header line */
333 if (getline(&line_buf, &line_len, fp) != -1) {
334 while(getline(&line_buf, &line_len, fp) != -1) {
335 if (process_line_ip4(line_buf, &rt) != 0)
336 break;
337 if (collect_item(&rt, ctx) != 0)
338 break;
339 }
340 }
341
342 if (!feof(fp)) {
343 /* error */
344 dE("An error ocured while reading /proc/net/route: %s", strerror(errno));
345 }
346 break;
347 case OVAL_DATATYPE_IPV6ADDR:
348 fp = fopen("/proc/net/ipv6_route", "r");
349
350 while(getline(&line_buf, &line_len, fp) != -1) {
351 if (process_line_ip6(line_buf, &rt) != 0)
352 break;
353 if (collect_item(&rt, ctx) != 0)
354 break;
355 }
356
357 if (!feof(fp)) {
358 /* error */
359 dE("An error ocured while reading /proc/net/ipv6_route: %s", strerror(errno));
360 }
361 break;
362 default:
363 probe_ret = EINVAL;
364 }
365
366 if (fp != NULL)
367 fclose(fp);
368 if (line_buf != NULL)
369 free(line_buf);
370
371 SEXP_free(dst_ent);
372
373 return (probe_ret);
374 }