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 | |
Yan Burman | 0268bd0 | 2006-12-12 19:54:52 +0100 | [diff] [blame^] | 337 | device = kzalloc(sizeof(*device), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | if (device == NULL) { |
| 339 | printk(KERN_ERR "eth_configure failed to allocate uml_net\n"); |
| 340 | return(1); |
| 341 | } |
| 342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | INIT_LIST_HEAD(&device->list); |
| 344 | device->index = n; |
| 345 | |
| 346 | spin_lock(&devices_lock); |
| 347 | list_add(&device->list, &devices); |
| 348 | spin_unlock(&devices_lock); |
| 349 | |
Jeff Dike | b10aeee | 2006-09-29 01:58:50 -0700 | [diff] [blame] | 350 | setup_etheraddr(mac, device->mac); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | printk(KERN_INFO "Netdevice %d ", n); |
Jeff Dike | b10aeee | 2006-09-29 01:58:50 -0700 | [diff] [blame] | 353 | printk("(%02x:%02x:%02x:%02x:%02x:%02x) ", |
| 354 | device->mac[0], device->mac[1], |
| 355 | device->mac[2], device->mac[3], |
| 356 | device->mac[4], device->mac[5]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | printk(": "); |
| 358 | dev = alloc_etherdev(size); |
| 359 | if (dev == NULL) { |
| 360 | printk(KERN_ERR "eth_configure: failed to allocate device\n"); |
| 361 | return 1; |
| 362 | } |
| 363 | |
Paolo 'Blaisorblade' Giarrusso | e56a788 | 2006-01-18 17:42:55 -0800 | [diff] [blame] | 364 | lp = dev->priv; |
| 365 | /* This points to the transport private data. It's still clear, but we |
| 366 | * must memset it to 0 *now*. Let's help the drivers. */ |
| 367 | memset(lp, 0, size); |
| 368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | /* sysfs register */ |
| 370 | if (!driver_registered) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 371 | platform_driver_register(¨_net_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | driver_registered = 1; |
| 373 | } |
| 374 | device->pdev.id = n; |
| 375 | device->pdev.name = DRIVER_NAME; |
| 376 | platform_device_register(&device->pdev); |
| 377 | SET_NETDEV_DEV(dev,&device->pdev.dev); |
| 378 | |
| 379 | /* If this name ends up conflicting with an existing registered |
| 380 | * netdevice, that is OK, register_netdev{,ice}() will notice this |
| 381 | * and fail. |
| 382 | */ |
| 383 | snprintf(dev->name, sizeof(dev->name), "eth%d", n); |
| 384 | device->dev = dev; |
| 385 | |
| 386 | (*transport->kern->init)(dev, init); |
| 387 | |
| 388 | dev->mtu = transport->user->max_packet; |
| 389 | dev->open = uml_net_open; |
| 390 | dev->hard_start_xmit = uml_net_start_xmit; |
| 391 | dev->stop = uml_net_close; |
| 392 | dev->get_stats = uml_net_get_stats; |
| 393 | dev->set_multicast_list = uml_net_set_multicast_list; |
| 394 | dev->tx_timeout = uml_net_tx_timeout; |
| 395 | dev->set_mac_address = uml_net_set_mac; |
| 396 | dev->change_mtu = uml_net_change_mtu; |
Christoph Hellwig | 6d38748 | 2005-11-07 06:21:21 +0100 | [diff] [blame] | 397 | dev->ethtool_ops = ¨_net_ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | dev->watchdog_timeo = (HZ >> 1); |
| 399 | dev->irq = UM_ETH_IRQ; |
| 400 | |
| 401 | rtnl_lock(); |
| 402 | err = register_netdevice(dev); |
| 403 | rtnl_unlock(); |
| 404 | if (err) { |
| 405 | device->dev = NULL; |
| 406 | /* XXX: should we call ->remove() here? */ |
| 407 | free_netdev(dev); |
| 408 | return 1; |
| 409 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
| 411 | /* lp.user is the first four bytes of the transport data, which |
| 412 | * has already been initialized. This structure assignment will |
| 413 | * overwrite that, so we make sure that .user gets overwritten with |
| 414 | * what it already has. |
| 415 | */ |
| 416 | save = lp->user[0]; |
| 417 | *lp = ((struct uml_net_private) |
| 418 | { .list = LIST_HEAD_INIT(lp->list), |
| 419 | .dev = dev, |
| 420 | .fd = -1, |
| 421 | .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | .protocol = transport->kern->protocol, |
| 423 | .open = transport->user->open, |
| 424 | .close = transport->user->close, |
| 425 | .remove = transport->user->remove, |
| 426 | .read = transport->kern->read, |
| 427 | .write = transport->kern->write, |
| 428 | .add_address = transport->user->add_address, |
| 429 | .delete_address = transport->user->delete_address, |
| 430 | .set_mtu = transport->user->set_mtu, |
| 431 | .user = { save } }); |
| 432 | |
| 433 | init_timer(&lp->tl); |
| 434 | spin_lock_init(&lp->lock); |
| 435 | lp->tl.function = uml_net_user_timer_expire; |
Jeff Dike | b10aeee | 2006-09-29 01:58:50 -0700 | [diff] [blame] | 436 | memcpy(lp->mac, device->mac, sizeof(lp->mac)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
| 438 | if (transport->user->init) |
| 439 | (*transport->user->init)(&lp->user, dev); |
| 440 | |
Jeff Dike | b10aeee | 2006-09-29 01:58:50 -0700 | [diff] [blame] | 441 | set_ether_mac(dev, device->mac); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Jeff Dike | 14d9ead | 2006-02-07 12:58:42 -0800 | [diff] [blame] | 443 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static struct uml_net *find_device(int n) |
| 447 | { |
| 448 | struct uml_net *device; |
| 449 | struct list_head *ele; |
| 450 | |
| 451 | spin_lock(&devices_lock); |
| 452 | list_for_each(ele, &devices){ |
| 453 | device = list_entry(ele, struct uml_net, list); |
| 454 | if(device->index == n) |
| 455 | goto out; |
| 456 | } |
| 457 | device = NULL; |
| 458 | out: |
| 459 | spin_unlock(&devices_lock); |
| 460 | return(device); |
| 461 | } |
| 462 | |
| 463 | static int eth_parse(char *str, int *index_out, char **str_out) |
| 464 | { |
| 465 | char *end; |
| 466 | int n; |
| 467 | |
| 468 | n = simple_strtoul(str, &end, 0); |
| 469 | if(end == str){ |
| 470 | printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str); |
| 471 | return(1); |
| 472 | } |
| 473 | if(n < 0){ |
| 474 | printk(KERN_ERR "eth_setup: device %d is negative\n", n); |
| 475 | return(1); |
| 476 | } |
| 477 | str = end; |
| 478 | if(*str != '='){ |
| 479 | printk(KERN_ERR |
| 480 | "eth_setup: expected '=' after device number\n"); |
| 481 | return(1); |
| 482 | } |
| 483 | str++; |
| 484 | if(find_device(n)){ |
| 485 | printk(KERN_ERR "eth_setup: Device %d already configured\n", |
| 486 | n); |
| 487 | return(1); |
| 488 | } |
| 489 | if(index_out) *index_out = n; |
| 490 | *str_out = str; |
| 491 | return(0); |
| 492 | } |
| 493 | |
| 494 | struct eth_init { |
| 495 | struct list_head list; |
| 496 | char *init; |
| 497 | int index; |
| 498 | }; |
| 499 | |
| 500 | /* Filled in at boot time. Will need locking if the transports become |
| 501 | * modular. |
| 502 | */ |
| 503 | struct list_head transports = LIST_HEAD_INIT(transports); |
| 504 | |
| 505 | /* Filled in during early boot */ |
| 506 | struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line); |
| 507 | |
| 508 | static int check_transport(struct transport *transport, char *eth, int n, |
| 509 | void **init_out, char **mac_out) |
| 510 | { |
| 511 | int len; |
| 512 | |
| 513 | len = strlen(transport->name); |
| 514 | if(strncmp(eth, transport->name, len)) |
| 515 | return(0); |
| 516 | |
| 517 | eth += len; |
| 518 | if(*eth == ',') |
| 519 | eth++; |
| 520 | else if(*eth != '\0') |
| 521 | return(0); |
| 522 | |
| 523 | *init_out = kmalloc(transport->setup_size, GFP_KERNEL); |
| 524 | if(*init_out == NULL) |
| 525 | return(1); |
| 526 | |
| 527 | if(!transport->setup(eth, mac_out, *init_out)){ |
| 528 | kfree(*init_out); |
| 529 | *init_out = NULL; |
| 530 | } |
| 531 | return(1); |
| 532 | } |
| 533 | |
| 534 | void register_transport(struct transport *new) |
| 535 | { |
| 536 | struct list_head *ele, *next; |
| 537 | struct eth_init *eth; |
| 538 | void *init; |
| 539 | char *mac = NULL; |
| 540 | int match; |
| 541 | |
| 542 | list_add(&new->list, &transports); |
| 543 | |
| 544 | list_for_each_safe(ele, next, ð_cmd_line){ |
| 545 | eth = list_entry(ele, struct eth_init, list); |
| 546 | match = check_transport(new, eth->init, eth->index, &init, |
| 547 | &mac); |
| 548 | if(!match) |
| 549 | continue; |
| 550 | else if(init != NULL){ |
| 551 | eth_configure(eth->index, init, mac, new); |
| 552 | kfree(init); |
| 553 | } |
| 554 | list_del(ð->list); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | static int eth_setup_common(char *str, int index) |
| 559 | { |
| 560 | struct list_head *ele; |
| 561 | struct transport *transport; |
| 562 | void *init; |
| 563 | char *mac = NULL; |
| 564 | |
| 565 | list_for_each(ele, &transports){ |
| 566 | transport = list_entry(ele, struct transport, list); |
| 567 | if(!check_transport(transport, str, index, &init, &mac)) |
| 568 | continue; |
| 569 | if(init != NULL){ |
| 570 | eth_configure(index, init, mac, transport); |
| 571 | kfree(init); |
| 572 | } |
| 573 | return(1); |
| 574 | } |
| 575 | return(0); |
| 576 | } |
| 577 | |
| 578 | static int eth_setup(char *str) |
| 579 | { |
| 580 | struct eth_init *new; |
| 581 | int n, err; |
| 582 | |
| 583 | err = eth_parse(str, &n, &str); |
Jeff Dike | 1183dc9 | 2006-09-27 01:50:43 -0700 | [diff] [blame] | 584 | if(err) |
| 585 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
Jeff Dike | 1183dc9 | 2006-09-27 01:50:43 -0700 | [diff] [blame] | 587 | new = alloc_bootmem(sizeof(*new)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | if (new == NULL){ |
| 589 | printk("eth_init : alloc_bootmem failed\n"); |
Jeff Dike | 1183dc9 | 2006-09-27 01:50:43 -0700 | [diff] [blame] | 590 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | INIT_LIST_HEAD(&new->list); |
| 594 | new->index = n; |
| 595 | new->init = str; |
| 596 | |
| 597 | list_add_tail(&new->list, ð_cmd_line); |
Jeff Dike | 1183dc9 | 2006-09-27 01:50:43 -0700 | [diff] [blame] | 598 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | __setup("eth", eth_setup); |
| 602 | __uml_help(eth_setup, |
| 603 | "eth[0-9]+=<transport>,<options>\n" |
| 604 | " Configure a network device.\n\n" |
| 605 | ); |
| 606 | |
| 607 | #if 0 |
| 608 | static int eth_init(void) |
| 609 | { |
| 610 | struct list_head *ele, *next; |
| 611 | struct eth_init *eth; |
| 612 | |
| 613 | list_for_each_safe(ele, next, ð_cmd_line){ |
| 614 | eth = list_entry(ele, struct eth_init, list); |
| 615 | |
| 616 | if(eth_setup_common(eth->init, eth->index)) |
| 617 | list_del(ð->list); |
| 618 | } |
| 619 | |
| 620 | return(1); |
| 621 | } |
| 622 | __initcall(eth_init); |
| 623 | #endif |
| 624 | |
| 625 | static int net_config(char *str) |
| 626 | { |
| 627 | int n, err; |
| 628 | |
| 629 | err = eth_parse(str, &n, &str); |
| 630 | if(err) return(err); |
| 631 | |
Jeff Dike | 970d6e3 | 2006-01-06 00:18:48 -0800 | [diff] [blame] | 632 | str = kstrdup(str, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | if(str == NULL){ |
| 634 | printk(KERN_ERR "net_config failed to strdup string\n"); |
| 635 | return(-1); |
| 636 | } |
| 637 | err = !eth_setup_common(str, n); |
| 638 | if(err) |
| 639 | kfree(str); |
| 640 | return(err); |
| 641 | } |
| 642 | |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 643 | static int net_id(char **str, int *start_out, int *end_out) |
| 644 | { |
| 645 | char *end; |
| 646 | int n; |
| 647 | |
| 648 | n = simple_strtoul(*str, &end, 0); |
| 649 | if((*end != '\0') || (end == *str)) |
| 650 | return -1; |
| 651 | |
| 652 | *start_out = n; |
| 653 | *end_out = n; |
| 654 | *str = end; |
| 655 | return n; |
| 656 | } |
| 657 | |
| 658 | static int net_remove(int n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | { |
| 660 | struct uml_net *device; |
| 661 | struct net_device *dev; |
| 662 | struct uml_net_private *lp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | device = find_device(n); |
| 665 | if(device == NULL) |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 666 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
| 668 | dev = device->dev; |
| 669 | lp = dev->priv; |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 670 | if(lp->fd > 0) |
| 671 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | if(lp->remove != NULL) (*lp->remove)(&lp->user); |
| 673 | unregister_netdev(dev); |
| 674 | platform_device_unregister(&device->pdev); |
| 675 | |
| 676 | list_del(&device->list); |
| 677 | kfree(device); |
| 678 | free_netdev(dev); |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 679 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | static struct mc_device net_mc = { |
| 683 | .name = "eth", |
| 684 | .config = net_config, |
| 685 | .get_config = NULL, |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 686 | .id = net_id, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | .remove = net_remove, |
| 688 | }; |
| 689 | |
| 690 | static int uml_inetaddr_event(struct notifier_block *this, unsigned long event, |
| 691 | void *ptr) |
| 692 | { |
| 693 | struct in_ifaddr *ifa = ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | struct net_device *dev = ifa->ifa_dev->dev; |
| 695 | struct uml_net_private *lp; |
| 696 | void (*proc)(unsigned char *, unsigned char *, void *); |
| 697 | unsigned char addr_buf[4], netmask_buf[4]; |
| 698 | |
| 699 | if(dev->open != uml_net_open) return(NOTIFY_DONE); |
| 700 | |
| 701 | lp = dev->priv; |
| 702 | |
| 703 | proc = NULL; |
| 704 | switch (event){ |
| 705 | case NETDEV_UP: |
| 706 | proc = lp->add_address; |
| 707 | break; |
| 708 | case NETDEV_DOWN: |
| 709 | proc = lp->delete_address; |
| 710 | break; |
| 711 | } |
| 712 | if(proc != NULL){ |
Bodo Stroesser | 0e76422 | 2005-11-07 00:58:47 -0800 | [diff] [blame] | 713 | memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf)); |
| 714 | memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | (*proc)(addr_buf, netmask_buf, &lp->user); |
| 716 | } |
| 717 | return(NOTIFY_DONE); |
| 718 | } |
| 719 | |
| 720 | struct notifier_block uml_inetaddr_notifier = { |
| 721 | .notifier_call = uml_inetaddr_event, |
| 722 | }; |
| 723 | |
| 724 | static int uml_net_init(void) |
| 725 | { |
| 726 | struct list_head *ele; |
| 727 | struct uml_net_private *lp; |
| 728 | struct in_device *ip; |
| 729 | struct in_ifaddr *in; |
| 730 | |
| 731 | mconsole_register_dev(&net_mc); |
| 732 | register_inetaddr_notifier(¨_inetaddr_notifier); |
| 733 | |
| 734 | /* Devices may have been opened already, so the uml_inetaddr_notifier |
| 735 | * didn't get a chance to run for them. This fakes it so that |
| 736 | * addresses which have already been set up get handled properly. |
| 737 | */ |
| 738 | list_for_each(ele, &opened){ |
| 739 | lp = list_entry(ele, struct uml_net_private, list); |
| 740 | ip = lp->dev->ip_ptr; |
| 741 | if(ip == NULL) continue; |
| 742 | in = ip->ifa_list; |
| 743 | while(in != NULL){ |
| 744 | uml_inetaddr_event(NULL, NETDEV_UP, in); |
| 745 | in = in->ifa_next; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | return(0); |
| 750 | } |
| 751 | |
| 752 | __initcall(uml_net_init); |
| 753 | |
| 754 | static void close_devices(void) |
| 755 | { |
| 756 | struct list_head *ele; |
| 757 | struct uml_net_private *lp; |
| 758 | |
| 759 | list_for_each(ele, &opened){ |
| 760 | lp = list_entry(ele, struct uml_net_private, list); |
Jeff Dike | 8d93c70 | 2006-01-06 00:19:06 -0800 | [diff] [blame] | 761 | free_irq(lp->dev->irq, lp->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | if((lp->close != NULL) && (lp->fd >= 0)) |
| 763 | (*lp->close)(lp->fd, &lp->user); |
| 764 | if(lp->remove != NULL) (*lp->remove)(&lp->user); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | __uml_exitcall(close_devices); |
| 769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra) |
| 771 | { |
| 772 | if((skb != NULL) && (skb_tailroom(skb) < extra)){ |
| 773 | struct sk_buff *skb2; |
| 774 | |
| 775 | skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC); |
| 776 | dev_kfree_skb(skb); |
| 777 | skb = skb2; |
| 778 | } |
| 779 | if(skb != NULL) skb_put(skb, extra); |
| 780 | return(skb); |
| 781 | } |
| 782 | |
| 783 | void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *, |
| 784 | void *), |
| 785 | void *arg) |
| 786 | { |
| 787 | struct net_device *dev = d; |
| 788 | struct in_device *ip = dev->ip_ptr; |
| 789 | struct in_ifaddr *in; |
| 790 | unsigned char address[4], netmask[4]; |
| 791 | |
| 792 | if(ip == NULL) return; |
| 793 | in = ip->ifa_list; |
| 794 | while(in != NULL){ |
Bodo Stroesser | 0e76422 | 2005-11-07 00:58:47 -0800 | [diff] [blame] | 795 | memcpy(address, &in->ifa_address, sizeof(address)); |
| 796 | memcpy(netmask, &in->ifa_mask, sizeof(netmask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | (*cb)(address, netmask, arg); |
| 798 | in = in->ifa_next; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | int dev_netmask(void *d, void *m) |
| 803 | { |
| 804 | struct net_device *dev = d; |
| 805 | struct in_device *ip = dev->ip_ptr; |
| 806 | struct in_ifaddr *in; |
Al Viro | a144ea4 | 2006-09-28 18:00:55 -0700 | [diff] [blame] | 807 | __be32 *mask_out = m; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
| 809 | if(ip == NULL) |
| 810 | return(1); |
| 811 | |
| 812 | in = ip->ifa_list; |
| 813 | if(in == NULL) |
| 814 | return(1); |
| 815 | |
| 816 | *mask_out = in->ifa_mask; |
| 817 | return(0); |
| 818 | } |
| 819 | |
| 820 | void *get_output_buffer(int *len_out) |
| 821 | { |
| 822 | void *ret; |
| 823 | |
| 824 | ret = (void *) __get_free_pages(GFP_KERNEL, 0); |
| 825 | if(ret) *len_out = PAGE_SIZE; |
| 826 | else *len_out = 0; |
| 827 | return(ret); |
| 828 | } |
| 829 | |
| 830 | void free_output_buffer(void *buffer) |
| 831 | { |
| 832 | free_pages((unsigned long) buffer, 0); |
| 833 | } |
| 834 | |
| 835 | int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out, |
| 836 | char **gate_addr) |
| 837 | { |
| 838 | char *remain; |
| 839 | |
| 840 | remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL); |
| 841 | if(remain != NULL){ |
| 842 | printk("tap_setup_common - Extra garbage on specification : " |
| 843 | "'%s'\n", remain); |
| 844 | return(1); |
| 845 | } |
| 846 | |
| 847 | return(0); |
| 848 | } |
| 849 | |
| 850 | unsigned short eth_protocol(struct sk_buff *skb) |
| 851 | { |
| 852 | return(eth_type_trans(skb, skb->dev)); |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 857 | * Emacs will notice this stuff at the end of the file and automatically |
| 858 | * adjust the settings for this buffer only. This must remain at the end |
| 859 | * of the file. |
| 860 | * --------------------------------------------------------------------------- |
| 861 | * Local variables: |
| 862 | * c-file-style: "linux" |
| 863 | * End: |
| 864 | */ |