libnl  3.3.0
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 <netlink/netfilter/nfnl.h>
17 #include <netlink/netfilter/log.h>
18 
19 #include <linux/netfilter/nfnetlink_log.h>
20 #include <linux/netlink.h>
21 
22 static struct nfnl_log *alloc_log(void)
23 {
24  struct nfnl_log *log;
25 
26  log = nfnl_log_alloc();
27  if (!log)
28  nl_cli_fatal(ENOMEM, "Unable to allocate log object");
29 
30  return log;
31 }
32 
33 static void obj_input(struct nl_object *obj, void *arg)
34 {
35  struct nl_dump_params dp = {
37  .dp_fd = stdout,
38  .dp_dump_msgtype = 1,
39  };
40 
41  nl_object_dump(obj, &dp);
42 }
43 
44 static int event_input(struct nl_msg *msg, void *arg)
45 {
46  if (nl_msg_parse(msg, &obj_input, NULL) < 0)
47  fprintf(stderr, "<<EVENT>> Unknown message type\n");
48 
49  /* Exit nl_recvmsgs_def() and return to the main select() */
50  return NL_STOP;
51 }
52 
53 int main(int argc, char *argv[])
54 {
55  struct nl_sock *nf_sock;
56  struct nl_sock *rt_sock;
57  struct nfnl_log *log;
58  int copy_mode;
59  uint32_t copy_range;
60  int err;
61  int family;
62 
63  nf_sock = nl_cli_alloc_socket();
65  nl_socket_modify_cb(nf_sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
66 
67  if ((argc > 1 && !strcasecmp(argv[1], "-h")) || argc < 3) {
68  printf("Usage: nf-log family group [ copy_mode ] "
69  "[copy_range] \n");
70  return 2;
71  }
72 
73  nl_cli_connect(nf_sock, NETLINK_NETFILTER);
74 
75  family = nl_str2af(argv[1]);
76  if (family == AF_UNSPEC)
77  nl_cli_fatal(NLE_INVAL, "Unknown family \"%s\": %s",
78  argv[1], nl_geterror(family));
79 
80  nfnl_log_pf_unbind(nf_sock, family);
81  if ((err = nfnl_log_pf_bind(nf_sock, family)) < 0)
82  nl_cli_fatal(err, "Unable to bind logger: %s",
83  nl_geterror(err));
84 
85  log = alloc_log();
86  nfnl_log_set_group(log, atoi(argv[2]));
87 
88  copy_mode = NFNL_LOG_COPY_META;
89  if (argc > 3) {
90  copy_mode = nfnl_log_str2copy_mode(argv[3]);
91  if (copy_mode < 0)
92  nl_cli_fatal(copy_mode,
93  "Unable to parse copy mode \"%s\": %s",
94  argv[3], nl_geterror(copy_mode));
95  }
96  nfnl_log_set_copy_mode(log, copy_mode);
97 
98  copy_range = 0xFFFF;
99  if (argc > 4)
100  copy_range = atoi(argv[4]);
101  nfnl_log_set_copy_range(log, copy_range);
102 
103  if ((err = nfnl_log_create(nf_sock, log)) < 0)
104  nl_cli_fatal(err, "Unable to bind instance: %s",
105  nl_geterror(err));
106 
107  {
108  struct nl_dump_params dp = {
110  .dp_fd = stdout,
111  .dp_dump_msgtype = 1,
112  };
113 
114  printf("log params: ");
115  nl_object_dump((struct nl_object *) log, &dp);
116  }
117 
118  rt_sock = nl_cli_alloc_socket();
119  nl_cli_connect(rt_sock, NETLINK_ROUTE);
120  nl_cli_link_alloc_cache(rt_sock);
121 
122  while (1) {
123  fd_set rfds;
124  int nffd, rtfd, maxfd, retval;
125 
126  FD_ZERO(&rfds);
127 
128  maxfd = nffd = nl_socket_get_fd(nf_sock);
129  FD_SET(nffd, &rfds);
130 
131  rtfd = nl_socket_get_fd(rt_sock);
132  FD_SET(rtfd, &rfds);
133  if (maxfd < rtfd)
134  maxfd = rtfd;
135 
136  /* wait for an incoming message on the netlink nf_socket */
137  retval = select(maxfd+1, &rfds, NULL, NULL, NULL);
138 
139  if (retval) {
140  if (FD_ISSET(nffd, &rfds))
141  nl_recvmsgs_default(nf_sock);
142  if (FD_ISSET(rtfd, &rfds))
143  nl_recvmsgs_default(rt_sock);
144  }
145  }
146 
147  return 0;
148 }
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:583
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:770
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:282
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:1093
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:71
Dumping parameters.
Definition: types.h:33
Dump all attributes including statistics.
Definition: types.h:24