net_kern.c 19 KB

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