Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 2 | #ifndef __NET_FIB_RULES_H |
| 3 | #define __NET_FIB_RULES_H |
| 4 | |
| 5 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 6 | #include <linux/slab.h> |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 7 | #include <linux/netdevice.h> |
| 8 | #include <linux/fib_rules.h> |
Reshetova, Elena | 717d1e9 | 2017-06-30 13:08:06 +0300 | [diff] [blame] | 9 | #include <linux/refcount.h> |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 10 | #include <net/flow.h> |
Thomas Graf | 9d9e6a5 | 2007-03-25 23:20:05 -0700 | [diff] [blame] | 11 | #include <net/rtnetlink.h> |
Ido Schimmel | 1b2a444 | 2017-08-03 13:28:14 +0200 | [diff] [blame] | 12 | #include <net/fib_notifier.h> |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 13 | |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 14 | struct fib_kuid_range { |
| 15 | kuid_t start; |
| 16 | kuid_t end; |
| 17 | }; |
| 18 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 19 | struct fib_rule { |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 20 | struct list_head list; |
Patrick McHardy | 491deb2 | 2009-12-03 01:25:54 +0000 | [diff] [blame] | 21 | int iifindex; |
Patrick McHardy | 1b038a5 | 2009-12-03 01:25:56 +0000 | [diff] [blame] | 22 | int oifindex; |
Thomas Graf | b8964ed | 2006-11-09 15:22:18 -0800 | [diff] [blame] | 23 | u32 mark; |
| 24 | u32 mark_mask; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 25 | u32 flags; |
| 26 | u32 table; |
| 27 | u8 action; |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 28 | u8 l3mdev; |
Donald Sharp | cac5620 | 2018-02-20 08:55:58 -0500 | [diff] [blame] | 29 | u8 proto; |
Roopa Prabhu | bfff486 | 2018-02-28 22:40:16 -0500 | [diff] [blame] | 30 | u8 ip_proto; |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 31 | u32 target; |
Thomas Graf | e703087 | 2015-07-21 10:44:01 +0200 | [diff] [blame] | 32 | __be64 tun_id; |
Eric Dumazet | 7a2b03c | 2010-10-26 09:24:55 +0000 | [diff] [blame] | 33 | struct fib_rule __rcu *ctarget; |
Eric Dumazet | fba3679 | 2013-08-03 11:50:35 -0700 | [diff] [blame] | 34 | struct net *fr_net; |
| 35 | |
Reshetova, Elena | 717d1e9 | 2017-06-30 13:08:06 +0300 | [diff] [blame] | 36 | refcount_t refcnt; |
Eric Dumazet | fba3679 | 2013-08-03 11:50:35 -0700 | [diff] [blame] | 37 | u32 pref; |
| 38 | int suppress_ifgroup; |
| 39 | int suppress_prefixlen; |
Patrick McHardy | 491deb2 | 2009-12-03 01:25:54 +0000 | [diff] [blame] | 40 | char iifname[IFNAMSIZ]; |
Patrick McHardy | 1b038a5 | 2009-12-03 01:25:56 +0000 | [diff] [blame] | 41 | char oifname[IFNAMSIZ]; |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 42 | struct fib_kuid_range uid_range; |
Roopa Prabhu | bfff486 | 2018-02-28 22:40:16 -0500 | [diff] [blame] | 43 | struct fib_rule_port_range sport_range; |
| 44 | struct fib_rule_port_range dport_range; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 45 | struct rcu_head rcu; |
| 46 | }; |
| 47 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 48 | struct fib_lookup_arg { |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 49 | void *lookup_ptr; |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 50 | const void *lookup_data; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 51 | void *result; |
| 52 | struct fib_rule *rule; |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 53 | u32 table; |
Eric Dumazet | ebc0ffa | 2010-10-05 10:41:36 +0000 | [diff] [blame] | 54 | int flags; |
Andy Gospodarek | 0eeb075 | 2015-06-23 13:45:37 -0400 | [diff] [blame] | 55 | #define FIB_LOOKUP_NOREF 1 |
| 56 | #define FIB_LOOKUP_IGNORE_LINKSTATE 2 |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 59 | struct fib_rules_ops { |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 60 | int family; |
| 61 | struct list_head list; |
| 62 | int rule_size; |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame] | 63 | int addr_size; |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 64 | int unresolved_rules; |
| 65 | int nr_goto_rules; |
Ido Schimmel | 1b2a444 | 2017-08-03 13:28:14 +0200 | [diff] [blame] | 66 | unsigned int fib_rules_seq; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 67 | |
| 68 | int (*action)(struct fib_rule *, |
| 69 | struct flowi *, int, |
| 70 | struct fib_lookup_arg *); |
Stefan Tomanek | 7764a45 | 2013-08-01 02:17:15 +0200 | [diff] [blame] | 71 | bool (*suppress)(struct fib_rule *, |
| 72 | struct fib_lookup_arg *); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 73 | int (*match)(struct fib_rule *, |
| 74 | struct flowi *, int); |
| 75 | int (*configure)(struct fib_rule *, |
| 76 | struct sk_buff *, |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 77 | struct fib_rule_hdr *, |
Roopa Prabhu | b16fb41 | 2018-04-21 09:41:31 -0700 | [diff] [blame] | 78 | struct nlattr **, |
| 79 | struct netlink_ext_ack *); |
Alexander Duyck | 0ddcf43 | 2015-03-06 13:47:00 -0800 | [diff] [blame] | 80 | int (*delete)(struct fib_rule *); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 81 | int (*compare)(struct fib_rule *, |
| 82 | struct fib_rule_hdr *, |
| 83 | struct nlattr **); |
| 84 | int (*fill)(struct fib_rule *, struct sk_buff *, |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 85 | struct fib_rule_hdr *); |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 86 | size_t (*nlmsg_payload)(struct fib_rule *); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 87 | |
Thomas Graf | 73417f6 | 2007-03-27 13:56:52 -0700 | [diff] [blame] | 88 | /* Called after modifications to the rules set, must flush |
| 89 | * the route cache if one exists. */ |
Denis V. Lunev | ae299fc | 2008-07-05 19:01:28 -0700 | [diff] [blame] | 90 | void (*flush_cache)(struct fib_rules_ops *ops); |
Thomas Graf | 73417f6 | 2007-03-27 13:56:52 -0700 | [diff] [blame] | 91 | |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 92 | int nlgroup; |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 93 | const struct nla_policy *policy; |
Denis V. Lunev | 76c72d4 | 2007-09-16 15:44:27 -0700 | [diff] [blame] | 94 | struct list_head rules_list; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 95 | struct module *owner; |
Denis V. Lunev | 0359238 | 2008-01-20 16:46:01 -0800 | [diff] [blame] | 96 | struct net *fro_net; |
Eric W. Biederman | e9c5158 | 2009-12-03 12:22:55 -0800 | [diff] [blame] | 97 | struct rcu_head rcu; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Ido Schimmel | 1b2a444 | 2017-08-03 13:28:14 +0200 | [diff] [blame] | 100 | struct fib_rule_notifier_info { |
| 101 | struct fib_notifier_info info; /* must be first */ |
| 102 | struct fib_rule *rule; |
| 103 | }; |
| 104 | |
Thomas Graf | 1f6c955 | 2006-11-09 15:22:48 -0800 | [diff] [blame] | 105 | #define FRA_GENERIC_POLICY \ |
Patrick McHardy | 491deb2 | 2009-12-03 01:25:54 +0000 | [diff] [blame] | 106 | [FRA_IIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \ |
Patrick McHardy | 1b038a5 | 2009-12-03 01:25:56 +0000 | [diff] [blame] | 107 | [FRA_OIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \ |
Thomas Graf | 1f6c955 | 2006-11-09 15:22:48 -0800 | [diff] [blame] | 108 | [FRA_PRIORITY] = { .type = NLA_U32 }, \ |
| 109 | [FRA_FWMARK] = { .type = NLA_U32 }, \ |
| 110 | [FRA_FWMASK] = { .type = NLA_U32 }, \ |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 111 | [FRA_TABLE] = { .type = NLA_U32 }, \ |
Stefan Tomanek | 73f5698 | 2013-08-03 14:14:43 +0200 | [diff] [blame] | 112 | [FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \ |
Stefan Tomanek | 6ef94cf | 2013-08-02 17:19:56 +0200 | [diff] [blame] | 113 | [FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \ |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 114 | [FRA_GOTO] = { .type = NLA_U32 }, \ |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 115 | [FRA_L3MDEV] = { .type = NLA_U8 }, \ |
Donald Sharp | 1b71af6 | 2018-02-23 14:01:52 -0500 | [diff] [blame] | 116 | [FRA_UID_RANGE] = { .len = sizeof(struct fib_rule_uid_range) }, \ |
Roopa Prabhu | 5f6f845 | 2018-03-01 17:55:37 -0800 | [diff] [blame] | 117 | [FRA_PROTOCOL] = { .type = NLA_U8 }, \ |
| 118 | [FRA_IP_PROTO] = { .type = NLA_U8 }, \ |
| 119 | [FRA_SPORT_RANGE] = { .len = sizeof(struct fib_rule_port_range) }, \ |
| 120 | [FRA_DPORT_RANGE] = { .len = sizeof(struct fib_rule_port_range) } |
| 121 | |
Thomas Graf | 1f6c955 | 2006-11-09 15:22:48 -0800 | [diff] [blame] | 122 | |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 123 | static inline void fib_rule_get(struct fib_rule *rule) |
| 124 | { |
Reshetova, Elena | 717d1e9 | 2017-06-30 13:08:06 +0300 | [diff] [blame] | 125 | refcount_inc(&rule->refcnt); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 128 | static inline void fib_rule_put(struct fib_rule *rule) |
| 129 | { |
Reshetova, Elena | 717d1e9 | 2017-06-30 13:08:06 +0300 | [diff] [blame] | 130 | if (refcount_dec_and_test(&rule->refcnt)) |
Eric W. Biederman | efd7ef1 | 2015-03-11 23:04:08 -0500 | [diff] [blame] | 131 | kfree_rcu(rule, rcu); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 132 | } |
| 133 | |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 134 | #ifdef CONFIG_NET_L3_MASTER_DEV |
| 135 | static inline u32 fib_rule_get_table(struct fib_rule *rule, |
| 136 | struct fib_lookup_arg *arg) |
| 137 | { |
| 138 | return rule->l3mdev ? arg->table : rule->table; |
| 139 | } |
| 140 | #else |
| 141 | static inline u32 fib_rule_get_table(struct fib_rule *rule, |
| 142 | struct fib_lookup_arg *arg) |
| 143 | { |
| 144 | return rule->table; |
| 145 | } |
| 146 | #endif |
| 147 | |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 148 | static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla) |
| 149 | { |
| 150 | if (nla[FRA_TABLE]) |
| 151 | return nla_get_u32(nla[FRA_TABLE]); |
| 152 | return frh->table; |
| 153 | } |
| 154 | |
Roopa Prabhu | bfff486 | 2018-02-28 22:40:16 -0500 | [diff] [blame] | 155 | static inline bool fib_rule_port_range_set(const struct fib_rule_port_range *range) |
| 156 | { |
| 157 | return range->start != 0 && range->end != 0; |
| 158 | } |
| 159 | |
| 160 | static inline bool fib_rule_port_inrange(const struct fib_rule_port_range *a, |
| 161 | __be16 port) |
| 162 | { |
| 163 | return ntohs(port) >= a->start && |
| 164 | ntohs(port) <= a->end; |
| 165 | } |
| 166 | |
| 167 | static inline bool fib_rule_port_range_valid(const struct fib_rule_port_range *a) |
| 168 | { |
| 169 | return a->start != 0 && a->end != 0 && a->end < 0xffff && |
| 170 | a->start <= a->end; |
| 171 | } |
| 172 | |
| 173 | static inline bool fib_rule_port_range_compare(struct fib_rule_port_range *a, |
| 174 | struct fib_rule_port_range *b) |
| 175 | { |
| 176 | return a->start == b->start && |
| 177 | a->end == b->end; |
| 178 | } |
| 179 | |
| 180 | static inline bool fib_rule_requires_fldissect(struct fib_rule *rule) |
| 181 | { |
| 182 | return rule->ip_proto || |
| 183 | fib_rule_port_range_set(&rule->sport_range) || |
| 184 | fib_rule_port_range_set(&rule->dport_range); |
| 185 | } |
| 186 | |
Joe Perches | 8de6879 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 187 | struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *, |
| 188 | struct net *); |
| 189 | void fib_rules_unregister(struct fib_rules_ops *); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 190 | |
Joe Perches | 8de6879 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 191 | int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags, |
| 192 | struct fib_lookup_arg *); |
| 193 | int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table, |
| 194 | u32 flags); |
Ido Schimmel | 3c71006 | 2017-03-16 09:08:12 +0100 | [diff] [blame] | 195 | bool fib_rule_matchall(const struct fib_rule *rule); |
Ido Schimmel | 1b2a444 | 2017-08-03 13:28:14 +0200 | [diff] [blame] | 196 | int fib_rules_dump(struct net *net, struct notifier_block *nb, int family); |
| 197 | unsigned int fib_rules_seq_read(struct net *net, int family); |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 198 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 199 | int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 200 | struct netlink_ext_ack *extack); |
| 201 | int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 202 | struct netlink_ext_ack *extack); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 203 | #endif |