Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 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> |
David S. Miller | 421d20a | 2009-07-26 13:39:10 -0700 | [diff] [blame] | 30 | #include <linux/proc_fs.h> |
Rémi Denis-Courmont | f5bb1c5 | 2009-09-09 00:00:05 +0000 | [diff] [blame] | 31 | #include <linux/if_arp.h> |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 32 | #include <net/sock.h> |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 33 | #include <net/netns/generic.h> |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 34 | #include <net/phonet/pn_dev.h> |
| 35 | |
Rémi Denis-Courmont | 55748ac | 2009-10-14 00:48:28 +0000 | [diff] [blame] | 36 | struct phonet_routes { |
| 37 | spinlock_t lock; |
| 38 | struct net_device *table[64]; |
| 39 | }; |
| 40 | |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 41 | struct phonet_net { |
| 42 | struct phonet_device_list pndevs; |
Rémi Denis-Courmont | 55748ac | 2009-10-14 00:48:28 +0000 | [diff] [blame] | 43 | struct phonet_routes routes; |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 46 | int phonet_net_id; |
| 47 | |
| 48 | struct phonet_device_list *phonet_device_list(struct net *net) |
| 49 | { |
| 50 | struct phonet_net *pnn = net_generic(net, phonet_net_id); |
| 51 | return &pnn->pndevs; |
| 52 | } |
| 53 | |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 54 | /* Allocate new Phonet device. */ |
| 55 | static struct phonet_device *__phonet_device_alloc(struct net_device *dev) |
| 56 | { |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 57 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 58 | struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC); |
| 59 | if (pnd == NULL) |
| 60 | return NULL; |
| 61 | pnd->netdev = dev; |
| 62 | bitmap_zero(pnd->addrs, 64); |
| 63 | |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 64 | list_add(&pnd->list, &pndevs->list); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 65 | return pnd; |
| 66 | } |
| 67 | |
| 68 | static struct phonet_device *__phonet_get(struct net_device *dev) |
| 69 | { |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 70 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 71 | struct phonet_device *pnd; |
| 72 | |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 73 | list_for_each_entry(pnd, &pndevs->list, list) { |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 74 | if (pnd->netdev == dev) |
| 75 | return pnd; |
| 76 | } |
| 77 | return NULL; |
| 78 | } |
| 79 | |
Rémi Denis-Courmont | 2be6fa4 | 2009-06-24 01:07:45 +0000 | [diff] [blame] | 80 | static void phonet_device_destroy(struct net_device *dev) |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 81 | { |
Rémi Denis-Courmont | 2be6fa4 | 2009-06-24 01:07:45 +0000 | [diff] [blame] | 82 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
| 83 | struct phonet_device *pnd; |
| 84 | |
| 85 | ASSERT_RTNL(); |
| 86 | |
| 87 | spin_lock_bh(&pndevs->lock); |
| 88 | pnd = __phonet_get(dev); |
| 89 | if (pnd) |
| 90 | list_del(&pnd->list); |
| 91 | spin_unlock_bh(&pndevs->lock); |
| 92 | |
| 93 | if (pnd) { |
| 94 | u8 addr; |
| 95 | |
| 96 | for (addr = find_first_bit(pnd->addrs, 64); addr < 64; |
| 97 | addr = find_next_bit(pnd->addrs, 64, 1+addr)) |
| 98 | phonet_address_notify(RTM_DELADDR, dev, addr); |
| 99 | kfree(pnd); |
| 100 | } |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | struct net_device *phonet_device_get(struct net *net) |
| 104 | { |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 105 | struct phonet_device_list *pndevs = phonet_device_list(net); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 106 | struct phonet_device *pnd; |
Eric Dumazet | 59e57f4 | 2009-07-27 08:03:18 -0700 | [diff] [blame] | 107 | struct net_device *dev = NULL; |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 108 | |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 109 | spin_lock_bh(&pndevs->lock); |
| 110 | list_for_each_entry(pnd, &pndevs->list, list) { |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 111 | dev = pnd->netdev; |
| 112 | BUG_ON(!dev); |
| 113 | |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 114 | if ((dev->reg_state == NETREG_REGISTERED) && |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 115 | ((pnd->netdev->flags & IFF_UP)) == IFF_UP) |
| 116 | break; |
| 117 | dev = NULL; |
| 118 | } |
| 119 | if (dev) |
| 120 | dev_hold(dev); |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 121 | spin_unlock_bh(&pndevs->lock); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 122 | return dev; |
| 123 | } |
| 124 | |
| 125 | int phonet_address_add(struct net_device *dev, u8 addr) |
| 126 | { |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 127 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 128 | struct phonet_device *pnd; |
| 129 | int err = 0; |
| 130 | |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 131 | spin_lock_bh(&pndevs->lock); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 132 | /* Find or create Phonet-specific device data */ |
| 133 | pnd = __phonet_get(dev); |
| 134 | if (pnd == NULL) |
| 135 | pnd = __phonet_device_alloc(dev); |
| 136 | if (unlikely(pnd == NULL)) |
| 137 | err = -ENOMEM; |
| 138 | else if (test_and_set_bit(addr >> 2, pnd->addrs)) |
| 139 | err = -EEXIST; |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 140 | spin_unlock_bh(&pndevs->lock); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 141 | return err; |
| 142 | } |
| 143 | |
| 144 | int phonet_address_del(struct net_device *dev, u8 addr) |
| 145 | { |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 146 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 147 | struct phonet_device *pnd; |
| 148 | int err = 0; |
| 149 | |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 150 | spin_lock_bh(&pndevs->lock); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 151 | pnd = __phonet_get(dev); |
| 152 | if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) |
| 153 | err = -EADDRNOTAVAIL; |
Rémi Denis-Courmont | 2be6fa4 | 2009-06-24 01:07:45 +0000 | [diff] [blame] | 154 | else if (bitmap_empty(pnd->addrs, 64)) { |
| 155 | list_del(&pnd->list); |
| 156 | kfree(pnd); |
| 157 | } |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 158 | spin_unlock_bh(&pndevs->lock); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 159 | return err; |
| 160 | } |
| 161 | |
| 162 | /* Gets a source address toward a destination, through a interface. */ |
Rémi Denis-Courmont | 55748ac | 2009-10-14 00:48:28 +0000 | [diff] [blame] | 163 | u8 phonet_address_get(struct net_device *dev, u8 daddr) |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 164 | { |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 165 | struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev)); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 166 | struct phonet_device *pnd; |
Rémi Denis-Courmont | 55748ac | 2009-10-14 00:48:28 +0000 | [diff] [blame] | 167 | u8 saddr; |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 168 | |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 169 | spin_lock_bh(&pndevs->lock); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 170 | pnd = __phonet_get(dev); |
| 171 | if (pnd) { |
| 172 | BUG_ON(bitmap_empty(pnd->addrs, 64)); |
| 173 | |
| 174 | /* Use same source address as destination, if possible */ |
Rémi Denis-Courmont | 55748ac | 2009-10-14 00:48:28 +0000 | [diff] [blame] | 175 | if (test_bit(daddr >> 2, pnd->addrs)) |
| 176 | saddr = daddr; |
| 177 | else |
| 178 | saddr = find_first_bit(pnd->addrs, 64) << 2; |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 179 | } else |
Rémi Denis-Courmont | 55748ac | 2009-10-14 00:48:28 +0000 | [diff] [blame] | 180 | saddr = PN_NO_ADDR; |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 181 | spin_unlock_bh(&pndevs->lock); |
Rémi Denis-Courmont | 55748ac | 2009-10-14 00:48:28 +0000 | [diff] [blame] | 182 | |
| 183 | if (saddr == PN_NO_ADDR) { |
| 184 | /* Fallback to another device */ |
| 185 | struct net_device *def_dev; |
| 186 | |
| 187 | def_dev = phonet_device_get(dev_net(dev)); |
| 188 | if (def_dev) { |
| 189 | if (def_dev != dev) |
| 190 | saddr = phonet_address_get(def_dev, daddr); |
| 191 | dev_put(def_dev); |
| 192 | } |
| 193 | } |
| 194 | return saddr; |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Rémi Denis-Courmont | 5240488 | 2008-12-03 15:42:56 -0800 | [diff] [blame] | 197 | int phonet_address_lookup(struct net *net, u8 addr) |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 198 | { |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 199 | struct phonet_device_list *pndevs = phonet_device_list(net); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 200 | struct phonet_device *pnd; |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 201 | int err = -EADDRNOTAVAIL; |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 202 | |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 203 | spin_lock_bh(&pndevs->lock); |
| 204 | list_for_each_entry(pnd, &pndevs->list, list) { |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 205 | /* Don't allow unregistering devices! */ |
| 206 | if ((pnd->netdev->reg_state != NETREG_REGISTERED) || |
| 207 | ((pnd->netdev->flags & IFF_UP)) != IFF_UP) |
| 208 | continue; |
| 209 | |
| 210 | if (test_bit(addr >> 2, pnd->addrs)) { |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 211 | err = 0; |
| 212 | goto found; |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 215 | found: |
| 216 | spin_unlock_bh(&pndevs->lock); |
| 217 | return err; |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Rémi Denis-Courmont | f5bb1c5 | 2009-09-09 00:00:05 +0000 | [diff] [blame] | 220 | /* automatically configure a Phonet device, if supported */ |
| 221 | static int phonet_device_autoconf(struct net_device *dev) |
| 222 | { |
| 223 | struct if_phonet_req req; |
| 224 | int ret; |
| 225 | |
| 226 | if (!dev->netdev_ops->ndo_do_ioctl) |
| 227 | return -EOPNOTSUPP; |
| 228 | |
| 229 | ret = dev->netdev_ops->ndo_do_ioctl(dev, (struct ifreq *)&req, |
| 230 | SIOCPNGAUTOCONF); |
| 231 | if (ret < 0) |
| 232 | return ret; |
Rémi Denis-Courmont | b11b516 | 2009-09-14 03:10:27 +0000 | [diff] [blame] | 233 | |
| 234 | ASSERT_RTNL(); |
| 235 | ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device); |
| 236 | if (ret) |
| 237 | return ret; |
| 238 | phonet_address_notify(RTM_NEWADDR, dev, |
| 239 | req.ifr_phonet_autoconf.device); |
| 240 | return 0; |
Rémi Denis-Courmont | f5bb1c5 | 2009-09-09 00:00:05 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Rémi Denis-Courmont | f062f41 | 2009-10-14 00:48:29 +0000 | [diff] [blame^] | 243 | static void phonet_route_autodel(struct net_device *dev) |
| 244 | { |
| 245 | struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id); |
| 246 | unsigned i; |
| 247 | DECLARE_BITMAP(deleted, 64); |
| 248 | |
| 249 | /* Remove left-over Phonet routes */ |
| 250 | bitmap_zero(deleted, 64); |
| 251 | spin_lock_bh(&pnn->routes.lock); |
| 252 | for (i = 0; i < 64; i++) |
| 253 | if (dev == pnn->routes.table[i]) { |
| 254 | set_bit(i, deleted); |
| 255 | pnn->routes.table[i] = NULL; |
| 256 | dev_put(dev); |
| 257 | } |
| 258 | spin_unlock_bh(&pnn->routes.lock); |
| 259 | for (i = find_first_bit(deleted, 64); i < 64; |
| 260 | i = find_next_bit(deleted, 64, i + 1)) |
| 261 | rtm_phonet_notify(RTM_DELROUTE, dev, i); |
| 262 | } |
| 263 | |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 264 | /* notify Phonet of device events */ |
| 265 | static int phonet_device_notify(struct notifier_block *me, unsigned long what, |
| 266 | void *arg) |
| 267 | { |
| 268 | struct net_device *dev = arg; |
| 269 | |
Rémi Denis-Courmont | f5bb1c5 | 2009-09-09 00:00:05 +0000 | [diff] [blame] | 270 | switch (what) { |
| 271 | case NETDEV_REGISTER: |
| 272 | if (dev->type == ARPHRD_PHONET) |
| 273 | phonet_device_autoconf(dev); |
| 274 | break; |
| 275 | case NETDEV_UNREGISTER: |
Rémi Denis-Courmont | 2be6fa4 | 2009-06-24 01:07:45 +0000 | [diff] [blame] | 276 | phonet_device_destroy(dev); |
Rémi Denis-Courmont | f062f41 | 2009-10-14 00:48:29 +0000 | [diff] [blame^] | 277 | phonet_route_autodel(dev); |
Rémi Denis-Courmont | f5bb1c5 | 2009-09-09 00:00:05 +0000 | [diff] [blame] | 278 | break; |
| 279 | } |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 280 | return 0; |
| 281 | |
| 282 | } |
| 283 | |
| 284 | static struct notifier_block phonet_device_notifier = { |
| 285 | .notifier_call = phonet_device_notify, |
| 286 | .priority = 0, |
| 287 | }; |
| 288 | |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 289 | /* Per-namespace Phonet devices handling */ |
| 290 | static int phonet_init_net(struct net *net) |
| 291 | { |
Rémi Denis-Courmont | 55748ac | 2009-10-14 00:48:28 +0000 | [diff] [blame] | 292 | struct phonet_net *pnn = kzalloc(sizeof(*pnn), GFP_KERNEL); |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 293 | if (!pnn) |
| 294 | return -ENOMEM; |
| 295 | |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 296 | if (!proc_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops)) { |
| 297 | kfree(pnn); |
| 298 | return -ENOMEM; |
| 299 | } |
| 300 | |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 301 | INIT_LIST_HEAD(&pnn->pndevs.list); |
| 302 | spin_lock_init(&pnn->pndevs.lock); |
Rémi Denis-Courmont | 55748ac | 2009-10-14 00:48:28 +0000 | [diff] [blame] | 303 | spin_lock_init(&pnn->routes.lock); |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 304 | net_assign_generic(net, phonet_net_id, pnn); |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static void phonet_exit_net(struct net *net) |
| 309 | { |
| 310 | struct phonet_net *pnn = net_generic(net, phonet_net_id); |
Rémi Denis-Courmont | 2be6fa4 | 2009-06-24 01:07:45 +0000 | [diff] [blame] | 311 | struct net_device *dev; |
Rémi Denis-Courmont | f062f41 | 2009-10-14 00:48:29 +0000 | [diff] [blame^] | 312 | unsigned i; |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 313 | |
Rémi Denis-Courmont | 2be6fa4 | 2009-06-24 01:07:45 +0000 | [diff] [blame] | 314 | rtnl_lock(); |
| 315 | for_each_netdev(net, dev) |
| 316 | phonet_device_destroy(dev); |
Rémi Denis-Courmont | f062f41 | 2009-10-14 00:48:29 +0000 | [diff] [blame^] | 317 | |
| 318 | for (i = 0; i < 64; i++) { |
| 319 | dev = pnn->routes.table[i]; |
| 320 | if (dev) { |
| 321 | rtm_phonet_notify(RTM_DELROUTE, dev, i); |
| 322 | dev_put(dev); |
| 323 | } |
| 324 | } |
Rémi Denis-Courmont | 2be6fa4 | 2009-06-24 01:07:45 +0000 | [diff] [blame] | 325 | rtnl_unlock(); |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 326 | |
| 327 | proc_net_remove(net, "phonet"); |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 328 | kfree(pnn); |
| 329 | } |
| 330 | |
| 331 | static struct pernet_operations phonet_net_ops = { |
| 332 | .init = phonet_init_net, |
| 333 | .exit = phonet_exit_net, |
| 334 | }; |
| 335 | |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 336 | /* Initialize Phonet devices list */ |
remi.denis-courmont@nokia | 76e02cf | 2009-01-23 03:00:27 +0000 | [diff] [blame] | 337 | int __init phonet_device_init(void) |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 338 | { |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 339 | int err = register_pernet_gen_device(&phonet_net_id, &phonet_net_ops); |
| 340 | if (err) |
| 341 | return err; |
remi.denis-courmont@nokia | 660f706 | 2009-01-23 03:00:28 +0000 | [diff] [blame] | 342 | |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 343 | register_netdevice_notifier(&phonet_device_notifier); |
remi.denis-courmont@nokia | 660f706 | 2009-01-23 03:00:28 +0000 | [diff] [blame] | 344 | err = phonet_netlink_register(); |
| 345 | if (err) |
| 346 | phonet_device_exit(); |
| 347 | return err; |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void phonet_device_exit(void) |
| 351 | { |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 352 | rtnl_unregister_all(PF_PHONET); |
remi.denis-courmont@nokia | 6530e0f | 2009-01-23 03:00:29 +0000 | [diff] [blame] | 353 | unregister_netdevice_notifier(&phonet_device_notifier); |
remi.denis-courmont@nokia | 9a3b7a4 | 2009-01-23 03:00:30 +0000 | [diff] [blame] | 354 | unregister_pernet_gen_device(phonet_net_id, &phonet_net_ops); |
Remi Denis-Courmont | f8ff602 | 2008-09-22 20:03:44 -0700 | [diff] [blame] | 355 | } |
Rémi Denis-Courmont | 55748ac | 2009-10-14 00:48:28 +0000 | [diff] [blame] | 356 | |
| 357 | int phonet_route_add(struct net_device *dev, u8 daddr) |
| 358 | { |
| 359 | struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id); |
| 360 | struct phonet_routes *routes = &pnn->routes; |
| 361 | int err = -EEXIST; |
| 362 | |
| 363 | daddr = daddr >> 2; |
| 364 | spin_lock_bh(&routes->lock); |
| 365 | if (routes->table[daddr] == NULL) { |
| 366 | routes->table[daddr] = dev; |
| 367 | dev_hold(dev); |
| 368 | err = 0; |
| 369 | } |
| 370 | spin_unlock_bh(&routes->lock); |
| 371 | return err; |
| 372 | } |
| 373 | |
| 374 | int phonet_route_del(struct net_device *dev, u8 daddr) |
| 375 | { |
| 376 | struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id); |
| 377 | struct phonet_routes *routes = &pnn->routes; |
| 378 | int err = -ENOENT; |
| 379 | |
| 380 | daddr = daddr >> 2; |
| 381 | spin_lock_bh(&routes->lock); |
| 382 | if (dev == routes->table[daddr]) { |
| 383 | routes->table[daddr] = NULL; |
| 384 | dev_put(dev); |
| 385 | err = 0; |
| 386 | } |
| 387 | spin_unlock_bh(&routes->lock); |
| 388 | return err; |
| 389 | } |
| 390 | |
| 391 | struct net_device *phonet_route_get(struct net *net, u8 daddr) |
| 392 | { |
| 393 | struct phonet_net *pnn = net_generic(net, phonet_net_id); |
| 394 | struct phonet_routes *routes = &pnn->routes; |
| 395 | struct net_device *dev; |
| 396 | |
| 397 | ASSERT_RTNL(); /* no need to hold the device */ |
| 398 | |
| 399 | daddr >>= 2; |
| 400 | spin_lock_bh(&routes->lock); |
| 401 | dev = routes->table[daddr]; |
| 402 | spin_unlock_bh(&routes->lock); |
| 403 | return dev; |
| 404 | } |
| 405 | |
| 406 | struct net_device *phonet_route_output(struct net *net, u8 daddr) |
| 407 | { |
| 408 | struct phonet_net *pnn = net_generic(net, phonet_net_id); |
| 409 | struct phonet_routes *routes = &pnn->routes; |
| 410 | struct net_device *dev; |
| 411 | |
| 412 | spin_lock_bh(&routes->lock); |
| 413 | dev = routes->table[daddr >> 2]; |
| 414 | if (dev) |
| 415 | dev_hold(dev); |
| 416 | spin_unlock_bh(&routes->lock); |
| 417 | |
| 418 | if (!dev) |
| 419 | dev = phonet_device_get(net); /* Default route */ |
| 420 | return dev; |
| 421 | } |