cregit-Linux how code gets into the kernel

Release 4.8 net/sched/act_ife.c

Directory: net/sched
/*
 * net/sched/ife.c      Inter-FE action based on ForCES WG InterFE LFB
 *
 *              Refer to:
 *              draft-ietf-forces-interfelfb-03
 *              and
 *              netdev01 paper:
 *              "Distributing Linux Traffic Control Classifier-Action
 *              Subsystem"
 *              Authors: Jamal Hadi Salim and Damascene M. Joachimpillai
 *
 *              This program is free software; you can redistribute it and/or
 *              modify it under the terms of the GNU General Public License
 *              as published by the Free Software Foundation; either version
 *              2 of the License, or (at your option) any later version.
 *
 * copyright Jamal Hadi Salim (2015)
 *
*/

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
#include <linux/rtnetlink.h>
#include <linux/module.h>
#include <linux/init.h>
#include <net/net_namespace.h>
#include <net/netlink.h>
#include <net/pkt_sched.h>
#include <uapi/linux/tc_act/tc_ife.h>
#include <net/tc_act/tc_ife.h>
#include <linux/etherdevice.h>


#define IFE_TAB_MASK 15


static int ife_net_id;

static int max_metacnt = IFE_META_MAX + 1;

static struct tc_action_ops act_ife_ops;


static const struct nla_policy ife_policy[TCA_IFE_MAX + 1] = {
	[TCA_IFE_PARMS] = { .len = sizeof(struct tc_ife)},
	[TCA_IFE_DMAC] = { .len = ETH_ALEN},
	[TCA_IFE_SMAC] = { .len = ETH_ALEN},
	[TCA_IFE_TYPE] = { .type = NLA_U16},
};

/* Caller takes care of presenting data in network order
*/

