libnftnl  1.0.8
nft-chain-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 <netinet/in.h>
16 
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/nf_tables.h>
19 
20 #include <libmnl/libmnl.h>
21 #include <libnftnl/chain.h>
22 
23 static struct nftnl_chain *chain_add_parse(int argc, char *argv[])
24 {
25  struct nftnl_chain *t;
26  int hooknum = 0;
27 
28  if (argc == 6) {
29  /* This is a base chain, set the hook number */
30  if (strcmp(argv[4], "NF_INET_LOCAL_IN") == 0)
31  hooknum = NF_INET_LOCAL_IN;
32  else if (strcmp(argv[4], "NF_INET_LOCAL_OUT") == 0)
33  hooknum = NF_INET_LOCAL_OUT;
34  else if (strcmp(argv[4], "NF_INET_PRE_ROUTING") == 0)
35  hooknum = NF_INET_PRE_ROUTING;
36  else if (strcmp(argv[4], "NF_INET_POST_ROUTING") == 0)
37  hooknum = NF_INET_POST_ROUTING;
38  else if (strcmp(argv[4], "NF_INET_FORWARD") == 0)
39  hooknum = NF_INET_FORWARD;
40  else {
41  fprintf(stderr, "Unknown hook: %s\n", argv[4]);
42  return NULL;
43  }
44  }
45 
46  t = nftnl_chain_alloc();
47  if (t == NULL) {
48  perror("OOM");
49  return NULL;
50  }
51  nftnl_chain_set(t, NFTNL_CHAIN_TABLE, argv[2]);
52  nftnl_chain_set(t, NFTNL_CHAIN_NAME, argv[3]);
53  if (argc == 6) {
54  nftnl_chain_set_u32(t, NFTNL_CHAIN_HOOKNUM, hooknum);
55  nftnl_chain_set_u32(t, NFTNL_CHAIN_PRIO, atoi(argv[5]));
56  }
57 
58  return t;
59 }
60 
61 int main(int argc, char *argv[])
62 {
63  struct mnl_socket *nl;
64  char buf[MNL_SOCKET_BUFFER_SIZE];
65  struct nlmsghdr *nlh;
66  uint32_t portid, seq, chain_seq;
67  int ret, family;
68  struct nftnl_chain *t;
69  struct mnl_nlmsg_batch *batch;
70  int batching;
71 
72  if (argc != 4 && argc != 6) {
73  fprintf(stderr, "Usage: %s <family> <table> <chain> "
74  "[<hooknum> <prio>]\n",
75  argv[0]);
76  exit(EXIT_FAILURE);
77  }
78 
79  if (strcmp(argv[1], "ip") == 0)
80  family = NFPROTO_IPV4;
81  else if (strcmp(argv[1], "ip6") == 0)
82  family = NFPROTO_IPV6;
83  else if (strcmp(argv[1], "bridge") == 0)
84  family = NFPROTO_BRIDGE;
85  else if (strcmp(argv[1], "arp") == 0)
86  family = NFPROTO_ARP;
87  else {
88  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
89  exit(EXIT_FAILURE);
90  }
91 
92  t = chain_add_parse(argc, argv);
93  if (t == NULL)
94  exit(EXIT_FAILURE);
95 
96  batching = nftnl_batch_is_supported();
97  if (batching < 0) {
98  perror("cannot talk to nfnetlink");
99  exit(EXIT_FAILURE);
100  }
101 
102  seq = time(NULL);
103  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
104 
105  if (batching) {
106  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
107  mnl_nlmsg_batch_next(batch);
108  }
109 
110  chain_seq = seq;
111  nlh = nftnl_chain_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
112  NFT_MSG_NEWCHAIN, family,
113  NLM_F_CREATE|NLM_F_ACK, seq++);
114  nftnl_chain_nlmsg_build_payload(nlh, t);
115  nftnl_chain_free(t);
116  mnl_nlmsg_batch_next(batch);
117 
118  if (batching) {
119  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
120  mnl_nlmsg_batch_next(batch);
121  }
122 
123  nl = mnl_socket_open(NETLINK_NETFILTER);
124  if (nl == NULL) {
125  perror("mnl_socket_open");
126  exit(EXIT_FAILURE);
127  }
128 
129  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
130  perror("mnl_socket_bind");
131  exit(EXIT_FAILURE);
132  }
133  portid = mnl_socket_get_portid(nl);
134 
135  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
136  mnl_nlmsg_batch_size(batch)) < 0) {
137  perror("mnl_socket_send");
138  exit(EXIT_FAILURE);
139  }
140 
141  mnl_nlmsg_batch_stop(batch);
142 
143  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
144  while (ret > 0) {
145  ret = mnl_cb_run(buf, ret, chain_seq, portid, NULL, NULL);
146  if (ret <= 0)
147  break;
148  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
149  }
150  if (ret == -1) {
151  perror("error");
152  exit(EXIT_FAILURE);
153  }
154  mnl_socket_close(nl);
155 
156  return EXIT_SUCCESS;
157 }