Skip to contentSkip to conversation

net: Netconsole port configuration and filtering

PATCH New Joe Hershberger 3 participants 8 messages Aug 17, 2012 – Sep 18, 2012

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

cover net: Netconsole port configuration and filtering

This series depends on this patch:
http://patchwork.ozlabs.org/patch/174282/

Joe Hershberger (2):
net: Allow netconsole settings to change after nc_start
net: Filter incoming netconsole packets by IP

 drivers/net/netconsole.c | 104 ++++++++++++++++++++++++++++++++---------------
 include/net.h            |   3 +-
 net/net.c                |   1 +
 3 files changed, 74 insertions(+), 34 deletions(-)
Signature · 1 line
1.7.11.5
#1 lore raw

PATCH 1/2 net: Allow netconsole settings to change after nc_start

Refresh the netconsole settings from the env before each packet instead of only on netconsole init.

---
 drivers/net/netconsole.c | 96 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 65 insertions(+), 31 deletions(-)
drivers/net/netconsole.c +65 -31
index 86f5301..a31b95c 100644
@@ -60,8 +60,69 @@ static void nc_timeout(void)
 	net_set_state(NETLOOP_SUCCESS);
 }
 
+static int is_broadcast(IPaddr_t ip)
+{
+	static IPaddr_t netmask;
+	static IPaddr_t our_ip;
+	static int env_changed_id;
+	int env_id = get_env_id();
+
+	/* update only when the environment has changed */
+	if (env_changed_id != env_id) {
+		netmask = getenv_IPaddr("netmask");
+		our_ip = getenv_IPaddr("ipaddr");
+
+		env_changed_id = env_id;
+	}
+
+	return (ip == ~0 ||				/* 255.255.255.255 */
+	    ((netmask & our_ip) == (netmask & ip) &&	/* on the same net */
+	    (netmask | ip) == ~0));		/* broadcast to our net */
+}
+
+static int refresh_settings_from_env(void)
+{
+	char *p;
+	static int env_changed_id;
+	int env_id = get_env_id();
+
+	/* update only when the environment has changed */
+	if (env_changed_id != env_id) {
+		if (getenv("ncip")) {
+			nc_ip = getenv_IPaddr("ncip");
+			if (!nc_ip)
+				return -1;	/* ncip is 0.0.0.0 */
+			p = strchr(getenv("ncip"), ':');
+			if (p != NULL) {
+				nc_out_port = simple_strtoul(p + 1, NULL, 10);
+				nc_in_port = nc_out_port;
+			}
+		} else
+			nc_ip = ~0; /* ncip is not set, so broadcast */
+
+		p = getenv("ncoutport");
+		if (p != NULL)
+			nc_out_port = simple_strtoul(p, NULL, 10);
+		p = getenv("ncinport");
+		if (p != NULL)
+			nc_in_port = simple_strtoul(p, NULL, 10);
+
+		if (is_broadcast(nc_ip))
+			/* broadcast MAC address */
+			memset(nc_ether, 0xff, sizeof(nc_ether));
+		else
+			/* force arp request */
+			memset(nc_ether, 0, sizeof(nc_ether));
+	}
+	return 0;
+}
+
+/**
+ * Called from NetLoop in net/net.c before each packet
+ */
 void NcStart(void)
 {
+	refresh_settings_from_env();
 	if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) {
 		/* going to check for input packet */
 		net_set_udp_handler(nc_handler);
@@ -148,41 +209,14 @@ static void nc_send_packet(const char *buf, int len)
 
 static int nc_start(void)
 {
-	int netmask, our_ip;
-	char *p;
+	int retval;
 
 	nc_out_port = 6666; /* default port */
 	nc_in_port = nc_out_port;
 
-	if (getenv("ncip")) {
-
-		nc_ip = getenv_IPaddr("ncip");
-		if (!nc_ip)
-			return -1;	/* ncip is 0.0.0.0 */
-		p = strchr(getenv("ncip"), ':');
-		if (p != NULL) {
-			nc_out_port = simple_strtoul(p + 1, NULL, 10);
-			nc_in_port = nc_out_port;
-		}
-	} else
-		nc_ip = ~0; /* ncip is not set, so broadcast */
-
-	p = getenv("ncoutport");
-	if (p != NULL)
-		nc_out_port = simple_strtoul(p, NULL, 10);
-	p = getenv("ncinport");
-	if (p != NULL)
-		nc_in_port = simple_strtoul(p, NULL, 10);
-
-	our_ip = getenv_IPaddr("ipaddr");
-	netmask = getenv_IPaddr("netmask");
-
-	if (nc_ip == ~0 ||				/* 255.255.255.255 */
-	    ((netmask & our_ip) == (netmask & nc_ip) &&	/* on the same net */
-	    (netmask | nc_ip) == ~0))		/* broadcast to our net */
-		memset(nc_ether, 0xff, sizeof(nc_ether));
-	else
-		memset(nc_ether, 0, sizeof(nc_ether));	/* force arp request */
+	retval = refresh_settings_from_env();
+	if (retval != 0)
+		return retval;
 
 	/*
 	 * Initialize the static IP settings and buffer pointers
Signature · 1 line
1.7.11.5
#2 lore raw

PATCH 1/2 net: Allow netconsole settings to change after nc_start

drivers/net/netconsole.c static void nc_timeout(void) · line 83

Quoted 4 lines · On Friday 17 August 2012 15:48:20 Joe Hershberger wrote:
drivers/net/netconsole.c +1 -0
 
+static int refresh_settings_from_env(void)

this func always returns 0. why not change it to void to simplify the code (source and generated) ?

drivers/net/netconsole.c static void nc_timeout(void) · line 85

Quoted 1 line
drivers/net/netconsole.c +1 -0
+	char *p;

const
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120817/14f09bcf/attachment.pgp>

#3 lore raw

PATCH 1/2 net: Allow netconsole settings to change after nc_start

Hi Mike,

Quoted 8 lines · Mike Frysinger

drivers/net/netconsole.c static void nc_timeout(void) · line 83

Quoted 4 lines · On Friday 17 August 2012 15:48:20 Joe Hershberger wrote:
drivers/net/netconsole.c +1 -0
 
+static int refresh_settings_from_env(void)

this func always returns 0. why not change it to void to simplify the code (source and generated) ?

It returns -1 in the case where ncip == 0 in the environment.

Quoted 3 lines
Quoted 1 line

+ char *p;

const

OK

Thanks,
-Joe

#4 lore raw

PATCH 2/2 net: Filter incoming netconsole packets by IP

In reply to Joe Hershberger

Check the incoming packets' source IP address... if ncip isn't set to a broadcast address, only listen to the client at ncip.

---
 drivers/net/netconsole.c | 8 ++++++--
 include/net.h            | 3 ++-
 net/net.c                | 1 +
 3 files changed, 9 insertions(+), 3 deletions(-)
drivers/net/netconsole.c +6 -2
index a31b95c..975507b 100644
@@ -138,13 +138,17 @@ void NcStart(void)
 	}
 }
 
-int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len)
+int nc_input_packet(uchar *pkt, IPaddr_t src_ip, unsigned dest_port,
+	unsigned src_port, unsigned len)
 {
 	int end, chunk;
 
-	if (dest != nc_in_port || !len)
+	if (dest_port != nc_in_port || !len)
 		return 0; /* not for us */
 
+	if (src_ip != nc_ip && !is_broadcast(nc_ip))
+		return 0; /* not from our client */
+
 	debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
 
 	if (input_size == sizeof(input_buffer))
include/net.h +2 -1
index 6d2d6cd..f0cf2ee 100644
@@ -529,7 +529,8 @@ extern void NetReceive(uchar *, int);
 
 #ifdef CONFIG_NETCONSOLE
 void NcStart(void);
-int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
+int nc_input_packet(uchar *pkt, IPaddr_t src_ip, unsigned dest_port,
+	unsigned src_port, unsigned len);
 #endif
 
 /*
net/net.c +1 -0
index e8ff066..83fcbe5 100644
@@ -1147,6 +1147,7 @@ NetReceive(uchar *inpkt, int len)
 
 #ifdef CONFIG_NETCONSOLE
 		nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
+					ntohl(ip->ip_src),
 					ntohs(ip->udp_dst),
 					ntohs(ip->udp_src),
 					ntohs(ip->udp_len) - UDP_HDR_SIZE);
Signature · 1 line
1.7.11.5
#5 lore raw

cover net: Netconsole port configuration and filtering

In reply to Joe Hershberger

This series depends on this patch:
http://patchwork.ozlabs.org/patch/174282/

Changes in v2:
- Made string pointer const

Joe Hershberger (2):
net: Allow netconsole settings to change after nc_start
net: Filter incoming netconsole packets by IP

 drivers/net/netconsole.c | 104 ++++++++++++++++++++++++++++++++---------------
 include/net.h            |   3 +-
 net/net.c                |   1 +
 3 files changed, 74 insertions(+), 34 deletions(-)
Signature · 1 line
1.7.11.5
#6 lore raw

PATCH 1/2 net: Allow netconsole settings to change after nc_start

Refresh the netconsole settings from the env before each packet instead of only on netconsole init.

---
Changes in v2:
- Made string pointer const

 drivers/net/netconsole.c | 96 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 65 insertions(+), 31 deletions(-)
drivers/net/netconsole.c +65 -31
index 86f5301..2e25fd5 100644
@@ -60,8 +60,69 @@ static void nc_timeout(void)
 	net_set_state(NETLOOP_SUCCESS);
 }
 
+static int is_broadcast(IPaddr_t ip)
+{
+	static IPaddr_t netmask;
+	static IPaddr_t our_ip;
+	static int env_changed_id;
+	int env_id = get_env_id();
+
+	/* update only when the environment has changed */
+	if (env_changed_id != env_id) {
+		netmask = getenv_IPaddr("netmask");
+		our_ip = getenv_IPaddr("ipaddr");
+
+		env_changed_id = env_id;
+	}
+
+	return (ip == ~0 ||				/* 255.255.255.255 */
+	    ((netmask & our_ip) == (netmask & ip) &&	/* on the same net */
+	    (netmask | ip) == ~0));		/* broadcast to our net */
+}
+
+static int refresh_settings_from_env(void)
+{
+	const char *p;
+	static int env_changed_id;
+	int env_id = get_env_id();
+
+	/* update only when the environment has changed */
+	if (env_changed_id != env_id) {
+		if (getenv("ncip")) {
+			nc_ip = getenv_IPaddr("ncip");
+			if (!nc_ip)
+				return -1;	/* ncip is 0.0.0.0 */
+			p = strchr(getenv("ncip"), ':');
+			if (p != NULL) {
+				nc_out_port = simple_strtoul(p + 1, NULL, 10);
+				nc_in_port = nc_out_port;
+			}
+		} else
+			nc_ip = ~0; /* ncip is not set, so broadcast */
+
+		p = getenv("ncoutport");
+		if (p != NULL)
+			nc_out_port = simple_strtoul(p, NULL, 10);
+		p = getenv("ncinport");
+		if (p != NULL)
+			nc_in_port = simple_strtoul(p, NULL, 10);
+
+		if (is_broadcast(nc_ip))
+			/* broadcast MAC address */
+			memset(nc_ether, 0xff, sizeof(nc_ether));
+		else
+			/* force arp request */
+			memset(nc_ether, 0, sizeof(nc_ether));
+	}
+	return 0;
+}
+
+/**
+ * Called from NetLoop in net/net.c before each packet
+ */
 void NcStart(void)
 {
+	refresh_settings_from_env();
 	if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) {
 		/* going to check for input packet */
 		net_set_udp_handler(nc_handler);
@@ -148,41 +209,14 @@ static void nc_send_packet(const char *buf, int len)
 
 static int nc_start(void)
 {
-	int netmask, our_ip;
-	char *p;
+	int retval;
 
 	nc_out_port = 6666; /* default port */
 	nc_in_port = nc_out_port;
 
-	if (getenv("ncip")) {
-
-		nc_ip = getenv_IPaddr("ncip");
-		if (!nc_ip)
-			return -1;	/* ncip is 0.0.0.0 */
-		p = strchr(getenv("ncip"), ':');
-		if (p != NULL) {
-			nc_out_port = simple_strtoul(p + 1, NULL, 10);
-			nc_in_port = nc_out_port;
-		}
-	} else
-		nc_ip = ~0; /* ncip is not set, so broadcast */
-
-	p = getenv("ncoutport");
-	if (p != NULL)
-		nc_out_port = simple_strtoul(p, NULL, 10);
-	p = getenv("ncinport");
-	if (p != NULL)
-		nc_in_port = simple_strtoul(p, NULL, 10);
-
-	our_ip = getenv_IPaddr("ipaddr");
-	netmask = getenv_IPaddr("netmask");
-
-	if (nc_ip == ~0 ||				/* 255.255.255.255 */
-	    ((netmask & our_ip) == (netmask & nc_ip) &&	/* on the same net */
-	    (netmask | nc_ip) == ~0))		/* broadcast to our net */
-		memset(nc_ether, 0xff, sizeof(nc_ether));
-	else
-		memset(nc_ether, 0, sizeof(nc_ether));	/* force arp request */
+	retval = refresh_settings_from_env();
+	if (retval != 0)
+		return retval;
 
 	/*
 	 * Initialize the static IP settings and buffer pointers
Signature · 1 line
1.7.11.5
#7 lore raw

PATCH 2/2 net: Filter incoming netconsole packets by IP

In reply to Joe Hershberger

Check the incoming packets' source IP address... if ncip isn't set to a broadcast address, only listen to the client at ncip.

---

 drivers/net/netconsole.c | 8 ++++++--
 include/net.h            | 3 ++-
 net/net.c                | 1 +
 3 files changed, 9 insertions(+), 3 deletions(-)
drivers/net/netconsole.c +6 -2
index 2e25fd5..b0cc81c 100644
@@ -138,13 +138,17 @@ void NcStart(void)
 	}
 }
 
-int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len)
+int nc_input_packet(uchar *pkt, IPaddr_t src_ip, unsigned dest_port,
+	unsigned src_port, unsigned len)
 {
 	int end, chunk;
 
-	if (dest != nc_in_port || !len)
+	if (dest_port != nc_in_port || !len)
 		return 0; /* not for us */
 
+	if (src_ip != nc_ip && !is_broadcast(nc_ip))
+		return 0; /* not from our client */
+
 	debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
 
 	if (input_size == sizeof(input_buffer))
include/net.h +2 -1
index 6d2d6cd..f0cf2ee 100644
@@ -529,7 +529,8 @@ extern void NetReceive(uchar *, int);
 
 #ifdef CONFIG_NETCONSOLE
 void NcStart(void);
-int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
+int nc_input_packet(uchar *pkt, IPaddr_t src_ip, unsigned dest_port,
+	unsigned src_port, unsigned len);
 #endif
 
 /*
net/net.c +1 -0
index e8ff066..83fcbe5 100644
@@ -1147,6 +1147,7 @@ NetReceive(uchar *inpkt, int len)
 
 #ifdef CONFIG_NETCONSOLE
 		nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
+					ntohl(ip->ip_src),
 					ntohs(ip->udp_dst),
 					ntohs(ip->udp_src),
 					ntohs(ip->udp_len) - UDP_HDR_SIZE);
Signature · 1 line
1.7.11.5