fib_rules.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /* 2 bytes hole, try to use */
  27. u32 target;
  28. __be64 tun_id;
  29. struct fib_rule __rcu *ctarget;
  30. struct net *fr_net;
  31. refcount_t refcnt;
  32. u32 pref;
  33. int suppress_ifgroup;
  34. int suppress_prefixlen;
  35. char iifname[IFNAMSIZ];
  36. char oifname[IFNAMSIZ];
  37. struct fib_kuid_range uid_range;
  38. struct rcu_head rcu;
  39. };
  40. struct fib_lookup_arg {
  41. void *lookup_ptr;
  42. void *result;
  43. struct fib_rule *rule;
  44. u32 table;
  45. int flags;
  46. #define FIB_LOOKUP_NOREF 1
  47. #define FIB_LOOKUP_IGNORE_LINKSTATE 2
  48. };
  49. struct fib_rules_ops {
  50. int family;
  51. struct list_head list;
  52. int rule_size;
  53. int addr_size;
  54. int unresolved_rules;
  55. int nr_goto_rules;
  56. unsigned int fib_rules_seq;
  57. int (*action)(struct fib_rule *,
  58. struct flowi *, int,
  59. struct fib_lookup_arg *);
  60. bool (*suppress)(struct fib_rule *,
  61. struct fib_lookup_arg *);
  62. int (*match)(struct fib_rule *,
  63. struct flowi *, int);
  64. int (*configure)(struct fib_rule *,
  65. struct sk_buff *,
  66. struct fib_rule_hdr *,
  67. struct nlattr **);
  68. int (*delete)(struct fib_rule *);
  69. int (*compare)(struct fib_rule *,
  70. struct fib_rule_hdr *,
  71. struct nlattr **);
  72. int (*fill)(struct fib_rule *, struct sk_buff *,
  73. struct fib_rule_hdr *);
  74. size_t (*nlmsg_payload)(struct fib_rule *);
  75. /* Called after modifications to the rules set, must flush
  76. * the route cache if one exists. */
  77. void (*flush_cache)(struct fib_rules_ops *ops);
  78. int nlgroup;
  79. const struct nla_policy *policy;
  80. struct list_head rules_list;
  81. struct module *owner;
  82. struct net *fro_net;
  83. struct rcu_head rcu;
  84. };
  85. struct fib_rule_notifier_info {
  86. struct fib_notifier_info info; /* must be first */
  87. struct fib_rule *rule;
  88. };
  89. #define FRA_GENERIC_POLICY \
  90. [FRA_IIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
  91. [FRA_OIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
  92. [FRA_PRIORITY] = { .type = NLA_U32 }, \
  93. [FRA_FWMARK] = { .type = NLA_U32 }, \
  94. [FRA_TUN_ID] = { .type = NLA_U64 }, \
  95. [FRA_FWMASK] = { .type = NLA_U32 }, \
  96. [FRA_TABLE] = { .type = NLA_U32 }, \
  97. [FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
  98. [FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
  99. [FRA_GOTO] = { .type = NLA_U32 }, \
  100. [FRA_L3MDEV] = { .type = NLA_U8 }, \
  101. [FRA_UID_RANGE] = { .len = sizeof(struct fib_rule_uid_range) }
  102. static inline void fib_rule_get(struct fib_rule *rule)
  103. {
  104. refcount_inc(&rule->refcnt);
  105. }
  106. static inline void fib_rule_put(struct fib_rule *rule)
  107. {
  108. if (refcount_dec_and_test(&rule->refcnt))
  109. kfree_rcu(rule, rcu);
  110. }
  111. #ifdef CONFIG_NET_L3_MASTER_DEV
  112. static inline u32 fib_rule_get_table(struct fib_rule *rule,
  113. struct fib_lookup_arg *arg)
  114. {
  115. return rule->l3mdev ? arg->table : rule->table;
  116. }
  117. #else
  118. static inline u32 fib_rule_get_table(struct fib_rule *rule,
  119. struct fib_lookup_arg *arg)
  120. {
  121. return rule->table;
  122. }
  123. #endif
  124. static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
  125. {
  126. if (nla[FRA_TABLE])
  127. return nla_get_u32(nla[FRA_TABLE]);
  128. return frh->table;
  129. }
  130. struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
  131. struct net *);
  132. void fib_rules_unregister(struct fib_rules_ops *);
  133. int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
  134. struct fib_lookup_arg *);
  135. int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
  136. u32 flags);
  137. bool fib_rule_matchall(const struct fib_rule *rule);
  138. int fib_rules_dump(struct net *net, struct notifier_block *nb, int family);
  139. unsigned int fib_rules_seq_read(struct net *net, int family);
  140. int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
  141. struct netlink_ext_ack *extack);
  142. int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
  143. struct netlink_ext_ack *extack);
  144. #endif