libnftnl 1.2.6
nft-rule-add.c
1/*
2 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
10 */
11
12#include <stdlib.h>
13#include <time.h>
14#include <string.h>
15#include <stddef.h> /* for offsetof */
16#include <netinet/in.h>
17#include <netinet/ip.h>
18#include <netinet/tcp.h>
19#include <arpa/inet.h>
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <errno.h>
23
24#include <linux/netfilter.h>
25#include <linux/netfilter/nfnetlink.h>
26#include <linux/netfilter/nf_tables.h>
27
28#include <libmnl/libmnl.h>
29#include <libnftnl/rule.h>
30#include <libnftnl/expr.h>
31
32static void add_payload(struct nftnl_rule *r, uint32_t base, uint32_t dreg,
33 uint32_t offset, uint32_t len)
34{
35 struct nftnl_expr *e;
36
37 e = nftnl_expr_alloc("payload");
38 if (e == NULL) {
39 perror("expr payload oom");
40 exit(EXIT_FAILURE);
41 }
42
43 nftnl_expr_set_u32(e, NFTNL_EXPR_PAYLOAD_BASE, base);
44 nftnl_expr_set_u32(e, NFTNL_EXPR_PAYLOAD_DREG, dreg);
45 nftnl_expr_set_u32(e, NFTNL_EXPR_PAYLOAD_OFFSET, offset);
46 nftnl_expr_set_u32(e, NFTNL_EXPR_PAYLOAD_LEN, len);
47
48 nftnl_rule_add_expr(r, e);
49}
50
51static void add_cmp(struct nftnl_rule *r, uint32_t sreg, uint32_t op,
52 const void *data, uint32_t data_len)
53{
54 struct nftnl_expr *e;
55
56 e = nftnl_expr_alloc("cmp");
57 if (e == NULL) {
58 perror("expr cmp oom");
59 exit(EXIT_FAILURE);
60 }
61
62 nftnl_expr_set_u32(e, NFTNL_EXPR_CMP_SREG, sreg);
63 nftnl_expr_set_u32(e, NFTNL_EXPR_CMP_OP, op);
64 nftnl_expr_set(e, NFTNL_EXPR_CMP_DATA, data, data_len);
65
66 nftnl_rule_add_expr(r, e);
67}
68
69static void add_counter(struct nftnl_rule *r)
70{
71 struct nftnl_expr *e;
72
73 e = nftnl_expr_alloc("counter");
74 if (e == NULL) {
75 perror("expr counter oom");
76 exit(EXIT_FAILURE);
77 }
78
79 nftnl_rule_add_expr(r, e);
80}
81
82static struct nftnl_rule *setup_rule(uint8_t family, const char *table,
83 const char *chain, const char *handle)
84{
85 struct nftnl_rule *r = NULL;
86 uint8_t proto;
87 uint16_t dport;
88 uint64_t handle_num;
89
90 r = nftnl_rule_alloc();
91 if (r == NULL) {
92 perror("OOM");
93 exit(EXIT_FAILURE);
94 }
95
96 nftnl_rule_set_str(r, NFTNL_RULE_TABLE, table);
97 nftnl_rule_set_str(r, NFTNL_RULE_CHAIN, chain);
98 nftnl_rule_set_u32(r, NFTNL_RULE_FAMILY, family);
99
100 if (handle != NULL) {
101 handle_num = atoll(handle);
102 nftnl_rule_set_u64(r, NFTNL_RULE_POSITION, handle_num);
103 }
104
105 proto = IPPROTO_TCP;
106 add_payload(r, NFT_PAYLOAD_NETWORK_HEADER, NFT_REG_1,
107 offsetof(struct iphdr, protocol), sizeof(uint8_t));
108 add_cmp(r, NFT_REG_1, NFT_CMP_EQ, &proto, sizeof(uint8_t));
109
110 dport = htons(22);
111 add_payload(r, NFT_PAYLOAD_TRANSPORT_HEADER, NFT_REG_1,
112 offsetof(struct tcphdr, dest), sizeof(uint16_t));
113 add_cmp(r, NFT_REG_1, NFT_CMP_EQ, &dport, sizeof(uint16_t));
114
115 add_counter(r);
116
117 return r;
118}
119
120int main(int argc, char *argv[])
121{
122 struct mnl_socket *nl;
123 struct nftnl_rule *r;
124 struct nlmsghdr *nlh;
125 struct mnl_nlmsg_batch *batch;
126 uint8_t family;
127 char buf[MNL_SOCKET_BUFFER_SIZE];
128 uint32_t seq = time(NULL);
129 int ret;
130
131 if (argc < 4 || argc > 5) {
132 fprintf(stderr, "Usage: %s <family> <table> <chain>\n", argv[0]);
133 exit(EXIT_FAILURE);
134 }
135
136 if (strcmp(argv[1], "ip") == 0)
137 family = NFPROTO_IPV4;
138 else if (strcmp(argv[1], "ip6") == 0)
139 family = NFPROTO_IPV6;
140 else if (strcmp(argv[1], "inet") == 0)
141 family = NFPROTO_INET;
142 else {
143 fprintf(stderr, "Unknown family: ip, ip6, inet\n");
144 exit(EXIT_FAILURE);
145 }
146
147 if (argc != 5)
148 r = setup_rule(family, argv[2], argv[3], NULL);
149 else
150 r = setup_rule(family, argv[2], argv[3], argv[4]);
151
152 nl = mnl_socket_open(NETLINK_NETFILTER);
153 if (nl == NULL) {
154 perror("mnl_socket_open");
155 exit(EXIT_FAILURE);
156 }
157
158 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
159 perror("mnl_socket_bind");
160 exit(EXIT_FAILURE);
161 }
162
163 batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
164
165 nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
166 mnl_nlmsg_batch_next(batch);
167
168 nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
169 NFT_MSG_NEWRULE,
170 nftnl_rule_get_u32(r, NFTNL_RULE_FAMILY),
171 NLM_F_APPEND | NLM_F_CREATE | NLM_F_ACK,
172 seq++);
173 nftnl_rule_nlmsg_build_payload(nlh, r);
174 nftnl_rule_free(r);
175 mnl_nlmsg_batch_next(batch);
176
177 nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
178 mnl_nlmsg_batch_next(batch);
179
180 ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
181 mnl_nlmsg_batch_size(batch));
182 if (ret == -1) {
183 perror("mnl_socket_sendto");
184 exit(EXIT_FAILURE);
185 }
186
187 mnl_nlmsg_batch_stop(batch);
188
189 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
190 if (ret == -1) {
191 perror("mnl_socket_recvfrom");
192 exit(EXIT_FAILURE);
193 }
194
195 ret = mnl_cb_run(buf, ret, 0, mnl_socket_get_portid(nl), NULL, NULL);
196 if (ret < 0) {
197 perror("mnl_cb_run");
198 exit(EXIT_FAILURE);
199 }
200
201 mnl_socket_close(nl);
202
203 return EXIT_SUCCESS;
204}