common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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 void vcc_release_cb(struct sock *sk)
  105. {
  106. struct atm_vcc *vcc = atm_sk(sk);
  107. if (vcc->release_cb)
  108. vcc->release_cb(vcc);
  109. }
  110. static struct proto vcc_proto = {
  111. .name = "VCC",
  112. .owner = THIS_MODULE,
  113. .obj_size = sizeof(struct atm_vcc),
  114. .release_cb = vcc_release_cb,
  115. };
  116. int vcc_create(struct net *net, struct socket *sock, int protocol, int family, int kern)
  117. {
  118. struct sock *sk;
  119. struct atm_vcc *vcc;
  120. sock->sk = NULL;
  121. if (sock->type == SOCK_STREAM)
  122. return -EINVAL;
  123. sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto, kern);
  124. if (!sk)
  125. return -ENOMEM;
  126. sock_init_data(sock, sk);
  127. sk->sk_state_change = vcc_def_wakeup;
  128. sk->sk_write_space = vcc_write_space;
  129. vcc = atm_sk(sk);
  130. vcc->dev = NULL;
  131. memset(&vcc->local, 0, sizeof(struct sockaddr_atmsvc));
  132. memset(&vcc->remote, 0, sizeof(struct sockaddr_atmsvc));
  133. vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
  134. atomic_set(&sk->sk_wmem_alloc, 1);
  135. atomic_set(&sk->sk_rmem_alloc, 0);
  136. vcc->push = NULL;
  137. vcc->pop = NULL;
  138. vcc->owner = NULL;
  139. vcc->push_oam = NULL;
  140. vcc->release_cb = NULL;
  141. vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
  142. vcc->atm_options = vcc->aal_options = 0;
  143. sk->sk_destruct = vcc_sock_destruct;
  144. return 0;
  145. }
  146. static void vcc_destroy_socket(struct sock *sk)
  147. {
  148. struct atm_vcc *vcc = atm_sk(sk);
  149. struct sk_buff *skb;
  150. set_bit(ATM_VF_CLOSE, &vcc->flags);
  151. clear_bit(ATM_VF_READY, &vcc->flags);
  152. if (vcc->dev) {
  153. if (vcc->dev->ops->close)
  154. vcc->dev->ops->close(vcc);
  155. if (vcc->push)
  156. vcc->push(vcc, NULL); /* atmarpd has no push */
  157. module_put(vcc->owner);
  158. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  159. atm_return(vcc, skb->truesize);
  160. kfree_skb(skb);
  161. }
  162. module_put(vcc->dev->ops->owner);
  163. atm_dev_put(vcc->dev);
  164. }
  165. vcc_remove_socket(sk);
  166. }
  167. int vcc_release(struct socket *sock)
  168. {
  169. struct sock *sk = sock->sk;
  170. if (sk) {
  171. lock_sock(sk);
  172. vcc_destroy_socket(sock->sk);
  173. release_sock(sk);
  174. sock_put(sk);
  175. }
  176. return 0;
  177. }
  178. void vcc_release_async(struct atm_vcc *vcc, int reply)
  179. {
  180. struct sock *sk = sk_atm(vcc);
  181. set_bit(ATM_VF_CLOSE, &vcc->flags);
  182. sk->sk_shutdown |= RCV_SHUTDOWN;
  183. sk->sk_err = -reply;
  184. clear_bit(ATM_VF_WAITING, &vcc->flags);
  185. sk->sk_state_change(sk);
  186. }
  187. EXPORT_SYMBOL(vcc_release_async);
  188. void vcc_process_recv_queue(struct atm_vcc *vcc)
  189. {
  190. struct sk_buff_head queue, *rq;
  191. struct sk_buff *skb, *tmp;
  192. unsigned long flags;
  193. __skb_queue_head_init(&queue);
  194. rq = &sk_atm(vcc)->sk_receive_queue;
  195. spin_lock_irqsave(&rq->lock, flags);
  196. skb_queue_splice_init(rq, &queue);
  197. spin_unlock_irqrestore(&rq->lock, flags);
  198. skb_queue_walk_safe(&queue, skb, tmp) {
  199. __skb_unlink(skb, &queue);
  200. vcc->push(vcc, skb);
  201. }
  202. }
  203. EXPORT_SYMBOL(vcc_process_recv_queue);
  204. void atm_dev_signal_change(struct atm_dev *dev, char signal)
  205. {
  206. pr_debug("%s signal=%d dev=%p number=%d dev->signal=%d\n",
  207. __func__, signal, dev, dev->number, dev->signal);
  208. /* atm driver sending invalid signal */
  209. WARN_ON(signal < ATM_PHY_SIG_LOST || signal > ATM_PHY_SIG_FOUND);
  210. if (dev->signal == signal)
  211. return; /* no change */
  212. dev->signal = signal;
  213. atomic_notifier_call_chain(&atm_dev_notify_chain, signal, dev);
  214. }
  215. EXPORT_SYMBOL(atm_dev_signal_change);
  216. void atm_dev_release_vccs(struct atm_dev *dev)
  217. {
  218. int i;
  219. write_lock_irq(&vcc_sklist_lock);
  220. for (i = 0; i < VCC_HTABLE_SIZE; i++) {
  221. struct hlist_head *head = &vcc_hash[i];
  222. struct hlist_node *tmp;
  223. struct sock *s;
  224. struct atm_vcc *vcc;
  225. sk_for_each_safe(s, tmp, head) {
  226. vcc = atm_sk(s);
  227. if (vcc->dev == dev) {
  228. vcc_release_async(vcc, -EPIPE);
  229. sk_del_node_init(s);
  230. }
  231. }
  232. }
  233. write_unlock_irq(&vcc_sklist_lock);
  234. }
  235. EXPORT_SYMBOL(atm_dev_release_vccs);
  236. static int adjust_tp(struct atm_trafprm *tp, unsigned char aal)
  237. {
  238. int max_sdu;
  239. if (!tp->traffic_class)
  240. return 0;
  241. switch (aal) {
  242. case ATM_AAL0:
  243. max_sdu = ATM_CELL_SIZE-1;
  244. break;
  245. case ATM_AAL34:
  246. max_sdu = ATM_MAX_AAL34_PDU;
  247. break;
  248. default:
  249. pr_warn("AAL problems ... (%d)\n", aal);
  250. /* fall through */
  251. case ATM_AAL5:
  252. max_sdu = ATM_MAX_AAL5_PDU;
  253. }
  254. if (!tp->max_sdu)
  255. tp->max_sdu = max_sdu;
  256. else if (tp->max_sdu > max_sdu)
  257. return -EINVAL;
  258. if (!tp->max_cdv)
  259. tp->max_cdv = ATM_MAX_CDV;
  260. return 0;
  261. }
  262. static int check_ci(const struct atm_vcc *vcc, short vpi, int vci)
  263. {
  264. struct hlist_head *head = &vcc_hash[vci & (VCC_HTABLE_SIZE - 1)];
  265. struct sock *s;
  266. struct atm_vcc *walk;
  267. sk_for_each(s, head) {
  268. walk = atm_sk(s);
  269. if (walk->dev != vcc->dev)
  270. continue;
  271. if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
  272. walk->vci == vci && ((walk->qos.txtp.traffic_class !=
  273. ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
  274. (walk->qos.rxtp.traffic_class != ATM_NONE &&
  275. vcc->qos.rxtp.traffic_class != ATM_NONE)))
  276. return -EADDRINUSE;
  277. }
  278. /* allow VCCs with same VPI/VCI iff they don't collide on
  279. TX/RX (but we may refuse such sharing for other reasons,
  280. e.g. if protocol requires to have both channels) */
  281. return 0;
  282. }
  283. static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci)
  284. {
  285. static short p; /* poor man's per-device cache */
  286. static int c;
  287. short old_p;
  288. int old_c;
  289. int err;
  290. if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
  291. err = check_ci(vcc, *vpi, *vci);
  292. return err;
  293. }
  294. /* last scan may have left values out of bounds for current device */
  295. if (*vpi != ATM_VPI_ANY)
  296. p = *vpi;
  297. else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
  298. p = 0;
  299. if (*vci != ATM_VCI_ANY)
  300. c = *vci;
  301. else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
  302. c = ATM_NOT_RSV_VCI;
  303. old_p = p;
  304. old_c = c;
  305. do {
  306. if (!check_ci(vcc, p, c)) {
  307. *vpi = p;
  308. *vci = c;
  309. return 0;
  310. }
  311. if (*vci == ATM_VCI_ANY) {
  312. c++;
  313. if (c >= 1 << vcc->dev->ci_range.vci_bits)
  314. c = ATM_NOT_RSV_VCI;
  315. }
  316. if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
  317. *vpi == ATM_VPI_ANY) {
  318. p++;
  319. if (p >= 1 << vcc->dev->ci_range.vpi_bits)
  320. p = 0;
  321. }
  322. } while (old_p != p || old_c != c);
  323. return -EADDRINUSE;
  324. }
  325. static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
  326. int vci)
  327. {
  328. struct sock *sk = sk_atm(vcc);
  329. int error;
  330. if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
  331. vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
  332. vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
  333. return -EINVAL;
  334. if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
  335. return -EPERM;
  336. error = -ENODEV;
  337. if (!try_module_get(dev->ops->owner))
  338. return error;
  339. vcc->dev = dev;
  340. write_lock_irq(&vcc_sklist_lock);
  341. if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
  342. (error = find_ci(vcc, &vpi, &vci))) {
  343. write_unlock_irq(&vcc_sklist_lock);
  344. goto fail_module_put;
  345. }
  346. vcc->vpi = vpi;
  347. vcc->vci = vci;
  348. __vcc_insert_socket(sk);
  349. write_unlock_irq(&vcc_sklist_lock);
  350. switch (vcc->qos.aal) {
  351. case ATM_AAL0:
  352. error = atm_init_aal0(vcc);
  353. vcc->stats = &dev->stats.aal0;
  354. break;
  355. case ATM_AAL34:
  356. error = atm_init_aal34(vcc);
  357. vcc->stats = &dev->stats.aal34;
  358. break;
  359. case ATM_NO_AAL:
  360. /* ATM_AAL5 is also used in the "0 for default" case */
  361. vcc->qos.aal = ATM_AAL5;
  362. /* fall through */
  363. case ATM_AAL5:
  364. error = atm_init_aal5(vcc);
  365. vcc->stats = &dev->stats.aal5;
  366. break;
  367. default:
  368. error = -EPROTOTYPE;
  369. }
  370. if (!error)
  371. error = adjust_tp(&vcc->qos.txtp, vcc->qos.aal);
  372. if (!error)
  373. error = adjust_tp(&vcc->qos.rxtp, vcc->qos.aal);
  374. if (error)
  375. goto fail;
  376. pr_debug("VCC %d.%d, AAL %d\n", vpi, vci, vcc->qos.aal);
  377. pr_debug(" TX: %d, PCR %d..%d, SDU %d\n",
  378. vcc->qos.txtp.traffic_class,
  379. vcc->qos.txtp.min_pcr,
  380. vcc->qos.txtp.max_pcr,
  381. vcc->qos.txtp.max_sdu);
  382. pr_debug(" RX: %d, PCR %d..%d, SDU %d\n",
  383. vcc->qos.rxtp.traffic_class,
  384. vcc->qos.rxtp.min_pcr,
  385. vcc->qos.rxtp.max_pcr,
  386. vcc->qos.rxtp.max_sdu);
  387. if (dev->ops->open) {
  388. error = dev->ops->open(vcc);
  389. if (error)
  390. goto fail;
  391. }
  392. return 0;
  393. fail:
  394. vcc_remove_socket(sk);
  395. fail_module_put:
  396. module_put(dev->ops->owner);
  397. /* ensure we get dev module ref count correct */
  398. vcc->dev = NULL;
  399. return error;
  400. }
  401. int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
  402. {
  403. struct atm_dev *dev;
  404. struct atm_vcc *vcc = ATM_SD(sock);
  405. int error;
  406. pr_debug("(vpi %d, vci %d)\n", vpi, vci);
  407. if (sock->state == SS_CONNECTED)
  408. return -EISCONN;
  409. if (sock->state != SS_UNCONNECTED)
  410. return -EINVAL;
  411. if (!(vpi || vci))
  412. return -EINVAL;
  413. if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
  414. clear_bit(ATM_VF_PARTIAL, &vcc->flags);
  415. else
  416. if (test_bit(ATM_VF_PARTIAL, &vcc->flags))
  417. return -EINVAL;
  418. pr_debug("(TX: cl %d,bw %d-%d,sdu %d; "
  419. "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
  420. vcc->qos.txtp.traffic_class, vcc->qos.txtp.min_pcr,
  421. vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_sdu,
  422. vcc->qos.rxtp.traffic_class, vcc->qos.rxtp.min_pcr,
  423. vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_sdu,
  424. vcc->qos.aal == ATM_AAL5 ? "" :
  425. vcc->qos.aal == ATM_AAL0 ? "" : " ??? code ",
  426. vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
  427. if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
  428. return -EBADFD;
  429. if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
  430. vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
  431. return -EINVAL;
  432. if (likely(itf != ATM_ITF_ANY)) {
  433. dev = try_then_request_module(atm_dev_lookup(itf),
  434. "atm-device-%d", itf);
  435. } else {
  436. dev = NULL;
  437. mutex_lock(&atm_dev_mutex);
  438. if (!list_empty(&atm_devs)) {
  439. dev = list_entry(atm_devs.next,
  440. struct atm_dev, dev_list);
  441. atm_dev_hold(dev);
  442. }
  443. mutex_unlock(&atm_dev_mutex);
  444. }
  445. if (!dev)
  446. return -ENODEV;
  447. error = __vcc_connect(vcc, dev, vpi, vci);
  448. if (error) {
  449. atm_dev_put(dev);
  450. return error;
  451. }
  452. if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
  453. set_bit(ATM_VF_PARTIAL, &vcc->flags);
  454. if (test_bit(ATM_VF_READY, &ATM_SD(sock)->flags))
  455. sock->state = SS_CONNECTED;
  456. return 0;
  457. }
  458. int vcc_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  459. int flags)
  460. {
  461. struct sock *sk = sock->sk;
  462. struct atm_vcc *vcc;
  463. struct sk_buff *skb;
  464. int copied, error = -EINVAL;
  465. if (sock->state != SS_CONNECTED)
  466. return -ENOTCONN;
  467. /* only handle MSG_DONTWAIT and MSG_PEEK */
  468. if (flags & ~(MSG_DONTWAIT | MSG_PEEK))
  469. return -EOPNOTSUPP;
  470. vcc = ATM_SD(sock);
  471. if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
  472. test_bit(ATM_VF_CLOSE, &vcc->flags) ||
  473. !test_bit(ATM_VF_READY, &vcc->flags))
  474. return 0;
  475. skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
  476. if (!skb)
  477. return error;
  478. copied = skb->len;
  479. if (copied > size) {
  480. copied = size;
  481. msg->msg_flags |= MSG_TRUNC;
  482. }
  483. error = skb_copy_datagram_msg(skb, 0, msg, copied);
  484. if (error)
  485. return error;
  486. sock_recv_ts_and_drops(msg, sk, skb);
  487. if (!(flags & MSG_PEEK)) {
  488. pr_debug("%d -= %d\n", atomic_read(&sk->sk_rmem_alloc),
  489. skb->truesize);
  490. atm_return(vcc, skb->truesize);
  491. }
  492. skb_free_datagram(sk, skb);
  493. return copied;
  494. }
  495. int vcc_sendmsg(struct socket *sock, struct msghdr *m, size_t size)
  496. {
  497. struct sock *sk = sock->sk;
  498. DEFINE_WAIT(wait);
  499. struct atm_vcc *vcc;
  500. struct sk_buff *skb;
  501. int eff, error;
  502. lock_sock(sk);
  503. if (sock->state != SS_CONNECTED) {
  504. error = -ENOTCONN;
  505. goto out;
  506. }
  507. if (m->msg_name) {
  508. error = -EISCONN;
  509. goto out;
  510. }
  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 > 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_iter(skb_put(skb, size), size, &m->msg_iter) != 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);