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