blob: 363799bf97f661e0a2f724b1e74aa1560e3c5650 [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 *
Rémi Denis-Courmont31fdc5552012-06-13 22:29:03 +00008 * Authors: Sakari Ailus <sakari.ailus@nokia.com>
9 * Remi Denis-Courmont
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070010 *
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,
Eric W. Biederman15e47302012-09-07 20:12:54 +000036 u32 portid, u32 seq, int event);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070037
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
Thomas Graf661d2962013-03-21 07:45:29 +000064static int addr_doit(struct sk_buff *skb, struct nlmsghdr *nlh)
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
Eric W. Biederman90f62cf2014-04-23 14:29:27 -070073 if (!netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +000074 return -EPERM;
75
Eric W. Biederman90f62cf2014-04-23 14:29:27 -070076 if (!netlink_capable(skb, CAP_SYS_ADMIN))
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070077 return -EPERM;
78
79 ASSERT_RTNL();
80
Johannes Bergfceb6432017-04-12 14:34:07 +020081 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_phonet_policy,
82 NULL);
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070083 if (err < 0)
84 return err;
85
86 ifm = nlmsg_data(nlh);
87 if (tb[IFA_LOCAL] == NULL)
88 return -EINVAL;
89 pnaddr = nla_get_u8(tb[IFA_LOCAL]);
90 if (pnaddr & 3)
91 /* Phonet addresses only have 6 high-order bits */
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070092 return -EINVAL;
93
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070094 dev = __dev_get_by_index(net, ifm->ifa_index);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070095 if (dev == NULL)
96 return -ENODEV;
97
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070098 if (nlh->nlmsg_type == RTM_NEWADDR)
99 err = phonet_address_add(dev, pnaddr);
100 else
101 err = phonet_address_del(dev, pnaddr);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700102 if (!err)
Rémi Denis-Courmontc7a1a4c2009-06-24 01:07:44 +0000103 phonet_address_notify(nlh->nlmsg_type, dev, pnaddr);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700104 return err;
105}
106
107static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000108 u32 portid, u32 seq, int event)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700109{
110 struct ifaddrmsg *ifm;
111 struct nlmsghdr *nlh;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700112
Eric W. Biederman15e47302012-09-07 20:12:54 +0000113 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*ifm), 0);
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700114 if (nlh == NULL)
115 return -EMSGSIZE;
116
117 ifm = nlmsg_data(nlh);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700118 ifm->ifa_family = AF_PHONET;
119 ifm->ifa_prefixlen = 0;
120 ifm->ifa_flags = IFA_F_PERMANENT;
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700121 ifm->ifa_scope = RT_SCOPE_LINK;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700122 ifm->ifa_index = dev->ifindex;
David S. Miller7f116b52012-03-29 23:15:10 -0400123 if (nla_put_u8(skb, IFA_LOCAL, addr))
124 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +0100125 nlmsg_end(skb, nlh);
126 return 0;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700127
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700128nla_put_failure:
129 nlmsg_cancel(skb, nlh);
130 return -EMSGSIZE;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700131}
132
133static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
134{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000135 struct phonet_device_list *pndevs;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700136 struct phonet_device *pnd;
137 int dev_idx = 0, dev_start_idx = cb->args[0];
138 int addr_idx = 0, addr_start_idx = cb->args[1];
139
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000140 pndevs = phonet_device_list(sock_net(skb->sk));
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800141 rcu_read_lock();
142 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700143 u8 addr;
144
145 if (dev_idx > dev_start_idx)
146 addr_start_idx = 0;
147 if (dev_idx++ < dev_start_idx)
148 continue;
149
150 addr_idx = 0;
Akinobu Mitaa1ca14a2010-03-11 12:07:49 +0000151 for_each_set_bit(addr, pnd->addrs, 64) {
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700152 if (addr_idx++ < addr_start_idx)
153 continue;
154
155 if (fill_addr(skb, pnd->netdev, addr << 2,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000156 NETLINK_CB(cb->skb).portid,
Rémi Denis-Courmont998ec752009-09-08 23:59:51 +0000157 cb->nlh->nlmsg_seq, RTM_NEWADDR) < 0)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700158 goto out;
159 }
160 }
161
162out:
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800163 rcu_read_unlock();
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700164 cb->args[0] = dev_idx;
165 cb->args[1] = addr_idx;
166
167 return skb->len;
168}
169
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000170/* Routes handling */
171
172static int fill_route(struct sk_buff *skb, struct net_device *dev, u8 dst,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000173 u32 portid, u32 seq, int event)
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000174{
175 struct rtmsg *rtm;
176 struct nlmsghdr *nlh;
177
Eric W. Biederman15e47302012-09-07 20:12:54 +0000178 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), 0);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000179 if (nlh == NULL)
180 return -EMSGSIZE;
181
182 rtm = nlmsg_data(nlh);
183 rtm->rtm_family = AF_PHONET;
184 rtm->rtm_dst_len = 6;
185 rtm->rtm_src_len = 0;
186 rtm->rtm_tos = 0;
187 rtm->rtm_table = RT_TABLE_MAIN;
188 rtm->rtm_protocol = RTPROT_STATIC;
189 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
190 rtm->rtm_type = RTN_UNICAST;
191 rtm->rtm_flags = 0;
David S. Miller7f116b52012-03-29 23:15:10 -0400192 if (nla_put_u8(skb, RTA_DST, dst) ||
193 nla_put_u32(skb, RTA_OIF, dev->ifindex))
194 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +0100195 nlmsg_end(skb, nlh);
196 return 0;
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000197
198nla_put_failure:
199 nlmsg_cancel(skb, nlh);
200 return -EMSGSIZE;
201}
202
203void rtm_phonet_notify(int event, struct net_device *dev, u8 dst)
204{
205 struct sk_buff *skb;
206 int err = -ENOBUFS;
207
208 skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
209 nla_total_size(1) + nla_total_size(4), GFP_KERNEL);
210 if (skb == NULL)
211 goto errout;
212 err = fill_route(skb, dev, dst, 0, 0, event);
213 if (err < 0) {
214 WARN_ON(err == -EMSGSIZE);
215 kfree_skb(skb);
216 goto errout;
217 }
218 rtnl_notify(skb, dev_net(dev), 0,
219 RTNLGRP_PHONET_ROUTE, NULL, GFP_KERNEL);
220 return;
221errout:
Rémi Denis-Courmont4b7673a2009-11-02 22:41:28 +0000222 rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_ROUTE, err);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000223}
224
225static const struct nla_policy rtm_phonet_policy[RTA_MAX+1] = {
226 [RTA_DST] = { .type = NLA_U8 },
227 [RTA_OIF] = { .type = NLA_U32 },
228};
229
Thomas Graf661d2962013-03-21 07:45:29 +0000230static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh)
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000231{
232 struct net *net = sock_net(skb->sk);
233 struct nlattr *tb[RTA_MAX+1];
234 struct net_device *dev;
235 struct rtmsg *rtm;
236 int err;
237 u8 dst;
238
Eric W. Biederman90f62cf2014-04-23 14:29:27 -0700239 if (!netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000240 return -EPERM;
241
Eric W. Biederman90f62cf2014-04-23 14:29:27 -0700242 if (!netlink_capable(skb, CAP_SYS_ADMIN))
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000243 return -EPERM;
244
245 ASSERT_RTNL();
246
Johannes Bergfceb6432017-04-12 14:34:07 +0200247 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_phonet_policy,
248 NULL);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000249 if (err < 0)
250 return err;
251
252 rtm = nlmsg_data(nlh);
253 if (rtm->rtm_table != RT_TABLE_MAIN || rtm->rtm_type != RTN_UNICAST)
254 return -EINVAL;
255 if (tb[RTA_DST] == NULL || tb[RTA_OIF] == NULL)
256 return -EINVAL;
257 dst = nla_get_u8(tb[RTA_DST]);
258 if (dst & 3) /* Phonet addresses only have 6 high-order bits */
259 return -EINVAL;
260
261 dev = __dev_get_by_index(net, nla_get_u32(tb[RTA_OIF]));
262 if (dev == NULL)
263 return -ENODEV;
264
265 if (nlh->nlmsg_type == RTM_NEWROUTE)
266 err = phonet_route_add(dev, dst);
267 else
268 err = phonet_route_del(dev, dst);
269 if (!err)
270 rtm_phonet_notify(nlh->nlmsg_type, dev, dst);
271 return err;
272}
273
274static int route_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
275{
276 struct net *net = sock_net(skb->sk);
Johannes Berg926e9872015-01-19 12:15:24 +0100277 u8 addr;
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000278
Eric Dumazete67f88d2011-04-27 22:56:07 +0000279 rcu_read_lock();
Johannes Berg926e9872015-01-19 12:15:24 +0100280 for (addr = cb->args[0]; addr < 64; addr++) {
281 struct net_device *dev = phonet_route_get_rcu(net, addr << 2);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000282
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000283 if (!dev)
284 continue;
285
Johannes Berg926e9872015-01-19 12:15:24 +0100286 if (fill_route(skb, dev, addr << 2, NETLINK_CB(cb->skb).portid,
287 cb->nlh->nlmsg_seq, RTM_NEWROUTE) < 0)
288 goto out;
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000289 }
290
291out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000292 rcu_read_unlock();
Johannes Berg926e9872015-01-19 12:15:24 +0100293 cb->args[0] = addr;
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000294
295 return skb->len;
296}
297
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000298int __init phonet_netlink_register(void)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700299{
Greg Rosec7ac8672011-06-10 01:27:09 +0000300 int err = __rtnl_register(PF_PHONET, RTM_NEWADDR, addr_doit,
301 NULL, NULL);
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000302 if (err)
303 return err;
304
305 /* Further __rtnl_register() cannot fail */
Greg Rosec7ac8672011-06-10 01:27:09 +0000306 __rtnl_register(PF_PHONET, RTM_DELADDR, addr_doit, NULL, NULL);
307 __rtnl_register(PF_PHONET, RTM_GETADDR, NULL, getaddr_dumpit, NULL);
308 __rtnl_register(PF_PHONET, RTM_NEWROUTE, route_doit, NULL, NULL);
309 __rtnl_register(PF_PHONET, RTM_DELROUTE, route_doit, NULL, NULL);
310 __rtnl_register(PF_PHONET, RTM_GETROUTE, NULL, route_dumpit, NULL);
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000311 return 0;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700312}