libnl  3.2.27
nf-log.c
1 /*
2  * src/nf-log.c Monitor netfilter log events
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
10  * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
11  * Copyright (c) 2007 Secure Computing Corporation
12  */
13 
14 #include <netlink/cli/utils.h>
15 #include <netlink/cli/link.h>
16 #include <linux/netfilter/nfnetlink_log.h>
17 #include <netlink/netfilter/nfnl.h>
18 #include <netlink/netfilter/log.h>
19 
20 static struct nfnl_log *alloc_log(void)
21 {
22  struct nfnl_log *log;
23 
24  log = nfnl_log_alloc();
25  if (!log)
26  nl_cli_fatal(ENOMEM, "Unable to allocate log object");
27 
28  return log;
29 }
30 
31 static void obj_input(struct nl_object *obj, void *arg)
32 {
33  struct nl_dump_params dp = {
35  .dp_fd = stdout,
36  .dp_dump_msgtype = 1,
37  };
38 
39  nl_object_dump(obj, &dp);
40 }
41 
42 static int event_input(struct nl_msg *msg, void *arg)
43 {
44  if (nl_msg_parse(msg, &obj_input, NULL) < 0)
45  fprintf(stderr, "<<EVENT>> Unknown message type\n");
46 
47  /* Exit nl_recvmsgs_def() and return to the main select() */
48  return NL_STOP;
49 }
50 
51 int main(int argc, char *argv[])
52 {
53  struct nl_sock *nf_sock;
54  struct nl_sock *rt_sock;
55  struct nfnl_log *log;
56  int copy_mode;
57  uint32_t copy_range;
58  int err;
59  int family;
60 
61  nf_sock = nl_cli_alloc_socket();
63  nl_socket_modify_cb(nf_sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
64 
65  if ((argc > 1 && !strcasecmp(argv[1], "-h")) || argc < 3) {
66  printf("Usage: nf-log family group [ copy_mode ] "
67  "[copy_range] \n");
68  return 2;
69  }
70 
71  nl_cli_connect(nf_sock, NETLINK_NETFILTER);
72 
73  family = nl_str2af(argv[1]);
74  if (family == AF_UNSPEC)
75  nl_cli_fatal(NLE_INVAL, "Unknown family \"%s\": %s",
76  argv[1], nl_geterror(family));
77 
78  nfnl_log_pf_unbind(nf_sock, family);
79  if ((err = nfnl_log_pf_bind(nf_sock, family)) < 0)
80  nl_cli_fatal(err, "Unable to bind logger: %s",
81  nl_geterror(err));
82 
83  log = alloc_log();
84  nfnl_log_set_group(log, atoi(argv[2]));
85 
86  copy_mode = NFNL_LOG_COPY_META;
87  if (argc > 3) {
88  copy_mode = nfnl_log_str2copy_mode(argv[3]);
89  if (copy_mode < 0)
90  nl_cli_fatal(copy_mode,
91  "Unable to parse copy mode \"%s\": %s",
92  argv[3], nl_geterror(copy_mode));
93  }
94  nfnl_log_set_copy_mode(log, copy_mode);
95 
96  copy_range = 0xFFFF;
97  if (argc > 4)
98  copy_range = atoi(argv[4]);
99  nfnl_log_set_copy_range(log, copy_range);
100 
101  if ((err = nfnl_log_create(nf_sock, log)) < 0)
102  nl_cli_fatal(err, "Unable to bind instance: %s",
103  nl_geterror(err));
104 
105  {
106  struct nl_dump_params dp = {
108  .dp_fd = stdout,
109  .dp_dump_msgtype = 1,
110  };
111 
112  printf("log params: ");
113  nl_object_dump((struct nl_object *) log, &dp);
114  }
115 
116  rt_sock = nl_cli_alloc_socket();
117  nl_cli_connect(rt_sock, NETLINK_ROUTE);
118  nl_cli_link_alloc_cache(rt_sock);
119 
120  while (1) {
121  fd_set rfds;
122  int nffd, rtfd, maxfd, retval;
123 
124  FD_ZERO(&rfds);
125 
126  maxfd = nffd = nl_socket_get_fd(nf_sock);
127  FD_SET(nffd, &rfds);
128 
129  rtfd = nl_socket_get_fd(rt_sock);
130  FD_SET(rtfd, &rfds);
131  if (maxfd < rtfd)
132  maxfd = rtfd;
133 
134  /* wait for an incoming message on the netlink nf_socket */
135  retval = select(maxfd+1, &rfds, NULL, NULL, NULL);
136 
137  if (retval) {
138  if (FD_ISSET(nffd, &rfds))
139  nl_recvmsgs_default(nf_sock);
140  if (FD_ISSET(rtfd, &rfds))
141  nl_recvmsgs_default(rt_sock);
142  }
143  }
144 
145  return 0;
146 }
Customized handler specified by the user.
Definition: handlers.h:80
int nl_socket_get_fd(const struct nl_sock *sk)
Return the file descriptor of the backing socket.
Definition: socket.c:586
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:65
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:288
void nl_socket_disable_seq_check(struct nl_sock *sk)
Disable sequence number checking.
Definition: socket.c:281
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:1098
Message is valid.
Definition: handlers.h:92
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:70
Dumping parameters.
Definition: types.h:33
Dump all attributes including statistics.
Definition: types.h:24