blob: 58b3b1f991ed2db16d78d2d192eefb4b4d3ad80e [file] [log] [blame]
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -07001/*
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
26#include <linux/kernel.h>
27#include <linux/netlink.h>
28#include <linux/phonet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070030#include <net/sock.h>
31#include <net/phonet/pn_dev.h>
32
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +000033/* Device address handling */
34
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070035static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
36 u32 pid, u32 seq, int event);
37
Rémi Denis-Courmontc7a1a4c2009-06-24 01:07:44 +000038void phonet_address_notify(int event, struct net_device *dev, u8 addr)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070039{
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 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -080053 rtnl_notify(skb, dev_net(dev), 0,
54 RTNLGRP_PHONET_IFADDR, NULL, GFP_KERNEL);
55 return;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070056errout:
Rémi Denis-Courmont4b7673a2009-11-02 22:41:28 +000057 rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_IFADDR, err);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070058}
59
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070060static const struct nla_policy ifa_phonet_policy[IFA_MAX+1] = {
61 [IFA_LOCAL] = { .type = NLA_U8 },
62};
63
64static int addr_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *attr)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070065{
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070066 struct net *net = sock_net(skb->sk);
67 struct nlattr *tb[IFA_MAX+1];
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070068 struct net_device *dev;
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070069 struct ifaddrmsg *ifm;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070070 int err;
71 u8 pnaddr;
72
73 if (!capable(CAP_SYS_ADMIN))
74 return -EPERM;
75
76 ASSERT_RTNL();
77
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070078 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 */
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070088 return -EINVAL;
89
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070090 dev = __dev_get_by_index(net, ifm->ifa_index);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070091 if (dev == NULL)
92 return -ENODEV;
93
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070094 if (nlh->nlmsg_type == RTM_NEWADDR)
95 err = phonet_address_add(dev, pnaddr);
96 else
97 err = phonet_address_del(dev, pnaddr);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070098 if (!err)
Rémi Denis-Courmontc7a1a4c2009-06-24 01:07:44 +000099 phonet_address_notify(nlh->nlmsg_type, dev, pnaddr);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700100 return err;
101}
102
103static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
104 u32 pid, u32 seq, int event)
105{
106 struct ifaddrmsg *ifm;
107 struct nlmsghdr *nlh;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700108
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700109 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), 0);
110 if (nlh == NULL)
111 return -EMSGSIZE;
112
113 ifm = nlmsg_data(nlh);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700114 ifm->ifa_family = AF_PHONET;
115 ifm->ifa_prefixlen = 0;
116 ifm->ifa_flags = IFA_F_PERMANENT;
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700117 ifm->ifa_scope = RT_SCOPE_LINK;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700118 ifm->ifa_index = dev->ifindex;
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700119 NLA_PUT_U8(skb, IFA_LOCAL, addr);
120 return nlmsg_end(skb, nlh);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700121
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700122nla_put_failure:
123 nlmsg_cancel(skb, nlh);
124 return -EMSGSIZE;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700125}
126
127static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
128{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000129 struct phonet_device_list *pndevs;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700130 struct phonet_device *pnd;
131 int dev_idx = 0, dev_start_idx = cb->args[0];
132 int addr_idx = 0, addr_start_idx = cb->args[1];
133
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000134 pndevs = phonet_device_list(sock_net(skb->sk));
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800135 rcu_read_lock();
136 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700137 u8 addr;
138
139 if (dev_idx > dev_start_idx)
140 addr_start_idx = 0;
141 if (dev_idx++ < dev_start_idx)
142 continue;
143
144 addr_idx = 0;
Akinobu Mitaa1ca14a2010-03-11 12:07:49 +0000145 for_each_set_bit(addr, pnd->addrs, 64) {
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700146 if (addr_idx++ < addr_start_idx)
147 continue;
148
149 if (fill_addr(skb, pnd->netdev, addr << 2,
150 NETLINK_CB(cb->skb).pid,
Rémi Denis-Courmont998ec752009-09-08 23:59:51 +0000151 cb->nlh->nlmsg_seq, RTM_NEWADDR) < 0)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700152 goto out;
153 }
154 }
155
156out:
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800157 rcu_read_unlock();
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700158 cb->args[0] = dev_idx;
159 cb->args[1] = addr_idx;
160
161 return skb->len;
162}
163
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000164/* Routes handling */
165
166static int fill_route(struct sk_buff *skb, struct net_device *dev, u8 dst,
167 u32 pid, u32 seq, int event)
168{
169 struct rtmsg *rtm;
170 struct nlmsghdr *nlh;
171
172 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), 0);
173 if (nlh == NULL)
174 return -EMSGSIZE;
175
176 rtm = nlmsg_data(nlh);
177 rtm->rtm_family = AF_PHONET;
178 rtm->rtm_dst_len = 6;
179 rtm->rtm_src_len = 0;
180 rtm->rtm_tos = 0;
181 rtm->rtm_table = RT_TABLE_MAIN;
182 rtm->rtm_protocol = RTPROT_STATIC;
183 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
184 rtm->rtm_type = RTN_UNICAST;
185 rtm->rtm_flags = 0;
186 NLA_PUT_U8(skb, RTA_DST, dst);
187 NLA_PUT_U32(skb, RTA_OIF, dev->ifindex);
188 return nlmsg_end(skb, nlh);
189
190nla_put_failure:
191 nlmsg_cancel(skb, nlh);
192 return -EMSGSIZE;
193}
194
195void rtm_phonet_notify(int event, struct net_device *dev, u8 dst)
196{
197 struct sk_buff *skb;
198 int err = -ENOBUFS;
199
200 skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
201 nla_total_size(1) + nla_total_size(4), GFP_KERNEL);
202 if (skb == NULL)
203 goto errout;
204 err = fill_route(skb, dev, dst, 0, 0, event);
205 if (err < 0) {
206 WARN_ON(err == -EMSGSIZE);
207 kfree_skb(skb);
208 goto errout;
209 }
210 rtnl_notify(skb, dev_net(dev), 0,
211 RTNLGRP_PHONET_ROUTE, NULL, GFP_KERNEL);
212 return;
213errout:
Rémi Denis-Courmont4b7673a2009-11-02 22:41:28 +0000214 rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_ROUTE, err);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000215}
216
217static const struct nla_policy rtm_phonet_policy[RTA_MAX+1] = {
218 [RTA_DST] = { .type = NLA_U8 },
219 [RTA_OIF] = { .type = NLA_U32 },
220};
221
222static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *attr)
223{
224 struct net *net = sock_net(skb->sk);
225 struct nlattr *tb[RTA_MAX+1];
226 struct net_device *dev;
227 struct rtmsg *rtm;
228 int err;
229 u8 dst;
230
231 if (!capable(CAP_SYS_ADMIN))
232 return -EPERM;
233
234 ASSERT_RTNL();
235
236 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_phonet_policy);
237 if (err < 0)
238 return err;
239
240 rtm = nlmsg_data(nlh);
241 if (rtm->rtm_table != RT_TABLE_MAIN || rtm->rtm_type != RTN_UNICAST)
242 return -EINVAL;
243 if (tb[RTA_DST] == NULL || tb[RTA_OIF] == NULL)
244 return -EINVAL;
245 dst = nla_get_u8(tb[RTA_DST]);
246 if (dst & 3) /* Phonet addresses only have 6 high-order bits */
247 return -EINVAL;
248
249 dev = __dev_get_by_index(net, nla_get_u32(tb[RTA_OIF]));
250 if (dev == NULL)
251 return -ENODEV;
252
253 if (nlh->nlmsg_type == RTM_NEWROUTE)
254 err = phonet_route_add(dev, dst);
255 else
256 err = phonet_route_del(dev, dst);
257 if (!err)
258 rtm_phonet_notify(nlh->nlmsg_type, dev, dst);
259 return err;
260}
261
262static int route_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
263{
264 struct net *net = sock_net(skb->sk);
265 u8 addr, addr_idx = 0, addr_start_idx = cb->args[0];
266
267 for (addr = 0; addr < 64; addr++) {
268 struct net_device *dev;
269
270 dev = phonet_route_get(net, addr << 2);
271 if (!dev)
272 continue;
273
274 if (addr_idx++ < addr_start_idx)
275 continue;
276 if (fill_route(skb, dev, addr << 2, NETLINK_CB(cb->skb).pid,
277 cb->nlh->nlmsg_seq, RTM_NEWROUTE))
278 goto out;
279 }
280
281out:
282 cb->args[0] = addr_idx;
283 cb->args[1] = 0;
284
285 return skb->len;
286}
287
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000288int __init phonet_netlink_register(void)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700289{
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000290 int err = __rtnl_register(PF_PHONET, RTM_NEWADDR, addr_doit, NULL);
291 if (err)
292 return err;
293
294 /* Further __rtnl_register() cannot fail */
295 __rtnl_register(PF_PHONET, RTM_DELADDR, addr_doit, NULL);
296 __rtnl_register(PF_PHONET, RTM_GETADDR, NULL, getaddr_dumpit);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000297 __rtnl_register(PF_PHONET, RTM_NEWROUTE, route_doit, NULL);
298 __rtnl_register(PF_PHONET, RTM_DELROUTE, route_doit, NULL);
299 __rtnl_register(PF_PHONET, RTM_GETROUTE, NULL, route_dumpit);
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000300 return 0;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700301}