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