isdn_x25iface.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* $Id: isdn_x25iface.c,v 1.1.2.2 2004/01/12 22:37:19 keil Exp $
  2. *
  3. * Linux ISDN subsystem, X.25 related functions
  4. *
  5. * This software may be used and distributed according to the terms
  6. * of the GNU General Public License, incorporated herein by reference.
  7. *
  8. * stuff needed to support the Linux X.25 PLP code on top of devices that
  9. * can provide a lab_b service using the concap_proto mechanism.
  10. * This module supports a network interface which provides lapb_sematics
  11. * -- as defined in Documentation/networking/x25-iface.txt -- to
  12. * the upper layer and assumes that the lower layer provides a reliable
  13. * data link service by means of the concap_device_ops callbacks.
  14. *
  15. * Only protocol specific stuff goes here. Device specific stuff
  16. * goes to another -- device related -- concap_proto support source file.
  17. *
  18. */
  19. /* #include <linux/isdn.h> */
  20. #include <linux/netdevice.h>
  21. #include <linux/concap.h>
  22. #include <linux/wanrouter.h>
  23. #include <net/x25device.h>
  24. #include "isdn_x25iface.h"
  25. /* for debugging messages not to cause an oops when device pointer is NULL*/
  26. #define MY_DEVNAME(dev) ( (dev) ? (dev)->name : "DEVICE UNSPECIFIED" )
  27. typedef struct isdn_x25iface_proto_data {
  28. int magic;
  29. enum wan_states state;
  30. /* Private stuff, not to be accessed via proto_data. We provide the
  31. other storage for the concap_proto instance here as well,
  32. enabling us to allocate both with just one kmalloc(): */
  33. struct concap_proto priv;
  34. } ix25_pdata_t;
  35. /* is now in header file (extern): struct concap_proto * isdn_x25iface_proto_new(void); */
  36. static void isdn_x25iface_proto_del( struct concap_proto * );
  37. static int isdn_x25iface_proto_close( struct concap_proto * );
  38. static int isdn_x25iface_proto_restart( struct concap_proto *,
  39. struct net_device *,
  40. struct concap_device_ops *);
  41. static int isdn_x25iface_xmit( struct concap_proto *, struct sk_buff * );
  42. static int isdn_x25iface_receive( struct concap_proto *, struct sk_buff * );
  43. static int isdn_x25iface_connect_ind( struct concap_proto * );
  44. static int isdn_x25iface_disconn_ind( struct concap_proto * );
  45. static struct concap_proto_ops ix25_pops = {
  46. &isdn_x25iface_proto_new,
  47. &isdn_x25iface_proto_del,
  48. &isdn_x25iface_proto_restart,
  49. &isdn_x25iface_proto_close,
  50. &isdn_x25iface_xmit,
  51. &isdn_x25iface_receive,
  52. &isdn_x25iface_connect_ind,
  53. &isdn_x25iface_disconn_ind
  54. };
  55. /* error message helper function */
  56. static void illegal_state_warn( unsigned state, unsigned char firstbyte)
  57. {
  58. printk( KERN_WARNING "isdn_x25iface: firstbyte %x illegal in"
  59. "current state %d\n",firstbyte, state );
  60. }
  61. /* check protocol data field for consistency */
  62. static int pdata_is_bad( ix25_pdata_t * pda ){
  63. if( pda && pda -> magic == ISDN_X25IFACE_MAGIC ) return 0;
  64. printk( KERN_WARNING
  65. "isdn_x25iface_xxx: illegal pointer to proto data\n" );
  66. return 1;
  67. }
  68. /* create a new x25 interface protocol instance
  69. */
  70. struct concap_proto * isdn_x25iface_proto_new(void)
  71. {
  72. ix25_pdata_t * tmp = kmalloc(sizeof(ix25_pdata_t),GFP_KERNEL);
  73. IX25DEBUG("isdn_x25iface_proto_new\n");
  74. if( tmp ){
  75. tmp -> magic = ISDN_X25IFACE_MAGIC;
  76. tmp -> state = WAN_UNCONFIGURED;
  77. /* private data space used to hold the concap_proto data.
  78. Only to be accessed via the returned pointer */
  79. spin_lock_init(&tmp->priv.lock);
  80. tmp -> priv.dops = NULL;
  81. tmp -> priv.net_dev = NULL;
  82. tmp -> priv.pops = &ix25_pops;
  83. tmp -> priv.flags = 0;
  84. tmp -> priv.proto_data = tmp;
  85. return( &(tmp -> priv) );
  86. }
  87. return NULL;
  88. };
  89. /* close the x25iface encapsulation protocol
  90. */
  91. static int isdn_x25iface_proto_close(struct concap_proto *cprot){
  92. ix25_pdata_t *tmp;
  93. int ret = 0;
  94. ulong flags;
  95. if( ! cprot ){
  96. printk( KERN_ERR "isdn_x25iface_proto_close: "
  97. "invalid concap_proto pointer\n" );
  98. return -1;
  99. }
  100. IX25DEBUG( "isdn_x25iface_proto_close %s \n", MY_DEVNAME(cprot -> net_dev) );
  101. spin_lock_irqsave(&cprot->lock, flags);
  102. cprot -> dops = NULL;
  103. cprot -> net_dev = NULL;
  104. tmp = cprot -> proto_data;
  105. if( pdata_is_bad( tmp ) ){
  106. ret = -1;
  107. } else {
  108. tmp -> state = WAN_UNCONFIGURED;
  109. }
  110. spin_unlock_irqrestore(&cprot->lock, flags);
  111. return ret;
  112. }
  113. /* Delete the x25iface encapsulation protocol instance
  114. */
  115. static void isdn_x25iface_proto_del(struct concap_proto *cprot){
  116. ix25_pdata_t * tmp;
  117. IX25DEBUG( "isdn_x25iface_proto_del \n" );
  118. if( ! cprot ){
  119. printk( KERN_ERR "isdn_x25iface_proto_del: "
  120. "concap_proto pointer is NULL\n" );
  121. return;
  122. }
  123. tmp = cprot -> proto_data;
  124. if( tmp == NULL ){
  125. printk( KERN_ERR "isdn_x25iface_proto_del: inconsistent "
  126. "proto_data pointer (maybe already deleted?)\n");
  127. return;
  128. }
  129. /* close if the protocol is still open */
  130. if( cprot -> dops ) isdn_x25iface_proto_close(cprot);
  131. /* freeing the storage should be sufficient now. But some additional
  132. settings might help to catch wild pointer bugs */
  133. tmp -> magic = 0;
  134. cprot -> proto_data = NULL;
  135. kfree( tmp );
  136. return;
  137. }
  138. /* (re-)initialize the data structures for x25iface encapsulation
  139. */
  140. static int isdn_x25iface_proto_restart(struct concap_proto *cprot,
  141. struct net_device *ndev,
  142. struct concap_device_ops *dops)
  143. {
  144. ix25_pdata_t * pda = cprot -> proto_data ;
  145. ulong flags;
  146. IX25DEBUG( "isdn_x25iface_proto_restart %s \n", MY_DEVNAME(ndev) );
  147. if ( pdata_is_bad( pda ) ) return -1;
  148. if( !( dops && dops -> data_req && dops -> connect_req
  149. && dops -> disconn_req ) ){
  150. printk( KERN_WARNING "isdn_x25iface_restart: required dops"
  151. " missing\n" );
  152. isdn_x25iface_proto_close(cprot);
  153. return -1;
  154. }
  155. spin_lock_irqsave(&cprot->lock, flags);
  156. cprot -> net_dev = ndev;
  157. cprot -> pops = &ix25_pops;
  158. cprot -> dops = dops;
  159. pda -> state = WAN_DISCONNECTED;
  160. spin_unlock_irqrestore(&cprot->lock, flags);
  161. return 0;
  162. }
  163. /* deliver a dl_data frame received from i4l HL driver to the network layer
  164. */
  165. static int isdn_x25iface_receive(struct concap_proto *cprot, struct sk_buff *skb)
  166. {
  167. IX25DEBUG( "isdn_x25iface_receive %s \n", MY_DEVNAME(cprot->net_dev) );
  168. if ( ( (ix25_pdata_t*) (cprot->proto_data) )
  169. -> state == WAN_CONNECTED ){
  170. if( skb_push(skb, 1)){
  171. skb -> data[0]=0x00;
  172. skb->protocol = x25_type_trans(skb, cprot->net_dev);
  173. netif_rx(skb);
  174. return 0;
  175. }
  176. }
  177. printk(KERN_WARNING "isdn_x25iface_receive %s: not connected, skb dropped\n", MY_DEVNAME(cprot->net_dev) );
  178. dev_kfree_skb(skb);
  179. return -1;
  180. }
  181. /* a connection set up is indicated by lower layer
  182. */
  183. static int isdn_x25iface_connect_ind(struct concap_proto *cprot)
  184. {
  185. struct sk_buff * skb;
  186. enum wan_states *state_p
  187. = &( ( (ix25_pdata_t*) (cprot->proto_data) ) -> state);
  188. IX25DEBUG( "isdn_x25iface_connect_ind %s \n"
  189. , MY_DEVNAME(cprot->net_dev) );
  190. if( *state_p == WAN_UNCONFIGURED ){
  191. printk(KERN_WARNING
  192. "isdn_x25iface_connect_ind while unconfigured %s\n"
  193. , MY_DEVNAME(cprot->net_dev) );
  194. return -1;
  195. }
  196. *state_p = WAN_CONNECTED;
  197. skb = dev_alloc_skb(1);
  198. if( skb ){
  199. *( skb_put(skb, 1) ) = 0x01;
  200. skb->protocol = x25_type_trans(skb, cprot->net_dev);
  201. netif_rx(skb);
  202. return 0;
  203. } else {
  204. printk(KERN_WARNING "isdn_x25iface_connect_ind: "
  205. " out of memory -- disconnecting\n");
  206. cprot -> dops -> disconn_req(cprot);
  207. return -1;
  208. }
  209. }
  210. /* a disconnect is indicated by lower layer
  211. */
  212. static int isdn_x25iface_disconn_ind(struct concap_proto *cprot)
  213. {
  214. struct sk_buff *skb;
  215. enum wan_states *state_p
  216. = &( ( (ix25_pdata_t*) (cprot->proto_data) ) -> state);
  217. IX25DEBUG( "isdn_x25iface_disconn_ind %s \n", MY_DEVNAME(cprot -> net_dev) );
  218. if( *state_p == WAN_UNCONFIGURED ){
  219. printk(KERN_WARNING
  220. "isdn_x25iface_disconn_ind while unconfigured\n");
  221. return -1;
  222. }
  223. if(! cprot -> net_dev) return -1;
  224. *state_p = WAN_DISCONNECTED;
  225. skb = dev_alloc_skb(1);
  226. if( skb ){
  227. *( skb_put(skb, 1) ) = 0x02;
  228. skb->protocol = x25_type_trans(skb, cprot->net_dev);
  229. netif_rx(skb);
  230. return 0;
  231. } else {
  232. printk(KERN_WARNING "isdn_x25iface_disconn_ind:"
  233. " out of memory\n");
  234. return -1;
  235. }
  236. }
  237. /* process a frame handed over to us from linux network layer. First byte
  238. semantics as defined in Documentation/networking/x25-iface.txt
  239. */
  240. static int isdn_x25iface_xmit(struct concap_proto *cprot, struct sk_buff *skb)
  241. {
  242. unsigned char firstbyte = skb->data[0];
  243. enum wan_states *state = &((ix25_pdata_t*)cprot->proto_data)->state;
  244. int ret = 0;
  245. IX25DEBUG( "isdn_x25iface_xmit: %s first=%x state=%d \n", MY_DEVNAME(cprot -> net_dev), firstbyte, *state );
  246. switch ( firstbyte ){
  247. case 0x00: /* dl_data request */
  248. if( *state == WAN_CONNECTED ){
  249. skb_pull(skb, 1);
  250. cprot -> net_dev -> trans_start = jiffies;
  251. ret = ( cprot -> dops -> data_req(cprot, skb) );
  252. /* prepare for future retransmissions */
  253. if( ret ) skb_push(skb,1);
  254. return ret;
  255. }
  256. illegal_state_warn( *state, firstbyte );
  257. break;
  258. case 0x01: /* dl_connect request */
  259. if( *state == WAN_DISCONNECTED ){
  260. *state = WAN_CONNECTING;
  261. ret = cprot -> dops -> connect_req(cprot);
  262. if(ret){
  263. /* reset state and notify upper layer about
  264. * immidiatly failed attempts */
  265. isdn_x25iface_disconn_ind(cprot);
  266. }
  267. } else {
  268. illegal_state_warn( *state, firstbyte );
  269. }
  270. break;
  271. case 0x02: /* dl_disconnect request */
  272. switch ( *state ){
  273. case WAN_DISCONNECTED:
  274. /* Should not happen. However, give upper layer a
  275. chance to recover from inconstistency but don't
  276. trust the lower layer sending the disconn_confirm
  277. when already disconnected */
  278. printk(KERN_WARNING "isdn_x25iface_xmit: disconnect "
  279. " requested while disconnected\n" );
  280. isdn_x25iface_disconn_ind(cprot);
  281. break; /* prevent infinite loops */
  282. case WAN_CONNECTING:
  283. case WAN_CONNECTED:
  284. *state = WAN_DISCONNECTED;
  285. cprot -> dops -> disconn_req(cprot);
  286. break;
  287. default:
  288. illegal_state_warn( *state, firstbyte );
  289. }
  290. break;
  291. case 0x03: /* changing lapb parameters requested */
  292. printk(KERN_WARNING "isdn_x25iface_xmit: setting of lapb"
  293. " options not yet supported\n");
  294. break;
  295. default:
  296. printk(KERN_WARNING "isdn_x25iface_xmit: frame with illegal"
  297. " first byte %x ignored:\n", firstbyte);
  298. }
  299. dev_kfree_skb(skb);
  300. return 0;
  301. }