pkt_cls.h 9.1 KB

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