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. struct fib_kuid_range {
  13. kuid_t start;
  14. kuid_t end;
  15. };
  16. struct fib_rule {
  17. struct list_head list;
  18. int iifindex;
  19. int oifindex;
  20. u32 mark;
  21. u32 mark_mask;
  22. u32 flags;
  23. u32 table;
  24. u8 action;
  25. u8 l3mdev;
  26. u8 proto;
  27. u8 ip_proto;
  28. u32 target;
  29. __be64 tun_id;
  30. struct fib_rule __rcu *ctarget;
  31. struct net *fr_net;
  32. refcount_t refcnt;
  33. u32 pref;
  34. int suppress_ifgroup;
  35. int suppress_prefixlen;
  36. char iifname[IFNAMSIZ];
  37. char oifname[IFNAMSIZ];
  38. struct fib_kuid_range uid_range;
  39. struct fib_rule_port_range sport_range;
  40. struct fib_rule_port_range dport_range;
  41. struct rcu_head rcu;
  42. };
  43. struct fib_lookup_arg {
  44. void *lookup_ptr;
  45. const void *lookup_data;
  46. void *result;
  47. struct fib_rule *rule;
  48. u32 table;
  49. int flags;
  50. #define FIB_LOOKUP_NOREF 1
  51. #define FIB_LOOKUP_IGNORE_LINKSTATE 2
  52. };
  53. struct fib_rules_ops {
  54. int family;
  55. struct list_head list;
  56. int rule_size;
  57. int addr_size;
  58. int unresolved_rules;
  59. int nr_goto_rules;
  60. unsigned int fib_rules_seq;
  61. int (*action)(struct fib_rule *,
  62. struct flowi *, int,
  63. struct fib_lookup_arg *);
  64. bool (*suppress)(struct fib_rule *,
  65. struct fib_lookup_arg *);
  66. int (*match)(struct fib_rule *,
  67. struct flowi *, int);
  68. int (*configure)(struct fib_rule *,
  69. struct sk_buff *,
  70. struct fib_rule_hdr *,
  71. struct nlattr **,
  72. struct netlink_ext_ack *);
  73. int (*delete)(struct fib_rule *);
  74. int (*compare)(struct fib_rule *,
  75. struct fib_rule_hdr *,
  76. struct nlattr **);
  77. int (*fill)(struct fib_rule *, struct sk_buff *,
  78. struct fib_rule_hdr *);
  79. size_t (*nlmsg_payload)(struct fib_rule *);
  80. /* Called after modifications to the rules set, must flush
  81. * the route cache if one exists. */
  82. void (*flush_cache)(struct fib_rules_ops *ops);
  83. int nlgroup;
  84. const struct nla_policy *policy;
  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. #define FRA_GENERIC_POLICY \
  95. [FRA_IIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
  96. [FRA_OIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
  97. [FRA_PRIORITY] = { .type = NLA_U32 }, \
  98. [FRA_FWMARK] = { .type = NLA_U32 }, \
  99. [FRA_TUN_ID] = { .type = NLA_U64 }, \
  100. [FRA_FWMASK] = { .type = NLA_U32 }, \
  101. [FRA_TABLE] = { .type = NLA_U32 }, \
  102. [FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
  103. [FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
  104. [FRA_GOTO] = { .type = NLA_U32 }, \
  105. [FRA_L3MDEV] = { .type = NLA_U8 }, \
  106. [FRA_UID_RANGE] = { .len = sizeof(struct fib_rule_uid_range) }, \
  107. [FRA_PROTOCOL] = { .type = NLA_U8 }, \
  108. [FRA_IP_PROTO] = { .type = NLA_U8 }, \
  109. [FRA_SPORT_RANGE] = { .len = sizeof(struct fib_rule_port_range) }, \
  110. [FRA_DPORT_RANGE] = { .len = sizeof(struct fib_rule_port_range) }
  111. static inline void fib_rule_get(struct fib_rule *rule)
  112. {
  113. refcount_inc(&rule->refcnt);
  114. }
  115. static inline void fib_rule_put(struct fib_rule *rule)
  116. {
  117. if (refcount_dec_and_test(&rule->refcnt))
  118. kfree_rcu(rule, rcu);
  119. }
  120. #ifdef CONFIG_NET_L3_MASTER_DEV
  121. static inline u32 fib_rule_get_table(struct fib_rule *rule,
  122. struct fib_lookup_arg *arg)
  123. {
  124. return rule->l3mdev ? arg->table : rule->table;
  125. }
  126. #else
  127. static inline u32 fib_rule_get_table(struct fib_rule *rule,
  128. struct fib_lookup_arg *arg)
  129. {
  130. return rule->table;
  131. }
  132. #endif
  133. static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
  134. {
  135. if (nla[FRA_TABLE])
  136. return nla_get_u32(nla[FRA_TABLE]);
  137. return frh->table;
  138. }
  139. static inline bool fib_rule_port_range_set(const struct fib_rule_port_range *range)
  140. {
  141. return range->start != 0 && range->end != 0;
  142. }
  143. static inline bool fib_rule_port_inrange(const struct fib_rule_port_range *a,
  144. __be16 port)
  145. {
  146. return ntohs(port) >= a->start &&
  147. ntohs(port) <= a->end;
  148. }
  149. static inline bool fib_rule_port_range_valid(const struct fib_rule_port_range *a)
  150. {
  151. return a->start != 0 && a->end != 0 && a->end < 0xffff &&
  152. a->start <= a->end;
  153. }
  154. static inline bool fib_rule_port_range_compare(struct fib_rule_port_range *a,
  155. struct fib_rule_port_range *b)
  156. {
  157. return a->start == b->start &&
  158. a->end == b->end;
  159. }
  160. static inline bool fib_rule_requires_fldissect(struct fib_rule *rule)
  161. {
  162. return rule->ip_proto ||
  163. fib_rule_port_range_set(&rule->sport_range) ||
  164. fib_rule_port_range_set(&rule->dport_range);
  165. }
  166. struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
  167. struct net *);
  168. void fib_rules_unregister(struct fib_rules_ops *);
  169. int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
  170. struct fib_lookup_arg *);
  171. int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
  172. u32 flags);
  173. bool fib_rule_matchall(const struct fib_rule *rule);
  174. int fib_rules_dump(struct net *net, struct notifier_block *nb, int family);
  175. unsigned int fib_rules_seq_read(struct net *net, int family);
  176. int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
  177. struct netlink_ext_ack *extack);
  178. int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
  179. struct netlink_ext_ack *extack);
  180. #endif