blob: 9b4ced6e0968dc99ecf198bbdfba1b4201aed0a4 [file] [log] [blame]
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070029#include <linux/netdevice.h>
30#include <linux/phonet.h>
David S. Miller421d20a2009-07-26 13:39:10 -070031#include <linux/proc_fs.h>
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +000032#include <linux/if_arp.h>
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070033#include <net/sock.h>
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000034#include <net/netns/generic.h>
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070035#include <net/phonet/pn_dev.h>
36
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +000037struct phonet_routes {
Rémi Denis-Courmont88880132009-11-13 05:01:19 +000038 struct mutex lock;
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +000039 struct net_device *table[64];
40};
41
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000042struct phonet_net {
43 struct phonet_device_list pndevs;
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +000044 struct phonet_routes routes;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070045};
46
Eric Dumazetf99189b2009-11-17 10:42:49 +000047int phonet_net_id __read_mostly;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000048
49struct phonet_device_list *phonet_device_list(struct net *net)
50{
51 struct phonet_net *pnn = net_generic(net, phonet_net_id);
52 return &pnn->pndevs;
53}
54
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070055/* Allocate new Phonet device. */
56static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
57{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000058 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070059 struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
60 if (pnd == NULL)
61 return NULL;
62 pnd->netdev = dev;
63 bitmap_zero(pnd->addrs, 64);
64
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -080065 BUG_ON(!mutex_is_locked(&pndevs->lock));
66 list_add_rcu(&pnd->list, &pndevs->list);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070067 return pnd;
68}
69
70static struct phonet_device *__phonet_get(struct net_device *dev)
71{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000072 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070073 struct phonet_device *pnd;
74
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -080075 BUG_ON(!mutex_is_locked(&pndevs->lock));
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000076 list_for_each_entry(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070077 if (pnd->netdev == dev)
78 return pnd;
79 }
80 return NULL;
81}
82
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -080083static struct phonet_device *__phonet_get_rcu(struct net_device *dev)
84{
85 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
86 struct phonet_device *pnd;
87
88 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
89 if (pnd->netdev == dev)
90 return pnd;
91 }
92 return NULL;
93}
94
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +000095static void phonet_device_destroy(struct net_device *dev)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070096{
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +000097 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
98 struct phonet_device *pnd;
99
100 ASSERT_RTNL();
101
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800102 mutex_lock(&pndevs->lock);
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000103 pnd = __phonet_get(dev);
104 if (pnd)
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800105 list_del_rcu(&pnd->list);
106 mutex_unlock(&pndevs->lock);
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000107
108 if (pnd) {
109 u8 addr;
110
Akinobu Mitaa1ca14a2010-03-11 12:07:49 +0000111 for_each_set_bit(addr, pnd->addrs, 64)
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000112 phonet_address_notify(RTM_DELADDR, dev, addr);
113 kfree(pnd);
114 }
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700115}
116
117struct net_device *phonet_device_get(struct net *net)
118{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000119 struct phonet_device_list *pndevs = phonet_device_list(net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700120 struct phonet_device *pnd;
Eric Dumazet59e57f42009-07-27 08:03:18 -0700121 struct net_device *dev = NULL;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700122
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800123 rcu_read_lock();
124 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700125 dev = pnd->netdev;
126 BUG_ON(!dev);
127
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000128 if ((dev->reg_state == NETREG_REGISTERED) &&
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700129 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
130 break;
131 dev = NULL;
132 }
133 if (dev)
134 dev_hold(dev);
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800135 rcu_read_unlock();
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700136 return dev;
137}
138
139int phonet_address_add(struct net_device *dev, u8 addr)
140{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000141 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700142 struct phonet_device *pnd;
143 int err = 0;
144
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800145 mutex_lock(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700146 /* Find or create Phonet-specific device data */
147 pnd = __phonet_get(dev);
148 if (pnd == NULL)
149 pnd = __phonet_device_alloc(dev);
150 if (unlikely(pnd == NULL))
151 err = -ENOMEM;
152 else if (test_and_set_bit(addr >> 2, pnd->addrs))
153 err = -EEXIST;
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800154 mutex_unlock(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700155 return err;
156}
157
158int phonet_address_del(struct net_device *dev, u8 addr)
159{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000160 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700161 struct phonet_device *pnd;
162 int err = 0;
163
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800164 mutex_lock(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700165 pnd = __phonet_get(dev);
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800166 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700167 err = -EADDRNOTAVAIL;
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800168 pnd = NULL;
169 } else if (bitmap_empty(pnd->addrs, 64))
170 list_del_rcu(&pnd->list);
171 else
172 pnd = NULL;
173 mutex_unlock(&pndevs->lock);
174
175 if (pnd) {
176 synchronize_rcu();
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000177 kfree(pnd);
178 }
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700179 return err;
180}
181
182/* Gets a source address toward a destination, through a interface. */
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000183u8 phonet_address_get(struct net_device *dev, u8 daddr)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700184{
185 struct phonet_device *pnd;
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000186 u8 saddr;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700187
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800188 rcu_read_lock();
189 pnd = __phonet_get_rcu(dev);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700190 if (pnd) {
191 BUG_ON(bitmap_empty(pnd->addrs, 64));
192
193 /* Use same source address as destination, if possible */
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000194 if (test_bit(daddr >> 2, pnd->addrs))
195 saddr = daddr;
196 else
197 saddr = find_first_bit(pnd->addrs, 64) << 2;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700198 } else
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000199 saddr = PN_NO_ADDR;
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800200 rcu_read_unlock();
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000201
202 if (saddr == PN_NO_ADDR) {
203 /* Fallback to another device */
204 struct net_device *def_dev;
205
206 def_dev = phonet_device_get(dev_net(dev));
207 if (def_dev) {
208 if (def_dev != dev)
209 saddr = phonet_address_get(def_dev, daddr);
210 dev_put(def_dev);
211 }
212 }
213 return saddr;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700214}
215
Rémi Denis-Courmont52404882008-12-03 15:42:56 -0800216int phonet_address_lookup(struct net *net, u8 addr)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700217{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000218 struct phonet_device_list *pndevs = phonet_device_list(net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700219 struct phonet_device *pnd;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000220 int err = -EADDRNOTAVAIL;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700221
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800222 rcu_read_lock();
223 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700224 /* Don't allow unregistering devices! */
225 if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
226 ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
227 continue;
228
229 if (test_bit(addr >> 2, pnd->addrs)) {
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000230 err = 0;
231 goto found;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700232 }
233 }
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000234found:
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800235 rcu_read_unlock();
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000236 return err;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700237}
238
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000239/* automatically configure a Phonet device, if supported */
240static int phonet_device_autoconf(struct net_device *dev)
241{
242 struct if_phonet_req req;
243 int ret;
244
245 if (!dev->netdev_ops->ndo_do_ioctl)
246 return -EOPNOTSUPP;
247
248 ret = dev->netdev_ops->ndo_do_ioctl(dev, (struct ifreq *)&req,
249 SIOCPNGAUTOCONF);
250 if (ret < 0)
251 return ret;
Rémi Denis-Courmontb11b5162009-09-14 03:10:27 +0000252
253 ASSERT_RTNL();
254 ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device);
255 if (ret)
256 return ret;
257 phonet_address_notify(RTM_NEWADDR, dev,
258 req.ifr_phonet_autoconf.device);
259 return 0;
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000260}
261
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000262static void phonet_route_autodel(struct net_device *dev)
263{
264 struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
265 unsigned i;
266 DECLARE_BITMAP(deleted, 64);
267
268 /* Remove left-over Phonet routes */
269 bitmap_zero(deleted, 64);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000270 mutex_lock(&pnn->routes.lock);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000271 for (i = 0; i < 64; i++)
272 if (dev == pnn->routes.table[i]) {
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000273 rcu_assign_pointer(pnn->routes.table[i], NULL);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000274 set_bit(i, deleted);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000275 }
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000276 mutex_unlock(&pnn->routes.lock);
277
278 if (bitmap_empty(deleted, 64))
279 return; /* short-circuit RCU */
280 synchronize_rcu();
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000281 for (i = find_first_bit(deleted, 64); i < 64;
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000282 i = find_next_bit(deleted, 64, i + 1)) {
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000283 rtm_phonet_notify(RTM_DELROUTE, dev, i);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000284 dev_put(dev);
285 }
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000286}
287
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700288/* notify Phonet of device events */
289static int phonet_device_notify(struct notifier_block *me, unsigned long what,
290 void *arg)
291{
292 struct net_device *dev = arg;
293
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000294 switch (what) {
295 case NETDEV_REGISTER:
296 if (dev->type == ARPHRD_PHONET)
297 phonet_device_autoconf(dev);
298 break;
299 case NETDEV_UNREGISTER:
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000300 phonet_device_destroy(dev);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000301 phonet_route_autodel(dev);
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000302 break;
303 }
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700304 return 0;
305
306}
307
308static struct notifier_block phonet_device_notifier = {
309 .notifier_call = phonet_device_notify,
310 .priority = 0,
311};
312
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000313/* Per-namespace Phonet devices handling */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000314static int __net_init phonet_init_net(struct net *net)
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000315{
Eric W. Biedermand2b3eb62009-11-29 15:46:12 +0000316 struct phonet_net *pnn = net_generic(net, phonet_net_id);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000317
Eric W. Biedermand2b3eb62009-11-29 15:46:12 +0000318 if (!proc_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops))
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000319 return -ENOMEM;
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000320
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000321 INIT_LIST_HEAD(&pnn->pndevs.list);
Rémi Denis-Courmonteeb74a92009-11-18 10:08:26 -0800322 mutex_init(&pnn->pndevs.lock);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000323 mutex_init(&pnn->routes.lock);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000324 return 0;
325}
326
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000327static void __net_exit phonet_exit_net(struct net *net)
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000328{
329 struct phonet_net *pnn = net_generic(net, phonet_net_id);
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000330 struct net_device *dev;
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000331 unsigned i;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000332
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000333 rtnl_lock();
334 for_each_netdev(net, dev)
335 phonet_device_destroy(dev);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000336
337 for (i = 0; i < 64; i++) {
338 dev = pnn->routes.table[i];
339 if (dev) {
340 rtm_phonet_notify(RTM_DELROUTE, dev, i);
341 dev_put(dev);
342 }
343 }
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000344 rtnl_unlock();
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000345
346 proc_net_remove(net, "phonet");
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000347}
348
349static struct pernet_operations phonet_net_ops = {
350 .init = phonet_init_net,
351 .exit = phonet_exit_net,
Eric W. Biedermand2b3eb62009-11-29 15:46:12 +0000352 .id = &phonet_net_id,
353 .size = sizeof(struct phonet_net),
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000354};
355
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700356/* Initialize Phonet devices list */
remi.denis-courmont@nokia76e02cf2009-01-23 03:00:27 +0000357int __init phonet_device_init(void)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700358{
Eric W. Biedermand2b3eb62009-11-29 15:46:12 +0000359 int err = register_pernet_device(&phonet_net_ops);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000360 if (err)
361 return err;
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000362
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700363 register_netdevice_notifier(&phonet_device_notifier);
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000364 err = phonet_netlink_register();
365 if (err)
366 phonet_device_exit();
367 return err;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700368}
369
370void phonet_device_exit(void)
371{
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700372 rtnl_unregister_all(PF_PHONET);
remi.denis-courmont@nokia6530e0f2009-01-23 03:00:29 +0000373 unregister_netdevice_notifier(&phonet_device_notifier);
Eric W. Biedermand2b3eb62009-11-29 15:46:12 +0000374 unregister_pernet_device(&phonet_net_ops);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700375}
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000376
377int phonet_route_add(struct net_device *dev, u8 daddr)
378{
379 struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
380 struct phonet_routes *routes = &pnn->routes;
381 int err = -EEXIST;
382
383 daddr = daddr >> 2;
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000384 mutex_lock(&routes->lock);
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000385 if (routes->table[daddr] == NULL) {
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000386 rcu_assign_pointer(routes->table[daddr], dev);
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000387 dev_hold(dev);
388 err = 0;
389 }
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000390 mutex_unlock(&routes->lock);
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000391 return err;
392}
393
394int phonet_route_del(struct net_device *dev, u8 daddr)
395{
396 struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
397 struct phonet_routes *routes = &pnn->routes;
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000398
399 daddr = daddr >> 2;
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000400 mutex_lock(&routes->lock);
401 if (dev == routes->table[daddr])
402 rcu_assign_pointer(routes->table[daddr], NULL);
403 else
404 dev = NULL;
405 mutex_unlock(&routes->lock);
406
407 if (!dev)
408 return -ENOENT;
409 synchronize_rcu();
410 dev_put(dev);
411 return 0;
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000412}
413
414struct net_device *phonet_route_get(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 ASSERT_RTNL(); /* no need to hold the device */
421
422 daddr >>= 2;
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000423 rcu_read_lock();
424 dev = rcu_dereference(routes->table[daddr]);
425 rcu_read_unlock();
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000426 return dev;
427}
428
429struct net_device *phonet_route_output(struct net *net, u8 daddr)
430{
431 struct phonet_net *pnn = net_generic(net, phonet_net_id);
432 struct phonet_routes *routes = &pnn->routes;
433 struct net_device *dev;
434
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000435 daddr >>= 2;
436 rcu_read_lock();
437 dev = rcu_dereference(routes->table[daddr]);
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000438 if (dev)
439 dev_hold(dev);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000440 rcu_read_unlock();
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000441
442 if (!dev)
443 dev = phonet_device_get(net); /* Default route */
444 return dev;
445}