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. static inline void set_ether_mac(struct net_device *dev, unsigned char *addr)
  33. {
  34. memcpy(dev->dev_addr, addr, ETH_ALEN);
  35. }
  36. #define DRIVER_NAME "uml-netdev"
  37. static DEFINE_SPINLOCK(opened_lock);
  38. static LIST_HEAD(opened);
  39. static int uml_net_rx(struct net_device *dev)
  40. {
  41. struct uml_net_private *lp = dev->priv;
  42. int pkt_len;
  43. struct sk_buff *skb;
  44. /* If we can't allocate memory, try again next round. */
  45. skb = dev_alloc_skb(dev->mtu);
  46. if (skb == NULL) {
  47. lp->stats.rx_dropped++;
  48. return 0;
  49. }
  50. skb->dev = dev;
  51. skb_put(skb, dev->mtu);
  52. skb->mac.raw = skb->data;
  53. pkt_len = (*lp->read)(lp->fd, &skb, lp);
  54. if (pkt_len > 0) {
  55. skb_trim(skb, pkt_len);
  56. skb->protocol = (*lp->protocol)(skb);
  57. netif_rx(skb);
  58. lp->stats.rx_bytes += skb->len;
  59. lp->stats.rx_packets++;
  60. return pkt_len;
  61. }
  62. kfree_skb(skb);
  63. return pkt_len;
  64. }
  65. static void uml_dev_close(void* dev)
  66. {
  67. dev_close( (struct net_device *) dev);
  68. }
  69. irqreturn_t uml_net_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  70. {
  71. struct net_device *dev = dev_id;
  72. struct uml_net_private *lp = dev->priv;
  73. int err;
  74. if(!netif_running(dev))
  75. return(IRQ_NONE);
  76. spin_lock(&lp->lock);
  77. while((err = uml_net_rx(dev)) > 0) ;
  78. if(err < 0) {
  79. DECLARE_WORK(close_work, uml_dev_close, dev);
  80. printk(KERN_ERR
  81. "Device '%s' read returned %d, shutting it down\n",
  82. dev->name, err);
  83. /* dev_close can't be called in interrupt context, and takes
  84. * again lp->lock.
  85. * And dev_close() can be safely called multiple times on the
  86. * same device, since it tests for (dev->flags & IFF_UP). So
  87. * there's no harm in delaying the device shutdown. */
  88. schedule_work(&close_work);
  89. goto out;
  90. }
  91. reactivate_fd(lp->fd, UM_ETH_IRQ);
  92. out:
  93. spin_unlock(&lp->lock);
  94. return(IRQ_HANDLED);
  95. }
  96. static int uml_net_open(struct net_device *dev)
  97. {
  98. struct uml_net_private *lp = dev->priv;
  99. int err;
  100. if(lp->fd >= 0){
  101. err = -ENXIO;
  102. goto out;
  103. }
  104. if(!lp->have_mac){
  105. dev_ip_addr(dev, &lp->mac[2]);
  106. set_ether_mac(dev, lp->mac);
  107. }
  108. lp->fd = (*lp->open)(&lp->user);
  109. if(lp->fd < 0){
  110. err = lp->fd;
  111. goto out;
  112. }
  113. err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
  114. IRQF_DISABLED | IRQF_SHARED, dev->name, dev);
  115. if(err != 0){
  116. printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
  117. err = -ENETUNREACH;
  118. goto out_close;
  119. }
  120. lp->tl.data = (unsigned long) &lp->user;
  121. netif_start_queue(dev);
  122. /* clear buffer - it can happen that the host side of the interface
  123. * is full when we get here. In this case, new data is never queued,
  124. * SIGIOs never arrive, and the net never works.
  125. */
  126. while((err = uml_net_rx(dev)) > 0) ;
  127. spin_lock(&opened_lock);
  128. list_add(&lp->list, &opened);
  129. spin_unlock(&opened_lock);
  130. return 0;
  131. out_close:
  132. if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
  133. lp->fd = -1;
  134. out:
  135. return err;
  136. }
  137. static int uml_net_close(struct net_device *dev)
  138. {
  139. struct uml_net_private *lp = dev->priv;
  140. netif_stop_queue(dev);
  141. free_irq(dev->irq, dev);
  142. if(lp->close != NULL)
  143. (*lp->close)(lp->fd, &lp->user);
  144. lp->fd = -1;
  145. spin_lock(&opened_lock);
  146. list_del(&lp->list);
  147. spin_unlock(&opened_lock);
  148. return 0;
  149. }
  150. static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
  151. {
  152. struct uml_net_private *lp = dev->priv;
  153. unsigned long flags;
  154. int len;
  155. netif_stop_queue(dev);
  156. spin_lock_irqsave(&lp->lock, flags);
  157. len = (*lp->write)(lp->fd, &skb, lp);
  158. if(len == skb->len) {
  159. lp->stats.tx_packets++;
  160. lp->stats.tx_bytes += skb->len;
  161. dev->trans_start = jiffies;
  162. netif_start_queue(dev);
  163. /* this is normally done in the interrupt when tx finishes */
  164. netif_wake_queue(dev);
  165. }
  166. else if(len == 0){
  167. netif_start_queue(dev);
  168. lp->stats.tx_dropped++;
  169. }
  170. else {
  171. netif_start_queue(dev);
  172. printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
  173. }
  174. spin_unlock_irqrestore(&lp->lock, flags);
  175. dev_kfree_skb(skb);
  176. return 0;
  177. }
  178. static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
  179. {
  180. struct uml_net_private *lp = dev->priv;
  181. return &lp->stats;
  182. }
  183. static void uml_net_set_multicast_list(struct net_device *dev)
  184. {
  185. if (dev->flags & IFF_PROMISC) return;
  186. else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
  187. else dev->flags &= ~IFF_ALLMULTI;
  188. }
  189. static void uml_net_tx_timeout(struct net_device *dev)
  190. {
  191. dev->trans_start = jiffies;
  192. netif_wake_queue(dev);
  193. }
  194. static int uml_net_set_mac(struct net_device *dev, void *addr)
  195. {
  196. struct uml_net_private *lp = dev->priv;
  197. struct sockaddr *hwaddr = addr;
  198. spin_lock_irq(&lp->lock);
  199. set_ether_mac(dev, hwaddr->sa_data);
  200. spin_unlock_irq(&lp->lock);
  201. return(0);
  202. }
  203. static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
  204. {
  205. struct uml_net_private *lp = dev->priv;
  206. int err = 0;
  207. spin_lock_irq(&lp->lock);
  208. new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
  209. if(new_mtu < 0){
  210. err = new_mtu;
  211. goto out;
  212. }
  213. dev->mtu = new_mtu;
  214. out:
  215. spin_unlock_irq(&lp->lock);
  216. return err;
  217. }
  218. static void uml_net_get_drvinfo(struct net_device *dev,
  219. struct ethtool_drvinfo *info)
  220. {
  221. strcpy(info->driver, DRIVER_NAME);
  222. strcpy(info->version, "42");
  223. }
  224. static struct ethtool_ops uml_net_ethtool_ops = {
  225. .get_drvinfo = uml_net_get_drvinfo,
  226. .get_link = ethtool_op_get_link,
  227. };
  228. void uml_net_user_timer_expire(unsigned long _conn)
  229. {
  230. #ifdef undef
  231. struct connection *conn = (struct connection *)_conn;
  232. dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
  233. do_connect(conn);
  234. #endif
  235. }
  236. static DEFINE_SPINLOCK(devices_lock);
  237. static LIST_HEAD(devices);
  238. static struct platform_driver uml_net_driver = {
  239. .driver = {
  240. .name = DRIVER_NAME,
  241. },
  242. };
  243. static int driver_registered;
  244. static int eth_configure(int n, void *init, char *mac,
  245. struct transport *transport)
  246. {
  247. struct uml_net *device;
  248. struct net_device *dev;
  249. struct uml_net_private *lp;
  250. int save, err, size;
  251. size = transport->private_size + sizeof(struct uml_net_private) +
  252. sizeof(((struct uml_net_private *) 0)->user);
  253. device = kmalloc(sizeof(*device), GFP_KERNEL);
  254. if (device == NULL) {
  255. printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
  256. return(1);
  257. }
  258. memset(device, 0, sizeof(*device));
  259. INIT_LIST_HEAD(&device->list);
  260. device->index = n;
  261. spin_lock(&devices_lock);
  262. list_add(&device->list, &devices);
  263. spin_unlock(&devices_lock);
  264. if (setup_etheraddr(mac, device->mac))
  265. device->have_mac = 1;
  266. printk(KERN_INFO "Netdevice %d ", n);
  267. if (device->have_mac)
  268. printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
  269. device->mac[0], device->mac[1],
  270. device->mac[2], device->mac[3],
  271. device->mac[4], device->mac[5]);
  272. printk(": ");
  273. dev = alloc_etherdev(size);
  274. if (dev == NULL) {
  275. printk(KERN_ERR "eth_configure: failed to allocate device\n");
  276. return 1;
  277. }
  278. lp = dev->priv;
  279. /* This points to the transport private data. It's still clear, but we
  280. * must memset it to 0 *now*. Let's help the drivers. */
  281. memset(lp, 0, size);
  282. /* sysfs register */
  283. if (!driver_registered) {
  284. platform_driver_register(&uml_net_driver);
  285. driver_registered = 1;
  286. }
  287. device->pdev.id = n;
  288. device->pdev.name = DRIVER_NAME;
  289. platform_device_register(&device->pdev);
  290. SET_NETDEV_DEV(dev,&device->pdev.dev);
  291. /* If this name ends up conflicting with an existing registered
  292. * netdevice, that is OK, register_netdev{,ice}() will notice this
  293. * and fail.
  294. */
  295. snprintf(dev->name, sizeof(dev->name), "eth%d", n);
  296. device->dev = dev;
  297. (*transport->kern->init)(dev, init);
  298. dev->mtu = transport->user->max_packet;
  299. dev->open = uml_net_open;
  300. dev->hard_start_xmit = uml_net_start_xmit;
  301. dev->stop = uml_net_close;
  302. dev->get_stats = uml_net_get_stats;
  303. dev->set_multicast_list = uml_net_set_multicast_list;
  304. dev->tx_timeout = uml_net_tx_timeout;
  305. dev->set_mac_address = uml_net_set_mac;
  306. dev->change_mtu = uml_net_change_mtu;
  307. dev->ethtool_ops = &uml_net_ethtool_ops;
  308. dev->watchdog_timeo = (HZ >> 1);
  309. dev->irq = UM_ETH_IRQ;
  310. rtnl_lock();
  311. err = register_netdevice(dev);
  312. rtnl_unlock();
  313. if (err) {
  314. device->dev = NULL;
  315. /* XXX: should we call ->remove() here? */
  316. free_netdev(dev);
  317. return 1;
  318. }
  319. /* lp.user is the first four bytes of the transport data, which
  320. * has already been initialized. This structure assignment will
  321. * overwrite that, so we make sure that .user gets overwritten with
  322. * what it already has.
  323. */
  324. save = lp->user[0];
  325. *lp = ((struct uml_net_private)
  326. { .list = LIST_HEAD_INIT(lp->list),
  327. .dev = dev,
  328. .fd = -1,
  329. .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
  330. .have_mac = device->have_mac,
  331. .protocol = transport->kern->protocol,
  332. .open = transport->user->open,
  333. .close = transport->user->close,
  334. .remove = transport->user->remove,
  335. .read = transport->kern->read,
  336. .write = transport->kern->write,
  337. .add_address = transport->user->add_address,
  338. .delete_address = transport->user->delete_address,
  339. .set_mtu = transport->user->set_mtu,
  340. .user = { save } });
  341. init_timer(&lp->tl);
  342. spin_lock_init(&lp->lock);
  343. lp->tl.function = uml_net_user_timer_expire;
  344. if (lp->have_mac)
  345. memcpy(lp->mac, device->mac, sizeof(lp->mac));
  346. if (transport->user->init)
  347. (*transport->user->init)(&lp->user, dev);
  348. if (device->have_mac)
  349. set_ether_mac(dev, device->mac);
  350. return 0;
  351. }
  352. static struct uml_net *find_device(int n)
  353. {
  354. struct uml_net *device;
  355. struct list_head *ele;
  356. spin_lock(&devices_lock);
  357. list_for_each(ele, &devices){
  358. device = list_entry(ele, struct uml_net, list);
  359. if(device->index == n)
  360. goto out;
  361. }
  362. device = NULL;
  363. out:
  364. spin_unlock(&devices_lock);
  365. return(device);
  366. }
  367. static int eth_parse(char *str, int *index_out, char **str_out)
  368. {
  369. char *end;
  370. int n;
  371. n = simple_strtoul(str, &end, 0);
  372. if(end == str){
  373. printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
  374. return(1);
  375. }
  376. if(n < 0){
  377. printk(KERN_ERR "eth_setup: device %d is negative\n", n);
  378. return(1);
  379. }
  380. str = end;
  381. if(*str != '='){
  382. printk(KERN_ERR
  383. "eth_setup: expected '=' after device number\n");
  384. return(1);
  385. }
  386. str++;
  387. if(find_device(n)){
  388. printk(KERN_ERR "eth_setup: Device %d already configured\n",
  389. n);
  390. return(1);
  391. }
  392. if(index_out) *index_out = n;
  393. *str_out = str;
  394. return(0);
  395. }
  396. struct eth_init {
  397. struct list_head list;
  398. char *init;
  399. int index;
  400. };
  401. /* Filled in at boot time. Will need locking if the transports become
  402. * modular.
  403. */
  404. struct list_head transports = LIST_HEAD_INIT(transports);
  405. /* Filled in during early boot */
  406. struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
  407. static int check_transport(struct transport *transport, char *eth, int n,
  408. void **init_out, char **mac_out)
  409. {
  410. int len;
  411. len = strlen(transport->name);
  412. if(strncmp(eth, transport->name, len))
  413. return(0);
  414. eth += len;
  415. if(*eth == ',')
  416. eth++;
  417. else if(*eth != '\0')
  418. return(0);
  419. *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
  420. if(*init_out == NULL)
  421. return(1);
  422. if(!transport->setup(eth, mac_out, *init_out)){
  423. kfree(*init_out);
  424. *init_out = NULL;
  425. }
  426. return(1);
  427. }
  428. void register_transport(struct transport *new)
  429. {
  430. struct list_head *ele, *next;
  431. struct eth_init *eth;
  432. void *init;
  433. char *mac = NULL;
  434. int match;
  435. list_add(&new->list, &transports);
  436. list_for_each_safe(ele, next, &eth_cmd_line){
  437. eth = list_entry(ele, struct eth_init, list);
  438. match = check_transport(new, eth->init, eth->index, &init,
  439. &mac);
  440. if(!match)
  441. continue;
  442. else if(init != NULL){
  443. eth_configure(eth->index, init, mac, new);
  444. kfree(init);
  445. }
  446. list_del(&eth->list);
  447. }
  448. }
  449. static int eth_setup_common(char *str, int index)
  450. {
  451. struct list_head *ele;
  452. struct transport *transport;
  453. void *init;
  454. char *mac = NULL;
  455. list_for_each(ele, &transports){
  456. transport = list_entry(ele, struct transport, list);
  457. if(!check_transport(transport, str, index, &init, &mac))
  458. continue;
  459. if(init != NULL){
  460. eth_configure(index, init, mac, transport);
  461. kfree(init);
  462. }
  463. return(1);
  464. }
  465. return(0);
  466. }
  467. static int eth_setup(char *str)
  468. {
  469. struct eth_init *new;
  470. int n, err;
  471. err = eth_parse(str, &n, &str);
  472. if(err)
  473. 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. struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
  663. {
  664. if((skb != NULL) && (skb_tailroom(skb) < extra)){
  665. struct sk_buff *skb2;
  666. skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
  667. dev_kfree_skb(skb);
  668. skb = skb2;
  669. }
  670. if(skb != NULL) skb_put(skb, extra);
  671. return(skb);
  672. }
  673. void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
  674. void *),
  675. void *arg)
  676. {
  677. struct net_device *dev = d;
  678. struct in_device *ip = dev->ip_ptr;
  679. struct in_ifaddr *in;
  680. unsigned char address[4], netmask[4];
  681. if(ip == NULL) return;
  682. in = ip->ifa_list;
  683. while(in != NULL){
  684. memcpy(address, &in->ifa_address, sizeof(address));
  685. memcpy(netmask, &in->ifa_mask, sizeof(netmask));
  686. (*cb)(address, netmask, arg);
  687. in = in->ifa_next;
  688. }
  689. }
  690. int dev_netmask(void *d, void *m)
  691. {
  692. struct net_device *dev = d;
  693. struct in_device *ip = dev->ip_ptr;
  694. struct in_ifaddr *in;
  695. __u32 *mask_out = m;
  696. if(ip == NULL)
  697. return(1);
  698. in = ip->ifa_list;
  699. if(in == NULL)
  700. return(1);
  701. *mask_out = in->ifa_mask;
  702. return(0);
  703. }
  704. void *get_output_buffer(int *len_out)
  705. {
  706. void *ret;
  707. ret = (void *) __get_free_pages(GFP_KERNEL, 0);
  708. if(ret) *len_out = PAGE_SIZE;
  709. else *len_out = 0;
  710. return(ret);
  711. }
  712. void free_output_buffer(void *buffer)
  713. {
  714. free_pages((unsigned long) buffer, 0);
  715. }
  716. int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
  717. char **gate_addr)
  718. {
  719. char *remain;
  720. remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
  721. if(remain != NULL){
  722. printk("tap_setup_common - Extra garbage on specification : "
  723. "'%s'\n", remain);
  724. return(1);
  725. }
  726. return(0);
  727. }
  728. unsigned short eth_protocol(struct sk_buff *skb)
  729. {
  730. return(eth_type_trans(skb, skb->dev));
  731. }
  732. /*
  733. * Overrides for Emacs so that we follow Linus's tabbing style.
  734. * Emacs will notice this stuff at the end of the file and automatically
  735. * adjust the settings for this buffer only. This must remain at the end
  736. * of the file.
  737. * ---------------------------------------------------------------------------
  738. * Local variables:
  739. * c-file-style: "linux"
  740. * End:
  741. */