Phonet: fix netlink address dump error handling
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
f8ff6028 31#include <net/sock.h>
9a3b7a42 32#include <net/netns/generic.h>
f8ff6028
RDC
33#include <net/phonet/pn_dev.h>
34
9a3b7a42 35struct phonet_net {
36 struct phonet_device_list pndevs;
f8ff6028
RDC
37};
38
9a3b7a42 39int phonet_net_id;
40
41struct 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
f8ff6028
RDC
47/* Allocate new Phonet device. */
48static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
49{
9a3b7a42 50 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
f8ff6028
RDC
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
9a3b7a42 57 list_add(&pnd->list, &pndevs->list);
f8ff6028
RDC
58 return pnd;
59}
60
61static struct phonet_device *__phonet_get(struct net_device *dev)
62{
9a3b7a42 63 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
f8ff6028
RDC
64 struct phonet_device *pnd;
65
9a3b7a42 66 list_for_each_entry(pnd, &pndevs->list, list) {
f8ff6028
RDC
67 if (pnd->netdev == dev)
68 return pnd;
69 }
70 return NULL;
71}
72
2be6fa4c 73static void phonet_device_destroy(struct net_device *dev)
f8ff6028 74{
2be6fa4c
RDC
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 }
f8ff6028
RDC
94}
95
96struct net_device *phonet_device_get(struct net *net)
97{
9a3b7a42 98 struct phonet_device_list *pndevs = phonet_device_list(net);
f8ff6028 99 struct phonet_device *pnd;
59e57f44 100 struct net_device *dev = NULL;
f8ff6028 101
9a3b7a42 102 spin_lock_bh(&pndevs->lock);
103 list_for_each_entry(pnd, &pndevs->list, list) {
f8ff6028
RDC
104 dev = pnd->netdev;
105 BUG_ON(!dev);
106
9a3b7a42 107 if ((dev->reg_state == NETREG_REGISTERED) &&
f8ff6028
RDC
108 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
109 break;
110 dev = NULL;
111 }
112 if (dev)
113 dev_hold(dev);
9a3b7a42 114 spin_unlock_bh(&pndevs->lock);
f8ff6028
RDC
115 return dev;
116}
117
118int phonet_address_add(struct net_device *dev, u8 addr)
119{
9a3b7a42 120 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
f8ff6028
RDC
121 struct phonet_device *pnd;
122 int err = 0;
123
9a3b7a42 124 spin_lock_bh(&pndevs->lock);
f8ff6028
RDC
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;
9a3b7a42 133 spin_unlock_bh(&pndevs->lock);
f8ff6028
RDC
134 return err;
135}
136
137int phonet_address_del(struct net_device *dev, u8 addr)
138{
9a3b7a42 139 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
f8ff6028
RDC
140 struct phonet_device *pnd;
141 int err = 0;
142
9a3b7a42 143 spin_lock_bh(&pndevs->lock);
f8ff6028
RDC
144 pnd = __phonet_get(dev);
145 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs))
146 err = -EADDRNOTAVAIL;
2be6fa4c
RDC
147 else if (bitmap_empty(pnd->addrs, 64)) {
148 list_del(&pnd->list);
149 kfree(pnd);
150 }
9a3b7a42 151 spin_unlock_bh(&pndevs->lock);
f8ff6028
RDC
152 return err;
153}
154
155/* Gets a source address toward a destination, through a interface. */
156u8 phonet_address_get(struct net_device *dev, u8 addr)
157{
9a3b7a42 158 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
f8ff6028
RDC
159 struct phonet_device *pnd;
160
9a3b7a42 161 spin_lock_bh(&pndevs->lock);
f8ff6028
RDC
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;
9a3b7a42 171 spin_unlock_bh(&pndevs->lock);
f8ff6028
RDC
172 return addr;
173}
174
52404881 175int phonet_address_lookup(struct net *net, u8 addr)
f8ff6028 176{
9a3b7a42 177 struct phonet_device_list *pndevs = phonet_device_list(net);
f8ff6028 178 struct phonet_device *pnd;
9a3b7a42 179 int err = -EADDRNOTAVAIL;
f8ff6028 180
9a3b7a42 181 spin_lock_bh(&pndevs->lock);
182 list_for_each_entry(pnd, &pndevs->list, list) {
f8ff6028
RDC
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)) {
9a3b7a42 189 err = 0;
190 goto found;
f8ff6028
RDC
191 }
192 }
9a3b7a42 193found:
194 spin_unlock_bh(&pndevs->lock);
195 return err;
f8ff6028
RDC
196}
197
198/* notify Phonet of device events */
199static int phonet_device_notify(struct notifier_block *me, unsigned long what,
200 void *arg)
201{
202 struct net_device *dev = arg;
203
2be6fa4c
RDC
204 if (what == NETDEV_UNREGISTER)
205 phonet_device_destroy(dev);
f8ff6028
RDC
206 return 0;
207
208}
209
210static struct notifier_block phonet_device_notifier = {
211 .notifier_call = phonet_device_notify,
212 .priority = 0,
213};
214
9a3b7a42 215/* Per-namespace Phonet devices handling */
216static 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
c1dc13e9
RDC
222 if (!proc_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops)) {
223 kfree(pnn);
224 return -ENOMEM;
225 }
226
9a3b7a42 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
233static void phonet_exit_net(struct net *net)
234{
235 struct phonet_net *pnn = net_generic(net, phonet_net_id);
2be6fa4c 236 struct net_device *dev;
9a3b7a42 237
2be6fa4c
RDC
238 rtnl_lock();
239 for_each_netdev(net, dev)
240 phonet_device_destroy(dev);
241 rtnl_unlock();
c1dc13e9
RDC
242
243 proc_net_remove(net, "phonet");
9a3b7a42 244 kfree(pnn);
245}
246
247static struct pernet_operations phonet_net_ops = {
248 .init = phonet_init_net,
249 .exit = phonet_exit_net,
250};
251
f8ff6028 252/* Initialize Phonet devices list */
76e02cf6 253int __init phonet_device_init(void)
f8ff6028 254{
9a3b7a42 255 int err = register_pernet_gen_device(&phonet_net_id, &phonet_net_ops);
256 if (err)
257 return err;
660f706d 258
f8ff6028 259 register_netdevice_notifier(&phonet_device_notifier);
660f706d 260 err = phonet_netlink_register();
261 if (err)
262 phonet_device_exit();
263 return err;
f8ff6028
RDC
264}
265
266void phonet_device_exit(void)
267{
f8ff6028 268 rtnl_unregister_all(PF_PHONET);
6530e0fe 269 unregister_netdevice_notifier(&phonet_device_notifier);
9a3b7a42 270 unregister_pernet_gen_device(phonet_net_id, &phonet_net_ops);
f8ff6028 271}