Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 1 | /* |
| 2 | * net/sched/ife.c Inter-FE action based on ForCES WG InterFE LFB |
| 3 | * |
| 4 | * Refer to: |
| 5 | * draft-ietf-forces-interfelfb-03 |
| 6 | * and |
| 7 | * netdev01 paper: |
| 8 | * "Distributing Linux Traffic Control Classifier-Action |
| 9 | * Subsystem" |
| 10 | * Authors: Jamal Hadi Salim and Damascene M. Joachimpillai |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version |
| 15 | * 2 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * copyright Jamal Hadi Salim (2015) |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/skbuff.h> |
| 26 | #include <linux/rtnetlink.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <net/net_namespace.h> |
| 30 | #include <net/netlink.h> |
| 31 | #include <net/pkt_sched.h> |
| 32 | #include <uapi/linux/tc_act/tc_ife.h> |
| 33 | #include <net/tc_act/tc_ife.h> |
| 34 | #include <linux/etherdevice.h> |
| 35 | |
| 36 | #define IFE_TAB_MASK 15 |
| 37 | |
| 38 | static int ife_net_id; |
| 39 | static int max_metacnt = IFE_META_MAX + 1; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 40 | static struct tc_action_ops act_ife_ops; |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 41 | |
| 42 | static const struct nla_policy ife_policy[TCA_IFE_MAX + 1] = { |
| 43 | [TCA_IFE_PARMS] = { .len = sizeof(struct tc_ife)}, |
| 44 | [TCA_IFE_DMAC] = { .len = ETH_ALEN}, |
| 45 | [TCA_IFE_SMAC] = { .len = ETH_ALEN}, |
| 46 | [TCA_IFE_TYPE] = { .type = NLA_U16}, |
| 47 | }; |
| 48 | |
| 49 | /* Caller takes care of presenting data in network order |
| 50 | */ |
| 51 | int ife_tlv_meta_encode(void *skbdata, u16 attrtype, u16 dlen, const void *dval) |
| 52 | { |
| 53 | u32 *tlv = (u32 *)(skbdata); |
| 54 | u16 totlen = nla_total_size(dlen); /*alignment + hdr */ |
| 55 | char *dptr = (char *)tlv + NLA_HDRLEN; |
Yotam Gigi | c006da0 | 2016-09-26 13:45:26 +0300 | [diff] [blame] | 56 | u32 htlv = attrtype << 16 | (dlen + NLA_HDRLEN); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 57 | |
| 58 | *tlv = htonl(htlv); |
| 59 | memset(dptr, 0, totlen - NLA_HDRLEN); |
| 60 | memcpy(dptr, dval, dlen); |
| 61 | |
| 62 | return totlen; |
| 63 | } |
| 64 | EXPORT_SYMBOL_GPL(ife_tlv_meta_encode); |
| 65 | |
Jamal Hadi Salim | 6a5d58b | 2016-09-18 07:31:42 -0400 | [diff] [blame] | 66 | int ife_encode_meta_u16(u16 metaval, void *skbdata, struct tcf_meta_info *mi) |
| 67 | { |
| 68 | u16 edata = 0; |
| 69 | |
| 70 | if (mi->metaval) |
| 71 | edata = *(u16 *)mi->metaval; |
| 72 | else if (metaval) |
| 73 | edata = metaval; |
| 74 | |
| 75 | if (!edata) /* will not encode */ |
| 76 | return 0; |
| 77 | |
| 78 | edata = htons(edata); |
| 79 | return ife_tlv_meta_encode(skbdata, mi->metaid, 2, &edata); |
| 80 | } |
| 81 | EXPORT_SYMBOL_GPL(ife_encode_meta_u16); |
| 82 | |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 83 | int ife_get_meta_u32(struct sk_buff *skb, struct tcf_meta_info *mi) |
| 84 | { |
| 85 | if (mi->metaval) |
| 86 | return nla_put_u32(skb, mi->metaid, *(u32 *)mi->metaval); |
| 87 | else |
| 88 | return nla_put(skb, mi->metaid, 0, NULL); |
| 89 | } |
| 90 | EXPORT_SYMBOL_GPL(ife_get_meta_u32); |
| 91 | |
| 92 | int ife_check_meta_u32(u32 metaval, struct tcf_meta_info *mi) |
| 93 | { |
| 94 | if (metaval || mi->metaval) |
| 95 | return 8; /* T+L+V == 2+2+4 */ |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | EXPORT_SYMBOL_GPL(ife_check_meta_u32); |
| 100 | |
Jamal Hadi Salim | 6a5d58b | 2016-09-18 07:31:42 -0400 | [diff] [blame] | 101 | int ife_check_meta_u16(u16 metaval, struct tcf_meta_info *mi) |
| 102 | { |
| 103 | if (metaval || mi->metaval) |
| 104 | return 8; /* T+L+(V) == 2+2+(2+2bytepad) */ |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | EXPORT_SYMBOL_GPL(ife_check_meta_u16); |
| 109 | |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 110 | int ife_encode_meta_u32(u32 metaval, void *skbdata, struct tcf_meta_info *mi) |
| 111 | { |
| 112 | u32 edata = metaval; |
| 113 | |
| 114 | if (mi->metaval) |
| 115 | edata = *(u32 *)mi->metaval; |
| 116 | else if (metaval) |
| 117 | edata = metaval; |
| 118 | |
| 119 | if (!edata) /* will not encode */ |
| 120 | return 0; |
| 121 | |
| 122 | edata = htonl(edata); |
| 123 | return ife_tlv_meta_encode(skbdata, mi->metaid, 4, &edata); |
| 124 | } |
| 125 | EXPORT_SYMBOL_GPL(ife_encode_meta_u32); |
| 126 | |
| 127 | int ife_get_meta_u16(struct sk_buff *skb, struct tcf_meta_info *mi) |
| 128 | { |
| 129 | if (mi->metaval) |
| 130 | return nla_put_u16(skb, mi->metaid, *(u16 *)mi->metaval); |
| 131 | else |
| 132 | return nla_put(skb, mi->metaid, 0, NULL); |
| 133 | } |
| 134 | EXPORT_SYMBOL_GPL(ife_get_meta_u16); |
| 135 | |
WANG Cong | 067a7cd | 2016-06-20 13:37:18 -0700 | [diff] [blame] | 136 | int ife_alloc_meta_u32(struct tcf_meta_info *mi, void *metaval, gfp_t gfp) |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 137 | { |
WANG Cong | 067a7cd | 2016-06-20 13:37:18 -0700 | [diff] [blame] | 138 | mi->metaval = kmemdup(metaval, sizeof(u32), gfp); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 139 | if (!mi->metaval) |
| 140 | return -ENOMEM; |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | EXPORT_SYMBOL_GPL(ife_alloc_meta_u32); |
| 145 | |
WANG Cong | 067a7cd | 2016-06-20 13:37:18 -0700 | [diff] [blame] | 146 | int ife_alloc_meta_u16(struct tcf_meta_info *mi, void *metaval, gfp_t gfp) |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 147 | { |
WANG Cong | 067a7cd | 2016-06-20 13:37:18 -0700 | [diff] [blame] | 148 | mi->metaval = kmemdup(metaval, sizeof(u16), gfp); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 149 | if (!mi->metaval) |
| 150 | return -ENOMEM; |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | EXPORT_SYMBOL_GPL(ife_alloc_meta_u16); |
| 155 | |
| 156 | void ife_release_meta_gen(struct tcf_meta_info *mi) |
| 157 | { |
| 158 | kfree(mi->metaval); |
| 159 | } |
| 160 | EXPORT_SYMBOL_GPL(ife_release_meta_gen); |
| 161 | |
| 162 | int ife_validate_meta_u32(void *val, int len) |
| 163 | { |
Jamal Hadi Salim | 28a10c4 | 2016-08-22 07:10:20 -0400 | [diff] [blame] | 164 | if (len == sizeof(u32)) |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 165 | return 0; |
| 166 | |
| 167 | return -EINVAL; |
| 168 | } |
| 169 | EXPORT_SYMBOL_GPL(ife_validate_meta_u32); |
| 170 | |
| 171 | int ife_validate_meta_u16(void *val, int len) |
| 172 | { |
Jamal Hadi Salim | 28a10c4 | 2016-08-22 07:10:20 -0400 | [diff] [blame] | 173 | /* length will not include padding */ |
| 174 | if (len == sizeof(u16)) |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 175 | return 0; |
| 176 | |
| 177 | return -EINVAL; |
| 178 | } |
| 179 | EXPORT_SYMBOL_GPL(ife_validate_meta_u16); |
| 180 | |
| 181 | static LIST_HEAD(ifeoplist); |
| 182 | static DEFINE_RWLOCK(ife_mod_lock); |
| 183 | |
| 184 | static struct tcf_meta_ops *find_ife_oplist(u16 metaid) |
| 185 | { |
| 186 | struct tcf_meta_ops *o; |
| 187 | |
| 188 | read_lock(&ife_mod_lock); |
| 189 | list_for_each_entry(o, &ifeoplist, list) { |
| 190 | if (o->metaid == metaid) { |
| 191 | if (!try_module_get(o->owner)) |
| 192 | o = NULL; |
| 193 | read_unlock(&ife_mod_lock); |
| 194 | return o; |
| 195 | } |
| 196 | } |
| 197 | read_unlock(&ife_mod_lock); |
| 198 | |
| 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | int register_ife_op(struct tcf_meta_ops *mops) |
| 203 | { |
| 204 | struct tcf_meta_ops *m; |
| 205 | |
| 206 | if (!mops->metaid || !mops->metatype || !mops->name || |
| 207 | !mops->check_presence || !mops->encode || !mops->decode || |
| 208 | !mops->get || !mops->alloc) |
| 209 | return -EINVAL; |
| 210 | |
| 211 | write_lock(&ife_mod_lock); |
| 212 | |
| 213 | list_for_each_entry(m, &ifeoplist, list) { |
| 214 | if (m->metaid == mops->metaid || |
| 215 | (strcmp(mops->name, m->name) == 0)) { |
| 216 | write_unlock(&ife_mod_lock); |
| 217 | return -EEXIST; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if (!mops->release) |
| 222 | mops->release = ife_release_meta_gen; |
| 223 | |
| 224 | list_add_tail(&mops->list, &ifeoplist); |
| 225 | write_unlock(&ife_mod_lock); |
| 226 | return 0; |
| 227 | } |
| 228 | EXPORT_SYMBOL_GPL(unregister_ife_op); |
| 229 | |
| 230 | int unregister_ife_op(struct tcf_meta_ops *mops) |
| 231 | { |
| 232 | struct tcf_meta_ops *m; |
| 233 | int err = -ENOENT; |
| 234 | |
| 235 | write_lock(&ife_mod_lock); |
| 236 | list_for_each_entry(m, &ifeoplist, list) { |
| 237 | if (m->metaid == mops->metaid) { |
| 238 | list_del(&mops->list); |
| 239 | err = 0; |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | write_unlock(&ife_mod_lock); |
| 244 | |
| 245 | return err; |
| 246 | } |
| 247 | EXPORT_SYMBOL_GPL(register_ife_op); |
| 248 | |
| 249 | static int ife_validate_metatype(struct tcf_meta_ops *ops, void *val, int len) |
| 250 | { |
| 251 | int ret = 0; |
| 252 | /* XXX: unfortunately cant use nla_policy at this point |
| 253 | * because a length of 0 is valid in the case of |
| 254 | * "allow". "use" semantics do enforce for proper |
| 255 | * length and i couldve use nla_policy but it makes it hard |
| 256 | * to use it just for that.. |
| 257 | */ |
| 258 | if (ops->validate) |
| 259 | return ops->validate(val, len); |
| 260 | |
| 261 | if (ops->metatype == NLA_U32) |
| 262 | ret = ife_validate_meta_u32(val, len); |
| 263 | else if (ops->metatype == NLA_U16) |
| 264 | ret = ife_validate_meta_u16(val, len); |
| 265 | |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | /* called when adding new meta information |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 270 | */ |
Cong Wang | c892575 | 2018-08-19 12:22:12 -0700 | [diff] [blame] | 271 | static int load_metaops_and_vet(u32 metaid, void *val, int len) |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 272 | { |
| 273 | struct tcf_meta_ops *ops = find_ife_oplist(metaid); |
| 274 | int ret = 0; |
| 275 | |
| 276 | if (!ops) { |
| 277 | ret = -ENOENT; |
| 278 | #ifdef CONFIG_MODULES |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 279 | rtnl_unlock(); |
| 280 | request_module("ifemeta%u", metaid); |
| 281 | rtnl_lock(); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 282 | ops = find_ife_oplist(metaid); |
| 283 | #endif |
| 284 | } |
| 285 | |
| 286 | if (ops) { |
| 287 | ret = 0; |
| 288 | if (len) |
| 289 | ret = ife_validate_metatype(ops, val, len); |
| 290 | |
| 291 | module_put(ops->owner); |
| 292 | } |
| 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | /* called when adding new meta information |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 298 | */ |
Cong Wang | a93288c | 2018-08-19 12:22:13 -0700 | [diff] [blame] | 299 | static int __add_metainfo(const struct tcf_meta_ops *ops, |
| 300 | struct tcf_ife_info *ife, u32 metaid, void *metaval, |
| 301 | int len, bool atomic, bool exists) |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 302 | { |
| 303 | struct tcf_meta_info *mi = NULL; |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 304 | int ret = 0; |
| 305 | |
WANG Cong | 817e9f2 | 2016-06-20 13:37:19 -0700 | [diff] [blame] | 306 | mi = kzalloc(sizeof(*mi), atomic ? GFP_ATOMIC : GFP_KERNEL); |
Cong Wang | a93288c | 2018-08-19 12:22:13 -0700 | [diff] [blame] | 307 | if (!mi) |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 308 | return -ENOMEM; |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 309 | |
| 310 | mi->metaid = metaid; |
| 311 | mi->ops = ops; |
| 312 | if (len > 0) { |
WANG Cong | 817e9f2 | 2016-06-20 13:37:19 -0700 | [diff] [blame] | 313 | ret = ops->alloc(mi, metaval, atomic ? GFP_ATOMIC : GFP_KERNEL); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 314 | if (ret != 0) { |
| 315 | kfree(mi); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 316 | return ret; |
| 317 | } |
| 318 | } |
| 319 | |
Cong Wang | c892575 | 2018-08-19 12:22:12 -0700 | [diff] [blame] | 320 | if (exists) |
| 321 | spin_lock_bh(&ife->tcf_lock); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 322 | list_add_tail(&mi->metalist, &ife->metalist); |
Cong Wang | c892575 | 2018-08-19 12:22:12 -0700 | [diff] [blame] | 323 | if (exists) |
| 324 | spin_unlock_bh(&ife->tcf_lock); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 325 | |
| 326 | return ret; |
| 327 | } |
| 328 | |
Vlad Buslov | 947c262 | 2018-09-04 00:44:42 +0300 | [diff] [blame] | 329 | static int add_metainfo_and_get_ops(const struct tcf_meta_ops *ops, |
| 330 | struct tcf_ife_info *ife, u32 metaid, |
| 331 | bool exists) |
| 332 | { |
| 333 | int ret; |
| 334 | |
| 335 | if (!try_module_get(ops->owner)) |
| 336 | return -ENOENT; |
| 337 | ret = __add_metainfo(ops, ife, metaid, NULL, 0, true, exists); |
| 338 | if (ret) |
| 339 | module_put(ops->owner); |
| 340 | return ret; |
| 341 | } |
| 342 | |
Cong Wang | a93288c | 2018-08-19 12:22:13 -0700 | [diff] [blame] | 343 | static int add_metainfo(struct tcf_ife_info *ife, u32 metaid, void *metaval, |
| 344 | int len, bool exists) |
| 345 | { |
| 346 | const struct tcf_meta_ops *ops = find_ife_oplist(metaid); |
| 347 | int ret; |
| 348 | |
| 349 | if (!ops) |
| 350 | return -ENOENT; |
| 351 | ret = __add_metainfo(ops, ife, metaid, metaval, len, false, exists); |
| 352 | if (ret) |
| 353 | /*put back what find_ife_oplist took */ |
| 354 | module_put(ops->owner); |
| 355 | return ret; |
| 356 | } |
| 357 | |
Cong Wang | c892575 | 2018-08-19 12:22:12 -0700 | [diff] [blame] | 358 | static int use_all_metadata(struct tcf_ife_info *ife, bool exists) |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 359 | { |
| 360 | struct tcf_meta_ops *o; |
| 361 | int rc = 0; |
| 362 | int installed = 0; |
| 363 | |
WANG Cong | 817e9f2 | 2016-06-20 13:37:19 -0700 | [diff] [blame] | 364 | read_lock(&ife_mod_lock); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 365 | list_for_each_entry(o, &ifeoplist, list) { |
Vlad Buslov | 947c262 | 2018-09-04 00:44:42 +0300 | [diff] [blame] | 366 | rc = add_metainfo_and_get_ops(o, ife, o->metaid, exists); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 367 | if (rc == 0) |
| 368 | installed += 1; |
| 369 | } |
WANG Cong | 817e9f2 | 2016-06-20 13:37:19 -0700 | [diff] [blame] | 370 | read_unlock(&ife_mod_lock); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 371 | |
| 372 | if (installed) |
| 373 | return 0; |
| 374 | else |
| 375 | return -EINVAL; |
| 376 | } |
| 377 | |
| 378 | static int dump_metalist(struct sk_buff *skb, struct tcf_ife_info *ife) |
| 379 | { |
| 380 | struct tcf_meta_info *e; |
| 381 | struct nlattr *nest; |
| 382 | unsigned char *b = skb_tail_pointer(skb); |
| 383 | int total_encoded = 0; |
| 384 | |
| 385 | /*can only happen on decode */ |
| 386 | if (list_empty(&ife->metalist)) |
| 387 | return 0; |
| 388 | |
| 389 | nest = nla_nest_start(skb, TCA_IFE_METALST); |
| 390 | if (!nest) |
| 391 | goto out_nlmsg_trim; |
| 392 | |
| 393 | list_for_each_entry(e, &ife->metalist, metalist) { |
| 394 | if (!e->ops->get(skb, e)) |
| 395 | total_encoded += 1; |
| 396 | } |
| 397 | |
| 398 | if (!total_encoded) |
| 399 | goto out_nlmsg_trim; |
| 400 | |
| 401 | nla_nest_end(skb, nest); |
| 402 | |
| 403 | return 0; |
| 404 | |
| 405 | out_nlmsg_trim: |
| 406 | nlmsg_trim(skb, b); |
| 407 | return -1; |
| 408 | } |
| 409 | |
| 410 | /* under ife->tcf_lock */ |
| 411 | static void _tcf_ife_cleanup(struct tc_action *a, int bind) |
| 412 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 413 | struct tcf_ife_info *ife = to_ife(a); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 414 | struct tcf_meta_info *e, *n; |
| 415 | |
| 416 | list_for_each_entry_safe(e, n, &ife->metalist, metalist) { |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 417 | list_del(&e->metalist); |
| 418 | if (e->metaval) { |
| 419 | if (e->ops->release) |
| 420 | e->ops->release(e); |
| 421 | else |
| 422 | kfree(e->metaval); |
| 423 | } |
Cong Wang | 4fb15ff | 2018-09-03 11:08:15 -0700 | [diff] [blame] | 424 | module_put(e->ops->owner); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 425 | kfree(e); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | static void tcf_ife_cleanup(struct tc_action *a, int bind) |
| 430 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 431 | struct tcf_ife_info *ife = to_ife(a); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 432 | |
| 433 | spin_lock_bh(&ife->tcf_lock); |
| 434 | _tcf_ife_cleanup(a, bind); |
| 435 | spin_unlock_bh(&ife->tcf_lock); |
| 436 | } |
| 437 | |
WANG Cong | 067a7cd | 2016-06-20 13:37:18 -0700 | [diff] [blame] | 438 | static int populate_metalist(struct tcf_ife_info *ife, struct nlattr **tb, |
| 439 | bool exists) |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 440 | { |
| 441 | int len = 0; |
| 442 | int rc = 0; |
| 443 | int i = 0; |
| 444 | void *val; |
| 445 | |
| 446 | for (i = 1; i < max_metacnt; i++) { |
| 447 | if (tb[i]) { |
| 448 | val = nla_data(tb[i]); |
| 449 | len = nla_len(tb[i]); |
| 450 | |
Cong Wang | c892575 | 2018-08-19 12:22:12 -0700 | [diff] [blame] | 451 | rc = load_metaops_and_vet(i, val, len); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 452 | if (rc != 0) |
| 453 | return rc; |
| 454 | |
Cong Wang | a93288c | 2018-08-19 12:22:13 -0700 | [diff] [blame] | 455 | rc = add_metainfo(ife, i, val, len, exists); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 456 | if (rc) |
| 457 | return rc; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | return rc; |
| 462 | } |
| 463 | |
| 464 | static int tcf_ife_init(struct net *net, struct nlattr *nla, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 465 | struct nlattr *est, struct tc_action **a, |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 466 | int ovr, int bind) |
| 467 | { |
| 468 | struct tc_action_net *tn = net_generic(net, ife_net_id); |
| 469 | struct nlattr *tb[TCA_IFE_MAX + 1]; |
| 470 | struct nlattr *tb2[IFE_META_MAX + 1]; |
| 471 | struct tcf_ife_info *ife; |
| 472 | struct tc_ife *parm; |
| 473 | u16 ife_type = 0; |
| 474 | u8 *daddr = NULL; |
| 475 | u8 *saddr = NULL; |
WANG Cong | b231307 | 2016-06-13 13:46:28 -0700 | [diff] [blame] | 476 | bool exists = false; |
| 477 | int ret = 0; |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 478 | int err; |
| 479 | |
Cong Wang | 264e020 | 2019-07-22 21:43:00 -0700 | [diff] [blame] | 480 | if (!nla) |
| 481 | return -EINVAL; |
| 482 | |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 483 | err = nla_parse_nested(tb, TCA_IFE_MAX, nla, ife_policy); |
| 484 | if (err < 0) |
| 485 | return err; |
| 486 | |
| 487 | if (!tb[TCA_IFE_PARMS]) |
| 488 | return -EINVAL; |
| 489 | |
| 490 | parm = nla_data(tb[TCA_IFE_PARMS]); |
| 491 | |
Jamal Hadi Salim | 4e8c861 | 2016-05-10 16:49:31 -0400 | [diff] [blame] | 492 | exists = tcf_hash_check(tn, parm->index, a, bind); |
| 493 | if (exists && bind) |
| 494 | return 0; |
| 495 | |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 496 | if (parm->flags & IFE_ENCODE) { |
| 497 | /* Until we get issued the ethertype, we cant have |
| 498 | * a default.. |
| 499 | **/ |
| 500 | if (!tb[TCA_IFE_TYPE]) { |
Jamal Hadi Salim | 4e8c861 | 2016-05-10 16:49:31 -0400 | [diff] [blame] | 501 | if (exists) |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 502 | tcf_hash_release(*a, bind); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 503 | pr_info("You MUST pass etherype for encoding\n"); |
| 504 | return -EINVAL; |
| 505 | } |
| 506 | } |
| 507 | |
Jamal Hadi Salim | 4e8c861 | 2016-05-10 16:49:31 -0400 | [diff] [blame] | 508 | if (!exists) { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 509 | ret = tcf_hash_create(tn, parm->index, est, a, &act_ife_ops, |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 510 | bind, false); |
| 511 | if (ret) |
| 512 | return ret; |
| 513 | ret = ACT_P_CREATED; |
| 514 | } else { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 515 | tcf_hash_release(*a, bind); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 516 | if (!ovr) |
| 517 | return -EEXIST; |
| 518 | } |
| 519 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 520 | ife = to_ife(*a); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 521 | ife->flags = parm->flags; |
| 522 | |
| 523 | if (parm->flags & IFE_ENCODE) { |
| 524 | ife_type = nla_get_u16(tb[TCA_IFE_TYPE]); |
| 525 | if (tb[TCA_IFE_DMAC]) |
| 526 | daddr = nla_data(tb[TCA_IFE_DMAC]); |
| 527 | if (tb[TCA_IFE_SMAC]) |
| 528 | saddr = nla_data(tb[TCA_IFE_SMAC]); |
| 529 | } |
| 530 | |
WANG Cong | 067a7cd | 2016-06-20 13:37:18 -0700 | [diff] [blame] | 531 | if (exists) |
| 532 | spin_lock_bh(&ife->tcf_lock); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 533 | ife->tcf_action = parm->action; |
Cong Wang | c892575 | 2018-08-19 12:22:12 -0700 | [diff] [blame] | 534 | if (exists) |
| 535 | spin_unlock_bh(&ife->tcf_lock); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 536 | |
| 537 | if (parm->flags & IFE_ENCODE) { |
| 538 | if (daddr) |
| 539 | ether_addr_copy(ife->eth_dst, daddr); |
| 540 | else |
| 541 | eth_zero_addr(ife->eth_dst); |
| 542 | |
| 543 | if (saddr) |
| 544 | ether_addr_copy(ife->eth_src, saddr); |
| 545 | else |
| 546 | eth_zero_addr(ife->eth_src); |
| 547 | |
| 548 | ife->eth_type = ife_type; |
| 549 | } |
| 550 | |
| 551 | if (ret == ACT_P_CREATED) |
| 552 | INIT_LIST_HEAD(&ife->metalist); |
| 553 | |
| 554 | if (tb[TCA_IFE_METALST]) { |
| 555 | err = nla_parse_nested(tb2, IFE_META_MAX, tb[TCA_IFE_METALST], |
| 556 | NULL); |
| 557 | if (err) { |
| 558 | metadata_parse_err: |
Jamal Hadi Salim | 4e8c861 | 2016-05-10 16:49:31 -0400 | [diff] [blame] | 559 | if (exists) |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 560 | tcf_hash_release(*a, bind); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 561 | if (ret == ACT_P_CREATED) |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 562 | _tcf_ife_cleanup(*a, bind); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 563 | return err; |
| 564 | } |
| 565 | |
WANG Cong | 067a7cd | 2016-06-20 13:37:18 -0700 | [diff] [blame] | 566 | err = populate_metalist(ife, tb2, exists); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 567 | if (err) |
| 568 | goto metadata_parse_err; |
| 569 | |
| 570 | } else { |
| 571 | /* if no passed metadata allow list or passed allow-all |
| 572 | * then here we process by adding as many supported metadatum |
| 573 | * as we can. You better have at least one else we are |
| 574 | * going to bail out |
| 575 | */ |
Cong Wang | c892575 | 2018-08-19 12:22:12 -0700 | [diff] [blame] | 576 | err = use_all_metadata(ife, exists); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 577 | if (err) { |
| 578 | if (ret == ACT_P_CREATED) |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 579 | _tcf_ife_cleanup(*a, bind); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 580 | return err; |
| 581 | } |
| 582 | } |
| 583 | |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 584 | if (ret == ACT_P_CREATED) |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 585 | tcf_hash_insert(tn, *a); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 586 | |
| 587 | return ret; |
| 588 | } |
| 589 | |
| 590 | static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind, |
| 591 | int ref) |
| 592 | { |
| 593 | unsigned char *b = skb_tail_pointer(skb); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 594 | struct tcf_ife_info *ife = to_ife(a); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 595 | struct tc_ife opt = { |
| 596 | .index = ife->tcf_index, |
| 597 | .refcnt = ife->tcf_refcnt - ref, |
| 598 | .bindcnt = ife->tcf_bindcnt - bind, |
| 599 | .action = ife->tcf_action, |
| 600 | .flags = ife->flags, |
| 601 | }; |
| 602 | struct tcf_t t; |
| 603 | |
| 604 | if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt)) |
| 605 | goto nla_put_failure; |
| 606 | |
Jamal Hadi Salim | 48d8ee1 | 2016-06-06 06:32:55 -0400 | [diff] [blame] | 607 | tcf_tm_dump(&t, &ife->tcf_tm); |
Nicolas Dichtel | 9854518 | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 608 | if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD)) |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 609 | goto nla_put_failure; |
| 610 | |
| 611 | if (!is_zero_ether_addr(ife->eth_dst)) { |
| 612 | if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, ife->eth_dst)) |
| 613 | goto nla_put_failure; |
| 614 | } |
| 615 | |
| 616 | if (!is_zero_ether_addr(ife->eth_src)) { |
| 617 | if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, ife->eth_src)) |
| 618 | goto nla_put_failure; |
| 619 | } |
| 620 | |
| 621 | if (nla_put(skb, TCA_IFE_TYPE, 2, &ife->eth_type)) |
| 622 | goto nla_put_failure; |
| 623 | |
| 624 | if (dump_metalist(skb, ife)) { |
| 625 | /*ignore failure to dump metalist */ |
| 626 | pr_info("Failed to dump metalist\n"); |
| 627 | } |
| 628 | |
| 629 | return skb->len; |
| 630 | |
| 631 | nla_put_failure: |
| 632 | nlmsg_trim(skb, b); |
| 633 | return -1; |
| 634 | } |
| 635 | |
| 636 | int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_info *ife, |
| 637 | u16 metaid, u16 mlen, void *mdata) |
| 638 | { |
| 639 | struct tcf_meta_info *e; |
| 640 | |
| 641 | /* XXX: use hash to speed up */ |
| 642 | list_for_each_entry(e, &ife->metalist, metalist) { |
| 643 | if (metaid == e->metaid) { |
| 644 | if (e->ops) { |
| 645 | /* We check for decode presence already */ |
| 646 | return e->ops->decode(skb, mdata, mlen); |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
Alexander Aring | e5d0f6a6 | 2018-04-20 15:15:03 -0400 | [diff] [blame] | 651 | return -ENOENT; |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | struct ifeheadr { |
| 655 | __be16 metalen; |
| 656 | u8 tlv_data[]; |
| 657 | }; |
| 658 | |
| 659 | struct meta_tlvhdr { |
| 660 | __be16 type; |
| 661 | __be16 len; |
| 662 | }; |
| 663 | |
| 664 | static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a, |
| 665 | struct tcf_result *res) |
| 666 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 667 | struct tcf_ife_info *ife = to_ife(a); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 668 | int action = ife->tcf_action; |
| 669 | struct ifeheadr *ifehdr = (struct ifeheadr *)skb->data; |
Yotam Gigi | c006da0 | 2016-09-26 13:45:26 +0300 | [diff] [blame] | 670 | int ifehdrln = (int)ifehdr->metalen; |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 671 | struct meta_tlvhdr *tlv = (struct meta_tlvhdr *)(ifehdr->tlv_data); |
| 672 | |
| 673 | spin_lock(&ife->tcf_lock); |
| 674 | bstats_update(&ife->tcf_bstats, skb); |
Jamal Hadi Salim | 9c4a4e4 | 2016-06-06 06:32:53 -0400 | [diff] [blame] | 675 | tcf_lastuse_update(&ife->tcf_tm); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 676 | spin_unlock(&ife->tcf_lock); |
| 677 | |
| 678 | ifehdrln = ntohs(ifehdrln); |
| 679 | if (unlikely(!pskb_may_pull(skb, ifehdrln))) { |
| 680 | spin_lock(&ife->tcf_lock); |
| 681 | ife->tcf_qstats.drops++; |
| 682 | spin_unlock(&ife->tcf_lock); |
| 683 | return TC_ACT_SHOT; |
| 684 | } |
| 685 | |
| 686 | skb_set_mac_header(skb, ifehdrln); |
| 687 | __skb_pull(skb, ifehdrln); |
| 688 | skb->protocol = eth_type_trans(skb, skb->dev); |
| 689 | ifehdrln -= IFE_METAHDRLEN; |
| 690 | |
| 691 | while (ifehdrln > 0) { |
| 692 | u8 *tlvdata = (u8 *)tlv; |
| 693 | u16 mtype = tlv->type; |
| 694 | u16 mlen = tlv->len; |
Jamal Hadi Salim | 28a10c4 | 2016-08-22 07:10:20 -0400 | [diff] [blame] | 695 | u16 alen; |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 696 | |
| 697 | mtype = ntohs(mtype); |
| 698 | mlen = ntohs(mlen); |
Jamal Hadi Salim | 28a10c4 | 2016-08-22 07:10:20 -0400 | [diff] [blame] | 699 | alen = NLA_ALIGN(mlen); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 700 | |
Jamal Hadi Salim | 28a10c4 | 2016-08-22 07:10:20 -0400 | [diff] [blame] | 701 | if (find_decode_metaid(skb, ife, mtype, (mlen - NLA_HDRLEN), |
| 702 | (void *)(tlvdata + NLA_HDRLEN))) { |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 703 | /* abuse overlimits to count when we receive metadata |
| 704 | * but dont have an ops for it |
| 705 | */ |
| 706 | pr_info_ratelimited("Unknown metaid %d alnlen %d\n", |
| 707 | mtype, mlen); |
| 708 | ife->tcf_qstats.overlimits++; |
| 709 | } |
| 710 | |
Jamal Hadi Salim | 28a10c4 | 2016-08-22 07:10:20 -0400 | [diff] [blame] | 711 | tlvdata += alen; |
| 712 | ifehdrln -= alen; |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 713 | tlv = (struct meta_tlvhdr *)tlvdata; |
| 714 | } |
| 715 | |
| 716 | skb_reset_network_header(skb); |
| 717 | return action; |
| 718 | } |
| 719 | |
| 720 | /*XXX: check if we can do this at install time instead of current |
| 721 | * send data path |
| 722 | **/ |
| 723 | static int ife_get_sz(struct sk_buff *skb, struct tcf_ife_info *ife) |
| 724 | { |
| 725 | struct tcf_meta_info *e, *n; |
| 726 | int tot_run_sz = 0, run_sz = 0; |
| 727 | |
| 728 | list_for_each_entry_safe(e, n, &ife->metalist, metalist) { |
| 729 | if (e->ops->check_presence) { |
| 730 | run_sz = e->ops->check_presence(skb, e); |
| 731 | tot_run_sz += run_sz; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | return tot_run_sz; |
| 736 | } |
| 737 | |
| 738 | static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a, |
| 739 | struct tcf_result *res) |
| 740 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 741 | struct tcf_ife_info *ife = to_ife(a); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 742 | int action = ife->tcf_action; |
| 743 | struct ethhdr *oethh; /* outer ether header */ |
| 744 | struct ethhdr *iethh; /* inner eth header */ |
| 745 | struct tcf_meta_info *e; |
| 746 | /* |
| 747 | OUTERHDR:TOTMETALEN:{TLVHDR:Metadatum:TLVHDR..}:ORIGDATA |
| 748 | where ORIGDATA = original ethernet header ... |
| 749 | */ |
| 750 | u16 metalen = ife_get_sz(skb, ife); |
| 751 | int hdrm = metalen + skb->dev->hard_header_len + IFE_METAHDRLEN; |
| 752 | unsigned int skboff = skb->dev->hard_header_len; |
| 753 | u32 at = G_TC_AT(skb->tc_verd); |
| 754 | int new_len = skb->len + hdrm; |
| 755 | bool exceed_mtu = false; |
| 756 | int err; |
| 757 | |
| 758 | if (at & AT_EGRESS) { |
| 759 | if (new_len > skb->dev->mtu) |
| 760 | exceed_mtu = true; |
| 761 | } |
| 762 | |
| 763 | spin_lock(&ife->tcf_lock); |
| 764 | bstats_update(&ife->tcf_bstats, skb); |
Jamal Hadi Salim | 9c4a4e4 | 2016-06-06 06:32:53 -0400 | [diff] [blame] | 765 | tcf_lastuse_update(&ife->tcf_tm); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 766 | |
| 767 | if (!metalen) { /* no metadata to send */ |
| 768 | /* abuse overlimits to count when we allow packet |
| 769 | * with no metadata |
| 770 | */ |
| 771 | ife->tcf_qstats.overlimits++; |
| 772 | spin_unlock(&ife->tcf_lock); |
| 773 | return action; |
| 774 | } |
| 775 | /* could be stupid policy setup or mtu config |
| 776 | * so lets be conservative.. */ |
| 777 | if ((action == TC_ACT_SHOT) || exceed_mtu) { |
| 778 | ife->tcf_qstats.drops++; |
| 779 | spin_unlock(&ife->tcf_lock); |
| 780 | return TC_ACT_SHOT; |
| 781 | } |
| 782 | |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 783 | err = skb_cow_head(skb, hdrm); |
| 784 | if (unlikely(err)) { |
| 785 | ife->tcf_qstats.drops++; |
| 786 | spin_unlock(&ife->tcf_lock); |
| 787 | return TC_ACT_SHOT; |
| 788 | } |
| 789 | |
| 790 | if (!(at & AT_EGRESS)) |
| 791 | skb_push(skb, skb->dev->hard_header_len); |
| 792 | |
Yotam Gigi | 4b1d488 | 2016-09-26 13:45:25 +0300 | [diff] [blame] | 793 | iethh = (struct ethhdr *)skb->data; |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 794 | __skb_push(skb, hdrm); |
| 795 | memcpy(skb->data, iethh, skb->mac_len); |
| 796 | skb_reset_mac_header(skb); |
| 797 | oethh = eth_hdr(skb); |
| 798 | |
| 799 | /*total metadata length */ |
| 800 | metalen += IFE_METAHDRLEN; |
| 801 | metalen = htons(metalen); |
| 802 | memcpy((skb->data + skboff), &metalen, IFE_METAHDRLEN); |
| 803 | skboff += IFE_METAHDRLEN; |
| 804 | |
| 805 | /* XXX: we dont have a clever way of telling encode to |
| 806 | * not repeat some of the computations that are done by |
| 807 | * ops->presence_check... |
| 808 | */ |
| 809 | list_for_each_entry(e, &ife->metalist, metalist) { |
| 810 | if (e->ops->encode) { |
| 811 | err = e->ops->encode(skb, (void *)(skb->data + skboff), |
| 812 | e); |
| 813 | } |
| 814 | if (err < 0) { |
| 815 | /* too corrupt to keep around if overwritten */ |
| 816 | ife->tcf_qstats.drops++; |
| 817 | spin_unlock(&ife->tcf_lock); |
| 818 | return TC_ACT_SHOT; |
| 819 | } |
| 820 | skboff += err; |
| 821 | } |
| 822 | |
| 823 | if (!is_zero_ether_addr(ife->eth_src)) |
| 824 | ether_addr_copy(oethh->h_source, ife->eth_src); |
| 825 | else |
| 826 | ether_addr_copy(oethh->h_source, iethh->h_source); |
| 827 | if (!is_zero_ether_addr(ife->eth_dst)) |
| 828 | ether_addr_copy(oethh->h_dest, ife->eth_dst); |
| 829 | else |
| 830 | ether_addr_copy(oethh->h_dest, iethh->h_dest); |
| 831 | oethh->h_proto = htons(ife->eth_type); |
| 832 | |
| 833 | if (!(at & AT_EGRESS)) |
| 834 | skb_pull(skb, skb->dev->hard_header_len); |
| 835 | |
| 836 | spin_unlock(&ife->tcf_lock); |
| 837 | |
| 838 | return action; |
| 839 | } |
| 840 | |
| 841 | static int tcf_ife_act(struct sk_buff *skb, const struct tc_action *a, |
| 842 | struct tcf_result *res) |
| 843 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 844 | struct tcf_ife_info *ife = to_ife(a); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 845 | |
| 846 | if (ife->flags & IFE_ENCODE) |
| 847 | return tcf_ife_encode(skb, a, res); |
| 848 | |
| 849 | if (!(ife->flags & IFE_ENCODE)) |
| 850 | return tcf_ife_decode(skb, a, res); |
| 851 | |
| 852 | pr_info_ratelimited("unknown failure(policy neither de/encode\n"); |
| 853 | spin_lock(&ife->tcf_lock); |
| 854 | bstats_update(&ife->tcf_bstats, skb); |
Jamal Hadi Salim | 9c4a4e4 | 2016-06-06 06:32:53 -0400 | [diff] [blame] | 855 | tcf_lastuse_update(&ife->tcf_tm); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 856 | ife->tcf_qstats.drops++; |
| 857 | spin_unlock(&ife->tcf_lock); |
| 858 | |
| 859 | return TC_ACT_SHOT; |
| 860 | } |
| 861 | |
| 862 | static int tcf_ife_walker(struct net *net, struct sk_buff *skb, |
| 863 | struct netlink_callback *cb, int type, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 864 | const struct tc_action_ops *ops) |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 865 | { |
| 866 | struct tc_action_net *tn = net_generic(net, ife_net_id); |
| 867 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 868 | return tcf_generic_walker(tn, skb, cb, type, ops); |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 869 | } |
| 870 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 871 | static int tcf_ife_search(struct net *net, struct tc_action **a, u32 index) |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 872 | { |
| 873 | struct tc_action_net *tn = net_generic(net, ife_net_id); |
| 874 | |
| 875 | return tcf_hash_search(tn, a, index); |
| 876 | } |
| 877 | |
| 878 | static struct tc_action_ops act_ife_ops = { |
| 879 | .kind = "ife", |
| 880 | .type = TCA_ACT_IFE, |
| 881 | .owner = THIS_MODULE, |
| 882 | .act = tcf_ife_act, |
| 883 | .dump = tcf_ife_dump, |
| 884 | .cleanup = tcf_ife_cleanup, |
| 885 | .init = tcf_ife_init, |
| 886 | .walk = tcf_ife_walker, |
| 887 | .lookup = tcf_ife_search, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 888 | .size = sizeof(struct tcf_ife_info), |
Jamal Hadi Salim | ef6980b | 2016-02-27 08:08:54 -0500 | [diff] [blame] | 889 | }; |
| 890 | |
| 891 | static __net_init int ife_init_net(struct net *net) |
| 892 | { |
| 893 | struct tc_action_net *tn = net_generic(net, ife_net_id); |
| 894 | |
| 895 | return tc_action_net_init(tn, &act_ife_ops, IFE_TAB_MASK); |
| 896 | } |
| 897 | |
| 898 | static void __net_exit ife_exit_net(struct net *net) |
| 899 | { |
| 900 | struct tc_action_net *tn = net_generic(net, ife_net_id); |
| 901 | |
| 902 | tc_action_net_exit(tn); |
| 903 | } |
| 904 | |
| 905 | static struct pernet_operations ife_net_ops = { |
| 906 | .init = ife_init_net, |
| 907 | .exit = ife_exit_net, |
| 908 | .id = &ife_net_id, |
| 909 | .size = sizeof(struct tc_action_net), |
| 910 | }; |
| 911 | |
| 912 | static int __init ife_init_module(void) |
| 913 | { |
| 914 | return tcf_register_action(&act_ife_ops, &ife_net_ops); |
| 915 | } |
| 916 | |
| 917 | static void __exit ife_cleanup_module(void) |
| 918 | { |
| 919 | tcf_unregister_action(&act_ife_ops, &ife_net_ops); |
| 920 | } |
| 921 | |
| 922 | module_init(ife_init_module); |
| 923 | module_exit(ife_cleanup_module); |
| 924 | |
| 925 | MODULE_AUTHOR("Jamal Hadi Salim(2015)"); |
| 926 | MODULE_DESCRIPTION("Inter-FE LFB action"); |
| 927 | MODULE_LICENSE("GPL"); |