pn_netlink.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * File: pn_netlink.c
  3. *
  4. * Phonet netlink interface
  5. *
  6. * Copyright (C) 2008 Nokia Corporation.
  7. *
  8. * Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
  9. * Original author: Sakari Ailus <sakari.ailus@nokia.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * version 2 as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  23. * 02110-1301 USA
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/netlink.h>
  27. #include <linux/phonet.h>
  28. #include <linux/slab.h>
  29. #include <net/sock.h>
  30. #include <net/phonet/pn_dev.h>
  31. /* Device address handling */
  32. static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
  33. u32 pid, u32 seq, int event);
  34. void phonet_address_notify(int event, struct net_device *dev, u8 addr)
  35. {
  36. struct sk_buff *skb;
  37. int err = -ENOBUFS;
  38. skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
  39. nla_total_size(1), GFP_KERNEL);
  40. if (skb == NULL)
  41. goto errout;
  42. err = fill_addr(skb, dev, addr, 0, 0, event);
  43. if (err < 0) {
  44. WARN_ON(err == -EMSGSIZE);
  45. kfree_skb(skb);
  46. goto errout;
  47. }
  48. rtnl_notify(skb, dev_net(dev), 0,
  49. RTNLGRP_PHONET_IFADDR, NULL, GFP_KERNEL);
  50. return;
  51. errout:
  52. rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_IFADDR, err);
  53. }
  54. static const struct nla_policy ifa_phonet_policy[IFA_MAX+1] = {
  55. [IFA_LOCAL] = { .type = NLA_U8 },
  56. };
  57. static int addr_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *attr)
  58. {
  59. struct net *net = sock_net(skb->sk);
  60. struct nlattr *tb[IFA_MAX+1];
  61. struct net_device *dev;
  62. struct ifaddrmsg *ifm;
  63. int err;
  64. u8 pnaddr;
  65. if (!capable(CAP_SYS_ADMIN))
  66. return -EPERM;
  67. ASSERT_RTNL();
  68. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_phonet_policy);
  69. if (err < 0)
  70. return err;
  71. ifm = nlmsg_data(nlh);
  72. if (tb[IFA_LOCAL] == NULL)
  73. return -EINVAL;
  74. pnaddr = nla_get_u8(tb[IFA_LOCAL]);
  75. if (pnaddr & 3)
  76. /* Phonet addresses only have 6 high-order bits */
  77. return -EINVAL;
  78. dev = __dev_get_by_index(net, ifm->ifa_index);
  79. if (dev == NULL)
  80. return -ENODEV;
  81. if (nlh->nlmsg_type == RTM_NEWADDR)
  82. err = phonet_address_add(dev, pnaddr);
  83. else
  84. err = phonet_address_del(dev, pnaddr);
  85. if (!err)
  86. phonet_address_notify(nlh->nlmsg_type, dev, pnaddr);
  87. return err;
  88. }
  89. static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
  90. u32 pid, u32 seq, int event)
  91. {
  92. struct ifaddrmsg *ifm;
  93. struct nlmsghdr *nlh;
  94. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), 0);
  95. if (nlh == NULL)
  96. return -EMSGSIZE;
  97. ifm = nlmsg_data(nlh);
  98. ifm->ifa_family = AF_PHONET;
  99. ifm->ifa_prefixlen = 0;
  100. ifm->ifa_flags = IFA_F_PERMANENT;
  101. ifm->ifa_scope = RT_SCOPE_LINK;
  102. ifm->ifa_index = dev->ifindex;
  103. NLA_PUT_U8(skb, IFA_LOCAL, addr);
  104. return nlmsg_end(skb, nlh);
  105. nla_put_failure:
  106. nlmsg_cancel(skb, nlh);
  107. return -EMSGSIZE;
  108. }
  109. static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
  110. {
  111. struct phonet_device_list *pndevs;
  112. struct phonet_device *pnd;
  113. int dev_idx = 0, dev_start_idx = cb->args[0];
  114. int addr_idx = 0, addr_start_idx = cb->args[1];
  115. pndevs = phonet_device_list(sock_net(skb->sk));
  116. rcu_read_lock();
  117. list_for_each_entry_rcu(pnd, &pndevs->list, list) {
  118. u8 addr;
  119. if (dev_idx > dev_start_idx)
  120. addr_start_idx = 0;
  121. if (dev_idx++ < dev_start_idx)
  122. continue;
  123. addr_idx = 0;
  124. for_each_set_bit(addr, pnd->addrs, 64) {
  125. if (addr_idx++ < addr_start_idx)
  126. continue;
  127. if (fill_addr(skb, pnd->netdev, addr << 2,
  128. NETLINK_CB(cb->skb).pid,
  129. cb->nlh->nlmsg_seq, RTM_NEWADDR) < 0)
  130. goto out;
  131. }
  132. }
  133. out:
  134. rcu_read_unlock();
  135. cb->args[0] = dev_idx;
  136. cb->args[1] = addr_idx;
  137. return skb->len;
  138. }
  139. /* Routes handling */
  140. static int fill_route(struct sk_buff *skb, struct net_device *dev, u8 dst,
  141. u32 pid, u32 seq, int event)
  142. {
  143. struct rtmsg *rtm;
  144. struct nlmsghdr *nlh;
  145. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), 0);
  146. if (nlh == NULL)
  147. return -EMSGSIZE;
  148. rtm = nlmsg_data(nlh);
  149. rtm->rtm_family = AF_PHONET;
  150. rtm->rtm_dst_len = 6;
  151. rtm->rtm_src_len = 0;
  152. rtm->rtm_tos = 0;
  153. rtm->rtm_table = RT_TABLE_MAIN;
  154. rtm->rtm_protocol = RTPROT_STATIC;
  155. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  156. rtm->rtm_type = RTN_UNICAST;
  157. rtm->rtm_flags = 0;
  158. NLA_PUT_U8(skb, RTA_DST, dst);
  159. NLA_PUT_U32(skb, RTA_OIF, dev->ifindex);
  160. return nlmsg_end(skb, nlh);
  161. nla_put_failure:
  162. nlmsg_cancel(skb, nlh);
  163. return -EMSGSIZE;
  164. }
  165. void rtm_phonet_notify(int event, struct net_device *dev, u8 dst)
  166. {
  167. struct sk_buff *skb;
  168. int err = -ENOBUFS;
  169. skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
  170. nla_total_size(1) + nla_total_size(4), GFP_KERNEL);
  171. if (skb == NULL)
  172. goto errout;
  173. err = fill_route(skb, dev, dst, 0, 0, event);
  174. if (err < 0) {
  175. WARN_ON(err == -EMSGSIZE);
  176. kfree_skb(skb);
  177. goto errout;
  178. }
  179. rtnl_notify(skb, dev_net(dev), 0,
  180. RTNLGRP_PHONET_ROUTE, NULL, GFP_KERNEL);
  181. return;
  182. errout:
  183. rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_ROUTE, err);
  184. }
  185. static const struct nla_policy rtm_phonet_policy[RTA_MAX+1] = {
  186. [RTA_DST] = { .type = NLA_U8 },
  187. [RTA_OIF] = { .type = NLA_U32 },
  188. };
  189. static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *attr)
  190. {
  191. struct net *net = sock_net(skb->sk);
  192. struct nlattr *tb[RTA_MAX+1];
  193. struct net_device *dev;
  194. struct rtmsg *rtm;
  195. int err;
  196. u8 dst;
  197. if (!capable(CAP_SYS_ADMIN))
  198. return -EPERM;
  199. ASSERT_RTNL();
  200. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_phonet_policy);
  201. if (err < 0)
  202. return err;
  203. rtm = nlmsg_data(nlh);
  204. if (rtm->rtm_table != RT_TABLE_MAIN || rtm->rtm_type != RTN_UNICAST)
  205. return -EINVAL;
  206. if (tb[RTA_DST] == NULL || tb[RTA_OIF] == NULL)
  207. return -EINVAL;
  208. dst = nla_get_u8(tb[RTA_DST]);
  209. if (dst & 3) /* Phonet addresses only have 6 high-order bits */
  210. return -EINVAL;
  211. dev = __dev_get_by_index(net, nla_get_u32(tb[RTA_OIF]));
  212. if (dev == NULL)
  213. return -ENODEV;
  214. if (nlh->nlmsg_type == RTM_NEWROUTE)
  215. err = phonet_route_add(dev, dst);
  216. else
  217. err = phonet_route_del(dev, dst);
  218. if (!err)
  219. rtm_phonet_notify(nlh->nlmsg_type, dev, dst);
  220. return err;
  221. }
  222. static int route_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
  223. {
  224. struct net *net = sock_net(skb->sk);
  225. u8 addr, addr_idx = 0, addr_start_idx = cb->args[0];
  226. rcu_read_lock();
  227. for (addr = 0; addr < 64; addr++) {
  228. struct net_device *dev;
  229. dev = phonet_route_get_rcu(net, addr << 2);
  230. if (!dev)
  231. continue;
  232. if (addr_idx++ < addr_start_idx)
  233. continue;
  234. if (fill_route(skb, dev, addr << 2, NETLINK_CB(cb->skb).pid,
  235. cb->nlh->nlmsg_seq, RTM_NEWROUTE))
  236. goto out;
  237. }
  238. out:
  239. rcu_read_unlock();
  240. cb->args[0] = addr_idx;
  241. cb->args[1] = 0;
  242. return skb->len;
  243. }
  244. int __init phonet_netlink_register(void)
  245. {
  246. int err = __rtnl_register(PF_PHONET, RTM_NEWADDR, addr_doit, NULL);
  247. if (err)
  248. return err;
  249. /* Further __rtnl_register() cannot fail */
  250. __rtnl_register(PF_PHONET, RTM_DELADDR, addr_doit, NULL);
  251. __rtnl_register(PF_PHONET, RTM_GETADDR, NULL, getaddr_dumpit);
  252. __rtnl_register(PF_PHONET, RTM_NEWROUTE, route_doit, NULL);
  253. __rtnl_register(PF_PHONET, RTM_DELROUTE, route_doit, NULL);
  254. __rtnl_register(PF_PHONET, RTM_GETROUTE, NULL, route_dumpit);
  255. return 0;
  256. }