nr_dev.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * Copyright Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  8. */
  9. #include <linux/config.h>
  10. #include <linux/module.h>
  11. #include <linux/proc_fs.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/fs.h>
  16. #include <linux/types.h>
  17. #include <linux/sysctl.h>
  18. #include <linux/string.h>
  19. #include <linux/socket.h>
  20. #include <linux/errno.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/in.h>
  23. #include <linux/if_ether.h> /* For the statistics structure. */
  24. #include <asm/system.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/io.h>
  27. #include <linux/inet.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/if_arp.h>
  31. #include <linux/skbuff.h>
  32. #include <net/ip.h>
  33. #include <net/arp.h>
  34. #include <net/ax25.h>
  35. #include <net/netrom.h>
  36. #ifdef CONFIG_INET
  37. /*
  38. * Only allow IP over NET/ROM frames through if the netrom device is up.
  39. */
  40. int nr_rx_ip(struct sk_buff *skb, struct net_device *dev)
  41. {
  42. struct net_device_stats *stats = netdev_priv(dev);
  43. if (!netif_running(dev)) {
  44. stats->rx_errors++;
  45. return 0;
  46. }
  47. stats->rx_packets++;
  48. stats->rx_bytes += skb->len;
  49. skb->protocol = htons(ETH_P_IP);
  50. /* Spoof incoming device */
  51. skb->dev = dev;
  52. skb->h.raw = skb->data;
  53. skb->nh.raw = skb->data;
  54. skb->pkt_type = PACKET_HOST;
  55. ip_rcv(skb, skb->dev, NULL);
  56. return 1;
  57. }
  58. static int nr_rebuild_header(struct sk_buff *skb)
  59. {
  60. struct net_device *dev = skb->dev;
  61. struct net_device_stats *stats = netdev_priv(dev);
  62. struct sk_buff *skbn;
  63. unsigned char *bp = skb->data;
  64. int len;
  65. if (arp_find(bp + 7, skb)) {
  66. return 1;
  67. }
  68. bp[6] &= ~AX25_CBIT;
  69. bp[6] &= ~AX25_EBIT;
  70. bp[6] |= AX25_SSSID_SPARE;
  71. bp += AX25_ADDR_LEN;
  72. bp[6] &= ~AX25_CBIT;
  73. bp[6] |= AX25_EBIT;
  74. bp[6] |= AX25_SSSID_SPARE;
  75. if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
  76. kfree_skb(skb);
  77. return 1;
  78. }
  79. if (skb->sk != NULL)
  80. skb_set_owner_w(skbn, skb->sk);
  81. kfree_skb(skb);
  82. len = skbn->len;
  83. if (!nr_route_frame(skbn, NULL)) {
  84. kfree_skb(skbn);
  85. stats->tx_errors++;
  86. }
  87. stats->tx_packets++;
  88. stats->tx_bytes += len;
  89. return 1;
  90. }
  91. #else
  92. static int nr_rebuild_header(struct sk_buff *skb)
  93. {
  94. return 1;
  95. }
  96. #endif
  97. static int nr_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
  98. void *daddr, void *saddr, unsigned len)
  99. {
  100. unsigned char *buff = skb_push(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
  101. memcpy(buff, (saddr != NULL) ? saddr : dev->dev_addr, dev->addr_len);
  102. buff[6] &= ~AX25_CBIT;
  103. buff[6] &= ~AX25_EBIT;
  104. buff[6] |= AX25_SSSID_SPARE;
  105. buff += AX25_ADDR_LEN;
  106. if (daddr != NULL)
  107. memcpy(buff, daddr, dev->addr_len);
  108. buff[6] &= ~AX25_CBIT;
  109. buff[6] |= AX25_EBIT;
  110. buff[6] |= AX25_SSSID_SPARE;
  111. buff += AX25_ADDR_LEN;
  112. *buff++ = sysctl_netrom_network_ttl_initialiser;
  113. *buff++ = NR_PROTO_IP;
  114. *buff++ = NR_PROTO_IP;
  115. *buff++ = 0;
  116. *buff++ = 0;
  117. *buff++ = NR_PROTOEXT;
  118. if (daddr != NULL)
  119. return 37;
  120. return -37;
  121. }
  122. static int nr_set_mac_address(struct net_device *dev, void *addr)
  123. {
  124. struct sockaddr *sa = addr;
  125. if (dev->flags & IFF_UP)
  126. ax25_listen_release((ax25_address *)dev->dev_addr, NULL);
  127. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  128. if (dev->flags & IFF_UP)
  129. ax25_listen_register((ax25_address *)dev->dev_addr, NULL);
  130. return 0;
  131. }
  132. static int nr_open(struct net_device *dev)
  133. {
  134. netif_start_queue(dev);
  135. ax25_listen_register((ax25_address *)dev->dev_addr, NULL);
  136. return 0;
  137. }
  138. static int nr_close(struct net_device *dev)
  139. {
  140. ax25_listen_release((ax25_address *)dev->dev_addr, NULL);
  141. netif_stop_queue(dev);
  142. return 0;
  143. }
  144. static int nr_xmit(struct sk_buff *skb, struct net_device *dev)
  145. {
  146. struct net_device_stats *stats = netdev_priv(dev);
  147. dev_kfree_skb(skb);
  148. stats->tx_errors++;
  149. return 0;
  150. }
  151. static struct net_device_stats *nr_get_stats(struct net_device *dev)
  152. {
  153. return netdev_priv(dev);
  154. }
  155. void nr_setup(struct net_device *dev)
  156. {
  157. SET_MODULE_OWNER(dev);
  158. dev->mtu = NR_MAX_PACKET_SIZE;
  159. dev->hard_start_xmit = nr_xmit;
  160. dev->open = nr_open;
  161. dev->stop = nr_close;
  162. dev->hard_header = nr_header;
  163. dev->hard_header_len = NR_NETWORK_LEN + NR_TRANSPORT_LEN;
  164. dev->addr_len = AX25_ADDR_LEN;
  165. dev->type = ARPHRD_NETROM;
  166. dev->tx_queue_len = 40;
  167. dev->rebuild_header = nr_rebuild_header;
  168. dev->set_mac_address = nr_set_mac_address;
  169. /* New-style flags. */
  170. dev->flags = 0;
  171. dev->get_stats = nr_get_stats;
  172. }