Debian Codegraph @

src/expr/rt.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Copyright (c) 2016 Anders K. Pedersen <[email protected]>
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
18 struct nftnl_expr_rt {
19 enum nft_rt_keys key;
20 enum nft_registers dreg;
21 };
22
23 static int
24 nftnl_expr_rt_set(struct nftnl_expr *e, uint16_t type,
25 const void *data, uint32_t data_len, uint32_t byteorder)
26 {
27 struct nftnl_expr_rt *rt = nftnl_expr_data(e);
28
29 switch (type) {
30 case NFTNL_EXPR_RT_KEY:
31 memcpy(&rt->key, data, data_len);
32 break;
33 case NFTNL_EXPR_RT_DREG:
34 memcpy(&rt->dreg, data, data_len);
35 break;
36 }
37 return 0;
38 }
39
40 static const void *
41 nftnl_expr_rt_get(const struct nftnl_expr *e, uint16_t type,
42 uint32_t *data_len)
43 {
44 struct nftnl_expr_rt *rt = nftnl_expr_data(e);
45
46 switch (type) {
47 case NFTNL_EXPR_RT_KEY:
48 *data_len = sizeof(rt->key);
49 return &rt->key;
50 case NFTNL_EXPR_RT_DREG:
51 *data_len = sizeof(rt->dreg);
52 return &rt->dreg;
53 }
54 return NULL;
55 }
56
57 static int nftnl_expr_rt_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_RT_MAX) < 0)
63 return MNL_CB_OK;
64
65 switch (type) {
66 case NFTA_RT_KEY:
67 case NFTA_RT_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
77 static void
78 nftnl_expr_rt_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
79 {
80 struct nftnl_expr_rt *rt = nftnl_expr_data(e);
81
82 if (e->flags & (1 << NFTNL_EXPR_RT_KEY))
83 mnl_attr_put_u32(nlh, NFTA_RT_KEY, htonl(rt->key));
84 if (e->flags & (1 << NFTNL_EXPR_RT_DREG))
85 mnl_attr_put_u32(nlh, NFTA_RT_DREG, htonl(rt->dreg));
86 }
87
88 static int
89 nftnl_expr_rt_parse(struct nftnl_expr *e, struct nlattr *attr)
90 {
91 struct nftnl_expr_rt *rt = nftnl_expr_data(e);
92 struct nlattr *tb[NFTA_RT_MAX+1] = {};
93
94 if (mnl_attr_parse_nested(attr, nftnl_expr_rt_cb, tb) < 0)
95 return -1;
96
97 if (tb[NFTA_RT_KEY]) {
98 rt->key = ntohl(mnl_attr_get_u32(tb[NFTA_RT_KEY]));
99 e->flags |= (1 << NFTNL_EXPR_RT_KEY);
100 }
101 if (tb[NFTA_RT_DREG]) {
102 rt->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_RT_DREG]));
103 e->flags |= (1 << NFTNL_EXPR_RT_DREG);
104 }
105
106 return 0;
107 }
108
109 static const char *rt_key2str_array[NFT_RT_MAX + 1] = {
110 [NFT_RT_CLASSID] = "classid",
111 [NFT_RT_NEXTHOP4] = "nexthop4",
112 [NFT_RT_NEXTHOP6] = "nexthop6",
113 [NFT_RT_TCPMSS] = "tcpmss",
114 [NFT_RT_XFRM] = "ipsec",
115 };
116
117 static const char *rt_key2str(uint8_t key)
118 {
119 if (key <= NFT_RT_MAX)
120 return rt_key2str_array[key];
121
122 return "unknown";
123 }
124
125 static int
126 nftnl_expr_rt_snprintf(char *buf, size_t len,
127 uint32_t flags, const struct nftnl_expr *e)
128 {
129 struct nftnl_expr_rt *rt = nftnl_expr_data(e);
130
131 if (e->flags & (1 << NFTNL_EXPR_RT_DREG)) {
132 return snprintf(buf, len, "load %s => reg %u ",
133 rt_key2str(rt->key), rt->dreg);
134 }
135 return 0;
136 }
137
138 static struct attr_policy rt_attr_policy[__NFTNL_EXPR_RT_MAX] = {
139 [NFTNL_EXPR_RT_KEY] = { .maxlen = sizeof(uint32_t) },
140 [NFTNL_EXPR_RT_DREG] = { .maxlen = sizeof(uint32_t) },
141 };
142
143 struct expr_ops expr_ops_rt = {
144 .name = "rt",
145 .alloc_len = sizeof(struct nftnl_expr_rt),
146 .nftnl_max_attr = __NFTNL_EXPR_RT_MAX - 1,
147 .attr_policy = rt_attr_policy,
148 .set = nftnl_expr_rt_set,
149 .get = nftnl_expr_rt_get,
150 .parse = nftnl_expr_rt_parse,
151 .build = nftnl_expr_rt_build,
152 .output = nftnl_expr_rt_snprintf,
153 };