net: constify remaining proto_ops
[deliverable/linux.git] / net / phonet / pn_dev.c
CommitLineData
f8ff6028
RDC
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>
421d20a3 30#include <linux/proc_fs.h>
f5bb1c55 31#include <linux/if_arp.h>
f8ff6028 32#include <net/sock.h>
9a3b7a42 33#include <net/netns/generic.h>
f8ff6028
RDC
34#include <net/phonet/pn_dev.h>
35
9a3b7a42 36struct phonet_net {
37 struct phonet_device_list pndevs;
f8ff6028
RDC
38};
39
9a3b7a42 40int phonet_net_id;
41
42struct phonet_device_list *phonet_device_list(struct net *net)
43{
44 struct phonet_net *pnn = net_generic(net, phonet_net_id);
45 return &pnn->pndevs;
46}
47
f8ff6028
RDC
48/* Allocate new Phonet device. */
49static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
50{
9a3b7a42 51 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
f8ff6028
RDC
52 struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
53 if (pnd == NULL)
54 return NULL;
55 pnd->netdev = dev;
56 bitmap_zero(pnd->addrs, 64);
57
9a3b7a42 58 list_add(&pnd->list, &pndevs->list);
f8ff6028
RDC
59 return pnd;
60}
61
62static struct phonet_device *__phonet_get(struct net_device *dev)
63{
9a3b7a42 64 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
f8ff6028
RDC
65 struct phonet_device *pnd;
66
9a3b7a42 67 list_for_each_entry(pnd, &pndevs->list, list) {
f8ff6028
RDC
68 if (pnd->netdev == dev)
69 return pnd;
70 }
71 return NULL;
72}
73
2be6fa4c 74static void phonet_device_destroy(struct net_device *dev)
f8ff6028 75{
2be6fa4c
RDC
76 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
77 struct phonet_device *pnd;
78
79 ASSERT_RTNL();
80
81 spin_lock_bh(&pndevs->lock);
82 pnd = __phonet_get(dev);
83 if (pnd)
84 list_del(&pnd->list);
85 spin_unlock_bh(&pndevs->lock);
86
87 if (pnd) {
88 u8 addr;
89
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);
93 kfree(pnd);
94 }
f8ff6028
RDC
95}
96
97struct net_device *phonet_device_get(struct net *net)
98{
9a3b7a42 99 struct phonet_device_list *pndevs = phonet_device_list(net);
f8ff6028 100 struct phonet_device *pnd;
59e57f44 101 struct net_device *dev = NULL;
f8ff6028 102
9a3b7a42 103 spin_lock_bh(&pndevs->lock);
104 list_for_each_entry(pnd, &pndevs->list, list) {
f8ff6028
RDC
105 dev = pnd->netdev;
106 BUG_ON(!dev);
107
9a3b7a42 108 if ((dev->reg_state == NETREG_REGISTERED) &&
f8ff6028
RDC
109 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
110 break;
111 dev = NULL;
112 }
113 if (dev)
114 dev_hold(dev);
9a3b7a42 115 spin_unlock_bh(&pndevs->lock);
f8ff6028
RDC
116 return dev;
117}
118
119int phonet_address_add(struct net_device *dev, u8 addr)
120{
9a3b7a42 121 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
f8ff6028
RDC
122 struct phonet_device *pnd;
123 int err = 0;
124
9a3b7a42 125 spin_lock_bh(&pndevs->lock);
f8ff6028
RDC
126 /* Find or create Phonet-specific device data */
127 pnd = __phonet_get(dev);
128 if (pnd == NULL)
129 pnd = __phonet_device_alloc(dev);
130 if (unlikely(pnd == NULL))
131 err = -ENOMEM;
132 else if (test_and_set_bit(addr >> 2, pnd->addrs))
133 err = -EEXIST;
9a3b7a42 134 spin_unlock_bh(&pndevs->lock);
f8ff6028
RDC
135 return err;
136}
137
138int phonet_address_del(struct net_device *dev, u8 addr)
139{
9a3b7a42 140 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
f8ff6028
RDC
141 struct phonet_device *pnd;
142 int err = 0;
143
9a3b7a42 144 spin_lock_bh(&pndevs->lock);
f8ff6028
RDC
145 pnd = __phonet_get(dev);
146 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs))
147 err = -EADDRNOTAVAIL;
2be6fa4c
RDC
148 else if (bitmap_empty(pnd->addrs, 64)) {
149 list_del(&pnd->list);
150 kfree(pnd);
151 }
9a3b7a42 152 spin_unlock_bh(&pndevs->lock);
f8ff6028
RDC
153 return err;
154}
155
156/* Gets a source address toward a destination, through a interface. */
157u8 phonet_address_get(struct net_device *dev, u8 addr)
158{
9a3b7a42 159 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
f8ff6028
RDC
160 struct phonet_device *pnd;
161
9a3b7a42 162 spin_lock_bh(&pndevs->lock);
f8ff6028
RDC
163 pnd = __phonet_get(dev);
164 if (pnd) {
165 BUG_ON(bitmap_empty(pnd->addrs, 64));
166
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;
170 } else
171 addr = PN_NO_ADDR;
9a3b7a42 172 spin_unlock_bh(&pndevs->lock);
f8ff6028
RDC
173 return addr;
174}
175
52404881 176int phonet_address_lookup(struct net *net, u8 addr)
f8ff6028 177{
9a3b7a42 178 struct phonet_device_list *pndevs = phonet_device_list(net);
f8ff6028 179 struct phonet_device *pnd;
9a3b7a42 180 int err = -EADDRNOTAVAIL;
f8ff6028 181
9a3b7a42 182 spin_lock_bh(&pndevs->lock);
183 list_for_each_entry(pnd, &pndevs->list, list) {
f8ff6028
RDC
184 /* Don't allow unregistering devices! */
185 if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
186 ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
187 continue;
188
189 if (test_bit(addr >> 2, pnd->addrs)) {
9a3b7a42 190 err = 0;
191 goto found;
f8ff6028
RDC
192 }
193 }
9a3b7a42 194found:
195 spin_unlock_bh(&pndevs->lock);
196 return err;
f8ff6028
RDC
197}
198
f5bb1c55
RDC
199/* automatically configure a Phonet device, if supported */
200static int phonet_device_autoconf(struct net_device *dev)
201{
202 struct if_phonet_req req;
203 int ret;
204
205 if (!dev->netdev_ops->ndo_do_ioctl)
206 return -EOPNOTSUPP;
207
208 ret = dev->netdev_ops->ndo_do_ioctl(dev, (struct ifreq *)&req,
209 SIOCPNGAUTOCONF);
210 if (ret < 0)
211 return ret;
212 return phonet_address_add(dev, req.ifr_phonet_autoconf.device);
213}
214
f8ff6028
RDC
215/* notify Phonet of device events */
216static int phonet_device_notify(struct notifier_block *me, unsigned long what,
217 void *arg)
218{
219 struct net_device *dev = arg;
220
f5bb1c55
RDC
221 switch (what) {
222 case NETDEV_REGISTER:
223 if (dev->type == ARPHRD_PHONET)
224 phonet_device_autoconf(dev);
225 break;
226 case NETDEV_UNREGISTER:
2be6fa4c 227 phonet_device_destroy(dev);
f5bb1c55
RDC
228 break;
229 }
f8ff6028
RDC
230 return 0;
231
232}
233
234static struct notifier_block phonet_device_notifier = {
235 .notifier_call = phonet_device_notify,
236 .priority = 0,
237};
238
9a3b7a42 239/* Per-namespace Phonet devices handling */
240static int phonet_init_net(struct net *net)
241{
242 struct phonet_net *pnn = kmalloc(sizeof(*pnn), GFP_KERNEL);
243 if (!pnn)
244 return -ENOMEM;
245
c1dc13e9
RDC
246 if (!proc_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops)) {
247 kfree(pnn);
248 return -ENOMEM;
249 }
250
9a3b7a42 251 INIT_LIST_HEAD(&pnn->pndevs.list);
252 spin_lock_init(&pnn->pndevs.lock);
253 net_assign_generic(net, phonet_net_id, pnn);
254 return 0;
255}
256
257static void phonet_exit_net(struct net *net)
258{
259 struct phonet_net *pnn = net_generic(net, phonet_net_id);
2be6fa4c 260 struct net_device *dev;
9a3b7a42 261
2be6fa4c
RDC
262 rtnl_lock();
263 for_each_netdev(net, dev)
264 phonet_device_destroy(dev);
265 rtnl_unlock();
c1dc13e9
RDC
266
267 proc_net_remove(net, "phonet");
9a3b7a42 268 kfree(pnn);
269}
270
271static struct pernet_operations phonet_net_ops = {
272 .init = phonet_init_net,
273 .exit = phonet_exit_net,
274};
275
f8ff6028 276/* Initialize Phonet devices list */
76e02cf6 277int __init phonet_device_init(void)
f8ff6028 278{
9a3b7a42 279 int err = register_pernet_gen_device(&phonet_net_id, &phonet_net_ops);
280 if (err)
281 return err;
660f706d 282
f8ff6028 283 register_netdevice_notifier(&phonet_device_notifier);
660f706d 284 err = phonet_netlink_register();
285 if (err)
286 phonet_device_exit();
287 return err;
f8ff6028
RDC
288}
289
290void phonet_device_exit(void)
291{
f8ff6028 292 rtnl_unregister_all(PF_PHONET);
6530e0fe 293 unregister_netdevice_notifier(&phonet_device_notifier);
9a3b7a42 294 unregister_pernet_gen_device(phonet_net_id, &phonet_net_ops);
f8ff6028 295}
This page took 0.152557 seconds and 5 git commands to generate.