6b30cc1b94b8c4b14494eb4751878023d3d96f84
[linux-2.6.git] / net / phonet / pn_dev.c
1 /*
2  * File: pn_dev.c
3  *
4  * Phonet network device
5  *
6  * Copyright (C) 2008 Nokia Corporation.
7  *
8  * Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
9  * Original author: Sakari Ailus <sakari.ailus@nokia.com>
10  *
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.
14  *
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.
19  *
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
23  * 02110-1301 USA
24  */
25
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 <net/sock.h>
32 #include <net/netns/generic.h>
33 #include <net/phonet/pn_dev.h>
34
35 struct phonet_net {
36         struct phonet_device_list pndevs;
37 };
38
39 int phonet_net_id;
40
41 struct phonet_device_list *phonet_device_list(struct net *net)
42 {
43         struct phonet_net *pnn = net_generic(net, phonet_net_id);
44         return &pnn->pndevs;
45 }
46
47 /* Allocate new Phonet device. */
48 static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
49 {
50         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
51         struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
52         if (pnd == NULL)
53                 return NULL;
54         pnd->netdev = dev;
55         bitmap_zero(pnd->addrs, 64);
56
57         list_add(&pnd->list, &pndevs->list);
58         return pnd;
59 }
60
61 static struct phonet_device *__phonet_get(struct net_device *dev)
62 {
63         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
64         struct phonet_device *pnd;
65
66         list_for_each_entry(pnd, &pndevs->list, list) {
67                 if (pnd->netdev == dev)
68                         return pnd;
69         }
70         return NULL;
71 }
72
73 static void phonet_device_destroy(struct net_device *dev)
74 {
75         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
76         struct phonet_device *pnd;
77
78         ASSERT_RTNL();
79
80         spin_lock_bh(&pndevs->lock);
81         pnd = __phonet_get(dev);
82         if (pnd)
83                 list_del(&pnd->list);
84         spin_unlock_bh(&pndevs->lock);
85
86         if (pnd) {
87                 u8 addr;
88
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);
92                 kfree(pnd);
93         }
94 }
95
96 struct net_device *phonet_device_get(struct net *net)
97 {
98         struct phonet_device_list *pndevs = phonet_device_list(net);
99         struct phonet_device *pnd;
100         struct net_device *dev;
101
102         spin_lock_bh(&pndevs->lock);
103         list_for_each_entry(pnd, &pndevs->list, list) {
104                 dev = pnd->netdev;
105                 BUG_ON(!dev);
106
107                 if ((dev->reg_state == NETREG_REGISTERED) &&
108                         ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
109                         break;
110                 dev = NULL;
111         }
112         if (dev)
113                 dev_hold(dev);
114         spin_unlock_bh(&pndevs->lock);
115         return dev;
116 }
117
118 int phonet_address_add(struct net_device *dev, u8 addr)
119 {
120         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
121         struct phonet_device *pnd;
122         int err = 0;
123
124         spin_lock_bh(&pndevs->lock);
125         /* Find or create Phonet-specific device data */
126         pnd = __phonet_get(dev);
127         if (pnd == NULL)
128                 pnd = __phonet_device_alloc(dev);
129         if (unlikely(pnd == NULL))
130                 err = -ENOMEM;
131         else if (test_and_set_bit(addr >> 2, pnd->addrs))
132                 err = -EEXIST;
133         spin_unlock_bh(&pndevs->lock);
134         return err;
135 }
136
137 int phonet_address_del(struct net_device *dev, u8 addr)
138 {
139         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
140         struct phonet_device *pnd;
141         int err = 0;
142
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);
149                 kfree(pnd);
150         }
151         spin_unlock_bh(&pndevs->lock);
152         return err;
153 }
154
155 /* Gets a source address toward a destination, through a interface. */
156 u8 phonet_address_get(struct net_device *dev, u8 addr)
157 {
158         struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
159         struct phonet_device *pnd;
160
161         spin_lock_bh(&pndevs->lock);
162         pnd = __phonet_get(dev);
163         if (pnd) {
164                 BUG_ON(bitmap_empty(pnd->addrs, 64));
165
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;
169         } else
170                 addr = PN_NO_ADDR;
171         spin_unlock_bh(&pndevs->lock);
172         return addr;
173 }
174
175 int phonet_address_lookup(struct net *net, u8 addr)
176 {
177         struct phonet_device_list *pndevs = phonet_device_list(net);
178         struct phonet_device *pnd;
179         int err = -EADDRNOTAVAIL;
180
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)
186                         continue;
187
188                 if (test_bit(addr >> 2, pnd->addrs)) {
189                         err = 0;
190                         goto found;
191                 }
192         }
193 found:
194         spin_unlock_bh(&pndevs->lock);
195         return err;
196 }
197
198 /* notify Phonet of device events */
199 static int phonet_device_notify(struct notifier_block *me, unsigned long what,
200                                 void *arg)
201 {
202         struct net_device *dev = arg;
203
204         if (what == NETDEV_UNREGISTER)
205                 phonet_device_destroy(dev);
206         return 0;
207
208 }
209
210 static struct notifier_block phonet_device_notifier = {
211         .notifier_call = phonet_device_notify,
212         .priority = 0,
213 };
214
215 /* Per-namespace Phonet devices handling */
216 static int phonet_init_net(struct net *net)
217 {
218         struct phonet_net *pnn = kmalloc(sizeof(*pnn), GFP_KERNEL);
219         if (!pnn)
220                 return -ENOMEM;
221
222         if (!proc_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops)) {
223                 kfree(pnn);
224                 return -ENOMEM;
225         }
226
227         INIT_LIST_HEAD(&pnn->pndevs.list);
228         spin_lock_init(&pnn->pndevs.lock);
229         net_assign_generic(net, phonet_net_id, pnn);
230         return 0;
231 }
232
233 static void phonet_exit_net(struct net *net)
234 {
235         struct phonet_net *pnn = net_generic(net, phonet_net_id);
236         struct net_device *dev;
237
238         rtnl_lock();
239         for_each_netdev(net, dev)
240                 phonet_device_destroy(dev);
241         rtnl_unlock();
242
243         proc_net_remove(net, "phonet");
244         kfree(pnn);
245 }
246
247 static struct pernet_operations phonet_net_ops = {
248         .init = phonet_init_net,
249         .exit = phonet_exit_net,
250 };
251
252 /* Initialize Phonet devices list */
253 int __init phonet_device_init(void)
254 {
255         int err = register_pernet_gen_device(&phonet_net_id, &phonet_net_ops);
256         if (err)
257                 return err;
258
259         register_netdevice_notifier(&phonet_device_notifier);
260         err = phonet_netlink_register();
261         if (err)
262                 phonet_device_exit();
263         return err;
264 }
265
266 void phonet_device_exit(void)
267 {
268         rtnl_unregister_all(PF_PHONET);
269         unregister_netdevice_notifier(&phonet_device_notifier);
270         unregister_pernet_gen_device(phonet_net_id, &phonet_net_ops);
271 }