1 // SPDX-License-Identifier: GPL-2.0-only
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>
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)
130 int phonet_address_add(struct net_device *dev, u8 addr)
132 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
133 struct phonet_device *pnd;
136 mutex_lock(&pndevs->lock);
137 /* Find or create Phonet-specific device data */
138 pnd = __phonet_get(dev);
140 pnd = __phonet_device_alloc(dev);
141 if (unlikely(pnd == NULL))
143 else if (test_and_set_bit(addr >> 2, pnd->addrs))
145 mutex_unlock(&pndevs->lock);
149 int phonet_address_del(struct net_device *dev, u8 addr)
151 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
152 struct phonet_device *pnd;
155 mutex_lock(&pndevs->lock);
156 pnd = __phonet_get(dev);
157 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) {
158 err = -EADDRNOTAVAIL;
160 } else if (bitmap_empty(pnd->addrs, 64))
161 list_del_rcu(&pnd->list);
164 mutex_unlock(&pndevs->lock);
172 /* Gets a source address toward a destination, through a interface. */
173 u8 phonet_address_get(struct net_device *dev, u8 daddr)
175 struct phonet_device *pnd;
179 pnd = __phonet_get_rcu(dev);
181 BUG_ON(bitmap_empty(pnd->addrs, 64));
183 /* Use same source address as destination, if possible */
184 if (test_bit(daddr >> 2, pnd->addrs))
187 saddr = find_first_bit(pnd->addrs, 64) << 2;
192 if (saddr == PN_NO_ADDR) {
193 /* Fallback to another device */
194 struct net_device *def_dev;
196 def_dev = phonet_device_get(dev_net(dev));
199 saddr = phonet_address_get(def_dev, daddr);
206 int phonet_address_lookup(struct net *net, u8 addr)
208 struct phonet_device_list *pndevs = phonet_device_list(net);
209 struct phonet_device *pnd;
210 int err = -EADDRNOTAVAIL;
213 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
214 /* Don't allow unregistering devices! */
215 if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
216 ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
219 if (test_bit(addr >> 2, pnd->addrs)) {
229 /* automatically configure a Phonet device, if supported */
230 static int phonet_device_autoconf(struct net_device *dev)
232 struct if_phonet_req req;
235 if (!dev->netdev_ops->ndo_siocdevprivate)
238 ret = dev->netdev_ops->ndo_siocdevprivate(dev, (struct ifreq *)&req,
239 NULL, SIOCPNGAUTOCONF);
244 ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device);
247 phonet_address_notify(RTM_NEWADDR, dev,
248 req.ifr_phonet_autoconf.device);
252 static void phonet_route_autodel(struct net_device *dev)
254 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
256 DECLARE_BITMAP(deleted, 64);
258 /* Remove left-over Phonet routes */
259 bitmap_zero(deleted, 64);
260 mutex_lock(&pnn->routes.lock);
261 for (i = 0; i < 64; i++)
262 if (rcu_access_pointer(pnn->routes.table[i]) == dev) {
263 RCU_INIT_POINTER(pnn->routes.table[i], NULL);
266 mutex_unlock(&pnn->routes.lock);
268 if (bitmap_empty(deleted, 64))
269 return; /* short-circuit RCU */
271 for_each_set_bit(i, deleted, 64) {
272 rtm_phonet_notify(RTM_DELROUTE, dev, i);
277 /* notify Phonet of device events */
278 static int phonet_device_notify(struct notifier_block *me, unsigned long what,
281 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
284 case NETDEV_REGISTER:
285 if (dev->type == ARPHRD_PHONET)
286 phonet_device_autoconf(dev);
288 case NETDEV_UNREGISTER:
289 phonet_device_destroy(dev);
290 phonet_route_autodel(dev);
297 static struct notifier_block phonet_device_notifier = {
298 .notifier_call = phonet_device_notify,
302 /* Per-namespace Phonet devices handling */
303 static int __net_init phonet_init_net(struct net *net)
305 struct phonet_net *pnn = phonet_pernet(net);
307 if (!proc_create_net("phonet", 0, net->proc_net, &pn_sock_seq_ops,
308 sizeof(struct seq_net_private)))
311 INIT_LIST_HEAD(&pnn->pndevs.list);
312 mutex_init(&pnn->pndevs.lock);
313 mutex_init(&pnn->routes.lock);
317 static void __net_exit phonet_exit_net(struct net *net)
319 struct phonet_net *pnn = phonet_pernet(net);
321 remove_proc_entry("phonet", net->proc_net);
322 WARN_ON_ONCE(!list_empty(&pnn->pndevs.list));
325 static struct pernet_operations phonet_net_ops = {
326 .init = phonet_init_net,
327 .exit = phonet_exit_net,
328 .id = &phonet_net_id,
329 .size = sizeof(struct phonet_net),
332 /* Initialize Phonet devices list */
333 int __init phonet_device_init(void)
335 int err = register_pernet_subsys(&phonet_net_ops);
339 proc_create_net("pnresource", 0, init_net.proc_net, &pn_res_seq_ops,
340 sizeof(struct seq_net_private));
341 register_netdevice_notifier(&phonet_device_notifier);
342 err = phonet_netlink_register();
344 phonet_device_exit();
348 void phonet_device_exit(void)
350 rtnl_unregister_all(PF_PHONET);
351 unregister_netdevice_notifier(&phonet_device_notifier);
352 unregister_pernet_subsys(&phonet_net_ops);
353 remove_proc_entry("pnresource", init_net.proc_net);
356 int phonet_route_add(struct net_device *dev, u8 daddr)
358 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
359 struct phonet_routes *routes = &pnn->routes;
363 mutex_lock(&routes->lock);
364 if (routes->table[daddr] == NULL) {
365 rcu_assign_pointer(routes->table[daddr], dev);
369 mutex_unlock(&routes->lock);
373 int phonet_route_del(struct net_device *dev, u8 daddr)
375 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
376 struct phonet_routes *routes = &pnn->routes;
379 mutex_lock(&routes->lock);
380 if (rcu_access_pointer(routes->table[daddr]) == dev)
381 RCU_INIT_POINTER(routes->table[daddr], NULL);
384 mutex_unlock(&routes->lock);
393 struct net_device *phonet_route_get_rcu(struct net *net, u8 daddr)
395 struct phonet_net *pnn = phonet_pernet(net);
396 struct phonet_routes *routes = &pnn->routes;
397 struct net_device *dev;
400 dev = rcu_dereference(routes->table[daddr]);
404 struct net_device *phonet_route_output(struct net *net, u8 daddr)
406 struct phonet_net *pnn = phonet_pernet(net);
407 struct phonet_routes *routes = &pnn->routes;
408 struct net_device *dev;
412 dev = rcu_dereference(routes->table[daddr]);
417 dev = phonet_device_get(net); /* Default route */