libnftnl 1.3.2
expr/tunnel.c
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * (C) 2018 by Pablo Neira Ayuso <pablo@netfilter.org>
4 */
5
6#include <stdio.h>
7#include <stdint.h>
8#include <string.h>
9#include <arpa/inet.h>
10#include <errno.h>
11#include <linux/netfilter/nf_tables.h>
12
13#include "internal.h"
14#include <libmnl/libmnl.h>
15#include <libnftnl/expr.h>
16#include <libnftnl/rule.h>
17
19 enum nft_tunnel_keys key;
20 enum nft_registers dreg;
21};
22
23static int
24nftnl_expr_tunnel_set(struct nftnl_expr *e, uint16_t type,
25 const void *data, uint32_t data_len, uint32_t byteorder)
26{
27 struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e);
28
29 switch(type) {
30 case NFTNL_EXPR_TUNNEL_KEY:
31 memcpy(&tunnel->key, data, data_len);
32 break;
33 case NFTNL_EXPR_TUNNEL_DREG:
34 memcpy(&tunnel->dreg, data, data_len);
35 break;
36 }
37 return 0;
38}
39
40static const void *
41nftnl_expr_tunnel_get(const struct nftnl_expr *e, uint16_t type,
42 uint32_t *data_len)
43{
44 struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e);
45
46 switch(type) {
47 case NFTNL_EXPR_TUNNEL_KEY:
48 *data_len = sizeof(tunnel->key);
49 return &tunnel->key;
50 case NFTNL_EXPR_TUNNEL_DREG:
51 *data_len = sizeof(tunnel->dreg);
52 return &tunnel->dreg;
53 }
54 return NULL;
55}
56
57static int nftnl_expr_tunnel_cb(const struct nlattr *attr, void *data)
58{
59 const struct nlattr **tb = data;
60 int type = mnl_attr_get_type(attr);
61
62 if (mnl_attr_type_valid(attr, NFTA_TUNNEL_MAX) < 0)
63 return MNL_CB_OK;
64
65 switch(type) {
66 case NFTA_TUNNEL_KEY:
67 case NFTA_TUNNEL_DREG:
68 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
69 abi_breakage();
70 break;
71 }
72
73 tb[type] = attr;
74 return MNL_CB_OK;
75}
76
77static void
78nftnl_expr_tunnel_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
79{
80 struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e);
81
82 if (e->flags & (1 << NFTNL_EXPR_TUNNEL_KEY))
83 mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY, htonl(tunnel->key));
84 if (e->flags & (1 << NFTNL_EXPR_TUNNEL_DREG))
85 mnl_attr_put_u32(nlh, NFTA_TUNNEL_DREG, htonl(tunnel->dreg));
86}
87
88static int
89nftnl_expr_tunnel_parse(struct nftnl_expr *e, struct nlattr *attr)
90{
91 struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e);
92 struct nlattr *tb[NFTA_TUNNEL_MAX + 1] = {};
93
94 if (mnl_attr_parse_nested(attr, nftnl_expr_tunnel_cb, tb) < 0)
95 return -1;
96
97 if (tb[NFTA_TUNNEL_KEY]) {
98 tunnel->key = ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY]));
99 e->flags |= (1 << NFTNL_EXPR_TUNNEL_KEY);
100 }
101 if (tb[NFTA_TUNNEL_DREG]) {
102 tunnel->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_DREG]));
103 e->flags |= (1 << NFTNL_EXPR_TUNNEL_DREG);
104 }
105
106 return 0;
107}
108
109static const char *tunnel_key2str_array[NFT_TUNNEL_MAX + 1] = {
110 [NFT_TUNNEL_PATH] = "path",
111 [NFT_TUNNEL_ID] = "id",
112};
113
114static const char *tunnel_key2str(uint8_t key)
115{
116 if (key <= NFT_TUNNEL_MAX)
117 return tunnel_key2str_array[key];
118
119 return "unknown";
120}
121
122static int
123nftnl_expr_tunnel_snprintf(char *buf, size_t len,
124 uint32_t flags, const struct nftnl_expr *e)
125{
126 struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e);
127
128 if (e->flags & (1 << NFTNL_EXPR_TUNNEL_DREG)) {
129 return snprintf(buf, len, "load %s => reg %u ",
130 tunnel_key2str(tunnel->key), tunnel->dreg);
131 }
132 return 0;
133}
134
135static struct attr_policy tunnel_attr_policy[__NFTNL_EXPR_TUNNEL_MAX] = {
136 [NFTNL_EXPR_TUNNEL_KEY] = { .maxlen = sizeof(uint32_t) },
137 [NFTNL_EXPR_TUNNEL_DREG] = { .maxlen = sizeof(uint32_t) },
138};
139
140struct expr_ops expr_ops_tunnel = {
141 .name = "tunnel",
142 .alloc_len = sizeof(struct nftnl_expr_tunnel),
143 .nftnl_max_attr = __NFTNL_EXPR_TUNNEL_MAX - 1,
144 .attr_policy = tunnel_attr_policy,
145 .set = nftnl_expr_tunnel_set,
146 .get = nftnl_expr_tunnel_get,
147 .parse = nftnl_expr_tunnel_parse,
148 .build = nftnl_expr_tunnel_build,
149 .output = nftnl_expr_tunnel_snprintf,
150};