libnftnl  1.0.6
reject.c
1 /*
2  * (C) 2013 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  * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
10  */
11 
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <string.h>
15 #include <arpa/inet.h>
16 #include <errno.h>
17 #include <linux/netfilter/nf_tables.h>
18 
19 #include "internal.h"
20 #include <libmnl/libmnl.h>
21 #include <libnftnl/expr.h>
22 #include <libnftnl/rule.h>
23 
25  uint32_t type;
26  uint8_t icmp_code;
27 };
28 
29 static int nftnl_expr_reject_set(struct nftnl_expr *e, uint16_t type,
30  const void *data, uint32_t data_len)
31 {
32  struct nftnl_expr_reject *reject = nftnl_expr_data(e);
33 
34  switch(type) {
35  case NFTNL_EXPR_REJECT_TYPE:
36  reject->type = *((uint32_t *)data);
37  break;
38  case NFTNL_EXPR_REJECT_CODE:
39  reject->icmp_code = *((uint8_t *)data);
40  break;
41  default:
42  return -1;
43  }
44  return 0;
45 }
46 
47 static const void *
48 nftnl_expr_reject_get(const struct nftnl_expr *e, uint16_t type,
49  uint32_t *data_len)
50 {
51  struct nftnl_expr_reject *reject = nftnl_expr_data(e);
52 
53  switch(type) {
54  case NFTNL_EXPR_REJECT_TYPE:
55  *data_len = sizeof(reject->type);
56  return &reject->type;
57  case NFTNL_EXPR_REJECT_CODE:
58  *data_len = sizeof(reject->icmp_code);
59  return &reject->icmp_code;
60  }
61  return NULL;
62 }
63 
64 static int nftnl_expr_reject_cb(const struct nlattr *attr, void *data)
65 {
66  const struct nlattr **tb = data;
67  int type = mnl_attr_get_type(attr);
68 
69  if (mnl_attr_type_valid(attr, NFTA_REJECT_MAX) < 0)
70  return MNL_CB_OK;
71 
72  switch(type) {
73  case NFTA_REJECT_TYPE:
74  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
75  abi_breakage();
76  break;
77  case NFTA_REJECT_ICMP_CODE:
78  if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
79  abi_breakage();
80  break;
81  }
82 
83  tb[type] = attr;
84  return MNL_CB_OK;
85 }
86 
87 static void
88 nftnl_expr_reject_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
89 {
90  struct nftnl_expr_reject *reject = nftnl_expr_data(e);
91 
92  if (e->flags & (1 << NFTNL_EXPR_REJECT_TYPE))
93  mnl_attr_put_u32(nlh, NFTA_REJECT_TYPE, htonl(reject->type));
94  if (e->flags & (1 << NFTNL_EXPR_REJECT_CODE))
95  mnl_attr_put_u8(nlh, NFTA_REJECT_ICMP_CODE, reject->icmp_code);
96 }
97 
98 static int
99 nftnl_expr_reject_parse(struct nftnl_expr *e, struct nlattr *attr)
100 {
101  struct nftnl_expr_reject *reject = nftnl_expr_data(e);
102  struct nlattr *tb[NFTA_REJECT_MAX+1] = {};
103 
104  if (mnl_attr_parse_nested(attr, nftnl_expr_reject_cb, tb) < 0)
105  return -1;
106 
107  if (tb[NFTA_REJECT_TYPE]) {
108  reject->type = ntohl(mnl_attr_get_u32(tb[NFTA_REJECT_TYPE]));
109  e->flags |= (1 << NFTNL_EXPR_REJECT_TYPE);
110  }
111  if (tb[NFTA_REJECT_ICMP_CODE]) {
112  reject->icmp_code = mnl_attr_get_u8(tb[NFTA_REJECT_ICMP_CODE]);
113  e->flags |= (1 << NFTNL_EXPR_REJECT_CODE);
114  }
115 
116  return 0;
117 }
118 
119 static int
120 nftnl_expr_reject_json_parse(struct nftnl_expr *e, json_t *root,
121  struct nftnl_parse_err *err)
122 {
123 #ifdef JSON_PARSING
124  uint32_t type;
125  uint8_t code;
126 
127  if (nftnl_jansson_parse_val(root, "type", NFTNL_TYPE_U32, &type, err) == 0)
128  nftnl_expr_set_u32(e, NFTNL_EXPR_REJECT_TYPE, type);
129 
130  if (nftnl_jansson_parse_val(root, "code", NFTNL_TYPE_U8, &code, err) == 0)
131  nftnl_expr_set_u8(e, NFTNL_EXPR_REJECT_CODE, code);
132 
133  return 0;
134 #else
135  errno = EOPNOTSUPP;
136  return -1;
137 #endif
138 }
139 
140 static int
141 nftnl_expr_reject_xml_parse(struct nftnl_expr *e, mxml_node_t *tree,
142  struct nftnl_parse_err *err)
143 {
144 #ifdef XML_PARSING
145  uint32_t type;
146  uint8_t code;
147 
148  if (nftnl_mxml_num_parse(tree, "type", MXML_DESCEND_FIRST, BASE_DEC,
149  &type, NFTNL_TYPE_U32, NFTNL_XML_MAND, err) == 0)
150  nftnl_expr_set_u32(e, NFTNL_EXPR_REJECT_TYPE, type);
151 
152  if (nftnl_mxml_num_parse(tree, "code", MXML_DESCEND_FIRST, BASE_DEC,
153  &code, NFTNL_TYPE_U8, NFTNL_XML_MAND, err) == 0)
154  nftnl_expr_set_u8(e, NFTNL_EXPR_REJECT_CODE, code);
155 
156  return 0;
157 #else
158  errno = EOPNOTSUPP;
159  return -1;
160 #endif
161 }
162 
163 static int nftnl_expr_reject_snprintf_default(char *buf, size_t len,
164  const struct nftnl_expr *e)
165 {
166  struct nftnl_expr_reject *reject = nftnl_expr_data(e);
167 
168  return snprintf(buf, len, "type %u code %u ",
169  reject->type, reject->icmp_code);
170 }
171 
172 static int nftnl_expr_reject_export(char *buf, size_t size,
173  const struct nftnl_expr *e, int type)
174 {
175  struct nftnl_expr_reject *reject = nftnl_expr_data(e);
176  NFTNL_BUF_INIT(b, buf, size);
177 
178  if (e->flags & (1 << NFTNL_EXPR_REJECT_TYPE))
179  nftnl_buf_u32(&b, type, reject->type, TYPE);
180  if (e->flags & (1 << NFTNL_EXPR_REJECT_CODE))
181  nftnl_buf_u32(&b, type, reject->icmp_code, CODE);
182 
183  return nftnl_buf_done(&b);
184 }
185 
186 static int
187 nftnl_expr_reject_snprintf(char *buf, size_t len, uint32_t type,
188  uint32_t flags, const struct nftnl_expr *e)
189 {
190  switch (type) {
191  case NFTNL_OUTPUT_DEFAULT:
192  return nftnl_expr_reject_snprintf_default(buf, len, e);
193  case NFTNL_OUTPUT_XML:
194  case NFTNL_OUTPUT_JSON:
195  return nftnl_expr_reject_export(buf, len, e, type);
196  default:
197  break;
198  }
199  return -1;
200 }
201 
202 struct expr_ops expr_ops_reject = {
203  .name = "reject",
204  .alloc_len = sizeof(struct nftnl_expr_reject),
205  .max_attr = NFTA_REJECT_MAX,
206  .set = nftnl_expr_reject_set,
207  .get = nftnl_expr_reject_get,
208  .parse = nftnl_expr_reject_parse,
209  .build = nftnl_expr_reject_build,
210  .snprintf = nftnl_expr_reject_snprintf,
211  .xml_parse = nftnl_expr_reject_xml_parse,
212  .json_parse = nftnl_expr_reject_json_parse,
213 };