pn_netlink.c 7.5 KB

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