Debian Codegraph @

src/OVAL/probes/unix/interface_probe.c

raw symbols SCIP
1 /**
2 * @file interface_probe.c
3 * @brief interface probe
4 * @author "Tomas Heinrich" <[email protected]>
5 * @author "Steve Grubb" <[email protected]>
6 *
7 * 2010/06/13 [email protected]
8 * This probe is able to process an interface_object as defined in OVAL 5.4 and 5.5.
9 *
10 */
11
12 /*
13 * Copyright 2009,2010 Red Hat Inc., Durham, North Carolina.
14 * All Rights Reserved.
15 *
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 *
30 * Authors:
31 * "Tomas Heinrich" <[email protected]>
32 * "Steve Grubb" <[email protected]>
33 */
34
35 /*
36 * interface probe
37 *
38 * reference:
39 * http://oval.mitre.org/language/version5.6/ovaldefinition/documentation/unix-definitions-schema.html#interface_object
40 * http://oval.mitre.org/language/version5.6/ovalsc/documentation/unix-system-characteristics-schema.html#interface_item
41 *
42 * object:
43 * name oval-def:EntityObjectStringType 1 1
44 *
45 * item:
46 * name oval-sc:EntityItemStringType 0 1
47 * type unix-sc:EntityItemInterfaceType 0 1
48 * hardware_addr oval-sc:EntityItemStringType 0 1
49 * inet_addr oval-sc:EntityItemStringType 0 1
50 * broadcast_addr oval-sc:EntityItemStringType 0 1
51 * netmask oval-sc:EntityItemStringType 0 1
52 * flag oval-sc:EntityItemStringType 0 unbounded
53 */
54
55 #ifdef HAVE_CONFIG_H
56 #include <config.h>
57 #endif
58
59 #include <stdio.h>
60
61 #include <probe-api.h>
62 #include <probe/entcmp.h>
63 #include "interface_probe.h"
64
65 #if defined(OS_FREEBSD)
66 #include <netinet/in.h>
67
68 #define NUM_IF_FLAGS 24
69 #endif
70
71 #if defined(OS_LINUX) || defined(OS_FREEBSD)
72 #include <unistd.h>
73 #include <sys/socket.h>
74 #include <ifaddrs.h>
75 #include <netdb.h>
76 #include <sys/ioctl.h>
77 #include <string.h>
78 #include <net/if.h>
79 #include <net/if_arp.h>
80 #include <arpa/inet.h>
81
82 static void get_l2_info(const struct ifaddrs *ifa, char **mp, char **tp, int fd)
83 {
84 struct ifreq ifr;
85 unsigned char mac[6];
86 static char mac_buf[20];
87
88 *mp = mac_buf;
89 *tp = "";
90
91 memset(&ifr, 0, sizeof(struct ifreq));
92 strcpy(ifr.ifr_name, ifa->ifa_name);
93
94 #if defined(OS_LINUX)
95 if (ioctl(fd, SIOCGIFHWADDR, &ifr) >= 0) {
96 memcpy(mac, ifr.ifr_hwaddr.sa_data, sizeof(mac));
97 snprintf(mac_buf, sizeof(mac_buf),
98 "%02X:%02X:%02X:%02X:%02X:%02X",
99 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
100 switch (ifr.ifr_hwaddr.sa_family) {
101
102 case ARPHRD_ETHER:
103 *tp = "ARPHRD_ETHER";
104 break;
105 case ARPHRD_FDDI:
106 *tp = "ARPHRD_FDDI";
107 break;
108 case ARPHRD_LOOPBACK:
109 *tp = "ARPHRD_LOOPBACK";
110 break;
111 case ARPHRD_PPP:
112 *tp = "ARPHRD_PPP";
113 break;
114 case ARPHRD_PRONET:
115 *tp = "ARPHRD_PRONET";
116 break;
117 case ARPHRD_SLIP:
118 *tp = "ARPHRD_SLIP";
119 break;
120 case ARPHRD_VOID:
121 *tp = "ARPHRD_VOID";
122 break;
123 }
124 #elif defined(OS_FREEBSD)
125 if (ioctl(fd, SIOCGHWADDR, &ifr) >= 0) {
126 memcpy(mac, ifr.ifr_addr.sa_data, sizeof(mac));
127 snprintf(mac_buf, sizeof(mac_buf),
128 "%02X:%02X:%02X:%02X:%02X:%02X",
129 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
130 switch(ifr.ifr_addr.sa_family) {
131
132 case ARPHRD_ETHER:
133 *tp = "ARPHRD_ETHER";
134 break;
135 case ARPHRD_INFINIBAND:
136 *tp = "ARPHDR_INFINIBAND";
137 break;
138 }
139 #endif
140 } else
141 mac_buf[0] = 0;
142 }
143
144 #if defined(OS_LINUX)
145 static void get_flags(const struct ifaddrs *ifa, char ***fp) {
146 static char *flags_buf[17];
147 int i = 0;
148
149 *fp = flags_buf;
150
151 if (ifa != NULL) {
152 /* follow values from net/if.h */
153 if (ifa->ifa_flags & IFF_UP) {
154 flags_buf[i] = "UP";
155 i++;
156 }
157 if (ifa->ifa_flags & IFF_BROADCAST) {
158 flags_buf[i] = "BROADCAST";
159 i++;
160 }
161 if (ifa->ifa_flags & IFF_DEBUG) {
162 flags_buf[i] = "DEBUG";
163 i++;
164 }
165 if (ifa->ifa_flags & IFF_LOOPBACK) {
166 flags_buf[i] = "LOOPBACK";
167 i++;
168 }
169 if (ifa->ifa_flags & IFF_POINTOPOINT) {
170 flags_buf[i] = "POINTOPOINT";
171 i++;
172 }
173 if (ifa->ifa_flags & IFF_NOTRAILERS) {
174 flags_buf[i] = "NOTRAILERS";
175 i++;
176 }
177 if (ifa->ifa_flags & IFF_RUNNING) {
178 flags_buf[i] = "RUNNING";
179 i++;
180 }
181 if (ifa->ifa_flags & IFF_NOARP) {
182 flags_buf[i] = "NOAPP";
183 i++;
184 }
185 if (ifa->ifa_flags & IFF_PROMISC) {
186 flags_buf[i] = "PROMISC";
187 i++;
188 }
189 if (ifa->ifa_flags & IFF_ALLMULTI) {
190 flags_buf[i] = "ALLMULTI";
191 i++;
192 }
193 if (ifa->ifa_flags & IFF_MASTER) {
194 flags_buf[i] = "MASTER";
195 i++;
196 }
197 if (ifa->ifa_flags & IFF_SLAVE) {
198 flags_buf[i] = "SLAVE";
199 i++;
200 }
201 if (ifa->ifa_flags & IFF_MULTICAST) {
202 flags_buf[i] = "MULTICAST";
203 i++;
204 }
205 if (ifa->ifa_flags & IFF_PORTSEL) {
206 flags_buf[i] = "PORTSEL";
207 i++;
208 }
209 if (ifa->ifa_flags & IFF_AUTOMEDIA) {
210 flags_buf[i] = "AUTOMEDIA";
211 i++;
212 }
213 if (ifa->ifa_flags & IFF_DYNAMIC) {
214 flags_buf[i] = "DYNAMIC";
215 i++;
216 }
217 }
218 flags_buf[i] = NULL;
219
220 }
221 #elif defined(OS_FREEBSD)
222 static void get_flags(const struct ifaddrs *ifa, char ***fp) {
223 int i = 0;
224 static char *flags_buf[NUM_IF_FLAGS + 1];
225 *fp = flags_buf;
226
227 if (ifa != NULL) {
228 /* follow values from net/if.h */
229 if (ifa->ifa_flags & IFF_UP)
230 flags_buf[i++] = "UP";
231
232 if (ifa->ifa_flags & IFF_BROADCAST)
233 flags_buf[i++] = "BROADCAST";
234
235 if (ifa->ifa_flags & IFF_DEBUG)
236 flags_buf[i++] = "DEBUG";
237
238 if (ifa->ifa_flags & IFF_LOOPBACK)
239 flags_buf[i++] = "LOOPBACK";
240
241 if (ifa->ifa_flags & IFF_POINTOPOINT)
242 flags_buf[i++] = "POINTOPOINT";
243
244 /* Defined on FreeBSD 13+ */
245 #ifdef IFF_KNOWSEPOCH
246 if (ifa->ifa_flags & IFF_KNOWSEPOCH)
247 flags_buf[i++] = "KNOWSEPOCH";
248 #endif
249
250 if (ifa->ifa_flags & IFF_DRV_RUNNING)
251 flags_buf[i++] = "DRV_RUNNING";
252
253 if (ifa->ifa_flags & IFF_NOARP)
254 flags_buf[i++] = "NOARP";
255
256 if (ifa->ifa_flags & IFF_PROMISC)
257 flags_buf[i++] = "PROMISC";
258
259 if (ifa->ifa_flags & IFF_ALLMULTI)
260 flags_buf[i++] = "ALLMULTI";
261
262 if (ifa->ifa_flags & IFF_DRV_OACTIVE)
263 flags_buf[i++] = "DRV_OACTIVE";
264
265 if (ifa->ifa_flags & IFF_SIMPLEX)
266 flags_buf[i++] = "SIMPLEX";
267
268 if (ifa->ifa_flags & IFF_LINK0)
269 flags_buf[i++] = "LINK0";
270
271 if (ifa->ifa_flags & IFF_LINK1)
272 flags_buf[i++] = "LINK1";
273
274 if (ifa->ifa_flags & IFF_LINK2)
275 flags_buf[i++] = "LINK2";
276
277 if (ifa->ifa_flags & IFF_ALTPHYS)
278 flags_buf[i++] = "ALTPHYS";
279
280 if (ifa->ifa_flags & IFF_MULTICAST)
281 flags_buf[i++] = "MULTICAST";
282
283 if (ifa->ifa_flags & IFF_CANTCONFIG)
284 flags_buf[i++] = "CANTCONFIG";
285
286 if (ifa->ifa_flags & IFF_PPROMISC)
287 flags_buf[i++] = "PPROMISC";
288
289 if (ifa->ifa_flags & IFF_MONITOR)
290 flags_buf[i++] = "MONITOR";
291
292 if (ifa->ifa_flags & IFF_STATICARP)
293 flags_buf[i++] = "STATICARP";
294
295 if (ifa->ifa_flags & IFF_DYING)
296 flags_buf[i++] = "DYING";
297
298 if (ifa->ifa_flags & IFF_RENAMING)
299 flags_buf[i++] = "RENAMING";
300
301 if (ifa->ifa_flags & IFF_NOGROUP)
302 flags_buf[i++] = "NOGROUP";
303 }
304
305 flags_buf[i] = NULL;
306 }
307 #endif /* end of #ifdef for get_flags */
308
309 static int get_ifs(SEXP_t *name_ent, probe_ctx *ctx, oval_schema_version_t over)
310 {
311 struct ifaddrs *ifaddr, *ifa;
312 int family, rc=1;
313 char host[NI_MAXHOST], broad[NI_MAXHOST], mask[NI_MAXHOST], *mac, *type, **flags;
314 oval_datatype_t address_type;
315 SEXP_t *item;
316 bool include_type, use_ipstring;
317
318 if (getifaddrs(&ifaddr) == -1) {
319 SEXP_t *msg;
320
321 msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_ERROR, "getifaddrs() failed.");
322 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
323 SEXP_free(msg);
324 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
325
326 return rc;
327 }
328
329 int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
330 if (fd < 0) {
331 SEXP_t *msg;
332
333 msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_ERROR, "socket() failed.");
334 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
335 SEXP_free(msg);
336 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
337
338 goto leave1;
339 }
340
341 include_type = oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.6)) >= 0;
342 use_ipstring = oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.8)) >= 0;
343
344 /* Walk through linked list, maintaining head pointer so we
345 can free list later */
346 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
347 SEXP_t *sname;
348
349 if (!ifa->ifa_addr)
350 continue;
351
352 family = ifa->ifa_addr->sa_family;
353 if (family != AF_INET && family != AF_INET6)
354 continue;
355
356 sname = SEXP_string_newf("%s", ifa->ifa_name);
357 if (probe_entobj_cmp(name_ent, sname) != OVAL_RESULT_TRUE) {
358 SEXP_free(sname);
359 continue;
360 }
361 SEXP_free(sname);
362
363 get_l2_info(ifa, &mac, &type, fd);
364 get_flags(ifa, &flags);
365
366 /* The inet_addr entity is the IP address of the specific interface.
367 * Note that the IP address can be IPv4 or IPv6. If the IP address is an IPv6 address,
368 * this entity should be expressed as an IPv6 address prefix using CIDR notation and
369 * the netmask entity should not be collected.
370 */
371 if (family == AF_INET) {
372 rc = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in),
373 host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
374 if (rc) {
375 SEXP_t *msg;
376
377 msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_ERROR, "getnameinfo() failed.");
378 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
379 SEXP_free(msg);
380 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
381 rc = 1;
382
383 goto leave2;
384 }
385 address_type = use_ipstring ? OVAL_DATATYPE_IPV4ADDR : OVAL_DATATYPE_STRING;
386
387 rc = getnameinfo(ifa->ifa_netmask, sizeof(struct sockaddr_in),
388 mask, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
389 if (rc) {
390 SEXP_t *msg;
391
392 msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_ERROR, "getnameinfo() failed.");
393 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
394 SEXP_free(msg);
395 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
396 rc = 1;
397
398 goto leave2;
399 }
400
401 }
402 else {
403 struct sockaddr_in6 *sin6p;
404 char host_tmp[NI_MAXHOST];
405 int byte, prefix = 0;
406 u_int32_t bit, tmp;
407 int host_len;
408
409 sin6p = (struct sockaddr_in6 *) ifa->ifa_addr;
410
411 if (! inet_ntop(family, (const void *)&sin6p->sin6_addr, host, NI_MAXHOST)) {
412 SEXP_t *msg;
413
414 msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_ERROR, "inet_ntop() failed.");
415 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
416 SEXP_free(msg);
417 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
418 rc = 1;
419
420 goto leave2;
421 }
422
423 address_type = use_ipstring ? OVAL_DATATYPE_IPV6ADDR : OVAL_DATATYPE_STRING;
424 *mask = '\0';
425
426 /* count prefix */
427 sin6p = (struct sockaddr_in6 *) ifa->ifa_netmask;
428
429 #if defined(OS_LINUX)
430 for (byte = 0; byte < 4 && sin6p->sin6_addr.s6_addr32[byte] == 0xffffffff; byte++) {
431 prefix += 32;
432 }
433 if (byte < 4) {
434 tmp = ntohl(sin6p->sin6_addr.s6_addr32[byte]);
435 for (bit = 31; tmp & (1 << bit); bit--)
436 prefix++;
437 }
438 #elif defined(OS_FREEBSD)
439 for (byte = 0; byte < 4 && sin6p->sin6_addr.__u6_addr.__u6_addr32[byte] == 0xffffffff; byte++) {
440 prefix += 32;
441 }
442 if (byte < 4) {
443 tmp = ntohl(sin6p->sin6_addr.__u6_addr.__u6_addr32[byte]);
444 for (bit = 31; tmp & (1 << bit); bit--)
445 prefix++;
446 }
447 #endif
448 host_len = strlen(host);
449 if (host_len + 1 + 11 >= NI_MAXHOST) {
450 /*
451 * If we might overflow the size of host_tmp, compute an
452 * exact value for the size of prefix and bound the size of
453 * the printed host accordingly.
454 *
455 * This prevents us from overwriting memory at the expense of
456 * not having a correct host.
457 */
458 host_len = snprintf(host_tmp, NI_MAXHOST, "%d", prefix);
459 host_len = NI_MAXHOST - host_len - 1;
460 }
461
462 snprintf(host_tmp, NI_MAXHOST, "%.*s/%d", host_len, host, prefix);
463 strncpy(host, host_tmp, NI_MAXHOST);
464 }
465
466 if (family == AF_INET && ifa->ifa_flags & IFF_BROADCAST) {
467 rc = getnameinfo(ifa->ifa_broadaddr, (family == AF_INET) ?
468 sizeof(struct sockaddr_in) :
469 sizeof(struct sockaddr_in6), broad, NI_MAXHOST,
470 NULL, 0, NI_NUMERICHOST);
471 if (rc) {
472 SEXP_t *msg;
473
474 msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_ERROR, "getnameinfo() failed.");
475 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
476 SEXP_free(msg);
477 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
478 rc = 1;
479
480 goto leave2;
481 }
482 } else
483 *broad = '\0';
484
485 item = probe_item_create(OVAL_UNIX_INTERFACE, NULL,
486 "name", OVAL_DATATYPE_STRING, ifa->ifa_name,
487 "type", OVAL_DATATYPE_STRING, include_type ? type : NULL,
488 "hardware_addr", OVAL_DATATYPE_STRING, mac,
489 "inet_addr", address_type, host,
490 "broadcast_addr", address_type, broad,
491 "netmask", address_type, mask,
492 "flag", OVAL_DATATYPE_STRING_M, flags,
493 NULL);
494
495 probe_item_collect(ctx, item);
496 }
497
498 rc = 0;
499 leave2:
500 close(fd);
501 leave1:
502 freeifaddrs(ifaddr);
503 return rc;
504 }
505 #else
506 static int get_ifs(SEXP_t *name_ent, probe_ctx *ctx, oval_schema_version_t over)
507 {
508 /* todo */
509 SEXP_t *item;
510
511 item = probe_item_create(OVAL_UNIX_INTERFACE, NULL,
512 "name", OVAL_DATATYPE_STRING, "dummy0"
513 "hardware_addr", OVAL_DATATYPE_STRING, "aa:bb:cc:dd:ee:ff",
514 "inet_addr", OVAL_DATATYPE_STRING, "1.2.3.4",
515 "broadcast_addr", OVAL_DATATYPE_STRING, "1.2.3.254",
516 "netmask", OVAL_DATATYPE_STRING, "255.255.255.0",
517 NULL);
518
519 probe_item_collect(ctx, item);
520
521 return (0);
522 }
523 #endif
524
525 int interface_probe_main(probe_ctx *ctx, void *arg)
526 {
527 SEXP_t *name_ent, *probe_in;
528 oval_schema_version_t over;
529
530 (void)arg;
531
532 probe_in = probe_ctx_getobject(ctx);
533 name_ent = probe_obj_getent(probe_in, "name", 1);
534 over = probe_obj_get_platform_schema_version(probe_in);
535
536 if (name_ent == NULL) {
537 return PROBE_ENOELM;
538 }
539
540 get_ifs(name_ent, ctx, over);
541 SEXP_free(name_ent);
542
543 return 0;
544 }