1a10e763bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
214c0b97dSThomas Graf /*
314c0b97dSThomas Graf * net/core/fib_rules.c Generic Routing Rules
414c0b97dSThomas Graf *
514c0b97dSThomas Graf * Authors: Thomas Graf <tgraf@suug.ch>
614c0b97dSThomas Graf */
714c0b97dSThomas Graf
814c0b97dSThomas Graf #include <linux/types.h>
914c0b97dSThomas Graf #include <linux/kernel.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
1114c0b97dSThomas Graf #include <linux/list.h>
123a9a231dSPaul Gortmaker #include <linux/module.h>
13e9dc8653SEric W. Biederman #include <net/net_namespace.h>
14881d966bSEric W. Biederman #include <net/sock.h>
1514c0b97dSThomas Graf #include <net/fib_rules.h>
16e7030878SThomas Graf #include <net/ip_tunnels.h>
17b9aaec8fSBrian Vazquez #include <linux/indirect_call_wrapper.h>
1814c0b97dSThomas Graf
19923f614cSBrian Vazquez #if defined(CONFIG_IPV6) && defined(CONFIG_IPV6_MULTIPLE_TABLES)
208b66a6fdSBrian Vazquez #ifdef CONFIG_IP_MULTIPLE_TABLES
2141d707b7SBrian Vazquez #define INDIRECT_CALL_MT(f, f2, f1, ...) \
2241d707b7SBrian Vazquez INDIRECT_CALL_INET(f, f2, f1, __VA_ARGS__)
2341d707b7SBrian Vazquez #else
248b66a6fdSBrian Vazquez #define INDIRECT_CALL_MT(f, f2, f1, ...) INDIRECT_CALL_1(f, f2, __VA_ARGS__)
258b66a6fdSBrian Vazquez #endif
2680fbbb16SYueHaibing #elif defined(CONFIG_IP_MULTIPLE_TABLES)
2741d707b7SBrian Vazquez #define INDIRECT_CALL_MT(f, f2, f1, ...) INDIRECT_CALL_1(f, f1, __VA_ARGS__)
288b66a6fdSBrian Vazquez #else
298b66a6fdSBrian Vazquez #define INDIRECT_CALL_MT(f, f2, f1, ...) f(__VA_ARGS__)
3041d707b7SBrian Vazquez #endif
3141d707b7SBrian Vazquez
32622ec2c9SLorenzo Colitti static const struct fib_kuid_range fib_kuid_range_unset = {
33622ec2c9SLorenzo Colitti KUIDT_INIT(0),
34622ec2c9SLorenzo Colitti KUIDT_INIT(~0),
35622ec2c9SLorenzo Colitti };
36622ec2c9SLorenzo Colitti
fib_rule_matchall(const struct fib_rule * rule)373c71006dSIdo Schimmel bool fib_rule_matchall(const struct fib_rule *rule)
383c71006dSIdo Schimmel {
393c71006dSIdo Schimmel if (rule->iifindex || rule->oifindex || rule->mark || rule->tun_id ||
403c71006dSIdo Schimmel rule->flags)
413c71006dSIdo Schimmel return false;
423c71006dSIdo Schimmel if (rule->suppress_ifgroup != -1 || rule->suppress_prefixlen != -1)
433c71006dSIdo Schimmel return false;
443c71006dSIdo Schimmel if (!uid_eq(rule->uid_range.start, fib_kuid_range_unset.start) ||
453c71006dSIdo Schimmel !uid_eq(rule->uid_range.end, fib_kuid_range_unset.end))
463c71006dSIdo Schimmel return false;
47bfff4862SRoopa Prabhu if (fib_rule_port_range_set(&rule->sport_range))
48bfff4862SRoopa Prabhu return false;
49bfff4862SRoopa Prabhu if (fib_rule_port_range_set(&rule->dport_range))
50bfff4862SRoopa Prabhu return false;
513c71006dSIdo Schimmel return true;
523c71006dSIdo Schimmel }
533c71006dSIdo Schimmel EXPORT_SYMBOL_GPL(fib_rule_matchall);
543c71006dSIdo Schimmel
fib_default_rule_add(struct fib_rules_ops * ops,u32 pref,u32 table,u32 flags)552994c638SDenis V. Lunev int fib_default_rule_add(struct fib_rules_ops *ops,
562994c638SDenis V. Lunev u32 pref, u32 table, u32 flags)
572994c638SDenis V. Lunev {
582994c638SDenis V. Lunev struct fib_rule *r;
592994c638SDenis V. Lunev
606126891cSVasily Averin r = kzalloc(ops->rule_size, GFP_KERNEL_ACCOUNT);
612994c638SDenis V. Lunev if (r == NULL)
622994c638SDenis V. Lunev return -ENOMEM;
632994c638SDenis V. Lunev
64717d1e99SReshetova, Elena refcount_set(&r->refcnt, 1);
652994c638SDenis V. Lunev r->action = FR_ACT_TO_TBL;
662994c638SDenis V. Lunev r->pref = pref;
672994c638SDenis V. Lunev r->table = table;
682994c638SDenis V. Lunev r->flags = flags;
69cac56209SDonald Sharp r->proto = RTPROT_KERNEL;
70efd7ef1cSEric W. Biederman r->fr_net = ops->fro_net;
71622ec2c9SLorenzo Colitti r->uid_range = fib_kuid_range_unset;
722994c638SDenis V. Lunev
7373f5698eSStefan Tomanek r->suppress_prefixlen = -1;
7473f5698eSStefan Tomanek r->suppress_ifgroup = -1;
7573f5698eSStefan Tomanek
762994c638SDenis V. Lunev /* The lock is not required here, the list in unreacheable
772994c638SDenis V. Lunev * at the moment this function is called */
782994c638SDenis V. Lunev list_add_tail(&r->list, &ops->rules_list);
792994c638SDenis V. Lunev return 0;
802994c638SDenis V. Lunev }
812994c638SDenis V. Lunev EXPORT_SYMBOL(fib_default_rule_add);
822994c638SDenis V. Lunev
fib_default_rule_pref(struct fib_rules_ops * ops)83f53de1e9SPhil Sutter static u32 fib_default_rule_pref(struct fib_rules_ops *ops)
84d8a566beSPatrick McHardy {
85d8a566beSPatrick McHardy struct list_head *pos;
86d8a566beSPatrick McHardy struct fib_rule *rule;
87d8a566beSPatrick McHardy
88d8a566beSPatrick McHardy if (!list_empty(&ops->rules_list)) {
89d8a566beSPatrick McHardy pos = ops->rules_list.next;
90d8a566beSPatrick McHardy if (pos->next != &ops->rules_list) {
91d8a566beSPatrick McHardy rule = list_entry(pos->next, struct fib_rule, list);
92d8a566beSPatrick McHardy if (rule->pref)
93d8a566beSPatrick McHardy return rule->pref - 1;
94d8a566beSPatrick McHardy }
95d8a566beSPatrick McHardy }
96d8a566beSPatrick McHardy
97d8a566beSPatrick McHardy return 0;
98d8a566beSPatrick McHardy }
99d8a566beSPatrick McHardy
1009e3a5487SDenis V. Lunev static void notify_rule_change(int event, struct fib_rule *rule,
101c17084d2SThomas Graf struct fib_rules_ops *ops, struct nlmsghdr *nlh,
102c17084d2SThomas Graf u32 pid);
10314c0b97dSThomas Graf
lookup_rules_ops(struct net * net,int family)1045fd30ee7SDenis V. Lunev static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
10514c0b97dSThomas Graf {
10614c0b97dSThomas Graf struct fib_rules_ops *ops;
10714c0b97dSThomas Graf
10814c0b97dSThomas Graf rcu_read_lock();
1095fd30ee7SDenis V. Lunev list_for_each_entry_rcu(ops, &net->rules_ops, list) {
11014c0b97dSThomas Graf if (ops->family == family) {
11114c0b97dSThomas Graf if (!try_module_get(ops->owner))
11214c0b97dSThomas Graf ops = NULL;
11314c0b97dSThomas Graf rcu_read_unlock();
11414c0b97dSThomas Graf return ops;
11514c0b97dSThomas Graf }
11614c0b97dSThomas Graf }
11714c0b97dSThomas Graf rcu_read_unlock();
11814c0b97dSThomas Graf
11914c0b97dSThomas Graf return NULL;
12014c0b97dSThomas Graf }
12114c0b97dSThomas Graf
rules_ops_put(struct fib_rules_ops * ops)12214c0b97dSThomas Graf static void rules_ops_put(struct fib_rules_ops *ops)
12314c0b97dSThomas Graf {
12414c0b97dSThomas Graf if (ops)
12514c0b97dSThomas Graf module_put(ops->owner);
12614c0b97dSThomas Graf }
12714c0b97dSThomas Graf
flush_route_cache(struct fib_rules_ops * ops)12873417f61SThomas Graf static void flush_route_cache(struct fib_rules_ops *ops)
12973417f61SThomas Graf {
13073417f61SThomas Graf if (ops->flush_cache)
131ae299fc0SDenis V. Lunev ops->flush_cache(ops);
13273417f61SThomas Graf }
13373417f61SThomas Graf
__fib_rules_register(struct fib_rules_ops * ops)134e9c5158aSEric W. Biederman static int __fib_rules_register(struct fib_rules_ops *ops)
13514c0b97dSThomas Graf {
13614c0b97dSThomas Graf int err = -EEXIST;
13714c0b97dSThomas Graf struct fib_rules_ops *o;
1389e3a5487SDenis V. Lunev struct net *net;
1399e3a5487SDenis V. Lunev
1409e3a5487SDenis V. Lunev net = ops->fro_net;
14114c0b97dSThomas Graf
14214c0b97dSThomas Graf if (ops->rule_size < sizeof(struct fib_rule))
14314c0b97dSThomas Graf return -EINVAL;
14414c0b97dSThomas Graf
14514c0b97dSThomas Graf if (ops->match == NULL || ops->configure == NULL ||
14614c0b97dSThomas Graf ops->compare == NULL || ops->fill == NULL ||
14714c0b97dSThomas Graf ops->action == NULL)
14814c0b97dSThomas Graf return -EINVAL;
14914c0b97dSThomas Graf
1505fd30ee7SDenis V. Lunev spin_lock(&net->rules_mod_lock);
1515fd30ee7SDenis V. Lunev list_for_each_entry(o, &net->rules_ops, list)
15214c0b97dSThomas Graf if (ops->family == o->family)
15314c0b97dSThomas Graf goto errout;
15414c0b97dSThomas Graf
1555fd30ee7SDenis V. Lunev list_add_tail_rcu(&ops->list, &net->rules_ops);
15614c0b97dSThomas Graf err = 0;
15714c0b97dSThomas Graf errout:
1585fd30ee7SDenis V. Lunev spin_unlock(&net->rules_mod_lock);
15914c0b97dSThomas Graf
16014c0b97dSThomas Graf return err;
16114c0b97dSThomas Graf }
16214c0b97dSThomas Graf
163e9c5158aSEric W. Biederman struct fib_rules_ops *
fib_rules_register(const struct fib_rules_ops * tmpl,struct net * net)1643d0c9c4eSPatrick McHardy fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
165e9c5158aSEric W. Biederman {
166e9c5158aSEric W. Biederman struct fib_rules_ops *ops;
167e9c5158aSEric W. Biederman int err;
168e9c5158aSEric W. Biederman
169e9c5158aSEric W. Biederman ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
170e9c5158aSEric W. Biederman if (ops == NULL)
171e9c5158aSEric W. Biederman return ERR_PTR(-ENOMEM);
172e9c5158aSEric W. Biederman
173e9c5158aSEric W. Biederman INIT_LIST_HEAD(&ops->rules_list);
174e9c5158aSEric W. Biederman ops->fro_net = net;
175e9c5158aSEric W. Biederman
176e9c5158aSEric W. Biederman err = __fib_rules_register(ops);
177e9c5158aSEric W. Biederman if (err) {
178e9c5158aSEric W. Biederman kfree(ops);
179e9c5158aSEric W. Biederman ops = ERR_PTR(err);
180e9c5158aSEric W. Biederman }
181e9c5158aSEric W. Biederman
182e9c5158aSEric W. Biederman return ops;
183e9c5158aSEric W. Biederman }
18414c0b97dSThomas Graf EXPORT_SYMBOL_GPL(fib_rules_register);
18514c0b97dSThomas Graf
fib_rules_cleanup_ops(struct fib_rules_ops * ops)1861df9916eSstephen hemminger static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
18714c0b97dSThomas Graf {
18814c0b97dSThomas Graf struct fib_rule *rule, *tmp;
18914c0b97dSThomas Graf
19076c72d4fSDenis V. Lunev list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
19114c0b97dSThomas Graf list_del_rcu(&rule->list);
1927a9bc9b8SDavid S. Miller if (ops->delete)
1937a9bc9b8SDavid S. Miller ops->delete(rule);
19414c0b97dSThomas Graf fib_rule_put(rule);
19514c0b97dSThomas Graf }
19614c0b97dSThomas Graf }
19714c0b97dSThomas Graf
fib_rules_unregister(struct fib_rules_ops * ops)1989e3a5487SDenis V. Lunev void fib_rules_unregister(struct fib_rules_ops *ops)
19914c0b97dSThomas Graf {
2009e3a5487SDenis V. Lunev struct net *net = ops->fro_net;
20114c0b97dSThomas Graf
2025fd30ee7SDenis V. Lunev spin_lock(&net->rules_mod_lock);
20372132c1bSDenis V. Lunev list_del_rcu(&ops->list);
2045fd30ee7SDenis V. Lunev spin_unlock(&net->rules_mod_lock);
20514c0b97dSThomas Graf
206419df12fSWANG Cong fib_rules_cleanup_ops(ops);
207efd7ef1cSEric W. Biederman kfree_rcu(ops, rcu);
20814c0b97dSThomas Graf }
20914c0b97dSThomas Graf EXPORT_SYMBOL_GPL(fib_rules_unregister);
21014c0b97dSThomas Graf
uid_range_set(struct fib_kuid_range * range)211622ec2c9SLorenzo Colitti static int uid_range_set(struct fib_kuid_range *range)
212622ec2c9SLorenzo Colitti {
213622ec2c9SLorenzo Colitti return uid_valid(range->start) && uid_valid(range->end);
214622ec2c9SLorenzo Colitti }
215622ec2c9SLorenzo Colitti
nla_get_kuid_range(struct nlattr ** tb)216622ec2c9SLorenzo Colitti static struct fib_kuid_range nla_get_kuid_range(struct nlattr **tb)
217622ec2c9SLorenzo Colitti {
218622ec2c9SLorenzo Colitti struct fib_rule_uid_range *in;
219622ec2c9SLorenzo Colitti struct fib_kuid_range out;
220622ec2c9SLorenzo Colitti
221622ec2c9SLorenzo Colitti in = (struct fib_rule_uid_range *)nla_data(tb[FRA_UID_RANGE]);
222622ec2c9SLorenzo Colitti
223622ec2c9SLorenzo Colitti out.start = make_kuid(current_user_ns(), in->start);
224622ec2c9SLorenzo Colitti out.end = make_kuid(current_user_ns(), in->end);
225622ec2c9SLorenzo Colitti
226622ec2c9SLorenzo Colitti return out;
227622ec2c9SLorenzo Colitti }
228622ec2c9SLorenzo Colitti
nla_put_uid_range(struct sk_buff * skb,struct fib_kuid_range * range)229622ec2c9SLorenzo Colitti static int nla_put_uid_range(struct sk_buff *skb, struct fib_kuid_range *range)
230622ec2c9SLorenzo Colitti {
231622ec2c9SLorenzo Colitti struct fib_rule_uid_range out = {
232622ec2c9SLorenzo Colitti from_kuid_munged(current_user_ns(), range->start),
233622ec2c9SLorenzo Colitti from_kuid_munged(current_user_ns(), range->end)
234622ec2c9SLorenzo Colitti };
235622ec2c9SLorenzo Colitti
236622ec2c9SLorenzo Colitti return nla_put(skb, FRA_UID_RANGE, sizeof(out), &out);
237622ec2c9SLorenzo Colitti }
238622ec2c9SLorenzo Colitti
nla_get_port_range(struct nlattr * pattr,struct fib_rule_port_range * port_range)239bfff4862SRoopa Prabhu static int nla_get_port_range(struct nlattr *pattr,
240bfff4862SRoopa Prabhu struct fib_rule_port_range *port_range)
241bfff4862SRoopa Prabhu {
242bfff4862SRoopa Prabhu const struct fib_rule_port_range *pr = nla_data(pattr);
243bfff4862SRoopa Prabhu
244bfff4862SRoopa Prabhu if (!fib_rule_port_range_valid(pr))
245bfff4862SRoopa Prabhu return -EINVAL;
246bfff4862SRoopa Prabhu
247bfff4862SRoopa Prabhu port_range->start = pr->start;
248bfff4862SRoopa Prabhu port_range->end = pr->end;
249bfff4862SRoopa Prabhu
250bfff4862SRoopa Prabhu return 0;
251bfff4862SRoopa Prabhu }
252bfff4862SRoopa Prabhu
nla_put_port_range(struct sk_buff * skb,int attrtype,struct fib_rule_port_range * range)253bfff4862SRoopa Prabhu static int nla_put_port_range(struct sk_buff *skb, int attrtype,
254bfff4862SRoopa Prabhu struct fib_rule_port_range *range)
255bfff4862SRoopa Prabhu {
256bfff4862SRoopa Prabhu return nla_put(skb, attrtype, sizeof(*range), range);
257bfff4862SRoopa Prabhu }
258bfff4862SRoopa Prabhu
fib_rule_match(struct fib_rule * rule,struct fib_rules_ops * ops,struct flowi * fl,int flags,struct fib_lookup_arg * arg)2593dfbcc41SThomas Graf static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
26096c63fa7SDavid Ahern struct flowi *fl, int flags,
26196c63fa7SDavid Ahern struct fib_lookup_arg *arg)
2623dfbcc41SThomas Graf {
2633dfbcc41SThomas Graf int ret = 0;
2643dfbcc41SThomas Graf
2651d28f42cSDavid S. Miller if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
2663dfbcc41SThomas Graf goto out;
2673dfbcc41SThomas Graf
2681d28f42cSDavid S. Miller if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
2691b038a5eSPatrick McHardy goto out;
2701b038a5eSPatrick McHardy
2711d28f42cSDavid S. Miller if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
2723dfbcc41SThomas Graf goto out;
2733dfbcc41SThomas Graf
274e7030878SThomas Graf if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
275e7030878SThomas Graf goto out;
276e7030878SThomas Graf
27796c63fa7SDavid Ahern if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
27896c63fa7SDavid Ahern goto out;
27996c63fa7SDavid Ahern
280622ec2c9SLorenzo Colitti if (uid_lt(fl->flowi_uid, rule->uid_range.start) ||
281622ec2c9SLorenzo Colitti uid_gt(fl->flowi_uid, rule->uid_range.end))
282622ec2c9SLorenzo Colitti goto out;
283622ec2c9SLorenzo Colitti
28441d707b7SBrian Vazquez ret = INDIRECT_CALL_MT(ops->match,
285b9aaec8fSBrian Vazquez fib6_rule_match,
286b9aaec8fSBrian Vazquez fib4_rule_match,
287b9aaec8fSBrian Vazquez rule, fl, flags);
2883dfbcc41SThomas Graf out:
2893dfbcc41SThomas Graf return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
2903dfbcc41SThomas Graf }
2913dfbcc41SThomas Graf
fib_rules_lookup(struct fib_rules_ops * ops,struct flowi * fl,int flags,struct fib_lookup_arg * arg)29214c0b97dSThomas Graf int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
29314c0b97dSThomas Graf int flags, struct fib_lookup_arg *arg)
29414c0b97dSThomas Graf {
29514c0b97dSThomas Graf struct fib_rule *rule;
29614c0b97dSThomas Graf int err;
29714c0b97dSThomas Graf
29814c0b97dSThomas Graf rcu_read_lock();
29914c0b97dSThomas Graf
30076c72d4fSDenis V. Lunev list_for_each_entry_rcu(rule, &ops->rules_list, list) {
3010947c9feSThomas Graf jumped:
30296c63fa7SDavid Ahern if (!fib_rule_match(rule, ops, fl, flags, arg))
30314c0b97dSThomas Graf continue;
30414c0b97dSThomas Graf
3050947c9feSThomas Graf if (rule->action == FR_ACT_GOTO) {
3060947c9feSThomas Graf struct fib_rule *target;
3070947c9feSThomas Graf
3080947c9feSThomas Graf target = rcu_dereference(rule->ctarget);
3090947c9feSThomas Graf if (target == NULL) {
3100947c9feSThomas Graf continue;
3110947c9feSThomas Graf } else {
3120947c9feSThomas Graf rule = target;
3130947c9feSThomas Graf goto jumped;
3140947c9feSThomas Graf }
315fa0b2d1dSThomas Graf } else if (rule->action == FR_ACT_NOP)
316fa0b2d1dSThomas Graf continue;
317fa0b2d1dSThomas Graf else
31841d707b7SBrian Vazquez err = INDIRECT_CALL_MT(ops->action,
319b9aaec8fSBrian Vazquez fib6_rule_action,
320b9aaec8fSBrian Vazquez fib4_rule_action,
321b9aaec8fSBrian Vazquez rule, fl, flags, arg);
3220947c9feSThomas Graf
32341d707b7SBrian Vazquez if (!err && ops->suppress && INDIRECT_CALL_MT(ops->suppress,
324b9aaec8fSBrian Vazquez fib6_rule_suppress,
325b9aaec8fSBrian Vazquez fib4_rule_suppress,
326cdef4852Smsizanoen1 rule, flags, arg))
3277764a45aSStefan Tomanek continue;
3287764a45aSStefan Tomanek
32914c0b97dSThomas Graf if (err != -EAGAIN) {
330ebc0ffaeSEric Dumazet if ((arg->flags & FIB_LOOKUP_NOREF) ||
331717d1e99SReshetova, Elena likely(refcount_inc_not_zero(&rule->refcnt))) {
33214c0b97dSThomas Graf arg->rule = rule;
33314c0b97dSThomas Graf goto out;
33414c0b97dSThomas Graf }
3357fa7cb71SEric Dumazet break;
3367fa7cb71SEric Dumazet }
33714c0b97dSThomas Graf }
33814c0b97dSThomas Graf
33983886b6bSSteven Whitehouse err = -ESRCH;
34014c0b97dSThomas Graf out:
34114c0b97dSThomas Graf rcu_read_unlock();
34214c0b97dSThomas Graf
34314c0b97dSThomas Graf return err;
34414c0b97dSThomas Graf }
34514c0b97dSThomas Graf EXPORT_SYMBOL_GPL(fib_rules_lookup);
34614c0b97dSThomas Graf
call_fib_rule_notifier(struct notifier_block * nb,enum fib_event_type event_type,struct fib_rule * rule,int family,struct netlink_ext_ack * extack)3477c550dafSJiri Pirko static int call_fib_rule_notifier(struct notifier_block *nb,
3481b2a4440SIdo Schimmel enum fib_event_type event_type,
349b7a59557SJiri Pirko struct fib_rule *rule, int family,
350b7a59557SJiri Pirko struct netlink_ext_ack *extack)
3511b2a4440SIdo Schimmel {
3521b2a4440SIdo Schimmel struct fib_rule_notifier_info info = {
3531b2a4440SIdo Schimmel .info.family = family,
354b7a59557SJiri Pirko .info.extack = extack,
3551b2a4440SIdo Schimmel .rule = rule,
3561b2a4440SIdo Schimmel };
3571b2a4440SIdo Schimmel
3587c550dafSJiri Pirko return call_fib_notifier(nb, event_type, &info.info);
3591b2a4440SIdo Schimmel }
3601b2a4440SIdo Schimmel
call_fib_rule_notifiers(struct net * net,enum fib_event_type event_type,struct fib_rule * rule,struct fib_rules_ops * ops,struct netlink_ext_ack * extack)3611b2a4440SIdo Schimmel static int call_fib_rule_notifiers(struct net *net,
3621b2a4440SIdo Schimmel enum fib_event_type event_type,
3631b2a4440SIdo Schimmel struct fib_rule *rule,
3646c31e5a9SDavid Ahern struct fib_rules_ops *ops,
3656c31e5a9SDavid Ahern struct netlink_ext_ack *extack)
3661b2a4440SIdo Schimmel {
3671b2a4440SIdo Schimmel struct fib_rule_notifier_info info = {
3681b2a4440SIdo Schimmel .info.family = ops->family,
3696c31e5a9SDavid Ahern .info.extack = extack,
3701b2a4440SIdo Schimmel .rule = rule,
3711b2a4440SIdo Schimmel };
3721b2a4440SIdo Schimmel
3731b2a4440SIdo Schimmel ops->fib_rules_seq++;
3741b2a4440SIdo Schimmel return call_fib_notifiers(net, event_type, &info.info);
3751b2a4440SIdo Schimmel }
3761b2a4440SIdo Schimmel
3771b2a4440SIdo Schimmel /* Called with rcu_read_lock() */
fib_rules_dump(struct net * net,struct notifier_block * nb,int family,struct netlink_ext_ack * extack)378b7a59557SJiri Pirko int fib_rules_dump(struct net *net, struct notifier_block *nb, int family,
379b7a59557SJiri Pirko struct netlink_ext_ack *extack)
3801b2a4440SIdo Schimmel {
3811b2a4440SIdo Schimmel struct fib_rules_ops *ops;
3821b2a4440SIdo Schimmel struct fib_rule *rule;
38355c894f7SJiri Pirko int err = 0;
3841b2a4440SIdo Schimmel
3851b2a4440SIdo Schimmel ops = lookup_rules_ops(net, family);
3861b2a4440SIdo Schimmel if (!ops)
3871b2a4440SIdo Schimmel return -EAFNOSUPPORT;
38855c894f7SJiri Pirko list_for_each_entry_rcu(rule, &ops->rules_list, list) {
38955c894f7SJiri Pirko err = call_fib_rule_notifier(nb, FIB_EVENT_RULE_ADD,
390b7a59557SJiri Pirko rule, family, extack);
39155c894f7SJiri Pirko if (err)
39255c894f7SJiri Pirko break;
39355c894f7SJiri Pirko }
3941b2a4440SIdo Schimmel rules_ops_put(ops);
3951b2a4440SIdo Schimmel
39655c894f7SJiri Pirko return err;
3971b2a4440SIdo Schimmel }
3981b2a4440SIdo Schimmel EXPORT_SYMBOL_GPL(fib_rules_dump);
3991b2a4440SIdo Schimmel
fib_rules_seq_read(struct net * net,int family)4001b2a4440SIdo Schimmel unsigned int fib_rules_seq_read(struct net *net, int family)
4011b2a4440SIdo Schimmel {
4021b2a4440SIdo Schimmel unsigned int fib_rules_seq;
4031b2a4440SIdo Schimmel struct fib_rules_ops *ops;
4041b2a4440SIdo Schimmel
4051b2a4440SIdo Schimmel ASSERT_RTNL();
4061b2a4440SIdo Schimmel
4071b2a4440SIdo Schimmel ops = lookup_rules_ops(net, family);
4081b2a4440SIdo Schimmel if (!ops)
4091b2a4440SIdo Schimmel return 0;
4101b2a4440SIdo Schimmel fib_rules_seq = ops->fib_rules_seq;
4111b2a4440SIdo Schimmel rules_ops_put(ops);
4121b2a4440SIdo Schimmel
4131b2a4440SIdo Schimmel return fib_rules_seq;
4141b2a4440SIdo Schimmel }
4151b2a4440SIdo Schimmel EXPORT_SYMBOL_GPL(fib_rules_seq_read);
4161b2a4440SIdo Schimmel
rule_find(struct fib_rules_ops * ops,struct fib_rule_hdr * frh,struct nlattr ** tb,struct fib_rule * rule,bool user_priority)417f9d4b0c1SRoopa Prabhu static struct fib_rule *rule_find(struct fib_rules_ops *ops,
418f9d4b0c1SRoopa Prabhu struct fib_rule_hdr *frh,
419f9d4b0c1SRoopa Prabhu struct nlattr **tb,
420f9d4b0c1SRoopa Prabhu struct fib_rule *rule,
421f9d4b0c1SRoopa Prabhu bool user_priority)
422153380ecSMateusz Bajorski {
423153380ecSMateusz Bajorski struct fib_rule *r;
424153380ecSMateusz Bajorski
425153380ecSMateusz Bajorski list_for_each_entry(r, &ops->rules_list, list) {
426f9d4b0c1SRoopa Prabhu if (rule->action && r->action != rule->action)
427153380ecSMateusz Bajorski continue;
428153380ecSMateusz Bajorski
429f9d4b0c1SRoopa Prabhu if (rule->table && r->table != rule->table)
430153380ecSMateusz Bajorski continue;
431153380ecSMateusz Bajorski
432f9d4b0c1SRoopa Prabhu if (user_priority && r->pref != rule->pref)
433153380ecSMateusz Bajorski continue;
434153380ecSMateusz Bajorski
435f9d4b0c1SRoopa Prabhu if (rule->iifname[0] &&
436f9d4b0c1SRoopa Prabhu memcmp(r->iifname, rule->iifname, IFNAMSIZ))
437153380ecSMateusz Bajorski continue;
438153380ecSMateusz Bajorski
439f9d4b0c1SRoopa Prabhu if (rule->oifname[0] &&
440f9d4b0c1SRoopa Prabhu memcmp(r->oifname, rule->oifname, IFNAMSIZ))
441153380ecSMateusz Bajorski continue;
442153380ecSMateusz Bajorski
443f9d4b0c1SRoopa Prabhu if (rule->mark && r->mark != rule->mark)
444153380ecSMateusz Bajorski continue;
445153380ecSMateusz Bajorski
4467c8f4e6dSJason A. Donenfeld if (rule->suppress_ifgroup != -1 &&
4477c8f4e6dSJason A. Donenfeld r->suppress_ifgroup != rule->suppress_ifgroup)
4487c8f4e6dSJason A. Donenfeld continue;
4497c8f4e6dSJason A. Donenfeld
4507c8f4e6dSJason A. Donenfeld if (rule->suppress_prefixlen != -1 &&
4517c8f4e6dSJason A. Donenfeld r->suppress_prefixlen != rule->suppress_prefixlen)
4527c8f4e6dSJason A. Donenfeld continue;
4537c8f4e6dSJason A. Donenfeld
454f9d4b0c1SRoopa Prabhu if (rule->mark_mask && r->mark_mask != rule->mark_mask)
455153380ecSMateusz Bajorski continue;
456153380ecSMateusz Bajorski
457f9d4b0c1SRoopa Prabhu if (rule->tun_id && r->tun_id != rule->tun_id)
458153380ecSMateusz Bajorski continue;
459153380ecSMateusz Bajorski
460153380ecSMateusz Bajorski if (r->fr_net != rule->fr_net)
461153380ecSMateusz Bajorski continue;
462153380ecSMateusz Bajorski
463f9d4b0c1SRoopa Prabhu if (rule->l3mdev && r->l3mdev != rule->l3mdev)
464153380ecSMateusz Bajorski continue;
465153380ecSMateusz Bajorski
466f9d4b0c1SRoopa Prabhu if (uid_range_set(&rule->uid_range) &&
467f9d4b0c1SRoopa Prabhu (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
468f9d4b0c1SRoopa Prabhu !uid_eq(r->uid_range.end, rule->uid_range.end)))
46935b80733SLorenzo Colitti continue;
47035b80733SLorenzo Colitti
471f9d4b0c1SRoopa Prabhu if (rule->ip_proto && r->ip_proto != rule->ip_proto)
472bfff4862SRoopa Prabhu continue;
473bfff4862SRoopa Prabhu
47435e8c7baSRoopa Prabhu if (rule->proto && r->proto != rule->proto)
47535e8c7baSRoopa Prabhu continue;
47635e8c7baSRoopa Prabhu
477f9d4b0c1SRoopa Prabhu if (fib_rule_port_range_set(&rule->sport_range) &&
478f9d4b0c1SRoopa Prabhu !fib_rule_port_range_compare(&r->sport_range,
479bfff4862SRoopa Prabhu &rule->sport_range))
480bfff4862SRoopa Prabhu continue;
481bfff4862SRoopa Prabhu
482f9d4b0c1SRoopa Prabhu if (fib_rule_port_range_set(&rule->dport_range) &&
483f9d4b0c1SRoopa Prabhu !fib_rule_port_range_compare(&r->dport_range,
484bfff4862SRoopa Prabhu &rule->dport_range))
485bfff4862SRoopa Prabhu continue;
486bfff4862SRoopa Prabhu
487153380ecSMateusz Bajorski if (!ops->compare(r, frh, tb))
488153380ecSMateusz Bajorski continue;
489f9d4b0c1SRoopa Prabhu return r;
490153380ecSMateusz Bajorski }
491153380ecSMateusz Bajorski
492f9d4b0c1SRoopa Prabhu return NULL;
493f9d4b0c1SRoopa Prabhu }
494f9d4b0c1SRoopa Prabhu
495c77bbc64SDavid Ahern #ifdef CONFIG_NET_L3_MASTER_DEV
fib_nl2rule_l3mdev(struct nlattr * nla,struct fib_rule * nlrule,struct netlink_ext_ack * extack)496c77bbc64SDavid Ahern static int fib_nl2rule_l3mdev(struct nlattr *nla, struct fib_rule *nlrule,
497c77bbc64SDavid Ahern struct netlink_ext_ack *extack)
498c77bbc64SDavid Ahern {
499c77bbc64SDavid Ahern nlrule->l3mdev = nla_get_u8(nla);
500c77bbc64SDavid Ahern if (nlrule->l3mdev != 1) {
501c77bbc64SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid l3mdev attribute");
502c77bbc64SDavid Ahern return -1;
503c77bbc64SDavid Ahern }
504c77bbc64SDavid Ahern
505c77bbc64SDavid Ahern return 0;
506c77bbc64SDavid Ahern }
507c77bbc64SDavid Ahern #else
fib_nl2rule_l3mdev(struct nlattr * nla,struct fib_rule * nlrule,struct netlink_ext_ack * extack)508c77bbc64SDavid Ahern static int fib_nl2rule_l3mdev(struct nlattr *nla, struct fib_rule *nlrule,
509c77bbc64SDavid Ahern struct netlink_ext_ack *extack)
510c77bbc64SDavid Ahern {
511c77bbc64SDavid Ahern NL_SET_ERR_MSG(extack, "l3mdev support is not enabled in kernel");
512c77bbc64SDavid Ahern return -1;
513c77bbc64SDavid Ahern }
514c77bbc64SDavid Ahern #endif
515c77bbc64SDavid Ahern
fib_nl2rule(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack,struct fib_rules_ops * ops,struct nlattr * tb[],struct fib_rule ** rule,bool * user_priority)516f9d4b0c1SRoopa Prabhu static int fib_nl2rule(struct sk_buff *skb, struct nlmsghdr *nlh,
517f9d4b0c1SRoopa Prabhu struct netlink_ext_ack *extack,
518f9d4b0c1SRoopa Prabhu struct fib_rules_ops *ops,
519f9d4b0c1SRoopa Prabhu struct nlattr *tb[],
520f9d4b0c1SRoopa Prabhu struct fib_rule **rule,
521f9d4b0c1SRoopa Prabhu bool *user_priority)
52214c0b97dSThomas Graf {
5233b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk);
52414c0b97dSThomas Graf struct fib_rule_hdr *frh = nlmsg_data(nlh);
525f9d4b0c1SRoopa Prabhu struct fib_rule *nlrule = NULL;
526f9d4b0c1SRoopa Prabhu int err = -EINVAL;
52714c0b97dSThomas Graf
528f9d4b0c1SRoopa Prabhu if (frh->src_len)
529f9d4b0c1SRoopa Prabhu if (!tb[FRA_SRC] ||
530f9d4b0c1SRoopa Prabhu frh->src_len > (ops->addr_size * 8) ||
531b16fb418SRoopa Prabhu nla_len(tb[FRA_SRC]) != ops->addr_size) {
532b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid source address");
53314c0b97dSThomas Graf goto errout;
534b16fb418SRoopa Prabhu }
53514c0b97dSThomas Graf
536f9d4b0c1SRoopa Prabhu if (frh->dst_len)
537f9d4b0c1SRoopa Prabhu if (!tb[FRA_DST] ||
538f9d4b0c1SRoopa Prabhu frh->dst_len > (ops->addr_size * 8) ||
539b16fb418SRoopa Prabhu nla_len(tb[FRA_DST]) != ops->addr_size) {
540b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid dst address");
54114c0b97dSThomas Graf goto errout;
542b16fb418SRoopa Prabhu }
54314c0b97dSThomas Graf
5446126891cSVasily Averin nlrule = kzalloc(ops->rule_size, GFP_KERNEL_ACCOUNT);
545f9d4b0c1SRoopa Prabhu if (!nlrule) {
54614c0b97dSThomas Graf err = -ENOMEM;
54714c0b97dSThomas Graf goto errout;
54814c0b97dSThomas Graf }
549f9d4b0c1SRoopa Prabhu refcount_set(&nlrule->refcnt, 1);
550f9d4b0c1SRoopa Prabhu nlrule->fr_net = net;
55114c0b97dSThomas Graf
552f9d4b0c1SRoopa Prabhu if (tb[FRA_PRIORITY]) {
553f9d4b0c1SRoopa Prabhu nlrule->pref = nla_get_u32(tb[FRA_PRIORITY]);
554f9d4b0c1SRoopa Prabhu *user_priority = true;
555f9d4b0c1SRoopa Prabhu } else {
556f9d4b0c1SRoopa Prabhu nlrule->pref = fib_default_rule_pref(ops);
557f9d4b0c1SRoopa Prabhu }
55814c0b97dSThomas Graf
559f9d4b0c1SRoopa Prabhu nlrule->proto = tb[FRA_PROTOCOL] ?
5601b71af60SDonald Sharp nla_get_u8(tb[FRA_PROTOCOL]) : RTPROT_UNSPEC;
5611b71af60SDonald Sharp
562491deb24SPatrick McHardy if (tb[FRA_IIFNAME]) {
56314c0b97dSThomas Graf struct net_device *dev;
56414c0b97dSThomas Graf
565f9d4b0c1SRoopa Prabhu nlrule->iifindex = -1;
566872f6903SFrancis Laniel nla_strscpy(nlrule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
567f9d4b0c1SRoopa Prabhu dev = __dev_get_by_name(net, nlrule->iifname);
56814c0b97dSThomas Graf if (dev)
569f9d4b0c1SRoopa Prabhu nlrule->iifindex = dev->ifindex;
57014c0b97dSThomas Graf }
57114c0b97dSThomas Graf
5721b038a5eSPatrick McHardy if (tb[FRA_OIFNAME]) {
5731b038a5eSPatrick McHardy struct net_device *dev;
5741b038a5eSPatrick McHardy
575f9d4b0c1SRoopa Prabhu nlrule->oifindex = -1;
576872f6903SFrancis Laniel nla_strscpy(nlrule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
577f9d4b0c1SRoopa Prabhu dev = __dev_get_by_name(net, nlrule->oifname);
5781b038a5eSPatrick McHardy if (dev)
579f9d4b0c1SRoopa Prabhu nlrule->oifindex = dev->ifindex;
5801b038a5eSPatrick McHardy }
5811b038a5eSPatrick McHardy
582b8964ed9SThomas Graf if (tb[FRA_FWMARK]) {
583f9d4b0c1SRoopa Prabhu nlrule->mark = nla_get_u32(tb[FRA_FWMARK]);
584f9d4b0c1SRoopa Prabhu if (nlrule->mark)
585b8964ed9SThomas Graf /* compatibility: if the mark value is non-zero all bits
586b8964ed9SThomas Graf * are compared unless a mask is explicitly specified.
587b8964ed9SThomas Graf */
588f9d4b0c1SRoopa Prabhu nlrule->mark_mask = 0xFFFFFFFF;
589b8964ed9SThomas Graf }
590b8964ed9SThomas Graf
591b8964ed9SThomas Graf if (tb[FRA_FWMASK])
592f9d4b0c1SRoopa Prabhu nlrule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
593b8964ed9SThomas Graf
594e7030878SThomas Graf if (tb[FRA_TUN_ID])
595f9d4b0c1SRoopa Prabhu nlrule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
596e7030878SThomas Graf
597adeb45cbSWei Yongjun err = -EINVAL;
598c77bbc64SDavid Ahern if (tb[FRA_L3MDEV] &&
599c77bbc64SDavid Ahern fib_nl2rule_l3mdev(tb[FRA_L3MDEV], nlrule, extack) < 0)
60096c63fa7SDavid Ahern goto errout_free;
60196c63fa7SDavid Ahern
602f9d4b0c1SRoopa Prabhu nlrule->action = frh->action;
603f9d4b0c1SRoopa Prabhu nlrule->flags = frh->flags;
604f9d4b0c1SRoopa Prabhu nlrule->table = frh_get_table(frh, tb);
60573f5698eSStefan Tomanek if (tb[FRA_SUPPRESS_PREFIXLEN])
606f9d4b0c1SRoopa Prabhu nlrule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
60773f5698eSStefan Tomanek else
608f9d4b0c1SRoopa Prabhu nlrule->suppress_prefixlen = -1;
60914c0b97dSThomas Graf
6106ef94cfaSStefan Tomanek if (tb[FRA_SUPPRESS_IFGROUP])
611f9d4b0c1SRoopa Prabhu nlrule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
61273f5698eSStefan Tomanek else
613f9d4b0c1SRoopa Prabhu nlrule->suppress_ifgroup = -1;
6146ef94cfaSStefan Tomanek
6150947c9feSThomas Graf if (tb[FRA_GOTO]) {
616b16fb418SRoopa Prabhu if (nlrule->action != FR_ACT_GOTO) {
617b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unexpected goto");
618f9d4b0c1SRoopa Prabhu goto errout_free;
6190947c9feSThomas Graf }
6200947c9feSThomas Graf
621b16fb418SRoopa Prabhu nlrule->target = nla_get_u32(tb[FRA_GOTO]);
622b16fb418SRoopa Prabhu /* Backward jumps are prohibited to avoid endless loops */
623b16fb418SRoopa Prabhu if (nlrule->target <= nlrule->pref) {
624b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "Backward goto not supported");
62596c63fa7SDavid Ahern goto errout_free;
626b16fb418SRoopa Prabhu }
627b16fb418SRoopa Prabhu } else if (nlrule->action == FR_ACT_GOTO) {
628b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "Missing goto target for action goto");
629b16fb418SRoopa Prabhu goto errout_free;
630b16fb418SRoopa Prabhu }
631b16fb418SRoopa Prabhu
632b16fb418SRoopa Prabhu if (nlrule->l3mdev && nlrule->table) {
633b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "l3mdev and table are mutually exclusive");
634b16fb418SRoopa Prabhu goto errout_free;
635b16fb418SRoopa Prabhu }
63696c63fa7SDavid Ahern
637622ec2c9SLorenzo Colitti if (tb[FRA_UID_RANGE]) {
638622ec2c9SLorenzo Colitti if (current_user_ns() != net->user_ns) {
639622ec2c9SLorenzo Colitti err = -EPERM;
640b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "No permission to set uid");
641622ec2c9SLorenzo Colitti goto errout_free;
642622ec2c9SLorenzo Colitti }
643622ec2c9SLorenzo Colitti
644f9d4b0c1SRoopa Prabhu nlrule->uid_range = nla_get_kuid_range(tb);
645622ec2c9SLorenzo Colitti
646f9d4b0c1SRoopa Prabhu if (!uid_range_set(&nlrule->uid_range) ||
647b16fb418SRoopa Prabhu !uid_lte(nlrule->uid_range.start, nlrule->uid_range.end)) {
648b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid uid range");
649622ec2c9SLorenzo Colitti goto errout_free;
650b16fb418SRoopa Prabhu }
651622ec2c9SLorenzo Colitti } else {
652f9d4b0c1SRoopa Prabhu nlrule->uid_range = fib_kuid_range_unset;
653622ec2c9SLorenzo Colitti }
654622ec2c9SLorenzo Colitti
655bfff4862SRoopa Prabhu if (tb[FRA_IP_PROTO])
656f9d4b0c1SRoopa Prabhu nlrule->ip_proto = nla_get_u8(tb[FRA_IP_PROTO]);
657bfff4862SRoopa Prabhu
658bfff4862SRoopa Prabhu if (tb[FRA_SPORT_RANGE]) {
659bfff4862SRoopa Prabhu err = nla_get_port_range(tb[FRA_SPORT_RANGE],
660f9d4b0c1SRoopa Prabhu &nlrule->sport_range);
661b16fb418SRoopa Prabhu if (err) {
662b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid sport range");
663bfff4862SRoopa Prabhu goto errout_free;
664bfff4862SRoopa Prabhu }
665b16fb418SRoopa Prabhu }
666bfff4862SRoopa Prabhu
667bfff4862SRoopa Prabhu if (tb[FRA_DPORT_RANGE]) {
668bfff4862SRoopa Prabhu err = nla_get_port_range(tb[FRA_DPORT_RANGE],
669f9d4b0c1SRoopa Prabhu &nlrule->dport_range);
670b16fb418SRoopa Prabhu if (err) {
671b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid dport range");
672bfff4862SRoopa Prabhu goto errout_free;
673bfff4862SRoopa Prabhu }
674b16fb418SRoopa Prabhu }
675bfff4862SRoopa Prabhu
676f9d4b0c1SRoopa Prabhu *rule = nlrule;
677f9d4b0c1SRoopa Prabhu
678f9d4b0c1SRoopa Prabhu return 0;
679f9d4b0c1SRoopa Prabhu
680f9d4b0c1SRoopa Prabhu errout_free:
681f9d4b0c1SRoopa Prabhu kfree(nlrule);
682f9d4b0c1SRoopa Prabhu errout:
683f9d4b0c1SRoopa Prabhu return err;
684f9d4b0c1SRoopa Prabhu }
685f9d4b0c1SRoopa Prabhu
rule_exists(struct fib_rules_ops * ops,struct fib_rule_hdr * frh,struct nlattr ** tb,struct fib_rule * rule)68635e8c7baSRoopa Prabhu static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh,
68735e8c7baSRoopa Prabhu struct nlattr **tb, struct fib_rule *rule)
68835e8c7baSRoopa Prabhu {
68935e8c7baSRoopa Prabhu struct fib_rule *r;
69035e8c7baSRoopa Prabhu
69135e8c7baSRoopa Prabhu list_for_each_entry(r, &ops->rules_list, list) {
69235e8c7baSRoopa Prabhu if (r->action != rule->action)
69335e8c7baSRoopa Prabhu continue;
69435e8c7baSRoopa Prabhu
69535e8c7baSRoopa Prabhu if (r->table != rule->table)
69635e8c7baSRoopa Prabhu continue;
69735e8c7baSRoopa Prabhu
69835e8c7baSRoopa Prabhu if (r->pref != rule->pref)
69935e8c7baSRoopa Prabhu continue;
70035e8c7baSRoopa Prabhu
70135e8c7baSRoopa Prabhu if (memcmp(r->iifname, rule->iifname, IFNAMSIZ))
70235e8c7baSRoopa Prabhu continue;
70335e8c7baSRoopa Prabhu
70435e8c7baSRoopa Prabhu if (memcmp(r->oifname, rule->oifname, IFNAMSIZ))
70535e8c7baSRoopa Prabhu continue;
70635e8c7baSRoopa Prabhu
70735e8c7baSRoopa Prabhu if (r->mark != rule->mark)
70835e8c7baSRoopa Prabhu continue;
70935e8c7baSRoopa Prabhu
71035e8c7baSRoopa Prabhu if (r->suppress_ifgroup != rule->suppress_ifgroup)
71135e8c7baSRoopa Prabhu continue;
71235e8c7baSRoopa Prabhu
71335e8c7baSRoopa Prabhu if (r->suppress_prefixlen != rule->suppress_prefixlen)
71435e8c7baSRoopa Prabhu continue;
71535e8c7baSRoopa Prabhu
71635e8c7baSRoopa Prabhu if (r->mark_mask != rule->mark_mask)
71735e8c7baSRoopa Prabhu continue;
71835e8c7baSRoopa Prabhu
71935e8c7baSRoopa Prabhu if (r->tun_id != rule->tun_id)
72035e8c7baSRoopa Prabhu continue;
72135e8c7baSRoopa Prabhu
72235e8c7baSRoopa Prabhu if (r->fr_net != rule->fr_net)
72335e8c7baSRoopa Prabhu continue;
72435e8c7baSRoopa Prabhu
72535e8c7baSRoopa Prabhu if (r->l3mdev != rule->l3mdev)
72635e8c7baSRoopa Prabhu continue;
72735e8c7baSRoopa Prabhu
72835e8c7baSRoopa Prabhu if (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
72935e8c7baSRoopa Prabhu !uid_eq(r->uid_range.end, rule->uid_range.end))
73035e8c7baSRoopa Prabhu continue;
73135e8c7baSRoopa Prabhu
73235e8c7baSRoopa Prabhu if (r->ip_proto != rule->ip_proto)
73335e8c7baSRoopa Prabhu continue;
73435e8c7baSRoopa Prabhu
73535e8c7baSRoopa Prabhu if (r->proto != rule->proto)
73635e8c7baSRoopa Prabhu continue;
73735e8c7baSRoopa Prabhu
73835e8c7baSRoopa Prabhu if (!fib_rule_port_range_compare(&r->sport_range,
73935e8c7baSRoopa Prabhu &rule->sport_range))
74035e8c7baSRoopa Prabhu continue;
74135e8c7baSRoopa Prabhu
74235e8c7baSRoopa Prabhu if (!fib_rule_port_range_compare(&r->dport_range,
74335e8c7baSRoopa Prabhu &rule->dport_range))
74435e8c7baSRoopa Prabhu continue;
74535e8c7baSRoopa Prabhu
74635e8c7baSRoopa Prabhu if (!ops->compare(r, frh, tb))
74735e8c7baSRoopa Prabhu continue;
74835e8c7baSRoopa Prabhu return 1;
74935e8c7baSRoopa Prabhu }
75035e8c7baSRoopa Prabhu return 0;
75135e8c7baSRoopa Prabhu }
75235e8c7baSRoopa Prabhu
75392e1bceeSFlorian Westphal static const struct nla_policy fib_rule_policy[FRA_MAX + 1] = {
754*66495f30SFlorian Westphal [FRA_UNSPEC] = { .strict_start_type = FRA_DPORT_RANGE + 1 },
755*66495f30SFlorian Westphal [FRA_IIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
756*66495f30SFlorian Westphal [FRA_OIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
757*66495f30SFlorian Westphal [FRA_PRIORITY] = { .type = NLA_U32 },
758*66495f30SFlorian Westphal [FRA_FWMARK] = { .type = NLA_U32 },
75992e1bceeSFlorian Westphal [FRA_FLOW] = { .type = NLA_U32 },
760*66495f30SFlorian Westphal [FRA_TUN_ID] = { .type = NLA_U64 },
761*66495f30SFlorian Westphal [FRA_FWMASK] = { .type = NLA_U32 },
762*66495f30SFlorian Westphal [FRA_TABLE] = { .type = NLA_U32 },
763*66495f30SFlorian Westphal [FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 },
764*66495f30SFlorian Westphal [FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 },
765*66495f30SFlorian Westphal [FRA_GOTO] = { .type = NLA_U32 },
766*66495f30SFlorian Westphal [FRA_L3MDEV] = { .type = NLA_U8 },
767*66495f30SFlorian Westphal [FRA_UID_RANGE] = { .len = sizeof(struct fib_rule_uid_range) },
768*66495f30SFlorian Westphal [FRA_PROTOCOL] = { .type = NLA_U8 },
769*66495f30SFlorian Westphal [FRA_IP_PROTO] = { .type = NLA_U8 },
770*66495f30SFlorian Westphal [FRA_SPORT_RANGE] = { .len = sizeof(struct fib_rule_port_range) },
771*66495f30SFlorian Westphal [FRA_DPORT_RANGE] = { .len = sizeof(struct fib_rule_port_range) }
77292e1bceeSFlorian Westphal };
77392e1bceeSFlorian Westphal
fib_nl_newrule(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)774f9d4b0c1SRoopa Prabhu int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
775f9d4b0c1SRoopa Prabhu struct netlink_ext_ack *extack)
776f9d4b0c1SRoopa Prabhu {
777f9d4b0c1SRoopa Prabhu struct net *net = sock_net(skb->sk);
778f9d4b0c1SRoopa Prabhu struct fib_rule_hdr *frh = nlmsg_data(nlh);
779f9d4b0c1SRoopa Prabhu struct fib_rules_ops *ops = NULL;
780f9d4b0c1SRoopa Prabhu struct fib_rule *rule = NULL, *r, *last = NULL;
781f9d4b0c1SRoopa Prabhu struct nlattr *tb[FRA_MAX + 1];
782f9d4b0c1SRoopa Prabhu int err = -EINVAL, unresolved = 0;
783f9d4b0c1SRoopa Prabhu bool user_priority = false;
784f9d4b0c1SRoopa Prabhu
785b16fb418SRoopa Prabhu if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
786b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid msg length");
787f9d4b0c1SRoopa Prabhu goto errout;
788b16fb418SRoopa Prabhu }
789f9d4b0c1SRoopa Prabhu
790f9d4b0c1SRoopa Prabhu ops = lookup_rules_ops(net, frh->family);
791f9d4b0c1SRoopa Prabhu if (!ops) {
792f9d4b0c1SRoopa Prabhu err = -EAFNOSUPPORT;
793b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "Rule family not supported");
794f9d4b0c1SRoopa Prabhu goto errout;
795f9d4b0c1SRoopa Prabhu }
796f9d4b0c1SRoopa Prabhu
7978cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*frh), tb, FRA_MAX,
79892e1bceeSFlorian Westphal fib_rule_policy, extack);
799b16fb418SRoopa Prabhu if (err < 0) {
800b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "Error parsing msg");
801f9d4b0c1SRoopa Prabhu goto errout;
802b16fb418SRoopa Prabhu }
803f9d4b0c1SRoopa Prabhu
804f9d4b0c1SRoopa Prabhu err = fib_nl2rule(skb, nlh, extack, ops, tb, &rule, &user_priority);
805f9d4b0c1SRoopa Prabhu if (err)
806f9d4b0c1SRoopa Prabhu goto errout;
807f9d4b0c1SRoopa Prabhu
8084970b42dSHangbin Liu if ((nlh->nlmsg_flags & NLM_F_EXCL) &&
8094970b42dSHangbin Liu rule_exists(ops, frh, tb, rule)) {
810153380ecSMateusz Bajorski err = -EEXIST;
811153380ecSMateusz Bajorski goto errout_free;
812153380ecSMateusz Bajorski }
813153380ecSMateusz Bajorski
814b16fb418SRoopa Prabhu err = ops->configure(rule, skb, frh, tb, extack);
81514c0b97dSThomas Graf if (err < 0)
81614c0b97dSThomas Graf goto errout_free;
81714c0b97dSThomas Graf
8189776d325SDavid Ahern err = call_fib_rule_notifiers(net, FIB_EVENT_RULE_ADD, rule, ops,
8199776d325SDavid Ahern extack);
8209776d325SDavid Ahern if (err < 0)
8219776d325SDavid Ahern goto errout_free;
8229776d325SDavid Ahern
82376c72d4fSDenis V. Lunev list_for_each_entry(r, &ops->rules_list, list) {
824f9d4b0c1SRoopa Prabhu if (r->pref == rule->target) {
825f9d4b0c1SRoopa Prabhu RCU_INIT_POINTER(rule->ctarget, r);
826f9d4b0c1SRoopa Prabhu break;
827f9d4b0c1SRoopa Prabhu }
828f9d4b0c1SRoopa Prabhu }
829f9d4b0c1SRoopa Prabhu
830f9d4b0c1SRoopa Prabhu if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
831f9d4b0c1SRoopa Prabhu unresolved = 1;
832f9d4b0c1SRoopa Prabhu
833f9d4b0c1SRoopa Prabhu list_for_each_entry(r, &ops->rules_list, list) {
83414c0b97dSThomas Graf if (r->pref > rule->pref)
83514c0b97dSThomas Graf break;
83614c0b97dSThomas Graf last = r;
83714c0b97dSThomas Graf }
83814c0b97dSThomas Graf
839ebb9fed2SEric Dumazet if (last)
840ebb9fed2SEric Dumazet list_add_rcu(&rule->list, &last->list);
841ebb9fed2SEric Dumazet else
842ebb9fed2SEric Dumazet list_add_rcu(&rule->list, &ops->rules_list);
843ebb9fed2SEric Dumazet
8440947c9feSThomas Graf if (ops->unresolved_rules) {
8450947c9feSThomas Graf /*
8460947c9feSThomas Graf * There are unresolved goto rules in the list, check if
8470947c9feSThomas Graf * any of them are pointing to this new rule.
8480947c9feSThomas Graf */
84976c72d4fSDenis V. Lunev list_for_each_entry(r, &ops->rules_list, list) {
8500947c9feSThomas Graf if (r->action == FR_ACT_GOTO &&
851561dac2dSGao feng r->target == rule->pref &&
852561dac2dSGao feng rtnl_dereference(r->ctarget) == NULL) {
8530947c9feSThomas Graf rcu_assign_pointer(r->ctarget, rule);
8540947c9feSThomas Graf if (--ops->unresolved_rules == 0)
8550947c9feSThomas Graf break;
8560947c9feSThomas Graf }
8570947c9feSThomas Graf }
8580947c9feSThomas Graf }
8590947c9feSThomas Graf
8600947c9feSThomas Graf if (rule->action == FR_ACT_GOTO)
8610947c9feSThomas Graf ops->nr_goto_rules++;
8620947c9feSThomas Graf
8630947c9feSThomas Graf if (unresolved)
8640947c9feSThomas Graf ops->unresolved_rules++;
8650947c9feSThomas Graf
866e7030878SThomas Graf if (rule->tun_id)
867e7030878SThomas Graf ip_tunnel_need_metadata();
868e7030878SThomas Graf
86915e47304SEric W. Biederman notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
87073417f61SThomas Graf flush_route_cache(ops);
87114c0b97dSThomas Graf rules_ops_put(ops);
87214c0b97dSThomas Graf return 0;
87314c0b97dSThomas Graf
87414c0b97dSThomas Graf errout_free:
87514c0b97dSThomas Graf kfree(rule);
87614c0b97dSThomas Graf errout:
87714c0b97dSThomas Graf rules_ops_put(ops);
87814c0b97dSThomas Graf return err;
87914c0b97dSThomas Graf }
88096c63fa7SDavid Ahern EXPORT_SYMBOL_GPL(fib_nl_newrule);
88114c0b97dSThomas Graf
fib_nl_delrule(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)882c21ef3e3SDavid Ahern int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
883c21ef3e3SDavid Ahern struct netlink_ext_ack *extack)
88414c0b97dSThomas Graf {
8853b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk);
88614c0b97dSThomas Graf struct fib_rule_hdr *frh = nlmsg_data(nlh);
88714c0b97dSThomas Graf struct fib_rules_ops *ops = NULL;
888f9d4b0c1SRoopa Prabhu struct fib_rule *rule = NULL, *r, *nlrule = NULL;
88914c0b97dSThomas Graf struct nlattr *tb[FRA_MAX+1];
89014c0b97dSThomas Graf int err = -EINVAL;
891f9d4b0c1SRoopa Prabhu bool user_priority = false;
89214c0b97dSThomas Graf
893b16fb418SRoopa Prabhu if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
894b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid msg length");
89514c0b97dSThomas Graf goto errout;
896b16fb418SRoopa Prabhu }
89714c0b97dSThomas Graf
8985fd30ee7SDenis V. Lunev ops = lookup_rules_ops(net, frh->family);
89914c0b97dSThomas Graf if (ops == NULL) {
9002fe195cfSPatrick McHardy err = -EAFNOSUPPORT;
901b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "Rule family not supported");
90214c0b97dSThomas Graf goto errout;
90314c0b97dSThomas Graf }
90414c0b97dSThomas Graf
9058cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*frh), tb, FRA_MAX,
90692e1bceeSFlorian Westphal fib_rule_policy, extack);
907b16fb418SRoopa Prabhu if (err < 0) {
908b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "Error parsing msg");
90914c0b97dSThomas Graf goto errout;
910b16fb418SRoopa Prabhu }
91114c0b97dSThomas Graf
912f9d4b0c1SRoopa Prabhu err = fib_nl2rule(skb, nlh, extack, ops, tb, &nlrule, &user_priority);
913bfff4862SRoopa Prabhu if (err)
914bfff4862SRoopa Prabhu goto errout;
915bfff4862SRoopa Prabhu
916f9d4b0c1SRoopa Prabhu rule = rule_find(ops, frh, tb, nlrule, user_priority);
917f9d4b0c1SRoopa Prabhu if (!rule) {
918f9d4b0c1SRoopa Prabhu err = -ENOENT;
919bfff4862SRoopa Prabhu goto errout;
920bfff4862SRoopa Prabhu }
921bfff4862SRoopa Prabhu
92214c0b97dSThomas Graf if (rule->flags & FIB_RULE_PERMANENT) {
92314c0b97dSThomas Graf err = -EPERM;
92414c0b97dSThomas Graf goto errout;
92514c0b97dSThomas Graf }
92614c0b97dSThomas Graf
9270ddcf43dSAlexander Duyck if (ops->delete) {
9280ddcf43dSAlexander Duyck err = ops->delete(rule);
9290ddcf43dSAlexander Duyck if (err)
9300ddcf43dSAlexander Duyck goto errout;
9310ddcf43dSAlexander Duyck }
9320ddcf43dSAlexander Duyck
933e7030878SThomas Graf if (rule->tun_id)
934e7030878SThomas Graf ip_tunnel_unneed_metadata();
935e7030878SThomas Graf
93614c0b97dSThomas Graf list_del_rcu(&rule->list);
9370947c9feSThomas Graf
938afaef734SYan, Zheng if (rule->action == FR_ACT_GOTO) {
9390947c9feSThomas Graf ops->nr_goto_rules--;
940afaef734SYan, Zheng if (rtnl_dereference(rule->ctarget) == NULL)
941afaef734SYan, Zheng ops->unresolved_rules--;
942afaef734SYan, Zheng }
9430947c9feSThomas Graf
9440947c9feSThomas Graf /*
9450947c9feSThomas Graf * Check if this rule is a target to any of them. If so,
946bdaf32c3SSerhey Popovych * adjust to the next one with the same preference or
9470947c9feSThomas Graf * disable them. As this operation is eventually very
948bdaf32c3SSerhey Popovych * expensive, it is only performed if goto rules, except
949bdaf32c3SSerhey Popovych * current if it is goto rule, have actually been added.
9500947c9feSThomas Graf */
9510947c9feSThomas Graf if (ops->nr_goto_rules > 0) {
952bdaf32c3SSerhey Popovych struct fib_rule *n;
953bdaf32c3SSerhey Popovych
954bdaf32c3SSerhey Popovych n = list_next_entry(rule, list);
955bdaf32c3SSerhey Popovych if (&n->list == &ops->rules_list || n->pref != rule->pref)
956bdaf32c3SSerhey Popovych n = NULL;
957bdaf32c3SSerhey Popovych list_for_each_entry(r, &ops->rules_list, list) {
958bdaf32c3SSerhey Popovych if (rtnl_dereference(r->ctarget) != rule)
959bdaf32c3SSerhey Popovych continue;
960bdaf32c3SSerhey Popovych rcu_assign_pointer(r->ctarget, n);
961bdaf32c3SSerhey Popovych if (!n)
9620947c9feSThomas Graf ops->unresolved_rules++;
9630947c9feSThomas Graf }
9640947c9feSThomas Graf }
9650947c9feSThomas Graf
9666c31e5a9SDavid Ahern call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule, ops,
9676c31e5a9SDavid Ahern NULL);
9689e3a5487SDenis V. Lunev notify_rule_change(RTM_DELRULE, rule, ops, nlh,
96915e47304SEric W. Biederman NETLINK_CB(skb).portid);
97014c0b97dSThomas Graf fib_rule_put(rule);
97173417f61SThomas Graf flush_route_cache(ops);
97214c0b97dSThomas Graf rules_ops_put(ops);
973f9d4b0c1SRoopa Prabhu kfree(nlrule);
97414c0b97dSThomas Graf return 0;
97514c0b97dSThomas Graf
97614c0b97dSThomas Graf errout:
977f9d4b0c1SRoopa Prabhu kfree(nlrule);
97814c0b97dSThomas Graf rules_ops_put(ops);
97914c0b97dSThomas Graf return err;
98014c0b97dSThomas Graf }
98196c63fa7SDavid Ahern EXPORT_SYMBOL_GPL(fib_nl_delrule);
98214c0b97dSThomas Graf
fib_rule_nlmsg_size(struct fib_rules_ops * ops,struct fib_rule * rule)983339bf98fSThomas Graf static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
984339bf98fSThomas Graf struct fib_rule *rule)
985339bf98fSThomas Graf {
986339bf98fSThomas Graf size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
987491deb24SPatrick McHardy + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
9881b038a5eSPatrick McHardy + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
989339bf98fSThomas Graf + nla_total_size(4) /* FRA_PRIORITY */
990339bf98fSThomas Graf + nla_total_size(4) /* FRA_TABLE */
99173f5698eSStefan Tomanek + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
9926ef94cfaSStefan Tomanek + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
993339bf98fSThomas Graf + nla_total_size(4) /* FRA_FWMARK */
994e7030878SThomas Graf + nla_total_size(4) /* FRA_FWMASK */
995622ec2c9SLorenzo Colitti + nla_total_size_64bit(8) /* FRA_TUN_ID */
9961b71af60SDonald Sharp + nla_total_size(sizeof(struct fib_kuid_range))
997bfff4862SRoopa Prabhu + nla_total_size(1) /* FRA_PROTOCOL */
998bfff4862SRoopa Prabhu + nla_total_size(1) /* FRA_IP_PROTO */
999bfff4862SRoopa Prabhu + nla_total_size(sizeof(struct fib_rule_port_range)) /* FRA_SPORT_RANGE */
1000bfff4862SRoopa Prabhu + nla_total_size(sizeof(struct fib_rule_port_range)); /* FRA_DPORT_RANGE */
1001339bf98fSThomas Graf
1002339bf98fSThomas Graf if (ops->nlmsg_payload)
1003339bf98fSThomas Graf payload += ops->nlmsg_payload(rule);
1004339bf98fSThomas Graf
1005339bf98fSThomas Graf return payload;
1006339bf98fSThomas Graf }
1007339bf98fSThomas Graf
fib_nl_fill_rule(struct sk_buff * skb,struct fib_rule * rule,u32 pid,u32 seq,int type,int flags,struct fib_rules_ops * ops)100814c0b97dSThomas Graf static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
100914c0b97dSThomas Graf u32 pid, u32 seq, int type, int flags,
101014c0b97dSThomas Graf struct fib_rules_ops *ops)
101114c0b97dSThomas Graf {
101214c0b97dSThomas Graf struct nlmsghdr *nlh;
101314c0b97dSThomas Graf struct fib_rule_hdr *frh;
101414c0b97dSThomas Graf
101514c0b97dSThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
101614c0b97dSThomas Graf if (nlh == NULL)
101726932566SPatrick McHardy return -EMSGSIZE;
101814c0b97dSThomas Graf
101914c0b97dSThomas Graf frh = nlmsg_data(nlh);
102028bb1726SPatrick McHardy frh->family = ops->family;
1021540e585aSJethro Beekman frh->table = rule->table < 256 ? rule->table : RT_TABLE_COMPAT;
10220e3cea7bSDavid S. Miller if (nla_put_u32(skb, FRA_TABLE, rule->table))
10230e3cea7bSDavid S. Miller goto nla_put_failure;
102473f5698eSStefan Tomanek if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
10257764a45aSStefan Tomanek goto nla_put_failure;
102614c0b97dSThomas Graf frh->res1 = 0;
10271b71af60SDonald Sharp frh->res2 = 0;
102814c0b97dSThomas Graf frh->action = rule->action;
102914c0b97dSThomas Graf frh->flags = rule->flags;
10301b71af60SDonald Sharp
10311b71af60SDonald Sharp if (nla_put_u8(skb, FRA_PROTOCOL, rule->proto))
10321b71af60SDonald Sharp goto nla_put_failure;
103314c0b97dSThomas Graf
10347a2b03c5SEric Dumazet if (rule->action == FR_ACT_GOTO &&
103533d480ceSEric Dumazet rcu_access_pointer(rule->ctarget) == NULL)
10360947c9feSThomas Graf frh->flags |= FIB_RULE_UNRESOLVED;
10370947c9feSThomas Graf
1038491deb24SPatrick McHardy if (rule->iifname[0]) {
10390e3cea7bSDavid S. Miller if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
10400e3cea7bSDavid S. Miller goto nla_put_failure;
1041491deb24SPatrick McHardy if (rule->iifindex == -1)
1042491deb24SPatrick McHardy frh->flags |= FIB_RULE_IIF_DETACHED;
10432b443683SThomas Graf }
10442b443683SThomas Graf
10451b038a5eSPatrick McHardy if (rule->oifname[0]) {
10460e3cea7bSDavid S. Miller if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
10470e3cea7bSDavid S. Miller goto nla_put_failure;
10481b038a5eSPatrick McHardy if (rule->oifindex == -1)
10491b038a5eSPatrick McHardy frh->flags |= FIB_RULE_OIF_DETACHED;
10501b038a5eSPatrick McHardy }
10511b038a5eSPatrick McHardy
10520e3cea7bSDavid S. Miller if ((rule->pref &&
10530e3cea7bSDavid S. Miller nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
10540e3cea7bSDavid S. Miller (rule->mark &&
10550e3cea7bSDavid S. Miller nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
10560e3cea7bSDavid S. Miller ((rule->mark_mask || rule->mark) &&
10570e3cea7bSDavid S. Miller nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
10580e3cea7bSDavid S. Miller (rule->target &&
1059e7030878SThomas Graf nla_put_u32(skb, FRA_GOTO, rule->target)) ||
1060e7030878SThomas Graf (rule->tun_id &&
106196c63fa7SDavid Ahern nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
106296c63fa7SDavid Ahern (rule->l3mdev &&
1063622ec2c9SLorenzo Colitti nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)) ||
1064622ec2c9SLorenzo Colitti (uid_range_set(&rule->uid_range) &&
1065bfff4862SRoopa Prabhu nla_put_uid_range(skb, &rule->uid_range)) ||
1066bfff4862SRoopa Prabhu (fib_rule_port_range_set(&rule->sport_range) &&
1067bfff4862SRoopa Prabhu nla_put_port_range(skb, FRA_SPORT_RANGE, &rule->sport_range)) ||
1068bfff4862SRoopa Prabhu (fib_rule_port_range_set(&rule->dport_range) &&
1069bfff4862SRoopa Prabhu nla_put_port_range(skb, FRA_DPORT_RANGE, &rule->dport_range)) ||
1070bfff4862SRoopa Prabhu (rule->ip_proto && nla_put_u8(skb, FRA_IP_PROTO, rule->ip_proto)))
10710e3cea7bSDavid S. Miller goto nla_put_failure;
10726ef94cfaSStefan Tomanek
10736ef94cfaSStefan Tomanek if (rule->suppress_ifgroup != -1) {
10746ef94cfaSStefan Tomanek if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
10756ef94cfaSStefan Tomanek goto nla_put_failure;
10766ef94cfaSStefan Tomanek }
10776ef94cfaSStefan Tomanek
107804af8cf6SRami Rosen if (ops->fill(rule, skb, frh) < 0)
107914c0b97dSThomas Graf goto nla_put_failure;
108014c0b97dSThomas Graf
1081053c095aSJohannes Berg nlmsg_end(skb, nlh);
1082053c095aSJohannes Berg return 0;
108314c0b97dSThomas Graf
108414c0b97dSThomas Graf nla_put_failure:
108526932566SPatrick McHardy nlmsg_cancel(skb, nlh);
108626932566SPatrick McHardy return -EMSGSIZE;
108714c0b97dSThomas Graf }
108814c0b97dSThomas Graf
dump_rules(struct sk_buff * skb,struct netlink_callback * cb,struct fib_rules_ops * ops)1089c454673dSThomas Graf static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
1090c454673dSThomas Graf struct fib_rules_ops *ops)
109114c0b97dSThomas Graf {
109214c0b97dSThomas Graf int idx = 0;
109314c0b97dSThomas Graf struct fib_rule *rule;
109441fc0143SWilson Kok int err = 0;
109514c0b97dSThomas Graf
1096e67f88ddSEric Dumazet rcu_read_lock();
1097e67f88ddSEric Dumazet list_for_each_entry_rcu(rule, &ops->rules_list, list) {
1098c454673dSThomas Graf if (idx < cb->args[1])
109914c0b97dSThomas Graf goto skip;
110014c0b97dSThomas Graf
110141fc0143SWilson Kok err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
110214c0b97dSThomas Graf cb->nlh->nlmsg_seq, RTM_NEWRULE,
110341fc0143SWilson Kok NLM_F_MULTI, ops);
110441fc0143SWilson Kok if (err)
110514c0b97dSThomas Graf break;
110614c0b97dSThomas Graf skip:
110714c0b97dSThomas Graf idx++;
110814c0b97dSThomas Graf }
11092907c35fSEric Dumazet rcu_read_unlock();
1110c454673dSThomas Graf cb->args[1] = idx;
111114c0b97dSThomas Graf rules_ops_put(ops);
111214c0b97dSThomas Graf
111341fc0143SWilson Kok return err;
111414c0b97dSThomas Graf }
111514c0b97dSThomas Graf
fib_valid_dumprule_req(const struct nlmsghdr * nlh,struct netlink_ext_ack * extack)11164a73e5e5SDavid Ahern static int fib_valid_dumprule_req(const struct nlmsghdr *nlh,
11174a73e5e5SDavid Ahern struct netlink_ext_ack *extack)
11184a73e5e5SDavid Ahern {
11194a73e5e5SDavid Ahern struct fib_rule_hdr *frh;
11204a73e5e5SDavid Ahern
11214a73e5e5SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
11224a73e5e5SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for fib rule dump request");
11234a73e5e5SDavid Ahern return -EINVAL;
11244a73e5e5SDavid Ahern }
11254a73e5e5SDavid Ahern
11264a73e5e5SDavid Ahern frh = nlmsg_data(nlh);
11274a73e5e5SDavid Ahern if (frh->dst_len || frh->src_len || frh->tos || frh->table ||
11284a73e5e5SDavid Ahern frh->res1 || frh->res2 || frh->action || frh->flags) {
11294a73e5e5SDavid Ahern NL_SET_ERR_MSG(extack,
11304a73e5e5SDavid Ahern "Invalid values in header for fib rule dump request");
11314a73e5e5SDavid Ahern return -EINVAL;
11324a73e5e5SDavid Ahern }
11334a73e5e5SDavid Ahern
11344a73e5e5SDavid Ahern if (nlmsg_attrlen(nlh, sizeof(*frh))) {
11354a73e5e5SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid data after header in fib rule dump request");
11364a73e5e5SDavid Ahern return -EINVAL;
11374a73e5e5SDavid Ahern }
11384a73e5e5SDavid Ahern
11394a73e5e5SDavid Ahern return 0;
11404a73e5e5SDavid Ahern }
11414a73e5e5SDavid Ahern
fib_nl_dumprule(struct sk_buff * skb,struct netlink_callback * cb)1142c454673dSThomas Graf static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
1143c454673dSThomas Graf {
11444a73e5e5SDavid Ahern const struct nlmsghdr *nlh = cb->nlh;
11453b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk);
1146c454673dSThomas Graf struct fib_rules_ops *ops;
1147c454673dSThomas Graf int idx = 0, family;
1148c454673dSThomas Graf
11494a73e5e5SDavid Ahern if (cb->strict_check) {
11504a73e5e5SDavid Ahern int err = fib_valid_dumprule_req(nlh, cb->extack);
11514a73e5e5SDavid Ahern
11524a73e5e5SDavid Ahern if (err < 0)
11534a73e5e5SDavid Ahern return err;
11544a73e5e5SDavid Ahern }
11554a73e5e5SDavid Ahern
11564a73e5e5SDavid Ahern family = rtnl_msg_family(nlh);
1157c454673dSThomas Graf if (family != AF_UNSPEC) {
1158c454673dSThomas Graf /* Protocol specific dump request */
11595fd30ee7SDenis V. Lunev ops = lookup_rules_ops(net, family);
1160c454673dSThomas Graf if (ops == NULL)
1161c454673dSThomas Graf return -EAFNOSUPPORT;
1162c454673dSThomas Graf
116341fc0143SWilson Kok dump_rules(skb, cb, ops);
116441fc0143SWilson Kok
116541fc0143SWilson Kok return skb->len;
1166c454673dSThomas Graf }
1167c454673dSThomas Graf
1168c454673dSThomas Graf rcu_read_lock();
11695fd30ee7SDenis V. Lunev list_for_each_entry_rcu(ops, &net->rules_ops, list) {
1170c454673dSThomas Graf if (idx < cb->args[0] || !try_module_get(ops->owner))
1171c454673dSThomas Graf goto skip;
1172c454673dSThomas Graf
1173c454673dSThomas Graf if (dump_rules(skb, cb, ops) < 0)
1174c454673dSThomas Graf break;
1175c454673dSThomas Graf
1176c454673dSThomas Graf cb->args[1] = 0;
1177c454673dSThomas Graf skip:
1178c454673dSThomas Graf idx++;
1179c454673dSThomas Graf }
1180c454673dSThomas Graf rcu_read_unlock();
1181c454673dSThomas Graf cb->args[0] = idx;
1182c454673dSThomas Graf
1183c454673dSThomas Graf return skb->len;
1184c454673dSThomas Graf }
118514c0b97dSThomas Graf
notify_rule_change(int event,struct fib_rule * rule,struct fib_rules_ops * ops,struct nlmsghdr * nlh,u32 pid)11869e3a5487SDenis V. Lunev static void notify_rule_change(int event, struct fib_rule *rule,
1187c17084d2SThomas Graf struct fib_rules_ops *ops, struct nlmsghdr *nlh,
1188c17084d2SThomas Graf u32 pid)
118914c0b97dSThomas Graf {
11909e3a5487SDenis V. Lunev struct net *net;
1191c17084d2SThomas Graf struct sk_buff *skb;
119259607863SZheng Yongjun int err = -ENOMEM;
119314c0b97dSThomas Graf
11949e3a5487SDenis V. Lunev net = ops->fro_net;
1195339bf98fSThomas Graf skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
119614c0b97dSThomas Graf if (skb == NULL)
1197c17084d2SThomas Graf goto errout;
1198c17084d2SThomas Graf
1199c17084d2SThomas Graf err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
120026932566SPatrick McHardy if (err < 0) {
120126932566SPatrick McHardy /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
120226932566SPatrick McHardy WARN_ON(err == -EMSGSIZE);
120326932566SPatrick McHardy kfree_skb(skb);
120426932566SPatrick McHardy goto errout;
120526932566SPatrick McHardy }
12069e3a5487SDenis V. Lunev
12071ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
12081ce85fe4SPablo Neira Ayuso return;
1209c17084d2SThomas Graf errout:
1210c17084d2SThomas Graf if (err < 0)
12115fd30ee7SDenis V. Lunev rtnl_set_sk_err(net, ops->nlgroup, err);
121214c0b97dSThomas Graf }
121314c0b97dSThomas Graf
attach_rules(struct list_head * rules,struct net_device * dev)121414c0b97dSThomas Graf static void attach_rules(struct list_head *rules, struct net_device *dev)
121514c0b97dSThomas Graf {
121614c0b97dSThomas Graf struct fib_rule *rule;
121714c0b97dSThomas Graf
121814c0b97dSThomas Graf list_for_each_entry(rule, rules, list) {
1219491deb24SPatrick McHardy if (rule->iifindex == -1 &&
1220491deb24SPatrick McHardy strcmp(dev->name, rule->iifname) == 0)
1221491deb24SPatrick McHardy rule->iifindex = dev->ifindex;
12221b038a5eSPatrick McHardy if (rule->oifindex == -1 &&
12231b038a5eSPatrick McHardy strcmp(dev->name, rule->oifname) == 0)
12241b038a5eSPatrick McHardy rule->oifindex = dev->ifindex;
122514c0b97dSThomas Graf }
122614c0b97dSThomas Graf }
122714c0b97dSThomas Graf
detach_rules(struct list_head * rules,struct net_device * dev)122814c0b97dSThomas Graf static void detach_rules(struct list_head *rules, struct net_device *dev)
122914c0b97dSThomas Graf {
123014c0b97dSThomas Graf struct fib_rule *rule;
123114c0b97dSThomas Graf
12321b038a5eSPatrick McHardy list_for_each_entry(rule, rules, list) {
1233491deb24SPatrick McHardy if (rule->iifindex == dev->ifindex)
1234491deb24SPatrick McHardy rule->iifindex = -1;
12351b038a5eSPatrick McHardy if (rule->oifindex == dev->ifindex)
12361b038a5eSPatrick McHardy rule->oifindex = -1;
12371b038a5eSPatrick McHardy }
123814c0b97dSThomas Graf }
123914c0b97dSThomas Graf
124014c0b97dSThomas Graf
fib_rules_event(struct notifier_block * this,unsigned long event,void * ptr)124114c0b97dSThomas Graf static int fib_rules_event(struct notifier_block *this, unsigned long event,
124214c0b97dSThomas Graf void *ptr)
124314c0b97dSThomas Graf {
1244351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1245c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev);
124614c0b97dSThomas Graf struct fib_rules_ops *ops;
124714c0b97dSThomas Graf
1248748e2d93SEric Dumazet ASSERT_RTNL();
124914c0b97dSThomas Graf
125014c0b97dSThomas Graf switch (event) {
125114c0b97dSThomas Graf case NETDEV_REGISTER:
12525fd30ee7SDenis V. Lunev list_for_each_entry(ops, &net->rules_ops, list)
125376c72d4fSDenis V. Lunev attach_rules(&ops->rules_list, dev);
125414c0b97dSThomas Graf break;
125514c0b97dSThomas Graf
1256946c032eSMaciej Żenczykowski case NETDEV_CHANGENAME:
1257946c032eSMaciej Żenczykowski list_for_each_entry(ops, &net->rules_ops, list) {
1258946c032eSMaciej Żenczykowski detach_rules(&ops->rules_list, dev);
1259946c032eSMaciej Żenczykowski attach_rules(&ops->rules_list, dev);
1260946c032eSMaciej Żenczykowski }
1261946c032eSMaciej Żenczykowski break;
1262946c032eSMaciej Żenczykowski
126314c0b97dSThomas Graf case NETDEV_UNREGISTER:
12645fd30ee7SDenis V. Lunev list_for_each_entry(ops, &net->rules_ops, list)
126576c72d4fSDenis V. Lunev detach_rules(&ops->rules_list, dev);
126614c0b97dSThomas Graf break;
126714c0b97dSThomas Graf }
126814c0b97dSThomas Graf
126914c0b97dSThomas Graf return NOTIFY_DONE;
127014c0b97dSThomas Graf }
127114c0b97dSThomas Graf
127214c0b97dSThomas Graf static struct notifier_block fib_rules_notifier = {
127314c0b97dSThomas Graf .notifier_call = fib_rules_event,
127414c0b97dSThomas Graf };
127514c0b97dSThomas Graf
fib_rules_net_init(struct net * net)12762c8c1e72SAlexey Dobriyan static int __net_init fib_rules_net_init(struct net *net)
12775fd30ee7SDenis V. Lunev {
12785fd30ee7SDenis V. Lunev INIT_LIST_HEAD(&net->rules_ops);
12795fd30ee7SDenis V. Lunev spin_lock_init(&net->rules_mod_lock);
12805fd30ee7SDenis V. Lunev return 0;
12815fd30ee7SDenis V. Lunev }
12825fd30ee7SDenis V. Lunev
fib_rules_net_exit(struct net * net)1283ce2b7db3SVasily Averin static void __net_exit fib_rules_net_exit(struct net *net)
1284ce2b7db3SVasily Averin {
1285ce2b7db3SVasily Averin WARN_ON_ONCE(!list_empty(&net->rules_ops));
1286ce2b7db3SVasily Averin }
1287ce2b7db3SVasily Averin
12885fd30ee7SDenis V. Lunev static struct pernet_operations fib_rules_net_ops = {
12895fd30ee7SDenis V. Lunev .init = fib_rules_net_init,
1290ce2b7db3SVasily Averin .exit = fib_rules_net_exit,
12915fd30ee7SDenis V. Lunev };
12925fd30ee7SDenis V. Lunev
fib_rules_init(void)129314c0b97dSThomas Graf static int __init fib_rules_init(void)
129414c0b97dSThomas Graf {
12955fd30ee7SDenis V. Lunev int err;
1296b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, 0);
1297b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, 0);
1298b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, 0);
12999d9e6a58SThomas Graf
13005d6d4809SEric W. Biederman err = register_pernet_subsys(&fib_rules_net_ops);
13015fd30ee7SDenis V. Lunev if (err < 0)
13025fd30ee7SDenis V. Lunev goto fail;
13035fd30ee7SDenis V. Lunev
13045d6d4809SEric W. Biederman err = register_netdevice_notifier(&fib_rules_notifier);
13055fd30ee7SDenis V. Lunev if (err < 0)
13065fd30ee7SDenis V. Lunev goto fail_unregister;
13075d6d4809SEric W. Biederman
13085fd30ee7SDenis V. Lunev return 0;
13095fd30ee7SDenis V. Lunev
13105fd30ee7SDenis V. Lunev fail_unregister:
13115d6d4809SEric W. Biederman unregister_pernet_subsys(&fib_rules_net_ops);
13125fd30ee7SDenis V. Lunev fail:
13135fd30ee7SDenis V. Lunev rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
13145fd30ee7SDenis V. Lunev rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
13155fd30ee7SDenis V. Lunev rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
13165fd30ee7SDenis V. Lunev return err;
131714c0b97dSThomas Graf }
131814c0b97dSThomas Graf
131914c0b97dSThomas Graf subsys_initcall(fib_rules_init);
1320