isdn_x25iface.c 9.7 KB

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