net_kern.c 20 KB

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