netns: net_identifiers should be read_mostly
[linux-2.6.git] / net / phonet / pn_dev.c
1 /*
2  * File: pn_dev.c
3  *
4  * Phonet network device
5  *
6  * Copyright (C) 2008 Nokia Corporation.
7  *
8  * Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
9  * Original author: Sakari Ailus <sakari.ailus@nokia.com>
10  *
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.
14  *
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.
19  *
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
23  * 02110-1301 USA
24  */
25
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>
32 #include <net/sock.h>
33 #include <net/netns/generic.h>
34 #include <net/phonet/pn_dev.h>
35
36 struct phonet_routes {
37         struct mutex            lock;
38         struct net_device       *table[64];
39 };
40
41 struct phonet_net {
42         struct phonet_device_list pndevs;
43         struct phonet_routes routes;
44 };
45
46 int phonet_net_id __read_mostly;
47
48 struct phonet_device_list *phonet_device_list(struct net *net)
49 {
50         struct phonet_net *pnn = net_generic(net, phonet_net_id);
51         return &pnn->pndevs;
52 }
53
54 /* Allocate new Phonet device. */
55 static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
56 {
57         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
58         struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
59         if (pnd == NULL)
60                 return NULL;
61         pnd->netdev = dev;
62         bitmap_zero(pnd->addrs, 64);
63
64         list_add(&pnd->list, &pndevs->list);
65         return pnd;
66 }
67
68 static struct phonet_device *__phonet_get(struct net_device *dev)
69 {
70         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
71         struct phonet_device *pnd;
72
73         list_for_each_entry(pnd, &pndevs->list, list) {
74                 if (pnd->netdev == dev)
75                         return pnd;
76         }
77         return NULL;
78 }
79
80 static void phonet_device_destroy(struct net_device *dev)
81 {
82         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
83         struct phonet_device *pnd;
84
85         ASSERT_RTNL();
86
87         spin_lock_bh(&pndevs->lock);
88         pnd = __phonet_get(dev);
89         if (pnd)
90                 list_del(&pnd->list);
91         spin_unlock_bh(&pndevs->lock);
92
93         if (pnd) {
94                 u8 addr;
95
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);
99                 kfree(pnd);
100         }
101 }
102
103 struct net_device *phonet_device_get(struct net *net)
104 {
105         struct phonet_device_list *pndevs = phonet_device_list(net);
106         struct phonet_device *pnd;
107         struct net_device *dev = NULL;
108
109         spin_lock_bh(&pndevs->lock);
110         list_for_each_entry(pnd, &pndevs->list, list) {
111                 dev = pnd->netdev;
112                 BUG_ON(!dev);
113
114                 if ((dev->reg_state == NETREG_REGISTERED) &&
115                         ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
116                         break;
117                 dev = NULL;
118         }
119         if (dev)
120                 dev_hold(dev);
121         spin_unlock_bh(&pndevs->lock);
122         return dev;
123 }
124
125 int phonet_address_add(struct net_device *dev, u8 addr)
126 {
127         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
128         struct phonet_device *pnd;
129         int err = 0;
130
131         spin_lock_bh(&pndevs->lock);
132         /* Find or create Phonet-specific device data */
133         pnd = __phonet_get(dev);
134         if (pnd == NULL)
135                 pnd = __phonet_device_alloc(dev);
136         if (unlikely(pnd == NULL))
137                 err = -ENOMEM;
138         else if (test_and_set_bit(addr >> 2, pnd->addrs))
139                 err = -EEXIST;
140         spin_unlock_bh(&pndevs->lock);
141         return err;
142 }
143
144 int phonet_address_del(struct net_device *dev, u8 addr)
145 {
146         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
147         struct phonet_device *pnd;
148         int err = 0;
149
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);
156                 kfree(pnd);
157         }
158         spin_unlock_bh(&pndevs->lock);
159         return err;
160 }
161
162 /* Gets a source address toward a destination, through a interface. */
163 u8 phonet_address_get(struct net_device *dev, u8 daddr)
164 {
165         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
166         struct phonet_device *pnd;
167         u8 saddr;
168
169         spin_lock_bh(&pndevs->lock);
170         pnd = __phonet_get(dev);
171         if (pnd) {
172                 BUG_ON(bitmap_empty(pnd->addrs, 64));
173
174                 /* Use same source address as destination, if possible */
175                 if (test_bit(daddr >> 2, pnd->addrs))
176                         saddr = daddr;
177                 else
178                         saddr = find_first_bit(pnd->addrs, 64) << 2;
179         } else
180                 saddr = PN_NO_ADDR;
181         spin_unlock_bh(&pndevs->lock);
182
183         if (saddr == PN_NO_ADDR) {
184                 /* Fallback to another device */
185                 struct net_device *def_dev;
186
187                 def_dev = phonet_device_get(dev_net(dev));
188                 if (def_dev) {
189                         if (def_dev != dev)
190                                 saddr = phonet_address_get(def_dev, daddr);
191                         dev_put(def_dev);
192                 }
193         }
194         return saddr;
195 }
196
197 int phonet_address_lookup(struct net *net, u8 addr)
198 {
199         struct phonet_device_list *pndevs = phonet_device_list(net);
200         struct phonet_device *pnd;
201         int err = -EADDRNOTAVAIL;
202
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)
208                         continue;
209
210                 if (test_bit(addr >> 2, pnd->addrs)) {
211                         err = 0;
212                         goto found;
213                 }
214         }
215 found:
216         spin_unlock_bh(&pndevs->lock);
217         return err;
218 }
219
220 /* automatically configure a Phonet device, if supported */
221 static int phonet_device_autoconf(struct net_device *dev)
222 {
223         struct if_phonet_req req;
224         int ret;
225
226         if (!dev->netdev_ops->ndo_do_ioctl)
227                 return -EOPNOTSUPP;
228
229         ret = dev->netdev_ops->ndo_do_ioctl(dev, (struct ifreq *)&req,
230                                                 SIOCPNGAUTOCONF);
231         if (ret < 0)
232                 return ret;
233
234         ASSERT_RTNL();
235         ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device);
236         if (ret)
237                 return ret;
238         phonet_address_notify(RTM_NEWADDR, dev,
239                                 req.ifr_phonet_autoconf.device);
240         return 0;
241 }
242
243 static void phonet_route_autodel(struct net_device *dev)
244 {
245         struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
246         unsigned i;
247         DECLARE_BITMAP(deleted, 64);
248
249         /* Remove left-over Phonet routes */
250         bitmap_zero(deleted, 64);
251         mutex_lock(&pnn->routes.lock);
252         for (i = 0; i < 64; i++)
253                 if (dev == pnn->routes.table[i]) {
254                         rcu_assign_pointer(pnn->routes.table[i], NULL);
255                         set_bit(i, deleted);
256                 }
257         mutex_unlock(&pnn->routes.lock);
258
259         if (bitmap_empty(deleted, 64))
260                 return; /* short-circuit RCU */
261         synchronize_rcu();
262         for (i = find_first_bit(deleted, 64); i < 64;
263                         i = find_next_bit(deleted, 64, i + 1)) {
264                 rtm_phonet_notify(RTM_DELROUTE, dev, i);
265                 dev_put(dev);
266         }
267 }
268
269 /* notify Phonet of device events */
270 static int phonet_device_notify(struct notifier_block *me, unsigned long what,
271                                 void *arg)
272 {
273         struct net_device *dev = arg;
274
275         switch (what) {
276         case NETDEV_REGISTER:
277                 if (dev->type == ARPHRD_PHONET)
278                         phonet_device_autoconf(dev);
279                 break;
280         case NETDEV_UNREGISTER:
281                 phonet_device_destroy(dev);
282                 phonet_route_autodel(dev);
283                 break;
284         }
285         return 0;
286
287 }
288
289 static struct notifier_block phonet_device_notifier = {
290         .notifier_call = phonet_device_notify,
291         .priority = 0,
292 };
293
294 /* Per-namespace Phonet devices handling */
295 static int phonet_init_net(struct net *net)
296 {
297         struct phonet_net *pnn = kzalloc(sizeof(*pnn), GFP_KERNEL);
298         if (!pnn)
299                 return -ENOMEM;
300
301         if (!proc_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops)) {
302                 kfree(pnn);
303                 return -ENOMEM;
304         }
305
306         INIT_LIST_HEAD(&pnn->pndevs.list);
307         spin_lock_init(&pnn->pndevs.lock);
308         mutex_init(&pnn->routes.lock);
309         net_assign_generic(net, phonet_net_id, pnn);
310         return 0;
311 }
312
313 static void phonet_exit_net(struct net *net)
314 {
315         struct phonet_net *pnn = net_generic(net, phonet_net_id);
316         struct net_device *dev;
317         unsigned i;
318
319         rtnl_lock();
320         for_each_netdev(net, dev)
321                 phonet_device_destroy(dev);
322
323         for (i = 0; i < 64; i++) {
324                 dev = pnn->routes.table[i];
325                 if (dev) {
326                         rtm_phonet_notify(RTM_DELROUTE, dev, i);
327                         dev_put(dev);
328                 }
329         }
330         rtnl_unlock();
331
332         proc_net_remove(net, "phonet");
333         kfree(pnn);
334 }
335
336 static struct pernet_operations phonet_net_ops = {
337         .init = phonet_init_net,
338         .exit = phonet_exit_net,
339 };
340
341 /* Initialize Phonet devices list */
342 int __init phonet_device_init(void)
343 {
344         int err = register_pernet_gen_device(&phonet_net_id, &phonet_net_ops);
345         if (err)
346                 return err;
347
348         register_netdevice_notifier(&phonet_device_notifier);
349         err = phonet_netlink_register();
350         if (err)
351                 phonet_device_exit();
352         return err;
353 }
354
355 void phonet_device_exit(void)
356 {
357         rtnl_unregister_all(PF_PHONET);
358         unregister_netdevice_notifier(&phonet_device_notifier);
359         unregister_pernet_gen_device(phonet_net_id, &phonet_net_ops);
360 }
361
362 int phonet_route_add(struct net_device *dev, u8 daddr)
363 {
364         struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
365         struct phonet_routes *routes = &pnn->routes;
366         int err = -EEXIST;
367
368         daddr = daddr >> 2;
369         mutex_lock(&routes->lock);
370         if (routes->table[daddr] == NULL) {
371                 rcu_assign_pointer(routes->table[daddr], dev);
372                 dev_hold(dev);
373                 err = 0;
374         }
375         mutex_unlock(&routes->lock);
376         return err;
377 }
378
379 int phonet_route_del(struct net_device *dev, u8 daddr)
380 {
381         struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
382         struct phonet_routes *routes = &pnn->routes;
383
384         daddr = daddr >> 2;
385         mutex_lock(&routes->lock);
386         if (dev == routes->table[daddr])
387                 rcu_assign_pointer(routes->table[daddr], NULL);
388         else
389                 dev = NULL;
390         mutex_unlock(&routes->lock);
391
392         if (!dev)
393                 return -ENOENT;
394         synchronize_rcu();
395         dev_put(dev);
396         return 0;
397 }
398
399 struct net_device *phonet_route_get(struct net *net, u8 daddr)
400 {
401         struct phonet_net *pnn = net_generic(net, phonet_net_id);
402         struct phonet_routes *routes = &pnn->routes;
403         struct net_device *dev;
404
405         ASSERT_RTNL(); /* no need to hold the device */
406
407         daddr >>= 2;
408         rcu_read_lock();
409         dev = rcu_dereference(routes->table[daddr]);
410         rcu_read_unlock();
411         return dev;
412 }
413
414 struct net_device *phonet_route_output(struct net *net, u8 daddr)
415 {
416         struct phonet_net *pnn = net_generic(net, phonet_net_id);
417         struct phonet_routes *routes = &pnn->routes;
418         struct net_device *dev;
419
420         daddr >>= 2;
421         rcu_read_lock();
422         dev = rcu_dereference(routes->table[daddr]);
423         if (dev)
424                 dev_hold(dev);
425         rcu_read_unlock();
426
427         if (!dev)
428                 dev = phonet_device_get(net); /* Default route */
429         return dev;
430 }