libnl: /data/src/libnl/lib/route/cls/fw.c Source File

libnl 3.12.0

fw.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
4 * Copyright (c) 2006 Petr Gotthard <petr.gotthard@siemens.com>
5 * Copyright (c) 2006 Siemens AG Oesterreich
6 */
7
8/**
9 * @ingroup cls
10 * @defgroup cls_fw Firewall Classifier
11 *
12 * @{
13 */
14
15#include "nl-default.h"
16
17#include <netlink/netlink.h>
18#include <netlink/route/classifier.h>
19#include <netlink/route/cls/fw.h>
20
21#include "tc-api.h"
22
23/** @cond SKIP */
24struct rtnl_fw {
25 uint32_t cf_classid;
26 struct nl_data *cf_act;
27 struct nl_data *cf_police;
28 char cf_indev[IFNAMSIZ];
29 uint32_t cf_fwmask;
30 int cf_mask;
31};
32
33#define FW_ATTR_CLASSID 0x001
34#define FW_ATTR_ACTION 0x002
35#define FW_ATTR_POLICE 0x004
36#define FW_ATTR_INDEV 0x008
37#define FW_ATTR_MASK 0x010
38/** @endcond */
39
40static struct nla_policy fw_policy[TCA_FW_MAX+1] = {
41 [TCA_FW_CLASSID] = { .type = NLA_U32 },
42 [TCA_FW_INDEV] = { .type = NLA_STRING,
43 .maxlen = IFNAMSIZ },
44 [TCA_FW_MASK] = { .type = NLA_U32 },
45};
46
47static int fw_msg_parser(struct rtnl_tc *tc, void *data)
48{
49 struct nlattr *tb[TCA_FW_MAX + 1];
50 struct rtnl_fw *f = data;
51 int err;
52
53 err = tca_parse(tb, TCA_FW_MAX, tc, fw_policy);
54 if (err < 0)
55 return err;
56
57 if (tb[TCA_FW_CLASSID]) {
58 f->cf_classid = nla_get_u32(tb[TCA_FW_CLASSID]);
59 f->cf_mask |= FW_ATTR_CLASSID;
60 }
61
62 if (tb[TCA_FW_ACT]) {
63 f->cf_act = nl_data_alloc_attr(tb[TCA_FW_ACT]);
64 if (!f->cf_act)
65 return -NLE_NOMEM;
66 f->cf_mask |= FW_ATTR_ACTION;
67 }
68
69 if (tb[TCA_FW_POLICE]) {
70 f->cf_police = nl_data_alloc_attr(tb[TCA_FW_POLICE]);
71 if (!f->cf_police)
72 return -NLE_NOMEM;
73 f->cf_mask |= FW_ATTR_POLICE;
74 }
75
76 if (tb[TCA_FW_INDEV]) {
77 nla_strlcpy(f->cf_indev, tb[TCA_FW_INDEV], IFNAMSIZ);
78 f->cf_mask |= FW_ATTR_INDEV;
79 }
80
81 if (tb[TCA_FW_MASK]) {
82 f->cf_fwmask = nla_get_u32(tb[TCA_FW_MASK]);
83 f->cf_mask |= FW_ATTR_MASK;
84 }
85
86 return 0;
87}
88
89static void fw_free_data(struct rtnl_tc *tc, void *data)
90{
91 struct rtnl_fw *f = data;
92
93 nl_data_free(f->cf_act);
94 nl_data_free(f->cf_police);
95}
96
97static int fw_clone(void *_dst, void *_src)
98{
99 struct rtnl_fw *dst = _dst, *src = _src;
100
101 dst->cf_act = NULL;
102 dst->cf_police = NULL;
103
104 if (src->cf_act && !(dst->cf_act = nl_data_clone(src->cf_act)))
105 return -NLE_NOMEM;
106
107 if (src->cf_police && !(dst->cf_police = nl_data_clone(src->cf_police)))
108 return -NLE_NOMEM;
109
110 return 0;
111}
112
113static void fw_dump_line(struct rtnl_tc *tc, void *data,
114 struct nl_dump_params *p)
115{
116 struct rtnl_fw *f = data;
117
118 if (!f)
119 return;
120
121 if (f->cf_mask & FW_ATTR_CLASSID) {
122 char buf[32];
123
124 nl_dump(p, " target %s",
125 rtnl_tc_handle2str(f->cf_classid, buf, sizeof(buf)));
126 }
127
128 if (f->cf_mask & FW_ATTR_MASK)
129 nl_dump(p, " mask 0x%x", f->cf_fwmask);
130}
131
132static void fw_dump_details(struct rtnl_tc *tc, void *data,
133 struct nl_dump_params *p)
134{
135 struct rtnl_fw *f = data;
136
137 if (f && f->cf_mask & FW_ATTR_INDEV)
138 nl_dump(p, "indev %s ", f->cf_indev);
139}
140
141static int fw_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
142{
143 struct rtnl_fw *f = data;
144
145 if (!f)
146 return 0;
147
148 if (f->cf_mask & FW_ATTR_CLASSID)
149 NLA_PUT_U32(msg, TCA_FW_CLASSID, f->cf_classid);
150
151 if (f->cf_mask & FW_ATTR_ACTION)
152 NLA_PUT_DATA(msg, TCA_FW_ACT, f->cf_act);
153
154 if (f->cf_mask & FW_ATTR_POLICE)
155 NLA_PUT_DATA(msg, TCA_FW_POLICE, f->cf_police);
156
157 if (f->cf_mask & FW_ATTR_INDEV)
158 NLA_PUT_STRING(msg, TCA_FW_INDEV, f->cf_indev);
159
160 if (f->cf_mask & FW_ATTR_MASK)
161 NLA_PUT_U32(msg, TCA_FW_MASK, f->cf_fwmask);
162
163 return 0;
164
165nla_put_failure:
166 return -NLE_MSGSIZE;
167}
168
169/**
170 * @name Attribute Modifications
171 * @{
172 */
173
174int rtnl_fw_set_classid(struct rtnl_cls *cls, uint32_t classid)
175{
176 struct rtnl_fw *f;
177
178 if (!(f = rtnl_tc_data(TC_CAST(cls))))
179 return -NLE_NOMEM;
180
181 f->cf_classid = classid;
182 f->cf_mask |= FW_ATTR_CLASSID;
183
184 return 0;
185}
186
187int rtnl_fw_set_mask(struct rtnl_cls *cls, uint32_t mask)
188{
189 struct rtnl_fw *f;
190
191 if (!(f = rtnl_tc_data(TC_CAST(cls))))
192 return -NLE_NOMEM;
193
194 f->cf_fwmask = mask;
195 f->cf_mask |= FW_ATTR_MASK;
196
197 return 0;
198}
199
200/** @} */
201
202static struct rtnl_tc_ops fw_ops = {
203 .to_kind = "fw",
204 .to_type = RTNL_TC_TYPE_CLS,
205 .to_size = sizeof(struct rtnl_fw),
206 .to_msg_parser = fw_msg_parser,
207 .to_msg_fill = fw_msg_fill,
208 .to_free_data = fw_free_data,
209 .to_clone = fw_clone,
210 .to_dump = {
211 [NL_DUMP_LINE] = fw_dump_line,
212 [NL_DUMP_DETAILS] = fw_dump_details,
213 },
214};
215
216static void _nl_init fw_init(void)
217{
218 rtnl_tc_register(&fw_ops);
219}
220
221static void _nl_exit fw_exit(void)
222{
223 rtnl_tc_unregister(&fw_ops);
224}
225
226/** @} */
nla_get_u32
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition attr.c:714
NLA_PUT_DATA
#define NLA_PUT_DATA(msg, attrtype, data)
Add abstract data attribute to netlink message.
Definition attr.h:300
NLA_PUT_U32
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition attr.h:237
nla_strlcpy
size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
Copy string attribute payload to a buffer.
Definition attr.c:383
NLA_PUT_STRING
#define NLA_PUT_STRING(msg, attrtype, value)
Add string attribute to netlink message.
Definition attr.h:264
NLA_STRING
@ NLA_STRING
NUL terminated character string.
Definition attr.h:39
NLA_U32
@ NLA_U32
32 bit integer
Definition attr.h:37
rtnl_tc_handle2str
char * rtnl_tc_handle2str(uint32_t handle, char *buf, size_t len)
Convert a traffic control handle to a character string (Reentrant).
Definition classid.c:109
nl_data_clone
struct nl_data * nl_data_clone(const struct nl_data *src)
Clone an abstract data object.
Definition data.c:95
nl_data_free
void nl_data_free(struct nl_data *data)
Free an abstract data object.
Definition data.c:134
nl_data_alloc_attr
struct nl_data * nl_data_alloc_attr(const struct nlattr *nla)
Allocate abstract data object based on netlink attribute.
Definition data.c:84
TC_CAST
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
Definition tc.h:50
rtnl_tc_data
void * rtnl_tc_data(struct rtnl_tc *)
Return pointer to private data of traffic control object.
Definition tc.c:1079
rtnl_tc_register
int rtnl_tc_register(struct rtnl_tc_ops *)
Register a traffic control module.
Definition tc.c:1018
rtnl_tc_unregister
void rtnl_tc_unregister(struct rtnl_tc_ops *)
Unregister a traffic control module.
Definition tc.c:1052
nl_dump
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition utils.c:1015
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition types.h:20
NL_DUMP_DETAILS
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition types.h:21
nl_dump_params
Dumping parameters.
Definition types.h:32
nla_policy
Attribute validation policy.
Definition attr.h:66
rtnl_tc
Definition nl-route.h:115