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