1 // SPDX-License-Identifier: GPL-2.0-only
5 * Phonet network device
7 * Copyright (C) 2008 Nokia Corporation.
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>
21 #include <net/netns/generic.h>
22 #include <net/phonet/pn_dev.h>
24 struct phonet_routes {
26 struct net_device __rcu *table[64];
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);
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);
55 bitmap_zero(pnd->addrs, 64);
57 BUG_ON(!mutex_is_locked(&pndevs->lock));
58 list_add_rcu(&pnd->list, &pndevs->list);
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 BUG_ON(!mutex_is_locked(&pndevs->lock));
68 list_for_each_entry(pnd, &pndevs->list, list) {
69 if (pnd->netdev == dev)
75 static struct phonet_device *__phonet_get_rcu(struct net_device *dev)
77 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
78 struct phonet_device *pnd;
80 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
81 if (pnd->netdev == dev)
87 static void phonet_device_destroy(struct net_device *dev)
89 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
90 struct phonet_device *pnd;
94 mutex_lock(&pndevs->lock);
95 pnd = __phonet_get(dev);
97 list_del_rcu(&pnd->list);
98 mutex_unlock(&pndevs->lock);
103 for_each_set_bit(addr, pnd->addrs, 64)
104 phonet_address_notify(RTM_DELADDR, dev, addr);
109 struct net_device *phonet_device_get(struct net *net)
111 struct phonet_device_list *pndevs = phonet_device_list(net);
112 struct phonet_device *pnd;
113 struct net_device *dev = NULL;
116 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
120 if ((dev->reg_state == NETREG_REGISTERED) &&
121 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
131 int phonet_address_add(struct net_device *dev, u8 addr)
133 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
134 struct phonet_device *pnd;
137 mutex_lock(&pndevs->lock);
138 /* Find or create Phonet-specific device data */
139 pnd = __phonet_get(dev);
141 pnd = __phonet_device_alloc(dev);
142 if (unlikely(pnd == NULL))
144 else if (test_and_set_bit(addr >> 2, pnd->addrs))
146 mutex_unlock(&pndevs->lock);
150 int phonet_address_del(struct net_device *dev, u8 addr)
152 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
153 struct phonet_device *pnd;
156 mutex_lock(&pndevs->lock);
157 pnd = __phonet_get(dev);
158 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) {
159 err = -EADDRNOTAVAIL;
161 } else if (bitmap_empty(pnd->addrs, 64))
162 list_del_rcu(&pnd->list);
165 mutex_unlock(&pndevs->lock);
173 /* Gets a source address toward a destination, through a interface. */
174 u8 phonet_address_get(struct net_device *dev, u8 daddr)
176 struct phonet_device *pnd;
180 pnd = __phonet_get_rcu(dev);
182 BUG_ON(bitmap_empty(pnd->addrs, 64));
184 /* Use same source address as destination, if possible */
185 if (test_bit(daddr >> 2, pnd->addrs))
188 saddr = find_first_bit(pnd->addrs, 64) << 2;
193 if (saddr == PN_NO_ADDR) {
194 /* Fallback to another device */
195 struct net_device *def_dev;
197 def_dev = phonet_device_get(dev_net(dev));
200 saddr = phonet_address_get(def_dev, daddr);
207 int phonet_address_lookup(struct net *net, u8 addr)
209 struct phonet_device_list *pndevs = phonet_device_list(net);
210 struct phonet_device *pnd;
211 int err = -EADDRNOTAVAIL;
214 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
215 /* Don't allow unregistering devices! */
216 if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
217 ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
220 if (test_bit(addr >> 2, pnd->addrs)) {
230 /* automatically configure a Phonet device, if supported */
231 static int phonet_device_autoconf(struct net_device *dev)
233 struct if_phonet_req req;
236 if (!dev->netdev_ops->ndo_do_ioctl)
239 ret = dev->netdev_ops->ndo_do_ioctl(dev, (struct ifreq *)&req,
245 ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device);
248 phonet_address_notify(RTM_NEWADDR, dev,
249 req.ifr_phonet_autoconf.device);
253 static void phonet_route_autodel(struct net_device *dev)
255 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
257 DECLARE_BITMAP(deleted, 64);
259 /* Remove left-over Phonet routes */
260 bitmap_zero(deleted, 64);
261 mutex_lock(&pnn->routes.lock);
262 for (i = 0; i < 64; i++)
263 if (rcu_access_pointer(pnn->routes.table[i]) == dev) {
264 RCU_INIT_POINTER(pnn->routes.table[i], NULL);
267 mutex_unlock(&pnn->routes.lock);
269 if (bitmap_empty(deleted, 64))
270 return; /* short-circuit RCU */
272 for_each_set_bit(i, deleted, 64) {
273 rtm_phonet_notify(RTM_DELROUTE, dev, i);
278 /* notify Phonet of device events */
279 static int phonet_device_notify(struct notifier_block *me, unsigned long what,
282 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
285 case NETDEV_REGISTER:
286 if (dev->type == ARPHRD_PHONET)
287 phonet_device_autoconf(dev);
289 case NETDEV_UNREGISTER:
290 phonet_device_destroy(dev);
291 phonet_route_autodel(dev);
298 static struct notifier_block phonet_device_notifier = {
299 .notifier_call = phonet_device_notify,
303 /* Per-namespace Phonet devices handling */
304 static int __net_init phonet_init_net(struct net *net)
306 struct phonet_net *pnn = phonet_pernet(net);
308 if (!proc_create_net("phonet", 0, net->proc_net, &pn_sock_seq_ops,
309 sizeof(struct seq_net_private)))
312 INIT_LIST_HEAD(&pnn->pndevs.list);
313 mutex_init(&pnn->pndevs.lock);
314 mutex_init(&pnn->routes.lock);
318 static void __net_exit phonet_exit_net(struct net *net)
320 struct phonet_net *pnn = phonet_pernet(net);
322 remove_proc_entry("phonet", net->proc_net);
323 WARN_ON_ONCE(!list_empty(&pnn->pndevs.list));
326 static struct pernet_operations phonet_net_ops = {
327 .init = phonet_init_net,
328 .exit = phonet_exit_net,
329 .id = &phonet_net_id,
330 .size = sizeof(struct phonet_net),
333 /* Initialize Phonet devices list */
334 int __init phonet_device_init(void)
336 int err = register_pernet_subsys(&phonet_net_ops);
340 proc_create_net("pnresource", 0, init_net.proc_net, &pn_res_seq_ops,
341 sizeof(struct seq_net_private));
342 register_netdevice_notifier(&phonet_device_notifier);
343 err = phonet_netlink_register();
345 phonet_device_exit();
349 void phonet_device_exit(void)
351 rtnl_unregister_all(PF_PHONET);
352 unregister_netdevice_notifier(&phonet_device_notifier);
353 unregister_pernet_subsys(&phonet_net_ops);
354 remove_proc_entry("pnresource", init_net.proc_net);
357 int phonet_route_add(struct net_device *dev, u8 daddr)
359 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
360 struct phonet_routes *routes = &pnn->routes;
364 mutex_lock(&routes->lock);
365 if (routes->table[daddr] == NULL) {
366 rcu_assign_pointer(routes->table[daddr], dev);
370 mutex_unlock(&routes->lock);
374 int phonet_route_del(struct net_device *dev, u8 daddr)
376 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
377 struct phonet_routes *routes = &pnn->routes;
380 mutex_lock(&routes->lock);
381 if (rcu_access_pointer(routes->table[daddr]) == dev)
382 RCU_INIT_POINTER(routes->table[daddr], NULL);
385 mutex_unlock(&routes->lock);
394 struct net_device *phonet_route_get_rcu(struct net *net, u8 daddr)
396 struct phonet_net *pnn = phonet_pernet(net);
397 struct phonet_routes *routes = &pnn->routes;
398 struct net_device *dev;
401 dev = rcu_dereference(routes->table[daddr]);
405 struct net_device *phonet_route_output(struct net *net, u8 daddr)
407 struct phonet_net *pnn = phonet_pernet(net);
408 struct phonet_routes *routes = &pnn->routes;
409 struct net_device *dev;
413 dev = rcu_dereference(routes->table[daddr]);
419 dev = phonet_device_get(net); /* Default route */