net_kern.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /*
  2. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
  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. #include <linux/bootmem.h>
  9. #include <linux/etherdevice.h>
  10. #include <linux/ethtool.h>
  11. #include <linux/inetdevice.h>
  12. #include <linux/init.h>
  13. #include <linux/list.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/rtnetlink.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/slab.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"
  24. #include "net_kern.h"
  25. #include "net_user.h"
  26. #define DRIVER_NAME "uml-netdev"
  27. static DEFINE_SPINLOCK(opened_lock);
  28. static LIST_HEAD(opened);
  29. /*
  30. * The drop_skb is used when we can't allocate an skb. The
  31. * packet is read into drop_skb in order to get the data off the
  32. * connection to the host.
  33. * It is reallocated whenever a maximum packet size is seen which is
  34. * larger than any seen before. update_drop_skb is called from
  35. * eth_configure when a new interface is added.
  36. */
  37. static DEFINE_SPINLOCK(drop_lock);
  38. static struct sk_buff *drop_skb;
  39. static int drop_max;
  40. static int update_drop_skb(int max)
  41. {
  42. struct sk_buff *new;
  43. unsigned long flags;
  44. int err = 0;
  45. spin_lock_irqsave(&drop_lock, flags);
  46. if (max <= drop_max)
  47. goto out;
  48. err = -ENOMEM;
  49. new = dev_alloc_skb(max);
  50. if (new == NULL)
  51. goto out;
  52. skb_put(new, max);
  53. kfree_skb(drop_skb);
  54. drop_skb = new;
  55. drop_max = max;
  56. err = 0;
  57. out:
  58. spin_unlock_irqrestore(&drop_lock, flags);
  59. return err;
  60. }
  61. static int uml_net_rx(struct net_device *dev)
  62. {
  63. struct uml_net_private *lp = netdev_priv(dev);
  64. int pkt_len;
  65. struct sk_buff *skb;
  66. /* If we can't allocate memory, try again next round. */
  67. skb = dev_alloc_skb(lp->max_packet);
  68. if (skb == NULL) {
  69. drop_skb->dev = dev;
  70. /* Read a packet into drop_skb and don't do anything with it. */
  71. (*lp->read)(lp->fd, drop_skb, lp);
  72. dev->stats.rx_dropped++;
  73. return 0;
  74. }
  75. skb->dev = dev;
  76. skb_put(skb, lp->max_packet);
  77. skb_reset_mac_header(skb);
  78. pkt_len = (*lp->read)(lp->fd, skb, lp);
  79. if (pkt_len > 0) {
  80. skb_trim(skb, pkt_len);
  81. skb->protocol = (*lp->protocol)(skb);
  82. dev->stats.rx_bytes += skb->len;
  83. dev->stats.rx_packets++;
  84. netif_rx(skb);
  85. return pkt_len;
  86. }
  87. kfree_skb(skb);
  88. return pkt_len;
  89. }
  90. static void uml_dev_close(struct work_struct *work)
  91. {
  92. struct uml_net_private *lp =
  93. container_of(work, struct uml_net_private, work);
  94. dev_close(lp->dev);
  95. }
  96. static irqreturn_t uml_net_interrupt(int irq, void *dev_id)
  97. {
  98. struct net_device *dev = dev_id;
  99. struct uml_net_private *lp = netdev_priv(dev);
  100. int err;
  101. if (!netif_running(dev))
  102. return IRQ_NONE;
  103. spin_lock(&lp->lock);
  104. while ((err = uml_net_rx(dev)) > 0) ;
  105. if (err < 0) {
  106. printk(KERN_ERR
  107. "Device '%s' read returned %d, shutting it down\n",
  108. dev->name, err);
  109. /* dev_close can't be called in interrupt context, and takes
  110. * again lp->lock.
  111. * And dev_close() can be safely called multiple times on the
  112. * same device, since it tests for (dev->flags & IFF_UP). So
  113. * there's no harm in delaying the device shutdown.
  114. * Furthermore, the workqueue will not re-enqueue an already
  115. * enqueued work item. */
  116. schedule_work(&lp->work);
  117. goto out;
  118. }
  119. reactivate_fd(lp->fd, UM_ETH_IRQ);
  120. out:
  121. spin_unlock(&lp->lock);
  122. return IRQ_HANDLED;
  123. }
  124. static int uml_net_open(struct net_device *dev)
  125. {
  126. struct uml_net_private *lp = netdev_priv(dev);
  127. int err;
  128. if (lp->fd >= 0) {
  129. err = -ENXIO;
  130. goto out;
  131. }
  132. lp->fd = (*lp->open)(&lp->user);
  133. if (lp->fd < 0) {
  134. err = lp->fd;
  135. goto out;
  136. }
  137. err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
  138. IRQF_SHARED, dev->name, dev);
  139. if (err != 0) {
  140. printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
  141. err = -ENETUNREACH;
  142. goto out_close;
  143. }
  144. lp->tl.data = (unsigned long) &lp->user;
  145. netif_start_queue(dev);
  146. /* clear buffer - it can happen that the host side of the interface
  147. * is full when we get here. In this case, new data is never queued,
  148. * SIGIOs never arrive, and the net never works.
  149. */
  150. while ((err = uml_net_rx(dev)) > 0) ;
  151. spin_lock(&opened_lock);
  152. list_add(&lp->list, &opened);
  153. spin_unlock(&opened_lock);
  154. return 0;
  155. out_close:
  156. if (lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
  157. lp->fd = -1;
  158. out:
  159. return err;
  160. }
  161. static int uml_net_close(struct net_device *dev)
  162. {
  163. struct uml_net_private *lp = netdev_priv(dev);
  164. netif_stop_queue(dev);
  165. free_irq(dev->irq, dev);
  166. if (lp->close != NULL)
  167. (*lp->close)(lp->fd, &lp->user);
  168. lp->fd = -1;
  169. spin_lock(&opened_lock);
  170. list_del(&lp->list);
  171. spin_unlock(&opened_lock);
  172. return 0;
  173. }
  174. static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
  175. {
  176. struct uml_net_private *lp = netdev_priv(dev);
  177. unsigned long flags;
  178. int len;
  179. netif_stop_queue(dev);
  180. spin_lock_irqsave(&lp->lock, flags);
  181. len = (*lp->write)(lp->fd, skb, lp);
  182. if (len == skb->len) {
  183. dev->stats.tx_packets++;
  184. dev->stats.tx_bytes += skb->len;
  185. dev->trans_start = jiffies;
  186. netif_start_queue(dev);
  187. /* this is normally done in the interrupt when tx finishes */
  188. netif_wake_queue(dev);
  189. }
  190. else if (len == 0) {
  191. netif_start_queue(dev);
  192. dev->stats.tx_dropped++;
  193. }
  194. else {
  195. netif_start_queue(dev);
  196. printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
  197. }
  198. spin_unlock_irqrestore(&lp->lock, flags);
  199. dev_kfree_skb(skb);
  200. return NETDEV_TX_OK;
  201. }
  202. static void uml_net_set_multicast_list(struct net_device *dev)
  203. {
  204. return;
  205. }
  206. static void uml_net_tx_timeout(struct net_device *dev)
  207. {
  208. dev->trans_start = jiffies;
  209. netif_wake_queue(dev);
  210. }
  211. static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
  212. {
  213. dev->mtu = new_mtu;
  214. return 0;
  215. }
  216. #ifdef CONFIG_NET_POLL_CONTROLLER
  217. static void uml_net_poll_controller(struct net_device *dev)
  218. {
  219. disable_irq(dev->irq);
  220. uml_net_interrupt(dev->irq, dev);
  221. enable_irq(dev->irq);
  222. }
  223. #endif
  224. static void uml_net_get_drvinfo(struct net_device *dev,
  225. struct ethtool_drvinfo *info)
  226. {
  227. strcpy(info->driver, DRIVER_NAME);
  228. strcpy(info->version, "42");
  229. }
  230. static const struct ethtool_ops uml_net_ethtool_ops = {
  231. .get_drvinfo = uml_net_get_drvinfo,
  232. .get_link = ethtool_op_get_link,
  233. };
  234. static void uml_net_user_timer_expire(unsigned long _conn)
  235. {
  236. #ifdef undef
  237. struct connection *conn = (struct connection *)_conn;
  238. dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
  239. do_connect(conn);
  240. #endif
  241. }
  242. static int setup_etheraddr(char *str, unsigned char *addr, char *name)
  243. {
  244. char *end;
  245. int i;
  246. if (str == NULL)
  247. goto random;
  248. for (i = 0; i < 6; i++) {
  249. addr[i] = simple_strtoul(str, &end, 16);
  250. if ((end == str) ||
  251. ((*end != ':') && (*end != ',') && (*end != '\0'))) {
  252. printk(KERN_ERR
  253. "setup_etheraddr: failed to parse '%s' "
  254. "as an ethernet address\n", str);
  255. goto random;
  256. }
  257. str = end + 1;
  258. }
  259. if (is_multicast_ether_addr(addr)) {
  260. printk(KERN_ERR
  261. "Attempt to assign a multicast ethernet address to a "
  262. "device disallowed\n");
  263. goto random;
  264. }
  265. if (!is_valid_ether_addr(addr)) {
  266. printk(KERN_ERR
  267. "Attempt to assign an invalid ethernet address to a "
  268. "device disallowed\n");
  269. goto random;
  270. }
  271. if (!is_local_ether_addr(addr)) {
  272. printk(KERN_WARNING
  273. "Warning: Assigning a globally valid ethernet "
  274. "address to a device\n");
  275. printk(KERN_WARNING "You should set the 2nd rightmost bit in "
  276. "the first byte of the MAC,\n");
  277. printk(KERN_WARNING "i.e. %02x:%02x:%02x:%02x:%02x:%02x\n",
  278. addr[0] | 0x02, addr[1], addr[2], addr[3], addr[4],
  279. addr[5]);
  280. }
  281. return 0;
  282. random:
  283. printk(KERN_INFO
  284. "Choosing a random ethernet address for device %s\n", name);
  285. random_ether_addr(addr);
  286. return 1;
  287. }
  288. static DEFINE_SPINLOCK(devices_lock);
  289. static LIST_HEAD(devices);
  290. static struct platform_driver uml_net_driver = {
  291. .driver = {
  292. .name = DRIVER_NAME,
  293. },
  294. };
  295. static void net_device_release(struct device *dev)
  296. {
  297. struct uml_net *device = dev_get_drvdata(dev);
  298. struct net_device *netdev = device->dev;
  299. struct uml_net_private *lp = netdev_priv(netdev);
  300. if (lp->remove != NULL)
  301. (*lp->remove)(&lp->user);
  302. list_del(&device->list);
  303. kfree(device);
  304. free_netdev(netdev);
  305. }
  306. static const struct net_device_ops uml_netdev_ops = {
  307. .ndo_open = uml_net_open,
  308. .ndo_stop = uml_net_close,
  309. .ndo_start_xmit = uml_net_start_xmit,
  310. .ndo_set_rx_mode = uml_net_set_multicast_list,
  311. .ndo_tx_timeout = uml_net_tx_timeout,
  312. .ndo_set_mac_address = eth_mac_addr,
  313. .ndo_change_mtu = uml_net_change_mtu,
  314. .ndo_validate_addr = eth_validate_addr,
  315. #ifdef CONFIG_NET_POLL_CONTROLLER
  316. .ndo_poll_controller = uml_net_poll_controller,
  317. #endif
  318. };
  319. /*
  320. * Ensures that platform_driver_register is called only once by
  321. * eth_configure. Will be set in an initcall.
  322. */
  323. static int driver_registered;
  324. static void eth_configure(int n, void *init, char *mac,
  325. struct transport *transport)
  326. {
  327. struct uml_net *device;
  328. struct net_device *dev;
  329. struct uml_net_private *lp;
  330. int err, size;
  331. int random_mac;
  332. size = transport->private_size + sizeof(struct uml_net_private);
  333. device = kzalloc(sizeof(*device), GFP_KERNEL);
  334. if (device == NULL) {
  335. printk(KERN_ERR "eth_configure failed to allocate struct "
  336. "uml_net\n");
  337. return;
  338. }
  339. dev = alloc_etherdev(size);
  340. if (dev == NULL) {
  341. printk(KERN_ERR "eth_configure: failed to allocate struct "
  342. "net_device for eth%d\n", n);
  343. goto out_free_device;
  344. }
  345. INIT_LIST_HEAD(&device->list);
  346. device->index = n;
  347. /* If this name ends up conflicting with an existing registered
  348. * netdevice, that is OK, register_netdev{,ice}() will notice this
  349. * and fail.
  350. */
  351. snprintf(dev->name, sizeof(dev->name), "eth%d", n);
  352. random_mac = setup_etheraddr(mac, device->mac, dev->name);
  353. printk(KERN_INFO "Netdevice %d (%pM) : ", n, device->mac);
  354. lp = netdev_priv(dev);
  355. /* This points to the transport private data. It's still clear, but we
  356. * must memset it to 0 *now*. Let's help the drivers. */
  357. memset(lp, 0, size);
  358. INIT_WORK(&lp->work, uml_dev_close);
  359. /* sysfs register */
  360. if (!driver_registered) {
  361. platform_driver_register(&uml_net_driver);
  362. driver_registered = 1;
  363. }
  364. device->pdev.id = n;
  365. device->pdev.name = DRIVER_NAME;
  366. device->pdev.dev.release = net_device_release;
  367. dev_set_drvdata(&device->pdev.dev, device);
  368. if (platform_device_register(&device->pdev))
  369. goto out_free_netdev;
  370. SET_NETDEV_DEV(dev,&device->pdev.dev);
  371. device->dev = dev;
  372. /*
  373. * These just fill in a data structure, so there's no failure
  374. * to be worried about.
  375. */
  376. (*transport->kern->init)(dev, init);
  377. *lp = ((struct uml_net_private)
  378. { .list = LIST_HEAD_INIT(lp->list),
  379. .dev = dev,
  380. .fd = -1,
  381. .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
  382. .max_packet = transport->user->max_packet,
  383. .protocol = transport->kern->protocol,
  384. .open = transport->user->open,
  385. .close = transport->user->close,
  386. .remove = transport->user->remove,
  387. .read = transport->kern->read,
  388. .write = transport->kern->write,
  389. .add_address = transport->user->add_address,
  390. .delete_address = transport->user->delete_address });
  391. init_timer(&lp->tl);
  392. spin_lock_init(&lp->lock);
  393. lp->tl.function = uml_net_user_timer_expire;
  394. memcpy(lp->mac, device->mac, sizeof(lp->mac));
  395. if ((transport->user->init != NULL) &&
  396. ((*transport->user->init)(&lp->user, dev) != 0))
  397. goto out_unregister;
  398. /* don't use eth_mac_addr, it will not work here */
  399. memcpy(dev->dev_addr, device->mac, ETH_ALEN);
  400. if (random_mac)
  401. dev->addr_assign_type |= NET_ADDR_RANDOM;
  402. dev->mtu = transport->user->mtu;
  403. dev->netdev_ops = &uml_netdev_ops;
  404. dev->ethtool_ops = &uml_net_ethtool_ops;
  405. dev->watchdog_timeo = (HZ >> 1);
  406. dev->irq = UM_ETH_IRQ;
  407. err = update_drop_skb(lp->max_packet);
  408. if (err)
  409. goto out_undo_user_init;
  410. rtnl_lock();
  411. err = register_netdevice(dev);
  412. rtnl_unlock();
  413. if (err)
  414. goto out_undo_user_init;
  415. spin_lock(&devices_lock);
  416. list_add(&device->list, &devices);
  417. spin_unlock(&devices_lock);
  418. return;
  419. out_undo_user_init:
  420. if (transport->user->remove != NULL)
  421. (*transport->user->remove)(&lp->user);
  422. out_unregister:
  423. platform_device_unregister(&device->pdev);
  424. return; /* platform_device_unregister frees dev and device */
  425. out_free_netdev:
  426. free_netdev(dev);
  427. out_free_device:
  428. kfree(device);
  429. }
  430. static struct uml_net *find_device(int n)
  431. {
  432. struct uml_net *device;
  433. struct list_head *ele;
  434. spin_lock(&devices_lock);
  435. list_for_each(ele, &devices) {
  436. device = list_entry(ele, struct uml_net, list);
  437. if (device->index == n)
  438. goto out;
  439. }
  440. device = NULL;
  441. out:
  442. spin_unlock(&devices_lock);
  443. return device;
  444. }
  445. static int eth_parse(char *str, int *index_out, char **str_out,
  446. char **error_out)
  447. {
  448. char *end;
  449. int n, err = -EINVAL;
  450. n = simple_strtoul(str, &end, 0);
  451. if (end == str) {
  452. *error_out = "Bad device number";
  453. return err;
  454. }
  455. str = end;
  456. if (*str != '=') {
  457. *error_out = "Expected '=' after device number";
  458. return err;
  459. }
  460. str++;
  461. if (find_device(n)) {
  462. *error_out = "Device already configured";
  463. return err;
  464. }
  465. *index_out = n;
  466. *str_out = str;
  467. return 0;
  468. }
  469. struct eth_init {
  470. struct list_head list;
  471. char *init;
  472. int index;
  473. };
  474. static DEFINE_SPINLOCK(transports_lock);
  475. static LIST_HEAD(transports);
  476. /* Filled in during early boot */
  477. static LIST_HEAD(eth_cmd_line);
  478. static int check_transport(struct transport *transport, char *eth, int n,
  479. void **init_out, char **mac_out)
  480. {
  481. int len;
  482. len = strlen(transport->name);
  483. if (strncmp(eth, transport->name, len))
  484. return 0;
  485. eth += len;
  486. if (*eth == ',')
  487. eth++;
  488. else if (*eth != '\0')
  489. return 0;
  490. *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
  491. if (*init_out == NULL)
  492. return 1;
  493. if (!transport->setup(eth, mac_out, *init_out)) {
  494. kfree(*init_out);
  495. *init_out = NULL;
  496. }
  497. return 1;
  498. }
  499. void register_transport(struct transport *new)
  500. {
  501. struct list_head *ele, *next;
  502. struct eth_init *eth;
  503. void *init;
  504. char *mac = NULL;
  505. int match;
  506. spin_lock(&transports_lock);
  507. BUG_ON(!list_empty(&new->list));
  508. list_add(&new->list, &transports);
  509. spin_unlock(&transports_lock);
  510. list_for_each_safe(ele, next, &eth_cmd_line) {
  511. eth = list_entry(ele, struct eth_init, list);
  512. match = check_transport(new, eth->init, eth->index, &init,
  513. &mac);
  514. if (!match)
  515. continue;
  516. else if (init != NULL) {
  517. eth_configure(eth->index, init, mac, new);
  518. kfree(init);
  519. }
  520. list_del(&eth->list);
  521. }
  522. }
  523. static int eth_setup_common(char *str, int index)
  524. {
  525. struct list_head *ele;
  526. struct transport *transport;
  527. void *init;
  528. char *mac = NULL;
  529. int found = 0;
  530. spin_lock(&transports_lock);
  531. list_for_each(ele, &transports) {
  532. transport = list_entry(ele, struct transport, list);
  533. if (!check_transport(transport, str, index, &init, &mac))
  534. continue;
  535. if (init != NULL) {
  536. eth_configure(index, init, mac, transport);
  537. kfree(init);
  538. }
  539. found = 1;
  540. break;
  541. }
  542. spin_unlock(&transports_lock);
  543. return found;
  544. }
  545. static int __init eth_setup(char *str)
  546. {
  547. struct eth_init *new;
  548. char *error;
  549. int n, err;
  550. err = eth_parse(str, &n, &str, &error);
  551. if (err) {
  552. printk(KERN_ERR "eth_setup - Couldn't parse '%s' : %s\n",
  553. str, error);
  554. return 1;
  555. }
  556. new = alloc_bootmem(sizeof(*new));
  557. if (new == NULL) {
  558. printk(KERN_ERR "eth_init : alloc_bootmem failed\n");
  559. return 1;
  560. }
  561. INIT_LIST_HEAD(&new->list);
  562. new->index = n;
  563. new->init = str;
  564. list_add_tail(&new->list, &eth_cmd_line);
  565. return 1;
  566. }
  567. __setup("eth", eth_setup);
  568. __uml_help(eth_setup,
  569. "eth[0-9]+=<transport>,<options>\n"
  570. " Configure a network device.\n\n"
  571. );
  572. static int net_config(char *str, char **error_out)
  573. {
  574. int n, err;
  575. err = eth_parse(str, &n, &str, error_out);
  576. if (err)
  577. return err;
  578. /* This string is broken up and the pieces used by the underlying
  579. * driver. So, it is freed only if eth_setup_common fails.
  580. */
  581. str = kstrdup(str, GFP_KERNEL);
  582. if (str == NULL) {
  583. *error_out = "net_config failed to strdup string";
  584. return -ENOMEM;
  585. }
  586. err = !eth_setup_common(str, n);
  587. if (err)
  588. kfree(str);
  589. return err;
  590. }
  591. static int net_id(char **str, int *start_out, int *end_out)
  592. {
  593. char *end;
  594. int n;
  595. n = simple_strtoul(*str, &end, 0);
  596. if ((*end != '\0') || (end == *str))
  597. return -1;
  598. *start_out = n;
  599. *end_out = n;
  600. *str = end;
  601. return n;
  602. }
  603. static int net_remove(int n, char **error_out)
  604. {
  605. struct uml_net *device;
  606. struct net_device *dev;
  607. struct uml_net_private *lp;
  608. device = find_device(n);
  609. if (device == NULL)
  610. return -ENODEV;
  611. dev = device->dev;
  612. lp = netdev_priv(dev);
  613. if (lp->fd > 0)
  614. return -EBUSY;
  615. unregister_netdev(dev);
  616. platform_device_unregister(&device->pdev);
  617. return 0;
  618. }
  619. static struct mc_device net_mc = {
  620. .list = LIST_HEAD_INIT(net_mc.list),
  621. .name = "eth",
  622. .config = net_config,
  623. .get_config = NULL,
  624. .id = net_id,
  625. .remove = net_remove,
  626. };
  627. #ifdef CONFIG_INET
  628. static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
  629. void *ptr)
  630. {
  631. struct in_ifaddr *ifa = ptr;
  632. struct net_device *dev = ifa->ifa_dev->dev;
  633. struct uml_net_private *lp;
  634. void (*proc)(unsigned char *, unsigned char *, void *);
  635. unsigned char addr_buf[4], netmask_buf[4];
  636. if (dev->netdev_ops->ndo_open != uml_net_open)
  637. return NOTIFY_DONE;
  638. lp = netdev_priv(dev);
  639. proc = NULL;
  640. switch (event) {
  641. case NETDEV_UP:
  642. proc = lp->add_address;
  643. break;
  644. case NETDEV_DOWN:
  645. proc = lp->delete_address;
  646. break;
  647. }
  648. if (proc != NULL) {
  649. memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
  650. memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
  651. (*proc)(addr_buf, netmask_buf, &lp->user);
  652. }
  653. return NOTIFY_DONE;
  654. }
  655. /* uml_net_init shouldn't be called twice on two CPUs at the same time */
  656. static struct notifier_block uml_inetaddr_notifier = {
  657. .notifier_call = uml_inetaddr_event,
  658. };
  659. static void inet_register(void)
  660. {
  661. struct list_head *ele;
  662. struct uml_net_private *lp;
  663. struct in_device *ip;
  664. struct in_ifaddr *in;
  665. register_inetaddr_notifier(&uml_inetaddr_notifier);
  666. /* Devices may have been opened already, so the uml_inetaddr_notifier
  667. * didn't get a chance to run for them. This fakes it so that
  668. * addresses which have already been set up get handled properly.
  669. */
  670. spin_lock(&opened_lock);
  671. list_for_each(ele, &opened) {
  672. lp = list_entry(ele, struct uml_net_private, list);
  673. ip = lp->dev->ip_ptr;
  674. if (ip == NULL)
  675. continue;
  676. in = ip->ifa_list;
  677. while (in != NULL) {
  678. uml_inetaddr_event(NULL, NETDEV_UP, in);
  679. in = in->ifa_next;
  680. }
  681. }
  682. spin_unlock(&opened_lock);
  683. }
  684. #else
  685. static inline void inet_register(void)
  686. {
  687. }
  688. #endif
  689. static int uml_net_init(void)
  690. {
  691. mconsole_register_dev(&net_mc);
  692. inet_register();
  693. return 0;
  694. }
  695. __initcall(uml_net_init);
  696. static void close_devices(void)
  697. {
  698. struct list_head *ele;
  699. struct uml_net_private *lp;
  700. spin_lock(&opened_lock);
  701. list_for_each(ele, &opened) {
  702. lp = list_entry(ele, struct uml_net_private, list);
  703. free_irq(lp->dev->irq, lp->dev);
  704. if ((lp->close != NULL) && (lp->fd >= 0))
  705. (*lp->close)(lp->fd, &lp->user);
  706. if (lp->remove != NULL)
  707. (*lp->remove)(&lp->user);
  708. }
  709. spin_unlock(&opened_lock);
  710. }
  711. __uml_exitcall(close_devices);
  712. void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
  713. void *),
  714. void *arg)
  715. {
  716. struct net_device *dev = d;
  717. struct in_device *ip = dev->ip_ptr;
  718. struct in_ifaddr *in;
  719. unsigned char address[4], netmask[4];
  720. if (ip == NULL) return;
  721. in = ip->ifa_list;
  722. while (in != NULL) {
  723. memcpy(address, &in->ifa_address, sizeof(address));
  724. memcpy(netmask, &in->ifa_mask, sizeof(netmask));
  725. (*cb)(address, netmask, arg);
  726. in = in->ifa_next;
  727. }
  728. }
  729. int dev_netmask(void *d, void *m)
  730. {
  731. struct net_device *dev = d;
  732. struct in_device *ip = dev->ip_ptr;
  733. struct in_ifaddr *in;
  734. __be32 *mask_out = m;
  735. if (ip == NULL)
  736. return 1;
  737. in = ip->ifa_list;
  738. if (in == NULL)
  739. return 1;
  740. *mask_out = in->ifa_mask;
  741. return 0;
  742. }
  743. void *get_output_buffer(int *len_out)
  744. {
  745. void *ret;
  746. ret = (void *) __get_free_pages(GFP_KERNEL, 0);
  747. if (ret) *len_out = PAGE_SIZE;
  748. else *len_out = 0;
  749. return ret;
  750. }
  751. void free_output_buffer(void *buffer)
  752. {
  753. free_pages((unsigned long) buffer, 0);
  754. }
  755. int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
  756. char **gate_addr)
  757. {
  758. char *remain;
  759. remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
  760. if (remain != NULL) {
  761. printk(KERN_ERR "tap_setup_common - Extra garbage on "
  762. "specification : '%s'\n", remain);
  763. return 1;
  764. }
  765. return 0;
  766. }
  767. unsigned short eth_protocol(struct sk_buff *skb)
  768. {
  769. return eth_type_trans(skb, skb->dev);
  770. }