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