blob: b166ef07e6d4fbed02a234d77432f8657226b0aa [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;
30 /* 1 byte hole, try to use */
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;
Thomas Graf14c0b972006-08-04 03:38:38 -070043 struct rcu_head rcu;
44};
45
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000046struct fib_lookup_arg {
Thomas Graf14c0b972006-08-04 03:38:38 -070047 void *lookup_ptr;
48 void *result;
49 struct fib_rule *rule;
David Ahern96c63fa2016-06-08 10:55:39 -070050 u32 table;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +000051 int flags;
Andy Gospodarek0eeb0752015-06-23 13:45:37 -040052#define FIB_LOOKUP_NOREF 1
53#define FIB_LOOKUP_IGNORE_LINKSTATE 2
Thomas Graf14c0b972006-08-04 03:38:38 -070054};
55
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000056struct fib_rules_ops {
Thomas Graf14c0b972006-08-04 03:38:38 -070057 int family;
58 struct list_head list;
59 int rule_size;
Thomas Grafe1701c62007-03-24 12:46:02 -070060 int addr_size;
Thomas Graf0947c9fe2007-03-26 17:14:15 -070061 int unresolved_rules;
62 int nr_goto_rules;
Ido Schimmel1b2a4442017-08-03 13:28:14 +020063 unsigned int fib_rules_seq;
Thomas Graf14c0b972006-08-04 03:38:38 -070064
65 int (*action)(struct fib_rule *,
66 struct flowi *, int,
67 struct fib_lookup_arg *);
Stefan Tomanek7764a452013-08-01 02:17:15 +020068 bool (*suppress)(struct fib_rule *,
69 struct fib_lookup_arg *);
Thomas Graf14c0b972006-08-04 03:38:38 -070070 int (*match)(struct fib_rule *,
71 struct flowi *, int);
72 int (*configure)(struct fib_rule *,
73 struct sk_buff *,
Thomas Graf14c0b972006-08-04 03:38:38 -070074 struct fib_rule_hdr *,
75 struct nlattr **);
Alexander Duyck0ddcf432015-03-06 13:47:00 -080076 int (*delete)(struct fib_rule *);
Thomas Graf14c0b972006-08-04 03:38:38 -070077 int (*compare)(struct fib_rule *,
78 struct fib_rule_hdr *,
79 struct nlattr **);
80 int (*fill)(struct fib_rule *, struct sk_buff *,
Thomas Graf14c0b972006-08-04 03:38:38 -070081 struct fib_rule_hdr *);
Thomas Graf339bf982006-11-10 14:10:15 -080082 size_t (*nlmsg_payload)(struct fib_rule *);
Thomas Graf14c0b972006-08-04 03:38:38 -070083
Thomas Graf73417f62007-03-27 13:56:52 -070084 /* Called after modifications to the rules set, must flush
85 * the route cache if one exists. */
Denis V. Lunevae299fc2008-07-05 19:01:28 -070086 void (*flush_cache)(struct fib_rules_ops *ops);
Thomas Graf73417f62007-03-27 13:56:52 -070087
Thomas Graf14c0b972006-08-04 03:38:38 -070088 int nlgroup;
Patrick McHardyef7c79e2007-06-05 12:38:30 -070089 const struct nla_policy *policy;
Denis V. Lunev76c72d42007-09-16 15:44:27 -070090 struct list_head rules_list;
Thomas Graf14c0b972006-08-04 03:38:38 -070091 struct module *owner;
Denis V. Lunev03592382008-01-20 16:46:01 -080092 struct net *fro_net;
Eric W. Biedermane9c51582009-12-03 12:22:55 -080093 struct rcu_head rcu;
Thomas Graf14c0b972006-08-04 03:38:38 -070094};
95
Ido Schimmel1b2a4442017-08-03 13:28:14 +020096struct fib_rule_notifier_info {
97 struct fib_notifier_info info; /* must be first */
98 struct fib_rule *rule;
99};
100
Thomas Graf1f6c9552006-11-09 15:22:48 -0800101#define FRA_GENERIC_POLICY \
Patrick McHardy491deb22009-12-03 01:25:54 +0000102 [FRA_IIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
Patrick McHardy1b038a52009-12-03 01:25:56 +0000103 [FRA_OIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
Thomas Graf1f6c9552006-11-09 15:22:48 -0800104 [FRA_PRIORITY] = { .type = NLA_U32 }, \
105 [FRA_FWMARK] = { .type = NLA_U32 }, \
106 [FRA_FWMASK] = { .type = NLA_U32 }, \
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700107 [FRA_TABLE] = { .type = NLA_U32 }, \
Stefan Tomanek73f56982013-08-03 14:14:43 +0200108 [FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200109 [FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
David Ahern96c63fa2016-06-08 10:55:39 -0700110 [FRA_GOTO] = { .type = NLA_U32 }, \
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900111 [FRA_L3MDEV] = { .type = NLA_U8 }, \
112 [FRA_UID_RANGE] = { .len = sizeof(struct fib_rule_uid_range) }
Thomas Graf1f6c9552006-11-09 15:22:48 -0800113
Thomas Graf14c0b972006-08-04 03:38:38 -0700114static inline void fib_rule_get(struct fib_rule *rule)
115{
Reshetova, Elena717d1e92017-06-30 13:08:06 +0300116 refcount_inc(&rule->refcnt);
Thomas Graf14c0b972006-08-04 03:38:38 -0700117}
118
Thomas Graf14c0b972006-08-04 03:38:38 -0700119static inline void fib_rule_put(struct fib_rule *rule)
120{
Reshetova, Elena717d1e92017-06-30 13:08:06 +0300121 if (refcount_dec_and_test(&rule->refcnt))
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500122 kfree_rcu(rule, rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700123}
124
David Ahern96c63fa2016-06-08 10:55:39 -0700125#ifdef CONFIG_NET_L3_MASTER_DEV
126static inline u32 fib_rule_get_table(struct fib_rule *rule,
127 struct fib_lookup_arg *arg)
128{
129 return rule->l3mdev ? arg->table : rule->table;
130}
131#else
132static inline u32 fib_rule_get_table(struct fib_rule *rule,
133 struct fib_lookup_arg *arg)
134{
135 return rule->table;
136}
137#endif
138
Patrick McHardy9e762a42006-08-10 23:09:48 -0700139static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
140{
141 if (nla[FRA_TABLE])
142 return nla_get_u32(nla[FRA_TABLE]);
143 return frh->table;
144}
145
Joe Perches8de68792013-09-20 11:23:23 -0700146struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
147 struct net *);
148void fib_rules_unregister(struct fib_rules_ops *);
Thomas Graf14c0b972006-08-04 03:38:38 -0700149
Joe Perches8de68792013-09-20 11:23:23 -0700150int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
151 struct fib_lookup_arg *);
152int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
153 u32 flags);
Ido Schimmel3c710062017-03-16 09:08:12 +0100154bool fib_rule_matchall(const struct fib_rule *rule);
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200155int fib_rules_dump(struct net *net, struct notifier_block *nb, int family);
156unsigned int fib_rules_seq_read(struct net *net, int family);
David Ahern96c63fa2016-06-08 10:55:39 -0700157
David Ahernc21ef3e2017-04-16 09:48:24 -0700158int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
159 struct netlink_ext_ack *extack);
160int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
161 struct netlink_ext_ack *extack);
Thomas Graf14c0b972006-08-04 03:38:38 -0700162#endif