Merge tag 'rproc-v6.14' of git://git.kernel.org/pub/scm/linux/kernel/git/remoteproc...
[linux.git] / net / phonet / pn_dev.c
blob5c36bae37b8fa85d2e97bf11d099c6c8599dcc5f
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * File: pn_dev.c
5 * Phonet network device
7 * Copyright (C) 2008 Nokia Corporation.
9 * Authors: Sakari Ailus <sakari.ailus@nokia.com>
10 * RĂ©mi Denis-Courmont
13 #include <linux/kernel.h>
14 #include <linux/net.h>
15 #include <linux/slab.h>
16 #include <linux/netdevice.h>
17 #include <linux/phonet.h>
18 #include <linux/proc_fs.h>
19 #include <linux/if_arp.h>
20 #include <net/sock.h>
21 #include <net/netns/generic.h>
22 #include <net/phonet/pn_dev.h>
24 struct phonet_routes {
25 spinlock_t lock;
26 struct net_device __rcu *table[64];
29 struct phonet_net {
30 struct phonet_device_list pndevs;
31 struct phonet_routes routes;
34 static unsigned int phonet_net_id __read_mostly;
36 static struct phonet_net *phonet_pernet(struct net *net)
38 return net_generic(net, phonet_net_id);
41 struct phonet_device_list *phonet_device_list(struct net *net)
43 struct phonet_net *pnn = phonet_pernet(net);
44 return &pnn->pndevs;
47 /* Allocate new Phonet device. */
48 static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
50 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
51 struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
52 if (pnd == NULL)
53 return NULL;
54 pnd->netdev = dev;
55 bitmap_zero(pnd->addrs, 64);
57 lockdep_assert_held(&pndevs->lock);
58 list_add_rcu(&pnd->list, &pndevs->list);
59 return pnd;
62 static struct phonet_device *__phonet_get(struct net_device *dev)
64 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
65 struct phonet_device *pnd;
67 lockdep_assert_held(&pndevs->lock);
69 list_for_each_entry(pnd, &pndevs->list, list) {
70 if (pnd->netdev == dev)
71 return pnd;
73 return NULL;
76 static struct phonet_device *__phonet_get_rcu(struct net_device *dev)
78 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
79 struct phonet_device *pnd;
81 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
82 if (pnd->netdev == dev)
83 return pnd;
85 return NULL;
88 static void phonet_device_destroy(struct net_device *dev)
90 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
91 struct phonet_device *pnd;
93 ASSERT_RTNL();
95 spin_lock(&pndevs->lock);
97 pnd = __phonet_get(dev);
98 if (pnd)
99 list_del_rcu(&pnd->list);
101 spin_unlock(&pndevs->lock);
103 if (pnd) {
104 struct net *net = dev_net(dev);
105 u32 ifindex = dev->ifindex;
106 u8 addr;
108 for_each_set_bit(addr, pnd->addrs, 64)
109 phonet_address_notify(net, RTM_DELADDR, ifindex, addr);
111 kfree(pnd);
115 struct net_device *phonet_device_get(struct net *net)
117 struct phonet_device_list *pndevs = phonet_device_list(net);
118 struct phonet_device *pnd;
119 struct net_device *dev = NULL;
121 rcu_read_lock();
122 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
123 dev = pnd->netdev;
124 BUG_ON(!dev);
126 if ((dev->reg_state == NETREG_REGISTERED) &&
127 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
128 break;
129 dev = NULL;
131 dev_hold(dev);
132 rcu_read_unlock();
133 return dev;
136 int phonet_address_add(struct net_device *dev, u8 addr)
138 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
139 struct phonet_device *pnd;
140 int err = 0;
142 spin_lock(&pndevs->lock);
144 /* Find or create Phonet-specific device data */
145 pnd = __phonet_get(dev);
146 if (pnd == NULL)
147 pnd = __phonet_device_alloc(dev);
148 if (unlikely(pnd == NULL))
149 err = -ENOMEM;
150 else if (test_and_set_bit(addr >> 2, pnd->addrs))
151 err = -EEXIST;
153 spin_unlock(&pndevs->lock);
155 return err;
158 int phonet_address_del(struct net_device *dev, u8 addr)
160 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
161 struct phonet_device *pnd;
162 int err = 0;
164 spin_lock(&pndevs->lock);
166 pnd = __phonet_get(dev);
167 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) {
168 err = -EADDRNOTAVAIL;
169 pnd = NULL;
170 } else if (bitmap_empty(pnd->addrs, 64))
171 list_del_rcu(&pnd->list);
172 else
173 pnd = NULL;
175 spin_unlock(&pndevs->lock);
177 if (pnd)
178 kfree_rcu(pnd, rcu);
180 return err;
183 /* Gets a source address toward a destination, through a interface. */
184 u8 phonet_address_get(struct net_device *dev, u8 daddr)
186 struct phonet_device *pnd;
187 u8 saddr;
189 rcu_read_lock();
190 pnd = __phonet_get_rcu(dev);
191 if (pnd) {
192 BUG_ON(bitmap_empty(pnd->addrs, 64));
194 /* Use same source address as destination, if possible */
195 if (test_bit(daddr >> 2, pnd->addrs))
196 saddr = daddr;
197 else
198 saddr = find_first_bit(pnd->addrs, 64) << 2;
199 } else
200 saddr = PN_NO_ADDR;
201 rcu_read_unlock();
203 if (saddr == PN_NO_ADDR) {
204 /* Fallback to another device */
205 struct net_device *def_dev;
207 def_dev = phonet_device_get(dev_net(dev));
208 if (def_dev) {
209 if (def_dev != dev)
210 saddr = phonet_address_get(def_dev, daddr);
211 dev_put(def_dev);
214 return saddr;
217 int phonet_address_lookup(struct net *net, u8 addr)
219 struct phonet_device_list *pndevs = phonet_device_list(net);
220 struct phonet_device *pnd;
221 int err = -EADDRNOTAVAIL;
223 rcu_read_lock();
224 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
225 /* Don't allow unregistering devices! */
226 if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
227 ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
228 continue;
230 if (test_bit(addr >> 2, pnd->addrs)) {
231 err = 0;
232 goto found;
235 found:
236 rcu_read_unlock();
237 return err;
240 /* automatically configure a Phonet device, if supported */
241 static int phonet_device_autoconf(struct net_device *dev)
243 struct if_phonet_req req;
244 int ret;
246 if (!dev->netdev_ops->ndo_siocdevprivate)
247 return -EOPNOTSUPP;
249 ret = dev->netdev_ops->ndo_siocdevprivate(dev, (struct ifreq *)&req,
250 NULL, SIOCPNGAUTOCONF);
251 if (ret < 0)
252 return ret;
254 ASSERT_RTNL();
255 ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device);
256 if (ret)
257 return ret;
259 phonet_address_notify(dev_net(dev), RTM_NEWADDR, dev->ifindex,
260 req.ifr_phonet_autoconf.device);
261 return 0;
264 static void phonet_route_autodel(struct net_device *dev)
266 struct net *net = dev_net(dev);
267 DECLARE_BITMAP(deleted, 64);
268 u32 ifindex = dev->ifindex;
269 struct phonet_net *pnn;
270 unsigned int i;
272 pnn = phonet_pernet(net);
274 /* Remove left-over Phonet routes */
275 bitmap_zero(deleted, 64);
277 spin_lock(&pnn->routes.lock);
278 for (i = 0; i < 64; i++) {
279 if (rcu_access_pointer(pnn->routes.table[i]) == dev) {
280 RCU_INIT_POINTER(pnn->routes.table[i], NULL);
281 set_bit(i, deleted);
284 spin_unlock(&pnn->routes.lock);
286 if (bitmap_empty(deleted, 64))
287 return; /* short-circuit RCU */
288 synchronize_rcu();
289 for_each_set_bit(i, deleted, 64) {
290 rtm_phonet_notify(net, RTM_DELROUTE, ifindex, i);
291 dev_put(dev);
295 /* notify Phonet of device events */
296 static int phonet_device_notify(struct notifier_block *me, unsigned long what,
297 void *ptr)
299 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
301 switch (what) {
302 case NETDEV_REGISTER:
303 if (dev->type == ARPHRD_PHONET)
304 phonet_device_autoconf(dev);
305 break;
306 case NETDEV_UNREGISTER:
307 phonet_device_destroy(dev);
308 phonet_route_autodel(dev);
309 break;
311 return 0;
315 static struct notifier_block phonet_device_notifier = {
316 .notifier_call = phonet_device_notify,
317 .priority = 0,
320 /* Per-namespace Phonet devices handling */
321 static int __net_init phonet_init_net(struct net *net)
323 struct phonet_net *pnn = phonet_pernet(net);
325 if (!proc_create_net("phonet", 0, net->proc_net, &pn_sock_seq_ops,
326 sizeof(struct seq_net_private)))
327 return -ENOMEM;
329 INIT_LIST_HEAD(&pnn->pndevs.list);
330 spin_lock_init(&pnn->pndevs.lock);
331 spin_lock_init(&pnn->routes.lock);
332 return 0;
335 static void __net_exit phonet_exit_net(struct net *net)
337 struct phonet_net *pnn = phonet_pernet(net);
339 remove_proc_entry("phonet", net->proc_net);
340 WARN_ON_ONCE(!list_empty(&pnn->pndevs.list));
343 static struct pernet_operations phonet_net_ops = {
344 .init = phonet_init_net,
345 .exit = phonet_exit_net,
346 .id = &phonet_net_id,
347 .size = sizeof(struct phonet_net),
350 /* Initialize Phonet devices list */
351 int __init phonet_device_init(void)
353 int err = register_pernet_subsys(&phonet_net_ops);
354 if (err)
355 return err;
357 proc_create_net("pnresource", 0, init_net.proc_net, &pn_res_seq_ops,
358 sizeof(struct seq_net_private));
359 register_netdevice_notifier(&phonet_device_notifier);
360 err = phonet_netlink_register();
361 if (err)
362 phonet_device_exit();
363 return err;
366 void phonet_device_exit(void)
368 rtnl_unregister_all(PF_PHONET);
369 unregister_netdevice_notifier(&phonet_device_notifier);
370 unregister_pernet_subsys(&phonet_net_ops);
371 remove_proc_entry("pnresource", init_net.proc_net);
374 int phonet_route_add(struct net_device *dev, u8 daddr)
376 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
377 struct phonet_routes *routes = &pnn->routes;
378 int err = -EEXIST;
380 daddr = daddr >> 2;
382 spin_lock(&routes->lock);
383 if (routes->table[daddr] == NULL) {
384 rcu_assign_pointer(routes->table[daddr], dev);
385 dev_hold(dev);
386 err = 0;
388 spin_unlock(&routes->lock);
390 return err;
393 int phonet_route_del(struct net_device *dev, u8 daddr)
395 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
396 struct phonet_routes *routes = &pnn->routes;
398 daddr = daddr >> 2;
400 spin_lock(&routes->lock);
401 if (rcu_access_pointer(routes->table[daddr]) == dev)
402 RCU_INIT_POINTER(routes->table[daddr], NULL);
403 else
404 dev = NULL;
405 spin_unlock(&routes->lock);
407 if (!dev)
408 return -ENOENT;
410 /* Note : our caller must call synchronize_rcu() and dev_put(dev) */
412 return 0;
415 struct net_device *phonet_route_get_rcu(struct net *net, u8 daddr)
417 struct phonet_net *pnn = phonet_pernet(net);
418 struct phonet_routes *routes = &pnn->routes;
419 struct net_device *dev;
421 daddr >>= 2;
422 dev = rcu_dereference(routes->table[daddr]);
423 return dev;
426 struct net_device *phonet_route_output(struct net *net, u8 daddr)
428 struct phonet_net *pnn = phonet_pernet(net);
429 struct phonet_routes *routes = &pnn->routes;
430 struct net_device *dev;
432 daddr >>= 2;
433 rcu_read_lock();
434 dev = rcu_dereference(routes->table[daddr]);
435 dev_hold(dev);
436 rcu_read_unlock();
438 if (!dev)
439 dev = phonet_device_get(net); /* Default route */
440 return dev;