net_kern.c 19 KB

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