libnetfilter_queue  1.0.3
old.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <time.h>
6 #include <arpa/inet.h>
7 
8 #include <libmnl/libmnl.h>
9 #include <linux/netfilter.h>
10 #include <linux/netfilter/nfnetlink.h>
11 
12 #include <linux/types.h>
13 #include <linux/netfilter/nfnetlink_queue.h>
14 
15 #include <libnetfilter_queue/libnetfilter_queue.h>
16 
17 static struct mnl_socket *nl;
18 
19 static struct nlmsghdr *
20 nfq_build_header(char *buf, int type, uint32_t queue_num)
21 {
22  struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
23  nlh->nlmsg_type = (NFNL_SUBSYS_QUEUE << 8) | type;
24  nlh->nlmsg_flags = NLM_F_REQUEST;
25 
26  struct nfgenmsg *nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
27  nfg->nfgen_family = AF_UNSPEC;
28  nfg->version = NFNETLINK_V0;
29  nfg->res_id = htons(queue_num);
30 
31  return nlh;
32 }
33 
34 static int
35 nfq_send_verdict(int queue_num, uint32_t id, uint8_t *pkt, uint16_t pktlen)
36 {
37  char buf[MNL_SOCKET_BUFFER_SIZE];
38  struct nlmsghdr *nlh;
39  int ret;
40 
41  nlh = nfq_build_header(buf, NFQNL_MSG_VERDICT, queue_num);
42  nfq_nlmsg_verdict_build(nlh, id, NF_ACCEPT);
43  nfq_nlmsg_verdict_payload(nlh, pkt, pktlen);
44 
45  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
46  perror("mnl_socket_send");
47  exit(EXIT_FAILURE);
48  }
49 
50  return ret;
51 }
52 
53 static int queue_cb(const struct nlmsghdr *nlh, void *data)
54 {
55  struct nfqnl_msg_packet_hdr *ph = NULL;
56  struct nlattr *attr[NFQA_MAX+1];
57  uint32_t id = 0;
58  struct nfgenmsg *nfg;
59  uint8_t *pkt;
60  uint16_t pktlen;
61 
62  if (nfq_nlmsg_parse(nlh, attr) < 0) {
63  perror("problems parsing");
64  return MNL_CB_ERROR;
65  }
66 
67  nfg = mnl_nlmsg_get_payload(nlh);
68 
69  ph = (struct nfqnl_msg_packet_hdr *)
70  mnl_attr_get_payload(attr[NFQA_PACKET_HDR]);
71  if (ph == NULL) {
72  perror("problems retrieving metaheader");
73  return MNL_CB_ERROR;
74  }
75 
76  id = ntohl(ph->packet_id);
77 
78  printf("packet received (id=%u hw=0x%04x hook=%u)\n",
79  id, ntohs(ph->hw_protocol), ph->hook);
80 
81  pkt = mnl_attr_get_payload(attr[NFQA_PAYLOAD]);
82  pktlen = mnl_attr_get_payload_len(attr[NFQA_PAYLOAD]);
83 
84  nfq_send_verdict(ntohs(nfg->res_id), id, pkt, pktlen);
85 
86  return MNL_CB_OK;
87 }
88 
89 int main(int argc, char *argv[])
90 {
91  char buf[MNL_SOCKET_BUFFER_SIZE];
92  struct nlmsghdr *nlh;
93  int ret;
94  unsigned int portid, queue_num;
95 
96  if (argc != 2) {
97  printf("Usage: %s [queue_num]\n", argv[0]);
98  exit(EXIT_FAILURE);
99  }
100  queue_num = atoi(argv[1]);
101 
102  nl = mnl_socket_open(NETLINK_NETFILTER);
103  if (nl == NULL) {
104  perror("mnl_socket_open");
105  exit(EXIT_FAILURE);
106  }
107 
108  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
109  perror("mnl_socket_bind");
110  exit(EXIT_FAILURE);
111  }
112  portid = mnl_socket_get_portid(nl);
113 
114  nlh = nfq_build_header(buf, NFQNL_MSG_CONFIG, 0);
115  nfq_nlmsg_cfg_build_request(nlh, AF_INET, NFQNL_CFG_CMD_PF_UNBIND);
116 
117  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
118  perror("mnl_socket_send");
119  exit(EXIT_FAILURE);
120  }
121 
122  nlh = nfq_build_header(buf, NFQNL_MSG_CONFIG, 0);
123  nfq_nlmsg_cfg_build_request(nlh, AF_INET, NFQNL_CFG_CMD_PF_BIND);
124 
125  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
126  perror("mnl_socket_send");
127  exit(EXIT_FAILURE);
128  }
129 
130  nlh = nfq_build_header(buf, NFQNL_MSG_CONFIG, queue_num);
131  nfq_nlmsg_cfg_build_request(nlh, AF_INET, NFQNL_CFG_CMD_BIND);
132 
133  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
134  perror("mnl_socket_send");
135  exit(EXIT_FAILURE);
136  }
137 
138  nlh = nfq_build_header(buf, NFQNL_MSG_CONFIG, queue_num);
139  nfq_nlmsg_cfg_add_copy(nlh, NFQNL_COPY_PACKET, 0xffff);
140 
141  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
142  perror("mnl_socket_send");
143  exit(EXIT_FAILURE);
144  }
145 
146  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
147  if (ret == -1) {
148  perror("mnl_socket_recvfrom");
149  exit(EXIT_FAILURE);
150  }
151  while (ret > 0) {
152  uint32_t id;
153 
154  ret = mnl_cb_run(buf, ret, 0, portid, queue_cb, NULL);
155  if (ret < 0){
156  perror("mnl_cb_run");
157  exit(EXIT_FAILURE);
158  }
159 
160  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
161  if (ret == -1) {
162  perror("mnl_socket_recvfrom");
163  exit(EXIT_FAILURE);
164  }
165  }
166 
167  mnl_socket_close(nl);
168 
169  return 0;
170 }
int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr)
Definition: nlmsg.c:182