naccyde / rpms / iproute

Forked from rpms/iproute 9 months ago
Clone

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

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