int ife_tlv_meta_encode(void *skbdata, u16 attrtype, u16 dlen, const void *dval) { u32 *tlv = (u32 *)(skbdata); u16 totlen = nla_total_size(dlen); /*alignment + hdr */ char *dptr = (char *)tlv + NLA_HDRLEN; u32 htlv = attrtype << 16 | (dlen + NLA_HDRLEN); *tlv = htonl(htlv); memset(dptr, 0, totlen - NLA_HDRLEN); memcpy(dptr, dval, dlen); return totlen; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim9395.88%266.67%
yotam gigiyotam gigi44.12%133.33%
Total97100.00%3100.00%

EXPORT_SYMBOL_GPL(ife_tlv_meta_encode);
int ife_get_meta_u32(struct sk_buff *skb, struct tcf_meta_info *mi) { if (mi->metaval) return nla_put_u32(skb, mi->metaid, *(u32 *)mi->metaval); else return nla_put(skb, mi->metaid, 0, NULL); }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim55100.00%1100.00%
Total55100.00%1100.00%

EXPORT_SYMBOL_GPL(ife_get_meta_u32);
int ife_check_meta_u32(u32 metaval, struct tcf_meta_info *mi) { if (metaval || mi->metaval) return 8; /* T+L+V == 2+2+4 */ return 0; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim28100.00%1100.00%
Total28100.00%1100.00%

EXPORT_SYMBOL_GPL(ife_check_meta_u32);
int ife_encode_meta_u32(u32 metaval, void *skbdata, struct tcf_meta_info *mi) { u32 edata = metaval; if (mi->metaval) edata = *(u32 *)mi->metaval; else if (metaval) edata = metaval; if (!edata) /* will not encode */ return 0; edata = htonl(edata); return ife_tlv_meta_encode(skbdata, mi->metaid, 4, &edata); }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim79100.00%1100.00%
Total79100.00%1100.00%

EXPORT_SYMBOL_GPL(ife_encode_meta_u32);
int ife_get_meta_u16(struct sk_buff *skb, struct tcf_meta_info *mi) { if (mi->metaval) return nla_put_u16(skb, mi->metaid, *(u16 *)mi->metaval); else return nla_put(skb, mi->metaid, 0, NULL); }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim55100.00%1100.00%
Total55100.00%1100.00%

EXPORT_SYMBOL_GPL(ife_get_meta_u16);
int ife_alloc_meta_u32(struct tcf_meta_info *mi, void *metaval, gfp_t gfp) { mi->metaval = kmemdup(metaval, sizeof(u32), gfp); if (!mi->metaval) return -ENOMEM; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim4391.49%150.00%
americo wangamerico wang48.51%150.00%
Total47100.00%2100.00%

EXPORT_SYMBOL_GPL(ife_alloc_meta_u32);
int ife_alloc_meta_u16(struct tcf_meta_info *mi, void *metaval, gfp_t gfp) { mi->metaval = kmemdup(metaval, sizeof(u16), gfp); if (!mi->metaval) return -ENOMEM; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim4391.49%150.00%
americo wangamerico wang48.51%150.00%
Total47100.00%2100.00%

EXPORT_SYMBOL_GPL(ife_alloc_meta_u16);
void ife_release_meta_gen(struct tcf_meta_info *mi) { kfree(mi->metaval); }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim17100.00%1100.00%
Total17100.00%1100.00%

EXPORT_SYMBOL_GPL(ife_release_meta_gen);
int ife_validate_meta_u32(void *val, int len) { if (len == sizeof(u32)) return 0; return -EINVAL; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim28100.00%2100.00%
Total28100.00%2100.00%

EXPORT_SYMBOL_GPL(ife_validate_meta_u32);
int ife_validate_meta_u16(void *val, int len) { /* length will not include padding */ if (len == sizeof(u16)) return 0; return -EINVAL; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim29100.00%2100.00%
Total29100.00%2100.00%

EXPORT_SYMBOL_GPL(ife_validate_meta_u16); static LIST_HEAD(ifeoplist); static DEFINE_RWLOCK(ife_mod_lock);
static struct tcf_meta_ops *find_ife_oplist(u16 metaid) { struct tcf_meta_ops *o; read_lock(&ife_mod_lock); list_for_each_entry(o, &ifeoplist, list) { if (o->metaid == metaid) { if (!try_module_get(o->owner)) o = NULL; read_unlock(&ife_mod_lock); return o; } } read_unlock(&ife_mod_lock); return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim74100.00%1100.00%
Total74100.00%1100.00%


int register_ife_op(struct tcf_meta_ops *mops) { struct tcf_meta_ops *m; if (!mops->metaid || !mops->metatype || !mops->name || !mops->check_presence || !mops->encode || !mops->decode || !mops->get || !mops->alloc) return -EINVAL; write_lock(&ife_mod_lock); list_for_each_entry(m, &ifeoplist, list) { if (m->metaid == mops->metaid || (strcmp(mops->name, m->name) == 0)) { write_unlock(&ife_mod_lock); return -EEXIST; } } if (!mops->release) mops->release = ife_release_meta_gen; list_add_tail(&mops->list, &ifeoplist); write_unlock(&ife_mod_lock); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim147100.00%1100.00%
Total147100.00%1100.00%

EXPORT_SYMBOL_GPL(unregister_ife_op);
int unregister_ife_op(struct tcf_meta_ops *mops) { struct tcf_meta_ops *m; int err = -ENOENT; write_lock(&ife_mod_lock); list_for_each_entry(m, &ifeoplist, list) { if (m->metaid == mops->metaid) { list_del(&mops->list); err = 0; break; } } write_unlock(&ife_mod_lock); return err; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim71100.00%1100.00%
Total71100.00%1100.00%

EXPORT_SYMBOL_GPL(register_ife_op);
static int ife_validate_metatype(struct tcf_meta_ops *ops, void *val, int len) { int ret = 0; /* XXX: unfortunately cant use nla_policy at this point * because a length of 0 is valid in the case of * "allow". "use" semantics do enforce for proper * length and i couldve use nla_policy but it makes it hard * to use it just for that.. */ if (ops->validate) return ops->validate(val, len); if (ops->metatype == NLA_U32) ret = ife_validate_meta_u32(val, len); else if (ops->metatype == NLA_U16) ret = ife_validate_meta_u16(val, len); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim78100.00%1100.00%
Total78100.00%1100.00%

/* called when adding new meta information * under ife->tcf_lock for existing action */
static int load_metaops_and_vet(struct tcf_ife_info *ife, u32 metaid, void *val, int len, bool exists) { struct tcf_meta_ops *ops = find_ife_oplist(metaid); int ret = 0; if (!ops) { ret = -ENOENT; #ifdef CONFIG_MODULES if (exists) spin_unlock_bh(&ife->tcf_lock); rtnl_unlock(); request_module("ifemeta%u", metaid); rtnl_lock(); if (exists) spin_lock_bh(&ife->tcf_lock); ops = find_ife_oplist(metaid); #endif } if (ops) { ret = 0; if (len) ret = ife_validate_metatype(ops, val, len); module_put(ops->owner); } return ret; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim12491.85%150.00%
americo wangamerico wang118.15%150.00%
Total135100.00%2100.00%

/* called when adding new meta information * under ife->tcf_lock for existing action */
static int add_metainfo(struct tcf_ife_info *ife, u32 metaid, void *metaval, int len, bool atomic) { struct tcf_meta_info *mi = NULL; struct tcf_meta_ops *ops = find_ife_oplist(metaid); int ret = 0; if (!ops) return -ENOENT; mi = kzalloc(sizeof(*mi), atomic ? GFP_ATOMIC : GFP_KERNEL); if (!mi) { /*put back what find_ife_oplist took */ module_put(ops->owner); return -ENOMEM; } mi->metaid = metaid; mi->ops = ops; if (len > 0) { ret = ops->alloc(mi, metaval, atomic ? GFP_ATOMIC : GFP_KERNEL); if (ret != 0) { kfree(mi); module_put(ops->owner); return ret; } } list_add_tail(&mi->metalist, &ife->metalist); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim15492.22%133.33%
americo wangamerico wang137.78%266.67%
Total167100.00%3100.00%


static int use_all_metadata(struct tcf_ife_info *ife) { struct tcf_meta_ops *o; int rc = 0; int installed = 0; read_lock(&ife_mod_lock); list_for_each_entry(o, &ifeoplist, list) { rc = add_metainfo(ife, o->metaid, NULL, 0, true); if (rc == 0) installed += 1; } read_unlock(&ife_mod_lock); if (installed) return 0; else return -EINVAL; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim7383.91%133.33%
americo wangamerico wang1416.09%266.67%
Total87100.00%3100.00%


static int dump_metalist(struct sk_buff *skb, struct tcf_ife_info *ife) { struct tcf_meta_info *e; struct nlattr *nest; unsigned char *b = skb_tail_pointer(skb); int total_encoded = 0; /*can only happen on decode */ if (list_empty(&ife->metalist)) return 0; nest = nla_nest_start(skb, TCA_IFE_METALST); if (!nest) goto out_nlmsg_trim; list_for_each_entry(e, &ife->metalist, metalist) { if (!e->ops->get(skb, e)) total_encoded += 1; } if (!total_encoded) goto out_nlmsg_trim; nla_nest_end(skb, nest); return 0; out_nlmsg_trim: nlmsg_trim(skb, b); return -1; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim131100.00%1100.00%
Total131100.00%1100.00%

/* under ife->tcf_lock */
static void _tcf_ife_cleanup(struct tc_action *a, int bind) { struct tcf_ife_info *ife = to_ife(a); struct tcf_meta_info *e, *n; list_for_each_entry_safe(e, n, &ife->metalist, metalist) { module_put(e->ops->owner); list_del(&e->metalist); if (e->metaval) { if (e->ops->release) e->ops->release(e); else kfree(e->metaval); } kfree(e); } }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim9696.97%150.00%
americo wangamerico wang33.03%150.00%
Total99100.00%2100.00%


static void tcf_ife_cleanup(struct tc_action *a, int bind) { struct tcf_ife_info *ife = to_ife(a); spin_lock_bh(&ife->tcf_lock); _tcf_ife_cleanup(a, bind); spin_unlock_bh(&ife->tcf_lock); }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim4493.62%150.00%
americo wangamerico wang36.38%150.00%
Total47100.00%2100.00%

/* under ife->tcf_lock for existing action */
static int populate_metalist(struct tcf_ife_info *ife, struct nlattr **tb, bool exists) { int len = 0; int rc = 0; int i = 0; void *val; for (i = 1; i < max_metacnt; i++) { if (tb[i]) { val = nla_data(tb[i]); len = nla_len(tb[i]); rc = load_metaops_and_vet(ife, i, val, len, exists); if (rc != 0) return rc; rc = add_metainfo(ife, i, val, len, exists); if (rc) return rc; } } return rc; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim12594.70%150.00%
americo wangamerico wang75.30%150.00%
Total132100.00%2100.00%


static int tcf_ife_init(struct net *net, struct nlattr *nla, struct nlattr *est, struct tc_action **a, int ovr, int bind) { struct tc_action_net *tn = net_generic(net, ife_net_id); struct nlattr *tb[TCA_IFE_MAX + 1]; struct nlattr *tb2[IFE_META_MAX + 1]; struct tcf_ife_info *ife; struct tc_ife *parm; u16 ife_type = 0; u8 *daddr = NULL; u8 *saddr = NULL; bool exists = false; int ret = 0; int err; err = nla_parse_nested(tb, TCA_IFE_MAX, nla, ife_policy); if (err < 0) return err; if (!tb[TCA_IFE_PARMS]) return -EINVAL; parm = nla_data(tb[TCA_IFE_PARMS]); exists = tcf_hash_check(tn, parm->index, a, bind); if (exists && bind) return 0; if (parm->flags & IFE_ENCODE) { /* Until we get issued the ethertype, we cant have * a default.. **/ if (!tb[TCA_IFE_TYPE]) { if (exists) tcf_hash_release(*a, bind); pr_info("You MUST pass etherype for encoding\n"); return -EINVAL; } } if (!exists) { ret = tcf_hash_create(tn, parm->index, est, a, &act_ife_ops, bind, false); if (ret) return ret; ret = ACT_P_CREATED; } else { tcf_hash_release(*a, bind); if (!ovr) return -EEXIST; } ife = to_ife(*a); ife->flags = parm->flags; if (parm->flags & IFE_ENCODE) { ife_type = nla_get_u16(tb[TCA_IFE_TYPE]); if (tb[TCA_IFE_DMAC]) daddr = nla_data(tb[TCA_IFE_DMAC]); if (tb[TCA_IFE_SMAC]) saddr = nla_data(tb[TCA_IFE_SMAC]); } if (exists) spin_lock_bh(&ife->tcf_lock); ife->tcf_action = parm->action; if (parm->flags & IFE_ENCODE) { if (daddr) ether_addr_copy(ife->eth_dst, daddr); else eth_zero_addr(ife->eth_dst); if (saddr) ether_addr_copy(ife->eth_src, saddr); else eth_zero_addr(ife->eth_src); ife->eth_type = ife_type; } if (ret == ACT_P_CREATED) INIT_LIST_HEAD(&ife->metalist); if (tb[TCA_IFE_METALST]) { err = nla_parse_nested(tb2, IFE_META_MAX, tb[TCA_IFE_METALST], NULL); if (err) { metadata_parse_err: if (exists) tcf_hash_release(*a, bind); if (ret == ACT_P_CREATED) _tcf_ife_cleanup(*a, bind); if (exists) spin_unlock_bh(&ife->tcf_lock); return err; } err = populate_metalist(ife, tb2, exists); if (err) goto metadata_parse_err; } else { /* if no passed metadata allow list or passed allow-all * then here we process by adding as many supported metadatum * as we can. You better have at least one else we are * going to bail out */ err = use_all_metadata(ife); if (err) { if (ret == ACT_P_CREATED) _tcf_ife_cleanup(*a, bind); if (exists) spin_unlock_bh(&ife->tcf_lock); return err; } } if (exists) spin_unlock_bh(&ife->tcf_lock); if (ret == ACT_P_CREATED) tcf_hash_insert(tn, *a); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim57194.54%240.00%
americo wangamerico wang335.46%360.00%
Total604100.00%5100.00%


static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref) { unsigned char *b = skb_tail_pointer(skb); struct tcf_ife_info *ife = to_ife(a); struct tc_ife opt = { .index = ife->tcf_index, .refcnt = ife->tcf_refcnt - ref, .bindcnt = ife->tcf_bindcnt - bind, .action = ife->tcf_action, .flags = ife->flags, }; struct tcf_t t; if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt)) goto nla_put_failure; tcf_tm_dump(&t, &ife->tcf_tm); if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD)) goto nla_put_failure; if (!is_zero_ether_addr(ife->eth_dst)) { if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, ife->eth_dst)) goto nla_put_failure; } if (!is_zero_ether_addr(ife->eth_src)) { if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, ife->eth_src)) goto nla_put_failure; } if (nla_put(skb, TCA_IFE_TYPE, 2, &ife->eth_type)) goto nla_put_failure; if (dump_metalist(skb, ife)) { /*ignore failure to dump metalist */ pr_info("Failed to dump metalist\n"); } return skb->len; nla_put_failure: nlmsg_trim(skb, b); return -1; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim25297.67%250.00%
americo wangamerico wang31.16%125.00%
nicolas dichtelnicolas dichtel31.16%125.00%
Total258100.00%4100.00%


int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_info *ife, u16 metaid, u16 mlen, void *mdata) { struct tcf_meta_info *e; /* XXX: use hash to speed up */ list_for_each_entry(e, &ife->metalist, metalist) { if (metaid == e->metaid) { if (e->ops) { /* We check for decode presence already */ return e->ops->decode(skb, mdata, mlen); } } } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
jamal hadi salimjamal hadi salim77100.00%1100.00%
Total77100.00%1100.00%

struct ifeheadr { __be16 metalen; u8 tlv_data[]; }; struct meta_tlvhdr { __be16 type; __be16 len; };
static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a, struct tcf_result *res) { struct tcf_ife_info *ife = to_ife(a); int action = ife->tcf_action; struct ifeheadr *ifehdr = (struct ifeheadr *)skb->data; int ifehdrln = (int)ifehdr->metalen; struct meta_tlvhdr *tlv = (struct meta_tlvhdr *)(ifehdr->tlv_data); spin_lock(&ife->tcf_lock); bstats_update(&ife->tcf_bstats, skb); tcf_lastuse_update(&ife->tcf_tm); spin_unlock(&ife->tcf_lock); ifehdrln = ntohs(ifehdrln); if (unlikely(!pskb_may_pull(skb, ifehdrln))) { spin_lock(&ife->tcf_lock); ife->tcf_qstats.drops++; spin_unlock(&ife->tcf_lock); return TC_ACT_SHOT; } skb_set_mac_header(skb, ifehdrln); __skb_pull(skb, ifehdrln); skb->protocol = eth_type_trans(skb, skb->dev); ifehdrln -= IFE_METAHDRLEN; while (ifehdrln > 0) { u8 *tlvdata = (u8 *)tlv; u16 mtype = tlv->type; u16 mlen = tlv->len; u16 alen; mtype = ntohs(mtype); mlen = ntohs(mlen); alen = NLA_ALIGN(mlen); if (find_decode_metaid(skb, ife, mtype, (mlen - NLA_HDRLEN), (void *)(tlvdata + NLA_HDRLEN))) { /* abuse overlimits to count when we receive metadata * but dont have an ops for it */ pr_info_ratelimited("Unknown metaid %d alnlen %d\n", mtype, mlen); ife->tcf_qstats.overlimits++; } tlvdata += alen; ifehdrln -= alen; tlv =