fib_rules.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_FIB_RULES_H
  3. #define __NET_FIB_RULES_H
  4. #include <linux/types.h>
  5. #include <linux/slab.h>
  6. #include <linux/netdevice.h>
  7. #include <linux/fib_rules.h>
  8. #include <linux/refcount.h>
  9. #include <net/flow.h>
  10. #include <net/rtnetlink.h>
  11. #include <net/fib_notifier.h>
  12. #include <linux/indirect_call_wrapper.h>
  13. struct fib_kuid_range {
  14. kuid_t start;
  15. kuid_t end;
  16. };
  17. struct fib_rule {
  18. struct list_head list;
  19. int iifindex;
  20. int oifindex;
  21. u32 mark;
  22. u32 mark_mask;
  23. u32 flags;
  24. u32 table;
  25. u8 action;
  26. u8 l3mdev;
  27. u8 proto;
  28. u8 ip_proto;
  29. u32 target;
  30. __be64 tun_id;
  31. struct fib_rule __rcu *ctarget;
  32. struct net *fr_net;
  33. refcount_t refcnt;
  34. u32 pref;
  35. int suppress_ifgroup;
  36. int suppress_prefixlen;
  37. char iifname[IFNAMSIZ];
  38. char oifname[IFNAMSIZ];
  39. struct fib_kuid_range uid_range;
  40. struct fib_rule_port_range sport_range;
  41. struct fib_rule_port_range dport_range;
  42. struct rcu_head rcu;
  43. };
  44. struct fib_lookup_arg {
  45. void *lookup_ptr;
  46. const void *lookup_data;
  47. void *result;
  48. struct fib_rule *rule;
  49. u32 table;
  50. int flags;
  51. #define FIB_LOOKUP_NOREF 1
  52. #define FIB_LOOKUP_IGNORE_LINKSTATE 2
  53. };
  54. struct fib_rules_ops {
  55. int family;
  56. struct list_head list;
  57. int rule_size;
  58. int addr_size;
  59. int unresolved_rules;
  60. int nr_goto_rules;
  61. unsigned int fib_rules_seq;
  62. int (*action)(struct fib_rule *,
  63. struct flowi *, int,
  64. struct fib_lookup_arg *);
  65. bool (*suppress)(struct fib_rule *, int,
  66. struct fib_lookup_arg *);
  67. int (*match)(struct fib_rule *,
  68. struct flowi *, int);
  69. int (*configure)(struct fib_rule *,
  70. struct sk_buff *,
  71. struct fib_rule_hdr *,
  72. struct nlattr **,
  73. struct netlink_ext_ack *);
  74. int (*delete)(struct fib_rule *);
  75. int (*compare)(struct fib_rule *,
  76. struct fib_rule_hdr *,
  77. struct nlattr **);
  78. int (*fill)(struct fib_rule *, struct sk_buff *,
  79. struct fib_rule_hdr *);
  80. size_t (*nlmsg_payload)(struct fib_rule *);
  81. /* Called after modifications to the rules set, must flush
  82. * the route cache if one exists. */
  83. void (*flush_cache)(struct fib_rules_ops *ops);
  84. int nlgroup;
  85. struct list_head rules_list;
  86. struct module *owner;
  87. struct net *fro_net;
  88. struct rcu_head rcu;
  89. };
  90. struct fib_rule_notifier_info {
  91. struct fib_notifier_info info; /* must be first */
  92. struct fib_rule *rule;
  93. };
  94. static inline void fib_rule_get(struct fib_rule *rule)
  95. {
  96. refcount_inc(&rule->refcnt);
  97. }
  98. static inline void fib_rule_put(struct fib_rule *rule)
  99. {
  100. if (refcount_dec_and_test(&rule->refcnt))
  101. kfree_rcu(rule, rcu);
  102. }
  103. #ifdef CONFIG_NET_L3_MASTER_DEV
  104. static inline u32 fib_rule_get_table(struct fib_rule *rule,
  105. struct fib_lookup_arg *arg)
  106. {
  107. return rule->l3mdev ? arg->table : rule->table;
  108. }
  109. #else
  110. static inline u32 fib_rule_get_table(struct fib_rule *rule,
  111. struct fib_lookup_arg *arg)
  112. {
  113. return rule->table;
  114. }
  115. #endif
  116. static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
  117. {
  118. if (nla[FRA_TABLE])
  119. return nla_get_u32(nla[FRA_TABLE]);
  120. return frh->table;
  121. }
  122. static inline bool fib_rule_port_range_set(const struct fib_rule_port_range *range)
  123. {
  124. return range->start != 0 && range->end != 0;
  125. }
  126. static inline bool fib_rule_port_inrange(const struct fib_rule_port_range *a,
  127. __be16 port)
  128. {
  129. return ntohs(port) >= a->start &&
  130. ntohs(port) <= a->end;
  131. }
  132. static inline bool fib_rule_port_range_valid(const struct fib_rule_port_range *a)
  133. {
  134. return a->start != 0 && a->end != 0 && a->end < 0xffff &&
  135. a->start <= a->end;
  136. }
  137. static inline bool fib_rule_port_range_compare(struct fib_rule_port_range *a,
  138. struct fib_rule_port_range *b)
  139. {
  140. return a->start == b->start &&
  141. a->end == b->end;
  142. }
  143. static inline bool fib_rule_requires_fldissect(struct fib_rule *rule)
  144. {
  145. return rule->iifindex != LOOPBACK_IFINDEX && (rule->ip_proto ||
  146. fib_rule_port_range_set(&rule->sport_range) ||
  147. fib_rule_port_range_set(&rule->dport_range));
  148. }
  149. struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
  150. struct net *);
  151. void fib_rules_unregister(struct fib_rules_ops *);
  152. int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
  153. struct fib_lookup_arg *);
  154. int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
  155. u32 flags);
  156. bool fib_rule_matchall(const struct fib_rule *rule);
  157. int fib_rules_dump(struct net *net, struct notifier_block *nb, int family,
  158. struct netlink_ext_ack *extack);
  159. unsigned int fib_rules_seq_read(struct net *net, int family);
  160. int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
  161. struct netlink_ext_ack *extack);
  162. int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
  163. struct netlink_ext_ack *extack);
  164. INDIRECT_CALLABLE_DECLARE(int fib6_rule_match(struct fib_rule *rule,
  165. struct flowi *fl, int flags));
  166. INDIRECT_CALLABLE_DECLARE(int fib4_rule_match(struct fib_rule *rule,
  167. struct flowi *fl, int flags));
  168. INDIRECT_CALLABLE_DECLARE(int fib6_rule_action(struct fib_rule *rule,
  169. struct flowi *flp, int flags,
  170. struct fib_lookup_arg *arg));
  171. INDIRECT_CALLABLE_DECLARE(int fib4_rule_action(struct fib_rule *rule,
  172. struct flowi *flp, int flags,
  173. struct fib_lookup_arg *arg));
  174. INDIRECT_CALLABLE_DECLARE(bool fib6_rule_suppress(struct fib_rule *rule,
  175. int flags,
  176. struct fib_lookup_arg *arg));
  177. INDIRECT_CALLABLE_DECLARE(bool fib4_rule_suppress(struct fib_rule *rule,
  178. int flags,
  179. struct fib_lookup_arg *arg));
  180. #endif