libnetfilter_queue  1.0.5
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 void
24 nfq_send_verdict(int queue_num, uint32_t id)
25 {
26  char buf[MNL_SOCKET_BUFFER_SIZE];
27  struct nlmsghdr *nlh;
28  struct nlattr *nest;
29 
30  nlh = nfq_nlmsg_put(buf, NFQNL_MSG_VERDICT, queue_num);
31  nfq_nlmsg_verdict_put(nlh, id, NF_ACCEPT);
32 
33  /* example to set the connmark. First, start NFQA_CT section: */
34  nest = mnl_attr_nest_start(nlh, NFQA_CT);
35 
36  /* then, add the connmark attribute: */
37  mnl_attr_put_u32(nlh, CTA_MARK, htonl(42));
38  /* more conntrack attributes, e.g. CTA_LABELS could be set here */
39 
40  /* end conntrack section */
41  mnl_attr_nest_end(nlh, nest);
42 
43  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
44  perror("mnl_socket_send");
45  exit(EXIT_FAILURE);
46  }
47 }
48 
49 static int queue_cb(const struct nlmsghdr *nlh, void *data)
50 {
51  struct nfqnl_msg_packet_hdr *ph = NULL;
52  struct nlattr *attr[NFQA_MAX+1] = {};
53  uint32_t id = 0, skbinfo;
54  struct nfgenmsg *nfg;
55  uint16_t plen;
56 
57  if (nfq_nlmsg_parse(nlh, attr) < 0) {
58  perror("problems parsing");
59  return MNL_CB_ERROR;
60  }
61 
62  nfg = mnl_nlmsg_get_payload(nlh);
63 
64  if (attr[NFQA_PACKET_HDR] == NULL) {
65  fputs("metaheader not set\n", stderr);
66  return MNL_CB_ERROR;
67  }
68 
69  ph = mnl_attr_get_payload(attr[NFQA_PACKET_HDR]);
70 
71  plen = mnl_attr_get_payload_len(attr[NFQA_PAYLOAD]);
72  /* void *payload = mnl_attr_get_payload(attr[NFQA_PAYLOAD]); */
73 
74  skbinfo = attr[NFQA_SKB_INFO] ? ntohl(mnl_attr_get_u32(attr[NFQA_SKB_INFO])) : 0;
75 
76  if (attr[NFQA_CAP_LEN]) {
77  uint32_t orig_len = ntohl(mnl_attr_get_u32(attr[NFQA_CAP_LEN]));
78  if (orig_len != plen)
79  printf("truncated ");
80  }
81 
82  if (skbinfo & NFQA_SKB_GSO)
83  printf("GSO ");
84 
85  id = ntohl(ph->packet_id);
86  printf("packet received (id=%u hw=0x%04x hook=%u, payload len %u",
87  id, ntohs(ph->hw_protocol), ph->hook, plen);
88 
89  /*
90  * ip/tcp checksums are not yet valid, e.g. due to GRO/GSO.
91  * The application should behave as if the checksums are correct.
92  *
93  * If these packets are later forwarded/sent out, the checksums will
94  * be corrected by kernel/hardware.
95  */
96  if (skbinfo & NFQA_SKB_CSUMNOTREADY)
97  printf(", checksum not ready");
98  puts(")");
99 
100  nfq_send_verdict(ntohs(nfg->res_id), id);
101 
102  return MNL_CB_OK;
103 }
104 
105 int main(int argc, char *argv[])
106 {
107  char *buf;
108  /* largest possible packet payload, plus netlink data overhead: */
109  size_t sizeof_buf = 0xffff + (MNL_SOCKET_BUFFER_SIZE/2);
110  struct nlmsghdr *nlh;
111  int ret;
112  unsigned int portid, queue_num;
113 
114  if (argc != 2) {
115  printf("Usage: %s [queue_num]\n", argv[0]);
116  exit(EXIT_FAILURE);
117  }
118  queue_num = atoi(argv[1]);
119 
120  nl = mnl_socket_open(NETLINK_NETFILTER);
121  if (nl == NULL) {
122  perror("mnl_socket_open");
123  exit(EXIT_FAILURE);
124  }
125 
126  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
127  perror("mnl_socket_bind");
128  exit(EXIT_FAILURE);
129  }
130  portid = mnl_socket_get_portid(nl);
131 
132  buf = malloc(sizeof_buf);
133  if (!buf) {
134  perror("allocate receive buffer");
135  exit(EXIT_FAILURE);
136  }
137 
138  nlh = nfq_nlmsg_put(buf, NFQNL_MSG_CONFIG, queue_num);
139  nfq_nlmsg_cfg_put_cmd(nlh, AF_INET, NFQNL_CFG_CMD_BIND);
140 
141  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
142  perror("mnl_socket_send");
143  exit(EXIT_FAILURE);
144  }
145 
146  nlh = nfq_nlmsg_put(buf, NFQNL_MSG_CONFIG, queue_num);
147  nfq_nlmsg_cfg_put_params(nlh, NFQNL_COPY_PACKET, 0xffff);
148 
149  mnl_attr_put_u32(nlh, NFQA_CFG_FLAGS, htonl(NFQA_CFG_F_GSO));
150  mnl_attr_put_u32(nlh, NFQA_CFG_MASK, htonl(NFQA_CFG_F_GSO));
151 
152  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
153  perror("mnl_socket_send");
154  exit(EXIT_FAILURE);
155  }
156 
157  /* ENOBUFS is signalled to userspace when packets were lost
158  * on kernel side. In most cases, userspace isn't interested
159  * in this information, so turn it off.
160  */
161  ret = 1;
162  mnl_socket_setsockopt(nl, NETLINK_NO_ENOBUFS, &ret, sizeof(int));
163 
164  for (;;) {
165  ret = mnl_socket_recvfrom(nl, buf, sizeof_buf);
166  if (ret == -1) {
167  perror("mnl_socket_recvfrom");
168  exit(EXIT_FAILURE);
169  }
170 
171  ret = mnl_cb_run(buf, ret, 0, portid, queue_cb, NULL);
172  if (ret < 0){
173  perror("mnl_cb_run");
174  exit(EXIT_FAILURE);
175  }
176  }
177 
178  mnl_socket_close(nl);
179 
180  return 0;
181 }
void nfq_nlmsg_cfg_put_cmd(struct nlmsghdr *nlh, uint16_t pf, uint8_t cmd)
Definition: nlmsg.c:166
void nfq_nlmsg_verdict_put(struct nlmsghdr *nlh, int id, int verdict)
Definition: nlmsg.c:72
struct nlmsghdr * nfq_nlmsg_put(char *buf, int type, uint32_t queue_num)
Definition: nlmsg.c:283
int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr)
Definition: nlmsg.c:269
void nfq_nlmsg_cfg_put_params(struct nlmsghdr *nlh, uint8_t mode, int range)
Definition: nlmsg.c:182