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