pn_netlink.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * File: pn_netlink.c
  3. *
  4. * Phonet netlink interface
  5. *
  6. * Copyright (C) 2008 Nokia Corporation.
  7. *
  8. * Authors: Sakari Ailus <sakari.ailus@nokia.com>
  9. * Remi Denis-Courmont
  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 portid, 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)
  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 (!netlink_capable(skb, CAP_NET_ADMIN))
  66. return -EPERM;
  67. if (!netlink_capable(skb, CAP_SYS_ADMIN))
  68. return -EPERM;
  69. ASSERT_RTNL();
  70. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_phonet_policy);
  71. if (err < 0)
  72. return err;
  73. ifm = nlmsg_data(nlh);
  74. if (tb[IFA_LOCAL] == NULL)
  75. return -EINVAL;
  76. pnaddr = nla_get_u8(tb[IFA_LOCAL]);
  77. if (pnaddr & 3)
  78. /* Phonet addresses only have 6 high-order bits */
  79. return -EINVAL;
  80. dev = __dev_get_by_index(net, ifm->ifa_index);
  81. if (dev == NULL)
  82. return -ENODEV;
  83. if (nlh->nlmsg_type == RTM_NEWADDR)
  84. err = phonet_address_add(dev, pnaddr);
  85. else
  86. err = phonet_address_del(dev, pnaddr);
  87. if (!err)
  88. phonet_address_notify(nlh->nlmsg_type, dev, pnaddr);
  89. return err;
  90. }
  91. static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
  92. u32 portid, u32 seq, int event)
  93. {
  94. struct ifaddrmsg *ifm;
  95. struct nlmsghdr *nlh;
  96. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*ifm), 0);
  97. if (nlh == NULL)
  98. return -EMSGSIZE;
  99. ifm = nlmsg_data(nlh);
  100. ifm->ifa_family = AF_PHONET;
  101. ifm->ifa_prefixlen = 0;
  102. ifm->ifa_flags = IFA_F_PERMANENT;
  103. ifm->ifa_scope = RT_SCOPE_LINK;
  104. ifm->ifa_index = dev->ifindex;
  105. if (nla_put_u8(skb, IFA_LOCAL, addr))
  106. goto nla_put_failure;
  107. nlmsg_end(skb, nlh);
  108. return 0;
  109. nla_put_failure:
  110. nlmsg_cancel(skb, nlh);
  111. return -EMSGSIZE;
  112. }
  113. static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
  114. {
  115. struct phonet_device_list *pndevs;
  116. struct phonet_device *pnd;
  117. int dev_idx = 0, dev_start_idx = cb->args[0];
  118. int addr_idx = 0, addr_start_idx = cb->args[1];
  119. pndevs = phonet_device_list(sock_net(skb->sk));
  120. rcu_read_lock();
  121. list_for_each_entry_rcu(pnd, &pndevs->list, list) {
  122. u8 addr;
  123. if (dev_idx > dev_start_idx)
  124. addr_start_idx = 0;
  125. if (dev_idx++ < dev_start_idx)
  126. continue;
  127. addr_idx = 0;
  128. for_each_set_bit(addr, pnd->addrs, 64) {
  129. if (addr_idx++ < addr_start_idx)
  130. continue;
  131. if (fill_addr(skb, pnd->netdev, addr << 2,
  132. NETLINK_CB(cb->skb).portid,
  133. cb->nlh->nlmsg_seq, RTM_NEWADDR) < 0)
  134. goto out;
  135. }
  136. }
  137. out:
  138. rcu_read_unlock();
  139. cb->args[0] = dev_idx;
  140. cb->args[1] = addr_idx;
  141. return skb->len;
  142. }
  143. /* Routes handling */
  144. static int fill_route(struct sk_buff *skb, struct net_device *dev, u8 dst,
  145. u32 portid, u32 seq, int event)
  146. {
  147. struct rtmsg *rtm;
  148. struct nlmsghdr *nlh;
  149. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), 0);
  150. if (nlh == NULL)
  151. return -EMSGSIZE;
  152. rtm = nlmsg_data(nlh);
  153. rtm->rtm_family = AF_PHONET;
  154. rtm->rtm_dst_len = 6;
  155. rtm->rtm_src_len = 0;
  156. rtm->rtm_tos = 0;
  157. rtm->rtm_table = RT_TABLE_MAIN;
  158. rtm->rtm_protocol = RTPROT_STATIC;
  159. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  160. rtm->rtm_type = RTN_UNICAST;
  161. rtm->rtm_flags = 0;
  162. if (nla_put_u8(skb, RTA_DST, dst) ||
  163. nla_put_u32(skb, RTA_OIF, dev->ifindex))
  164. goto nla_put_failure;
  165. nlmsg_end(skb, nlh);
  166. return 0;
  167. nla_put_failure:
  168. nlmsg_cancel(skb, nlh);
  169. return -EMSGSIZE;
  170. }
  171. void rtm_phonet_notify(int event, struct net_device *dev, u8 dst)
  172. {
  173. struct sk_buff *skb;
  174. int err = -ENOBUFS;
  175. skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
  176. nla_total_size(1) + nla_total_size(4), GFP_KERNEL);
  177. if (skb == NULL)
  178. goto errout;
  179. err = fill_route(skb, dev, dst, 0, 0, event);
  180. if (err < 0) {
  181. WARN_ON(err == -EMSGSIZE);
  182. kfree_skb(skb);
  183. goto errout;
  184. }
  185. rtnl_notify(skb, dev_net(dev), 0,
  186. RTNLGRP_PHONET_ROUTE, NULL, GFP_KERNEL);
  187. return;
  188. errout:
  189. rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_ROUTE, err);
  190. }
  191. static const struct nla_policy rtm_phonet_policy[RTA_MAX+1] = {
  192. [RTA_DST] = { .type = NLA_U8 },
  193. [RTA_OIF] = { .type = NLA_U32 },
  194. };
  195. static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh)
  196. {
  197. struct net *net = sock_net(skb->sk);
  198. struct nlattr *tb[RTA_MAX+1];
  199. struct net_device *dev;
  200. struct rtmsg *rtm;
  201. int err;
  202. u8 dst;
  203. if (!netlink_capable(skb, CAP_NET_ADMIN))
  204. return -EPERM;
  205. if (!netlink_capable(skb, CAP_SYS_ADMIN))
  206. return -EPERM;
  207. ASSERT_RTNL();
  208. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_phonet_policy);
  209. if (err < 0)
  210. return err;
  211. rtm = nlmsg_data(nlh);
  212. if (rtm->rtm_table != RT_TABLE_MAIN || rtm->rtm_type != RTN_UNICAST)
  213. return -EINVAL;
  214. if (tb[RTA_DST] == NULL || tb[RTA_OIF] == NULL)
  215. return -EINVAL;
  216. dst = nla_get_u8(tb[RTA_DST]);
  217. if (dst & 3) /* Phonet addresses only have 6 high-order bits */
  218. return -EINVAL;
  219. dev = __dev_get_by_index(net, nla_get_u32(tb[RTA_OIF]));
  220. if (dev == NULL)
  221. return -ENODEV;
  222. if (nlh->nlmsg_type == RTM_NEWROUTE)
  223. err = phonet_route_add(dev, dst);
  224. else
  225. err = phonet_route_del(dev, dst);
  226. if (!err)
  227. rtm_phonet_notify(nlh->nlmsg_type, dev, dst);
  228. return err;
  229. }
  230. static int route_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
  231. {
  232. struct net *net = sock_net(skb->sk);
  233. u8 addr;
  234. rcu_read_lock();
  235. for (addr = cb->args[0]; addr < 64; addr++) {
  236. struct net_device *dev = phonet_route_get_rcu(net, addr << 2);
  237. if (!dev)
  238. continue;
  239. if (fill_route(skb, dev, addr << 2, NETLINK_CB(cb->skb).portid,
  240. cb->nlh->nlmsg_seq, RTM_NEWROUTE) < 0)
  241. goto out;
  242. }
  243. out:
  244. rcu_read_unlock();
  245. cb->args[0] = addr;
  246. return skb->len;
  247. }
  248. int __init phonet_netlink_register(void)
  249. {
  250. int err = __rtnl_register(PF_PHONET, RTM_NEWADDR, addr_doit,
  251. NULL, NULL);
  252. if (err)
  253. return err;
  254. /* Further __rtnl_register() cannot fail */
  255. __rtnl_register(PF_PHONET, RTM_DELADDR, addr_doit, NULL, NULL);
  256. __rtnl_register(PF_PHONET, RTM_GETADDR, NULL, getaddr_dumpit, NULL);
  257. __rtnl_register(PF_PHONET, RTM_NEWROUTE, route_doit, NULL, NULL);
  258. __rtnl_register(PF_PHONET, RTM_DELROUTE, route_doit, NULL, NULL);
  259. __rtnl_register(PF_PHONET, RTM_GETROUTE, NULL, route_dumpit, NULL);
  260. return 0;
  261. }