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(ð, skb_in->data, sizeof(struct ethhdr));
124 mac_header_data = ð
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(ð, skb_in->data, sizeof(struct ethhdr));
247 if (ntohs(eth.h_proto) != ETH_P_IPV6)
248 return -EPROTONOSUPPORT;
249 mac_header_data = ð
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 *)ð
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 num_packet = gdm_dev16_to_cpu(endian, multi_sdu->num_packet);
692
693 for (i = 0; i < num_packet; i++) {
694 copied = data - multi_sdu->data;
695 if (len < copied + sizeof(*sdu)) {
696 pr_err("rx prevent buffer overflow");
697 return;
698 }
699
700 sdu = (struct sdu *)data;
701
702 cmd_evt = gdm_dev16_to_cpu(endian, sdu->cmd_evt);
703 hci_len = gdm_dev16_to_cpu(endian, sdu->len);
704 nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
705
706 if (cmd_evt != LTE_RX_SDU) {
707 pr_err("rx sdu wrong hci %04x\n", cmd_evt);
708 return;
709 }
710 if (hci_len < 12 ||
711 len < copied + sizeof(*sdu) + (hci_len - 12)) {
712 pr_err("rx sdu invalid len %d\n", hci_len);
713 return;
714 }
715
716 index = find_dev_index(nic_type);
717 if (index < 0) {
718 pr_err("rx sdu invalid nic_type :%x\n", nic_type);
719 return;
720 }
721 dev = phy_dev->dev[index];
722 gdm_lte_netif_rx(dev, (char *)sdu->data,
723 (int)(hci_len - 12), nic_type);
724
725 data += ((hci_len + 3) & 0xfffc) + HCI_HEADER_SIZE;
726 }
727 }
728
gdm_lte_pdn_table(struct net_device * dev,char * buf,int len)729 static void gdm_lte_pdn_table(struct net_device *dev, char *buf, int len)
730 {
731 struct nic *nic = netdev_priv(dev);
732 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
733 u8 ed = nic->phy_dev->get_endian(nic->phy_dev->priv_dev);
734
735 if (!pdn_table->activate) {
736 memset(&nic->pdn_table, 0x00, sizeof(struct pdn_table));
737 netdev_info(dev, "pdn deactivated\n");
738
739 return;
740 }
741
742 nic->pdn_table.activate = pdn_table->activate;
743 nic->pdn_table.dft_eps_id = gdm_dev32_to_cpu(ed, pdn_table->dft_eps_id);
744 nic->pdn_table.nic_type = gdm_dev32_to_cpu(ed, pdn_table->nic_type);
745
746 netdev_info(dev, "pdn activated, nic_type=0x%x\n",
747 nic->pdn_table.nic_type);
748 }
749
gdm_lte_receive_pkt(struct phy_dev * phy_dev,char * buf,int len)750 static int gdm_lte_receive_pkt(struct phy_dev *phy_dev, char *buf, int len)
751 {
752 struct hci_packet *hci = (struct hci_packet *)buf;
753 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
754 struct sdu *sdu;
755 struct net_device *dev;
756 u8 endian = phy_dev->get_endian(phy_dev->priv_dev);
757 int ret = 0;
758 u16 cmd_evt;
759 u32 nic_type;
760 int index;
761
762 if (!len)
763 return ret;
764
765 cmd_evt = gdm_dev16_to_cpu(endian, hci->cmd_evt);
766
767 dev = phy_dev->dev[0];
768 if (!dev)
769 return 0;
770
771 switch (cmd_evt) {
772 case LTE_RX_SDU:
773 sdu = (struct sdu *)hci->data;
774 nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
775 index = find_dev_index(nic_type);
776 if (index < 0)
777 return index;
778 dev = phy_dev->dev[index];
779 gdm_lte_netif_rx(dev, hci->data, len, nic_type);
780 break;
781 case LTE_RX_MULTI_SDU:
782 gdm_lte_multi_sdu_pkt(phy_dev, buf, len);
783 break;
784 case LTE_LINK_ON_OFF_INDICATION:
785 netdev_info(dev, "link %s\n",
786 ((struct hci_connect_ind *)buf)->connect
787 ? "on" : "off");
788 break;
789 case LTE_PDN_TABLE_IND:
790 pdn_table = (struct hci_pdn_table_ind *)buf;
791 nic_type = gdm_dev32_to_cpu(endian, pdn_table->nic_type);
792 index = find_dev_index(nic_type);
793 if (index < 0)
794 return index;
795 dev = phy_dev->dev[index];
796 gdm_lte_pdn_table(dev, buf, len);
797 fallthrough;
798 default:
799 ret = gdm_lte_event_send(dev, buf, len);
800 break;
801 }
802
803 return ret;
804 }
805
rx_complete(void * arg,void * data,int len,int context)806 static int rx_complete(void *arg, void *data, int len, int context)
807 {
808 struct phy_dev *phy_dev = arg;
809
810 return gdm_lte_receive_pkt(phy_dev, data, len);
811 }
812
start_rx_proc(struct phy_dev * phy_dev)813 void start_rx_proc(struct phy_dev *phy_dev)
814 {
815 int i;
816
817 for (i = 0; i < MAX_RX_SUBMIT_COUNT; i++)
818 phy_dev->rcv_func(phy_dev->priv_dev,
819 rx_complete, phy_dev, USB_COMPLETE);
820 }
821
822 static const struct net_device_ops gdm_netdev_ops = {
823 .ndo_open = gdm_lte_open,
824 .ndo_stop = gdm_lte_close,
825 .ndo_set_config = gdm_lte_set_config,
826 .ndo_start_xmit = gdm_lte_tx,
827 .ndo_get_stats = gdm_lte_stats,
828 };
829
830 static u8 gdm_lte_macaddr[ETH_ALEN] = {0x00, 0x0a, 0x3b, 0x00, 0x00, 0x00};
831
form_mac_address(u8 * dev_addr,u8 * nic_src,u8 * nic_dest,u8 * mac_address,u8 index)832 static void form_mac_address(u8 *dev_addr, u8 *nic_src, u8 *nic_dest,
833 u8 *mac_address, u8 index)
834 {
835 /* Form the dev_addr */
836 if (!mac_address)
837 ether_addr_copy(dev_addr, gdm_lte_macaddr);
838 else
839 ether_addr_copy(dev_addr, mac_address);
840
841 /* The last byte of the mac address
842 * should be less than or equal to 0xFC
843 */
844 dev_addr[ETH_ALEN - 1] += index;
845
846 /* Create random nic src and copy the first
847 * 3 bytes to be the same as dev_addr
848 */
849 eth_random_addr(nic_src);
850 memcpy(nic_src, dev_addr, 3);
851
852 /* Copy the nic_dest from dev_addr*/
853 ether_addr_copy(nic_dest, dev_addr);
854 }
855
validate_mac_address(u8 * mac_address)856 static void validate_mac_address(u8 *mac_address)
857 {
858 /* if zero address or multicast bit set, restore the default value */
859 if (is_zero_ether_addr(mac_address) || (mac_address[0] & 0x01)) {
860 pr_err("MAC invalid, restoring default\n");
861 memcpy(mac_address, gdm_lte_macaddr, 6);
862 }
863 }
864
register_lte_device(struct phy_dev * phy_dev,struct device * dev,u8 * mac_address)865 int register_lte_device(struct phy_dev *phy_dev,
866 struct device *dev, u8 *mac_address)
867 {
868 struct nic *nic;
869 struct net_device *net;
870 char pdn_dev_name[16];
871 int ret = 0;
872 u8 index;
873
874 validate_mac_address(mac_address);
875
876 for (index = 0; index < MAX_NIC_TYPE; index++) {
877 /* Create device name lteXpdnX */
878 sprintf(pdn_dev_name, "lte%%dpdn%d", index);
879
880 /* Allocate netdev */
881 net = alloc_netdev(sizeof(struct nic), pdn_dev_name,
882 NET_NAME_UNKNOWN, ether_setup);
883 if (!net) {
884 ret = -ENOMEM;
885 goto err;
886 }
887 net->netdev_ops = &gdm_netdev_ops;
888 net->flags &= ~IFF_MULTICAST;
889 net->mtu = DEFAULT_MTU_SIZE;
890
891 nic = netdev_priv(net);
892 memset(nic, 0, sizeof(struct nic));
893 nic->netdev = net;
894 nic->phy_dev = phy_dev;
895 nic->nic_id = index;
896
897 form_mac_address(net->dev_addr,
898 nic->src_mac_addr,
899 nic->dest_mac_addr,
900 mac_address,
901 index);
902
903 SET_NETDEV_DEV(net, dev);
904 SET_NETDEV_DEVTYPE(net, &wwan_type);
905
906 ret = register_netdev(net);
907 if (ret)
908 goto err;
909
910 netif_carrier_on(net);
911
912 phy_dev->dev[index] = net;
913 }
914
915 return 0;
916
917 err:
918 unregister_lte_device(phy_dev);
919
920 return ret;
921 }
922
unregister_lte_device(struct phy_dev * phy_dev)923 void unregister_lte_device(struct phy_dev *phy_dev)
924 {
925 struct net_device *net;
926 int index;
927
928 for (index = 0; index < MAX_NIC_TYPE; index++) {
929 net = phy_dev->dev[index];
930 if (!net)
931 continue;
932
933 unregister_netdev(net);
934 free_netdev(net);
935 }
936 }
937