Skip to contentSkip to conversation

net: Allow a rule to track originating protocol

PATCH In review Donald Sharp 4 participants 5 messages Feb 20, 2018 – Feb 22, 2018

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: Allow a rule to track originating protocol

Allow a rule that is being added/deleted/modified or dumped to contain the originating protocol's id.

The protocol is handled just like a routes originating protocol is. This is especially useful because there is starting to be a plethora of different user space programs adding rules.

Allow the vrf device to specify that the kernel is the originator of the rule created for this device.

---
 drivers/net/vrf.c              | 1 +
 include/net/fib_rules.h        | 3 ++-
 include/uapi/linux/fib_rules.h | 2 +-
 net/core/fib_rules.c           | 7 ++++++-
 4 files changed, 10 insertions(+), 3 deletions(-)
drivers/net/vrf.c +1 -0
index 139c61c8244a..ec6d2d623b60 100644
@@ -1175,6 +1175,7 @@ static int vrf_fib_rule(const struct net_device *dev, __u8 family, bool add_it)
 	memset(frh, 0, sizeof(*frh));
 	frh->family = family;
 	frh->action = FR_ACT_TO_TBL;
+	frh->proto = RTPROT_KERNEL;
 
 	if (nla_put_u8(skb, FRA_L3MDEV, 1))
 		goto nla_put_failure;
include/net/fib_rules.h +2 -1
index 648caf90ec07..b166ef07e6d4 100644
@@ -26,7 +26,8 @@ struct fib_rule {
 	u32			table;
 	u8			action;
 	u8			l3mdev;
-	/* 2 bytes hole, try to use */
+	u8                      proto;
+	/* 1 byte hole, try to use */
 	u32			target;
 	__be64			tun_id;
 	struct fib_rule __rcu	*ctarget;
include/uapi/linux/fib_rules.h +1 -1
index 2b642bf9b5a0..925539172d5b 100644
@@ -23,8 +23,8 @@ struct fib_rule_hdr {
 	__u8		tos;
 
 	__u8		table;
+	__u8		proto;
 	__u8		res1;	/* reserved */
-	__u8		res2;	/* reserved */
 	__u8		action;
 
 	__u32		flags;
net/core/fib_rules.c +6 -1
index 98e1066c3d55..c1d4ab5b2d9f 100644
@@ -51,6 +51,7 @@ int fib_default_rule_add(struct fib_rules_ops *ops,
 	r->pref = pref;
 	r->table = table;
 	r->flags = flags;
+	r->proto = RTPROT_KERNEL;
 	r->fr_net = ops->fro_net;
 	r->uid_range = fib_kuid_range_unset;
 
@@ -465,6 +466,7 @@ int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 	}
 	refcount_set(&rule->refcnt, 1);
 	rule->fr_net = net;
+	rule->proto = frh->proto;
 
 	rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY])
 	                              : fib_default_rule_pref(ops);
