]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/phonet/pn_dev.c
Merge branch 'mx51' into mxc-rc
[mv-sheeva.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         BUG_ON(!mutex_is_locked(&pndevs->lock));
65         list_add_rcu(&pnd->list, &pndevs->list);
66         return pnd;
67 }
68
69 static struct phonet_device *__phonet_get(struct net_device *dev)
70 {
71         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
72         struct phonet_device *pnd;
73
74         BUG_ON(!mutex_is_locked(&pndevs->lock));
75         list_for_each_entry(pnd, &pndevs->list, list) {
76                 if (pnd->netdev == dev)
77                         return pnd;
78         }
79         return NULL;
80 }
81
82 static struct phonet_device *__phonet_get_rcu(struct net_device *dev)
83 {
84         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
85         struct phonet_device *pnd;
86
87         list_for_each_entry_rcu(pnd, &pndevs->list, list) {
88                 if (pnd->netdev == dev)
89                         return pnd;
90         }
91         return NULL;
92 }
93
94 static void phonet_device_destroy(struct net_device *dev)
95 {
96         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
97         struct phonet_device *pnd;
98
99         ASSERT_RTNL();
100
101         mutex_lock(&pndevs->lock);
102         pnd = __phonet_get(dev);
103         if (pnd)
104                 list_del_rcu(&pnd->list);
105         mutex_unlock(&pndevs->lock);
106
107         if (pnd) {
108                 u8 addr;
109
110                 for_each_set_bit(addr, pnd->addrs, 64)
111                         phonet_address_notify(RTM_DELADDR, dev, addr);
112                 kfree(pnd);
113         }
114 }
115
116 struct net_device *phonet_device_get(struct net *net)
117 {
118         struct phonet_device_list *pndevs = phonet_device_list(net);
119         struct phonet_device *pnd;
120         struct net_device *dev = NULL;
121
122         rcu_read_lock();
123         list_for_each_entry_rcu(pnd, &pndevs->list, list) {
124                 dev = pnd->netdev;
125                 BUG_ON(!dev);
126
127                 if ((dev->reg_state == NETREG_REGISTERED) &&
128                         ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
129                         break;
130                 dev = NULL;
131         }
132         if (dev)
133                 dev_hold(dev);
134         rcu_read_unlock();
135         return dev;
136 }
137
138 int phonet_address_add(struct net_device *dev, u8 addr)
139 {
140         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
141         struct phonet_device *pnd;
142         int err = 0;
143
144         mutex_lock(&pndevs->lock);
145         /* Find or create Phonet-specific device data */
146         pnd = __phonet_get(dev);
147         if (pnd == NULL)
148                 pnd = __phonet_device_alloc(dev);
149         if (unlikely(pnd == NULL))
150                 err = -ENOMEM;
151         else if (test_and_set_bit(addr >> 2, pnd->addrs))
152                 err = -EEXIST;
153         mutex_unlock(&pndevs->lock);
154         return err;
155 }
156
157 int phonet_address_del(struct net_device *dev, u8 addr)
158 {
159         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
160         struct phonet_device *pnd;
161         int err = 0;
162
163         mutex_lock(&pndevs->lock);
164         pnd = __phonet_get(dev);
165         if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) {
166                 err = -EADDRNOTAVAIL;
167                 pnd = NULL;
168         } else if (bitmap_empty(pnd->addrs, 64))
169                 list_del_rcu(&pnd->list);
170         else
171                 pnd = NULL;
172         mutex_unlock(&pndevs->lock);
173
174         if (pnd) {
175                 synchronize_rcu();
176                 kfree(pnd);
177         }
178         return err;
179 }
180
181 /* Gets a source address toward a destination, through a interface. */
182 u8 phonet_address_get(struct net_device *dev, u8 daddr)
183 {
184         struct phonet_device *pnd;
185         u8 saddr;
186
187         rcu_read_lock();
188         pnd = __phonet_get_rcu(dev);
189         if (pnd) {
190                 BUG_ON(bitmap_empty(pnd->addrs, 64));
191
192                 /* Use same source address as destination, if possible */
193                 if (test_bit(daddr >> 2, pnd->addrs))
194                         saddr = daddr;
195                 else
196                         saddr = find_first_bit(pnd->addrs, 64) << 2;
197         } else
198                 saddr = PN_NO_ADDR;
199         rcu_read_unlock();
200
201         if (saddr == PN_NO_ADDR) {
202                 /* Fallback to another device */
203                 struct net_device *def_dev;
204
205                 def_dev = phonet_device_get(dev_net(dev));
206                 if (def_dev) {
207                         if (def_dev != dev)
208                                 saddr = phonet_address_get(def_dev, daddr);
209                         dev_put(def_dev);
210                 }
211         }
212         return saddr;
213 }
214
215 int phonet_address_lookup(struct net *net, u8 addr)
216 {
217         struct phonet_device_list *pndevs = phonet_device_list(net);
218         struct phonet_device *pnd;
219         int err = -EADDRNOTAVAIL;
220
221         rcu_read_lock();
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)
226                         continue;
227
228                 if (test_bit(addr >> 2, pnd->addrs)) {
229                         err = 0;
230                         goto found;
231                 }
232         }
233 found:
234         rcu_read_unlock();
235         return err;
236 }
237
238 /* automatically configure a Phonet device, if supported */
239 static int phonet_device_autoconf(struct net_device *dev)
240 {
241         struct if_phonet_req req;
242         int ret;
243
244         if (!dev->netdev_ops->ndo_do_ioctl)
245                 return -EOPNOTSUPP;
246
247         ret = dev->netdev_ops->ndo_do_ioctl(dev, (struct ifreq *)&req,
248                                                 SIOCPNGAUTOCONF);
249         if (ret < 0)
250                 return ret;
251
252         ASSERT_RTNL();
253         ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device);
254         if (ret)
255                 return ret;
256         phonet_address_notify(RTM_NEWADDR, dev,
257                                 req.ifr_phonet_autoconf.device);
258         return 0;
259 }
260
261 static void phonet_route_autodel(struct net_device *dev)
262 {
263         struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
264         unsigned i;
265         DECLARE_BITMAP(deleted, 64);
266
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);
273                         set_bit(i, deleted);
274                 }
275         mutex_unlock(&pnn->routes.lock);
276
277         if (bitmap_empty(deleted, 64))
278                 return; /* short-circuit RCU */
279         synchronize_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);
283                 dev_put(dev);
284         }
285 }
286
287 /* notify Phonet of device events */
288 static int phonet_device_notify(struct notifier_block *me, unsigned long what,
289                                 void *arg)
290 {
291         struct net_device *dev = arg;
292
293         switch (what) {
294         case NETDEV_REGISTER:
295                 if (dev->type == ARPHRD_PHONET)
296                         phonet_device_autoconf(dev);
297                 break;
298         case NETDEV_UNREGISTER:
299                 phonet_device_destroy(dev);
300                 phonet_route_autodel(dev);
301                 break;
302         }
303         return 0;
304
305 }
306
307 static struct notifier_block phonet_device_notifier = {
308         .notifier_call = phonet_device_notify,
309         .priority = 0,
310 };
311
312 /* Per-namespace Phonet devices handling */
313 static int __net_init phonet_init_net(struct net *net)
314 {
315         struct phonet_net *pnn = net_generic(net, phonet_net_id);
316
317         if (!proc_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops))
318                 return -ENOMEM;
319
320         INIT_LIST_HEAD(&pnn->pndevs.list);
321         mutex_init(&pnn->pndevs.lock);
322         mutex_init(&pnn->routes.lock);
323         return 0;
324 }
325
326 static void __net_exit phonet_exit_net(struct net *net)
327 {
328         struct phonet_net *pnn = net_generic(net, phonet_net_id);
329         struct net_device *dev;
330         unsigned i;
331
332         rtnl_lock();
333         for_each_netdev(net, dev)
334                 phonet_device_destroy(dev);
335
336         for (i = 0; i < 64; i++) {
337                 dev = pnn->routes.table[i];
338                 if (dev) {
339                         rtm_phonet_notify(RTM_DELROUTE, dev, i);
340                         dev_put(dev);
341                 }
342         }
343         rtnl_unlock();
344
345         proc_net_remove(net, "phonet");
346 }
347
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),
353 };
354
355 /* Initialize Phonet devices list */
356 int __init phonet_device_init(void)
357 {
358         int err = register_pernet_device(&phonet_net_ops);
359         if (err)
360                 return err;
361
362         register_netdevice_notifier(&phonet_device_notifier);
363         err = phonet_netlink_register();
364         if (err)
365                 phonet_device_exit();
366         return err;
367 }
368
369 void phonet_device_exit(void)
370 {
371         rtnl_unregister_all(PF_PHONET);
372         unregister_netdevice_notifier(&phonet_device_notifier);
373         unregister_pernet_device(&phonet_net_ops);
374 }
375
376 int phonet_route_add(struct net_device *dev, u8 daddr)
377 {
378         struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
379         struct phonet_routes *routes = &pnn->routes;
380         int err = -EEXIST;
381
382         daddr = daddr >> 2;
383         mutex_lock(&routes->lock);
384         if (routes->table[daddr] == NULL) {
385                 rcu_assign_pointer(routes->table[daddr], dev);
386                 dev_hold(dev);
387                 err = 0;
388         }
389         mutex_unlock(&routes->lock);
390         return err;
391 }
392
393 int phonet_route_del(struct net_device *dev, u8 daddr)
394 {
395         struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
396         struct phonet_routes *routes = &pnn->routes;
397
398         daddr = daddr >> 2;
399         mutex_lock(&routes->lock);
400         if (dev == routes->table[daddr])
401                 rcu_assign_pointer(routes->table[daddr], NULL);
402         else
403                 dev = NULL;
404         mutex_unlock(&routes->lock);
405
406         if (!dev)
407                 return -ENOENT;
408         synchronize_rcu();
409         dev_put(dev);
410         return 0;
411 }
412
413 struct net_device *phonet_route_get(struct net *net, u8 daddr)
414 {
415         struct phonet_net *pnn = net_generic(net, phonet_net_id);
416         struct phonet_routes *routes = &pnn->routes;
417         struct net_device *dev;
418
419         ASSERT_RTNL(); /* no need to hold the device */
420
421         daddr >>= 2;
422         rcu_read_lock();
423         dev = rcu_dereference(routes->table[daddr]);
424         rcu_read_unlock();
425         return dev;
426 }
427
428 struct net_device *phonet_route_output(struct net *net, u8 daddr)
429 {
430         struct phonet_net *pnn = net_generic(net, phonet_net_id);
431         struct phonet_routes *routes = &pnn->routes;
432         struct net_device *dev;
433
434         daddr >>= 2;
435         rcu_read_lock();
436         dev = rcu_dereference(routes->table[daddr]);
437         if (dev)
438                 dev_hold(dev);
439         rcu_read_unlock();
440
441         if (!dev)
442                 dev = phonet_device_get(net); /* Default route */
443         return dev;
444 }