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