pkt_cls.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. static inline unsigned long
  16. __cls_set_class(unsigned long *clp, unsigned long cl)
  17. {
  18. return xchg(clp, cl);
  19. }
  20. static inline unsigned long
  21. cls_set_class(struct tcf_proto *tp, unsigned long *clp,
  22. unsigned long cl)
  23. {
  24. unsigned long old_cl;
  25. tcf_tree_lock(tp);
  26. old_cl = __cls_set_class(clp, cl);
  27. tcf_tree_unlock(tp);
  28. return old_cl;
  29. }
  30. static inline void
  31. tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
  32. {
  33. unsigned long cl;
  34. cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid);
  35. cl = cls_set_class(tp, &r->class, cl);
  36. if (cl)
  37. tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
  38. }
  39. static inline void
  40. tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
  41. {
  42. unsigned long cl;
  43. if ((cl = __cls_set_class(&r->class, 0)) != 0)
  44. tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
  45. }
  46. struct tcf_exts {
  47. #ifdef CONFIG_NET_CLS_ACT
  48. __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
  49. struct list_head actions;
  50. #endif
  51. /* Map to export classifier specific extension TLV types to the
  52. * generic extensions API. Unsupported extensions must be set to 0.
  53. */
  54. int action;
  55. int police;
  56. };
  57. static inline void tcf_exts_init(struct tcf_exts *exts, int action, int police)
  58. {
  59. #ifdef CONFIG_NET_CLS_ACT
  60. exts->type = 0;
  61. INIT_LIST_HEAD(&exts->actions);
  62. #endif
  63. exts->action = action;
  64. exts->police = police;
  65. }
  66. /**
  67. * tcf_exts_is_predicative - check if a predicative extension is present
  68. * @exts: tc filter extensions handle
  69. *
  70. * Returns 1 if a predicative extension is present, i.e. an extension which
  71. * might cause further actions and thus overrule the regular tcf_result.
  72. */
  73. static inline int
  74. tcf_exts_is_predicative(struct tcf_exts *exts)
  75. {
  76. #ifdef CONFIG_NET_CLS_ACT
  77. return !list_empty(&exts->actions);
  78. #else
  79. return 0;
  80. #endif
  81. }
  82. /**
  83. * tcf_exts_is_available - check if at least one extension is present
  84. * @exts: tc filter extensions handle
  85. *
  86. * Returns 1 if at least one extension is present.
  87. */
  88. static inline int
  89. tcf_exts_is_available(struct tcf_exts *exts)
  90. {
  91. /* All non-predicative extensions must be added here. */
  92. return tcf_exts_is_predicative(exts);
  93. }
  94. /**
  95. * tcf_exts_exec - execute tc filter extensions
  96. * @skb: socket buffer
  97. * @exts: tc filter extensions handle
  98. * @res: desired result
  99. *
  100. * Executes all configured extensions. Returns 0 on a normal execution,
  101. * a negative number if the filter must be considered unmatched or
  102. * a positive action code (TC_ACT_*) which must be returned to the
  103. * underlying layer.
  104. */
  105. static inline int
  106. tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
  107. struct tcf_result *res)
  108. {
  109. #ifdef CONFIG_NET_CLS_ACT
  110. if (!list_empty(&exts->actions))
  111. return tcf_action_exec(skb, &exts->actions, res);
  112. #endif
  113. return 0;
  114. }
  115. int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
  116. struct nlattr **tb, struct nlattr *rate_tlv,
  117. struct tcf_exts *exts, bool ovr);
  118. void tcf_exts_destroy(struct tcf_exts *exts);
  119. void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
  120. struct tcf_exts *src);
  121. int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
  122. int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
  123. /**
  124. * struct tcf_pkt_info - packet information
  125. */
  126. struct tcf_pkt_info {
  127. unsigned char * ptr;
  128. int nexthdr;
  129. };
  130. #ifdef CONFIG_NET_EMATCH
  131. struct tcf_ematch_ops;
  132. /**
  133. * struct tcf_ematch - extended match (ematch)
  134. *
  135. * @matchid: identifier to allow userspace to reidentify a match
  136. * @flags: flags specifying attributes and the relation to other matches
  137. * @ops: the operations lookup table of the corresponding ematch module
  138. * @datalen: length of the ematch specific configuration data
  139. * @data: ematch specific data
  140. */
  141. struct tcf_ematch {
  142. struct tcf_ematch_ops * ops;
  143. unsigned long data;
  144. unsigned int datalen;
  145. u16 matchid;
  146. u16 flags;
  147. struct net *net;
  148. };
  149. static inline int tcf_em_is_container(struct tcf_ematch *em)
  150. {
  151. return !em->ops;
  152. }
  153. static inline int tcf_em_is_simple(struct tcf_ematch *em)
  154. {
  155. return em->flags & TCF_EM_SIMPLE;
  156. }
  157. static inline int tcf_em_is_inverted(struct tcf_ematch *em)
  158. {
  159. return em->flags & TCF_EM_INVERT;
  160. }
  161. static inline int tcf_em_last_match(struct tcf_ematch *em)
  162. {
  163. return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
  164. }
  165. static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
  166. {
  167. if (tcf_em_last_match(em))
  168. return 1;
  169. if (result == 0 && em->flags & TCF_EM_REL_AND)
  170. return 1;
  171. if (result != 0 && em->flags & TCF_EM_REL_OR)
  172. return 1;
  173. return 0;
  174. }
  175. /**
  176. * struct tcf_ematch_tree - ematch tree handle
  177. *
  178. * @hdr: ematch tree header supplied by userspace
  179. * @matches: array of ematches
  180. */
  181. struct tcf_ematch_tree {
  182. struct tcf_ematch_tree_hdr hdr;
  183. struct tcf_ematch * matches;
  184. };
  185. /**
  186. * struct tcf_ematch_ops - ematch module operations
  187. *
  188. * @kind: identifier (kind) of this ematch module
  189. * @datalen: length of expected configuration data (optional)
  190. * @change: called during validation (optional)
  191. * @match: called during ematch tree evaluation, must return 1/0
  192. * @destroy: called during destroyage (optional)
  193. * @dump: called during dumping process (optional)
  194. * @owner: owner, must be set to THIS_MODULE
  195. * @link: link to previous/next ematch module (internal use)
  196. */
  197. struct tcf_ematch_ops {
  198. int kind;
  199. int datalen;
  200. int (*change)(struct net *net, void *,
  201. int, struct tcf_ematch *);
  202. int (*match)(struct sk_buff *, struct tcf_ematch *,
  203. struct tcf_pkt_info *);
  204. void (*destroy)(struct tcf_ematch *);
  205. int (*dump)(struct sk_buff *, struct tcf_ematch *);
  206. struct module *owner;
  207. struct list_head link;
  208. };
  209. int tcf_em_register(struct tcf_ematch_ops *);
  210. void tcf_em_unregister(struct tcf_ematch_ops *);
  211. int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
  212. struct tcf_ematch_tree *);
  213. void tcf_em_tree_destroy(struct tcf_ematch_tree *);
  214. int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
  215. int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
  216. struct tcf_pkt_info *);
  217. /**
  218. * tcf_em_tree_change - replace ematch tree of a running classifier
  219. *
  220. * @tp: classifier kind handle
  221. * @dst: destination ematch tree variable
  222. * @src: source ematch tree (temporary tree from tcf_em_tree_validate)
  223. *
  224. * This functions replaces the ematch tree in @dst with the ematch
  225. * tree in @src. The classifier in charge of the ematch tree may be
  226. * running.
  227. */
  228. static inline void tcf_em_tree_change(struct tcf_proto *tp,
  229. struct tcf_ematch_tree *dst,
  230. struct tcf_ematch_tree *src)
  231. {
  232. tcf_tree_lock(tp);
  233. memcpy(dst, src, sizeof(*dst));
  234. tcf_tree_unlock(tp);
  235. }
  236. /**
  237. * tcf_em_tree_match - evaulate an ematch tree
  238. *
  239. * @skb: socket buffer of the packet in question
  240. * @tree: ematch tree to be used for evaluation
  241. * @info: packet information examined by classifier
  242. *
  243. * This function matches @skb against the ematch tree in @tree by going
  244. * through all ematches respecting their logic relations returning
  245. * as soon as the result is obvious.
  246. *
  247. * Returns 1 if the ematch tree as-one matches, no ematches are configured
  248. * or ematch is not enabled in the kernel, otherwise 0 is returned.
  249. */
  250. static inline int tcf_em_tree_match(struct sk_buff *skb,
  251. struct tcf_ematch_tree *tree,
  252. struct tcf_pkt_info *info)
  253. {
  254. if (tree->hdr.nmatches)
  255. return __tcf_em_tree_match(skb, tree, info);
  256. else
  257. return 1;
  258. }
  259. #define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind))
  260. #else /* CONFIG_NET_EMATCH */
  261. struct tcf_ematch_tree {
  262. };
  263. #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
  264. #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
  265. #define tcf_em_tree_dump(skb, t, tlv) (0)
  266. #define tcf_em_tree_change(tp, dst, src) do { } while(0)
  267. #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
  268. #endif /* CONFIG_NET_EMATCH */
  269. static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
  270. {
  271. switch (layer) {
  272. case TCF_LAYER_LINK:
  273. return skb->data;
  274. case TCF_LAYER_NETWORK:
  275. return skb_network_header(skb);
  276. case TCF_LAYER_TRANSPORT:
  277. return skb_transport_header(skb);
  278. }
  279. return NULL;
  280. }
  281. static inline int tcf_valid_offset(const struct sk_buff *skb,
  282. const unsigned char *ptr, const int len)
  283. {
  284. return likely((ptr + len) <= skb_tail_pointer(skb) &&
  285. ptr >= skb->head &&
  286. (ptr <= (ptr + len)));
  287. }
  288. #ifdef CONFIG_NET_CLS_IND
  289. #include <net/net_namespace.h>
  290. static inline int
  291. tcf_change_indev(struct net *net, struct nlattr *indev_tlv)
  292. {
  293. char indev[IFNAMSIZ];
  294. struct net_device *dev;
  295. if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
  296. return -EINVAL;
  297. dev = __dev_get_by_name(net, indev);
  298. if (!dev)
  299. return -ENODEV;
  300. return dev->ifindex;
  301. }
  302. static inline bool
  303. tcf_match_indev(struct sk_buff *skb, int ifindex)
  304. {
  305. if (!ifindex)
  306. return true;
  307. if (!skb->skb_iif)
  308. return false;
  309. return ifindex == skb->skb_iif;
  310. }
  311. #endif /* CONFIG_NET_CLS_IND */
  312. #endif