naccyde / rpms / iproute

Forked from rpms/iproute 9 months ago
Clone

Blame SOURCES/0132-tc-flower-Support-matching-ARP.patch

4aca6e
From 8b5f12cb701561aea1e7ece9267719bde085d328 Mon Sep 17 00:00:00 2001
4aca6e
From: Phil Sutter <psutter@redhat.com>
4aca6e
Date: Fri, 17 Mar 2017 13:24:17 +0100
4aca6e
Subject: [PATCH] tc: flower: Support matching ARP
4aca6e
4aca6e
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1422629
4aca6e
Upstream Status: iproute2.git commit f888f4e20534a
4aca6e
Conflicts: Context changes due to previously backported commits.
4aca6e
4aca6e
commit f888f4e20534ae44ec4c5a2cfc5209b105645a04
4aca6e
Author: Simon Horman <simon.horman@netronome.com>
4aca6e
Date:   Thu Jan 12 09:11:59 2017 +0100
4aca6e
4aca6e
    tc: flower: Support matching ARP
4aca6e
4aca6e
    Support matching on ARP operation, and hardware and protocol addresses
4aca6e
    for Ethernet hardware and IPv4 protocol addresses.
4aca6e
4aca6e
    Example usage:
4aca6e
4aca6e
    tc qdisc add dev eth0 ingress
4aca6e
4aca6e
    tc filter add dev eth0 protocol arp parent ffff: flower indev eth0 \                    arp_op request arp_sip 10.0.0.1 action drop
4aca6e
    tc filter add dev eth0 protocol rarp parent ffff: flower indev eth0 \                   arp_op reply arp_tha 52:54:3f:00:00:00/24 action drop
4aca6e
4aca6e
    Signed-off-by: Simon Horman <simon.horman@netronome.com>
4aca6e
---
4aca6e
 man/man8/tc-flower.8 |  41 +++++++++-
4aca6e
 tc/f_flower.c        | 208 +++++++++++++++++++++++++++++++++++++++++++++++----
4aca6e
 2 files changed, 232 insertions(+), 17 deletions(-)
4aca6e
4aca6e
diff --git a/man/man8/tc-flower.8 b/man/man8/tc-flower.8
4aca6e
index 5904a9e..2dd2c5e 100644
4aca6e
--- a/man/man8/tc-flower.8
4aca6e
+++ b/man/man8/tc-flower.8
4aca6e
@@ -34,7 +34,13 @@ flower \- flow based traffic control filter
4aca6e
 .BR dst_ip " | " src_ip " } "
4aca6e
 .IR PREFIX " | { "
4aca6e
 .BR dst_port " | " src_port " } "
4aca6e
-.IR port_number " } | "
4aca6e
+.IR port_number " } | { "
4aca6e
+.BR arp_tip " | " arp_sip " } "
4aca6e
+.IR PREFIX " | "
4aca6e
+.BR arp_op " { " request " | " reply " | "
4aca6e
+.IR OP " } | { "
4aca6e
+.BR arp_tha " | " arp_sha " } "
4aca6e
+.IR MASKED_LLADDR " | "
4aca6e
 .B enc_key_id
4aca6e
 .IR KEY-ID " | {"
4aca6e
 .BR enc_dst_ip " | " enc_src_ip " } { "
4aca6e
@@ -131,6 +137,36 @@ Match on ICMP type or code. Only available for
4aca6e
 .BR ip_proto " values " icmp  " and " icmpv6
4aca6e
 which have to be specified in beforehand.
4aca6e
 .TP
