• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved. */
3 
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5 
6 #include <linux/etherdevice.h>
7 #include <linux/ip.h>
8 #include <linux/ipv6.h>
9 #include <linux/udp.h>
10 #include <linux/in.h>
11 #include <linux/if_arp.h>
12 #include <linux/if_ether.h>
13 #include <linux/if_vlan.h>
14 #include <linux/in6.h>
15 #include <linux/tcp.h>
16 #include <linux/icmp.h>
17 #include <linux/icmpv6.h>
18 #include <linux/uaccess.h>
19 #include <linux/errno.h>
20 #include <net/ndisc.h>
21 
22 #include "gdm_lte.h"
23 #include "netlink_k.h"
24 #include "hci.h"
25 #include "hci_packet.h"
26 #include "gdm_endian.h"
27 
28 /*
29  * Netlink protocol number
30  */
31 #define NETLINK_LTE 30
32 
33 /*
34  * Default MTU Size
35  */
36 #define DEFAULT_MTU_SIZE 1500
37 
38 #define IP_VERSION_4	4
39 #define IP_VERSION_6	6
40 
41 static struct {
42 	int ref_cnt;
43 	struct sock *sock;
44 } lte_event;
45 
46 static struct device_type wwan_type = {
47 	.name   = "wwan",
48 };
49 
gdm_lte_open(struct net_device * dev)50 static int gdm_lte_open(struct net_device *dev)
51 {
52 	netif_start_queue(dev);
53 	return 0;
54 }
55 
gdm_lte_close(struct net_device * dev)56 static int gdm_lte_close(struct net_device *dev)
57 {
58 	netif_stop_queue(dev);
59 	return 0;
60 }
61 
gdm_lte_set_config(struct net_device * dev,struct ifmap * map)62 static int gdm_lte_set_config(struct net_device *dev, struct ifmap *map)
63 {
64 	if (dev->flags & IFF_UP)
65 		return -EBUSY;
66 	return 0;
67 }
68 
tx_complete(void * arg)69 static void tx_complete(void *arg)
70 {
71 	struct nic *nic = arg;
72 
73 	if (netif_queue_stopped(nic->netdev))
74 		netif_wake_queue(nic->netdev);
75 }
76 
gdm_lte_rx(struct sk_buff * skb,struct nic * nic,int nic_type)77 static int gdm_lte_rx(struct sk_buff *skb, struct nic *nic, int nic_type)
78 {
79 	int ret, len;
80 
81 	len = skb->len + ETH_HLEN;
82 	ret = netif_rx_ni(skb);
83 	if (ret == NET_RX_DROP) {
84 		nic->stats.rx_dropped++;
85 	} else {
86 		nic->stats.rx_packets++;
87 		nic->stats.rx_bytes += len;
88 	}
89 
90 	return 0;
91 }
92 
gdm_lte_emulate_arp(struct sk_buff * skb_in,u32 nic_type)93 static int gdm_lte_emulate_arp(struct sk_buff *skb_in, u32 nic_type)
94 {
95 	struct nic *nic = netdev_priv(skb_in->dev);
96 	struct sk_buff *skb_out;
97 	struct ethhdr eth;
98 	struct vlan_ethhdr vlan_eth;
99 	struct arphdr *arp_in;
100 	struct arphdr *arp_out;
101 	struct arpdata {
102 		u8 ar_sha[ETH_ALEN];
103 		u8 ar_sip[4];
104 		u8 ar_tha[ETH_ALEN];
105 		u8 ar_tip[4];
106 	};
107 	struct arpdata *arp_data_in;
108 	struct arpdata *arp_data_out;
109 	u8 arp_temp[60];
110 	void *mac_header_data;
111 	u32 mac_header_len;
112 
113 	/* Check for skb->len, discard if empty */
114 	if (skb_in->len == 0)
115 		return -ENODATA;
116 
117 	/* Format the mac header so that it can be put to skb */
118 	if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
119 		memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
120 		mac_header_data = &vlan_eth;
121 		mac_header_len = VLAN_ETH_HLEN;
122 	} else {
123 		memcpy(&eth, skb_in->data, sizeof(struct ethhdr));
124 		mac_header_data = &eth;
125 		mac_header_len = ETH_HLEN;
126 	}
127 
128 	/* Get the pointer of the original request */
129 	arp_in = (struct arphdr *)(skb_in->data + mac_header_len);
130 	arp_data_in = (struct arpdata *)(skb_in->data + mac_header_len +
131 					sizeof(struct arphdr));
132 
133 	/* Get the pointer of the outgoing response */
134 	arp_out = (struct arphdr *)arp_temp;
135 	arp_data_out = (struct arpdata *)(arp_temp + sizeof(struct arphdr));
136 
137 	/* Copy the arp header */
138 	memcpy(arp_out, arp_in, sizeof(struct arphdr));
139 	arp_out->ar_op = htons(ARPOP_REPLY);
140 
141 	/* Copy the arp payload: based on 2 bytes of mac and fill the IP */
142 	arp_data_out->ar_sha[0] = arp_data_in->ar_sha[0];
143 	arp_data_out->ar_sha[1] = arp_data_in->ar_sha[1];
144 	memcpy(&arp_data_out->ar_sha[2], &arp_data_in->ar_tip[0], 4);
145 	memcpy(&arp_data_out->ar_sip[0], &arp_data_in->ar_tip[0], 4);
146 	memcpy(&arp_data_out->ar_tha[0], &arp_data_in->ar_sha[0], 6);
147 	memcpy(&arp_data_out->ar_tip[0], &arp_data_in->ar_sip[0], 4);
148 
149 	/* Fill the destination mac with source mac of the received packet */
150 	memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
151 	/* Fill the source mac with nic's source mac */
152 	memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
153 
154 	/* Alloc skb and reserve align */
155 	skb_out = dev_alloc_skb(skb_in->len);
156 	if (!skb_out)
157 		return -ENOMEM;
158 	skb_reserve(skb_out, NET_IP_ALIGN);
159 
160 	skb_put_data(skb_out, mac_header_data, mac_header_len);
161 	skb_put_data(skb_out, arp_out, sizeof(struct arphdr));
162 	skb_put_data(skb_out, arp_data_out, sizeof(struct arpdata));
163 
164 	skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
165 	skb_out->dev = skb_in->dev;
166 	skb_reset_mac_header(skb_out);
167 	skb_pull(skb_out, ETH_HLEN);
168 
169 	gdm_lte_rx(skb_out, nic, nic_type);
170 
171 	return 0;
172 }
173 
icmp6_checksum(struct ipv6hdr * ipv6,u16 * ptr,int len)174 static __sum16 icmp6_checksum(struct ipv6hdr *ipv6, u16 *ptr, int len)
175 {
176 	unsigned short *w;
177 	__wsum sum = 0;
178 	int i;
179 	u16 pa;
180 
181 	union {
182 		struct {
183 			u8 ph_src[16];
184 			u8 ph_dst[16];
185 			u32 ph_len;
186 			u8 ph_zero[3];
187 			u8 ph_nxt;
188 		} ph __packed;
189 		u16 pa[20];
190 	} pseudo_header;
191 
192 	memset(&pseudo_header, 0, sizeof(pseudo_header));
193 	memcpy(&pseudo_header.ph.ph_src, &ipv6->saddr.in6_u.u6_addr8, 16);
194 	memcpy(&pseudo_header.ph.ph_dst, &ipv6->daddr.in6_u.u6_addr8, 16);
195 	pseudo_header.ph.ph_len = be16_to_cpu(ipv6->payload_len);
196 	pseudo_header.ph.ph_nxt = ipv6->nexthdr;
197 
198 	w = (u16 *)&pseudo_header;
199 	for (i = 0; i < ARRAY_SIZE(pseudo_header.pa); i++) {
200 		pa = pseudo_header.pa[i];
201 		sum = csum_add(sum, csum_unfold((__force __sum16)pa));
202 	}
203 
204 	w = ptr;
205 	while (len > 1) {
206 		sum = csum_add(sum, csum_unfold((__force __sum16)*w++));
207 		len -= 2;
208 	}
209 
210 	return csum_fold(sum);
211 }
212 
gdm_lte_emulate_ndp(struct sk_buff * skb_in,u32 nic_type)213 static int gdm_lte_emulate_ndp(struct sk_buff *skb_in, u32 nic_type)
214 {
215 	struct nic *nic = netdev_priv(skb_in->dev);
216 	struct sk_buff *skb_out;
217 	struct ethhdr eth;
218 	struct vlan_ethhdr vlan_eth;
219 	struct neighbour_advertisement {
220 		u8 target_address[16];
221 		u8 type;
222 		u8 length;
223 		u8 link_layer_address[6];
224 	};
225 	struct neighbour_advertisement na;
226 	struct neighbour_solicitation {
227 		u8 target_address[16];
228 	};
229 	struct neighbour_solicitation *ns;
230 	struct ipv6hdr *ipv6_in;
231 	struct ipv6hdr ipv6_out;
232 	struct icmp6hdr *icmp6_in;
233 	struct icmp6hdr icmp6_out;
234 
235 	void *mac_header_data;
236 	u32 mac_header_len;
237 
238 	/* Format the mac header so that it can be put to skb */
239 	if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
240 		memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
241 		if (ntohs(vlan_eth.h_vlan_encapsulated_proto) != ETH_P_IPV6)
242 			return -EPROTONOSUPPORT;
243 		mac_header_data = &vlan_eth;
244 		mac_header_len = VLAN_ETH_HLEN;
245 	} else {
246 		memcpy(&eth, skb_in->data, sizeof(struct ethhdr));
247 		if (ntohs(eth.h_proto) != ETH_P_IPV6)
248 			return -EPROTONOSUPPORT;
249 		mac_header_data = &eth;
250 		mac_header_len = ETH_HLEN;
251 	}
252 
253 	/* Check if this is IPv6 ICMP packet */
254 	ipv6_in = (struct ipv6hdr *)(skb_in->data + mac_header_len);
255 	if (ipv6_in->version != 6 || ipv6_in->nexthdr != IPPROTO_ICMPV6)
256 		return -EPROTONOSUPPORT;
257 
258 	/* Check if this is NDP packet */
259 	icmp6_in = (struct icmp6hdr *)(skb_in->data + mac_header_len +
260 					sizeof(struct ipv6hdr));
261 	if (icmp6_in->icmp6_type == NDISC_ROUTER_SOLICITATION) { /* Check RS */
262 		return -EPROTONOSUPPORT;
263 	} else if (icmp6_in->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
264 		/* Check NS */
265 		u8 icmp_na[sizeof(struct icmp6hdr) +
266 			sizeof(struct neighbour_advertisement)];
267 		u8 zero_addr8[16] = {0,};
268 
269 		if (memcmp(ipv6_in->saddr.in6_u.u6_addr8, zero_addr8, 16) == 0)
270 			/* Duplicate Address Detection: Source IP is all zero */
271 			return 0;
272 
273 		icmp6_out.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
274 		icmp6_out.icmp6_code = 0;
275 		icmp6_out.icmp6_cksum = 0;
276 		/* R=0, S=1, O=1 */
277 		icmp6_out.icmp6_dataun.un_data32[0] = htonl(0x60000000);
278 
279 		ns = (struct neighbour_solicitation *)
280 			(skb_in->data + mac_header_len +
281 			 sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr));
282 		memcpy(&na.target_address, ns->target_address, 16);
283 		na.type = 0x02;
284 		na.length = 1;
285 		na.link_layer_address[0] = 0x00;
286 		na.link_layer_address[1] = 0x0a;
287 		na.link_layer_address[2] = 0x3b;
288 		na.link_layer_address[3] = 0xaf;
289 		na.link_layer_address[4] = 0x63;
290 		na.link_layer_address[5] = 0xc7;
291 
292 		memcpy(&ipv6_out, ipv6_in, sizeof(struct ipv6hdr));
293 		memcpy(ipv6_out.saddr.in6_u.u6_addr8, &na.target_address, 16);
294 		memcpy(ipv6_out.daddr.in6_u.u6_addr8,
295 		       ipv6_in->saddr.in6_u.u6_addr8, 16);
296 		ipv6_out.payload_len = htons(sizeof(struct icmp6hdr) +
297 				sizeof(struct neighbour_advertisement));
298 
299 		memcpy(icmp_na, &icmp6_out, sizeof(struct icmp6hdr));
300 		memcpy(icmp_na + sizeof(struct icmp6hdr), &na,
301 		       sizeof(struct neighbour_advertisement));
302 
303 		icmp6_out.icmp6_cksum = icmp6_checksum(&ipv6_out,
304 						       (u16 *)icmp_na,
305 						       sizeof(icmp_na));
306 	} else {
307 		return -EINVAL;
308 	}
309 
310 	/* Fill the destination mac with source mac of the received packet */
311 	memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
312 	/* Fill the source mac with nic's source mac */
313 	memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
314 
315 	/* Alloc skb and reserve align */
316 	skb_out = dev_alloc_skb(skb_in->len);
317 	if (!skb_out)
318 		return -ENOMEM;
319 	skb_reserve(skb_out, NET_IP_ALIGN);
320 
321 	skb_put_data(skb_out, mac_header_data, mac_header_len);
322 	skb_put_data(skb_out, &ipv6_out, sizeof(struct ipv6hdr));
323 	skb_put_data(skb_out, &icmp6_out, sizeof(struct icmp6hdr));
324 	skb_put_data(skb_out, &na, sizeof(struct neighbour_advertisement));
325 
326 	skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
327 	skb_out->dev = skb_in->dev;
328 	skb_reset_mac_header(skb_out);
329 	skb_pull(skb_out, ETH_HLEN);
330 
331 	gdm_lte_rx(skb_out, nic, nic_type);
332 
333 	return 0;
334 }
335 
gdm_lte_tx_nic_type(struct net_device * dev,struct sk_buff * skb)336 static s32 gdm_lte_tx_nic_type(struct net_device *dev, struct sk_buff *skb)
337 {
338 	struct nic *nic = netdev_priv(dev);
339 	struct ethhdr *eth;
340 	struct vlan_ethhdr *vlan_eth;
341 	struct iphdr *ip;
342 	struct ipv6hdr *ipv6;
343 	int mac_proto;
344 	void *network_data;
345 	u32 nic_type;
346 
347 	/* NIC TYPE is based on the nic_id of this net_device */
348 	nic_type = 0x00000010 | nic->nic_id;
349 
350 	/* Get ethernet protocol */
351 	eth = (struct ethhdr *)skb->data;
352 	if (ntohs(eth->h_proto) == ETH_P_8021Q) {
353 		vlan_eth = skb_vlan_eth_hdr(skb);
354 		mac_proto = ntohs(vlan_eth->h_vlan_encapsulated_proto);
355 		network_data = skb->data + VLAN_ETH_HLEN;
356 		nic_type |= NIC_TYPE_F_VLAN;
357 	} else {
358 		mac_proto = ntohs(eth->h_proto);
359 		network_data = skb->data + ETH_HLEN;
360 	}
361 
362 	/* Process packet for nic type */
363 	switch (mac_proto) {
364 	case ETH_P_ARP:
365 		nic_type |= NIC_TYPE_ARP;
366 		break;
367 	case ETH_P_IP:
368 		nic_type |= NIC_TYPE_F_IPV4;
369 		ip = network_data;
370 
371 		/* Check DHCPv4 */
372 		if (ip->protocol == IPPROTO_UDP) {
373 			struct udphdr *udp =
374 					network_data + sizeof(struct iphdr);
375 			if (ntohs(udp->dest) == 67 || ntohs(udp->dest) == 68)
376 				nic_type |= NIC_TYPE_F_DHCP;
377 		}
378 		break;
379 	case ETH_P_IPV6:
380 		nic_type |= NIC_TYPE_F_IPV6;
381 		ipv6 = network_data;
382 
383 		if (ipv6->nexthdr == IPPROTO_ICMPV6) /* Check NDP request */ {
384 			struct icmp6hdr *icmp6 =
385 					network_data + sizeof(struct ipv6hdr);
386 			if (icmp6->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
387 				nic_type |= NIC_TYPE_ICMPV6;
388 		} else if (ipv6->nexthdr == IPPROTO_UDP) /* Check DHCPv6 */ {
389 			struct udphdr *udp =
390 					network_data + sizeof(struct ipv6hdr);
391 			if (ntohs(udp->dest) == 546 || ntohs(udp->dest) == 547)
392 				nic_type |= NIC_TYPE_F_DHCP;
393 		}
394 		break;
395 	default:
396 		break;
397 	}
398 
399 	return nic_type;
400 }
401 
gdm_lte_tx(struct sk_buff * skb,struct net_device * dev)402 static netdev_tx_t gdm_lte_tx(struct sk_buff *skb, struct net_device *dev)
403 {
404 	struct nic *nic = netdev_priv(dev);
405 	u32 nic_type;
406 	void *data_buf;
407 	int data_len;
408 	int idx;
409 	int ret = 0;
410 
411 	nic_type = gdm_lte_tx_nic_type(dev, skb);
412 	if (nic_type == 0) {
413 		netdev_err(dev, "tx - invalid nic_type\n");
414 		return -EMEDIUMTYPE;
415 	}
416 
417 	if (nic_type & NIC_TYPE_ARP) {
418 		if (gdm_lte_emulate_arp(skb, nic_type) == 0) {
419 			dev_kfree_skb(skb);
420 			return 0;
421 		}
422 	}
423 
424 	if (nic_type & NIC_TYPE_ICMPV6) {
425 		if (gdm_lte_emulate_ndp(skb, nic_type) == 0) {
426 			dev_kfree_skb(skb);
427 			return 0;
428 		}
429 	}
430 
431 	/*
432 	 * Need byte shift (that is, remove VLAN tag) if there is one
433 	 * For the case of ARP, this breaks the offset as vlan_ethhdr+4
434 	 * is treated as ethhdr	However, it shouldn't be a problem as
435 	 * the response starts from arp_hdr and ethhdr is created by this
436 	 * driver based on the NIC mac
437 	 */
438 	if (nic_type & NIC_TYPE_F_VLAN) {
439 		struct vlan_ethhdr *vlan_eth = skb_vlan_eth_hdr(skb);
440 
441 		nic->vlan_id = ntohs(vlan_eth->h_vlan_TCI) & VLAN_VID_MASK;
442 		data_buf = skb->data + (VLAN_ETH_HLEN - ETH_HLEN);
443 		data_len = skb->len - (VLAN_ETH_HLEN - ETH_HLEN);
444 	} else {
445 		nic->vlan_id = 0;
446 		data_buf = skb->data;
447 		data_len = skb->len;
448 	}
449 
450 	/* If it is a ICMPV6 packet, clear all the other bits :
451 	 * for backward compatibility with the firmware
452 	 */
453 	if (nic_type & NIC_TYPE_ICMPV6)
454 		nic_type = NIC_TYPE_ICMPV6;
455 
456 	/* If it is not a dhcp packet, clear all the flag bits :
457 	 * original NIC, otherwise the special flag (IPVX | DHCP)
458 	 */
459 	if (!(nic_type & NIC_TYPE_F_DHCP))
460 		nic_type &= NIC_TYPE_MASK;
461 
462 	ret = sscanf(dev->name, "lte%d", &idx);
463 	if (ret != 1) {
464 		dev_kfree_skb(skb);
465 		return -EINVAL;
466 	}
467 
468 	ret = nic->phy_dev->send_sdu_func(nic->phy_dev->priv_dev,
469 					  data_buf, data_len,
470 					  nic->pdn_table.dft_eps_id, 0,
471 					  tx_complete, nic, idx,
472 					  nic_type);
473 
474 	if (ret == TX_NO_BUFFER || ret == TX_NO_SPC) {
475 		netif_stop_queue(dev);
476 		if (ret == TX_NO_BUFFER)
477 			ret = 0;
478 		else
479 			ret = -ENOSPC;
480 	} else if (ret == TX_NO_DEV) {
481 		ret = -ENODEV;
482 	}
483 
484 	/* Updates tx stats */
485 	if (ret) {
486 		nic->stats.tx_dropped++;
487 	} else {
488 		nic->stats.tx_packets++;
489 		nic->stats.tx_bytes += data_len;
490 	}
491 	dev_kfree_skb(skb);
492 
493 	return 0;
494 }
495 
gdm_lte_stats(struct net_device * dev)496 static struct net_device_stats *gdm_lte_stats(struct net_device *dev)
497 {
498 	struct nic *nic = netdev_priv(dev);
499 
500 	return &nic->stats;
501 }
502 
gdm_lte_event_send(struct net_device * dev,char * buf,int len)503 static int gdm_lte_event_send(struct net_device *dev, char *buf, int len)
504 {
505 	struct phy_dev *phy_dev = ((struct nic *)netdev_priv(dev))->phy_dev;
506 	struct hci_packet *hci = (struct hci_packet *)buf;
507 	int length;
508 	int idx;
509 	int ret;
510 
511 	ret = sscanf(dev->name, "lte%d", &idx);
512 	if (ret != 1)
513 		return -EINVAL;
514 
515 	length = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev),
516 				  hci->len) + HCI_HEADER_SIZE;
517 	return netlink_send(lte_event.sock, idx, 0, buf, length, dev);
518 }
519 
gdm_lte_event_rcv(struct net_device * dev,u16 type,void * msg,int len)520 static void gdm_lte_event_rcv(struct net_device *dev, u16 type,
521 			      void *msg, int len)
522 {
523 	struct nic *nic = netdev_priv(dev);
524 
525 	nic->phy_dev->send_hci_func(nic->phy_dev->priv_dev, msg, len, NULL,
526 				    NULL);
527 }
528 
gdm_lte_event_init(void)529 int gdm_lte_event_init(void)
530 {
531 	if (lte_event.ref_cnt == 0)
532 		lte_event.sock = netlink_init(NETLINK_LTE, gdm_lte_event_rcv);
533 
534 	if (lte_event.sock) {
535 		lte_event.ref_cnt++;
536 		return 0;
537 	}
538 
539 	pr_err("event init failed\n");
540 	return -ENODATA;
541 }
542 
gdm_lte_event_exit(void)543 void gdm_lte_event_exit(void)
544 {
545 	if (lte_event.sock && --lte_event.ref_cnt == 0) {
546 		sock_release(lte_event.sock->sk_socket);
547 		lte_event.sock = NULL;
548 	}
549 }
550 
find_dev_index(u32 nic_type)551 static int find_dev_index(u32 nic_type)
552 {
553 	u8 index;
554 
555 	index = (u8)(nic_type & 0x0000000f);
556 	if (index >= MAX_NIC_TYPE)
557 		return -EINVAL;
558 
559 	return index;
560 }
561 
gdm_lte_netif_rx(struct net_device * dev,char * buf,int len,int flagged_nic_type)562 static void gdm_lte_netif_rx(struct net_device *dev, char *buf,
563 			     int len, int flagged_nic_type)
564 {
565 	u32 nic_type;
566 	struct nic *nic;
567 	struct sk_buff *skb;
568 	struct ethhdr eth;
569 	struct vlan_ethhdr vlan_eth;
570 	void *mac_header_data;
571 	u32 mac_header_len;
572 	char ip_version = 0;
573 
574 	nic_type = flagged_nic_type & NIC_TYPE_MASK;
575 	nic = netdev_priv(dev);
576 
577 	if (flagged_nic_type & NIC_TYPE_F_DHCP) {
578 		/* Change the destination mac address
579 		 * with the one requested the IP
580 		 */
581 		if (flagged_nic_type & NIC_TYPE_F_IPV4) {
582 			struct dhcp_packet {
583 				u8 op;      /* BOOTREQUEST or BOOTREPLY */
584 				u8 htype;   /* hardware address type.
585 					     * 1 = 10mb ethernet
586 					     */
587 				u8 hlen;    /* hardware address length */
588 				u8 hops;    /* used by relay agents only */
589 				u32 xid;    /* unique id */
590 				u16 secs;   /* elapsed since client began
591 					     * acquisition/renewal
592 					     */
593 				u16 flags;  /* only one flag so far: */
594 				#define BROADCAST_FLAG 0x8000
595 				/* "I need broadcast replies" */
596 				u32 ciaddr; /* client IP (if client is in
597 					     * BOUND, RENEW or REBINDING state)
598 					     */
599 				u32 yiaddr; /* 'your' (client) IP address */
600 				/* IP address of next server to use in
601 				 * bootstrap, returned in DHCPOFFER,
602 				 * DHCPACK by server
603 				 */
604 				u32 siaddr_nip;
605 				u32 gateway_nip; /* relay agent IP address */
606 				u8 chaddr[16];   /* link-layer client hardware
607 						  * address (MAC)
608 						  */
609 				u8 sname[64];    /* server host name (ASCIZ) */
610 				u8 file[128];    /* boot file name (ASCIZ) */
611 				u32 cookie;      /* fixed first four option
612 						  * bytes (99,130,83,99 dec)
613 						  */
614 			} __packed;
615 			int offset = sizeof(struct iphdr) +
616 				     sizeof(struct udphdr) +
617 				     offsetof(struct dhcp_packet, chaddr);
618 			if (offset + ETH_ALEN > len)
619 				return;
620 			ether_addr_copy(nic->dest_mac_addr, buf + offset);
621 		}
622 	}
623 
624 	if (nic->vlan_id > 0) {
625 		mac_header_data = (void *)&vlan_eth;
626 		mac_header_len = VLAN_ETH_HLEN;
627 	} else {
628 		mac_header_data = (void *)&eth;
629 		mac_header_len = ETH_HLEN;
630 	}
631 
632 	/* Format the data so that it can be put to skb */
633 	ether_addr_copy(mac_header_data, nic->dest_mac_addr);
634 	memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
635 
636 	vlan_eth.h_vlan_TCI = htons(nic->vlan_id);
637 	vlan_eth.h_vlan_proto = htons(ETH_P_8021Q);
638 
639 	if (nic_type == NIC_TYPE_ARP) {
640 		/* Should be response: Only happens because
641 		 * there was a request from the host
642 		 */
643 		eth.h_proto = htons(ETH_P_ARP);
644 		vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_ARP);
645 	} else {
646 		ip_version = buf[0] >> 4;
647 		if (ip_version == IP_VERSION_4) {
648 			eth.h_proto = htons(ETH_P_IP);
649 			vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IP);
650 		} else if (ip_version == IP_VERSION_6) {
651 			eth.h_proto = htons(ETH_P_IPV6);
652 			vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IPV6);
653 		} else {
654 			netdev_err(dev, "Unknown IP version %d\n", ip_version);
655 			return;
656 		}
657 	}
658 
659 	/* Alloc skb and reserve align */
660 	skb = dev_alloc_skb(len + mac_header_len + NET_IP_ALIGN);
661 	if (!skb)
662 		return;
663 	skb_reserve(skb, NET_IP_ALIGN);
664 
665 	skb_put_data(skb, mac_header_data, mac_header_len);
666 	skb_put_data(skb, buf, len);
667 
668 	skb->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
669 	skb->dev = dev;
670 	skb_reset_mac_header(skb);
671 	skb_pull(skb, ETH_HLEN);
672 
673 	gdm_lte_rx(skb, nic, nic_type);
674 }
675 
gdm_lte_multi_sdu_pkt(struct phy_dev * phy_dev,char * buf,int len)676 static void gdm_lte_multi_sdu_pkt(struct phy_dev *phy_dev, char *buf, int len)
677 {
678 	struct net_device *dev;
679 	struct multi_sdu *multi_sdu = (struct multi_sdu *)buf;
680 	struct sdu *sdu = NULL;
681 	u8 endian = phy_dev->get_endian(phy_dev->priv_dev);
682 	u8 *data = (u8 *)multi_sdu->data;
683 	int copied;
684 	u16 i = 0;
685 	u16 num_packet;
686 	u16 hci_len;
687 	u16 cmd_evt;
688 	u32 nic_type;
689 	int index;
690 
691 	hci_len = gdm_dev16_to_cpu(endian, multi_sdu->len);
692 	num_packet = gdm_dev16_to_cpu(endian, multi_sdu->num_packet);
693 
694 	for (i = 0; i < num_packet; i++) {
695 		copied = data - multi_sdu->data;
696 		if (len < copied + sizeof(*sdu)) {
697 			pr_err("rx prevent buffer overflow");
698 			return;
699 		}
700 
701 		sdu = (struct sdu *)data;
702 
703 		cmd_evt  = gdm_dev16_to_cpu(endian, sdu->cmd_evt);
704 		hci_len  = gdm_dev16_to_cpu(endian, sdu->len);
705 		nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
706 
707 		if (cmd_evt != LTE_RX_SDU) {
708 			pr_err("rx sdu wrong hci %04x\n", cmd_evt);
709 			return;
710 		}
711 		if (hci_len < 12 ||
712 		    len < copied + sizeof(*sdu) + (hci_len - 12)) {
713 			pr_err("rx sdu invalid len %d\n", hci_len);
714 			return;
715 		}
716 
717 		index = find_dev_index(nic_type);
718 		if (index < 0) {
719 			pr_err("rx sdu invalid nic_type :%x\n", nic_type);
720 			return;
721 		}
722 		dev = phy_dev->dev[index];
723 		gdm_lte_netif_rx(dev, (char *)sdu->data,
724 				 (int)(hci_len - 12), nic_type);
725 
726 		data += ((hci_len + 3) & 0xfffc) + HCI_HEADER_SIZE;
727 	}
728 }
729 
gdm_lte_pdn_table(struct net_device * dev,char * buf,int len)730 static void gdm_lte_pdn_table(struct net_device *dev, char *buf, int len)
731 {
732 	struct nic *nic = netdev_priv(dev);
733 	struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
734 	u8 ed = nic->phy_dev->get_endian(nic->phy_dev->priv_dev);
735 
736 	if (!pdn_table->activate) {
737 		memset(&nic->pdn_table, 0x00, sizeof(struct pdn_table));
738 		netdev_info(dev, "pdn deactivated\n");
739 
740 		return;
741 	}
742 
743 	nic->pdn_table.activate = pdn_table->activate;
744 	nic->pdn_table.dft_eps_id = gdm_dev32_to_cpu(ed, pdn_table->dft_eps_id);
745 	nic->pdn_table.nic_type = gdm_dev32_to_cpu(ed, pdn_table->nic_type);
746 
747 	netdev_info(dev, "pdn activated, nic_type=0x%x\n",
748 		    nic->pdn_table.nic_type);
749 }
750 
gdm_lte_receive_pkt(struct phy_dev * phy_dev,char * buf,int len)751 static int gdm_lte_receive_pkt(struct phy_dev *phy_dev, char *buf, int len)
752 {
753 	struct hci_packet *hci = (struct hci_packet *)buf;
754 	struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
755 	struct sdu *sdu;
756 	struct net_device *dev;
757 	u8 endian = phy_dev->get_endian(phy_dev->priv_dev);
758 	int ret = 0;
759 	u16 cmd_evt;
760 	u32 nic_type;
761 	int index;
762 
763 	if (!len)
764 		return ret;
765 
766 	cmd_evt = gdm_dev16_to_cpu(endian, hci->cmd_evt);
767 
768 	dev = phy_dev->dev[0];
769 	if (!dev)
770 		return 0;
771 
772 	switch (cmd_evt) {
773 	case LTE_RX_SDU:
774 		sdu = (struct sdu *)hci->data;
775 		nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
776 		index = find_dev_index(nic_type);
777 		if (index < 0)
778 			return index;
779 		dev = phy_dev->dev[index];
780 		gdm_lte_netif_rx(dev, hci->data, len, nic_type);
781 		break;
782 	case LTE_RX_MULTI_SDU:
783 		gdm_lte_multi_sdu_pkt(phy_dev, buf, len);
784 		break;
785 	case LTE_LINK_ON_OFF_INDICATION:
786 		netdev_info(dev, "link %s\n",
787 			    ((struct hci_connect_ind *)buf)->connect
788 			    ? "on" : "off");
789 		break;
790 	case LTE_PDN_TABLE_IND:
791 		pdn_table = (struct hci_pdn_table_ind *)buf;
792 		nic_type = gdm_dev32_to_cpu(endian, pdn_table->nic_type);
793 		index = find_dev_index(nic_type);
794 		if (index < 0)
795 			return index;
796 		dev = phy_dev->dev[index];
797 		gdm_lte_pdn_table(dev, buf, len);
798 		fallthrough;
799 	default:
800 		ret = gdm_lte_event_send(dev, buf, len);
801 		break;
802 	}
803 
804 	return ret;
805 }
806 
rx_complete(void * arg,void * data,int len,int context)807 static int rx_complete(void *arg, void *data, int len, int context)
808 {
809 	struct phy_dev *phy_dev = arg;
810 
811 	return gdm_lte_receive_pkt(phy_dev, data, len);
812 }
813 
start_rx_proc(struct phy_dev * phy_dev)814 void start_rx_proc(struct phy_dev *phy_dev)
815 {
816 	int i;
817 
818 	for (i = 0; i < MAX_RX_SUBMIT_COUNT; i++)
819 		phy_dev->rcv_func(phy_dev->priv_dev,
820 				rx_complete, phy_dev, USB_COMPLETE);
821 }
822 
823 static const struct net_device_ops gdm_netdev_ops = {
824 	.ndo_open			= gdm_lte_open,
825 	.ndo_stop			= gdm_lte_close,
826 	.ndo_set_config			= gdm_lte_set_config,
827 	.ndo_start_xmit			= gdm_lte_tx,
828 	.ndo_get_stats			= gdm_lte_stats,
829 };
830 
831 static u8 gdm_lte_macaddr[ETH_ALEN] = {0x00, 0x0a, 0x3b, 0x00, 0x00, 0x00};
832 
form_mac_address(u8 * dev_addr,u8 * nic_src,u8 * nic_dest,u8 * mac_address,u8 index)833 static void form_mac_address(u8 *dev_addr, u8 *nic_src, u8 *nic_dest,
834 			     u8 *mac_address, u8 index)
835 {
836 	/* Form the dev_addr */
837 	if (!mac_address)
838 		ether_addr_copy(dev_addr, gdm_lte_macaddr);
839 	else
840 		ether_addr_copy(dev_addr, mac_address);
841 
842 	/* The last byte of the mac address
843 	 * should be less than or equal to 0xFC
844 	 */
845 	dev_addr[ETH_ALEN - 1] += index;
846 
847 	/* Create random nic src and copy the first
848 	 * 3 bytes to be the same as dev_addr
849 	 */
850 	eth_random_addr(nic_src);
851 	memcpy(nic_src, dev_addr, 3);
852 
853 	/* Copy the nic_dest from dev_addr*/
854 	ether_addr_copy(nic_dest, dev_addr);
855 }
856 
validate_mac_address(u8 * mac_address)857 static void validate_mac_address(u8 *mac_address)
858 {
859 	/* if zero address or multicast bit set, restore the default value */
860 	if (is_zero_ether_addr(mac_address) || (mac_address[0] & 0x01)) {
861 		pr_err("MAC invalid, restoring default\n");
862 		memcpy(mac_address, gdm_lte_macaddr, 6);
863 	}
864 }
865 
register_lte_device(struct phy_dev * phy_dev,struct device * dev,u8 * mac_address)866 int register_lte_device(struct phy_dev *phy_dev,
867 			struct device *dev, u8 *mac_address)
868 {
869 	struct nic *nic;
870 	struct net_device *net;
871 	char pdn_dev_name[16];
872 	int ret = 0;
873 	u8 index;
874 
875 	validate_mac_address(mac_address);
876 
877 	for (index = 0; index < MAX_NIC_TYPE; index++) {
878 		/* Create device name lteXpdnX */
879 		sprintf(pdn_dev_name, "lte%%dpdn%d", index);
880 
881 		/* Allocate netdev */
882 		net = alloc_netdev(sizeof(struct nic), pdn_dev_name,
883 				   NET_NAME_UNKNOWN, ether_setup);
884 		if (!net) {
885 			ret = -ENOMEM;
886 			goto err;
887 		}
888 		net->netdev_ops = &gdm_netdev_ops;
889 		net->flags &= ~IFF_MULTICAST;
890 		net->mtu = DEFAULT_MTU_SIZE;
891 
892 		nic = netdev_priv(net);
893 		memset(nic, 0, sizeof(struct nic));
894 		nic->netdev = net;
895 		nic->phy_dev = phy_dev;
896 		nic->nic_id = index;
897 
898 		form_mac_address(net->dev_addr,
899 				 nic->src_mac_addr,
900 				 nic->dest_mac_addr,
901 				 mac_address,
902 				 index);
903 
904 		SET_NETDEV_DEV(net, dev);
905 		SET_NETDEV_DEVTYPE(net, &wwan_type);
906 
907 		ret = register_netdev(net);
908 		if (ret)
909 			goto err;
910 
911 		netif_carrier_on(net);
912 
913 		phy_dev->dev[index] = net;
914 	}
915 
916 	return 0;
917 
918 err:
919 	unregister_lte_device(phy_dev);
920 
921 	return ret;
922 }
923 
unregister_lte_device(struct phy_dev * phy_dev)924 void unregister_lte_device(struct phy_dev *phy_dev)
925 {
926 	struct net_device *net;
927 	int index;
928 
929 	for (index = 0; index < MAX_NIC_TYPE; index++) {
930 		net = phy_dev->dev[index];
931 		if (!net)
932 			continue;
933 
934 		unregister_netdev(net);
935 		free_netdev(net);
936 	}
937 }
938