1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/kernel.h>
27#include <linux/net.h>
28#include <linux/slab.h>
29#include <linux/netdevice.h>
30#include <linux/phonet.h>
31#include <linux/proc_fs.h>
32#include <linux/if_arp.h>
33#include <net/sock.h>
34#include <net/netns/generic.h>
35#include <net/phonet/pn_dev.h>
36
37struct phonet_routes {
38 struct mutex lock;
39 struct net_device __rcu *table[64];
40};
41
42struct phonet_net {
43 struct phonet_device_list pndevs;
44 struct phonet_routes routes;
45};
46
47static unsigned int phonet_net_id __read_mostly;
48
49static struct phonet_net *phonet_pernet(struct net *net)
50{
51 BUG_ON(!net);
52
53 return net_generic(net, phonet_net_id);
54}
55
56struct phonet_device_list *phonet_device_list(struct net *net)
57{
58 struct phonet_net *pnn = phonet_pernet(net);
59 return &pnn->pndevs;
60}
61
62
63static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
64{
65 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
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
72 BUG_ON(!mutex_is_locked(&pndevs->lock));
73 list_add_rcu(&pnd->list, &pndevs->list);
74 return pnd;
75}
76
77static struct phonet_device *__phonet_get(struct net_device *dev)
78{
79 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
80 struct phonet_device *pnd;
81
82 BUG_ON(!mutex_is_locked(&pndevs->lock));
83 list_for_each_entry(pnd, &pndevs->list, list) {
84 if (pnd->netdev == dev)
85 return pnd;
86 }
87 return NULL;
88}
89
90static 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
102static void phonet_device_destroy(struct net_device *dev)
103{
104 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
105 struct phonet_device *pnd;
106
107 ASSERT_RTNL();
108
109 mutex_lock(&pndevs->lock);
110 pnd = __phonet_get(dev);
111 if (pnd)
112 list_del_rcu(&pnd->list);
113 mutex_unlock(&pndevs->lock);
114
115 if (pnd) {
116 u8 addr;
117
118 for_each_set_bit(addr, pnd->addrs, 64)
119 phonet_address_notify(RTM_DELADDR, dev, addr);
120 kfree(pnd);
121 }
122}
123
124struct net_device *phonet_device_get(struct net *net)
125{
126 struct phonet_device_list *pndevs = phonet_device_list(net);
127 struct phonet_device *pnd;
128 struct net_device *dev = NULL;
129
130 rcu_read_lock();
131 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
132 dev = pnd->netdev;
133 BUG_ON(!dev);
134
135 if ((dev->reg_state == NETREG_REGISTERED) &&
136 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
137 break;
138 dev = NULL;
139 }
140 if (dev)
141 dev_hold(dev);
142 rcu_read_unlock();
143 return dev;
144}
145
146int phonet_address_add(struct net_device *dev, u8 addr)
147{
148 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
149 struct phonet_device *pnd;
150 int err = 0;
151
152 mutex_lock(&pndevs->lock);
153
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;
161 mutex_unlock(&pndevs->lock);
162 return err;
163}
164
165int phonet_address_del(struct net_device *dev, u8 addr)
166{
167 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
168 struct phonet_device *pnd;
169 int err = 0;
170
171 mutex_lock(&pndevs->lock);
172 pnd = __phonet_get(dev);
173 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) {
174 err = -EADDRNOTAVAIL;
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
182 if (pnd)
183 kfree_rcu(pnd, rcu);
184
185 return err;
186}
187
188
189u8 phonet_address_get(struct net_device *dev, u8 daddr)
190{
191 struct phonet_device *pnd;
192 u8 saddr;
193
194 rcu_read_lock();
195 pnd = __phonet_get_rcu(dev);
196 if (pnd) {
197 BUG_ON(bitmap_empty(pnd->addrs, 64));
198
199
200 if (test_bit(daddr >> 2, pnd->addrs))
201 saddr = daddr;
202 else
203 saddr = find_first_bit(pnd->addrs, 64) << 2;
204 } else
205 saddr = PN_NO_ADDR;
206 rcu_read_unlock();
207
208 if (saddr == PN_NO_ADDR) {
209
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;
220}
221
222int phonet_address_lookup(struct net *net, u8 addr)
223{
224 struct phonet_device_list *pndevs = phonet_device_list(net);
225 struct phonet_device *pnd;
226 int err = -EADDRNOTAVAIL;
227
228 rcu_read_lock();
229 list_for_each_entry_rcu(pnd, &pndevs->list, list) {
230
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)) {
236 err = 0;
237 goto found;
238 }
239 }
240found:
241 rcu_read_unlock();
242 return err;
243}
244
245
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;
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;
266}
267
268static void phonet_route_autodel(struct net_device *dev)
269{
270 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
271 unsigned int i;
272 DECLARE_BITMAP(deleted, 64);
273
274
275 bitmap_zero(deleted, 64);
276 mutex_lock(&pnn->routes.lock);
277 for (i = 0; i < 64; i++)
278 if (rcu_access_pointer(pnn->routes.table[i]) == dev) {
279 RCU_INIT_POINTER(pnn->routes.table[i], NULL);
280 set_bit(i, deleted);
281 }
282 mutex_unlock(&pnn->routes.lock);
283
284 if (bitmap_empty(deleted, 64))
285 return;
286 synchronize_rcu();
287 for_each_set_bit(i, deleted, 64) {
288 rtm_phonet_notify(RTM_DELROUTE, dev, i);
289 dev_put(dev);
290 }
291}
292
293
294static int phonet_device_notify(struct notifier_block *me, unsigned long what,
295 void *ptr)
296{
297 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
298
299 switch (what) {
300 case NETDEV_REGISTER:
301 if (dev->type == ARPHRD_PHONET)
302 phonet_device_autoconf(dev);
303 break;
304 case NETDEV_UNREGISTER:
305 phonet_device_destroy(dev);
306 phonet_route_autodel(dev);
307 break;
308 }
309 return 0;
310
311}
312
313static struct notifier_block phonet_device_notifier = {
314 .notifier_call = phonet_device_notify,
315 .priority = 0,
316};
317
318
319static int __net_init phonet_init_net(struct net *net)
320{
321 struct phonet_net *pnn = phonet_pernet(net);
322
323 if (!proc_create_net("phonet", 0, net->proc_net, &pn_sock_seq_ops,
324 sizeof(struct seq_net_private)))
325 return -ENOMEM;
326
327 INIT_LIST_HEAD(&pnn->pndevs.list);
328 mutex_init(&pnn->pndevs.lock);
329 mutex_init(&pnn->routes.lock);
330 return 0;
331}
332
333static void __net_exit phonet_exit_net(struct net *net)
334{
335 struct phonet_net *pnn = phonet_pernet(net);
336
337 remove_proc_entry("phonet", net->proc_net);
338 WARN_ON_ONCE(!list_empty(&pnn->pndevs.list));
339}
340
341static struct pernet_operations phonet_net_ops = {
342 .init = phonet_init_net,
343 .exit = phonet_exit_net,
344 .id = &phonet_net_id,
345 .size = sizeof(struct phonet_net),
346};
347
348
349int __init phonet_device_init(void)
350{
351 int err = register_pernet_subsys(&phonet_net_ops);
352 if (err)
353 return err;
354
355 proc_create_net("pnresource", 0, init_net.proc_net, &pn_res_seq_ops,
356 sizeof(struct seq_net_private));
357 register_netdevice_notifier(&phonet_device_notifier);
358 err = phonet_netlink_register();
359 if (err)
360 phonet_device_exit();
361 return err;
362}
363
364void phonet_device_exit(void)
365{
366 rtnl_unregister_all(PF_PHONET);
367 unregister_netdevice_notifier(&phonet_device_notifier);
368 unregister_pernet_subsys(&phonet_net_ops);
369 remove_proc_entry("pnresource", init_net.proc_net);
370}
371
372int phonet_route_add(struct net_device *dev, u8 daddr)
373{
374 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
375 struct phonet_routes *routes = &pnn->routes;
376 int err = -EEXIST;
377
378 daddr = daddr >> 2;
379 mutex_lock(&routes->lock);
380 if (routes->table[daddr] == NULL) {
381 rcu_assign_pointer(routes->table[daddr], dev);
382 dev_hold(dev);
383 err = 0;
384 }
385 mutex_unlock(&routes->lock);
386 return err;
387}
388
389int phonet_route_del(struct net_device *dev, u8 daddr)
390{
391 struct phonet_net *pnn = phonet_pernet(dev_net(dev));
392 struct phonet_routes *routes = &pnn->routes;
393
394 daddr = daddr >> 2;
395 mutex_lock(&routes->lock);
396 if (rcu_access_pointer(routes->table[daddr]) == dev)
397 RCU_INIT_POINTER(routes->table[daddr], NULL);
398 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;
407}
408
409struct net_device *phonet_route_get_rcu(struct net *net, u8 daddr)
410{
411 struct phonet_net *pnn = phonet_pernet(net);
412 struct phonet_routes *routes = &pnn->routes;
413 struct net_device *dev;
414
415 daddr >>= 2;
416 dev = rcu_dereference(routes->table[daddr]);
417 return dev;
418}
419
420struct net_device *phonet_route_output(struct net *net, u8 daddr)
421{
422 struct phonet_net *pnn = phonet_pernet(net);
423 struct phonet_routes *routes = &pnn->routes;
424 struct net_device *dev;
425
426 daddr >>= 2;
427 rcu_read_lock();
428 dev = rcu_dereference(routes->table[daddr]);
429 if (dev)
430 dev_hold(dev);
431 rcu_read_unlock();
432
433 if (!dev)
434 dev = phonet_device_get(net);
435 return dev;
436}
437