6lowpan.c 27 KB

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