nr_dev.c 3.9 KB

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