Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved. */
0003 
0004 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0005 
0006 #include <linux/etherdevice.h>
0007 #include <linux/ip.h>
0008 #include <linux/ipv6.h>
0009 #include <linux/udp.h>
0010 #include <linux/in.h>
0011 #include <linux/if_arp.h>
0012 #include <linux/if_ether.h>
0013 #include <linux/if_vlan.h>
0014 #include <linux/in6.h>
0015 #include <linux/tcp.h>
0016 #include <linux/icmp.h>
0017 #include <linux/icmpv6.h>
0018 #include <linux/uaccess.h>
0019 #include <linux/errno.h>
0020 #include <net/ndisc.h>
0021 
0022 #include "gdm_lte.h"
0023 #include "netlink_k.h"
0024 #include "hci.h"
0025 #include "hci_packet.h"
0026 #include "gdm_endian.h"
0027 
0028 /*
0029  * Netlink protocol number
0030  */
0031 #define NETLINK_LTE 30
0032 
0033 /*
0034  * Default MTU Size
0035  */
0036 #define DEFAULT_MTU_SIZE 1500
0037 
0038 #define IP_VERSION_4    4
0039 #define IP_VERSION_6    6
0040 
0041 static struct {
0042     int ref_cnt;
0043     struct sock *sock;
0044 } lte_event;
0045 
0046 static struct device_type wwan_type = {
0047     .name   = "wwan",
0048 };
0049 
0050 static int gdm_lte_open(struct net_device *dev)
0051 {
0052     netif_start_queue(dev);
0053     return 0;
0054 }
0055 
0056 static int gdm_lte_close(struct net_device *dev)
0057 {
0058     netif_stop_queue(dev);
0059     return 0;
0060 }
0061 
0062 static int gdm_lte_set_config(struct net_device *dev, struct ifmap *map)
0063 {
0064     if (dev->flags & IFF_UP)
0065         return -EBUSY;
0066     return 0;
0067 }
0068 
0069 static void tx_complete(void *arg)
0070 {
0071     struct nic *nic = arg;
0072 
0073     if (netif_queue_stopped(nic->netdev))
0074         netif_wake_queue(nic->netdev);
0075 }
0076 
0077 static int gdm_lte_rx(struct sk_buff *skb, struct nic *nic, int nic_type)
0078 {
0079     int ret, len;
0080 
0081     len = skb->len + ETH_HLEN;
0082     ret = netif_rx(skb);
0083     if (ret == NET_RX_DROP) {
0084         nic->stats.rx_dropped++;
0085     } else {
0086         nic->stats.rx_packets++;
0087         nic->stats.rx_bytes += len;
0088     }
0089 
0090     return 0;
0091 }
0092 
0093 static int gdm_lte_emulate_arp(struct sk_buff *skb_in, u32 nic_type)
0094 {
0095     struct nic *nic = netdev_priv(skb_in->dev);
0096     struct sk_buff *skb_out;
0097     struct ethhdr eth;
0098     struct vlan_ethhdr vlan_eth;
0099     struct arphdr *arp_in;
0100     struct arphdr *arp_out;
0101     struct arpdata {
0102         u8 ar_sha[ETH_ALEN];
0103         u8 ar_sip[4];
0104         u8 ar_tha[ETH_ALEN];
0105         u8 ar_tip[4];
0106     };
0107     struct arpdata *arp_data_in;
0108     struct arpdata *arp_data_out;
0109     u8 arp_temp[60];
0110     void *mac_header_data;
0111     u32 mac_header_len;
0112 
0113     /* Check for skb->len, discard if empty */
0114     if (skb_in->len == 0)
0115         return -ENODATA;
0116 
0117     /* Format the mac header so that it can be put to skb */
0118     if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
0119         memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
0120         mac_header_data = &vlan_eth;
0121         mac_header_len = VLAN_ETH_HLEN;
0122     } else {
0123         memcpy(&eth, skb_in->data, sizeof(struct ethhdr));
0124         mac_header_data = &eth;
0125         mac_header_len = ETH_HLEN;
0126     }
0127 
0128     /* Get the pointer of the original request */
0129     arp_in = (struct arphdr *)(skb_in->data + mac_header_len);
0130     arp_data_in = (struct arpdata *)(skb_in->data + mac_header_len +
0131                     sizeof(struct arphdr));
0132 
0133     /* Get the pointer of the outgoing response */
0134     arp_out = (struct arphdr *)arp_temp;
0135     arp_data_out = (struct arpdata *)(arp_temp + sizeof(struct arphdr));
0136 
0137     /* Copy the arp header */
0138     memcpy(arp_out, arp_in, sizeof(struct arphdr));
0139     arp_out->ar_op = htons(ARPOP_REPLY);
0140 
0141     /* Copy the arp payload: based on 2 bytes of mac and fill the IP */
0142     arp_data_out->ar_sha[0] = arp_data_in->ar_sha[0];
0143     arp_data_out->ar_sha[1] = arp_data_in->ar_sha[1];
0144     memcpy(&arp_data_out->ar_sha[2], &arp_data_in->ar_tip[0], 4);
0145     memcpy(&arp_data_out->ar_sip[0], &arp_data_in->ar_tip[0], 4);
0146     memcpy(&arp_data_out->ar_tha[0], &arp_data_in->ar_sha[0], 6);
0147     memcpy(&arp_data_out->ar_tip[0], &arp_data_in->ar_sip[0], 4);
0148 
0149     /* Fill the destination mac with source mac of the received packet */
0150     memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
0151     /* Fill the source mac with nic's source mac */
0152     memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
0153 
0154     /* Alloc skb and reserve align */
0155     skb_out = dev_alloc_skb(skb_in->len);
0156     if (!skb_out)
0157         return -ENOMEM;
0158     skb_reserve(skb_out, NET_IP_ALIGN);
0159 
0160     skb_put_data(skb_out, mac_header_data, mac_header_len);
0161     skb_put_data(skb_out, arp_out, sizeof(struct arphdr));
0162     skb_put_data(skb_out, arp_data_out, sizeof(struct arpdata));
0163 
0164     skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
0165     skb_out->dev = skb_in->dev;
0166     skb_reset_mac_header(skb_out);
0167     skb_pull(skb_out, ETH_HLEN);
0168 
0169     gdm_lte_rx(skb_out, nic, nic_type);
0170 
0171     return 0;
0172 }
0173 
0174 static __sum16 icmp6_checksum(struct ipv6hdr *ipv6, u16 *ptr, int len)
0175 {
0176     unsigned short *w;
0177     __wsum sum = 0;
0178     int i;
0179     u16 pa;
0180 
0181     union {
0182         struct {
0183             u8 ph_src[16];
0184             u8 ph_dst[16];
0185             u32 ph_len;
0186             u8 ph_zero[3];
0187             u8 ph_nxt;
0188         } ph __packed;
0189         u16 pa[20];
0190     } pseudo_header;
0191 
0192     memset(&pseudo_header, 0, sizeof(pseudo_header));
0193     memcpy(&pseudo_header.ph.ph_src, &ipv6->saddr.in6_u.u6_addr8, 16);
0194     memcpy(&pseudo_header.ph.ph_dst, &ipv6->daddr.in6_u.u6_addr8, 16);
0195     pseudo_header.ph.ph_len = be16_to_cpu(ipv6->payload_len);
0196     pseudo_header.ph.ph_nxt = ipv6->nexthdr;
0197 
0198     for (i = 0; i < ARRAY_SIZE(pseudo_header.pa); i++) {
0199         pa = pseudo_header.pa[i];
0200         sum = csum_add(sum, csum_unfold((__force __sum16)pa));
0201     }
0202 
0203     w = ptr;
0204     while (len > 1) {
0205         sum = csum_add(sum, csum_unfold((__force __sum16)*w++));
0206         len -= 2;
0207     }
0208 
0209     return csum_fold(sum);
0210 }
0211 
0212 static int gdm_lte_emulate_ndp(struct sk_buff *skb_in, u32 nic_type)
0213 {
0214     struct nic *nic = netdev_priv(skb_in->dev);
0215     struct sk_buff *skb_out;
0216     struct ethhdr eth;
0217     struct vlan_ethhdr vlan_eth;
0218     struct neighbour_advertisement {
0219         u8 target_address[16];
0220         u8 type;
0221         u8 length;
0222         u8 link_layer_address[6];
0223     };
0224     struct neighbour_advertisement na;
0225     struct neighbour_solicitation {
0226         u8 target_address[16];
0227     };
0228     struct neighbour_solicitation *ns;
0229     struct ipv6hdr *ipv6_in;
0230     struct ipv6hdr ipv6_out;
0231     struct icmp6hdr *icmp6_in;
0232     struct icmp6hdr icmp6_out;
0233 
0234     void *mac_header_data;
0235     u32 mac_header_len;
0236 
0237     /* Format the mac header so that it can be put to skb */
0238     if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
0239         memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
0240         if (ntohs(vlan_eth.h_vlan_encapsulated_proto) != ETH_P_IPV6)
0241             return -EPROTONOSUPPORT;
0242         mac_header_data = &vlan_eth;
0243         mac_header_len = VLAN_ETH_HLEN;
0244     } else {
0245         memcpy(&eth, skb_in->data, sizeof(struct ethhdr));
0246         if (ntohs(eth.h_proto) != ETH_P_IPV6)
0247             return -EPROTONOSUPPORT;
0248         mac_header_data = &eth;
0249         mac_header_len = ETH_HLEN;
0250     }
0251 
0252     /* Check if this is IPv6 ICMP packet */
0253     ipv6_in = (struct ipv6hdr *)(skb_in->data + mac_header_len);
0254     if (ipv6_in->version != 6 || ipv6_in->nexthdr != IPPROTO_ICMPV6)
0255         return -EPROTONOSUPPORT;
0256 
0257     /* Check if this is NDP packet */
0258     icmp6_in = (struct icmp6hdr *)(skb_in->data + mac_header_len +
0259                     sizeof(struct ipv6hdr));
0260     if (icmp6_in->icmp6_type == NDISC_ROUTER_SOLICITATION) { /* Check RS */
0261         return -EPROTONOSUPPORT;
0262     } else if (icmp6_in->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
0263         /* Check NS */
0264         u8 icmp_na[sizeof(struct icmp6hdr) +
0265             sizeof(struct neighbour_advertisement)];
0266         u8 zero_addr8[16] = {0,};
0267 
0268         if (memcmp(ipv6_in->saddr.in6_u.u6_addr8, zero_addr8, 16) == 0)
0269             /* Duplicate Address Detection: Source IP is all zero */
0270             return 0;
0271 
0272         icmp6_out.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
0273         icmp6_out.icmp6_code = 0;
0274         icmp6_out.icmp6_cksum = 0;
0275         /* R=0, S=1, O=1 */
0276         icmp6_out.icmp6_dataun.un_data32[0] = htonl(0x60000000);
0277 
0278         ns = (struct neighbour_solicitation *)
0279             (skb_in->data + mac_header_len +
0280              sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr));
0281         memcpy(&na.target_address, ns->target_address, 16);
0282         na.type = 0x02;
0283         na.length = 1;
0284         na.link_layer_address[0] = 0x00;
0285         na.link_layer_address[1] = 0x0a;
0286         na.link_layer_address[2] = 0x3b;
0287         na.link_layer_address[3] = 0xaf;
0288         na.link_layer_address[4] = 0x63;
0289         na.link_layer_address[5] = 0xc7;
0290 
0291         memcpy(&ipv6_out, ipv6_in, sizeof(struct ipv6hdr));
0292         memcpy(ipv6_out.saddr.in6_u.u6_addr8, &na.target_address, 16);
0293         memcpy(ipv6_out.daddr.in6_u.u6_addr8,
0294                ipv6_in->saddr.in6_u.u6_addr8, 16);
0295         ipv6_out.payload_len = htons(sizeof(struct icmp6hdr) +
0296                 sizeof(struct neighbour_advertisement));
0297 
0298         memcpy(icmp_na, &icmp6_out, sizeof(struct icmp6hdr));
0299         memcpy(icmp_na + sizeof(struct icmp6hdr), &na,
0300                sizeof(struct neighbour_advertisement));
0301 
0302         icmp6_out.icmp6_cksum = icmp6_checksum(&ipv6_out,
0303                                (u16 *)icmp_na,
0304                                sizeof(icmp_na));
0305     } else {
0306         return -EINVAL;
0307     }
0308 
0309     /* Fill the destination mac with source mac of the received packet */
0310     memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
0311     /* Fill the source mac with nic's source mac */
0312     memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
0313 
0314     /* Alloc skb and reserve align */
0315     skb_out = dev_alloc_skb(skb_in->len);
0316     if (!skb_out)
0317         return -ENOMEM;
0318     skb_reserve(skb_out, NET_IP_ALIGN);
0319 
0320     skb_put_data(skb_out, mac_header_data, mac_header_len);
0321     skb_put_data(skb_out, &ipv6_out, sizeof(struct ipv6hdr));
0322     skb_put_data(skb_out, &icmp6_out, sizeof(struct icmp6hdr));
0323     skb_put_data(skb_out, &na, sizeof(struct neighbour_advertisement));
0324 
0325     skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
0326     skb_out->dev = skb_in->dev;
0327     skb_reset_mac_header(skb_out);
0328     skb_pull(skb_out, ETH_HLEN);
0329 
0330     gdm_lte_rx(skb_out, nic, nic_type);
0331 
0332     return 0;
0333 }
0334 
0335 static s32 gdm_lte_tx_nic_type(struct net_device *dev, struct sk_buff *skb)
0336 {
0337     struct nic *nic = netdev_priv(dev);
0338     struct ethhdr *eth;
0339     struct vlan_ethhdr *vlan_eth;
0340     struct iphdr *ip;
0341     struct ipv6hdr *ipv6;
0342     int mac_proto;
0343     void *network_data;
0344     u32 nic_type;
0345 
0346     /* NIC TYPE is based on the nic_id of this net_device */
0347     nic_type = 0x00000010 | nic->nic_id;
0348 
0349     /* Get ethernet protocol */
0350     eth = (struct ethhdr *)skb->data;
0351     if (ntohs(eth->h_proto) == ETH_P_8021Q) {
0352         vlan_eth = (struct vlan_ethhdr *)skb->data;
0353         mac_proto = ntohs(vlan_eth->h_vlan_encapsulated_proto);
0354         network_data = skb->data + VLAN_ETH_HLEN;
0355         nic_type |= NIC_TYPE_F_VLAN;
0356     } else {
0357         mac_proto = ntohs(eth->h_proto);
0358         network_data = skb->data + ETH_HLEN;
0359     }
0360 
0361     /* Process packet for nic type */
0362     switch (mac_proto) {
0363     case ETH_P_ARP:
0364         nic_type |= NIC_TYPE_ARP;
0365         break;
0366     case ETH_P_IP:
0367         nic_type |= NIC_TYPE_F_IPV4;
0368         ip = network_data;
0369 
0370         /* Check DHCPv4 */
0371         if (ip->protocol == IPPROTO_UDP) {
0372             struct udphdr *udp =
0373                     network_data + sizeof(struct iphdr);
0374             if (ntohs(udp->dest) == 67 || ntohs(udp->dest) == 68)
0375                 nic_type |= NIC_TYPE_F_DHCP;
0376         }
0377         break;
0378     case ETH_P_IPV6:
0379         nic_type |= NIC_TYPE_F_IPV6;
0380         ipv6 = network_data;
0381 
0382         if (ipv6->nexthdr == IPPROTO_ICMPV6) /* Check NDP request */ {
0383             struct icmp6hdr *icmp6 =
0384                     network_data + sizeof(struct ipv6hdr);
0385             if (icmp6->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
0386                 nic_type |= NIC_TYPE_ICMPV6;
0387         } else if (ipv6->nexthdr == IPPROTO_UDP) /* Check DHCPv6 */ {
0388             struct udphdr *udp =
0389                     network_data + sizeof(struct ipv6hdr);
0390             if (ntohs(udp->dest) == 546 || ntohs(udp->dest) == 547)
0391                 nic_type |= NIC_TYPE_F_DHCP;
0392         }
0393         break;
0394     default:
0395         break;
0396     }
0397 
0398     return nic_type;
0399 }
0400 
0401 static netdev_tx_t gdm_lte_tx(struct sk_buff *skb, struct net_device *dev)
0402 {
0403     struct nic *nic = netdev_priv(dev);
0404     u32 nic_type;
0405     void *data_buf;
0406     int data_len;
0407     int idx;
0408     int ret = 0;
0409 
0410     nic_type = gdm_lte_tx_nic_type(dev, skb);
0411     if (nic_type == 0) {
0412         netdev_err(dev, "tx - invalid nic_type\n");
0413         return -EMEDIUMTYPE;
0414     }
0415 
0416     if (nic_type & NIC_TYPE_ARP) {
0417         if (gdm_lte_emulate_arp(skb, nic_type) == 0) {
0418             dev_kfree_skb(skb);
0419             return 0;
0420         }
0421     }
0422 
0423     if (nic_type & NIC_TYPE_ICMPV6) {
0424         if (gdm_lte_emulate_ndp(skb, nic_type) == 0) {
0425             dev_kfree_skb(skb);
0426             return 0;
0427         }
0428     }
0429 
0430     /*
0431      * Need byte shift (that is, remove VLAN tag) if there is one
0432      * For the case of ARP, this breaks the offset as vlan_ethhdr+4
0433      * is treated as ethhdr However, it shouldn't be a problem as
0434      * the response starts from arp_hdr and ethhdr is created by this
0435      * driver based on the NIC mac
0436      */
0437     if (nic_type & NIC_TYPE_F_VLAN) {
0438         struct vlan_ethhdr *vlan_eth = (struct vlan_ethhdr *)skb->data;
0439 
0440         nic->vlan_id = ntohs(vlan_eth->h_vlan_TCI) & VLAN_VID_MASK;
0441         data_buf = skb->data + (VLAN_ETH_HLEN - ETH_HLEN);
0442         data_len = skb->len - (VLAN_ETH_HLEN - ETH_HLEN);
0443     } else {
0444         nic->vlan_id = 0;
0445         data_buf = skb->data;
0446         data_len = skb->len;
0447     }
0448 
0449     /* If it is a ICMPV6 packet, clear all the other bits :
0450      * for backward compatibility with the firmware
0451      */
0452     if (nic_type & NIC_TYPE_ICMPV6)
0453         nic_type = NIC_TYPE_ICMPV6;
0454 
0455     /* If it is not a dhcp packet, clear all the flag bits :
0456      * original NIC, otherwise the special flag (IPVX | DHCP)
0457      */
0458     if (!(nic_type & NIC_TYPE_F_DHCP))
0459         nic_type &= NIC_TYPE_MASK;
0460 
0461     ret = sscanf(dev->name, "lte%d", &idx);
0462     if (ret != 1) {
0463         dev_kfree_skb(skb);
0464         return -EINVAL;
0465     }
0466 
0467     ret = nic->phy_dev->send_sdu_func(nic->phy_dev->priv_dev,
0468                       data_buf, data_len,
0469                       nic->pdn_table.dft_eps_id, 0,
0470                       tx_complete, nic, idx,
0471                       nic_type);
0472 
0473     if (ret == TX_NO_BUFFER || ret == TX_NO_SPC) {
0474         netif_stop_queue(dev);
0475         if (ret == TX_NO_BUFFER)
0476             ret = 0;
0477         else
0478             ret = -ENOSPC;
0479     } else if (ret == TX_NO_DEV) {
0480         ret = -ENODEV;
0481     }
0482 
0483     /* Updates tx stats */
0484     if (ret) {
0485         nic->stats.tx_dropped++;
0486     } else {
0487         nic->stats.tx_packets++;
0488         nic->stats.tx_bytes += data_len;
0489     }
0490     dev_kfree_skb(skb);
0491 
0492     return 0;
0493 }
0494 
0495 static struct net_device_stats *gdm_lte_stats(struct net_device *dev)
0496 {
0497     struct nic *nic = netdev_priv(dev);
0498 
0499     return &nic->stats;
0500 }
0501 
0502 static int gdm_lte_event_send(struct net_device *dev, char *buf, int len)
0503 {
0504     struct phy_dev *phy_dev = ((struct nic *)netdev_priv(dev))->phy_dev;
0505     struct hci_packet *hci = (struct hci_packet *)buf;
0506     int length;
0507     int idx;
0508     int ret;
0509 
0510     ret = sscanf(dev->name, "lte%d", &idx);
0511     if (ret != 1)
0512         return -EINVAL;
0513 
0514     length = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev),
0515                   hci->len) + HCI_HEADER_SIZE;
0516     return netlink_send(lte_event.sock, idx, 0, buf, length, dev);
0517 }
0518 
0519 static void gdm_lte_event_rcv(struct net_device *dev, u16 type,
0520                   void *msg, int len)
0521 {
0522     struct nic *nic = netdev_priv(dev);
0523 
0524     nic->phy_dev->send_hci_func(nic->phy_dev->priv_dev, msg, len, NULL,
0525                     NULL);
0526 }
0527 
0528 int gdm_lte_event_init(void)
0529 {
0530     if (lte_event.ref_cnt == 0)
0531         lte_event.sock = netlink_init(NETLINK_LTE, gdm_lte_event_rcv);
0532 
0533     if (lte_event.sock) {
0534         lte_event.ref_cnt++;
0535         return 0;
0536     }
0537 
0538     pr_err("event init failed\n");
0539     return -ENODATA;
0540 }
0541 
0542 void gdm_lte_event_exit(void)
0543 {
0544     if (lte_event.sock && --lte_event.ref_cnt == 0) {
0545         sock_release(lte_event.sock->sk_socket);
0546         lte_event.sock = NULL;
0547     }
0548 }
0549 
0550 static int find_dev_index(u32 nic_type)
0551 {
0552     u8 index;
0553 
0554     index = (u8)(nic_type & 0x0000000f);
0555     if (index >= MAX_NIC_TYPE)
0556         return -EINVAL;
0557 
0558     return index;
0559 }
0560 
0561 static void gdm_lte_netif_rx(struct net_device *dev, char *buf,
0562                  int len, int flagged_nic_type)
0563 {
0564     u32 nic_type;
0565     struct nic *nic;
0566     struct sk_buff *skb;
0567     struct ethhdr eth;
0568     struct vlan_ethhdr vlan_eth;
0569     void *mac_header_data;
0570     u32 mac_header_len;
0571     char ip_version = 0;
0572 
0573     nic_type = flagged_nic_type & NIC_TYPE_MASK;
0574     nic = netdev_priv(dev);
0575 
0576     if (flagged_nic_type & NIC_TYPE_F_DHCP) {
0577         /* Change the destination mac address
0578          * with the one requested the IP
0579          */
0580         if (flagged_nic_type & NIC_TYPE_F_IPV4) {
0581             struct dhcp_packet {
0582                 u8 op;      /* BOOTREQUEST or BOOTREPLY */
0583                 u8 htype;   /* hardware address type.
0584                          * 1 = 10mb ethernet
0585                          */
0586                 u8 hlen;    /* hardware address length */
0587                 u8 hops;    /* used by relay agents only */
0588                 u32 xid;    /* unique id */
0589                 u16 secs;   /* elapsed since client began
0590                          * acquisition/renewal
0591                          */
0592                 u16 flags;  /* only one flag so far: */
0593                 #define BROADCAST_FLAG 0x8000
0594                 /* "I need broadcast replies" */
0595                 u32 ciaddr; /* client IP (if client is in
0596                          * BOUND, RENEW or REBINDING state)
0597                          */
0598                 u32 yiaddr; /* 'your' (client) IP address */
0599                 /* IP address of next server to use in
0600                  * bootstrap, returned in DHCPOFFER,
0601                  * DHCPACK by server
0602                  */
0603                 u32 siaddr_nip;
0604                 u32 gateway_nip; /* relay agent IP address */
0605                 u8 chaddr[16];   /* link-layer client hardware
0606                           * address (MAC)
0607                           */
0608                 u8 sname[64];    /* server host name (ASCIZ) */
0609                 u8 file[128];    /* boot file name (ASCIZ) */
0610                 u32 cookie;      /* fixed first four option
0611                           * bytes (99,130,83,99 dec)
0612                           */
0613             } __packed;
0614             int offset = sizeof(struct iphdr) +
0615                      sizeof(struct udphdr) +
0616                      offsetof(struct dhcp_packet, chaddr);
0617             if (offset + ETH_ALEN > len)
0618                 return;
0619             ether_addr_copy(nic->dest_mac_addr, buf + offset);
0620         }
0621     }
0622 
0623     if (nic->vlan_id > 0) {
0624         mac_header_data = (void *)&vlan_eth;
0625         mac_header_len = VLAN_ETH_HLEN;
0626     } else {
0627         mac_header_data = (void *)&eth;
0628         mac_header_len = ETH_HLEN;
0629     }
0630 
0631     /* Format the data so that it can be put to skb */
0632     ether_addr_copy(mac_header_data, nic->dest_mac_addr);
0633     memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
0634 
0635     vlan_eth.h_vlan_TCI = htons(nic->vlan_id);
0636     vlan_eth.h_vlan_proto = htons(ETH_P_8021Q);
0637 
0638     if (nic_type == NIC_TYPE_ARP) {
0639         /* Should be response: Only happens because
0640          * there was a request from the host
0641          */
0642         eth.h_proto = htons(ETH_P_ARP);
0643         vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_ARP);
0644     } else {
0645         ip_version = buf[0] >> 4;
0646         if (ip_version == IP_VERSION_4) {
0647             eth.h_proto = htons(ETH_P_IP);
0648             vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IP);
0649         } else if (ip_version == IP_VERSION_6) {
0650             eth.h_proto = htons(ETH_P_IPV6);
0651             vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IPV6);
0652         } else {
0653             netdev_err(dev, "Unknown IP version %d\n", ip_version);
0654             return;
0655         }
0656     }
0657 
0658     /* Alloc skb and reserve align */
0659     skb = dev_alloc_skb(len + mac_header_len + NET_IP_ALIGN);
0660     if (!skb)
0661         return;
0662     skb_reserve(skb, NET_IP_ALIGN);
0663 
0664     skb_put_data(skb, mac_header_data, mac_header_len);
0665     skb_put_data(skb, buf, len);
0666 
0667     skb->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
0668     skb->dev = dev;
0669     skb_reset_mac_header(skb);
0670     skb_pull(skb, ETH_HLEN);
0671 
0672     gdm_lte_rx(skb, nic, nic_type);
0673 }
0674 
0675 static void gdm_lte_multi_sdu_pkt(struct phy_dev *phy_dev, char *buf, int len)
0676 {
0677     struct net_device *dev;
0678     struct multi_sdu *multi_sdu = (struct multi_sdu *)buf;
0679     struct sdu *sdu = NULL;
0680     u8 endian = phy_dev->get_endian(phy_dev->priv_dev);
0681     u8 *data = (u8 *)multi_sdu->data;
0682     int copied;
0683     u16 i = 0;
0684     u16 num_packet;
0685     u16 hci_len;
0686     u16 cmd_evt;
0687     u32 nic_type;
0688     int index;
0689 
0690     num_packet = gdm_dev16_to_cpu(endian, multi_sdu->num_packet);
0691 
0692     for (i = 0; i < num_packet; i++) {
0693         copied = data - multi_sdu->data;
0694         if (len < copied + sizeof(*sdu)) {
0695             pr_err("rx prevent buffer overflow");
0696             return;
0697         }
0698 
0699         sdu = (struct sdu *)data;
0700 
0701         cmd_evt  = gdm_dev16_to_cpu(endian, sdu->cmd_evt);
0702         hci_len  = gdm_dev16_to_cpu(endian, sdu->len);
0703         nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
0704 
0705         if (cmd_evt != LTE_RX_SDU) {
0706             pr_err("rx sdu wrong hci %04x\n", cmd_evt);
0707             return;
0708         }
0709         if (hci_len < 12 ||
0710             len < copied + sizeof(*sdu) + (hci_len - 12)) {
0711             pr_err("rx sdu invalid len %d\n", hci_len);
0712             return;
0713         }
0714 
0715         index = find_dev_index(nic_type);
0716         if (index < 0) {
0717             pr_err("rx sdu invalid nic_type :%x\n", nic_type);
0718             return;
0719         }
0720         dev = phy_dev->dev[index];
0721         gdm_lte_netif_rx(dev, (char *)sdu->data,
0722                  (int)(hci_len - 12), nic_type);
0723 
0724         data += ((hci_len + 3) & 0xfffc) + HCI_HEADER_SIZE;
0725     }
0726 }
0727 
0728 static void gdm_lte_pdn_table(struct net_device *dev, char *buf, int len)
0729 {
0730     struct nic *nic = netdev_priv(dev);
0731     struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
0732     u8 ed = nic->phy_dev->get_endian(nic->phy_dev->priv_dev);
0733 
0734     if (!pdn_table->activate) {
0735         memset(&nic->pdn_table, 0x00, sizeof(struct pdn_table));
0736         netdev_info(dev, "pdn deactivated\n");
0737 
0738         return;
0739     }
0740 
0741     nic->pdn_table.activate = pdn_table->activate;
0742     nic->pdn_table.dft_eps_id = gdm_dev32_to_cpu(ed, pdn_table->dft_eps_id);
0743     nic->pdn_table.nic_type = gdm_dev32_to_cpu(ed, pdn_table->nic_type);
0744 
0745     netdev_info(dev, "pdn activated, nic_type=0x%x\n",
0746             nic->pdn_table.nic_type);
0747 }
0748 
0749 static int gdm_lte_receive_pkt(struct phy_dev *phy_dev, char *buf, int len)
0750 {
0751     struct hci_packet *hci = (struct hci_packet *)buf;
0752     struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
0753     struct sdu *sdu;
0754     struct net_device *dev;
0755     u8 endian = phy_dev->get_endian(phy_dev->priv_dev);
0756     int ret = 0;
0757     u16 cmd_evt;
0758     u32 nic_type;
0759     int index;
0760 
0761     if (!len)
0762         return ret;
0763 
0764     cmd_evt = gdm_dev16_to_cpu(endian, hci->cmd_evt);
0765 
0766     dev = phy_dev->dev[0];
0767     if (!dev)
0768         return 0;
0769 
0770     switch (cmd_evt) {
0771     case LTE_RX_SDU:
0772         sdu = (struct sdu *)hci->data;
0773         nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
0774         index = find_dev_index(nic_type);
0775         if (index < 0)
0776             return index;
0777         dev = phy_dev->dev[index];
0778         gdm_lte_netif_rx(dev, hci->data, len, nic_type);
0779         break;
0780     case LTE_RX_MULTI_SDU:
0781         gdm_lte_multi_sdu_pkt(phy_dev, buf, len);
0782         break;
0783     case LTE_LINK_ON_OFF_INDICATION:
0784         netdev_info(dev, "link %s\n",
0785                 ((struct hci_connect_ind *)buf)->connect
0786                 ? "on" : "off");
0787         break;
0788     case LTE_PDN_TABLE_IND:
0789         pdn_table = (struct hci_pdn_table_ind *)buf;
0790         nic_type = gdm_dev32_to_cpu(endian, pdn_table->nic_type);
0791         index = find_dev_index(nic_type);
0792         if (index < 0)
0793             return index;
0794         dev = phy_dev->dev[index];
0795         gdm_lte_pdn_table(dev, buf, len);
0796         fallthrough;
0797     default:
0798         ret = gdm_lte_event_send(dev, buf, len);
0799         break;
0800     }
0801 
0802     return ret;
0803 }
0804 
0805 static int rx_complete(void *arg, void *data, int len, int context)
0806 {
0807     struct phy_dev *phy_dev = arg;
0808 
0809     return gdm_lte_receive_pkt(phy_dev, data, len);
0810 }
0811 
0812 void start_rx_proc(struct phy_dev *phy_dev)
0813 {
0814     int i;
0815 
0816     for (i = 0; i < MAX_RX_SUBMIT_COUNT; i++)
0817         phy_dev->rcv_func(phy_dev->priv_dev,
0818                 rx_complete, phy_dev, USB_COMPLETE);
0819 }
0820 
0821 static const struct net_device_ops gdm_netdev_ops = {
0822     .ndo_open           = gdm_lte_open,
0823     .ndo_stop           = gdm_lte_close,
0824     .ndo_set_config         = gdm_lte_set_config,
0825     .ndo_start_xmit         = gdm_lte_tx,
0826     .ndo_get_stats          = gdm_lte_stats,
0827 };
0828 
0829 static u8 gdm_lte_macaddr[ETH_ALEN] = {0x00, 0x0a, 0x3b, 0x00, 0x00, 0x00};
0830 
0831 static void form_mac_address(u8 *dev_addr, u8 *nic_src, u8 *nic_dest,
0832                  u8 *mac_address, u8 index)
0833 {
0834     /* Form the dev_addr */
0835     if (!mac_address)
0836         ether_addr_copy(dev_addr, gdm_lte_macaddr);
0837     else
0838         ether_addr_copy(dev_addr, mac_address);
0839 
0840     /* The last byte of the mac address
0841      * should be less than or equal to 0xFC
0842      */
0843     dev_addr[ETH_ALEN - 1] += index;
0844 
0845     /* Create random nic src and copy the first
0846      * 3 bytes to be the same as dev_addr
0847      */
0848     eth_random_addr(nic_src);
0849     memcpy(nic_src, dev_addr, 3);
0850 
0851     /* Copy the nic_dest from dev_addr*/
0852     ether_addr_copy(nic_dest, dev_addr);
0853 }
0854 
0855 static void validate_mac_address(u8 *mac_address)
0856 {
0857     /* if zero address or multicast bit set, restore the default value */
0858     if (is_zero_ether_addr(mac_address) || (mac_address[0] & 0x01)) {
0859         pr_err("MAC invalid, restoring default\n");
0860         memcpy(mac_address, gdm_lte_macaddr, 6);
0861     }
0862 }
0863 
0864 int register_lte_device(struct phy_dev *phy_dev,
0865             struct device *dev, u8 *mac_address)
0866 {
0867     struct nic *nic;
0868     struct net_device *net;
0869     char pdn_dev_name[16];
0870     u8 addr[ETH_ALEN];
0871     int ret = 0;
0872     u8 index;
0873 
0874     validate_mac_address(mac_address);
0875 
0876     for (index = 0; index < MAX_NIC_TYPE; index++) {
0877         /* Create device name lteXpdnX */
0878         sprintf(pdn_dev_name, "lte%%dpdn%d", index);
0879 
0880         /* Allocate netdev */
0881         net = alloc_netdev(sizeof(struct nic), pdn_dev_name,
0882                    NET_NAME_UNKNOWN, ether_setup);
0883         if (!net) {
0884             ret = -ENOMEM;
0885             goto err;
0886         }
0887         net->netdev_ops = &gdm_netdev_ops;
0888         net->flags &= ~IFF_MULTICAST;
0889         net->mtu = DEFAULT_MTU_SIZE;
0890 
0891         nic = netdev_priv(net);
0892         memset(nic, 0, sizeof(struct nic));
0893         nic->netdev = net;
0894         nic->phy_dev = phy_dev;
0895         nic->nic_id = index;
0896 
0897         form_mac_address(addr,
0898                  nic->src_mac_addr,
0899                  nic->dest_mac_addr,
0900                  mac_address,
0901                  index);
0902         eth_hw_addr_set(net, addr);
0903 
0904         SET_NETDEV_DEV(net, dev);
0905         SET_NETDEV_DEVTYPE(net, &wwan_type);
0906 
0907         ret = register_netdev(net);
0908         if (ret)
0909             goto err;
0910 
0911         netif_carrier_on(net);
0912 
0913         phy_dev->dev[index] = net;
0914     }
0915 
0916     return 0;
0917 
0918 err:
0919     unregister_lte_device(phy_dev);
0920 
0921     return ret;
0922 }
0923 
0924 void unregister_lte_device(struct phy_dev *phy_dev)
0925 {
0926     struct net_device *net;
0927     int index;
0928 
0929     for (index = 0; index < MAX_NIC_TYPE; index++) {
0930         net = phy_dev->dev[index];
0931         if (!net)
0932             continue;
0933 
0934         unregister_netdev(net);
0935         free_netdev(net);
0936     }
0937 }