net_kern.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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) return(1);
  473. new = alloc_bootmem(sizeof(new));
  474. if (new == NULL){
  475. printk("eth_init : alloc_bootmem failed\n");
  476. return(1);
  477. }
  478. INIT_LIST_HEAD(&new->list);
  479. new->index = n;
  480. new->init = str;
  481. list_add_tail(&new->list, &eth_cmd_line);
  482. return(1);
  483. }
  484. __setup("eth", eth_setup);
  485. __uml_help(eth_setup,
  486. "eth[0-9]+=<transport>,<options>\n"
  487. " Configure a network device.\n\n"
  488. );
  489. #if 0
  490. static int eth_init(void)
  491. {
  492. struct list_head *ele, *next;
  493. struct eth_init *eth;
  494. list_for_each_safe(ele, next, &eth_cmd_line){
  495. eth = list_entry(ele, struct eth_init, list);
  496. if(eth_setup_common(eth->init, eth->index))
  497. list_del(&eth->list);
  498. }
  499. return(1);
  500. }
  501. __initcall(eth_init);
  502. #endif
  503. static int net_config(char *str)
  504. {
  505. int n, err;
  506. err = eth_parse(str, &n, &str);
  507. if(err) return(err);
  508. str = kstrdup(str, GFP_KERNEL);
  509. if(str == NULL){
  510. printk(KERN_ERR "net_config failed to strdup string\n");
  511. return(-1);
  512. }
  513. err = !eth_setup_common(str, n);
  514. if(err)
  515. kfree(str);
  516. return(err);
  517. }
  518. static int net_id(char **str, int *start_out, int *end_out)
  519. {
  520. char *end;
  521. int n;
  522. n = simple_strtoul(*str, &end, 0);
  523. if((*end != '\0') || (end == *str))
  524. return -1;
  525. *start_out = n;
  526. *end_out = n;
  527. *str = end;
  528. return n;
  529. }
  530. static int net_remove(int n)
  531. {
  532. struct uml_net *device;
  533. struct net_device *dev;
  534. struct uml_net_private *lp;
  535. device = find_device(n);
  536. if(device == NULL)
  537. return -ENODEV;
  538. dev = device->dev;
  539. lp = dev->priv;
  540. if(lp->fd > 0)
  541. return -EBUSY;
  542. if(lp->remove != NULL) (*lp->remove)(&lp->user);
  543. unregister_netdev(dev);
  544. platform_device_unregister(&device->pdev);
  545. list_del(&device->list);
  546. kfree(device);
  547. free_netdev(dev);
  548. return 0;
  549. }
  550. static struct mc_device net_mc = {
  551. .name = "eth",
  552. .config = net_config,
  553. .get_config = NULL,
  554. .id = net_id,
  555. .remove = net_remove,
  556. };
  557. static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
  558. void *ptr)
  559. {
  560. struct in_ifaddr *ifa = ptr;
  561. struct net_device *dev = ifa->ifa_dev->dev;
  562. struct uml_net_private *lp;
  563. void (*proc)(unsigned char *, unsigned char *, void *);
  564. unsigned char addr_buf[4], netmask_buf[4];
  565. if(dev->open != uml_net_open) return(NOTIFY_DONE);
  566. lp = dev->priv;
  567. proc = NULL;
  568. switch (event){
  569. case NETDEV_UP:
  570. proc = lp->add_address;
  571. break;
  572. case NETDEV_DOWN:
  573. proc = lp->delete_address;
  574. break;
  575. }
  576. if(proc != NULL){
  577. memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
  578. memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
  579. (*proc)(addr_buf, netmask_buf, &lp->user);
  580. }
  581. return(NOTIFY_DONE);
  582. }
  583. struct notifier_block uml_inetaddr_notifier = {
  584. .notifier_call = uml_inetaddr_event,
  585. };
  586. static int uml_net_init(void)
  587. {
  588. struct list_head *ele;
  589. struct uml_net_private *lp;
  590. struct in_device *ip;
  591. struct in_ifaddr *in;
  592. mconsole_register_dev(&net_mc);
  593. register_inetaddr_notifier(&uml_inetaddr_notifier);
  594. /* Devices may have been opened already, so the uml_inetaddr_notifier
  595. * didn't get a chance to run for them. This fakes it so that
  596. * addresses which have already been set up get handled properly.
  597. */
  598. list_for_each(ele, &opened){
  599. lp = list_entry(ele, struct uml_net_private, list);
  600. ip = lp->dev->ip_ptr;
  601. if(ip == NULL) continue;
  602. in = ip->ifa_list;
  603. while(in != NULL){
  604. uml_inetaddr_event(NULL, NETDEV_UP, in);
  605. in = in->ifa_next;
  606. }
  607. }
  608. return(0);
  609. }
  610. __initcall(uml_net_init);
  611. static void close_devices(void)
  612. {
  613. struct list_head *ele;
  614. struct uml_net_private *lp;
  615. list_for_each(ele, &opened){
  616. lp = list_entry(ele, struct uml_net_private, list);
  617. free_irq(lp->dev->irq, lp->dev);
  618. if((lp->close != NULL) && (lp->fd >= 0))
  619. (*lp->close)(lp->fd, &lp->user);
  620. if(lp->remove != NULL) (*lp->remove)(&lp->user);
  621. }
  622. }
  623. __uml_exitcall(close_devices);
  624. int setup_etheraddr(char *str, unsigned char *addr)
  625. {
  626. char *end;
  627. int i;
  628. if(str == NULL)
  629. return(0);
  630. for(i=0;i<6;i++){
  631. addr[i] = simple_strtoul(str, &end, 16);
  632. if((end == str) ||
  633. ((*end != ':') && (*end != ',') && (*end != '\0'))){
  634. printk(KERN_ERR
  635. "setup_etheraddr: failed to parse '%s' "
  636. "as an ethernet address\n", str);
  637. return(0);
  638. }
  639. str = end + 1;
  640. }
  641. if(addr[0] & 1){
  642. printk(KERN_ERR
  643. "Attempt to assign a broadcast ethernet address to a "
  644. "device disallowed\n");
  645. return(0);
  646. }
  647. return(1);
  648. }
  649. void dev_ip_addr(void *d, unsigned char *bin_buf)
  650. {
  651. struct net_device *dev = d;
  652. struct in_device *ip = dev->ip_ptr;
  653. struct in_ifaddr *in;
  654. if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
  655. printk(KERN_WARNING "dev_ip_addr - device not assigned an "
  656. "IP address\n");
  657. return;
  658. }
  659. memcpy(bin_buf, &in->ifa_address, sizeof(in->ifa_address));
  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. */