blob: 59aebe29689077bfa77d37516aea4617fe3b8a50 [file] [log] [blame]
Thomas Gleixner2b27bdc2019-05-29 16:57:50 -07001// SPDX-License-Identifier: GPL-2.0-only
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -07002/*
3 * File: pn_netlink.c
4 *
5 * Phonet netlink interface
6 *
7 * Copyright (C) 2008 Nokia Corporation.
8 *
Rémi Denis-Courmont31fdc552012-06-13 22:29:03 +00009 * Authors: Sakari Ailus <sakari.ailus@nokia.com>
10 * Remi Denis-Courmont
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070011 */
12
13#include <linux/kernel.h>
14#include <linux/netlink.h>
15#include <linux/phonet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070017#include <net/sock.h>
18#include <net/phonet/pn_dev.h>
19
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +000020/* Device address handling */
21
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070022static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
Eric W. Biederman15e47302012-09-07 20:12:54 +000023 u32 portid, u32 seq, int event);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070024
Rémi Denis-Courmontc7a1a4c2009-06-24 01:07:44 +000025void phonet_address_notify(int event, struct net_device *dev, u8 addr)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070026{
27 struct sk_buff *skb;
28 int err = -ENOBUFS;
29
30 skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
31 nla_total_size(1), GFP_KERNEL);
32 if (skb == NULL)
33 goto errout;
34 err = fill_addr(skb, dev, addr, 0, 0, event);
35 if (err < 0) {
36 WARN_ON(err == -EMSGSIZE);
37 kfree_skb(skb);
38 goto errout;
39 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -080040 rtnl_notify(skb, dev_net(dev), 0,
41 RTNLGRP_PHONET_IFADDR, NULL, GFP_KERNEL);
42 return;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070043errout:
Rémi Denis-Courmont4b7673a2009-11-02 22:41:28 +000044 rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_IFADDR, err);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070045}
46
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070047static const struct nla_policy ifa_phonet_policy[IFA_MAX+1] = {
48 [IFA_LOCAL] = { .type = NLA_U8 },
49};
50
David Ahernc21ef3e2017-04-16 09:48:24 -070051static int addr_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
52 struct netlink_ext_ack *extack)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070053{
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070054 struct net *net = sock_net(skb->sk);
55 struct nlattr *tb[IFA_MAX+1];
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070056 struct net_device *dev;
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070057 struct ifaddrmsg *ifm;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070058 int err;
59 u8 pnaddr;
60
Eric W. Biederman90f62cf2014-04-23 14:29:27 -070061 if (!netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +000062 return -EPERM;
63
Eric W. Biederman90f62cf2014-04-23 14:29:27 -070064 if (!netlink_capable(skb, CAP_SYS_ADMIN))
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070065 return -EPERM;
66
67 ASSERT_RTNL();
68
Johannes Berg8cb08172019-04-26 14:07:28 +020069 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
70 ifa_phonet_policy, extack);
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070071 if (err < 0)
72 return err;
73
74 ifm = nlmsg_data(nlh);
75 if (tb[IFA_LOCAL] == NULL)
76 return -EINVAL;
77 pnaddr = nla_get_u8(tb[IFA_LOCAL]);
78 if (pnaddr & 3)
79 /* Phonet addresses only have 6 high-order bits */
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070080 return -EINVAL;
81
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070082 dev = __dev_get_by_index(net, ifm->ifa_index);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070083 if (dev == NULL)
84 return -ENODEV;
85
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070086 if (nlh->nlmsg_type == RTM_NEWADDR)
87 err = phonet_address_add(dev, pnaddr);
88 else
89 err = phonet_address_del(dev, pnaddr);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070090 if (!err)
Rémi Denis-Courmontc7a1a4c2009-06-24 01:07:44 +000091 phonet_address_notify(nlh->nlmsg_type, dev, pnaddr);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070092 return err;
93}
94
95static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
Eric W. Biederman15e47302012-09-07 20:12:54 +000096 u32 portid, u32 seq, int event)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070097{
98 struct ifaddrmsg *ifm;
99 struct nlmsghdr *nlh;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700100
Eric W. Biederman15e47302012-09-07 20:12:54 +0000101 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*ifm), 0);
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700102 if (nlh == NULL)
103 return -EMSGSIZE;
104
105 ifm = nlmsg_data(nlh);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700106 ifm->ifa_family = AF_PHONET;
107 ifm->ifa_prefixlen = 0;
108 ifm->ifa_flags = IFA_F_PERMANENT;
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700109 ifm->ifa_scope = RT_SCOPE_LINK;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700110 ifm->ifa_index = dev->ifindex;
David S. Miller7f116b52012-03-29 23:15:10 -0400111 if (nla_put_u8(skb, IFA_LOCAL, addr))
112 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +0100113 nlmsg_end(skb, nlh);
114 return 0;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700115
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700116nla_put_failure:
117 nlmsg_cancel(skb, nlh);
118 return -EMSGSIZE;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700119}
120
121static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
122{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000123 struct phonet_device_list *pndevs;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700124 struct phonet_device *pnd;
125 int dev_idx = 0, dev_start_idx = cb->args[0];
126 int addr_idx = 0, addr_start_idx = cb->args[1];
127
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000128 pndevs = phonet_device_list(sock_net(skb->sk));
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800129 rcu_read_lock();
130 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700131 u8 addr;
132
133 if (dev_idx > dev_start_idx)
134 addr_start_idx = 0;
135 if (dev_idx++ < dev_start_idx)
136 continue;
137
138 addr_idx = 0;
Akinobu Mitaa1ca14a2010-03-11 12:07:49 +0000139 for_each_set_bit(addr, pnd->addrs, 64) {
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700140 if (addr_idx++ < addr_start_idx)
141 continue;
142
143 if (fill_addr(skb, pnd->netdev, addr << 2,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000144 NETLINK_CB(cb->skb).portid,
Rémi Denis-Courmont998ec752009-09-08 23:59:51 +0000145 cb->nlh->nlmsg_seq, RTM_NEWADDR) < 0)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700146 goto out;
147 }
148 }
149
150out:
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800151 rcu_read_unlock();
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700152 cb->args[0] = dev_idx;
153 cb->args[1] = addr_idx;
154
155 return skb->len;
156}
157
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000158/* Routes handling */
159
160static int fill_route(struct sk_buff *skb, struct net_device *dev, u8 dst,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000161 u32 portid, u32 seq, int event)
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000162{
163 struct rtmsg *rtm;
164 struct nlmsghdr *nlh;
165
Eric W. Biederman15e47302012-09-07 20:12:54 +0000166 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), 0);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000167 if (nlh == NULL)
168 return -EMSGSIZE;
169
170 rtm = nlmsg_data(nlh);
171 rtm->rtm_family = AF_PHONET;
172 rtm->rtm_dst_len = 6;
173 rtm->rtm_src_len = 0;
174 rtm->rtm_tos = 0;
175 rtm->rtm_table = RT_TABLE_MAIN;
176 rtm->rtm_protocol = RTPROT_STATIC;
177 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
178 rtm->rtm_type = RTN_UNICAST;
179 rtm->rtm_flags = 0;
David S. Miller7f116b52012-03-29 23:15:10 -0400180 if (nla_put_u8(skb, RTA_DST, dst) ||
181 nla_put_u32(skb, RTA_OIF, dev->ifindex))
182 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +0100183 nlmsg_end(skb, nlh);
184 return 0;
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000185
186nla_put_failure:
187 nlmsg_cancel(skb, nlh);
188 return -EMSGSIZE;
189}
190
191void rtm_phonet_notify(int event, struct net_device *dev, u8 dst)
192{
193 struct sk_buff *skb;
194 int err = -ENOBUFS;
195
196 skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
197 nla_total_size(1) + nla_total_size(4), GFP_KERNEL);
198 if (skb == NULL)
199 goto errout;
200 err = fill_route(skb, dev, dst, 0, 0, event);
201 if (err < 0) {
202 WARN_ON(err == -EMSGSIZE);
203 kfree_skb(skb);
204 goto errout;
205 }
206 rtnl_notify(skb, dev_net(dev), 0,
207 RTNLGRP_PHONET_ROUTE, NULL, GFP_KERNEL);
208 return;
209errout:
Rémi Denis-Courmont4b7673a2009-11-02 22:41:28 +0000210 rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_ROUTE, err);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000211}
212
213static const struct nla_policy rtm_phonet_policy[RTA_MAX+1] = {
214 [RTA_DST] = { .type = NLA_U8 },
215 [RTA_OIF] = { .type = NLA_U32 },
216};
217
David Ahernc21ef3e2017-04-16 09:48:24 -0700218static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
219 struct netlink_ext_ack *extack)
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000220{
221 struct net *net = sock_net(skb->sk);
222 struct nlattr *tb[RTA_MAX+1];
223 struct net_device *dev;
224 struct rtmsg *rtm;
225 int err;
226 u8 dst;
227
Eric W. Biederman90f62cf2014-04-23 14:29:27 -0700228 if (!netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000229 return -EPERM;
230
Eric W. Biederman90f62cf2014-04-23 14:29:27 -0700231 if (!netlink_capable(skb, CAP_SYS_ADMIN))
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000232 return -EPERM;
233
234 ASSERT_RTNL();
235
Johannes Berg8cb08172019-04-26 14:07:28 +0200236 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
237 rtm_phonet_policy, extack);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000238 if (err < 0)
239 return err;
240
241 rtm = nlmsg_data(nlh);
242 if (rtm->rtm_table != RT_TABLE_MAIN || rtm->rtm_type != RTN_UNICAST)
243 return -EINVAL;
244 if (tb[RTA_DST] == NULL || tb[RTA_OIF] == NULL)
245 return -EINVAL;
246 dst = nla_get_u8(tb[RTA_DST]);
247 if (dst & 3) /* Phonet addresses only have 6 high-order bits */
248 return -EINVAL;
249
250 dev = __dev_get_by_index(net, nla_get_u32(tb[RTA_OIF]));
251 if (dev == NULL)
252 return -ENODEV;
253
254 if (nlh->nlmsg_type == RTM_NEWROUTE)
255 err = phonet_route_add(dev, dst);
256 else
257 err = phonet_route_del(dev, dst);
258 if (!err)
259 rtm_phonet_notify(nlh->nlmsg_type, dev, dst);
260 return err;
261}
262
263static int route_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
264{
265 struct net *net = sock_net(skb->sk);
Johannes Berg926e9872015-01-19 12:15:24 +0100266 u8 addr;
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000267
Eric Dumazete67f88d2011-04-27 22:56:07 +0000268 rcu_read_lock();
Johannes Berg926e9872015-01-19 12:15:24 +0100269 for (addr = cb->args[0]; addr < 64; addr++) {
270 struct net_device *dev = phonet_route_get_rcu(net, addr << 2);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000271
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000272 if (!dev)
273 continue;
274
Johannes Berg926e9872015-01-19 12:15:24 +0100275 if (fill_route(skb, dev, addr << 2, NETLINK_CB(cb->skb).portid,
276 cb->nlh->nlmsg_seq, RTM_NEWROUTE) < 0)
277 goto out;
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000278 }
279
280out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000281 rcu_read_unlock();
Johannes Berg926e9872015-01-19 12:15:24 +0100282 cb->args[0] = addr;
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000283
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{
Florian Westphalc1c502b2017-12-02 21:44:07 +0100289 int err = rtnl_register_module(THIS_MODULE, PF_PHONET, RTM_NEWADDR,
290 addr_doit, NULL, 0);
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000291 if (err)
292 return err;
293
Florian Westphalc1c502b2017-12-02 21:44:07 +0100294 /* Further rtnl_register_module() cannot fail */
295 rtnl_register_module(THIS_MODULE, PF_PHONET, RTM_DELADDR,
296 addr_doit, NULL, 0);
297 rtnl_register_module(THIS_MODULE, PF_PHONET, RTM_GETADDR,
298 NULL, getaddr_dumpit, 0);
299 rtnl_register_module(THIS_MODULE, PF_PHONET, RTM_NEWROUTE,
300 route_doit, NULL, 0);
301 rtnl_register_module(THIS_MODULE, PF_PHONET, RTM_DELROUTE,
302 route_doit, NULL, 0);
303 rtnl_register_module(THIS_MODULE, PF_PHONET, RTM_GETROUTE,
304 NULL, route_dumpit, 0);
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000305 return 0;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700306}