blob: 5f42f30dd1682bdc2427e7b387390fe231ab305e [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>
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +000031#include <linux/if_arp.h>
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070032#include <net/sock.h>
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000033#include <net/netns/generic.h>
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070034#include <net/phonet/pn_dev.h>
35
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000036struct phonet_net {
37 struct phonet_device_list pndevs;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070038};
39
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000040int 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
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070048/* Allocate new Phonet device. */
49static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
50{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000051 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070052 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
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000058 list_add(&pnd->list, &pndevs->list);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070059 return pnd;
60}
61
62static struct phonet_device *__phonet_get(struct net_device *dev)
63{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000064 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070065 struct phonet_device *pnd;
66
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000067 list_for_each_entry(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070068 if (pnd->netdev == dev)
69 return pnd;
70 }
71 return NULL;
72}
73
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +000074static void phonet_device_destroy(struct net_device *dev)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070075{
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +000076 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 }
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -070095}
96
97struct net_device *phonet_device_get(struct net *net)
98{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +000099 struct phonet_device_list *pndevs = phonet_device_list(net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700100 struct phonet_device *pnd;
Eric Dumazet59e57f42009-07-27 08:03:18 -0700101 struct net_device *dev = NULL;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700102
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000103 spin_lock_bh(&pndevs->lock);
104 list_for_each_entry(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700105 dev = pnd->netdev;
106 BUG_ON(!dev);
107
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000108 if ((dev->reg_state == NETREG_REGISTERED) &&
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700109 ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
110 break;
111 dev = NULL;
112 }
113 if (dev)
114 dev_hold(dev);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000115 spin_unlock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700116 return dev;
117}
118
119int phonet_address_add(struct net_device *dev, u8 addr)
120{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000121 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700122 struct phonet_device *pnd;
123 int err = 0;
124
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000125 spin_lock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700126 /* 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;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000134 spin_unlock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700135 return err;
136}
137
138int phonet_address_del(struct net_device *dev, u8 addr)
139{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000140 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700141 struct phonet_device *pnd;
142 int err = 0;
143
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000144 spin_lock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700145 pnd = __phonet_get(dev);
146 if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs))
147 err = -EADDRNOTAVAIL;
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000148 else if (bitmap_empty(pnd->addrs, 64)) {
149 list_del(&pnd->list);
150 kfree(pnd);
151 }
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000152 spin_unlock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700153 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{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000159 struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700160 struct phonet_device *pnd;
161
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000162 spin_lock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700163 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;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000172 spin_unlock_bh(&pndevs->lock);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700173 return addr;
174}
175
Rémi Denis-Courmont52404882008-12-03 15:42:56 -0800176int phonet_address_lookup(struct net *net, u8 addr)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700177{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000178 struct phonet_device_list *pndevs = phonet_device_list(net);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700179 struct phonet_device *pnd;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000180 int err = -EADDRNOTAVAIL;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700181
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000182 spin_lock_bh(&pndevs->lock);
183 list_for_each_entry(pnd, &pndevs->list, list) {
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700184 /* 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)) {
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000190 err = 0;
191 goto found;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700192 }
193 }
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000194found:
195 spin_unlock_bh(&pndevs->lock);
196 return err;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700197}
198
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000199/* 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;
Rémi Denis-Courmontb11b5162009-09-14 03:10:27 +0000212
213 ASSERT_RTNL();
214 ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device);
215 if (ret)
216 return ret;
217 phonet_address_notify(RTM_NEWADDR, dev,
218 req.ifr_phonet_autoconf.device);
219 return 0;
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000220}
221
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700222/* notify Phonet of device events */
223static int phonet_device_notify(struct notifier_block *me, unsigned long what,
224 void *arg)
225{
226 struct net_device *dev = arg;
227
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000228 switch (what) {
229 case NETDEV_REGISTER:
230 if (dev->type == ARPHRD_PHONET)
231 phonet_device_autoconf(dev);
232 break;
233 case NETDEV_UNREGISTER:
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000234 phonet_device_destroy(dev);
Rémi Denis-Courmontf5bb1c52009-09-09 00:00:05 +0000235 break;
236 }
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700237 return 0;
238
239}
240
241static struct notifier_block phonet_device_notifier = {
242 .notifier_call = phonet_device_notify,
243 .priority = 0,
244};
245
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000246/* Per-namespace Phonet devices handling */
247static int phonet_init_net(struct net *net)
248{
249 struct phonet_net *pnn = kmalloc(sizeof(*pnn), GFP_KERNEL);
250 if (!pnn)
251 return -ENOMEM;
252
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000253 if (!proc_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops)) {
254 kfree(pnn);
255 return -ENOMEM;
256 }
257
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000258 INIT_LIST_HEAD(&pnn->pndevs.list);
259 spin_lock_init(&pnn->pndevs.lock);
260 net_assign_generic(net, phonet_net_id, pnn);
261 return 0;
262}
263
264static void phonet_exit_net(struct net *net)
265{
266 struct phonet_net *pnn = net_generic(net, phonet_net_id);
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000267 struct net_device *dev;
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000268
Rémi Denis-Courmont2be6fa42009-06-24 01:07:45 +0000269 rtnl_lock();
270 for_each_netdev(net, dev)
271 phonet_device_destroy(dev);
272 rtnl_unlock();
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000273
274 proc_net_remove(net, "phonet");
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000275 kfree(pnn);
276}
277
278static struct pernet_operations phonet_net_ops = {
279 .init = phonet_init_net,
280 .exit = phonet_exit_net,
281};
282
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700283/* Initialize Phonet devices list */
remi.denis-courmont@nokia76e02cf2009-01-23 03:00:27 +0000284int __init phonet_device_init(void)
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700285{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000286 int err = register_pernet_gen_device(&phonet_net_id, &phonet_net_ops);
287 if (err)
288 return err;
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000289
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700290 register_netdevice_notifier(&phonet_device_notifier);
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000291 err = phonet_netlink_register();
292 if (err)
293 phonet_device_exit();
294 return err;
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700295}
296
297void phonet_device_exit(void)
298{
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700299 rtnl_unregister_all(PF_PHONET);
remi.denis-courmont@nokia6530e0f2009-01-23 03:00:29 +0000300 unregister_netdevice_notifier(&phonet_device_notifier);
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000301 unregister_pernet_gen_device(phonet_net_id, &phonet_net_ops);
Remi Denis-Courmontf8ff6022008-09-22 20:03:44 -0700302}