libnftnl  1.1.4
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 
32 static 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 
51 static 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 
69 static 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 
82 static 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(r, NFTNL_RULE_TABLE, table);
97  nftnl_rule_set(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 
120 int 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 {
141  fprintf(stderr, "Unknown family: ip, ip6\n");
142  exit(EXIT_FAILURE);
143  }
144 
145  if (argc != 5)
146  r = setup_rule(family, argv[2], argv[3], NULL);
147  else
148  r = setup_rule(family, argv[2], argv[3], argv[4]);
149 
150  nl = mnl_socket_open(NETLINK_NETFILTER);
151  if (nl == NULL) {
152  perror("mnl_socket_open");
153  exit(EXIT_FAILURE);
154  }
155 
156  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
157  perror("mnl_socket_bind");
158  exit(EXIT_FAILURE);
159  }
160 
161  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
162 
163  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
164  mnl_nlmsg_batch_next(batch);
165 
166  nlh = nftnl_rule_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
167  NFT_MSG_NEWRULE,
168  nftnl_rule_get_u32(r, NFTNL_RULE_FAMILY),
169  NLM_F_APPEND|NLM_F_CREATE|NLM_F_ACK, seq++);
170 
171  nftnl_rule_nlmsg_build_payload(nlh, r);
172  nftnl_rule_free(r);
173  mnl_nlmsg_batch_next(batch);
174 
175  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
176  mnl_nlmsg_batch_next(batch);
177 
178  ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
179  mnl_nlmsg_batch_size(batch));
180  if (ret == -1) {
181  perror("mnl_socket_sendto");
182  exit(EXIT_FAILURE);
183  }
184 
185  mnl_nlmsg_batch_stop(batch);
186 
187  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
188  if (ret == -1) {
189  perror("mnl_socket_recvfrom");
190  exit(EXIT_FAILURE);
191  }
192 
193  ret = mnl_cb_run(buf, ret, 0, mnl_socket_get_portid(nl), NULL, NULL);
194  if (ret < 0) {
195  perror("mnl_cb_run");
196  exit(EXIT_FAILURE);
197  }
198 
199  mnl_socket_close(nl);
200 
201  return EXIT_SUCCESS;
202 }