Commit | Line | Data |
---|---|---|
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> | |
5a0e3ad6 | 28 | #include <linux/slab.h> |
f8ff6028 RDC |
29 | #include <linux/netdevice.h> |
30 | #include <linux/phonet.h> | |
421d20a3 | 31 | #include <linux/proc_fs.h> |
f5bb1c55 | 32 | #include <linux/if_arp.h> |
f8ff6028 | 33 | #include <net/sock.h> |
9a3b7a42 | 34 | #include <net/netns/generic.h> |
f8ff6028 RDC |
35 | #include <net/phonet/pn_dev.h> |
36 | ||
55748ac0 | 37 | struct phonet_routes { |
88880135 | 38 | struct mutex lock; |
55748ac0 RDC |
39 | struct net_device *table[64]; |
40 | }; | |
41 | ||
9a3b7a42 | 42 | struct phonet_net { |
43 | struct phonet_device_list pndevs; | |
55748ac0 | 44 | struct phonet_routes routes; |
f8ff6028 RDC |
45 | }; |
46 | ||
f99189b1 | 47 | int phonet_net_id __read_mostly; |
9a3b7a42 | 48 | |
49 | struct 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 | ||
f8ff6028 RDC |
55 | /* Allocate new Phonet device. */ |
56 | static struct phonet_device *__phonet_device_alloc(struct net_device *dev) | |
57 | { | |
9a3b7a42 | 58 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
f8ff6028 RDC |
59 | 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 | ||
eeb74a9d RDC |
65 | BUG_ON(!mutex_is_locked(&pndevs->lock)); |
66 | list_add_rcu(&pnd->list, &pndevs->list); | |
f8ff6028 RDC |
67 | return pnd; |
68 | } | |
69 | ||
70 | static struct phonet_device *__phonet_get(struct net_device *dev) | |
71 | { | |
9a3b7a42 | 72 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
f8ff6028 RDC |
73 | struct phonet_device *pnd; |
74 | ||
eeb74a9d | 75 | BUG_ON(!mutex_is_locked(&pndevs->lock)); |
9a3b7a42 | 76 | list_for_each_entry(pnd, &pndevs->list, list) { |
f8ff6028 RDC |
77 | if (pnd->netdev == dev) |
78 | return pnd; | |
79 | } | |
80 | return NULL; | |
81 | } | |
82 | ||
eeb74a9d RDC |
83 | static 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 | ||
2be6fa4c | 95 | static void phonet_device_destroy(struct net_device *dev) |
f8ff6028 | 96 | { |
2be6fa4c RDC |
97 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
98 | struct phonet_device *pnd; | |
99 | ||
100 | ASSERT_RTNL(); | |
101 | ||
eeb74a9d | 102 | mutex_lock(&pndevs->lock); |
2be6fa4c RDC |
103 | pnd = __phonet_get(dev); |
104 | if (pnd) | |
eeb74a9d RDC |
105 | list_del_rcu(&pnd->list); |
106 | mutex_unlock(&pndevs->lock); | |
2be6fa4c RDC |
107 | |
108 | if (pnd) { | |
109 | u8 addr; | |
110 | ||
a1ca14ac | 111 | for_each_set_bit(addr, pnd->addrs, 64) |
2be6fa4c RDC |
112 | phonet_address_notify(RTM_DELADDR, dev, addr); |
113 | kfree(pnd); | |
114 | } | |
f8ff6028 RDC |
115 | } |
116 | ||
117 | struct net_device *phonet_device_get(struct net *net) | |
118 | { | |
9a3b7a42 | 119 | struct phonet_device_list *pndevs = phonet_device_list(net); |
f8ff6028 | 120 | struct phonet_device *pnd; |
59e57f44 | 121 | struct net_device *dev = NULL; |
f8ff6028 | 122 | |
eeb74a9d RDC |
123 | rcu_read_lock(); |
124 | list_for_each_entry_rcu(pnd, &pndevs->list, list) { | |
f8ff6028 RDC |
125 | dev = pnd->netdev; |
126 | BUG_ON(!dev); | |
127 | ||
9a3b7a42 | 128 | if ((dev->reg_state == NETREG_REGISTERED) && |
f8ff6028 RDC |
129 | ((pnd->netdev->flags & IFF_UP)) == IFF_UP) |
130 | break; | |
131 | dev = NULL; | |
132 | } | |
133 | if (dev) | |
134 | dev_hold(dev); | |
eeb74a9d | 135 | rcu_read_unlock(); |
f8ff6028 RDC |
136 | return dev; |
137 | } | |
138 | ||
139 | int phonet_address_add(struct net_device *dev, u8 addr) | |
140 | { | |
9a3b7a42 | 141 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
f8ff6028 RDC |
142 | struct phonet_device *pnd; |
143 | int err = 0; | |
144 | ||
eeb74a9d | 145 | mutex_lock(&pndevs->lock); |
f8ff6028 RDC |
146 | /* 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; | |
eeb74a9d | 154 | mutex_unlock(&pndevs->lock); |
f8ff6028 RDC |
155 | return err; |
156 | } | |
157 | ||
158 | int phonet_address_del(struct net_device *dev, u8 addr) | |
159 | { | |
9a3b7a42 | 160 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
f8ff6028 RDC |
161 | struct phonet_device *pnd; |
162 | int err = 0; | |
163 | ||
eeb74a9d | 164 | mutex_lock(&pndevs->lock); |
f8ff6028 | 165 | pnd = __phonet_get(dev); |
eeb74a9d | 166 | if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) { |
f8ff6028 | 167 | err = -EADDRNOTAVAIL; |
eeb74a9d RDC |
168 | 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(); | |
2be6fa4c RDC |
177 | kfree(pnd); |
178 | } | |
f8ff6028 RDC |
179 | return err; |
180 | } | |
181 | ||
182 | /* Gets a source address toward a destination, through a interface. */ | |
55748ac0 | 183 | u8 phonet_address_get(struct net_device *dev, u8 daddr) |
f8ff6028 RDC |
184 | { |
185 | struct phonet_device *pnd; | |
55748ac0 | 186 | u8 saddr; |
f8ff6028 | 187 | |
eeb74a9d RDC |
188 | rcu_read_lock(); |
189 | pnd = __phonet_get_rcu(dev); | |
f8ff6028 RDC |
190 | if (pnd) { |
191 | BUG_ON(bitmap_empty(pnd->addrs, 64)); | |
192 | ||
193 | /* Use same source address as destination, if possible */ | |
55748ac0 RDC |
194 | if (test_bit(daddr >> 2, pnd->addrs)) |
195 | saddr = daddr; | |
196 | else | |
197 | saddr = find_first_bit(pnd->addrs, 64) << 2; | |
f8ff6028 | 198 | } else |
55748ac0 | 199 | saddr = PN_NO_ADDR; |
eeb74a9d | 200 | rcu_read_unlock(); |
55748ac0 RDC |
201 | |
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; | |
f8ff6028 RDC |
214 | } |
215 | ||
52404881 | 216 | int phonet_address_lookup(struct net *net, u8 addr) |
f8ff6028 | 217 | { |
9a3b7a42 | 218 | struct phonet_device_list *pndevs = phonet_device_list(net); |
f8ff6028 | 219 | struct phonet_device *pnd; |
9a3b7a42 | 220 | int err = -EADDRNOTAVAIL; |
f8ff6028 | 221 | |
eeb74a9d RDC |
222 | rcu_read_lock(); |
223 | list_for_each_entry_rcu(pnd, &pndevs->list, list) { | |
f8ff6028 RDC |
224 | /* 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)) { | |
9a3b7a42 | 230 | err = 0; |
231 | goto found; | |
f8ff6028 RDC |
232 | } |
233 | } | |
9a3b7a42 | 234 | found: |
eeb74a9d | 235 | rcu_read_unlock(); |
9a3b7a42 | 236 | return err; |
f8ff6028 RDC |
237 | } |
238 | ||
f5bb1c55 RDC |
239 | /* automatically configure a Phonet device, if supported */ |
240 | static 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; | |
b11b5165 RDC |
252 | |
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; | |
f5bb1c55 RDC |
260 | } |
261 | ||
f062f41d RDC |
262 | static 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); | |
88880135 | 270 | mutex_lock(&pnn->routes.lock); |
f062f41d RDC |
271 | for (i = 0; i < 64; i++) |
272 | if (dev == pnn->routes.table[i]) { | |
88880135 | 273 | rcu_assign_pointer(pnn->routes.table[i], NULL); |
f062f41d | 274 | set_bit(i, deleted); |
f062f41d | 275 | } |
88880135 RDC |
276 | mutex_unlock(&pnn->routes.lock); |
277 | ||
278 | if (bitmap_empty(deleted, 64)) | |
279 | return; /* short-circuit RCU */ | |
280 | synchronize_rcu(); | |
f062f41d | 281 | for (i = find_first_bit(deleted, 64); i < 64; |
88880135 | 282 | i = find_next_bit(deleted, 64, i + 1)) { |
f062f41d | 283 | rtm_phonet_notify(RTM_DELROUTE, dev, i); |
88880135 RDC |
284 | dev_put(dev); |
285 | } | |
f062f41d RDC |
286 | } |
287 | ||
f8ff6028 RDC |
288 | /* notify Phonet of device events */ |
289 | static int phonet_device_notify(struct notifier_block *me, unsigned long what, | |
290 | void *arg) | |
291 | { | |
292 | struct net_device *dev = arg; | |
293 | ||
f5bb1c55 RDC |
294 | switch (what) { |
295 | case NETDEV_REGISTER: | |
296 | if (dev->type == ARPHRD_PHONET) | |
297 | phonet_device_autoconf(dev); | |
298 | break; | |
299 | case NETDEV_UNREGISTER: | |
2be6fa4c | 300 | phonet_device_destroy(dev); |
f062f41d | 301 | phonet_route_autodel(dev); |
f5bb1c55 RDC |
302 | break; |
303 | } | |
f8ff6028 RDC |
304 | return 0; |
305 | ||
306 | } | |
307 | ||
308 | static struct notifier_block phonet_device_notifier = { | |
309 | .notifier_call = phonet_device_notify, | |
310 | .priority = 0, | |
311 | }; | |
312 | ||
9a3b7a42 | 313 | /* Per-namespace Phonet devices handling */ |
2c8c1e72 | 314 | static int __net_init phonet_init_net(struct net *net) |
9a3b7a42 | 315 | { |
d2b3eb63 | 316 | struct phonet_net *pnn = net_generic(net, phonet_net_id); |
9a3b7a42 | 317 | |
d2b3eb63 | 318 | if (!proc_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops)) |
c1dc13e9 | 319 | return -ENOMEM; |
c1dc13e9 | 320 | |
9a3b7a42 | 321 | INIT_LIST_HEAD(&pnn->pndevs.list); |
eeb74a9d | 322 | mutex_init(&pnn->pndevs.lock); |
88880135 | 323 | mutex_init(&pnn->routes.lock); |
9a3b7a42 | 324 | return 0; |
325 | } | |
326 | ||
2c8c1e72 | 327 | static void __net_exit phonet_exit_net(struct net *net) |
9a3b7a42 | 328 | { |
329 | struct phonet_net *pnn = net_generic(net, phonet_net_id); | |
2be6fa4c | 330 | struct net_device *dev; |
f062f41d | 331 | unsigned i; |
9a3b7a42 | 332 | |
2be6fa4c RDC |
333 | rtnl_lock(); |
334 | for_each_netdev(net, dev) | |
335 | phonet_device_destroy(dev); | |
f062f41d RDC |
336 | |
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 | } | |
2be6fa4c | 344 | rtnl_unlock(); |
c1dc13e9 RDC |
345 | |
346 | proc_net_remove(net, "phonet"); | |
9a3b7a42 | 347 | } |
348 | ||
349 | static struct pernet_operations phonet_net_ops = { | |
350 | .init = phonet_init_net, | |
351 | .exit = phonet_exit_net, | |
d2b3eb63 EB |
352 | .id = &phonet_net_id, |
353 | .size = sizeof(struct phonet_net), | |
9a3b7a42 | 354 | }; |
355 | ||
f8ff6028 | 356 | /* Initialize Phonet devices list */ |
76e02cf6 | 357 | int __init phonet_device_init(void) |
f8ff6028 | 358 | { |
d2b3eb63 | 359 | int err = register_pernet_device(&phonet_net_ops); |
9a3b7a42 | 360 | if (err) |
361 | return err; | |
660f706d | 362 | |
f8ff6028 | 363 | register_netdevice_notifier(&phonet_device_notifier); |
660f706d | 364 | err = phonet_netlink_register(); |
365 | if (err) | |
366 | phonet_device_exit(); | |
367 | return err; | |
f8ff6028 RDC |
368 | } |
369 | ||
370 | void phonet_device_exit(void) | |
371 | { | |
f8ff6028 | 372 | rtnl_unregister_all(PF_PHONET); |
6530e0fe | 373 | unregister_netdevice_notifier(&phonet_device_notifier); |
d2b3eb63 | 374 | unregister_pernet_device(&phonet_net_ops); |
f8ff6028 | 375 | } |
55748ac0 RDC |
376 | |
377 | int 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; | |
88880135 | 384 | mutex_lock(&routes->lock); |
55748ac0 | 385 | if (routes->table[daddr] == NULL) { |
88880135 | 386 | rcu_assign_pointer(routes->table[daddr], dev); |
55748ac0 RDC |
387 | dev_hold(dev); |
388 | err = 0; | |
389 | } | |
88880135 | 390 | mutex_unlock(&routes->lock); |
55748ac0 RDC |
391 | return err; |
392 | } | |
393 | ||
394 | int 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; | |
55748ac0 RDC |
398 | |
399 | daddr = daddr >> 2; | |
88880135 RDC |
400 | 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; | |
55748ac0 RDC |
412 | } |
413 | ||
414 | struct 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; | |
88880135 RDC |
423 | rcu_read_lock(); |
424 | dev = rcu_dereference(routes->table[daddr]); | |
425 | rcu_read_unlock(); | |
55748ac0 RDC |
426 | return dev; |
427 | } | |
428 | ||
429 | struct 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 | ||
88880135 RDC |
435 | daddr >>= 2; |
436 | rcu_read_lock(); | |
437 | dev = rcu_dereference(routes->table[daddr]); | |
55748ac0 RDC |
438 | if (dev) |
439 | dev_hold(dev); | |
88880135 | 440 | rcu_read_unlock(); |
55748ac0 RDC |
441 | |
442 | if (!dev) | |
443 | dev = phonet_device_get(net); /* Default route */ | |
444 | return dev; | |
445 | } |