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