1 // SPDX-License-Identifier: GPL-2.0-only
5 * Phonet network device
7 * Copyright (C) 2008 Nokia Corporation.
9 * Authors: Sakari Ailus <sakari.ailus@nokia.com>
10 * RĂ©mi Denis-Courmont
13 #include <linux/kernel.h>
14 #include <linux/net.h>
15 #include <linux/slab.h>
16 #include <linux/netdevice.h>
17 #include <linux/phonet.h>
18 #include <linux/proc_fs.h>
19 #include <linux/if_arp.h>
21 #include <net/netns/generic.h>
22 #include <net/phonet/pn_dev.h>
24 struct phonet_routes
{
26 struct net_device __rcu
*table
[64];
30 struct phonet_device_list pndevs
;
31 struct phonet_routes routes
;
34 static unsigned int phonet_net_id __read_mostly
;
36 static struct phonet_net
*phonet_pernet(struct net
*net
)
38 return net_generic(net
, phonet_net_id
);
41 struct phonet_device_list
*phonet_device_list(struct net
*net
)
43 struct phonet_net
*pnn
= phonet_pernet(net
);
47 /* Allocate new Phonet device. */
48 static struct phonet_device
*__phonet_device_alloc(struct net_device
*dev
)
50 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
51 struct phonet_device
*pnd
= kmalloc(sizeof(*pnd
), GFP_ATOMIC
);
55 bitmap_zero(pnd
->addrs
, 64);
57 lockdep_assert_held(&pndevs
->lock
);
58 list_add_rcu(&pnd
->list
, &pndevs
->list
);
62 static struct phonet_device
*__phonet_get(struct net_device
*dev
)
64 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
65 struct phonet_device
*pnd
;
67 lockdep_assert_held(&pndevs
->lock
);
69 list_for_each_entry(pnd
, &pndevs
->list
, list
) {
70 if (pnd
->netdev
== dev
)
76 static struct phonet_device
*__phonet_get_rcu(struct net_device
*dev
)
78 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
79 struct phonet_device
*pnd
;
81 list_for_each_entry_rcu(pnd
, &pndevs
->list
, list
) {
82 if (pnd
->netdev
== dev
)
88 static void phonet_device_destroy(struct net_device
*dev
)
90 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
91 struct phonet_device
*pnd
;
95 spin_lock(&pndevs
->lock
);
97 pnd
= __phonet_get(dev
);
99 list_del_rcu(&pnd
->list
);
101 spin_unlock(&pndevs
->lock
);
104 struct net
*net
= dev_net(dev
);
105 u32 ifindex
= dev
->ifindex
;
108 for_each_set_bit(addr
, pnd
->addrs
, 64)
109 phonet_address_notify(net
, RTM_DELADDR
, ifindex
, addr
);
115 struct net_device
*phonet_device_get(struct net
*net
)
117 struct phonet_device_list
*pndevs
= phonet_device_list(net
);
118 struct phonet_device
*pnd
;
119 struct net_device
*dev
= NULL
;
122 list_for_each_entry_rcu(pnd
, &pndevs
->list
, list
) {
126 if ((dev
->reg_state
== NETREG_REGISTERED
) &&
127 ((pnd
->netdev
->flags
& IFF_UP
)) == IFF_UP
)
136 int phonet_address_add(struct net_device
*dev
, u8 addr
)
138 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
139 struct phonet_device
*pnd
;
142 spin_lock(&pndevs
->lock
);
144 /* Find or create Phonet-specific device data */
145 pnd
= __phonet_get(dev
);
147 pnd
= __phonet_device_alloc(dev
);
148 if (unlikely(pnd
== NULL
))
150 else if (test_and_set_bit(addr
>> 2, pnd
->addrs
))
153 spin_unlock(&pndevs
->lock
);
158 int phonet_address_del(struct net_device
*dev
, u8 addr
)
160 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
161 struct phonet_device
*pnd
;
164 spin_lock(&pndevs
->lock
);
166 pnd
= __phonet_get(dev
);
167 if (!pnd
|| !test_and_clear_bit(addr
>> 2, pnd
->addrs
)) {
168 err
= -EADDRNOTAVAIL
;
170 } else if (bitmap_empty(pnd
->addrs
, 64))
171 list_del_rcu(&pnd
->list
);
175 spin_unlock(&pndevs
->lock
);
183 /* Gets a source address toward a destination, through a interface. */
184 u8
phonet_address_get(struct net_device
*dev
, u8 daddr
)
186 struct phonet_device
*pnd
;
190 pnd
= __phonet_get_rcu(dev
);
192 BUG_ON(bitmap_empty(pnd
->addrs
, 64));
194 /* Use same source address as destination, if possible */
195 if (test_bit(daddr
>> 2, pnd
->addrs
))
198 saddr
= find_first_bit(pnd
->addrs
, 64) << 2;
203 if (saddr
== PN_NO_ADDR
) {
204 /* Fallback to another device */
205 struct net_device
*def_dev
;
207 def_dev
= phonet_device_get(dev_net(dev
));
210 saddr
= phonet_address_get(def_dev
, daddr
);
217 int phonet_address_lookup(struct net
*net
, u8 addr
)
219 struct phonet_device_list
*pndevs
= phonet_device_list(net
);
220 struct phonet_device
*pnd
;
221 int err
= -EADDRNOTAVAIL
;
224 list_for_each_entry_rcu(pnd
, &pndevs
->list
, list
) {
225 /* Don't allow unregistering devices! */
226 if ((pnd
->netdev
->reg_state
!= NETREG_REGISTERED
) ||
227 ((pnd
->netdev
->flags
& IFF_UP
)) != IFF_UP
)
230 if (test_bit(addr
>> 2, pnd
->addrs
)) {
240 /* automatically configure a Phonet device, if supported */
241 static int phonet_device_autoconf(struct net_device
*dev
)
243 struct if_phonet_req req
;
246 if (!dev
->netdev_ops
->ndo_siocdevprivate
)
249 ret
= dev
->netdev_ops
->ndo_siocdevprivate(dev
, (struct ifreq
*)&req
,
250 NULL
, SIOCPNGAUTOCONF
);
255 ret
= phonet_address_add(dev
, req
.ifr_phonet_autoconf
.device
);
259 phonet_address_notify(dev_net(dev
), RTM_NEWADDR
, dev
->ifindex
,
260 req
.ifr_phonet_autoconf
.device
);
264 static void phonet_route_autodel(struct net_device
*dev
)
266 struct net
*net
= dev_net(dev
);
267 DECLARE_BITMAP(deleted
, 64);
268 u32 ifindex
= dev
->ifindex
;
269 struct phonet_net
*pnn
;
272 pnn
= phonet_pernet(net
);
274 /* Remove left-over Phonet routes */
275 bitmap_zero(deleted
, 64);
277 spin_lock(&pnn
->routes
.lock
);
278 for (i
= 0; i
< 64; i
++) {
279 if (rcu_access_pointer(pnn
->routes
.table
[i
]) == dev
) {
280 RCU_INIT_POINTER(pnn
->routes
.table
[i
], NULL
);
284 spin_unlock(&pnn
->routes
.lock
);
286 if (bitmap_empty(deleted
, 64))
287 return; /* short-circuit RCU */
289 for_each_set_bit(i
, deleted
, 64) {
290 rtm_phonet_notify(net
, RTM_DELROUTE
, ifindex
, i
);
295 /* notify Phonet of device events */
296 static int phonet_device_notify(struct notifier_block
*me
, unsigned long what
,
299 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
302 case NETDEV_REGISTER
:
303 if (dev
->type
== ARPHRD_PHONET
)
304 phonet_device_autoconf(dev
);
306 case NETDEV_UNREGISTER
:
307 phonet_device_destroy(dev
);
308 phonet_route_autodel(dev
);
315 static struct notifier_block phonet_device_notifier
= {
316 .notifier_call
= phonet_device_notify
,
320 /* Per-namespace Phonet devices handling */
321 static int __net_init
phonet_init_net(struct net
*net
)
323 struct phonet_net
*pnn
= phonet_pernet(net
);
325 if (!proc_create_net("phonet", 0, net
->proc_net
, &pn_sock_seq_ops
,
326 sizeof(struct seq_net_private
)))
329 INIT_LIST_HEAD(&pnn
->pndevs
.list
);
330 spin_lock_init(&pnn
->pndevs
.lock
);
331 spin_lock_init(&pnn
->routes
.lock
);
335 static void __net_exit
phonet_exit_net(struct net
*net
)
337 struct phonet_net
*pnn
= phonet_pernet(net
);
339 remove_proc_entry("phonet", net
->proc_net
);
340 WARN_ON_ONCE(!list_empty(&pnn
->pndevs
.list
));
343 static struct pernet_operations phonet_net_ops
= {
344 .init
= phonet_init_net
,
345 .exit
= phonet_exit_net
,
346 .id
= &phonet_net_id
,
347 .size
= sizeof(struct phonet_net
),
350 /* Initialize Phonet devices list */
351 int __init
phonet_device_init(void)
353 int err
= register_pernet_subsys(&phonet_net_ops
);
357 proc_create_net("pnresource", 0, init_net
.proc_net
, &pn_res_seq_ops
,
358 sizeof(struct seq_net_private
));
359 register_netdevice_notifier(&phonet_device_notifier
);
360 err
= phonet_netlink_register();
362 phonet_device_exit();
366 void phonet_device_exit(void)
368 rtnl_unregister_all(PF_PHONET
);
369 unregister_netdevice_notifier(&phonet_device_notifier
);
370 unregister_pernet_subsys(&phonet_net_ops
);
371 remove_proc_entry("pnresource", init_net
.proc_net
);
374 int phonet_route_add(struct net_device
*dev
, u8 daddr
)
376 struct phonet_net
*pnn
= phonet_pernet(dev_net(dev
));
377 struct phonet_routes
*routes
= &pnn
->routes
;
382 spin_lock(&routes
->lock
);
383 if (routes
->table
[daddr
] == NULL
) {
384 rcu_assign_pointer(routes
->table
[daddr
], dev
);
388 spin_unlock(&routes
->lock
);
393 int phonet_route_del(struct net_device
*dev
, u8 daddr
)
395 struct phonet_net
*pnn
= phonet_pernet(dev_net(dev
));
396 struct phonet_routes
*routes
= &pnn
->routes
;
400 spin_lock(&routes
->lock
);
401 if (rcu_access_pointer(routes
->table
[daddr
]) == dev
)
402 RCU_INIT_POINTER(routes
->table
[daddr
], NULL
);
405 spin_unlock(&routes
->lock
);
410 /* Note : our caller must call synchronize_rcu() and dev_put(dev) */
415 struct net_device
*phonet_route_get_rcu(struct net
*net
, u8 daddr
)
417 struct phonet_net
*pnn
= phonet_pernet(net
);
418 struct phonet_routes
*routes
= &pnn
->routes
;
419 struct net_device
*dev
;
422 dev
= rcu_dereference(routes
->table
[daddr
]);
426 struct net_device
*phonet_route_output(struct net
*net
, u8 daddr
)
428 struct phonet_net
*pnn
= phonet_pernet(net
);
429 struct phonet_routes
*routes
= &pnn
->routes
;
430 struct net_device
*dev
;
434 dev
= rcu_dereference(routes
->table
[daddr
]);
439 dev
= phonet_device_get(net
); /* Default route */