atmtcp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* drivers/atm/atmtcp.c - ATM over TCP "device" driver */
  2. /* Written 1997-2000 by Werner Almesberger, EPFL LRC/ICA */
  3. #include <linux/module.h>
  4. #include <linux/wait.h>
  5. #include <linux/atmdev.h>
  6. #include <linux/atm_tcp.h>
  7. #include <linux/bitops.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <asm/uaccess.h>
  11. #include <asm/atomic.h>
  12. extern int atm_init_aal5(struct atm_vcc *vcc); /* "raw" AAL5 transport */
  13. #define PRIV(dev) ((struct atmtcp_dev_data *) ((dev)->dev_data))
  14. struct atmtcp_dev_data {
  15. struct atm_vcc *vcc; /* control VCC; NULL if detached */
  16. int persist; /* non-zero if persistent */
  17. };
  18. #define DEV_LABEL "atmtcp"
  19. #define MAX_VPI_BITS 8 /* simplifies life */
  20. #define MAX_VCI_BITS 16
  21. /*
  22. * Hairy code ahead: the control VCC may be closed while we're still
  23. * waiting for an answer, so we need to re-validate out_vcc every once
  24. * in a while.
  25. */
  26. static int atmtcp_send_control(struct atm_vcc *vcc,int type,
  27. const struct atmtcp_control *msg,int flag)
  28. {
  29. DECLARE_WAITQUEUE(wait,current);
  30. struct atm_vcc *out_vcc;
  31. struct sk_buff *skb;
  32. struct atmtcp_control *new_msg;
  33. int old_test;
  34. int error = 0;
  35. out_vcc = PRIV(vcc->dev) ? PRIV(vcc->dev)->vcc : NULL;
  36. if (!out_vcc) return -EUNATCH;
  37. skb = alloc_skb(sizeof(*msg),GFP_KERNEL);
  38. if (!skb) return -ENOMEM;
  39. mb();
  40. out_vcc = PRIV(vcc->dev) ? PRIV(vcc->dev)->vcc : NULL;
  41. if (!out_vcc) {
  42. dev_kfree_skb(skb);
  43. return -EUNATCH;
  44. }
  45. atm_force_charge(out_vcc,skb->truesize);
  46. new_msg = (struct atmtcp_control *) skb_put(skb,sizeof(*new_msg));
  47. *new_msg = *msg;
  48. new_msg->hdr.length = ATMTCP_HDR_MAGIC;
  49. new_msg->type = type;
  50. memset(&new_msg->vcc,0,sizeof(atm_kptr_t));
  51. *(struct atm_vcc **) &new_msg->vcc = vcc;
  52. old_test = test_bit(flag,&vcc->flags);
  53. out_vcc->push(out_vcc,skb);
  54. add_wait_queue(sk_sleep(sk_atm(vcc)), &wait);
  55. while (test_bit(flag,&vcc->flags) == old_test) {
  56. mb();
  57. out_vcc = PRIV(vcc->dev) ? PRIV(vcc->dev)->vcc : NULL;
  58. if (!out_vcc) {
  59. error = -EUNATCH;
  60. break;
  61. }
  62. set_current_state(TASK_UNINTERRUPTIBLE);
  63. schedule();
  64. }
  65. set_current_state(TASK_RUNNING);
  66. remove_wait_queue(sk_sleep(sk_atm(vcc)), &wait);
  67. return error;
  68. }
  69. static int atmtcp_recv_control(const struct atmtcp_control *msg)
  70. {
  71. struct atm_vcc *vcc = *(struct atm_vcc **) &msg->vcc;
  72. vcc->vpi = msg->addr.sap_addr.vpi;
  73. vcc->vci = msg->addr.sap_addr.vci;
  74. vcc->qos = msg->qos;
  75. sk_atm(vcc)->sk_err = -msg->result;
  76. switch (msg->type) {
  77. case ATMTCP_CTRL_OPEN:
  78. change_bit(ATM_VF_READY,&vcc->flags);
  79. break;
  80. case ATMTCP_CTRL_CLOSE:
  81. change_bit(ATM_VF_ADDR,&vcc->flags);
  82. break;
  83. default:
  84. printk(KERN_ERR "atmtcp_recv_control: unknown type %d\n",
  85. msg->type);
  86. return -EINVAL;
  87. }
  88. wake_up(sk_sleep(sk_atm(vcc)));
  89. return 0;
  90. }
  91. static void atmtcp_v_dev_close(struct atm_dev *dev)
  92. {
  93. /* Nothing.... Isn't this simple :-) -- REW */
  94. }
  95. static int atmtcp_v_open(struct atm_vcc *vcc)
  96. {
  97. struct atmtcp_control msg;
  98. int error;
  99. short vpi = vcc->vpi;
  100. int vci = vcc->vci;
  101. memset(&msg,0,sizeof(msg));
  102. msg.addr.sap_family = AF_ATMPVC;
  103. msg.hdr.vpi = htons(vpi);
  104. msg.addr.sap_addr.vpi = vpi;
  105. msg.hdr.vci = htons(vci);
  106. msg.addr.sap_addr.vci = vci;
  107. if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC) return 0;
  108. msg.type = ATMTCP_CTRL_OPEN;
  109. msg.qos = vcc->qos;
  110. set_bit(ATM_VF_ADDR,&vcc->flags);
  111. clear_bit(ATM_VF_READY,&vcc->flags); /* just in case ... */
  112. error = atmtcp_send_control(vcc,ATMTCP_CTRL_OPEN,&msg,ATM_VF_READY);
  113. if (error) return error;
  114. return -sk_atm(vcc)->sk_err;
  115. }
  116. static void atmtcp_v_close(struct atm_vcc *vcc)
  117. {
  118. struct atmtcp_control msg;
  119. memset(&msg,0,sizeof(msg));
  120. msg.addr.sap_family = AF_ATMPVC;
  121. msg.addr.sap_addr.vpi = vcc->vpi;
  122. msg.addr.sap_addr.vci = vcc->vci;
  123. clear_bit(ATM_VF_READY,&vcc->flags);
  124. (void) atmtcp_send_control(vcc,ATMTCP_CTRL_CLOSE,&msg,ATM_VF_ADDR);
  125. }
  126. static int atmtcp_v_ioctl(struct atm_dev *dev,unsigned int cmd,void __user *arg)
  127. {
  128. struct atm_cirange ci;
  129. struct atm_vcc *vcc;
  130. struct hlist_node *node;
  131. struct sock *s;
  132. int i;
  133. if (cmd != ATM_SETCIRANGE) return -ENOIOCTLCMD;
  134. if (copy_from_user(&ci, arg,sizeof(ci))) return -EFAULT;
  135. if (ci.vpi_bits == ATM_CI_MAX) ci.vpi_bits = MAX_VPI_BITS;
  136. if (ci.vci_bits == ATM_CI_MAX) ci.vci_bits = MAX_VCI_BITS;
  137. if (ci.vpi_bits > MAX_VPI_BITS || ci.vpi_bits < 0 ||
  138. ci.vci_bits > MAX_VCI_BITS || ci.vci_bits < 0) return -EINVAL;
  139. read_lock(&vcc_sklist_lock);
  140. for(i = 0; i < VCC_HTABLE_SIZE; ++i) {
  141. struct hlist_head *head = &vcc_hash[i];
  142. sk_for_each(s, node, head) {
  143. vcc = atm_sk(s);
  144. if (vcc->dev != dev)
  145. continue;
  146. if ((vcc->vpi >> ci.vpi_bits) ||
  147. (vcc->vci >> ci.vci_bits)) {
  148. read_unlock(&vcc_sklist_lock);
  149. return -EBUSY;
  150. }
  151. }
  152. }
  153. read_unlock(&vcc_sklist_lock);
  154. dev->ci_range = ci;
  155. return 0;
  156. }
  157. static int atmtcp_v_send(struct atm_vcc *vcc,struct sk_buff *skb)
  158. {
  159. struct atmtcp_dev_data *dev_data;
  160. struct atm_vcc *out_vcc=NULL; /* Initializer quietens GCC warning */
  161. struct sk_buff *new_skb;
  162. struct atmtcp_hdr *hdr;
  163. int size;
  164. if (vcc->qos.txtp.traffic_class == ATM_NONE) {
  165. if (vcc->pop) vcc->pop(vcc,skb);
  166. else dev_kfree_skb(skb);
  167. return -EINVAL;
  168. }
  169. dev_data = PRIV(vcc->dev);
  170. if (dev_data) out_vcc = dev_data->vcc;
  171. if (!dev_data || !out_vcc) {
  172. if (vcc->pop) vcc->pop(vcc,skb);
  173. else dev_kfree_skb(skb);
  174. if (dev_data) return 0;
  175. atomic_inc(&vcc->stats->tx_err);
  176. return -ENOLINK;
  177. }
  178. size = skb->len+sizeof(struct atmtcp_hdr);
  179. new_skb = atm_alloc_charge(out_vcc,size,GFP_ATOMIC);
  180. if (!new_skb) {
  181. if (vcc->pop) vcc->pop(vcc,skb);
  182. else dev_kfree_skb(skb);
  183. atomic_inc(&vcc->stats->tx_err);
  184. return -ENOBUFS;
  185. }
  186. hdr = (void *) skb_put(new_skb,sizeof(struct atmtcp_hdr));
  187. hdr->vpi = htons(vcc->vpi);
  188. hdr->vci = htons(vcc->vci);
  189. hdr->length = htonl(skb->len);
  190. skb_copy_from_linear_data(skb, skb_put(new_skb, skb->len), skb->len);
  191. if (vcc->pop) vcc->pop(vcc,skb);
  192. else dev_kfree_skb(skb);
  193. out_vcc->push(out_vcc,new_skb);
  194. atomic_inc(&vcc->stats->tx);
  195. atomic_inc(&out_vcc->stats->rx);
  196. return 0;
  197. }
  198. static int atmtcp_v_proc(struct atm_dev *dev,loff_t *pos,char *page)
  199. {
  200. struct atmtcp_dev_data *dev_data = PRIV(dev);
  201. if (*pos) return 0;
  202. if (!dev_data->persist) return sprintf(page,"ephemeral\n");
  203. return sprintf(page,"persistent, %sconnected\n",
  204. dev_data->vcc ? "" : "dis");
  205. }
  206. static void atmtcp_c_close(struct atm_vcc *vcc)
  207. {
  208. struct atm_dev *atmtcp_dev;
  209. struct atmtcp_dev_data *dev_data;
  210. atmtcp_dev = (struct atm_dev *) vcc->dev_data;
  211. dev_data = PRIV(atmtcp_dev);
  212. dev_data->vcc = NULL;
  213. if (dev_data->persist) return;
  214. atmtcp_dev->dev_data = NULL;
  215. kfree(dev_data);
  216. atm_dev_deregister(atmtcp_dev);
  217. vcc->dev_data = NULL;
  218. module_put(THIS_MODULE);
  219. }
  220. static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
  221. {
  222. struct hlist_head *head;
  223. struct atm_vcc *vcc;
  224. struct hlist_node *node;
  225. struct sock *s;
  226. head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
  227. sk_for_each(s, node, head) {
  228. vcc = atm_sk(s);
  229. if (vcc->dev == dev &&
  230. vcc->vci == vci && vcc->vpi == vpi &&
  231. vcc->qos.rxtp.traffic_class != ATM_NONE) {
  232. return vcc;
  233. }
  234. }
  235. return NULL;
  236. }
  237. static int atmtcp_c_send(struct atm_vcc *vcc,struct sk_buff *skb)
  238. {
  239. struct atm_dev *dev;
  240. struct atmtcp_hdr *hdr;
  241. struct atm_vcc *out_vcc;
  242. struct sk_buff *new_skb;
  243. int result = 0;
  244. if (!skb->len) return 0;
  245. dev = vcc->dev_data;
  246. hdr = (struct atmtcp_hdr *) skb->data;
  247. if (hdr->length == ATMTCP_HDR_MAGIC) {
  248. result = atmtcp_recv_control(
  249. (struct atmtcp_control *) skb->data);
  250. goto done;
  251. }
  252. read_lock(&vcc_sklist_lock);
  253. out_vcc = find_vcc(dev, ntohs(hdr->vpi), ntohs(hdr->vci));
  254. read_unlock(&vcc_sklist_lock);
  255. if (!out_vcc) {
  256. atomic_inc(&vcc->stats->tx_err);
  257. goto done;
  258. }
  259. skb_pull(skb,sizeof(struct atmtcp_hdr));
  260. new_skb = atm_alloc_charge(out_vcc,skb->len,GFP_KERNEL);
  261. if (!new_skb) {
  262. result = -ENOBUFS;
  263. goto done;
  264. }
  265. __net_timestamp(new_skb);
  266. skb_copy_from_linear_data(skb, skb_put(new_skb, skb->len), skb->len);
  267. out_vcc->push(out_vcc,new_skb);
  268. atomic_inc(&vcc->stats->tx);
  269. atomic_inc(&out_vcc->stats->rx);
  270. done:
  271. if (vcc->pop) vcc->pop(vcc,skb);
  272. else dev_kfree_skb(skb);
  273. return result;
  274. }
  275. /*
  276. * Device operations for the virtual ATM devices created by ATMTCP.
  277. */
  278. static struct atmdev_ops atmtcp_v_dev_ops = {
  279. .dev_close = atmtcp_v_dev_close,
  280. .open = atmtcp_v_open,
  281. .close = atmtcp_v_close,
  282. .ioctl = atmtcp_v_ioctl,
  283. .send = atmtcp_v_send,
  284. .proc_read = atmtcp_v_proc,
  285. .owner = THIS_MODULE
  286. };
  287. /*
  288. * Device operations for the ATMTCP control device.
  289. */
  290. static struct atmdev_ops atmtcp_c_dev_ops = {
  291. .close = atmtcp_c_close,
  292. .send = atmtcp_c_send
  293. };
  294. static struct atm_dev atmtcp_control_dev = {
  295. .ops = &atmtcp_c_dev_ops,
  296. .type = "atmtcp",
  297. .number = 999,
  298. .lock = __SPIN_LOCK_UNLOCKED(atmtcp_control_dev.lock)
  299. };
  300. static int atmtcp_create(int itf,int persist,struct atm_dev **result)
  301. {
  302. struct atmtcp_dev_data *dev_data;
  303. struct atm_dev *dev;
  304. dev_data = kmalloc(sizeof(*dev_data),GFP_KERNEL);
  305. if (!dev_data)
  306. return -ENOMEM;
  307. dev = atm_dev_register(DEV_LABEL,NULL,&atmtcp_v_dev_ops,itf,NULL);
  308. if (!dev) {
  309. kfree(dev_data);
  310. return itf == -1 ? -ENOMEM : -EBUSY;
  311. }
  312. dev->ci_range.vpi_bits = MAX_VPI_BITS;
  313. dev->ci_range.vci_bits = MAX_VCI_BITS;
  314. dev->dev_data = dev_data;
  315. PRIV(dev)->vcc = NULL;
  316. PRIV(dev)->persist = persist;
  317. if (result) *result = dev;
  318. return 0;
  319. }
  320. static int atmtcp_attach(struct atm_vcc *vcc,int itf)
  321. {
  322. struct atm_dev *dev;
  323. dev = NULL;
  324. if (itf != -1) dev = atm_dev_lookup(itf);
  325. if (dev) {
  326. if (dev->ops != &atmtcp_v_dev_ops) {
  327. atm_dev_put(dev);
  328. return -EMEDIUMTYPE;
  329. }
  330. if (PRIV(dev)->vcc) {
  331. atm_dev_put(dev);
  332. return -EBUSY;
  333. }
  334. }
  335. else {
  336. int error;
  337. error = atmtcp_create(itf,0,&dev);
  338. if (error) return error;
  339. }
  340. PRIV(dev)->vcc = vcc;
  341. vcc->dev = &atmtcp_control_dev;
  342. vcc_insert_socket(sk_atm(vcc));
  343. set_bit(ATM_VF_META,&vcc->flags);
  344. set_bit(ATM_VF_READY,&vcc->flags);
  345. vcc->dev_data = dev;
  346. (void) atm_init_aal5(vcc); /* @@@ losing AAL in transit ... */
  347. vcc->stats = &atmtcp_control_dev.stats.aal5;
  348. return dev->number;
  349. }
  350. static int atmtcp_create_persistent(int itf)
  351. {
  352. return atmtcp_create(itf,1,NULL);
  353. }
  354. static int atmtcp_remove_persistent(int itf)
  355. {
  356. struct atm_dev *dev;
  357. struct atmtcp_dev_data *dev_data;
  358. dev = atm_dev_lookup(itf);
  359. if (!dev) return -ENODEV;
  360. if (dev->ops != &atmtcp_v_dev_ops) {
  361. atm_dev_put(dev);
  362. return -EMEDIUMTYPE;
  363. }
  364. dev_data = PRIV(dev);
  365. if (!dev_data->persist) return 0;
  366. dev_data->persist = 0;
  367. if (PRIV(dev)->vcc) return 0;
  368. kfree(dev_data);
  369. atm_dev_put(dev);
  370. atm_dev_deregister(dev);
  371. return 0;
  372. }
  373. static int atmtcp_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  374. {
  375. int err = 0;
  376. struct atm_vcc *vcc = ATM_SD(sock);
  377. if (cmd != SIOCSIFATMTCP && cmd != ATMTCP_CREATE && cmd != ATMTCP_REMOVE)
  378. return -ENOIOCTLCMD;
  379. if (!capable(CAP_NET_ADMIN))
  380. return -EPERM;
  381. switch (cmd) {
  382. case SIOCSIFATMTCP:
  383. err = atmtcp_attach(vcc, (int) arg);
  384. if (err >= 0) {
  385. sock->state = SS_CONNECTED;
  386. __module_get(THIS_MODULE);
  387. }
  388. break;
  389. case ATMTCP_CREATE:
  390. err = atmtcp_create_persistent((int) arg);
  391. break;
  392. case ATMTCP_REMOVE:
  393. err = atmtcp_remove_persistent((int) arg);
  394. break;
  395. }
  396. return err;
  397. }
  398. static struct atm_ioctl atmtcp_ioctl_ops = {
  399. .owner = THIS_MODULE,
  400. .ioctl = atmtcp_ioctl,
  401. };
  402. static __init int atmtcp_init(void)
  403. {
  404. register_atm_ioctl(&atmtcp_ioctl_ops);
  405. return 0;
  406. }
  407. static void __exit atmtcp_exit(void)
  408. {
  409. deregister_atm_ioctl(&atmtcp_ioctl_ops);
  410. }
  411. MODULE_LICENSE("GPL");
  412. module_init(atmtcp_init);
  413. module_exit(atmtcp_exit);