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