af_ieee802154.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Written by:
  20. * Sergey Lapin <slapin@ossfans.org>
  21. * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
  22. */
  23. #include <linux/net.h>
  24. #include <linux/capability.h>
  25. #include <linux/module.h>
  26. #include <linux/if_arp.h>
  27. #include <linux/if.h>
  28. #include <linux/termios.h> /* For TIOCOUTQ/INQ */
  29. #include <linux/list.h>
  30. #include <linux/slab.h>
  31. #include <net/datalink.h>
  32. #include <net/psnap.h>
  33. #include <net/sock.h>
  34. #include <net/tcp_states.h>
  35. #include <net/route.h>
  36. #include <net/af_ieee802154.h>
  37. #include <net/ieee802154_netdev.h>
  38. #include "af802154.h"
  39. /*
  40. * Utility function for families
  41. */
  42. struct net_device *ieee802154_get_dev(struct net *net,
  43. struct ieee802154_addr *addr)
  44. {
  45. struct net_device *dev = NULL;
  46. struct net_device *tmp;
  47. u16 pan_id, short_addr;
  48. switch (addr->addr_type) {
  49. case IEEE802154_ADDR_LONG:
  50. rcu_read_lock();
  51. dev = dev_getbyhwaddr_rcu(net, ARPHRD_IEEE802154, addr->hwaddr);
  52. if (dev)
  53. dev_hold(dev);
  54. rcu_read_unlock();
  55. break;
  56. case IEEE802154_ADDR_SHORT:
  57. if (addr->pan_id == 0xffff ||
  58. addr->short_addr == IEEE802154_ADDR_UNDEF ||
  59. addr->short_addr == 0xffff)
  60. break;
  61. rtnl_lock();
  62. for_each_netdev(net, tmp) {
  63. if (tmp->type != ARPHRD_IEEE802154)
  64. continue;
  65. pan_id = ieee802154_mlme_ops(tmp)->get_pan_id(tmp);
  66. short_addr =
  67. ieee802154_mlme_ops(tmp)->get_short_addr(tmp);
  68. if (pan_id == addr->pan_id &&
  69. short_addr == addr->short_addr) {
  70. dev = tmp;
  71. dev_hold(dev);
  72. break;
  73. }
  74. }
  75. rtnl_unlock();
  76. break;
  77. default:
  78. pr_warning("Unsupported ieee802154 address type: %d\n",
  79. addr->addr_type);
  80. break;
  81. }
  82. return dev;
  83. }
  84. static int ieee802154_sock_release(struct socket *sock)
  85. {
  86. struct sock *sk = sock->sk;
  87. if (sk) {
  88. sock->sk = NULL;
  89. sk->sk_prot->close(sk, 0);
  90. }
  91. return 0;
  92. }
  93. static int ieee802154_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
  94. struct msghdr *msg, size_t len)
  95. {
  96. struct sock *sk = sock->sk;
  97. return sk->sk_prot->sendmsg(iocb, sk, msg, len);
  98. }
  99. static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr,
  100. int addr_len)
  101. {
  102. struct sock *sk = sock->sk;
  103. if (sk->sk_prot->bind)
  104. return sk->sk_prot->bind(sk, uaddr, addr_len);
  105. return sock_no_bind(sock, uaddr, addr_len);
  106. }
  107. static int ieee802154_sock_connect(struct socket *sock, struct sockaddr *uaddr,
  108. int addr_len, int flags)
  109. {
  110. struct sock *sk = sock->sk;
  111. if (addr_len < sizeof(uaddr->sa_family))
  112. return -EINVAL;
  113. if (uaddr->sa_family == AF_UNSPEC)
  114. return sk->sk_prot->disconnect(sk, flags);
  115. return sk->sk_prot->connect(sk, uaddr, addr_len);
  116. }
  117. static int ieee802154_dev_ioctl(struct sock *sk, struct ifreq __user *arg,
  118. unsigned int cmd)
  119. {
  120. struct ifreq ifr;
  121. int ret = -ENOIOCTLCMD;
  122. struct net_device *dev;
  123. if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
  124. return -EFAULT;
  125. ifr.ifr_name[IFNAMSIZ-1] = 0;
  126. dev_load(sock_net(sk), ifr.ifr_name);
  127. dev = dev_get_by_name(sock_net(sk), ifr.ifr_name);
  128. if (!dev)
  129. return -ENODEV;
  130. if (dev->type == ARPHRD_IEEE802154 && dev->netdev_ops->ndo_do_ioctl)
  131. ret = dev->netdev_ops->ndo_do_ioctl(dev, &ifr, cmd);
  132. if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
  133. ret = -EFAULT;
  134. dev_put(dev);
  135. return ret;
  136. }
  137. static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
  138. unsigned long arg)
  139. {
  140. struct sock *sk = sock->sk;
  141. switch (cmd) {
  142. case SIOCGSTAMP:
  143. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  144. case SIOCGSTAMPNS:
  145. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  146. case SIOCGIFADDR:
  147. case SIOCSIFADDR:
  148. return ieee802154_dev_ioctl(sk, (struct ifreq __user *)arg,
  149. cmd);
  150. default:
  151. if (!sk->sk_prot->ioctl)
  152. return -ENOIOCTLCMD;
  153. return sk->sk_prot->ioctl(sk, cmd, arg);
  154. }
  155. }
  156. static const struct proto_ops ieee802154_raw_ops = {
  157. .family = PF_IEEE802154,
  158. .owner = THIS_MODULE,
  159. .release = ieee802154_sock_release,
  160. .bind = ieee802154_sock_bind,
  161. .connect = ieee802154_sock_connect,
  162. .socketpair = sock_no_socketpair,
  163. .accept = sock_no_accept,
  164. .getname = sock_no_getname,
  165. .poll = datagram_poll,
  166. .ioctl = ieee802154_sock_ioctl,
  167. .listen = sock_no_listen,
  168. .shutdown = sock_no_shutdown,
  169. .setsockopt = sock_common_setsockopt,
  170. .getsockopt = sock_common_getsockopt,
  171. .sendmsg = ieee802154_sock_sendmsg,
  172. .recvmsg = sock_common_recvmsg,
  173. .mmap = sock_no_mmap,
  174. .sendpage = sock_no_sendpage,
  175. #ifdef CONFIG_COMPAT
  176. .compat_setsockopt = compat_sock_common_setsockopt,
  177. .compat_getsockopt = compat_sock_common_getsockopt,
  178. #endif
  179. };
  180. static const struct proto_ops ieee802154_dgram_ops = {
  181. .family = PF_IEEE802154,
  182. .owner = THIS_MODULE,
  183. .release = ieee802154_sock_release,
  184. .bind = ieee802154_sock_bind,
  185. .connect = ieee802154_sock_connect,
  186. .socketpair = sock_no_socketpair,
  187. .accept = sock_no_accept,
  188. .getname = sock_no_getname,
  189. .poll = datagram_poll,
  190. .ioctl = ieee802154_sock_ioctl,
  191. .listen = sock_no_listen,
  192. .shutdown = sock_no_shutdown,
  193. .setsockopt = sock_common_setsockopt,
  194. .getsockopt = sock_common_getsockopt,
  195. .sendmsg = ieee802154_sock_sendmsg,
  196. .recvmsg = sock_common_recvmsg,
  197. .mmap = sock_no_mmap,
  198. .sendpage = sock_no_sendpage,
  199. #ifdef CONFIG_COMPAT
  200. .compat_setsockopt = compat_sock_common_setsockopt,
  201. .compat_getsockopt = compat_sock_common_getsockopt,
  202. #endif
  203. };
  204. /*
  205. * Create a socket. Initialise the socket, blank the addresses
  206. * set the state.
  207. */
  208. static int ieee802154_create(struct net *net, struct socket *sock,
  209. int protocol, int kern)
  210. {
  211. struct sock *sk;
  212. int rc;
  213. struct proto *proto;
  214. const struct proto_ops *ops;
  215. if (!net_eq(net, &init_net))
  216. return -EAFNOSUPPORT;
  217. switch (sock->type) {
  218. case SOCK_RAW:
  219. proto = &ieee802154_raw_prot;
  220. ops = &ieee802154_raw_ops;
  221. break;
  222. case SOCK_DGRAM:
  223. proto = &ieee802154_dgram_prot;
  224. ops = &ieee802154_dgram_ops;
  225. break;
  226. default:
  227. rc = -ESOCKTNOSUPPORT;
  228. goto out;
  229. }
  230. rc = -ENOMEM;
  231. sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto);
  232. if (!sk)
  233. goto out;
  234. rc = 0;
  235. sock->ops = ops;
  236. sock_init_data(sock, sk);
  237. /* FIXME: sk->sk_destruct */
  238. sk->sk_family = PF_IEEE802154;
  239. /* Checksums on by default */
  240. sock_set_flag(sk, SOCK_ZAPPED);
  241. if (sk->sk_prot->hash)
  242. sk->sk_prot->hash(sk);
  243. if (sk->sk_prot->init) {
  244. rc = sk->sk_prot->init(sk);
  245. if (rc)
  246. sk_common_release(sk);
  247. }
  248. out:
  249. return rc;
  250. }
  251. static const struct net_proto_family ieee802154_family_ops = {
  252. .family = PF_IEEE802154,
  253. .create = ieee802154_create,
  254. .owner = THIS_MODULE,
  255. };
  256. static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev,
  257. struct packet_type *pt, struct net_device *orig_dev)
  258. {
  259. if (!netif_running(dev))
  260. return -ENODEV;
  261. pr_debug("got frame, type %d, dev %p\n", dev->type, dev);
  262. #ifdef DEBUG
  263. print_hex_dump_bytes("ieee802154_rcv ", DUMP_PREFIX_NONE, skb->data, skb->len);
  264. #endif
  265. if (!net_eq(dev_net(dev), &init_net))
  266. goto drop;
  267. ieee802154_raw_deliver(dev, skb);
  268. if (dev->type != ARPHRD_IEEE802154)
  269. goto drop;
  270. if (skb->pkt_type != PACKET_OTHERHOST)
  271. return ieee802154_dgram_deliver(dev, skb);
  272. drop:
  273. kfree_skb(skb);
  274. return NET_RX_DROP;
  275. }
  276. static struct packet_type ieee802154_packet_type = {
  277. .type = __constant_htons(ETH_P_IEEE802154),
  278. .func = ieee802154_rcv,
  279. };
  280. static int __init af_ieee802154_init(void)
  281. {
  282. int rc = -EINVAL;
  283. rc = proto_register(&ieee802154_raw_prot, 1);
  284. if (rc)
  285. goto out;
  286. rc = proto_register(&ieee802154_dgram_prot, 1);
  287. if (rc)
  288. goto err_dgram;
  289. /* Tell SOCKET that we are alive */
  290. rc = sock_register(&ieee802154_family_ops);
  291. if (rc)
  292. goto err_sock;
  293. dev_add_pack(&ieee802154_packet_type);
  294. rc = 0;
  295. goto out;
  296. err_sock:
  297. proto_unregister(&ieee802154_dgram_prot);
  298. err_dgram:
  299. proto_unregister(&ieee802154_raw_prot);
  300. out:
  301. return rc;
  302. }
  303. static void __exit af_ieee802154_remove(void)
  304. {
  305. dev_remove_pack(&ieee802154_packet_type);
  306. sock_unregister(PF_IEEE802154);
  307. proto_unregister(&ieee802154_dgram_prot);
  308. proto_unregister(&ieee802154_raw_prot);
  309. }
  310. module_init(af_ieee802154_init);
  311. module_exit(af_ieee802154_remove);
  312. MODULE_LICENSE("GPL");
  313. MODULE_ALIAS_NETPROTO(PF_IEEE802154);