6lowpan.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  1. /*
  2. Copyright (c) 2013-2014 Intel Corp.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License version 2 and
  5. only version 2 as published by the Free Software Foundation.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. */
  11. #include <linux/if_arp.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/module.h>
  15. #include <linux/debugfs.h>
  16. #include <net/ipv6.h>
  17. #include <net/ip6_route.h>
  18. #include <net/addrconf.h>
  19. #include <net/bluetooth/bluetooth.h>
  20. #include <net/bluetooth/hci_core.h>
  21. #include <net/bluetooth/l2cap.h>
  22. #include <net/6lowpan.h> /* for the compression support */
  23. #define VERSION "0.1"
  24. static struct dentry *lowpan_enable_debugfs;
  25. static struct dentry *lowpan_control_debugfs;
  26. #define IFACE_NAME_TEMPLATE "bt%d"
  27. struct skb_cb {
  28. struct in6_addr addr;
  29. struct in6_addr gw;
  30. struct l2cap_chan *chan;
  31. int status;
  32. };
  33. #define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb))
  34. /* The devices list contains those devices that we are acting
  35. * as a proxy. The BT 6LoWPAN device is a virtual device that
  36. * connects to the Bluetooth LE device. The real connection to
  37. * BT device is done via l2cap layer. There exists one
  38. * virtual device / one BT 6LoWPAN network (=hciX device).
  39. * The list contains struct lowpan_dev elements.
  40. */
  41. static LIST_HEAD(bt_6lowpan_devices);
  42. static DEFINE_SPINLOCK(devices_lock);
  43. static bool enable_6lowpan;
  44. /* We are listening incoming connections via this channel
  45. */
  46. static struct l2cap_chan *listen_chan;
  47. struct lowpan_peer {
  48. struct list_head list;
  49. struct rcu_head rcu;
  50. struct l2cap_chan *chan;
  51. /* peer addresses in various formats */
  52. unsigned char eui64_addr[EUI64_ADDR_LEN];
  53. struct in6_addr peer_addr;
  54. };
  55. struct lowpan_btle_dev {
  56. struct list_head list;
  57. struct hci_dev *hdev;
  58. struct net_device *netdev;
  59. struct list_head peers;
  60. atomic_t peer_count; /* number of items in peers list */
  61. struct work_struct delete_netdev;
  62. struct delayed_work notify_peers;
  63. };
  64. static inline struct lowpan_btle_dev *
  65. lowpan_btle_dev(const struct net_device *netdev)
  66. {
  67. return (struct lowpan_btle_dev *)lowpan_dev(netdev)->priv;
  68. }
  69. static inline void peer_add(struct lowpan_btle_dev *dev,
  70. struct lowpan_peer *peer)
  71. {
  72. list_add_rcu(&peer->list, &dev->peers);
  73. atomic_inc(&dev->peer_count);
  74. }
  75. static inline bool peer_del(struct lowpan_btle_dev *dev,
  76. struct lowpan_peer *peer)
  77. {
  78. list_del_rcu(&peer->list);
  79. kfree_rcu(peer, rcu);
  80. module_put(THIS_MODULE);
  81. if (atomic_dec_and_test(&dev->peer_count)) {
  82. BT_DBG("last peer");
  83. return true;
  84. }
  85. return false;
  86. }
  87. static inline struct lowpan_peer *peer_lookup_ba(struct lowpan_btle_dev *dev,
  88. bdaddr_t *ba, __u8 type)
  89. {
  90. struct lowpan_peer *peer;
  91. BT_DBG("peers %d addr %pMR type %d", atomic_read(&dev->peer_count),
  92. ba, type);
  93. rcu_read_lock();
  94. list_for_each_entry_rcu(peer, &dev->peers, list) {
  95. BT_DBG("dst addr %pMR dst type %d",
  96. &peer->chan->dst, peer->chan->dst_type);
  97. if (bacmp(&peer->chan->dst, ba))
  98. continue;
  99. if (type == peer->chan->dst_type) {
  100. rcu_read_unlock();
  101. return peer;
  102. }
  103. }
  104. rcu_read_unlock();
  105. return NULL;
  106. }
  107. static inline struct lowpan_peer *
  108. __peer_lookup_chan(struct lowpan_btle_dev *dev, struct l2cap_chan *chan)
  109. {
  110. struct lowpan_peer *peer;
  111. list_for_each_entry_rcu(peer, &dev->peers, list) {
  112. if (peer->chan == chan)
  113. return peer;
  114. }
  115. return NULL;
  116. }
  117. static inline struct lowpan_peer *
  118. __peer_lookup_conn(struct lowpan_btle_dev *dev, struct l2cap_conn *conn)
  119. {
  120. struct lowpan_peer *peer;
  121. list_for_each_entry_rcu(peer, &dev->peers, list) {
  122. if (peer->chan->conn == conn)
  123. return peer;
  124. }
  125. return NULL;
  126. }
  127. static inline struct lowpan_peer *peer_lookup_dst(struct lowpan_btle_dev *dev,
  128. struct in6_addr *daddr,
  129. struct sk_buff *skb)
  130. {
  131. struct lowpan_peer *peer;
  132. struct in6_addr *nexthop;
  133. struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
  134. int count = atomic_read(&dev->peer_count);
  135. BT_DBG("peers %d addr %pI6c rt %p", count, daddr, rt);
  136. /* If we have multiple 6lowpan peers, then check where we should
  137. * send the packet. If only one peer exists, then we can send the
  138. * packet right away.
  139. */
  140. if (count == 1) {
  141. rcu_read_lock();
  142. peer = list_first_or_null_rcu(&dev->peers, struct lowpan_peer,
  143. list);
  144. rcu_read_unlock();
  145. return peer;
  146. }
  147. if (!rt) {
  148. nexthop = &lowpan_cb(skb)->gw;
  149. if (ipv6_addr_any(nexthop))
  150. return NULL;
  151. } else {
  152. nexthop = rt6_nexthop(rt, daddr);
  153. /* We need to remember the address because it is needed
  154. * by bt_xmit() when sending the packet. In bt_xmit(), the
  155. * destination routing info is not set.
  156. */
  157. memcpy(&lowpan_cb(skb)->gw, nexthop, sizeof(struct in6_addr));
  158. }
  159. BT_DBG("gw %pI6c", nexthop);
  160. rcu_read_lock();
  161. list_for_each_entry_rcu(peer, &dev->peers, list) {
  162. BT_DBG("dst addr %pMR dst type %d ip %pI6c",
  163. &peer->chan->dst, peer->chan->dst_type,
  164. &peer->peer_addr);
  165. if (!ipv6_addr_cmp(&peer->peer_addr, nexthop)) {
  166. rcu_read_unlock();
  167. return peer;
  168. }
  169. }
  170. rcu_read_unlock();
  171. return NULL;
  172. }
  173. static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn)
  174. {
  175. struct lowpan_btle_dev *entry;
  176. struct lowpan_peer *peer = NULL;
  177. rcu_read_lock();
  178. list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
  179. peer = __peer_lookup_conn(entry, conn);
  180. if (peer)
  181. break;
  182. }
  183. rcu_read_unlock();
  184. return peer;
  185. }
  186. static struct lowpan_btle_dev *lookup_dev(struct l2cap_conn *conn)
  187. {
  188. struct lowpan_btle_dev *entry;
  189. struct lowpan_btle_dev *dev = NULL;
  190. rcu_read_lock();
  191. list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
  192. if (conn->hcon->hdev == entry->hdev) {
  193. dev = entry;
  194. break;
  195. }
  196. }
  197. rcu_read_unlock();
  198. return dev;
  199. }
  200. static int give_skb_to_upper(struct sk_buff *skb, struct net_device *dev)
  201. {
  202. struct sk_buff *skb_cp;
  203. skb_cp = skb_copy(skb, GFP_ATOMIC);
  204. if (!skb_cp)
  205. return NET_RX_DROP;
  206. return netif_rx_ni(skb_cp);
  207. }
  208. static int iphc_decompress(struct sk_buff *skb, struct net_device *netdev,
  209. struct l2cap_chan *chan)
  210. {
  211. const u8 *saddr, *daddr;
  212. struct lowpan_btle_dev *dev;
  213. struct lowpan_peer *peer;
  214. dev = lowpan_btle_dev(netdev);
  215. rcu_read_lock();
  216. peer = __peer_lookup_chan(dev, chan);
  217. rcu_read_unlock();
  218. if (!peer)
  219. return -EINVAL;
  220. saddr = peer->eui64_addr;
  221. daddr = dev->netdev->dev_addr;
  222. return lowpan_header_decompress(skb, netdev, daddr, saddr);
  223. }
  224. static int recv_pkt(struct sk_buff *skb, struct net_device *dev,
  225. struct l2cap_chan *chan)
  226. {
  227. struct sk_buff *local_skb;
  228. int ret;
  229. if (!netif_running(dev))
  230. goto drop;
  231. if (dev->type != ARPHRD_6LOWPAN || !skb->len)
  232. goto drop;
  233. skb_reset_network_header(skb);
  234. skb = skb_share_check(skb, GFP_ATOMIC);
  235. if (!skb)
  236. goto drop;
  237. /* check that it's our buffer */
  238. if (lowpan_is_ipv6(*skb_network_header(skb))) {
  239. /* Pull off the 1-byte of 6lowpan header. */
  240. skb_pull(skb, 1);
  241. /* Copy the packet so that the IPv6 header is
  242. * properly aligned.
  243. */
  244. local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
  245. skb_tailroom(skb), GFP_ATOMIC);
  246. if (!local_skb)
  247. goto drop;
  248. local_skb->protocol = htons(ETH_P_IPV6);
  249. local_skb->pkt_type = PACKET_HOST;
  250. local_skb->dev = dev;
  251. skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
  252. if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) {
  253. kfree_skb(local_skb);
  254. goto drop;
  255. }
  256. dev->stats.rx_bytes += skb->len;
  257. dev->stats.rx_packets++;
  258. consume_skb(local_skb);
  259. consume_skb(skb);
  260. } else if (lowpan_is_iphc(*skb_network_header(skb))) {
  261. local_skb = skb_clone(skb, GFP_ATOMIC);
  262. if (!local_skb)
  263. goto drop;
  264. local_skb->dev = dev;
  265. ret = iphc_decompress(local_skb, dev, chan);
  266. if (ret < 0) {
  267. kfree_skb(local_skb);
  268. goto drop;
  269. }
  270. local_skb->protocol = htons(ETH_P_IPV6);
  271. local_skb->pkt_type = PACKET_HOST;
  272. if (give_skb_to_upper(local_skb, dev)
  273. != NET_RX_SUCCESS) {
  274. kfree_skb(local_skb);
  275. goto drop;
  276. }
  277. dev->stats.rx_bytes += skb->len;
  278. dev->stats.rx_packets++;
  279. consume_skb(local_skb);
  280. consume_skb(skb);
  281. } else {
  282. goto drop;
  283. }
  284. return NET_RX_SUCCESS;
  285. drop:
  286. dev->stats.rx_dropped++;
  287. return NET_RX_DROP;
  288. }
  289. /* Packet from BT LE device */
  290. static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
  291. {
  292. struct lowpan_btle_dev *dev;
  293. struct lowpan_peer *peer;
  294. int err;
  295. peer = lookup_peer(chan->conn);
  296. if (!peer)
  297. return -ENOENT;
  298. dev = lookup_dev(chan->conn);
  299. if (!dev || !dev->netdev)
  300. return -ENOENT;
  301. err = recv_pkt(skb, dev->netdev, chan);
  302. if (err) {
  303. BT_DBG("recv pkt %d", err);
  304. err = -EAGAIN;
  305. }
  306. return err;
  307. }
  308. static u8 get_addr_type_from_eui64(u8 byte)
  309. {
  310. /* Is universal(0) or local(1) bit */
  311. return ((byte & 0x02) ? BDADDR_LE_RANDOM : BDADDR_LE_PUBLIC);
  312. }
  313. static void copy_to_bdaddr(struct in6_addr *ip6_daddr, bdaddr_t *addr)
  314. {
  315. u8 *eui64 = ip6_daddr->s6_addr + 8;
  316. addr->b[0] = eui64[7];
  317. addr->b[1] = eui64[6];
  318. addr->b[2] = eui64[5];
  319. addr->b[3] = eui64[2];
  320. addr->b[4] = eui64[1];
  321. addr->b[5] = eui64[0];
  322. }
  323. static void convert_dest_bdaddr(struct in6_addr *ip6_daddr,
  324. bdaddr_t *addr, u8 *addr_type)
  325. {
  326. copy_to_bdaddr(ip6_daddr, addr);
  327. /* We need to toggle the U/L bit that we got from IPv6 address
  328. * so that we get the proper address and type of the BD address.
  329. */
  330. addr->b[5] ^= 0x02;
  331. *addr_type = get_addr_type_from_eui64(addr->b[5]);
  332. }
  333. static int setup_header(struct sk_buff *skb, struct net_device *netdev,
  334. bdaddr_t *peer_addr, u8 *peer_addr_type)
  335. {
  336. struct in6_addr ipv6_daddr;
  337. struct ipv6hdr *hdr;
  338. struct lowpan_btle_dev *dev;
  339. struct lowpan_peer *peer;
  340. bdaddr_t addr, *any = BDADDR_ANY;
  341. u8 *daddr = any->b;
  342. int err, status = 0;
  343. hdr = ipv6_hdr(skb);
  344. dev = lowpan_btle_dev(netdev);
  345. memcpy(&ipv6_daddr, &hdr->daddr, sizeof(ipv6_daddr));
  346. if (ipv6_addr_is_multicast(&ipv6_daddr)) {
  347. lowpan_cb(skb)->chan = NULL;
  348. } else {
  349. u8 addr_type;
  350. /* Get destination BT device from skb.
  351. * If there is no such peer then discard the packet.
  352. */
  353. convert_dest_bdaddr(&ipv6_daddr, &addr, &addr_type);
  354. BT_DBG("dest addr %pMR type %d IP %pI6c", &addr,
  355. addr_type, &ipv6_daddr);
  356. peer = peer_lookup_ba(dev, &addr, addr_type);
  357. if (!peer) {
  358. /* The packet might be sent to 6lowpan interface
  359. * because of routing (either via default route
  360. * or user set route) so get peer according to
  361. * the destination address.
  362. */
  363. peer = peer_lookup_dst(dev, &ipv6_daddr, skb);
  364. if (!peer) {
  365. BT_DBG("no such peer %pMR found", &addr);
  366. return -ENOENT;
  367. }
  368. }
  369. daddr = peer->eui64_addr;
  370. *peer_addr = addr;
  371. *peer_addr_type = addr_type;
  372. lowpan_cb(skb)->chan = peer->chan;
  373. status = 1;
  374. }
  375. lowpan_header_compress(skb, netdev, daddr, dev->netdev->dev_addr);
  376. err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0);
  377. if (err < 0)
  378. return err;
  379. return status;
  380. }
  381. static int header_create(struct sk_buff *skb, struct net_device *netdev,
  382. unsigned short type, const void *_daddr,
  383. const void *_saddr, unsigned int len)
  384. {
  385. if (type != ETH_P_IPV6)
  386. return -EINVAL;
  387. return 0;
  388. }
  389. /* Packet to BT LE device */
  390. static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
  391. struct net_device *netdev)
  392. {
  393. struct msghdr msg;
  394. struct kvec iv;
  395. int err;
  396. /* Remember the skb so that we can send EAGAIN to the caller if
  397. * we run out of credits.
  398. */
  399. chan->data = skb;
  400. iv.iov_base = skb->data;
  401. iv.iov_len = skb->len;
  402. memset(&msg, 0, sizeof(msg));
  403. iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iv, 1, skb->len);
  404. err = l2cap_chan_send(chan, &msg, skb->len);
  405. if (err > 0) {
  406. netdev->stats.tx_bytes += err;
  407. netdev->stats.tx_packets++;
  408. return 0;
  409. }
  410. if (!err)
  411. err = lowpan_cb(skb)->status;
  412. if (err < 0) {
  413. if (err == -EAGAIN)
  414. netdev->stats.tx_dropped++;
  415. else
  416. netdev->stats.tx_errors++;
  417. }
  418. return err;
  419. }
  420. static int send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
  421. {
  422. struct sk_buff *local_skb;
  423. struct lowpan_btle_dev *entry;
  424. int err = 0;
  425. rcu_read_lock();
  426. list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
  427. struct lowpan_peer *pentry;
  428. struct lowpan_btle_dev *dev;
  429. if (entry->netdev != netdev)
  430. continue;
  431. dev = lowpan_btle_dev(entry->netdev);
  432. list_for_each_entry_rcu(pentry, &dev->peers, list) {
  433. int ret;
  434. local_skb = skb_clone(skb, GFP_ATOMIC);
  435. BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
  436. netdev->name,
  437. &pentry->chan->dst, pentry->chan->dst_type,
  438. &pentry->peer_addr, pentry->chan);
  439. ret = send_pkt(pentry->chan, local_skb, netdev);
  440. if (ret < 0)
  441. err = ret;
  442. kfree_skb(local_skb);
  443. }
  444. }
  445. rcu_read_unlock();
  446. return err;
  447. }
  448. static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
  449. {
  450. int err = 0;
  451. bdaddr_t addr;
  452. u8 addr_type;
  453. /* We must take a copy of the skb before we modify/replace the ipv6
  454. * header as the header could be used elsewhere
  455. */
  456. skb = skb_unshare(skb, GFP_ATOMIC);
  457. if (!skb)
  458. return NET_XMIT_DROP;
  459. /* Return values from setup_header()
  460. * <0 - error, packet is dropped
  461. * 0 - this is a multicast packet
  462. * 1 - this is unicast packet
  463. */
  464. err = setup_header(skb, netdev, &addr, &addr_type);
  465. if (err < 0) {
  466. kfree_skb(skb);
  467. return NET_XMIT_DROP;
  468. }
  469. if (err) {
  470. if (lowpan_cb(skb)->chan) {
  471. BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
  472. netdev->name, &addr, addr_type,
  473. &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan);
  474. err = send_pkt(lowpan_cb(skb)->chan, skb, netdev);
  475. } else {
  476. err = -ENOENT;
  477. }
  478. } else {
  479. /* We need to send the packet to every device behind this
  480. * interface.
  481. */
  482. err = send_mcast_pkt(skb, netdev);
  483. }
  484. dev_kfree_skb(skb);
  485. if (err)
  486. BT_DBG("ERROR: xmit failed (%d)", err);
  487. return err < 0 ? NET_XMIT_DROP : err;
  488. }
  489. static int bt_dev_init(struct net_device *dev)
  490. {
  491. netdev_lockdep_set_classes(dev);
  492. return 0;
  493. }
  494. static const struct net_device_ops netdev_ops = {
  495. .ndo_init = bt_dev_init,
  496. .ndo_start_xmit = bt_xmit,
  497. };
  498. static struct header_ops header_ops = {
  499. .create = header_create,
  500. };
  501. static void netdev_setup(struct net_device *dev)
  502. {
  503. dev->hard_header_len = 0;
  504. dev->needed_tailroom = 0;
  505. dev->flags = IFF_RUNNING | IFF_POINTOPOINT |
  506. IFF_MULTICAST;
  507. dev->watchdog_timeo = 0;
  508. dev->netdev_ops = &netdev_ops;
  509. dev->header_ops = &header_ops;
  510. dev->destructor = free_netdev;
  511. }
  512. static struct device_type bt_type = {
  513. .name = "bluetooth",
  514. };
  515. static void set_addr(u8 *eui, u8 *addr, u8 addr_type)
  516. {
  517. /* addr is the BT address in little-endian format */
  518. eui[0] = addr[5];
  519. eui[1] = addr[4];
  520. eui[2] = addr[3];
  521. eui[3] = 0xFF;
  522. eui[4] = 0xFE;
  523. eui[5] = addr[2];
  524. eui[6] = addr[1];
  525. eui[7] = addr[0];
  526. /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */
  527. if (addr_type == BDADDR_LE_PUBLIC)
  528. eui[0] &= ~0x02;
  529. else
  530. eui[0] |= 0x02;
  531. BT_DBG("type %d addr %*phC", addr_type, 8, eui);
  532. }
  533. static void set_dev_addr(struct net_device *netdev, bdaddr_t *addr,
  534. u8 addr_type)
  535. {
  536. netdev->addr_assign_type = NET_ADDR_PERM;
  537. set_addr(netdev->dev_addr, addr->b, addr_type);
  538. }
  539. static void ifup(struct net_device *netdev)
  540. {
  541. int err;
  542. rtnl_lock();
  543. err = dev_open(netdev);
  544. if (err < 0)
  545. BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
  546. rtnl_unlock();
  547. }
  548. static void ifdown(struct net_device *netdev)
  549. {
  550. int err;
  551. rtnl_lock();
  552. err = dev_close(netdev);
  553. if (err < 0)
  554. BT_INFO("iface %s cannot be closed (%d)", netdev->name, err);
  555. rtnl_unlock();
  556. }
  557. static void do_notify_peers(struct work_struct *work)
  558. {
  559. struct lowpan_btle_dev *dev = container_of(work, struct lowpan_btle_dev,
  560. notify_peers.work);
  561. netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
  562. }
  563. static bool is_bt_6lowpan(struct hci_conn *hcon)
  564. {
  565. if (hcon->type != LE_LINK)
  566. return false;
  567. if (!enable_6lowpan)
  568. return false;
  569. return true;
  570. }
  571. static struct l2cap_chan *chan_create(void)
  572. {
  573. struct l2cap_chan *chan;
  574. chan = l2cap_chan_create();
  575. if (!chan)
  576. return NULL;
  577. l2cap_chan_set_defaults(chan);
  578. chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
  579. chan->mode = L2CAP_MODE_LE_FLOWCTL;
  580. chan->imtu = 1280;
  581. return chan;
  582. }
  583. static void set_ip_addr_bits(u8 addr_type, u8 *addr)
  584. {
  585. if (addr_type == BDADDR_LE_PUBLIC)
  586. *addr |= 0x02;
  587. else
  588. *addr &= ~0x02;
  589. }
  590. static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
  591. struct lowpan_btle_dev *dev,
  592. bool new_netdev)
  593. {
  594. struct lowpan_peer *peer;
  595. peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
  596. if (!peer)
  597. return NULL;
  598. peer->chan = chan;
  599. memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
  600. /* RFC 2464 ch. 5 */
  601. peer->peer_addr.s6_addr[0] = 0xFE;
  602. peer->peer_addr.s6_addr[1] = 0x80;
  603. set_addr((u8 *)&peer->peer_addr.s6_addr + 8, chan->dst.b,
  604. chan->dst_type);
  605. memcpy(&peer->eui64_addr, (u8 *)&peer->peer_addr.s6_addr + 8,
  606. EUI64_ADDR_LEN);
  607. /* IPv6 address needs to have the U/L bit set properly so toggle
  608. * it back here.
  609. */
  610. set_ip_addr_bits(chan->dst_type, (u8 *)&peer->peer_addr.s6_addr + 8);
  611. spin_lock(&devices_lock);
  612. INIT_LIST_HEAD(&peer->list);
  613. peer_add(dev, peer);
  614. spin_unlock(&devices_lock);
  615. /* Notifying peers about us needs to be done without locks held */
  616. if (new_netdev)
  617. INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
  618. schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
  619. return peer->chan;
  620. }
  621. static int setup_netdev(struct l2cap_chan *chan, struct lowpan_btle_dev **dev)
  622. {
  623. struct net_device *netdev;
  624. int err = 0;
  625. netdev = alloc_netdev(LOWPAN_PRIV_SIZE(sizeof(struct lowpan_btle_dev)),
  626. IFACE_NAME_TEMPLATE, NET_NAME_UNKNOWN,
  627. netdev_setup);
  628. if (!netdev)
  629. return -ENOMEM;
  630. set_dev_addr(netdev, &chan->src, chan->src_type);
  631. netdev->netdev_ops = &netdev_ops;
  632. SET_NETDEV_DEV(netdev, &chan->conn->hcon->hdev->dev);
  633. SET_NETDEV_DEVTYPE(netdev, &bt_type);
  634. *dev = lowpan_btle_dev(netdev);
  635. (*dev)->netdev = netdev;
  636. (*dev)->hdev = chan->conn->hcon->hdev;
  637. INIT_LIST_HEAD(&(*dev)->peers);
  638. spin_lock(&devices_lock);
  639. INIT_LIST_HEAD(&(*dev)->list);
  640. list_add_rcu(&(*dev)->list, &bt_6lowpan_devices);
  641. spin_unlock(&devices_lock);
  642. err = lowpan_register_netdev(netdev, LOWPAN_LLTYPE_BTLE);
  643. if (err < 0) {
  644. BT_INFO("register_netdev failed %d", err);
  645. spin_lock(&devices_lock);
  646. list_del_rcu(&(*dev)->list);
  647. spin_unlock(&devices_lock);
  648. free_netdev(netdev);
  649. goto out;
  650. }
  651. BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
  652. netdev->ifindex, &chan->dst, chan->dst_type,
  653. &chan->src, chan->src_type);
  654. set_bit(__LINK_STATE_PRESENT, &netdev->state);
  655. return 0;
  656. out:
  657. return err;
  658. }
  659. static inline void chan_ready_cb(struct l2cap_chan *chan)
  660. {
  661. struct lowpan_btle_dev *dev;
  662. bool new_netdev = false;
  663. dev = lookup_dev(chan->conn);
  664. BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
  665. if (!dev) {
  666. if (setup_netdev(chan, &dev) < 0) {
  667. l2cap_chan_del(chan, -ENOENT);
  668. return;
  669. }
  670. new_netdev = true;
  671. }
  672. if (!try_module_get(THIS_MODULE))
  673. return;
  674. add_peer_chan(chan, dev, new_netdev);
  675. ifup(dev->netdev);
  676. }
  677. static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
  678. {
  679. struct l2cap_chan *chan;
  680. chan = chan_create();
  681. if (!chan)
  682. return NULL;
  683. chan->ops = pchan->ops;
  684. BT_DBG("chan %p pchan %p", chan, pchan);
  685. return chan;
  686. }
  687. static void delete_netdev(struct work_struct *work)
  688. {
  689. struct lowpan_btle_dev *entry = container_of(work,
  690. struct lowpan_btle_dev,
  691. delete_netdev);
  692. lowpan_unregister_netdev(entry->netdev);
  693. /* The entry pointer is deleted by the netdev destructor. */
  694. }
  695. static void chan_close_cb(struct l2cap_chan *chan)
  696. {
  697. struct lowpan_btle_dev *entry;
  698. struct lowpan_btle_dev *dev = NULL;
  699. struct lowpan_peer *peer;
  700. int err = -ENOENT;
  701. bool last = false, remove = true;
  702. BT_DBG("chan %p conn %p", chan, chan->conn);
  703. if (chan->conn && chan->conn->hcon) {
  704. if (!is_bt_6lowpan(chan->conn->hcon))
  705. return;
  706. /* If conn is set, then the netdev is also there and we should
  707. * not remove it.
  708. */
  709. remove = false;
  710. }
  711. spin_lock(&devices_lock);
  712. list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
  713. dev = lowpan_btle_dev(entry->netdev);
  714. peer = __peer_lookup_chan(dev, chan);
  715. if (peer) {
  716. last = peer_del(dev, peer);
  717. err = 0;
  718. BT_DBG("dev %p removing %speer %p", dev,
  719. last ? "last " : "1 ", peer);
  720. BT_DBG("chan %p orig refcnt %d", chan,
  721. atomic_read(&chan->kref.refcount));
  722. l2cap_chan_put(chan);
  723. break;
  724. }
  725. }
  726. if (!err && last && dev && !atomic_read(&dev->peer_count)) {
  727. spin_unlock(&devices_lock);
  728. cancel_delayed_work_sync(&dev->notify_peers);
  729. ifdown(dev->netdev);
  730. if (remove) {
  731. INIT_WORK(&entry->delete_netdev, delete_netdev);
  732. schedule_work(&entry->delete_netdev);
  733. }
  734. } else {
  735. spin_unlock(&devices_lock);
  736. }
  737. return;
  738. }
  739. static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
  740. {
  741. BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
  742. state_to_string(state), err);
  743. }
  744. static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
  745. unsigned long hdr_len,
  746. unsigned long len, int nb)
  747. {
  748. /* Note that we must allocate using GFP_ATOMIC here as
  749. * this function is called originally from netdev hard xmit
  750. * function in atomic context.
  751. */
  752. return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
  753. }
  754. static void chan_suspend_cb(struct l2cap_chan *chan)
  755. {
  756. struct sk_buff *skb = chan->data;
  757. BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
  758. if (!skb)
  759. return;
  760. lowpan_cb(skb)->status = -EAGAIN;
  761. }
  762. static void chan_resume_cb(struct l2cap_chan *chan)
  763. {
  764. struct sk_buff *skb = chan->data;
  765. BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
  766. if (!skb)
  767. return;
  768. lowpan_cb(skb)->status = 0;
  769. }
  770. static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
  771. {
  772. return L2CAP_CONN_TIMEOUT;
  773. }
  774. static const struct l2cap_ops bt_6lowpan_chan_ops = {
  775. .name = "L2CAP 6LoWPAN channel",
  776. .new_connection = chan_new_conn_cb,
  777. .recv = chan_recv_cb,
  778. .close = chan_close_cb,
  779. .state_change = chan_state_change_cb,
  780. .ready = chan_ready_cb,
  781. .resume = chan_resume_cb,
  782. .suspend = chan_suspend_cb,
  783. .get_sndtimeo = chan_get_sndtimeo_cb,
  784. .alloc_skb = chan_alloc_skb_cb,
  785. .teardown = l2cap_chan_no_teardown,
  786. .defer = l2cap_chan_no_defer,
  787. .set_shutdown = l2cap_chan_no_set_shutdown,
  788. };
  789. static inline __u8 bdaddr_type(__u8 type)
  790. {
  791. if (type == ADDR_LE_DEV_PUBLIC)
  792. return BDADDR_LE_PUBLIC;
  793. else
  794. return BDADDR_LE_RANDOM;
  795. }
  796. static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
  797. {
  798. struct l2cap_chan *chan;
  799. int err;
  800. chan = chan_create();
  801. if (!chan)
  802. return -EINVAL;
  803. chan->ops = &bt_6lowpan_chan_ops;
  804. err = l2cap_chan_connect(chan, cpu_to_le16(L2CAP_PSM_IPSP), 0,
  805. addr, dst_type);
  806. BT_DBG("chan %p err %d", chan, err);
  807. if (err < 0)
  808. l2cap_chan_put(chan);
  809. return err;
  810. }
  811. static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
  812. {
  813. struct lowpan_peer *peer;
  814. BT_DBG("conn %p dst type %d", conn, dst_type);
  815. peer = lookup_peer(conn);
  816. if (!peer)
  817. return -ENOENT;
  818. BT_DBG("peer %p chan %p", peer, peer->chan);
  819. l2cap_chan_close(peer->chan, ENOENT);
  820. return 0;
  821. }
  822. static struct l2cap_chan *bt_6lowpan_listen(void)
  823. {
  824. bdaddr_t *addr = BDADDR_ANY;
  825. struct l2cap_chan *chan;
  826. int err;
  827. if (!enable_6lowpan)
  828. return NULL;
  829. chan = chan_create();
  830. if (!chan)
  831. return NULL;
  832. chan->ops = &bt_6lowpan_chan_ops;
  833. chan->state = BT_LISTEN;
  834. chan->src_type = BDADDR_LE_PUBLIC;
  835. atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
  836. BT_DBG("chan %p src type %d", chan, chan->src_type);
  837. err = l2cap_add_psm(chan, addr, cpu_to_le16(L2CAP_PSM_IPSP));
  838. if (err) {
  839. l2cap_chan_put(chan);
  840. BT_ERR("psm cannot be added err %d", err);
  841. return NULL;
  842. }
  843. return chan;
  844. }
  845. static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
  846. struct l2cap_conn **conn)
  847. {
  848. struct hci_conn *hcon;
  849. struct hci_dev *hdev;
  850. int n;
  851. n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
  852. &addr->b[5], &addr->b[4], &addr->b[3],
  853. &addr->b[2], &addr->b[1], &addr->b[0],
  854. addr_type);
  855. if (n < 7)
  856. return -EINVAL;
  857. /* The LE_PUBLIC address type is ignored because of BDADDR_ANY */
  858. hdev = hci_get_route(addr, BDADDR_ANY, BDADDR_LE_PUBLIC);
  859. if (!hdev)
  860. return -ENOENT;
  861. hci_dev_lock(hdev);
  862. hcon = hci_conn_hash_lookup_le(hdev, addr, *addr_type);
  863. hci_dev_unlock(hdev);
  864. if (!hcon)
  865. return -ENOENT;
  866. *conn = (struct l2cap_conn *)hcon->l2cap_data;
  867. BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
  868. return 0;
  869. }
  870. static void disconnect_all_peers(void)
  871. {
  872. struct lowpan_btle_dev *entry;
  873. struct lowpan_peer *peer, *tmp_peer, *new_peer;
  874. struct list_head peers;
  875. INIT_LIST_HEAD(&peers);
  876. /* We make a separate list of peers as the close_cb() will
  877. * modify the device peers list so it is better not to mess
  878. * with the same list at the same time.
  879. */
  880. rcu_read_lock();
  881. list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
  882. list_for_each_entry_rcu(peer, &entry->peers, list) {
  883. new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
  884. if (!new_peer)
  885. break;
  886. new_peer->chan = peer->chan;
  887. INIT_LIST_HEAD(&new_peer->list);
  888. list_add(&new_peer->list, &peers);
  889. }
  890. }
  891. rcu_read_unlock();
  892. spin_lock(&devices_lock);
  893. list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
  894. l2cap_chan_close(peer->chan, ENOENT);
  895. list_del_rcu(&peer->list);
  896. kfree_rcu(peer, rcu);
  897. }
  898. spin_unlock(&devices_lock);
  899. }
  900. struct set_enable {
  901. struct work_struct work;
  902. bool flag;
  903. };
  904. static void do_enable_set(struct work_struct *work)
  905. {
  906. struct set_enable *set_enable = container_of(work,
  907. struct set_enable, work);
  908. if (!set_enable->flag || enable_6lowpan != set_enable->flag)
  909. /* Disconnect existing connections if 6lowpan is
  910. * disabled
  911. */
  912. disconnect_all_peers();
  913. enable_6lowpan = set_enable->flag;
  914. if (listen_chan) {
  915. l2cap_chan_close(listen_chan, 0);
  916. l2cap_chan_put(listen_chan);
  917. }
  918. listen_chan = bt_6lowpan_listen();
  919. kfree(set_enable);
  920. }
  921. static int lowpan_enable_set(void *data, u64 val)
  922. {
  923. struct set_enable *set_enable;
  924. set_enable = kzalloc(sizeof(*set_enable), GFP_KERNEL);
  925. if (!set_enable)
  926. return -ENOMEM;
  927. set_enable->flag = !!val;
  928. INIT_WORK(&set_enable->work, do_enable_set);
  929. schedule_work(&set_enable->work);
  930. return 0;
  931. }
  932. static int lowpan_enable_get(void *data, u64 *val)
  933. {
  934. *val = enable_6lowpan;
  935. return 0;
  936. }
  937. DEFINE_SIMPLE_ATTRIBUTE(lowpan_enable_fops, lowpan_enable_get,
  938. lowpan_enable_set, "%llu\n");
  939. static ssize_t lowpan_control_write(struct file *fp,
  940. const char __user *user_buffer,
  941. size_t count,
  942. loff_t *position)
  943. {
  944. char buf[32];
  945. size_t buf_size = min(count, sizeof(buf) - 1);
  946. int ret;
  947. bdaddr_t addr;
  948. u8 addr_type;
  949. struct l2cap_conn *conn = NULL;
  950. if (copy_from_user(buf, user_buffer, buf_size))
  951. return -EFAULT;
  952. buf[buf_size] = '\0';
  953. if (memcmp(buf, "connect ", 8) == 0) {
  954. ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
  955. if (ret == -EINVAL)
  956. return ret;
  957. if (listen_chan) {
  958. l2cap_chan_close(listen_chan, 0);
  959. l2cap_chan_put(listen_chan);
  960. listen_chan = NULL;
  961. }
  962. if (conn) {
  963. struct lowpan_peer *peer;
  964. if (!is_bt_6lowpan(conn->hcon))
  965. return -EINVAL;
  966. peer = lookup_peer(conn);
  967. if (peer) {
  968. BT_DBG("6LoWPAN connection already exists");
  969. return -EALREADY;
  970. }
  971. BT_DBG("conn %p dst %pMR type %d user %d", conn,
  972. &conn->hcon->dst, conn->hcon->dst_type,
  973. addr_type);
  974. }
  975. ret = bt_6lowpan_connect(&addr, addr_type);
  976. if (ret < 0)
  977. return ret;
  978. return count;
  979. }
  980. if (memcmp(buf, "disconnect ", 11) == 0) {
  981. ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
  982. if (ret < 0)
  983. return ret;
  984. ret = bt_6lowpan_disconnect(conn, addr_type);
  985. if (ret < 0)
  986. return ret;
  987. return count;
  988. }
  989. return count;
  990. }
  991. static int lowpan_control_show(struct seq_file *f, void *ptr)
  992. {
  993. struct lowpan_btle_dev *entry;
  994. struct lowpan_peer *peer;
  995. spin_lock(&devices_lock);
  996. list_for_each_entry(entry, &bt_6lowpan_devices, list) {
  997. list_for_each_entry(peer, &entry->peers, list)
  998. seq_printf(f, "%pMR (type %u)\n",
  999. &peer->chan->dst, peer->chan->dst_type);
  1000. }
  1001. spin_unlock(&devices_lock);
  1002. return 0;
  1003. }
  1004. static int lowpan_control_open(struct inode *inode, struct file *file)
  1005. {
  1006. return single_open(file, lowpan_control_show, inode->i_private);
  1007. }
  1008. static const struct file_operations lowpan_control_fops = {
  1009. .open = lowpan_control_open,
  1010. .read = seq_read,
  1011. .write = lowpan_control_write,
  1012. .llseek = seq_lseek,
  1013. .release = single_release,
  1014. };
  1015. static void disconnect_devices(void)
  1016. {
  1017. struct lowpan_btle_dev *entry, *tmp, *new_dev;
  1018. struct list_head devices;
  1019. INIT_LIST_HEAD(&devices);
  1020. /* We make a separate list of devices because the unregister_netdev()
  1021. * will call device_event() which will also want to modify the same
  1022. * devices list.
  1023. */
  1024. rcu_read_lock();
  1025. list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
  1026. new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
  1027. if (!new_dev)
  1028. break;
  1029. new_dev->netdev = entry->netdev;
  1030. INIT_LIST_HEAD(&new_dev->list);
  1031. list_add_rcu(&new_dev->list, &devices);
  1032. }
  1033. rcu_read_unlock();
  1034. list_for_each_entry_safe(entry, tmp, &devices, list) {
  1035. ifdown(entry->netdev);
  1036. BT_DBG("Unregistering netdev %s %p",
  1037. entry->netdev->name, entry->netdev);
  1038. lowpan_unregister_netdev(entry->netdev);
  1039. kfree(entry);
  1040. }
  1041. }
  1042. static int device_event(struct notifier_block *unused,
  1043. unsigned long event, void *ptr)
  1044. {
  1045. struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
  1046. struct lowpan_btle_dev *entry;
  1047. if (netdev->type != ARPHRD_6LOWPAN)
  1048. return NOTIFY_DONE;
  1049. switch (event) {
  1050. case NETDEV_UNREGISTER:
  1051. spin_lock(&devices_lock);
  1052. list_for_each_entry(entry, &bt_6lowpan_devices, list) {
  1053. if (entry->netdev == netdev) {
  1054. BT_DBG("Unregistered netdev %s %p",
  1055. netdev->name, netdev);
  1056. list_del(&entry->list);
  1057. break;
  1058. }
  1059. }
  1060. spin_unlock(&devices_lock);
  1061. break;
  1062. }
  1063. return NOTIFY_DONE;
  1064. }
  1065. static struct notifier_block bt_6lowpan_dev_notifier = {
  1066. .notifier_call = device_event,
  1067. };
  1068. static int __init bt_6lowpan_init(void)
  1069. {
  1070. lowpan_enable_debugfs = debugfs_create_file("6lowpan_enable", 0644,
  1071. bt_debugfs, NULL,
  1072. &lowpan_enable_fops);
  1073. lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
  1074. bt_debugfs, NULL,
  1075. &lowpan_control_fops);
  1076. return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
  1077. }
  1078. static void __exit bt_6lowpan_exit(void)
  1079. {
  1080. debugfs_remove(lowpan_enable_debugfs);
  1081. debugfs_remove(lowpan_control_debugfs);
  1082. if (listen_chan) {
  1083. l2cap_chan_close(listen_chan, 0);
  1084. l2cap_chan_put(listen_chan);
  1085. }
  1086. disconnect_devices();
  1087. unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
  1088. }
  1089. module_init(bt_6lowpan_init);
  1090. module_exit(bt_6lowpan_exit);
  1091. MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
  1092. MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
  1093. MODULE_VERSION(VERSION);
  1094. MODULE_LICENSE("GPL");