Skip to contentSkip to conversation

net: eth: Clear MAC address in eth_pre_remove()

PATCH Superseded Bin Meng 2 participants 8 messages Sep 10, 2015 – Sep 11, 2015 Networking

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH v2 1/3 net: eth: Clear MAC address in eth_pre_remove()

When removing an Ethernet device, the MAC address saved in the dev's platdata should be cleared.

---

Changes in v2:
- New patch to clear MAC address in eth_pre_remove()

 net/eth.c | 5 +++++
 1 file changed, 5 insertions(+)
net/eth.c +5 -0
index 26520d3..35f9641 100644
@@ -564,8 +564,13 @@ static int eth_post_probe(struct udevice *dev)
 
 static int eth_pre_remove(struct udevice *dev)
 {
+	struct eth_pdata *pdata = dev->platdata;
+
 	eth_get_ops(dev)->stop(dev);
 
+	/* clear the MAC address */
+	memset(pdata->enetaddr, 0, 6);
+
 	return 0;
 }
 
Signature · 1 line
1.8.2.1
author
#1 lore raw

PATCH v2 2/3 dm: test: Add a new test case against dm eth codes for NULL pointer access

U-Boot crashes when doing a 'ping' with the following test scenario:

- All ethernet devices are not probed
- "ethaddr" for all ethernet devices are not set
- "ethact" is set to a valid ethernet device name

Add a new test case 'dm_test_eth_act' to hit such scenario.

---

Changes in v2:
- New patch to add a new test case against dm eth codes for NULL pointer access

 test/dm/eth.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
test/dm/eth.c +64 -0
index fcfb3e1..6288ae2 100644
@@ -13,11 +13,15 @@
 #include <malloc.h>
 #include <net.h>
 #include <dm/test.h>
+#include <dm/device-internal.h>
+#include <dm/uclass-internal.h>
 #include <asm/eth.h>
 #include <test/ut.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+#define DM_TEST_ETH_NUM		4
