2 * src/nf-log.c Monitor netfilter log events
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
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
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>
20 static struct nfnl_log *alloc_log(void)
24 log = nfnl_log_alloc();
26 nl_cli_fatal(ENOMEM, "Unable to allocate log object");
31 static void obj_input(struct nl_object *obj, void *arg)
33 struct nl_dump_params dp = {
34 .dp_type = NL_DUMP_STATS,
39 nl_object_dump(obj, &dp);
42 static int event_input(struct nl_msg *msg, void *arg)
44 if (nl_msg_parse(msg, &obj_input, NULL) < 0)
45 fprintf(stderr, "<<EVENT>> Unknown message type\n");
47 /* Exit nl_recvmsgs_def() and return to the main select() */
51 int main(int argc, char *argv[])
53 struct nl_sock *nf_sock;
54 struct nl_sock *rt_sock;
55 struct nl_cache *link_cache;
57 enum nfnl_log_copy_mode copy_mode;
62 nf_sock = nl_cli_alloc_socket();
63 nl_socket_disable_seq_check(nf_sock);
64 nl_socket_modify_cb(nf_sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
66 if ((argc > 1 && !strcasecmp(argv[1], "-h")) || argc < 3) {
67 printf("Usage: nf-log family group [ copy_mode ] "
72 nl_cli_connect(nf_sock, NETLINK_NETFILTER);
74 family = nl_str2af(argv[1]);
75 if (family == AF_UNSPEC)
76 nl_cli_fatal(NLE_INVAL, "Unknown family \"%s\": %s",
77 argv[1], nl_geterror(family));
79 nfnl_log_pf_unbind(nf_sock, family);
80 if ((err = nfnl_log_pf_bind(nf_sock, family)) < 0)
81 nl_cli_fatal(err, "Unable to bind logger: %s",
85 nfnl_log_set_group(log, atoi(argv[2]));
87 copy_mode = NFNL_LOG_COPY_META;
89 copy_mode = nfnl_log_str2copy_mode(argv[3]);
91 nl_cli_fatal(copy_mode,
92 "Unable to parse copy mode \"%s\": %s",
93 argv[3], nl_geterror(copy_mode));
95 nfnl_log_set_copy_mode(log, copy_mode);
99 copy_range = atoi(argv[4]);
100 nfnl_log_set_copy_range(log, copy_range);
102 if ((err = nfnl_log_create(nf_sock, log)) < 0)
103 nl_cli_fatal(err, "Unable to bind instance: %s",
107 struct nl_dump_params dp = {
108 .dp_type = NL_DUMP_STATS,
110 .dp_dump_msgtype = 1,
113 printf("log params: ");
114 nl_object_dump((struct nl_object *) log, &dp);
117 rt_sock = nl_cli_alloc_socket();
118 nl_cli_connect(rt_sock, NETLINK_ROUTE);
119 link_cache = nl_cli_link_alloc_cache(rt_sock);
123 int nffd, rtfd, maxfd, retval;
127 maxfd = nffd = nl_socket_get_fd(nf_sock);
130 rtfd = nl_socket_get_fd(rt_sock);
135 /* wait for an incoming message on the netlink nf_socket */
136 retval = select(maxfd+1, &rfds, NULL, NULL, NULL);
139 if (FD_ISSET(nffd, &rfds))
140 nl_recvmsgs_default(nf_sock);
141 if (FD_ISSET(rtfd, &rfds))
142 nl_recvmsgs_default(rt_sock);