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;
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 list_add(&pnd->list, &pndevs->list);
68 static struct phonet_device *__phonet_get(struct net_device *dev)
70 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
71 struct phonet_device *pnd;
73 list_for_each_entry(pnd, &pndevs->list, list) {
74 if (pnd->netdev == dev)
80 static void phonet_device_destroy(struct net_device *dev)
82 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
83 struct phonet_device *pnd;
87 spin_lock_bh(&pndevs->lock);
88 pnd = __phonet_get(dev);
91 spin_unlock_bh(&pndevs->lock);
96 for (addr = find_first_bit(pnd->addrs, 64); addr < 64;
97 addr = find_next_bit(pnd->addrs, 64, 1+addr))
98 phonet_address_notify(RTM_DELADDR, dev, addr);
103 struct net_device *phonet_device_get(struct net *net)
105 struct phonet_device_list *pndevs = phonet_device_list(net);
106 struct phonet_device *pnd;
107 struct net_device *dev = NULL;
109 spin_lock_bh(&pndevs->lock);
110 list_for_each_entry(pnd, &pndevs->list, list) {
114 if ((dev->reg_state == NETREG_REGISTERED) &&
115 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
121 spin_unlock_bh(&pndevs->lock);
125 int phonet_address_add(struct net_device *dev, u8 addr)
127 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
128 struct phonet_device *pnd;
131 spin_lock_bh(&pndevs->lock);
132 /* Find or create Phonet-specific device data */
133 pnd = __phonet_get(dev);
135 pnd = __phonet_device_alloc(dev);
136 if (unlikely(pnd == NULL))
138 else if (test_and_set_bit(addr >> 2, pnd->addrs))
140 spin_unlock_bh(&pndevs->lock);
144 int phonet_address_del(struct net_device *dev, u8 addr)
146 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
147 struct phonet_device *pnd;
150 spin_lock_bh(&pndevs->lock);
151 pnd = __phonet_get(dev);
152 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs))
153 err = -EADDRNOTAVAIL;
154 else if (bitmap_empty(pnd->addrs, 64)) {
155 list_del(&pnd->list);
158 spin_unlock_bh(&pndevs->lock);
162 /* Gets a source address toward a destination, through a interface. */
163 u8 phonet_address_get(struct net_device *dev, u8 daddr)
165 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
166 struct phonet_device *pnd;
169 spin_lock_bh(&pndevs->lock);
170 pnd = __phonet_get(dev);
172 BUG_ON(bitmap_empty(pnd->addrs, 64));
174 /* Use same source address as destination, if possible */
175 if (test_bit(daddr >> 2, pnd->addrs))
178 saddr = find_first_bit(pnd->addrs, 64) << 2;
181 spin_unlock_bh(&pndevs->lock);
183 if (saddr == PN_NO_ADDR) {
184 /* Fallback to another device */
185 struct net_device *def_dev;
187 def_dev = phonet_device_get(dev_net(dev));
190 saddr = phonet_address_get(def_dev, daddr);
197 int phonet_address_lookup(struct net *net, u8 addr)
199 struct phonet_device_list *pndevs = phonet_device_list(net);
200 struct phonet_device *pnd;
201 int err = -EADDRNOTAVAIL;
203 spin_lock_bh(&pndevs->lock);
204 list_for_each_entry(pnd, &pndevs->list, list) {
205 /* Don't allow unregistering devices! */
206 if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
207 ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
210 if (test_bit(addr >> 2, pnd->addrs)) {
216 spin_unlock_bh(&pndevs->lock);
220 /* automatically configure a Phonet device, if supported */
221 static int phonet_device_autoconf(struct net_device *dev)
223 struct if_phonet_req req;
226 if (!dev->netdev_ops->ndo_do_ioctl)
229 ret = dev->netdev_ops->ndo_do_ioctl(dev, (struct ifreq *)&req,
235 ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device);
238 phonet_address_notify(RTM_NEWADDR, dev,
239 req.ifr_phonet_autoconf.device);
243 /* notify Phonet of device events */
244 static int phonet_device_notify(struct notifier_block *me, unsigned long what,
247 struct net_device *dev = arg;
250 case NETDEV_REGISTER:
251 if (dev->type == ARPHRD_PHONET)
252 phonet_device_autoconf(dev);
254 case NETDEV_UNREGISTER:
255 phonet_device_destroy(dev);
262 static struct notifier_block phonet_device_notifier = {
263 .notifier_call = phonet_device_notify,
267 /* Per-namespace Phonet devices handling */
268 static int phonet_init_net(struct net *net)
270 struct phonet_net *pnn = kzalloc(sizeof(*pnn), GFP_KERNEL);
274 if (!proc_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops)) {
279 INIT_LIST_HEAD(&pnn->pndevs.list);
280 spin_lock_init(&pnn->pndevs.lock);
281 spin_lock_init(&pnn->routes.lock);
282 net_assign_generic(net, phonet_net_id, pnn);
286 static void phonet_exit_net(struct net *net)
288 struct phonet_net *pnn = net_generic(net, phonet_net_id);
289 struct net_device *dev;
292 for_each_netdev(net, dev)
293 phonet_device_destroy(dev);
296 proc_net_remove(net, "phonet");
300 static struct pernet_operations phonet_net_ops = {
301 .init = phonet_init_net,
302 .exit = phonet_exit_net,
305 /* Initialize Phonet devices list */
306 int __init phonet_device_init(void)
308 int err = register_pernet_gen_device(&phonet_net_id, &phonet_net_ops);
312 register_netdevice_notifier(&phonet_device_notifier);
313 err = phonet_netlink_register();
315 phonet_device_exit();
319 void phonet_device_exit(void)
321 rtnl_unregister_all(PF_PHONET);
322 unregister_netdevice_notifier(&phonet_device_notifier);
323 unregister_pernet_gen_device(phonet_net_id, &phonet_net_ops);
326 int phonet_route_add(struct net_device *dev, u8 daddr)
328 struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
329 struct phonet_routes *routes = &pnn->routes;
333 spin_lock_bh(&routes->lock);
334 if (routes->table[daddr] == NULL) {
335 routes->table[daddr] = dev;
339 spin_unlock_bh(&routes->lock);
343 int phonet_route_del(struct net_device *dev, u8 daddr)
345 struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
346 struct phonet_routes *routes = &pnn->routes;
350 spin_lock_bh(&routes->lock);
351 if (dev == routes->table[daddr]) {
352 routes->table[daddr] = NULL;
356 spin_unlock_bh(&routes->lock);
360 struct net_device *phonet_route_get(struct net *net, u8 daddr)
362 struct phonet_net *pnn = net_generic(net, phonet_net_id);
363 struct phonet_routes *routes = &pnn->routes;
364 struct net_device *dev;
366 ASSERT_RTNL(); /* no need to hold the device */
369 spin_lock_bh(&routes->lock);
370 dev = routes->table[daddr];
371 spin_unlock_bh(&routes->lock);
375 struct net_device *phonet_route_output(struct net *net, u8 daddr)
377 struct phonet_net *pnn = net_generic(net, phonet_net_id);
378 struct phonet_routes *routes = &pnn->routes;
379 struct net_device *dev;
381 spin_lock_bh(&routes->lock);
382 dev = routes->table[daddr >> 2];
385 spin_unlock_bh(&routes->lock);
388 dev = phonet_device_get(net); /* Default route */