net_kern.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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/memblock.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. netif_start_queue(dev);
  145. /* clear buffer - it can happen that the host side of the interface
  146. * is full when we get here. In this case, new data is never queued,
  147. * SIGIOs never arrive, and the net never works.
  148. */
  149. while ((err = uml_net_rx(dev)) > 0) ;
  150. spin_lock(&opened_lock);
  151. list_add(&lp->list, &opened);
  152. spin_unlock(&opened_lock);
  153. return 0;
  154. out_close:
  155. if (lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
  156. lp->fd = -1;
  157. out:
  158. return err;
  159. }
  160. static int uml_net_close(struct net_device *dev)
  161. {
  162. struct uml_net_private *lp = netdev_priv(dev);
  163. netif_stop_queue(dev);
  164. um_free_irq(dev->irq, dev);
  165. if (lp->close != NULL)
  166. (*lp->close)(lp->fd, &lp->user);
  167. lp->fd = -1;
  168. spin_lock(&opened_lock);
  169. list_del(&lp->list);
  170. spin_unlock(&opened_lock);
  171. return 0;
  172. }
  173. static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
  174. {
  175. struct uml_net_private *lp = netdev_priv(dev);
  176. unsigned long flags;
  177. int len;
  178. netif_stop_queue(dev);
  179. spin_lock_irqsave(&lp->lock, flags);
  180. len = (*lp->write)(lp->fd, skb, lp);
  181. skb_tx_timestamp(skb);
  182. if (len == skb->len) {
  183. dev->stats.tx_packets++;
  184. dev->stats.tx_bytes += skb->len;
  185. netif_trans_update(dev);
  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_consume_skb_any(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. netif_trans_update(dev);
  209. netif_wake_queue(dev);
  210. }
  211. #ifdef CONFIG_NET_POLL_CONTROLLER
  212. static void uml_net_poll_controller(struct net_device *dev)
  213. {
  214. disable_irq(dev->irq);
  215. uml_net_interrupt(dev->irq, dev);
  216. enable_irq(dev->irq);
  217. }
  218. #endif
  219. static void uml_net_get_drvinfo(struct net_device *dev,
  220. struct ethtool_drvinfo *info)
  221. {
  222. strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
  223. strlcpy(info->version, "42", sizeof(info->version));
  224. }
  225. static const struct ethtool_ops uml_net_ethtool_ops = {
  226. .get_drvinfo = uml_net_get_drvinfo,
  227. .get_link = ethtool_op_get_link,
  228. .get_ts_info = ethtool_op_get_ts_info,
  229. };
  230. static void uml_net_user_timer_expire(struct timer_list *t)
  231. {
  232. #ifdef undef
  233. struct uml_net_private *lp = from_timer(lp, t, tl);
  234. struct connection *conn = &lp->user;
  235. dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
  236. do_connect(conn);
  237. #endif
  238. }
  239. void uml_net_setup_etheraddr(struct net_device *dev, char *str)
  240. {
  241. unsigned char *addr = dev->dev_addr;
  242. char *end;
  243. int i;
  244. if (str == NULL)
  245. goto random;
  246. for (i = 0; i < 6; i++) {
  247. addr[i] = simple_strtoul(str, &end, 16);
  248. if ((end == str) ||
  249. ((*end != ':') && (*end != ',') && (*end != '\0'))) {
  250. printk(KERN_ERR
  251. "setup_etheraddr: failed to parse '%s' "
  252. "as an ethernet address\n", str);
  253. goto random;
  254. }
  255. str = end + 1;
  256. }
  257. if (is_multicast_ether_addr(addr)) {
  258. printk(KERN_ERR
  259. "Attempt to assign a multicast ethernet address to a "
  260. "device disallowed\n");
  261. goto random;
  262. }
  263. if (!is_valid_ether_addr(addr)) {
  264. printk(KERN_ERR
  265. "Attempt to assign an invalid ethernet address to a "
  266. "device disallowed\n");
  267. goto random;
  268. }
  269. if (!is_local_ether_addr(addr)) {
  270. printk(KERN_WARNING
  271. "Warning: Assigning a globally valid ethernet "
  272. "address to a device\n");
  273. printk(KERN_WARNING "You should set the 2nd rightmost bit in "
  274. "the first byte of the MAC,\n");
  275. printk(KERN_WARNING "i.e. %02x:%02x:%02x:%02x:%02x:%02x\n",
  276. addr[0] | 0x02, addr[1], addr[2], addr[3], addr[4],
  277. addr[5]);
  278. }
  279. return;
  280. random:
  281. printk(KERN_INFO
  282. "Choosing a random ethernet address for device %s\n", dev->name);
  283. eth_hw_addr_random(dev);
  284. }
  285. static DEFINE_SPINLOCK(devices_lock);
  286. static LIST_HEAD(devices);
  287. static struct platform_driver uml_net_driver = {
  288. .driver = {
  289. .name = DRIVER_NAME,
  290. },
  291. };
  292. static void net_device_release(struct device *dev)
  293. {
  294. struct uml_net *device = dev_get_drvdata(dev);
  295. struct net_device *netdev = device->dev;
  296. struct uml_net_private *lp = netdev_priv(netdev);
  297. if (lp->remove != NULL)
  298. (*lp->remove)(&lp->user);
  299. list_del(&device->list);
  300. kfree(device);
  301. free_netdev(netdev);
  302. }
  303. static const struct net_device_ops uml_netdev_ops = {
  304. .ndo_open = uml_net_open,
  305. .ndo_stop = uml_net_close,
  306. .ndo_start_xmit = uml_net_start_xmit,
  307. .ndo_set_rx_mode = uml_net_set_multicast_list,
  308. .ndo_tx_timeout = uml_net_tx_timeout,
  309. .ndo_set_mac_address = eth_mac_addr,
  310. .ndo_validate_addr = eth_validate_addr,
  311. #ifdef CONFIG_NET_POLL_CONTROLLER
  312. .ndo_poll_controller = uml_net_poll_controller,
  313. #endif
  314. };
  315. /*
  316. * Ensures that platform_driver_register is called only once by
  317. * eth_configure. Will be set in an initcall.
  318. */
  319. static int driver_registered;
  320. static void eth_configure(int n, void *init, char *mac,
  321. struct transport *transport, gfp_t gfp_mask)
  322. {
  323. struct uml_net *device;
  324. struct net_device *dev;
  325. struct uml_net_private *lp;
  326. int err, size;
  327. size = transport->private_size + sizeof(struct uml_net_private);
  328. device = kzalloc(sizeof(*device), gfp_mask);
  329. if (device == NULL) {
  330. printk(KERN_ERR "eth_configure failed to allocate struct "
  331. "uml_net\n");
  332. return;
  333. }
  334. dev = alloc_etherdev(size);
  335. if (dev == NULL) {
  336. printk(KERN_ERR "eth_configure: failed to allocate struct "
  337. "net_device for eth%d\n", n);
  338. goto out_free_device;
  339. }
  340. INIT_LIST_HEAD(&device->list);
  341. device->index = n;
  342. /* If this name ends up conflicting with an existing registered
  343. * netdevice, that is OK, register_netdev{,ice}() will notice this
  344. * and fail.
  345. */
  346. snprintf(dev->name, sizeof(dev->name), "eth%d", n);
  347. uml_net_setup_etheraddr(dev, mac);
  348. printk(KERN_INFO "Netdevice %d (%pM) : ", n, dev->dev_addr);
  349. lp = netdev_priv(dev);
  350. /* This points to the transport private data. It's still clear, but we
  351. * must memset it to 0 *now*. Let's help the drivers. */
  352. memset(lp, 0, size);
  353. INIT_WORK(&lp->work, uml_dev_close);
  354. /* sysfs register */
  355. if (!driver_registered) {
  356. platform_driver_register(&uml_net_driver);
  357. driver_registered = 1;
  358. }
  359. device->pdev.id = n;
  360. device->pdev.name = DRIVER_NAME;
  361. device->pdev.dev.release = net_device_release;
  362. dev_set_drvdata(&device->pdev.dev, device);
  363. if (platform_device_register(&device->pdev))
  364. goto out_free_netdev;
  365. SET_NETDEV_DEV(dev,&device->pdev.dev);
  366. device->dev = dev;
  367. /*
  368. * These just fill in a data structure, so there's no failure
  369. * to be worried about.
  370. */
  371. (*transport->kern->init)(dev, init);
  372. *lp = ((struct uml_net_private)
  373. { .list = LIST_HEAD_INIT(lp->list),
  374. .dev = dev,
  375. .fd = -1,
  376. .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
  377. .max_packet = transport->user->max_packet,
  378. .protocol = transport->kern->protocol,
  379. .open = transport->user->open,
  380. .close = transport->user->close,
  381. .remove = transport->user->remove,
  382. .read = transport->kern->read,
  383. .write = transport->kern->write,
  384. .add_address = transport->user->add_address,
  385. .delete_address = transport->user->delete_address });
  386. timer_setup(&lp->tl, uml_net_user_timer_expire, 0);
  387. spin_lock_init(&lp->lock);
  388. memcpy(lp->mac, dev->dev_addr, sizeof(lp->mac));
  389. if ((transport->user->init != NULL) &&
  390. ((*transport->user->init)(&lp->user, dev) != 0))
  391. goto out_unregister;
  392. dev->mtu = transport->user->mtu;
  393. dev->netdev_ops = &uml_netdev_ops;
  394. dev->ethtool_ops = &uml_net_ethtool_ops;
  395. dev->watchdog_timeo = (HZ >> 1);
  396. dev->irq = UM_ETH_IRQ;
  397. err = update_drop_skb(lp->max_packet);
  398. if (err)
  399. goto out_undo_user_init;
  400. rtnl_lock();
  401. err = register_netdevice(dev);
  402. rtnl_unlock();
  403. if (err)
  404. goto out_undo_user_init;
  405. spin_lock(&devices_lock);
  406. list_add(&device->list, &devices);
  407. spin_unlock(&devices_lock);
  408. return;
  409. out_undo_user_init:
  410. if (transport->user->remove != NULL)
  411. (*transport->user->remove)(&lp->user);
  412. out_unregister:
  413. platform_device_unregister(&device->pdev);
  414. return; /* platform_device_unregister frees dev and device */
  415. out_free_netdev:
  416. free_netdev(dev);
  417. out_free_device:
  418. kfree(device);
  419. }
  420. static struct uml_net *find_device(int n)
  421. {
  422. struct uml_net *device;
  423. struct list_head *ele;
  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. static int eth_parse(char *str, int *index_out, char **str_out,
  436. char **error_out)
  437. {
  438. char *end;
  439. int n, err = -EINVAL;
  440. n = simple_strtoul(str, &end, 0);
  441. if (end == str) {
  442. *error_out = "Bad device number";
  443. return err;
  444. }
  445. str = end;
  446. if (*str != '=') {
  447. *error_out = "Expected '=' after device number";
  448. return err;
  449. }
  450. str++;
  451. if (find_device(n)) {
  452. *error_out = "Device already configured";
  453. return err;
  454. }
  455. *index_out = n;
  456. *str_out = str;
  457. return 0;
  458. }
  459. struct eth_init {
  460. struct list_head list;
  461. char *init;
  462. int index;
  463. };
  464. static DEFINE_SPINLOCK(transports_lock);
  465. static LIST_HEAD(transports);
  466. /* Filled in during early boot */
  467. static LIST_HEAD(eth_cmd_line);
  468. static int check_transport(struct transport *transport, char *eth, int n,
  469. void **init_out, char **mac_out, gfp_t gfp_mask)
  470. {
  471. int len;
  472. len = strlen(transport->name);
  473. if (strncmp(eth, transport->name, len))
  474. return 0;
  475. eth += len;
  476. if (*eth == ',')
  477. eth++;
  478. else if (*eth != '\0')
  479. return 0;
  480. *init_out = kmalloc(transport->setup_size, gfp_mask);
  481. if (*init_out == NULL)
  482. return 1;
  483. if (!transport->setup(eth, mac_out, *init_out)) {
  484. kfree(*init_out);
  485. *init_out = NULL;
  486. }
  487. return 1;
  488. }
  489. void register_transport(struct transport *new)
  490. {
  491. struct list_head *ele, *next;
  492. struct eth_init *eth;
  493. void *init;
  494. char *mac = NULL;
  495. int match;
  496. spin_lock(&transports_lock);
  497. BUG_ON(!list_empty(&new->list));
  498. list_add(&new->list, &transports);
  499. spin_unlock(&transports_lock);
  500. list_for_each_safe(ele, next, &eth_cmd_line) {
  501. eth = list_entry(ele, struct eth_init, list);
  502. match = check_transport(new, eth->init, eth->index, &init,
  503. &mac, GFP_KERNEL);
  504. if (!match)
  505. continue;
  506. else if (init != NULL) {
  507. eth_configure(eth->index, init, mac, new, GFP_KERNEL);
  508. kfree(init);
  509. }
  510. list_del(&eth->list);
  511. }
  512. }
  513. static int eth_setup_common(char *str, int index)
  514. {
  515. struct list_head *ele;
  516. struct transport *transport;
  517. void *init;
  518. char *mac = NULL;
  519. int found = 0;
  520. spin_lock(&transports_lock);
  521. list_for_each(ele, &transports) {
  522. transport = list_entry(ele, struct transport, list);
  523. if (!check_transport(transport, str, index, &init,
  524. &mac, GFP_ATOMIC))
  525. continue;
  526. if (init != NULL) {
  527. eth_configure(index, init, mac, transport, GFP_ATOMIC);
  528. kfree(init);
  529. }
  530. found = 1;
  531. break;
  532. }
  533. spin_unlock(&transports_lock);
  534. return found;
  535. }
  536. static int __init eth_setup(char *str)
  537. {
  538. struct eth_init *new;
  539. char *error;
  540. int n, err;
  541. err = eth_parse(str, &n, &str, &error);
  542. if (err) {
  543. printk(KERN_ERR "eth_setup - Couldn't parse '%s' : %s\n",
  544. str, error);
  545. return 1;
  546. }
  547. new = memblock_alloc(sizeof(*new), SMP_CACHE_BYTES);
  548. INIT_LIST_HEAD(&new->list);
  549. new->index = n;
  550. new->init = str;
  551. list_add_tail(&new->list, &eth_cmd_line);
  552. return 1;
  553. }
  554. __setup("eth", eth_setup);
  555. __uml_help(eth_setup,
  556. "eth[0-9]+=<transport>,<options>\n"
  557. " Configure a network device.\n\n"
  558. );
  559. static int net_config(char *str, char **error_out)
  560. {
  561. int n, err;
  562. err = eth_parse(str, &n, &str, error_out);
  563. if (err)
  564. return err;
  565. /* This string is broken up and the pieces used by the underlying
  566. * driver. So, it is freed only if eth_setup_common fails.
  567. */
  568. str = kstrdup(str, GFP_KERNEL);
  569. if (str == NULL) {
  570. *error_out = "net_config failed to strdup string";
  571. return -ENOMEM;
  572. }
  573. err = !eth_setup_common(str, n);
  574. if (err)
  575. kfree(str);
  576. return err;
  577. }
  578. static int net_id(char **str, int *start_out, int *end_out)
  579. {
  580. char *end;
  581. int n;
  582. n = simple_strtoul(*str, &end, 0);
  583. if ((*end != '\0') || (end == *str))
  584. return -1;
  585. *start_out = n;
  586. *end_out = n;
  587. *str = end;
  588. return n;
  589. }
  590. static int net_remove(int n, char **error_out)
  591. {
  592. struct uml_net *device;
  593. struct net_device *dev;
  594. struct uml_net_private *lp;
  595. device = find_device(n);
  596. if (device == NULL)
  597. return -ENODEV;
  598. dev = device->dev;
  599. lp = netdev_priv(dev);
  600. if (lp->fd > 0)
  601. return -EBUSY;
  602. unregister_netdev(dev);
  603. platform_device_unregister(&device->pdev);
  604. return 0;
  605. }
  606. static struct mc_device net_mc = {
  607. .list = LIST_HEAD_INIT(net_mc.list),
  608. .name = "eth",
  609. .config = net_config,
  610. .get_config = NULL,
  611. .id = net_id,
  612. .remove = net_remove,
  613. };
  614. #ifdef CONFIG_INET
  615. static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
  616. void *ptr)
  617. {
  618. struct in_ifaddr *ifa = ptr;
  619. struct net_device *dev = ifa->ifa_dev->dev;
  620. struct uml_net_private *lp;
  621. void (*proc)(unsigned char *, unsigned char *, void *);
  622. unsigned char addr_buf[4], netmask_buf[4];
  623. if (dev->netdev_ops->ndo_open != uml_net_open)
  624. return NOTIFY_DONE;
  625. lp = netdev_priv(dev);
  626. proc = NULL;
  627. switch (event) {
  628. case NETDEV_UP:
  629. proc = lp->add_address;
  630. break;
  631. case NETDEV_DOWN:
  632. proc = lp->delete_address;
  633. break;
  634. }
  635. if (proc != NULL) {
  636. memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
  637. memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
  638. (*proc)(addr_buf, netmask_buf, &lp->user);
  639. }
  640. return NOTIFY_DONE;
  641. }
  642. /* uml_net_init shouldn't be called twice on two CPUs at the same time */
  643. static struct notifier_block uml_inetaddr_notifier = {
  644. .notifier_call = uml_inetaddr_event,
  645. };
  646. static void inet_register(void)
  647. {
  648. struct list_head *ele;
  649. struct uml_net_private *lp;
  650. struct in_device *ip;
  651. struct in_ifaddr *in;
  652. register_inetaddr_notifier(&uml_inetaddr_notifier);
  653. /* Devices may have been opened already, so the uml_inetaddr_notifier
  654. * didn't get a chance to run for them. This fakes it so that
  655. * addresses which have already been set up get handled properly.
  656. */
  657. spin_lock(&opened_lock);
  658. list_for_each(ele, &opened) {
  659. lp = list_entry(ele, struct uml_net_private, list);
  660. ip = lp->dev->ip_ptr;
  661. if (ip == NULL)
  662. continue;
  663. in = ip->ifa_list;
  664. while (in != NULL) {
  665. uml_inetaddr_event(NULL, NETDEV_UP, in);
  666. in = in->ifa_next;
  667. }
  668. }
  669. spin_unlock(&opened_lock);
  670. }
  671. #else
  672. static inline void inet_register(void)
  673. {
  674. }
  675. #endif
  676. static int uml_net_init(void)
  677. {
  678. mconsole_register_dev(&net_mc);
  679. inet_register();
  680. return 0;
  681. }
  682. __initcall(uml_net_init);
  683. static void close_devices(void)
  684. {
  685. struct list_head *ele;
  686. struct uml_net_private *lp;
  687. spin_lock(&opened_lock);
  688. list_for_each(ele, &opened) {
  689. lp = list_entry(ele, struct uml_net_private, list);
  690. um_free_irq(lp->dev->irq, lp->dev);
  691. if ((lp->close != NULL) && (lp->fd >= 0))
  692. (*lp->close)(lp->fd, &lp->user);
  693. if (lp->remove != NULL)
  694. (*lp->remove)(&lp->user);
  695. }
  696. spin_unlock(&opened_lock);
  697. }
  698. __uml_exitcall(close_devices);
  699. void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
  700. void *),
  701. void *arg)
  702. {
  703. struct net_device *dev = d;
  704. struct in_device *ip = dev->ip_ptr;
  705. struct in_ifaddr *in;
  706. unsigned char address[4], netmask[4];
  707. if (ip == NULL) return;
  708. in = ip->ifa_list;
  709. while (in != NULL) {
  710. memcpy(address, &in->ifa_address, sizeof(address));
  711. memcpy(netmask, &in->ifa_mask, sizeof(netmask));
  712. (*cb)(address, netmask, arg);
  713. in = in->ifa_next;
  714. }
  715. }
  716. int dev_netmask(void *d, void *m)
  717. {
  718. struct net_device *dev = d;
  719. struct in_device *ip = dev->ip_ptr;
  720. struct in_ifaddr *in;
  721. __be32 *mask_out = m;
  722. if (ip == NULL)
  723. return 1;
  724. in = ip->ifa_list;
  725. if (in == NULL)
  726. return 1;
  727. *mask_out = in->ifa_mask;
  728. return 0;
  729. }
  730. void *get_output_buffer(int *len_out)
  731. {
  732. void *ret;
  733. ret = (void *) __get_free_pages(GFP_KERNEL, 0);
  734. if (ret) *len_out = PAGE_SIZE;
  735. else *len_out = 0;
  736. return ret;
  737. }
  738. void free_output_buffer(void *buffer)
  739. {
  740. free_pages((unsigned long) buffer, 0);
  741. }
  742. int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
  743. char **gate_addr)
  744. {
  745. char *remain;
  746. remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
  747. if (remain != NULL) {
  748. printk(KERN_ERR "tap_setup_common - Extra garbage on "
  749. "specification : '%s'\n", remain);
  750. return 1;
  751. }
  752. return 0;
  753. }
  754. unsigned short eth_protocol(struct sk_buff *skb)
  755. {
  756. return eth_type_trans(skb, skb->dev);
  757. }