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