caif_socket.c 26 KB

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