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)
40 return net_generic(net, phonet_net_id);
43 struct phonet_device_list *phonet_device_list(struct net *net)
45 struct phonet_net *pnn = phonet_pernet(net);
49 /* Allocate new Phonet device. */
50 static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
52 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
53 struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
57 bitmap_zero(pnd->addrs, 64);
59 BUG_ON(!mutex_is_locked(&pndevs->lock));
60 list_add_rcu(&pnd->list, &pndevs->list);
64 static struct phonet_device *__phonet_get(struct net_device *dev)
66 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
67 struct phonet_device *pnd;
69 BUG_ON(!mutex_is_locked(&pndevs->lock));
70 list_for_each_entry(pnd, &pndevs->list, list) {
71 if (pnd->netdev == dev)
77 static struct phonet_device *__phonet_get_rcu(struct net_device *dev)
79 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
80 struct phonet_device *pnd;
82 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
83 if (pnd->netdev == dev)
89 static void phonet_device_destroy(struct net_device *dev)
91 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
92 struct phonet_device *pnd;
96 mutex_lock(&pndevs->lock);
97 pnd = __phonet_get(dev);
99 list_del_rcu(&pnd->list);
100 mutex_unlock(&pndevs->lock);
105 for_each_set_bit(addr, pnd->addrs, 64)
106 phonet_address_notify(RTM_DELADDR, dev, addr);
111 struct net_device *phonet_device_get(struct net *net)
113 struct phonet_device_list *pndevs = phonet_device_list(net);
114 struct phonet_device *pnd;
115 struct net_device *dev = NULL;
118 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
122 if ((dev->reg_state == NETREG_REGISTERED) &&
123 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
133 int phonet_address_add(struct net_device *dev, u8 addr)
135 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
136 struct phonet_device *pnd;
139 mutex_lock(&pndevs->lock);
140 /* Find or create Phonet-specific device data */
141 pnd = __phonet_get(dev);
143 pnd = __phonet_device_alloc(dev);
144 if (unlikely(pnd == NULL))
146 else if (test_and_set_bit(addr >> 2, pnd->addrs))
148 mutex_unlock(&pndevs->lock);
152 int phonet_address_del(struct net_device *dev, u8 addr)
154 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
155 struct phonet_device *pnd;
158 mutex_lock(&pndevs->lock);
159 pnd = __phonet_get(dev);
160 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) {
161 err = -EADDRNOTAVAIL;
163 } else if (bitmap_empty(pnd->addrs, 64))
164 list_del_rcu(&pnd->list);
167 mutex_unlock(&pndevs->lock);
175 /* Gets a source address toward a destination, through a interface. */
176 u8 phonet_address_get(struct net_device *dev, u8 daddr)
178 struct phonet_device *pnd;
182 pnd = __phonet_get_rcu(dev);
184 BUG_ON(bitmap_empty(pnd->addrs, 64));
186 /* Use same source address as destination, if possible */
187 if (test_bit(daddr >> 2, pnd->addrs))
190 saddr = find_first_bit(pnd->addrs, 64) << 2;
195 if (saddr == PN_NO_ADDR) {
196 /* Fallback to another device */
197 struct net_device *def_dev;
199 def_dev = phonet_device_get(dev_net(dev));
202 saddr = phonet_address_get(def_dev, daddr);
209 int phonet_address_lookup(struct net *net, u8 addr)
211 struct phonet_device_list *pndevs = phonet_device_list(net);
212 struct phonet_device *pnd;
213 int err = -EADDRNOTAVAIL;
216 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
217 /* Don't allow unregistering devices! */
218 if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
219 ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
222 if (test_bit(addr >> 2, pnd->addrs)) {
232 /* automatically configure a Phonet device, if supported */
233 static int phonet_device_autoconf(struct net_device *dev)
235 struct if_phonet_req req;
238 if (!dev->netdev_ops->ndo_do_ioctl)
241 ret = dev->netdev_ops->ndo_do_ioctl(dev, (struct ifreq *)&req,
247 ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device);
250 phonet_address_notify(RTM_NEWADDR, dev,
251 req.ifr_phonet_autoconf.device);
255 static void phonet_route_autodel(struct net_device *dev)
257 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
259 DECLARE_BITMAP(deleted, 64);
261 /* Remove left-over Phonet routes */
262 bitmap_zero(deleted, 64);
263 mutex_lock(&pnn->routes.lock);
264 for (i = 0; i < 64; i++)
265 if (rcu_access_pointer(pnn->routes.table[i]) == dev) {
266 RCU_INIT_POINTER(pnn->routes.table[i], NULL);
269 mutex_unlock(&pnn->routes.lock);
271 if (bitmap_empty(deleted, 64))
272 return; /* short-circuit RCU */
274 for_each_set_bit(i, deleted, 64) {
275 rtm_phonet_notify(RTM_DELROUTE, dev, i);
280 /* notify Phonet of device events */
281 static int phonet_device_notify(struct notifier_block *me, unsigned long what,
284 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
287 case NETDEV_REGISTER:
288 if (dev->type == ARPHRD_PHONET)
289 phonet_device_autoconf(dev);
291 case NETDEV_UNREGISTER:
292 phonet_device_destroy(dev);
293 phonet_route_autodel(dev);
300 static struct notifier_block phonet_device_notifier = {
301 .notifier_call = phonet_device_notify,
305 /* Per-namespace Phonet devices handling */
306 static int __net_init phonet_init_net(struct net *net)
308 struct phonet_net *pnn = phonet_pernet(net);
310 if (!proc_create_net("phonet", 0, net->proc_net, &pn_sock_seq_ops,
311 sizeof(struct seq_net_private)))
314 INIT_LIST_HEAD(&pnn->pndevs.list);
315 mutex_init(&pnn->pndevs.lock);
316 mutex_init(&pnn->routes.lock);
320 static void __net_exit phonet_exit_net(struct net *net)
322 struct phonet_net *pnn = phonet_pernet(net);
324 remove_proc_entry("phonet", net->proc_net);
325 WARN_ON_ONCE(!list_empty(&pnn->pndevs.list));
328 static struct pernet_operations phonet_net_ops = {
329 .init = phonet_init_net,
330 .exit = phonet_exit_net,
331 .id = &phonet_net_id,
332 .size = sizeof(struct phonet_net),
335 /* Initialize Phonet devices list */
336 int __init phonet_device_init(void)
338 int err = register_pernet_subsys(&phonet_net_ops);
342 proc_create_net("pnresource", 0, init_net.proc_net, &pn_res_seq_ops,
343 sizeof(struct seq_net_private));
344 register_netdevice_notifier(&phonet_device_notifier);
345 err = phonet_netlink_register();
347 phonet_device_exit();
351 void phonet_device_exit(void)
353 rtnl_unregister_all(PF_PHONET);
354 unregister_netdevice_notifier(&phonet_device_notifier);
355 unregister_pernet_subsys(&phonet_net_ops);
356 remove_proc_entry("pnresource", init_net.proc_net);
359 int phonet_route_add(struct net_device *dev, u8 daddr)
361 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
362 struct phonet_routes *routes = &pnn->routes;
366 mutex_lock(&routes->lock);
367 if (routes->table[daddr] == NULL) {
368 rcu_assign_pointer(routes->table[daddr], dev);
372 mutex_unlock(&routes->lock);
376 int phonet_route_del(struct net_device *dev, u8 daddr)
378 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
379 struct phonet_routes *routes = &pnn->routes;
382 mutex_lock(&routes->lock);
383 if (rcu_access_pointer(routes->table[daddr]) == dev)
384 RCU_INIT_POINTER(routes->table[daddr], NULL);
387 mutex_unlock(&routes->lock);
396 struct net_device *phonet_route_get_rcu(struct net *net, u8 daddr)
398 struct phonet_net *pnn = phonet_pernet(net);
399 struct phonet_routes *routes = &pnn->routes;
400 struct net_device *dev;
403 dev = rcu_dereference(routes->table[daddr]);
407 struct net_device *phonet_route_output(struct net *net, u8 daddr)
409 struct phonet_net *pnn = phonet_pernet(net);
410 struct phonet_routes *routes = &pnn->routes;
411 struct net_device *dev;
415 dev = rcu_dereference(routes->table[daddr]);
421 dev = phonet_device_get(net); /* Default route */