caif_socket.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * Copyright (C) ST-Ericsson AB 2010
  3. * Author: Sjur Brendeland sjur.brandeland@stericsson.com
  4. * License terms: GNU General Public License (GPL) version 2
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
  7. #include <linux/fs.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/mutex.h>
  13. #include <linux/list.h>
  14. #include <linux/wait.h>
  15. #include <linux/poll.h>
  16. #include <linux/tcp.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/caif/caif_socket.h>
  20. #include <linux/atomic.h>
  21. #include <net/sock.h>
  22. #include <net/tcp_states.h>
  23. #include <net/caif/caif_layer.h>
  24. #include <net/caif/caif_dev.h>
  25. #include <net/caif/cfpkt.h>
  26. MODULE_LICENSE("GPL");
  27. MODULE_ALIAS_NETPROTO(AF_CAIF);
  28. /*
  29. * CAIF state is re-using the TCP socket states.
  30. * caif_states stored in sk_state reflect the state as reported by
  31. * the CAIF stack, while sk_socket->state is the state of the socket.
  32. */
  33. enum caif_states {
  34. CAIF_CONNECTED = TCP_ESTABLISHED,
  35. CAIF_CONNECTING = TCP_SYN_SENT,
  36. CAIF_DISCONNECTED = TCP_CLOSE
  37. };
  38. #define TX_FLOW_ON_BIT 1
  39. #define RX_FLOW_ON_BIT 2
  40. struct caifsock {
  41. struct sock sk; /* must be first member */
  42. struct cflayer layer;
  43. u32 flow_state;
  44. struct caif_connect_request conn_req;
  45. struct mutex readlock;
  46. struct dentry *debugfs_socket_dir;
  47. int headroom, tailroom, maxframe;
  48. };
  49. static int rx_flow_is_on(struct caifsock *cf_sk)
  50. {
  51. return test_bit(RX_FLOW_ON_BIT,
  52. (void *) &cf_sk->flow_state);
  53. }
  54. static int tx_flow_is_on(struct caifsock *cf_sk)
  55. {
  56. return test_bit(TX_FLOW_ON_BIT,
  57. (void *) &cf_sk->flow_state);
  58. }
  59. static void set_rx_flow_off(struct caifsock *cf_sk)
  60. {
  61. clear_bit(RX_FLOW_ON_BIT,
  62. (void *) &cf_sk->flow_state);
  63. }
  64. static void set_rx_flow_on(struct caifsock *cf_sk)
  65. {
  66. set_bit(RX_FLOW_ON_BIT,
  67. (void *) &cf_sk->flow_state);
  68. }
  69. static void set_tx_flow_off(struct caifsock *cf_sk)
  70. {
  71. clear_bit(TX_FLOW_ON_BIT,
  72. (void *) &cf_sk->flow_state);
  73. }
  74. static void set_tx_flow_on(struct caifsock *cf_sk)
  75. {
  76. set_bit(TX_FLOW_ON_BIT,
  77. (void *) &cf_sk->flow_state);
  78. }
  79. static void caif_read_lock(struct sock *sk)
  80. {
  81. struct caifsock *cf_sk;
  82. cf_sk = container_of(sk, struct caifsock, sk);
  83. mutex_lock(&cf_sk->readlock);
  84. }
  85. static void caif_read_unlock(struct sock *sk)
  86. {
  87. struct caifsock *cf_sk;
  88. cf_sk = container_of(sk, struct caifsock, sk);
  89. mutex_unlock(&cf_sk->readlock);
  90. }
  91. static int sk_rcvbuf_lowwater(struct caifsock *cf_sk)
  92. {
  93. /* A quarter of full buffer is used a low water mark */
  94. return cf_sk->sk.sk_rcvbuf / 4;
  95. }
  96. static void caif_flow_ctrl(struct sock *sk, int mode)
  97. {
  98. struct caifsock *cf_sk;
  99. cf_sk = container_of(sk, struct caifsock, sk);
  100. if (cf_sk->layer.dn && cf_sk->layer.dn->modemcmd)
  101. cf_sk->layer.dn->modemcmd(cf_sk->layer.dn, mode);
  102. }
  103. /*
  104. * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
  105. * not dropped, but CAIF is sending flow off instead.
  106. */
  107. static int caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  108. {
  109. int err;
  110. int skb_len;
  111. unsigned long flags;
  112. struct sk_buff_head *list = &sk->sk_receive_queue;
  113. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  114. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  115. (unsigned int)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) {
  116. net_dbg_ratelimited("sending flow OFF (queue len = %d %d)\n",
  117. atomic_read(&cf_sk->sk.sk_rmem_alloc),
  118. sk_rcvbuf_lowwater(cf_sk));
  119. set_rx_flow_off(cf_sk);
  120. caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
  121. }
  122. err = sk_filter(sk, skb);
  123. if (err)
  124. return err;
  125. if (!sk_rmem_schedule(sk, skb->truesize) && rx_flow_is_on(cf_sk)) {
  126. set_rx_flow_off(cf_sk);
  127. net_dbg_ratelimited("sending flow OFF due to rmem_schedule\n");
  128. caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
  129. }
  130. skb->dev = NULL;
  131. skb_set_owner_r(skb, sk);
  132. /* Cache the SKB length before we tack it onto the receive
  133. * queue. Once it is added it no longer belongs to us and
  134. * may be freed by other threads of control pulling packets
  135. * from the queue.
  136. */
  137. skb_len = skb->len;
  138. spin_lock_irqsave(&list->lock, flags);
  139. if (!sock_flag(sk, SOCK_DEAD))
  140. __skb_queue_tail(list, skb);
  141. spin_unlock_irqrestore(&list->lock, flags);
  142. if (!sock_flag(sk, SOCK_DEAD))
  143. sk->sk_data_ready(sk, skb_len);
  144. else
  145. kfree_skb(skb);
  146. return 0;
  147. }
  148. /* Packet Receive Callback function called from CAIF Stack */
  149. static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt)
  150. {
  151. struct caifsock *cf_sk;
  152. struct sk_buff *skb;
  153. cf_sk = container_of(layr, struct caifsock, layer);
  154. skb = cfpkt_tonative(pkt);
  155. if (unlikely(cf_sk->sk.sk_state != CAIF_CONNECTED)) {
  156. kfree_skb(skb);
  157. return 0;
  158. }
  159. caif_queue_rcv_skb(&cf_sk->sk, skb);
  160. return 0;
  161. }
  162. static void cfsk_hold(struct cflayer *layr)
  163. {
  164. struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
  165. sock_hold(&cf_sk->sk);
  166. }
  167. static void cfsk_put(struct cflayer *layr)
  168. {
  169. struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
  170. sock_put(&cf_sk->sk);
  171. }
  172. /* Packet Control Callback function called from CAIF */
  173. static void caif_ctrl_cb(struct cflayer *layr,
  174. enum caif_ctrlcmd flow,
  175. int phyid)
  176. {
  177. struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
  178. switch (flow) {
  179. case CAIF_CTRLCMD_FLOW_ON_IND:
  180. /* OK from modem to start sending again */
  181. set_tx_flow_on(cf_sk);
  182. cf_sk->sk.sk_state_change(&cf_sk->sk);
  183. break;
  184. case CAIF_CTRLCMD_FLOW_OFF_IND:
  185. /* Modem asks us to shut up */
  186. set_tx_flow_off(cf_sk);
  187. cf_sk->sk.sk_state_change(&cf_sk->sk);
  188. break;
  189. case CAIF_CTRLCMD_INIT_RSP:
  190. /* We're now connected */
  191. caif_client_register_refcnt(&cf_sk->layer,
  192. cfsk_hold, cfsk_put);
  193. cf_sk->sk.sk_state = CAIF_CONNECTED;
  194. set_tx_flow_on(cf_sk);
  195. cf_sk->sk.sk_state_change(&cf_sk->sk);
  196. break;
  197. case CAIF_CTRLCMD_DEINIT_RSP:
  198. /* We're now disconnected */
  199. cf_sk->sk.sk_state = CAIF_DISCONNECTED;
  200. cf_sk->sk.sk_state_change(&cf_sk->sk);
  201. break;
  202. case CAIF_CTRLCMD_INIT_FAIL_RSP:
  203. /* Connect request failed */
  204. cf_sk->sk.sk_err = ECONNREFUSED;
  205. cf_sk->sk.sk_state = CAIF_DISCONNECTED;
  206. cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
  207. /*
  208. * Socket "standards" seems to require POLLOUT to
  209. * be set at connect failure.
  210. */
  211. set_tx_flow_on(cf_sk);
  212. cf_sk->sk.sk_state_change(&cf_sk->sk);
  213. break;
  214. case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
  215. /* Modem has closed this connection, or device is down. */
  216. cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
  217. cf_sk->sk.sk_err = ECONNRESET;
  218. set_rx_flow_on(cf_sk);
  219. cf_sk->sk.sk_error_report(&cf_sk->sk);
  220. break;
  221. default:
  222. pr_debug("Unexpected flow command %d\n", flow);
  223. }
  224. }
  225. static void caif_check_flow_release(struct sock *sk)
  226. {
  227. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  228. if (rx_flow_is_on(cf_sk))
  229. return;
  230. if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) {
  231. set_rx_flow_on(cf_sk);
  232. caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_ON_REQ);
  233. }
  234. }
  235. /*
  236. * Copied from unix_dgram_recvmsg, but removed credit checks,
  237. * changed locking, address handling and added MSG_TRUNC.
  238. */
  239. static int caif_seqpkt_recvmsg(struct kiocb *iocb, struct socket *sock,
  240. struct msghdr *m, size_t len, int flags)
  241. {
  242. struct sock *sk = sock->sk;
  243. struct sk_buff *skb;
  244. int ret;
  245. int copylen;
  246. ret = -EOPNOTSUPP;
  247. if (m->msg_flags&MSG_OOB)
  248. goto read_error;
  249. skb = skb_recv_datagram(sk, flags, 0 , &ret);
  250. if (!skb)
  251. goto read_error;
  252. copylen = skb->len;
  253. if (len < copylen) {
  254. m->msg_flags |= MSG_TRUNC;
  255. copylen = len;
  256. }
  257. ret = skb_copy_datagram_iovec(skb, 0, m->msg_iov, copylen);
  258. if (ret)
  259. goto out_free;
  260. ret = (flags & MSG_TRUNC) ? skb->len : copylen;
  261. out_free:
  262. skb_free_datagram(sk, skb);
  263. caif_check_flow_release(sk);
  264. return ret;
  265. read_error:
  266. return ret;
  267. }
  268. /* Copied from unix_stream_wait_data, identical except for lock call. */
  269. static long caif_stream_data_wait(struct sock *sk, long timeo)
  270. {
  271. DEFINE_WAIT(wait);
  272. lock_sock(sk);
  273. for (;;) {
  274. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  275. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  276. sk->sk_err ||
  277. sk->sk_state != CAIF_CONNECTED ||
  278. sock_flag(sk, SOCK_DEAD) ||
  279. (sk->sk_shutdown & RCV_SHUTDOWN) ||
  280. signal_pending(current) ||
  281. !timeo)
  282. break;
  283. set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  284. release_sock(sk);
  285. timeo = schedule_timeout(timeo);
  286. lock_sock(sk);
  287. if (sock_flag(sk, SOCK_DEAD))
  288. break;
  289. clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  290. }
  291. finish_wait(sk_sleep(sk), &wait);
  292. release_sock(sk);
  293. return timeo;
  294. }
  295. /*
  296. * Copied from unix_stream_recvmsg, but removed credit checks,
  297. * changed locking calls, changed address handling.
  298. */
  299. static int caif_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
  300. struct msghdr *msg, size_t size,
  301. int flags)
  302. {
  303. struct sock *sk = sock->sk;
  304. int copied = 0;
  305. int target;
  306. int err = 0;
  307. long timeo;
  308. err = -EOPNOTSUPP;
  309. if (flags&MSG_OOB)
  310. goto out;
  311. /*
  312. * Lock the socket to prevent queue disordering
  313. * while sleeps in memcpy_tomsg
  314. */
  315. err = -EAGAIN;
  316. if (sk->sk_state == CAIF_CONNECTING)
  317. goto out;
  318. caif_read_lock(sk);
  319. target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
  320. timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
  321. do {
  322. int chunk;
  323. struct sk_buff *skb;
  324. lock_sock(sk);
  325. if (sock_flag(sk, SOCK_DEAD)) {
  326. err = -ECONNRESET;
  327. goto unlock;
  328. }
  329. skb = skb_dequeue(&sk->sk_receive_queue);
  330. caif_check_flow_release(sk);
  331. if (skb == NULL) {
  332. if (copied >= target)
  333. goto unlock;
  334. /*
  335. * POSIX 1003.1g mandates this order.
  336. */
  337. err = sock_error(sk);
  338. if (err)
  339. goto unlock;
  340. err = -ECONNRESET;
  341. if (sk->sk_shutdown & RCV_SHUTDOWN)
  342. goto unlock;
  343. err = -EPIPE;
  344. if (sk->sk_state != CAIF_CONNECTED)
  345. goto unlock;
  346. if (sock_flag(sk, SOCK_DEAD))
  347. goto unlock;
  348. release_sock(sk);
  349. err = -EAGAIN;
  350. if (!timeo)
  351. break;
  352. caif_read_unlock(sk);
  353. timeo = caif_stream_data_wait(sk, timeo);
  354. if (signal_pending(current)) {
  355. err = sock_intr_errno(timeo);
  356. goto out;
  357. }
  358. caif_read_lock(sk);
  359. continue;
  360. unlock:
  361. release_sock(sk);
  362. break;
  363. }
  364. release_sock(sk);
  365. chunk = min_t(unsigned int, skb->len, size);
  366. if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
  367. skb_queue_head(&sk->sk_receive_queue, skb);
  368. if (copied == 0)
  369. copied = -EFAULT;
  370. break;
  371. }
  372. copied += chunk;
  373. size -= chunk;
  374. /* Mark read part of skb as used */
  375. if (!(flags & MSG_PEEK)) {
  376. skb_pull(skb, chunk);
  377. /* put the skb back if we didn't use it up. */
  378. if (skb->len) {
  379. skb_queue_head(&sk->sk_receive_queue, skb);
  380. break;
  381. }
  382. kfree_skb(skb);
  383. } else {
  384. /*
  385. * It is questionable, see note in unix_dgram_recvmsg.
  386. */
  387. /* put message back and return */
  388. skb_queue_head(&sk->sk_receive_queue, skb);
  389. break;
  390. }
  391. } while (size);
  392. caif_read_unlock(sk);
  393. out:
  394. return copied ? : err;
  395. }
  396. /*
  397. * Copied from sock.c:sock_wait_for_wmem, but change to wait for
  398. * CAIF flow-on and sock_writable.
  399. */
  400. static long caif_wait_for_flow_on(struct caifsock *cf_sk,
  401. int wait_writeable, long timeo, int *err)
  402. {
  403. struct sock *sk = &cf_sk->sk;
  404. DEFINE_WAIT(wait);
  405. for (;;) {
  406. *err = 0;
  407. if (tx_flow_is_on(cf_sk) &&
  408. (!wait_writeable || sock_writeable(&cf_sk->sk)))
  409. break;
  410. *err = -ETIMEDOUT;
  411. if (!timeo)
  412. break;
  413. *err = -ERESTARTSYS;
  414. if (signal_pending(current))
  415. break;
  416. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  417. *err = -ECONNRESET;
  418. if (sk->sk_shutdown & SHUTDOWN_MASK)
  419. break;
  420. *err = -sk->sk_err;
  421. if (sk->sk_err)
  422. break;
  423. *err = -EPIPE;
  424. if (cf_sk->sk.sk_state != CAIF_CONNECTED)
  425. break;
  426. timeo = schedule_timeout(timeo);
  427. }
  428. finish_wait(sk_sleep(sk), &wait);
  429. return timeo;
  430. }
  431. /*
  432. * Transmit a SKB. The device may temporarily request re-transmission
  433. * by returning EAGAIN.
  434. */
  435. static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
  436. int noblock, long timeo)
  437. {
  438. struct cfpkt *pkt;
  439. pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
  440. memset(skb->cb, 0, sizeof(struct caif_payload_info));
  441. if (cf_sk->layer.dn == NULL) {
  442. kfree_skb(skb);
  443. return -EINVAL;
  444. }
  445. return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
  446. }
  447. /* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
  448. static int caif_seqpkt_sendmsg(struct kiocb *kiocb, struct socket *sock,
  449. struct msghdr *msg, size_t len)
  450. {
  451. struct sock *sk = sock->sk;
  452. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  453. int buffer_size;
  454. int ret = 0;
  455. struct sk_buff *skb = NULL;
  456. int noblock;
  457. long timeo;
  458. caif_assert(cf_sk);
  459. ret = sock_error(sk);
  460. if (ret)
  461. goto err;
  462. ret = -EOPNOTSUPP;
  463. if (msg->msg_flags&MSG_OOB)
  464. goto err;
  465. ret = -EOPNOTSUPP;
  466. if (msg->msg_namelen)
  467. goto err;
  468. ret = -EINVAL;
  469. if (unlikely(msg->msg_iov->iov_base == NULL))
  470. goto err;
  471. noblock = msg->msg_flags & MSG_DONTWAIT;
  472. timeo = sock_sndtimeo(sk, noblock);
  473. timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
  474. 1, timeo, &ret);
  475. if (ret)
  476. goto err;
  477. ret = -EPIPE;
  478. if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
  479. sock_flag(sk, SOCK_DEAD) ||
  480. (sk->sk_shutdown & RCV_SHUTDOWN))
  481. goto err;
  482. /* Error if trying to write more than maximum frame size. */
  483. ret = -EMSGSIZE;
  484. if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM)
  485. goto err;
  486. buffer_size = len + cf_sk->headroom + cf_sk->tailroom;
  487. ret = -ENOMEM;
  488. skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
  489. if (!skb || skb_tailroom(skb) < buffer_size)
  490. goto err;
  491. skb_reserve(skb, cf_sk->headroom);
  492. ret = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
  493. if (ret)
  494. goto err;
  495. ret = transmit_skb(skb, cf_sk, noblock, timeo);
  496. if (ret < 0)
  497. /* skb is already freed */
  498. return ret;
  499. return len;
  500. err:
  501. kfree_skb(skb);
  502. return ret;
  503. }
  504. /*
  505. * Copied from unix_stream_sendmsg and adapted to CAIF:
  506. * Changed removed permission handling and added waiting for flow on
  507. * and other minor adaptations.
  508. */
  509. static int caif_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
  510. struct msghdr *msg, size_t len)
  511. {
  512. struct sock *sk = sock->sk;
  513. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  514. int err, size;
  515. struct sk_buff *skb;
  516. int sent = 0;
  517. long timeo;
  518. err = -EOPNOTSUPP;
  519. if (unlikely(msg->msg_flags&MSG_OOB))
  520. goto out_err;
  521. if (unlikely(msg->msg_namelen))
  522. goto out_err;
  523. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  524. timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
  525. if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
  526. goto pipe_err;
  527. while (sent < len) {
  528. size = len-sent;
  529. if (size > cf_sk->maxframe)
  530. size = cf_sk->maxframe;
  531. /* If size is more than half of sndbuf, chop up message */
  532. if (size > ((sk->sk_sndbuf >> 1) - 64))
  533. size = (sk->sk_sndbuf >> 1) - 64;
  534. if (size > SKB_MAX_ALLOC)
  535. size = SKB_MAX_ALLOC;
  536. skb = sock_alloc_send_skb(sk,
  537. size + cf_sk->headroom +
  538. cf_sk->tailroom,
  539. msg->msg_flags&MSG_DONTWAIT,
  540. &err);
  541. if (skb == NULL)
  542. goto out_err;
  543. skb_reserve(skb, cf_sk->headroom);
  544. /*
  545. * If you pass two values to the sock_alloc_send_skb
  546. * it tries to grab the large buffer with GFP_NOFS
  547. * (which can fail easily), and if it fails grab the
  548. * fallback size buffer which is under a page and will
  549. * succeed. [Alan]
  550. */
  551. size = min_t(int, size, skb_tailroom(skb));
  552. err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
  553. if (err) {
  554. kfree_skb(skb);
  555. goto out_err;
  556. }
  557. err = transmit_skb(skb, cf_sk,
  558. msg->msg_flags&MSG_DONTWAIT, timeo);
  559. if (err < 0)
  560. /* skb is already freed */
  561. goto pipe_err;
  562. sent += size;
  563. }
  564. return sent;
  565. pipe_err:
  566. if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
  567. send_sig(SIGPIPE, current, 0);
  568. err = -EPIPE;
  569. out_err:
  570. return sent ? : err;
  571. }
  572. static int setsockopt(struct socket *sock,
  573. int lvl, int opt, char __user *ov, unsigned int ol)
  574. {
  575. struct sock *sk = sock->sk;
  576. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  577. int linksel;
  578. if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
  579. return -ENOPROTOOPT;
  580. switch (opt) {
  581. case CAIFSO_LINK_SELECT:
  582. if (ol < sizeof(int))
  583. return -EINVAL;
  584. if (lvl != SOL_CAIF)
  585. goto bad_sol;
  586. if (copy_from_user(&linksel, ov, sizeof(int)))
  587. return -EINVAL;
  588. lock_sock(&(cf_sk->sk));
  589. cf_sk->conn_req.link_selector = linksel;
  590. release_sock(&cf_sk->sk);
  591. return 0;
  592. case CAIFSO_REQ_PARAM:
  593. if (lvl != SOL_CAIF)
  594. goto bad_sol;
  595. if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
  596. return -ENOPROTOOPT;
  597. lock_sock(&(cf_sk->sk));
  598. if (ol > sizeof(cf_sk->conn_req.param.data) ||
  599. copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) {
  600. release_sock(&cf_sk->sk);
  601. return -EINVAL;
  602. }
  603. cf_sk->conn_req.param.size = ol;
  604. release_sock(&cf_sk->sk);
  605. return 0;
  606. default:
  607. return -ENOPROTOOPT;
  608. }
  609. return 0;
  610. bad_sol:
  611. return -ENOPROTOOPT;
  612. }
  613. /*
  614. * caif_connect() - Connect a CAIF Socket
  615. * Copied and modified af_irda.c:irda_connect().
  616. *
  617. * Note : by consulting "errno", the user space caller may learn the cause
  618. * of the failure. Most of them are visible in the function, others may come
  619. * from subroutines called and are listed here :
  620. * o -EAFNOSUPPORT: bad socket family or type.
  621. * o -ESOCKTNOSUPPORT: bad socket type or protocol
  622. * o -EINVAL: bad socket address, or CAIF link type
  623. * o -ECONNREFUSED: remote end refused the connection.
  624. * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
  625. * o -EISCONN: already connected.
  626. * o -ETIMEDOUT: Connection timed out (send timeout)
  627. * o -ENODEV: No link layer to send request
  628. * o -ECONNRESET: Received Shutdown indication or lost link layer
  629. * o -ENOMEM: Out of memory
  630. *
  631. * State Strategy:
  632. * o sk_state: holds the CAIF_* protocol state, it's updated by
  633. * caif_ctrl_cb.
  634. * o sock->state: holds the SS_* socket state and is updated by connect and
  635. * disconnect.
  636. */
  637. static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
  638. int addr_len, int flags)
  639. {
  640. struct sock *sk = sock->sk;
  641. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  642. long timeo;
  643. int err;
  644. int ifindex, headroom, tailroom;
  645. unsigned int mtu;
  646. struct net_device *dev;
  647. lock_sock(sk);
  648. err = -EINVAL;
  649. if (addr_len < offsetofend(struct sockaddr, sa_family))
  650. goto out;
  651. err = -EAFNOSUPPORT;
  652. if (uaddr->sa_family != AF_CAIF)
  653. goto out;
  654. switch (sock->state) {
  655. case SS_UNCONNECTED:
  656. /* Normal case, a fresh connect */
  657. caif_assert(sk->sk_state == CAIF_DISCONNECTED);
  658. break;
  659. case SS_CONNECTING:
  660. switch (sk->sk_state) {
  661. case CAIF_CONNECTED:
  662. sock->state = SS_CONNECTED;
  663. err = -EISCONN;
  664. goto out;
  665. case CAIF_DISCONNECTED:
  666. /* Reconnect allowed */
  667. break;
  668. case CAIF_CONNECTING:
  669. err = -EALREADY;
  670. if (flags & O_NONBLOCK)
  671. goto out;
  672. goto wait_connect;
  673. }
  674. break;
  675. case SS_CONNECTED:
  676. caif_assert(sk->sk_state == CAIF_CONNECTED ||
  677. sk->sk_state == CAIF_DISCONNECTED);
  678. if (sk->sk_shutdown & SHUTDOWN_MASK) {
  679. /* Allow re-connect after SHUTDOWN_IND */
  680. caif_disconnect_client(sock_net(sk), &cf_sk->layer);
  681. caif_free_client(&cf_sk->layer);
  682. break;
  683. }
  684. /* No reconnect on a seqpacket socket */
  685. err = -EISCONN;
  686. goto out;
  687. case SS_DISCONNECTING:
  688. case SS_FREE:
  689. caif_assert(1); /*Should never happen */
  690. break;
  691. }
  692. sk->sk_state = CAIF_DISCONNECTED;
  693. sock->state = SS_UNCONNECTED;
  694. sk_stream_kill_queues(&cf_sk->sk);
  695. err = -EINVAL;
  696. if (addr_len != sizeof(struct sockaddr_caif))
  697. goto out;
  698. memcpy(&cf_sk->conn_req.sockaddr, uaddr,
  699. sizeof(struct sockaddr_caif));
  700. /* Move to connecting socket, start sending Connect Requests */
  701. sock->state = SS_CONNECTING;
  702. sk->sk_state = CAIF_CONNECTING;
  703. /* Check priority value comming from socket */
  704. /* if priority value is out of range it will be ajusted */
  705. if (cf_sk->sk.sk_priority > CAIF_PRIO_MAX)
  706. cf_sk->conn_req.priority = CAIF_PRIO_MAX;
  707. else if (cf_sk->sk.sk_priority < CAIF_PRIO_MIN)
  708. cf_sk->conn_req.priority = CAIF_PRIO_MIN;
  709. else
  710. cf_sk->conn_req.priority = cf_sk->sk.sk_priority;
  711. /*ifindex = id of the interface.*/
  712. cf_sk->conn_req.ifindex = cf_sk->sk.sk_bound_dev_if;
  713. cf_sk->layer.receive = caif_sktrecv_cb;
  714. err = caif_connect_client(sock_net(sk), &cf_sk->conn_req,
  715. &cf_sk->layer, &ifindex, &headroom, &tailroom);
  716. if (err < 0) {
  717. cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
  718. cf_sk->sk.sk_state = CAIF_DISCONNECTED;
  719. goto out;
  720. }
  721. err = -ENODEV;
  722. rcu_read_lock();
  723. dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
  724. if (!dev) {
  725. rcu_read_unlock();
  726. goto out;
  727. }
  728. cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
  729. mtu = dev->mtu;
  730. rcu_read_unlock();
  731. cf_sk->tailroom = tailroom;
  732. cf_sk->maxframe = mtu - (headroom + tailroom);
  733. if (cf_sk->maxframe < 1) {
  734. pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
  735. err = -ENODEV;
  736. goto out;
  737. }
  738. err = -EINPROGRESS;
  739. wait_connect:
  740. if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
  741. goto out;
  742. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  743. release_sock(sk);
  744. err = -ERESTARTSYS;
  745. timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
  746. sk->sk_state != CAIF_CONNECTING,
  747. timeo);
  748. lock_sock(sk);
  749. if (timeo < 0)
  750. goto out; /* -ERESTARTSYS */
  751. err = -ETIMEDOUT;
  752. if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
  753. goto out;
  754. if (sk->sk_state != CAIF_CONNECTED) {
  755. sock->state = SS_UNCONNECTED;
  756. err = sock_error(sk);
  757. if (!err)
  758. err = -ECONNREFUSED;
  759. goto out;
  760. }
  761. sock->state = SS_CONNECTED;
  762. err = 0;
  763. out:
  764. release_sock(sk);
  765. return err;
  766. }
  767. /*
  768. * caif_release() - Disconnect a CAIF Socket
  769. * Copied and modified af_irda.c:irda_release().
  770. */
  771. static int caif_release(struct socket *sock)
  772. {
  773. struct sock *sk = sock->sk;
  774. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  775. if (!sk)
  776. return 0;
  777. set_tx_flow_off(cf_sk);
  778. /*
  779. * Ensure that packets are not queued after this point in time.
  780. * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
  781. * this ensures no packets when sock is dead.
  782. */
  783. spin_lock_bh(&sk->sk_receive_queue.lock);
  784. sock_set_flag(sk, SOCK_DEAD);
  785. spin_unlock_bh(&sk->sk_receive_queue.lock);
  786. sock->sk = NULL;
  787. WARN_ON(IS_ERR(cf_sk->debugfs_socket_dir));
  788. if (cf_sk->debugfs_socket_dir != NULL)
  789. debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
  790. lock_sock(&(cf_sk->sk));
  791. sk->sk_state = CAIF_DISCONNECTED;
  792. sk->sk_shutdown = SHUTDOWN_MASK;
  793. caif_disconnect_client(sock_net(sk), &cf_sk->layer);
  794. cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
  795. wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP);
  796. sock_orphan(sk);
  797. sk_stream_kill_queues(&cf_sk->sk);
  798. release_sock(sk);
  799. sock_put(sk);
  800. return 0;
  801. }
  802. /* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
  803. static unsigned int caif_poll(struct file *file,
  804. struct socket *sock, poll_table *wait)
  805. {
  806. struct sock *sk = sock->sk;
  807. unsigned int mask;
  808. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  809. sock_poll_wait(file, sk_sleep(sk), wait);
  810. mask = 0;
  811. /* exceptional events? */
  812. if (sk->sk_err)
  813. mask |= POLLERR;
  814. if (sk->sk_shutdown == SHUTDOWN_MASK)
  815. mask |= POLLHUP;
  816. if (sk->sk_shutdown & RCV_SHUTDOWN)
  817. mask |= POLLRDHUP;
  818. /* readable? */
  819. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  820. (sk->sk_shutdown & RCV_SHUTDOWN))
  821. mask |= POLLIN | POLLRDNORM;
  822. /*
  823. * we set writable also when the other side has shut down the
  824. * connection. This prevents stuck sockets.
  825. */
  826. if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
  827. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  828. return mask;
  829. }
  830. static const struct proto_ops caif_seqpacket_ops = {
  831. .family = PF_CAIF,
  832. .owner = THIS_MODULE,
  833. .release = caif_release,
  834. .bind = sock_no_bind,
  835. .connect = caif_connect,
  836. .socketpair = sock_no_socketpair,
  837. .accept = sock_no_accept,
  838. .getname = sock_no_getname,
  839. .poll = caif_poll,
  840. .ioctl = sock_no_ioctl,
  841. .listen = sock_no_listen,
  842. .shutdown = sock_no_shutdown,
  843. .setsockopt = setsockopt,
  844. .getsockopt = sock_no_getsockopt,
  845. .sendmsg = caif_seqpkt_sendmsg,
  846. .recvmsg = caif_seqpkt_recvmsg,
  847. .mmap = sock_no_mmap,
  848. .sendpage = sock_no_sendpage,
  849. };
  850. static const struct proto_ops caif_stream_ops = {
  851. .family = PF_CAIF,
  852. .owner = THIS_MODULE,
  853. .release = caif_release,
  854. .bind = sock_no_bind,
  855. .connect = caif_connect,
  856. .socketpair = sock_no_socketpair,
  857. .accept = sock_no_accept,
  858. .getname = sock_no_getname,
  859. .poll = caif_poll,
  860. .ioctl = sock_no_ioctl,
  861. .listen = sock_no_listen,
  862. .shutdown = sock_no_shutdown,
  863. .setsockopt = setsockopt,
  864. .getsockopt = sock_no_getsockopt,
  865. .sendmsg = caif_stream_sendmsg,
  866. .recvmsg = caif_stream_recvmsg,
  867. .mmap = sock_no_mmap,
  868. .sendpage = sock_no_sendpage,
  869. };
  870. /* This function is called when a socket is finally destroyed. */
  871. static void caif_sock_destructor(struct sock *sk)
  872. {
  873. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  874. caif_assert(!atomic_read(&sk->sk_wmem_alloc));
  875. caif_assert(sk_unhashed(sk));
  876. caif_assert(!sk->sk_socket);
  877. if (!sock_flag(sk, SOCK_DEAD)) {
  878. WARN(1, "Attempt to release alive CAIF socket: %p\n", sk);
  879. return;
  880. }
  881. sk_stream_kill_queues(&cf_sk->sk);
  882. caif_free_client(&cf_sk->layer);
  883. }
  884. static int caif_create(struct net *net, struct socket *sock, int protocol,
  885. int kern)
  886. {
  887. struct sock *sk = NULL;
  888. struct caifsock *cf_sk = NULL;
  889. static struct proto prot = {.name = "PF_CAIF",
  890. .owner = THIS_MODULE,
  891. .obj_size = sizeof(struct caifsock),
  892. };
  893. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
  894. return -EPERM;
  895. /*
  896. * The sock->type specifies the socket type to use.
  897. * The CAIF socket is a packet stream in the sense
  898. * that it is packet based. CAIF trusts the reliability
  899. * of the link, no resending is implemented.
  900. */
  901. if (sock->type == SOCK_SEQPACKET)
  902. sock->ops = &caif_seqpacket_ops;
  903. else if (sock->type == SOCK_STREAM)
  904. sock->ops = &caif_stream_ops;
  905. else
  906. return -ESOCKTNOSUPPORT;
  907. if (protocol < 0 || protocol >= CAIFPROTO_MAX)
  908. return -EPROTONOSUPPORT;
  909. /*
  910. * Set the socket state to unconnected. The socket state
  911. * is really not used at all in the net/core or socket.c but the
  912. * initialization makes sure that sock->state is not uninitialized.
  913. */
  914. sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot);
  915. if (!sk)
  916. return -ENOMEM;
  917. cf_sk = container_of(sk, struct caifsock, sk);
  918. /* Store the protocol */
  919. sk->sk_protocol = (unsigned char) protocol;
  920. /*
  921. * Lock in order to try to stop someone from opening the socket
  922. * too early.
  923. */
  924. lock_sock(&(cf_sk->sk));
  925. /* Initialize the nozero default sock structure data. */
  926. sock_init_data(sock, sk);
  927. sk->sk_destruct = caif_sock_destructor;
  928. mutex_init(&cf_sk->readlock); /* single task reading lock */
  929. cf_sk->layer.ctrlcmd = caif_ctrl_cb;
  930. cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
  931. cf_sk->sk.sk_state = CAIF_DISCONNECTED;
  932. set_tx_flow_off(cf_sk);
  933. set_rx_flow_on(cf_sk);
  934. /* Set default options on configuration */
  935. cf_sk->sk.sk_priority = CAIF_PRIO_NORMAL;
  936. cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
  937. cf_sk->conn_req.protocol = protocol;
  938. release_sock(&cf_sk->sk);
  939. return 0;
  940. }
  941. static struct net_proto_family caif_family_ops = {
  942. .family = PF_CAIF,
  943. .create = caif_create,
  944. .owner = THIS_MODULE,
  945. };
  946. static int __init caif_sktinit_module(void)
  947. {
  948. int err = sock_register(&caif_family_ops);
  949. if (!err)
  950. return err;
  951. return 0;
  952. }
  953. static void __exit caif_sktexit_module(void)
  954. {
  955. sock_unregister(PF_CAIF);
  956. }
  957. module_init(caif_sktinit_module);
  958. module_exit(caif_sktexit_module);