irlan_eth.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*********************************************************************
  2. *
  3. * Filename: irlan_eth.c
  4. * Version:
  5. * Description:
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Thu Oct 15 08:37:58 1998
  9. * Modified at: Tue Mar 21 09:06:41 2000
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. * Sources: skeleton.c by Donald Becker <becker@CESDIS.gsfc.nasa.gov>
  12. * slip.c by Laurence Culhane, <loz@holmes.demon.co.uk>
  13. * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  14. *
  15. * Copyright (c) 1998-2000 Dag Brattli, All Rights Reserved.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * Neither Dag Brattli nor University of Tromsø admit liability nor
  23. * provide warranty for any of this software. This material is
  24. * provided "AS-IS" and at no charge.
  25. *
  26. ********************************************************************/
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/inetdevice.h>
  30. #include <linux/if_arp.h>
  31. #include <linux/module.h>
  32. #include <net/arp.h>
  33. #include <net/irda/irda.h>
  34. #include <net/irda/irmod.h>
  35. #include <net/irda/irlan_common.h>
  36. #include <net/irda/irlan_client.h>
  37. #include <net/irda/irlan_event.h>
  38. #include <net/irda/irlan_eth.h>
  39. static int irlan_eth_open(struct net_device *dev);
  40. static int irlan_eth_close(struct net_device *dev);
  41. static int irlan_eth_xmit(struct sk_buff *skb, struct net_device *dev);
  42. static void irlan_eth_set_multicast_list( struct net_device *dev);
  43. static struct net_device_stats *irlan_eth_get_stats(struct net_device *dev);
  44. /*
  45. * Function irlan_eth_setup (dev)
  46. *
  47. * The network device initialization function.
  48. *
  49. */
  50. static void irlan_eth_setup(struct net_device *dev)
  51. {
  52. dev->open = irlan_eth_open;
  53. dev->stop = irlan_eth_close;
  54. dev->hard_start_xmit = irlan_eth_xmit;
  55. dev->get_stats = irlan_eth_get_stats;
  56. dev->set_multicast_list = irlan_eth_set_multicast_list;
  57. dev->destructor = free_netdev;
  58. SET_MODULE_OWNER(dev);
  59. ether_setup(dev);
  60. /*
  61. * Lets do all queueing in IrTTP instead of this device driver.
  62. * Queueing here as well can introduce some strange latency
  63. * problems, which we will avoid by setting the queue size to 0.
  64. */
  65. /*
  66. * The bugs in IrTTP and IrLAN that created this latency issue
  67. * have now been fixed, and we can propagate flow control properly
  68. * to the network layer. However, this requires a minimal queue of
  69. * packets for the device.
  70. * Without flow control, the Tx Queue is 14 (ttp) + 0 (dev) = 14
  71. * With flow control, the Tx Queue is 7 (ttp) + 4 (dev) = 11
  72. * See irlan_eth_flow_indication()...
  73. * Note : this number was randomly selected and would need to
  74. * be adjusted.
  75. * Jean II */
  76. dev->tx_queue_len = 4;
  77. }
  78. /*
  79. * Function alloc_irlandev
  80. *
  81. * Allocate network device and control block
  82. *
  83. */
  84. struct net_device *alloc_irlandev(const char *name)
  85. {
  86. return alloc_netdev(sizeof(struct irlan_cb), name,
  87. irlan_eth_setup);
  88. }
  89. /*
  90. * Function irlan_eth_open (dev)
  91. *
  92. * Network device has been opened by user
  93. *
  94. */
  95. static int irlan_eth_open(struct net_device *dev)
  96. {
  97. struct irlan_cb *self = netdev_priv(dev);
  98. IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
  99. /* Ready to play! */
  100. netif_stop_queue(dev); /* Wait until data link is ready */
  101. /* We are now open, so time to do some work */
  102. self->disconnect_reason = 0;
  103. irlan_client_wakeup(self, self->saddr, self->daddr);
  104. /* Make sure we have a hardware address before we return,
  105. so DHCP clients gets happy */
  106. return wait_event_interruptible(self->open_wait,
  107. !self->tsap_data->connected);
  108. }
  109. /*
  110. * Function irlan_eth_close (dev)
  111. *
  112. * Stop the ether network device, his function will usually be called by
  113. * ifconfig down. We should now disconnect the link, We start the
  114. * close timer, so that the instance will be removed if we are unable
  115. * to discover the remote device after the disconnect.
  116. */
  117. static int irlan_eth_close(struct net_device *dev)
  118. {
  119. struct irlan_cb *self = netdev_priv(dev);
  120. IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
  121. /* Stop device */
  122. netif_stop_queue(dev);
  123. irlan_close_data_channel(self);
  124. irlan_close_tsaps(self);
  125. irlan_do_client_event(self, IRLAN_LMP_DISCONNECT, NULL);
  126. irlan_do_provider_event(self, IRLAN_LMP_DISCONNECT, NULL);
  127. /* Remove frames queued on the control channel */
  128. skb_queue_purge(&self->client.txq);
  129. self->client.tx_busy = 0;
  130. return 0;
  131. }
  132. /*
  133. * Function irlan_eth_tx (skb)
  134. *
  135. * Transmits ethernet frames over IrDA link.
  136. *
  137. */
  138. static int irlan_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  139. {
  140. struct irlan_cb *self = netdev_priv(dev);
  141. int ret;
  142. /* skb headroom large enough to contain all IrDA-headers? */
  143. if ((skb_headroom(skb) < self->max_header_size) || (skb_shared(skb))) {
  144. struct sk_buff *new_skb =
  145. skb_realloc_headroom(skb, self->max_header_size);
  146. /* We have to free the original skb anyway */
  147. dev_kfree_skb(skb);
  148. /* Did the realloc succeed? */
  149. if (new_skb == NULL)
  150. return 0;
  151. /* Use the new skb instead */
  152. skb = new_skb;
  153. }
  154. dev->trans_start = jiffies;
  155. /* Now queue the packet in the transport layer */
  156. if (self->use_udata)
  157. ret = irttp_udata_request(self->tsap_data, skb);
  158. else
  159. ret = irttp_data_request(self->tsap_data, skb);
  160. if (ret < 0) {
  161. /*
  162. * IrTTPs tx queue is full, so we just have to
  163. * drop the frame! You might think that we should
  164. * just return -1 and don't deallocate the frame,
  165. * but that is dangerous since it's possible that
  166. * we have replaced the original skb with a new
  167. * one with larger headroom, and that would really
  168. * confuse do_dev_queue_xmit() in dev.c! I have
  169. * tried :-) DB
  170. */
  171. /* irttp_data_request already free the packet */
  172. self->stats.tx_dropped++;
  173. } else {
  174. self->stats.tx_packets++;
  175. self->stats.tx_bytes += skb->len;
  176. }
  177. return 0;
  178. }
  179. /*
  180. * Function irlan_eth_receive (handle, skb)
  181. *
  182. * This function gets the data that is received on the data channel
  183. *
  184. */
  185. int irlan_eth_receive(void *instance, void *sap, struct sk_buff *skb)
  186. {
  187. struct irlan_cb *self = instance;
  188. if (skb == NULL) {
  189. ++self->stats.rx_dropped;
  190. return 0;
  191. }
  192. if (skb->len < ETH_HLEN) {
  193. IRDA_DEBUG(0, "%s() : IrLAN frame too short (%d)\n",
  194. __FUNCTION__, skb->len);
  195. ++self->stats.rx_dropped;
  196. dev_kfree_skb(skb);
  197. return 0;
  198. }
  199. /*
  200. * Adopt this frame! Important to set all these fields since they
  201. * might have been previously set by the low level IrDA network
  202. * device driver
  203. */
  204. skb->dev = self->dev;
  205. skb->protocol=eth_type_trans(skb, skb->dev); /* Remove eth header */
  206. self->stats.rx_packets++;
  207. self->stats.rx_bytes += skb->len;
  208. netif_rx(skb); /* Eat it! */
  209. return 0;
  210. }
  211. /*
  212. * Function irlan_eth_flow (status)
  213. *
  214. * Do flow control between IP/Ethernet and IrLAN/IrTTP. This is done by
  215. * controlling the queue stop/start.
  216. *
  217. * The IrDA link layer has the advantage to have flow control, and
  218. * IrTTP now properly handles that. Flow controlling the higher layers
  219. * prevent us to drop Tx packets in here (up to 15% for a TCP socket,
  220. * more for UDP socket).
  221. * Also, this allow us to reduce the overall transmit queue, which means
  222. * less latency in case of mixed traffic.
  223. * Jean II
  224. */
  225. void irlan_eth_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
  226. {
  227. struct irlan_cb *self;
  228. struct net_device *dev;
  229. self = (struct irlan_cb *) instance;
  230. IRDA_ASSERT(self != NULL, return;);
  231. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  232. dev = self->dev;
  233. IRDA_ASSERT(dev != NULL, return;);
  234. IRDA_DEBUG(0, "%s() : flow %s ; running %d\n", __FUNCTION__,
  235. flow == FLOW_STOP ? "FLOW_STOP" : "FLOW_START",
  236. netif_running(dev));
  237. switch (flow) {
  238. case FLOW_STOP:
  239. /* IrTTP is full, stop higher layers */
  240. netif_stop_queue(dev);
  241. break;
  242. case FLOW_START:
  243. default:
  244. /* Tell upper layers that its time to transmit frames again */
  245. /* Schedule network layer */
  246. netif_wake_queue(dev);
  247. break;
  248. }
  249. }
  250. /*
  251. * Function irlan_etc_send_gratuitous_arp (dev)
  252. *
  253. * Send gratuitous ARP to announce that we have changed
  254. * hardware address, so that all peers updates their ARP tables
  255. */
  256. void irlan_eth_send_gratuitous_arp(struct net_device *dev)
  257. {
  258. struct in_device *in_dev;
  259. /*
  260. * When we get a new MAC address do a gratuitous ARP. This
  261. * is useful if we have changed access points on the same
  262. * subnet.
  263. */
  264. #ifdef CONFIG_INET
  265. IRDA_DEBUG(4, "IrLAN: Sending gratuitous ARP\n");
  266. rcu_read_lock();
  267. in_dev = __in_dev_get_rcu(dev);
  268. if (in_dev == NULL)
  269. goto out;
  270. if (in_dev->ifa_list)
  271. arp_send(ARPOP_REQUEST, ETH_P_ARP,
  272. in_dev->ifa_list->ifa_address,
  273. dev,
  274. in_dev->ifa_list->ifa_address,
  275. NULL, dev->dev_addr, NULL);
  276. out:
  277. rcu_read_unlock();
  278. #endif /* CONFIG_INET */
  279. }
  280. /*
  281. * Function set_multicast_list (dev)
  282. *
  283. * Configure the filtering of the device
  284. *
  285. */
  286. #define HW_MAX_ADDRS 4 /* Must query to get it! */
  287. static void irlan_eth_set_multicast_list(struct net_device *dev)
  288. {
  289. struct irlan_cb *self = netdev_priv(dev);
  290. IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
  291. /* Check if data channel has been connected yet */
  292. if (self->client.state != IRLAN_DATA) {
  293. IRDA_DEBUG(1, "%s(), delaying!\n", __FUNCTION__ );
  294. return;
  295. }
  296. if (dev->flags & IFF_PROMISC) {
  297. /* Enable promiscuous mode */
  298. IRDA_WARNING("Promiscous mode not implemented by IrLAN!\n");
  299. }
  300. else if ((dev->flags & IFF_ALLMULTI) || dev->mc_count > HW_MAX_ADDRS) {
  301. /* Disable promiscuous mode, use normal mode. */
  302. IRDA_DEBUG(4, "%s(), Setting multicast filter\n", __FUNCTION__ );
  303. /* hardware_set_filter(NULL); */
  304. irlan_set_multicast_filter(self, TRUE);
  305. }
  306. else if (dev->mc_count) {
  307. IRDA_DEBUG(4, "%s(), Setting multicast filter\n", __FUNCTION__ );
  308. /* Walk the address list, and load the filter */
  309. /* hardware_set_filter(dev->mc_list); */
  310. irlan_set_multicast_filter(self, TRUE);
  311. }
  312. else {
  313. IRDA_DEBUG(4, "%s(), Clearing multicast filter\n", __FUNCTION__ );
  314. irlan_set_multicast_filter(self, FALSE);
  315. }
  316. if (dev->flags & IFF_BROADCAST)
  317. irlan_set_broadcast_filter(self, TRUE);
  318. else
  319. irlan_set_broadcast_filter(self, FALSE);
  320. }
  321. /*
  322. * Function irlan_get_stats (dev)
  323. *
  324. * Get the current statistics for this device
  325. *
  326. */
  327. static struct net_device_stats *irlan_eth_get_stats(struct net_device *dev)
  328. {
  329. struct irlan_cb *self = netdev_priv(dev);
  330. return &self->stats;
  331. }