signaling.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* net/atm/signaling.c - ATM signaling */
  3. /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
  4. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  5. #include <linux/errno.h> /* error codes */
  6. #include <linux/kernel.h> /* printk */
  7. #include <linux/skbuff.h>
  8. #include <linux/wait.h>
  9. #include <linux/sched.h> /* jiffies and HZ */
  10. #include <linux/atm.h> /* ATM stuff */
  11. #include <linux/atmsap.h>
  12. #include <linux/atmsvc.h>
  13. #include <linux/atmdev.h>
  14. #include <linux/bitops.h>
  15. #include <linux/slab.h>
  16. #include "resources.h"
  17. #include "signaling.h"
  18. struct atm_vcc *sigd = NULL;
  19. static void sigd_put_skb(struct sk_buff *skb)
  20. {
  21. if (!sigd) {
  22. pr_debug("atmsvc: no signaling daemon\n");
  23. kfree_skb(skb);
  24. return;
  25. }
  26. atm_force_charge(sigd, skb->truesize);
  27. skb_queue_tail(&sk_atm(sigd)->sk_receive_queue, skb);
  28. sk_atm(sigd)->sk_data_ready(sk_atm(sigd));
  29. }
  30. static void modify_qos(struct atm_vcc *vcc, struct atmsvc_msg *msg)
  31. {
  32. struct sk_buff *skb;
  33. if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
  34. !test_bit(ATM_VF_READY, &vcc->flags))
  35. return;
  36. msg->type = as_error;
  37. if (!vcc->dev->ops->change_qos)
  38. msg->reply = -EOPNOTSUPP;
  39. else {
  40. /* should lock VCC */
  41. msg->reply = vcc->dev->ops->change_qos(vcc, &msg->qos,
  42. msg->reply);
  43. if (!msg->reply)
  44. msg->type = as_okay;
  45. }
  46. /*
  47. * Should probably just turn around the old skb. But the, the buffer
  48. * space accounting needs to follow the change too. Maybe later.
  49. */
  50. while (!(skb = alloc_skb(sizeof(struct atmsvc_msg), GFP_KERNEL)))
  51. schedule();
  52. *(struct atmsvc_msg *)skb_put(skb, sizeof(struct atmsvc_msg)) = *msg;
  53. sigd_put_skb(skb);
  54. }
  55. static int sigd_send(struct atm_vcc *vcc, struct sk_buff *skb)
  56. {
  57. struct atmsvc_msg *msg;
  58. struct atm_vcc *session_vcc;
  59. struct sock *sk;
  60. msg = (struct atmsvc_msg *) skb->data;
  61. WARN_ON(refcount_sub_and_test(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc));
  62. vcc = *(struct atm_vcc **) &msg->vcc;
  63. pr_debug("%d (0x%lx)\n", (int)msg->type, (unsigned long)vcc);
  64. sk = sk_atm(vcc);
  65. switch (msg->type) {
  66. case as_okay:
  67. sk->sk_err = -msg->reply;
  68. clear_bit(ATM_VF_WAITING, &vcc->flags);
  69. if (!*vcc->local.sas_addr.prv && !*vcc->local.sas_addr.pub) {
  70. vcc->local.sas_family = AF_ATMSVC;
  71. memcpy(vcc->local.sas_addr.prv,
  72. msg->local.sas_addr.prv, ATM_ESA_LEN);
  73. memcpy(vcc->local.sas_addr.pub,
  74. msg->local.sas_addr.pub, ATM_E164_LEN + 1);
  75. }
  76. session_vcc = vcc->session ? vcc->session : vcc;
  77. if (session_vcc->vpi || session_vcc->vci)
  78. break;
  79. session_vcc->itf = msg->pvc.sap_addr.itf;
  80. session_vcc->vpi = msg->pvc.sap_addr.vpi;
  81. session_vcc->vci = msg->pvc.sap_addr.vci;
  82. if (session_vcc->vpi || session_vcc->vci)
  83. session_vcc->qos = msg->qos;
  84. break;
  85. case as_error:
  86. clear_bit(ATM_VF_REGIS, &vcc->flags);
  87. clear_bit(ATM_VF_READY, &vcc->flags);
  88. sk->sk_err = -msg->reply;
  89. clear_bit(ATM_VF_WAITING, &vcc->flags);
  90. break;
  91. case as_indicate:
  92. vcc = *(struct atm_vcc **)&msg->listen_vcc;
  93. sk = sk_atm(vcc);
  94. pr_debug("as_indicate!!!\n");
  95. lock_sock(sk);
  96. if (sk_acceptq_is_full(sk)) {
  97. sigd_enq(NULL, as_reject, vcc, NULL, NULL);
  98. dev_kfree_skb(skb);
  99. goto as_indicate_complete;
  100. }
  101. sk->sk_ack_backlog++;
  102. skb_queue_tail(&sk->sk_receive_queue, skb);
  103. pr_debug("waking sk_sleep(sk) 0x%p\n", sk_sleep(sk));
  104. sk->sk_state_change(sk);
  105. as_indicate_complete:
  106. release_sock(sk);
  107. return 0;
  108. case as_close:
  109. set_bit(ATM_VF_RELEASED, &vcc->flags);
  110. vcc_release_async(vcc, msg->reply);
  111. goto out;
  112. case as_modify:
  113. modify_qos(vcc, msg);
  114. break;
  115. case as_addparty:
  116. case as_dropparty:
  117. sk->sk_err_soft = -msg->reply;
  118. /* < 0 failure, otherwise ep_ref */
  119. clear_bit(ATM_VF_WAITING, &vcc->flags);
  120. break;
  121. default:
  122. pr_alert("bad message type %d\n", (int)msg->type);
  123. return -EINVAL;
  124. }
  125. sk->sk_state_change(sk);
  126. out:
  127. dev_kfree_skb(skb);
  128. return 0;
  129. }
  130. void sigd_enq2(struct atm_vcc *vcc, enum atmsvc_msg_type type,
  131. struct atm_vcc *listen_vcc, const struct sockaddr_atmpvc *pvc,
  132. const struct sockaddr_atmsvc *svc, const struct atm_qos *qos,
  133. int reply)
  134. {
  135. struct sk_buff *skb;
  136. struct atmsvc_msg *msg;
  137. static unsigned int session = 0;
  138. pr_debug("%d (0x%p)\n", (int)type, vcc);
  139. while (!(skb = alloc_skb(sizeof(struct atmsvc_msg), GFP_KERNEL)))
  140. schedule();
  141. msg = skb_put_zero(skb, sizeof(struct atmsvc_msg));
  142. msg->type = type;
  143. *(struct atm_vcc **) &msg->vcc = vcc;
  144. *(struct atm_vcc **) &msg->listen_vcc = listen_vcc;
  145. msg->reply = reply;
  146. if (qos)
  147. msg->qos = *qos;
  148. if (vcc)
  149. msg->sap = vcc->sap;
  150. if (svc)
  151. msg->svc = *svc;
  152. if (vcc)
  153. msg->local = vcc->local;
  154. if (pvc)
  155. msg->pvc = *pvc;
  156. if (vcc) {
  157. if (type == as_connect && test_bit(ATM_VF_SESSION, &vcc->flags))
  158. msg->session = ++session;
  159. /* every new pmp connect gets the next session number */
  160. }
  161. sigd_put_skb(skb);
  162. if (vcc)
  163. set_bit(ATM_VF_REGIS, &vcc->flags);
  164. }
  165. void sigd_enq(struct atm_vcc *vcc, enum atmsvc_msg_type type,
  166. struct atm_vcc *listen_vcc, const struct sockaddr_atmpvc *pvc,
  167. const struct sockaddr_atmsvc *svc)
  168. {
  169. sigd_enq2(vcc, type, listen_vcc, pvc, svc, vcc ? &vcc->qos : NULL, 0);
  170. /* other ISP applications may use "reply" */
  171. }
  172. static void purge_vcc(struct atm_vcc *vcc)
  173. {
  174. if (sk_atm(vcc)->sk_family == PF_ATMSVC &&
  175. !test_bit(ATM_VF_META, &vcc->flags)) {
  176. set_bit(ATM_VF_RELEASED, &vcc->flags);
  177. clear_bit(ATM_VF_REGIS, &vcc->flags);
  178. vcc_release_async(vcc, -EUNATCH);
  179. }
  180. }
  181. static void sigd_close(struct atm_vcc *vcc)
  182. {
  183. struct sock *s;
  184. int i;
  185. pr_debug("\n");
  186. sigd = NULL;
  187. if (skb_peek(&sk_atm(vcc)->sk_receive_queue))
  188. pr_err("closing with requests pending\n");
  189. skb_queue_purge(&sk_atm(vcc)->sk_receive_queue);
  190. read_lock(&vcc_sklist_lock);
  191. for (i = 0; i < VCC_HTABLE_SIZE; ++i) {
  192. struct hlist_head *head = &vcc_hash[i];
  193. sk_for_each(s, head) {
  194. vcc = atm_sk(s);
  195. purge_vcc(vcc);
  196. }
  197. }
  198. read_unlock(&vcc_sklist_lock);
  199. }
  200. static const struct atmdev_ops sigd_dev_ops = {
  201. .close = sigd_close,
  202. .send = sigd_send
  203. };
  204. static struct atm_dev sigd_dev = {
  205. .ops = &sigd_dev_ops,
  206. .type = "sig",
  207. .number = 999,
  208. .lock = __SPIN_LOCK_UNLOCKED(sigd_dev.lock)
  209. };
  210. int sigd_attach(struct atm_vcc *vcc)
  211. {
  212. if (sigd)
  213. return -EADDRINUSE;
  214. pr_debug("\n");
  215. sigd = vcc;
  216. vcc->dev = &sigd_dev;
  217. vcc_insert_socket(sk_atm(vcc));
  218. set_bit(ATM_VF_META, &vcc->flags);
  219. set_bit(ATM_VF_READY, &vcc->flags);
  220. return 0;
  221. }