socket.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. /*
  2. * IEEE802154.4 socket interface
  3. *
  4. * Copyright 2007, 2008 Siemens AG
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * Written by:
  16. * Sergey Lapin <slapin@ossfans.org>
  17. * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
  18. */
  19. #include <linux/net.h>
  20. #include <linux/capability.h>
  21. #include <linux/module.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/if.h>
  24. #include <linux/termios.h> /* For TIOCOUTQ/INQ */
  25. #include <linux/list.h>
  26. #include <linux/slab.h>
  27. #include <linux/socket.h>
  28. #include <net/datalink.h>
  29. #include <net/psnap.h>
  30. #include <net/sock.h>
  31. #include <net/tcp_states.h>
  32. #include <net/route.h>
  33. #include <net/af_ieee802154.h>
  34. #include <net/ieee802154_netdev.h>
  35. /* Utility function for families */
  36. static struct net_device*
  37. ieee802154_get_dev(struct net *net, const struct ieee802154_addr *addr)
  38. {
  39. struct net_device *dev = NULL;
  40. struct net_device *tmp;
  41. __le16 pan_id, short_addr;
  42. u8 hwaddr[IEEE802154_ADDR_LEN];
  43. switch (addr->mode) {
  44. case IEEE802154_ADDR_LONG:
  45. ieee802154_devaddr_to_raw(hwaddr, addr->extended_addr);
  46. rcu_read_lock();
  47. dev = dev_getbyhwaddr_rcu(net, ARPHRD_IEEE802154, hwaddr);
  48. if (dev)
  49. dev_hold(dev);
  50. rcu_read_unlock();
  51. break;
  52. case IEEE802154_ADDR_SHORT:
  53. if (addr->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST) ||
  54. addr->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
  55. addr->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST))
  56. break;
  57. rtnl_lock();
  58. for_each_netdev(net, tmp) {
  59. if (tmp->type != ARPHRD_IEEE802154)
  60. continue;
  61. pan_id = tmp->ieee802154_ptr->pan_id;
  62. short_addr = tmp->ieee802154_ptr->short_addr;
  63. if (pan_id == addr->pan_id &&
  64. short_addr == addr->short_addr) {
  65. dev = tmp;
  66. dev_hold(dev);
  67. break;
  68. }
  69. }
  70. rtnl_unlock();
  71. break;
  72. default:
  73. pr_warn("Unsupported ieee802154 address type: %d\n",
  74. addr->mode);
  75. break;
  76. }
  77. return dev;
  78. }
  79. static int ieee802154_sock_release(struct socket *sock)
  80. {
  81. struct sock *sk = sock->sk;
  82. if (sk) {
  83. sock->sk = NULL;
  84. sk->sk_prot->close(sk, 0);
  85. }
  86. return 0;
  87. }
  88. static int ieee802154_sock_sendmsg(struct socket *sock, struct msghdr *msg,
  89. size_t len)
  90. {
  91. struct sock *sk = sock->sk;
  92. return sk->sk_prot->sendmsg(sk, msg, len);
  93. }
  94. static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr,
  95. int addr_len)
  96. {
  97. struct sock *sk = sock->sk;
  98. if (sk->sk_prot->bind)
  99. return sk->sk_prot->bind(sk, uaddr, addr_len);
  100. return sock_no_bind(sock, uaddr, addr_len);
  101. }
  102. static int ieee802154_sock_connect(struct socket *sock, struct sockaddr *uaddr,
  103. int addr_len, int flags)
  104. {
  105. struct sock *sk = sock->sk;
  106. if (addr_len < sizeof(uaddr->sa_family))
  107. return -EINVAL;
  108. if (uaddr->sa_family == AF_UNSPEC)
  109. return sk->sk_prot->disconnect(sk, flags);
  110. return sk->sk_prot->connect(sk, uaddr, addr_len);
  111. }
  112. static int ieee802154_dev_ioctl(struct sock *sk, struct ifreq __user *arg,
  113. unsigned int cmd)
  114. {
  115. struct ifreq ifr;
  116. int ret = -ENOIOCTLCMD;
  117. struct net_device *dev;
  118. if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
  119. return -EFAULT;
  120. ifr.ifr_name[IFNAMSIZ-1] = 0;
  121. dev_load(sock_net(sk), ifr.ifr_name);
  122. dev = dev_get_by_name(sock_net(sk), ifr.ifr_name);
  123. if (!dev)
  124. return -ENODEV;
  125. if (dev->type == ARPHRD_IEEE802154 && dev->netdev_ops->ndo_do_ioctl)
  126. ret = dev->netdev_ops->ndo_do_ioctl(dev, &ifr, cmd);
  127. if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
  128. ret = -EFAULT;
  129. dev_put(dev);
  130. return ret;
  131. }
  132. static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
  133. unsigned long arg)
  134. {
  135. struct sock *sk = sock->sk;
  136. switch (cmd) {
  137. case SIOCGSTAMP:
  138. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  139. case SIOCGSTAMPNS:
  140. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  141. case SIOCGIFADDR:
  142. case SIOCSIFADDR:
  143. return ieee802154_dev_ioctl(sk, (struct ifreq __user *)arg,
  144. cmd);
  145. default:
  146. if (!sk->sk_prot->ioctl)
  147. return -ENOIOCTLCMD;
  148. return sk->sk_prot->ioctl(sk, cmd, arg);
  149. }
  150. }
  151. /* RAW Sockets (802.15.4 created in userspace) */
  152. static HLIST_HEAD(raw_head);
  153. static DEFINE_RWLOCK(raw_lock);
  154. static int raw_hash(struct sock *sk)
  155. {
  156. write_lock_bh(&raw_lock);
  157. sk_add_node(sk, &raw_head);
  158. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  159. write_unlock_bh(&raw_lock);
  160. return 0;
  161. }
  162. static void raw_unhash(struct sock *sk)
  163. {
  164. write_lock_bh(&raw_lock);
  165. if (sk_del_node_init(sk))
  166. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  167. write_unlock_bh(&raw_lock);
  168. }
  169. static void raw_close(struct sock *sk, long timeout)
  170. {
  171. sk_common_release(sk);
  172. }
  173. static int raw_bind(struct sock *sk, struct sockaddr *_uaddr, int len)
  174. {
  175. struct ieee802154_addr addr;
  176. struct sockaddr_ieee802154 *uaddr = (struct sockaddr_ieee802154 *)_uaddr;
  177. int err = 0;
  178. struct net_device *dev = NULL;
  179. if (len < sizeof(*uaddr))
  180. return -EINVAL;
  181. uaddr = (struct sockaddr_ieee802154 *)_uaddr;
  182. if (uaddr->family != AF_IEEE802154)
  183. return -EINVAL;
  184. lock_sock(sk);
  185. ieee802154_addr_from_sa(&addr, &uaddr->addr);
  186. dev = ieee802154_get_dev(sock_net(sk), &addr);
  187. if (!dev) {
  188. err = -ENODEV;
  189. goto out;
  190. }
  191. sk->sk_bound_dev_if = dev->ifindex;
  192. sk_dst_reset(sk);
  193. dev_put(dev);
  194. out:
  195. release_sock(sk);
  196. return err;
  197. }
  198. static int raw_connect(struct sock *sk, struct sockaddr *uaddr,
  199. int addr_len)
  200. {
  201. return -ENOTSUPP;
  202. }
  203. static int raw_disconnect(struct sock *sk, int flags)
  204. {
  205. return 0;
  206. }
  207. static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
  208. {
  209. struct net_device *dev;
  210. unsigned int mtu;
  211. struct sk_buff *skb;
  212. int hlen, tlen;
  213. int err;
  214. if (msg->msg_flags & MSG_OOB) {
  215. pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
  216. return -EOPNOTSUPP;
  217. }
  218. lock_sock(sk);
  219. if (!sk->sk_bound_dev_if)
  220. dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
  221. else
  222. dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
  223. release_sock(sk);
  224. if (!dev) {
  225. pr_debug("no dev\n");
  226. err = -ENXIO;
  227. goto out;
  228. }
  229. mtu = IEEE802154_MTU;
  230. pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
  231. if (size > mtu) {
  232. pr_debug("size = %zu, mtu = %u\n", size, mtu);
  233. err = -EMSGSIZE;
  234. goto out_dev;
  235. }
  236. hlen = LL_RESERVED_SPACE(dev);
  237. tlen = dev->needed_tailroom;
  238. skb = sock_alloc_send_skb(sk, hlen + tlen + size,
  239. msg->msg_flags & MSG_DONTWAIT, &err);
  240. if (!skb)
  241. goto out_dev;
  242. skb_reserve(skb, hlen);
  243. skb_reset_mac_header(skb);
  244. skb_reset_network_header(skb);
  245. err = memcpy_from_msg(skb_put(skb, size), msg, size);
  246. if (err < 0)
  247. goto out_skb;
  248. skb->dev = dev;
  249. skb->protocol = htons(ETH_P_IEEE802154);
  250. err = dev_queue_xmit(skb);
  251. if (err > 0)
  252. err = net_xmit_errno(err);
  253. dev_put(dev);
  254. return err ?: size;
  255. out_skb:
  256. kfree_skb(skb);
  257. out_dev:
  258. dev_put(dev);
  259. out:
  260. return err;
  261. }
  262. static int raw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  263. int noblock, int flags, int *addr_len)
  264. {
  265. size_t copied = 0;
  266. int err = -EOPNOTSUPP;
  267. struct sk_buff *skb;
  268. skb = skb_recv_datagram(sk, flags, noblock, &err);
  269. if (!skb)
  270. goto out;
  271. copied = skb->len;
  272. if (len < copied) {
  273. msg->msg_flags |= MSG_TRUNC;
  274. copied = len;
  275. }
  276. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  277. if (err)
  278. goto done;
  279. sock_recv_ts_and_drops(msg, sk, skb);
  280. if (flags & MSG_TRUNC)
  281. copied = skb->len;
  282. done:
  283. skb_free_datagram(sk, skb);
  284. out:
  285. if (err)
  286. return err;
  287. return copied;
  288. }
  289. static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb)
  290. {
  291. skb = skb_share_check(skb, GFP_ATOMIC);
  292. if (!skb)
  293. return NET_RX_DROP;
  294. if (sock_queue_rcv_skb(sk, skb) < 0) {
  295. kfree_skb(skb);
  296. return NET_RX_DROP;
  297. }
  298. return NET_RX_SUCCESS;
  299. }
  300. static void ieee802154_raw_deliver(struct net_device *dev, struct sk_buff *skb)
  301. {
  302. struct sock *sk;
  303. read_lock(&raw_lock);
  304. sk_for_each(sk, &raw_head) {
  305. bh_lock_sock(sk);
  306. if (!sk->sk_bound_dev_if ||
  307. sk->sk_bound_dev_if == dev->ifindex) {
  308. struct sk_buff *clone;
  309. clone = skb_clone(skb, GFP_ATOMIC);
  310. if (clone)
  311. raw_rcv_skb(sk, clone);
  312. }
  313. bh_unlock_sock(sk);
  314. }
  315. read_unlock(&raw_lock);
  316. }
  317. static int raw_getsockopt(struct sock *sk, int level, int optname,
  318. char __user *optval, int __user *optlen)
  319. {
  320. return -EOPNOTSUPP;
  321. }
  322. static int raw_setsockopt(struct sock *sk, int level, int optname,
  323. char __user *optval, unsigned int optlen)
  324. {
  325. return -EOPNOTSUPP;
  326. }
  327. static struct proto ieee802154_raw_prot = {
  328. .name = "IEEE-802.15.4-RAW",
  329. .owner = THIS_MODULE,
  330. .obj_size = sizeof(struct sock),
  331. .close = raw_close,
  332. .bind = raw_bind,
  333. .sendmsg = raw_sendmsg,
  334. .recvmsg = raw_recvmsg,
  335. .hash = raw_hash,
  336. .unhash = raw_unhash,
  337. .connect = raw_connect,
  338. .disconnect = raw_disconnect,
  339. .getsockopt = raw_getsockopt,
  340. .setsockopt = raw_setsockopt,
  341. };
  342. static const struct proto_ops ieee802154_raw_ops = {
  343. .family = PF_IEEE802154,
  344. .owner = THIS_MODULE,
  345. .release = ieee802154_sock_release,
  346. .bind = ieee802154_sock_bind,
  347. .connect = ieee802154_sock_connect,
  348. .socketpair = sock_no_socketpair,
  349. .accept = sock_no_accept,
  350. .getname = sock_no_getname,
  351. .poll = datagram_poll,
  352. .ioctl = ieee802154_sock_ioctl,
  353. .listen = sock_no_listen,
  354. .shutdown = sock_no_shutdown,
  355. .setsockopt = sock_common_setsockopt,
  356. .getsockopt = sock_common_getsockopt,
  357. .sendmsg = ieee802154_sock_sendmsg,
  358. .recvmsg = sock_common_recvmsg,
  359. .mmap = sock_no_mmap,
  360. .sendpage = sock_no_sendpage,
  361. #ifdef CONFIG_COMPAT
  362. .compat_setsockopt = compat_sock_common_setsockopt,
  363. .compat_getsockopt = compat_sock_common_getsockopt,
  364. #endif
  365. };
  366. /* DGRAM Sockets (802.15.4 dataframes) */
  367. static HLIST_HEAD(dgram_head);
  368. static DEFINE_RWLOCK(dgram_lock);
  369. struct dgram_sock {
  370. struct sock sk;
  371. struct ieee802154_addr src_addr;
  372. struct ieee802154_addr dst_addr;
  373. unsigned int bound:1;
  374. unsigned int connected:1;
  375. unsigned int want_ack:1;
  376. unsigned int want_lqi:1;
  377. unsigned int secen:1;
  378. unsigned int secen_override:1;
  379. unsigned int seclevel:3;
  380. unsigned int seclevel_override:1;
  381. };
  382. static inline struct dgram_sock *dgram_sk(const struct sock *sk)
  383. {
  384. return container_of(sk, struct dgram_sock, sk);
  385. }
  386. static int dgram_hash(struct sock *sk)
  387. {
  388. write_lock_bh(&dgram_lock);
  389. sk_add_node(sk, &dgram_head);
  390. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  391. write_unlock_bh(&dgram_lock);
  392. return 0;
  393. }
  394. static void dgram_unhash(struct sock *sk)
  395. {
  396. write_lock_bh(&dgram_lock);
  397. if (sk_del_node_init(sk))
  398. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  399. write_unlock_bh(&dgram_lock);
  400. }
  401. static int dgram_init(struct sock *sk)
  402. {
  403. struct dgram_sock *ro = dgram_sk(sk);
  404. ro->want_ack = 1;
  405. ro->want_lqi = 0;
  406. return 0;
  407. }
  408. static void dgram_close(struct sock *sk, long timeout)
  409. {
  410. sk_common_release(sk);
  411. }
  412. static int dgram_bind(struct sock *sk, struct sockaddr *uaddr, int len)
  413. {
  414. struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
  415. struct ieee802154_addr haddr;
  416. struct dgram_sock *ro = dgram_sk(sk);
  417. int err = -EINVAL;
  418. struct net_device *dev;
  419. lock_sock(sk);
  420. ro->bound = 0;
  421. if (len < sizeof(*addr))
  422. goto out;
  423. if (addr->family != AF_IEEE802154)
  424. goto out;
  425. ieee802154_addr_from_sa(&haddr, &addr->addr);
  426. dev = ieee802154_get_dev(sock_net(sk), &haddr);
  427. if (!dev) {
  428. err = -ENODEV;
  429. goto out;
  430. }
  431. if (dev->type != ARPHRD_IEEE802154) {
  432. err = -ENODEV;
  433. goto out_put;
  434. }
  435. ro->src_addr = haddr;
  436. ro->bound = 1;
  437. err = 0;
  438. out_put:
  439. dev_put(dev);
  440. out:
  441. release_sock(sk);
  442. return err;
  443. }
  444. static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg)
  445. {
  446. switch (cmd) {
  447. case SIOCOUTQ:
  448. {
  449. int amount = sk_wmem_alloc_get(sk);
  450. return put_user(amount, (int __user *)arg);
  451. }
  452. case SIOCINQ:
  453. {
  454. struct sk_buff *skb;
  455. unsigned long amount;
  456. amount = 0;
  457. spin_lock_bh(&sk->sk_receive_queue.lock);
  458. skb = skb_peek(&sk->sk_receive_queue);
  459. if (skb) {
  460. /* We will only return the amount
  461. * of this packet since that is all
  462. * that will be read.
  463. */
  464. amount = skb->len - ieee802154_hdr_length(skb);
  465. }
  466. spin_unlock_bh(&sk->sk_receive_queue.lock);
  467. return put_user(amount, (int __user *)arg);
  468. }
  469. }
  470. return -ENOIOCTLCMD;
  471. }
  472. /* FIXME: autobind */
  473. static int dgram_connect(struct sock *sk, struct sockaddr *uaddr,
  474. int len)
  475. {
  476. struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
  477. struct dgram_sock *ro = dgram_sk(sk);
  478. int err = 0;
  479. if (len < sizeof(*addr))
  480. return -EINVAL;
  481. if (addr->family != AF_IEEE802154)
  482. return -EINVAL;
  483. lock_sock(sk);
  484. if (!ro->bound) {
  485. err = -ENETUNREACH;
  486. goto out;
  487. }
  488. ieee802154_addr_from_sa(&ro->dst_addr, &addr->addr);
  489. ro->connected = 1;
  490. out:
  491. release_sock(sk);
  492. return err;
  493. }
  494. static int dgram_disconnect(struct sock *sk, int flags)
  495. {
  496. struct dgram_sock *ro = dgram_sk(sk);
  497. lock_sock(sk);
  498. ro->connected = 0;
  499. release_sock(sk);
  500. return 0;
  501. }
  502. static int dgram_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
  503. {
  504. struct net_device *dev;
  505. unsigned int mtu;
  506. struct sk_buff *skb;
  507. struct ieee802154_mac_cb *cb;
  508. struct dgram_sock *ro = dgram_sk(sk);
  509. struct ieee802154_addr dst_addr;
  510. int hlen, tlen;
  511. int err;
  512. if (msg->msg_flags & MSG_OOB) {
  513. pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
  514. return -EOPNOTSUPP;
  515. }
  516. if (!ro->connected && !msg->msg_name)
  517. return -EDESTADDRREQ;
  518. else if (ro->connected && msg->msg_name)
  519. return -EISCONN;
  520. if (!ro->bound)
  521. dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
  522. else
  523. dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr);
  524. if (!dev) {
  525. pr_debug("no dev\n");
  526. err = -ENXIO;
  527. goto out;
  528. }
  529. mtu = IEEE802154_MTU;
  530. pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
  531. if (size > mtu) {
  532. pr_debug("size = %zu, mtu = %u\n", size, mtu);
  533. err = -EMSGSIZE;
  534. goto out_dev;
  535. }
  536. hlen = LL_RESERVED_SPACE(dev);
  537. tlen = dev->needed_tailroom;
  538. skb = sock_alloc_send_skb(sk, hlen + tlen + size,
  539. msg->msg_flags & MSG_DONTWAIT,
  540. &err);
  541. if (!skb)
  542. goto out_dev;
  543. skb_reserve(skb, hlen);
  544. skb_reset_network_header(skb);
  545. cb = mac_cb_init(skb);
  546. cb->type = IEEE802154_FC_TYPE_DATA;
  547. cb->ackreq = ro->want_ack;
  548. if (msg->msg_name) {
  549. DECLARE_SOCKADDR(struct sockaddr_ieee802154*,
  550. daddr, msg->msg_name);
  551. ieee802154_addr_from_sa(&dst_addr, &daddr->addr);
  552. } else {
  553. dst_addr = ro->dst_addr;
  554. }
  555. cb->secen = ro->secen;
  556. cb->secen_override = ro->secen_override;
  557. cb->seclevel = ro->seclevel;
  558. cb->seclevel_override = ro->seclevel_override;
  559. err = wpan_dev_hard_header(skb, dev, &dst_addr,
  560. ro->bound ? &ro->src_addr : NULL, size);
  561. if (err < 0)
  562. goto out_skb;
  563. err = memcpy_from_msg(skb_put(skb, size), msg, size);
  564. if (err < 0)
  565. goto out_skb;
  566. skb->dev = dev;
  567. skb->protocol = htons(ETH_P_IEEE802154);
  568. err = dev_queue_xmit(skb);
  569. if (err > 0)
  570. err = net_xmit_errno(err);
  571. dev_put(dev);
  572. return err ?: size;
  573. out_skb:
  574. kfree_skb(skb);
  575. out_dev:
  576. dev_put(dev);
  577. out:
  578. return err;
  579. }
  580. static int dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  581. int noblock, int flags, int *addr_len)
  582. {
  583. size_t copied = 0;
  584. int err = -EOPNOTSUPP;
  585. struct sk_buff *skb;
  586. struct dgram_sock *ro = dgram_sk(sk);
  587. DECLARE_SOCKADDR(struct sockaddr_ieee802154 *, saddr, msg->msg_name);
  588. skb = skb_recv_datagram(sk, flags, noblock, &err);
  589. if (!skb)
  590. goto out;
  591. copied = skb->len;
  592. if (len < copied) {
  593. msg->msg_flags |= MSG_TRUNC;
  594. copied = len;
  595. }
  596. /* FIXME: skip headers if necessary ?! */
  597. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  598. if (err)
  599. goto done;
  600. sock_recv_ts_and_drops(msg, sk, skb);
  601. if (saddr) {
  602. /* Clear the implicit padding in struct sockaddr_ieee802154
  603. * (16 bits between 'family' and 'addr') and in struct
  604. * ieee802154_addr_sa (16 bits at the end of the structure).
  605. */
  606. memset(saddr, 0, sizeof(*saddr));
  607. saddr->family = AF_IEEE802154;
  608. ieee802154_addr_to_sa(&saddr->addr, &mac_cb(skb)->source);
  609. *addr_len = sizeof(*saddr);
  610. }
  611. if (ro->want_lqi) {
  612. err = put_cmsg(msg, SOL_IEEE802154, WPAN_WANTLQI,
  613. sizeof(uint8_t), &(mac_cb(skb)->lqi));
  614. if (err)
  615. goto done;
  616. }
  617. if (flags & MSG_TRUNC)
  618. copied = skb->len;
  619. done:
  620. skb_free_datagram(sk, skb);
  621. out:
  622. if (err)
  623. return err;
  624. return copied;
  625. }
  626. static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
  627. {
  628. skb = skb_share_check(skb, GFP_ATOMIC);
  629. if (!skb)
  630. return NET_RX_DROP;
  631. if (sock_queue_rcv_skb(sk, skb) < 0) {
  632. kfree_skb(skb);
  633. return NET_RX_DROP;
  634. }
  635. return NET_RX_SUCCESS;
  636. }
  637. static inline bool
  638. ieee802154_match_sock(__le64 hw_addr, __le16 pan_id, __le16 short_addr,
  639. struct dgram_sock *ro)
  640. {
  641. if (!ro->bound)
  642. return true;
  643. if (ro->src_addr.mode == IEEE802154_ADDR_LONG &&
  644. hw_addr == ro->src_addr.extended_addr)
  645. return true;
  646. if (ro->src_addr.mode == IEEE802154_ADDR_SHORT &&
  647. pan_id == ro->src_addr.pan_id &&
  648. short_addr == ro->src_addr.short_addr)
  649. return true;
  650. return false;
  651. }
  652. static int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
  653. {
  654. struct sock *sk, *prev = NULL;
  655. int ret = NET_RX_SUCCESS;
  656. __le16 pan_id, short_addr;
  657. __le64 hw_addr;
  658. /* Data frame processing */
  659. BUG_ON(dev->type != ARPHRD_IEEE802154);
  660. pan_id = dev->ieee802154_ptr->pan_id;
  661. short_addr = dev->ieee802154_ptr->short_addr;
  662. hw_addr = dev->ieee802154_ptr->extended_addr;
  663. read_lock(&dgram_lock);
  664. sk_for_each(sk, &dgram_head) {
  665. if (ieee802154_match_sock(hw_addr, pan_id, short_addr,
  666. dgram_sk(sk))) {
  667. if (prev) {
  668. struct sk_buff *clone;
  669. clone = skb_clone(skb, GFP_ATOMIC);
  670. if (clone)
  671. dgram_rcv_skb(prev, clone);
  672. }
  673. prev = sk;
  674. }
  675. }
  676. if (prev) {
  677. dgram_rcv_skb(prev, skb);
  678. } else {
  679. kfree_skb(skb);
  680. ret = NET_RX_DROP;
  681. }
  682. read_unlock(&dgram_lock);
  683. return ret;
  684. }
  685. static int dgram_getsockopt(struct sock *sk, int level, int optname,
  686. char __user *optval, int __user *optlen)
  687. {
  688. struct dgram_sock *ro = dgram_sk(sk);
  689. int val, len;
  690. if (level != SOL_IEEE802154)
  691. return -EOPNOTSUPP;
  692. if (get_user(len, optlen))
  693. return -EFAULT;
  694. len = min_t(unsigned int, len, sizeof(int));
  695. switch (optname) {
  696. case WPAN_WANTACK:
  697. val = ro->want_ack;
  698. break;
  699. case WPAN_WANTLQI:
  700. val = ro->want_lqi;
  701. break;
  702. case WPAN_SECURITY:
  703. if (!ro->secen_override)
  704. val = WPAN_SECURITY_DEFAULT;
  705. else if (ro->secen)
  706. val = WPAN_SECURITY_ON;
  707. else
  708. val = WPAN_SECURITY_OFF;
  709. break;
  710. case WPAN_SECURITY_LEVEL:
  711. if (!ro->seclevel_override)
  712. val = WPAN_SECURITY_LEVEL_DEFAULT;
  713. else
  714. val = ro->seclevel;
  715. break;
  716. default:
  717. return -ENOPROTOOPT;
  718. }
  719. if (put_user(len, optlen))
  720. return -EFAULT;
  721. if (copy_to_user(optval, &val, len))
  722. return -EFAULT;
  723. return 0;
  724. }
  725. static int dgram_setsockopt(struct sock *sk, int level, int optname,
  726. char __user *optval, unsigned int optlen)
  727. {
  728. struct dgram_sock *ro = dgram_sk(sk);
  729. struct net *net = sock_net(sk);
  730. int val;
  731. int err = 0;
  732. if (optlen < sizeof(int))
  733. return -EINVAL;
  734. if (get_user(val, (int __user *)optval))
  735. return -EFAULT;
  736. lock_sock(sk);
  737. switch (optname) {
  738. case WPAN_WANTACK:
  739. ro->want_ack = !!val;
  740. break;
  741. case WPAN_WANTLQI:
  742. ro->want_lqi = !!val;
  743. break;
  744. case WPAN_SECURITY:
  745. if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
  746. !ns_capable(net->user_ns, CAP_NET_RAW)) {
  747. err = -EPERM;
  748. break;
  749. }
  750. switch (val) {
  751. case WPAN_SECURITY_DEFAULT:
  752. ro->secen_override = 0;
  753. break;
  754. case WPAN_SECURITY_ON:
  755. ro->secen_override = 1;
  756. ro->secen = 1;
  757. break;
  758. case WPAN_SECURITY_OFF:
  759. ro->secen_override = 1;
  760. ro->secen = 0;
  761. break;
  762. default:
  763. err = -EINVAL;
  764. break;
  765. }
  766. break;
  767. case WPAN_SECURITY_LEVEL:
  768. if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
  769. !ns_capable(net->user_ns, CAP_NET_RAW)) {
  770. err = -EPERM;
  771. break;
  772. }
  773. if (val < WPAN_SECURITY_LEVEL_DEFAULT ||
  774. val > IEEE802154_SCF_SECLEVEL_ENC_MIC128) {
  775. err = -EINVAL;
  776. } else if (val == WPAN_SECURITY_LEVEL_DEFAULT) {
  777. ro->seclevel_override = 0;
  778. } else {
  779. ro->seclevel_override = 1;
  780. ro->seclevel = val;
  781. }
  782. break;
  783. default:
  784. err = -ENOPROTOOPT;
  785. break;
  786. }
  787. release_sock(sk);
  788. return err;
  789. }
  790. static struct proto ieee802154_dgram_prot = {
  791. .name = "IEEE-802.15.4-MAC",
  792. .owner = THIS_MODULE,
  793. .obj_size = sizeof(struct dgram_sock),
  794. .init = dgram_init,
  795. .close = dgram_close,
  796. .bind = dgram_bind,
  797. .sendmsg = dgram_sendmsg,
  798. .recvmsg = dgram_recvmsg,
  799. .hash = dgram_hash,
  800. .unhash = dgram_unhash,
  801. .connect = dgram_connect,
  802. .disconnect = dgram_disconnect,
  803. .ioctl = dgram_ioctl,
  804. .getsockopt = dgram_getsockopt,
  805. .setsockopt = dgram_setsockopt,
  806. };
  807. static const struct proto_ops ieee802154_dgram_ops = {
  808. .family = PF_IEEE802154,
  809. .owner = THIS_MODULE,
  810. .release = ieee802154_sock_release,
  811. .bind = ieee802154_sock_bind,
  812. .connect = ieee802154_sock_connect,
  813. .socketpair = sock_no_socketpair,
  814. .accept = sock_no_accept,
  815. .getname = sock_no_getname,
  816. .poll = datagram_poll,
  817. .ioctl = ieee802154_sock_ioctl,
  818. .listen = sock_no_listen,
  819. .shutdown = sock_no_shutdown,
  820. .setsockopt = sock_common_setsockopt,
  821. .getsockopt = sock_common_getsockopt,
  822. .sendmsg = ieee802154_sock_sendmsg,
  823. .recvmsg = sock_common_recvmsg,
  824. .mmap = sock_no_mmap,
  825. .sendpage = sock_no_sendpage,
  826. #ifdef CONFIG_COMPAT
  827. .compat_setsockopt = compat_sock_common_setsockopt,
  828. .compat_getsockopt = compat_sock_common_getsockopt,
  829. #endif
  830. };
  831. /* Create a socket. Initialise the socket, blank the addresses
  832. * set the state.
  833. */
  834. static int ieee802154_create(struct net *net, struct socket *sock,
  835. int protocol, int kern)
  836. {
  837. struct sock *sk;
  838. int rc;
  839. struct proto *proto;
  840. const struct proto_ops *ops;
  841. if (!net_eq(net, &init_net))
  842. return -EAFNOSUPPORT;
  843. switch (sock->type) {
  844. case SOCK_RAW:
  845. rc = -EPERM;
  846. if (!capable(CAP_NET_RAW))
  847. goto out;
  848. proto = &ieee802154_raw_prot;
  849. ops = &ieee802154_raw_ops;
  850. break;
  851. case SOCK_DGRAM:
  852. proto = &ieee802154_dgram_prot;
  853. ops = &ieee802154_dgram_ops;
  854. break;
  855. default:
  856. rc = -ESOCKTNOSUPPORT;
  857. goto out;
  858. }
  859. rc = -ENOMEM;
  860. sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto, kern);
  861. if (!sk)
  862. goto out;
  863. rc = 0;
  864. sock->ops = ops;
  865. sock_init_data(sock, sk);
  866. /* FIXME: sk->sk_destruct */
  867. sk->sk_family = PF_IEEE802154;
  868. /* Checksums on by default */
  869. sock_set_flag(sk, SOCK_ZAPPED);
  870. if (sk->sk_prot->hash) {
  871. rc = sk->sk_prot->hash(sk);
  872. if (rc) {
  873. sk_common_release(sk);
  874. goto out;
  875. }
  876. }
  877. if (sk->sk_prot->init) {
  878. rc = sk->sk_prot->init(sk);
  879. if (rc)
  880. sk_common_release(sk);
  881. }
  882. out:
  883. return rc;
  884. }
  885. static const struct net_proto_family ieee802154_family_ops = {
  886. .family = PF_IEEE802154,
  887. .create = ieee802154_create,
  888. .owner = THIS_MODULE,
  889. };
  890. static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev,
  891. struct packet_type *pt, struct net_device *orig_dev)
  892. {
  893. if (!netif_running(dev))
  894. goto drop;
  895. pr_debug("got frame, type %d, dev %p\n", dev->type, dev);
  896. #ifdef DEBUG
  897. print_hex_dump_bytes("ieee802154_rcv ",
  898. DUMP_PREFIX_NONE, skb->data, skb->len);
  899. #endif
  900. if (!net_eq(dev_net(dev), &init_net))
  901. goto drop;
  902. ieee802154_raw_deliver(dev, skb);
  903. if (dev->type != ARPHRD_IEEE802154)
  904. goto drop;
  905. if (skb->pkt_type != PACKET_OTHERHOST)
  906. return ieee802154_dgram_deliver(dev, skb);
  907. drop:
  908. kfree_skb(skb);
  909. return NET_RX_DROP;
  910. }
  911. static struct packet_type ieee802154_packet_type = {
  912. .type = htons(ETH_P_IEEE802154),
  913. .func = ieee802154_rcv,
  914. };
  915. static int __init af_ieee802154_init(void)
  916. {
  917. int rc = -EINVAL;
  918. rc = proto_register(&ieee802154_raw_prot, 1);
  919. if (rc)
  920. goto out;
  921. rc = proto_register(&ieee802154_dgram_prot, 1);
  922. if (rc)
  923. goto err_dgram;
  924. /* Tell SOCKET that we are alive */
  925. rc = sock_register(&ieee802154_family_ops);
  926. if (rc)
  927. goto err_sock;
  928. dev_add_pack(&ieee802154_packet_type);
  929. rc = 0;
  930. goto out;
  931. err_sock:
  932. proto_unregister(&ieee802154_dgram_prot);
  933. err_dgram:
  934. proto_unregister(&ieee802154_raw_prot);
  935. out:
  936. return rc;
  937. }
  938. static void __exit af_ieee802154_remove(void)
  939. {
  940. dev_remove_pack(&ieee802154_packet_type);
  941. sock_unregister(PF_IEEE802154);
  942. proto_unregister(&ieee802154_dgram_prot);
  943. proto_unregister(&ieee802154_raw_prot);
  944. }
  945. module_init(af_ieee802154_init);
  946. module_exit(af_ieee802154_remove);
  947. MODULE_LICENSE("GPL");
  948. /* MODULE_ALIAS_NETPROTO(PF_IEEE802154); */