common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /* net/atm/common.c - ATM sockets (common part for PVC and SVC) */
  2. /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
  3. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  4. #include <linux/module.h>
  5. #include <linux/kmod.h>
  6. #include <linux/net.h> /* struct socket, struct proto_ops */
  7. #include <linux/atm.h> /* ATM stuff */
  8. #include <linux/atmdev.h>
  9. #include <linux/socket.h> /* SOL_SOCKET */
  10. #include <linux/errno.h> /* error codes */
  11. #include <linux/capability.h>
  12. #include <linux/mm.h>
  13. #include <linux/sched.h>
  14. #include <linux/time.h> /* struct timeval */
  15. #include <linux/skbuff.h>
  16. #include <linux/bitops.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <net/sock.h> /* struct sock */
  20. #include <linux/uaccess.h>
  21. #include <linux/poll.h>
  22. #include <linux/atomic.h>
  23. #include "resources.h" /* atm_find_dev */
  24. #include "common.h" /* prototypes */
  25. #include "protocols.h" /* atm_init_<transport> */
  26. #include "addr.h" /* address registry */
  27. #include "signaling.h" /* for WAITING and sigd_attach */
  28. struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
  29. EXPORT_SYMBOL(vcc_hash);
  30. DEFINE_RWLOCK(vcc_sklist_lock);
  31. EXPORT_SYMBOL(vcc_sklist_lock);
  32. static ATOMIC_NOTIFIER_HEAD(atm_dev_notify_chain);
  33. static void __vcc_insert_socket(struct sock *sk)
  34. {
  35. struct atm_vcc *vcc = atm_sk(sk);
  36. struct hlist_head *head = &vcc_hash[vcc->vci & (VCC_HTABLE_SIZE - 1)];
  37. sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1);
  38. sk_add_node(sk, head);
  39. }
  40. void vcc_insert_socket(struct sock *sk)
  41. {
  42. write_lock_irq(&vcc_sklist_lock);
  43. __vcc_insert_socket(sk);
  44. write_unlock_irq(&vcc_sklist_lock);
  45. }
  46. EXPORT_SYMBOL(vcc_insert_socket);
  47. static void vcc_remove_socket(struct sock *sk)
  48. {
  49. write_lock_irq(&vcc_sklist_lock);
  50. sk_del_node_init(sk);
  51. write_unlock_irq(&vcc_sklist_lock);
  52. }
  53. static struct sk_buff *alloc_tx(struct atm_vcc *vcc, unsigned int size)
  54. {
  55. struct sk_buff *skb;
  56. struct sock *sk = sk_atm(vcc);
  57. if (sk_wmem_alloc_get(sk) && !atm_may_send(vcc, size)) {
  58. pr_debug("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
  59. sk_wmem_alloc_get(sk), size, sk->sk_sndbuf);
  60. return NULL;
  61. }
  62. while (!(skb = alloc_skb(size, GFP_KERNEL)))
  63. schedule();
  64. pr_debug("%d += %d\n", sk_wmem_alloc_get(sk), skb->truesize);
  65. atomic_add(skb->truesize, &sk->sk_wmem_alloc);
  66. return skb;
  67. }
  68. static void vcc_sock_destruct(struct sock *sk)
  69. {
  70. if (atomic_read(&sk->sk_rmem_alloc))
  71. printk(KERN_DEBUG "%s: rmem leakage (%d bytes) detected.\n",
  72. __func__, atomic_read(&sk->sk_rmem_alloc));
  73. if (atomic_read(&sk->sk_wmem_alloc))
  74. printk(KERN_DEBUG "%s: wmem leakage (%d bytes) detected.\n",
  75. __func__, atomic_read(&sk->sk_wmem_alloc));
  76. }
  77. static void vcc_def_wakeup(struct sock *sk)
  78. {
  79. struct socket_wq *wq;
  80. rcu_read_lock();
  81. wq = rcu_dereference(sk->sk_wq);
  82. if (wq_has_sleeper(wq))
  83. wake_up(&wq->wait);
  84. rcu_read_unlock();
  85. }
  86. static inline int vcc_writable(struct sock *sk)
  87. {
  88. struct atm_vcc *vcc = atm_sk(sk);
  89. return (vcc->qos.txtp.max_sdu +
  90. atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
  91. }
  92. static void vcc_write_space(struct sock *sk)
  93. {
  94. struct socket_wq *wq;
  95. rcu_read_lock();
  96. if (vcc_writable(sk)) {
  97. wq = rcu_dereference(sk->sk_wq);
  98. if (wq_has_sleeper(wq))
  99. wake_up_interruptible(&wq->wait);
  100. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  101. }
  102. rcu_read_unlock();
  103. }
  104. static struct proto vcc_proto = {
  105. .name = "VCC",
  106. .owner = THIS_MODULE,
  107. .obj_size = sizeof(struct atm_vcc),
  108. };
  109. int vcc_create(struct net *net, struct socket *sock, int protocol, int family)
  110. {
  111. struct sock *sk;
  112. struct atm_vcc *vcc;
  113. sock->sk = NULL;
  114. if (sock->type == SOCK_STREAM)
  115. return -EINVAL;
  116. sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto);
  117. if (!sk)
  118. return -ENOMEM;
  119. sock_init_data(sock, sk);
  120. sk->sk_state_change = vcc_def_wakeup;
  121. sk->sk_write_space = vcc_write_space;
  122. vcc = atm_sk(sk);
  123. vcc->dev = NULL;
  124. memset(&vcc->local, 0, sizeof(struct sockaddr_atmsvc));
  125. memset(&vcc->remote, 0, sizeof(struct sockaddr_atmsvc));
  126. vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
  127. atomic_set(&sk->sk_wmem_alloc, 1);
  128. atomic_set(&sk->sk_rmem_alloc, 0);
  129. vcc->push = NULL;
  130. vcc->pop = NULL;
  131. vcc->push_oam = NULL;
  132. vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
  133. vcc->atm_options = vcc->aal_options = 0;
  134. sk->sk_destruct = vcc_sock_destruct;
  135. return 0;
  136. }
  137. static void vcc_destroy_socket(struct sock *sk)
  138. {
  139. struct atm_vcc *vcc = atm_sk(sk);
  140. struct sk_buff *skb;
  141. set_bit(ATM_VF_CLOSE, &vcc->flags);
  142. clear_bit(ATM_VF_READY, &vcc->flags);
  143. if (vcc->dev) {
  144. if (vcc->dev->ops->close)
  145. vcc->dev->ops->close(vcc);
  146. if (vcc->push)
  147. vcc->push(vcc, NULL); /* atmarpd has no push */
  148. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  149. atm_return(vcc, skb->truesize);
  150. kfree_skb(skb);
  151. }
  152. module_put(vcc->dev->ops->owner);
  153. atm_dev_put(vcc->dev);
  154. }
  155. vcc_remove_socket(sk);
  156. }
  157. int vcc_release(struct socket *sock)
  158. {
  159. struct sock *sk = sock->sk;
  160. if (sk) {
  161. lock_sock(sk);
  162. vcc_destroy_socket(sock->sk);
  163. release_sock(sk);
  164. sock_put(sk);
  165. }
  166. return 0;
  167. }
  168. void vcc_release_async(struct atm_vcc *vcc, int reply)
  169. {
  170. struct sock *sk = sk_atm(vcc);
  171. set_bit(ATM_VF_CLOSE, &vcc->flags);
  172. sk->sk_shutdown |= RCV_SHUTDOWN;
  173. sk->sk_err = -reply;
  174. clear_bit(ATM_VF_WAITING, &vcc->flags);
  175. sk->sk_state_change(sk);
  176. }
  177. EXPORT_SYMBOL(vcc_release_async);
  178. void vcc_process_recv_queue(struct atm_vcc *vcc)
  179. {
  180. struct sk_buff_head queue, *rq;
  181. struct sk_buff *skb, *tmp;
  182. unsigned long flags;
  183. __skb_queue_head_init(&queue);
  184. rq = &sk_atm(vcc)->sk_receive_queue;
  185. spin_lock_irqsave(&rq->lock, flags);
  186. skb_queue_splice_init(rq, &queue);
  187. spin_unlock_irqrestore(&rq->lock, flags);
  188. skb_queue_walk_safe(&queue, skb, tmp) {
  189. __skb_unlink(skb, &queue);
  190. vcc->push(vcc, skb);
  191. }
  192. }
  193. EXPORT_SYMBOL(vcc_process_recv_queue);
  194. void atm_dev_signal_change(struct atm_dev *dev, char signal)
  195. {
  196. pr_debug("%s signal=%d dev=%p number=%d dev->signal=%d\n",
  197. __func__, signal, dev, dev->number, dev->signal);
  198. /* atm driver sending invalid signal */
  199. WARN_ON(signal < ATM_PHY_SIG_LOST || signal > ATM_PHY_SIG_FOUND);
  200. if (dev->signal == signal)
  201. return; /* no change */
  202. dev->signal = signal;
  203. atomic_notifier_call_chain(&atm_dev_notify_chain, signal, dev);
  204. }
  205. EXPORT_SYMBOL(atm_dev_signal_change);
  206. void atm_dev_release_vccs(struct atm_dev *dev)
  207. {
  208. int i;
  209. write_lock_irq(&vcc_sklist_lock);
  210. for (i = 0; i < VCC_HTABLE_SIZE; i++) {
  211. struct hlist_head *head = &vcc_hash[i];
  212. struct hlist_node *node, *tmp;
  213. struct sock *s;
  214. struct atm_vcc *vcc;
  215. sk_for_each_safe(s, node, tmp, head) {
  216. vcc = atm_sk(s);
  217. if (vcc->dev == dev) {
  218. vcc_release_async(vcc, -EPIPE);
  219. sk_del_node_init(s);
  220. }
  221. }
  222. }
  223. write_unlock_irq(&vcc_sklist_lock);
  224. }
  225. EXPORT_SYMBOL(atm_dev_release_vccs);
  226. static int adjust_tp(struct atm_trafprm *tp, unsigned char aal)
  227. {
  228. int max_sdu;
  229. if (!tp->traffic_class)
  230. return 0;
  231. switch (aal) {
  232. case ATM_AAL0:
  233. max_sdu = ATM_CELL_SIZE-1;
  234. break;
  235. case ATM_AAL34:
  236. max_sdu = ATM_MAX_AAL34_PDU;
  237. break;
  238. default:
  239. pr_warning("AAL problems ... (%d)\n", aal);
  240. /* fall through */
  241. case ATM_AAL5:
  242. max_sdu = ATM_MAX_AAL5_PDU;
  243. }
  244. if (!tp->max_sdu)
  245. tp->max_sdu = max_sdu;
  246. else if (tp->max_sdu > max_sdu)
  247. return -EINVAL;
  248. if (!tp->max_cdv)
  249. tp->max_cdv = ATM_MAX_CDV;
  250. return 0;
  251. }
  252. static int check_ci(const struct atm_vcc *vcc, short vpi, int vci)
  253. {
  254. struct hlist_head *head = &vcc_hash[vci & (VCC_HTABLE_SIZE - 1)];
  255. struct hlist_node *node;
  256. struct sock *s;
  257. struct atm_vcc *walk;
  258. sk_for_each(s, node, head) {
  259. walk = atm_sk(s);
  260. if (walk->dev != vcc->dev)
  261. continue;
  262. if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
  263. walk->vci == vci && ((walk->qos.txtp.traffic_class !=
  264. ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
  265. (walk->qos.rxtp.traffic_class != ATM_NONE &&
  266. vcc->qos.rxtp.traffic_class != ATM_NONE)))
  267. return -EADDRINUSE;
  268. }
  269. /* allow VCCs with same VPI/VCI iff they don't collide on
  270. TX/RX (but we may refuse such sharing for other reasons,
  271. e.g. if protocol requires to have both channels) */
  272. return 0;
  273. }
  274. static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci)
  275. {
  276. static short p; /* poor man's per-device cache */
  277. static int c;
  278. short old_p;
  279. int old_c;
  280. int err;
  281. if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
  282. err = check_ci(vcc, *vpi, *vci);
  283. return err;
  284. }
  285. /* last scan may have left values out of bounds for current device */
  286. if (*vpi != ATM_VPI_ANY)
  287. p = *vpi;
  288. else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
  289. p = 0;
  290. if (*vci != ATM_VCI_ANY)
  291. c = *vci;
  292. else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
  293. c = ATM_NOT_RSV_VCI;
  294. old_p = p;
  295. old_c = c;
  296. do {
  297. if (!check_ci(vcc, p, c)) {
  298. *vpi = p;
  299. *vci = c;
  300. return 0;
  301. }
  302. if (*vci == ATM_VCI_ANY) {
  303. c++;
  304. if (c >= 1 << vcc->dev->ci_range.vci_bits)
  305. c = ATM_NOT_RSV_VCI;
  306. }
  307. if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
  308. *vpi == ATM_VPI_ANY) {
  309. p++;
  310. if (p >= 1 << vcc->dev->ci_range.vpi_bits)
  311. p = 0;
  312. }
  313. } while (old_p != p || old_c != c);
  314. return -EADDRINUSE;
  315. }
  316. static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
  317. int vci)
  318. {
  319. struct sock *sk = sk_atm(vcc);
  320. int error;
  321. if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
  322. vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
  323. vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
  324. return -EINVAL;
  325. if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
  326. return -EPERM;
  327. error = -ENODEV;
  328. if (!try_module_get(dev->ops->owner))
  329. return error;
  330. vcc->dev = dev;
  331. write_lock_irq(&vcc_sklist_lock);
  332. if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
  333. (error = find_ci(vcc, &vpi, &vci))) {
  334. write_unlock_irq(&vcc_sklist_lock);
  335. goto fail_module_put;
  336. }
  337. vcc->vpi = vpi;
  338. vcc->vci = vci;
  339. __vcc_insert_socket(sk);
  340. write_unlock_irq(&vcc_sklist_lock);
  341. switch (vcc->qos.aal) {
  342. case ATM_AAL0:
  343. error = atm_init_aal0(vcc);
  344. vcc->stats = &dev->stats.aal0;
  345. break;
  346. case ATM_AAL34:
  347. error = atm_init_aal34(vcc);
  348. vcc->stats = &dev->stats.aal34;
  349. break;
  350. case ATM_NO_AAL:
  351. /* ATM_AAL5 is also used in the "0 for default" case */
  352. vcc->qos.aal = ATM_AAL5;
  353. /* fall through */
  354. case ATM_AAL5:
  355. error = atm_init_aal5(vcc);
  356. vcc->stats = &dev->stats.aal5;
  357. break;
  358. default:
  359. error = -EPROTOTYPE;
  360. }
  361. if (!error)
  362. error = adjust_tp(&vcc->qos.txtp, vcc->qos.aal);
  363. if (!error)
  364. error = adjust_tp(&vcc->qos.rxtp, vcc->qos.aal);
  365. if (error)
  366. goto fail;
  367. pr_debug("VCC %d.%d, AAL %d\n", vpi, vci, vcc->qos.aal);
  368. pr_debug(" TX: %d, PCR %d..%d, SDU %d\n",
  369. vcc->qos.txtp.traffic_class,
  370. vcc->qos.txtp.min_pcr,
  371. vcc->qos.txtp.max_pcr,
  372. vcc->qos.txtp.max_sdu);
  373. pr_debug(" RX: %d, PCR %d..%d, SDU %d\n",
  374. vcc->qos.rxtp.traffic_class,
  375. vcc->qos.rxtp.min_pcr,
  376. vcc->qos.rxtp.max_pcr,
  377. vcc->qos.rxtp.max_sdu);
  378. if (dev->ops->open) {
  379. error = dev->ops->open(vcc);
  380. if (error)
  381. goto fail;
  382. }
  383. return 0;
  384. fail:
  385. vcc_remove_socket(sk);
  386. fail_module_put:
  387. module_put(dev->ops->owner);
  388. /* ensure we get dev module ref count correct */
  389. vcc->dev = NULL;
  390. return error;
  391. }
  392. int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
  393. {
  394. struct atm_dev *dev;
  395. struct atm_vcc *vcc = ATM_SD(sock);
  396. int error;
  397. pr_debug("(vpi %d, vci %d)\n", vpi, vci);
  398. if (sock->state == SS_CONNECTED)
  399. return -EISCONN;
  400. if (sock->state != SS_UNCONNECTED)
  401. return -EINVAL;
  402. if (!(vpi || vci))
  403. return -EINVAL;
  404. if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
  405. clear_bit(ATM_VF_PARTIAL, &vcc->flags);
  406. else
  407. if (test_bit(ATM_VF_PARTIAL, &vcc->flags))
  408. return -EINVAL;
  409. pr_debug("(TX: cl %d,bw %d-%d,sdu %d; "
  410. "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
  411. vcc->qos.txtp.traffic_class, vcc->qos.txtp.min_pcr,
  412. vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_sdu,
  413. vcc->qos.rxtp.traffic_class, vcc->qos.rxtp.min_pcr,
  414. vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_sdu,
  415. vcc->qos.aal == ATM_AAL5 ? "" :
  416. vcc->qos.aal == ATM_AAL0 ? "" : " ??? code ",
  417. vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
  418. if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
  419. return -EBADFD;
  420. if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
  421. vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
  422. return -EINVAL;
  423. if (likely(itf != ATM_ITF_ANY)) {
  424. dev = try_then_request_module(atm_dev_lookup(itf),
  425. "atm-device-%d", itf);
  426. } else {
  427. dev = NULL;
  428. mutex_lock(&atm_dev_mutex);
  429. if (!list_empty(&atm_devs)) {
  430. dev = list_entry(atm_devs.next,
  431. struct atm_dev, dev_list);
  432. atm_dev_hold(dev);
  433. }
  434. mutex_unlock(&atm_dev_mutex);
  435. }
  436. if (!dev)
  437. return -ENODEV;
  438. error = __vcc_connect(vcc, dev, vpi, vci);
  439. if (error) {
  440. atm_dev_put(dev);
  441. return error;
  442. }
  443. if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
  444. set_bit(ATM_VF_PARTIAL, &vcc->flags);
  445. if (test_bit(ATM_VF_READY, &ATM_SD(sock)->flags))
  446. sock->state = SS_CONNECTED;
  447. return 0;
  448. }
  449. int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
  450. size_t size, int flags)
  451. {
  452. struct sock *sk = sock->sk;
  453. struct atm_vcc *vcc;
  454. struct sk_buff *skb;
  455. int copied, error = -EINVAL;
  456. if (sock->state != SS_CONNECTED)
  457. return -ENOTCONN;
  458. /* only handle MSG_DONTWAIT and MSG_PEEK */
  459. if (flags & ~(MSG_DONTWAIT | MSG_PEEK))
  460. return -EOPNOTSUPP;
  461. vcc = ATM_SD(sock);
  462. if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
  463. test_bit(ATM_VF_CLOSE, &vcc->flags) ||
  464. !test_bit(ATM_VF_READY, &vcc->flags))
  465. return 0;
  466. skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
  467. if (!skb)
  468. return error;
  469. copied = skb->len;
  470. if (copied > size) {
  471. copied = size;
  472. msg->msg_flags |= MSG_TRUNC;
  473. }
  474. error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  475. if (error)
  476. return error;
  477. sock_recv_ts_and_drops(msg, sk, skb);
  478. if (!(flags & MSG_PEEK)) {
  479. pr_debug("%d -= %d\n", atomic_read(&sk->sk_rmem_alloc),
  480. skb->truesize);
  481. atm_return(vcc, skb->truesize);
  482. }
  483. skb_free_datagram(sk, skb);
  484. return copied;
  485. }
  486. int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
  487. size_t total_len)
  488. {
  489. struct sock *sk = sock->sk;
  490. DEFINE_WAIT(wait);
  491. struct atm_vcc *vcc;
  492. struct sk_buff *skb;
  493. int eff, error;
  494. const void __user *buff;
  495. int size;
  496. lock_sock(sk);
  497. if (sock->state != SS_CONNECTED) {
  498. error = -ENOTCONN;
  499. goto out;
  500. }
  501. if (m->msg_name) {
  502. error = -EISCONN;
  503. goto out;
  504. }
  505. if (m->msg_iovlen != 1) {
  506. error = -ENOSYS; /* fix this later @@@ */
  507. goto out;
  508. }
  509. buff = m->msg_iov->iov_base;
  510. size = m->msg_iov->iov_len;
  511. vcc = ATM_SD(sock);
  512. if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
  513. test_bit(ATM_VF_CLOSE, &vcc->flags) ||
  514. !test_bit(ATM_VF_READY, &vcc->flags)) {
  515. error = -EPIPE;
  516. send_sig(SIGPIPE, current, 0);
  517. goto out;
  518. }
  519. if (!size) {
  520. error = 0;
  521. goto out;
  522. }
  523. if (size < 0 || size > vcc->qos.txtp.max_sdu) {
  524. error = -EMSGSIZE;
  525. goto out;
  526. }
  527. eff = (size+3) & ~3; /* align to word boundary */
  528. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  529. error = 0;
  530. while (!(skb = alloc_tx(vcc, eff))) {
  531. if (m->msg_flags & MSG_DONTWAIT) {
  532. error = -EAGAIN;
  533. break;
  534. }
  535. schedule();
  536. if (signal_pending(current)) {
  537. error = -ERESTARTSYS;
  538. break;
  539. }
  540. if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
  541. test_bit(ATM_VF_CLOSE, &vcc->flags) ||
  542. !test_bit(ATM_VF_READY, &vcc->flags)) {
  543. error = -EPIPE;
  544. send_sig(SIGPIPE, current, 0);
  545. break;
  546. }
  547. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  548. }
  549. finish_wait(sk_sleep(sk), &wait);
  550. if (error)
  551. goto out;
  552. skb->dev = NULL; /* for paths shared with net_device interfaces */
  553. ATM_SKB(skb)->atm_options = vcc->atm_options;
  554. if (copy_from_user(skb_put(skb, size), buff, size)) {
  555. kfree_skb(skb);
  556. error = -EFAULT;
  557. goto out;
  558. }
  559. if (eff != size)
  560. memset(skb->data + size, 0, eff-size);
  561. error = vcc->dev->ops->send(vcc, skb);
  562. error = error ? error : size;
  563. out:
  564. release_sock(sk);
  565. return error;
  566. }
  567. unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
  568. {
  569. struct sock *sk = sock->sk;
  570. struct atm_vcc *vcc;
  571. unsigned int mask;
  572. sock_poll_wait(file, sk_sleep(sk), wait);
  573. mask = 0;
  574. vcc = ATM_SD(sock);
  575. /* exceptional events */
  576. if (sk->sk_err)
  577. mask = POLLERR;
  578. if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
  579. test_bit(ATM_VF_CLOSE, &vcc->flags))
  580. mask |= POLLHUP;
  581. /* readable? */
  582. if (!skb_queue_empty(&sk->sk_receive_queue))
  583. mask |= POLLIN | POLLRDNORM;
  584. /* writable? */
  585. if (sock->state == SS_CONNECTING &&
  586. test_bit(ATM_VF_WAITING, &vcc->flags))
  587. return mask;
  588. if (vcc->qos.txtp.traffic_class != ATM_NONE &&
  589. vcc_writable(sk))
  590. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  591. return mask;
  592. }
  593. static int atm_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
  594. {
  595. int error;
  596. /*
  597. * Don't let the QoS change the already connected AAL type nor the
  598. * traffic class.
  599. */
  600. if (qos->aal != vcc->qos.aal ||
  601. qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
  602. qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
  603. return -EINVAL;
  604. error = adjust_tp(&qos->txtp, qos->aal);
  605. if (!error)
  606. error = adjust_tp(&qos->rxtp, qos->aal);
  607. if (error)
  608. return error;
  609. if (!vcc->dev->ops->change_qos)
  610. return -EOPNOTSUPP;
  611. if (sk_atm(vcc)->sk_family == AF_ATMPVC)
  612. return vcc->dev->ops->change_qos(vcc, qos, ATM_MF_SET);
  613. return svc_change_qos(vcc, qos);
  614. }
  615. static int check_tp(const struct atm_trafprm *tp)
  616. {
  617. /* @@@ Should be merged with adjust_tp */
  618. if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS)
  619. return 0;
  620. if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
  621. !tp->max_pcr)
  622. return -EINVAL;
  623. if (tp->min_pcr == ATM_MAX_PCR)
  624. return -EINVAL;
  625. if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
  626. tp->min_pcr > tp->max_pcr)
  627. return -EINVAL;
  628. /*
  629. * We allow pcr to be outside [min_pcr,max_pcr], because later
  630. * adjustment may still push it in the valid range.
  631. */
  632. return 0;
  633. }
  634. static int check_qos(const struct atm_qos *qos)
  635. {
  636. int error;
  637. if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
  638. return -EINVAL;
  639. if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
  640. qos->txtp.traffic_class && qos->rxtp.traffic_class &&
  641. qos->txtp.traffic_class != ATM_ANYCLASS &&
  642. qos->rxtp.traffic_class != ATM_ANYCLASS)
  643. return -EINVAL;
  644. error = check_tp(&qos->txtp);
  645. if (error)
  646. return error;
  647. return check_tp(&qos->rxtp);
  648. }
  649. int vcc_setsockopt(struct socket *sock, int level, int optname,
  650. char __user *optval, unsigned int optlen)
  651. {
  652. struct atm_vcc *vcc;
  653. unsigned long value;
  654. int error;
  655. if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
  656. return -EINVAL;
  657. vcc = ATM_SD(sock);
  658. switch (optname) {
  659. case SO_ATMQOS:
  660. {
  661. struct atm_qos qos;
  662. if (copy_from_user(&qos, optval, sizeof(qos)))
  663. return -EFAULT;
  664. error = check_qos(&qos);
  665. if (error)
  666. return error;
  667. if (sock->state == SS_CONNECTED)
  668. return atm_change_qos(vcc, &qos);
  669. if (sock->state != SS_UNCONNECTED)
  670. return -EBADFD;
  671. vcc->qos = qos;
  672. set_bit(ATM_VF_HASQOS, &vcc->flags);
  673. return 0;
  674. }
  675. case SO_SETCLP:
  676. if (get_user(value, (unsigned long __user *)optval))
  677. return -EFAULT;
  678. if (value)
  679. vcc->atm_options |= ATM_ATMOPT_CLP;
  680. else
  681. vcc->atm_options &= ~ATM_ATMOPT_CLP;
  682. return 0;
  683. default:
  684. if (level == SOL_SOCKET)
  685. return -EINVAL;
  686. break;
  687. }
  688. if (!vcc->dev || !vcc->dev->ops->setsockopt)
  689. return -EINVAL;
  690. return vcc->dev->ops->setsockopt(vcc, level, optname, optval, optlen);
  691. }
  692. int vcc_getsockopt(struct socket *sock, int level, int optname,
  693. char __user *optval, int __user *optlen)
  694. {
  695. struct atm_vcc *vcc;
  696. int len;
  697. if (get_user(len, optlen))
  698. return -EFAULT;
  699. if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
  700. return -EINVAL;
  701. vcc = ATM_SD(sock);
  702. switch (optname) {
  703. case SO_ATMQOS:
  704. if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
  705. return -EINVAL;
  706. return copy_to_user(optval, &vcc->qos, sizeof(vcc->qos))
  707. ? -EFAULT : 0;
  708. case SO_SETCLP:
  709. return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 : 0,
  710. (unsigned long __user *)optval) ? -EFAULT : 0;
  711. case SO_ATMPVC:
  712. {
  713. struct sockaddr_atmpvc pvc;
  714. if (!vcc->dev || !test_bit(ATM_VF_ADDR, &vcc->flags))
  715. return -ENOTCONN;
  716. memset(&pvc, 0, sizeof(pvc));
  717. pvc.sap_family = AF_ATMPVC;
  718. pvc.sap_addr.itf = vcc->dev->number;
  719. pvc.sap_addr.vpi = vcc->vpi;
  720. pvc.sap_addr.vci = vcc->vci;
  721. return copy_to_user(optval, &pvc, sizeof(pvc)) ? -EFAULT : 0;
  722. }
  723. default:
  724. if (level == SOL_SOCKET)
  725. return -EINVAL;
  726. break;
  727. }
  728. if (!vcc->dev || !vcc->dev->ops->getsockopt)
  729. return -EINVAL;
  730. return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
  731. }
  732. int register_atmdevice_notifier(struct notifier_block *nb)
  733. {
  734. return atomic_notifier_chain_register(&atm_dev_notify_chain, nb);
  735. }
  736. EXPORT_SYMBOL_GPL(register_atmdevice_notifier);
  737. void unregister_atmdevice_notifier(struct notifier_block *nb)
  738. {
  739. atomic_notifier_chain_unregister(&atm_dev_notify_chain, nb);
  740. }
  741. EXPORT_SYMBOL_GPL(unregister_atmdevice_notifier);
  742. static int __init atm_init(void)
  743. {
  744. int error;
  745. error = proto_register(&vcc_proto, 0);
  746. if (error < 0)
  747. goto out;
  748. error = atmpvc_init();
  749. if (error < 0) {
  750. pr_err("atmpvc_init() failed with %d\n", error);
  751. goto out_unregister_vcc_proto;
  752. }
  753. error = atmsvc_init();
  754. if (error < 0) {
  755. pr_err("atmsvc_init() failed with %d\n", error);
  756. goto out_atmpvc_exit;
  757. }
  758. error = atm_proc_init();
  759. if (error < 0) {
  760. pr_err("atm_proc_init() failed with %d\n", error);
  761. goto out_atmsvc_exit;
  762. }
  763. error = atm_sysfs_init();
  764. if (error < 0) {
  765. pr_err("atm_sysfs_init() failed with %d\n", error);
  766. goto out_atmproc_exit;
  767. }
  768. out:
  769. return error;
  770. out_atmproc_exit:
  771. atm_proc_exit();
  772. out_atmsvc_exit:
  773. atmsvc_exit();
  774. out_atmpvc_exit:
  775. atmsvc_exit();
  776. out_unregister_vcc_proto:
  777. proto_unregister(&vcc_proto);
  778. goto out;
  779. }
  780. static void __exit atm_exit(void)
  781. {
  782. atm_proc_exit();
  783. atm_sysfs_exit();
  784. atmsvc_exit();
  785. atmpvc_exit();
  786. proto_unregister(&vcc_proto);
  787. }
  788. subsys_initcall(atm_init);
  789. module_exit(atm_exit);
  790. MODULE_LICENSE("GPL");
  791. MODULE_ALIAS_NETPROTO(PF_ATMPVC);
  792. MODULE_ALIAS_NETPROTO(PF_ATMSVC);