blob: 6cb4f602ab71586144ceb358a4eeac7c1eadde00 [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 *
Rémi Denis-Courmont31fdc5552012-06-13 22:29:03 +00008 * Authors: Sakari Ailus <sakari.ailus@nokia.com>
9 * Rémi Denis-Courmont
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070010 *
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;
Fabian Frederick79952bc2014-10-06 20:15:20 +020039 struct net_device __rcu *table[64];
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +000040};
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
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030047static unsigned int phonet_net_id __read_mostly;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000048
Jiri Pirko0db3f0f2010-04-26 03:41:00 +000049static struct phonet_net *phonet_pernet(struct net *net)
50{
51 BUG_ON(!net);
52
53 return net_generic(net, phonet_net_id);
54}
55
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000056struct phonet_device_list *phonet_device_list(struct net *net)
57{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +000058 struct phonet_net *pnn = phonet_pernet(net);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000059 return &pnn->pndevs;
60}
61
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070062/* Allocate new Phonet device. */
63static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
64{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000065 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070066 struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
67 if (pnd == NULL)
68 return NULL;
69 pnd->netdev = dev;
70 bitmap_zero(pnd->addrs, 64);
71
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -080072 BUG_ON(!mutex_is_locked(&pndevs->lock));
73 list_add_rcu(&pnd->list, &pndevs->list);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070074 return pnd;
75}
76
77static struct phonet_device *__phonet_get(struct net_device *dev)
78{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000079 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070080 struct phonet_device *pnd;
81
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -080082 BUG_ON(!mutex_is_locked(&pndevs->lock));
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000083 list_for_each_entry(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070084 if (pnd->netdev == dev)
85 return pnd;
86 }
87 return NULL;
88}
89
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -080090static struct phonet_device *__phonet_get_rcu(struct net_device *dev)
91{
92 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
93 struct phonet_device *pnd;
94
95 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
96 if (pnd->netdev == dev)
97 return pnd;
98 }
99 return NULL;
100}
101
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000102static void phonet_device_destroy(struct net_device *dev)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700103{
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000104 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
105 struct phonet_device *pnd;
106
107 ASSERT_RTNL();
108
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800109 mutex_lock(&pndevs->lock);
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000110 pnd = __phonet_get(dev);
111 if (pnd)
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800112 list_del_rcu(&pnd->list);
113 mutex_unlock(&pndevs->lock);
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000114
115 if (pnd) {
116 u8 addr;
117
Akinobu Mitaa1ca14a2010-03-11 12:07:49 +0000118 for_each_set_bit(addr, pnd->addrs, 64)
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000119 phonet_address_notify(RTM_DELADDR, dev, addr);
120 kfree(pnd);
121 }
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700122}
123
124struct net_device *phonet_device_get(struct net *net)
125{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000126 struct phonet_device_list *pndevs = phonet_device_list(net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700127 struct phonet_device *pnd;
Eric Dumazet59e57f42009-07-27 08:03:18 -0700128 struct net_device *dev = NULL;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700129
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800130 rcu_read_lock();
131 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700132 dev = pnd->netdev;
133 BUG_ON(!dev);
134
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000135 if ((dev->reg_state == NETREG_REGISTERED) &&
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700136 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
137 break;
138 dev = NULL;
139 }
140 if (dev)
141 dev_hold(dev);
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800142 rcu_read_unlock();
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700143 return dev;
144}
145
146int phonet_address_add(struct net_device *dev, u8 addr)
147{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000148 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700149 struct phonet_device *pnd;
150 int err = 0;
151
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800152 mutex_lock(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700153 /* Find or create Phonet-specific device data */
154 pnd = __phonet_get(dev);
155 if (pnd == NULL)
156 pnd = __phonet_device_alloc(dev);
157 if (unlikely(pnd == NULL))
158 err = -ENOMEM;
159 else if (test_and_set_bit(addr >> 2, pnd->addrs))
160 err = -EEXIST;
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800161 mutex_unlock(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700162 return err;
163}
164
165int phonet_address_del(struct net_device *dev, u8 addr)
166{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000167 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700168 struct phonet_device *pnd;
169 int err = 0;
170
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800171 mutex_lock(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700172 pnd = __phonet_get(dev);
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800173 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700174 err = -EADDRNOTAVAIL;
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800175 pnd = NULL;
176 } else if (bitmap_empty(pnd->addrs, 64))
177 list_del_rcu(&pnd->list);
178 else
179 pnd = NULL;
180 mutex_unlock(&pndevs->lock);
181
Jiri Pirko88e75942010-06-07 03:27:39 +0000182 if (pnd)
Lai Jiangshan7e113a92011-03-18 12:09:03 +0800183 kfree_rcu(pnd, rcu);
Jiri Pirko88e75942010-06-07 03:27:39 +0000184
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700185 return err;
186}
187
188/* Gets a source address toward a destination, through a interface. */
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000189u8 phonet_address_get(struct net_device *dev, u8 daddr)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700190{
191 struct phonet_device *pnd;
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000192 u8 saddr;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700193
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800194 rcu_read_lock();
195 pnd = __phonet_get_rcu(dev);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700196 if (pnd) {
197 BUG_ON(bitmap_empty(pnd->addrs, 64));
198
199 /* Use same source address as destination, if possible */
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000200 if (test_bit(daddr >> 2, pnd->addrs))
201 saddr = daddr;
202 else
203 saddr = find_first_bit(pnd->addrs, 64) << 2;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700204 } else
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000205 saddr = PN_NO_ADDR;
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800206 rcu_read_unlock();
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000207
208 if (saddr == PN_NO_ADDR) {
209 /* Fallback to another device */
210 struct net_device *def_dev;
211
212 def_dev = phonet_device_get(dev_net(dev));
213 if (def_dev) {
214 if (def_dev != dev)
215 saddr = phonet_address_get(def_dev, daddr);
216 dev_put(def_dev);
217 }
218 }
219 return saddr;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700220}
221
Rémi Denis-Courmont52404882008-12-03 15:42:56 -0800222int phonet_address_lookup(struct net *net, u8 addr)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700223{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000224 struct phonet_device_list *pndevs = phonet_device_list(net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700225 struct phonet_device *pnd;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000226 int err = -EADDRNOTAVAIL;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700227
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800228 rcu_read_lock();
229 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700230 /* Don't allow unregistering devices! */
231 if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
232 ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
233 continue;
234
235 if (test_bit(addr >> 2, pnd->addrs)) {
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000236 err = 0;
237 goto found;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700238 }
239 }
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000240found:
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800241 rcu_read_unlock();
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000242 return err;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700243}
244
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000245/* automatically configure a Phonet device, if supported */
246static int phonet_device_autoconf(struct net_device *dev)
247{
248 struct if_phonet_req req;
249 int ret;
250
251 if (!dev->netdev_ops->ndo_do_ioctl)
252 return -EOPNOTSUPP;
253
254 ret = dev->netdev_ops->ndo_do_ioctl(dev, (struct ifreq *)&req,
255 SIOCPNGAUTOCONF);
256 if (ret < 0)
257 return ret;
Rémi Denis-Courmontb11b5162009-09-14 03:10:27 +0000258
259 ASSERT_RTNL();
260 ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device);
261 if (ret)
262 return ret;
263 phonet_address_notify(RTM_NEWADDR, dev,
264 req.ifr_phonet_autoconf.device);
265 return 0;
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000266}
267
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000268static void phonet_route_autodel(struct net_device *dev)
269{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000270 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
Eric Dumazet95c96172012-04-15 05:58:06 +0000271 unsigned int i;
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000272 DECLARE_BITMAP(deleted, 64);
273
274 /* Remove left-over Phonet routes */
275 bitmap_zero(deleted, 64);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000276 mutex_lock(&pnn->routes.lock);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000277 for (i = 0; i < 64; i++)
Fabian Frederick79952bc2014-10-06 20:15:20 +0200278 if (rcu_access_pointer(pnn->routes.table[i]) == dev) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000279 RCU_INIT_POINTER(pnn->routes.table[i], NULL);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000280 set_bit(i, deleted);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000281 }
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000282 mutex_unlock(&pnn->routes.lock);
283
284 if (bitmap_empty(deleted, 64))
285 return; /* short-circuit RCU */
286 synchronize_rcu();
Akinobu Mita6a499b22010-08-27 19:08:26 +0000287 for_each_set_bit(i, deleted, 64) {
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000288 rtm_phonet_notify(RTM_DELROUTE, dev, i);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000289 dev_put(dev);
290 }
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000291}
292
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700293/* notify Phonet of device events */
294static int phonet_device_notify(struct notifier_block *me, unsigned long what,
Jiri Pirko351638e2013-05-28 01:30:21 +0000295 void *ptr)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700296{
Jiri Pirko351638e2013-05-28 01:30:21 +0000297 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700298
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000299 switch (what) {
300 case NETDEV_REGISTER:
301 if (dev->type == ARPHRD_PHONET)
302 phonet_device_autoconf(dev);
303 break;
304 case NETDEV_UNREGISTER:
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000305 phonet_device_destroy(dev);
Rémi Denis-Courmontf062f412009-10-14 00:48:29 +0000306 phonet_route_autodel(dev);
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000307 break;
308 }
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700309 return 0;
310
311}
312
313static struct notifier_block phonet_device_notifier = {
314 .notifier_call = phonet_device_notify,
315 .priority = 0,
316};
317
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000318/* Per-namespace Phonet devices handling */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000319static int __net_init phonet_init_net(struct net *net)
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000320{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000321 struct phonet_net *pnn = phonet_pernet(net);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000322
Christoph Hellwigc3506372018-04-10 19:42:55 +0200323 if (!proc_create_net("phonet", 0, net->proc_net, &pn_sock_seq_ops,
324 sizeof(struct seq_net_private)))
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000325 return -ENOMEM;
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000326
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000327 INIT_LIST_HEAD(&pnn->pndevs.list);
Rémi Denis-Courmonteeb74a9d2009-11-18 10:08:26 -0800328 mutex_init(&pnn->pndevs.lock);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000329 mutex_init(&pnn->routes.lock);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000330 return 0;
331}
332
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000333static void __net_exit phonet_exit_net(struct net *net)
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000334{
Vasily Averinae61e8c2017-11-12 22:32:47 +0300335 struct phonet_net *pnn = phonet_pernet(net);
336
Gao fengece31ff2013-02-18 01:34:56 +0000337 remove_proc_entry("phonet", net->proc_net);
Vasily Averinae61e8c2017-11-12 22:32:47 +0300338 WARN_ON_ONCE(!list_empty(&pnn->pndevs.list));
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000339}
340
341static struct pernet_operations phonet_net_ops = {
342 .init = phonet_init_net,
343 .exit = phonet_exit_net,
Eric W. Biedermand2b3eb62009-11-29 15:46:12 +0000344 .id = &phonet_net_id,
345 .size = sizeof(struct phonet_net),
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000346};
347
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700348/* Initialize Phonet devices list */
remi.denis-courmont@nokia76e02cf2009-01-23 03:00:27 +0000349int __init phonet_device_init(void)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700350{
Eric W. Biederman03478752012-04-06 15:35:39 +0000351 int err = register_pernet_subsys(&phonet_net_ops);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000352 if (err)
353 return err;
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000354
Christoph Hellwigc3506372018-04-10 19:42:55 +0200355 proc_create_net("pnresource", 0, init_net.proc_net, &pn_res_seq_ops,
356 sizeof(struct seq_net_private));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700357 register_netdevice_notifier(&phonet_device_notifier);
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000358 err = phonet_netlink_register();
359 if (err)
360 phonet_device_exit();
361 return err;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700362}
363
364void phonet_device_exit(void)
365{
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700366 rtnl_unregister_all(PF_PHONET);
remi.denis-courmont@nokia6530e0f2009-01-23 03:00:29 +0000367 unregister_netdevice_notifier(&phonet_device_notifier);
Eric W. Biederman03478752012-04-06 15:35:39 +0000368 unregister_pernet_subsys(&phonet_net_ops);
Gao fengece31ff2013-02-18 01:34:56 +0000369 remove_proc_entry("pnresource", init_net.proc_net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700370}
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000371
372int phonet_route_add(struct net_device *dev, u8 daddr)
373{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000374 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000375 struct phonet_routes *routes = &pnn->routes;
376 int err = -EEXIST;
377
378 daddr = daddr >> 2;
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000379 mutex_lock(&routes->lock);
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000380 if (routes->table[daddr] == NULL) {
Eric Dumazetcf778b02012-01-12 04:41:32 +0000381 rcu_assign_pointer(routes->table[daddr], dev);
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000382 dev_hold(dev);
383 err = 0;
384 }
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000385 mutex_unlock(&routes->lock);
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000386 return err;
387}
388
389int phonet_route_del(struct net_device *dev, u8 daddr)
390{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000391 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000392 struct phonet_routes *routes = &pnn->routes;
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000393
394 daddr = daddr >> 2;
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000395 mutex_lock(&routes->lock);
Fabian Frederick79952bc2014-10-06 20:15:20 +0200396 if (rcu_access_pointer(routes->table[daddr]) == dev)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000397 RCU_INIT_POINTER(routes->table[daddr], NULL);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000398 else
399 dev = NULL;
400 mutex_unlock(&routes->lock);
401
402 if (!dev)
403 return -ENOENT;
404 synchronize_rcu();
405 dev_put(dev);
406 return 0;
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000407}
408
Eric Dumazete67f88d2011-04-27 22:56:07 +0000409struct net_device *phonet_route_get_rcu(struct net *net, u8 daddr)
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000410{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000411 struct phonet_net *pnn = phonet_pernet(net);
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000412 struct phonet_routes *routes = &pnn->routes;
413 struct net_device *dev;
414
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000415 daddr >>= 2;
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000416 dev = rcu_dereference(routes->table[daddr]);
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000417 return dev;
418}
419
420struct net_device *phonet_route_output(struct net *net, u8 daddr)
421{
Jiri Pirko0db3f0f2010-04-26 03:41:00 +0000422 struct phonet_net *pnn = phonet_pernet(net);
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000423 struct phonet_routes *routes = &pnn->routes;
424 struct net_device *dev;
425
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000426 daddr >>= 2;
427 rcu_read_lock();
428 dev = rcu_dereference(routes->table[daddr]);
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000429 if (dev)
430 dev_hold(dev);
Rémi Denis-Courmont88880132009-11-13 05:01:19 +0000431 rcu_read_unlock();
Rémi Denis-Courmont55748ac2009-10-14 00:48:28 +0000432
433 if (!dev)
434 dev = phonet_device_get(net); /* Default route */
435 return dev;
436}