blob: 49bd76a87461baa3bca689c96faac43f2a917a0a [file] [log] [blame]
Thomas Gleixner2b27bdc2019-05-29 16:57:50 -07001// SPDX-License-Identifier: GPL-2.0-only
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -07002/*
3 * File: pn_dev.c
4 *
5 * Phonet network device
6 *
7 * Copyright (C) 2008 Nokia Corporation.
8 *
Rémi Denis-Courmont31fdc552012-06-13 22:29:03 +00009 * Authors: Sakari Ailus <sakari.ailus@nokia.com>
10 * Rémi Denis-Courmont
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070011 */
12
13#include <linux/kernel.h>
14#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070016#include <linux/netdevice.h>
17#include <linux/phonet.h>
David S. Miller421d20a2009-07-26 13:39:10 -070018#include <linux/proc_fs.h>
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +000019#include <linux/if_arp.h>
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070020#include <net/sock.h>
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000021#include <net/netns/generic.h>
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070022#include <net/phonet/pn_dev.h>
23
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +000024struct phonet_routes {
Rémi Denis-Courmont88880132009-11-13 05:01:19 +000025 struct mutex lock;
Fabian Frederick79952bc2014-10-06 20:15:20 +020026 struct net_device __rcu *table[64];
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +000027};
28
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000029struct phonet_net {
30 struct phonet_device_list pndevs;
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +000031 struct phonet_routes routes;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070032};
33
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030034static unsigned int phonet_net_id __read_mostly;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000035
Jiri Pirko0db3f0f2010-04-26 03:41:00 +000036static struct phonet_net *phonet_pernet(struct net *net)
37{
38 BUG_ON(!net);
39
40 return net_generic(net, phonet_net_id);
41}
42
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000043struct phonet_device_list *phonet_device_list(struct net *net)
44{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +000045 struct phonet_net *pnn = phonet_pernet(net);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000046 return &pnn->pndevs;
47}
48
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070049/* Allocate new Phonet device. */
50static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
51{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000052 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070053 struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
54 if (pnd == NULL)
55 return NULL;
56 pnd->netdev = dev;
57 bitmap_zero(pnd->addrs, 64);
58
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -080059 BUG_ON(!mutex_is_locked(&pndevs->lock));
60 list_add_rcu(&pnd->list, &pndevs->list);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070061 return pnd;
62}
63
64static struct phonet_device *__phonet_get(struct net_device *dev)
65{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000066 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070067 struct phonet_device *pnd;
68
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -080069 BUG_ON(!mutex_is_locked(&pndevs->lock));
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000070 list_for_each_entry(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070071 if (pnd->netdev == dev)
72 return pnd;
73 }
74 return NULL;
75}
76
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -080077static struct phonet_device *__phonet_get_rcu(struct net_device *dev)
78{
79 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
80 struct phonet_device *pnd;
81
82 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
83 if (pnd->netdev == dev)
84 return pnd;
85 }
86 return NULL;
87}
88
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +000089static void phonet_device_destroy(struct net_device *dev)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070090{
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +000091 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
92 struct phonet_device *pnd;
93
94 ASSERT_RTNL();
95
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -080096 mutex_lock(&pndevs->lock);
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +000097 pnd = __phonet_get(dev);
98 if (pnd)
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -080099 list_del_rcu(&pnd->list);
100 mutex_unlock(&pndevs->lock);
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000101
102 if (pnd) {
103 u8 addr;
104
Akinobu Mitaa1ca14a2010-03-11 12:07:49 +0000105 for_each_set_bit(addr, pnd->addrs, 64)
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000106 phonet_address_notify(RTM_DELADDR, dev, addr);
107 kfree(pnd);
108 }
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700109}
110
111struct net_device *phonet_device_get(struct net *net)
112{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000113 struct phonet_device_list *pndevs = phonet_device_list(net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700114 struct phonet_device *pnd;
Eric Dumazet59e57f42009-07-27 08:03:18 -0700115 struct net_device *dev = NULL;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700116
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800117 rcu_read_lock();
118 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700119 dev = pnd->netdev;
120 BUG_ON(!dev);
121
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000122 if ((dev->reg_state == NETREG_REGISTERED) &&
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700123 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
124 break;
125 dev = NULL;
126 }
127 if (dev)
128 dev_hold(dev);
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800129 rcu_read_unlock();
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700130 return dev;
131}
132
133int phonet_address_add(struct net_device *dev, u8 addr)
134{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000135 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700136 struct phonet_device *pnd;
137 int err = 0;
138
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800139 mutex_lock(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700140 /* Find or create Phonet-specific device data */
141 pnd = __phonet_get(dev);
142 if (pnd == NULL)
143 pnd = __phonet_device_alloc(dev);
144 if (unlikely(pnd == NULL))
145 err = -ENOMEM;
146 else if (test_and_set_bit(addr >> 2, pnd->addrs))
147 err = -EEXIST;
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800148 mutex_unlock(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700149 return err;
150}
151
152int phonet_address_del(struct net_device *dev, u8 addr)
153{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000154 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700155 struct phonet_device *pnd;
156 int err = 0;
157
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800158 mutex_lock(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700159 pnd = __phonet_get(dev);
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800160 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700161 err = -EADDRNOTAVAIL;
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800162 pnd = NULL;
163 } else if (bitmap_empty(pnd->addrs, 64))
164 list_del_rcu(&pnd->list);
165 else
166 pnd = NULL;
167 mutex_unlock(&pndevs->lock);
168
Jiri Pirko88e75942010-06-07 03:27:39 +0000169 if (pnd)
Lai Jiangshan7e113a92011-03-18 12:09:03 +0800170 kfree_rcu(pnd, rcu);
Jiri Pirko88e75942010-06-07 03:27:39 +0000171
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700172 return err;
173}
174
175/* Gets a source address toward a destination, through a interface. */
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000176u8 phonet_address_get(struct net_device *dev, u8 daddr)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700177{
178 struct phonet_device *pnd;
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000179 u8 saddr;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700180
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800181 rcu_read_lock();
182 pnd = __phonet_get_rcu(dev);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700183 if (pnd) {
184 BUG_ON(bitmap_empty(pnd->addrs, 64));
185
186 /* Use same source address as destination, if possible */
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000187 if (test_bit(daddr >> 2, pnd->addrs))
188 saddr = daddr;
189 else
190 saddr = find_first_bit(pnd->addrs, 64) << 2;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700191 } else
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000192 saddr = PN_NO_ADDR;
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800193 rcu_read_unlock();
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000194
195 if (saddr == PN_NO_ADDR) {
196 /* Fallback to another device */
197 struct net_device *def_dev;
198
199 def_dev = phonet_device_get(dev_net(dev));
200 if (def_dev) {
201 if (def_dev != dev)
202 saddr = phonet_address_get(def_dev, daddr);
203 dev_put(def_dev);
204 }
205 }
206 return saddr;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700207}
208
Rémi Denis-Courmont52404882008-12-03 15:42:56 -0800209int phonet_address_lookup(struct net *net, u8 addr)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700210{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000211 struct phonet_device_list *pndevs = phonet_device_list(net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700212 struct phonet_device *pnd;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000213 int err = -EADDRNOTAVAIL;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700214
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800215 rcu_read_lock();
216 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700217 /* Don't allow unregistering devices! */
218 if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
219 ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
220 continue;
221
222 if (test_bit(addr >> 2, pnd->addrs)) {
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000223 err = 0;
224 goto found;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700225 }
226 }
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000227found:
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800228 rcu_read_unlock();
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000229 return err;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700230}
231
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000232/* automatically configure a Phonet device, if supported */
233static int phonet_device_autoconf(struct net_device *dev)
234{
235 struct if_phonet_req req;
236 int ret;
237
238 if (!dev->netdev_ops->ndo_do_ioctl)
239 return -EOPNOTSUPP;
240
241 ret = dev->netdev_ops->ndo_do_ioctl(dev, (struct ifreq *)&req,
242 SIOCPNGAUTOCONF);
243 if (ret < 0)
244 return ret;
Rémi Denis-Courmontb11b5162009-09-14 03:10:27 +0000245
246 ASSERT_RTNL();
247 ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device);
248 if (ret)
249 return ret;
250 phonet_address_notify(RTM_NEWADDR, dev,
251 req.ifr_phonet_autoconf.device);
252 return 0;
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000253}
254
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000255static void phonet_route_autodel(struct net_device *dev)
256{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000257 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
Eric Dumazet95c96172012-04-15 05:58:06 +0000258 unsigned int i;
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000259 DECLARE_BITMAP(deleted, 64);
260
261 /* Remove left-over Phonet routes */
262 bitmap_zero(deleted, 64);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000263 mutex_lock(&pnn->routes.lock);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000264 for (i = 0; i < 64; i++)
Fabian Frederick79952bc2014-10-06 20:15:20 +0200265 if (rcu_access_pointer(pnn->routes.table[i]) == dev) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000266 RCU_INIT_POINTER(pnn->routes.table[i], NULL);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000267 set_bit(i, deleted);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000268 }
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000269 mutex_unlock(&pnn->routes.lock);
270
271 if (bitmap_empty(deleted, 64))
272 return; /* short-circuit RCU */
273 synchronize_rcu();
Akinobu Mita6a499b22010-08-27 19:08:26 +0000274 for_each_set_bit(i, deleted, 64) {
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000275 rtm_phonet_notify(RTM_DELROUTE, dev, i);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000276 dev_put(dev);
277 }
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000278}
279
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700280/* notify Phonet of device events */
281static int phonet_device_notify(struct notifier_block *me, unsigned long what,
Jiri Pirko351638e2013-05-28 01:30:21 +0000282 void *ptr)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700283{
Jiri Pirko351638e2013-05-28 01:30:21 +0000284 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700285
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000286 switch (what) {
287 case NETDEV_REGISTER:
288 if (dev->type == ARPHRD_PHONET)
289 phonet_device_autoconf(dev);
290 break;
291 case NETDEV_UNREGISTER:
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000292 phonet_device_destroy(dev);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000293 phonet_route_autodel(dev);
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000294 break;
295 }
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700296 return 0;
297
298}
299
300static struct notifier_block phonet_device_notifier = {
301 .notifier_call = phonet_device_notify,
302 .priority = 0,
303};
304
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000305/* Per-namespace Phonet devices handling */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000306static int __net_init phonet_init_net(struct net *net)
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000307{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000308 struct phonet_net *pnn = phonet_pernet(net);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000309
Christoph Hellwigc3506372018-04-10 19:42:55 +0200310 if (!proc_create_net("phonet", 0, net->proc_net, &pn_sock_seq_ops,
311 sizeof(struct seq_net_private)))
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000312 return -ENOMEM;
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000313
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000314 INIT_LIST_HEAD(&pnn->pndevs.list);
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800315 mutex_init(&pnn->pndevs.lock);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000316 mutex_init(&pnn->routes.lock);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000317 return 0;
318}
319
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000320static void __net_exit phonet_exit_net(struct net *net)
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000321{
Vasily Averinae61e8c2017-11-12 22:32:47 +0300322 struct phonet_net *pnn = phonet_pernet(net);
323
Gao fengece31ff2013-02-18 01:34:56 +0000324 remove_proc_entry("phonet", net->proc_net);
Vasily Averinae61e8c2017-11-12 22:32:47 +0300325 WARN_ON_ONCE(!list_empty(&pnn->pndevs.list));
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000326}
327
328static struct pernet_operations phonet_net_ops = {
329 .init = phonet_init_net,
330 .exit = phonet_exit_net,
Eric W. Biedermand2b3eb62009-11-29 15:46:12 +0000331 .id = &phonet_net_id,
332 .size = sizeof(struct phonet_net),
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000333};
334
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700335/* Initialize Phonet devices list */
remi.denis-courmont@nokia76e02cf2009-01-23 03:00:27 +0000336int __init phonet_device_init(void)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700337{
Eric W. Biederman03478752012-04-06 15:35:39 +0000338 int err = register_pernet_subsys(&phonet_net_ops);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000339 if (err)
340 return err;
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000341
Christoph Hellwigc3506372018-04-10 19:42:55 +0200342 proc_create_net("pnresource", 0, init_net.proc_net, &pn_res_seq_ops,
343 sizeof(struct seq_net_private));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700344 register_netdevice_notifier(&phonet_device_notifier);
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000345 err = phonet_netlink_register();
346 if (err)
347 phonet_device_exit();
348 return err;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700349}
350
351void phonet_device_exit(void)
352{
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700353 rtnl_unregister_all(PF_PHONET);
remi.denis-courmont@nokia6530e0f2009-01-23 03:00:29 +0000354 unregister_netdevice_notifier(&phonet_device_notifier);
Eric W. Biederman03478752012-04-06 15:35:39 +0000355 unregister_pernet_subsys(&phonet_net_ops);
Gao fengece31ff2013-02-18 01:34:56 +0000356 remove_proc_entry("pnresource", init_net.proc_net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700357}
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000358
359int phonet_route_add(struct net_device *dev, u8 daddr)
360{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000361 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000362 struct phonet_routes *routes = &pnn->routes;
363 int err = -EEXIST;
364
365 daddr = daddr >> 2;
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000366 mutex_lock(&routes->lock);
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000367 if (routes->table[daddr] == NULL) {
Eric Dumazetcf778b02012-01-12 04:41:32 +0000368 rcu_assign_pointer(routes->table[daddr], dev);
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000369 dev_hold(dev);
370 err = 0;
371 }
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000372 mutex_unlock(&routes->lock);
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000373 return err;
374}
375
376int phonet_route_del(struct net_device *dev, u8 daddr)
377{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000378 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000379 struct phonet_routes *routes = &pnn->routes;
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000380
381 daddr = daddr >> 2;
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000382 mutex_lock(&routes->lock);
Fabian Frederick79952bc2014-10-06 20:15:20 +0200383 if (rcu_access_pointer(routes->table[daddr]) == dev)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000384 RCU_INIT_POINTER(routes->table[daddr], NULL);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000385 else
386 dev = NULL;
387 mutex_unlock(&routes->lock);
388
389 if (!dev)
390 return -ENOENT;
391 synchronize_rcu();
392 dev_put(dev);
393 return 0;
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000394}
395
Eric Dumazete67f88d2011-04-27 22:56:07 +0000396struct net_device *phonet_route_get_rcu(struct net *net, u8 daddr)
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000397{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000398 struct phonet_net *pnn = phonet_pernet(net);
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000399 struct phonet_routes *routes = &pnn->routes;
400 struct net_device *dev;
401
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000402 daddr >>= 2;
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000403 dev = rcu_dereference(routes->table[daddr]);
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000404 return dev;
405}
406
407struct net_device *phonet_route_output(struct net *net, u8 daddr)
408{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000409 struct phonet_net *pnn = phonet_pernet(net);
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000410 struct phonet_routes *routes = &pnn->routes;
411 struct net_device *dev;
412
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000413 daddr >>= 2;
414 rcu_read_lock();
415 dev = rcu_dereference(routes->table[daddr]);
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000416 if (dev)
417 dev_hold(dev);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000418 rcu_read_unlock();
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000419
420 if (!dev)
421 dev = phonet_device_get(net); /* Default route */
422 return dev;
423}