Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __NET_PKT_CLS_H |
| 2 | #define __NET_PKT_CLS_H |
| 3 | |
| 4 | #include <linux/pkt_cls.h> |
| 5 | #include <net/sch_generic.h> |
| 6 | #include <net/act_api.h> |
| 7 | |
| 8 | /* Basic packet classifier frontend definitions. */ |
| 9 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 10 | struct tcf_walker { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | int stop; |
| 12 | int skip; |
| 13 | int count; |
| 14 | int (*fn)(struct tcf_proto *, unsigned long node, struct tcf_walker *); |
| 15 | }; |
| 16 | |
Joe Perches | 5c15257 | 2013-07-30 22:47:13 -0700 | [diff] [blame] | 17 | int register_tcf_proto_ops(struct tcf_proto_ops *ops); |
| 18 | int unregister_tcf_proto_ops(struct tcf_proto_ops *ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Jiri Pirko | 8ae7003 | 2017-02-15 11:57:50 +0100 | [diff] [blame] | 20 | #ifdef CONFIG_NET_CLS |
WANG Cong | 367a8ce | 2017-05-23 09:42:37 -0700 | [diff] [blame] | 21 | struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index, |
| 22 | bool create); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 23 | void tcf_chain_put(struct tcf_chain *chain); |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 24 | int tcf_block_get(struct tcf_block **p_block, |
| 25 | struct tcf_proto __rcu **p_filter_chain); |
| 26 | void tcf_block_put(struct tcf_block *block); |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 27 | int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
| 28 | struct tcf_result *res, bool compat_mode); |
| 29 | |
Jiri Pirko | 8ae7003 | 2017-02-15 11:57:50 +0100 | [diff] [blame] | 30 | #else |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 31 | static inline |
| 32 | int tcf_block_get(struct tcf_block **p_block, |
| 33 | struct tcf_proto __rcu **p_filter_chain) |
| 34 | { |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | static inline void tcf_block_put(struct tcf_block *block) |
Jiri Pirko | 8ae7003 | 2017-02-15 11:57:50 +0100 | [diff] [blame] | 39 | { |
| 40 | } |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 41 | |
| 42 | static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
| 43 | struct tcf_result *res, bool compat_mode) |
| 44 | { |
| 45 | return TC_ACT_UNSPEC; |
| 46 | } |
Jiri Pirko | 8ae7003 | 2017-02-15 11:57:50 +0100 | [diff] [blame] | 47 | #endif |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | static inline unsigned long |
| 50 | __cls_set_class(unsigned long *clp, unsigned long cl) |
| 51 | { |
WANG Cong | a0efb80 | 2014-09-30 16:07:24 -0700 | [diff] [blame] | 52 | return xchg(clp, cl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static inline unsigned long |
| 56 | cls_set_class(struct tcf_proto *tp, unsigned long *clp, |
| 57 | unsigned long cl) |
| 58 | { |
| 59 | unsigned long old_cl; |
| 60 | |
| 61 | tcf_tree_lock(tp); |
| 62 | old_cl = __cls_set_class(clp, cl); |
| 63 | tcf_tree_unlock(tp); |
| 64 | |
| 65 | return old_cl; |
| 66 | } |
| 67 | |
| 68 | static inline void |
| 69 | tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base) |
| 70 | { |
| 71 | unsigned long cl; |
| 72 | |
| 73 | cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid); |
| 74 | cl = cls_set_class(tp, &r->class, cl); |
| 75 | if (cl) |
| 76 | tp->q->ops->cl_ops->unbind_tcf(tp->q, cl); |
| 77 | } |
| 78 | |
| 79 | static inline void |
| 80 | tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r) |
| 81 | { |
| 82 | unsigned long cl; |
| 83 | |
| 84 | if ((cl = __cls_set_class(&r->class, 0)) != 0) |
| 85 | tp->q->ops->cl_ops->unbind_tcf(tp->q, cl); |
| 86 | } |
| 87 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 88 | struct tcf_exts { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | #ifdef CONFIG_NET_CLS_ACT |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 90 | __u32 type; /* for backward compat(TCA_OLD_COMPAT) */ |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 91 | int nr_actions; |
| 92 | struct tc_action **actions; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | #endif |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 94 | /* Map to export classifier specific extension TLV types to the |
| 95 | * generic extensions API. Unsupported extensions must be set to 0. |
| 96 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | int action; |
| 98 | int police; |
| 99 | }; |
| 100 | |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 101 | static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 102 | { |
| 103 | #ifdef CONFIG_NET_CLS_ACT |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 104 | exts->type = 0; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 105 | exts->nr_actions = 0; |
| 106 | exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *), |
| 107 | GFP_KERNEL); |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 108 | if (!exts->actions) |
| 109 | return -ENOMEM; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 110 | #endif |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 111 | exts->action = action; |
| 112 | exts->police = police; |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 113 | return 0; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 114 | } |
| 115 | |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 116 | static inline void tcf_exts_to_list(const struct tcf_exts *exts, |
| 117 | struct list_head *actions) |
| 118 | { |
| 119 | #ifdef CONFIG_NET_CLS_ACT |
| 120 | int i; |
| 121 | |
| 122 | for (i = 0; i < exts->nr_actions; i++) { |
| 123 | struct tc_action *a = exts->actions[i]; |
| 124 | |
Hadar Hen Zion | fa5effe | 2016-09-27 11:09:51 +0300 | [diff] [blame] | 125 | list_add_tail(&a->list, actions); |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 126 | } |
| 127 | #endif |
| 128 | } |
| 129 | |
Jakub Kicinski | d897a63 | 2017-05-31 08:06:43 -0700 | [diff] [blame] | 130 | static inline void |
| 131 | tcf_exts_stats_update(const struct tcf_exts *exts, |
| 132 | u64 bytes, u64 packets, u64 lastuse) |
| 133 | { |
| 134 | #ifdef CONFIG_NET_CLS_ACT |
| 135 | int i; |
| 136 | |
| 137 | preempt_disable(); |
| 138 | |
| 139 | for (i = 0; i < exts->nr_actions; i++) { |
| 140 | struct tc_action *a = exts->actions[i]; |
| 141 | |
| 142 | tcf_action_stats_update(a, bytes, packets, lastuse); |
| 143 | } |
| 144 | |
| 145 | preempt_enable(); |
| 146 | #endif |
| 147 | } |
| 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | /** |
Jiri Pirko | 3bcc0ce | 2017-08-04 14:28:58 +0200 | [diff] [blame] | 150 | * tcf_exts_has_actions - check if at least one action is present |
| 151 | * @exts: tc filter extensions handle |
| 152 | * |
| 153 | * Returns true if at least one action is present. |
| 154 | */ |
| 155 | static inline bool tcf_exts_has_actions(struct tcf_exts *exts) |
| 156 | { |
WANG Cong | 2734437e | 2016-08-13 22:34:59 -0700 | [diff] [blame] | 157 | #ifdef CONFIG_NET_CLS_ACT |
Jiri Pirko | 3bcc0ce | 2017-08-04 14:28:58 +0200 | [diff] [blame] | 158 | return exts->nr_actions; |
| 159 | #else |
| 160 | return false; |
| 161 | #endif |
| 162 | } |
WANG Cong | 2734437e | 2016-08-13 22:34:59 -0700 | [diff] [blame] | 163 | |
Jiri Pirko | 3bcc0ce | 2017-08-04 14:28:58 +0200 | [diff] [blame] | 164 | /** |
| 165 | * tcf_exts_has_one_action - check if exactly one action is present |
| 166 | * @exts: tc filter extensions handle |
| 167 | * |
| 168 | * Returns true if exactly one action is present. |
| 169 | */ |
| 170 | static inline bool tcf_exts_has_one_action(struct tcf_exts *exts) |
| 171 | { |
| 172 | #ifdef CONFIG_NET_CLS_ACT |
| 173 | return exts->nr_actions == 1; |
| 174 | #else |
| 175 | return false; |
| 176 | #endif |
| 177 | } |
WANG Cong | 2734437e | 2016-08-13 22:34:59 -0700 | [diff] [blame] | 178 | |
Jiri Pirko | af69afc | 2017-08-04 14:28:59 +0200 | [diff] [blame] | 179 | /** |
| 180 | * tcf_exts_exec - execute tc filter extensions |
| 181 | * @skb: socket buffer |
| 182 | * @exts: tc filter extensions handle |
| 183 | * @res: desired result |
| 184 | * |
Jiri Pirko | af089e7 | 2017-08-04 14:29:01 +0200 | [diff] [blame] | 185 | * Executes all configured extensions. Returns TC_ACT_OK on a normal execution, |
Jiri Pirko | af69afc | 2017-08-04 14:28:59 +0200 | [diff] [blame] | 186 | * a negative number if the filter must be considered unmatched or |
| 187 | * a positive action code (TC_ACT_*) which must be returned to the |
| 188 | * underlying layer. |
| 189 | */ |
| 190 | static inline int |
| 191 | tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts, |
| 192 | struct tcf_result *res) |
| 193 | { |
| 194 | #ifdef CONFIG_NET_CLS_ACT |
Jiri Pirko | ec1a9cc | 2017-08-04 14:29:02 +0200 | [diff] [blame] | 195 | return tcf_action_exec(skb, exts->actions, exts->nr_actions, res); |
Jiri Pirko | af69afc | 2017-08-04 14:28:59 +0200 | [diff] [blame] | 196 | #endif |
Jiri Pirko | af089e7 | 2017-08-04 14:29:01 +0200 | [diff] [blame] | 197 | return TC_ACT_OK; |
Jiri Pirko | af69afc | 2017-08-04 14:28:59 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Joe Perches | 5c15257 | 2013-07-30 22:47:13 -0700 | [diff] [blame] | 200 | int tcf_exts_validate(struct net *net, struct tcf_proto *tp, |
| 201 | struct nlattr **tb, struct nlattr *rate_tlv, |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 202 | struct tcf_exts *exts, bool ovr); |
WANG Cong | 18d0264 | 2014-09-25 10:26:37 -0700 | [diff] [blame] | 203 | void tcf_exts_destroy(struct tcf_exts *exts); |
Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 204 | void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src); |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 205 | int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts); |
| 206 | int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts); |
Hadar Hen Zion | 7091d8c | 2016-12-01 14:06:37 +0200 | [diff] [blame] | 207 | int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts, |
| 208 | struct net_device **hw_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * struct tcf_pkt_info - packet information |
| 212 | */ |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 213 | struct tcf_pkt_info { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | unsigned char * ptr; |
| 215 | int nexthdr; |
| 216 | }; |
| 217 | |
| 218 | #ifdef CONFIG_NET_EMATCH |
| 219 | |
| 220 | struct tcf_ematch_ops; |
| 221 | |
| 222 | /** |
| 223 | * struct tcf_ematch - extended match (ematch) |
| 224 | * |
| 225 | * @matchid: identifier to allow userspace to reidentify a match |
| 226 | * @flags: flags specifying attributes and the relation to other matches |
| 227 | * @ops: the operations lookup table of the corresponding ematch module |
| 228 | * @datalen: length of the ematch specific configuration data |
| 229 | * @data: ematch specific data |
| 230 | */ |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 231 | struct tcf_ematch { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | struct tcf_ematch_ops * ops; |
| 233 | unsigned long data; |
| 234 | unsigned int datalen; |
| 235 | u16 matchid; |
| 236 | u16 flags; |
John Fastabend | 82a470f | 2014-10-05 21:27:53 -0700 | [diff] [blame] | 237 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | static inline int tcf_em_is_container(struct tcf_ematch *em) |
| 241 | { |
| 242 | return !em->ops; |
| 243 | } |
| 244 | |
| 245 | static inline int tcf_em_is_simple(struct tcf_ematch *em) |
| 246 | { |
| 247 | return em->flags & TCF_EM_SIMPLE; |
| 248 | } |
| 249 | |
| 250 | static inline int tcf_em_is_inverted(struct tcf_ematch *em) |
| 251 | { |
| 252 | return em->flags & TCF_EM_INVERT; |
| 253 | } |
| 254 | |
| 255 | static inline int tcf_em_last_match(struct tcf_ematch *em) |
| 256 | { |
| 257 | return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END; |
| 258 | } |
| 259 | |
| 260 | static inline int tcf_em_early_end(struct tcf_ematch *em, int result) |
| 261 | { |
| 262 | if (tcf_em_last_match(em)) |
| 263 | return 1; |
| 264 | |
| 265 | if (result == 0 && em->flags & TCF_EM_REL_AND) |
| 266 | return 1; |
| 267 | |
| 268 | if (result != 0 && em->flags & TCF_EM_REL_OR) |
| 269 | return 1; |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * struct tcf_ematch_tree - ematch tree handle |
| 276 | * |
| 277 | * @hdr: ematch tree header supplied by userspace |
| 278 | * @matches: array of ematches |
| 279 | */ |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 280 | struct tcf_ematch_tree { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | struct tcf_ematch_tree_hdr hdr; |
| 282 | struct tcf_ematch * matches; |
| 283 | |
| 284 | }; |
| 285 | |
| 286 | /** |
| 287 | * struct tcf_ematch_ops - ematch module operations |
| 288 | * |
| 289 | * @kind: identifier (kind) of this ematch module |
| 290 | * @datalen: length of expected configuration data (optional) |
| 291 | * @change: called during validation (optional) |
| 292 | * @match: called during ematch tree evaluation, must return 1/0 |
| 293 | * @destroy: called during destroyage (optional) |
| 294 | * @dump: called during dumping process (optional) |
| 295 | * @owner: owner, must be set to THIS_MODULE |
| 296 | * @link: link to previous/next ematch module (internal use) |
| 297 | */ |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 298 | struct tcf_ematch_ops { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | int kind; |
| 300 | int datalen; |
John Fastabend | 82a470f | 2014-10-05 21:27:53 -0700 | [diff] [blame] | 301 | int (*change)(struct net *net, void *, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | int, struct tcf_ematch *); |
| 303 | int (*match)(struct sk_buff *, struct tcf_ematch *, |
| 304 | struct tcf_pkt_info *); |
John Fastabend | 82a470f | 2014-10-05 21:27:53 -0700 | [diff] [blame] | 305 | void (*destroy)(struct tcf_ematch *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | int (*dump)(struct sk_buff *, struct tcf_ematch *); |
| 307 | struct module *owner; |
| 308 | struct list_head link; |
| 309 | }; |
| 310 | |
Joe Perches | 5c15257 | 2013-07-30 22:47:13 -0700 | [diff] [blame] | 311 | int tcf_em_register(struct tcf_ematch_ops *); |
| 312 | void tcf_em_unregister(struct tcf_ematch_ops *); |
| 313 | int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *, |
| 314 | struct tcf_ematch_tree *); |
John Fastabend | 82a470f | 2014-10-05 21:27:53 -0700 | [diff] [blame] | 315 | void tcf_em_tree_destroy(struct tcf_ematch_tree *); |
Joe Perches | 5c15257 | 2013-07-30 22:47:13 -0700 | [diff] [blame] | 316 | int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int); |
| 317 | int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *, |
| 318 | struct tcf_pkt_info *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | * tcf_em_tree_match - evaulate an ematch tree |
| 322 | * |
| 323 | * @skb: socket buffer of the packet in question |
| 324 | * @tree: ematch tree to be used for evaluation |
| 325 | * @info: packet information examined by classifier |
| 326 | * |
| 327 | * This function matches @skb against the ematch tree in @tree by going |
| 328 | * through all ematches respecting their logic relations returning |
| 329 | * as soon as the result is obvious. |
| 330 | * |
| 331 | * Returns 1 if the ematch tree as-one matches, no ematches are configured |
| 332 | * or ematch is not enabled in the kernel, otherwise 0 is returned. |
| 333 | */ |
| 334 | static inline int tcf_em_tree_match(struct sk_buff *skb, |
| 335 | struct tcf_ematch_tree *tree, |
| 336 | struct tcf_pkt_info *info) |
| 337 | { |
| 338 | if (tree->hdr.nmatches) |
| 339 | return __tcf_em_tree_match(skb, tree, info); |
| 340 | else |
| 341 | return 1; |
| 342 | } |
| 343 | |
Patrick McHardy | db3d99c | 2007-07-11 19:46:26 -0700 | [diff] [blame] | 344 | #define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind)) |
| 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | #else /* CONFIG_NET_EMATCH */ |
| 347 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 348 | struct tcf_ematch_tree { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0) |
John Fastabend | 82a470f | 2014-10-05 21:27:53 -0700 | [diff] [blame] | 352 | #define tcf_em_tree_destroy(t) do { (void)(t); } while(0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | #define tcf_em_tree_dump(skb, t, tlv) (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | #define tcf_em_tree_match(skb, t, info) ((void)(info), 1) |
| 355 | |
| 356 | #endif /* CONFIG_NET_EMATCH */ |
| 357 | |
| 358 | static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer) |
| 359 | { |
| 360 | switch (layer) { |
| 361 | case TCF_LAYER_LINK: |
| 362 | return skb->data; |
| 363 | case TCF_LAYER_NETWORK: |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 364 | return skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | case TCF_LAYER_TRANSPORT: |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 366 | return skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | return NULL; |
| 370 | } |
| 371 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 372 | static inline int tcf_valid_offset(const struct sk_buff *skb, |
| 373 | const unsigned char *ptr, const int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | { |
David S. Miller | da521b2 | 2010-12-21 12:43:16 -0800 | [diff] [blame] | 375 | return likely((ptr + len) <= skb_tail_pointer(skb) && |
| 376 | ptr >= skb->head && |
| 377 | (ptr <= (ptr + len))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | #ifdef CONFIG_NET_CLS_IND |
Denis V. Lunev | 0eeb8ff | 2007-12-04 01:15:45 -0800 | [diff] [blame] | 381 | #include <net/net_namespace.h> |
| 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | static inline int |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 384 | tcf_change_indev(struct net *net, struct nlattr *indev_tlv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 386 | char indev[IFNAMSIZ]; |
Patrick McHardy | c01003c | 2007-03-29 11:46:52 -0700 | [diff] [blame] | 387 | struct net_device *dev; |
| 388 | |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 389 | if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ) |
| 390 | return -EINVAL; |
| 391 | dev = __dev_get_by_name(net, indev); |
| 392 | if (!dev) |
| 393 | return -ENODEV; |
| 394 | return dev->ifindex; |
| 395 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 397 | static inline bool |
| 398 | tcf_match_indev(struct sk_buff *skb, int ifindex) |
| 399 | { |
| 400 | if (!ifindex) |
| 401 | return true; |
| 402 | if (!skb->skb_iif) |
| 403 | return false; |
| 404 | return ifindex == skb->skb_iif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |
| 406 | #endif /* CONFIG_NET_CLS_IND */ |
| 407 | |
Jiri Pirko | 5fd9fc4 | 2017-08-07 10:15:29 +0200 | [diff] [blame^] | 408 | struct tc_cls_common_offload { |
| 409 | u32 handle; |
| 410 | u32 chain_index; |
| 411 | __be16 protocol; |
| 412 | }; |
| 413 | |
| 414 | static inline void |
| 415 | tc_cls_common_offload_init(struct tc_cls_common_offload *cls_common, |
| 416 | const struct tcf_proto *tp) |
| 417 | { |
| 418 | cls_common->handle = tp->q->handle; |
| 419 | cls_common->chain_index = tp->chain->index; |
| 420 | cls_common->protocol = tp->protocol; |
| 421 | } |
| 422 | |
John Fastabend | a1b7c5f | 2016-02-16 21:17:09 -0800 | [diff] [blame] | 423 | struct tc_cls_u32_knode { |
| 424 | struct tcf_exts *exts; |
John Fastabend | e014860 | 2016-02-17 14:59:30 -0800 | [diff] [blame] | 425 | struct tc_u32_sel *sel; |
John Fastabend | a1b7c5f | 2016-02-16 21:17:09 -0800 | [diff] [blame] | 426 | u32 handle; |
| 427 | u32 val; |
| 428 | u32 mask; |
| 429 | u32 link_handle; |
John Fastabend | e014860 | 2016-02-17 14:59:30 -0800 | [diff] [blame] | 430 | u8 fshift; |
John Fastabend | a1b7c5f | 2016-02-16 21:17:09 -0800 | [diff] [blame] | 431 | }; |
| 432 | |
| 433 | struct tc_cls_u32_hnode { |
| 434 | u32 handle; |
| 435 | u32 prio; |
| 436 | unsigned int divisor; |
| 437 | }; |
| 438 | |
| 439 | enum tc_clsu32_command { |
| 440 | TC_CLSU32_NEW_KNODE, |
| 441 | TC_CLSU32_REPLACE_KNODE, |
| 442 | TC_CLSU32_DELETE_KNODE, |
| 443 | TC_CLSU32_NEW_HNODE, |
| 444 | TC_CLSU32_REPLACE_HNODE, |
| 445 | TC_CLSU32_DELETE_HNODE, |
| 446 | }; |
| 447 | |
| 448 | struct tc_cls_u32_offload { |
Jiri Pirko | 5fd9fc4 | 2017-08-07 10:15:29 +0200 | [diff] [blame^] | 449 | struct tc_cls_common_offload common; |
John Fastabend | a1b7c5f | 2016-02-16 21:17:09 -0800 | [diff] [blame] | 450 | /* knode values */ |
| 451 | enum tc_clsu32_command command; |
| 452 | union { |
| 453 | struct tc_cls_u32_knode knode; |
| 454 | struct tc_cls_u32_hnode hnode; |
| 455 | }; |
| 456 | }; |
| 457 | |
Hadar Hen Zion | 55330f0 | 2016-12-01 14:06:33 +0200 | [diff] [blame] | 458 | static inline bool tc_can_offload(const struct net_device *dev, |
| 459 | const struct tcf_proto *tp) |
John Fastabend | 6843e7a | 2016-02-26 07:53:49 -0800 | [diff] [blame] | 460 | { |
Daniel Borkmann | 92c075d | 2016-06-06 22:50:39 +0200 | [diff] [blame] | 461 | const struct Qdisc *sch = tp->q; |
| 462 | const struct Qdisc_class_ops *cops = sch->ops->cl_ops; |
| 463 | |
John Fastabend | 2b6ab0d | 2016-02-26 07:54:13 -0800 | [diff] [blame] | 464 | if (!(dev->features & NETIF_F_HW_TC)) |
| 465 | return false; |
John Fastabend | 9e8ce79 | 2016-02-26 07:54:39 -0800 | [diff] [blame] | 466 | if (!dev->netdev_ops->ndo_setup_tc) |
| 467 | return false; |
Daniel Borkmann | 92c075d | 2016-06-06 22:50:39 +0200 | [diff] [blame] | 468 | if (cops && cops->tcf_cl_offload) |
| 469 | return cops->tcf_cl_offload(tp->classid); |
John Fastabend | 9e8ce79 | 2016-02-26 07:54:39 -0800 | [diff] [blame] | 470 | |
| 471 | return true; |
John Fastabend | 6843e7a | 2016-02-26 07:53:49 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Hadar Hen Zion | 55330f0 | 2016-12-01 14:06:33 +0200 | [diff] [blame] | 474 | static inline bool tc_skip_hw(u32 flags) |
| 475 | { |
| 476 | return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false; |
| 477 | } |
| 478 | |
| 479 | static inline bool tc_should_offload(const struct net_device *dev, |
| 480 | const struct tcf_proto *tp, u32 flags) |
| 481 | { |
| 482 | if (tc_skip_hw(flags)) |
| 483 | return false; |
| 484 | return tc_can_offload(dev, tp); |
| 485 | } |
| 486 | |
Samudrala, Sridhar | d34e3e1 | 2016-05-12 17:08:23 -0700 | [diff] [blame] | 487 | static inline bool tc_skip_sw(u32 flags) |
| 488 | { |
| 489 | return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false; |
| 490 | } |
| 491 | |
| 492 | /* SKIP_HW and SKIP_SW are mutually exclusive flags. */ |
| 493 | static inline bool tc_flags_valid(u32 flags) |
| 494 | { |
| 495 | if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)) |
| 496 | return false; |
| 497 | |
| 498 | if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))) |
| 499 | return false; |
| 500 | |
| 501 | return true; |
| 502 | } |
| 503 | |
Or Gerlitz | e696028 | 2017-02-16 10:31:12 +0200 | [diff] [blame] | 504 | static inline bool tc_in_hw(u32 flags) |
| 505 | { |
| 506 | return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false; |
| 507 | } |
| 508 | |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 509 | enum tc_fl_command { |
| 510 | TC_CLSFLOWER_REPLACE, |
| 511 | TC_CLSFLOWER_DESTROY, |
Amir Vadai | 10cbc68 | 2016-05-13 12:55:37 +0000 | [diff] [blame] | 512 | TC_CLSFLOWER_STATS, |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 513 | }; |
| 514 | |
| 515 | struct tc_cls_flower_offload { |
Jiri Pirko | 5fd9fc4 | 2017-08-07 10:15:29 +0200 | [diff] [blame^] | 516 | struct tc_cls_common_offload common; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 517 | enum tc_fl_command command; |
Jiri Pirko | 69ca05c | 2017-02-03 10:29:08 +0100 | [diff] [blame] | 518 | u32 prio; |
Amir Vadai | 8208d21 | 2016-03-11 11:08:45 +0200 | [diff] [blame] | 519 | unsigned long cookie; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 520 | struct flow_dissector *dissector; |
| 521 | struct fl_flow_key *mask; |
| 522 | struct fl_flow_key *key; |
| 523 | struct tcf_exts *exts; |
Jiri Pirko | 3e0e826 | 2017-08-07 10:15:19 +0200 | [diff] [blame] | 524 | bool egress_dev; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 525 | }; |
| 526 | |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 527 | enum tc_matchall_command { |
| 528 | TC_CLSMATCHALL_REPLACE, |
| 529 | TC_CLSMATCHALL_DESTROY, |
| 530 | }; |
| 531 | |
| 532 | struct tc_cls_matchall_offload { |
Jiri Pirko | 5fd9fc4 | 2017-08-07 10:15:29 +0200 | [diff] [blame^] | 533 | struct tc_cls_common_offload common; |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 534 | enum tc_matchall_command command; |
| 535 | struct tcf_exts *exts; |
| 536 | unsigned long cookie; |
| 537 | }; |
| 538 | |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 539 | enum tc_clsbpf_command { |
| 540 | TC_CLSBPF_ADD, |
| 541 | TC_CLSBPF_REPLACE, |
| 542 | TC_CLSBPF_DESTROY, |
Jakub Kicinski | 68d6406 | 2016-09-21 11:44:02 +0100 | [diff] [blame] | 543 | TC_CLSBPF_STATS, |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 544 | }; |
| 545 | |
| 546 | struct tc_cls_bpf_offload { |
Jiri Pirko | 5fd9fc4 | 2017-08-07 10:15:29 +0200 | [diff] [blame^] | 547 | struct tc_cls_common_offload common; |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 548 | enum tc_clsbpf_command command; |
| 549 | struct tcf_exts *exts; |
| 550 | struct bpf_prog *prog; |
| 551 | const char *name; |
| 552 | bool exts_integrated; |
Jakub Kicinski | 0d01d45 | 2016-09-21 11:43:54 +0100 | [diff] [blame] | 553 | u32 gen_flags; |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 554 | }; |
| 555 | |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 556 | |
| 557 | /* This structure holds cookie structure that is passed from user |
| 558 | * to the kernel for actions and classifiers |
| 559 | */ |
| 560 | struct tc_cookie { |
| 561 | u8 *data; |
| 562 | u32 len; |
| 563 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | #endif |