socket.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
  1. /*
  2. * net/tipc/socket.c: TIPC socket API
  3. *
  4. * Copyright (c) 2001-2007, 2012 Ericsson AB
  5. * Copyright (c) 2004-2008, 2010-2012, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "core.h"
  37. #include "port.h"
  38. #include <linux/export.h>
  39. #include <net/sock.h>
  40. #define SS_LISTENING -1 /* socket is listening */
  41. #define SS_READY -2 /* socket is connectionless */
  42. #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
  43. struct tipc_sock {
  44. struct sock sk;
  45. struct tipc_port *p;
  46. struct tipc_portid peer_name;
  47. unsigned int conn_timeout;
  48. };
  49. #define tipc_sk(sk) ((struct tipc_sock *)(sk))
  50. #define tipc_sk_port(sk) (tipc_sk(sk)->p)
  51. #define tipc_rx_ready(sock) (!skb_queue_empty(&sock->sk->sk_receive_queue) || \
  52. (sock->state == SS_DISCONNECTING))
  53. static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
  54. static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
  55. static void wakeupdispatch(struct tipc_port *tport);
  56. static void tipc_data_ready(struct sock *sk, int len);
  57. static void tipc_write_space(struct sock *sk);
  58. static const struct proto_ops packet_ops;
  59. static const struct proto_ops stream_ops;
  60. static const struct proto_ops msg_ops;
  61. static struct proto tipc_proto;
  62. static int sockets_enabled;
  63. /*
  64. * Revised TIPC socket locking policy:
  65. *
  66. * Most socket operations take the standard socket lock when they start
  67. * and hold it until they finish (or until they need to sleep). Acquiring
  68. * this lock grants the owner exclusive access to the fields of the socket
  69. * data structures, with the exception of the backlog queue. A few socket
  70. * operations can be done without taking the socket lock because they only
  71. * read socket information that never changes during the life of the socket.
  72. *
  73. * Socket operations may acquire the lock for the associated TIPC port if they
  74. * need to perform an operation on the port. If any routine needs to acquire
  75. * both the socket lock and the port lock it must take the socket lock first
  76. * to avoid the risk of deadlock.
  77. *
  78. * The dispatcher handling incoming messages cannot grab the socket lock in
  79. * the standard fashion, since invoked it runs at the BH level and cannot block.
  80. * Instead, it checks to see if the socket lock is currently owned by someone,
  81. * and either handles the message itself or adds it to the socket's backlog
  82. * queue; in the latter case the queued message is processed once the process
  83. * owning the socket lock releases it.
  84. *
  85. * NOTE: Releasing the socket lock while an operation is sleeping overcomes
  86. * the problem of a blocked socket operation preventing any other operations
  87. * from occurring. However, applications must be careful if they have
  88. * multiple threads trying to send (or receive) on the same socket, as these
  89. * operations might interfere with each other. For example, doing a connect
  90. * and a receive at the same time might allow the receive to consume the
  91. * ACK message meant for the connect. While additional work could be done
  92. * to try and overcome this, it doesn't seem to be worthwhile at the present.
  93. *
  94. * NOTE: Releasing the socket lock while an operation is sleeping also ensures
  95. * that another operation that must be performed in a non-blocking manner is
  96. * not delayed for very long because the lock has already been taken.
  97. *
  98. * NOTE: This code assumes that certain fields of a port/socket pair are
  99. * constant over its lifetime; such fields can be examined without taking
  100. * the socket lock and/or port lock, and do not need to be re-read even
  101. * after resuming processing after waiting. These fields include:
  102. * - socket type
  103. * - pointer to socket sk structure (aka tipc_sock structure)
  104. * - pointer to port structure
  105. * - port reference
  106. */
  107. /**
  108. * advance_rx_queue - discard first buffer in socket receive queue
  109. *
  110. * Caller must hold socket lock
  111. */
  112. static void advance_rx_queue(struct sock *sk)
  113. {
  114. kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
  115. }
  116. /**
  117. * reject_rx_queue - reject all buffers in socket receive queue
  118. *
  119. * Caller must hold socket lock
  120. */
  121. static void reject_rx_queue(struct sock *sk)
  122. {
  123. struct sk_buff *buf;
  124. while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
  125. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  126. }
  127. /**
  128. * tipc_create - create a TIPC socket
  129. * @net: network namespace (must be default network)
  130. * @sock: pre-allocated socket structure
  131. * @protocol: protocol indicator (must be 0)
  132. * @kern: caused by kernel or by userspace?
  133. *
  134. * This routine creates additional data structures used by the TIPC socket,
  135. * initializes them, and links them together.
  136. *
  137. * Returns 0 on success, errno otherwise
  138. */
  139. static int tipc_create(struct net *net, struct socket *sock, int protocol,
  140. int kern)
  141. {
  142. const struct proto_ops *ops;
  143. socket_state state;
  144. struct sock *sk;
  145. struct tipc_port *tp_ptr;
  146. /* Validate arguments */
  147. if (unlikely(protocol != 0))
  148. return -EPROTONOSUPPORT;
  149. switch (sock->type) {
  150. case SOCK_STREAM:
  151. ops = &stream_ops;
  152. state = SS_UNCONNECTED;
  153. break;
  154. case SOCK_SEQPACKET:
  155. ops = &packet_ops;
  156. state = SS_UNCONNECTED;
  157. break;
  158. case SOCK_DGRAM:
  159. case SOCK_RDM:
  160. ops = &msg_ops;
  161. state = SS_READY;
  162. break;
  163. default:
  164. return -EPROTOTYPE;
  165. }
  166. /* Allocate socket's protocol area */
  167. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
  168. if (sk == NULL)
  169. return -ENOMEM;
  170. /* Allocate TIPC port for socket to use */
  171. tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
  172. TIPC_LOW_IMPORTANCE);
  173. if (unlikely(!tp_ptr)) {
  174. sk_free(sk);
  175. return -ENOMEM;
  176. }
  177. /* Finish initializing socket data structures */
  178. sock->ops = ops;
  179. sock->state = state;
  180. sock_init_data(sock, sk);
  181. sk->sk_backlog_rcv = backlog_rcv;
  182. sk->sk_rcvbuf = sysctl_tipc_rmem[1];
  183. sk->sk_data_ready = tipc_data_ready;
  184. sk->sk_write_space = tipc_write_space;
  185. tipc_sk(sk)->p = tp_ptr;
  186. tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
  187. spin_unlock_bh(tp_ptr->lock);
  188. if (sock->state == SS_READY) {
  189. tipc_set_portunreturnable(tp_ptr->ref, 1);
  190. if (sock->type == SOCK_DGRAM)
  191. tipc_set_portunreliable(tp_ptr->ref, 1);
  192. }
  193. return 0;
  194. }
  195. /**
  196. * release - destroy a TIPC socket
  197. * @sock: socket to destroy
  198. *
  199. * This routine cleans up any messages that are still queued on the socket.
  200. * For DGRAM and RDM socket types, all queued messages are rejected.
  201. * For SEQPACKET and STREAM socket types, the first message is rejected
  202. * and any others are discarded. (If the first message on a STREAM socket
  203. * is partially-read, it is discarded and the next one is rejected instead.)
  204. *
  205. * NOTE: Rejected messages are not necessarily returned to the sender! They
  206. * are returned or discarded according to the "destination droppable" setting
  207. * specified for the message by the sender.
  208. *
  209. * Returns 0 on success, errno otherwise
  210. */
  211. static int release(struct socket *sock)
  212. {
  213. struct sock *sk = sock->sk;
  214. struct tipc_port *tport;
  215. struct sk_buff *buf;
  216. int res;
  217. /*
  218. * Exit if socket isn't fully initialized (occurs when a failed accept()
  219. * releases a pre-allocated child socket that was never used)
  220. */
  221. if (sk == NULL)
  222. return 0;
  223. tport = tipc_sk_port(sk);
  224. lock_sock(sk);
  225. /*
  226. * Reject all unreceived messages, except on an active connection
  227. * (which disconnects locally & sends a 'FIN+' to peer)
  228. */
  229. while (sock->state != SS_DISCONNECTING) {
  230. buf = __skb_dequeue(&sk->sk_receive_queue);
  231. if (buf == NULL)
  232. break;
  233. if (TIPC_SKB_CB(buf)->handle != 0)
  234. kfree_skb(buf);
  235. else {
  236. if ((sock->state == SS_CONNECTING) ||
  237. (sock->state == SS_CONNECTED)) {
  238. sock->state = SS_DISCONNECTING;
  239. tipc_disconnect(tport->ref);
  240. }
  241. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  242. }
  243. }
  244. /*
  245. * Delete TIPC port; this ensures no more messages are queued
  246. * (also disconnects an active connection & sends a 'FIN-' to peer)
  247. */
  248. res = tipc_deleteport(tport->ref);
  249. /* Discard any remaining (connection-based) messages in receive queue */
  250. __skb_queue_purge(&sk->sk_receive_queue);
  251. /* Reject any messages that accumulated in backlog queue */
  252. sock->state = SS_DISCONNECTING;
  253. release_sock(sk);
  254. sock_put(sk);
  255. sock->sk = NULL;
  256. return res;
  257. }
  258. /**
  259. * bind - associate or disassocate TIPC name(s) with a socket
  260. * @sock: socket structure
  261. * @uaddr: socket address describing name(s) and desired operation
  262. * @uaddr_len: size of socket address data structure
  263. *
  264. * Name and name sequence binding is indicated using a positive scope value;
  265. * a negative scope value unbinds the specified name. Specifying no name
  266. * (i.e. a socket address length of 0) unbinds all names from the socket.
  267. *
  268. * Returns 0 on success, errno otherwise
  269. *
  270. * NOTE: This routine doesn't need to take the socket lock since it doesn't
  271. * access any non-constant socket information.
  272. */
  273. static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
  274. {
  275. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  276. u32 portref = tipc_sk_port(sock->sk)->ref;
  277. if (unlikely(!uaddr_len))
  278. return tipc_withdraw(portref, 0, NULL);
  279. if (uaddr_len < sizeof(struct sockaddr_tipc))
  280. return -EINVAL;
  281. if (addr->family != AF_TIPC)
  282. return -EAFNOSUPPORT;
  283. if (addr->addrtype == TIPC_ADDR_NAME)
  284. addr->addr.nameseq.upper = addr->addr.nameseq.lower;
  285. else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
  286. return -EAFNOSUPPORT;
  287. if (addr->addr.nameseq.type < TIPC_RESERVED_TYPES)
  288. return -EACCES;
  289. return (addr->scope > 0) ?
  290. tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
  291. tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
  292. }
  293. /**
  294. * get_name - get port ID of socket or peer socket
  295. * @sock: socket structure
  296. * @uaddr: area for returned socket address
  297. * @uaddr_len: area for returned length of socket address
  298. * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
  299. *
  300. * Returns 0 on success, errno otherwise
  301. *
  302. * NOTE: This routine doesn't need to take the socket lock since it only
  303. * accesses socket information that is unchanging (or which changes in
  304. * a completely predictable manner).
  305. */
  306. static int get_name(struct socket *sock, struct sockaddr *uaddr,
  307. int *uaddr_len, int peer)
  308. {
  309. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  310. struct tipc_sock *tsock = tipc_sk(sock->sk);
  311. memset(addr, 0, sizeof(*addr));
  312. if (peer) {
  313. if ((sock->state != SS_CONNECTED) &&
  314. ((peer != 2) || (sock->state != SS_DISCONNECTING)))
  315. return -ENOTCONN;
  316. addr->addr.id.ref = tsock->peer_name.ref;
  317. addr->addr.id.node = tsock->peer_name.node;
  318. } else {
  319. addr->addr.id.ref = tsock->p->ref;
  320. addr->addr.id.node = tipc_own_addr;
  321. }
  322. *uaddr_len = sizeof(*addr);
  323. addr->addrtype = TIPC_ADDR_ID;
  324. addr->family = AF_TIPC;
  325. addr->scope = 0;
  326. addr->addr.name.domain = 0;
  327. return 0;
  328. }
  329. /**
  330. * poll - read and possibly block on pollmask
  331. * @file: file structure associated with the socket
  332. * @sock: socket for which to calculate the poll bits
  333. * @wait: ???
  334. *
  335. * Returns pollmask value
  336. *
  337. * COMMENTARY:
  338. * It appears that the usual socket locking mechanisms are not useful here
  339. * since the pollmask info is potentially out-of-date the moment this routine
  340. * exits. TCP and other protocols seem to rely on higher level poll routines
  341. * to handle any preventable race conditions, so TIPC will do the same ...
  342. *
  343. * TIPC sets the returned events as follows:
  344. *
  345. * socket state flags set
  346. * ------------ ---------
  347. * unconnected no read flags
  348. * POLLOUT if port is not congested
  349. *
  350. * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
  351. * no write flags
  352. *
  353. * connected POLLIN/POLLRDNORM if data in rx queue
  354. * POLLOUT if port is not congested
  355. *
  356. * disconnecting POLLIN/POLLRDNORM/POLLHUP
  357. * no write flags
  358. *
  359. * listening POLLIN if SYN in rx queue
  360. * no write flags
  361. *
  362. * ready POLLIN/POLLRDNORM if data in rx queue
  363. * [connectionless] POLLOUT (since port cannot be congested)
  364. *
  365. * IMPORTANT: The fact that a read or write operation is indicated does NOT
  366. * imply that the operation will succeed, merely that it should be performed
  367. * and will not block.
  368. */
  369. static unsigned int poll(struct file *file, struct socket *sock,
  370. poll_table *wait)
  371. {
  372. struct sock *sk = sock->sk;
  373. u32 mask = 0;
  374. sock_poll_wait(file, sk_sleep(sk), wait);
  375. switch ((int)sock->state) {
  376. case SS_UNCONNECTED:
  377. if (!tipc_sk_port(sk)->congested)
  378. mask |= POLLOUT;
  379. break;
  380. case SS_READY:
  381. case SS_CONNECTED:
  382. if (!tipc_sk_port(sk)->congested)
  383. mask |= POLLOUT;
  384. /* fall thru' */
  385. case SS_CONNECTING:
  386. case SS_LISTENING:
  387. if (!skb_queue_empty(&sk->sk_receive_queue))
  388. mask |= (POLLIN | POLLRDNORM);
  389. break;
  390. case SS_DISCONNECTING:
  391. mask = (POLLIN | POLLRDNORM | POLLHUP);
  392. break;
  393. }
  394. return mask;
  395. }
  396. /**
  397. * dest_name_check - verify user is permitted to send to specified port name
  398. * @dest: destination address
  399. * @m: descriptor for message to be sent
  400. *
  401. * Prevents restricted configuration commands from being issued by
  402. * unauthorized users.
  403. *
  404. * Returns 0 if permission is granted, otherwise errno
  405. */
  406. static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
  407. {
  408. struct tipc_cfg_msg_hdr hdr;
  409. if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
  410. return 0;
  411. if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
  412. return 0;
  413. if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
  414. return -EACCES;
  415. if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
  416. return -EMSGSIZE;
  417. if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
  418. return -EFAULT;
  419. if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
  420. return -EACCES;
  421. return 0;
  422. }
  423. /**
  424. * send_msg - send message in connectionless manner
  425. * @iocb: if NULL, indicates that socket lock is already held
  426. * @sock: socket structure
  427. * @m: message to send
  428. * @total_len: length of message
  429. *
  430. * Message must have an destination specified explicitly.
  431. * Used for SOCK_RDM and SOCK_DGRAM messages,
  432. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  433. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  434. *
  435. * Returns the number of bytes sent on success, or errno otherwise
  436. */
  437. static int send_msg(struct kiocb *iocb, struct socket *sock,
  438. struct msghdr *m, size_t total_len)
  439. {
  440. struct sock *sk = sock->sk;
  441. struct tipc_port *tport = tipc_sk_port(sk);
  442. struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
  443. int needs_conn;
  444. long timeout_val;
  445. int res = -EINVAL;
  446. if (unlikely(!dest))
  447. return -EDESTADDRREQ;
  448. if (unlikely((m->msg_namelen < sizeof(*dest)) ||
  449. (dest->family != AF_TIPC)))
  450. return -EINVAL;
  451. if (total_len > TIPC_MAX_USER_MSG_SIZE)
  452. return -EMSGSIZE;
  453. if (iocb)
  454. lock_sock(sk);
  455. needs_conn = (sock->state != SS_READY);
  456. if (unlikely(needs_conn)) {
  457. if (sock->state == SS_LISTENING) {
  458. res = -EPIPE;
  459. goto exit;
  460. }
  461. if (sock->state != SS_UNCONNECTED) {
  462. res = -EISCONN;
  463. goto exit;
  464. }
  465. if (tport->published) {
  466. res = -EOPNOTSUPP;
  467. goto exit;
  468. }
  469. if (dest->addrtype == TIPC_ADDR_NAME) {
  470. tport->conn_type = dest->addr.name.name.type;
  471. tport->conn_instance = dest->addr.name.name.instance;
  472. }
  473. /* Abort any pending connection attempts (very unlikely) */
  474. reject_rx_queue(sk);
  475. }
  476. timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  477. do {
  478. if (dest->addrtype == TIPC_ADDR_NAME) {
  479. res = dest_name_check(dest, m);
  480. if (res)
  481. break;
  482. res = tipc_send2name(tport->ref,
  483. &dest->addr.name.name,
  484. dest->addr.name.domain,
  485. m->msg_iovlen,
  486. m->msg_iov,
  487. total_len);
  488. } else if (dest->addrtype == TIPC_ADDR_ID) {
  489. res = tipc_send2port(tport->ref,
  490. &dest->addr.id,
  491. m->msg_iovlen,
  492. m->msg_iov,
  493. total_len);
  494. } else if (dest->addrtype == TIPC_ADDR_MCAST) {
  495. if (needs_conn) {
  496. res = -EOPNOTSUPP;
  497. break;
  498. }
  499. res = dest_name_check(dest, m);
  500. if (res)
  501. break;
  502. res = tipc_multicast(tport->ref,
  503. &dest->addr.nameseq,
  504. m->msg_iovlen,
  505. m->msg_iov,
  506. total_len);
  507. }
  508. if (likely(res != -ELINKCONG)) {
  509. if (needs_conn && (res >= 0))
  510. sock->state = SS_CONNECTING;
  511. break;
  512. }
  513. if (timeout_val <= 0L) {
  514. res = timeout_val ? timeout_val : -EWOULDBLOCK;
  515. break;
  516. }
  517. release_sock(sk);
  518. timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
  519. !tport->congested, timeout_val);
  520. lock_sock(sk);
  521. } while (1);
  522. exit:
  523. if (iocb)
  524. release_sock(sk);
  525. return res;
  526. }
  527. /**
  528. * send_packet - send a connection-oriented message
  529. * @iocb: if NULL, indicates that socket lock is already held
  530. * @sock: socket structure
  531. * @m: message to send
  532. * @total_len: length of message
  533. *
  534. * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
  535. *
  536. * Returns the number of bytes sent on success, or errno otherwise
  537. */
  538. static int send_packet(struct kiocb *iocb, struct socket *sock,
  539. struct msghdr *m, size_t total_len)
  540. {
  541. struct sock *sk = sock->sk;
  542. struct tipc_port *tport = tipc_sk_port(sk);
  543. struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
  544. long timeout_val;
  545. int res;
  546. /* Handle implied connection establishment */
  547. if (unlikely(dest))
  548. return send_msg(iocb, sock, m, total_len);
  549. if (total_len > TIPC_MAX_USER_MSG_SIZE)
  550. return -EMSGSIZE;
  551. if (iocb)
  552. lock_sock(sk);
  553. timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  554. do {
  555. if (unlikely(sock->state != SS_CONNECTED)) {
  556. if (sock->state == SS_DISCONNECTING)
  557. res = -EPIPE;
  558. else
  559. res = -ENOTCONN;
  560. break;
  561. }
  562. res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov,
  563. total_len);
  564. if (likely(res != -ELINKCONG))
  565. break;
  566. if (timeout_val <= 0L) {
  567. res = timeout_val ? timeout_val : -EWOULDBLOCK;
  568. break;
  569. }
  570. release_sock(sk);
  571. timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
  572. (!tport->congested || !tport->connected), timeout_val);
  573. lock_sock(sk);
  574. } while (1);
  575. if (iocb)
  576. release_sock(sk);
  577. return res;
  578. }
  579. /**
  580. * send_stream - send stream-oriented data
  581. * @iocb: (unused)
  582. * @sock: socket structure
  583. * @m: data to send
  584. * @total_len: total length of data to be sent
  585. *
  586. * Used for SOCK_STREAM data.
  587. *
  588. * Returns the number of bytes sent on success (or partial success),
  589. * or errno if no data sent
  590. */
  591. static int send_stream(struct kiocb *iocb, struct socket *sock,
  592. struct msghdr *m, size_t total_len)
  593. {
  594. struct sock *sk = sock->sk;
  595. struct tipc_port *tport = tipc_sk_port(sk);
  596. struct msghdr my_msg;
  597. struct iovec my_iov;
  598. struct iovec *curr_iov;
  599. int curr_iovlen;
  600. char __user *curr_start;
  601. u32 hdr_size;
  602. int curr_left;
  603. int bytes_to_send;
  604. int bytes_sent;
  605. int res;
  606. lock_sock(sk);
  607. /* Handle special cases where there is no connection */
  608. if (unlikely(sock->state != SS_CONNECTED)) {
  609. if (sock->state == SS_UNCONNECTED) {
  610. res = send_packet(NULL, sock, m, total_len);
  611. goto exit;
  612. } else if (sock->state == SS_DISCONNECTING) {
  613. res = -EPIPE;
  614. goto exit;
  615. } else {
  616. res = -ENOTCONN;
  617. goto exit;
  618. }
  619. }
  620. if (unlikely(m->msg_name)) {
  621. res = -EISCONN;
  622. goto exit;
  623. }
  624. if (total_len > (unsigned int)INT_MAX) {
  625. res = -EMSGSIZE;
  626. goto exit;
  627. }
  628. /*
  629. * Send each iovec entry using one or more messages
  630. *
  631. * Note: This algorithm is good for the most likely case
  632. * (i.e. one large iovec entry), but could be improved to pass sets
  633. * of small iovec entries into send_packet().
  634. */
  635. curr_iov = m->msg_iov;
  636. curr_iovlen = m->msg_iovlen;
  637. my_msg.msg_iov = &my_iov;
  638. my_msg.msg_iovlen = 1;
  639. my_msg.msg_flags = m->msg_flags;
  640. my_msg.msg_name = NULL;
  641. bytes_sent = 0;
  642. hdr_size = msg_hdr_sz(&tport->phdr);
  643. while (curr_iovlen--) {
  644. curr_start = curr_iov->iov_base;
  645. curr_left = curr_iov->iov_len;
  646. while (curr_left) {
  647. bytes_to_send = tport->max_pkt - hdr_size;
  648. if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
  649. bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
  650. if (curr_left < bytes_to_send)
  651. bytes_to_send = curr_left;
  652. my_iov.iov_base = curr_start;
  653. my_iov.iov_len = bytes_to_send;
  654. res = send_packet(NULL, sock, &my_msg, bytes_to_send);
  655. if (res < 0) {
  656. if (bytes_sent)
  657. res = bytes_sent;
  658. goto exit;
  659. }
  660. curr_left -= bytes_to_send;
  661. curr_start += bytes_to_send;
  662. bytes_sent += bytes_to_send;
  663. }
  664. curr_iov++;
  665. }
  666. res = bytes_sent;
  667. exit:
  668. release_sock(sk);
  669. return res;
  670. }
  671. /**
  672. * auto_connect - complete connection setup to a remote port
  673. * @sock: socket structure
  674. * @msg: peer's response message
  675. *
  676. * Returns 0 on success, errno otherwise
  677. */
  678. static int auto_connect(struct socket *sock, struct tipc_msg *msg)
  679. {
  680. struct tipc_sock *tsock = tipc_sk(sock->sk);
  681. struct tipc_port *p_ptr;
  682. tsock->peer_name.ref = msg_origport(msg);
  683. tsock->peer_name.node = msg_orignode(msg);
  684. p_ptr = tipc_port_deref(tsock->p->ref);
  685. if (!p_ptr)
  686. return -EINVAL;
  687. __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
  688. if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
  689. return -EINVAL;
  690. msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
  691. sock->state = SS_CONNECTED;
  692. return 0;
  693. }
  694. /**
  695. * set_orig_addr - capture sender's address for received message
  696. * @m: descriptor for message info
  697. * @msg: received message header
  698. *
  699. * Note: Address is not captured if not requested by receiver.
  700. */
  701. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  702. {
  703. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
  704. if (addr) {
  705. addr->family = AF_TIPC;
  706. addr->addrtype = TIPC_ADDR_ID;
  707. memset(&addr->addr, 0, sizeof(addr->addr));
  708. addr->addr.id.ref = msg_origport(msg);
  709. addr->addr.id.node = msg_orignode(msg);
  710. addr->addr.name.domain = 0; /* could leave uninitialized */
  711. addr->scope = 0; /* could leave uninitialized */
  712. m->msg_namelen = sizeof(struct sockaddr_tipc);
  713. }
  714. }
  715. /**
  716. * anc_data_recv - optionally capture ancillary data for received message
  717. * @m: descriptor for message info
  718. * @msg: received message header
  719. * @tport: TIPC port associated with message
  720. *
  721. * Note: Ancillary data is not captured if not requested by receiver.
  722. *
  723. * Returns 0 if successful, otherwise errno
  724. */
  725. static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  726. struct tipc_port *tport)
  727. {
  728. u32 anc_data[3];
  729. u32 err;
  730. u32 dest_type;
  731. int has_name;
  732. int res;
  733. if (likely(m->msg_controllen == 0))
  734. return 0;
  735. /* Optionally capture errored message object(s) */
  736. err = msg ? msg_errcode(msg) : 0;
  737. if (unlikely(err)) {
  738. anc_data[0] = err;
  739. anc_data[1] = msg_data_sz(msg);
  740. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  741. if (res)
  742. return res;
  743. if (anc_data[1]) {
  744. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  745. msg_data(msg));
  746. if (res)
  747. return res;
  748. }
  749. }
  750. /* Optionally capture message destination object */
  751. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  752. switch (dest_type) {
  753. case TIPC_NAMED_MSG:
  754. has_name = 1;
  755. anc_data[0] = msg_nametype(msg);
  756. anc_data[1] = msg_namelower(msg);
  757. anc_data[2] = msg_namelower(msg);
  758. break;
  759. case TIPC_MCAST_MSG:
  760. has_name = 1;
  761. anc_data[0] = msg_nametype(msg);
  762. anc_data[1] = msg_namelower(msg);
  763. anc_data[2] = msg_nameupper(msg);
  764. break;
  765. case TIPC_CONN_MSG:
  766. has_name = (tport->conn_type != 0);
  767. anc_data[0] = tport->conn_type;
  768. anc_data[1] = tport->conn_instance;
  769. anc_data[2] = tport->conn_instance;
  770. break;
  771. default:
  772. has_name = 0;
  773. }
  774. if (has_name) {
  775. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  776. if (res)
  777. return res;
  778. }
  779. return 0;
  780. }
  781. /**
  782. * recv_msg - receive packet-oriented message
  783. * @iocb: (unused)
  784. * @m: descriptor for message info
  785. * @buf_len: total size of user buffer area
  786. * @flags: receive flags
  787. *
  788. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  789. * If the complete message doesn't fit in user area, truncate it.
  790. *
  791. * Returns size of returned message data, errno otherwise
  792. */
  793. static int recv_msg(struct kiocb *iocb, struct socket *sock,
  794. struct msghdr *m, size_t buf_len, int flags)
  795. {
  796. struct sock *sk = sock->sk;
  797. struct tipc_port *tport = tipc_sk_port(sk);
  798. struct sk_buff *buf;
  799. struct tipc_msg *msg;
  800. long timeout;
  801. unsigned int sz;
  802. u32 err;
  803. int res;
  804. /* Catch invalid receive requests */
  805. if (unlikely(!buf_len))
  806. return -EINVAL;
  807. lock_sock(sk);
  808. if (unlikely(sock->state == SS_UNCONNECTED)) {
  809. res = -ENOTCONN;
  810. goto exit;
  811. }
  812. /* will be updated in set_orig_addr() if needed */
  813. m->msg_namelen = 0;
  814. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  815. restart:
  816. /* Look for a message in receive queue; wait if necessary */
  817. while (skb_queue_empty(&sk->sk_receive_queue)) {
  818. if (sock->state == SS_DISCONNECTING) {
  819. res = -ENOTCONN;
  820. goto exit;
  821. }
  822. if (timeout <= 0L) {
  823. res = timeout ? timeout : -EWOULDBLOCK;
  824. goto exit;
  825. }
  826. release_sock(sk);
  827. timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
  828. tipc_rx_ready(sock),
  829. timeout);
  830. lock_sock(sk);
  831. }
  832. /* Look at first message in receive queue */
  833. buf = skb_peek(&sk->sk_receive_queue);
  834. msg = buf_msg(buf);
  835. sz = msg_data_sz(msg);
  836. err = msg_errcode(msg);
  837. /* Discard an empty non-errored message & try again */
  838. if ((!sz) && (!err)) {
  839. advance_rx_queue(sk);
  840. goto restart;
  841. }
  842. /* Capture sender's address (optional) */
  843. set_orig_addr(m, msg);
  844. /* Capture ancillary data (optional) */
  845. res = anc_data_recv(m, msg, tport);
  846. if (res)
  847. goto exit;
  848. /* Capture message data (if valid) & compute return value (always) */
  849. if (!err) {
  850. if (unlikely(buf_len < sz)) {
  851. sz = buf_len;
  852. m->msg_flags |= MSG_TRUNC;
  853. }
  854. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
  855. m->msg_iov, sz);
  856. if (res)
  857. goto exit;
  858. res = sz;
  859. } else {
  860. if ((sock->state == SS_READY) ||
  861. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  862. res = 0;
  863. else
  864. res = -ECONNRESET;
  865. }
  866. /* Consume received message (optional) */
  867. if (likely(!(flags & MSG_PEEK))) {
  868. if ((sock->state != SS_READY) &&
  869. (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  870. tipc_acknowledge(tport->ref, tport->conn_unacked);
  871. advance_rx_queue(sk);
  872. }
  873. exit:
  874. release_sock(sk);
  875. return res;
  876. }
  877. /**
  878. * recv_stream - receive stream-oriented data
  879. * @iocb: (unused)
  880. * @m: descriptor for message info
  881. * @buf_len: total size of user buffer area
  882. * @flags: receive flags
  883. *
  884. * Used for SOCK_STREAM messages only. If not enough data is available
  885. * will optionally wait for more; never truncates data.
  886. *
  887. * Returns size of returned message data, errno otherwise
  888. */
  889. static int recv_stream(struct kiocb *iocb, struct socket *sock,
  890. struct msghdr *m, size_t buf_len, int flags)
  891. {
  892. struct sock *sk = sock->sk;
  893. struct tipc_port *tport = tipc_sk_port(sk);
  894. struct sk_buff *buf;
  895. struct tipc_msg *msg;
  896. long timeout;
  897. unsigned int sz;
  898. int sz_to_copy, target, needed;
  899. int sz_copied = 0;
  900. u32 err;
  901. int res = 0;
  902. /* Catch invalid receive attempts */
  903. if (unlikely(!buf_len))
  904. return -EINVAL;
  905. lock_sock(sk);
  906. if (unlikely((sock->state == SS_UNCONNECTED))) {
  907. res = -ENOTCONN;
  908. goto exit;
  909. }
  910. /* will be updated in set_orig_addr() if needed */
  911. m->msg_namelen = 0;
  912. target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
  913. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  914. restart:
  915. /* Look for a message in receive queue; wait if necessary */
  916. while (skb_queue_empty(&sk->sk_receive_queue)) {
  917. if (sock->state == SS_DISCONNECTING) {
  918. res = -ENOTCONN;
  919. goto exit;
  920. }
  921. if (timeout <= 0L) {
  922. res = timeout ? timeout : -EWOULDBLOCK;
  923. goto exit;
  924. }
  925. release_sock(sk);
  926. timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
  927. tipc_rx_ready(sock),
  928. timeout);
  929. lock_sock(sk);
  930. }
  931. /* Look at first message in receive queue */
  932. buf = skb_peek(&sk->sk_receive_queue);
  933. msg = buf_msg(buf);
  934. sz = msg_data_sz(msg);
  935. err = msg_errcode(msg);
  936. /* Discard an empty non-errored message & try again */
  937. if ((!sz) && (!err)) {
  938. advance_rx_queue(sk);
  939. goto restart;
  940. }
  941. /* Optionally capture sender's address & ancillary data of first msg */
  942. if (sz_copied == 0) {
  943. set_orig_addr(m, msg);
  944. res = anc_data_recv(m, msg, tport);
  945. if (res)
  946. goto exit;
  947. }
  948. /* Capture message data (if valid) & compute return value (always) */
  949. if (!err) {
  950. u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
  951. sz -= offset;
  952. needed = (buf_len - sz_copied);
  953. sz_to_copy = (sz <= needed) ? sz : needed;
  954. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
  955. m->msg_iov, sz_to_copy);
  956. if (res)
  957. goto exit;
  958. sz_copied += sz_to_copy;
  959. if (sz_to_copy < sz) {
  960. if (!(flags & MSG_PEEK))
  961. TIPC_SKB_CB(buf)->handle =
  962. (void *)(unsigned long)(offset + sz_to_copy);
  963. goto exit;
  964. }
  965. } else {
  966. if (sz_copied != 0)
  967. goto exit; /* can't add error msg to valid data */
  968. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  969. res = 0;
  970. else
  971. res = -ECONNRESET;
  972. }
  973. /* Consume received message (optional) */
  974. if (likely(!(flags & MSG_PEEK))) {
  975. if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  976. tipc_acknowledge(tport->ref, tport->conn_unacked);
  977. advance_rx_queue(sk);
  978. }
  979. /* Loop around if more data is required */
  980. if ((sz_copied < buf_len) && /* didn't get all requested data */
  981. (!skb_queue_empty(&sk->sk_receive_queue) ||
  982. (sz_copied < target)) && /* and more is ready or required */
  983. (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
  984. (!err)) /* and haven't reached a FIN */
  985. goto restart;
  986. exit:
  987. release_sock(sk);
  988. return sz_copied ? sz_copied : res;
  989. }
  990. /**
  991. * tipc_write_space - wake up thread if port congestion is released
  992. * @sk: socket
  993. */
  994. static void tipc_write_space(struct sock *sk)
  995. {
  996. struct socket_wq *wq;
  997. rcu_read_lock();
  998. wq = rcu_dereference(sk->sk_wq);
  999. if (wq_has_sleeper(wq))
  1000. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1001. POLLWRNORM | POLLWRBAND);
  1002. rcu_read_unlock();
  1003. }
  1004. /**
  1005. * tipc_data_ready - wake up threads to indicate messages have been received
  1006. * @sk: socket
  1007. * @len: the length of messages
  1008. */
  1009. static void tipc_data_ready(struct sock *sk, int len)
  1010. {
  1011. struct socket_wq *wq;
  1012. rcu_read_lock();
  1013. wq = rcu_dereference(sk->sk_wq);
  1014. if (wq_has_sleeper(wq))
  1015. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  1016. POLLRDNORM | POLLRDBAND);
  1017. rcu_read_unlock();
  1018. }
  1019. /**
  1020. * filter_connect - Handle all incoming messages for a connection-based socket
  1021. * @tsock: TIPC socket
  1022. * @msg: message
  1023. *
  1024. * Returns TIPC error status code and socket error status code
  1025. * once it encounters some errors
  1026. */
  1027. static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
  1028. {
  1029. struct socket *sock = tsock->sk.sk_socket;
  1030. struct tipc_msg *msg = buf_msg(*buf);
  1031. struct sock *sk = &tsock->sk;
  1032. u32 retval = TIPC_ERR_NO_PORT;
  1033. int res;
  1034. if (msg_mcast(msg))
  1035. return retval;
  1036. switch ((int)sock->state) {
  1037. case SS_CONNECTED:
  1038. /* Accept only connection-based messages sent by peer */
  1039. if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
  1040. if (unlikely(msg_errcode(msg))) {
  1041. sock->state = SS_DISCONNECTING;
  1042. __tipc_disconnect(tsock->p);
  1043. }
  1044. retval = TIPC_OK;
  1045. }
  1046. break;
  1047. case SS_CONNECTING:
  1048. /* Accept only ACK or NACK message */
  1049. if (unlikely(msg_errcode(msg))) {
  1050. sock->state = SS_DISCONNECTING;
  1051. sk->sk_err = -ECONNREFUSED;
  1052. retval = TIPC_OK;
  1053. break;
  1054. }
  1055. if (unlikely(!msg_connected(msg)))
  1056. break;
  1057. res = auto_connect(sock, msg);
  1058. if (res) {
  1059. sock->state = SS_DISCONNECTING;
  1060. sk->sk_err = res;
  1061. retval = TIPC_OK;
  1062. break;
  1063. }
  1064. /* If an incoming message is an 'ACK-', it should be
  1065. * discarded here because it doesn't contain useful
  1066. * data. In addition, we should try to wake up
  1067. * connect() routine if sleeping.
  1068. */
  1069. if (msg_data_sz(msg) == 0) {
  1070. kfree_skb(*buf);
  1071. *buf = NULL;
  1072. if (waitqueue_active(sk_sleep(sk)))
  1073. wake_up_interruptible(sk_sleep(sk));
  1074. }
  1075. retval = TIPC_OK;
  1076. break;
  1077. case SS_LISTENING:
  1078. case SS_UNCONNECTED:
  1079. /* Accept only SYN message */
  1080. if (!msg_connected(msg) && !(msg_errcode(msg)))
  1081. retval = TIPC_OK;
  1082. break;
  1083. case SS_DISCONNECTING:
  1084. break;
  1085. default:
  1086. pr_err("Unknown socket state %u\n", sock->state);
  1087. }
  1088. return retval;
  1089. }
  1090. /**
  1091. * rcvbuf_limit - get proper overload limit of socket receive queue
  1092. * @sk: socket
  1093. * @buf: message
  1094. *
  1095. * For all connection oriented messages, irrespective of importance,
  1096. * the default overload value (i.e. 67MB) is set as limit.
  1097. *
  1098. * For all connectionless messages, by default new queue limits are
  1099. * as belows:
  1100. *
  1101. * TIPC_LOW_IMPORTANCE (4 MB)
  1102. * TIPC_MEDIUM_IMPORTANCE (8 MB)
  1103. * TIPC_HIGH_IMPORTANCE (16 MB)
  1104. * TIPC_CRITICAL_IMPORTANCE (32 MB)
  1105. *
  1106. * Returns overload limit according to corresponding message importance
  1107. */
  1108. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
  1109. {
  1110. struct tipc_msg *msg = buf_msg(buf);
  1111. unsigned int limit;
  1112. if (msg_connected(msg))
  1113. limit = sysctl_tipc_rmem[2];
  1114. else
  1115. limit = sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
  1116. msg_importance(msg);
  1117. return limit;
  1118. }
  1119. /**
  1120. * filter_rcv - validate incoming message
  1121. * @sk: socket
  1122. * @buf: message
  1123. *
  1124. * Enqueues message on receive queue if acceptable; optionally handles
  1125. * disconnect indication for a connected socket.
  1126. *
  1127. * Called with socket lock already taken; port lock may also be taken.
  1128. *
  1129. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  1130. */
  1131. static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
  1132. {
  1133. struct socket *sock = sk->sk_socket;
  1134. struct tipc_msg *msg = buf_msg(buf);
  1135. unsigned int limit = rcvbuf_limit(sk, buf);
  1136. u32 res = TIPC_OK;
  1137. /* Reject message if it is wrong sort of message for socket */
  1138. if (msg_type(msg) > TIPC_DIRECT_MSG)
  1139. return TIPC_ERR_NO_PORT;
  1140. if (sock->state == SS_READY) {
  1141. if (msg_connected(msg))
  1142. return TIPC_ERR_NO_PORT;
  1143. } else {
  1144. res = filter_connect(tipc_sk(sk), &buf);
  1145. if (res != TIPC_OK || buf == NULL)
  1146. return res;
  1147. }
  1148. /* Reject message if there isn't room to queue it */
  1149. if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
  1150. return TIPC_ERR_OVERLOAD;
  1151. /* Enqueue message */
  1152. TIPC_SKB_CB(buf)->handle = 0;
  1153. __skb_queue_tail(&sk->sk_receive_queue, buf);
  1154. skb_set_owner_r(buf, sk);
  1155. sk->sk_data_ready(sk, 0);
  1156. return TIPC_OK;
  1157. }
  1158. /**
  1159. * backlog_rcv - handle incoming message from backlog queue
  1160. * @sk: socket
  1161. * @buf: message
  1162. *
  1163. * Caller must hold socket lock, but not port lock.
  1164. *
  1165. * Returns 0
  1166. */
  1167. static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
  1168. {
  1169. u32 res;
  1170. res = filter_rcv(sk, buf);
  1171. if (res)
  1172. tipc_reject_msg(buf, res);
  1173. return 0;
  1174. }
  1175. /**
  1176. * dispatch - handle incoming message
  1177. * @tport: TIPC port that received message
  1178. * @buf: message
  1179. *
  1180. * Called with port lock already taken.
  1181. *
  1182. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  1183. */
  1184. static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
  1185. {
  1186. struct sock *sk = (struct sock *)tport->usr_handle;
  1187. u32 res;
  1188. /*
  1189. * Process message if socket is unlocked; otherwise add to backlog queue
  1190. *
  1191. * This code is based on sk_receive_skb(), but must be distinct from it
  1192. * since a TIPC-specific filter/reject mechanism is utilized
  1193. */
  1194. bh_lock_sock(sk);
  1195. if (!sock_owned_by_user(sk)) {
  1196. res = filter_rcv(sk, buf);
  1197. } else {
  1198. if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
  1199. res = TIPC_ERR_OVERLOAD;
  1200. else
  1201. res = TIPC_OK;
  1202. }
  1203. bh_unlock_sock(sk);
  1204. return res;
  1205. }
  1206. /**
  1207. * wakeupdispatch - wake up port after congestion
  1208. * @tport: port to wakeup
  1209. *
  1210. * Called with port lock already taken.
  1211. */
  1212. static void wakeupdispatch(struct tipc_port *tport)
  1213. {
  1214. struct sock *sk = (struct sock *)tport->usr_handle;
  1215. sk->sk_write_space(sk);
  1216. }
  1217. /**
  1218. * connect - establish a connection to another TIPC port
  1219. * @sock: socket structure
  1220. * @dest: socket address for destination port
  1221. * @destlen: size of socket address data structure
  1222. * @flags: file-related flags associated with socket
  1223. *
  1224. * Returns 0 on success, errno otherwise
  1225. */
  1226. static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
  1227. int flags)
  1228. {
  1229. struct sock *sk = sock->sk;
  1230. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1231. struct msghdr m = {NULL,};
  1232. unsigned int timeout;
  1233. int res;
  1234. lock_sock(sk);
  1235. /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
  1236. if (sock->state == SS_READY) {
  1237. res = -EOPNOTSUPP;
  1238. goto exit;
  1239. }
  1240. /*
  1241. * Reject connection attempt using multicast address
  1242. *
  1243. * Note: send_msg() validates the rest of the address fields,
  1244. * so there's no need to do it here
  1245. */
  1246. if (dst->addrtype == TIPC_ADDR_MCAST) {
  1247. res = -EINVAL;
  1248. goto exit;
  1249. }
  1250. timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
  1251. switch (sock->state) {
  1252. case SS_UNCONNECTED:
  1253. /* Send a 'SYN-' to destination */
  1254. m.msg_name = dest;
  1255. m.msg_namelen = destlen;
  1256. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  1257. * indicate send_msg() is never blocked.
  1258. */
  1259. if (!timeout)
  1260. m.msg_flags = MSG_DONTWAIT;
  1261. res = send_msg(NULL, sock, &m, 0);
  1262. if ((res < 0) && (res != -EWOULDBLOCK))
  1263. goto exit;
  1264. /* Just entered SS_CONNECTING state; the only
  1265. * difference is that return value in non-blocking
  1266. * case is EINPROGRESS, rather than EALREADY.
  1267. */
  1268. res = -EINPROGRESS;
  1269. break;
  1270. case SS_CONNECTING:
  1271. res = -EALREADY;
  1272. break;
  1273. case SS_CONNECTED:
  1274. res = -EISCONN;
  1275. break;
  1276. default:
  1277. res = -EINVAL;
  1278. goto exit;
  1279. }
  1280. if (sock->state == SS_CONNECTING) {
  1281. if (!timeout)
  1282. goto exit;
  1283. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  1284. release_sock(sk);
  1285. res = wait_event_interruptible_timeout(*sk_sleep(sk),
  1286. sock->state != SS_CONNECTING,
  1287. timeout ? (long)msecs_to_jiffies(timeout)
  1288. : MAX_SCHEDULE_TIMEOUT);
  1289. lock_sock(sk);
  1290. if (res <= 0) {
  1291. if (res == 0)
  1292. res = -ETIMEDOUT;
  1293. else
  1294. ; /* leave "res" unchanged */
  1295. goto exit;
  1296. }
  1297. }
  1298. if (unlikely(sock->state == SS_DISCONNECTING))
  1299. res = sock_error(sk);
  1300. else
  1301. res = 0;
  1302. exit:
  1303. release_sock(sk);
  1304. return res;
  1305. }
  1306. /**
  1307. * listen - allow socket to listen for incoming connections
  1308. * @sock: socket structure
  1309. * @len: (unused)
  1310. *
  1311. * Returns 0 on success, errno otherwise
  1312. */
  1313. static int listen(struct socket *sock, int len)
  1314. {
  1315. struct sock *sk = sock->sk;
  1316. int res;
  1317. lock_sock(sk);
  1318. if (sock->state != SS_UNCONNECTED)
  1319. res = -EINVAL;
  1320. else {
  1321. sock->state = SS_LISTENING;
  1322. res = 0;
  1323. }
  1324. release_sock(sk);
  1325. return res;
  1326. }
  1327. /**
  1328. * accept - wait for connection request
  1329. * @sock: listening socket
  1330. * @newsock: new socket that is to be connected
  1331. * @flags: file-related flags associated with socket
  1332. *
  1333. * Returns 0 on success, errno otherwise
  1334. */
  1335. static int accept(struct socket *sock, struct socket *new_sock, int flags)
  1336. {
  1337. struct sock *new_sk, *sk = sock->sk;
  1338. struct sk_buff *buf;
  1339. struct tipc_sock *new_tsock;
  1340. struct tipc_port *new_tport;
  1341. struct tipc_msg *msg;
  1342. u32 new_ref;
  1343. int res;
  1344. lock_sock(sk);
  1345. if (sock->state != SS_LISTENING) {
  1346. res = -EINVAL;
  1347. goto exit;
  1348. }
  1349. while (skb_queue_empty(&sk->sk_receive_queue)) {
  1350. if (flags & O_NONBLOCK) {
  1351. res = -EWOULDBLOCK;
  1352. goto exit;
  1353. }
  1354. release_sock(sk);
  1355. res = wait_event_interruptible(*sk_sleep(sk),
  1356. (!skb_queue_empty(&sk->sk_receive_queue)));
  1357. lock_sock(sk);
  1358. if (res)
  1359. goto exit;
  1360. }
  1361. buf = skb_peek(&sk->sk_receive_queue);
  1362. res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
  1363. if (res)
  1364. goto exit;
  1365. new_sk = new_sock->sk;
  1366. new_tsock = tipc_sk(new_sk);
  1367. new_tport = new_tsock->p;
  1368. new_ref = new_tport->ref;
  1369. msg = buf_msg(buf);
  1370. /* we lock on new_sk; but lockdep sees the lock on sk */
  1371. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  1372. /*
  1373. * Reject any stray messages received by new socket
  1374. * before the socket lock was taken (very, very unlikely)
  1375. */
  1376. reject_rx_queue(new_sk);
  1377. /* Connect new socket to it's peer */
  1378. new_tsock->peer_name.ref = msg_origport(msg);
  1379. new_tsock->peer_name.node = msg_orignode(msg);
  1380. tipc_connect(new_ref, &new_tsock->peer_name);
  1381. new_sock->state = SS_CONNECTED;
  1382. tipc_set_portimportance(new_ref, msg_importance(msg));
  1383. if (msg_named(msg)) {
  1384. new_tport->conn_type = msg_nametype(msg);
  1385. new_tport->conn_instance = msg_nameinst(msg);
  1386. }
  1387. /*
  1388. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1389. * Respond to 'SYN+' by queuing it on new socket.
  1390. */
  1391. if (!msg_data_sz(msg)) {
  1392. struct msghdr m = {NULL,};
  1393. advance_rx_queue(sk);
  1394. send_packet(NULL, new_sock, &m, 0);
  1395. } else {
  1396. __skb_dequeue(&sk->sk_receive_queue);
  1397. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  1398. skb_set_owner_r(buf, new_sk);
  1399. }
  1400. release_sock(new_sk);
  1401. exit:
  1402. release_sock(sk);
  1403. return res;
  1404. }
  1405. /**
  1406. * shutdown - shutdown socket connection
  1407. * @sock: socket structure
  1408. * @how: direction to close (must be SHUT_RDWR)
  1409. *
  1410. * Terminates connection (if necessary), then purges socket's receive queue.
  1411. *
  1412. * Returns 0 on success, errno otherwise
  1413. */
  1414. static int shutdown(struct socket *sock, int how)
  1415. {
  1416. struct sock *sk = sock->sk;
  1417. struct tipc_port *tport = tipc_sk_port(sk);
  1418. struct sk_buff *buf;
  1419. int res;
  1420. if (how != SHUT_RDWR)
  1421. return -EINVAL;
  1422. lock_sock(sk);
  1423. switch (sock->state) {
  1424. case SS_CONNECTING:
  1425. case SS_CONNECTED:
  1426. restart:
  1427. /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
  1428. buf = __skb_dequeue(&sk->sk_receive_queue);
  1429. if (buf) {
  1430. if (TIPC_SKB_CB(buf)->handle != 0) {
  1431. kfree_skb(buf);
  1432. goto restart;
  1433. }
  1434. tipc_disconnect(tport->ref);
  1435. tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
  1436. } else {
  1437. tipc_shutdown(tport->ref);
  1438. }
  1439. sock->state = SS_DISCONNECTING;
  1440. /* fall through */
  1441. case SS_DISCONNECTING:
  1442. /* Discard any unreceived messages */
  1443. __skb_queue_purge(&sk->sk_receive_queue);
  1444. /* Wake up anyone sleeping in poll */
  1445. sk->sk_state_change(sk);
  1446. res = 0;
  1447. break;
  1448. default:
  1449. res = -ENOTCONN;
  1450. }
  1451. release_sock(sk);
  1452. return res;
  1453. }
  1454. /**
  1455. * setsockopt - set socket option
  1456. * @sock: socket structure
  1457. * @lvl: option level
  1458. * @opt: option identifier
  1459. * @ov: pointer to new option value
  1460. * @ol: length of option value
  1461. *
  1462. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  1463. * (to ease compatibility).
  1464. *
  1465. * Returns 0 on success, errno otherwise
  1466. */
  1467. static int setsockopt(struct socket *sock,
  1468. int lvl, int opt, char __user *ov, unsigned int ol)
  1469. {
  1470. struct sock *sk = sock->sk;
  1471. struct tipc_port *tport = tipc_sk_port(sk);
  1472. u32 value;
  1473. int res;
  1474. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1475. return 0;
  1476. if (lvl != SOL_TIPC)
  1477. return -ENOPROTOOPT;
  1478. if (ol < sizeof(value))
  1479. return -EINVAL;
  1480. res = get_user(value, (u32 __user *)ov);
  1481. if (res)
  1482. return res;
  1483. lock_sock(sk);
  1484. switch (opt) {
  1485. case TIPC_IMPORTANCE:
  1486. res = tipc_set_portimportance(tport->ref, value);
  1487. break;
  1488. case TIPC_SRC_DROPPABLE:
  1489. if (sock->type != SOCK_STREAM)
  1490. res = tipc_set_portunreliable(tport->ref, value);
  1491. else
  1492. res = -ENOPROTOOPT;
  1493. break;
  1494. case TIPC_DEST_DROPPABLE:
  1495. res = tipc_set_portunreturnable(tport->ref, value);
  1496. break;
  1497. case TIPC_CONN_TIMEOUT:
  1498. tipc_sk(sk)->conn_timeout = value;
  1499. /* no need to set "res", since already 0 at this point */
  1500. break;
  1501. default:
  1502. res = -EINVAL;
  1503. }
  1504. release_sock(sk);
  1505. return res;
  1506. }
  1507. /**
  1508. * getsockopt - get socket option
  1509. * @sock: socket structure
  1510. * @lvl: option level
  1511. * @opt: option identifier
  1512. * @ov: receptacle for option value
  1513. * @ol: receptacle for length of option value
  1514. *
  1515. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  1516. * (to ease compatibility).
  1517. *
  1518. * Returns 0 on success, errno otherwise
  1519. */
  1520. static int getsockopt(struct socket *sock,
  1521. int lvl, int opt, char __user *ov, int __user *ol)
  1522. {
  1523. struct sock *sk = sock->sk;
  1524. struct tipc_port *tport = tipc_sk_port(sk);
  1525. int len;
  1526. u32 value;
  1527. int res;
  1528. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1529. return put_user(0, ol);
  1530. if (lvl != SOL_TIPC)
  1531. return -ENOPROTOOPT;
  1532. res = get_user(len, ol);
  1533. if (res)
  1534. return res;
  1535. lock_sock(sk);
  1536. switch (opt) {
  1537. case TIPC_IMPORTANCE:
  1538. res = tipc_portimportance(tport->ref, &value);
  1539. break;
  1540. case TIPC_SRC_DROPPABLE:
  1541. res = tipc_portunreliable(tport->ref, &value);
  1542. break;
  1543. case TIPC_DEST_DROPPABLE:
  1544. res = tipc_portunreturnable(tport->ref, &value);
  1545. break;
  1546. case TIPC_CONN_TIMEOUT:
  1547. value = tipc_sk(sk)->conn_timeout;
  1548. /* no need to set "res", since already 0 at this point */
  1549. break;
  1550. case TIPC_NODE_RECVQ_DEPTH:
  1551. value = 0; /* was tipc_queue_size, now obsolete */
  1552. break;
  1553. case TIPC_SOCK_RECVQ_DEPTH:
  1554. value = skb_queue_len(&sk->sk_receive_queue);
  1555. break;
  1556. default:
  1557. res = -EINVAL;
  1558. }
  1559. release_sock(sk);
  1560. if (res)
  1561. return res; /* "get" failed */
  1562. if (len < sizeof(value))
  1563. return -EINVAL;
  1564. if (copy_to_user(ov, &value, sizeof(value)))
  1565. return -EFAULT;
  1566. return put_user(sizeof(value), ol);
  1567. }
  1568. /* Protocol switches for the various types of TIPC sockets */
  1569. static const struct proto_ops msg_ops = {
  1570. .owner = THIS_MODULE,
  1571. .family = AF_TIPC,
  1572. .release = release,
  1573. .bind = bind,
  1574. .connect = connect,
  1575. .socketpair = sock_no_socketpair,
  1576. .accept = sock_no_accept,
  1577. .getname = get_name,
  1578. .poll = poll,
  1579. .ioctl = sock_no_ioctl,
  1580. .listen = sock_no_listen,
  1581. .shutdown = shutdown,
  1582. .setsockopt = setsockopt,
  1583. .getsockopt = getsockopt,
  1584. .sendmsg = send_msg,
  1585. .recvmsg = recv_msg,
  1586. .mmap = sock_no_mmap,
  1587. .sendpage = sock_no_sendpage
  1588. };
  1589. static const struct proto_ops packet_ops = {
  1590. .owner = THIS_MODULE,
  1591. .family = AF_TIPC,
  1592. .release = release,
  1593. .bind = bind,
  1594. .connect = connect,
  1595. .socketpair = sock_no_socketpair,
  1596. .accept = accept,
  1597. .getname = get_name,
  1598. .poll = poll,
  1599. .ioctl = sock_no_ioctl,
  1600. .listen = listen,
  1601. .shutdown = shutdown,
  1602. .setsockopt = setsockopt,
  1603. .getsockopt = getsockopt,
  1604. .sendmsg = send_packet,
  1605. .recvmsg = recv_msg,
  1606. .mmap = sock_no_mmap,
  1607. .sendpage = sock_no_sendpage
  1608. };
  1609. static const struct proto_ops stream_ops = {
  1610. .owner = THIS_MODULE,
  1611. .family = AF_TIPC,
  1612. .release = release,
  1613. .bind = bind,
  1614. .connect = connect,
  1615. .socketpair = sock_no_socketpair,
  1616. .accept = accept,
  1617. .getname = get_name,
  1618. .poll = poll,
  1619. .ioctl = sock_no_ioctl,
  1620. .listen = listen,
  1621. .shutdown = shutdown,
  1622. .setsockopt = setsockopt,
  1623. .getsockopt = getsockopt,
  1624. .sendmsg = send_stream,
  1625. .recvmsg = recv_stream,
  1626. .mmap = sock_no_mmap,
  1627. .sendpage = sock_no_sendpage
  1628. };
  1629. static const struct net_proto_family tipc_family_ops = {
  1630. .owner = THIS_MODULE,
  1631. .family = AF_TIPC,
  1632. .create = tipc_create
  1633. };
  1634. static struct proto tipc_proto = {
  1635. .name = "TIPC",
  1636. .owner = THIS_MODULE,
  1637. .obj_size = sizeof(struct tipc_sock),
  1638. .sysctl_rmem = sysctl_tipc_rmem
  1639. };
  1640. /**
  1641. * tipc_socket_init - initialize TIPC socket interface
  1642. *
  1643. * Returns 0 on success, errno otherwise
  1644. */
  1645. int tipc_socket_init(void)
  1646. {
  1647. int res;
  1648. res = proto_register(&tipc_proto, 1);
  1649. if (res) {
  1650. pr_err("Failed to register TIPC protocol type\n");
  1651. goto out;
  1652. }
  1653. res = sock_register(&tipc_family_ops);
  1654. if (res) {
  1655. pr_err("Failed to register TIPC socket type\n");
  1656. proto_unregister(&tipc_proto);
  1657. goto out;
  1658. }
  1659. sockets_enabled = 1;
  1660. out:
  1661. return res;
  1662. }
  1663. /**
  1664. * tipc_socket_stop - stop TIPC socket interface
  1665. */
  1666. void tipc_socket_stop(void)
  1667. {
  1668. if (!sockets_enabled)
  1669. return;
  1670. sockets_enabled = 0;
  1671. sock_unregister(tipc_family_ops.family);
  1672. proto_unregister(&tipc_proto);
  1673. }