libnftnl 1.2.7
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
27static 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, data_len);
35 break;
36 case NFTNL_EXPR_TUNNEL_DREG:
37 memcpy(&tunnel->dreg, data, data_len);
38 break;
39 }
40 return 0;
41}
42
43static const void *
44nftnl_expr_tunnel_get(const struct nftnl_expr *e, uint16_t type,
45 uint32_t *data_len)
46{
47 struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e);
48
49 switch(type) {
50 case NFTNL_EXPR_TUNNEL_KEY:
51 *data_len = sizeof(tunnel->key);
52 return &tunnel->key;
53 case NFTNL_EXPR_TUNNEL_DREG:
54 *data_len = sizeof(tunnel->dreg);
55 return &tunnel->dreg;
56 }
57 return NULL;
58}
59
60static int nftnl_expr_tunnel_cb(const struct nlattr *attr, void *data)
61{
62 const struct nlattr **tb = data;
63 int type = mnl_attr_get_type(attr);
64
65 if (mnl_attr_type_valid(attr, NFTA_TUNNEL_MAX) < 0)
66 return MNL_CB_OK;
67
68 switch(type) {
69 case NFTA_TUNNEL_KEY:
70 case NFTA_TUNNEL_DREG:
71 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
72 abi_breakage();
73 break;
74 }
75
76 tb[type] = attr;
77 return MNL_CB_OK;
78}
79
80static void
81nftnl_expr_tunnel_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
82{
83 struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e);
84
85 if (e->flags & (1 << NFTNL_EXPR_TUNNEL_KEY))
86 mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY, htonl(tunnel->key));
87 if (e->flags & (1 << NFTNL_EXPR_TUNNEL_DREG))
88 mnl_attr_put_u32(nlh, NFTA_TUNNEL_DREG, htonl(tunnel->dreg));
89}
90
91static int
92nftnl_expr_tunnel_parse(struct nftnl_expr *e, struct nlattr *attr)
93{
94 struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e);
95 struct nlattr *tb[NFTA_TUNNEL_MAX + 1] = {};
96
97 if (mnl_attr_parse_nested(attr, nftnl_expr_tunnel_cb, tb) < 0)
98 return -1;
99
100 if (tb[NFTA_TUNNEL_KEY]) {
101 tunnel->key = ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY]));
102 e->flags |= (1 << NFTNL_EXPR_TUNNEL_KEY);
103 }
104 if (tb[NFTA_TUNNEL_DREG]) {
105 tunnel->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_DREG]));
106 e->flags |= (1 << NFTNL_EXPR_TUNNEL_DREG);
107 }
108
109 return 0;
110}
111
112static const char *tunnel_key2str_array[NFT_TUNNEL_MAX + 1] = {
113 [NFT_TUNNEL_PATH] = "path",
114 [NFT_TUNNEL_ID] = "id",
115};
116
117static const char *tunnel_key2str(uint8_t key)
118{
119 if (key <= NFT_TUNNEL_MAX)
120 return tunnel_key2str_array[key];
121
122 return "unknown";
123}
124
125static int
126nftnl_expr_tunnel_snprintf(char *buf, size_t len,
127 uint32_t flags, const struct nftnl_expr *e)
128{
129 struct nftnl_expr_tunnel *tunnel = nftnl_expr_data(e);
130
131 if (e->flags & (1 << NFTNL_EXPR_TUNNEL_DREG)) {
132 return snprintf(buf, len, "load %s => reg %u ",
133 tunnel_key2str(tunnel->key), tunnel->dreg);
134 }
135 return 0;
136}
137
138static struct attr_policy tunnel_attr_policy[__NFTNL_EXPR_TUNNEL_MAX] = {
139 [NFTNL_EXPR_TUNNEL_KEY] = { .maxlen = sizeof(uint32_t) },
140 [NFTNL_EXPR_TUNNEL_DREG] = { .maxlen = sizeof(uint32_t) },
141};
142
143struct expr_ops expr_ops_tunnel = {
144 .name = "tunnel",
145 .alloc_len = sizeof(struct nftnl_expr_tunnel),
146 .nftnl_max_attr = __NFTNL_EXPR_TUNNEL_MAX - 1,
147 .attr_policy = tunnel_attr_policy,
148 .set = nftnl_expr_tunnel_set,
149 .get = nftnl_expr_tunnel_get,
150 .parse = nftnl_expr_tunnel_parse,
151 .build = nftnl_expr_tunnel_build,
152 .output = nftnl_expr_tunnel_snprintf,
153};