Skip to contentSkip to conversation

Allow 'ip rule' command to use protocol

PATCH Superseded Donald Sharp 2 participants 17 messages Feb 21, 2018 – Mar 1, 2018

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

cover Allow 'ip rule' command to use protocol

Fix iprule.c to use the actual struct fib_rule_hdr and to allow the end user to see and use the protocol keyword for rule manipulations.

Donald Sharp (4):
ip: Use the struct fib_rule_hdr for rules
ip: Display ip rule protocol used
ip: Allow rules to accept a specified protocol
ip: Add ability to flush a rule based upon protocol

 include/uapi/linux/fib_rules.h |   2 +-
 ip/iprule.c                    | 167 ++++++++++++++++++++++++-----------------
 man/man8/ip-rule.8             |  18 ++++-
 3 files changed, 117 insertions(+), 70 deletions(-)
Signature · 1 line
2.14.3
#1 lore raw

v2 cover Allow 'ip rule' command to use protocol

Fix iprule.c to use the actual struct fib_rule_hdr and to allow the end user to see and use the protocol keyword for rule manipulations.

v2: Rearrange and code changes as per David Ahern

Donald Sharp (3):
ip: Use the struct fib_rule_hdr for rules
ip: Display ip rule protocol used
ip: Allow rules to accept a specified protocol

 include/uapi/linux/fib_rules.h |   2 +-
 ip/iprule.c                    | 164 ++++++++++++++++++++++++-----------------
 man/man8/ip-rule.8             |  18 ++++-
 3 files changed, 114 insertions(+), 70 deletions(-)
Signature · 1 line
2.14.3
#2 lore raw

PATCH v2 1/3 ip: Use the `struct fib_rule_hdr` for rules

In reply to Donald Sharp

The iprule.c code was using struct rtmsg as the data type to pass into the kernel for the netlink message. While 'struct rtmsg' and struct fib_rule_hdr are the same size and mostly the same, we should use the correct data structure. This commit translates the data structures to have iprule.c use the correct one.

Additionally copy over the modified fib_rules.h file

---
 include/uapi/linux/fib_rules.h |   2 +-
 ip/iprule.c                    | 129 ++++++++++++++++++++++-------------------
 2 files changed, 69 insertions(+), 62 deletions(-)
include/uapi/linux/fib_rules.h +1 -1
index 2b642bf9..92553917 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;
ip/iprule.c +68 -61

320 lines not on this page · raw message

Signature · 1 line
2.14.3
#4 lore raw

PATCH v2 2/3 ip: Display ip rule protocol used

In reply to Donald Sharp

Modify 'ip rule' command to notice when the kernel passes to us the originating protocol.

Add code to allow the ip rule flush protocol XXX command to be accepted and properly handled.

Modify the documentation to reflect these code changes.

---
 ip/iprule.c        | 29 ++++++++++++++++++++++-------
 man/man8/ip-rule.8 | 18 +++++++++++++++++-
 2 files changed, 39 insertions(+), 8 deletions(-)
ip/iprule.c +22 -7
index 00a6c26a..39008768 100644
@@ -47,6 +47,7 @@ static void usage(void)
 		"            [ iif STRING ] [ oif STRING ] [ pref NUMBER ] [ l3mdev ]\n"
 		"            [ uidrange NUMBER-NUMBER ]\n"
 		"ACTION := [ table TABLE_ID ]\n"
+		"          [ protocol RPROTO ]\n"
 		"          [ nat ADDRESS ]\n"
 		"          [ realms [SRCREALM/]DSTREALM ]\n"
 		"          [ goto NUMBER ]\n"
@@ -71,6 +72,8 @@ static struct
 	struct fib_rule_uid_range range;
 	inet_prefix src;
 	inet_prefix dst;
+	int protocol;
+	int protocolmask;
 } filter;
 
 static inline int frh_get_table(struct fib_rule_hdr *frh, struct rtattr **tb)
@@ -338,6 +341,10 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 			rtnl_rtntype_n2a(frh->action,
 					 b1, sizeof(b1)));
 
+	if (frh->proto != RTPROT_UNSPEC)
+		fprintf(fp, " proto %s ",
+			rtnl_rtprot_n2a(frh->proto, b1, sizeof(b1)));
+
 	fprintf(fp, "\n");
 	fflush(fp);
 	return 0;
@@ -391,6 +398,9 @@ static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n,
 
 	parse_rtattr(tb, FRA_MAX, RTM_RTA(frh), len);
 
