libnetfilter_queue  1.0.3
nlmsg.c
1 /*
2  * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  */
9 #include <arpa/inet.h>
10 #include <time.h>
11 #include <endian.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include <libmnl/libmnl.h>
16 
17 #ifndef __aligned_be64
18 #define __aligned_be64 __be64 __attribute__((aligned(8)))
19 #define __aligned_le64 __le64 __attribute__((aligned(8)))
20 #endif
21 
22 #include <linux/netfilter/nfnetlink_queue.h>
23 
24 #include <libnetfilter_queue/libnetfilter_queue.h>
25 
26 #include "internal.h"
27 
33 void nfq_nlmsg_verdict_put(struct nlmsghdr *nlh, int id, int verdict)
34 {
35  struct nfqnl_msg_verdict_hdr vh = {
36  .verdict = htonl(verdict),
37  .id = htonl(id),
38  };
39  mnl_attr_put(nlh, NFQA_VERDICT_HDR, sizeof(vh), &vh);
40 }
41 EXPORT_SYMBOL(nfq_nlmsg_verdict_put);
42 
43 void nfq_nlmsg_verdict_put_mark(struct nlmsghdr *nlh, uint32_t mark)
44 {
45  mnl_attr_put_u32(nlh, NFQA_MARK, htonl(mark));
46 }
47 EXPORT_SYMBOL(nfq_nlmsg_verdict_put_mark);
48 
49 void
50 nfq_nlmsg_verdict_put_pkt(struct nlmsghdr *nlh, const void *pkt, uint32_t plen)
51 {
52  mnl_attr_put(nlh, NFQA_PAYLOAD, plen, pkt);
53 }
54 EXPORT_SYMBOL(nfq_nlmsg_verdict_put_pkt);
55 
88 void nfq_nlmsg_cfg_put_cmd(struct nlmsghdr *nlh, uint16_t pf, uint8_t cmd)
89 {
90  struct nfqnl_msg_config_cmd command = {
91  .command = cmd,
92  .pf = htons(pf),
93  };
94  mnl_attr_put(nlh, NFQA_CFG_CMD, sizeof(command), &command);
95 }
96 EXPORT_SYMBOL(nfq_nlmsg_cfg_put_cmd);
97 
98 void nfq_nlmsg_cfg_put_params(struct nlmsghdr *nlh, uint8_t mode, int range)
99 {
100  struct nfqnl_msg_config_params params = {
101  .copy_range = htonl(range),
102  .copy_mode = mode,
103  };
104  mnl_attr_put(nlh, NFQA_CFG_PARAMS, sizeof(params), &params);
105 }
106 EXPORT_SYMBOL(nfq_nlmsg_cfg_put_params);
107 
108 void nfq_nlmsg_cfg_put_qmaxlen(struct nlmsghdr *nlh, uint32_t queue_maxlen)
109 {
110  mnl_attr_put_u32(nlh, NFQA_CFG_QUEUE_MAXLEN, htonl(queue_maxlen));
111 }
112 EXPORT_SYMBOL(nfq_nlmsg_cfg_put_qmaxlen);
113 
123 static int nfq_pkt_parse_attr_cb(const struct nlattr *attr, void *data)
124 {
125  const struct nlattr **tb = data;
126  int type = mnl_attr_get_type(attr);
127 
128  /* skip unsupported attribute in user-space */
129  if (mnl_attr_type_valid(attr, NFQA_MAX) < 0)
130  return MNL_CB_OK;
131 
132  switch(type) {
133  case NFQA_MARK:
134  case NFQA_IFINDEX_INDEV:
135  case NFQA_IFINDEX_OUTDEV:
136  case NFQA_IFINDEX_PHYSINDEV:
137  case NFQA_IFINDEX_PHYSOUTDEV:
138  case NFQA_CAP_LEN:
139  case NFQA_SKB_INFO:
140  case NFQA_SECCTX:
141  case NFQA_UID:
142  case NFQA_GID:
143  case NFQA_CT_INFO:
144  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
145  return MNL_CB_ERROR;
146  break;
147  case NFQA_TIMESTAMP:
148  if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC,
149  sizeof(struct nfqnl_msg_packet_timestamp)) < 0) {
150  return MNL_CB_ERROR;
151  }
152  break;
153  case NFQA_HWADDR:
154  if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC,
155  sizeof(struct nfqnl_msg_packet_hw)) < 0) {
156  return MNL_CB_ERROR;
157  }
158  break;
159  case NFQA_PACKET_HDR:
160  if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC,
161  sizeof(struct nfqnl_msg_packet_hdr)) < 0) {
162  return MNL_CB_ERROR;
163  }
164  break;
165  case NFQA_PAYLOAD:
166  case NFQA_CT:
167  case NFQA_EXP:
168  break;
169  }
170  tb[type] = attr;
171  return MNL_CB_OK;
172 }
173 
182 int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr)
183 {
184  return mnl_attr_parse(nlh, sizeof(struct nfgenmsg),
185  nfq_pkt_parse_attr_cb, attr);
186 }
187 EXPORT_SYMBOL(nfq_nlmsg_parse);
188 
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