libnl  3.2.14
queue_msg.c
1 /*
2  * lib/netfilter/queue_msg.c Netfilter Queue Messages
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2007, 2008 Patrick McHardy <kaber@trash.net>
10  * Copyright (c) 2010 Karl Hiramoto <karl@hiramoto.org>
11  */
12 
13 /**
14  * @ingroup nfnl
15  * @defgroup queue Queue
16  * @brief
17  * @{
18  */
19 
20 #include <sys/types.h>
21 #include <linux/netfilter/nfnetlink_queue.h>
22 
23 #include <netlink-local.h>
24 #include <netlink/attr.h>
25 #include <netlink/netfilter/nfnl.h>
26 #include <netlink/netfilter/queue_msg.h>
27 #include <byteswap.h>
28 
29 static struct nl_cache_ops nfnl_queue_msg_ops;
30 
31 #if __BYTE_ORDER == __BIG_ENDIAN
32 static uint64_t ntohll(uint64_t x)
33 {
34  return x;
35 }
36 #elif __BYTE_ORDER == __LITTLE_ENDIAN
37 static uint64_t ntohll(uint64_t x)
38 {
39  return bswap_64(x);
40 }
41 #endif
42 
43 static struct nla_policy queue_policy[NFQA_MAX+1] = {
44  [NFQA_PACKET_HDR] = {
45  .minlen = sizeof(struct nfqnl_msg_packet_hdr),
46  },
47  [NFQA_VERDICT_HDR] = {
48  .minlen = sizeof(struct nfqnl_msg_verdict_hdr),
49  },
50  [NFQA_MARK] = { .type = NLA_U32 },
51  [NFQA_TIMESTAMP] = {
52  .minlen = sizeof(struct nfqnl_msg_packet_timestamp),
53  },
54  [NFQA_IFINDEX_INDEV] = { .type = NLA_U32 },
55  [NFQA_IFINDEX_OUTDEV] = { .type = NLA_U32 },
56  [NFQA_IFINDEX_PHYSINDEV] = { .type = NLA_U32 },
57  [NFQA_IFINDEX_PHYSOUTDEV] = { .type = NLA_U32 },
58  [NFQA_HWADDR] = {
59  .minlen = sizeof(struct nfqnl_msg_packet_hw),
60  },
61 };
62 
63 int nfnlmsg_queue_msg_parse(struct nlmsghdr *nlh,
64  struct nfnl_queue_msg **result)
65 {
66  struct nfnl_queue_msg *msg;
67  struct nlattr *tb[NFQA_MAX+1];
68  struct nlattr *attr;
69  int err;
70 
71  msg = nfnl_queue_msg_alloc();
72  if (!msg)
73  return -NLE_NOMEM;
74 
75  msg->ce_msgtype = nlh->nlmsg_type;
76 
77  err = nlmsg_parse(nlh, sizeof(struct nfgenmsg), tb, NFQA_MAX,
78  queue_policy);
79  if (err < 0)
80  goto errout;
81 
82  nfnl_queue_msg_set_group(msg, nfnlmsg_res_id(nlh));
83  nfnl_queue_msg_set_family(msg, nfnlmsg_family(nlh));
84 
85  attr = tb[NFQA_PACKET_HDR];
86  if (attr) {
87  struct nfqnl_msg_packet_hdr *hdr = nla_data(attr);
88 
89  nfnl_queue_msg_set_packetid(msg, ntohl(hdr->packet_id));
90  if (hdr->hw_protocol)
91  nfnl_queue_msg_set_hwproto(msg, hdr->hw_protocol);
92  nfnl_queue_msg_set_hook(msg, hdr->hook);
93  }
94 
95  attr = tb[NFQA_MARK];
96  if (attr)
97  nfnl_queue_msg_set_mark(msg, ntohl(nla_get_u32(attr)));
98 
99  attr = tb[NFQA_TIMESTAMP];
100  if (attr) {
101  struct nfqnl_msg_packet_timestamp *timestamp = nla_data(attr);
102  struct timeval tv;
103 
104  tv.tv_sec = ntohll(timestamp->sec);
105  tv.tv_usec = ntohll(timestamp->usec);
106  nfnl_queue_msg_set_timestamp(msg, &tv);
107  }
108 
109  attr = tb[NFQA_IFINDEX_INDEV];
110  if (attr)
111  nfnl_queue_msg_set_indev(msg, ntohl(nla_get_u32(attr)));
112 
113  attr = tb[NFQA_IFINDEX_OUTDEV];
114  if (attr)
115  nfnl_queue_msg_set_outdev(msg, ntohl(nla_get_u32(attr)));
116 
117  attr = tb[NFQA_IFINDEX_PHYSINDEV];
118  if (attr)
119  nfnl_queue_msg_set_physindev(msg, ntohl(nla_get_u32(attr)));
120 
121  attr = tb[NFQA_IFINDEX_PHYSOUTDEV];
122  if (attr)
123  nfnl_queue_msg_set_physoutdev(msg, ntohl(nla_get_u32(attr)));
124 
125  attr = tb[NFQA_HWADDR];
126  if (attr) {
127  struct nfqnl_msg_packet_hw *hw = nla_data(attr);
128 
129  nfnl_queue_msg_set_hwaddr(msg, hw->hw_addr,
130  ntohs(hw->hw_addrlen));
131  }
132 
133  attr = tb[NFQA_PAYLOAD];
134  if (attr) {
135  err = nfnl_queue_msg_set_payload(msg, nla_data(attr),
136  nla_len(attr));
137  if (err < 0)
138  goto errout;
139  }
140 
141  *result = msg;
142  return 0;
143 
144 errout:
145  nfnl_queue_msg_put(msg);
146  return err;
147 }
148 
149 static int queue_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
150  struct nlmsghdr *nlh, struct nl_parser_param *pp)
151 {
152  struct nfnl_queue_msg *msg;
153  int err;
154 
155  if ((err = nfnlmsg_queue_msg_parse(nlh, &msg)) < 0)
156  goto errout;
157 
158  err = pp->pp_cb((struct nl_object *) msg, pp);
159 errout:
160  nfnl_queue_msg_put(msg);
161  return err;
162 }
163 
164 /** @} */
165 
166 struct nl_msg *nfnl_queue_msg_build_verdict(const struct nfnl_queue_msg *msg)
167 {
168  struct nl_msg *nlmsg;
169  struct nfqnl_msg_verdict_hdr verdict;
170 
171  nlmsg = nfnlmsg_alloc_simple(NFNL_SUBSYS_QUEUE, NFQNL_MSG_VERDICT, 0,
172  nfnl_queue_msg_get_family(msg),
173  nfnl_queue_msg_get_group(msg));
174  if (nlmsg == NULL)
175  return NULL;
176 
177  verdict.id = htonl(nfnl_queue_msg_get_packetid(msg));
178  verdict.verdict = htonl(nfnl_queue_msg_get_verdict(msg));
179  if (nla_put(nlmsg, NFQA_VERDICT_HDR, sizeof(verdict), &verdict) < 0)
180  goto nla_put_failure;
181 
182  if (nfnl_queue_msg_test_mark(msg) &&
183  nla_put_u32(nlmsg, NFQA_MARK,
184  ntohl(nfnl_queue_msg_get_mark(msg))) < 0)
185  goto nla_put_failure;
186 
187  return nlmsg;
188 
189 nla_put_failure:
190  nlmsg_free(nlmsg);
191  return NULL;
192 }
193 
194 /**
195 * Send a message verdict/mark
196 * @arg nlh netlink messsage header
197 * @arg msg queue msg
198 * @return 0 on OK or error code
199 */
200 int nfnl_queue_msg_send_verdict(struct nl_sock *nlh,
201  const struct nfnl_queue_msg *msg)
202 {
203  struct nl_msg *nlmsg;
204  int err;
205 
206  nlmsg = nfnl_queue_msg_build_verdict(msg);
207  if (nlmsg == NULL)
208  return -NLE_NOMEM;
209 
210  err = nl_send_auto_complete(nlh, nlmsg);
211  nlmsg_free(nlmsg);
212  if (err < 0)
213  return err;
214  return wait_for_ack(nlh);
215 }
216 
217 /**
218 * Send a message verdict including the payload
219 * @arg nlh netlink messsage header
220 * @arg msg queue msg
221 * @arg payload_data packet payload data
222 * @arg payload_len payload length
223 * @return 0 on OK or error code
224 */
225 int nfnl_queue_msg_send_verdict_payload(struct nl_sock *nlh,
226  const struct nfnl_queue_msg *msg,
227  const void *payload_data, unsigned payload_len)
228 {
229  struct nl_msg *nlmsg;
230  int err;
231  struct iovec iov[3];
232  struct nlattr nla;
233 
234  nlmsg = nfnl_queue_msg_build_verdict(msg);
235  if (nlmsg == NULL)
236  return -NLE_NOMEM;
237 
238  memset(iov, 0, sizeof(iov));
239 
240  iov[0].iov_base = (void *) nlmsg_hdr(nlmsg);
241  iov[0].iov_len = nlmsg_hdr(nlmsg)->nlmsg_len;
242 
243  nla.nla_type = NFQA_PAYLOAD;
244  nla.nla_len = payload_len + sizeof(nla);
245  nlmsg_hdr(nlmsg)->nlmsg_len += nla.nla_len;
246 
247  iov[1].iov_base = (void *) &nla;
248  iov[1].iov_len = sizeof(nla);
249 
250  iov[2].iov_base = (void *) payload_data;
251  iov[2].iov_len = NLA_ALIGN(payload_len);
252 
253  nl_complete_msg(nlh, nlmsg);
254  err = nl_send_iovec(nlh, nlmsg, iov, 3);
255 
256  nlmsg_free(nlmsg);
257  if (err < 0)
258  return err;
259  return wait_for_ack(nlh);
260 }
261 
262 #define NFNLMSG_QUEUE_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_QUEUE, (type))
263 static struct nl_cache_ops nfnl_queue_msg_ops = {
264  .co_name = "netfilter/queue_msg",
265  .co_hdrsize = NFNL_HDRLEN,
266  .co_msgtypes = {
267  { NFNLMSG_QUEUE_TYPE(NFQNL_MSG_PACKET), NL_ACT_NEW, "new" },
268  END_OF_MSGTYPES_LIST,
269  },
270  .co_protocol = NETLINK_NETFILTER,
271  .co_msg_parser = queue_msg_parser,
272  .co_obj_ops = &queue_msg_obj_ops,
273 };
274 
275 static void __init nfnl_msg_queue_init(void)
276 {
277  nl_cache_mngt_register(&nfnl_queue_msg_ops);
278 }
279 
280 static void __exit nfnl_queue_msg_exit(void)
281 {
282  nl_cache_mngt_unregister(&nfnl_queue_msg_ops);
283 }
284 
285 /** @} */