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