email, exec and unixsock plugins: enlarge buffer for getgrnam_r result
[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
60   uint64_t rx_length_errors;
61   uint64_t rx_over_errors;
62   uint64_t rx_crc_errors;
63   uint64_t rx_frame_errors;
64   uint64_t rx_fifo_errors;
65   uint64_t rx_missed_errors;
66
67   uint64_t tx_aborted_errors;
68   uint64_t tx_carrier_errors;
69   uint64_t tx_fifo_errors;
70   uint64_t tx_heartbeat_errors;
71   uint64_t tx_window_errors;
72 };
73
74 union ir_link_stats_u {
75   struct rtnl_link_stats *stats32;
76 #ifdef HAVE_RTNL_LINK_STATS64
77   struct rtnl_link_stats64 *stats64;
78 #endif
79 };
80
81 typedef struct ir_ignorelist_s {
82   char *device;
83   char *type;
84   char *inst;
85   struct ir_ignorelist_s *next;
86 } ir_ignorelist_t;
87
88 static int ir_ignorelist_invert = 1;
89 static ir_ignorelist_t *ir_ignorelist_head = NULL;
90
91 static struct mnl_socket *nl;
92
93 static char **iflist = NULL;
94 static size_t iflist_len = 0;
95
96 static const char *config_keys[] = {"Interface", "VerboseInterface",
97                                     "QDisc",     "Class",
98                                     "Filter",    "IgnoreSelected"};
99 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
100
101 static int add_ignorelist(const char *dev, const char *type, const char *inst) {
102   ir_ignorelist_t *entry;
103
104   entry = calloc(1, sizeof(*entry));
105   if (entry == NULL)
106     return (-1);
107
108   if (strcasecmp(dev, "All") != 0) {
109     entry->device = strdup(dev);
110     if (entry->device == NULL) {
111       sfree(entry);
112       return (-1);
113     }
114   }
115
116   entry->type = strdup(type);
117   if (entry->type == NULL) {
118     sfree(entry->device);
119     sfree(entry);
120     return (-1);
121   }
122
123   if (inst != NULL) {
124     entry->inst = strdup(inst);
125     if (entry->inst == NULL) {
126       sfree(entry->type);
127       sfree(entry->device);
128       sfree(entry);
129       return (-1);
130     }
131   }
132
133   entry->next = ir_ignorelist_head;
134   ir_ignorelist_head = entry;
135
136   return (0);
137 } /* int add_ignorelist */
138
139 /*
140  * Checks wether a data set should be ignored. Returns `true' is the value
141  * should be ignored, `false' otherwise.
142  */
143 static int check_ignorelist(const char *dev, const char *type,
144                             const char *type_instance) {
145   assert((dev != NULL) && (type != NULL));
146
147   if (ir_ignorelist_head == NULL)
148     return (ir_ignorelist_invert ? 0 : 1);
149
150   for (ir_ignorelist_t *i = ir_ignorelist_head; i != NULL; i = i->next) {
151     /* i->device == NULL  =>  match all devices */
152     if ((i->device != NULL) && (strcasecmp(i->device, dev) != 0))
153       continue;
154
155     if (strcasecmp(i->type, type) != 0)
156       continue;
157
158     if ((i->inst != NULL) && (type_instance != NULL) &&
159         (strcasecmp(i->inst, type_instance) != 0))
160       continue;
161
162     DEBUG("netlink plugin: check_ignorelist: "
163           "(dev = %s; type = %s; inst = %s) matched "
164           "(dev = %s; type = %s; inst = %s)",
165           dev, type, type_instance == NULL ? "(nil)" : type_instance,
166           i->device == NULL ? "(nil)" : i->device, i->type,
167           i->inst == NULL ? "(nil)" : i->inst);
168
169     return (ir_ignorelist_invert ? 0 : 1);
170   } /* for i */
171
172   return (ir_ignorelist_invert);
173 } /* int check_ignorelist */
174
175 static void submit_one(const char *dev, const char *type,
176                        const char *type_instance, derive_t value) {
177   value_t values[1];
178   value_list_t vl = VALUE_LIST_INIT;
179
180   values[0].derive = value;
181
182   vl.values = values;
183   vl.values_len = 1;
184   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
185   sstrncpy(vl.plugin, "netlink", sizeof(vl.plugin));
186   sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
187   sstrncpy(vl.type, type, sizeof(vl.type));
188
189   if (type_instance != NULL)
190     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
191
192   plugin_dispatch_values(&vl);
193 } /* void submit_one */
194
195 static void submit_two(const char *dev, const char *type,
196                        const char *type_instance, derive_t rx, derive_t tx) {
197   value_t values[2];
198   value_list_t vl = VALUE_LIST_INIT;
199
200   values[0].derive = rx;
201   values[1].derive = tx;
202
203   vl.values = values;
204   vl.values_len = 2;
205   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
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
258     submit_one(dev, "if_rx_errors", "length", stats->rx_length_errors);
259     submit_one(dev, "if_rx_errors", "over", stats->rx_over_errors);
260     submit_one(dev, "if_rx_errors", "crc", stats->rx_crc_errors);
261     submit_one(dev, "if_rx_errors", "frame", stats->rx_frame_errors);
262     submit_one(dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
263     submit_one(dev, "if_rx_errors", "missed", stats->rx_missed_errors);
264
265     submit_one(dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
266     submit_one(dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
267     submit_one(dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
268     submit_one(dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
269     submit_one(dev, "if_tx_errors", "window", stats->tx_window_errors);
270   } else {
271     DEBUG("netlink plugin: Ignoring %s/if_detail.", dev);
272   }
273
274 } /* void check_ignorelist_and_submit */
275
276 #define COPY_RTNL_LINK_VALUE(dst_stats, src_stats, value_name)                 \
277   (dst_stats)->value_name = (src_stats)->value_name
278
279 #define COPY_RTNL_LINK_STATS(dst_stats, src_stats)                             \
280   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_packets);                      \
281   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_packets);                      \
282   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_bytes);                        \
283   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_bytes);                        \
284   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_errors);                       \
285   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_errors);                       \
286   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_dropped);                      \
287   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_dropped);                      \
288   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, multicast);                       \
289   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, collisions);                      \
290   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_length_errors);                \
291   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_over_errors);                  \
292   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_crc_errors);                   \
293   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_frame_errors);                 \
294   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_fifo_errors);                  \
295   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_missed_errors);                \
296   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_aborted_errors);               \
297   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_carrier_errors);               \
298   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_fifo_errors);                  \
299   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_heartbeat_errors);             \
300   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_window_errors)
301
302 #ifdef HAVE_RTNL_LINK_STATS64
303 static void check_ignorelist_and_submit64(const char *dev,
304                                           struct rtnl_link_stats64 *stats) {
305   struct ir_link_stats_storage_s s;
306
307   COPY_RTNL_LINK_STATS(&s, stats);
308
309   check_ignorelist_and_submit(dev, &s);
310 }
311 #endif
312
313 static void check_ignorelist_and_submit32(const char *dev,
314                                           struct rtnl_link_stats *stats) {
315   struct ir_link_stats_storage_s s;
316
317   COPY_RTNL_LINK_STATS(&s, stats);
318
319   check_ignorelist_and_submit(dev, &s);
320 }
321
322 static int link_filter_cb(const struct nlmsghdr *nlh,
323                           void *args __attribute__((unused))) {
324   struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
325   struct nlattr *attr;
326   const char *dev = NULL;
327   union ir_link_stats_u stats;
328
329   if (nlh->nlmsg_type != RTM_NEWLINK) {
330     ERROR("netlink plugin: link_filter_cb: Don't know how to handle type %i.",
331           nlh->nlmsg_type);
332     return MNL_CB_ERROR;
333   }
334
335   /* Scan attribute list for device name. */
336   mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
337     if (mnl_attr_get_type(attr) != IFLA_IFNAME)
338       continue;
339
340     if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
341       ERROR("netlink plugin: link_filter_cb: IFLA_IFNAME mnl_attr_validate "
342             "failed.");
343       return MNL_CB_ERROR;
344     }
345
346     dev = mnl_attr_get_str(attr);
347     if (update_iflist(ifm, dev) < 0)
348       return MNL_CB_ERROR;
349     break;
350   }
351
352   if (dev == NULL) {
353     ERROR("netlink plugin: link_filter_cb: dev == NULL");
354     return MNL_CB_ERROR;
355   }
356 #ifdef HAVE_RTNL_LINK_STATS64
357   mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
358     if (mnl_attr_get_type(attr) != IFLA_STATS64)
359       continue;
360
361     if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*stats.stats64)) < 0) {
362       ERROR("netlink plugin: link_filter_cb: IFLA_STATS64 mnl_attr_validate2 "
363             "failed.");
364       return MNL_CB_ERROR;
365     }
366     stats.stats64 = mnl_attr_get_payload(attr);
367
368     check_ignorelist_and_submit64(dev, stats.stats64);
369
370     return MNL_CB_OK;
371   }
372 #endif
373   mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
374     if (mnl_attr_get_type(attr) != IFLA_STATS)
375       continue;
376
377     if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*stats.stats32)) < 0) {
378       ERROR("netlink plugin: link_filter_cb: IFLA_STATS mnl_attr_validate2 "
379             "failed.");
380       return MNL_CB_ERROR;
381     }
382     stats.stats32 = mnl_attr_get_payload(attr);
383
384     check_ignorelist_and_submit32(dev, stats.stats32);
385
386     return MNL_CB_OK;
387   }
388
389   DEBUG("netlink plugin: link_filter: No statistics for interface %s.", dev);
390   return MNL_CB_OK;
391
392 } /* int link_filter_cb */
393
394 #if HAVE_TCA_STATS2
395 static int qos_attr_cb(const struct nlattr *attr, void *data) {
396   struct gnet_stats_basic **bs = (struct gnet_stats_basic **)data;
397
398   /* skip unsupported attribute in user-space */
399   if (mnl_attr_type_valid(attr, TCA_STATS_MAX) < 0)
400     return MNL_CB_OK;
401
402   if (mnl_attr_get_type(attr) == TCA_STATS_BASIC) {
403     if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(**bs)) < 0) {
404       ERROR("netlink plugin: qos_attr_cb: TCA_STATS_BASIC mnl_attr_validate2 "
405             "failed.");
406       return MNL_CB_ERROR;
407     }
408     *bs = mnl_attr_get_payload(attr);
409     return MNL_CB_STOP;
410   }
411
412   return MNL_CB_OK;
413 } /* qos_attr_cb */
414 #endif
415
416 static int qos_filter_cb(const struct nlmsghdr *nlh, void *args) {
417   struct tcmsg *tm = mnl_nlmsg_get_payload(nlh);
418   struct nlattr *attr;
419
420   int wanted_ifindex = *((int *)args);
421
422   const char *dev;
423   const char *kind = NULL;
424
425   /* char *type_instance; */
426   const char *tc_type;
427   char tc_inst[DATA_MAX_NAME_LEN];
428
429   _Bool stats_submitted = 0;
430
431   if (nlh->nlmsg_type == RTM_NEWQDISC)
432     tc_type = "qdisc";
433   else if (nlh->nlmsg_type == RTM_NEWTCLASS)
434     tc_type = "class";
435   else if (nlh->nlmsg_type == RTM_NEWTFILTER)
436     tc_type = "filter";
437   else {
438     ERROR("netlink plugin: qos_filter_cb: Don't know how to handle type %i.",
439           nlh->nlmsg_type);
440     return MNL_CB_ERROR;
441   }
442
443   if (tm->tcm_ifindex != wanted_ifindex) {
444     DEBUG("netlink plugin: qos_filter_cb: Got %s for interface #%i, "
445           "but expected #%i.",
446           tc_type, tm->tcm_ifindex, wanted_ifindex);
447     return MNL_CB_OK;
448   }
449
450   if ((tm->tcm_ifindex >= 0) && ((size_t)tm->tcm_ifindex >= iflist_len)) {
451     ERROR("netlink plugin: qos_filter_cb: tm->tcm_ifindex = %i "
452           ">= iflist_len = %zu",
453           tm->tcm_ifindex, iflist_len);
454     return MNL_CB_ERROR;
455   }
456
457   dev = iflist[tm->tcm_ifindex];
458   if (dev == NULL) {
459     ERROR("netlink plugin: qos_filter_cb: iflist[%i] == NULL", tm->tcm_ifindex);
460     return MNL_CB_ERROR;
461   }
462
463   mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
464     if (mnl_attr_get_type(attr) != TCA_KIND)
465       continue;
466
467     if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
468       ERROR(
469           "netlink plugin: qos_filter_cb: TCA_KIND mnl_attr_validate failed.");
470       return MNL_CB_ERROR;
471     }
472
473     kind = mnl_attr_get_str(attr);
474     break;
475   }
476
477   if (kind == NULL) {
478     ERROR("netlink plugin: qos_filter_cb: kind == NULL");
479     return (-1);
480   }
481
482   { /* The ID */
483     uint32_t numberic_id;
484
485     numberic_id = tm->tcm_handle;
486     if (strcmp(tc_type, "filter") == 0)
487       numberic_id = tm->tcm_parent;
488
489     ssnprintf(tc_inst, sizeof(tc_inst), "%s-%x:%x", kind, numberic_id >> 16,
490               numberic_id & 0x0000FFFF);
491   }
492
493   DEBUG("netlink plugin: qos_filter_cb: got %s for %s (%i).", tc_type, dev,
494         tm->tcm_ifindex);
495
496   if (check_ignorelist(dev, tc_type, tc_inst))
497     return MNL_CB_OK;
498
499 #if HAVE_TCA_STATS2
500   mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
501     struct gnet_stats_basic *bs = NULL;
502
503     if (mnl_attr_get_type(attr) != TCA_STATS2)
504       continue;
505
506     if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) {
507       ERROR("netlink plugin: qos_filter_cb: TCA_STATS2 mnl_attr_validate "
508             "failed.");
509       return MNL_CB_ERROR;
510     }
511
512     mnl_attr_parse_nested(attr, qos_attr_cb, &bs);
513
514     if (bs != NULL) {
515       char type_instance[DATA_MAX_NAME_LEN];
516
517       stats_submitted = 1;
518
519       ssnprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type,
520                 tc_inst);
521
522       submit_one(dev, "ipt_bytes", type_instance, bs->bytes);
523       submit_one(dev, "ipt_packets", type_instance, bs->packets);
524     }
525
526     break;
527   }
528 #endif /* TCA_STATS2 */
529
530 #if HAVE_TCA_STATS
531   mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
532     struct tc_stats *ts = NULL;
533
534     if (mnl_attr_get_type(attr) != TCA_STATS)
535       continue;
536
537     if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*ts)) < 0) {
538       ERROR("netlink plugin: qos_filter_cb: TCA_STATS mnl_attr_validate2 "
539             "failed.");
540       return MNL_CB_ERROR;
541     }
542     ts = mnl_attr_get_payload(attr);
543
544     if (!stats_submitted && ts != NULL) {
545       char type_instance[DATA_MAX_NAME_LEN];
546
547       ssnprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type,
548                 tc_inst);
549
550       submit_one(dev, "ipt_bytes", type_instance, ts->bytes);
551       submit_one(dev, "ipt_packets", type_instance, ts->packets);
552     }
553
554     break;
555   }
556
557 #endif /* TCA_STATS */
558
559 #if !(HAVE_TCA_STATS && HAVE_TCA_STATS2)
560   DEBUG("netlink plugin: qos_filter_cb: Have neither TCA_STATS2 nor "
561         "TCA_STATS.");
562 #endif
563
564   return MNL_CB_OK;
565 } /* int qos_filter_cb */
566
567 static int ir_config(const char *key, const char *value) {
568   char *new_val;
569   char *fields[8];
570   int fields_num;
571   int status = 1;
572
573   new_val = strdup(value);
574   if (new_val == NULL)
575     return (-1);
576
577   fields_num = strsplit(new_val, fields, STATIC_ARRAY_SIZE(fields));
578   if ((fields_num < 1) || (fields_num > 8)) {
579     sfree(new_val);
580     return (-1);
581   }
582
583   if ((strcasecmp(key, "Interface") == 0) ||
584       (strcasecmp(key, "VerboseInterface") == 0)) {
585     if (fields_num != 1) {
586       ERROR("netlink plugin: Invalid number of fields for option "
587             "`%s'. Got %i, expected 1.",
588             key, fields_num);
589       status = -1;
590     } else {
591       add_ignorelist(fields[0], "interface", NULL);
592       if (strcasecmp(key, "VerboseInterface") == 0)
593         add_ignorelist(fields[0], "if_detail", NULL);
594       status = 0;
595     }
596   } else if ((strcasecmp(key, "QDisc") == 0) ||
597              (strcasecmp(key, "Class") == 0) ||
598              (strcasecmp(key, "Filter") == 0)) {
599     if ((fields_num < 1) || (fields_num > 2)) {
600       ERROR("netlink plugin: Invalid number of fields for option "
601             "`%s'. Got %i, expected 1 or 2.",
602             key, fields_num);
603       return (-1);
604     } else {
605       add_ignorelist(fields[0], key, (fields_num == 2) ? fields[1] : NULL);
606       status = 0;
607     }
608   } else if (strcasecmp(key, "IgnoreSelected") == 0) {
609     if (fields_num != 1) {
610       ERROR("netlink plugin: Invalid number of fields for option "
611             "`IgnoreSelected'. Got %i, expected 1.",
612             fields_num);
613       status = -1;
614     } else {
615       if (IS_TRUE(fields[0]))
616         ir_ignorelist_invert = 0;
617       else
618         ir_ignorelist_invert = 1;
619       status = 0;
620     }
621   }
622
623   sfree(new_val);
624
625   return (status);
626 } /* int ir_config */
627
628 static int ir_init(void) {
629   nl = mnl_socket_open(NETLINK_ROUTE);
630   if (nl == NULL) {
631     ERROR("netlink plugin: ir_init: mnl_socket_open failed.");
632     return (-1);
633   }
634
635   if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
636     ERROR("netlink plugin: ir_init: mnl_socket_bind failed.");
637     return (-1);
638   }
639
640   return (0);
641 } /* int ir_init */
642
643 static int ir_read(void) {
644   char buf[MNL_SOCKET_BUFFER_SIZE];
645   struct nlmsghdr *nlh;
646   struct rtgenmsg *rt;
647   int ret;
648   unsigned int seq, portid;
649
650   static const int type_id[] = {RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER};
651   static const char *type_name[] = {"qdisc", "class", "filter"};
652
653   portid = mnl_socket_get_portid(nl);
654
655   nlh = mnl_nlmsg_put_header(buf);
656   nlh->nlmsg_type = RTM_GETLINK;
657   nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
658   nlh->nlmsg_seq = seq = time(NULL);
659   rt = mnl_nlmsg_put_extra_header(nlh, sizeof(*rt));
660   rt->rtgen_family = AF_PACKET;
661
662   if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
663     ERROR("netlink plugin: ir_read: rtnl_wilddump_request failed.");
664     return (-1);
665   }
666
667   ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
668   while (ret > 0) {
669     ret = mnl_cb_run(buf, ret, seq, portid, link_filter_cb, NULL);
670     if (ret <= MNL_CB_STOP)
671       break;
672     ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
673   }
674   if (ret < 0) {
675     ERROR("netlink plugin: ir_read: mnl_socket_recvfrom failed.");
676     return (-1);
677   }
678
679   /* `link_filter_cb' will update `iflist' which is used here to iterate
680    * over all interfaces. */
681   for (size_t ifindex = 1; ifindex < iflist_len; ifindex++) {
682     struct tcmsg *tm;
683
684     if (iflist[ifindex] == NULL)
685       continue;
686
687     for (size_t type_index = 0; type_index < STATIC_ARRAY_SIZE(type_id);
688          type_index++) {
689       if (check_ignorelist(iflist[ifindex], type_name[type_index], NULL)) {
690         DEBUG("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
691               "== TRUE",
692               iflist[ifindex], type_name[type_index]);
693         continue;
694       }
695
696       DEBUG("netlink plugin: ir_read: querying %s from %s (%zu).",
697             type_name[type_index], iflist[ifindex], ifindex);
698
699       nlh = mnl_nlmsg_put_header(buf);
700       nlh->nlmsg_type = type_id[type_index];
701       nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
702       nlh->nlmsg_seq = seq = time(NULL);
703       tm = mnl_nlmsg_put_extra_header(nlh, sizeof(*tm));
704       tm->tcm_family = AF_PACKET;
705       tm->tcm_ifindex = ifindex;
706
707       if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
708         ERROR("netlink plugin: ir_read: mnl_socket_sendto failed.");
709         continue;
710       }
711
712       ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
713       while (ret > 0) {
714         ret = mnl_cb_run(buf, ret, seq, portid, qos_filter_cb, &ifindex);
715         if (ret <= MNL_CB_STOP)
716           break;
717         ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
718       }
719       if (ret < 0) {
720         ERROR("netlink plugin: ir_read:mnl_socket_recvfrom failed.");
721         continue;
722       }
723
724     } /* for (type_index) */
725   }   /* for (if_index) */
726
727   return (0);
728 } /* int ir_read */
729
730 static int ir_shutdown(void) {
731   if (nl) {
732     mnl_socket_close(nl);
733     nl = NULL;
734   }
735
736   return (0);
737 } /* int ir_shutdown */
738
739 void module_register(void) {
740   plugin_register_config("netlink", ir_config, config_keys, config_keys_num);
741   plugin_register_init("netlink", ir_init);
742   plugin_register_read("netlink", ir_read);
743   plugin_register_shutdown("netlink", ir_shutdown);
744 } /* void module_register */
745
746 /*
747  * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
748  */