pkt_cls.h 9.2 KB

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