| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2003-2013 Thomas Graf <[email protected]> |
| 4 |
* Copyright (c) 2006 Petr Gotthard <[email protected]> |
| 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 */ |
| 24 |
struct 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 |
|
| 40 |
static 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 |
|
| 47 |
static 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 |
|
| 89 |
static 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 |
|
| 97 |
static 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 |
|
| 113 |
static 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 |
|
| 132 |
static 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 |
|
| 141 |
static 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 |
|
| 165 |
nla_put_failure: |
| 166 |
return -NLE_MSGSIZE; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* @name Attribute Modifications |
| 171 |
* @{ |
| 172 |
*/ |
| 173 |
|
| 174 |
int 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 |
|
| 187 |
int 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 |
|
| 202 |
static 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 |
|
| 216 |
static void _nl_init fw_init(void) |
| 217 |
{ |
| 218 |
rtnl_tc_register(&fw_ops); |
| 219 |
} |
| 220 |
|
| 221 |
static void _nl_exit fw_exit(void) |
| 222 |
{ |
| 223 |
rtnl_tc_unregister(&fw_ops); |
| 224 |
} |
| 225 |
|
| 226 |
/** @} */ |