hdlc_x25.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic HDLC support routines for Linux
  4. * X.25 support
  5. *
  6. * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/gfp.h>
  10. #include <linux/hdlc.h>
  11. #include <linux/if_arp.h>
  12. #include <linux/inetdevice.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/lapb.h>
  16. #include <linux/module.h>
  17. #include <linux/pkt_sched.h>
  18. #include <linux/poll.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/skbuff.h>
  21. #include <net/x25device.h>
  22. static int x25_ioctl(struct net_device *dev, struct ifreq *ifr);
  23. /* These functions are callbacks called by LAPB layer */
  24. static void x25_connect_disconnect(struct net_device *dev, int reason, int code)
  25. {
  26. struct sk_buff *skb;
  27. unsigned char *ptr;
  28. if ((skb = dev_alloc_skb(1)) == NULL) {
  29. netdev_err(dev, "out of memory\n");
  30. return;
  31. }
  32. ptr = skb_put(skb, 1);
  33. *ptr = code;
  34. skb->protocol = x25_type_trans(skb, dev);
  35. netif_rx(skb);
  36. }
  37. static void x25_connected(struct net_device *dev, int reason)
  38. {
  39. x25_connect_disconnect(dev, reason, X25_IFACE_CONNECT);
  40. }
  41. static void x25_disconnected(struct net_device *dev, int reason)
  42. {
  43. x25_connect_disconnect(dev, reason, X25_IFACE_DISCONNECT);
  44. }
  45. static int x25_data_indication(struct net_device *dev, struct sk_buff *skb)
  46. {
  47. unsigned char *ptr;
  48. if (skb_cow(skb, 1)) {
  49. kfree_skb(skb);
  50. return NET_RX_DROP;
  51. }
  52. skb_push(skb, 1);
  53. skb_reset_network_header(skb);
  54. ptr = skb->data;
  55. *ptr = X25_IFACE_DATA;
  56. skb->protocol = x25_type_trans(skb, dev);
  57. return netif_rx(skb);
  58. }
  59. static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb)
  60. {
  61. hdlc_device *hdlc = dev_to_hdlc(dev);
  62. skb_reset_network_header(skb);
  63. skb->protocol = hdlc_type_trans(skb, dev);
  64. if (dev_nit_active(dev))
  65. dev_queue_xmit_nit(skb, dev);
  66. hdlc->xmit(skb, dev); /* Ignore return value :-( */
  67. }
  68. static netdev_tx_t x25_xmit(struct sk_buff *skb, struct net_device *dev)
  69. {
  70. int result;
  71. /* X.25 to LAPB */
  72. switch (skb->data[0]) {
  73. case X25_IFACE_DATA: /* Data to be transmitted */
  74. skb_pull(skb, 1);
  75. skb_reset_network_header(skb);
  76. if ((result = lapb_data_request(dev, skb)) != LAPB_OK)
  77. dev_kfree_skb(skb);
  78. return NETDEV_TX_OK;
  79. case X25_IFACE_CONNECT:
  80. if ((result = lapb_connect_request(dev))!= LAPB_OK) {
  81. if (result == LAPB_CONNECTED)
  82. /* Send connect confirm. msg to level 3 */
  83. x25_connected(dev, 0);
  84. else
  85. netdev_err(dev, "LAPB connect request failed, error code = %i\n",
  86. result);
  87. }
  88. break;
  89. case X25_IFACE_DISCONNECT:
  90. if ((result = lapb_disconnect_request(dev)) != LAPB_OK) {
  91. if (result == LAPB_NOTCONNECTED)
  92. /* Send disconnect confirm. msg to level 3 */
  93. x25_disconnected(dev, 0);
  94. else
  95. netdev_err(dev, "LAPB disconnect request failed, error code = %i\n",
  96. result);
  97. }
  98. break;
  99. default: /* to be defined */
  100. break;
  101. }
  102. dev_kfree_skb(skb);
  103. return NETDEV_TX_OK;
  104. }
  105. static int x25_open(struct net_device *dev)
  106. {
  107. int result;
  108. static const struct lapb_register_struct cb = {
  109. .connect_confirmation = x25_connected,
  110. .connect_indication = x25_connected,
  111. .disconnect_confirmation = x25_disconnected,
  112. .disconnect_indication = x25_disconnected,
  113. .data_indication = x25_data_indication,
  114. .data_transmit = x25_data_transmit,
  115. };
  116. result = lapb_register(dev, &cb);
  117. if (result != LAPB_OK)
  118. return result;
  119. return 0;
  120. }
  121. static void x25_close(struct net_device *dev)
  122. {
  123. lapb_unregister(dev);
  124. }
  125. static int x25_rx(struct sk_buff *skb)
  126. {
  127. struct net_device *dev = skb->dev;
  128. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
  129. dev->stats.rx_dropped++;
  130. return NET_RX_DROP;
  131. }
  132. if (lapb_data_received(dev, skb) == LAPB_OK)
  133. return NET_RX_SUCCESS;
  134. dev->stats.rx_errors++;
  135. dev_kfree_skb_any(skb);
  136. return NET_RX_DROP;
  137. }
  138. static struct hdlc_proto proto = {
  139. .open = x25_open,
  140. .close = x25_close,
  141. .ioctl = x25_ioctl,
  142. .netif_rx = x25_rx,
  143. .xmit = x25_xmit,
  144. .module = THIS_MODULE,
  145. };
  146. static int x25_ioctl(struct net_device *dev, struct ifreq *ifr)
  147. {
  148. hdlc_device *hdlc = dev_to_hdlc(dev);
  149. int result;
  150. switch (ifr->ifr_settings.type) {
  151. case IF_GET_PROTO:
  152. if (dev_to_hdlc(dev)->proto != &proto)
  153. return -EINVAL;
  154. ifr->ifr_settings.type = IF_PROTO_X25;
  155. return 0; /* return protocol only, no settable parameters */
  156. case IF_PROTO_X25:
  157. if (!capable(CAP_NET_ADMIN))
  158. return -EPERM;
  159. if (dev->flags & IFF_UP)
  160. return -EBUSY;
  161. result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  162. if (result)
  163. return result;
  164. if ((result = attach_hdlc_protocol(dev, &proto, 0)))
  165. return result;
  166. dev->type = ARPHRD_X25;
  167. call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
  168. netif_dormant_off(dev);
  169. return 0;
  170. }
  171. return -EINVAL;
  172. }
  173. static int __init mod_init(void)
  174. {
  175. register_hdlc_protocol(&proto);
  176. return 0;
  177. }
  178. static void __exit mod_exit(void)
  179. {
  180. unregister_hdlc_protocol(&proto);
  181. }
  182. module_init(mod_init);
  183. module_exit(mod_exit);
  184. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  185. MODULE_DESCRIPTION("X.25 protocol support for generic HDLC");
  186. MODULE_LICENSE("GPL v2");