socket.c 23 KB

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