pn_netlink.c 4.1 KB

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