blob: fe2e7088ee07b10d387aa5db7b6bc9e7d369853a [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>
29#include <net/sock.h>
30#include <net/phonet/pn_dev.h>
31
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +000032/* Device address handling */
33
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070034static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
35 u32 pid, u32 seq, int event);
36
Rémi Denis-Courmontc7a1a4c2009-06-24 01:07:44 +000037void phonet_address_notify(int event, struct net_device *dev, u8 addr)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070038{
39 struct sk_buff *skb;
40 int err = -ENOBUFS;
41
42 skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
43 nla_total_size(1), GFP_KERNEL);
44 if (skb == NULL)
45 goto errout;
46 err = fill_addr(skb, dev, addr, 0, 0, event);
47 if (err < 0) {
48 WARN_ON(err == -EMSGSIZE);
49 kfree_skb(skb);
50 goto errout;
51 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -080052 rtnl_notify(skb, dev_net(dev), 0,
53 RTNLGRP_PHONET_IFADDR, NULL, GFP_KERNEL);
54 return;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070055errout:
Rémi Denis-Courmont4b7673a2009-11-02 22:41:28 +000056 rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_IFADDR, err);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070057}
58
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070059static const struct nla_policy ifa_phonet_policy[IFA_MAX+1] = {
60 [IFA_LOCAL] = { .type = NLA_U8 },
61};
62
63static int addr_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *attr)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070064{
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070065 struct net *net = sock_net(skb->sk);
66 struct nlattr *tb[IFA_MAX+1];
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070067 struct net_device *dev;
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070068 struct ifaddrmsg *ifm;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070069 int err;
70 u8 pnaddr;
71
72 if (!capable(CAP_SYS_ADMIN))
73 return -EPERM;
74
75 ASSERT_RTNL();
76
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070077 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_phonet_policy);
78 if (err < 0)
79 return err;
80
81 ifm = nlmsg_data(nlh);
82 if (tb[IFA_LOCAL] == NULL)
83 return -EINVAL;
84 pnaddr = nla_get_u8(tb[IFA_LOCAL]);
85 if (pnaddr & 3)
86 /* Phonet addresses only have 6 high-order bits */
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070087 return -EINVAL;
88
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070089 dev = __dev_get_by_index(net, ifm->ifa_index);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070090 if (dev == NULL)
91 return -ENODEV;
92
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070093 if (nlh->nlmsg_type == RTM_NEWADDR)
94 err = phonet_address_add(dev, pnaddr);
95 else
96 err = phonet_address_del(dev, pnaddr);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070097 if (!err)
Rémi Denis-Courmontc7a1a4c2009-06-24 01:07:44 +000098 phonet_address_notify(nlh->nlmsg_type, dev, pnaddr);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070099 return err;
100}
101
102static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
103 u32 pid, u32 seq, int event)
104{
105 struct ifaddrmsg *ifm;
106 struct nlmsghdr *nlh;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700107
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700108 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), 0);
109 if (nlh == NULL)
110 return -EMSGSIZE;
111
112 ifm = nlmsg_data(nlh);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700113 ifm->ifa_family = AF_PHONET;
114 ifm->ifa_prefixlen = 0;
115 ifm->ifa_flags = IFA_F_PERMANENT;
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700116 ifm->ifa_scope = RT_SCOPE_LINK;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700117 ifm->ifa_index = dev->ifindex;
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700118 NLA_PUT_U8(skb, IFA_LOCAL, addr);
119 return nlmsg_end(skb, nlh);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700120
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700121nla_put_failure:
122 nlmsg_cancel(skb, nlh);
123 return -EMSGSIZE;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700124}
125
126static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
127{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000128 struct phonet_device_list *pndevs;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700129 struct phonet_device *pnd;
130 int dev_idx = 0, dev_start_idx = cb->args[0];
131 int addr_idx = 0, addr_start_idx = cb->args[1];
132
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000133 pndevs = phonet_device_list(sock_net(skb->sk));
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800134 rcu_read_lock();
135 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700136 u8 addr;
137
138 if (dev_idx > dev_start_idx)
139 addr_start_idx = 0;
140 if (dev_idx++ < dev_start_idx)
141 continue;
142
143 addr_idx = 0;
Akinobu Mitaa1ca14a2010-03-11 12:07:49 +0000144 for_each_set_bit(addr, pnd->addrs, 64) {
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700145 if (addr_idx++ < addr_start_idx)
146 continue;
147
148 if (fill_addr(skb, pnd->netdev, addr << 2,
149 NETLINK_CB(cb->skb).pid,
Rémi Denis-Courmont998ec752009-09-08 23:59:51 +0000150 cb->nlh->nlmsg_seq, RTM_NEWADDR) < 0)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700151 goto out;
152 }
153 }
154
155out:
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800156 rcu_read_unlock();
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700157 cb->args[0] = dev_idx;
158 cb->args[1] = addr_idx;
159
160 return skb->len;
161}
162
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000163/* Routes handling */
164
165static int fill_route(struct sk_buff *skb, struct net_device *dev, u8 dst,
166 u32 pid, u32 seq, int event)
167{
168 struct rtmsg *rtm;
169 struct nlmsghdr *nlh;
170
171 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), 0);
172 if (nlh == NULL)
173 return -EMSGSIZE;
174
175 rtm = nlmsg_data(nlh);
176 rtm->rtm_family = AF_PHONET;
177 rtm->rtm_dst_len = 6;
178 rtm->rtm_src_len = 0;
179 rtm->rtm_tos = 0;
180 rtm->rtm_table = RT_TABLE_MAIN;
181 rtm->rtm_protocol = RTPROT_STATIC;
182 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
183 rtm->rtm_type = RTN_UNICAST;
184 rtm->rtm_flags = 0;
185 NLA_PUT_U8(skb, RTA_DST, dst);
186 NLA_PUT_U32(skb, RTA_OIF, dev->ifindex);
187 return nlmsg_end(skb, nlh);
188
189nla_put_failure:
190 nlmsg_cancel(skb, nlh);
191 return -EMSGSIZE;
192}
193
194void rtm_phonet_notify(int event, struct net_device *dev, u8 dst)
195{
196 struct sk_buff *skb;
197 int err = -ENOBUFS;
198
199 skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
200 nla_total_size(1) + nla_total_size(4), GFP_KERNEL);
201 if (skb == NULL)
202 goto errout;
203 err = fill_route(skb, dev, dst, 0, 0, event);
204 if (err < 0) {
205 WARN_ON(err == -EMSGSIZE);
206 kfree_skb(skb);
207 goto errout;
208 }
209 rtnl_notify(skb, dev_net(dev), 0,
210 RTNLGRP_PHONET_ROUTE, NULL, GFP_KERNEL);
211 return;
212errout:
Rémi Denis-Courmont4b7673a2009-11-02 22:41:28 +0000213 rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_ROUTE, err);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000214}
215
216static const struct nla_policy rtm_phonet_policy[RTA_MAX+1] = {
217 [RTA_DST] = { .type = NLA_U8 },
218 [RTA_OIF] = { .type = NLA_U32 },
219};
220
221static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *attr)
222{
223 struct net *net = sock_net(skb->sk);
224 struct nlattr *tb[RTA_MAX+1];
225 struct net_device *dev;
226 struct rtmsg *rtm;
227 int err;
228 u8 dst;
229
230 if (!capable(CAP_SYS_ADMIN))
231 return -EPERM;
232
233 ASSERT_RTNL();
234
235 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_phonet_policy);
236 if (err < 0)
237 return err;
238
239 rtm = nlmsg_data(nlh);
240 if (rtm->rtm_table != RT_TABLE_MAIN || rtm->rtm_type != RTN_UNICAST)
241 return -EINVAL;
242 if (tb[RTA_DST] == NULL || tb[RTA_OIF] == NULL)
243 return -EINVAL;
244 dst = nla_get_u8(tb[RTA_DST]);
245 if (dst & 3) /* Phonet addresses only have 6 high-order bits */
246 return -EINVAL;
247
248 dev = __dev_get_by_index(net, nla_get_u32(tb[RTA_OIF]));
249 if (dev == NULL)
250 return -ENODEV;
251
252 if (nlh->nlmsg_type == RTM_NEWROUTE)
253 err = phonet_route_add(dev, dst);
254 else
255 err = phonet_route_del(dev, dst);
256 if (!err)
257 rtm_phonet_notify(nlh->nlmsg_type, dev, dst);
258 return err;
259}
260
261static int route_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
262{
263 struct net *net = sock_net(skb->sk);
264 u8 addr, addr_idx = 0, addr_start_idx = cb->args[0];
265
266 for (addr = 0; addr < 64; addr++) {
267 struct net_device *dev;
268
269 dev = phonet_route_get(net, addr << 2);
270 if (!dev)
271 continue;
272
273 if (addr_idx++ < addr_start_idx)
274 continue;
275 if (fill_route(skb, dev, addr << 2, NETLINK_CB(cb->skb).pid,
276 cb->nlh->nlmsg_seq, RTM_NEWROUTE))
277 goto out;
278 }
279
280out:
281 cb->args[0] = addr_idx;
282 cb->args[1] = 0;
283
284 return skb->len;
285}
286
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000287int __init phonet_netlink_register(void)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700288{
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000289 int err = __rtnl_register(PF_PHONET, RTM_NEWADDR, addr_doit, NULL);
290 if (err)
291 return err;
292
293 /* Further __rtnl_register() cannot fail */
294 __rtnl_register(PF_PHONET, RTM_DELADDR, addr_doit, NULL);
295 __rtnl_register(PF_PHONET, RTM_GETADDR, NULL, getaddr_dumpit);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000296 __rtnl_register(PF_PHONET, RTM_NEWROUTE, route_doit, NULL);
297 __rtnl_register(PF_PHONET, RTM_DELROUTE, route_doit, NULL);
298 __rtnl_register(PF_PHONET, RTM_GETROUTE, NULL, route_dumpit);
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000299 return 0;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700300}