+	if ((filter.protocol^frh->proto)&filter.protocolmask)
+		return 0;
+
 	if (tb[FRA_PRIORITY]) {
 		n->nlmsg_type = RTM_DELRULE;
 		n->nlmsg_flags = NLM_F_REQUEST;
@@ -415,12 +425,6 @@ static int iprule_list_flush_or_save(int argc, char **argv, int action)
 	if (af == AF_UNSPEC)
 		af = AF_INET;
 
-	if (action != IPRULE_LIST && argc > 0) {
-		fprintf(stderr, "\"ip rule %s\" does not take any arguments.\n",
-				action == IPRULE_SAVE ? "save" : "flush");
-		return -1;
-	}
-
 	switch (action) {
 	case IPRULE_SAVE:
 		if (save_rule_prep())
@@ -508,7 +512,18 @@ static int iprule_list_flush_or_save(int argc, char **argv, int action)
 			NEXT_ARG();
 			if (get_prefix(&filter.src, *argv, af))
 				invarg("from value is invalid\n", *argv);
-		} else {
+		} else if (matches(*argv, "protocol") == 0) {
+			__u32 prot;
+			NEXT_ARG();
+			filter.protocolmask = -1;
+			if (rtnl_rtprot_a2n(&prot, *argv)) {
+				if (strcmp(*argv, "all") != 0)
+					invarg("invalid \"protocol\"\n", *argv);
+				prot = 0;
+				filter.protocolmask = 0;
+			}
+			filter.protocol = prot;
+		} else{
 			if (matches(*argv, "dst") == 0 ||
 			    matches(*argv, "to") == 0) {
 				NEXT_ARG();
man/man8/ip-rule.8 +17 -1
index a5c47981..98b2573d 100644
@@ -50,6 +50,8 @@ ip-rule \- routing policy database management
 .IR ACTION " := [ "
 .B  table
 .IR TABLE_ID " ] [ "
+.B  protocol
+.IR RPROTO " ] [ "
 .B  nat
 .IR ADDRESS " ] [ "
 .B realms
@@ -240,6 +242,10 @@ The options preference and order are synonyms with priority.
 the routing table identifier to lookup if the rule selector matches.
 It is also possible to use lookup instead of table.
 
+.TP
+.BI protocol " RPROTO"
+the protocol who installed the rule in question.
+
 .TP
 .BI suppress_prefixlength " NUMBER"
 reject routing decisions that have a prefix length of NUMBER or less.
@@ -275,7 +281,11 @@ updates, it flushes the routing cache with
 .RE
 .TP
 .B ip rule flush - also dumps all the deleted rules.
-This command has no arguments.
+.RS
+.TP
+.BI protocol " RPROTO"
+Select the originating protocol.
+.RE
 .TP
 .B ip rule show - list rules
 This command has no arguments.
@@ -283,6 +293,12 @@ The options list or lst are synonyms with show.
 
 .TP
 .B ip rule save
+.RS
+.TP
+.BI protocl " RPROTO"
+Select the originating protocol.
+.RE
+.TP
 save rules table information to stdout
 .RS
 This command behaves like
Signature · 1 line
2.14.3
#6 lore raw

PATCH v2 3/3 ip: Allow rules to accept a specified protocol

In reply to Donald Sharp

Allow the specification of a protocol when the user adds/modifies/deletes a rule.

---
 ip/iprule.c | 6 ++++++
 1 file changed, 6 insertions(+)
ip/iprule.c +6 -0
index 39008768..192fe215 100644
@@ -683,6 +683,12 @@ static int iprule_modify(int cmd, int argc, char **argv)
 			if (get_rt_realms_or_raw(&realm, *argv))
 				invarg("invalid realms\n", *argv);
 			addattr32(&req.n, sizeof(req), FRA_FLOW, realm);
+		} else if (matches(*argv, "protocol") == 0) {
+			__u32 proto;
+			NEXT_ARG();
+			if (rtnl_rtprot_a2n(&proto, *argv))
+				invarg("\"protocol\" value is invalid\n", *argv);
+			req.frh.proto = proto;
 		} else if (matches(*argv, "table") == 0 ||
 			   strcmp(*argv, "lookup") == 0) {
 			NEXT_ARG();
Signature · 1 line
2.14.3
#3 lore raw

Re: [PATCH v2 iproute2-next 1/3] ip: Use the `struct fib_rule_hdr` for rules

In reply to Donald Sharp
Quoted 16 lines · Donald Sharp
ip/iprule.c +2 -6
@@ -577,21 +585,20 @@ static int iprule_modify(int cmd, int argc, char **argv)
 	__u32 tid = 0;
 	struct {
 		struct nlmsghdr	n;
-		struct rtmsg		r;
+		struct fib_rule_hdr	frh;
 		char			buf[1024];
 	} req = {
 		.n.nlmsg_type = cmd,
-		.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)),
+		.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
 		.n.nlmsg_flags = NLM_F_REQUEST,
-		.r.rtm_family = preferred_family,
-		.r.rtm_protocol = RTPROT_BOOT,
-		.r.rtm_scope = RT_SCOPE_UNIVERSE,
-		.r.rtm_type = RTN_UNSPEC,

ip/iprule.c static int iprule_modify(int cmd, int argc, char **argv) · line 602

Quoted 10 lines
ip/iprule.c +4 -1
+		.frh.family = preferred_family,
+		.frh.proto = RTPROT_BOOT,
+		.frh.action = RTN_UNSPEC,
 	};
 
 	if (cmd == RTM_NEWRULE) {
 		req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
-		req.r.rtm_type = RTN_UNICAST;
+		req.frh.action = RTN_UNICAST;
 	}

The action should be FR_ACT_TO_TBL; RTN_UNICAST == 1 == FR_ACT_TO_TBL; the latter is the proper enum for fib rules.

#5 lore raw

Re: [PATCH v2 iproute2-next 2/3] ip: Display ip rule protocol used

In reply to Donald Sharp

ip/iprule.c static void usage(void) · line 50

Quoted 9 lines · Donald Sharp
ip/iprule.c +1 -0
index 00a6c26a..39008768 100644
@@ -47,6 +47,7 @@ static void usage(void)
 		"            [ iif STRING ] [ oif STRING ] [ pref NUMBER ] [ l3mdev ]\n"
 		"            [ uidrange NUMBER-NUMBER ]\n"
 		"ACTION := [ table TABLE_ID ]\n"
+		"          [ protocol RPROTO ]\n"

Drop the 'R' makes it harder to read; just 'PROTO' is fine.

Quoted 71 lines
ip/iprule.c +21 -7
 		"          [ nat ADDRESS ]\n"
 		"          [ realms [SRCREALM/]DSTREALM ]\n"
 		"          [ goto NUMBER ]\n"
@@ -71,6 +72,8 @@ static struct
 	struct fib_rule_uid_range range;
 	inet_prefix src;
 	inet_prefix dst;
+	int protocol;
+	int protocolmask;
 } filter;
 
 static inline int frh_get_table(struct fib_rule_hdr *frh, struct rtattr **tb)
