libnftnl  1.2.8
meta.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  * 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 
24 #ifndef NFT_META_MAX
25 #define NFT_META_MAX (NFT_META_BRI_BROUTE + 1)
26 #endif
27 
29  enum nft_meta_keys key;
30  enum nft_registers dreg;
31  enum nft_registers sreg;
32 };
33 
34 static int
35 nftnl_expr_meta_set(struct nftnl_expr *e, uint16_t type,
36  const void *data, uint32_t data_len)
37 {
38  struct nftnl_expr_meta *meta = nftnl_expr_data(e);
39 
40  switch(type) {
41  case NFTNL_EXPR_META_KEY:
42  memcpy(&meta->key, data, data_len);
43  break;
44  case NFTNL_EXPR_META_DREG:
45  memcpy(&meta->dreg, data, data_len);
46  break;
47  case NFTNL_EXPR_META_SREG:
48  memcpy(&meta->sreg, data, data_len);
49  break;
50  }
51  return 0;
52 }
53 
54 static const void *
55 nftnl_expr_meta_get(const struct nftnl_expr *e, uint16_t type,
56  uint32_t *data_len)
57 {
58  struct nftnl_expr_meta *meta = nftnl_expr_data(e);
59 
60  switch(type) {
61  case NFTNL_EXPR_META_KEY:
62  *data_len = sizeof(meta->key);
63  return &meta->key;
64  case NFTNL_EXPR_META_DREG:
65  *data_len = sizeof(meta->dreg);
66  return &meta->dreg;
67  case NFTNL_EXPR_META_SREG:
68  *data_len = sizeof(meta->sreg);
69  return &meta->sreg;
70  }
71  return NULL;
72 }
73 
74 static int nftnl_expr_meta_cb(const struct nlattr *attr, void *data)
75 {
76  const struct nlattr **tb = data;
77  int type = mnl_attr_get_type(attr);
78 
79  if (mnl_attr_type_valid(attr, NFTA_META_MAX) < 0)
80  return MNL_CB_OK;
81 
82  switch(type) {
83  case NFTA_META_KEY:
84  case NFTA_META_DREG:
85  case NFTA_META_SREG:
86  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
87  abi_breakage();
88  break;
89  }
90 
91  tb[type] = attr;
92  return MNL_CB_OK;
93 }
94 
95 static void
96 nftnl_expr_meta_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
97 {
98  struct nftnl_expr_meta *meta = nftnl_expr_data(e);
99 
100  if (e->flags & (1 << NFTNL_EXPR_META_KEY))
101  mnl_attr_put_u32(nlh, NFTA_META_KEY, htonl(meta->key));
102  if (e->flags & (1 << NFTNL_EXPR_META_DREG))
103  mnl_attr_put_u32(nlh, NFTA_META_DREG, htonl(meta->dreg));
104  if (e->flags & (1 << NFTNL_EXPR_META_SREG))
105  mnl_attr_put_u32(nlh, NFTA_META_SREG, htonl(meta->sreg));
106 }
107 
108 static int
109 nftnl_expr_meta_parse(struct nftnl_expr *e, struct nlattr *attr)
110 {
111  struct nftnl_expr_meta *meta = nftnl_expr_data(e);
112  struct nlattr *tb[NFTA_META_MAX+1] = {};
113 
114  if (mnl_attr_parse_nested(attr, nftnl_expr_meta_cb, tb) < 0)
115  return -1;
116 
117  if (tb[NFTA_META_KEY]) {
118  meta->key = ntohl(mnl_attr_get_u32(tb[NFTA_META_KEY]));
119  e->flags |= (1 << NFTNL_EXPR_META_KEY);
120  }
121  if (tb[NFTA_META_DREG]) {
122  meta->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_META_DREG]));
123  e->flags |= (1 << NFTNL_EXPR_META_DREG);
124  }
125  if (tb[NFTA_META_SREG]) {
126  meta->sreg = ntohl(mnl_attr_get_u32(tb[NFTA_META_SREG]));
127  e->flags |= (1 << NFTNL_EXPR_META_SREG);
128  }
129 
130  return 0;
131 }
132 
133 static const char *meta_key2str_array[NFT_META_MAX] = {
134  [NFT_META_LEN] = "len",
135  [NFT_META_PROTOCOL] = "protocol",
136  [NFT_META_NFPROTO] = "nfproto",
137  [NFT_META_L4PROTO] = "l4proto",
138  [NFT_META_PRIORITY] = "priority",
139  [NFT_META_MARK] = "mark",
140  [NFT_META_IIF] = "iif",
141  [NFT_META_OIF] = "oif",
142  [NFT_META_IIFNAME] = "iifname",
143  [NFT_META_OIFNAME] = "oifname",
144  [NFT_META_IIFTYPE] = "iiftype",
145  [NFT_META_OIFTYPE] = "oiftype",
146  [NFT_META_SKUID] = "skuid",
147  [NFT_META_SKGID] = "skgid",
148  [NFT_META_NFTRACE] = "nftrace",
149  [NFT_META_RTCLASSID] = "rtclassid",
150  [NFT_META_SECMARK] = "secmark",
151  [NFT_META_BRI_IIFNAME] = "bri_iifname",
152  [NFT_META_BRI_OIFNAME] = "bri_oifname",
153  [NFT_META_PKTTYPE] = "pkttype",
154  [NFT_META_CPU] = "cpu",
155  [NFT_META_IIFGROUP] = "iifgroup",
156  [NFT_META_OIFGROUP] = "oifgroup",
157  [NFT_META_CGROUP] = "cgroup",
158  [NFT_META_PRANDOM] = "prandom",
159  [NFT_META_SECPATH] = "secpath",
160  [NFT_META_IIFKIND] = "iifkind",
161  [NFT_META_OIFKIND] = "oifkind",
162  [NFT_META_BRI_IIFPVID] = "bri_iifpvid",
163  [NFT_META_BRI_IIFVPROTO] = "bri_iifvproto",
164  [NFT_META_TIME_NS] = "time",
165  [NFT_META_TIME_DAY] = "day",
166  [NFT_META_TIME_HOUR] = "hour",
167  [NFT_META_SDIF] = "sdif",
168  [NFT_META_SDIFNAME] = "sdifname",
169  [NFT_META_BRI_BROUTE] = "broute",
170 };
171 
172 static const char *meta_key2str(uint8_t key)
173 {
174  if (key < NFT_META_MAX)
175  return meta_key2str_array[key];
176 
177  return "unknown";
178 }
179 
180 static inline int str2meta_key(const char *str)
181 {
182  int i;
183 
184  for (i = 0; i < NFT_META_MAX; i++) {
185  if (strcmp(str, meta_key2str_array[i]) == 0)
186  return i;
187  }
188 
189  errno = EINVAL;
190  return -1;
191 }
192 
193 static int
194 nftnl_expr_meta_snprintf(char *buf, size_t len,
195  uint32_t flags, const struct nftnl_expr *e)
196 {
197  struct nftnl_expr_meta *meta = nftnl_expr_data(e);
198 
199  if (e->flags & (1 << NFTNL_EXPR_META_SREG)) {
200  return snprintf(buf, len, "set %s with reg %u ",
201  meta_key2str(meta->key), meta->sreg);
202  }
203  if (e->flags & (1 << NFTNL_EXPR_META_DREG)) {
204  return snprintf(buf, len, "load %s => reg %u ",
205  meta_key2str(meta->key), meta->dreg);
206  }
207  return 0;
208 }
209 
210 static struct attr_policy meta_attr_policy[__NFTNL_EXPR_META_MAX] = {
211  [NFTNL_EXPR_META_KEY] = { .maxlen = sizeof(uint32_t) },
212  [NFTNL_EXPR_META_DREG] = { .maxlen = sizeof(uint32_t) },
213  [NFTNL_EXPR_META_SREG] = { .maxlen = sizeof(uint32_t) },
214 };
215 
216 struct expr_ops expr_ops_meta = {
217  .name = "meta",
218  .alloc_len = sizeof(struct nftnl_expr_meta),
219  .nftnl_max_attr = __NFTNL_EXPR_META_MAX - 1,
220  .attr_policy = meta_attr_policy,
221  .set = nftnl_expr_meta_set,
222  .get = nftnl_expr_meta_get,
223  .parse = nftnl_expr_meta_parse,
224  .build = nftnl_expr_meta_build,
225  .output = nftnl_expr_meta_snprintf,
226 };