pn_netlink.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <net/sock.h>
  29. #include <net/phonet/pn_dev.h>
  30. static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
  31. u32 pid, u32 seq, int event);
  32. static void rtmsg_notify(int event, struct net_device *dev, u8 addr)
  33. {
  34. struct sk_buff *skb;
  35. int err = -ENOBUFS;
  36. skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
  37. nla_total_size(1), GFP_KERNEL);
  38. if (skb == NULL)
  39. goto errout;
  40. err = fill_addr(skb, dev, addr, 0, 0, event);
  41. if (err < 0) {
  42. WARN_ON(err == -EMSGSIZE);
  43. kfree_skb(skb);
  44. goto errout;
  45. }
  46. rtnl_notify(skb, dev_net(dev), 0,
  47. RTNLGRP_PHONET_IFADDR, NULL, GFP_KERNEL);
  48. return;
  49. errout:
  50. if (err < 0)
  51. rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_IFADDR, err);
  52. }
  53. static const struct nla_policy ifa_phonet_policy[IFA_MAX+1] = {
  54. [IFA_LOCAL] = { .type = NLA_U8 },
  55. };
  56. static int addr_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *attr)
  57. {
  58. struct net *net = sock_net(skb->sk);
  59. struct nlattr *tb[IFA_MAX+1];
  60. struct net_device *dev;
  61. struct ifaddrmsg *ifm;
  62. int err;
  63. u8 pnaddr;
  64. if (!capable(CAP_SYS_ADMIN))
  65. return -EPERM;
  66. ASSERT_RTNL();
  67. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_phonet_policy);
  68. if (err < 0)
  69. return err;
  70. ifm = nlmsg_data(nlh);
  71. if (tb[IFA_LOCAL] == NULL)
  72. return -EINVAL;
  73. pnaddr = nla_get_u8(tb[IFA_LOCAL]);
  74. if (pnaddr & 3)
  75. /* Phonet addresses only have 6 high-order bits */
  76. return -EINVAL;
  77. dev = __dev_get_by_index(net, ifm->ifa_index);
  78. if (dev == NULL)
  79. return -ENODEV;
  80. if (nlh->nlmsg_type == RTM_NEWADDR)
  81. err = phonet_address_add(dev, pnaddr);
  82. else
  83. err = phonet_address_del(dev, pnaddr);
  84. if (!err)
  85. rtmsg_notify(nlh->nlmsg_type, dev, pnaddr);
  86. return err;
  87. }
  88. static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
  89. u32 pid, u32 seq, int event)
  90. {
  91. struct ifaddrmsg *ifm;
  92. struct nlmsghdr *nlh;
  93. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), 0);
  94. if (nlh == NULL)
  95. return -EMSGSIZE;
  96. ifm = nlmsg_data(nlh);
  97. ifm->ifa_family = AF_PHONET;
  98. ifm->ifa_prefixlen = 0;
  99. ifm->ifa_flags = IFA_F_PERMANENT;
  100. ifm->ifa_scope = RT_SCOPE_LINK;
  101. ifm->ifa_index = dev->ifindex;
  102. NLA_PUT_U8(skb, IFA_LOCAL, addr);
  103. return nlmsg_end(skb, nlh);
  104. nla_put_failure:
  105. nlmsg_cancel(skb, nlh);
  106. return -EMSGSIZE;
  107. }
  108. static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
  109. {
  110. struct phonet_device_list *pndevs;
  111. struct phonet_device *pnd;
  112. int dev_idx = 0, dev_start_idx = cb->args[0];
  113. int addr_idx = 0, addr_start_idx = cb->args[1];
  114. pndevs = phonet_device_list(sock_net(skb->sk));
  115. spin_lock_bh(&pndevs->lock);
  116. list_for_each_entry(pnd, &pndevs->list, list) {
  117. u8 addr;
  118. if (dev_idx > dev_start_idx)
  119. addr_start_idx = 0;
  120. if (dev_idx++ < dev_start_idx)
  121. continue;
  122. addr_idx = 0;
  123. for (addr = find_first_bit(pnd->addrs, 64); addr < 64;
  124. addr = find_next_bit(pnd->addrs, 64, 1+addr)) {
  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))
  130. goto out;
  131. }
  132. }
  133. out:
  134. spin_unlock_bh(&pndevs->lock);
  135. cb->args[0] = dev_idx;
  136. cb->args[1] = addr_idx;
  137. return skb->len;
  138. }
  139. int __init phonet_netlink_register(void)
  140. {
  141. int err = __rtnl_register(PF_PHONET, RTM_NEWADDR, addr_doit, NULL);
  142. if (err)
  143. return err;
  144. /* Further __rtnl_register() cannot fail */
  145. __rtnl_register(PF_PHONET, RTM_DELADDR, addr_doit, NULL);
  146. __rtnl_register(PF_PHONET, RTM_GETADDR, NULL, getaddr_dumpit);
  147. return 0;
  148. }