6 #include <libmnl/libmnl.h>
7 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
10 struct mnl_socket *nl;
15 static void set_label(
struct nf_conntrack *ct,
struct callback_args *cbargs)
17 struct nfct_bitmask *b = (
void *)
nfct_get_attr(ct, ATTR_CONNLABELS);
18 int bit = cbargs->bit;
19 char buf[MNL_SOCKET_BUFFER_SIZE];
25 b = nfct_bitmask_new(0);
26 else if (nfct_bitmask_test_bit(b, bit))
29 b = nfct_bitmask_new(0);
35 nfct_bitmask_set_bit(b, bit);
39 b = nfct_bitmask_new(bit);
41 nfct_bitmask_set_bit(b, bit);
48 nlh = mnl_nlmsg_put_header(buf);
49 nlh->nlmsg_type = (NFNL_SUBSYS_CTNETLINK << 8) | IPCTNL_MSG_CT_NEW;
50 nlh->nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE;
51 nlh->nlmsg_seq = cbargs->seq;
53 nfh = mnl_nlmsg_put_extra_header(nlh,
sizeof(
struct nfgenmsg));
55 nfh->version = NFNETLINK_V0;
58 nfct_nlmsg_build(nlh, ct);
60 if (mnl_socket_sendto(cbargs->nl, nlh, nlh->nlmsg_len) < 0)
61 perror(
"mnl_socket_sendto");
64 static int data_cb(
const struct nlmsghdr *nlh,
void *data)
66 struct nf_conntrack *ct;
73 nfct_nlmsg_parse(nlh, ct);
75 nfct_snprintf(buf,
sizeof(buf), ct, NFCT_T_UNKNOWN, NFCT_O_DEFAULT, 0);
91 fputs(
"usage: program label, configured labels are:\n", stderr);
94 fprintf(stderr,
"%s -> bit %d\n", name, i);
98 fputs(
"no labels configured, usage: program bit\n", stderr);
103 static struct mnl_socket *sock_nl_create(
void)
105 struct mnl_socket *nl;
107 nl = mnl_socket_open(NETLINK_NETFILTER);
109 perror(
"mnl_socket_open");
113 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
114 perror(
"mnl_socket_bind");
121 int main(
int argc,
char *argv[])
123 struct mnl_socket *nl;
124 struct nlmsghdr *nlh;
125 struct nfgenmsg *nfh;
126 char buf[MNL_SOCKET_BUFFER_SIZE];
127 unsigned int seq, portid;
137 if (cbargs.bit < 0) {
138 cbargs.bit = atoi(argv[1]);
139 if (cbargs.bit == 0 && argv[1][0] !=
'0')
144 puts(
"will clear all labels");
146 printf(
"will set label bit %d\n", cbargs.bit);
148 nl = sock_nl_create();
149 portid = mnl_socket_get_portid(nl);
151 nlh = mnl_nlmsg_put_header(buf);
152 nlh->nlmsg_type = (NFNL_SUBSYS_CTNETLINK << 8) | IPCTNL_MSG_CT_GET;
153 nlh->nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP;
154 nlh->nlmsg_seq = seq = time(NULL);
156 nfh = mnl_nlmsg_put_extra_header(nlh,
sizeof(
struct nfgenmsg));
157 nfh->nfgen_family = AF_UNSPEC;
158 nfh->version = NFNETLINK_V0;
162 ret = mnl_socket_sendto(nl, nlh, nlh->nlmsg_len);
164 perror(
"mnl_socket_sendto");
168 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
171 cbargs.nl = sock_nl_create();
175 ret = mnl_cb_run(buf, ret, seq, portid, data_cb, &cbargs);
176 if (ret <= MNL_CB_STOP)
178 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
181 perror(
"mnl_socket_recvfrom");
187 mnl_socket_close(nl);
uint8_t nfct_get_attr_u8(const struct nf_conntrack *ct, const enum nf_conntrack_attr type)
const char * nfct_labelmap_get_name(struct nfct_labelmap *m, unsigned int bit)
void nfct_set_attr(struct nf_conntrack *ct, const enum nf_conntrack_attr type, const void *value)
void nfct_destroy(struct nf_conntrack *ct)
int nfct_snprintf(char *buf, unsigned int size, const struct nf_conntrack *ct, const unsigned int msg_type, const unsigned int out_type, const unsigned int out_flags)
struct nfct_labelmap * nfct_labelmap_new(const char *mapfile)
struct nf_conntrack * nfct_new(void)
const void * nfct_get_attr(const struct nf_conntrack *ct, const enum nf_conntrack_attr type)
int nfct_labelmap_get_bit(struct nfct_labelmap *m, const char *name)
void nfct_labelmap_destroy(struct nfct_labelmap *map)