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