socket.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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 <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. proto = &ieee802154_raw_prot;
  832. ops = &ieee802154_raw_ops;
  833. break;
  834. case SOCK_DGRAM:
  835. proto = &ieee802154_dgram_prot;
  836. ops = &ieee802154_dgram_ops;
  837. break;
  838. default:
  839. rc = -ESOCKTNOSUPPORT;
  840. goto out;
  841. }
  842. rc = -ENOMEM;
  843. sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto, kern);
  844. if (!sk)
  845. goto out;
  846. rc = 0;
  847. sock->ops = ops;
  848. sock_init_data(sock, sk);
  849. /* FIXME: sk->sk_destruct */
  850. sk->sk_family = PF_IEEE802154;
  851. /* Checksums on by default */
  852. sock_set_flag(sk, SOCK_ZAPPED);
  853. if (sk->sk_prot->hash) {
  854. rc = sk->sk_prot->hash(sk);
  855. if (rc) {
  856. sk_common_release(sk);
  857. goto out;
  858. }
  859. }
  860. if (sk->sk_prot->init) {
  861. rc = sk->sk_prot->init(sk);
  862. if (rc)
  863. sk_common_release(sk);
  864. }
  865. out:
  866. return rc;
  867. }
  868. static const struct net_proto_family ieee802154_family_ops = {
  869. .family = PF_IEEE802154,
  870. .create = ieee802154_create,
  871. .owner = THIS_MODULE,
  872. };
  873. static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev,
  874. struct packet_type *pt, struct net_device *orig_dev)
  875. {
  876. if (!netif_running(dev))
  877. goto drop;
  878. pr_debug("got frame, type %d, dev %p\n", dev->type, dev);
  879. #ifdef DEBUG
  880. print_hex_dump_bytes("ieee802154_rcv ",
  881. DUMP_PREFIX_NONE, skb->data, skb->len);
  882. #endif
  883. if (!net_eq(dev_net(dev), &init_net))
  884. goto drop;
  885. ieee802154_raw_deliver(dev, skb);
  886. if (dev->type != ARPHRD_IEEE802154)
  887. goto drop;
  888. if (skb->pkt_type != PACKET_OTHERHOST)
  889. return ieee802154_dgram_deliver(dev, skb);
  890. drop:
  891. kfree_skb(skb);
  892. return NET_RX_DROP;
  893. }
  894. static struct packet_type ieee802154_packet_type = {
  895. .type = htons(ETH_P_IEEE802154),
  896. .func = ieee802154_rcv,
  897. };
  898. static int __init af_ieee802154_init(void)
  899. {
  900. int rc = -EINVAL;
  901. rc = proto_register(&ieee802154_raw_prot, 1);
  902. if (rc)
  903. goto out;
  904. rc = proto_register(&ieee802154_dgram_prot, 1);
  905. if (rc)
  906. goto err_dgram;
  907. /* Tell SOCKET that we are alive */
  908. rc = sock_register(&ieee802154_family_ops);
  909. if (rc)
  910. goto err_sock;
  911. dev_add_pack(&ieee802154_packet_type);
  912. rc = 0;
  913. goto out;
  914. err_sock:
  915. proto_unregister(&ieee802154_dgram_prot);
  916. err_dgram:
  917. proto_unregister(&ieee802154_raw_prot);
  918. out:
  919. return rc;
  920. }
  921. static void __exit af_ieee802154_remove(void)
  922. {
  923. dev_remove_pack(&ieee802154_packet_type);
  924. sock_unregister(PF_IEEE802154);
  925. proto_unregister(&ieee802154_dgram_prot);
  926. proto_unregister(&ieee802154_raw_prot);
  927. }
  928. module_init(af_ieee802154_init);
  929. module_exit(af_ieee802154_remove);
  930. MODULE_LICENSE("GPL");
  931. MODULE_ALIAS_NETPROTO(PF_IEEE802154);