net_kern.c 20 KB

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