libnftnl  1.2.3
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 int
128 nftnl_expr_tunnel_snprintf(char *buf, size_t len,
129  uint32_t flags, const struct nftnl_expr *e)
130 {
131  struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e);
132 
133  if (e->flags & (1 << NFTNL_EXPR_TUNNEL_DREG)) {
134  return snprintf(buf, len, "load %s => reg %u ",
135  tunnel_key2str(tunnel->key), tunnel->dreg);
136  }
137  return 0;
138 }
139 
140 struct expr_ops expr_ops_tunnel = {
141  .name = "tunnel",
142  .alloc_len = sizeof(struct nftnl_expr_tunnel),
143  .max_attr = NFTA_TUNNEL_MAX,
144  .set = nftnl_expr_tunnel_set,
145  .get = nftnl_expr_tunnel_get,
146  .parse = nftnl_expr_tunnel_parse,
147  .build = nftnl_expr_tunnel_build,
148  .output = nftnl_expr_tunnel_snprintf,
149 };