libnftnl  1.1.5
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 
71  if (argc != 4 && argc != 6) {
72  fprintf(stderr, "Usage: %s <family> <table> <chain> "
73  "[<hooknum> <prio>]\n",
74  argv[0]);
75  exit(EXIT_FAILURE);
76  }
77 
78  if (strcmp(argv[1], "ip") == 0)
79  family = NFPROTO_IPV4;
80  else if (strcmp(argv[1], "ip6") == 0)
81  family = NFPROTO_IPV6;
82  else if (strcmp(argv[1], "bridge") == 0)
83  family = NFPROTO_BRIDGE;
84  else if (strcmp(argv[1], "arp") == 0)
85  family = NFPROTO_ARP;
86  else {
87  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
88  exit(EXIT_FAILURE);
89  }
90 
91  t = chain_add_parse(argc, argv);
92  if (t == NULL)
93  exit(EXIT_FAILURE);
94 
95  seq = time(NULL);
96  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
97 
98  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
99  mnl_nlmsg_batch_next(batch);
100 
101  chain_seq = seq;
102  nlh = nftnl_chain_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
103  NFT_MSG_NEWCHAIN, family,
104  NLM_F_CREATE|NLM_F_ACK, seq++);
105  nftnl_chain_nlmsg_build_payload(nlh, t);
106  nftnl_chain_free(t);
107  mnl_nlmsg_batch_next(batch);
108 
109  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
110  mnl_nlmsg_batch_next(batch);
111 
112  nl = mnl_socket_open(NETLINK_NETFILTER);
113  if (nl == NULL) {
114  perror("mnl_socket_open");
115  exit(EXIT_FAILURE);
116  }
117 
118  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
119  perror("mnl_socket_bind");
120  exit(EXIT_FAILURE);
121  }
122  portid = mnl_socket_get_portid(nl);
123 
124  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
125  mnl_nlmsg_batch_size(batch)) < 0) {
126  perror("mnl_socket_send");
127  exit(EXIT_FAILURE);
128  }
129 
130  mnl_nlmsg_batch_stop(batch);
131 
132  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
133  while (ret > 0) {
134  ret = mnl_cb_run(buf, ret, chain_seq, portid, NULL, NULL);
135  if (ret <= 0)
136  break;
137  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
138  }
139  if (ret == -1) {
140  perror("error");
141  exit(EXIT_FAILURE);
142  }
143  mnl_socket_close(nl);
144 
145  return EXIT_SUCCESS;
146 }