common.c 21 KB

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