naccyde / rpms / iproute

Forked from rpms/iproute 5 months ago
Clone

Blame SOURCES/0038-ss-allow-AF_FAMILY-constants-32.patch

cd1737
From f59533eb3cb188a23456444aeb19ac3634eddd8c Mon Sep 17 00:00:00 2001
cd1737
From: Stefano Brivio <sbrivio@redhat.com>
cd1737
Date: Sun, 22 Oct 2017 21:44:26 +0200
cd1737
Subject: [PATCH] ss: allow AF_FAMILY constants >32
cd1737
cd1737
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1472759
cd1737
Upstream Status: iproute2.git commit b338a3e7e7d9
cd1737
cd1737
commit b338a3e7e7d95c9d46de9748604da06287664033
cd1737
Author: Stefan Hajnoczi <stefanha@redhat.com>
cd1737
Date:   Fri Oct 6 11:48:39 2017 -0400
cd1737
cd1737
    ss: allow AF_FAMILY constants >32
cd1737
cd1737
    Linux has more than 32 address families defined in <bits/socket.h>.  Use
cd1737
    a 64-bit type so all of them can be represented in the filter->families
cd1737
    bitmask.
cd1737
cd1737
    It's easy to introduce bugs when using (1 << AF_FAMILY) because the
cd1737
    value is 32-bit.  This can produce incorrect results from bitmask
cd1737
    operations so introduce the FAMILY_MASK() macro to eliminate these bugs.
cd1737
cd1737
    Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
cd1737
cd1737
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
cd1737
---
cd1737
 misc/ss.c | 54 ++++++++++++++++++++++++++++--------------------------
cd1737
 1 file changed, 28 insertions(+), 26 deletions(-)
