caif_socket.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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. m->msg_namelen = 0;
  250. skb = skb_recv_datagram(sk, flags, 0 , &ret);
  251. if (!skb)
  252. goto read_error;
  253. copylen = skb->len;
  254. if (len < copylen) {
  255. m->msg_flags |= MSG_TRUNC;
  256. copylen = len;
  257. }
  258. ret = skb_copy_datagram_iovec(skb, 0, m->msg_iov, copylen);
  259. if (ret)
  260. goto out_free;
  261. ret = (flags & MSG_TRUNC) ? skb->len : copylen;
  262. out_free:
  263. skb_free_datagram(sk, skb);
  264. caif_check_flow_release(sk);
  265. return ret;
  266. read_error:
  267. return ret;
  268. }
  269. /* Copied from unix_stream_wait_data, identical except for lock call. */
  270. static long caif_stream_data_wait(struct sock *sk, long timeo)
  271. {
  272. DEFINE_WAIT(wait);
  273. lock_sock(sk);
  274. for (;;) {
  275. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  276. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  277. sk->sk_err ||
  278. sk->sk_state != CAIF_CONNECTED ||
  279. sock_flag(sk, SOCK_DEAD) ||
  280. (sk->sk_shutdown & RCV_SHUTDOWN) ||
  281. signal_pending(current) ||
  282. !timeo)
  283. break;
  284. set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  285. release_sock(sk);
  286. timeo = schedule_timeout(timeo);
  287. lock_sock(sk);
  288. if (sock_flag(sk, SOCK_DEAD))
  289. break;
  290. clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  291. }
  292. finish_wait(sk_sleep(sk), &wait);
  293. release_sock(sk);
  294. return timeo;
  295. }
  296. /*
  297. * Copied from unix_stream_recvmsg, but removed credit checks,
  298. * changed locking calls, changed address handling.
  299. */
  300. static int caif_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
  301. struct msghdr *msg, size_t size,
  302. int flags)
  303. {
  304. struct sock *sk = sock->sk;
  305. int copied = 0;
  306. int target;
  307. int err = 0;
  308. long timeo;
  309. err = -EOPNOTSUPP;
  310. if (flags&MSG_OOB)
  311. goto out;
  312. /*
  313. * Lock the socket to prevent queue disordering
  314. * while sleeps in memcpy_tomsg
  315. */
  316. err = -EAGAIN;
  317. if (sk->sk_state == CAIF_CONNECTING)
  318. goto out;
  319. caif_read_lock(sk);
  320. target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
  321. timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
  322. do {
  323. int chunk;
  324. struct sk_buff *skb;
  325. lock_sock(sk);
  326. if (sock_flag(sk, SOCK_DEAD)) {
  327. err = -ECONNRESET;
  328. goto unlock;
  329. }
  330. skb = skb_dequeue(&sk->sk_receive_queue);
  331. caif_check_flow_release(sk);
  332. if (skb == NULL) {
  333. if (copied >= target)
  334. goto unlock;
  335. /*
  336. * POSIX 1003.1g mandates this order.
  337. */
  338. err = sock_error(sk);
  339. if (err)
  340. goto unlock;
  341. err = -ECONNRESET;
  342. if (sk->sk_shutdown & RCV_SHUTDOWN)
  343. goto unlock;
  344. err = -EPIPE;
  345. if (sk->sk_state != CAIF_CONNECTED)
  346. goto unlock;
  347. if (sock_flag(sk, SOCK_DEAD))
  348. goto unlock;
  349. release_sock(sk);
  350. err = -EAGAIN;
  351. if (!timeo)
  352. break;
  353. caif_read_unlock(sk);
  354. timeo = caif_stream_data_wait(sk, timeo);
  355. if (signal_pending(current)) {
  356. err = sock_intr_errno(timeo);
  357. goto out;
  358. }
  359. caif_read_lock(sk);
  360. continue;
  361. unlock:
  362. release_sock(sk);
  363. break;
  364. }
  365. release_sock(sk);
  366. chunk = min_t(unsigned int, skb->len, size);
  367. if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
  368. skb_queue_head(&sk->sk_receive_queue, skb);
  369. if (copied == 0)
  370. copied = -EFAULT;
  371. break;
  372. }
  373. copied += chunk;
  374. size -= chunk;
  375. /* Mark read part of skb as used */
  376. if (!(flags & MSG_PEEK)) {
  377. skb_pull(skb, chunk);
  378. /* put the skb back if we didn't use it up. */
  379. if (skb->len) {
  380. skb_queue_head(&sk->sk_receive_queue, skb);
  381. break;
  382. }
  383. kfree_skb(skb);
  384. } else {
  385. /*
  386. * It is questionable, see note in unix_dgram_recvmsg.
  387. */
  388. /* put message back and return */
  389. skb_queue_head(&sk->sk_receive_queue, skb);
  390. break;
  391. }
  392. } while (size);
  393. caif_read_unlock(sk);
  394. out:
  395. return copied ? : err;
  396. }
  397. /*
  398. * Copied from sock.c:sock_wait_for_wmem, but change to wait for
  399. * CAIF flow-on and sock_writable.
  400. */
  401. static long caif_wait_for_flow_on(struct caifsock *cf_sk,
  402. int wait_writeable, long timeo, int *err)
  403. {
  404. struct sock *sk = &cf_sk->sk;
  405. DEFINE_WAIT(wait);
  406. for (;;) {
  407. *err = 0;
  408. if (tx_flow_is_on(cf_sk) &&
  409. (!wait_writeable || sock_writeable(&cf_sk->sk)))
  410. break;
  411. *err = -ETIMEDOUT;
  412. if (!timeo)
  413. break;
  414. *err = -ERESTARTSYS;
  415. if (signal_pending(current))
  416. break;
  417. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  418. *err = -ECONNRESET;
  419. if (sk->sk_shutdown & SHUTDOWN_MASK)
  420. break;
  421. *err = -sk->sk_err;
  422. if (sk->sk_err)
  423. break;
  424. *err = -EPIPE;
  425. if (cf_sk->sk.sk_state != CAIF_CONNECTED)
  426. break;
  427. timeo = schedule_timeout(timeo);
  428. }
  429. finish_wait(sk_sleep(sk), &wait);
  430. return timeo;
  431. }
  432. /*
  433. * Transmit a SKB. The device may temporarily request re-transmission
  434. * by returning EAGAIN.
  435. */
  436. static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
  437. int noblock, long timeo)
  438. {
  439. struct cfpkt *pkt;
  440. pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
  441. memset(skb->cb, 0, sizeof(struct caif_payload_info));
  442. if (cf_sk->layer.dn == NULL) {
  443. kfree_skb(skb);
  444. return -EINVAL;
  445. }
  446. return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
  447. }
  448. /* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
  449. static int caif_seqpkt_sendmsg(struct kiocb *kiocb, struct socket *sock,
  450. struct msghdr *msg, size_t len)
  451. {
  452. struct sock *sk = sock->sk;
  453. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  454. int buffer_size;
  455. int ret = 0;
  456. struct sk_buff *skb = NULL;
  457. int noblock;
  458. long timeo;
  459. caif_assert(cf_sk);
  460. ret = sock_error(sk);
  461. if (ret)
  462. goto err;
  463. ret = -EOPNOTSUPP;
  464. if (msg->msg_flags&MSG_OOB)
  465. goto err;
  466. ret = -EOPNOTSUPP;
  467. if (msg->msg_namelen)
  468. goto err;
  469. ret = -EINVAL;
  470. if (unlikely(msg->msg_iov->iov_base == NULL))
  471. goto err;
  472. noblock = msg->msg_flags & MSG_DONTWAIT;
  473. timeo = sock_sndtimeo(sk, noblock);
  474. timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
  475. 1, timeo, &ret);
  476. if (ret)
  477. goto err;
  478. ret = -EPIPE;
  479. if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
  480. sock_flag(sk, SOCK_DEAD) ||
  481. (sk->sk_shutdown & RCV_SHUTDOWN))
  482. goto err;
  483. /* Error if trying to write more than maximum frame size. */
  484. ret = -EMSGSIZE;
  485. if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM)
  486. goto err;
  487. buffer_size = len + cf_sk->headroom + cf_sk->tailroom;
  488. ret = -ENOMEM;
  489. skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
  490. if (!skb || skb_tailroom(skb) < buffer_size)
  491. goto err;
  492. skb_reserve(skb, cf_sk->headroom);
  493. ret = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
  494. if (ret)
  495. goto err;
  496. ret = transmit_skb(skb, cf_sk, noblock, timeo);
  497. if (ret < 0)
  498. /* skb is already freed */
  499. return ret;
  500. return len;
  501. err:
  502. kfree_skb(skb);
  503. return ret;
  504. }
  505. /*
  506. * Copied from unix_stream_sendmsg and adapted to CAIF:
  507. * Changed removed permission handling and added waiting for flow on
  508. * and other minor adaptations.
  509. */
  510. static int caif_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
  511. struct msghdr *msg, size_t len)
  512. {
  513. struct sock *sk = sock->sk;
  514. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  515. int err, size;
  516. struct sk_buff *skb;
  517. int sent = 0;
  518. long timeo;
  519. err = -EOPNOTSUPP;
  520. if (unlikely(msg->msg_flags&MSG_OOB))
  521. goto out_err;
  522. if (unlikely(msg->msg_namelen))
  523. goto out_err;
  524. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  525. timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
  526. if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
  527. goto pipe_err;
  528. while (sent < len) {
  529. size = len-sent;
  530. if (size > cf_sk->maxframe)
  531. size = cf_sk->maxframe;
  532. /* If size is more than half of sndbuf, chop up message */
  533. if (size > ((sk->sk_sndbuf >> 1) - 64))
  534. size = (sk->sk_sndbuf >> 1) - 64;
  535. if (size > SKB_MAX_ALLOC)
  536. size = SKB_MAX_ALLOC;
  537. skb = sock_alloc_send_skb(sk,
  538. size + cf_sk->headroom +
  539. cf_sk->tailroom,
  540. msg->msg_flags&MSG_DONTWAIT,
  541. &err);
  542. if (skb == NULL)
  543. goto out_err;
  544. skb_reserve(skb, cf_sk->headroom);
  545. /*
  546. * If you pass two values to the sock_alloc_send_skb
  547. * it tries to grab the large buffer with GFP_NOFS
  548. * (which can fail easily), and if it fails grab the
  549. * fallback size buffer which is under a page and will
  550. * succeed. [Alan]
  551. */
  552. size = min_t(int, size, skb_tailroom(skb));
  553. err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
  554. if (err) {
  555. kfree_skb(skb);
  556. goto out_err;
  557. }
  558. err = transmit_skb(skb, cf_sk,
  559. msg->msg_flags&MSG_DONTWAIT, timeo);
  560. if (err < 0)
  561. /* skb is already freed */
  562. goto pipe_err;
  563. sent += size;
  564. }
  565. return sent;
  566. pipe_err:
  567. if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
  568. send_sig(SIGPIPE, current, 0);
  569. err = -EPIPE;
  570. out_err:
  571. return sent ? : err;
  572. }
  573. static int setsockopt(struct socket *sock,
  574. int lvl, int opt, char __user *ov, unsigned int ol)
  575. {
  576. struct sock *sk = sock->sk;
  577. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  578. int linksel;
  579. if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
  580. return -ENOPROTOOPT;
  581. switch (opt) {
  582. case CAIFSO_LINK_SELECT:
  583. if (ol < sizeof(int))
  584. return -EINVAL;
  585. if (lvl != SOL_CAIF)
  586. goto bad_sol;
  587. if (copy_from_user(&linksel, ov, sizeof(int)))
  588. return -EINVAL;
  589. lock_sock(&(cf_sk->sk));
  590. cf_sk->conn_req.link_selector = linksel;
  591. release_sock(&cf_sk->sk);
  592. return 0;
  593. case CAIFSO_REQ_PARAM:
  594. if (lvl != SOL_CAIF)
  595. goto bad_sol;
  596. if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
  597. return -ENOPROTOOPT;
  598. lock_sock(&(cf_sk->sk));
  599. if (ol > sizeof(cf_sk->conn_req.param.data) ||
  600. copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) {
  601. release_sock(&cf_sk->sk);
  602. return -EINVAL;
  603. }
  604. cf_sk->conn_req.param.size = ol;
  605. release_sock(&cf_sk->sk);
  606. return 0;
  607. default:
  608. return -ENOPROTOOPT;
  609. }
  610. return 0;
  611. bad_sol:
  612. return -ENOPROTOOPT;
  613. }
  614. /*
  615. * caif_connect() - Connect a CAIF Socket
  616. * Copied and modified af_irda.c:irda_connect().
  617. *
  618. * Note : by consulting "errno", the user space caller may learn the cause
  619. * of the failure. Most of them are visible in the function, others may come
  620. * from subroutines called and are listed here :
  621. * o -EAFNOSUPPORT: bad socket family or type.
  622. * o -ESOCKTNOSUPPORT: bad socket type or protocol
  623. * o -EINVAL: bad socket address, or CAIF link type
  624. * o -ECONNREFUSED: remote end refused the connection.
  625. * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
  626. * o -EISCONN: already connected.
  627. * o -ETIMEDOUT: Connection timed out (send timeout)
  628. * o -ENODEV: No link layer to send request
  629. * o -ECONNRESET: Received Shutdown indication or lost link layer
  630. * o -ENOMEM: Out of memory
  631. *
  632. * State Strategy:
  633. * o sk_state: holds the CAIF_* protocol state, it's updated by
  634. * caif_ctrl_cb.
  635. * o sock->state: holds the SS_* socket state and is updated by connect and
  636. * disconnect.
  637. */
  638. static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
  639. int addr_len, int flags)
  640. {
  641. struct sock *sk = sock->sk;
  642. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  643. long timeo;
  644. int err;
  645. int ifindex, headroom, tailroom;
  646. unsigned int mtu;
  647. struct net_device *dev;
  648. lock_sock(sk);
  649. err = -EINVAL;
  650. if (addr_len < offsetofend(struct sockaddr, sa_family))
  651. goto out;
  652. err = -EAFNOSUPPORT;
  653. if (uaddr->sa_family != AF_CAIF)
  654. goto out;
  655. switch (sock->state) {
  656. case SS_UNCONNECTED:
  657. /* Normal case, a fresh connect */
  658. caif_assert(sk->sk_state == CAIF_DISCONNECTED);
  659. break;
  660. case SS_CONNECTING:
  661. switch (sk->sk_state) {
  662. case CAIF_CONNECTED:
  663. sock->state = SS_CONNECTED;
  664. err = -EISCONN;
  665. goto out;
  666. case CAIF_DISCONNECTED:
  667. /* Reconnect allowed */
  668. break;
  669. case CAIF_CONNECTING:
  670. err = -EALREADY;
  671. if (flags & O_NONBLOCK)
  672. goto out;
  673. goto wait_connect;
  674. }
  675. break;
  676. case SS_CONNECTED:
  677. caif_assert(sk->sk_state == CAIF_CONNECTED ||
  678. sk->sk_state == CAIF_DISCONNECTED);
  679. if (sk->sk_shutdown & SHUTDOWN_MASK) {
  680. /* Allow re-connect after SHUTDOWN_IND */
  681. caif_disconnect_client(sock_net(sk), &cf_sk->layer);
  682. caif_free_client(&cf_sk->layer);
  683. break;
  684. }
  685. /* No reconnect on a seqpacket socket */
  686. err = -EISCONN;
  687. goto out;
  688. case SS_DISCONNECTING:
  689. case SS_FREE:
  690. caif_assert(1); /*Should never happen */
  691. break;
  692. }
  693. sk->sk_state = CAIF_DISCONNECTED;
  694. sock->state = SS_UNCONNECTED;
  695. sk_stream_kill_queues(&cf_sk->sk);
  696. err = -EINVAL;
  697. if (addr_len != sizeof(struct sockaddr_caif))
  698. goto out;
  699. memcpy(&cf_sk->conn_req.sockaddr, uaddr,
  700. sizeof(struct sockaddr_caif));
  701. /* Move to connecting socket, start sending Connect Requests */
  702. sock->state = SS_CONNECTING;
  703. sk->sk_state = CAIF_CONNECTING;
  704. /* Check priority value comming from socket */
  705. /* if priority value is out of range it will be ajusted */
  706. if (cf_sk->sk.sk_priority > CAIF_PRIO_MAX)
  707. cf_sk->conn_req.priority = CAIF_PRIO_MAX;
  708. else if (cf_sk->sk.sk_priority < CAIF_PRIO_MIN)
  709. cf_sk->conn_req.priority = CAIF_PRIO_MIN;
  710. else
  711. cf_sk->conn_req.priority = cf_sk->sk.sk_priority;
  712. /*ifindex = id of the interface.*/
  713. cf_sk->conn_req.ifindex = cf_sk->sk.sk_bound_dev_if;
  714. cf_sk->layer.receive = caif_sktrecv_cb;
  715. err = caif_connect_client(sock_net(sk), &cf_sk->conn_req,
  716. &cf_sk->layer, &ifindex, &headroom, &tailroom);
  717. if (err < 0) {
  718. cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
  719. cf_sk->sk.sk_state = CAIF_DISCONNECTED;
  720. goto out;
  721. }
  722. err = -ENODEV;
  723. rcu_read_lock();
  724. dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
  725. if (!dev) {
  726. rcu_read_unlock();
  727. goto out;
  728. }
  729. cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
  730. mtu = dev->mtu;
  731. rcu_read_unlock();
  732. cf_sk->tailroom = tailroom;
  733. cf_sk->maxframe = mtu - (headroom + tailroom);
  734. if (cf_sk->maxframe < 1) {
  735. pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
  736. err = -ENODEV;
  737. goto out;
  738. }
  739. err = -EINPROGRESS;
  740. wait_connect:
  741. if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
  742. goto out;
  743. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  744. release_sock(sk);
  745. err = -ERESTARTSYS;
  746. timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
  747. sk->sk_state != CAIF_CONNECTING,
  748. timeo);
  749. lock_sock(sk);
  750. if (timeo < 0)
  751. goto out; /* -ERESTARTSYS */
  752. err = -ETIMEDOUT;
  753. if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
  754. goto out;
  755. if (sk->sk_state != CAIF_CONNECTED) {
  756. sock->state = SS_UNCONNECTED;
  757. err = sock_error(sk);
  758. if (!err)
  759. err = -ECONNREFUSED;
  760. goto out;
  761. }
  762. sock->state = SS_CONNECTED;
  763. err = 0;
  764. out:
  765. release_sock(sk);
  766. return err;
  767. }
  768. /*
  769. * caif_release() - Disconnect a CAIF Socket
  770. * Copied and modified af_irda.c:irda_release().
  771. */
  772. static int caif_release(struct socket *sock)
  773. {
  774. struct sock *sk = sock->sk;
  775. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  776. if (!sk)
  777. return 0;
  778. set_tx_flow_off(cf_sk);
  779. /*
  780. * Ensure that packets are not queued after this point in time.
  781. * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
  782. * this ensures no packets when sock is dead.
  783. */
  784. spin_lock_bh(&sk->sk_receive_queue.lock);
  785. sock_set_flag(sk, SOCK_DEAD);
  786. spin_unlock_bh(&sk->sk_receive_queue.lock);
  787. sock->sk = NULL;
  788. WARN_ON(IS_ERR(cf_sk->debugfs_socket_dir));
  789. if (cf_sk->debugfs_socket_dir != NULL)
  790. debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
  791. lock_sock(&(cf_sk->sk));
  792. sk->sk_state = CAIF_DISCONNECTED;
  793. sk->sk_shutdown = SHUTDOWN_MASK;
  794. caif_disconnect_client(sock_net(sk), &cf_sk->layer);
  795. cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
  796. wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP);
  797. sock_orphan(sk);
  798. sk_stream_kill_queues(&cf_sk->sk);
  799. release_sock(sk);
  800. sock_put(sk);
  801. return 0;
  802. }
  803. /* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
  804. static unsigned int caif_poll(struct file *file,
  805. struct socket *sock, poll_table *wait)
  806. {
  807. struct sock *sk = sock->sk;
  808. unsigned int mask;
  809. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  810. sock_poll_wait(file, sk_sleep(sk), wait);
  811. mask = 0;
  812. /* exceptional events? */
  813. if (sk->sk_err)
  814. mask |= POLLERR;
  815. if (sk->sk_shutdown == SHUTDOWN_MASK)
  816. mask |= POLLHUP;
  817. if (sk->sk_shutdown & RCV_SHUTDOWN)
  818. mask |= POLLRDHUP;
  819. /* readable? */
  820. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  821. (sk->sk_shutdown & RCV_SHUTDOWN))
  822. mask |= POLLIN | POLLRDNORM;
  823. /*
  824. * we set writable also when the other side has shut down the
  825. * connection. This prevents stuck sockets.
  826. */
  827. if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
  828. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  829. return mask;
  830. }
  831. static const struct proto_ops caif_seqpacket_ops = {
  832. .family = PF_CAIF,
  833. .owner = THIS_MODULE,
  834. .release = caif_release,
  835. .bind = sock_no_bind,
  836. .connect = caif_connect,
  837. .socketpair = sock_no_socketpair,
  838. .accept = sock_no_accept,
  839. .getname = sock_no_getname,
  840. .poll = caif_poll,
  841. .ioctl = sock_no_ioctl,
  842. .listen = sock_no_listen,
  843. .shutdown = sock_no_shutdown,
  844. .setsockopt = setsockopt,
  845. .getsockopt = sock_no_getsockopt,
  846. .sendmsg = caif_seqpkt_sendmsg,
  847. .recvmsg = caif_seqpkt_recvmsg,
  848. .mmap = sock_no_mmap,
  849. .sendpage = sock_no_sendpage,
  850. };
  851. static const struct proto_ops caif_stream_ops = {
  852. .family = PF_CAIF,
  853. .owner = THIS_MODULE,
  854. .release = caif_release,
  855. .bind = sock_no_bind,
  856. .connect = caif_connect,
  857. .socketpair = sock_no_socketpair,
  858. .accept = sock_no_accept,
  859. .getname = sock_no_getname,
  860. .poll = caif_poll,
  861. .ioctl = sock_no_ioctl,
  862. .listen = sock_no_listen,
  863. .shutdown = sock_no_shutdown,
  864. .setsockopt = setsockopt,
  865. .getsockopt = sock_no_getsockopt,
  866. .sendmsg = caif_stream_sendmsg,
  867. .recvmsg = caif_stream_recvmsg,
  868. .mmap = sock_no_mmap,
  869. .sendpage = sock_no_sendpage,
  870. };
  871. /* This function is called when a socket is finally destroyed. */
  872. static void caif_sock_destructor(struct sock *sk)
  873. {
  874. struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
  875. caif_assert(!atomic_read(&sk->sk_wmem_alloc));
  876. caif_assert(sk_unhashed(sk));
  877. caif_assert(!sk->sk_socket);
  878. if (!sock_flag(sk, SOCK_DEAD)) {
  879. WARN(1, "Attempt to release alive CAIF socket: %p\n", sk);
  880. return;
  881. }
  882. sk_stream_kill_queues(&cf_sk->sk);
  883. caif_free_client(&cf_sk->layer);
  884. }
  885. static int caif_create(struct net *net, struct socket *sock, int protocol,
  886. int kern)
  887. {
  888. struct sock *sk = NULL;
  889. struct caifsock *cf_sk = NULL;
  890. static struct proto prot = {.name = "PF_CAIF",
  891. .owner = THIS_MODULE,
  892. .obj_size = sizeof(struct caifsock),
  893. };
  894. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
  895. return -EPERM;
  896. /*
  897. * The sock->type specifies the socket type to use.
  898. * The CAIF socket is a packet stream in the sense
  899. * that it is packet based. CAIF trusts the reliability
  900. * of the link, no resending is implemented.
  901. */
  902. if (sock->type == SOCK_SEQPACKET)
  903. sock->ops = &caif_seqpacket_ops;
  904. else if (sock->type == SOCK_STREAM)
  905. sock->ops = &caif_stream_ops;
  906. else
  907. return -ESOCKTNOSUPPORT;
  908. if (protocol < 0 || protocol >= CAIFPROTO_MAX)
  909. return -EPROTONOSUPPORT;
  910. /*
  911. * Set the socket state to unconnected. The socket state
  912. * is really not used at all in the net/core or socket.c but the
  913. * initialization makes sure that sock->state is not uninitialized.
  914. */
  915. sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot);
  916. if (!sk)
  917. return -ENOMEM;
  918. cf_sk = container_of(sk, struct caifsock, sk);
  919. /* Store the protocol */
  920. sk->sk_protocol = (unsigned char) protocol;
  921. /*
  922. * Lock in order to try to stop someone from opening the socket
  923. * too early.
  924. */
  925. lock_sock(&(cf_sk->sk));
  926. /* Initialize the nozero default sock structure data. */
  927. sock_init_data(sock, sk);
  928. sk->sk_destruct = caif_sock_destructor;
  929. mutex_init(&cf_sk->readlock); /* single task reading lock */
  930. cf_sk->layer.ctrlcmd = caif_ctrl_cb;
  931. cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
  932. cf_sk->sk.sk_state = CAIF_DISCONNECTED;
  933. set_tx_flow_off(cf_sk);
  934. set_rx_flow_on(cf_sk);
  935. /* Set default options on configuration */
  936. cf_sk->sk.sk_priority = CAIF_PRIO_NORMAL;
  937. cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
  938. cf_sk->conn_req.protocol = protocol;
  939. release_sock(&cf_sk->sk);
  940. return 0;
  941. }
  942. static struct net_proto_family caif_family_ops = {
  943. .family = PF_CAIF,
  944. .create = caif_create,
  945. .owner = THIS_MODULE,
  946. };
  947. static int __init caif_sktinit_module(void)
  948. {
  949. int err = sock_register(&caif_family_ops);
  950. if (!err)
  951. return err;
  952. return 0;
  953. }
  954. static void __exit caif_sktexit_module(void)
  955. {
  956. sock_unregister(PF_CAIF);
  957. }
  958. module_init(caif_sktinit_module);
  959. module_exit(caif_sktexit_module);