blob: b0d6ddd82a9da1b20ce14bf2b76099cdf72362bc [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>
30#include <net/sock.h>
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000031#include <net/netns/generic.h>
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070032#include <net/phonet/pn_dev.h>
33
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000034struct phonet_net {
35 struct phonet_device_list pndevs;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070036};
37
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000038int phonet_net_id;
39
40struct phonet_device_list *phonet_device_list(struct net *net)
41{
42 struct phonet_net *pnn = net_generic(net, phonet_net_id);
43 return &pnn->pndevs;
44}
45
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070046/* Allocate new Phonet device. */
47static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
48{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000049 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070050 struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
51 if (pnd == NULL)
52 return NULL;
53 pnd->netdev = dev;
54 bitmap_zero(pnd->addrs, 64);
55
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000056 list_add(&pnd->list, &pndevs->list);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070057 return pnd;
58}
59
60static struct phonet_device *__phonet_get(struct net_device *dev)
61{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000062 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070063 struct phonet_device *pnd;
64
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000065 list_for_each_entry(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070066 if (pnd->netdev == dev)
67 return pnd;
68 }
69 return NULL;
70}
71
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +000072static void phonet_device_destroy(struct net_device *dev)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070073{
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +000074 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
75 struct phonet_device *pnd;
76
77 ASSERT_RTNL();
78
79 spin_lock_bh(&pndevs->lock);
80 pnd = __phonet_get(dev);
81 if (pnd)
82 list_del(&pnd->list);
83 spin_unlock_bh(&pndevs->lock);
84
85 if (pnd) {
86 u8 addr;
87
88 for (addr = find_first_bit(pnd->addrs, 64); addr < 64;
89 addr = find_next_bit(pnd->addrs, 64, 1+addr))
90 phonet_address_notify(RTM_DELADDR, dev, addr);
91 kfree(pnd);
92 }
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070093}
94
95struct net_device *phonet_device_get(struct net *net)
96{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000097 struct phonet_device_list *pndevs = phonet_device_list(net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070098 struct phonet_device *pnd;
99 struct net_device *dev;
100
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000101 spin_lock_bh(&pndevs->lock);
102 list_for_each_entry(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700103 dev = pnd->netdev;
104 BUG_ON(!dev);
105
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000106 if ((dev->reg_state == NETREG_REGISTERED) &&
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700107 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
108 break;
109 dev = NULL;
110 }
111 if (dev)
112 dev_hold(dev);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000113 spin_unlock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700114 return dev;
115}
116
117int phonet_address_add(struct net_device *dev, u8 addr)
118{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000119 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700120 struct phonet_device *pnd;
121 int err = 0;
122
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000123 spin_lock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700124 /* Find or create Phonet-specific device data */
125 pnd = __phonet_get(dev);
126 if (pnd == NULL)
127 pnd = __phonet_device_alloc(dev);
128 if (unlikely(pnd == NULL))
129 err = -ENOMEM;
130 else if (test_and_set_bit(addr >> 2, pnd->addrs))
131 err = -EEXIST;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000132 spin_unlock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700133 return err;
134}
135
136int phonet_address_del(struct net_device *dev, u8 addr)
137{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000138 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700139 struct phonet_device *pnd;
140 int err = 0;
141
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000142 spin_lock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700143 pnd = __phonet_get(dev);
144 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs))
145 err = -EADDRNOTAVAIL;
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000146 else if (bitmap_empty(pnd->addrs, 64)) {
147 list_del(&pnd->list);
148 kfree(pnd);
149 }
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000150 spin_unlock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700151 return err;
152}
153
154/* Gets a source address toward a destination, through a interface. */
155u8 phonet_address_get(struct net_device *dev, u8 addr)
156{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000157 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700158 struct phonet_device *pnd;
159
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000160 spin_lock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700161 pnd = __phonet_get(dev);
162 if (pnd) {
163 BUG_ON(bitmap_empty(pnd->addrs, 64));
164
165 /* Use same source address as destination, if possible */
166 if (!test_bit(addr >> 2, pnd->addrs))
167 addr = find_first_bit(pnd->addrs, 64) << 2;
168 } else
169 addr = PN_NO_ADDR;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000170 spin_unlock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700171 return addr;
172}
173
Rémi Denis-Courmont52404882008-12-03 15:42:56 -0800174int phonet_address_lookup(struct net *net, u8 addr)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700175{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000176 struct phonet_device_list *pndevs = phonet_device_list(net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700177 struct phonet_device *pnd;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000178 int err = -EADDRNOTAVAIL;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700179
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000180 spin_lock_bh(&pndevs->lock);
181 list_for_each_entry(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700182 /* Don't allow unregistering devices! */
183 if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
184 ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
185 continue;
186
187 if (test_bit(addr >> 2, pnd->addrs)) {
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000188 err = 0;
189 goto found;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700190 }
191 }
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000192found:
193 spin_unlock_bh(&pndevs->lock);
194 return err;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700195}
196
197/* notify Phonet of device events */
198static int phonet_device_notify(struct notifier_block *me, unsigned long what,
199 void *arg)
200{
201 struct net_device *dev = arg;
202
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000203 if (what == NETDEV_UNREGISTER)
204 phonet_device_destroy(dev);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700205 return 0;
206
207}
208
209static struct notifier_block phonet_device_notifier = {
210 .notifier_call = phonet_device_notify,
211 .priority = 0,
212};
213
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000214/* Per-namespace Phonet devices handling */
215static int phonet_init_net(struct net *net)
216{
217 struct phonet_net *pnn = kmalloc(sizeof(*pnn), GFP_KERNEL);
218 if (!pnn)
219 return -ENOMEM;
220
221 INIT_LIST_HEAD(&pnn->pndevs.list);
222 spin_lock_init(&pnn->pndevs.lock);
223 net_assign_generic(net, phonet_net_id, pnn);
224 return 0;
225}
226
227static void phonet_exit_net(struct net *net)
228{
229 struct phonet_net *pnn = net_generic(net, phonet_net_id);
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000230 struct net_device *dev;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000231
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000232 rtnl_lock();
233 for_each_netdev(net, dev)
234 phonet_device_destroy(dev);
235 rtnl_unlock();
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000236 kfree(pnn);
237}
238
239static struct pernet_operations phonet_net_ops = {
240 .init = phonet_init_net,
241 .exit = phonet_exit_net,
242};
243
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700244/* Initialize Phonet devices list */
remi.denis-courmont@nokia76e02cf2009-01-23 03:00:27 +0000245int __init phonet_device_init(void)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700246{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000247 int err = register_pernet_gen_device(&phonet_net_id, &phonet_net_ops);
248 if (err)
249 return err;
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000250
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700251 register_netdevice_notifier(&phonet_device_notifier);
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000252 err = phonet_netlink_register();
253 if (err)
254 phonet_device_exit();
255 return err;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700256}
257
258void phonet_device_exit(void)
259{
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700260 rtnl_unregister_all(PF_PHONET);
remi.denis-courmont@nokia6530e0f2009-01-23 03:00:29 +0000261 unregister_netdevice_notifier(&phonet_device_notifier);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000262 unregister_pernet_gen_device(phonet_net_id, &phonet_net_ops);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700263}