4aca6e
+.BI arp_tip " PREFIX"
4aca6e
+.TQ
4aca6e
+.BI arp_sip " PREFIX"
4aca6e
+Match on ARP or RARP sender or target IP address.
4aca6e
+.I PREFIX
4aca6e
+must be a valid IPv4 address optionally followed by a slash and the prefix
4aca6e
+length. If the prefix is missing, \fBtc\fR assumes a full-length host
4aca6e
+match.
4aca6e
+.TP
4aca6e
+.BI arp_op " ARP_OP"
4aca6e
+Match on ARP or RARP operation.
4aca6e
+.I ARP_OP
4aca6e
+may be
4aca6e
+.BR request ", " reply
4aca6e
+or an integer value 0, 1 or 2.  A mask may be optionally provided to limit
4aca6e
+the bits of the operation which are matched. A mask is provided by
4aca6e
+following the address with a slash and then the mask. It may be provided as
4aca6e
+an unsigned 8 bit value representing a bitwise mask. If the mask is missing
4aca6e
+then a match on all bits is assumed.
4aca6e
+.TP
4aca6e
+.BI arp_sha " MASKED_LLADDR"
4aca6e
+.TQ
4aca6e
+.BI arp_tha " MASKED_LLADDR"
4aca6e
+Match on ARP or RARP sender or target MAC address.  A mask may be optionally
4aca6e
+provided to limit the bits of the address which are matched. A mask is
4aca6e
+provided by following the address with a slash and then the mask. It may be
4aca6e
+provided in LLADDR format, in which case it is a bitwise mask, or as a
4aca6e
+number of high bits to match. If the mask is missing then a match on all
4aca6e
+bits is assumed.
4aca6e
+.TP
4aca6e
 .BI enc_key_id " NUMBER"
4aca6e
 .TQ
4aca6e
 .BI enc_dst_ip " PREFIX"
4aca6e
@@ -152,7 +188,8 @@ As stated above where applicable, matches of a certain layer implicitly depend
4aca6e
 on the matches of the next lower layer. Precisely, layer one and two matches
4aca6e
 (\fBindev\fR,  \fBdst_mac\fR and \fBsrc_mac\fR)
4aca6e
 have no dependency, layer three matches
4aca6e
-(\fBip_proto\fR, \fBdst_ip\fR and \fBsrc_ip\fR)
4aca6e
+(\fBip_proto\fR, \fBdst_ip\fR, \fBsrc_ip\fR, \fBarp_tip\fR, \fBarp_sip\fR,
4aca6e
+\fBarp_op\fR, \fBarp_tha\fR and \fBarp_sha\fR)
4aca6e
 depend on the
4aca6e
 .B protocol
4aca6e
 option of tc filter, layer four port matches
4aca6e
diff --git a/tc/f_flower.c b/tc/f_flower.c
4aca6e
index 20f9a85..2473aea 100644
4aca6e
--- a/tc/f_flower.c
4aca6e
+++ b/tc/f_flower.c
4aca6e
@@ -15,6 +15,7 @@
4aca6e
 #include <syslog.h>
4aca6e
 #include <string.h>
4aca6e
 #include <net/if.h>
4aca6e
+#include <linux/if_arp.h>
4aca6e
 #include <linux/if_ether.h>
4aca6e
 #include <linux/ip.h>
4aca6e
 #include <linux/tc_act/tc_vlan.h>
4aca6e
@@ -54,6 +55,11 @@ static void explain(void)
4aca6e
 		"                       src_port PORT-NUMBER |\n"
4aca6e
 		"                       type ICMP-TYPE |\n"
4aca6e
 		"                       code ICMP-CODE |\n"
4aca6e
+		"                       arp_tip PREFIX |\n"
4aca6e
+		"                       arp_sip PREFIX |\n"
4aca6e
+		"                       arp_op [ request | reply | OP ] |\n"
4aca6e
+		"                       arp_tha MASKED-LLADDR |\n"
4aca6e
+		"                       arp_sha MASKED-LLADDR |\n"
4aca6e
 		"                       enc_dst_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
4aca6e
 		"                       enc_src_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
4aca6e
 		"                       enc_key_id [ KEY-ID ] |\n"
4aca6e
@@ -192,27 +198,16 @@ err:
4aca6e
 	return -1;
4aca6e
 }
4aca6e
 
