Phonet: routing table backend
[deliverable/linux.git] / net / phonet / pn_dev.c
CommitLineData
f8ff6028
RDC
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>
421d20a3 30#include <linux/proc_fs.h>
f5bb1c55 31#include <linux/if_arp.h>
f8ff6028 32#include <net/sock.h>
9a3b7a42 33#include <net/netns/generic.h>
f8ff6028
RDC
34#include <net/phonet/pn_dev.h>
35
55748ac0
RDC
36struct phonet_routes {
37 spinlock_t lock;
38 struct net_device *table[64];
39};
40
9a3b7a42 41struct phonet_net {
42 struct phonet_device_list pndevs;
55748ac0 43 struct phonet_routes routes;
f8ff6028
RDC
44};
45
9a3b7a42 46int phonet_net_id;
47
48struct 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
f8ff6028
RDC
54/* Allocate new Phonet device. */
55static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
56{
9a3b7a42 57 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
f8ff6028
RDC
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
9a3b7a42 64 list_add(&pnd->list, &pndevs->list);
f8ff6028
RDC
65 return pnd;
66}
67
68static struct phonet_device *__phonet_get(struct net_device *dev)
69{
9a3b7a42 70 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
f8ff6028
RDC
71 struct phonet_device *pnd;
72
9a3b7a42 73 list_for_each_entry(pnd, &pndevs->list, list) {
f8ff6028
RDC
74 if (pnd->netdev == dev)
75 return pnd;
76 }
77 return NULL;
78}
79
2be6fa4c 80static void phonet_device_destroy(struct net_device *dev)
f8ff6028 81{
2be6fa4c
RDC
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 }
f8ff6028
RDC
101}
102
103struct net_device *phonet_device_get(struct net *net)
104{
9a3b7a42 105 struct phonet_device_list *pndevs = phonet_device_list(net);
f8ff6028 106 struct phonet_device *pnd;
59e57f44 107 struct net_device *dev = NULL;
f8ff6028 108
9a3b7a42 109 spin_lock_bh(&pndevs->lock);
110 list_for_each_entry(pnd, &pndevs->list, list) {
f8ff6028
RDC
111 dev = pnd->netdev;
112 BUG_ON(!dev);
113
9a3b7a42 114 if ((dev->reg_state == NETREG_REGISTERED) &&
f8ff6028
RDC
115 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
116 break;
117 dev = NULL;
118 }
119 if (dev)
120 dev_hold(dev);
9a3b7a42 121 spin_unlock_bh(&pndevs->lock);
f8ff6028
RDC
122 return dev;
123}
124
125int phonet_address_add(struct net_device *dev, u8 addr)
126{
9a3b7a42 127 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
f8ff6028
RDC
128 struct phonet_device *pnd;
129 int err = 0;
130
9a3b7a42 131 spin_lock_bh(&pndevs->lock);
f8ff6028
RDC
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;
9a3b7a42 140 spin_unlock_bh(&pndevs->lock);
f8ff6028
RDC
141 return err;
142}
143
144int phonet_address_del(struct net_device *dev, u8 addr)
145{
9a3b7a42 146 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
f8ff6028
RDC
147 struct phonet_device *pnd;
148 int err = 0;
149
9a3b7a42 150 spin_lock_bh(&pndevs->lock);
f8ff6028
RDC
151 pnd = __phonet_get(dev);
152 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs))
153 err = -EADDRNOTAVAIL;
2be6fa4c
RDC
154 else if (bitmap_empty(pnd->addrs, 64)) {
155 list_del(&pnd->list);
156 kfree(pnd);
157 }
9a3b7a42 158 spin_unlock_bh(&pndevs->lock);
f8ff6028
RDC
159 return err;
160}
161
162/* Gets a source address toward a destination, through a interface. */
55748ac0 163u8 phonet_address_get(struct net_device *dev, u8 daddr)
f8ff6028 164{
9a3b7a42 165 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
f8ff6028 166 struct phonet_device *pnd;
55748ac0 167 u8 saddr;
f8ff6028 168
9a3b7a42 169 spin_lock_bh(&pndevs->lock);
f8ff6028
RDC
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 */
55748ac0
RDC
175 if (test_bit(daddr >> 2, pnd->addrs))
176 saddr = daddr;
177 else
178 saddr = find_first_bit(pnd->addrs, 64) << 2;
f8ff6028 179 } else
55748ac0 180 saddr = PN_NO_ADDR;
9a3b7a42 181 spin_unlock_bh(&pndevs->lock);
55748ac0
RDC
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;
f8ff6028
RDC
195}
196
52404881 197int phonet_address_lookup(struct net *net, u8 addr)
f8ff6028 198{
9a3b7a42 199 struct phonet_device_list *pndevs = phonet_device_list(net);
f8ff6028 200 struct phonet_device *pnd;
9a3b7a42 201 int err = -EADDRNOTAVAIL;
f8ff6028 202
9a3b7a42 203 spin_lock_bh(&pndevs->lock);
204 list_for_each_entry(pnd, &pndevs->list, list) {
f8ff6028
RDC
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)) {
9a3b7a42 211 err = 0;
212 goto found;
f8ff6028
RDC
213 }
214 }
9a3b7a42 215found:
216 spin_unlock_bh(&pndevs->lock);
217 return err;
f8ff6028
RDC
218}
219
f5bb1c55
RDC
220/* automatically configure a Phonet device, if supported */
221static 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;
b11b5165
RDC
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;
f5bb1c55
RDC
241}
242
f8ff6028
RDC
243/* notify Phonet of device events */
244static int phonet_device_notify(struct notifier_block *me, unsigned long what,
245 void *arg)
246{
247 struct net_device *dev = arg;
248
f5bb1c55
RDC
249 switch (what) {
250 case NETDEV_REGISTER:
251 if (dev->type == ARPHRD_PHONET)
252 phonet_device_autoconf(dev);
253 break;
254 case NETDEV_UNREGISTER:
2be6fa4c 255 phonet_device_destroy(dev);
f5bb1c55
RDC
256 break;
257 }
f8ff6028
RDC
258 return 0;
259
260}
261
262static struct notifier_block phonet_device_notifier = {
263 .notifier_call = phonet_device_notify,
264 .priority = 0,
265};
266
9a3b7a42 267/* Per-namespace Phonet devices handling */
268static int phonet_init_net(struct net *net)
269{
55748ac0 270 struct phonet_net *pnn = kzalloc(sizeof(*pnn), GFP_KERNEL);
9a3b7a42 271 if (!pnn)
272 return -ENOMEM;
273
c1dc13e9
RDC
274 if (!proc_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops)) {
275 kfree(pnn);
276 return -ENOMEM;
277 }
278
9a3b7a42 279 INIT_LIST_HEAD(&pnn->pndevs.list);
280 spin_lock_init(&pnn->pndevs.lock);
55748ac0 281 spin_lock_init(&pnn->routes.lock);
9a3b7a42 282 net_assign_generic(net, phonet_net_id, pnn);
283 return 0;
284}
285
286static void phonet_exit_net(struct net *net)
287{
288 struct phonet_net *pnn = net_generic(net, phonet_net_id);
2be6fa4c 289 struct net_device *dev;
9a3b7a42 290
2be6fa4c
RDC
291 rtnl_lock();
292 for_each_netdev(net, dev)
293 phonet_device_destroy(dev);
294 rtnl_unlock();
c1dc13e9
RDC
295
296 proc_net_remove(net, "phonet");
9a3b7a42 297 kfree(pnn);
298}
299
300static struct pernet_operations phonet_net_ops = {
301 .init = phonet_init_net,
302 .exit = phonet_exit_net,
303};
304
f8ff6028 305/* Initialize Phonet devices list */
76e02cf6 306int __init phonet_device_init(void)
f8ff6028 307{
9a3b7a42 308 int err = register_pernet_gen_device(&phonet_net_id, &phonet_net_ops);
309 if (err)
310 return err;
660f706d 311
f8ff6028 312 register_netdevice_notifier(&phonet_device_notifier);
660f706d 313 err = phonet_netlink_register();
314 if (err)
315 phonet_device_exit();
316 return err;
f8ff6028
RDC
317}
318
319void phonet_device_exit(void)
320{
f8ff6028 321 rtnl_unregister_all(PF_PHONET);
6530e0fe 322 unregister_netdevice_notifier(&phonet_device_notifier);
9a3b7a42 323 unregister_pernet_gen_device(phonet_net_id, &phonet_net_ops);
f8ff6028 324}
55748ac0
RDC
325
326int phonet_route_add(struct net_device *dev, u8 daddr)
327{
328 struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
329 struct phonet_routes *routes = &pnn->routes;
330 int err = -EEXIST;
331
332 daddr = daddr >> 2;
333 spin_lock_bh(&routes->lock);
334 if (routes->table[daddr] == NULL) {
335 routes->table[daddr] = dev;
336 dev_hold(dev);
337 err = 0;
338 }
339 spin_unlock_bh(&routes->lock);
340 return err;
341}
342
343int phonet_route_del(struct net_device *dev, u8 daddr)
344{
345 struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
346 struct phonet_routes *routes = &pnn->routes;
347 int err = -ENOENT;
348
349 daddr = daddr >> 2;
350 spin_lock_bh(&routes->lock);
351 if (dev == routes->table[daddr]) {
352 routes->table[daddr] = NULL;
353 dev_put(dev);
354 err = 0;
355 }
356 spin_unlock_bh(&routes->lock);
357 return err;
358}
359
360struct net_device *phonet_route_get(struct net *net, u8 daddr)
361{
362 struct phonet_net *pnn = net_generic(net, phonet_net_id);
363 struct phonet_routes *routes = &pnn->routes;
364 struct net_device *dev;
365
366 ASSERT_RTNL(); /* no need to hold the device */
367
368 daddr >>= 2;
369 spin_lock_bh(&routes->lock);
370 dev = routes->table[daddr];
371 spin_unlock_bh(&routes->lock);
372 return dev;
373}
374
375struct net_device *phonet_route_output(struct net *net, u8 daddr)
376{
377 struct phonet_net *pnn = net_generic(net, phonet_net_id);
378 struct phonet_routes *routes = &pnn->routes;
379 struct net_device *dev;
380
381 spin_lock_bh(&routes->lock);
382 dev = routes->table[daddr >> 2];
383 if (dev)
384 dev_hold(dev);
385 spin_unlock_bh(&routes->lock);
386
387 if (!dev)
388 dev = phonet_device_get(net); /* Default route */
389 return dev;
390}
This page took 0.15514 seconds and 5 git commands to generate.