nr_dev.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/module.h>
  10. #include <linux/proc_fs.h>
  11. #include <linux/kernel.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/fs.h>
  14. #include <linux/types.h>
  15. #include <linux/sysctl.h>
  16. #include <linux/string.h>
  17. #include <linux/socket.h>
  18. #include <linux/errno.h>
  19. #include <linux/fcntl.h>
  20. #include <linux/in.h>
  21. #include <linux/if_ether.h> /* For the statistics structure. */
  22. #include <linux/slab.h>
  23. #include <asm/system.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/io.h>
  26. #include <linux/inet.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/skbuff.h>
  31. #include <net/ip.h>
  32. #include <net/arp.h>
  33. #include <net/ax25.h>
  34. #include <net/netrom.h>
  35. /*
  36. * Only allow IP over NET/ROM frames through if the netrom device is up.
  37. */
  38. int nr_rx_ip(struct sk_buff *skb, struct net_device *dev)
  39. {
  40. struct net_device_stats *stats = &dev->stats;
  41. if (!netif_running(dev)) {
  42. stats->rx_dropped++;
  43. return 0;
  44. }
  45. stats->rx_packets++;
  46. stats->rx_bytes += skb->len;
  47. skb->protocol = htons(ETH_P_IP);
  48. /* Spoof incoming device */
  49. skb->dev = dev;
  50. skb->mac_header = skb->network_header;
  51. skb_reset_network_header(skb);
  52. skb->pkt_type = PACKET_HOST;
  53. netif_rx(skb);
  54. return 1;
  55. }
  56. #ifdef CONFIG_INET
  57. static int nr_rebuild_header(struct sk_buff *skb)
  58. {
  59. unsigned char *bp = skb->data;
  60. if (arp_find(bp + 7, skb))
  61. return 1;
  62. bp[6] &= ~AX25_CBIT;
  63. bp[6] &= ~AX25_EBIT;
  64. bp[6] |= AX25_SSSID_SPARE;
  65. bp += AX25_ADDR_LEN;
  66. bp[6] &= ~AX25_CBIT;
  67. bp[6] |= AX25_EBIT;
  68. bp[6] |= AX25_SSSID_SPARE;
  69. return 0;
  70. }
  71. #else
  72. static int nr_rebuild_header(struct sk_buff *skb)
  73. {
  74. return 1;
  75. }
  76. #endif
  77. static int nr_header(struct sk_buff *skb, struct net_device *dev,
  78. unsigned short type,
  79. const void *daddr, const void *saddr, unsigned len)
  80. {
  81. unsigned char *buff = skb_push(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
  82. memcpy(buff, (saddr != NULL) ? saddr : dev->dev_addr, dev->addr_len);
  83. buff[6] &= ~AX25_CBIT;
  84. buff[6] &= ~AX25_EBIT;
  85. buff[6] |= AX25_SSSID_SPARE;
  86. buff += AX25_ADDR_LEN;
  87. if (daddr != NULL)
  88. memcpy(buff, daddr, dev->addr_len);
  89. buff[6] &= ~AX25_CBIT;
  90. buff[6] |= AX25_EBIT;
  91. buff[6] |= AX25_SSSID_SPARE;
  92. buff += AX25_ADDR_LEN;
  93. *buff++ = sysctl_netrom_network_ttl_initialiser;
  94. *buff++ = NR_PROTO_IP;
  95. *buff++ = NR_PROTO_IP;
  96. *buff++ = 0;
  97. *buff++ = 0;
  98. *buff++ = NR_PROTOEXT;
  99. if (daddr != NULL)
  100. return 37;
  101. return -37;
  102. }
  103. static int __must_check nr_set_mac_address(struct net_device *dev, void *addr)
  104. {
  105. struct sockaddr *sa = addr;
  106. int err;
  107. if (!memcmp(dev->dev_addr, sa->sa_data, dev->addr_len))
  108. return 0;
  109. if (dev->flags & IFF_UP) {
  110. err = ax25_listen_register((ax25_address *)sa->sa_data, NULL);
  111. if (err)
  112. return err;
  113. ax25_listen_release((ax25_address *)dev->dev_addr, NULL);
  114. }
  115. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  116. return 0;
  117. }
  118. static int nr_open(struct net_device *dev)
  119. {
  120. int err;
  121. err = ax25_listen_register((ax25_address *)dev->dev_addr, NULL);
  122. if (err)
  123. return err;
  124. netif_start_queue(dev);
  125. return 0;
  126. }
  127. static int nr_close(struct net_device *dev)
  128. {
  129. ax25_listen_release((ax25_address *)dev->dev_addr, NULL);
  130. netif_stop_queue(dev);
  131. return 0;
  132. }
  133. static netdev_tx_t nr_xmit(struct sk_buff *skb, struct net_device *dev)
  134. {
  135. struct net_device_stats *stats = &dev->stats;
  136. unsigned int len = skb->len;
  137. if (!nr_route_frame(skb, NULL)) {
  138. kfree_skb(skb);
  139. stats->tx_errors++;
  140. return NETDEV_TX_OK;
  141. }
  142. stats->tx_packets++;
  143. stats->tx_bytes += len;
  144. return NETDEV_TX_OK;
  145. }
  146. static const struct header_ops nr_header_ops = {
  147. .create = nr_header,
  148. .rebuild= nr_rebuild_header,
  149. };
  150. static const struct net_device_ops nr_netdev_ops = {
  151. .ndo_open = nr_open,
  152. .ndo_stop = nr_close,
  153. .ndo_start_xmit = nr_xmit,
  154. .ndo_set_mac_address = nr_set_mac_address,
  155. };
  156. void nr_setup(struct net_device *dev)
  157. {
  158. dev->mtu = NR_MAX_PACKET_SIZE;
  159. dev->netdev_ops = &nr_netdev_ops;
  160. dev->header_ops = &nr_header_ops;
  161. dev->hard_header_len = NR_NETWORK_LEN + NR_TRANSPORT_LEN;
  162. dev->addr_len = AX25_ADDR_LEN;
  163. dev->type = ARPHRD_NETROM;
  164. /* New-style flags. */
  165. dev->flags = IFF_NOARP;
  166. }