Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and |
| 3 | * James Leu (jleu@mindspring.net). |
| 4 | * Copyright (C) 2001 by various other people who didn't put their name here. |
| 5 | * Licensed under the GPL. |
| 6 | */ |
| 7 | |
| 8 | #include "linux/config.h" |
| 9 | #include "linux/kernel.h" |
| 10 | #include "linux/netdevice.h" |
| 11 | #include "linux/rtnetlink.h" |
| 12 | #include "linux/skbuff.h" |
| 13 | #include "linux/socket.h" |
| 14 | #include "linux/spinlock.h" |
| 15 | #include "linux/module.h" |
| 16 | #include "linux/init.h" |
| 17 | #include "linux/etherdevice.h" |
| 18 | #include "linux/list.h" |
| 19 | #include "linux/inetdevice.h" |
| 20 | #include "linux/ctype.h" |
| 21 | #include "linux/bootmem.h" |
| 22 | #include "linux/ethtool.h" |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 23 | #include "linux/platform_device.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "asm/uaccess.h" |
| 25 | #include "user_util.h" |
| 26 | #include "kern_util.h" |
| 27 | #include "net_kern.h" |
| 28 | #include "net_user.h" |
| 29 | #include "mconsole_kern.h" |
| 30 | #include "init.h" |
| 31 | #include "irq_user.h" |
| 32 | #include "irq_kern.h" |
| 33 | |
Paolo 'Blaisorblade' Giarrusso | bf61f50 | 2006-09-25 23:33:07 -0700 | [diff] [blame] | 34 | static inline void set_ether_mac(struct net_device *dev, unsigned char *addr) |
| 35 | { |
| 36 | memcpy(dev->dev_addr, addr, ETH_ALEN); |
| 37 | } |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #define DRIVER_NAME "uml-netdev" |
| 40 | |
| 41 | static DEFINE_SPINLOCK(opened_lock); |
Jeff Dike | 9010772 | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 42 | static LIST_HEAD(opened); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | static int uml_net_rx(struct net_device *dev) |
| 45 | { |
| 46 | struct uml_net_private *lp = dev->priv; |
| 47 | int pkt_len; |
| 48 | struct sk_buff *skb; |
| 49 | |
| 50 | /* If we can't allocate memory, try again next round. */ |
| 51 | skb = dev_alloc_skb(dev->mtu); |
| 52 | if (skb == NULL) { |
| 53 | lp->stats.rx_dropped++; |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | skb->dev = dev; |
| 58 | skb_put(skb, dev->mtu); |
| 59 | skb->mac.raw = skb->data; |
| 60 | pkt_len = (*lp->read)(lp->fd, &skb, lp); |
| 61 | |
| 62 | if (pkt_len > 0) { |
| 63 | skb_trim(skb, pkt_len); |
| 64 | skb->protocol = (*lp->protocol)(skb); |
| 65 | netif_rx(skb); |
| 66 | |
| 67 | lp->stats.rx_bytes += skb->len; |
| 68 | lp->stats.rx_packets++; |
| 69 | return pkt_len; |
| 70 | } |
| 71 | |
| 72 | kfree_skb(skb); |
| 73 | return pkt_len; |
| 74 | } |
| 75 | |
Paolo 'Blaisorblade' Giarrusso | 71c8d4c | 2006-01-18 17:42:56 -0800 | [diff] [blame] | 76 | static void uml_dev_close(void* dev) |
| 77 | { |
| 78 | dev_close( (struct net_device *) dev); |
| 79 | } |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | irqreturn_t uml_net_interrupt(int irq, void *dev_id, struct pt_regs *regs) |
| 82 | { |
| 83 | struct net_device *dev = dev_id; |
| 84 | struct uml_net_private *lp = dev->priv; |
| 85 | int err; |
| 86 | |
| 87 | if(!netif_running(dev)) |
| 88 | return(IRQ_NONE); |
| 89 | |
| 90 | spin_lock(&lp->lock); |
| 91 | while((err = uml_net_rx(dev)) > 0) ; |
| 92 | if(err < 0) { |
Paolo 'Blaisorblade' Giarrusso | 71c8d4c | 2006-01-18 17:42:56 -0800 | [diff] [blame] | 93 | DECLARE_WORK(close_work, uml_dev_close, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | printk(KERN_ERR |
| 95 | "Device '%s' read returned %d, shutting it down\n", |
| 96 | dev->name, err); |
Paolo 'Blaisorblade' Giarrusso | 71c8d4c | 2006-01-18 17:42:56 -0800 | [diff] [blame] | 97 | /* dev_close can't be called in interrupt context, and takes |
| 98 | * again lp->lock. |
| 99 | * And dev_close() can be safely called multiple times on the |
| 100 | * same device, since it tests for (dev->flags & IFF_UP). So |
| 101 | * there's no harm in delaying the device shutdown. */ |
| 102 | schedule_work(&close_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | goto out; |
| 104 | } |
| 105 | reactivate_fd(lp->fd, UM_ETH_IRQ); |
| 106 | |
Paolo 'Blaisorblade' Giarrusso | 71c8d4c | 2006-01-18 17:42:56 -0800 | [diff] [blame] | 107 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | spin_unlock(&lp->lock); |
| 109 | return(IRQ_HANDLED); |
| 110 | } |
| 111 | |
| 112 | static int uml_net_open(struct net_device *dev) |
| 113 | { |
| 114 | struct uml_net_private *lp = dev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | int err; |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | if(lp->fd >= 0){ |
| 118 | err = -ENXIO; |
| 119 | goto out; |
| 120 | } |
| 121 | |
| 122 | if(!lp->have_mac){ |
Bodo Stroesser | 0e76422 | 2005-11-07 00:58:47 -0800 | [diff] [blame] | 123 | dev_ip_addr(dev, &lp->mac[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | set_ether_mac(dev, lp->mac); |
| 125 | } |
| 126 | |
| 127 | lp->fd = (*lp->open)(&lp->user); |
| 128 | if(lp->fd < 0){ |
| 129 | err = lp->fd; |
| 130 | goto out; |
| 131 | } |
| 132 | |
| 133 | err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt, |
Thomas Gleixner | bd6aa65 | 2006-07-01 19:29:27 -0700 | [diff] [blame] | 134 | IRQF_DISABLED | IRQF_SHARED, dev->name, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | if(err != 0){ |
| 136 | printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | err = -ENETUNREACH; |
Jeff Dike | 14d9ead | 2006-02-07 12:58:42 -0800 | [diff] [blame] | 138 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | lp->tl.data = (unsigned long) &lp->user; |
| 142 | netif_start_queue(dev); |
| 143 | |
| 144 | /* clear buffer - it can happen that the host side of the interface |
| 145 | * is full when we get here. In this case, new data is never queued, |
| 146 | * SIGIOs never arrive, and the net never works. |
| 147 | */ |
| 148 | while((err = uml_net_rx(dev)) > 0) ; |
| 149 | |
Jeff Dike | 14d9ead | 2006-02-07 12:58:42 -0800 | [diff] [blame] | 150 | spin_lock(&opened_lock); |
| 151 | list_add(&lp->list, &opened); |
| 152 | spin_unlock(&opened_lock); |
| 153 | |
| 154 | return 0; |
| 155 | out_close: |
| 156 | if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user); |
| 157 | lp->fd = -1; |
| 158 | out: |
Jeff Dike | 14d9ead | 2006-02-07 12:58:42 -0800 | [diff] [blame] | 159 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static int uml_net_close(struct net_device *dev) |
| 163 | { |
| 164 | struct uml_net_private *lp = dev->priv; |
| 165 | |
| 166 | netif_stop_queue(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | free_irq(dev->irq, dev); |
| 169 | if(lp->close != NULL) |
| 170 | (*lp->close)(lp->fd, &lp->user); |
| 171 | lp->fd = -1; |
| 172 | |
Jeff Dike | 14d9ead | 2006-02-07 12:58:42 -0800 | [diff] [blame] | 173 | spin_lock(&opened_lock); |
| 174 | list_del(&lp->list); |
| 175 | spin_unlock(&opened_lock); |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 181 | { |
| 182 | struct uml_net_private *lp = dev->priv; |
| 183 | unsigned long flags; |
| 184 | int len; |
| 185 | |
| 186 | netif_stop_queue(dev); |
| 187 | |
| 188 | spin_lock_irqsave(&lp->lock, flags); |
| 189 | |
| 190 | len = (*lp->write)(lp->fd, &skb, lp); |
| 191 | |
| 192 | if(len == skb->len) { |
| 193 | lp->stats.tx_packets++; |
| 194 | lp->stats.tx_bytes += skb->len; |
| 195 | dev->trans_start = jiffies; |
| 196 | netif_start_queue(dev); |
| 197 | |
| 198 | /* this is normally done in the interrupt when tx finishes */ |
| 199 | netif_wake_queue(dev); |
| 200 | } |
| 201 | else if(len == 0){ |
| 202 | netif_start_queue(dev); |
| 203 | lp->stats.tx_dropped++; |
| 204 | } |
| 205 | else { |
| 206 | netif_start_queue(dev); |
| 207 | printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len); |
| 208 | } |
| 209 | |
| 210 | spin_unlock_irqrestore(&lp->lock, flags); |
| 211 | |
| 212 | dev_kfree_skb(skb); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static struct net_device_stats *uml_net_get_stats(struct net_device *dev) |
| 218 | { |
| 219 | struct uml_net_private *lp = dev->priv; |
| 220 | return &lp->stats; |
| 221 | } |
| 222 | |
| 223 | static void uml_net_set_multicast_list(struct net_device *dev) |
| 224 | { |
| 225 | if (dev->flags & IFF_PROMISC) return; |
| 226 | else if (dev->mc_count) dev->flags |= IFF_ALLMULTI; |
| 227 | else dev->flags &= ~IFF_ALLMULTI; |
| 228 | } |
| 229 | |
| 230 | static void uml_net_tx_timeout(struct net_device *dev) |
| 231 | { |
| 232 | dev->trans_start = jiffies; |
| 233 | netif_wake_queue(dev); |
| 234 | } |
| 235 | |
| 236 | static int uml_net_set_mac(struct net_device *dev, void *addr) |
| 237 | { |
| 238 | struct uml_net_private *lp = dev->priv; |
| 239 | struct sockaddr *hwaddr = addr; |
| 240 | |
Paolo 'Blaisorblade' Giarrusso | 48af05e | 2006-09-27 01:50:31 -0700 | [diff] [blame] | 241 | spin_lock_irq(&lp->lock); |
Paolo 'Blaisorblade' Giarrusso | bf61f50 | 2006-09-25 23:33:07 -0700 | [diff] [blame] | 242 | set_ether_mac(dev, hwaddr->sa_data); |
Paolo 'Blaisorblade' Giarrusso | 48af05e | 2006-09-27 01:50:31 -0700 | [diff] [blame] | 243 | spin_unlock_irq(&lp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | return(0); |
| 246 | } |
| 247 | |
| 248 | static int uml_net_change_mtu(struct net_device *dev, int new_mtu) |
| 249 | { |
| 250 | struct uml_net_private *lp = dev->priv; |
| 251 | int err = 0; |
| 252 | |
Paolo 'Blaisorblade' Giarrusso | 48af05e | 2006-09-27 01:50:31 -0700 | [diff] [blame] | 253 | spin_lock_irq(&lp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | new_mtu = (*lp->set_mtu)(new_mtu, &lp->user); |
| 256 | if(new_mtu < 0){ |
| 257 | err = new_mtu; |
| 258 | goto out; |
| 259 | } |
| 260 | |
| 261 | dev->mtu = new_mtu; |
| 262 | |
| 263 | out: |
Paolo 'Blaisorblade' Giarrusso | 48af05e | 2006-09-27 01:50:31 -0700 | [diff] [blame] | 264 | spin_unlock_irq(&lp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | return err; |
| 266 | } |
| 267 | |
Christoph Hellwig | 6d38748 | 2005-11-07 06:21:21 +0100 | [diff] [blame] | 268 | static void uml_net_get_drvinfo(struct net_device *dev, |
| 269 | struct ethtool_drvinfo *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
Christoph Hellwig | 6d38748 | 2005-11-07 06:21:21 +0100 | [diff] [blame] | 271 | strcpy(info->driver, DRIVER_NAME); |
| 272 | strcpy(info->version, "42"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Christoph Hellwig | 6d38748 | 2005-11-07 06:21:21 +0100 | [diff] [blame] | 275 | static struct ethtool_ops uml_net_ethtool_ops = { |
| 276 | .get_drvinfo = uml_net_get_drvinfo, |
| 277 | .get_link = ethtool_op_get_link, |
| 278 | }; |
| 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | void uml_net_user_timer_expire(unsigned long _conn) |
| 281 | { |
| 282 | #ifdef undef |
| 283 | struct connection *conn = (struct connection *)_conn; |
| 284 | |
| 285 | dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn); |
| 286 | do_connect(conn); |
| 287 | #endif |
| 288 | } |
| 289 | |
| 290 | static DEFINE_SPINLOCK(devices_lock); |
Jeff Dike | 9010772 | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 291 | static LIST_HEAD(devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 293 | static struct platform_driver uml_net_driver = { |
| 294 | .driver = { |
| 295 | .name = DRIVER_NAME, |
| 296 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | }; |
| 298 | static int driver_registered; |
| 299 | |
| 300 | static int eth_configure(int n, void *init, char *mac, |
| 301 | struct transport *transport) |
| 302 | { |
| 303 | struct uml_net *device; |
| 304 | struct net_device *dev; |
| 305 | struct uml_net_private *lp; |
| 306 | int save, err, size; |
| 307 | |
| 308 | size = transport->private_size + sizeof(struct uml_net_private) + |
| 309 | sizeof(((struct uml_net_private *) 0)->user); |
| 310 | |
| 311 | device = kmalloc(sizeof(*device), GFP_KERNEL); |
| 312 | if (device == NULL) { |
| 313 | printk(KERN_ERR "eth_configure failed to allocate uml_net\n"); |
| 314 | return(1); |
| 315 | } |
| 316 | |
| 317 | memset(device, 0, sizeof(*device)); |
| 318 | INIT_LIST_HEAD(&device->list); |
| 319 | device->index = n; |
| 320 | |
| 321 | spin_lock(&devices_lock); |
| 322 | list_add(&device->list, &devices); |
| 323 | spin_unlock(&devices_lock); |
| 324 | |
| 325 | if (setup_etheraddr(mac, device->mac)) |
| 326 | device->have_mac = 1; |
| 327 | |
| 328 | printk(KERN_INFO "Netdevice %d ", n); |
| 329 | if (device->have_mac) |
| 330 | printk("(%02x:%02x:%02x:%02x:%02x:%02x) ", |
| 331 | device->mac[0], device->mac[1], |
| 332 | device->mac[2], device->mac[3], |
| 333 | device->mac[4], device->mac[5]); |
| 334 | printk(": "); |
| 335 | dev = alloc_etherdev(size); |
| 336 | if (dev == NULL) { |
| 337 | printk(KERN_ERR "eth_configure: failed to allocate device\n"); |
| 338 | return 1; |
| 339 | } |
| 340 | |
Paolo 'Blaisorblade' Giarrusso | e56a788 | 2006-01-18 17:42:55 -0800 | [diff] [blame] | 341 | lp = dev->priv; |
| 342 | /* This points to the transport private data. It's still clear, but we |
| 343 | * must memset it to 0 *now*. Let's help the drivers. */ |
| 344 | memset(lp, 0, size); |
| 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | /* sysfs register */ |
| 347 | if (!driver_registered) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 348 | platform_driver_register(¨_net_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | driver_registered = 1; |
| 350 | } |
| 351 | device->pdev.id = n; |
| 352 | device->pdev.name = DRIVER_NAME; |
| 353 | platform_device_register(&device->pdev); |
| 354 | SET_NETDEV_DEV(dev,&device->pdev.dev); |
| 355 | |
| 356 | /* If this name ends up conflicting with an existing registered |
| 357 | * netdevice, that is OK, register_netdev{,ice}() will notice this |
| 358 | * and fail. |
| 359 | */ |
| 360 | snprintf(dev->name, sizeof(dev->name), "eth%d", n); |
| 361 | device->dev = dev; |
| 362 | |
| 363 | (*transport->kern->init)(dev, init); |
| 364 | |
| 365 | dev->mtu = transport->user->max_packet; |
| 366 | dev->open = uml_net_open; |
| 367 | dev->hard_start_xmit = uml_net_start_xmit; |
| 368 | dev->stop = uml_net_close; |
| 369 | dev->get_stats = uml_net_get_stats; |
| 370 | dev->set_multicast_list = uml_net_set_multicast_list; |
| 371 | dev->tx_timeout = uml_net_tx_timeout; |
| 372 | dev->set_mac_address = uml_net_set_mac; |
| 373 | dev->change_mtu = uml_net_change_mtu; |
Christoph Hellwig | 6d38748 | 2005-11-07 06:21:21 +0100 | [diff] [blame] | 374 | dev->ethtool_ops = ¨_net_ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | dev->watchdog_timeo = (HZ >> 1); |
| 376 | dev->irq = UM_ETH_IRQ; |
| 377 | |
| 378 | rtnl_lock(); |
| 379 | err = register_netdevice(dev); |
| 380 | rtnl_unlock(); |
| 381 | if (err) { |
| 382 | device->dev = NULL; |
| 383 | /* XXX: should we call ->remove() here? */ |
| 384 | free_netdev(dev); |
| 385 | return 1; |
| 386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
| 388 | /* lp.user is the first four bytes of the transport data, which |
| 389 | * has already been initialized. This structure assignment will |
| 390 | * overwrite that, so we make sure that .user gets overwritten with |
| 391 | * what it already has. |
| 392 | */ |
| 393 | save = lp->user[0]; |
| 394 | *lp = ((struct uml_net_private) |
| 395 | { .list = LIST_HEAD_INIT(lp->list), |
| 396 | .dev = dev, |
| 397 | .fd = -1, |
| 398 | .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0}, |
| 399 | .have_mac = device->have_mac, |
| 400 | .protocol = transport->kern->protocol, |
| 401 | .open = transport->user->open, |
| 402 | .close = transport->user->close, |
| 403 | .remove = transport->user->remove, |
| 404 | .read = transport->kern->read, |
| 405 | .write = transport->kern->write, |
| 406 | .add_address = transport->user->add_address, |
| 407 | .delete_address = transport->user->delete_address, |
| 408 | .set_mtu = transport->user->set_mtu, |
| 409 | .user = { save } }); |
| 410 | |
| 411 | init_timer(&lp->tl); |
| 412 | spin_lock_init(&lp->lock); |
| 413 | lp->tl.function = uml_net_user_timer_expire; |
| 414 | if (lp->have_mac) |
| 415 | memcpy(lp->mac, device->mac, sizeof(lp->mac)); |
| 416 | |
| 417 | if (transport->user->init) |
| 418 | (*transport->user->init)(&lp->user, dev); |
| 419 | |
| 420 | if (device->have_mac) |
| 421 | set_ether_mac(dev, device->mac); |
| 422 | |
Jeff Dike | 14d9ead | 2006-02-07 12:58:42 -0800 | [diff] [blame] | 423 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static struct uml_net *find_device(int n) |
| 427 | { |
| 428 | struct uml_net *device; |
| 429 | struct list_head *ele; |
| 430 | |
| 431 | spin_lock(&devices_lock); |
| 432 | list_for_each(ele, &devices){ |
| 433 | device = list_entry(ele, struct uml_net, list); |
| 434 | if(device->index == n) |
| 435 | goto out; |
| 436 | } |
| 437 | device = NULL; |
| 438 | out: |
| 439 | spin_unlock(&devices_lock); |
| 440 | return(device); |
| 441 | } |
| 442 | |
| 443 | static int eth_parse(char *str, int *index_out, char **str_out) |
| 444 | { |
| 445 | char *end; |
| 446 | int n; |
| 447 | |
| 448 | n = simple_strtoul(str, &end, 0); |
| 449 | if(end == str){ |
| 450 | printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str); |
| 451 | return(1); |
| 452 | } |
| 453 | if(n < 0){ |
| 454 | printk(KERN_ERR "eth_setup: device %d is negative\n", n); |
| 455 | return(1); |
| 456 | } |
| 457 | str = end; |
| 458 | if(*str != '='){ |
| 459 | printk(KERN_ERR |
| 460 | "eth_setup: expected '=' after device number\n"); |
| 461 | return(1); |
| 462 | } |
| 463 | str++; |
| 464 | if(find_device(n)){ |
| 465 | printk(KERN_ERR "eth_setup: Device %d already configured\n", |
| 466 | n); |
| 467 | return(1); |
| 468 | } |
| 469 | if(index_out) *index_out = n; |
| 470 | *str_out = str; |
| 471 | return(0); |
| 472 | } |
| 473 | |
| 474 | struct eth_init { |
| 475 | struct list_head list; |
| 476 | char *init; |
| 477 | int index; |
| 478 | }; |
| 479 | |
| 480 | /* Filled in at boot time. Will need locking if the transports become |
| 481 | * modular. |
| 482 | */ |
| 483 | struct list_head transports = LIST_HEAD_INIT(transports); |
| 484 | |
| 485 | /* Filled in during early boot */ |
| 486 | struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line); |
| 487 | |
| 488 | static int check_transport(struct transport *transport, char *eth, int n, |
| 489 | void **init_out, char **mac_out) |
| 490 | { |
| 491 | int len; |
| 492 | |
| 493 | len = strlen(transport->name); |
| 494 | if(strncmp(eth, transport->name, len)) |
| 495 | return(0); |
| 496 | |
| 497 | eth += len; |
| 498 | if(*eth == ',') |
| 499 | eth++; |
| 500 | else if(*eth != '\0') |
| 501 | return(0); |
| 502 | |
| 503 | *init_out = kmalloc(transport->setup_size, GFP_KERNEL); |
| 504 | if(*init_out == NULL) |
| 505 | return(1); |
| 506 | |
| 507 | if(!transport->setup(eth, mac_out, *init_out)){ |
| 508 | kfree(*init_out); |
| 509 | *init_out = NULL; |
| 510 | } |
| 511 | return(1); |
| 512 | } |
| 513 | |
| 514 | void register_transport(struct transport *new) |
| 515 | { |
| 516 | struct list_head *ele, *next; |
| 517 | struct eth_init *eth; |
| 518 | void *init; |
| 519 | char *mac = NULL; |
| 520 | int match; |
| 521 | |
| 522 | list_add(&new->list, &transports); |
| 523 | |
| 524 | list_for_each_safe(ele, next, ð_cmd_line){ |
| 525 | eth = list_entry(ele, struct eth_init, list); |
| 526 | match = check_transport(new, eth->init, eth->index, &init, |
| 527 | &mac); |
| 528 | if(!match) |
| 529 | continue; |
| 530 | else if(init != NULL){ |
| 531 | eth_configure(eth->index, init, mac, new); |
| 532 | kfree(init); |
| 533 | } |
| 534 | list_del(ð->list); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | static int eth_setup_common(char *str, int index) |
| 539 | { |
| 540 | struct list_head *ele; |
| 541 | struct transport *transport; |
| 542 | void *init; |
| 543 | char *mac = NULL; |
| 544 | |
| 545 | list_for_each(ele, &transports){ |
| 546 | transport = list_entry(ele, struct transport, list); |
| 547 | if(!check_transport(transport, str, index, &init, &mac)) |
| 548 | continue; |
| 549 | if(init != NULL){ |
| 550 | eth_configure(index, init, mac, transport); |
| 551 | kfree(init); |
| 552 | } |
| 553 | return(1); |
| 554 | } |
| 555 | return(0); |
| 556 | } |
| 557 | |
| 558 | static int eth_setup(char *str) |
| 559 | { |
| 560 | struct eth_init *new; |
| 561 | int n, err; |
| 562 | |
| 563 | err = eth_parse(str, &n, &str); |
Jeff Dike | 1183dc9 | 2006-09-27 01:50:43 -0700 | [diff] [blame] | 564 | if(err) |
| 565 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | |
Jeff Dike | 1183dc9 | 2006-09-27 01:50:43 -0700 | [diff] [blame] | 567 | new = alloc_bootmem(sizeof(*new)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | if (new == NULL){ |
| 569 | printk("eth_init : alloc_bootmem failed\n"); |
Jeff Dike | 1183dc9 | 2006-09-27 01:50:43 -0700 | [diff] [blame] | 570 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | INIT_LIST_HEAD(&new->list); |
| 574 | new->index = n; |
| 575 | new->init = str; |
| 576 | |
| 577 | list_add_tail(&new->list, ð_cmd_line); |
Jeff Dike | 1183dc9 | 2006-09-27 01:50:43 -0700 | [diff] [blame] | 578 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | __setup("eth", eth_setup); |
| 582 | __uml_help(eth_setup, |
| 583 | "eth[0-9]+=<transport>,<options>\n" |
| 584 | " Configure a network device.\n\n" |
| 585 | ); |
| 586 | |
| 587 | #if 0 |
| 588 | static int eth_init(void) |
| 589 | { |
| 590 | struct list_head *ele, *next; |
| 591 | struct eth_init *eth; |
| 592 | |
| 593 | list_for_each_safe(ele, next, ð_cmd_line){ |
| 594 | eth = list_entry(ele, struct eth_init, list); |
| 595 | |
| 596 | if(eth_setup_common(eth->init, eth->index)) |
| 597 | list_del(ð->list); |
| 598 | } |
| 599 | |
| 600 | return(1); |
| 601 | } |
| 602 | __initcall(eth_init); |
| 603 | #endif |
| 604 | |
| 605 | static int net_config(char *str) |
| 606 | { |
| 607 | int n, err; |
| 608 | |
| 609 | err = eth_parse(str, &n, &str); |
| 610 | if(err) return(err); |
| 611 | |
Jeff Dike | 970d6e3 | 2006-01-06 00:18:48 -0800 | [diff] [blame] | 612 | str = kstrdup(str, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | if(str == NULL){ |
| 614 | printk(KERN_ERR "net_config failed to strdup string\n"); |
| 615 | return(-1); |
| 616 | } |
| 617 | err = !eth_setup_common(str, n); |
| 618 | if(err) |
| 619 | kfree(str); |
| 620 | return(err); |
| 621 | } |
| 622 | |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 623 | static int net_id(char **str, int *start_out, int *end_out) |
| 624 | { |
| 625 | char *end; |
| 626 | int n; |
| 627 | |
| 628 | n = simple_strtoul(*str, &end, 0); |
| 629 | if((*end != '\0') || (end == *str)) |
| 630 | return -1; |
| 631 | |
| 632 | *start_out = n; |
| 633 | *end_out = n; |
| 634 | *str = end; |
| 635 | return n; |
| 636 | } |
| 637 | |
| 638 | static int net_remove(int n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | { |
| 640 | struct uml_net *device; |
| 641 | struct net_device *dev; |
| 642 | struct uml_net_private *lp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
| 644 | device = find_device(n); |
| 645 | if(device == NULL) |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 646 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
| 648 | dev = device->dev; |
| 649 | lp = dev->priv; |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 650 | if(lp->fd > 0) |
| 651 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | if(lp->remove != NULL) (*lp->remove)(&lp->user); |
| 653 | unregister_netdev(dev); |
| 654 | platform_device_unregister(&device->pdev); |
| 655 | |
| 656 | list_del(&device->list); |
| 657 | kfree(device); |
| 658 | free_netdev(dev); |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 659 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static struct mc_device net_mc = { |
| 663 | .name = "eth", |
| 664 | .config = net_config, |
| 665 | .get_config = NULL, |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 666 | .id = net_id, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | .remove = net_remove, |
| 668 | }; |
| 669 | |
| 670 | static int uml_inetaddr_event(struct notifier_block *this, unsigned long event, |
| 671 | void *ptr) |
| 672 | { |
| 673 | struct in_ifaddr *ifa = ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | struct net_device *dev = ifa->ifa_dev->dev; |
| 675 | struct uml_net_private *lp; |
| 676 | void (*proc)(unsigned char *, unsigned char *, void *); |
| 677 | unsigned char addr_buf[4], netmask_buf[4]; |
| 678 | |
| 679 | if(dev->open != uml_net_open) return(NOTIFY_DONE); |
| 680 | |
| 681 | lp = dev->priv; |
| 682 | |
| 683 | proc = NULL; |
| 684 | switch (event){ |
| 685 | case NETDEV_UP: |
| 686 | proc = lp->add_address; |
| 687 | break; |
| 688 | case NETDEV_DOWN: |
| 689 | proc = lp->delete_address; |
| 690 | break; |
| 691 | } |
| 692 | if(proc != NULL){ |
Bodo Stroesser | 0e76422 | 2005-11-07 00:58:47 -0800 | [diff] [blame] | 693 | memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf)); |
| 694 | memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | (*proc)(addr_buf, netmask_buf, &lp->user); |
| 696 | } |
| 697 | return(NOTIFY_DONE); |
| 698 | } |
| 699 | |
| 700 | struct notifier_block uml_inetaddr_notifier = { |
| 701 | .notifier_call = uml_inetaddr_event, |
| 702 | }; |
| 703 | |
| 704 | static int uml_net_init(void) |
| 705 | { |
| 706 | struct list_head *ele; |
| 707 | struct uml_net_private *lp; |
| 708 | struct in_device *ip; |
| 709 | struct in_ifaddr *in; |
| 710 | |
| 711 | mconsole_register_dev(&net_mc); |
| 712 | register_inetaddr_notifier(¨_inetaddr_notifier); |
| 713 | |
| 714 | /* Devices may have been opened already, so the uml_inetaddr_notifier |
| 715 | * didn't get a chance to run for them. This fakes it so that |
| 716 | * addresses which have already been set up get handled properly. |
| 717 | */ |
| 718 | list_for_each(ele, &opened){ |
| 719 | lp = list_entry(ele, struct uml_net_private, list); |
| 720 | ip = lp->dev->ip_ptr; |
| 721 | if(ip == NULL) continue; |
| 722 | in = ip->ifa_list; |
| 723 | while(in != NULL){ |
| 724 | uml_inetaddr_event(NULL, NETDEV_UP, in); |
| 725 | in = in->ifa_next; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | return(0); |
| 730 | } |
| 731 | |
| 732 | __initcall(uml_net_init); |
| 733 | |
| 734 | static void close_devices(void) |
| 735 | { |
| 736 | struct list_head *ele; |
| 737 | struct uml_net_private *lp; |
| 738 | |
| 739 | list_for_each(ele, &opened){ |
| 740 | lp = list_entry(ele, struct uml_net_private, list); |
Jeff Dike | 8d93c70 | 2006-01-06 00:19:06 -0800 | [diff] [blame] | 741 | free_irq(lp->dev->irq, lp->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | if((lp->close != NULL) && (lp->fd >= 0)) |
| 743 | (*lp->close)(lp->fd, &lp->user); |
| 744 | if(lp->remove != NULL) (*lp->remove)(&lp->user); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | __uml_exitcall(close_devices); |
| 749 | |
| 750 | int setup_etheraddr(char *str, unsigned char *addr) |
| 751 | { |
| 752 | char *end; |
| 753 | int i; |
| 754 | |
| 755 | if(str == NULL) |
| 756 | return(0); |
| 757 | for(i=0;i<6;i++){ |
| 758 | addr[i] = simple_strtoul(str, &end, 16); |
| 759 | if((end == str) || |
| 760 | ((*end != ':') && (*end != ',') && (*end != '\0'))){ |
| 761 | printk(KERN_ERR |
| 762 | "setup_etheraddr: failed to parse '%s' " |
| 763 | "as an ethernet address\n", str); |
| 764 | return(0); |
| 765 | } |
| 766 | str = end + 1; |
| 767 | } |
| 768 | if(addr[0] & 1){ |
| 769 | printk(KERN_ERR |
| 770 | "Attempt to assign a broadcast ethernet address to a " |
| 771 | "device disallowed\n"); |
| 772 | return(0); |
| 773 | } |
| 774 | return(1); |
| 775 | } |
| 776 | |
Bodo Stroesser | 0e76422 | 2005-11-07 00:58:47 -0800 | [diff] [blame] | 777 | void dev_ip_addr(void *d, unsigned char *bin_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | { |
| 779 | struct net_device *dev = d; |
| 780 | struct in_device *ip = dev->ip_ptr; |
| 781 | struct in_ifaddr *in; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
| 783 | if((ip == NULL) || ((in = ip->ifa_list) == NULL)){ |
| 784 | printk(KERN_WARNING "dev_ip_addr - device not assigned an " |
| 785 | "IP address\n"); |
| 786 | return; |
| 787 | } |
Bodo Stroesser | 0e76422 | 2005-11-07 00:58:47 -0800 | [diff] [blame] | 788 | memcpy(bin_buf, &in->ifa_address, sizeof(in->ifa_address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra) |
| 792 | { |
| 793 | if((skb != NULL) && (skb_tailroom(skb) < extra)){ |
| 794 | struct sk_buff *skb2; |
| 795 | |
| 796 | skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC); |
| 797 | dev_kfree_skb(skb); |
| 798 | skb = skb2; |
| 799 | } |
| 800 | if(skb != NULL) skb_put(skb, extra); |
| 801 | return(skb); |
| 802 | } |
| 803 | |
| 804 | void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *, |
| 805 | void *), |
| 806 | void *arg) |
| 807 | { |
| 808 | struct net_device *dev = d; |
| 809 | struct in_device *ip = dev->ip_ptr; |
| 810 | struct in_ifaddr *in; |
| 811 | unsigned char address[4], netmask[4]; |
| 812 | |
| 813 | if(ip == NULL) return; |
| 814 | in = ip->ifa_list; |
| 815 | while(in != NULL){ |
Bodo Stroesser | 0e76422 | 2005-11-07 00:58:47 -0800 | [diff] [blame] | 816 | memcpy(address, &in->ifa_address, sizeof(address)); |
| 817 | memcpy(netmask, &in->ifa_mask, sizeof(netmask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | (*cb)(address, netmask, arg); |
| 819 | in = in->ifa_next; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | int dev_netmask(void *d, void *m) |
| 824 | { |
| 825 | struct net_device *dev = d; |
| 826 | struct in_device *ip = dev->ip_ptr; |
| 827 | struct in_ifaddr *in; |
Al Viro | a144ea4 | 2006-09-28 18:00:55 -0700 | [diff] [blame^] | 828 | __be32 *mask_out = m; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
| 830 | if(ip == NULL) |
| 831 | return(1); |
| 832 | |
| 833 | in = ip->ifa_list; |
| 834 | if(in == NULL) |
| 835 | return(1); |
| 836 | |
| 837 | *mask_out = in->ifa_mask; |
| 838 | return(0); |
| 839 | } |
| 840 | |
| 841 | void *get_output_buffer(int *len_out) |
| 842 | { |
| 843 | void *ret; |
| 844 | |
| 845 | ret = (void *) __get_free_pages(GFP_KERNEL, 0); |
| 846 | if(ret) *len_out = PAGE_SIZE; |
| 847 | else *len_out = 0; |
| 848 | return(ret); |
| 849 | } |
| 850 | |
| 851 | void free_output_buffer(void *buffer) |
| 852 | { |
| 853 | free_pages((unsigned long) buffer, 0); |
| 854 | } |
| 855 | |
| 856 | int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out, |
| 857 | char **gate_addr) |
| 858 | { |
| 859 | char *remain; |
| 860 | |
| 861 | remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL); |
| 862 | if(remain != NULL){ |
| 863 | printk("tap_setup_common - Extra garbage on specification : " |
| 864 | "'%s'\n", remain); |
| 865 | return(1); |
| 866 | } |
| 867 | |
| 868 | return(0); |
| 869 | } |
| 870 | |
| 871 | unsigned short eth_protocol(struct sk_buff *skb) |
| 872 | { |
| 873 | return(eth_type_trans(skb, skb->dev)); |
| 874 | } |
| 875 | |
| 876 | /* |
| 877 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 878 | * Emacs will notice this stuff at the end of the file and automatically |
| 879 | * adjust the settings for this buffer only. This must remain at the end |
| 880 | * of the file. |
| 881 | * --------------------------------------------------------------------------- |
| 882 | * Local variables: |
| 883 | * c-file-style: "linux" |
| 884 | * End: |
| 885 | */ |