blob: b473df5b95121f3f810d8ffe29f858369f5c8fd5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Thomas Graf14c0b972006-08-04 03:38:38 -07002#ifndef __NET_FIB_RULES_H
3#define __NET_FIB_RULES_H
4
5#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Thomas Graf14c0b972006-08-04 03:38:38 -07007#include <linux/netdevice.h>
8#include <linux/fib_rules.h>
Reshetova, Elena717d1e92017-06-30 13:08:06 +03009#include <linux/refcount.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070010#include <net/flow.h>
Thomas Graf9d9e6a52007-03-25 23:20:05 -070011#include <net/rtnetlink.h>
Ido Schimmel1b2a4442017-08-03 13:28:14 +020012#include <net/fib_notifier.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070013
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +090014struct fib_kuid_range {
15 kuid_t start;
16 kuid_t end;
17};
18
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000019struct fib_rule {
Thomas Graf14c0b972006-08-04 03:38:38 -070020 struct list_head list;
Patrick McHardy491deb22009-12-03 01:25:54 +000021 int iifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +000022 int oifindex;
Thomas Grafb8964ed2006-11-09 15:22:18 -080023 u32 mark;
24 u32 mark_mask;
Thomas Graf14c0b972006-08-04 03:38:38 -070025 u32 flags;
26 u32 table;
27 u8 action;
David Ahern96c63fa2016-06-08 10:55:39 -070028 u8 l3mdev;
Donald Sharpcac56202018-02-20 08:55:58 -050029 u8 proto;
Roopa Prabhubfff4862018-02-28 22:40:16 -050030 u8 ip_proto;
Thomas Graf0947c9fe2007-03-26 17:14:15 -070031 u32 target;
Thomas Grafe7030872015-07-21 10:44:01 +020032 __be64 tun_id;
Eric Dumazet7a2b03c2010-10-26 09:24:55 +000033 struct fib_rule __rcu *ctarget;
Eric Dumazetfba36792013-08-03 11:50:35 -070034 struct net *fr_net;
35
Reshetova, Elena717d1e92017-06-30 13:08:06 +030036 refcount_t refcnt;
Eric Dumazetfba36792013-08-03 11:50:35 -070037 u32 pref;
38 int suppress_ifgroup;
39 int suppress_prefixlen;
Patrick McHardy491deb22009-12-03 01:25:54 +000040 char iifname[IFNAMSIZ];
Patrick McHardy1b038a52009-12-03 01:25:56 +000041 char oifname[IFNAMSIZ];
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +090042 struct fib_kuid_range uid_range;
Roopa Prabhubfff4862018-02-28 22:40:16 -050043 struct fib_rule_port_range sport_range;
44 struct fib_rule_port_range dport_range;
Thomas Graf14c0b972006-08-04 03:38:38 -070045 struct rcu_head rcu;
46};
47
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000048struct fib_lookup_arg {
Thomas Graf14c0b972006-08-04 03:38:38 -070049 void *lookup_ptr;
David Ahernb75cc8f2018-03-02 08:32:17 -080050 const void *lookup_data;
Thomas Graf14c0b972006-08-04 03:38:38 -070051 void *result;
52 struct fib_rule *rule;
David Ahern96c63fa2016-06-08 10:55:39 -070053 u32 table;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +000054 int flags;
Andy Gospodarek0eeb0752015-06-23 13:45:37 -040055#define FIB_LOOKUP_NOREF 1
56#define FIB_LOOKUP_IGNORE_LINKSTATE 2
Thomas Graf14c0b972006-08-04 03:38:38 -070057};
58
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000059struct fib_rules_ops {
Thomas Graf14c0b972006-08-04 03:38:38 -070060 int family;
61 struct list_head list;
62 int rule_size;
Thomas Grafe1701c62007-03-24 12:46:02 -070063 int addr_size;
Thomas Graf0947c9fe2007-03-26 17:14:15 -070064 int unresolved_rules;
65 int nr_goto_rules;
Ido Schimmel1b2a4442017-08-03 13:28:14 +020066 unsigned int fib_rules_seq;
Thomas Graf14c0b972006-08-04 03:38:38 -070067
68 int (*action)(struct fib_rule *,
69 struct flowi *, int,
70 struct fib_lookup_arg *);
Stefan Tomanek7764a452013-08-01 02:17:15 +020071 bool (*suppress)(struct fib_rule *,
72 struct fib_lookup_arg *);
Thomas Graf14c0b972006-08-04 03:38:38 -070073 int (*match)(struct fib_rule *,
74 struct flowi *, int);
75 int (*configure)(struct fib_rule *,
76 struct sk_buff *,
Thomas Graf14c0b972006-08-04 03:38:38 -070077 struct fib_rule_hdr *,
Roopa Prabhub16fb412018-04-21 09:41:31 -070078 struct nlattr **,
79 struct netlink_ext_ack *);
Alexander Duyck0ddcf432015-03-06 13:47:00 -080080 int (*delete)(struct fib_rule *);
Thomas Graf14c0b972006-08-04 03:38:38 -070081 int (*compare)(struct fib_rule *,
82 struct fib_rule_hdr *,
83 struct nlattr **);
84 int (*fill)(struct fib_rule *, struct sk_buff *,
Thomas Graf14c0b972006-08-04 03:38:38 -070085 struct fib_rule_hdr *);
Thomas Graf339bf982006-11-10 14:10:15 -080086 size_t (*nlmsg_payload)(struct fib_rule *);
Thomas Graf14c0b972006-08-04 03:38:38 -070087
Thomas Graf73417f62007-03-27 13:56:52 -070088 /* Called after modifications to the rules set, must flush
89 * the route cache if one exists. */
Denis V. Lunevae299fc2008-07-05 19:01:28 -070090 void (*flush_cache)(struct fib_rules_ops *ops);
Thomas Graf73417f62007-03-27 13:56:52 -070091
Thomas Graf14c0b972006-08-04 03:38:38 -070092 int nlgroup;
Patrick McHardyef7c79e2007-06-05 12:38:30 -070093 const struct nla_policy *policy;
Denis V. Lunev76c72d42007-09-16 15:44:27 -070094 struct list_head rules_list;
Thomas Graf14c0b972006-08-04 03:38:38 -070095 struct module *owner;
Denis V. Lunev03592382008-01-20 16:46:01 -080096 struct net *fro_net;
Eric W. Biedermane9c51582009-12-03 12:22:55 -080097 struct rcu_head rcu;
Thomas Graf14c0b972006-08-04 03:38:38 -070098};
99
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200100struct fib_rule_notifier_info {
101 struct fib_notifier_info info; /* must be first */
102 struct fib_rule *rule;
103};
104
Thomas Graf1f6c9552006-11-09 15:22:48 -0800105#define FRA_GENERIC_POLICY \
Patrick McHardy491deb22009-12-03 01:25:54 +0000106 [FRA_IIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
Patrick McHardy1b038a52009-12-03 01:25:56 +0000107 [FRA_OIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
Thomas Graf1f6c9552006-11-09 15:22:48 -0800108 [FRA_PRIORITY] = { .type = NLA_U32 }, \
109 [FRA_FWMARK] = { .type = NLA_U32 }, \
110 [FRA_FWMASK] = { .type = NLA_U32 }, \
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700111 [FRA_TABLE] = { .type = NLA_U32 }, \
Stefan Tomanek73f56982013-08-03 14:14:43 +0200112 [FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200113 [FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
David Ahern96c63fa2016-06-08 10:55:39 -0700114 [FRA_GOTO] = { .type = NLA_U32 }, \
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900115 [FRA_L3MDEV] = { .type = NLA_U8 }, \
Donald Sharp1b71af62018-02-23 14:01:52 -0500116 [FRA_UID_RANGE] = { .len = sizeof(struct fib_rule_uid_range) }, \
Roopa Prabhu5f6f8452018-03-01 17:55:37 -0800117 [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 Graf1f6c9552006-11-09 15:22:48 -0800122
Thomas Graf14c0b972006-08-04 03:38:38 -0700123static inline void fib_rule_get(struct fib_rule *rule)
124{
Reshetova, Elena717d1e92017-06-30 13:08:06 +0300125 refcount_inc(&rule->refcnt);
Thomas Graf14c0b972006-08-04 03:38:38 -0700126}
127
Thomas Graf14c0b972006-08-04 03:38:38 -0700128static inline void fib_rule_put(struct fib_rule *rule)
129{
Reshetova, Elena717d1e92017-06-30 13:08:06 +0300130 if (refcount_dec_and_test(&rule->refcnt))
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500131 kfree_rcu(rule, rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700132}
133
David Ahern96c63fa2016-06-08 10:55:39 -0700134#ifdef CONFIG_NET_L3_MASTER_DEV
135static 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
141static 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 McHardy9e762a42006-08-10 23:09:48 -0700148static 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 Prabhubfff4862018-02-28 22:40:16 -0500155static 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
160static 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
167static 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
173static 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
180static 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 Perches8de68792013-09-20 11:23:23 -0700187struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
188 struct net *);
189void fib_rules_unregister(struct fib_rules_ops *);
Thomas Graf14c0b972006-08-04 03:38:38 -0700190
Joe Perches8de68792013-09-20 11:23:23 -0700191int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
192 struct fib_lookup_arg *);
193int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
194 u32 flags);
Ido Schimmel3c710062017-03-16 09:08:12 +0100195bool fib_rule_matchall(const struct fib_rule *rule);
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200196int fib_rules_dump(struct net *net, struct notifier_block *nb, int family);
197unsigned int fib_rules_seq_read(struct net *net, int family);
David Ahern96c63fa2016-06-08 10:55:39 -0700198
David Ahernc21ef3e2017-04-16 09:48:24 -0700199int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
200 struct netlink_ext_ack *extack);
201int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
202 struct netlink_ext_ack *extack);
Thomas Graf14c0b972006-08-04 03:38:38 -0700203#endif