atmtcp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 <linux/uaccess.h>
  11. #include <linux/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 = 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 sock *s;
  131. int i;
  132. if (cmd != ATM_SETCIRANGE) return -ENOIOCTLCMD;
  133. if (copy_from_user(&ci, arg,sizeof(ci))) return -EFAULT;
  134. if (ci.vpi_bits == ATM_CI_MAX) ci.vpi_bits = MAX_VPI_BITS;
  135. if (ci.vci_bits == ATM_CI_MAX) ci.vci_bits = MAX_VCI_BITS;
  136. if (ci.vpi_bits > MAX_VPI_BITS || ci.vpi_bits < 0 ||
  137. ci.vci_bits > MAX_VCI_BITS || ci.vci_bits < 0) return -EINVAL;
  138. read_lock(&vcc_sklist_lock);
  139. for(i = 0; i < VCC_HTABLE_SIZE; ++i) {
  140. struct hlist_head *head = &vcc_hash[i];
  141. sk_for_each(s, head) {
  142. vcc = atm_sk(s);
  143. if (vcc->dev != dev)
  144. continue;
  145. if ((vcc->vpi >> ci.vpi_bits) ||
  146. (vcc->vci >> ci.vci_bits)) {
  147. read_unlock(&vcc_sklist_lock);
  148. return -EBUSY;
  149. }
  150. }
  151. }
  152. read_unlock(&vcc_sklist_lock);
  153. dev->ci_range = ci;
  154. return 0;
  155. }
  156. static int atmtcp_v_send(struct atm_vcc *vcc,struct sk_buff *skb)
  157. {
  158. struct atmtcp_dev_data *dev_data;
  159. struct atm_vcc *out_vcc=NULL; /* Initializer quietens GCC warning */
  160. struct sk_buff *new_skb;
  161. struct atmtcp_hdr *hdr;
  162. int size;
  163. if (vcc->qos.txtp.traffic_class == ATM_NONE) {
  164. if (vcc->pop) vcc->pop(vcc,skb);
  165. else dev_kfree_skb(skb);
  166. return -EINVAL;
  167. }
  168. dev_data = PRIV(vcc->dev);
  169. if (dev_data) out_vcc = dev_data->vcc;
  170. if (!dev_data || !out_vcc) {
  171. if (vcc->pop) vcc->pop(vcc,skb);
  172. else dev_kfree_skb(skb);
  173. if (dev_data) return 0;
  174. atomic_inc(&vcc->stats->tx_err);
  175. return -ENOLINK;
  176. }
  177. size = skb->len+sizeof(struct atmtcp_hdr);
  178. new_skb = atm_alloc_charge(out_vcc,size,GFP_ATOMIC);
  179. if (!new_skb) {
  180. if (vcc->pop) vcc->pop(vcc,skb);
  181. else dev_kfree_skb(skb);
  182. atomic_inc(&vcc->stats->tx_err);
  183. return -ENOBUFS;
  184. }
  185. hdr = skb_put(new_skb, sizeof(struct atmtcp_hdr));
  186. hdr->vpi = htons(vcc->vpi);
  187. hdr->vci = htons(vcc->vci);
  188. hdr->length = htonl(skb->len);
  189. skb_copy_from_linear_data(skb, skb_put(new_skb, skb->len), skb->len);
  190. if (vcc->pop) vcc->pop(vcc,skb);
  191. else dev_kfree_skb(skb);
  192. out_vcc->push(out_vcc,new_skb);
  193. atomic_inc(&vcc->stats->tx);
  194. atomic_inc(&out_vcc->stats->rx);
  195. return 0;
  196. }
  197. static int atmtcp_v_proc(struct atm_dev *dev,loff_t *pos,char *page)
  198. {
  199. struct atmtcp_dev_data *dev_data = PRIV(dev);
  200. if (*pos) return 0;
  201. if (!dev_data->persist) return sprintf(page,"ephemeral\n");
  202. return sprintf(page,"persistent, %sconnected\n",
  203. dev_data->vcc ? "" : "dis");
  204. }
  205. static void atmtcp_c_close(struct atm_vcc *vcc)
  206. {
  207. struct atm_dev *atmtcp_dev;
  208. struct atmtcp_dev_data *dev_data;
  209. atmtcp_dev = (struct atm_dev *) vcc->dev_data;
  210. dev_data = PRIV(atmtcp_dev);
  211. dev_data->vcc = NULL;
  212. if (dev_data->persist) return;
  213. atmtcp_dev->dev_data = NULL;
  214. kfree(dev_data);
  215. atm_dev_deregister(atmtcp_dev);
  216. vcc->dev_data = NULL;
  217. module_put(THIS_MODULE);
  218. }
  219. static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
  220. {
  221. struct hlist_head *head;
  222. struct atm_vcc *vcc;
  223. struct sock *s;
  224. head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
  225. sk_for_each(s, head) {
  226. vcc = atm_sk(s);
  227. if (vcc->dev == dev &&
  228. vcc->vci == vci && vcc->vpi == vpi &&
  229. vcc->qos.rxtp.traffic_class != ATM_NONE) {
  230. return vcc;
  231. }
  232. }
  233. return NULL;
  234. }
  235. static int atmtcp_c_send(struct atm_vcc *vcc,struct sk_buff *skb)
  236. {
  237. struct atm_dev *dev;
  238. struct atmtcp_hdr *hdr;
  239. struct atm_vcc *out_vcc;
  240. struct sk_buff *new_skb;
  241. int result = 0;
  242. if (!skb->len) return 0;
  243. dev = vcc->dev_data;
  244. hdr = (struct atmtcp_hdr *) skb->data;
  245. if (hdr->length == ATMTCP_HDR_MAGIC) {
  246. result = atmtcp_recv_control(
  247. (struct atmtcp_control *) skb->data);
  248. goto done;
  249. }
  250. read_lock(&vcc_sklist_lock);
  251. out_vcc = find_vcc(dev, ntohs(hdr->vpi), ntohs(hdr->vci));
  252. read_unlock(&vcc_sklist_lock);
  253. if (!out_vcc) {
  254. result = -EUNATCH;
  255. atomic_inc(&vcc->stats->tx_err);
  256. goto done;
  257. }
  258. skb_pull(skb,sizeof(struct atmtcp_hdr));
  259. new_skb = atm_alloc_charge(out_vcc,skb->len,GFP_KERNEL);
  260. if (!new_skb) {
  261. result = -ENOBUFS;
  262. goto done;
  263. }
  264. __net_timestamp(new_skb);
  265. skb_copy_from_linear_data(skb, skb_put(new_skb, skb->len), skb->len);
  266. out_vcc->push(out_vcc,new_skb);
  267. atomic_inc(&vcc->stats->tx);
  268. atomic_inc(&out_vcc->stats->rx);
  269. done:
  270. if (vcc->pop) vcc->pop(vcc,skb);
  271. else dev_kfree_skb(skb);
  272. return result;
  273. }
  274. /*
  275. * Device operations for the virtual ATM devices created by ATMTCP.
  276. */
  277. static struct atmdev_ops atmtcp_v_dev_ops = {
  278. .dev_close = atmtcp_v_dev_close,
  279. .open = atmtcp_v_open,
  280. .close = atmtcp_v_close,
  281. .ioctl = atmtcp_v_ioctl,
  282. .send = atmtcp_v_send,
  283. .proc_read = atmtcp_v_proc,
  284. .owner = THIS_MODULE
  285. };
  286. /*
  287. * Device operations for the ATMTCP control device.
  288. */
  289. static const struct atmdev_ops atmtcp_c_dev_ops = {
  290. .close = atmtcp_c_close,
  291. .send = atmtcp_c_send
  292. };
  293. static struct atm_dev atmtcp_control_dev = {
  294. .ops = &atmtcp_c_dev_ops,
  295. .type = "atmtcp",
  296. .number = 999,
  297. .lock = __SPIN_LOCK_UNLOCKED(atmtcp_control_dev.lock)
  298. };
  299. static int atmtcp_create(int itf,int persist,struct atm_dev **result)
  300. {
  301. struct atmtcp_dev_data *dev_data;
  302. struct atm_dev *dev;
  303. dev_data = kmalloc(sizeof(*dev_data),GFP_KERNEL);
  304. if (!dev_data)
  305. return -ENOMEM;
  306. dev = atm_dev_register(DEV_LABEL,NULL,&atmtcp_v_dev_ops,itf,NULL);
  307. if (!dev) {
  308. kfree(dev_data);
  309. return itf == -1 ? -ENOMEM : -EBUSY;
  310. }
  311. dev->ci_range.vpi_bits = MAX_VPI_BITS;
  312. dev->ci_range.vci_bits = MAX_VCI_BITS;
  313. dev->dev_data = dev_data;
  314. PRIV(dev)->vcc = NULL;
  315. PRIV(dev)->persist = persist;
  316. if (result) *result = dev;
  317. return 0;
  318. }
  319. static int atmtcp_attach(struct atm_vcc *vcc,int itf)
  320. {
  321. struct atm_dev *dev;
  322. dev = NULL;
  323. if (itf != -1) dev = atm_dev_lookup(itf);
  324. if (dev) {
  325. if (dev->ops != &atmtcp_v_dev_ops) {
  326. atm_dev_put(dev);
  327. return -EMEDIUMTYPE;
  328. }
  329. if (PRIV(dev)->vcc) {
  330. atm_dev_put(dev);
  331. return -EBUSY;
  332. }
  333. }
  334. else {
  335. int error;
  336. error = atmtcp_create(itf,0,&dev);
  337. if (error) return error;
  338. }
  339. PRIV(dev)->vcc = vcc;
  340. vcc->dev = &atmtcp_control_dev;
  341. vcc_insert_socket(sk_atm(vcc));
  342. set_bit(ATM_VF_META,&vcc->flags);
  343. set_bit(ATM_VF_READY,&vcc->flags);
  344. vcc->dev_data = dev;
  345. (void) atm_init_aal5(vcc); /* @@@ losing AAL in transit ... */
  346. vcc->stats = &atmtcp_control_dev.stats.aal5;
  347. return dev->number;
  348. }
  349. static int atmtcp_create_persistent(int itf)
  350. {
  351. return atmtcp_create(itf,1,NULL);
  352. }
  353. static int atmtcp_remove_persistent(int itf)
  354. {
  355. struct atm_dev *dev;
  356. struct atmtcp_dev_data *dev_data;
  357. dev = atm_dev_lookup(itf);
  358. if (!dev) return -ENODEV;
  359. if (dev->ops != &atmtcp_v_dev_ops) {
  360. atm_dev_put(dev);
  361. return -EMEDIUMTYPE;
  362. }
  363. dev_data = PRIV(dev);
  364. if (!dev_data->persist) {
  365. atm_dev_put(dev);
  366. return 0;
  367. }
  368. dev_data->persist = 0;
  369. if (PRIV(dev)->vcc) {
  370. atm_dev_put(dev);
  371. return 0;
  372. }
  373. kfree(dev_data);
  374. atm_dev_put(dev);
  375. atm_dev_deregister(dev);
  376. return 0;
  377. }
  378. static int atmtcp_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  379. {
  380. int err = 0;
  381. struct atm_vcc *vcc = ATM_SD(sock);
  382. if (cmd != SIOCSIFATMTCP && cmd != ATMTCP_CREATE && cmd != ATMTCP_REMOVE)
  383. return -ENOIOCTLCMD;
  384. if (!capable(CAP_NET_ADMIN))
  385. return -EPERM;
  386. switch (cmd) {
  387. case SIOCSIFATMTCP:
  388. err = atmtcp_attach(vcc, (int) arg);
  389. if (err >= 0) {
  390. sock->state = SS_CONNECTED;
  391. __module_get(THIS_MODULE);
  392. }
  393. break;
  394. case ATMTCP_CREATE:
  395. err = atmtcp_create_persistent((int) arg);
  396. break;
  397. case ATMTCP_REMOVE:
  398. err = atmtcp_remove_persistent((int) arg);
  399. break;
  400. }
  401. return err;
  402. }
  403. static struct atm_ioctl atmtcp_ioctl_ops = {
  404. .owner = THIS_MODULE,
  405. .ioctl = atmtcp_ioctl,
  406. };
  407. static __init int atmtcp_init(void)
  408. {
  409. register_atm_ioctl(&atmtcp_ioctl_ops);
  410. return 0;
  411. }
  412. static void __exit atmtcp_exit(void)
  413. {
  414. deregister_atm_ioctl(&atmtcp_ioctl_ops);
  415. }
  416. MODULE_LICENSE("GPL");
  417. module_init(atmtcp_init);
  418. module_exit(atmtcp_exit);