4 * Phonet network device
6 * Copyright (C) 2008 Nokia Corporation.
8 * Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
9 * Original author: Sakari Ailus <sakari.ailus@nokia.com>
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.
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.
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
26 #include <linux/kernel.h>
27 #include <linux/net.h>
28 #include <linux/netdevice.h>
29 #include <linux/phonet.h>
30 #include <linux/proc_fs.h>
31 #include <linux/if_arp.h>
33 #include <net/netns/generic.h>
34 #include <net/phonet/pn_dev.h>
36 struct phonet_routes
{
38 struct net_device
*table
[64];
42 struct phonet_device_list pndevs
;
43 struct phonet_routes routes
;
46 int phonet_net_id __read_mostly
;
48 struct phonet_device_list
*phonet_device_list(struct net
*net
)
50 struct phonet_net
*pnn
= net_generic(net
, phonet_net_id
);
54 /* Allocate new Phonet device. */
55 static struct phonet_device
*__phonet_device_alloc(struct net_device
*dev
)
57 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
58 struct phonet_device
*pnd
= kmalloc(sizeof(*pnd
), GFP_ATOMIC
);
62 bitmap_zero(pnd
->addrs
, 64);
64 BUG_ON(!mutex_is_locked(&pndevs
->lock
));
65 list_add_rcu(&pnd
->list
, &pndevs
->list
);
69 static struct phonet_device
*__phonet_get(struct net_device
*dev
)
71 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
72 struct phonet_device
*pnd
;
74 BUG_ON(!mutex_is_locked(&pndevs
->lock
));
75 list_for_each_entry(pnd
, &pndevs
->list
, list
) {
76 if (pnd
->netdev
== dev
)
82 static struct phonet_device
*__phonet_get_rcu(struct net_device
*dev
)
84 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
85 struct phonet_device
*pnd
;
87 list_for_each_entry_rcu(pnd
, &pndevs
->list
, list
) {
88 if (pnd
->netdev
== dev
)
94 static void phonet_device_destroy(struct net_device
*dev
)
96 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
97 struct phonet_device
*pnd
;
101 mutex_lock(&pndevs
->lock
);
102 pnd
= __phonet_get(dev
);
104 list_del_rcu(&pnd
->list
);
105 mutex_unlock(&pndevs
->lock
);
110 for (addr
= find_first_bit(pnd
->addrs
, 64); addr
< 64;
111 addr
= find_next_bit(pnd
->addrs
, 64, 1+addr
))
112 phonet_address_notify(RTM_DELADDR
, dev
, addr
);
117 struct net_device
*phonet_device_get(struct net
*net
)
119 struct phonet_device_list
*pndevs
= phonet_device_list(net
);
120 struct phonet_device
*pnd
;
121 struct net_device
*dev
= NULL
;
124 list_for_each_entry_rcu(pnd
, &pndevs
->list
, list
) {
128 if ((dev
->reg_state
== NETREG_REGISTERED
) &&
129 ((pnd
->netdev
->flags
& IFF_UP
)) == IFF_UP
)
139 int phonet_address_add(struct net_device
*dev
, u8 addr
)
141 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
142 struct phonet_device
*pnd
;
145 mutex_lock(&pndevs
->lock
);
146 /* Find or create Phonet-specific device data */
147 pnd
= __phonet_get(dev
);
149 pnd
= __phonet_device_alloc(dev
);
150 if (unlikely(pnd
== NULL
))
152 else if (test_and_set_bit(addr
>> 2, pnd
->addrs
))
154 mutex_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 mutex_lock(&pndevs
->lock
);
165 pnd
= __phonet_get(dev
);
166 if (!pnd
|| !test_and_clear_bit(addr
>> 2, pnd
->addrs
)) {
167 err
= -EADDRNOTAVAIL
;
169 } else if (bitmap_empty(pnd
->addrs
, 64))
170 list_del_rcu(&pnd
->list
);
173 mutex_unlock(&pndevs
->lock
);
182 /* Gets a source address toward a destination, through a interface. */
183 u8
phonet_address_get(struct net_device
*dev
, u8 daddr
)
185 struct phonet_device
*pnd
;
189 pnd
= __phonet_get_rcu(dev
);
191 BUG_ON(bitmap_empty(pnd
->addrs
, 64));
193 /* Use same source address as destination, if possible */
194 if (test_bit(daddr
>> 2, pnd
->addrs
))
197 saddr
= find_first_bit(pnd
->addrs
, 64) << 2;
202 if (saddr
== PN_NO_ADDR
) {
203 /* Fallback to another device */
204 struct net_device
*def_dev
;
206 def_dev
= phonet_device_get(dev_net(dev
));
209 saddr
= phonet_address_get(def_dev
, daddr
);
216 int phonet_address_lookup(struct net
*net
, u8 addr
)
218 struct phonet_device_list
*pndevs
= phonet_device_list(net
);
219 struct phonet_device
*pnd
;
220 int err
= -EADDRNOTAVAIL
;
223 list_for_each_entry_rcu(pnd
, &pndevs
->list
, list
) {
224 /* Don't allow unregistering devices! */
225 if ((pnd
->netdev
->reg_state
!= NETREG_REGISTERED
) ||
226 ((pnd
->netdev
->flags
& IFF_UP
)) != IFF_UP
)
229 if (test_bit(addr
>> 2, pnd
->addrs
)) {
239 /* automatically configure a Phonet device, if supported */
240 static int phonet_device_autoconf(struct net_device
*dev
)
242 struct if_phonet_req req
;
245 if (!dev
->netdev_ops
->ndo_do_ioctl
)
248 ret
= dev
->netdev_ops
->ndo_do_ioctl(dev
, (struct ifreq
*)&req
,
254 ret
= phonet_address_add(dev
, req
.ifr_phonet_autoconf
.device
);
257 phonet_address_notify(RTM_NEWADDR
, dev
,
258 req
.ifr_phonet_autoconf
.device
);
262 static void phonet_route_autodel(struct net_device
*dev
)
264 struct phonet_net
*pnn
= net_generic(dev_net(dev
), phonet_net_id
);
266 DECLARE_BITMAP(deleted
, 64);
268 /* Remove left-over Phonet routes */
269 bitmap_zero(deleted
, 64);
270 mutex_lock(&pnn
->routes
.lock
);
271 for (i
= 0; i
< 64; i
++)
272 if (dev
== pnn
->routes
.table
[i
]) {
273 rcu_assign_pointer(pnn
->routes
.table
[i
], NULL
);
276 mutex_unlock(&pnn
->routes
.lock
);
278 if (bitmap_empty(deleted
, 64))
279 return; /* short-circuit RCU */
281 for (i
= find_first_bit(deleted
, 64); i
< 64;
282 i
= find_next_bit(deleted
, 64, i
+ 1)) {
283 rtm_phonet_notify(RTM_DELROUTE
, dev
, i
);
288 /* notify Phonet of device events */
289 static int phonet_device_notify(struct notifier_block
*me
, unsigned long what
,
292 struct net_device
*dev
= arg
;
295 case NETDEV_REGISTER
:
296 if (dev
->type
== ARPHRD_PHONET
)
297 phonet_device_autoconf(dev
);
299 case NETDEV_UNREGISTER
:
300 phonet_device_destroy(dev
);
301 phonet_route_autodel(dev
);
308 static struct notifier_block phonet_device_notifier
= {
309 .notifier_call
= phonet_device_notify
,
313 /* Per-namespace Phonet devices handling */
314 static int __net_init
phonet_init_net(struct net
*net
)
316 struct phonet_net
*pnn
= net_generic(net
, phonet_net_id
);
318 if (!proc_net_fops_create(net
, "phonet", 0, &pn_sock_seq_fops
))
321 INIT_LIST_HEAD(&pnn
->pndevs
.list
);
322 mutex_init(&pnn
->pndevs
.lock
);
323 mutex_init(&pnn
->routes
.lock
);
327 static void __net_exit
phonet_exit_net(struct net
*net
)
329 struct phonet_net
*pnn
= net_generic(net
, phonet_net_id
);
330 struct net_device
*dev
;
334 for_each_netdev(net
, dev
)
335 phonet_device_destroy(dev
);
337 for (i
= 0; i
< 64; i
++) {
338 dev
= pnn
->routes
.table
[i
];
340 rtm_phonet_notify(RTM_DELROUTE
, dev
, i
);
346 proc_net_remove(net
, "phonet");
349 static struct pernet_operations phonet_net_ops
= {
350 .init
= phonet_init_net
,
351 .exit
= phonet_exit_net
,
352 .id
= &phonet_net_id
,
353 .size
= sizeof(struct phonet_net
),
356 /* Initialize Phonet devices list */
357 int __init
phonet_device_init(void)
359 int err
= register_pernet_device(&phonet_net_ops
);
363 register_netdevice_notifier(&phonet_device_notifier
);
364 err
= phonet_netlink_register();
366 phonet_device_exit();
370 void phonet_device_exit(void)
372 rtnl_unregister_all(PF_PHONET
);
373 unregister_netdevice_notifier(&phonet_device_notifier
);
374 unregister_pernet_device(&phonet_net_ops
);
377 int phonet_route_add(struct net_device
*dev
, u8 daddr
)
379 struct phonet_net
*pnn
= net_generic(dev_net(dev
), phonet_net_id
);
380 struct phonet_routes
*routes
= &pnn
->routes
;
384 mutex_lock(&routes
->lock
);
385 if (routes
->table
[daddr
] == NULL
) {
386 rcu_assign_pointer(routes
->table
[daddr
], dev
);
390 mutex_unlock(&routes
->lock
);
394 int phonet_route_del(struct net_device
*dev
, u8 daddr
)
396 struct phonet_net
*pnn
= net_generic(dev_net(dev
), phonet_net_id
);
397 struct phonet_routes
*routes
= &pnn
->routes
;
400 mutex_lock(&routes
->lock
);
401 if (dev
== routes
->table
[daddr
])
402 rcu_assign_pointer(routes
->table
[daddr
], NULL
);
405 mutex_unlock(&routes
->lock
);
414 struct net_device
*phonet_route_get(struct net
*net
, u8 daddr
)
416 struct phonet_net
*pnn
= net_generic(net
, phonet_net_id
);
417 struct phonet_routes
*routes
= &pnn
->routes
;
418 struct net_device
*dev
;
420 ASSERT_RTNL(); /* no need to hold the device */
424 dev
= rcu_dereference(routes
->table
[daddr
]);
429 struct net_device
*phonet_route_output(struct net
*net
, u8 daddr
)
431 struct phonet_net
*pnn
= net_generic(net
, phonet_net_id
);
432 struct phonet_routes
*routes
= &pnn
->routes
;
433 struct net_device
*dev
;
437 dev
= rcu_dereference(routes
->table
[daddr
]);
443 dev
= phonet_device_get(net
); /* Default route */