kernel: fix mac-address-increment patch
Fix mac address increment patch. Permit to overflow to the next
byte and correctly calculate the incremented mac.
Reported-by: Chen Minqiang <ptpt52@gmail.com>
Fixes: d284e6ef0f ("treewide: convert mtd-mac-address-increment* to generic implementation")
Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
			
			
This commit is contained in:
		 Ansuel Smith
					Ansuel Smith
				
			
				
					committed by
					
						 Hauke Mehrtens
						Hauke Mehrtens
					
				
			
			
				
	
			
			
			 Hauke Mehrtens
						Hauke Mehrtens
					
				
			
						parent
						
							c9536520bd
						
					
				
				
					commit
					63ce6fcd20
				
			| @@ -1,7 +1,7 @@ | |||||||
| From 639dba857aa554f2a78572adc4cf3c32de9ec2e2 Mon Sep 17 00:00:00 2001 | From 844c273286f328acf0dab5fbd5d864366b4904dc Mon Sep 17 00:00:00 2001 | ||||||
| From: Ansuel Smith <ansuelsmth@gmail.com> | From: Ansuel Smith <ansuelsmth@gmail.com> | ||||||
| Date: Tue, 30 Mar 2021 18:21:14 +0200 | Date: Tue, 30 Mar 2021 18:21:14 +0200 | ||||||
| Subject: [PATCH 2/2] of_net: add mac-address-increment support | Subject: [PATCH] of_net: add mac-address-increment support | ||||||
|  |  | ||||||
| Lots of embedded devices use the mac-address of other interface | Lots of embedded devices use the mac-address of other interface | ||||||
| extracted from nvmem cells and increments it by one or two. Add two | extracted from nvmem cells and increments it by one or two. Add two | ||||||
| @@ -15,12 +15,12 @@ early has to be increased. | |||||||
|  |  | ||||||
| Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com> | Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com> | ||||||
| --- | --- | ||||||
|  drivers/of/of_net.c | 59 ++++++++++++++++++++++++++++++++++----------- |  drivers/of/of_net.c | 43 +++++++++++++++++++++++++++++++++++++++---- | ||||||
|  1 file changed, 45 insertions(+), 14 deletions(-) |  1 file changed, 39 insertions(+), 4 deletions(-) | ||||||
|  |  | ||||||
| --- a/drivers/of/of_net.c | --- a/drivers/of/of_net.c | ||||||
| +++ b/drivers/of/of_net.c | +++ b/drivers/of/of_net.c | ||||||
| @@ -115,27 +115,52 @@ static int of_get_mac_addr_nvmem(struct | @@ -115,27 +115,62 @@ static int of_get_mac_addr_nvmem(struct | ||||||
|   * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists |   * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists | ||||||
|   * but is all zeros. |   * but is all zeros. | ||||||
|   * |   * | ||||||
| @@ -28,15 +28,17 @@ Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com> | |||||||
| + * using: | + * using: | ||||||
| + * - mac-address-increment-byte to decide what byte to increase | + * - mac-address-increment-byte to decide what byte to increase | ||||||
| + *   (if not defined is increased the last byte) | + *   (if not defined is increased the last byte) | ||||||
| + * - mac-address-increment to decide how much to increase. The value will | + * - mac-address-increment to decide how much to increase. The value WILL | ||||||
| + *   not overflow to other bytes if the increment is over 255. | + *   overflow to other bytes if the increment is over 255 or the total | ||||||
| + *   (example 00:01:02:03:04:ff + 1 == 00:01:02:03:04:00) | + *   increment will exceed 255 of the current byte. | ||||||
|  | + *   (example 00:01:02:03:04:ff + 1 == 00:01:02:03:05:00) | ||||||
|  | + *   (example 00:01:02:03:04:fe + 5 == 00:01:02:03:05:03) | ||||||
| + * | + * | ||||||
|   * Return: 0 on success and errno in case of error. |   * Return: 0 on success and errno in case of error. | ||||||
|  */ |  */ | ||||||
|  int of_get_mac_address(struct device_node *np, u8 *addr) |  int of_get_mac_address(struct device_node *np, u8 *addr) | ||||||
|  { |  { | ||||||
| +	u32 inc_idx, mac_inc; | +	u32 inc_idx, mac_inc, mac_val; | ||||||
|  	int ret; |  	int ret; | ||||||
|   |   | ||||||
| +	/* Check first if the increment byte is present and valid. | +	/* Check first if the increment byte is present and valid. | ||||||
| @@ -70,8 +72,16 @@ Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com> | |||||||
| +		return ret; | +		return ret; | ||||||
| + | + | ||||||
| +found: | +found: | ||||||
| +	if (!of_property_read_u32(np, "mac-address-increment", &mac_inc)) | +	if (!of_property_read_u32(np, "mac-address-increment", &mac_inc)) { | ||||||
| +		addr[inc_idx] += mac_inc; | +		/* Convert to a contiguous value */ | ||||||
|  | +		mac_val = (addr[3] << 16) + (addr[4] << 8) + addr[5]; | ||||||
|  | +		mac_val += mac_inc << 8 * (5-inc_idx); | ||||||
|  | + | ||||||
|  | +		/* Apply the incremented value handling overflow case */ | ||||||
|  | +		addr[3] = (mac_val >> 16) & 0xff; | ||||||
|  | +		addr[4] = (mac_val >> 8) & 0xff; | ||||||
|  | +		addr[5] = (mac_val >> 0) & 0xff; | ||||||
|  | +	} | ||||||
|   |   | ||||||
| -	return of_get_mac_addr_nvmem(np, addr); | -	return of_get_mac_addr_nvmem(np, addr); | ||||||
| +	return ret; | +	return ret; | ||||||
|   | |||||||
| @@ -28,9 +28,9 @@ | |||||||
|  /** |  /** | ||||||
|   * Search the device tree for the best MAC address to use.  'mac-address' is |   * Search the device tree for the best MAC address to use.  'mac-address' is | ||||||
|   * checked first, because that is supposed to contain to "most recent" MAC |   * checked first, because that is supposed to contain to "most recent" MAC | ||||||
| @@ -161,6 +182,7 @@ found: | @@ -171,6 +192,7 @@ found: | ||||||
|  	if (!of_property_read_u32(np, "mac-address-increment", &mac_inc)) |  		addr[5] = (mac_val >> 0) & 0xff; | ||||||
|  		addr[inc_idx] += mac_inc; |  	} | ||||||
|   |   | ||||||
| +	of_add_mac_address(np, addr); | +	of_add_mac_address(np, addr); | ||||||
|  	return ret; |  	return ret; | ||||||
|   | |||||||
| @@ -1,7 +1,7 @@ | |||||||
| From 639dba857aa554f2a78572adc4cf3c32de9ec2e2 Mon Sep 17 00:00:00 2001 | From 844c273286f328acf0dab5fbd5d864366b4904dc Mon Sep 17 00:00:00 2001 | ||||||
| From: Ansuel Smith <ansuelsmth@gmail.com> | From: Ansuel Smith <ansuelsmth@gmail.com> | ||||||
| Date: Tue, 30 Mar 2021 18:21:14 +0200 | Date: Tue, 30 Mar 2021 18:21:14 +0200 | ||||||
| Subject: [PATCH 2/2] of_net: add mac-address-increment support | Subject: [PATCH] of_net: add mac-address-increment support | ||||||
|  |  | ||||||
| Lots of embedded devices use the mac-address of other interface | Lots of embedded devices use the mac-address of other interface | ||||||
| extracted from nvmem cells and increments it by one or two. Add two | extracted from nvmem cells and increments it by one or two. Add two | ||||||
| @@ -15,12 +15,12 @@ early has to be increased. | |||||||
|  |  | ||||||
| Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com> | Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com> | ||||||
| --- | --- | ||||||
|  drivers/of/of_net.c | 59 ++++++++++++++++++++++++++++++++++----------- |  drivers/of/of_net.c | 43 +++++++++++++++++++++++++++++++++++++++---- | ||||||
|  1 file changed, 45 insertions(+), 14 deletions(-) |  1 file changed, 39 insertions(+), 4 deletions(-) | ||||||
|  |  | ||||||
| --- a/drivers/of/of_net.c | --- a/drivers/of/of_net.c | ||||||
| +++ b/drivers/of/of_net.c | +++ b/drivers/of/of_net.c | ||||||
| @@ -109,27 +109,52 @@ static int of_get_mac_addr_nvmem(struct | @@ -109,27 +109,62 @@ static int of_get_mac_addr_nvmem(struct | ||||||
|   * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists |   * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists | ||||||
|   * but is all zeros. |   * but is all zeros. | ||||||
|   * |   * | ||||||
| @@ -28,15 +28,17 @@ Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com> | |||||||
| + * using: | + * using: | ||||||
| + * - mac-address-increment-byte to decide what byte to increase | + * - mac-address-increment-byte to decide what byte to increase | ||||||
| + *   (if not defined is increased the last byte) | + *   (if not defined is increased the last byte) | ||||||
| + * - mac-address-increment to decide how much to increase. The value will | + * - mac-address-increment to decide how much to increase. The value WILL | ||||||
| + *   not overflow to other bytes if the increment is over 255. | + *   overflow to other bytes if the increment is over 255 or the total | ||||||
| + *   (example 00:01:02:03:04:ff + 1 == 00:01:02:03:04:00) | + *   increment will exceed 255 of the current byte. | ||||||
|  | + *   (example 00:01:02:03:04:ff + 1 == 00:01:02:03:05:00) | ||||||
|  | + *   (example 00:01:02:03:04:fe + 5 == 00:01:02:03:05:03) | ||||||
| + * | + * | ||||||
|   * Return: 0 on success and errno in case of error. |   * Return: 0 on success and errno in case of error. | ||||||
|  */ |  */ | ||||||
|  int of_get_mac_address(struct device_node *np, u8 *addr) |  int of_get_mac_address(struct device_node *np, u8 *addr) | ||||||
|  { |  { | ||||||
| +	u32 inc_idx, mac_inc; | +	u32 inc_idx, mac_inc, mac_val; | ||||||
|  	int ret; |  	int ret; | ||||||
|   |   | ||||||
| +	/* Check first if the increment byte is present and valid. | +	/* Check first if the increment byte is present and valid. | ||||||
| @@ -70,8 +72,16 @@ Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com> | |||||||
| +		return ret; | +		return ret; | ||||||
| + | + | ||||||
| +found: | +found: | ||||||
| +	if (!of_property_read_u32(np, "mac-address-increment", &mac_inc)) | +	if (!of_property_read_u32(np, "mac-address-increment", &mac_inc)) { | ||||||
| +		addr[inc_idx] += mac_inc; | +		/* Convert to a contiguous value */ | ||||||
|  | +		mac_val = (addr[3] << 16) + (addr[4] << 8) + addr[5]; | ||||||
|  | +		mac_val += mac_inc << 8 * (5-inc_idx); | ||||||
|  | + | ||||||
|  | +		/* Apply the incremented value handling overflow case */ | ||||||
|  | +		addr[3] = (mac_val >> 16) & 0xff; | ||||||
|  | +		addr[4] = (mac_val >> 8) & 0xff; | ||||||
|  | +		addr[5] = (mac_val >> 0) & 0xff; | ||||||
|  | +	} | ||||||
|   |   | ||||||
| -	return of_get_mac_addr_nvmem(np, addr); | -	return of_get_mac_addr_nvmem(np, addr); | ||||||
| +	return ret; | +	return ret; | ||||||
|   | |||||||
| @@ -28,9 +28,9 @@ | |||||||
|  /** |  /** | ||||||
|   * Search the device tree for the best MAC address to use.  'mac-address' is |   * Search the device tree for the best MAC address to use.  'mac-address' is | ||||||
|   * checked first, because that is supposed to contain to "most recent" MAC |   * checked first, because that is supposed to contain to "most recent" MAC | ||||||
| @@ -155,6 +176,7 @@ found: | @@ -165,6 +186,7 @@ found: | ||||||
|  	if (!of_property_read_u32(np, "mac-address-increment", &mac_inc)) |  		addr[5] = (mac_val >> 0) & 0xff; | ||||||
|  		addr[inc_idx] += mac_inc; |  	} | ||||||
|   |   | ||||||
| +	of_add_mac_address(np, addr); | +	of_add_mac_address(np, addr); | ||||||
|  	return ret; |  	return ret; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user