libnl 3.5.0

nf-ct-events.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * src/nf-ct-events.c Listen on Conntrack 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) 2018 Avast software
11 */
12
13 #include <netlink/cli/utils.h>
14 #include <netlink/cli/ct.h>
15
16 #include <linux/netlink.h>
17 #include <linux/netfilter/nfnetlink.h>
18 #include <linux/netfilter/nfnetlink_conntrack.h>
19
20 struct private_nl_object
21 {
22 int ce_refcnt;
23 struct nl_object_ops * ce_ops;
24 struct nl_cache * ce_cache;
25 struct nl_list_head ce_list;
26 int ce_msgtype;
27 int ce_flags;
28 uint64_t ce_mask;
29 };
30
31 static void nf_conntrack_parse_callback(struct nl_object *obj, void *opaque)
32 {
33 struct nl_dump_params params = {
34 .dp_fd = stdout,
35 .dp_type = NL_DUMP_DETAILS,
36 };
37
38 nl_object_dump(obj, &params);
39 }
40
41 static int nf_conntrack_event_callback(struct nl_msg *msg, void *opaque)
42 {
43 int err;
44 struct nlmsghdr *hdr = nlmsg_hdr(msg);
45
46 enum cntl_msg_types type = (enum cntl_msg_types) NFNL_MSG_TYPE(hdr->nlmsg_type);
47
48 int flags = hdr->nlmsg_flags;
49
50 if (type == IPCTNL_MSG_CT_DELETE) {
51 printf("DELETE ");
52 } else if (type == IPCTNL_MSG_CT_NEW) {
53 if (flags & (NLM_F_CREATE|NLM_F_EXCL)) {
54 printf("NEW ");
55 } else {
56 printf("UPDATE ");
57 }
58 } else {
59 printf("UNKNOWN ");
60 }
61
62 if ((err = nl_msg_parse(msg, &nf_conntrack_parse_callback, opaque)) < 0) {
63 nl_cli_fatal(err, "nl_msg_parse: %s", nl_geterror(err));
64 }
65 /* Continue with next event */
66 return NL_OK;
67 }
68
69 int main(int argc, char *argv[])
70 {
71 struct nl_sock *socket;
72 int err;
73
74 socket = nl_cli_alloc_socket();
75 if (socket == NULL) {
76 nl_cli_fatal(ENOBUFS, "Unable to allocate netlink socket");
77 }
78
79 /*
80 * Disable sequence number checking.
81 * This is required to allow messages to be processed which were not requested by
82 * a preceding request message, e.g. netlink events.
83 */
84 nl_socket_disable_seq_check(socket);
85
86 /* subscribe conntrack events */
87 nl_join_groups(socket, NF_NETLINK_CONNTRACK_NEW |
88 NF_NETLINK_CONNTRACK_UPDATE |
89 NF_NETLINK_CONNTRACK_DESTROY |
90 NF_NETLINK_CONNTRACK_EXP_NEW |
91 NF_NETLINK_CONNTRACK_EXP_UPDATE |
92 NF_NETLINK_CONNTRACK_EXP_DESTROY);
93
94 nl_cli_connect(socket, NETLINK_NETFILTER);
95
96 nl_socket_modify_cb(socket, NL_CB_VALID, NL_CB_CUSTOM, &nf_conntrack_event_callback, 0);
97
98 while (1) {
99
100 errno = 0;
101 if ((err = nl_recvmsgs_default(socket)) < 0) {
102 switch (errno) {
103 case ENOBUFS:
104 // just print warning
105 fprintf(stderr, "Lost events because of ENOBUFS\n");
106 break;
107 case EAGAIN:
108 case EINTR:
109 // continue reading
110 break;
111 default:
112 nl_cli_fatal(err, "Failed to receive: %s", nl_geterror(err));
113 }
114 }
115 }
116 }
nl_dump_params::dp_fd
FILE * dp_fd
File descriptor the dumping output should go to.
Definition: types.h:83
NL_CB_CUSTOM
Customized handler specified by the user.
Definition: handlers.h:83
NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:23
nl_socket_modify_cb
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
nl_object_dump
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
nl_socket_disable_seq_check
void nl_socket_disable_seq_check(struct nl_sock *sk)
Disable sequence number checking.
Definition: socket.c:283
nlmsg_hdr
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
Definition: msg.c:543
nl_recvmsgs_default
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
NL_CB_VALID
Message is valid.
Definition: handlers.h:95
nl_cli_fatal
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:78
NL_OK
Proceed with wathever would come next.
Definition: handlers.h:64
nl_dump_params
Dumping parameters.
Definition: types.h:33
nl_list_head
Definition: list.h:17
nl_join_groups
void nl_join_groups(struct nl_sock *sk, int groups)
Join multicast groups (deprecated)
Definition: socket.c:533
private_nl_object
Definition: nf-ct-events.c:20