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