libnl  3.5.0
nf-monitor.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * src/nf-monitor.c Monitor netfilter events
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation version 2.1
8  * of the License.
9  *
10  * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
11  * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
12  * Copyright (c) 2007 Secure Computing Corporation
13  */
14 
15 #include <netlink/cli/utils.h>
16 #include <netlink/netfilter/nfnl.h>
17 
18 #include <linux/netlink.h>
19 #include <linux/netfilter/nfnetlink.h>
20 
21 static void obj_input(struct nl_object *obj, void *arg)
22 {
23  struct nl_dump_params dp = {
25  .dp_fd = stdout,
26  .dp_dump_msgtype = 1,
27  };
28 
29  nl_object_dump(obj, &dp);
30 }
31 
32 static int event_input(struct nl_msg *msg, void *arg)
33 {
34  if (nl_msg_parse(msg, &obj_input, NULL) < 0)
35  fprintf(stderr, "<<EVENT>> Unknown message type\n");
36 
37  /* Exit nl_recvmsgs_def() and return to the main select() */
38  return NL_STOP;
39 }
40 
41 int main(int argc, char *argv[])
42 {
43  struct nl_sock *sock;
44  int err;
45  int i, idx;
46 
47  static const struct {
48  enum nfnetlink_groups gr_id;
49  const char* gr_name;
50  } groups[] = {
51  { NFNLGRP_CONNTRACK_NEW, "ct-new" },
52  { NFNLGRP_CONNTRACK_UPDATE, "ct-update" },
53  { NFNLGRP_CONNTRACK_DESTROY, "ct-destroy" },
54  { NFNLGRP_NONE, NULL }
55  };
56 
57  sock = nl_cli_alloc_socket();
59  nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
60 
61  if (argc > 1 && !strcasecmp(argv[1], "-h")) {
62  printf("Usage: nf-monitor [<groups>]\n");
63 
64  printf("Known groups:");
65  for (i = 0; groups[i].gr_id != NFNLGRP_NONE; i++)
66  printf(" %s", groups[i].gr_name);
67  printf("\n");
68  return 2;
69  }
70 
71  nl_cli_connect(sock, NETLINK_NETFILTER);
72 
73  for (idx = 1; argc > idx; idx++) {
74  for (i = 0; groups[i].gr_id != NFNLGRP_NONE; i++) {
75  if (strcmp(argv[idx], groups[i].gr_name))
76  continue;
77 
78  err = nl_socket_add_membership(sock, groups[i].gr_id);
79  if (err < 0)
80  nl_cli_fatal(err,
81  "Unable to add membership: %s",
82  nl_geterror(err));
83  break;
84  }
85 
86  if (groups[i].gr_id == NFNLGRP_NONE)
87  nl_cli_fatal(NLE_OBJ_NOTFOUND, "Unknown group: \"%s\"",
88  argv[idx]);
89  }
90 
91  while (1) {
92  fd_set rfds;
93  int fd, retval;
94 
95  fd = nl_socket_get_fd(sock);
96 
97  FD_ZERO(&rfds);
98  FD_SET(fd, &rfds);
99  /* wait for an incoming message on the netlink socket */
100  retval = select(fd+1, &rfds, NULL, NULL, NULL);
101 
102  if (retval) {
103  /* FD_ISSET(fd, &rfds) will be true */
104  nl_recvmsgs_default(sock);
105  }
106  }
107 
108  return 0;
109 }
Customized handler specified by the user.
Definition: handlers.h:83
int nl_socket_get_fd(const struct nl_sock *sk)
Return the file descriptor of the backing socket.
Definition: socket.c:584
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
Stop parsing altogether and discard remaining messages.
Definition: handlers.h:68
int nl_socket_modify_cb(struct nl_sock *sk, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
Modify the callback handler associated with the socket.
Definition: socket.c:771
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:289
void nl_socket_disable_seq_check(struct nl_sock *sk)
Disable sequence number checking.
Definition: socket.c:283
int nl_recvmsgs_default(struct nl_sock *sk)
Receive a set of message from a netlink socket using handlers in nl_sock.
Definition: nl.c:1094
Message is valid.
Definition: handlers.h:95
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:78
Dumping parameters.
Definition: types.h:33
Dump all attributes including statistics.
Definition: types.h:24