net_kern.c 19 KB

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