@@ -338,6 +341,10 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 			rtnl_rtntype_n2a(frh->action,
 					 b1, sizeof(b1)));
 
+	if (frh->proto != RTPROT_UNSPEC)
+		fprintf(fp, " proto %s ",
+			rtnl_rtprot_n2a(frh->proto, b1, sizeof(b1)));
+
 	fprintf(fp, "\n");
 	fflush(fp);
 	return 0;
@@ -391,6 +398,9 @@ static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n,
 
 	parse_rtattr(tb, FRA_MAX, RTM_RTA(frh), len);
 
+	if ((filter.protocol^frh->proto)&filter.protocolmask)
+		return 0;
+
 	if (tb[FRA_PRIORITY]) {
 		n->nlmsg_type = RTM_DELRULE;
 		n->nlmsg_flags = NLM_F_REQUEST;
@@ -415,12 +425,6 @@ static int iprule_list_flush_or_save(int argc, char **argv, int action)
 	if (af == AF_UNSPEC)
 		af = AF_INET;
 
-	if (action != IPRULE_LIST && argc > 0) {
-		fprintf(stderr, "\"ip rule %s\" does not take any arguments.\n",
-				action == IPRULE_SAVE ? "save" : "flush");
-		return -1;
-	}
-
 	switch (action) {
 	case IPRULE_SAVE:
 		if (save_rule_prep())
@@ -508,7 +512,18 @@ static int iprule_list_flush_or_save(int argc, char **argv, int action)
 			NEXT_ARG();
 			if (get_prefix(&filter.src, *argv, af))
 				invarg("from value is invalid\n", *argv);
-		} else {
+		} else if (matches(*argv, "protocol") == 0) {
+			__u32 prot;
+			NEXT_ARG();
+			filter.protocolmask = -1;
+			if (rtnl_rtprot_a2n(&prot, *argv)) {
+				if (strcmp(*argv, "all") != 0)
+					invarg("invalid \"protocol\"\n", *argv);
+				prot = 0;
+				filter.protocolmask = 0;
+			}
+			filter.protocol = prot;
+		} else{
 			if (matches(*argv, "dst") == 0 ||
 			    matches(*argv, "to") == 0) {
 				NEXT_ARG();
man/man8/ip-rule.8 +0 -0
index a5c47981..98b2573d 100644

man/man8/ip-rule.8 ip-rule \- routing policy database management · line 54

Quoted 5 lines
man/man8/ip-rule.8 +2 -0
@@ -50,6 +50,8 @@ ip-rule \- routing policy database management
 .IR ACTION " := [ "
 .B  table
 .IR TABLE_ID " ] [ "
+.B  protocol
+.IR RPROTO " ] [ "

same here and others in this file

#7 lore raw

v3 cover Allow 'ip rule' command to use protocol

In reply to Donald Sharp

Fix iprule.c to use the actual struct fib_rule_hdr and to allow the end user to see and use the protocol keyword for rule manipulation.

v2: Rearrange and code changes as per David Ahern
v3: Fix some missed RTN_XXX to appropriate FR_XX and doc changes

Donald Sharp (3):
ip: Use the struct fib_rule_hdr for rules
ip: Display ip rule protocol used
ip: Allow rules to accept a specified protocol

 include/uapi/linux/fib_rules.h |   1 +
 ip/iprule.c                    | 170 ++++++++++++++++++++++++-----------------
 man/man8/ip-rule.8             |  18 ++++-
 3 files changed, 120 insertions(+), 69 deletions(-)
Signature · 1 line
2.14.3
#8 lore raw

PATCH v3 1/3 ip: Use the `struct fib_rule_hdr` for rules

In reply to Donald Sharp

The iprule.c code was using struct rtmsg as the data type to pass into the kernel for the netlink message. While 'struct rtmsg' and struct fib_rule_hdr are the same size and mostly the same, we should use the correct data structure. This commit translates the data structures to have iprule.c use the correct one.

Additionally copy over the modified fib_rules.h file

---
 include/uapi/linux/fib_rules.h |   1 +
 ip/iprule.c                    | 128 +++++++++++++++++++++--------------------
 2 files changed, 68 insertions(+), 61 deletions(-)
include/uapi/linux/fib_rules.h +1 -0
index 2b642bf9..9477c3af 100644
@@ -58,6 +58,7 @@ enum {
 	FRA_PAD,
 	FRA_L3MDEV,	/* iif or oif is l3mdev goto its table */
 	FRA_UID_RANGE,	/* UID range */
+	FRA_PROTOCOL,
 	__FRA_MAX
 };
 
ip/iprule.c +67 -61

319 lines not on this page · raw message

Signature · 1 line
2.14.3
#9 lore raw

PATCH v3 2/3 ip: Display ip rule protocol used

In reply to Donald Sharp

Modify 'ip rule' command to notice when the kernel passes to us the originating protocol.

Add code to allow the ip rule flush protocol XXX command to be accepted and properly handled.

Modify the documentation to reflect these code changes.

---
 ip/iprule.c        | 36 +++++++++++++++++++++++++++++-------
 man/man8/ip-rule.8 | 18 +++++++++++++++++-
 2 files changed, 46 insertions(+), 8 deletions(-)
ip/iprule.c +29 -7
index 94356bf8..17df9e9b 100644
@@ -47,6 +47,7 @@ static void usage(void)
 		"            [ iif STRING ] [ oif STRING ] [ pref NUMBER ] [ l3mdev ]\n"
 		"            [ uidrange NUMBER-NUMBER ]\n"
 		"ACTION := [ table TABLE_ID ]\n"
+		"          [ protocol PROTO ]\n"
 		"          [ nat ADDRESS ]\n"
 		"          [ realms [SRCREALM/]DSTREALM ]\n"
 		"          [ goto NUMBER ]\n"
@@ -71,6 +72,8 @@ static struct
 	struct fib_rule_uid_range range;
 	inet_prefix src;
 	inet_prefix dst;
+	int protocol;
+	int protocolmask;
 } filter;
 
 static inline int frh_get_table(struct fib_rule_hdr *frh, struct rtattr **tb)
@@ -338,6 +341,16 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 			rtnl_rtntype_n2a(frh->action,
 					 b1, sizeof(b1)));
 
+	if (tb[FRA_PROTOCOL]) {
+		__u8 protocol = rta_getattr_u8(tb[FRA_PROTOCOL]);
+
+		if ((protocol && protocol != RTPROT_KERNEL) ||
+		    show_details > 0) {
+			fprintf(fp, " proto %s ",
+				rtnl_rtprot_n2a(protocol, b1, sizeof(b1)));
+		}
+	}
+
 	fprintf(fp, "\n");
 	fflush(fp);
 	return 0;
@@ -391,6 +404,10 @@ static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n,
 
 	parse_rtattr(tb, FRA_MAX, RTM_RTA(frh), len);
 
+	if (tb[FRA_PROTOCOL] &&
+	    (filter.protocol^rta_getattr_u8(tb[FRA_PROTOCOL]))&filter.protocolmask)
+		return 0;
+
 	if (tb[FRA_PRIORITY]) {
 		n->nlmsg_type = RTM_DELRULE;
 		n->nlmsg_flags = NLM_F_REQUEST;
@@ -415,12 +432,6 @@ static int iprule_list_flush_or_save(int argc, char **argv, int action)
 	if (af == AF_UNSPEC)
 		af = AF_INET;
 
-	if (action != IPRULE_LIST && argc > 0) {
-		fprintf(stderr, "\"ip rule %s\" does not take any arguments.\n",
-				action == IPRULE_SAVE ? "save" : "flush");
-		return -1;
-	}
-
 	switch (action) {
 	case IPRULE_SAVE:
 		if (save_rule_prep())
@@ -508,7 +519,18 @@ static int iprule_list_flush_or_save(int argc, char **argv, int action)
 			NEXT_ARG();
 			if (get_prefix(&filter.src, *argv, af))
 				invarg("from value is invalid\n", *argv);
-		} else {
+		} else if (matches(*argv, "protocol") == 0) {
+			__u32 prot;
+			NEXT_ARG();
+			filter.protocolmask = -1;
+			if (rtnl_rtprot_a2n(&prot, *argv)) {
+				if (strcmp(*argv, "all") != 0)
+					invarg("invalid \"protocol\"\n", *argv);
+				prot = 0;
+				filter.protocolmask = 0;
+			}
+			filter.protocol = prot;
+		} else{
 			if (matches(*argv, "dst") == 0 ||
 			    matches(*argv, "to") == 0) {
 				NEXT_ARG();
man/man8/ip-rule.8 +17 -1
index a5c47981..f4070542 100644
@@ -50,6 +50,8 @@ ip-rule \- routing policy database management
 .IR ACTION " := [ "
 .B  table
 .IR TABLE_ID " ] [ "
+.B  protocol
+.IR PROTO " ] [ "
 .B  nat
 .IR ADDRESS " ] [ "
 .B realms
@@ -240,6 +242,10 @@ The options preference and order are synonyms with priority.
 the routing table identifier to lookup if the rule selector matches.
 It is also possible to use lookup instead of table.
 
+.TP
+.BI protocol " PROTO"
+the protocol who installed the rule in question.
+
 .TP
 .BI suppress_prefixlength " NUMBER"
 reject routing decisions that have a prefix length of NUMBER or less.
@@ -275,7 +281,11 @@ updates, it flushes the routing cache with
 .RE
 .TP
 .B ip rule flush - also dumps all the deleted rules.
-This command has no arguments.
+.RS
+.TP
+.BI protocol " PROTO"
+Select the originating protocol.
+.RE
 .TP
 .B ip rule show - list rules
 This command has no arguments.
@@ -283,6 +293,12 @@ The options list or lst are synonyms with show.
 
 .TP
 .B ip rule save
+.RS
+.TP
+.BI protocl " PROTO"
+Select the originating protocol.
+.RE
+.TP
 save rules table information to stdout
 .RS
 This command behaves like
Signature · 1 line
2.14.3
#11 lore raw

PATCH v3 3/3 ip: Allow rules to accept a specified protocol

In reply to Donald Sharp

Allow the specification of a protocol when the user adds/modifies/deletes a rule.

---
 ip/iprule.c | 6 ++++++
 1 file changed, 6 insertions(+)
ip/iprule.c +6 -0
index 17df9e9b..796da3b3 100644
@@ -689,6 +689,12 @@ static int iprule_modify(int cmd, int argc, char **argv)
 			if (get_rt_realms_or_raw(&realm, *argv))
 				invarg("invalid realms\n", *argv);
 			addattr32(&req.n, sizeof(req), FRA_FLOW, realm);
+		} else if (matches(*argv, "protocol") == 0) {
+			__u32 proto;
+			NEXT_ARG();
+			if (rtnl_rtprot_a2n(&proto, *argv))
+				invarg("\"protocol\" value is invalid\n", *argv);
+			addattr8(&req.n, sizeof(req), FRA_PROTOCOL, proto);
 		} else if (matches(*argv, "table") == 0 ||
 			   strcmp(*argv, "lookup") == 0) {
 			NEXT_ARG();
Signature · 1 line
2.14.3
#10 lore raw

Re: [PATCH v3 iproute2-next 2/3] ip: Display ip rule protocol used

In reply to Donald Sharp

ip/iprule.c static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, · line 410

Quoted 8 lines · Donald Sharp
ip/iprule.c +4 -0
@@ -391,6 +404,10 @@ static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n,
 
 	parse_rtattr(tb, FRA_MAX, RTM_RTA(frh), len);
 
+	if (tb[FRA_PROTOCOL] &&
+	    (filter.protocol^rta_getattr_u8(tb[FRA_PROTOCOL]))&filter.protocolmask)
+		return 0;
+

Please use an intermediate variable for rta conversion and put whitespace around '^' and '&'; existing code that does the above is a poor example to follow. ;-)

ip/iprule.c static int iprule_list_flush_or_save(int argc, char **argv, int action) · line 420

Quoted 10 lines
ip/iprule.c +0 -3
 	if (tb[FRA_PRIORITY]) {
 		n->nlmsg_type = RTM_DELRULE;
 		n->nlmsg_flags = NLM_F_REQUEST;
@@ -415,12 +432,6 @@ static int iprule_list_flush_or_save(int argc, char **argv, int action)
 	if (af == AF_UNSPEC)
 		af = AF_INET;
 
-	if (action != IPRULE_LIST && argc > 0) {
-		fprintf(stderr, "\"ip rule %s\" does not take any arguments.\n",
-				action == IPRULE_SAVE ? "save" : "flush");

You need to keep this for IPRULE_SAVE; only IPRULE_FLUSH is allowing arguments with this patch.

Quoted 40 lines
ip/iprule.c +12 -4
-		return -1;
-	}
-
 	switch (action) {
 	case IPRULE_SAVE:
 		if (save_rule_prep())
@@ -508,7 +519,18 @@ static int iprule_list_flush_or_save(int argc, char **argv, int action)
 			NEXT_ARG();
 			if (get_prefix(&filter.src, *argv, af))
 				invarg("from value is invalid\n", *argv);
-		} else {
+		} else if (matches(*argv, "protocol") == 0) {
+			__u32 prot;
+			NEXT_ARG();
+			filter.protocolmask = -1;
+			if (rtnl_rtprot_a2n(&prot, *argv)) {
+				if (strcmp(*argv, "all") != 0)
+					invarg("invalid \"protocol\"\n", *argv);
+				prot = 0;
+				filter.protocolmask = 0;
+			}
+			filter.protocol = prot;
+		} else{
 			if (matches(*argv, "dst") == 0 ||
 			    matches(*argv, "to") == 0) {
 				NEXT_ARG();
man/man8/ip-rule.8 +2 -0
index a5c47981..f4070542 100644
@@ -50,6 +50,8 @@ ip-rule \- routing policy database management
 .IR ACTION " := [ "
 .B  table
 .IR TABLE_ID " ] [ "
+.B  protocol
+.IR PROTO " ] [ "
 .B  nat
 .IR ADDRESS " ] [ "
 .B realms

man/man8/ip-rule.8 The options preference and order are synonyms with priority. · line 247

Quoted 6 lines
man/man8/ip-rule.8 +3 -0
@@ -240,6 +242,10 @@ The options preference and order are synonyms with priority.
 the routing table identifier to lookup if the rule selector matches.
 It is also possible to use lookup instead of table.
 
+.TP
+.BI protocol " PROTO"
+the protocol who installed the rule in question.

"protocol" is getting a bit vague. Make it clearer by using something like 'routing protocol' as the description and given an example (e.g., zebra for RTPROT_ZEBRA)

Quoted 18 lines
man/man8/ip-rule.8 +6 -1
+
 .TP
 .BI suppress_prefixlength " NUMBER"
 reject routing decisions that have a prefix length of NUMBER or less.
@@ -275,7 +281,11 @@ updates, it flushes the routing cache with
 .RE
 .TP
 .B ip rule flush - also dumps all the deleted rules.
-This command has no arguments.
+.RS
+.TP
+.BI protocol " PROTO"
+Select the originating protocol.
+.RE
 .TP
 .B ip rule show - list rules
 This command has no arguments.

man/man8/ip-rule.8 The options list or lst are synonyms with show. · line 298

Quoted 6 lines
man/man8/ip-rule.8 +3 -0
@@ -283,6 +293,12 @@ The options list or lst are synonyms with show.
 
 .TP
 .B ip rule save
+.RS
+.TP
+.BI protocl " PROTO"

missing an 'o' between 'c' and 'l'

Quoted 7 lines
man/man8/ip-rule.8 +3 -0
+Select the originating protocol.
+.RE
+.TP
 save rules table information to stdout
 .RS
 This command behaves like
#12 lore raw

v4 cover Allow 'ip rule' command to use protocol

In reply to Donald Sharp

Fix iprule.c to use the actual struct fib_rule_hdr and to allow the end user to see and use the protocol keyword for rule manipulation.

v2: Rearrange and code changes as per David Ahern
v3: Fix some missed RTN_XXX to appropriate FR_XX and doc changes
v4: Cleanup some code, fix 'ip rule save' no parameters and doc changes

Donald Sharp (3):
ip: Use the struct fib_rule_hdr for rules
ip: Display ip rule protocol used
ip: Allow rules to accept a specified protocol

 include/uapi/linux/fib_rules.h |   1 +
 ip/iprule.c                    | 173 +++++++++++++++++++++++++----------------
 man/man8/ip-rule.8             |  18 ++++-
 3 files changed, 126 insertions(+), 66 deletions(-)
Signature · 1 line
2.14.3
#14 lore raw

PATCH v4 1/3 ip: Use the `struct fib_rule_hdr` for rules

In reply to Donald Sharp

The iprule.c code was using struct rtmsg as the data type to pass into the kernel for the netlink message. While 'struct rtmsg' and struct fib_rule_hdr are the same size and mostly the same, we should use the correct data structure. This commit translates the data structures to have iprule.c use the correct one.

Additionally copy over the modified fib_rules.h file

---
 include/uapi/linux/fib_rules.h |   1 +
 ip/iprule.c                    | 128 +++++++++++++++++++++--------------------
 2 files changed, 68 insertions(+), 61 deletions(-)
include/uapi/linux/fib_rules.h +1 -0
index 2b642bf9..9477c3af 100644
@@ -58,6 +58,7 @@ enum {
 	FRA_PAD,
 	FRA_L3MDEV,	/* iif or oif is l3mdev goto its table */
 	FRA_UID_RANGE,	/* UID range */
+	FRA_PROTOCOL,
 	__FRA_MAX
 };
 
ip/iprule.c +67 -61

319 lines not on this page · raw message

Signature · 1 line
2.14.3
#15 lore raw

PATCH v4 2/3 ip: Display ip rule protocol used

In reply to Donald Sharp

Modify 'ip rule' command to notice when the kernel passes to us the originating protocol.

Add code to allow the ip rule flush protocol XXX command to be accepted and properly handled.

Modify the documentation to reflect these code changes.

---
 ip/iprule.c        | 38 ++++++++++++++++++++++++++++++++++----
 man/man8/ip-rule.8 | 18 +++++++++++++++++-
 2 files changed, 51 insertions(+), 5 deletions(-)
ip/iprule.c +34 -4
index 94356bf8..8120520e 100644
@@ -47,6 +47,7 @@ static void usage(void)
 		"            [ iif STRING ] [ oif STRING ] [ pref NUMBER ] [ l3mdev ]\n"
 		"            [ uidrange NUMBER-NUMBER ]\n"
 		"ACTION := [ table TABLE_ID ]\n"
+		"          [ protocol PROTO ]\n"
 		"          [ nat ADDRESS ]\n"
 		"          [ realms [SRCREALM/]DSTREALM ]\n"
 		"          [ goto NUMBER ]\n"
@@ -71,6 +72,8 @@ static struct
 	struct fib_rule_uid_range range;
 	inet_prefix src;
 	inet_prefix dst;
+	int protocol;
+	int protocolmask;
 } filter;
 
 static inline int frh_get_table(struct fib_rule_hdr *frh, struct rtattr **tb)
@@ -338,6 +341,16 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 			rtnl_rtntype_n2a(frh->action,
 					 b1, sizeof(b1)));
 
+	if (tb[FRA_PROTOCOL]) {
+		__u8 protocol = rta_getattr_u8(tb[FRA_PROTOCOL]);
+
+		if ((protocol && protocol != RTPROT_KERNEL) ||
+		    show_details > 0) {
+			fprintf(fp, " proto %s ",
+				rtnl_rtprot_n2a(protocol, b1, sizeof(b1)));
+		}
+	}
+
 	fprintf(fp, "\n");
 	fflush(fp);
 	return 0;
@@ -391,6 +404,13 @@ static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n,
 
 	parse_rtattr(tb, FRA_MAX, RTM_RTA(frh), len);
 
+	if (tb[FRA_PROTOCOL]) {
+		__u8 protocol = rta_getattr_u8(tb[FRA_PROTOCOL]);
+
+		if ((filter.protocol ^ protocol) & filter.protocolmask)
+			return 0;
+	}
+
 	if (tb[FRA_PRIORITY]) {
 		n->nlmsg_type = RTM_DELRULE;
 		n->nlmsg_flags = NLM_F_REQUEST;
@@ -415,9 +435,8 @@ static int iprule_list_flush_or_save(int argc, char **argv, int action)
 	if (af == AF_UNSPEC)
 		af = AF_INET;
 
-	if (action != IPRULE_LIST && argc > 0) {
-		fprintf(stderr, "\"ip rule %s\" does not take any arguments.\n",
-				action == IPRULE_SAVE ? "save" : "flush");
+	if (action == IPRULE_SAVE && argc > 0) {
+		fprintf(stderr, "\"ip rule save\" does not take any arguments.\n");
 		return -1;
 	}
 
@@ -508,7 +527,18 @@ static int iprule_list_flush_or_save(int argc, char **argv, int action)
 			NEXT_ARG();
 			if (get_prefix(&filter.src, *argv, af))
 				invarg("from value is invalid\n", *argv);
-		} else {
+		} else if (matches(*argv, "protocol") == 0) {
+			__u32 prot;
+			NEXT_ARG();
+			filter.protocolmask = -1;
+			if (rtnl_rtprot_a2n(&prot, *argv)) {
+				if (strcmp(*argv, "all") != 0)
+					invarg("invalid \"protocol\"\n", *argv);
+				prot = 0;
+				filter.protocolmask = 0;
+			}
+			filter.protocol = prot;
+		} else{
 			if (matches(*argv, "dst") == 0 ||
 			    matches(*argv, "to") == 0) {
 				NEXT_ARG();
man/man8/ip-rule.8 +17 -1
index a5c47981..7cf8fd9a 100644
@@ -50,6 +50,8 @@ ip-rule \- routing policy database management
 .IR ACTION " := [ "
 .B  table
 .IR TABLE_ID " ] [ "
+.B  protocol
+.IR PROTO " ] [ "
 .B  nat
 .IR ADDRESS " ] [ "
 .B realms
@@ -240,6 +242,10 @@ The options preference and order are synonyms with priority.
 the routing table identifier to lookup if the rule selector matches.
 It is also possible to use lookup instead of table.
 
+.TP
+.BI protocol " PROTO"
+the routing protocol who installed the rule in question.  As an example when zebra installs a rule it would get RTPROT_ZEBRA as the installing protocol.
+
 .TP
 .BI suppress_prefixlength " NUMBER"
 reject routing decisions that have a prefix length of NUMBER or less.
@@ -275,7 +281,11 @@ updates, it flushes the routing cache with
 .RE
 .TP
 .B ip rule flush - also dumps all the deleted rules.
-This command has no arguments.
+.RS
+.TP
+.BI protocol " PROTO"
+Select the originating protocol.
+.RE
 .TP
 .B ip rule show - list rules
 This command has no arguments.
@@ -283,6 +293,12 @@ The options list or lst are synonyms with show.
 
 .TP
 .B ip rule save
+.RS
+.TP
+.BI protocol " PROTO"
+Select the originating protocol.
+.RE
+.TP
 save rules table information to stdout
 .RS
 This command behaves like
Signature · 1 line
2.14.3
#16 lore raw

PATCH v4 3/3 ip: Allow rules to accept a specified protocol

In reply to Donald Sharp

Allow the specification of a protocol when the user adds/modifies/deletes a rule.

---
 ip/iprule.c | 7 +++++++
 1 file changed, 7 insertions(+)
ip/iprule.c +7 -0
index 8120520e..6fdc9b5e 100644
@@ -697,6 +697,13 @@ static int iprule_modify(int cmd, int argc, char **argv)
 			if (get_rt_realms_or_raw(&realm, *argv))
 				invarg("invalid realms\n", *argv);
 			addattr32(&req.n, sizeof(req), FRA_FLOW, realm);
+		} else if (matches(*argv, "protocol") == 0) {
+			__u32 proto;
+
+			NEXT_ARG();
+			if (rtnl_rtprot_a2n(&proto, *argv))
+				invarg("\"protocol\" value is invalid\n", *argv);
+			addattr8(&req.n, sizeof(req), FRA_PROTOCOL, proto);
 		} else if (matches(*argv, "table") == 0 ||
 			   strcmp(*argv, "lookup") == 0) {
 			NEXT_ARG();
Signature · 1 line
2.14.3
Applied
#13 lore raw
In reply to Donald Sharp
Quoted 13 lines · Donald Sharp

Fix iprule.c to use the actual struct fib_rule_hdr and to allow the end user to see and use the protocol keyword for rule manipulation.

v2: Rearrange and code changes as per David Ahern
v3: Fix some missed RTN_XXX to appropriate FR_XX and doc changes
v4: Cleanup some code, fix 'ip rule save' no parameters and doc changes

Donald Sharp (3):
ip: Use the struct fib_rule_hdr for rules
ip: Display ip rule protocol used
ip: Allow rules to accept a specified protocol

Quoted 5 lines
 include/uapi/linux/fib_rules.h |   1 +
 ip/iprule.c                    | 173 +++++++++++++++++++++++++----------------
 man/man8/ip-rule.8             |  18 ++++-
 3 files changed, 126 insertions(+), 66 deletions(-)

Applied to iproute2-next.

Please submit a followup for listing with the protocol as a filter.