Don't initialize static numeric variables to 0
[collectd.git] / src / netlink.c
1 /**
2  * collectd - src/netlink.c
3  * Copyright (C) 2007-2010  Florian octo Forster
4  * Copyright (C) 2008-2012  Sebastian Harl
5  * Copyright (C) 2013       Andreas Henriksson
6  * Copyright (C) 2013       Marc Fournier
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; only version 2 of the License is applicable.
11  *
12  * This program 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 along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  *
21  * Authors:
22  *   Florian octo Forster <octo at collectd.org>
23  *   Sebastian Harl <sh at tokkee.org>
24  *   Andreas Henriksson <andreas at fatal.se>
25  *   Marc Fournier <marc.fournier at camptocamp.com>
26  **/
27
28 #include "collectd.h"
29
30 #include "common.h"
31 #include "plugin.h"
32
33 #include <asm/types.h>
34
35 #include <linux/netlink.h>
36 #include <linux/rtnetlink.h>
37 #if HAVE_LINUX_GEN_STATS_H
38 #include <linux/gen_stats.h>
39 #endif
40 #if HAVE_LINUX_PKT_SCHED_H
41 #include <linux/pkt_sched.h>
42 #endif
43
44 #include <libmnl/libmnl.h>
45
46 struct ir_link_stats_storage_s {
47
48   uint64_t rx_packets;
49   uint64_t tx_packets;
50   uint64_t rx_bytes;
51   uint64_t tx_bytes;
52   uint64_t rx_errors;
53   uint64_t tx_errors;
54
55   uint64_t rx_dropped;
56   uint64_t tx_dropped;
57   uint64_t multicast;
58   uint64_t collisions;
59   uint64_t rx_nohandler;
60
61   uint64_t rx_length_errors;
62   uint64_t rx_over_errors;
63   uint64_t rx_crc_errors;
64   uint64_t rx_frame_errors;
65   uint64_t rx_fifo_errors;
66   uint64_t rx_missed_errors;
67
68   uint64_t tx_aborted_errors;
69   uint64_t tx_carrier_errors;
70   uint64_t tx_fifo_errors;
71   uint64_t tx_heartbeat_errors;
72   uint64_t tx_window_errors;
73 };
74
75 union ir_link_stats_u {
76   struct rtnl_link_stats *stats32;
77 #ifdef HAVE_RTNL_LINK_STATS64
78   struct rtnl_link_stats64 *stats64;
79 #endif
80 };
81
82 typedef struct ir_ignorelist_s {
83   char *device;
84   char *type;
85   char *inst;
86   struct ir_ignorelist_s *next;
87 } ir_ignorelist_t;
88
89 struct qos_stats {
90   struct gnet_stats_basic *bs;
91   struct gnet_stats_queue *qs;
92 };
93
94 static int ir_ignorelist_invert = 1;
95 static ir_ignorelist_t *ir_ignorelist_head;
96
97 static struct mnl_socket *nl;
98
99 static char **iflist;
100 static size_t iflist_len;
101
102 static const char *config_keys[] = {"Interface", "VerboseInterface",
103                                     "QDisc",     "Class",
104                                     "Filter",    "IgnoreSelected"};
105 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
106
107 static int add_ignorelist(const char *dev, const char *type, const char *inst) {
108   ir_ignorelist_t *entry;
109
110   entry = calloc(1, sizeof(*entry));
111   if (entry == NULL)
112     return -1;
113
114   if (strcasecmp(dev, "All") != 0) {
115     entry->device = strdup(dev);
116     if (entry->device == NULL) {
117       sfree(entry);
118       return -1;
119     }
120   }
121
122   entry->type = strdup(type);
123   if (entry->type == NULL) {
124     sfree(entry->device);
125     sfree(entry);
126     return -1;
127   }
128
129   if (inst != NULL) {
130     entry->inst = strdup(inst);
131     if (entry->inst == NULL) {
132       sfree(entry->type);
133       sfree(entry->device);
134       sfree(entry);
135       return -1;
136     }
137   }
138
139   entry->next = ir_ignorelist_head;
140   ir_ignorelist_head = entry;
141
142   return 0;
143 } /* int add_ignorelist */
144
145 /*
146  * Checks wether a data set should be ignored. Returns `true' is the value
147  * should be ignored, `false' otherwise.
148  */
149 static int check_ignorelist(const char *dev, const char *type,
150                             const char *type_instance) {
151   assert((dev != NULL) && (type != NULL));
152
153   if (ir_ignorelist_head == NULL)
154     return ir_ignorelist_invert ? 0 : 1;
155
156   for (ir_ignorelist_t *i = ir_ignorelist_head; i != NULL; i = i->next) {
157     /* i->device == NULL  =>  match all devices */
158     if ((i->device != NULL) && (strcasecmp(i->device, dev) != 0))
159       continue;
160
161     if (strcasecmp(i->type, type) != 0)
162       continue;
163
164     if ((i->inst != NULL) && (type_instance != NULL) &&
165         (strcasecmp(i->inst, type_instance) != 0))
166       continue;
167
168     DEBUG("netlink plugin: check_ignorelist: "
169           "(dev = %s; type = %s; inst = %s) matched "
170           "(dev = %s; type = %s; inst = %s)",
171           dev, type, type_instance == NULL ? "(nil)" : type_instance,
172           i->device == NULL ? "(nil)" : i->device, i->type,
173           i->inst == NULL ? "(nil)" : i->inst);
174
175     return ir_ignorelist_invert ? 0 : 1;
176   } /* for i */
177
178   return ir_ignorelist_invert;
179 } /* int check_ignorelist */
180
181 static void submit_one(const char *dev, const char *type,
182                        const char *type_instance, derive_t value) {
183   value_list_t vl = VALUE_LIST_INIT;
184
185   vl.values = &(value_t){.derive = value};
186   vl.values_len = 1;
187   sstrncpy(vl.plugin, "netlink", sizeof(vl.plugin));
188   sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
189   sstrncpy(vl.type, type, sizeof(vl.type));
190
191   if (type_instance != NULL)
192     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
193
194   plugin_dispatch_values(&vl);
195 } /* void submit_one */
196
197 static void submit_two(const char *dev, const char *type,
198                        const char *type_instance, derive_t rx, derive_t tx) {
199   value_list_t vl = VALUE_LIST_INIT;
200   value_t values[] = {
201       {.derive = rx}, {.derive = tx},
202   };
203
204   vl.values = values;
205   vl.values_len = STATIC_ARRAY_SIZE(values);
206   sstrncpy(vl.plugin, "netlink", sizeof(vl.plugin));
207   sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
208   sstrncpy(vl.type, type, sizeof(vl.type));
209
210   if (type_instance != NULL)
211     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
212
213   plugin_dispatch_values(&vl);
214 } /* void submit_two */
215
216 static int update_iflist(struct ifinfomsg *msg, const char *dev) {
217   /* Update the `iflist'. It's used to know which interfaces exist and query
218    * them later for qdiscs and classes. */
219   if ((msg->ifi_index >= 0) && ((size_t)msg->ifi_index >= iflist_len)) {
220     char **temp;
221
222     temp = realloc(iflist, (msg->ifi_index + 1) * sizeof(char *));
223     if (temp == NULL) {
224       ERROR("netlink plugin: update_iflist: realloc failed.");
225       return -1;
226     }
227
228     memset(temp + iflist_len, '\0',
229            (msg->ifi_index + 1 - iflist_len) * sizeof(char *));
230     iflist = temp;
231     iflist_len = msg->ifi_index + 1;
232   }
233   if ((iflist[msg->ifi_index] == NULL) ||
234       (strcmp(iflist[msg->ifi_index], dev) != 0)) {
235     sfree(iflist[msg->ifi_index]);
236     iflist[msg->ifi_index] = strdup(dev);
237   }
238
239   return 0;
240 } /* int update_iflist */
241
242 static void check_ignorelist_and_submit(const char *dev,
243                                         struct ir_link_stats_storage_s *stats) {
244
245   if (check_ignorelist(dev, "interface", NULL) == 0) {
246     submit_two(dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
247     submit_two(dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
248     submit_two(dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
249   } else {
250     DEBUG("netlink plugin: Ignoring %s/interface.", dev);
251   }
252
253   if (check_ignorelist(dev, "if_detail", NULL) == 0) {
254     submit_two(dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
255     submit_one(dev, "if_multicast", NULL, stats->multicast);
256     submit_one(dev, "if_collisions", NULL, stats->collisions);
257 #if defined(HAVE_STRUCT_RTNL_LINK_STATS_RX_NOHANDLER) ||                       \
258     defined(HAVE_STRUCT_RTNL_LINK_STATS64_RX_NOHANDLER)
259     submit_one(dev, "if_rx_nohandler", NULL, stats->rx_nohandler);
260 #endif
261
262     submit_one(dev, "if_rx_errors", "length", stats->rx_length_errors);
263     submit_one(dev, "if_rx_errors", "over", stats->rx_over_errors);
264     submit_one(dev, "if_rx_errors", "crc", stats->rx_crc_errors);
265     submit_one(dev, "if_rx_errors", "frame", stats->rx_frame_errors);
266     submit_one(dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
267     submit_one(dev, "if_rx_errors", "missed", stats->rx_missed_errors);
268
269     submit_one(dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
270     submit_one(dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
271     submit_one(dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
272     submit_one(dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
273     submit_one(dev, "if_tx_errors", "window", stats->tx_window_errors);
274   } else {
275     DEBUG("netlink plugin: Ignoring %s/if_detail.", dev);
276   }
277
278 } /* void check_ignorelist_and_submit */
279
280 #define COPY_RTNL_LINK_VALUE(dst_stats, src_stats, value_name)                 \
281   (dst_stats)->value_name = (src_stats)->value_name
282
283 #define COPY_RTNL_LINK_STATS(dst_stats, src_stats)                             \
284   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_packets);                      \
285   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_packets);                      \
286   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_bytes);                        \
287   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_bytes);                        \
288   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_errors);                       \
289   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_errors);                       \
290   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_dropped);                      \
291   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_dropped);                      \
292   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, multicast);                       \
293   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, collisions);                      \
294   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_length_errors);                \
295   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_over_errors);                  \
296   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_crc_errors);                   \
297   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_frame_errors);                 \
298   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_fifo_errors);                  \
299   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_missed_errors);                \
300   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_aborted_errors);               \
301   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_carrier_errors);               \
302   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_fifo_errors);                  \
303   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_heartbeat_errors);             \
304   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_window_errors)
305
306 #ifdef HAVE_RTNL_LINK_STATS64
307 static void check_ignorelist_and_submit64(const char *dev,
308                                           struct rtnl_link_stats64 *stats) {
309   struct ir_link_stats_storage_s s;
310
311   COPY_RTNL_LINK_STATS(&s, stats);
312 #ifdef HAVE_STRUCT_RTNL_LINK_STATS64_RX_NOHANDLER
313   COPY_RTNL_LINK_VALUE(&s, stats, rx_nohandler);
314 #endif
315
316   check_ignorelist_and_submit(dev, &s);
317 }
318 #endif
319
320 static void check_ignorelist_and_submit32(const char *dev,
321                                           struct rtnl_link_stats *stats) {
322   struct ir_link_stats_storage_s s;
323
324   COPY_RTNL_LINK_STATS(&s, stats);
325 #ifdef HAVE_STRUCT_RTNL_LINK_STATS_RX_NOHANDLER
326   COPY_RTNL_LINK_VALUE(&s, stats, rx_nohandler);
327 #endif
328
329   check_ignorelist_and_submit(dev, &s);
330 }
331
332 static int link_filter_cb(const struct nlmsghdr *nlh,
333                           void *args __attribute__((unused))) {
334   struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
335   struct nlattr *attr;
336   const char *dev = NULL;
337   union ir_link_stats_u stats;
338
339   if (nlh->nlmsg_type != RTM_NEWLINK) {
340     ERROR("netlink plugin: link_filter_cb: Don't know how to handle type %i.",
341           nlh->nlmsg_type);
342     return MNL_CB_ERROR;
343   }
344
345   /* Scan attribute list for device name. */
346   mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
347     if (mnl_attr_get_type(attr) != IFLA_IFNAME)
348       continue;
349
350     if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
351       ERROR("netlink plugin: link_filter_cb: IFLA_IFNAME mnl_attr_validate "
352             "failed.");
353       return MNL_CB_ERROR;
354     }
355
356     dev = mnl_attr_get_str(attr);
357     if (update_iflist(ifm, dev) < 0)
358       return MNL_CB_ERROR;
359     break;
360   }
361
362   if (dev == NULL) {
363     ERROR("netlink plugin: link_filter_cb: dev == NULL");
364     return MNL_CB_ERROR;
365   }
366 #ifdef HAVE_RTNL_LINK_STATS64
367   mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
368     if (mnl_attr_get_type(attr) != IFLA_STATS64)
369       continue;
370
371     uint16_t attr_len = mnl_attr_get_payload_len(attr);
372     if (attr_len < sizeof(*stats.stats64)) {
373       ERROR("netlink plugin: link_filter_cb: IFLA_STATS64 attribute has "
374             "insufficient data.");
375       return MNL_CB_ERROR;
376     }
377     stats.stats64 = mnl_attr_get_payload(attr);
378
379     check_ignorelist_and_submit64(dev, stats.stats64);
380
381     return MNL_CB_OK;
382   }
383 #endif
384   mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
385     if (mnl_attr_get_type(attr) != IFLA_STATS)
386       continue;
387
388     uint16_t attr_len = mnl_attr_get_payload_len(attr);
389     if (attr_len < sizeof(*stats.stats32)) {
390       ERROR("netlink plugin: link_filter_cb: IFLA_STATS attribute has "
391             "insufficient data.");
392       return MNL_CB_ERROR;
393     }
394     stats.stats32 = mnl_attr_get_payload(attr);
395
396     check_ignorelist_and_submit32(dev, stats.stats32);
397
398     return MNL_CB_OK;
399   }
400
401   DEBUG("netlink plugin: link_filter: No statistics for interface %s.", dev);
402   return MNL_CB_OK;
403
404 } /* int link_filter_cb */
405
406 #if HAVE_TCA_STATS2
407 static int qos_attr_cb(const struct nlattr *attr, void *data) {
408   struct qos_stats *q_stats = (struct qos_stats *)data;
409
410   /* skip unsupported attribute in user-space */
411   if (mnl_attr_type_valid(attr, TCA_STATS_MAX) < 0)
412     return MNL_CB_OK;
413
414   if (mnl_attr_get_type(attr) == TCA_STATS_BASIC) {
415     if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*q_stats->bs)) < 0) {
416       ERROR("netlink plugin: qos_attr_cb: TCA_STATS_BASIC mnl_attr_validate2 "
417             "failed: %s",
418             STRERRNO);
419       return MNL_CB_ERROR;
420     }
421     q_stats->bs = mnl_attr_get_payload(attr);
422     return MNL_CB_OK;
423   }
424
425   if (mnl_attr_get_type(attr) == TCA_STATS_QUEUE) {
426     if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*q_stats->qs)) < 0) {
427       ERROR("netlink plugin: qos_attr_cb: TCA_STATS_QUEUE mnl_attr_validate2 "
428             "failed.");
429       return MNL_CB_ERROR;
430     }
431     q_stats->qs = mnl_attr_get_payload(attr);
432     return MNL_CB_OK;
433   }
434
435   return MNL_CB_OK;
436 } /* qos_attr_cb */
437 #endif
438
439 static int qos_filter_cb(const struct nlmsghdr *nlh, void *args) {
440   struct tcmsg *tm = mnl_nlmsg_get_payload(nlh);
441   struct nlattr *attr;
442
443   int wanted_ifindex = *((int *)args);
444
445   const char *dev;
446   const char *kind = NULL;
447
448   /* char *type_instance; */
449   const char *tc_type;
450   char tc_inst[DATA_MAX_NAME_LEN];
451
452   bool stats_submitted = false;
453
454   if (nlh->nlmsg_type == RTM_NEWQDISC)
455     tc_type = "qdisc";
456   else if (nlh->nlmsg_type == RTM_NEWTCLASS)
457     tc_type = "class";
458   else if (nlh->nlmsg_type == RTM_NEWTFILTER)
459     tc_type = "filter";
460   else {
461     ERROR("netlink plugin: qos_filter_cb: Don't know how to handle type %i.",
462           nlh->nlmsg_type);
463     return MNL_CB_ERROR;
464   }
465
466   if (tm->tcm_ifindex != wanted_ifindex) {
467     DEBUG("netlink plugin: qos_filter_cb: Got %s for interface #%i, "
468           "but expected #%i.",
469           tc_type, tm->tcm_ifindex, wanted_ifindex);
470     return MNL_CB_OK;
471   }
472
473   if ((tm->tcm_ifindex >= 0) && ((size_t)tm->tcm_ifindex >= iflist_len)) {
474     ERROR("netlink plugin: qos_filter_cb: tm->tcm_ifindex = %i "
475           ">= iflist_len = %" PRIsz,
476           tm->tcm_ifindex, iflist_len);
477     return MNL_CB_ERROR;
478   }
479
480   dev = iflist[tm->tcm_ifindex];
481   if (dev == NULL) {
482     ERROR("netlink plugin: qos_filter_cb: iflist[%i] == NULL", tm->tcm_ifindex);
483     return MNL_CB_ERROR;
484   }
485
486   mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
487     if (mnl_attr_get_type(attr) != TCA_KIND)
488       continue;
489
490     if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
491       ERROR(
492           "netlink plugin: qos_filter_cb: TCA_KIND mnl_attr_validate failed.");
493       return MNL_CB_ERROR;
494     }
495
496     kind = mnl_attr_get_str(attr);
497     break;
498   }
499
500   if (kind == NULL) {
501     ERROR("netlink plugin: qos_filter_cb: kind == NULL");
502     return -1;
503   }
504
505   { /* The ID */
506     uint32_t numberic_id;
507
508     numberic_id = tm->tcm_handle;
509     if (strcmp(tc_type, "filter") == 0)
510       numberic_id = tm->tcm_parent;
511
512     snprintf(tc_inst, sizeof(tc_inst), "%s-%x:%x", kind, numberic_id >> 16,
513              numberic_id & 0x0000FFFF);
514   }
515
516   DEBUG("netlink plugin: qos_filter_cb: got %s for %s (%i).", tc_type, dev,
517         tm->tcm_ifindex);
518
519   if (check_ignorelist(dev, tc_type, tc_inst))
520     return MNL_CB_OK;
521
522 #if HAVE_TCA_STATS2
523   mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
524     struct qos_stats q_stats;
525
526     memset(&q_stats, 0x0, sizeof(q_stats));
527
528     if (mnl_attr_get_type(attr) != TCA_STATS2)
529       continue;
530
531     if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) {
532       ERROR("netlink plugin: qos_filter_cb: TCA_STATS2 mnl_attr_validate "
533             "failed.");
534       return MNL_CB_ERROR;
535     }
536
537     mnl_attr_parse_nested(attr, qos_attr_cb, &q_stats);
538
539     if (q_stats.bs != NULL || q_stats.qs != NULL) {
540       char type_instance[DATA_MAX_NAME_LEN];
541
542       stats_submitted = true;
543
544       snprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type, tc_inst);
545
546       if (q_stats.bs != NULL) {
547         submit_one(dev, "ipt_bytes", type_instance, q_stats.bs->bytes);
548         submit_one(dev, "ipt_packets", type_instance, q_stats.bs->packets);
549       }
550       if (q_stats.qs != NULL) {
551         submit_one(dev, "if_tx_dropped", type_instance, q_stats.qs->drops);
552       }
553     }
554
555     break;
556   }
557 #endif /* TCA_STATS2 */
558
559 #if HAVE_TCA_STATS
560   mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
561     struct tc_stats *ts = NULL;
562
563     if (mnl_attr_get_type(attr) != TCA_STATS)
564       continue;
565
566     if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*ts)) < 0) {
567       ERROR("netlink plugin: qos_filter_cb: TCA_STATS mnl_attr_validate2 "
568             "failed: %s",
569             STRERRNO);
570       return MNL_CB_ERROR;
571     }
572     ts = mnl_attr_get_payload(attr);
573
574     if (!stats_submitted && ts != NULL) {
575       char type_instance[DATA_MAX_NAME_LEN];
576
577       snprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type, tc_inst);
578
579       submit_one(dev, "ipt_bytes", type_instance, ts->bytes);
580       submit_one(dev, "ipt_packets", type_instance, ts->packets);
581     }
582
583     break;
584   }
585
586 #endif /* TCA_STATS */
587
588 #if !(HAVE_TCA_STATS && HAVE_TCA_STATS2)
589   DEBUG("netlink plugin: qos_filter_cb: Have neither TCA_STATS2 nor "
590         "TCA_STATS.");
591 #endif
592
593   return MNL_CB_OK;
594 } /* int qos_filter_cb */
595
596 static int ir_config(const char *key, const char *value) {
597   char *new_val;
598   char *fields[8];
599   int fields_num;
600   int status = 1;
601
602   new_val = strdup(value);
603   if (new_val == NULL)
604     return -1;
605
606   fields_num = strsplit(new_val, fields, STATIC_ARRAY_SIZE(fields));
607   if ((fields_num < 1) || (fields_num > 8)) {
608     sfree(new_val);
609     return -1;
610   }
611
612   if ((strcasecmp(key, "Interface") == 0) ||
613       (strcasecmp(key, "VerboseInterface") == 0)) {
614     if (fields_num != 1) {
615       ERROR("netlink plugin: Invalid number of fields for option "
616             "`%s'. Got %i, expected 1.",
617             key, fields_num);
618       status = -1;
619     } else {
620       add_ignorelist(fields[0], "interface", NULL);
621       if (strcasecmp(key, "VerboseInterface") == 0)
622         add_ignorelist(fields[0], "if_detail", NULL);
623       status = 0;
624     }
625   } else if ((strcasecmp(key, "QDisc") == 0) ||
626              (strcasecmp(key, "Class") == 0) ||
627              (strcasecmp(key, "Filter") == 0)) {
628     if ((fields_num < 1) || (fields_num > 2)) {
629       ERROR("netlink plugin: Invalid number of fields for option "
630             "`%s'. Got %i, expected 1 or 2.",
631             key, fields_num);
632       return -1;
633     } else {
634       add_ignorelist(fields[0], key, (fields_num == 2) ? fields[1] : NULL);
635       status = 0;
636     }
637   } else if (strcasecmp(key, "IgnoreSelected") == 0) {
638     if (fields_num != 1) {
639       ERROR("netlink plugin: Invalid number of fields for option "
640             "`IgnoreSelected'. Got %i, expected 1.",
641             fields_num);
642       status = -1;
643     } else {
644       if (IS_TRUE(fields[0]))
645         ir_ignorelist_invert = 0;
646       else
647         ir_ignorelist_invert = 1;
648       status = 0;
649     }
650   }
651
652   sfree(new_val);
653
654   return status;
655 } /* int ir_config */
656
657 static int ir_init(void) {
658   nl = mnl_socket_open(NETLINK_ROUTE);
659   if (nl == NULL) {
660     ERROR("netlink plugin: ir_init: mnl_socket_open failed.");
661     return -1;
662   }
663
664   if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
665     ERROR("netlink plugin: ir_init: mnl_socket_bind failed.");
666     return -1;
667   }
668
669   return 0;
670 } /* int ir_init */
671
672 static int ir_read(void) {
673   char buf[MNL_SOCKET_BUFFER_SIZE];
674   struct nlmsghdr *nlh;
675   struct rtgenmsg *rt;
676   int ret;
677   unsigned int seq, portid;
678
679   static const int type_id[] = {RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER};
680   static const char *type_name[] = {"qdisc", "class", "filter"};
681
682   portid = mnl_socket_get_portid(nl);
683
684   nlh = mnl_nlmsg_put_header(buf);
685   nlh->nlmsg_type = RTM_GETLINK;
686   nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
687   nlh->nlmsg_seq = seq = time(NULL);
688   rt = mnl_nlmsg_put_extra_header(nlh, sizeof(*rt));
689   rt->rtgen_family = AF_PACKET;
690
691   if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
692     ERROR("netlink plugin: ir_read: rtnl_wilddump_request failed.");
693     return -1;
694   }
695
696   ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
697   while (ret > 0) {
698     ret = mnl_cb_run(buf, ret, seq, portid, link_filter_cb, NULL);
699     if (ret <= MNL_CB_STOP)
700       break;
701     ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
702   }
703   if (ret < 0) {
704     ERROR("netlink plugin: ir_read: mnl_socket_recvfrom failed: %s", STRERRNO);
705     return (-1);
706   }
707
708   /* `link_filter_cb' will update `iflist' which is used here to iterate
709    * over all interfaces. */
710   for (size_t ifindex = 1; ifindex < iflist_len; ifindex++) {
711     struct tcmsg *tm;
712
713     if (iflist[ifindex] == NULL)
714       continue;
715
716     for (size_t type_index = 0; type_index < STATIC_ARRAY_SIZE(type_id);
717          type_index++) {
718       if (check_ignorelist(iflist[ifindex], type_name[type_index], NULL)) {
719         DEBUG("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
720               "== TRUE",
721               iflist[ifindex], type_name[type_index]);
722         continue;
723       }
724
725       DEBUG("netlink plugin: ir_read: querying %s from %s (%" PRIsz ").",
726             type_name[type_index], iflist[ifindex], ifindex);
727
728       nlh = mnl_nlmsg_put_header(buf);
729       nlh->nlmsg_type = type_id[type_index];
730       nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
731       nlh->nlmsg_seq = seq = time(NULL);
732       tm = mnl_nlmsg_put_extra_header(nlh, sizeof(*tm));
733       tm->tcm_family = AF_PACKET;
734       tm->tcm_ifindex = ifindex;
735
736       if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
737         ERROR("netlink plugin: ir_read: mnl_socket_sendto failed.");
738         continue;
739       }
740
741       ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
742       while (ret > 0) {
743         ret = mnl_cb_run(buf, ret, seq, portid, qos_filter_cb, &ifindex);
744         if (ret <= MNL_CB_STOP)
745           break;
746         ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
747       }
748       if (ret < 0) {
749         ERROR("netlink plugin: ir_read: mnl_socket_recvfrom failed: %s",
750               STRERRNO);
751         continue;
752       }
753     } /* for (type_index) */
754   }   /* for (if_index) */
755
756   return 0;
757 } /* int ir_read */
758
759 static int ir_shutdown(void) {
760   if (nl) {
761     mnl_socket_close(nl);
762     nl = NULL;
763   }
764
765   return 0;
766 } /* int ir_shutdown */
767
768 void module_register(void) {
769   plugin_register_config("netlink", ir_config, config_keys, config_keys_num);
770   plugin_register_init("netlink", ir_init);
771   plugin_register_read("netlink", ir_read);
772   plugin_register_shutdown("netlink", ir_shutdown);
773 } /* void module_register */