4aca6e
-static int flower_parse_ip_addr(char *str, __be16 eth_type,
4aca6e
-				int addr4_type, int mask4_type,
4aca6e
-				int addr6_type, int mask6_type,
4aca6e
-				struct nlmsghdr *n)
4aca6e
+static int __flower_parse_ip_addr(char *str, int family,
4aca6e
+				  int addr4_type, int mask4_type,
4aca6e
+				  int addr6_type, int mask6_type,
4aca6e
+				  struct nlmsghdr *n)
4aca6e
 {
4aca6e
 	int ret;
4aca6e
 	inet_prefix addr;
4aca6e
-	int family;
4aca6e
 	int bits;
4aca6e
 	int i;
4aca6e
 
4aca6e
-	if (eth_type == htons(ETH_P_IP)) {
4aca6e
-		family = AF_INET;
4aca6e
-	} else if (eth_type == htons(ETH_P_IPV6)) {
4aca6e
-		family = AF_INET6;
4aca6e
-	} else if (!eth_type) {
4aca6e
-		family = AF_UNSPEC;
4aca6e
-	} else {
4aca6e
-		return -1;
4aca6e
-	}
4aca6e
-
4aca6e
 	ret = get_prefix(&addr, str, family);
4aca6e
 	if (ret)
4aca6e
 		return -1;
4aca6e
@@ -245,6 +240,89 @@ static int flower_parse_ip_addr(char *str, __be16 eth_type,
4aca6e
 	return 0;
4aca6e
 }
4aca6e
 
4aca6e
+static int flower_parse_ip_addr(char *str, __be16 eth_type,
4aca6e
+				int addr4_type, int mask4_type,
4aca6e
+				int addr6_type, int mask6_type,
4aca6e
+				struct nlmsghdr *n)
4aca6e
+{
4aca6e
+	int family;
4aca6e
+
4aca6e
+	if (eth_type == htons(ETH_P_IP)) {
4aca6e
+		family = AF_INET;
4aca6e
+	} else if (eth_type == htons(ETH_P_IPV6)) {
4aca6e
+		family = AF_INET6;
4aca6e
+	} else if (!eth_type) {
4aca6e
+		family = AF_UNSPEC;
4aca6e
+	} else {
4aca6e
+		return -1;
4aca6e
+	}
4aca6e
+
4aca6e
+	return __flower_parse_ip_addr(str, family, addr4_type, addr6_type,
4aca6e
+				      mask4_type, mask6_type, n);
4aca6e
+}
4aca6e
+
4aca6e
+static bool flower_eth_type_arp(__be16 eth_type)
4aca6e
+{
4aca6e
+	return eth_type == htons(ETH_P_ARP) || eth_type == htons(ETH_P_RARP);
4aca6e
+}
4aca6e
+
4aca6e
+static int flower_parse_arp_ip_addr(char *str, __be16 eth_type,
4aca6e
+				    int addr_type, int mask_type,
4aca6e
+				    struct nlmsghdr *n)
4aca6e
+{
4aca6e
+	if (!flower_eth_type_arp(eth_type))
4aca6e
+		return -1;
4aca6e
+
4aca6e
+	return __flower_parse_ip_addr(str, AF_INET, addr_type, mask_type,
4aca6e
+				      TCA_FLOWER_UNSPEC, TCA_FLOWER_UNSPEC, n);
4aca6e
+}
4aca6e
+
4aca6e
+static int flower_parse_arp_op(char *str, __be16 eth_type,
4aca6e
+			       int op_type, int mask_type,
4aca6e
+			       struct nlmsghdr *n)
4aca6e
+{
4aca6e
+	char *slash;
4aca6e
+	int ret, err = -1;
4aca6e
+	uint8_t value, mask;
4aca6e
+
4aca6e
+	slash = strchr(str, '/');
4aca6e
+	if (slash)
4aca6e
+		*slash = '\0';
4aca6e
+
4aca6e
+	if (!flower_eth_type_arp(eth_type))
4aca6e
+		goto err;
4aca6e
+
4aca6e
+	if (!strcmp(str, "request")) {
4aca6e
+		value = ARPOP_REQUEST;
4aca6e
+	} else if (!strcmp(str, "reply")) {
4aca6e
+		value = ARPOP_REPLY;
4aca6e
+	} else {
4aca6e
+		ret = get_u8(&value, str, 10);
4aca6e
+		if (ret)
4aca6e
+			goto err;
4aca6e
+		if (value && value != ARPOP_REQUEST && value != ARPOP_REPLY)
4aca6e
+			goto err;
4aca6e
+	}
4aca6e
+
4aca6e
+	if (slash) {
4aca6e
+		ret = get_u8(&mask, slash + 1, 10);
4aca6e
+		if (ret)
4aca6e
+			goto err;
4aca6e
+	}
4aca6e
+	else {
4aca6e
+		mask = UINT8_MAX;
4aca6e
+	}
4aca6e
+
4aca6e
+	addattr8(n, MAX_MSG, op_type, value);
4aca6e
+	addattr8(n, MAX_MSG, mask_type, mask);
4aca6e
+
4aca6e
+	err = 0;
4aca6e
+err:
4aca6e
+	if (slash)
4aca6e
+		*slash = '/';
4aca6e
+	return err;
4aca6e
+}
4aca6e
+
4aca6e
 static int flower_icmp_attr_type(__be16 eth_type, __u8 ip_proto,
4aca6e
 				 enum flower_icmp_field field)
4aca6e
 {
4aca6e
@@ -531,6 +609,59 @@ static int flower_parse_opt(struct filter_util *qu, char *handle,
4aca6e
 				fprintf(stderr, "Illegal \"icmp code\"\n");
4aca6e
 				return -1;
4aca6e
 			}
4aca6e
+		} else if (matches(*argv, "arp_tip") == 0) {
4aca6e
+			NEXT_ARG();
4aca6e
+			ret = flower_parse_arp_ip_addr(*argv, vlan_ethtype ?
4aca6e
+						       vlan_ethtype : eth_type,
4aca6e
+						       TCA_FLOWER_KEY_ARP_TIP,
4aca6e
+						       TCA_FLOWER_KEY_ARP_TIP_MASK,
4aca6e
+						       n);
4aca6e
+			if (ret < 0) {
4aca6e
+				fprintf(stderr, "Illegal \"arp_tip\"\n");
4aca6e
+				return -1;
4aca6e
+			}
4aca6e
+		} else if (matches(*argv, "arp_sip") == 0) {
4aca6e
+			NEXT_ARG();
4aca6e
+			ret = flower_parse_arp_ip_addr(*argv, vlan_ethtype ?
4aca6e
+						       vlan_ethtype : eth_type,
4aca6e
+						       TCA_FLOWER_KEY_ARP_SIP,
4aca6e
+						       TCA_FLOWER_KEY_ARP_SIP_MASK,
4aca6e
+						       n);
4aca6e
+			if (ret < 0) {
4aca6e
+				fprintf(stderr, "Illegal \"arp_sip\"\n");
4aca6e
+				return -1;
4aca6e
+			}
4aca6e
+		} else if (matches(*argv, "arp_op") == 0) {
4aca6e
+			NEXT_ARG();
4aca6e
+			ret = flower_parse_arp_op(*argv, vlan_ethtype ?
4aca6e
+						  vlan_ethtype : eth_type,
4aca6e
+						  TCA_FLOWER_KEY_ARP_OP,
4aca6e
+						  TCA_FLOWER_KEY_ARP_OP_MASK,
4aca6e
+						  n);
4aca6e
+			if (ret < 0) {
4aca6e
+				fprintf(stderr, "Illegal \"arp_op\"\n");
4aca6e
+				return -1;
4aca6e
+			}
4aca6e
+		} else if (matches(*argv, "arp_tha") == 0) {
4aca6e
+			NEXT_ARG();
4aca6e
+			ret = flower_parse_eth_addr(*argv,
4aca6e
+						    TCA_FLOWER_KEY_ARP_THA,
4aca6e
+						    TCA_FLOWER_KEY_ARP_THA_MASK,
4aca6e
+						    n);
4aca6e
+			if (ret < 0) {
4aca6e
+				fprintf(stderr, "Illegal \"arp_tha\"\n");
4aca6e
+				return -1;
4aca6e
+			}
4aca6e
+		} else if (matches(*argv, "arp_sha") == 0) {
4aca6e
+			NEXT_ARG();
4aca6e
+			ret = flower_parse_eth_addr(*argv,
4aca6e
+						    TCA_FLOWER_KEY_ARP_SHA,
4aca6e
+						    TCA_FLOWER_KEY_ARP_SHA_MASK,
4aca6e
+						    n);
4aca6e
+			if (ret < 0) {
4aca6e
+				fprintf(stderr, "Illegal \"arp_sha\"\n");
4aca6e
+				return -1;
4aca6e
+			}
4aca6e
 		} else if (matches(*argv, "enc_dst_ip") == 0) {
4aca6e
 			NEXT_ARG();
4aca6e
 			ret = flower_parse_ip_addr(*argv, 0,
4aca6e
@@ -662,6 +793,10 @@ static void flower_print_eth_type(FILE *f, __be16 *p_eth_type,
4aca6e
 		fprintf(f, "ipv4");
4aca6e
 	else if (eth_type == htons(ETH_P_IPV6))
4aca6e
 		fprintf(f, "ipv6");
4aca6e
+	else if (eth_type == htons(ETH_P_ARP))
4aca6e
+		fprintf(f, "arp");
4aca6e
+	else if (eth_type == htons(ETH_P_RARP))
4aca6e
+		fprintf(f, "rarp");
4aca6e
 	else
4aca6e
 		fprintf(f, "%04x", ntohs(eth_type));
4aca6e
 	*p_eth_type = eth_type;
4aca6e
@@ -739,6 +874,13 @@ static void flower_print_ip_addr(FILE *f, char *name, __be16 eth_type,
4aca6e
 	else if (bits < len * 8)
4aca6e
 		fprintf(f, "/%d", bits);
4aca6e
 }
4aca6e
+static void flower_print_ip4_addr(FILE *f, char *name,
4aca6e
+				  struct rtattr *addr_attr,
4aca6e
+				  struct rtattr *mask_attr)
4aca6e
+{
4aca6e
+	return flower_print_ip_addr(f, name, htons(ETH_P_IP),
4aca6e
+				    addr_attr, mask_attr, 0, 0);
4aca6e
+}
4aca6e
 
4aca6e
 static void flower_print_port(FILE *f, char *name, struct rtattr *attr)
4aca6e
 {
4aca6e
@@ -759,6 +901,31 @@ static void flower_print_icmp(FILE *f, char *name, struct rtattr *attr)
4aca6e
 		fprintf(f, "\n  %s %d", name, rta_getattr_u8(attr));
4aca6e
 }
4aca6e
 
4aca6e
+static void flower_print_arp_op(FILE *f, char *name,
4aca6e
+				struct rtattr *op_attr,
4aca6e
+				struct rtattr *mask_attr)
4aca6e
+{
4aca6e
+	uint8_t op, mask;
4aca6e
+
4aca6e
+	if (!op_attr)
4aca6e
+		return;
4aca6e
+
4aca6e
+	op = rta_getattr_u8(op_attr);
4aca6e
+	mask = mask_attr ? rta_getattr_u8(mask_attr) : UINT8_MAX;
4aca6e
+
4aca6e
+	fprintf(f, "\n  %s ", name);
4aca6e
+
4aca6e
+	if (mask == UINT8_MAX && op == ARPOP_REQUEST)
4aca6e
+		fprintf(f, "request");
4aca6e
+	else if (mask == UINT8_MAX && op == ARPOP_REPLY)
4aca6e
+		fprintf(f, "reply");
4aca6e
+	else
4aca6e
+		fprintf(f, "%d", op);
4aca6e
+
4aca6e
+	if (mask != UINT8_MAX)
4aca6e
+		fprintf(f, "/%d", mask);
4aca6e
+}
4aca6e
+
4aca6e
 static int flower_print_opt(struct filter_util *qu, FILE *f,
4aca6e
 			    struct rtattr *opt, __u32 handle)
4aca6e
 {
4aca6e
@@ -834,6 +1001,17 @@ static int flower_print_opt(struct filter_util *qu, FILE *f,
4aca6e
 	if (nl_type >= 0)
4aca6e
 		flower_print_icmp(f, "icmp_code", tb[nl_type]);
4aca6e
 
4aca6e
+	flower_print_ip4_addr(f, "arp_sip", tb[TCA_FLOWER_KEY_ARP_SIP],
4aca6e
+			     tb[TCA_FLOWER_KEY_ARP_SIP_MASK]);
4aca6e
+	flower_print_ip4_addr(f, "arp_tip", tb[TCA_FLOWER_KEY_ARP_TIP],
4aca6e
+			     tb[TCA_FLOWER_KEY_ARP_TIP_MASK]);
4aca6e
+	flower_print_arp_op(f, "arp_op", tb[TCA_FLOWER_KEY_ARP_OP],
4aca6e
+			    tb[TCA_FLOWER_KEY_ARP_OP_MASK]);
4aca6e
+	flower_print_eth_addr(f, "arp_sha", tb[TCA_FLOWER_KEY_ARP_SHA],
4aca6e
+			      tb[TCA_FLOWER_KEY_ARP_SHA_MASK]);
4aca6e
+	flower_print_eth_addr(f, "arp_tha", tb[TCA_FLOWER_KEY_ARP_THA],
4aca6e
+			      tb[TCA_FLOWER_KEY_ARP_THA_MASK]);
4aca6e
+
4aca6e
 	flower_print_ip_addr(f, "enc_dst_ip",
4aca6e
 			     tb[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] ?
4aca6e
 			     htons(ETH_P_IP) : htons(ETH_P_IPV6),
4aca6e
-- 
4aca6e
1.8.3.1
4aca6e