xref: /openbmc/linux/net/phonet/pn_netlink.c (revision 33ac9dba)
1 /*
2  * File: pn_netlink.c
3  *
4  * Phonet netlink interface
5  *
6  * Copyright (C) 2008 Nokia Corporation.
7  *
8  * Authors: Sakari Ailus <sakari.ailus@nokia.com>
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 
38 void phonet_address_notify(int event, struct net_device *dev, u8 addr)
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,
54 		    RTNLGRP_PHONET_IFADDR, NULL, GFP_KERNEL);
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)
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 (!netlink_capable(skb, CAP_NET_ADMIN))
74 		return -EPERM;
75 
76 	if (!netlink_capable(skb, CAP_SYS_ADMIN))
77 		return -EPERM;
78 
79 	ASSERT_RTNL();
80 
81 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_phonet_policy);
82 	if (err < 0)
83 		return err;
84 
85 	ifm = nlmsg_data(nlh);
86 	if (tb[IFA_LOCAL] == NULL)
87 		return -EINVAL;
88 	pnaddr = nla_get_u8(tb[IFA_LOCAL]);
89 	if (pnaddr & 3)
90 		/* Phonet addresses only have 6 high-order bits */
91 		return -EINVAL;
92 
93 	dev = __dev_get_by_index(net, ifm->ifa_index);
94 	if (dev == NULL)
95 		return -ENODEV;
96 
97 	if (nlh->nlmsg_type == RTM_NEWADDR)
98 		err = phonet_address_add(dev, pnaddr);
99 	else
100 		err = phonet_address_del(dev, pnaddr);
101 	if (!err)
102 		phonet_address_notify(nlh->nlmsg_type, dev, pnaddr);
103 	return err;
104 }
105 
106 static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
107 			u32 portid, u32 seq, int event)
108 {
109 	struct ifaddrmsg *ifm;
110 	struct nlmsghdr *nlh;
111 
112 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*ifm), 0);
113 	if (nlh == NULL)
114 		return -EMSGSIZE;
115 
116 	ifm = nlmsg_data(nlh);
117 	ifm->ifa_family = AF_PHONET;
118 	ifm->ifa_prefixlen = 0;
119 	ifm->ifa_flags = IFA_F_PERMANENT;
120 	ifm->ifa_scope = RT_SCOPE_LINK;
121 	ifm->ifa_index = dev->ifindex;
122 	if (nla_put_u8(skb, IFA_LOCAL, addr))
123 		goto nla_put_failure;
124 	return nlmsg_end(skb, nlh);
125 
126 nla_put_failure:
127 	nlmsg_cancel(skb, nlh);
128 	return -EMSGSIZE;
129 }
130 
131 static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
132 {
133 	struct phonet_device_list *pndevs;
134 	struct phonet_device *pnd;
135 	int dev_idx = 0, dev_start_idx = cb->args[0];
136 	int addr_idx = 0, addr_start_idx = cb->args[1];
137 
138 	pndevs = phonet_device_list(sock_net(skb->sk));
139 	rcu_read_lock();
140 	list_for_each_entry_rcu(pnd, &pndevs->list, list) {
141 		u8 addr;
142 
143 		if (dev_idx > dev_start_idx)
144 			addr_start_idx = 0;
145 		if (dev_idx++ < dev_start_idx)
146 			continue;
147 
148 		addr_idx = 0;
149 		for_each_set_bit(addr, pnd->addrs, 64) {
150 			if (addr_idx++ < addr_start_idx)
151 				continue;
152 
153 			if (fill_addr(skb, pnd->netdev, addr << 2,
154 					 NETLINK_CB(cb->skb).portid,
155 					cb->nlh->nlmsg_seq, RTM_NEWADDR) < 0)
156 				goto out;
157 		}
158 	}
159 
160 out:
161 	rcu_read_unlock();
162 	cb->args[0] = dev_idx;
163 	cb->args[1] = addr_idx;
164 
165 	return skb->len;
166 }
167 
168 /* Routes handling */
169 
170 static int fill_route(struct sk_buff *skb, struct net_device *dev, u8 dst,
171 			u32 portid, u32 seq, int event)
172 {
173 	struct rtmsg *rtm;
174 	struct nlmsghdr *nlh;
175 
176 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), 0);
177 	if (nlh == NULL)
178 		return -EMSGSIZE;
179 
180 	rtm = nlmsg_data(nlh);
181 	rtm->rtm_family = AF_PHONET;
182 	rtm->rtm_dst_len = 6;
183 	rtm->rtm_src_len = 0;
184 	rtm->rtm_tos = 0;
185 	rtm->rtm_table = RT_TABLE_MAIN;
186 	rtm->rtm_protocol = RTPROT_STATIC;
187 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
188 	rtm->rtm_type = RTN_UNICAST;
189 	rtm->rtm_flags = 0;
190 	if (nla_put_u8(skb, RTA_DST, dst) ||
191 	    nla_put_u32(skb, RTA_OIF, dev->ifindex))
192 		goto nla_put_failure;
193 	return nlmsg_end(skb, nlh);
194 
195 nla_put_failure:
196 	nlmsg_cancel(skb, nlh);
197 	return -EMSGSIZE;
198 }
199 
200 void rtm_phonet_notify(int event, struct net_device *dev, u8 dst)
201 {
202 	struct sk_buff *skb;
203 	int err = -ENOBUFS;
204 
205 	skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
206 			nla_total_size(1) + nla_total_size(4), GFP_KERNEL);
207 	if (skb == NULL)
208 		goto errout;
209 	err = fill_route(skb, dev, dst, 0, 0, event);
210 	if (err < 0) {
211 		WARN_ON(err == -EMSGSIZE);
212 		kfree_skb(skb);
213 		goto errout;
214 	}
215 	rtnl_notify(skb, dev_net(dev), 0,
216 			  RTNLGRP_PHONET_ROUTE, NULL, GFP_KERNEL);
217 	return;
218 errout:
219 	rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_ROUTE, err);
220 }
221 
222 static const struct nla_policy rtm_phonet_policy[RTA_MAX+1] = {
223 	[RTA_DST] = { .type = NLA_U8 },
224 	[RTA_OIF] = { .type = NLA_U32 },
225 };
226 
227 static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh)
228 {
229 	struct net *net = sock_net(skb->sk);
230 	struct nlattr *tb[RTA_MAX+1];
231 	struct net_device *dev;
232 	struct rtmsg *rtm;
233 	int err;
234 	u8 dst;
235 
236 	if (!netlink_capable(skb, CAP_NET_ADMIN))
237 		return -EPERM;
238 
239 	if (!netlink_capable(skb, CAP_SYS_ADMIN))
240 		return -EPERM;
241 
242 	ASSERT_RTNL();
243 
244 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_phonet_policy);
245 	if (err < 0)
246 		return err;
247 
248 	rtm = nlmsg_data(nlh);
249 	if (rtm->rtm_table != RT_TABLE_MAIN || rtm->rtm_type != RTN_UNICAST)
250 		return -EINVAL;
251 	if (tb[RTA_DST] == NULL || tb[RTA_OIF] == NULL)
252 		return -EINVAL;
253 	dst = nla_get_u8(tb[RTA_DST]);
254 	if (dst & 3) /* Phonet addresses only have 6 high-order bits */
255 		return -EINVAL;
256 
257 	dev = __dev_get_by_index(net, nla_get_u32(tb[RTA_OIF]));
258 	if (dev == NULL)
259 		return -ENODEV;
260 
261 	if (nlh->nlmsg_type == RTM_NEWROUTE)
262 		err = phonet_route_add(dev, dst);
263 	else
264 		err = phonet_route_del(dev, dst);
265 	if (!err)
266 		rtm_phonet_notify(nlh->nlmsg_type, dev, dst);
267 	return err;
268 }
269 
270 static int route_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
271 {
272 	struct net *net = sock_net(skb->sk);
273 	u8 addr, addr_idx = 0, addr_start_idx = cb->args[0];
274 
275 	rcu_read_lock();
276 	for (addr = 0; addr < 64; addr++) {
277 		struct net_device *dev;
278 
279 		dev = phonet_route_get_rcu(net, addr << 2);
280 		if (!dev)
281 			continue;
282 
283 		if (addr_idx++ < addr_start_idx)
284 			continue;
285 		if (fill_route(skb, dev, addr << 2, NETLINK_CB(cb->skb).portid,
286 				cb->nlh->nlmsg_seq, RTM_NEWROUTE))
287 			goto out;
288 	}
289 
290 out:
291 	rcu_read_unlock();
292 	cb->args[0] = addr_idx;
293 	cb->args[1] = 0;
294 
295 	return skb->len;
296 }
297 
298 int __init phonet_netlink_register(void)
299 {
300 	int err = __rtnl_register(PF_PHONET, RTM_NEWADDR, addr_doit,
301 				  NULL, NULL);
302 	if (err)
303 		return err;
304 
305 	/* Further __rtnl_register() cannot fail */
306 	__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);
311 	return 0;
312 }
313