cd1737
cd1737
diff --git a/misc/ss.c b/misc/ss.c
cd1737
index d3fb9a7..0d64527 100644
cd1737
--- a/misc/ss.c
cd1737
+++ b/misc/ss.c
cd1737
@@ -170,55 +170,57 @@ enum {
cd1737
 struct filter {
cd1737
 	int dbs;
cd1737
 	int states;
cd1737
-	int families;
cd1737
+	uint64_t families;
cd1737
 	struct ssfilter *f;
cd1737
 	bool kill;
cd1737
 };
cd1737
 
cd1737
+#define FAMILY_MASK(family) ((uint64_t)1 << (family))
cd1737
+
cd1737
 static const struct filter default_dbs[MAX_DB] = {
cd1737
 	[TCP_DB] = {
cd1737
 		.states   = SS_CONN,
cd1737
-		.families = (1 << AF_INET) | (1 << AF_INET6),
cd1737
+		.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
cd1737
 	},
cd1737
 	[DCCP_DB] = {
cd1737
 		.states   = SS_CONN,
cd1737
-		.families = (1 << AF_INET) | (1 << AF_INET6),
cd1737
+		.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
cd1737
 	},
cd1737
 	[UDP_DB] = {
cd1737
 		.states   = (1 << SS_ESTABLISHED),
cd1737
-		.families = (1 << AF_INET) | (1 << AF_INET6),
cd1737
+		.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
cd1737
 	},
cd1737
 	[RAW_DB] = {
cd1737
 		.states   = (1 << SS_ESTABLISHED),
cd1737
-		.families = (1 << AF_INET) | (1 << AF_INET6),
cd1737
+		.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
cd1737
 	},
cd1737
 	[UNIX_DG_DB] = {
cd1737
 		.states   = (1 << SS_CLOSE),
cd1737
-		.families = (1 << AF_UNIX),
cd1737
+		.families = FAMILY_MASK(AF_UNIX),
cd1737
 	},
cd1737
 	[UNIX_ST_DB] = {
cd1737
 		.states   = SS_CONN,
cd1737
-		.families = (1 << AF_UNIX),
cd1737
+		.families = FAMILY_MASK(AF_UNIX),
cd1737
 	},
cd1737
 	[UNIX_SQ_DB] = {
cd1737
 		.states   = SS_CONN,
cd1737
-		.families = (1 << AF_UNIX),
cd1737
+		.families = FAMILY_MASK(AF_UNIX),
cd1737
 	},
cd1737
 	[PACKET_DG_DB] = {
cd1737
 		.states   = (1 << SS_CLOSE),
cd1737
-		.families = (1 << AF_PACKET),
cd1737
+		.families = FAMILY_MASK(AF_PACKET),
cd1737
 	},
cd1737
 	[PACKET_R_DB] = {
cd1737
 		.states   = (1 << SS_CLOSE),
cd1737
-		.families = (1 << AF_PACKET),
cd1737
+		.families = FAMILY_MASK(AF_PACKET),
cd1737
 	},
cd1737
 	[NETLINK_DB] = {
cd1737
 		.states   = (1 << SS_CLOSE),
cd1737
-		.families = (1 << AF_NETLINK),
cd1737
+		.families = FAMILY_MASK(AF_NETLINK),
cd1737
 	},
cd1737
 	[SCTP_DB] = {
cd1737
 		.states   = SS_CONN,
cd1737
-		.families = (1 << AF_INET) | (1 << AF_INET6),
cd1737
+		.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
cd1737
 	},
cd1737
 };
cd1737
 
cd1737
@@ -258,14 +260,14 @@ static void filter_db_set(struct filter *f, int db)
cd1737
 static void filter_af_set(struct filter *f, int af)
cd1737
 {
cd1737
 	f->states	   |= default_afs[af].states;
cd1737
-	f->families	   |= 1 << af;
cd1737
+	f->families	   |= FAMILY_MASK(af);
cd1737
 	do_default	    = 0;
cd1737
 	preferred_family    = af;
cd1737
 }
cd1737
 
cd1737
 static int filter_af_get(struct filter *f, int af)
cd1737
 {
cd1737
-	return f->families & (1 << af);
cd1737
+	return !!(f->families & FAMILY_MASK(af));
cd1737
 }
cd1737
 
cd1737
 static void filter_default_dbs(struct filter *f)
cd1737
@@ -302,7 +304,7 @@ static void filter_merge_defaults(struct filter *f)
cd1737
 			f->families |= default_dbs[db].families;
cd1737
 	}
