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