blob: 3d7f1cefc6f582b328612a7085def93815e288ed [file] [log] [blame]
Thomas Graf14c0b972006-08-04 03:38:38 -07001#ifndef __NET_FIB_RULES_H
2#define __NET_FIB_RULES_H
3
4#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Thomas Graf14c0b972006-08-04 03:38:38 -07006#include <linux/netdevice.h>
7#include <linux/fib_rules.h>
Reshetova, Elena717d1e92017-06-30 13:08:06 +03008#include <linux/refcount.h>
Thomas Graf14c0b972006-08-04 03:38:38 -07009#include <net/flow.h>
Thomas Graf9d9e6a52007-03-25 23:20:05 -070010#include <net/rtnetlink.h>
Ido Schimmel1b2a4442017-08-03 13:28:14 +020011#include <net/fib_notifier.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070012
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +090013struct fib_kuid_range {
14 kuid_t start;
15 kuid_t end;
16};
17
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000018struct fib_rule {
Thomas Graf14c0b972006-08-04 03:38:38 -070019 struct list_head list;
Patrick McHardy491deb22009-12-03 01:25:54 +000020 int iifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +000021 int oifindex;
Thomas Grafb8964ed2006-11-09 15:22:18 -080022 u32 mark;
23 u32 mark_mask;
Thomas Graf14c0b972006-08-04 03:38:38 -070024 u32 flags;
25 u32 table;
26 u8 action;
David Ahern96c63fa2016-06-08 10:55:39 -070027 u8 l3mdev;
28 /* 2 bytes hole, try to use */
Thomas Graf0947c9fe2007-03-26 17:14:15 -070029 u32 target;
Thomas Grafe7030872015-07-21 10:44:01 +020030 __be64 tun_id;
Eric Dumazet7a2b03c2010-10-26 09:24:55 +000031 struct fib_rule __rcu *ctarget;
Eric Dumazetfba36792013-08-03 11:50:35 -070032 struct net *fr_net;
33
Reshetova, Elena717d1e92017-06-30 13:08:06 +030034 refcount_t refcnt;
Eric Dumazetfba36792013-08-03 11:50:35 -070035 u32 pref;
36 int suppress_ifgroup;
37 int suppress_prefixlen;
Patrick McHardy491deb22009-12-03 01:25:54 +000038 char iifname[IFNAMSIZ];
Patrick McHardy1b038a52009-12-03 01:25:56 +000039 char oifname[IFNAMSIZ];
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +090040 struct fib_kuid_range uid_range;
Thomas Graf14c0b972006-08-04 03:38:38 -070041 struct rcu_head rcu;
42};
43
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000044struct fib_lookup_arg {
Thomas Graf14c0b972006-08-04 03:38:38 -070045 void *lookup_ptr;
46 void *result;
47 struct fib_rule *rule;
David Ahern96c63fa2016-06-08 10:55:39 -070048 u32 table;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +000049 int flags;
Andy Gospodarek0eeb0752015-06-23 13:45:37 -040050#define FIB_LOOKUP_NOREF 1
51#define FIB_LOOKUP_IGNORE_LINKSTATE 2
Thomas Graf14c0b972006-08-04 03:38:38 -070052};
53
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000054struct fib_rules_ops {
Thomas Graf14c0b972006-08-04 03:38:38 -070055 int family;
56 struct list_head list;
57 int rule_size;
Thomas Grafe1701c62007-03-24 12:46:02 -070058 int addr_size;
Thomas Graf0947c9fe2007-03-26 17:14:15 -070059 int unresolved_rules;
60 int nr_goto_rules;
Ido Schimmel1b2a4442017-08-03 13:28:14 +020061 unsigned int fib_rules_seq;
Thomas Graf14c0b972006-08-04 03:38:38 -070062
63 int (*action)(struct fib_rule *,
64 struct flowi *, int,
65 struct fib_lookup_arg *);
Stefan Tomanek7764a452013-08-01 02:17:15 +020066 bool (*suppress)(struct fib_rule *,
67 struct fib_lookup_arg *);
Thomas Graf14c0b972006-08-04 03:38:38 -070068 int (*match)(struct fib_rule *,
69 struct flowi *, int);
70 int (*configure)(struct fib_rule *,
71 struct sk_buff *,
Thomas Graf14c0b972006-08-04 03:38:38 -070072 struct fib_rule_hdr *,
73 struct nlattr **);
Alexander Duyck0ddcf432015-03-06 13:47:00 -080074 int (*delete)(struct fib_rule *);
Thomas Graf14c0b972006-08-04 03:38:38 -070075 int (*compare)(struct fib_rule *,
76 struct fib_rule_hdr *,
77 struct nlattr **);
78 int (*fill)(struct fib_rule *, struct sk_buff *,
Thomas Graf14c0b972006-08-04 03:38:38 -070079 struct fib_rule_hdr *);
Thomas Graf339bf982006-11-10 14:10:15 -080080 size_t (*nlmsg_payload)(struct fib_rule *);
Thomas Graf14c0b972006-08-04 03:38:38 -070081
Thomas Graf73417f62007-03-27 13:56:52 -070082 /* Called after modifications to the rules set, must flush
83 * the route cache if one exists. */
Denis V. Lunevae299fc2008-07-05 19:01:28 -070084 void (*flush_cache)(struct fib_rules_ops *ops);
Thomas Graf73417f62007-03-27 13:56:52 -070085
Thomas Graf14c0b972006-08-04 03:38:38 -070086 int nlgroup;
Patrick McHardyef7c79e2007-06-05 12:38:30 -070087 const struct nla_policy *policy;
Denis V. Lunev76c72d42007-09-16 15:44:27 -070088 struct list_head rules_list;
Thomas Graf14c0b972006-08-04 03:38:38 -070089 struct module *owner;
Denis V. Lunev03592382008-01-20 16:46:01 -080090 struct net *fro_net;
Eric W. Biedermane9c51582009-12-03 12:22:55 -080091 struct rcu_head rcu;
Thomas Graf14c0b972006-08-04 03:38:38 -070092};
93
Ido Schimmel1b2a4442017-08-03 13:28:14 +020094struct fib_rule_notifier_info {
95 struct fib_notifier_info info; /* must be first */
96 struct fib_rule *rule;
97};
98
Thomas Graf1f6c9552006-11-09 15:22:48 -080099#define FRA_GENERIC_POLICY \
Patrick McHardy491deb22009-12-03 01:25:54 +0000100 [FRA_IIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
Patrick McHardy1b038a52009-12-03 01:25:56 +0000101 [FRA_OIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
Thomas Graf1f6c9552006-11-09 15:22:48 -0800102 [FRA_PRIORITY] = { .type = NLA_U32 }, \
103 [FRA_FWMARK] = { .type = NLA_U32 }, \
104 [FRA_FWMASK] = { .type = NLA_U32 }, \
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700105 [FRA_TABLE] = { .type = NLA_U32 }, \
Stefan Tomanek73f56982013-08-03 14:14:43 +0200106 [FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200107 [FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
David Ahern96c63fa2016-06-08 10:55:39 -0700108 [FRA_GOTO] = { .type = NLA_U32 }, \
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900109 [FRA_L3MDEV] = { .type = NLA_U8 }, \
110 [FRA_UID_RANGE] = { .len = sizeof(struct fib_rule_uid_range) }
Thomas Graf1f6c9552006-11-09 15:22:48 -0800111
Thomas Graf14c0b972006-08-04 03:38:38 -0700112static inline void fib_rule_get(struct fib_rule *rule)
113{
Reshetova, Elena717d1e92017-06-30 13:08:06 +0300114 refcount_inc(&rule->refcnt);
Thomas Graf14c0b972006-08-04 03:38:38 -0700115}
116
Thomas Graf14c0b972006-08-04 03:38:38 -0700117static inline void fib_rule_put(struct fib_rule *rule)
118{
Reshetova, Elena717d1e92017-06-30 13:08:06 +0300119 if (refcount_dec_and_test(&rule->refcnt))
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500120 kfree_rcu(rule, rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700121}
122
David Ahern96c63fa2016-06-08 10:55:39 -0700123#ifdef CONFIG_NET_L3_MASTER_DEV
124static inline u32 fib_rule_get_table(struct fib_rule *rule,
125 struct fib_lookup_arg *arg)
126{
127 return rule->l3mdev ? arg->table : rule->table;
128}
129#else
130static inline u32 fib_rule_get_table(struct fib_rule *rule,
131 struct fib_lookup_arg *arg)
132{
133 return rule->table;
134}
135#endif
136
Patrick McHardy9e762a42006-08-10 23:09:48 -0700137static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
138{
139 if (nla[FRA_TABLE])
140 return nla_get_u32(nla[FRA_TABLE]);
141 return frh->table;
142}
143
Joe Perches8de68792013-09-20 11:23:23 -0700144struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
145 struct net *);
146void fib_rules_unregister(struct fib_rules_ops *);
Thomas Graf14c0b972006-08-04 03:38:38 -0700147
Joe Perches8de68792013-09-20 11:23:23 -0700148int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
149 struct fib_lookup_arg *);
150int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
151 u32 flags);
Ido Schimmel3c710062017-03-16 09:08:12 +0100152bool fib_rule_matchall(const struct fib_rule *rule);
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200153int fib_rules_dump(struct net *net, struct notifier_block *nb, int family);
154unsigned int fib_rules_seq_read(struct net *net, int family);
David Ahern96c63fa2016-06-08 10:55:39 -0700155
David Ahernc21ef3e2017-04-16 09:48:24 -0700156int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
157 struct netlink_ext_ack *extack);
158int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
159 struct netlink_ext_ack *extack);
Thomas Graf14c0b972006-08-04 03:38:38 -0700160#endif