libnftnl  1.1.8
expr/tunnel.c
1 /*
2  * (C) 2018 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 
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <string.h>
13 #include <arpa/inet.h>
14 #include <errno.h>
15 #include <linux/netfilter/nf_tables.h>
16 
17 #include "internal.h"
18 #include <libmnl/libmnl.h>
19 #include <libnftnl/expr.h>
20 #include <libnftnl/rule.h>
21 
23  enum nft_tunnel_keys key;
24  enum nft_registers dreg;
25 };
26 
27 static int nftnl_expr_tunnel_set(struct nftnl_expr *e, uint16_t type,
28  const void *data, uint32_t data_len)
29 {
30  struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e);
31 
32  switch(type) {
33  case NFTNL_EXPR_TUNNEL_KEY:
34  memcpy(&tunnel->key, data, sizeof(tunnel->key));
35  break;
36  case NFTNL_EXPR_TUNNEL_DREG:
37  memcpy(&tunnel->dreg, data, sizeof(tunnel->dreg));
38  break;
39  default:
40  return -1;
41  }
42  return 0;
43 }
44 
45 static const void *
46 nftnl_expr_tunnel_get(const struct nftnl_expr *e, uint16_t type,
47  uint32_t *data_len)
48 {
49  struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e);
50 
51  switch(type) {
52  case NFTNL_EXPR_TUNNEL_KEY:
53  *data_len = sizeof(tunnel->key);
54  return &tunnel->key;
55  case NFTNL_EXPR_TUNNEL_DREG:
56  *data_len = sizeof(tunnel->dreg);
57  return &tunnel->dreg;
58  }
59  return NULL;
60 }
61 
62 static int nftnl_expr_tunnel_cb(const struct nlattr *attr, void *data)
63 {
64  const struct nlattr **tb = data;
65  int type = mnl_attr_get_type(attr);
66 
67  if (mnl_attr_type_valid(attr, NFTA_TUNNEL_MAX) < 0)
68  return MNL_CB_OK;
69 
70  switch(type) {
71  case NFTA_TUNNEL_KEY:
72  case NFTA_TUNNEL_DREG:
73  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
74  abi_breakage();
75  break;
76  }
77 
78  tb[type] = attr;
79  return MNL_CB_OK;
80 }
81 
82 static void
83 nftnl_expr_tunnel_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
84 {
85  struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e);
86 
87  if (e->flags & (1 << NFTNL_EXPR_TUNNEL_KEY))
88  mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY, htonl(tunnel->key));
89  if (e->flags & (1 << NFTNL_EXPR_TUNNEL_DREG))
90  mnl_attr_put_u32(nlh, NFTA_TUNNEL_DREG, htonl(tunnel->dreg));
91 }
92 
93 static int
94 nftnl_expr_tunnel_parse(struct nftnl_expr *e, struct nlattr *attr)
95 {
96  struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e);
97  struct nlattr *tb[NFTA_TUNNEL_MAX + 1] = {};
98 
99  if (mnl_attr_parse_nested(attr, nftnl_expr_tunnel_cb, tb) < 0)
100  return -1;
101 
102  if (tb[NFTA_TUNNEL_KEY]) {
103  tunnel->key = ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY]));
104  e->flags |= (1 << NFTNL_EXPR_TUNNEL_KEY);
105  }
106  if (tb[NFTA_TUNNEL_DREG]) {
107  tunnel->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_DREG]));
108  e->flags |= (1 << NFTNL_EXPR_TUNNEL_DREG);
109  }
110 
111  return 0;
112 }
113 
114 static const char *tunnel_key2str_array[NFT_TUNNEL_MAX + 1] = {
115  [NFT_TUNNEL_PATH] = "path",
116  [NFT_TUNNEL_ID] = "id",
117 };
118 
119 static const char *tunnel_key2str(uint8_t key)
120 {
121  if (key <= NFT_TUNNEL_MAX)
122  return tunnel_key2str_array[key];
123 
124  return "unknown";
125 }
126 
127 static inline int str2tunnel_key(const char *str)
128 {
129  int i;
130 
131  for (i = 0; i <= NFT_TUNNEL_MAX; i++) {
132  if (strcmp(str, tunnel_key2str_array[i]) == 0)
133  return i;
134  }
135 
136  errno = EINVAL;
137  return -1;
138 }
139 
140 static int
141 nftnl_expr_tunnel_snprintf_default(char *buf, size_t len,
142  const struct nftnl_expr *e)
143 {
144  struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e);
145 
146  if (e->flags & (1 << NFTNL_EXPR_TUNNEL_DREG)) {
147  return snprintf(buf, len, "load %s => reg %u ",
148  tunnel_key2str(tunnel->key), tunnel->dreg);
149  }
150  return 0;
151 }
152 
153 static int
154 nftnl_expr_tunnel_snprintf(char *buf, size_t len, uint32_t type,
155  uint32_t flags, const struct nftnl_expr *e)
156 {
157  switch (type) {
158  case NFTNL_OUTPUT_DEFAULT:
159  return nftnl_expr_tunnel_snprintf_default(buf, len, e);
160  case NFTNL_OUTPUT_XML:
161  case NFTNL_OUTPUT_JSON:
162  default:
163  break;
164  }
165  return -1;
166 }
167 
168 struct expr_ops expr_ops_tunnel = {
169  .name = "tunnel",
170  .alloc_len = sizeof(struct nftnl_expr_tunnel),
171  .max_attr = NFTA_TUNNEL_MAX,
172  .set = nftnl_expr_tunnel_set,
173  .get = nftnl_expr_tunnel_get,
174  .parse = nftnl_expr_tunnel_parse,
175  .build = nftnl_expr_tunnel_build,
176  .snprintf = nftnl_expr_tunnel_snprintf,
177 };
nftnl_expr_tunnel
Definition: expr/tunnel.c:22