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>
37 struct phonet_device_list pndevs
;
42 struct phonet_device_list
*phonet_device_list(struct net
*net
)
44 struct phonet_net
*pnn
= net_generic(net
, phonet_net_id
);
48 /* Allocate new Phonet device. */
49 static struct phonet_device
*__phonet_device_alloc(struct net_device
*dev
)
51 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
52 struct phonet_device
*pnd
= kmalloc(sizeof(*pnd
), GFP_ATOMIC
);
56 bitmap_zero(pnd
->addrs
, 64);
58 list_add(&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 list_for_each_entry(pnd
, &pndevs
->list
, list
) {
68 if (pnd
->netdev
== dev
)
74 static void phonet_device_destroy(struct net_device
*dev
)
76 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
77 struct phonet_device
*pnd
;
81 spin_lock_bh(&pndevs
->lock
);
82 pnd
= __phonet_get(dev
);
85 spin_unlock_bh(&pndevs
->lock
);
90 for (addr
= find_first_bit(pnd
->addrs
, 64); addr
< 64;
91 addr
= find_next_bit(pnd
->addrs
, 64, 1+addr
))
92 phonet_address_notify(RTM_DELADDR
, dev
, addr
);
97 struct net_device
*phonet_device_get(struct net
*net
)
99 struct phonet_device_list
*pndevs
= phonet_device_list(net
);
100 struct phonet_device
*pnd
;
101 struct net_device
*dev
= NULL
;
103 spin_lock_bh(&pndevs
->lock
);
104 list_for_each_entry(pnd
, &pndevs
->list
, list
) {
108 if ((dev
->reg_state
== NETREG_REGISTERED
) &&
109 ((pnd
->netdev
->flags
& IFF_UP
)) == IFF_UP
)
115 spin_unlock_bh(&pndevs
->lock
);
119 int phonet_address_add(struct net_device
*dev
, u8 addr
)
121 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
122 struct phonet_device
*pnd
;
125 spin_lock_bh(&pndevs
->lock
);
126 /* Find or create Phonet-specific device data */
127 pnd
= __phonet_get(dev
);
129 pnd
= __phonet_device_alloc(dev
);
130 if (unlikely(pnd
== NULL
))
132 else if (test_and_set_bit(addr
>> 2, pnd
->addrs
))
134 spin_unlock_bh(&pndevs
->lock
);
138 int phonet_address_del(struct net_device
*dev
, u8 addr
)
140 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
141 struct phonet_device
*pnd
;
144 spin_lock_bh(&pndevs
->lock
);
145 pnd
= __phonet_get(dev
);
146 if (!pnd
|| !test_and_clear_bit(addr
>> 2, pnd
->addrs
))
147 err
= -EADDRNOTAVAIL
;
148 else if (bitmap_empty(pnd
->addrs
, 64)) {
149 list_del(&pnd
->list
);
152 spin_unlock_bh(&pndevs
->lock
);
156 /* Gets a source address toward a destination, through a interface. */
157 u8
phonet_address_get(struct net_device
*dev
, u8 addr
)
159 struct phonet_device_list
*pndevs
= phonet_device_list(dev_net(dev
));
160 struct phonet_device
*pnd
;
162 spin_lock_bh(&pndevs
->lock
);
163 pnd
= __phonet_get(dev
);
165 BUG_ON(bitmap_empty(pnd
->addrs
, 64));
167 /* Use same source address as destination, if possible */
168 if (!test_bit(addr
>> 2, pnd
->addrs
))
169 addr
= find_first_bit(pnd
->addrs
, 64) << 2;
172 spin_unlock_bh(&pndevs
->lock
);
176 int phonet_address_lookup(struct net
*net
, u8 addr
)
178 struct phonet_device_list
*pndevs
= phonet_device_list(net
);
179 struct phonet_device
*pnd
;
180 int err
= -EADDRNOTAVAIL
;
182 spin_lock_bh(&pndevs
->lock
);
183 list_for_each_entry(pnd
, &pndevs
->list
, list
) {
184 /* Don't allow unregistering devices! */
185 if ((pnd
->netdev
->reg_state
!= NETREG_REGISTERED
) ||
186 ((pnd
->netdev
->flags
& IFF_UP
)) != IFF_UP
)
189 if (test_bit(addr
>> 2, pnd
->addrs
)) {
195 spin_unlock_bh(&pndevs
->lock
);
199 /* automatically configure a Phonet device, if supported */
200 static int phonet_device_autoconf(struct net_device
*dev
)
202 struct if_phonet_req req
;
205 if (!dev
->netdev_ops
->ndo_do_ioctl
)
208 ret
= dev
->netdev_ops
->ndo_do_ioctl(dev
, (struct ifreq
*)&req
,
214 ret
= phonet_address_add(dev
, req
.ifr_phonet_autoconf
.device
);
217 phonet_address_notify(RTM_NEWADDR
, dev
,
218 req
.ifr_phonet_autoconf
.device
);
222 /* notify Phonet of device events */
223 static int phonet_device_notify(struct notifier_block
*me
, unsigned long what
,
226 struct net_device
*dev
= arg
;
229 case NETDEV_REGISTER
:
230 if (dev
->type
== ARPHRD_PHONET
)
231 phonet_device_autoconf(dev
);
233 case NETDEV_UNREGISTER
:
234 phonet_device_destroy(dev
);
241 static struct notifier_block phonet_device_notifier
= {
242 .notifier_call
= phonet_device_notify
,
246 /* Per-namespace Phonet devices handling */
247 static int phonet_init_net(struct net
*net
)
249 struct phonet_net
*pnn
= kmalloc(sizeof(*pnn
), GFP_KERNEL
);
253 if (!proc_net_fops_create(net
, "phonet", 0, &pn_sock_seq_fops
)) {
258 INIT_LIST_HEAD(&pnn
->pndevs
.list
);
259 spin_lock_init(&pnn
->pndevs
.lock
);
260 net_assign_generic(net
, phonet_net_id
, pnn
);
264 static void phonet_exit_net(struct net
*net
)
266 struct phonet_net
*pnn
= net_generic(net
, phonet_net_id
);
267 struct net_device
*dev
;
270 for_each_netdev(net
, dev
)
271 phonet_device_destroy(dev
);
274 proc_net_remove(net
, "phonet");
278 static struct pernet_operations phonet_net_ops
= {
279 .init
= phonet_init_net
,
280 .exit
= phonet_exit_net
,
283 /* Initialize Phonet devices list */
284 int __init
phonet_device_init(void)
286 int err
= register_pernet_gen_device(&phonet_net_id
, &phonet_net_ops
);
290 register_netdevice_notifier(&phonet_device_notifier
);
291 err
= phonet_netlink_register();
293 phonet_device_exit();
297 void phonet_device_exit(void)
299 rtnl_unregister_all(PF_PHONET
);
300 unregister_netdevice_notifier(&phonet_device_notifier
);
301 unregister_pernet_gen_device(phonet_net_id
, &phonet_net_ops
);