cd1737
 	for (af = 0; af < AF_MAX; af++) {
cd1737
-		if (!(f->families & (1 << af)))
cd1737
+		if (!(f->families & FAMILY_MASK(af)))
cd1737
 			continue;
cd1737
 
cd1737
 		if (!(default_afs[af].dbs & f->dbs))
cd1737
@@ -2599,7 +2601,7 @@ static int show_one_inet_sock(const struct sockaddr_nl *addr,
cd1737
 	struct inet_diag_msg *r = NLMSG_DATA(h);
cd1737
 	struct sockstat s = {};
cd1737
 
cd1737
-	if (!(diag_arg->f->families & (1 << r->idiag_family)))
cd1737
+	if (!(diag_arg->f->families & FAMILY_MASK(r->idiag_family)))
cd1737
 		return 0;
cd1737
 
cd1737
 	parse_diag_msg(h, &s);
cd1737
@@ -2785,7 +2787,7 @@ static int tcp_show(struct filter *f)
cd1737
 		return -1;
cd1737
 	}
cd1737
 
cd1737
-	if (f->families & (1<
cd1737
+	if (f->families & FAMILY_MASK(AF_INET)) {
cd1737
 		if ((fp = net_tcp_open()) == NULL)
cd1737
 			goto outerr;
cd1737
 
cd1737
@@ -2795,7 +2797,7 @@ static int tcp_show(struct filter *f)
cd1737
 		fclose(fp);
cd1737
 	}
cd1737
 
cd1737
-	if ((f->families & (1<
cd1737
+	if ((f->families & FAMILY_MASK(AF_INET6)) &&
cd1737
 	    (fp = net_tcp6_open()) != NULL) {
cd1737
 		setbuffer(fp, buf, bufsize);
cd1737
 		if (generic_record_read(fp, tcp_show_line, f, AF_INET6))
cd1737
@@ -2894,7 +2896,7 @@ static int udp_show(struct filter *f)
cd1737
 	    && inet_show_netlink(f, NULL, IPPROTO_UDP) == 0)
cd1737
 		return 0;
cd1737
 
cd1737
-	if (f->families&(1<
cd1737
+	if (f->families&FAMILY_MASK(AF_INET)) {
cd1737
 		if ((fp = net_udp_open()) == NULL)
cd1737
 			goto outerr;
cd1737
 		if (generic_record_read(fp, dgram_show_line, f, AF_INET))
cd1737
@@ -2902,7 +2904,7 @@ static int udp_show(struct filter *f)
cd1737
 		fclose(fp);
cd1737
 	}
cd1737
 
cd1737
-	if ((f->families&(1<
cd1737
+	if ((f->families&FAMILY_MASK(AF_INET6)) &&
cd1737
 	    (fp = net_udp6_open()) != NULL) {
cd1737
 		if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
cd1737
 			goto outerr;
cd1737
@@ -2934,7 +2936,7 @@ static int raw_show(struct filter *f)
cd1737
 	    inet_show_netlink(f, NULL, IPPROTO_RAW) == 0)
cd1737
 		return 0;
cd1737
 
cd1737
-	if (f->families&(1<
cd1737
+	if (f->families&FAMILY_MASK(AF_INET)) {
cd1737
 		if ((fp = net_raw_open()) == NULL)
cd1737
 			goto outerr;
cd1737
 		if (generic_record_read(fp, dgram_show_line, f, AF_INET))
cd1737
@@ -2942,7 +2944,7 @@ static int raw_show(struct filter *f)
cd1737
 		fclose(fp);
cd1737
 	}
cd1737
 
cd1737
-	if ((f->families&(1<
cd1737
+	if ((f->families&FAMILY_MASK(AF_INET6)) &&
cd1737
 	    (fp = net_raw6_open()) != NULL) {
cd1737
 		if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
cd1737
 			goto outerr;
cd1737
@@ -3682,13 +3684,13 @@ static int handle_follow_request(struct filter *f)
cd1737
 	int groups = 0;
cd1737
 	struct rtnl_handle rth;
cd1737
 
cd1737
-	if (f->families & (1 << AF_INET) && f->dbs & (1 << TCP_DB))
cd1737
+	if (f->families & FAMILY_MASK(AF_INET) && f->dbs & (1 << TCP_DB))
cd1737
 		groups |= 1 << (SKNLGRP_INET_TCP_DESTROY - 1);
cd1737
-	if (f->families & (1 << AF_INET) && f->dbs & (1 << UDP_DB))
cd1737
+	if (f->families & FAMILY_MASK(AF_INET) && f->dbs & (1 << UDP_DB))
cd1737
 		groups |= 1 << (SKNLGRP_INET_UDP_DESTROY - 1);
cd1737
-	if (f->families & (1 << AF_INET6) && f->dbs & (1 << TCP_DB))
cd1737
+	if (f->families & FAMILY_MASK(AF_INET6) && f->dbs & (1 << TCP_DB))
cd1737
 		groups |= 1 << (SKNLGRP_INET6_TCP_DESTROY - 1);
cd1737
-	if (f->families & (1 << AF_INET6) && f->dbs & (1 << UDP_DB))
cd1737
+	if (f->families & FAMILY_MASK(AF_INET6) && f->dbs & (1 << UDP_DB))
cd1737
 		groups |= 1 << (SKNLGRP_INET6_UDP_DESTROY - 1);
cd1737
 
cd1737
 	if (groups == 0)
cd1737
-- 
cd1737
1.8.3.1
cd1737