blob: ac0fae06cc153f405eccf72a30b3b500ff9c3b21 [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{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +000038 return net_generic(net, phonet_net_id);
39}
40
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000041struct phonet_device_list *phonet_device_list(struct net *net)
42{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +000043 struct phonet_net *pnn = phonet_pernet(net);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000044 return &pnn->pndevs;
45}
46
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070047/* Allocate new Phonet device. */
48static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
49{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000050 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070051 struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
52 if (pnd == NULL)
53 return NULL;
54 pnd->netdev = dev;
55 bitmap_zero(pnd->addrs, 64);
56
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -080057 BUG_ON(!mutex_is_locked(&pndevs->lock));
58 list_add_rcu(&pnd->list, &pndevs->list);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070059 return pnd;
60}
61
62static struct phonet_device *__phonet_get(struct net_device *dev)
63{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000064 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070065 struct phonet_device *pnd;
66
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -080067 BUG_ON(!mutex_is_locked(&pndevs->lock));
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000068 list_for_each_entry(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070069 if (pnd->netdev == dev)
70 return pnd;
71 }
72 return NULL;
73}
74
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -080075static struct phonet_device *__phonet_get_rcu(struct net_device *dev)
76{
77 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
78 struct phonet_device *pnd;
79
80 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
81 if (pnd->netdev == dev)
82 return pnd;
83 }
84 return NULL;
85}
86
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +000087static void phonet_device_destroy(struct net_device *dev)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070088{
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +000089 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
90 struct phonet_device *pnd;
91
92 ASSERT_RTNL();
93
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -080094 mutex_lock(&pndevs->lock);
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +000095 pnd = __phonet_get(dev);
96 if (pnd)
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -080097 list_del_rcu(&pnd->list);
98 mutex_unlock(&pndevs->lock);
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +000099
100 if (pnd) {
101 u8 addr;
102
Akinobu Mitaa1ca14a2010-03-11 12:07:49 +0000103 for_each_set_bit(addr, pnd->addrs, 64)
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000104 phonet_address_notify(RTM_DELADDR, dev, addr);
105 kfree(pnd);
106 }
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700107}
108
109struct net_device *phonet_device_get(struct net *net)
110{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000111 struct phonet_device_list *pndevs = phonet_device_list(net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700112 struct phonet_device *pnd;
Eric Dumazet59e57f42009-07-27 08:03:18 -0700113 struct net_device *dev = NULL;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700114
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800115 rcu_read_lock();
116 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700117 dev = pnd->netdev;
118 BUG_ON(!dev);
119
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000120 if ((dev->reg_state == NETREG_REGISTERED) &&
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700121 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
122 break;
123 dev = NULL;
124 }
125 if (dev)
126 dev_hold(dev);
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800127 rcu_read_unlock();
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700128 return dev;
129}
130
131int phonet_address_add(struct net_device *dev, u8 addr)
132{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000133 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700134 struct phonet_device *pnd;
135 int err = 0;
136
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800137 mutex_lock(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700138 /* Find or create Phonet-specific device data */
139 pnd = __phonet_get(dev);
140 if (pnd == NULL)
141 pnd = __phonet_device_alloc(dev);
142 if (unlikely(pnd == NULL))
143 err = -ENOMEM;
144 else if (test_and_set_bit(addr >> 2, pnd->addrs))
145 err = -EEXIST;
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800146 mutex_unlock(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700147 return err;
148}
149
150int phonet_address_del(struct net_device *dev, u8 addr)
151{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000152 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700153 struct phonet_device *pnd;
154 int err = 0;
155
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800156 mutex_lock(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700157 pnd = __phonet_get(dev);
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800158 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700159 err = -EADDRNOTAVAIL;
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800160 pnd = NULL;
161 } else if (bitmap_empty(pnd->addrs, 64))
162 list_del_rcu(&pnd->list);
163 else
164 pnd = NULL;
165 mutex_unlock(&pndevs->lock);
166
Jiri Pirko88e75942010-06-07 03:27:39 +0000167 if (pnd)
Lai Jiangshan7e113a92011-03-18 12:09:03 +0800168 kfree_rcu(pnd, rcu);
Jiri Pirko88e75942010-06-07 03:27:39 +0000169
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700170 return err;
171}
172
173/* Gets a source address toward a destination, through a interface. */
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000174u8 phonet_address_get(struct net_device *dev, u8 daddr)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700175{
176 struct phonet_device *pnd;
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000177 u8 saddr;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700178
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800179 rcu_read_lock();
180 pnd = __phonet_get_rcu(dev);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700181 if (pnd) {
182 BUG_ON(bitmap_empty(pnd->addrs, 64));
183
184 /* Use same source address as destination, if possible */
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000185 if (test_bit(daddr >> 2, pnd->addrs))
186 saddr = daddr;
187 else
188 saddr = find_first_bit(pnd->addrs, 64) << 2;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700189 } else
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000190 saddr = PN_NO_ADDR;
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800191 rcu_read_unlock();
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000192
193 if (saddr == PN_NO_ADDR) {
194 /* Fallback to another device */
195 struct net_device *def_dev;
196
197 def_dev = phonet_device_get(dev_net(dev));
198 if (def_dev) {
199 if (def_dev != dev)
200 saddr = phonet_address_get(def_dev, daddr);
201 dev_put(def_dev);
202 }
203 }
204 return saddr;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700205}
206
Rémi Denis-Courmont52404882008-12-03 15:42:56 -0800207int phonet_address_lookup(struct net *net, u8 addr)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700208{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000209 struct phonet_device_list *pndevs = phonet_device_list(net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700210 struct phonet_device *pnd;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000211 int err = -EADDRNOTAVAIL;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700212
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800213 rcu_read_lock();
214 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700215 /* Don't allow unregistering devices! */
216 if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
217 ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
218 continue;
219
220 if (test_bit(addr >> 2, pnd->addrs)) {
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000221 err = 0;
222 goto found;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700223 }
224 }
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000225found:
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800226 rcu_read_unlock();
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000227 return err;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700228}
229
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000230/* automatically configure a Phonet device, if supported */
231static int phonet_device_autoconf(struct net_device *dev)
232{
233 struct if_phonet_req req;
234 int ret;
235
236 if (!dev->netdev_ops->ndo_do_ioctl)
237 return -EOPNOTSUPP;
238
239 ret = dev->netdev_ops->ndo_do_ioctl(dev, (struct ifreq *)&req,
240 SIOCPNGAUTOCONF);
241 if (ret < 0)
242 return ret;
Rémi Denis-Courmontb11b5162009-09-14 03:10:27 +0000243
244 ASSERT_RTNL();
245 ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device);
246 if (ret)
247 return ret;
248 phonet_address_notify(RTM_NEWADDR, dev,
249 req.ifr_phonet_autoconf.device);
250 return 0;
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000251}
252
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000253static void phonet_route_autodel(struct net_device *dev)
254{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000255 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
Eric Dumazet95c96172012-04-15 05:58:06 +0000256 unsigned int i;
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000257 DECLARE_BITMAP(deleted, 64);
258
259 /* Remove left-over Phonet routes */
260 bitmap_zero(deleted, 64);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000261 mutex_lock(&pnn->routes.lock);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000262 for (i = 0; i < 64; i++)
Fabian Frederick79952bc2014-10-06 20:15:20 +0200263 if (rcu_access_pointer(pnn->routes.table[i]) == dev) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000264 RCU_INIT_POINTER(pnn->routes.table[i], NULL);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000265 set_bit(i, deleted);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000266 }
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000267 mutex_unlock(&pnn->routes.lock);
268
269 if (bitmap_empty(deleted, 64))
270 return; /* short-circuit RCU */
271 synchronize_rcu();
Akinobu Mita6a499b22010-08-27 19:08:26 +0000272 for_each_set_bit(i, deleted, 64) {
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000273 rtm_phonet_notify(RTM_DELROUTE, dev, i);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000274 dev_put(dev);
275 }
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000276}
277
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700278/* notify Phonet of device events */
279static int phonet_device_notify(struct notifier_block *me, unsigned long what,
Jiri Pirko351638e2013-05-28 01:30:21 +0000280 void *ptr)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700281{
Jiri Pirko351638e2013-05-28 01:30:21 +0000282 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700283
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000284 switch (what) {
285 case NETDEV_REGISTER:
286 if (dev->type == ARPHRD_PHONET)
287 phonet_device_autoconf(dev);
288 break;
289 case NETDEV_UNREGISTER:
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000290 phonet_device_destroy(dev);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000291 phonet_route_autodel(dev);
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000292 break;
293 }
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700294 return 0;
295
296}
297
298static struct notifier_block phonet_device_notifier = {
299 .notifier_call = phonet_device_notify,
300 .priority = 0,
301};
302
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000303/* Per-namespace Phonet devices handling */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000304static int __net_init phonet_init_net(struct net *net)
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000305{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000306 struct phonet_net *pnn = phonet_pernet(net);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000307
Christoph Hellwigc3506372018-04-10 19:42:55 +0200308 if (!proc_create_net("phonet", 0, net->proc_net, &pn_sock_seq_ops,
309 sizeof(struct seq_net_private)))
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000310 return -ENOMEM;
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000311
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000312 INIT_LIST_HEAD(&pnn->pndevs.list);
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800313 mutex_init(&pnn->pndevs.lock);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000314 mutex_init(&pnn->routes.lock);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000315 return 0;
316}
317
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000318static void __net_exit phonet_exit_net(struct net *net)
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000319{
Vasily Averinae61e8c2017-11-12 22:32:47 +0300320 struct phonet_net *pnn = phonet_pernet(net);
321
Gao fengece31ff2013-02-18 01:34:56 +0000322 remove_proc_entry("phonet", net->proc_net);
Vasily Averinae61e8c2017-11-12 22:32:47 +0300323 WARN_ON_ONCE(!list_empty(&pnn->pndevs.list));
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000324}
325
326static struct pernet_operations phonet_net_ops = {
327 .init = phonet_init_net,
328 .exit = phonet_exit_net,
Eric W. Biedermand2b3eb62009-11-29 15:46:12 +0000329 .id = &phonet_net_id,
330 .size = sizeof(struct phonet_net),
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000331};
332
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700333/* Initialize Phonet devices list */
remi.denis-courmont@nokia76e02cf2009-01-23 03:00:27 +0000334int __init phonet_device_init(void)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700335{
Eric W. Biederman03478752012-04-06 15:35:39 +0000336 int err = register_pernet_subsys(&phonet_net_ops);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000337 if (err)
338 return err;
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000339
Christoph Hellwigc3506372018-04-10 19:42:55 +0200340 proc_create_net("pnresource", 0, init_net.proc_net, &pn_res_seq_ops,
341 sizeof(struct seq_net_private));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700342 register_netdevice_notifier(&phonet_device_notifier);
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000343 err = phonet_netlink_register();
344 if (err)
345 phonet_device_exit();
346 return err;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700347}
348
349void phonet_device_exit(void)
350{
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700351 rtnl_unregister_all(PF_PHONET);
remi.denis-courmont@nokia6530e0f2009-01-23 03:00:29 +0000352 unregister_netdevice_notifier(&phonet_device_notifier);
Eric W. Biederman03478752012-04-06 15:35:39 +0000353 unregister_pernet_subsys(&phonet_net_ops);
Gao fengece31ff2013-02-18 01:34:56 +0000354 remove_proc_entry("pnresource", init_net.proc_net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700355}
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000356
357int phonet_route_add(struct net_device *dev, u8 daddr)
358{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000359 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000360 struct phonet_routes *routes = &pnn->routes;
361 int err = -EEXIST;
362
363 daddr = daddr >> 2;
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000364 mutex_lock(&routes->lock);
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000365 if (routes->table[daddr] == NULL) {
Eric Dumazetcf778b02012-01-12 04:41:32 +0000366 rcu_assign_pointer(routes->table[daddr], dev);
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000367 dev_hold(dev);
368 err = 0;
369 }
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000370 mutex_unlock(&routes->lock);
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000371 return err;
372}
373
374int phonet_route_del(struct net_device *dev, u8 daddr)
375{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000376 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000377 struct phonet_routes *routes = &pnn->routes;
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000378
379 daddr = daddr >> 2;
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000380 mutex_lock(&routes->lock);
Fabian Frederick79952bc2014-10-06 20:15:20 +0200381 if (rcu_access_pointer(routes->table[daddr]) == dev)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000382 RCU_INIT_POINTER(routes->table[daddr], NULL);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000383 else
384 dev = NULL;
385 mutex_unlock(&routes->lock);
386
387 if (!dev)
388 return -ENOENT;
389 synchronize_rcu();
390 dev_put(dev);
391 return 0;
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000392}
393
Eric Dumazete67f88d2011-04-27 22:56:07 +0000394struct net_device *phonet_route_get_rcu(struct net *net, u8 daddr)
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000395{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000396 struct phonet_net *pnn = phonet_pernet(net);
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000397 struct phonet_routes *routes = &pnn->routes;
398 struct net_device *dev;
399
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000400 daddr >>= 2;
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000401 dev = rcu_dereference(routes->table[daddr]);
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000402 return dev;
403}
404
405struct net_device *phonet_route_output(struct net *net, u8 daddr)
406{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000407 struct phonet_net *pnn = phonet_pernet(net);
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000408 struct phonet_routes *routes = &pnn->routes;
409 struct net_device *dev;
410
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000411 daddr >>= 2;
412 rcu_read_lock();
413 dev = rcu_dereference(routes->table[daddr]);
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000414 if (dev)
415 dev_hold(dev);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000416 rcu_read_unlock();
Rémi Denis-Courmont55748ac02009-10-14 00:48:28 +0000417
418 if (!dev)
419 dev = phonet_device_get(net); /* Default route */
420 return dev;
421}