blob: 5ae4c01e8388d5db0ab7d0322857cf9dd86ed319 [file] [log] [blame]
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -07001/*
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>
David S. Miller421d20a2009-07-26 13:39:10 -070030#include <linux/proc_fs.h>
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070031#include <net/sock.h>
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000032#include <net/netns/generic.h>
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070033#include <net/phonet/pn_dev.h>
34
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000035struct phonet_net {
36 struct phonet_device_list pndevs;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070037};
38
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000039int 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
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070047/* Allocate new Phonet device. */
48static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
49{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000050 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070051 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
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000057 list_add(&pnd->list, &pndevs->list);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070058 return pnd;
59}
60
61static struct phonet_device *__phonet_get(struct net_device *dev)
62{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000063 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070064 struct phonet_device *pnd;
65
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000066 list_for_each_entry(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070067 if (pnd->netdev == dev)
68 return pnd;
69 }
70 return NULL;
71}
72
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +000073static void phonet_device_destroy(struct net_device *dev)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070074{
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +000075 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 }
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070094}
95
96struct net_device *phonet_device_get(struct net *net)
97{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000098 struct phonet_device_list *pndevs = phonet_device_list(net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070099 struct phonet_device *pnd;
Eric Dumazet59e57f42009-07-27 08:03:18 -0700100 struct net_device *dev = NULL;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700101
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000102 spin_lock_bh(&pndevs->lock);
103 list_for_each_entry(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700104 dev = pnd->netdev;
105 BUG_ON(!dev);
106
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000107 if ((dev->reg_state == NETREG_REGISTERED) &&
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700108 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
109 break;
110 dev = NULL;
111 }
112 if (dev)
113 dev_hold(dev);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000114 spin_unlock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700115 return dev;
116}
117
118int phonet_address_add(struct net_device *dev, u8 addr)
119{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000120 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700121 struct phonet_device *pnd;
122 int err = 0;
123
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000124 spin_lock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700125 /* 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;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000133 spin_unlock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700134 return err;
135}
136
137int phonet_address_del(struct net_device *dev, u8 addr)
138{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000139 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700140 struct phonet_device *pnd;
141 int err = 0;
142
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000143 spin_lock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700144 pnd = __phonet_get(dev);
145 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs))
146 err = -EADDRNOTAVAIL;
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000147 else if (bitmap_empty(pnd->addrs, 64)) {
148 list_del(&pnd->list);
149 kfree(pnd);
150 }
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000151 spin_unlock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700152 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{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000158 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700159 struct phonet_device *pnd;
160
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000161 spin_lock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700162 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;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000171 spin_unlock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700172 return addr;
173}
174
Rémi Denis-Courmont52404882008-12-03 15:42:56 -0800175int phonet_address_lookup(struct net *net, u8 addr)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700176{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000177 struct phonet_device_list *pndevs = phonet_device_list(net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700178 struct phonet_device *pnd;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000179 int err = -EADDRNOTAVAIL;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700180
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000181 spin_lock_bh(&pndevs->lock);
182 list_for_each_entry(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700183 /* 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)) {
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000189 err = 0;
190 goto found;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700191 }
192 }
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000193found:
194 spin_unlock_bh(&pndevs->lock);
195 return err;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700196}
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
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000204 if (what == NETDEV_UNREGISTER)
205 phonet_device_destroy(dev);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700206 return 0;
207
208}
209
210static struct notifier_block phonet_device_notifier = {
211 .notifier_call = phonet_device_notify,
212 .priority = 0,
213};
214
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000215/* 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
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000222 if (!proc_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops)) {
223 kfree(pnn);
224 return -ENOMEM;
225 }
226
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000227 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);
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000236 struct net_device *dev;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000237
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000238 rtnl_lock();
239 for_each_netdev(net, dev)
240 phonet_device_destroy(dev);
241 rtnl_unlock();
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000242
243 proc_net_remove(net, "phonet");
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000244 kfree(pnn);
245}
246
247static struct pernet_operations phonet_net_ops = {
248 .init = phonet_init_net,
249 .exit = phonet_exit_net,
250};
251
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700252/* Initialize Phonet devices list */
remi.denis-courmont@nokia76e02cf2009-01-23 03:00:27 +0000253int __init phonet_device_init(void)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700254{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000255 int err = register_pernet_gen_device(&phonet_net_id, &phonet_net_ops);
256 if (err)
257 return err;
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000258
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700259 register_netdevice_notifier(&phonet_device_notifier);
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000260 err = phonet_netlink_register();
261 if (err)
262 phonet_device_exit();
263 return err;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700264}
265
266void phonet_device_exit(void)
267{
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700268 rtnl_unregister_all(PF_PHONET);
remi.denis-courmont@nokia6530e0f2009-01-23 03:00:29 +0000269 unregister_netdevice_notifier(&phonet_device_notifier);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000270 unregister_pernet_gen_device(phonet_net_id, &phonet_net_ops);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700271}