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>
32 #include <net/netns/generic.h>
33 #include <net/phonet/pn_dev.h>
36 struct phonet_device_list pndevs;
41 struct phonet_device_list *phonet_device_list(struct net *net)
43 struct phonet_net *pnn = net_generic(net, phonet_net_id);
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 list_add(&pnd->list, &pndevs->list);
61 static struct phonet_device *__phonet_get(struct net_device *dev)
63 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
64 struct phonet_device *pnd;
66 list_for_each_entry(pnd, &pndevs->list, list) {
67 if (pnd->netdev == dev)
73 static void phonet_device_destroy(struct net_device *dev)
75 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
76 struct phonet_device *pnd;
80 spin_lock_bh(&pndevs->lock);
81 pnd = __phonet_get(dev);
84 spin_unlock_bh(&pndevs->lock);
89 for (addr = find_first_bit(pnd->addrs, 64); addr < 64;
90 addr = find_next_bit(pnd->addrs, 64, 1+addr))
91 phonet_address_notify(RTM_DELADDR, dev, addr);
96 struct net_device *phonet_device_get(struct net *net)
98 struct phonet_device_list *pndevs = phonet_device_list(net);
99 struct phonet_device *pnd;
100 struct net_device *dev = NULL;
102 spin_lock_bh(&pndevs->lock);
103 list_for_each_entry(pnd, &pndevs->list, list) {
107 if ((dev->reg_state == NETREG_REGISTERED) &&
108 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
114 spin_unlock_bh(&pndevs->lock);
118 int phonet_address_add(struct net_device *dev, u8 addr)
120 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
121 struct phonet_device *pnd;
124 spin_lock_bh(&pndevs->lock);
125 /* Find or create Phonet-specific device data */
126 pnd = __phonet_get(dev);
128 pnd = __phonet_device_alloc(dev);
129 if (unlikely(pnd == NULL))
131 else if (test_and_set_bit(addr >> 2, pnd->addrs))
133 spin_unlock_bh(&pndevs->lock);
137 int phonet_address_del(struct net_device *dev, u8 addr)
139 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
140 struct phonet_device *pnd;
143 spin_lock_bh(&pndevs->lock);
144 pnd = __phonet_get(dev);
145 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs))
146 err = -EADDRNOTAVAIL;
147 else if (bitmap_empty(pnd->addrs, 64)) {
148 list_del(&pnd->list);
151 spin_unlock_bh(&pndevs->lock);
155 /* Gets a source address toward a destination, through a interface. */
156 u8 phonet_address_get(struct net_device *dev, u8 addr)
158 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
159 struct phonet_device *pnd;
161 spin_lock_bh(&pndevs->lock);
162 pnd = __phonet_get(dev);
164 BUG_ON(bitmap_empty(pnd->addrs, 64));
166 /* Use same source address as destination, if possible */
167 if (!test_bit(addr >> 2, pnd->addrs))
168 addr = find_first_bit(pnd->addrs, 64) << 2;
171 spin_unlock_bh(&pndevs->lock);
175 int phonet_address_lookup(struct net *net, u8 addr)
177 struct phonet_device_list *pndevs = phonet_device_list(net);
178 struct phonet_device *pnd;
179 int err = -EADDRNOTAVAIL;
181 spin_lock_bh(&pndevs->lock);
182 list_for_each_entry(pnd, &pndevs->list, list) {
183 /* Don't allow unregistering devices! */
184 if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
185 ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
188 if (test_bit(addr >> 2, pnd->addrs)) {
194 spin_unlock_bh(&pndevs->lock);
198 /* notify Phonet of device events */
199 static int phonet_device_notify(struct notifier_block *me, unsigned long what,
202 struct net_device *dev = arg;
204 if (what == NETDEV_UNREGISTER)
205 phonet_device_destroy(dev);
210 static struct notifier_block phonet_device_notifier = {
211 .notifier_call = phonet_device_notify,
215 /* Per-namespace Phonet devices handling */
216 static int phonet_init_net(struct net *net)
218 struct phonet_net *pnn = kmalloc(sizeof(*pnn), GFP_KERNEL);
222 if (!proc_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops)) {
227 INIT_LIST_HEAD(&pnn->pndevs.list);
228 spin_lock_init(&pnn->pndevs.lock);
229 net_assign_generic(net, phonet_net_id, pnn);
233 static void phonet_exit_net(struct net *net)
235 struct phonet_net *pnn = net_generic(net, phonet_net_id);
236 struct net_device *dev;
239 for_each_netdev(net, dev)
240 phonet_device_destroy(dev);
243 proc_net_remove(net, "phonet");
247 static struct pernet_operations phonet_net_ops = {
248 .init = phonet_init_net,
249 .exit = phonet_exit_net,
252 /* Initialize Phonet devices list */
253 int __init phonet_device_init(void)
255 int err = register_pernet_gen_device(&phonet_net_id, &phonet_net_ops);
259 register_netdevice_notifier(&phonet_device_notifier);
260 err = phonet_netlink_register();
262 phonet_device_exit();
266 void phonet_device_exit(void)
268 rtnl_unregister_all(PF_PHONET);
269 unregister_netdevice_notifier(&phonet_device_notifier);
270 unregister_pernet_gen_device(phonet_net_id, &phonet_net_ops);