Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pn_netlink.c
Go to the documentation of this file.
1 /*
2  * File: pn_netlink.c
3  *
4  * Phonet netlink interface
5  *
6  * Copyright (C) 2008 Nokia Corporation.
7  *
8  * Authors: Sakari Ailus <[email protected]>
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 
26 #include <linux/kernel.h>
27 #include <linux/netlink.h>
28 #include <linux/phonet.h>
29 #include <linux/slab.h>
30 #include <net/sock.h>
31 #include <net/phonet/pn_dev.h>
32 
33 /* Device address handling */
34 
35 static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
36  u32 portid, u32 seq, int event);
37 
39 {
40  struct sk_buff *skb;
41  int err = -ENOBUFS;
42 
43  skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
44  nla_total_size(1), GFP_KERNEL);
45  if (skb == NULL)
46  goto errout;
47  err = fill_addr(skb, dev, addr, 0, 0, event);
48  if (err < 0) {
49  WARN_ON(err == -EMSGSIZE);
50  kfree_skb(skb);
51  goto errout;
52  }
53  rtnl_notify(skb, dev_net(dev), 0,
55  return;
56 errout:
57  rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_IFADDR, err);
58 }
59 
60 static const struct nla_policy ifa_phonet_policy[IFA_MAX+1] = {
61  [IFA_LOCAL] = { .type = NLA_U8 },
62 };
63 
64 static int addr_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *attr)
65 {
66  struct net *net = sock_net(skb->sk);
67  struct nlattr *tb[IFA_MAX+1];
68  struct net_device *dev;
69  struct ifaddrmsg *ifm;
70  int err;
71  u8 pnaddr;
72 
73  if (!capable(CAP_SYS_ADMIN))
74  return -EPERM;
75 
76  ASSERT_RTNL();
77 
78  err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_phonet_policy);
79  if (err < 0)
80  return err;
81 
82  ifm = nlmsg_data(nlh);
83  if (tb[IFA_LOCAL] == NULL)
84  return -EINVAL;
85  pnaddr = nla_get_u8(tb[IFA_LOCAL]);
86  if (pnaddr & 3)
87  /* Phonet addresses only have 6 high-order bits */
88  return -EINVAL;
89 
90  dev = __dev_get_by_index(net, ifm->ifa_index);
91  if (dev == NULL)
92  return -ENODEV;
93 
94  if (nlh->nlmsg_type == RTM_NEWADDR)
95  err = phonet_address_add(dev, pnaddr);
96  else
97  err = phonet_address_del(dev, pnaddr);
98  if (!err)
99  phonet_address_notify(nlh->nlmsg_type, dev, pnaddr);
100  return err;
101 }
102 
103 static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
104  u32 portid, u32 seq, int event)
105 {
106  struct ifaddrmsg *ifm;
107  struct nlmsghdr *nlh;
108 
109  nlh = nlmsg_put(skb, portid, seq, event, sizeof(*ifm), 0);
110  if (nlh == NULL)
111  return -EMSGSIZE;
112 
113  ifm = nlmsg_data(nlh);
114  ifm->ifa_family = AF_PHONET;
115  ifm->ifa_prefixlen = 0;
116  ifm->ifa_flags = IFA_F_PERMANENT;
117  ifm->ifa_scope = RT_SCOPE_LINK;
118  ifm->ifa_index = dev->ifindex;
119  if (nla_put_u8(skb, IFA_LOCAL, addr))
120  goto nla_put_failure;
121  return nlmsg_end(skb, nlh);
122 
123 nla_put_failure:
124  nlmsg_cancel(skb, nlh);
125  return -EMSGSIZE;
126 }
127 
128 static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
129 {
130  struct phonet_device_list *pndevs;
131  struct phonet_device *pnd;
132  int dev_idx = 0, dev_start_idx = cb->args[0];
133  int addr_idx = 0, addr_start_idx = cb->args[1];
134 
135  pndevs = phonet_device_list(sock_net(skb->sk));
136  rcu_read_lock();
137  list_for_each_entry_rcu(pnd, &pndevs->list, list) {
138  u8 addr;
139 
140  if (dev_idx > dev_start_idx)
141  addr_start_idx = 0;
142  if (dev_idx++ < dev_start_idx)
143  continue;
144 
145  addr_idx = 0;
146  for_each_set_bit(addr, pnd->addrs, 64) {
147  if (addr_idx++ < addr_start_idx)
148  continue;
149 
150  if (fill_addr(skb, pnd->netdev, addr << 2,
151  NETLINK_CB(cb->skb).portid,
152  cb->nlh->nlmsg_seq, RTM_NEWADDR) < 0)
153  goto out;
154  }
155  }
156 
157 out:
158  rcu_read_unlock();
159  cb->args[0] = dev_idx;
160  cb->args[1] = addr_idx;
161 
162  return skb->len;
163 }
164 
165 /* Routes handling */
166 
167 static int fill_route(struct sk_buff *skb, struct net_device *dev, u8 dst,
168  u32 portid, u32 seq, int event)
169 {
170  struct rtmsg *rtm;
171  struct nlmsghdr *nlh;
172 
173  nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), 0);
174  if (nlh == NULL)
175  return -EMSGSIZE;
176 
177  rtm = nlmsg_data(nlh);
178  rtm->rtm_family = AF_PHONET;
179  rtm->rtm_dst_len = 6;
180  rtm->rtm_src_len = 0;
181  rtm->rtm_tos = 0;
182  rtm->rtm_table = RT_TABLE_MAIN;
185  rtm->rtm_type = RTN_UNICAST;
186  rtm->rtm_flags = 0;
187  if (nla_put_u8(skb, RTA_DST, dst) ||
188  nla_put_u32(skb, RTA_OIF, dev->ifindex))
189  goto nla_put_failure;
190  return nlmsg_end(skb, nlh);
191 
192 nla_put_failure:
193  nlmsg_cancel(skb, nlh);
194  return -EMSGSIZE;
195 }
196 
197 void rtm_phonet_notify(int event, struct net_device *dev, u8 dst)
198 {
199  struct sk_buff *skb;
200  int err = -ENOBUFS;
201 
202  skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
203  nla_total_size(1) + nla_total_size(4), GFP_KERNEL);
204  if (skb == NULL)
205  goto errout;
206  err = fill_route(skb, dev, dst, 0, 0, event);
207  if (err < 0) {
208  WARN_ON(err == -EMSGSIZE);
209  kfree_skb(skb);
210  goto errout;
211  }
212  rtnl_notify(skb, dev_net(dev), 0,
214  return;
215 errout:
216  rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_ROUTE, err);
217 }
218 
219 static const struct nla_policy rtm_phonet_policy[RTA_MAX+1] = {
220  [RTA_DST] = { .type = NLA_U8 },
221  [RTA_OIF] = { .type = NLA_U32 },
222 };
223 
224 static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *attr)
225 {
226  struct net *net = sock_net(skb->sk);
227  struct nlattr *tb[RTA_MAX+1];
228  struct net_device *dev;
229  struct rtmsg *rtm;
230  int err;
231  u8 dst;
232 
233  if (!capable(CAP_SYS_ADMIN))
234  return -EPERM;
235 
236  ASSERT_RTNL();
237 
238  err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_phonet_policy);
239  if (err < 0)
240  return err;
241 
242  rtm = nlmsg_data(nlh);
243  if (rtm->rtm_table != RT_TABLE_MAIN || rtm->rtm_type != RTN_UNICAST)
244  return -EINVAL;
245  if (tb[RTA_DST] == NULL || tb[RTA_OIF] == NULL)
246  return -EINVAL;
247  dst = nla_get_u8(tb[RTA_DST]);
248  if (dst & 3) /* Phonet addresses only have 6 high-order bits */
249  return -EINVAL;
250 
251  dev = __dev_get_by_index(net, nla_get_u32(tb[RTA_OIF]));
252  if (dev == NULL)
253  return -ENODEV;
254 
255  if (nlh->nlmsg_type == RTM_NEWROUTE)
256  err = phonet_route_add(dev, dst);
257  else
258  err = phonet_route_del(dev, dst);
259  if (!err)
260  rtm_phonet_notify(nlh->nlmsg_type, dev, dst);
261  return err;
262 }
263 
264 static int route_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
265 {
266  struct net *net = sock_net(skb->sk);
267  u8 addr, addr_idx = 0, addr_start_idx = cb->args[0];
268 
269  rcu_read_lock();
270  for (addr = 0; addr < 64; addr++) {
271  struct net_device *dev;
272 
273  dev = phonet_route_get_rcu(net, addr << 2);
274  if (!dev)
275  continue;
276 
277  if (addr_idx++ < addr_start_idx)
278  continue;
279  if (fill_route(skb, dev, addr << 2, NETLINK_CB(cb->skb).portid,
280  cb->nlh->nlmsg_seq, RTM_NEWROUTE))
281  goto out;
282  }
283 
284 out:
285  rcu_read_unlock();
286  cb->args[0] = addr_idx;
287  cb->args[1] = 0;
288 
289  return skb->len;
290 }
291 
293 {
294  int err = __rtnl_register(PF_PHONET, RTM_NEWADDR, addr_doit,
295  NULL, NULL);
296  if (err)
297  return err;
298 
299  /* Further __rtnl_register() cannot fail */
301  __rtnl_register(PF_PHONET, RTM_GETADDR, NULL, getaddr_dumpit, NULL);
304  __rtnl_register(PF_PHONET, RTM_GETROUTE, NULL, route_dumpit, NULL);
305  return 0;
306 }