/arch/um/drivers/net_kern.c

https://bitbucket.org/evzijst/gittest · C · 896 lines · 714 code · 141 blank · 41 comment · 131 complexity · 8bba134a9a9eec68e534342a26a429e8 MD5 · raw file

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