libnftnl  1.0.6
nft-rule-del.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 
18 #include <linux/netfilter.h>
19 #include <linux/netfilter/nf_tables.h>
20 #include <linux/netfilter/nfnetlink.h>
21 
22 #include <libmnl/libmnl.h>
23 #include <libnftnl/rule.h>
24 
25 static void nftnl_mnl_batch_put(char *buf, uint16_t type, uint32_t seq)
26 {
27  struct nlmsghdr *nlh;
28  struct nfgenmsg *nfg;
29 
30  nlh = mnl_nlmsg_put_header(buf);
31  nlh->nlmsg_type = type;
32  nlh->nlmsg_flags = NLM_F_REQUEST;
33  nlh->nlmsg_seq = seq;
34 
35  nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
36  nfg->nfgen_family = AF_INET;
37  nfg->version = NFNETLINK_V0;
38  nfg->res_id = NFNL_SUBSYS_NFTABLES;
39 }
40 
41 int main(int argc, char *argv[])
42 {
43  struct mnl_socket *nl;
44  char buf[MNL_SOCKET_BUFFER_SIZE];
45  struct nlmsghdr *nlh;
46  struct mnl_nlmsg_batch *batch;
47  uint32_t portid, seq;
48  struct nftnl_rule *r = NULL;
49  int ret, family;
50 
51  if (argc < 4 || argc > 5) {
52  fprintf(stderr, "Usage: %s <family> <table> <chain> [<handle>]\n",
53  argv[0]);
54  exit(EXIT_FAILURE);
55  }
56 
57  r = nftnl_rule_alloc();
58  if (r == NULL) {
59  perror("OOM");
60  exit(EXIT_FAILURE);
61  }
62 
63  if (strcmp(argv[1], "ip") == 0)
64  family = NFPROTO_IPV4;
65  else if (strcmp(argv[1], "ip6") == 0)
66  family = NFPROTO_IPV6;
67  else if (strcmp(argv[1], "bridge") == 0)
68  family = NFPROTO_BRIDGE;
69  else if (strcmp(argv[1], "arp") == 0)
70  family = NFPROTO_ARP;
71  else {
72  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
73  exit(EXIT_FAILURE);
74  }
75 
76  seq = time(NULL);
77  nftnl_rule_set(r, NFTNL_RULE_TABLE, argv[2]);
78  nftnl_rule_set(r, NFTNL_RULE_CHAIN, argv[3]);
79 
80  /* If no handle is specified, delete all rules in the chain */
81  if (argc == 5)
82  nftnl_rule_set_u64(r, NFTNL_RULE_HANDLE, atoi(argv[4]));
83 
84  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
85 
86  nftnl_mnl_batch_put(mnl_nlmsg_batch_current(batch),
87  NFNL_MSG_BATCH_BEGIN, seq++);
88  mnl_nlmsg_batch_next(batch);
89 
90  nlh = nftnl_rule_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
91  NFT_MSG_DELRULE,
92  family,
93  NLM_F_ACK, seq++);
94 
95  nftnl_rule_nlmsg_build_payload(nlh, r);
96  nftnl_rule_free(r);
97  mnl_nlmsg_batch_next(batch);
98 
99  nftnl_mnl_batch_put(mnl_nlmsg_batch_current(batch), NFNL_MSG_BATCH_END,
100  seq++);
101  mnl_nlmsg_batch_next(batch);
102 
103  nl = mnl_socket_open(NETLINK_NETFILTER);
104  if (nl == NULL) {
105  perror("mnl_socket_open");
106  exit(EXIT_FAILURE);
107  }
108 
109  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
110  perror("mnl_socket_bind");
111  exit(EXIT_FAILURE);
112  }
113  portid = mnl_socket_get_portid(nl);
114 
115  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
116  mnl_nlmsg_batch_size(batch)) < 0) {
117  perror("mnl_socket_send");
118  exit(EXIT_FAILURE);
119  }
120 
121  mnl_nlmsg_batch_stop(batch);
122 
123  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
124  while (ret > 0) {
125  ret = mnl_cb_run(buf, ret, 0, portid, NULL, NULL);
126  if (ret <= 0)
127  break;
128  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
129  }
130  if (ret == -1) {
131  perror("error");
132  exit(EXIT_FAILURE);
133  }
134  mnl_socket_close(nl);
135 
136  return EXIT_SUCCESS;
137 }