gdm_lte.c 24 KB

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