pkt_cls.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. #ifndef __NET_PKT_CLS_H
  2. #define __NET_PKT_CLS_H
  3. #include <linux/pkt_cls.h>
  4. #include <net/sch_generic.h>
  5. #include <net/act_api.h>
  6. /* Basic packet classifier frontend definitions. */
  7. struct tcf_walker {
  8. int stop;
  9. int skip;
  10. int count;
  11. int (*fn)(struct tcf_proto *, unsigned long node, struct tcf_walker *);
  12. };
  13. int register_tcf_proto_ops(struct tcf_proto_ops *ops);
  14. int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
  15. #ifdef CONFIG_NET_CLS
  16. void tcf_destroy_chain(struct tcf_proto __rcu **fl);
  17. #else
  18. static inline void tcf_destroy_chain(struct tcf_proto __rcu **fl)
  19. {
  20. }
  21. #endif
  22. static inline unsigned long
  23. __cls_set_class(unsigned long *clp, unsigned long cl)
  24. {
  25. return xchg(clp, cl);
  26. }
  27. static inline unsigned long
  28. cls_set_class(struct tcf_proto *tp, unsigned long *clp,
  29. unsigned long cl)
  30. {
  31. unsigned long old_cl;
  32. tcf_tree_lock(tp);
  33. old_cl = __cls_set_class(clp, cl);
  34. tcf_tree_unlock(tp);
  35. return old_cl;
  36. }
  37. static inline void
  38. tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
  39. {
  40. unsigned long cl;
  41. cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid);
  42. cl = cls_set_class(tp, &r->class, cl);
  43. if (cl)
  44. tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
  45. }
  46. static inline void
  47. tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
  48. {
  49. unsigned long cl;
  50. if ((cl = __cls_set_class(&r->class, 0)) != 0)
  51. tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
  52. }
  53. struct tcf_exts {
  54. #ifdef CONFIG_NET_CLS_ACT
  55. __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
  56. int nr_actions;
  57. struct tc_action **actions;
  58. #endif
  59. /* Map to export classifier specific extension TLV types to the
  60. * generic extensions API. Unsupported extensions must be set to 0.
  61. */
  62. int action;
  63. int police;
  64. };
  65. static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
  66. {
  67. #ifdef CONFIG_NET_CLS_ACT
  68. exts->type = 0;
  69. exts->nr_actions = 0;
  70. exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
  71. GFP_KERNEL);
  72. if (!exts->actions)
  73. return -ENOMEM;
  74. #endif
  75. exts->action = action;
  76. exts->police = police;
  77. return 0;
  78. }
  79. /**
  80. * tcf_exts_is_predicative - check if a predicative extension is present
  81. * @exts: tc filter extensions handle
  82. *
  83. * Returns 1 if a predicative extension is present, i.e. an extension which
  84. * might cause further actions and thus overrule the regular tcf_result.
  85. */
  86. static inline int
  87. tcf_exts_is_predicative(struct tcf_exts *exts)
  88. {
  89. #ifdef CONFIG_NET_CLS_ACT
  90. return exts->nr_actions;
  91. #else
  92. return 0;
  93. #endif
  94. }
  95. /**
  96. * tcf_exts_is_available - check if at least one extension is present
  97. * @exts: tc filter extensions handle
  98. *
  99. * Returns 1 if at least one extension is present.
  100. */
  101. static inline int
  102. tcf_exts_is_available(struct tcf_exts *exts)
  103. {
  104. /* All non-predicative extensions must be added here. */
  105. return tcf_exts_is_predicative(exts);
  106. }
  107. static inline void tcf_exts_to_list(const struct tcf_exts *exts,
  108. struct list_head *actions)
  109. {
  110. #ifdef CONFIG_NET_CLS_ACT
  111. int i;
  112. for (i = 0; i < exts->nr_actions; i++) {
  113. struct tc_action *a = exts->actions[i];
  114. list_add_tail(&a->list, actions);
  115. }
  116. #endif
  117. }
  118. /**
  119. * tcf_exts_exec - execute tc filter extensions
  120. * @skb: socket buffer
  121. * @exts: tc filter extensions handle
  122. * @res: desired result
  123. *
  124. * Executes all configured extensions. Returns 0 on a normal execution,
  125. * a negative number if the filter must be considered unmatched or
  126. * a positive action code (TC_ACT_*) which must be returned to the
  127. * underlying layer.
  128. */
  129. static inline int
  130. tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
  131. struct tcf_result *res)
  132. {
  133. #ifdef CONFIG_NET_CLS_ACT
  134. if (exts->nr_actions)
  135. return tcf_action_exec(skb, exts->actions, exts->nr_actions,
  136. res);
  137. #endif
  138. return 0;
  139. }
  140. #ifdef CONFIG_NET_CLS_ACT
  141. #define tc_no_actions(_exts) ((_exts)->nr_actions == 0)
  142. #define tc_single_action(_exts) ((_exts)->nr_actions == 1)
  143. #else /* CONFIG_NET_CLS_ACT */
  144. #define tc_no_actions(_exts) true
  145. #define tc_single_action(_exts) false
  146. #endif /* CONFIG_NET_CLS_ACT */
  147. int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
  148. struct nlattr **tb, struct nlattr *rate_tlv,
  149. struct tcf_exts *exts, bool ovr);
  150. void tcf_exts_destroy(struct tcf_exts *exts);
  151. void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
  152. struct tcf_exts *src);
  153. int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
  154. int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
  155. int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
  156. struct net_device **hw_dev);
  157. /**
  158. * struct tcf_pkt_info - packet information
  159. */
  160. struct tcf_pkt_info {
  161. unsigned char * ptr;
  162. int nexthdr;
  163. };
  164. #ifdef CONFIG_NET_EMATCH
  165. struct tcf_ematch_ops;
  166. /**
  167. * struct tcf_ematch - extended match (ematch)
  168. *
  169. * @matchid: identifier to allow userspace to reidentify a match
  170. * @flags: flags specifying attributes and the relation to other matches
  171. * @ops: the operations lookup table of the corresponding ematch module
  172. * @datalen: length of the ematch specific configuration data
  173. * @data: ematch specific data
  174. */
  175. struct tcf_ematch {
  176. struct tcf_ematch_ops * ops;
  177. unsigned long data;
  178. unsigned int datalen;
  179. u16 matchid;
  180. u16 flags;
  181. struct net *net;
  182. };
  183. static inline int tcf_em_is_container(struct tcf_ematch *em)
  184. {
  185. return !em->ops;
  186. }
  187. static inline int tcf_em_is_simple(struct tcf_ematch *em)
  188. {
  189. return em->flags & TCF_EM_SIMPLE;
  190. }
  191. static inline int tcf_em_is_inverted(struct tcf_ematch *em)
  192. {
  193. return em->flags & TCF_EM_INVERT;
  194. }
  195. static inline int tcf_em_last_match(struct tcf_ematch *em)
  196. {
  197. return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
  198. }
  199. static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
  200. {
  201. if (tcf_em_last_match(em))
  202. return 1;
  203. if (result == 0 && em->flags & TCF_EM_REL_AND)
  204. return 1;
  205. if (result != 0 && em->flags & TCF_EM_REL_OR)
  206. return 1;
  207. return 0;
  208. }
  209. /**
  210. * struct tcf_ematch_tree - ematch tree handle
  211. *
  212. * @hdr: ematch tree header supplied by userspace
  213. * @matches: array of ematches
  214. */
  215. struct tcf_ematch_tree {
  216. struct tcf_ematch_tree_hdr hdr;
  217. struct tcf_ematch * matches;
  218. };
  219. /**
  220. * struct tcf_ematch_ops - ematch module operations
  221. *
  222. * @kind: identifier (kind) of this ematch module
  223. * @datalen: length of expected configuration data (optional)
  224. * @change: called during validation (optional)
  225. * @match: called during ematch tree evaluation, must return 1/0
  226. * @destroy: called during destroyage (optional)
  227. * @dump: called during dumping process (optional)
  228. * @owner: owner, must be set to THIS_MODULE
  229. * @link: link to previous/next ematch module (internal use)
  230. */
  231. struct tcf_ematch_ops {
  232. int kind;
  233. int datalen;
  234. int (*change)(struct net *net, void *,
  235. int, struct tcf_ematch *);
  236. int (*match)(struct sk_buff *, struct tcf_ematch *,
  237. struct tcf_pkt_info *);
  238. void (*destroy)(struct tcf_ematch *);
  239. int (*dump)(struct sk_buff *, struct tcf_ematch *);
  240. struct module *owner;
  241. struct list_head link;
  242. };
  243. int tcf_em_register(struct tcf_ematch_ops *);
  244. void tcf_em_unregister(struct tcf_ematch_ops *);
  245. int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
  246. struct tcf_ematch_tree *);
  247. void tcf_em_tree_destroy(struct tcf_ematch_tree *);
  248. int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
  249. int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
  250. struct tcf_pkt_info *);
  251. /**
  252. * tcf_em_tree_change - replace ematch tree of a running classifier
  253. *
  254. * @tp: classifier kind handle
  255. * @dst: destination ematch tree variable
  256. * @src: source ematch tree (temporary tree from tcf_em_tree_validate)
  257. *
  258. * This functions replaces the ematch tree in @dst with the ematch
  259. * tree in @src. The classifier in charge of the ematch tree may be
  260. * running.
  261. */
  262. static inline void tcf_em_tree_change(struct tcf_proto *tp,
  263. struct tcf_ematch_tree *dst,
  264. struct tcf_ematch_tree *src)
  265. {
  266. tcf_tree_lock(tp);
  267. memcpy(dst, src, sizeof(*dst));
  268. tcf_tree_unlock(tp);
  269. }
  270. /**
  271. * tcf_em_tree_match - evaulate an ematch tree
  272. *
  273. * @skb: socket buffer of the packet in question
  274. * @tree: ematch tree to be used for evaluation
  275. * @info: packet information examined by classifier
  276. *
  277. * This function matches @skb against the ematch tree in @tree by going
  278. * through all ematches respecting their logic relations returning
  279. * as soon as the result is obvious.
  280. *
  281. * Returns 1 if the ematch tree as-one matches, no ematches are configured
  282. * or ematch is not enabled in the kernel, otherwise 0 is returned.
  283. */
  284. static inline int tcf_em_tree_match(struct sk_buff *skb,
  285. struct tcf_ematch_tree *tree,
  286. struct tcf_pkt_info *info)
  287. {
  288. if (tree->hdr.nmatches)
  289. return __tcf_em_tree_match(skb, tree, info);
  290. else
  291. return 1;
  292. }
  293. #define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind))
  294. #else /* CONFIG_NET_EMATCH */
  295. struct tcf_ematch_tree {
  296. };
  297. #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
  298. #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
  299. #define tcf_em_tree_dump(skb, t, tlv) (0)
  300. #define tcf_em_tree_change(tp, dst, src) do { } while(0)
  301. #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
  302. #endif /* CONFIG_NET_EMATCH */
  303. static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
  304. {
  305. switch (layer) {
  306. case TCF_LAYER_LINK:
  307. return skb->data;
  308. case TCF_LAYER_NETWORK:
  309. return skb_network_header(skb);
  310. case TCF_LAYER_TRANSPORT:
  311. return skb_transport_header(skb);
  312. }
  313. return NULL;
  314. }
  315. static inline int tcf_valid_offset(const struct sk_buff *skb,
  316. const unsigned char *ptr, const int len)
  317. {
  318. return likely((ptr + len) <= skb_tail_pointer(skb) &&
  319. ptr >= skb->head &&
  320. (ptr <= (ptr + len)));
  321. }
  322. #ifdef CONFIG_NET_CLS_IND
  323. #include <net/net_namespace.h>
  324. static inline int
  325. tcf_change_indev(struct net *net, struct nlattr *indev_tlv)
  326. {
  327. char indev[IFNAMSIZ];
  328. struct net_device *dev;
  329. if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
  330. return -EINVAL;
  331. dev = __dev_get_by_name(net, indev);
  332. if (!dev)
  333. return -ENODEV;
  334. return dev->ifindex;
  335. }
  336. static inline bool
  337. tcf_match_indev(struct sk_buff *skb, int ifindex)
  338. {
  339. if (!ifindex)
  340. return true;
  341. if (!skb->skb_iif)
  342. return false;
  343. return ifindex == skb->skb_iif;
  344. }
  345. #endif /* CONFIG_NET_CLS_IND */
  346. struct tc_cls_u32_knode {
  347. struct tcf_exts *exts;
  348. struct tc_u32_sel *sel;
  349. u32 handle;
  350. u32 val;
  351. u32 mask;
  352. u32 link_handle;
  353. u8 fshift;
  354. };
  355. struct tc_cls_u32_hnode {
  356. u32 handle;
  357. u32 prio;
  358. unsigned int divisor;
  359. };
  360. enum tc_clsu32_command {
  361. TC_CLSU32_NEW_KNODE,
  362. TC_CLSU32_REPLACE_KNODE,
  363. TC_CLSU32_DELETE_KNODE,
  364. TC_CLSU32_NEW_HNODE,
  365. TC_CLSU32_REPLACE_HNODE,
  366. TC_CLSU32_DELETE_HNODE,
  367. };
  368. struct tc_cls_u32_offload {
  369. /* knode values */
  370. enum tc_clsu32_command command;
  371. union {
  372. struct tc_cls_u32_knode knode;
  373. struct tc_cls_u32_hnode hnode;
  374. };
  375. };
  376. static inline bool tc_can_offload(const struct net_device *dev,
  377. const struct tcf_proto *tp)
  378. {
  379. const struct Qdisc *sch = tp->q;
  380. const struct Qdisc_class_ops *cops = sch->ops->cl_ops;
  381. if (!(dev->features & NETIF_F_HW_TC))
  382. return false;
  383. if (!dev->netdev_ops->ndo_setup_tc)
  384. return false;
  385. if (cops && cops->tcf_cl_offload)
  386. return cops->tcf_cl_offload(tp->classid);
  387. return true;
  388. }
  389. static inline bool tc_skip_hw(u32 flags)
  390. {
  391. return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
  392. }
  393. static inline bool tc_should_offload(const struct net_device *dev,
  394. const struct tcf_proto *tp, u32 flags)
  395. {
  396. if (tc_skip_hw(flags))
  397. return false;
  398. return tc_can_offload(dev, tp);
  399. }
  400. static inline bool tc_skip_sw(u32 flags)
  401. {
  402. return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
  403. }
  404. /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
  405. static inline bool tc_flags_valid(u32 flags)
  406. {
  407. if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))
  408. return false;
  409. if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
  410. return false;
  411. return true;
  412. }
  413. static inline bool tc_in_hw(u32 flags)
  414. {
  415. return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
  416. }
  417. enum tc_fl_command {
  418. TC_CLSFLOWER_REPLACE,
  419. TC_CLSFLOWER_DESTROY,
  420. TC_CLSFLOWER_STATS,
  421. };
  422. struct tc_cls_flower_offload {
  423. enum tc_fl_command command;
  424. u32 prio;
  425. unsigned long cookie;
  426. struct flow_dissector *dissector;
  427. struct fl_flow_key *mask;
  428. struct fl_flow_key *key;
  429. struct tcf_exts *exts;
  430. };
  431. enum tc_matchall_command {
  432. TC_CLSMATCHALL_REPLACE,
  433. TC_CLSMATCHALL_DESTROY,
  434. };
  435. struct tc_cls_matchall_offload {
  436. enum tc_matchall_command command;
  437. struct tcf_exts *exts;
  438. unsigned long cookie;
  439. };
  440. enum tc_clsbpf_command {
  441. TC_CLSBPF_ADD,
  442. TC_CLSBPF_REPLACE,
  443. TC_CLSBPF_DESTROY,
  444. TC_CLSBPF_STATS,
  445. };
  446. struct tc_cls_bpf_offload {
  447. enum tc_clsbpf_command command;
  448. struct tcf_exts *exts;
  449. struct bpf_prog *prog;
  450. const char *name;
  451. bool exts_integrated;
  452. u32 gen_flags;
  453. };
  454. /* This structure holds cookie structure that is passed from user
  455. * to the kernel for actions and classifiers
  456. */
  457. struct tc_cookie {
  458. u8 *data;
  459. u32 len;
  460. };
  461. #endif