libnetfilter_queue  1.0.3
nf-queue.c
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <time.h>
7 #include <arpa/inet.h>
8 
9 #include <libmnl/libmnl.h>
10 #include <linux/netfilter.h>
11 #include <linux/netfilter/nfnetlink.h>
12 
13 #include <linux/types.h>
14 #include <linux/netfilter/nfnetlink_queue.h>
15 
16 #include <libnetfilter_queue/libnetfilter_queue.h>
17 
18 /* only for NFQA_CT, not needed otherwise: */
19 #include <linux/netfilter/nfnetlink_conntrack.h>
20 
21 static struct mnl_socket *nl;
22 
23 static struct nlmsghdr *
24 nfq_hdr_put(char *buf, int type, uint32_t queue_num)
25 {
26  struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
27  nlh->nlmsg_type = (NFNL_SUBSYS_QUEUE << 8) | type;
28  nlh->nlmsg_flags = NLM_F_REQUEST;
29 
30  struct nfgenmsg *nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
31  nfg->nfgen_family = AF_UNSPEC;
32  nfg->version = NFNETLINK_V0;
33  nfg->res_id = htons(queue_num);
34 
35  return nlh;
36 }
37 
38 static void
39 nfq_send_verdict(int queue_num, uint32_t id)
40 {
41  char buf[MNL_SOCKET_BUFFER_SIZE];
42  struct nlmsghdr *nlh;
43  struct nlattr *nest;
44 
45  nlh = nfq_hdr_put(buf, NFQNL_MSG_VERDICT, queue_num);
46  nfq_nlmsg_verdict_put(nlh, id, NF_ACCEPT);
47 
48  /* example to set the connmark. First, start NFQA_CT section: */
49  nest = mnl_attr_nest_start(nlh, NFQA_CT);
50 
51  /* then, add the connmark attribute: */
52  mnl_attr_put_u32(nlh, CTA_MARK, htonl(42));
53  /* more conntrack attributes, e.g. CTA_LABEL, could be set here */
54 
55  /* end conntrack section */
56  mnl_attr_nest_end(nlh, nest);
57 
58  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
59  perror("mnl_socket_send");
60  exit(EXIT_FAILURE);
61  }
62 }
63 
64 static int queue_cb(const struct nlmsghdr *nlh, void *data)
65 {
66  struct nfqnl_msg_packet_hdr *ph = NULL;
67  struct nlattr *attr[NFQA_MAX+1] = {};
68  uint32_t id = 0, skbinfo;
69  struct nfgenmsg *nfg;
70  uint16_t plen;
71 
72  if (nfq_nlmsg_parse(nlh, attr) < 0) {
73  perror("problems parsing");
74  return MNL_CB_ERROR;
75  }
76 
77  nfg = mnl_nlmsg_get_payload(nlh);
78 
79  if (attr[NFQA_PACKET_HDR] == NULL) {
80  fputs("metaheader not set\n", stderr);
81  return MNL_CB_ERROR;
82  }
83 
84  ph = mnl_attr_get_payload(attr[NFQA_PACKET_HDR]);
85 
86  plen = mnl_attr_get_payload_len(attr[NFQA_PAYLOAD]);
87  /* void *payload = mnl_attr_get_payload(attr[NFQA_PAYLOAD]); */
88 
89  skbinfo = attr[NFQA_SKB_INFO] ? ntohl(mnl_attr_get_u32(attr[NFQA_SKB_INFO])) : 0;
90 
91  if (attr[NFQA_CAP_LEN]) {
92  uint32_t orig_len = ntohl(mnl_attr_get_u32(attr[NFQA_CAP_LEN]));
93  if (orig_len != plen)
94  printf("truncated ");
95  }
96 
97  if (skbinfo & NFQA_SKB_GSO)
98  printf("GSO ");
99 
100  id = ntohl(ph->packet_id);
101  printf("packet received (id=%u hw=0x%04x hook=%u, payload len %u",
102  id, ntohs(ph->hw_protocol), ph->hook, plen);
103 
104  /*
105  * ip/tcp checksums are not yet valid, e.g. due to GRO/GSO.
106  * The application should behave as if the checksums are correct.
107  *
108  * If these packets are later forwarded/sent out, the checksums will
109  * be corrected by kernel/hardware.
110  */
111  if (skbinfo & NFQA_SKB_CSUMNOTREADY)
112  printf(", checksum not ready");
113  puts(")");
114 
115  nfq_send_verdict(ntohs(nfg->res_id), id);
116 
117  return MNL_CB_OK;
118 }
119 
120 int main(int argc, char *argv[])
121 {
122  char *buf;
123  /* largest possible packet payload, plus netlink data overhead: */
124  size_t sizeof_buf = 0xffff + (MNL_SOCKET_BUFFER_SIZE/2);
125  struct nlmsghdr *nlh;
126  int ret;
127  unsigned int portid, queue_num;
128 
129  if (argc != 2) {
130  printf("Usage: %s [queue_num]\n", argv[0]);
131  exit(EXIT_FAILURE);
132  }
133  queue_num = atoi(argv[1]);
134 
135  nl = mnl_socket_open(NETLINK_NETFILTER);
136  if (nl == NULL) {
137  perror("mnl_socket_open");
138  exit(EXIT_FAILURE);
139  }
140 
141  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
142  perror("mnl_socket_bind");
143  exit(EXIT_FAILURE);
144  }
145  portid = mnl_socket_get_portid(nl);
146 
147  buf = malloc(sizeof_buf);
148  if (!buf) {
149  perror("allocate receive buffer");
150  exit(EXIT_FAILURE);
151  }
152 
153  /* PF_(UN)BIND is not needed with kernels 3.8 and later */
154  nlh = nfq_hdr_put(buf, NFQNL_MSG_CONFIG, 0);
155  nfq_nlmsg_cfg_put_cmd(nlh, AF_INET, NFQNL_CFG_CMD_PF_UNBIND);
156 
157  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
158  perror("mnl_socket_send");
159  exit(EXIT_FAILURE);
160  }
161 
162  nlh = nfq_hdr_put(buf, NFQNL_MSG_CONFIG, 0);
163  nfq_nlmsg_cfg_put_cmd(nlh, AF_INET, NFQNL_CFG_CMD_PF_BIND);
164 
165  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
166  perror("mnl_socket_send");
167  exit(EXIT_FAILURE);
168  }
169 
170  nlh = nfq_hdr_put(buf, NFQNL_MSG_CONFIG, queue_num);
171  nfq_nlmsg_cfg_put_cmd(nlh, AF_INET, NFQNL_CFG_CMD_BIND);
172 
173  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
174  perror("mnl_socket_send");
175  exit(EXIT_FAILURE);
176  }
177 
178  nlh = nfq_hdr_put(buf, NFQNL_MSG_CONFIG, queue_num);
179  nfq_nlmsg_cfg_put_params(nlh, NFQNL_COPY_PACKET, 0xffff);
180 
181  mnl_attr_put_u32(nlh, NFQA_CFG_FLAGS, htonl(NFQA_CFG_F_GSO));
182  mnl_attr_put_u32(nlh, NFQA_CFG_MASK, htonl(NFQA_CFG_F_GSO));
183 
184  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
185  perror("mnl_socket_send");
186  exit(EXIT_FAILURE);
187  }
188 
189  /* ENOBUFS is signalled to userspace when packets were lost
190  * on kernel side. In most cases, userspace isn't interested
191  * in this information, so turn it off.
192  */
193  ret = 1;
194  mnl_socket_setsockopt(nl, NETLINK_NO_ENOBUFS, &ret, sizeof(int));
195 
196  for (;;) {
197  ret = mnl_socket_recvfrom(nl, buf, sizeof_buf);
198  if (ret == -1) {
199  perror("mnl_socket_recvfrom");
200  exit(EXIT_FAILURE);
201  }
202 
203  ret = mnl_cb_run(buf, ret, 0, portid, queue_cb, NULL);
204  if (ret < 0){
205  perror("mnl_cb_run");
206  exit(EXIT_FAILURE);
207  }
208  }
209 
210  mnl_socket_close(nl);
211 
212  return 0;
213 }
void nfq_nlmsg_cfg_put_cmd(struct nlmsghdr *nlh, uint16_t pf, uint8_t cmd)
Definition: nlmsg.c:88
int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr)
Definition: nlmsg.c:182