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>
31 #include <net/netns/generic.h>
32 #include <net/phonet/pn_dev.h>
35 struct phonet_device_list pndevs
;
40 struct phonet_device_list
*phonet_device_list(struct net
*net
)
42 struct phonet_net
*pnn
= net_generic(net
, phonet_net_id
);
46 /* Allocate new Phonet device. */
47 static struct phonet_device
*__phonet_device_alloc(struct net_device
*dev
)
49 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
50 struct phonet_device
*pnd
= kmalloc(sizeof(*pnd
), GFP_ATOMIC
);
54 bitmap_zero(pnd
->addrs
, 64);
56 list_add(&pnd
->list
, &pndevs
->list
);
60 static struct phonet_device
*__phonet_get(struct net_device
*dev
)
62 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
63 struct phonet_device
*pnd
;
65 list_for_each_entry(pnd
, &pndevs
->list
, list
) {
66 if (pnd
->netdev
== dev
)
72 static void phonet_device_destroy(struct net_device
*dev
)
74 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
75 struct phonet_device
*pnd
;
79 spin_lock_bh(&pndevs
->lock
);
80 pnd
= __phonet_get(dev
);
83 spin_unlock_bh(&pndevs
->lock
);
88 for (addr
= find_first_bit(pnd
->addrs
, 64); addr
< 64;
89 addr
= find_next_bit(pnd
->addrs
, 64, 1+addr
))
90 phonet_address_notify(RTM_DELADDR
, dev
, addr
);
95 struct net_device
*phonet_device_get(struct net
*net
)
97 struct phonet_device_list
*pndevs
= phonet_device_list(net
);
98 struct phonet_device
*pnd
;
99 struct net_device
*dev
= NULL
;
101 spin_lock_bh(&pndevs
->lock
);
102 list_for_each_entry(pnd
, &pndevs
->list
, list
) {
106 if ((dev
->reg_state
== NETREG_REGISTERED
) &&
107 ((pnd
->netdev
->flags
& IFF_UP
)) == IFF_UP
)
113 spin_unlock_bh(&pndevs
->lock
);
117 int phonet_address_add(struct net_device
*dev
, u8 addr
)
119 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
120 struct phonet_device
*pnd
;
123 spin_lock_bh(&pndevs
->lock
);
124 /* Find or create Phonet-specific device data */
125 pnd
= __phonet_get(dev
);
127 pnd
= __phonet_device_alloc(dev
);
128 if (unlikely(pnd
== NULL
))
130 else if (test_and_set_bit(addr
>> 2, pnd
->addrs
))
132 spin_unlock_bh(&pndevs
->lock
);
136 int phonet_address_del(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_bh(&pndevs
->lock
);
143 pnd
= __phonet_get(dev
);
144 if (!pnd
|| !test_and_clear_bit(addr
>> 2, pnd
->addrs
))
145 err
= -EADDRNOTAVAIL
;
146 else if (bitmap_empty(pnd
->addrs
, 64)) {
147 list_del(&pnd
->list
);
150 spin_unlock_bh(&pndevs
->lock
);
154 /* Gets a source address toward a destination, through a interface. */
155 u8
phonet_address_get(struct net_device
*dev
, u8 addr
)
157 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
158 struct phonet_device
*pnd
;
160 spin_lock_bh(&pndevs
->lock
);
161 pnd
= __phonet_get(dev
);
163 BUG_ON(bitmap_empty(pnd
->addrs
, 64));
165 /* Use same source address as destination, if possible */
166 if (!test_bit(addr
>> 2, pnd
->addrs
))
167 addr
= find_first_bit(pnd
->addrs
, 64) << 2;
170 spin_unlock_bh(&pndevs
->lock
);
174 int phonet_address_lookup(struct net
*net
, u8 addr
)
176 struct phonet_device_list
*pndevs
= phonet_device_list(net
);
177 struct phonet_device
*pnd
;
178 int err
= -EADDRNOTAVAIL
;
180 spin_lock_bh(&pndevs
->lock
);
181 list_for_each_entry(pnd
, &pndevs
->list
, list
) {
182 /* Don't allow unregistering devices! */
183 if ((pnd
->netdev
->reg_state
!= NETREG_REGISTERED
) ||
184 ((pnd
->netdev
->flags
& IFF_UP
)) != IFF_UP
)
187 if (test_bit(addr
>> 2, pnd
->addrs
)) {
193 spin_unlock_bh(&pndevs
->lock
);
197 /* notify Phonet of device events */
198 static int phonet_device_notify(struct notifier_block
*me
, unsigned long what
,
201 struct net_device
*dev
= arg
;
203 if (what
== NETDEV_UNREGISTER
)
204 phonet_device_destroy(dev
);
209 static struct notifier_block phonet_device_notifier
= {
210 .notifier_call
= phonet_device_notify
,
214 /* Per-namespace Phonet devices handling */
215 static int phonet_init_net(struct net
*net
)
217 struct phonet_net
*pnn
= kmalloc(sizeof(*pnn
), GFP_KERNEL
);
221 INIT_LIST_HEAD(&pnn
->pndevs
.list
);
222 spin_lock_init(&pnn
->pndevs
.lock
);
223 net_assign_generic(net
, phonet_net_id
, pnn
);
227 static void phonet_exit_net(struct net
*net
)
229 struct phonet_net
*pnn
= net_generic(net
, phonet_net_id
);
230 struct net_device
*dev
;
233 for_each_netdev(net
, dev
)
234 phonet_device_destroy(dev
);
239 static struct pernet_operations phonet_net_ops
= {
240 .init
= phonet_init_net
,
241 .exit
= phonet_exit_net
,
244 /* Initialize Phonet devices list */
245 int __init
phonet_device_init(void)
247 int err
= register_pernet_gen_device(&phonet_net_id
, &phonet_net_ops
);
251 register_netdevice_notifier(&phonet_device_notifier
);
252 err
= phonet_netlink_register();
254 phonet_device_exit();
258 void phonet_device_exit(void)
260 rtnl_unregister_all(PF_PHONET
);
261 unregister_netdevice_notifier(&phonet_device_notifier
);
262 unregister_pernet_gen_device(phonet_net_id
, &phonet_net_ops
);