4 * Phonet network device
6 * Copyright (C) 2008 Nokia Corporation.
8 * Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
9 * Original author: Sakari Ailus <sakari.ailus@nokia.com>
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.
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.
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
26 #include <linux/kernel.h>
27 #include <linux/net.h>
28 #include <linux/slab.h>
29 #include <linux/netdevice.h>
30 #include <linux/phonet.h>
31 #include <linux/proc_fs.h>
32 #include <linux/if_arp.h>
34 #include <net/netns/generic.h>
35 #include <net/phonet/pn_dev.h>
37 struct phonet_routes {
39 struct net_device *table[64];
43 struct phonet_device_list pndevs;
44 struct phonet_routes routes;
47 int phonet_net_id __read_mostly;
49 static struct phonet_net *phonet_pernet(struct net *net)
53 return net_generic(net, phonet_net_id);
56 struct phonet_device_list *phonet_device_list(struct net *net)
58 struct phonet_net *pnn = phonet_pernet(net);
62 /* Allocate new Phonet device. */
63 static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
65 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
66 struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
70 bitmap_zero(pnd->addrs, 64);
72 BUG_ON(!mutex_is_locked(&pndevs->lock));
73 list_add_rcu(&pnd->list, &pndevs->list);
77 static struct phonet_device *__phonet_get(struct net_device *dev)
79 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
80 struct phonet_device *pnd;
82 BUG_ON(!mutex_is_locked(&pndevs->lock));
83 list_for_each_entry(pnd, &pndevs->list, list) {
84 if (pnd->netdev == dev)
90 static struct phonet_device *__phonet_get_rcu(struct net_device *dev)
92 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
93 struct phonet_device *pnd;
95 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
96 if (pnd->netdev == dev)
102 static void phonet_device_destroy(struct net_device *dev)
104 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
105 struct phonet_device *pnd;
109 mutex_lock(&pndevs->lock);
110 pnd = __phonet_get(dev);
112 list_del_rcu(&pnd->list);
113 mutex_unlock(&pndevs->lock);
118 for_each_set_bit(addr, pnd->addrs, 64)
119 phonet_address_notify(RTM_DELADDR, dev, addr);
124 struct net_device *phonet_device_get(struct net *net)
126 struct phonet_device_list *pndevs = phonet_device_list(net);
127 struct phonet_device *pnd;
128 struct net_device *dev = NULL;
131 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
135 if ((dev->reg_state == NETREG_REGISTERED) &&
136 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
146 int phonet_address_add(struct net_device *dev, u8 addr)
148 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
149 struct phonet_device *pnd;
152 mutex_lock(&pndevs->lock);
153 /* Find or create Phonet-specific device data */
154 pnd = __phonet_get(dev);
156 pnd = __phonet_device_alloc(dev);
157 if (unlikely(pnd == NULL))
159 else if (test_and_set_bit(addr >> 2, pnd->addrs))
161 mutex_unlock(&pndevs->lock);
165 int phonet_address_del(struct net_device *dev, u8 addr)
167 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
168 struct phonet_device *pnd;
171 mutex_lock(&pndevs->lock);
172 pnd = __phonet_get(dev);
173 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) {
174 err = -EADDRNOTAVAIL;
176 } else if (bitmap_empty(pnd->addrs, 64))
177 list_del_rcu(&pnd->list);
180 mutex_unlock(&pndevs->lock);
189 /* Gets a source address toward a destination, through a interface. */
190 u8 phonet_address_get(struct net_device *dev, u8 daddr)
192 struct phonet_device *pnd;
196 pnd = __phonet_get_rcu(dev);
198 BUG_ON(bitmap_empty(pnd->addrs, 64));
200 /* Use same source address as destination, if possible */
201 if (test_bit(daddr >> 2, pnd->addrs))
204 saddr = find_first_bit(pnd->addrs, 64) << 2;
209 if (saddr == PN_NO_ADDR) {
210 /* Fallback to another device */
211 struct net_device *def_dev;
213 def_dev = phonet_device_get(dev_net(dev));
216 saddr = phonet_address_get(def_dev, daddr);
223 int phonet_address_lookup(struct net *net, u8 addr)
225 struct phonet_device_list *pndevs = phonet_device_list(net);
226 struct phonet_device *pnd;
227 int err = -EADDRNOTAVAIL;
230 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
231 /* Don't allow unregistering devices! */
232 if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
233 ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
236 if (test_bit(addr >> 2, pnd->addrs)) {
246 /* automatically configure a Phonet device, if supported */
247 static int phonet_device_autoconf(struct net_device *dev)
249 struct if_phonet_req req;
252 if (!dev->netdev_ops->ndo_do_ioctl)
255 ret = dev->netdev_ops->ndo_do_ioctl(dev, (struct ifreq *)&req,
261 ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device);
264 phonet_address_notify(RTM_NEWADDR, dev,
265 req.ifr_phonet_autoconf.device);
269 static void phonet_route_autodel(struct net_device *dev)
271 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
273 DECLARE_BITMAP(deleted, 64);
275 /* Remove left-over Phonet routes */
276 bitmap_zero(deleted, 64);
277 mutex_lock(&pnn->routes.lock);
278 for (i = 0; i < 64; i++)
279 if (dev == pnn->routes.table[i]) {
280 rcu_assign_pointer(pnn->routes.table[i], NULL);
283 mutex_unlock(&pnn->routes.lock);
285 if (bitmap_empty(deleted, 64))
286 return; /* short-circuit RCU */
288 for (i = find_first_bit(deleted, 64); i < 64;
289 i = find_next_bit(deleted, 64, i + 1)) {
290 rtm_phonet_notify(RTM_DELROUTE, dev, i);
295 /* notify Phonet of device events */
296 static int phonet_device_notify(struct notifier_block *me, unsigned long what,
299 struct net_device *dev = arg;
302 case NETDEV_REGISTER:
303 if (dev->type == ARPHRD_PHONET)
304 phonet_device_autoconf(dev);
306 case NETDEV_UNREGISTER:
307 phonet_device_destroy(dev);
308 phonet_route_autodel(dev);
315 static struct notifier_block phonet_device_notifier = {
316 .notifier_call = phonet_device_notify,
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_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops))
328 INIT_LIST_HEAD(&pnn->pndevs.list);
329 mutex_init(&pnn->pndevs.lock);
330 mutex_init(&pnn->routes.lock);
334 static void __net_exit phonet_exit_net(struct net *net)
336 struct phonet_net *pnn = phonet_pernet(net);
337 struct net_device *dev;
341 for_each_netdev(net, dev)
342 phonet_device_destroy(dev);
344 for (i = 0; i < 64; i++) {
345 dev = pnn->routes.table[i];
347 rtm_phonet_notify(RTM_DELROUTE, dev, i);
353 proc_net_remove(net, "phonet");
356 static struct pernet_operations phonet_net_ops = {
357 .init = phonet_init_net,
358 .exit = phonet_exit_net,
359 .id = &phonet_net_id,
360 .size = sizeof(struct phonet_net),
363 /* Initialize Phonet devices list */
364 int __init phonet_device_init(void)
366 int err = register_pernet_device(&phonet_net_ops);
370 register_netdevice_notifier(&phonet_device_notifier);
371 err = phonet_netlink_register();
373 phonet_device_exit();
377 void phonet_device_exit(void)
379 rtnl_unregister_all(PF_PHONET);
380 unregister_netdevice_notifier(&phonet_device_notifier);
381 unregister_pernet_device(&phonet_net_ops);
384 int phonet_route_add(struct net_device *dev, u8 daddr)
386 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
387 struct phonet_routes *routes = &pnn->routes;
391 mutex_lock(&routes->lock);
392 if (routes->table[daddr] == NULL) {
393 rcu_assign_pointer(routes->table[daddr], dev);
397 mutex_unlock(&routes->lock);
401 int phonet_route_del(struct net_device *dev, u8 daddr)
403 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
404 struct phonet_routes *routes = &pnn->routes;
407 mutex_lock(&routes->lock);
408 if (dev == routes->table[daddr])
409 rcu_assign_pointer(routes->table[daddr], NULL);
412 mutex_unlock(&routes->lock);
421 struct net_device *phonet_route_get(struct net *net, u8 daddr)
423 struct phonet_net *pnn = phonet_pernet(net);
424 struct phonet_routes *routes = &pnn->routes;
425 struct net_device *dev;
427 ASSERT_RTNL(); /* no need to hold the device */
431 dev = rcu_dereference(routes->table[daddr]);
436 struct net_device *phonet_route_output(struct net *net, u8 daddr)
438 struct phonet_net *pnn = phonet_pernet(net);
439 struct phonet_routes *routes = &pnn->routes;
440 struct net_device *dev;
444 dev = rcu_dereference(routes->table[daddr]);
450 dev = phonet_device_get(net); /* Default route */