libnftnl 1.2.7
rt.c
1/*
2 * Copyright (c) 2016 Anders K. Pedersen <akp@cohaesio.com>
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_rt_keys key;
24 enum nft_registers dreg;
25};
26
27static int
28nftnl_expr_rt_set(struct nftnl_expr *e, uint16_t type,
29 const void *data, uint32_t data_len)
30{
31 struct nftnl_expr_rt *rt = nftnl_expr_data(e);
32
33 switch (type) {
34 case NFTNL_EXPR_RT_KEY:
35 memcpy(&rt->key, data, data_len);
36 break;
37 case NFTNL_EXPR_RT_DREG:
38 memcpy(&rt->dreg, data, data_len);
39 break;
40 }
41 return 0;
42}
43
44static const void *
45nftnl_expr_rt_get(const struct nftnl_expr *e, uint16_t type,
46 uint32_t *data_len)
47{
48 struct nftnl_expr_rt *rt = nftnl_expr_data(e);
49
50 switch (type) {
51 case NFTNL_EXPR_RT_KEY:
52 *data_len = sizeof(rt->key);
53 return &rt->key;
54 case NFTNL_EXPR_RT_DREG:
55 *data_len = sizeof(rt->dreg);
56 return &rt->dreg;
57 }
58 return NULL;
59}
60
61static int nftnl_expr_rt_cb(const struct nlattr *attr, void *data)
62{
63 const struct nlattr **tb = data;
64 int type = mnl_attr_get_type(attr);
65
66 if (mnl_attr_type_valid(attr, NFTA_RT_MAX) < 0)
67 return MNL_CB_OK;
68
69 switch (type) {
70 case NFTA_RT_KEY:
71 case NFTA_RT_DREG:
72 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
73 abi_breakage();
74 break;
75 }
76
77 tb[type] = attr;
78 return MNL_CB_OK;
79}
80
81static void
82nftnl_expr_rt_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
83{
84 struct nftnl_expr_rt *rt = nftnl_expr_data(e);
85
86 if (e->flags & (1 << NFTNL_EXPR_RT_KEY))
87 mnl_attr_put_u32(nlh, NFTA_RT_KEY, htonl(rt->key));
88 if (e->flags & (1 << NFTNL_EXPR_RT_DREG))
89 mnl_attr_put_u32(nlh, NFTA_RT_DREG, htonl(rt->dreg));
90}
91
92static int
93nftnl_expr_rt_parse(struct nftnl_expr *e, struct nlattr *attr)
94{
95 struct nftnl_expr_rt *rt = nftnl_expr_data(e);
96 struct nlattr *tb[NFTA_RT_MAX+1] = {};
97
98 if (mnl_attr_parse_nested(attr, nftnl_expr_rt_cb, tb) < 0)
99 return -1;
100
101 if (tb[NFTA_RT_KEY]) {
102 rt->key = ntohl(mnl_attr_get_u32(tb[NFTA_RT_KEY]));
103 e->flags |= (1 << NFTNL_EXPR_RT_KEY);
104 }
105 if (tb[NFTA_RT_DREG]) {
106 rt->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_RT_DREG]));
107 e->flags |= (1 << NFTNL_EXPR_RT_DREG);
108 }
109
110 return 0;
111}
112
113static const char *rt_key2str_array[NFT_RT_MAX + 1] = {
114 [NFT_RT_CLASSID] = "classid",
115 [NFT_RT_NEXTHOP4] = "nexthop4",
116 [NFT_RT_NEXTHOP6] = "nexthop6",
117 [NFT_RT_TCPMSS] = "tcpmss",
118 [NFT_RT_XFRM] = "ipsec",
119};
120
121static const char *rt_key2str(uint8_t key)
122{
123 if (key <= NFT_RT_MAX)
124 return rt_key2str_array[key];
125
126 return "unknown";
127}
128
129static inline int str2rt_key(const char *str)
130{
131 int i;
132
133 for (i = 0; i < NFT_RT_MAX; i++) {
134 if (strcmp(str, rt_key2str_array[i]) == 0)
135 return i;
136 }
137
138 errno = EINVAL;
139 return -1;
140}
141
142static int
143nftnl_expr_rt_snprintf(char *buf, size_t len,
144 uint32_t flags, const struct nftnl_expr *e)
145{
146 struct nftnl_expr_rt *rt = nftnl_expr_data(e);
147
148 if (e->flags & (1 << NFTNL_EXPR_RT_DREG)) {
149 return snprintf(buf, len, "load %s => reg %u ",
150 rt_key2str(rt->key), rt->dreg);
151 }
152 return 0;
153}
154
155static struct attr_policy rt_attr_policy[__NFTNL_EXPR_RT_MAX] = {
156 [NFTNL_EXPR_RT_KEY] = { .maxlen = sizeof(uint32_t) },
157 [NFTNL_EXPR_RT_DREG] = { .maxlen = sizeof(uint32_t) },
158};
159
160struct expr_ops expr_ops_rt = {
161 .name = "rt",
162 .alloc_len = sizeof(struct nftnl_expr_rt),
163 .nftnl_max_attr = __NFTNL_EXPR_RT_MAX - 1,
164 .attr_policy = rt_attr_policy,
165 .set = nftnl_expr_rt_set,
166 .get = nftnl_expr_rt_get,
167 .parse = nftnl_expr_rt_parse,
168 .build = nftnl_expr_rt_build,
169 .output = nftnl_expr_rt_snprintf,
170};