@@ -664,6 +666,9 @@ int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 	}
 
 	list_for_each_entry(rule, &ops->rules_list, list) {
+		if (frh->proto && (frh->proto != rule->proto))
+			continue;
+
 		if (frh->action && (frh->action != rule->action))
 			continue;
 
@@ -808,9 +813,9 @@ static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
 	if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
 		goto nla_put_failure;
 	frh->res1 = 0;
-	frh->res2 = 0;
 	frh->action = rule->action;
 	frh->flags = rule->flags;
+	frh->proto = rule->proto;
 
 	if (rule->action == FR_ACT_GOTO &&
 	    rcu_access_pointer(rule->ctarget) == NULL)
Signature · 1 line
2.14.3
maintainer
#1 lore raw

From: Donald Sharp <[email protected]>
Date: Tue, 20 Feb 2018 08:55:58 -0500

Quoted 8 lines

Allow a rule that is being added/deleted/modified or dumped to contain the originating protocol's id.

The protocol is handled just like a routes originating protocol is. This is especially useful because there is starting to be a plethora of different user space programs adding rules.

Quoted 4 lines

Allow the vrf device to specify that the kernel is the originator of the rule created for this device.

Looks good, applied, thanks Donald.

#2 lore raw
In reply to Donald Sharp

Hi Donald,

Quoted 80 lines · Donald Sharp

Allow a rule that is being added/deleted/modified or dumped to contain the originating protocol's id.

The protocol is handled just like a routes originating protocol is. This is especially useful because there is starting to be a plethora of different user space programs adding rules.

Allow the vrf device to specify that the kernel is the originator of the rule created for this device.

---
 drivers/net/vrf.c              | 1 +
 include/net/fib_rules.h        | 3 ++-
 include/uapi/linux/fib_rules.h | 2 +-
 net/core/fib_rules.c           | 7 ++++++-
 4 files changed, 10 insertions(+), 3 deletions(-)
drivers/net/vrf.c +1 -0
index 139c61c8244a..ec6d2d623b60 100644
@@ -1175,6 +1175,7 @@ static int vrf_fib_rule(const struct net_device *dev, __u8 family, bool add_it)
 	memset(frh, 0, sizeof(*frh));
 	frh->family = family;
 	frh->action = FR_ACT_TO_TBL;
+	frh->proto = RTPROT_KERNEL;
 
 	if (nla_put_u8(skb, FRA_L3MDEV, 1))
 		goto nla_put_failure;
include/net/fib_rules.h +2 -1
index 648caf90ec07..b166ef07e6d4 100644
@@ -26,7 +26,8 @@ struct fib_rule {
 	u32			table;
 	u8			action;
 	u8			l3mdev;
-	/* 2 bytes hole, try to use */
+	u8                      proto;
+	/* 1 byte hole, try to use */
 	u32			target;
 	__be64			tun_id;
 	struct fib_rule __rcu	*ctarget;
include/uapi/linux/fib_rules.h +1 -1
index 2b642bf9b5a0..925539172d5b 100644
@@ -23,8 +23,8 @@ struct fib_rule_hdr {
 	__u8		tos;
 
 	__u8		table;
+	__u8		proto;
 	__u8		res1;	/* reserved */
-	__u8		res2;	/* reserved */
 	__u8		action;
 
 	__u32		flags;
net/core/fib_rules.c +2 -0
index 98e1066c3d55..c1d4ab5b2d9f 100644
@@ -51,6 +51,7 @@ int fib_default_rule_add(struct fib_rules_ops *ops,
 	r->pref = pref;
 	r->table = table;
 	r->flags = flags;
+	r->proto = RTPROT_KERNEL;
 	r->fr_net = ops->fro_net;
 	r->uid_range = fib_kuid_range_unset;
 
@@ -465,6 +466,7 @@ int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 	}
 	refcount_set(&rule->refcnt, 1);
 	rule->fr_net = net;
+	rule->proto = frh->proto;
 
 	rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY])
 	                              : fib_default_rule_pref(ops);

net/core/fib_rules.c int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh, · line 670

Quoted 5 lines
net/core/fib_rules.c +2 -0
@@ -664,6 +666,9 @@ int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 	}
 
 	list_for_each_entry(rule, &ops->rules_list, list) {
+		if (frh->proto && (frh->proto != rule->proto))
+			continue;

This breaks my scripts:

# ip -4 rule show
0:      from all lookup local
32766:  from all lookup main
32767:  from all lookup default
# ip -4 rule del pref 0
RTNETLINK answers: No such file or directory

Using iproute 4.15 in Fedora 27:

# ip -V
ip utility, iproute2-ss180129

Problem is iproute sets protocol to RTPROT_BOOT while rules are installed with RTPROT_KERNEL.

Maybe add FRA_PROTOCOL?

Thanks!

Quoted 18 lines
net/core/fib_rules.c +2 -1
+
 		if (frh->action && (frh->action != rule->action))
 			continue;
 
@@ -808,9 +813,9 @@ static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
 	if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
 		goto nla_put_failure;
 	frh->res1 = 0;
-	frh->res2 = 0;
 	frh->action = rule->action;
 	frh->flags = rule->flags;
+	frh->proto = rule->proto;
 
 	if (rule->action == FR_ACT_GOTO &&
 	    rcu_access_pointer(rule->ctarget) == NULL)
Signature · 1 line
2.14.3
#3 lore raw
Quoted 40 lines · Ido Schimmel
Quoted 21 lines
net/core/fib_rules.c +2 -0
index 98e1066c3d55..c1d4ab5b2d9f 100644
@@ -51,6 +51,7 @@ int fib_default_rule_add(struct fib_rules_ops *ops,
 	r->pref = pref;
 	r->table = table;
 	r->flags = flags;
+	r->proto = RTPROT_KERNEL;
 	r->fr_net = ops->fro_net;
 	r->uid_range = fib_kuid_range_unset;
 
@@ -465,6 +466,7 @@ int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 	}
 	refcount_set(&rule->refcnt, 1);
 	rule->fr_net = net;
+	rule->proto = frh->proto;
 
 	rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY])
 	                              : fib_default_rule_pref(ops);

