dnsmasq: full: disable ipset support by default
[openwrt/staging/mkresin.git] / target / linux / ath79 / patches-5.10 / 600-of_net-add-mac-address-ascii-support.patch
1 --- a/net/ethernet/eth.c
2 +++ b/net/ethernet/eth.c
3 @@ -545,6 +545,63 @@ int eth_platform_get_mac_address(struct
4 }
5 EXPORT_SYMBOL(eth_platform_get_mac_address);
6
7 +static void *nvmem_cell_get_mac_address(struct nvmem_cell *cell)
8 +{
9 + size_t len;
10 + void *mac;
11 +
12 + mac = nvmem_cell_read(cell, &len);
13 + if (IS_ERR(mac))
14 + return PTR_ERR(mac);
15 + if (len != ETH_ALEN) {
16 + kfree(mac);
17 + return ERR_PTR(-EINVAL);
18 + }
19 + return mac;
20 +}
21 +
22 +static void *nvmem_cell_get_mac_address_ascii(struct nvmem_cell *cell)
23 +{
24 + size_t len;
25 + int ret;
26 + void *mac_ascii;
27 + u8 *mac;
28 +
29 + mac_ascii = nvmem_cell_read(cell, &len);
30 + if (IS_ERR(mac_ascii))
31 + return PTR_ERR(mac_ascii);
32 + if (len != ETH_ALEN*2+5) {
33 + kfree(mac_ascii);
34 + return ERR_PTR(-EINVAL);
35 + }
36 + mac = kmalloc(ETH_ALEN, GFP_KERNEL);
37 + if (!mac) {
38 + kfree(mac_ascii);
39 + return ERR_PTR(-ENOMEM);
40 + }
41 + ret = sscanf(mac_ascii, "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx",
42 + &mac[0], &mac[1], &mac[2],
43 + &mac[3], &mac[4], &mac[5]);
44 + kfree(mac_ascii);
45 + if (ret == ETH_ALEN)
46 + return mac;
47 + kfree(mac);
48 + return ERR_PTR(-EINVAL);
49 +}
50 +
51 +static struct nvmem_cell_mac_address_property {
52 + char *name;
53 + void *(*read)(struct nvmem_cell *);
54 +} nvmem_cell_mac_address_properties[] = {
55 + {
56 + .name = "mac-address",
57 + .read = nvmem_cell_get_mac_address,
58 + }, {
59 + .name = "mac-address-ascii",
60 + .read = nvmem_cell_get_mac_address_ascii,
61 + },
62 +};
63 +
64 /**
65 * Obtain the MAC address from an nvmem cell named 'mac-address' associated
66 * with given device.
67 @@ -558,19 +615,23 @@ int nvmem_get_mac_address(struct device
68 {
69 struct nvmem_cell *cell;
70 const void *mac;
71 - size_t len;
72 + struct nvmem_cell_mac_address_property *property;
73 + int i;
74
75 - cell = nvmem_cell_get(dev, "mac-address");
76 - if (IS_ERR(cell))
77 - return PTR_ERR(cell);
78 -
79 - mac = nvmem_cell_read(cell, &len);
80 - nvmem_cell_put(cell);
81 -
82 - if (IS_ERR(mac))
83 - return PTR_ERR(mac);
84 + for (i = 0; i < ARRAY_SIZE(nvmem_cell_mac_address_properties); i++) {
85 + property = &nvmem_cell_mac_address_properties[i];
86 + cell = nvmem_cell_get(dev, property->name);
87 + if (IS_ERR(cell)) {
88 + if (i == ARRAY_SIZE(nvmem_cell_mac_address_properties) - 1)
89 + return PTR_ERR(cell);
90 + continue;
91 + }
92 + mac = property->read(cell);
93 + nvmem_cell_put(cell);
94 + break;
95 + }
96
97 - if (len != ETH_ALEN || !is_valid_ether_addr(mac)) {
98 + if (!is_valid_ether_addr(mac)) {
99 kfree(mac);
100 return -EINVAL;
101 }