[PATCH 05/19] staging: ks7010: remove magic numbers

Tobin C. Harding me at tobin.cc
Wed Apr 26 00:55:10 UTC 2017


Driver includes magic numbers. Defining constants or using existing
constants aids the readability of the code.

Magic number '12' is used for two ethernet addresses (6 bytes
each). ETH_ALEN is already defined within the kernel to 6. We can us
the expression '2 * ETH_ALEN' to make this code explicit.

Magic number '20' refers to the data size, in bytes, of a struct
ether_hdr (described in eap_packet.h). We can define a constant for
this purpose, making the code explicit and easier to read.

Define constant. Remove magic numbers, using newly defined constant
and/or expression using existing kernel constant.

Signed-off-by: Tobin C. Harding <me at tobin.cc>
---
 drivers/staging/ks7010/eap_packet.h |  2 ++
 drivers/staging/ks7010/ks_hostif.c  | 25 ++++++++++++++++---------
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/drivers/staging/ks7010/eap_packet.h b/drivers/staging/ks7010/eap_packet.h
index 7a3decf..b2d25ef 100644
--- a/drivers/staging/ks7010/eap_packet.h
+++ b/drivers/staging/ks7010/eap_packet.h
@@ -9,6 +9,8 @@
 #define ETH_ALEN 6
 #endif
 
+#define ETHER_HDR_SIZE 20
+
 struct ether_hdr {
 	unsigned char h_dest[ETH_ALEN];	/* destination eth addr */
 	unsigned char h_source[ETH_ALEN];	/* source ether addr    */
diff --git a/drivers/staging/ks7010/ks_hostif.c b/drivers/staging/ks7010/ks_hostif.c
index 11adc5f..6f90e23 100644
--- a/drivers/staging/ks7010/ks_hostif.c
+++ b/drivers/staging/ks7010/ks_hostif.c
@@ -399,6 +399,7 @@ void hostif_data_indication(struct ks_wlan_private *priv)
 	struct ether_hdr *eth_hdr;
 	unsigned short eth_proto;
 	struct ieee802_1x_hdr *aa1x_hdr;
+	size_t size;
 	int ret;
 
 	DPRINTK(3, "\n");
@@ -452,12 +453,15 @@ void hostif_data_indication(struct ks_wlan_private *priv)
 		}
 		DPRINTK(4, "SNAP, rx_ind_size = %d\n", rx_ind_size);
 
-		memcpy(skb_put(skb, 12), priv->rxp, 12);	/* 8802/FDDI MAC copy */
+		size = ETH_ALEN * 2;
+		memcpy(skb_put(skb, size), priv->rxp, size);
+
 		/* (SNAP+UI..) skip */
-		memcpy(skb_put(skb, rx_ind_size - 12), priv->rxp + 18,
-		       rx_ind_size - 12);	/* copy after Type */
 
-		aa1x_hdr = (struct ieee802_1x_hdr *)(priv->rxp + 20);
+		size = rx_ind_size - (ETH_ALEN * 2);
+		memcpy(skb_put(skb, size), &eth_hdr->h_proto, size);
+
+		aa1x_hdr = (struct ieee802_1x_hdr *)(priv->rxp + ETHER_HDR_SIZE);
 		if (aa1x_hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
 		    priv->wpa.rsn_enabled)
 			atomic_set(&priv->psstatus.snooze_guard, 1);
@@ -1113,6 +1117,7 @@ int hostif_data_request(struct ks_wlan_private *priv, struct sk_buff *skb)
 	struct ieee802_1x_hdr *aa1x_hdr;
 	struct wpa_eapol_key *eap_key;
 	struct ethhdr *eth;
+	size_t size;
 	int ret;
 
 	skb_len = skb->len;
@@ -1164,11 +1169,13 @@ int hostif_data_request(struct ks_wlan_private *priv, struct sk_buff *skb)
 		goto err_kfree;
 	}
 
-	/* MAC address copy */
-	memcpy(p, buffer, 12);	/* DST/SRC MAC address */
-	p += 12;
-	buffer += 12;
-	length -= 12;
+	/* dest and src MAC address copy */
+	size = ETH_ALEN * 2;
+	memcpy(p, buffer, size);
+	p += size;
+	buffer += size;
+	length -= size;
+
 	/* EtherType/Length check */
 	if (*(buffer + 1) + (*buffer << 8) > 1500) {
 		/* ProtocolEAP = *(buffer+1) + (*buffer << 8); */
-- 
2.7.4



More information about the devel mailing list