net_kern.c 20 KB

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