+
 static int dm_test_eth(struct unit_test_state *uts)
 {
 	net_ping_ip = string_to_ip("1.1.2.2");
@@ -82,6 +86,66 @@ static int dm_test_eth_prime(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT);
 
+/**
+ * This test case is trying to test the following scenario:
+ *	- All ethernet devices are not probed
+ *	- "ethaddr" for all ethernet devices are not set
+ *	- "ethact" is set to a valid ethernet device name
+ *
+ * With Sandbox default test configuration, all ethernet devices are
+ * probed after power-up, so we have to manually create such scenario:
+ *	- Remove all ethernet devices
+ *	- Remove all "ethaddr" environment variables
+ *	- Set "ethact" to the first ethernet device
+ *
+ * Do a ping test to see if anything goes wrong.
+ */
+static int dm_test_eth_act(struct unit_test_state *uts)
+{
+	struct udevice *dev[DM_TEST_ETH_NUM];
+	const char *ethname[DM_TEST_ETH_NUM] = {"eth at 10002000", "eth at 10003000",
+						"sbe5", "eth at 10004000"};
+	const char *addrname[DM_TEST_ETH_NUM] = {"ethaddr", "eth5addr",
+						 "eth3addr", "eth1addr"};
+	char ethaddr[DM_TEST_ETH_NUM][18];
+	int i;
+
+	net_ping_ip = string_to_ip("1.1.2.2");
+
+	/* Prepare the test scenario */
+	for (i = 0; i < DM_TEST_ETH_NUM; i++) {
+		ut_assertok(uclass_find_device_by_name(UCLASS_ETH,
+						       ethname[i], &dev[i]));
+		ut_assertok(device_remove(dev[i]));
+
+		/* Invalidate MAC address */
+		strcpy(ethaddr[i], getenv(addrname[i]));
+		/* Must disable access protection for ethaddr before clearing */
+		setenv(".flags", addrname[i]);
+		setenv(addrname[i], NULL);
+	}
+
+	/* Set ethact to "eth@10002000" */
+	setenv("ethact", ethname[0]);
+
+	/* Segment fault might happen if something is wrong */
+	ut_asserteq(-ENODEV, net_loop(PING));
+
+	for (i = 0; i < DM_TEST_ETH_NUM; i++) {
+		/* Restore the env */
+		setenv(".flags", addrname[i]);
+		setenv(addrname[i], ethaddr[i]);
+
+		/* Probe the device again */
+		ut_assertok(device_probe(dev[i]));
+	}
+	setenv(".flags", NULL);
+	setenv("ethact", NULL);
+
+	return 0;
+}
+DM_TEST(dm_test_eth_act, DM_TESTF_SCAN_FDT);
+
 /* The asserts include a return on fail; cleanup in the caller */
 static int _dm_test_eth_rotate1(struct unit_test_state *uts)
 {
Signature · 1 line
1.8.2.1
#2 lore raw

PATCH v2 2/3 dm: test: Add a new test case against dm eth codes for NULL pointer access

Hi Bin,

Quoted 9 lines · Bin Meng

U-Boot crashes when doing a 'ping' with the following test scenario:

- All ethernet devices are not probed
- "ethaddr" for all ethernet devices are not set
- "ethact" is set to a valid ethernet device name

Add a new test case 'dm_test_eth_act' to hit such scenario.

author
#3 lore raw

PATCH v2 3/3 net: eth: Check return value in various places

In reply to Bin Meng

eth_get_dev() can return NULL which means device_probe() fails for that ethernet device. Add return value check in various places or U-Boot will crash due to NULL pointer access.

With this commit, 'dm_test_eth_act' test case passes.

---

Changes in v2:
- Change check logic in eth_init() (do not use break)
- Add device_probe() return value check

 net/eth.c | 43 +++++++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 18 deletions(-)
net/eth.c +25 -18
index 35f9641..0040836 100644
@@ -179,8 +179,12 @@ struct udevice *eth_get_dev(void)
  */
 static void eth_set_dev(struct udevice *dev)
 {
-	if (dev && !device_active(dev))
+	if (dev && !device_active(dev)) {
 		eth_errno = device_probe(dev);
+		if (eth_errno)
+			dev = NULL;
+	}
+
 	eth_get_uclass_priv()->current = dev;
 }
 
@@ -213,10 +217,9 @@ struct udevice *eth_get_dev_by_name(const char *devname)
 		 * match an alias or it will match a literal name and we'll pick
 		 * up the error when we try to probe again in eth_set_dev().
 		 */
-		device_probe(it);
-		/*
-		 * Check for the name or the sequence number to match
-		 */
+		if (device_probe(it))
+			continue;
+		/* Check for the name or the sequence number to match */
 		if (strcmp(it->name, devname) == 0 ||
 		    (endp > startp && it->seq == seq))
 			return it;
@@ -346,23 +349,27 @@ int eth_init(void)
 
 	old_current = current;
 	do {
-		debug("Trying %s\n", current->name);
-
-		if (device_active(current)) {
-			ret = eth_get_ops(current)->start(current);
-			if (ret >= 0) {
-				struct eth_device_priv *priv =
-					current->uclass_priv;
-
-				priv->state = ETH_STATE_ACTIVE;
-				return 0;
+		if (current) {
+			debug("Trying %s\n", current->name);
+
+			if (device_active(current)) {
+				ret = eth_get_ops(current)->start(current);
+				if (ret >= 0) {
+					struct eth_device_priv *priv =
+						current->uclass_priv;
+
+					priv->state = ETH_STATE_ACTIVE;
+					return 0;
+				}
+			} else {
+				ret = eth_errno;
 			}
+
+			debug("FAIL\n");
 		} else {
-			ret = eth_errno;
+			debug("PROBE FAIL\n");
 		}
 
-		debug("FAIL\n");
-
 		/*
 		 * If ethrotate is enabled, this will change "current",
 		 * otherwise we will drop out of this while loop immediately
Signature · 1 line
1.8.2.1
#4 lore raw

PATCH v2 3/3 net: eth: Check return value in various places

Quoted 7 lines · Bin Meng

eth_get_dev() can return NULL which means device_probe() fails for that ethernet device. Add return value check in various places or U-Boot will crash due to NULL pointer access.

With this commit, 'dm_test_eth_act' test case passes.

#5 lore raw

PATCH v2 1/3 net: eth: Clear MAC address in eth_pre_remove()

In reply to Bin Meng

Hi Bin,

Quoted 2 lines · Bin Meng

When removing an Ethernet device, the MAC address saved in the dev's platdata should be cleared.

Why is this important to do? Test case?

Quoted 35 lines

---

Changes in v2:
- New patch to clear MAC address in eth_pre_remove()

 net/eth.c | 5 +++++
 1 file changed, 5 insertions(+)
net/eth.c +5 -1
index 26520d3..35f9641 100644
@@ -564,8 +564,13 @@ static int eth_post_probe(struct udevice *dev)
 
 static int eth_pre_remove(struct udevice *dev)
 {
+       struct eth_pdata *pdata = dev->platdata;
+
        eth_get_ops(dev)->stop(dev);
 
+       /* clear the MAC address */
+       memset(pdata->enetaddr, 0, 6);
+
        return 0;
 }
 
--

1.8.2.1

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

author
#6 lore raw

PATCH v2 1/3 net: eth: Clear MAC address in eth_pre_remove()

Hi Joe,

Quoted 7 lines · Joe Hershberger

Hi Bin,

Quoted 2 lines · Bin Meng

When removing an Ethernet device, the MAC address saved in the dev's platdata should be cleared.

Why is this important to do? Test case?

platdata->enetaddr was assigned to a value in last dev_probe(). If we don't clear it, for dev_probe() the second time, dm eth will end up treating it as a MAC address from ROM no matter where it came from originally (maybe env, ROM, or even random)

I don't think we need a test case to test this, but I can update commit message to include the reason above.

Quoted 23 lines
Quoted 23 lines

---

Changes in v2:
- New patch to clear MAC address in eth_pre_remove()

 net/eth.c | 5 +++++
 1 file changed, 5 insertions(+)
net/eth.c +2 -0
index 26520d3..35f9641 100644
@@ -564,8 +564,13 @@ static int eth_post_probe(struct udevice *dev)
 
 static int eth_pre_remove(struct udevice *dev)
 {
+       struct eth_pdata *pdata = dev->platdata;
+
        eth_get_ops(dev)->stop(dev);
 
Quoted 6 lines

net/eth.c static int eth_post_probe(struct udevice *dev) · line 576

Quoted 6 lines
net/eth.c +3 -0
+       /* clear the MAC address */
+       memset(pdata->enetaddr, 0, 6);
+
        return 0;
 }
 

Regards,
Bin

#7 lore raw

PATCH v2 1/3 net: eth: Clear MAC address in eth_pre_remove()

Hi Bin,

Quoted 12 lines · Bin Meng

Hi Joe,

Quoted 7 lines · Joe Hershberger

Hi Bin,

Quoted 2 lines · Bin Meng

When removing an Ethernet device, the MAC address saved in the dev's platdata should be cleared.

Why is this important to do? Test case?

Quoted 7 lines

platdata->enetaddr was assigned to a value in last dev_probe(). If we don't clear it, for dev_probe() the second time, dm eth will end up treating it as a MAC address from ROM no matter where it came from originally (maybe env, ROM, or even random)

I don't think we need a test case to test this, but I can update commit message to include the reason above.

OK. Sounds fine.

Thanks,
-Joe