net/core/fib_rules.c int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh, · line 670

Quoted 5 lines
net/core/fib_rules.c +2 -0
@@ -664,6 +666,9 @@ int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 	}
 
 	list_for_each_entry(rule, &ops->rules_list, list) {
+		if (frh->proto && (frh->proto != rule->proto))
+			continue;

This breaks my scripts:

# ip -4 rule show
0:      from all lookup local
32766:  from all lookup main
32767:  from all lookup default
# ip -4 rule del pref 0
RTNETLINK answers: No such file or directory

Using iproute 4.15 in Fedora 27:

# ip -V
ip utility, iproute2-ss180129
Quoted 6 lines

Problem is iproute sets protocol to RTPROT_BOOT while rules are installed with RTPROT_KERNEL.

Maybe add FRA_PROTOCOL?

Thanks!

ugh. Another iproute2 bug that the kernel has to deal with. iproute2 has been using rtm for the ancillary header for rules when it should have been fib_rule_hdr. That bug allowed someone to set the protocol field to RTPROT_BOOT which was complete nonsense for rules until Donald's recent patch.

That means all FIB rules need to default to RTPROT_BOOT. I hate to inherit that for the l3mdev rule, but looking at the iproute2 code I don't see any options.

Donald: send a patch that changes the protocol for kernel installed rules to RTPROT_BOOT.

#4 lore raw
Quoted 26 lines · David Ahern
Quoted 13 lines

This breaks my scripts:

# ip -4 rule show
0:      from all lookup local
32766:  from all lookup main
32767:  from all lookup default
# ip -4 rule del pref 0
RTNETLINK answers: No such file or directory

Using iproute 4.15 in Fedora 27:

# ip -V
ip utility, iproute2-ss180129
Quoted 6 lines

Problem is iproute sets protocol to RTPROT_BOOT while rules are installed with RTPROT_KERNEL.

Maybe add FRA_PROTOCOL?

Thanks!

ugh. Another iproute2 bug that the kernel has to deal with. iproute2 has been using rtm for the ancillary header for rules when it should have been fib_rule_hdr. That bug allowed someone to set the protocol field to RTPROT_BOOT which was complete nonsense for rules until Donald's recent patch.

Quoted 7 lines

That means all FIB rules need to default to RTPROT_BOOT. I hate to inherit that for the l3mdev rule, but looking at the iproute2 code I don't see any options.

Donald: send a patch that changes the protocol for kernel installed rules to RTPROT_BOOT.

After more thinking, Donald is going move the protocol to an FRA_PROTOCOL attribute as you suggested. That avoids breaking legacy iproute2 and allows us to keep a sane default protocol value.