]>
Commit | Line | Data |
---|---|---|
f8ff6028 RDC |
1 | /* |
2 | * File: pn_dev.c | |
3 | * | |
4 | * Phonet network device | |
5 | * | |
6 | * Copyright (C) 2008 Nokia Corporation. | |
7 | * | |
31fdc555 RDC |
8 | * Authors: Sakari Ailus <sakari.ailus@nokia.com> |
9 | * Rémi Denis-Courmont | |
f8ff6028 RDC |
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; |
79952bca | 39 | struct net_device __rcu *table[64]; |
55748ac0 RDC |
40 | }; |
41 | ||
9a3b7a42 | 42 | struct phonet_net { |
43 | struct phonet_device_list pndevs; | |
55748ac0 | 44 | struct phonet_routes routes; |
f8ff6028 RDC |
45 | }; |
46 | ||
3c490a87 | 47 | static int phonet_net_id __read_mostly; |
9a3b7a42 | 48 | |
0db3f0f4 JP |
49 | static struct phonet_net *phonet_pernet(struct net *net) |
50 | { | |
51 | BUG_ON(!net); | |
52 | ||
53 | return net_generic(net, phonet_net_id); | |
54 | } | |
55 | ||
9a3b7a42 | 56 | struct phonet_device_list *phonet_device_list(struct net *net) |
57 | { | |
0db3f0f4 | 58 | struct phonet_net *pnn = phonet_pernet(net); |
9a3b7a42 | 59 | return &pnn->pndevs; |
60 | } | |
61 | ||
f8ff6028 RDC |
62 | /* Allocate new Phonet device. */ |
63 | static struct phonet_device *__phonet_device_alloc(struct net_device *dev) | |
64 | { | |
9a3b7a42 | 65 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
f8ff6028 RDC |
66 | 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 | ||
eeb74a9d RDC |
72 | BUG_ON(!mutex_is_locked(&pndevs->lock)); |
73 | list_add_rcu(&pnd->list, &pndevs->list); | |
f8ff6028 RDC |
74 | return pnd; |
75 | } | |
76 | ||
77 | static struct phonet_device *__phonet_get(struct net_device *dev) | |
78 | { | |
9a3b7a42 | 79 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
f8ff6028 RDC |
80 | struct phonet_device *pnd; |
81 | ||
eeb74a9d | 82 | BUG_ON(!mutex_is_locked(&pndevs->lock)); |
9a3b7a42 | 83 | list_for_each_entry(pnd, &pndevs->list, list) { |
f8ff6028 RDC |
84 | if (pnd->netdev == dev) |
85 | return pnd; | |
86 | } | |
87 | return NULL; | |
88 | } | |
89 | ||
eeb74a9d RDC |
90 | static 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 | ||
2be6fa4c | 102 | static void phonet_device_destroy(struct net_device *dev) |
f8ff6028 | 103 | { |
2be6fa4c RDC |
104 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
105 | struct phonet_device *pnd; | |
106 | ||
107 | ASSERT_RTNL(); | |
108 | ||
eeb74a9d | 109 | mutex_lock(&pndevs->lock); |
2be6fa4c RDC |
110 | pnd = __phonet_get(dev); |
111 | if (pnd) | |
eeb74a9d RDC |
112 | list_del_rcu(&pnd->list); |
113 | mutex_unlock(&pndevs->lock); | |
2be6fa4c RDC |
114 | |
115 | if (pnd) { | |
116 | u8 addr; | |
117 | ||
a1ca14ac | 118 | for_each_set_bit(addr, pnd->addrs, 64) |
2be6fa4c RDC |
119 | phonet_address_notify(RTM_DELADDR, dev, addr); |
120 | kfree(pnd); | |
121 | } | |
f8ff6028 RDC |
122 | } |
123 | ||
124 | struct net_device *phonet_device_get(struct net *net) | |
125 | { | |
9a3b7a42 | 126 | struct phonet_device_list *pndevs = phonet_device_list(net); |
f8ff6028 | 127 | struct phonet_device *pnd; |
59e57f44 | 128 | struct net_device *dev = NULL; |
f8ff6028 | 129 | |
eeb74a9d RDC |
130 | rcu_read_lock(); |
131 | list_for_each_entry_rcu(pnd, &pndevs->list, list) { | |
f8ff6028 RDC |
132 | dev = pnd->netdev; |
133 | BUG_ON(!dev); | |
134 | ||
9a3b7a42 | 135 | if ((dev->reg_state == NETREG_REGISTERED) && |
f8ff6028 RDC |
136 | ((pnd->netdev->flags & IFF_UP)) == IFF_UP) |
137 | break; | |
138 | dev = NULL; | |
139 | } | |
140 | if (dev) | |
141 | dev_hold(dev); | |
eeb74a9d | 142 | rcu_read_unlock(); |
f8ff6028 RDC |
143 | return dev; |
144 | } | |
145 | ||
146 | int phonet_address_add(struct net_device *dev, u8 addr) | |
147 | { | |
9a3b7a42 | 148 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
f8ff6028 RDC |
149 | struct phonet_device *pnd; |
150 | int err = 0; | |
151 | ||
eeb74a9d | 152 | mutex_lock(&pndevs->lock); |
f8ff6028 RDC |
153 | /* 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; | |
eeb74a9d | 161 | mutex_unlock(&pndevs->lock); |
f8ff6028 RDC |
162 | return err; |
163 | } | |
164 | ||
165 | int phonet_address_del(struct net_device *dev, u8 addr) | |
166 | { | |
9a3b7a42 | 167 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
f8ff6028 RDC |
168 | struct phonet_device *pnd; |
169 | int err = 0; | |
170 | ||
eeb74a9d | 171 | mutex_lock(&pndevs->lock); |
f8ff6028 | 172 | pnd = __phonet_get(dev); |
eeb74a9d | 173 | if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) { |
f8ff6028 | 174 | err = -EADDRNOTAVAIL; |
eeb74a9d RDC |
175 | 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 | ||
88e7594a | 182 | if (pnd) |
7e113a9c | 183 | kfree_rcu(pnd, rcu); |
88e7594a | 184 | |
f8ff6028 RDC |
185 | return err; |
186 | } | |
187 | ||
188 | /* Gets a source address toward a destination, through a interface. */ | |
55748ac0 | 189 | u8 phonet_address_get(struct net_device *dev, u8 daddr) |
f8ff6028 RDC |
190 | { |
191 | struct phonet_device *pnd; | |
55748ac0 | 192 | u8 saddr; |
f8ff6028 | 193 | |
eeb74a9d RDC |
194 | rcu_read_lock(); |
195 | pnd = __phonet_get_rcu(dev); | |
f8ff6028 RDC |
196 | if (pnd) { |
197 | BUG_ON(bitmap_empty(pnd->addrs, 64)); | |
198 | ||
199 | /* Use same source address as destination, if possible */ | |
55748ac0 RDC |
200 | if (test_bit(daddr >> 2, pnd->addrs)) |
201 | saddr = daddr; | |
202 | else | |
203 | saddr = find_first_bit(pnd->addrs, 64) << 2; | |
f8ff6028 | 204 | } else |
55748ac0 | 205 | saddr = PN_NO_ADDR; |
eeb74a9d | 206 | rcu_read_unlock(); |
55748ac0 RDC |
207 | |
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; | |
f8ff6028 RDC |
220 | } |
221 | ||
52404881 | 222 | int phonet_address_lookup(struct net *net, u8 addr) |
f8ff6028 | 223 | { |
9a3b7a42 | 224 | struct phonet_device_list *pndevs = phonet_device_list(net); |
f8ff6028 | 225 | struct phonet_device *pnd; |
9a3b7a42 | 226 | int err = -EADDRNOTAVAIL; |
f8ff6028 | 227 | |
eeb74a9d RDC |
228 | rcu_read_lock(); |
229 | list_for_each_entry_rcu(pnd, &pndevs->list, list) { | |
f8ff6028 RDC |
230 | /* 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)) { | |
9a3b7a42 | 236 | err = 0; |
237 | goto found; | |
f8ff6028 RDC |
238 | } |
239 | } | |
9a3b7a42 | 240 | found: |
eeb74a9d | 241 | rcu_read_unlock(); |
9a3b7a42 | 242 | return err; |
f8ff6028 RDC |
243 | } |
244 | ||
f5bb1c55 RDC |
245 | /* automatically configure a Phonet device, if supported */ |
246 | static 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; | |
b11b5165 RDC |
258 | |
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; | |
f5bb1c55 RDC |
266 | } |
267 | ||
f062f41d RDC |
268 | static void phonet_route_autodel(struct net_device *dev) |
269 | { | |
0db3f0f4 | 270 | struct phonet_net *pnn = phonet_pernet(dev_net(dev)); |
95c96174 | 271 | unsigned int i; |
f062f41d RDC |
272 | DECLARE_BITMAP(deleted, 64); |
273 | ||
274 | /* Remove left-over Phonet routes */ | |
275 | bitmap_zero(deleted, 64); | |
88880135 | 276 | mutex_lock(&pnn->routes.lock); |
f062f41d | 277 | for (i = 0; i < 64; i++) |
79952bca | 278 | if (rcu_access_pointer(pnn->routes.table[i]) == dev) { |
a9b3cd7f | 279 | RCU_INIT_POINTER(pnn->routes.table[i], NULL); |
f062f41d | 280 | set_bit(i, deleted); |
f062f41d | 281 | } |
88880135 RDC |
282 | mutex_unlock(&pnn->routes.lock); |
283 | ||
284 | if (bitmap_empty(deleted, 64)) | |
285 | return; /* short-circuit RCU */ | |
286 | synchronize_rcu(); | |
6a499b24 | 287 | for_each_set_bit(i, deleted, 64) { |
f062f41d | 288 | rtm_phonet_notify(RTM_DELROUTE, dev, i); |
88880135 RDC |
289 | dev_put(dev); |
290 | } | |
f062f41d RDC |
291 | } |
292 | ||
f8ff6028 RDC |
293 | /* notify Phonet of device events */ |
294 | static int phonet_device_notify(struct notifier_block *me, unsigned long what, | |
351638e7 | 295 | void *ptr) |
f8ff6028 | 296 | { |
351638e7 | 297 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
f8ff6028 | 298 | |
f5bb1c55 RDC |
299 | switch (what) { |
300 | case NETDEV_REGISTER: | |
301 | if (dev->type == ARPHRD_PHONET) | |
302 | phonet_device_autoconf(dev); | |
303 | break; | |
304 | case NETDEV_UNREGISTER: | |
2be6fa4c | 305 | phonet_device_destroy(dev); |
f062f41d | 306 | phonet_route_autodel(dev); |
f5bb1c55 RDC |
307 | break; |
308 | } | |
f8ff6028 RDC |
309 | return 0; |
310 | ||
311 | } | |
312 | ||
313 | static struct notifier_block phonet_device_notifier = { | |
314 | .notifier_call = phonet_device_notify, | |
315 | .priority = 0, | |
316 | }; | |
317 | ||
9a3b7a42 | 318 | /* Per-namespace Phonet devices handling */ |
2c8c1e72 | 319 | static int __net_init phonet_init_net(struct net *net) |
9a3b7a42 | 320 | { |
0db3f0f4 | 321 | struct phonet_net *pnn = phonet_pernet(net); |
9a3b7a42 | 322 | |
d4beaa66 | 323 | if (!proc_create("phonet", 0, net->proc_net, &pn_sock_seq_fops)) |
c1dc13e9 | 324 | return -ENOMEM; |
c1dc13e9 | 325 | |
9a3b7a42 | 326 | INIT_LIST_HEAD(&pnn->pndevs.list); |
eeb74a9d | 327 | mutex_init(&pnn->pndevs.lock); |
88880135 | 328 | mutex_init(&pnn->routes.lock); |
9a3b7a42 | 329 | return 0; |
330 | } | |
331 | ||
2c8c1e72 | 332 | static void __net_exit phonet_exit_net(struct net *net) |
9a3b7a42 | 333 | { |
ece31ffd | 334 | remove_proc_entry("phonet", net->proc_net); |
9a3b7a42 | 335 | } |
336 | ||
337 | static struct pernet_operations phonet_net_ops = { | |
338 | .init = phonet_init_net, | |
339 | .exit = phonet_exit_net, | |
d2b3eb63 EB |
340 | .id = &phonet_net_id, |
341 | .size = sizeof(struct phonet_net), | |
9a3b7a42 | 342 | }; |
343 | ||
f8ff6028 | 344 | /* Initialize Phonet devices list */ |
76e02cf6 | 345 | int __init phonet_device_init(void) |
f8ff6028 | 346 | { |
03478756 | 347 | int err = register_pernet_subsys(&phonet_net_ops); |
9a3b7a42 | 348 | if (err) |
349 | return err; | |
660f706d | 350 | |
d4beaa66 | 351 | proc_create("pnresource", 0, init_net.proc_net, &pn_res_seq_fops); |
f8ff6028 | 352 | register_netdevice_notifier(&phonet_device_notifier); |
660f706d | 353 | err = phonet_netlink_register(); |
354 | if (err) | |
355 | phonet_device_exit(); | |
356 | return err; | |
f8ff6028 RDC |
357 | } |
358 | ||
359 | void phonet_device_exit(void) | |
360 | { | |
f8ff6028 | 361 | rtnl_unregister_all(PF_PHONET); |
6530e0fe | 362 | unregister_netdevice_notifier(&phonet_device_notifier); |
03478756 | 363 | unregister_pernet_subsys(&phonet_net_ops); |
ece31ffd | 364 | remove_proc_entry("pnresource", init_net.proc_net); |
f8ff6028 | 365 | } |
55748ac0 RDC |
366 | |
367 | int phonet_route_add(struct net_device *dev, u8 daddr) | |
368 | { | |
0db3f0f4 | 369 | struct phonet_net *pnn = phonet_pernet(dev_net(dev)); |
55748ac0 RDC |
370 | struct phonet_routes *routes = &pnn->routes; |
371 | int err = -EEXIST; | |
372 | ||
373 | daddr = daddr >> 2; | |
88880135 | 374 | mutex_lock(&routes->lock); |
55748ac0 | 375 | if (routes->table[daddr] == NULL) { |
cf778b00 | 376 | rcu_assign_pointer(routes->table[daddr], dev); |
55748ac0 RDC |
377 | dev_hold(dev); |
378 | err = 0; | |
379 | } | |
88880135 | 380 | mutex_unlock(&routes->lock); |
55748ac0 RDC |
381 | return err; |
382 | } | |
383 | ||
384 | int phonet_route_del(struct net_device *dev, u8 daddr) | |
385 | { | |
0db3f0f4 | 386 | struct phonet_net *pnn = phonet_pernet(dev_net(dev)); |
55748ac0 | 387 | struct phonet_routes *routes = &pnn->routes; |
55748ac0 RDC |
388 | |
389 | daddr = daddr >> 2; | |
88880135 | 390 | mutex_lock(&routes->lock); |
79952bca | 391 | if (rcu_access_pointer(routes->table[daddr]) == dev) |
a9b3cd7f | 392 | RCU_INIT_POINTER(routes->table[daddr], NULL); |
88880135 RDC |
393 | else |
394 | dev = NULL; | |
395 | mutex_unlock(&routes->lock); | |
396 | ||
397 | if (!dev) | |
398 | return -ENOENT; | |
399 | synchronize_rcu(); | |
400 | dev_put(dev); | |
401 | return 0; | |
55748ac0 RDC |
402 | } |
403 | ||
e67f88dd | 404 | struct net_device *phonet_route_get_rcu(struct net *net, u8 daddr) |
55748ac0 | 405 | { |
0db3f0f4 | 406 | struct phonet_net *pnn = phonet_pernet(net); |
55748ac0 RDC |
407 | struct phonet_routes *routes = &pnn->routes; |
408 | struct net_device *dev; | |
409 | ||
55748ac0 | 410 | daddr >>= 2; |
88880135 | 411 | dev = rcu_dereference(routes->table[daddr]); |
55748ac0 RDC |
412 | return dev; |
413 | } | |
414 | ||
415 | struct net_device *phonet_route_output(struct net *net, u8 daddr) | |
416 | { | |
0db3f0f4 | 417 | struct phonet_net *pnn = phonet_pernet(net); |
55748ac0 RDC |
418 | struct phonet_routes *routes = &pnn->routes; |
419 | struct net_device *dev; | |
420 | ||
88880135 RDC |
421 | daddr >>= 2; |
422 | rcu_read_lock(); | |
423 | dev = rcu_dereference(routes->table[daddr]); | |
55748ac0 RDC |
424 | if (dev) |
425 | dev_hold(dev); | |
88880135 | 426 | rcu_read_unlock(); |
55748ac0 RDC |
427 | |
428 | if (!dev) | |
429 | dev = phonet_device_get(net); /* Default route */ | |
430 | return dev; | |
431 | } |