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/netdevice.h>
29 #include <linux/phonet.h>
30 #include <linux/proc_fs.h>
31 #include <linux/if_arp.h>
33 #include <net/netns/generic.h>
34 #include <net/phonet/pn_dev.h>
36 struct phonet_routes {
38 struct net_device *table[64];
42 struct phonet_device_list pndevs;
43 struct phonet_routes routes;
46 int phonet_net_id __read_mostly;
48 struct phonet_device_list *phonet_device_list(struct net *net)
50 struct phonet_net *pnn = net_generic(net, phonet_net_id);
54 /* Allocate new Phonet device. */
55 static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
57 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
58 struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
62 bitmap_zero(pnd->addrs, 64);
64 BUG_ON(!mutex_is_locked(&pndevs->lock));
65 list_add_rcu(&pnd->list, &pndevs->list);
69 static struct phonet_device *__phonet_get(struct net_device *dev)
71 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
72 struct phonet_device *pnd;
74 BUG_ON(!mutex_is_locked(&pndevs->lock));
75 list_for_each_entry(pnd, &pndevs->list, list) {
76 if (pnd->netdev == dev)
82 static struct phonet_device *__phonet_get_rcu(struct net_device *dev)
84 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
85 struct phonet_device *pnd;
87 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
88 if (pnd->netdev == dev)
94 static void phonet_device_destroy(struct net_device *dev)
96 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
97 struct phonet_device *pnd;
101 mutex_lock(&pndevs->lock);
102 pnd = __phonet_get(dev);
104 list_del_rcu(&pnd->list);
105 mutex_unlock(&pndevs->lock);
110 for_each_set_bit(addr, pnd->addrs, 64)
111 phonet_address_notify(RTM_DELADDR, dev, addr);
116 struct net_device *phonet_device_get(struct net *net)
118 struct phonet_device_list *pndevs = phonet_device_list(net);
119 struct phonet_device *pnd;
120 struct net_device *dev = NULL;
123 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
127 if ((dev->reg_state == NETREG_REGISTERED) &&
128 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
138 int phonet_address_add(struct net_device *dev, u8 addr)
140 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
141 struct phonet_device *pnd;
144 mutex_lock(&pndevs->lock);
145 /* Find or create Phonet-specific device data */
146 pnd = __phonet_get(dev);
148 pnd = __phonet_device_alloc(dev);
149 if (unlikely(pnd == NULL))
151 else if (test_and_set_bit(addr >> 2, pnd->addrs))
153 mutex_unlock(&pndevs->lock);
157 int phonet_address_del(struct net_device *dev, u8 addr)
159 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
160 struct phonet_device *pnd;
163 mutex_lock(&pndevs->lock);
164 pnd = __phonet_get(dev);
165 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) {
166 err = -EADDRNOTAVAIL;
168 } else if (bitmap_empty(pnd->addrs, 64))
169 list_del_rcu(&pnd->list);
172 mutex_unlock(&pndevs->lock);
181 /* Gets a source address toward a destination, through a interface. */
182 u8 phonet_address_get(struct net_device *dev, u8 daddr)
184 struct phonet_device *pnd;
188 pnd = __phonet_get_rcu(dev);
190 BUG_ON(bitmap_empty(pnd->addrs, 64));
192 /* Use same source address as destination, if possible */
193 if (test_bit(daddr >> 2, pnd->addrs))
196 saddr = find_first_bit(pnd->addrs, 64) << 2;
201 if (saddr == PN_NO_ADDR) {
202 /* Fallback to another device */
203 struct net_device *def_dev;
205 def_dev = phonet_device_get(dev_net(dev));
208 saddr = phonet_address_get(def_dev, daddr);
215 int phonet_address_lookup(struct net *net, u8 addr)
217 struct phonet_device_list *pndevs = phonet_device_list(net);
218 struct phonet_device *pnd;
219 int err = -EADDRNOTAVAIL;
222 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
223 /* Don't allow unregistering devices! */
224 if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
225 ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
228 if (test_bit(addr >> 2, pnd->addrs)) {
238 /* automatically configure a Phonet device, if supported */
239 static int phonet_device_autoconf(struct net_device *dev)
241 struct if_phonet_req req;
244 if (!dev->netdev_ops->ndo_do_ioctl)
247 ret = dev->netdev_ops->ndo_do_ioctl(dev, (struct ifreq *)&req,
253 ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device);
256 phonet_address_notify(RTM_NEWADDR, dev,
257 req.ifr_phonet_autoconf.device);
261 static void phonet_route_autodel(struct net_device *dev)
263 struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
265 DECLARE_BITMAP(deleted, 64);
267 /* Remove left-over Phonet routes */
268 bitmap_zero(deleted, 64);
269 mutex_lock(&pnn->routes.lock);
270 for (i = 0; i < 64; i++)
271 if (dev == pnn->routes.table[i]) {
272 rcu_assign_pointer(pnn->routes.table[i], NULL);
275 mutex_unlock(&pnn->routes.lock);
277 if (bitmap_empty(deleted, 64))
278 return; /* short-circuit RCU */
280 for (i = find_first_bit(deleted, 64); i < 64;
281 i = find_next_bit(deleted, 64, i + 1)) {
282 rtm_phonet_notify(RTM_DELROUTE, dev, i);
287 /* notify Phonet of device events */
288 static int phonet_device_notify(struct notifier_block *me, unsigned long what,
291 struct net_device *dev = arg;
294 case NETDEV_REGISTER:
295 if (dev->type == ARPHRD_PHONET)
296 phonet_device_autoconf(dev);
298 case NETDEV_UNREGISTER:
299 phonet_device_destroy(dev);
300 phonet_route_autodel(dev);
307 static struct notifier_block phonet_device_notifier = {
308 .notifier_call = phonet_device_notify,
312 /* Per-namespace Phonet devices handling */
313 static int __net_init phonet_init_net(struct net *net)
315 struct phonet_net *pnn = net_generic(net, phonet_net_id);
317 if (!proc_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops))
320 INIT_LIST_HEAD(&pnn->pndevs.list);
321 mutex_init(&pnn->pndevs.lock);
322 mutex_init(&pnn->routes.lock);
326 static void __net_exit phonet_exit_net(struct net *net)
328 struct phonet_net *pnn = net_generic(net, phonet_net_id);
329 struct net_device *dev;
333 for_each_netdev(net, dev)
334 phonet_device_destroy(dev);
336 for (i = 0; i < 64; i++) {
337 dev = pnn->routes.table[i];
339 rtm_phonet_notify(RTM_DELROUTE, dev, i);
345 proc_net_remove(net, "phonet");
348 static struct pernet_operations phonet_net_ops = {
349 .init = phonet_init_net,
350 .exit = phonet_exit_net,
351 .id = &phonet_net_id,
352 .size = sizeof(struct phonet_net),
355 /* Initialize Phonet devices list */
356 int __init phonet_device_init(void)
358 int err = register_pernet_device(&phonet_net_ops);
362 register_netdevice_notifier(&phonet_device_notifier);
363 err = phonet_netlink_register();
365 phonet_device_exit();
369 void phonet_device_exit(void)
371 rtnl_unregister_all(PF_PHONET);
372 unregister_netdevice_notifier(&phonet_device_notifier);
373 unregister_pernet_device(&phonet_net_ops);
376 int phonet_route_add(struct net_device *dev, u8 daddr)
378 struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
379 struct phonet_routes *routes = &pnn->routes;
383 mutex_lock(&routes->lock);
384 if (routes->table[daddr] == NULL) {
385 rcu_assign_pointer(routes->table[daddr], dev);
389 mutex_unlock(&routes->lock);
393 int phonet_route_del(struct net_device *dev, u8 daddr)
395 struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
396 struct phonet_routes *routes = &pnn->routes;
399 mutex_lock(&routes->lock);
400 if (dev == routes->table[daddr])
401 rcu_assign_pointer(routes->table[daddr], NULL);
404 mutex_unlock(&routes->lock);
413 struct net_device *phonet_route_get(struct net *net, u8 daddr)
415 struct phonet_net *pnn = net_generic(net, phonet_net_id);
416 struct phonet_routes *routes = &pnn->routes;
417 struct net_device *dev;
419 ASSERT_RTNL(); /* no need to hold the device */
423 dev = rcu_dereference(routes->table[daddr]);
428 struct net_device *phonet_route_output(struct net *net, u8 daddr)
430 struct phonet_net *pnn = net_generic(net, phonet_net_id);
431 struct phonet_routes *routes = &pnn->routes;
432 struct net_device *dev;
436 dev = rcu_dereference(routes->table[daddr]);
442 dev = phonet_device_get(net); /* Default route */