123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- /*
- * lib/route/cls/fw.c fw classifier
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation version 2.1
- * of the License.
- *
- * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
- * Copyright (c) 2006 Petr Gotthard <petr.gotthard@siemens.com>
- * Copyright (c) 2006 Siemens AG Oesterreich
- */
- /**
- * @ingroup cls_api
- * @defgroup fw Firewall Classifier
- *
- * @{
- */
- #include <netlink-local.h>
- #include <netlink-tc.h>
- #include <netlink/netlink.h>
- #include <netlink/route/classifier.h>
- #include <netlink/route/classifier-modules.h>
- #include <netlink/route/cls/fw.h>
- /** @cond SKIP */
- #define FW_ATTR_CLASSID 0x001
- #define FW_ATTR_ACTION 0x002
- #define FW_ATTR_POLICE 0x004
- #define FW_ATTR_INDEV 0x008
- /** @endcond */
- static inline struct rtnl_fw *fw_cls(struct rtnl_cls *cls)
- {
- return (struct rtnl_fw *) cls->c_subdata;
- }
- static inline struct rtnl_fw *fw_alloc(struct rtnl_cls *cls)
- {
- if (!cls->c_subdata)
- cls->c_subdata = calloc(1, sizeof(struct rtnl_fw));
- return fw_cls(cls);
- }
- static struct nla_policy fw_policy[TCA_FW_MAX+1] = {
- [TCA_FW_CLASSID] = { .type = NLA_U32 },
- [TCA_FW_INDEV] = { .type = NLA_STRING,
- .maxlen = IFNAMSIZ },
- };
- static int fw_msg_parser(struct rtnl_cls *cls)
- {
- int err;
- struct nlattr *tb[TCA_FW_MAX + 1];
- struct rtnl_fw *f;
- err = tca_parse(tb, TCA_FW_MAX, (struct rtnl_tca *) cls, fw_policy);
- if (err < 0)
- return err;
- f = fw_alloc(cls);
- if (!f)
- goto errout_nomem;
- if (tb[TCA_FW_CLASSID]) {
- f->cf_classid = nla_get_u32(tb[TCA_FW_CLASSID]);
- f->cf_mask |= FW_ATTR_CLASSID;
- }
- if (tb[TCA_FW_ACT]) {
- f->cf_act = nla_get_data(tb[TCA_FW_ACT]);
- if (!f->cf_act)
- goto errout_nomem;
- f->cf_mask |= FW_ATTR_ACTION;
- }
- if (tb[TCA_FW_POLICE]) {
- f->cf_police = nla_get_data(tb[TCA_FW_POLICE]);
- if (!f->cf_police)
- goto errout_nomem;
- f->cf_mask |= FW_ATTR_POLICE;
- }
- if (tb[TCA_FW_INDEV]) {
- nla_strlcpy(f->cf_indev, tb[TCA_FW_INDEV], IFNAMSIZ);
- f->cf_mask |= FW_ATTR_INDEV;
- }
- return 0;
- errout_nomem:
- err = nl_errno(ENOMEM);
- return err;
- }
- static void fw_free_data(struct rtnl_cls *cls)
- {
- struct rtnl_fw *f = fw_cls(cls);
- if (!f)
- return;
- nl_data_free(f->cf_act);
- nl_data_free(f->cf_police);
- free(cls->c_subdata);
- }
- static int fw_clone(struct rtnl_cls *_dst, struct rtnl_cls *_src)
- {
- struct rtnl_fw *dst, *src = fw_cls(_src);
- if (!src)
- return 0;
- dst = fw_alloc(_dst);
- if (!dst)
- return nl_errno(ENOMEM);
- if (src->cf_act)
- if (!(dst->cf_act = nl_data_clone(src->cf_act)))
- goto errout;
-
- if (src->cf_police)
- if (!(dst->cf_police = nl_data_clone(src->cf_police)))
- goto errout;
- return 0;
- errout:
- return nl_get_errno();
- }
- static int fw_dump_brief(struct rtnl_cls *cls, struct nl_dump_params *p,
- int line)
- {
- struct rtnl_fw *f = fw_cls(cls);
- char buf[32];
- if (!f)
- goto ignore;
- if (f->cf_mask & FW_ATTR_CLASSID)
- dp_dump(p, " target %s",
- rtnl_tc_handle2str(f->cf_classid, buf, sizeof(buf)));
- ignore:
- return line;
- }
- static int fw_dump_full(struct rtnl_cls *cls, struct nl_dump_params *p,
- int line)
- {
- struct rtnl_fw *f = fw_cls(cls);
- if (!f)
- goto ignore;
- if (f->cf_mask & FW_ATTR_INDEV)
- dp_dump(p, "indev %s ", f->cf_indev);
- ignore:
- return line;
- }
- static int fw_dump_stats(struct rtnl_cls *cls, struct nl_dump_params *p,
- int line)
- {
- struct rtnl_fw *f = fw_cls(cls);
- if (!f)
- goto ignore;
- ignore:
- return line;
- }
- static struct nl_msg *fw_get_opts(struct rtnl_cls *cls)
- {
- struct rtnl_fw *f;
- struct nl_msg *msg;
-
- f = fw_cls(cls);
- if (!f)
- return NULL;
- msg = nlmsg_alloc();
- if (!msg)
- return NULL;
- if (f->cf_mask & FW_ATTR_CLASSID)
- nla_put_u32(msg, TCA_FW_CLASSID, f->cf_classid);
- if (f->cf_mask & FW_ATTR_ACTION)
- nla_put_data(msg, TCA_FW_ACT, f->cf_act);
- if (f->cf_mask & FW_ATTR_POLICE)
- nla_put_data(msg, TCA_FW_POLICE, f->cf_police);
- if (f->cf_mask & FW_ATTR_INDEV)
- nla_put_string(msg, TCA_FW_INDEV, f->cf_indev);
- return msg;
- }
- /**
- * @name Attribute Modifications
- * @{
- */
- int rtnl_fw_set_classid(struct rtnl_cls *cls, uint32_t classid)
- {
- struct rtnl_fw *f;
-
- f = fw_alloc(cls);
- if (!f)
- return nl_errno(ENOMEM);
- f->cf_classid = classid;
- f->cf_mask |= FW_ATTR_CLASSID;
- return 0;
- }
- /** @} */
- static struct rtnl_cls_ops fw_ops = {
- .co_kind = "fw",
- .co_msg_parser = fw_msg_parser,
- .co_free_data = fw_free_data,
- .co_clone = fw_clone,
- .co_get_opts = fw_get_opts,
- .co_dump[NL_DUMP_BRIEF] = fw_dump_brief,
- .co_dump[NL_DUMP_FULL] = fw_dump_full,
- .co_dump[NL_DUMP_STATS] = fw_dump_stats,
- };
- static void __init fw_init(void)
- {
- rtnl_cls_register(&fw_ops);
- }
- static void __exit fw_exit(void)
- {
- rtnl_cls_unregister(&fw_ops);
- }
- /** @} */
|