net_kern.c 19 KB

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