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