socket.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208
  1. /*
  2. * net/tipc/socket.c: TIPC socket API
  3. *
  4. * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
  5. * Copyright (c) 2004-2008, 2010-2013, 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 "name_table.h"
  39. #include "node.h"
  40. #include "link.h"
  41. #include <linux/export.h>
  42. #define SS_LISTENING -1 /* socket is listening */
  43. #define SS_READY -2 /* socket is connectionless */
  44. #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
  45. #define TIPC_FWD_MSG 1
  46. static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
  47. static void tipc_data_ready(struct sock *sk);
  48. static void tipc_write_space(struct sock *sk);
  49. static int tipc_release(struct socket *sock);
  50. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
  51. static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
  52. static const struct proto_ops packet_ops;
  53. static const struct proto_ops stream_ops;
  54. static const struct proto_ops msg_ops;
  55. static struct proto tipc_proto;
  56. static struct proto tipc_proto_kern;
  57. /*
  58. * Revised TIPC socket locking policy:
  59. *
  60. * Most socket operations take the standard socket lock when they start
  61. * and hold it until they finish (or until they need to sleep). Acquiring
  62. * this lock grants the owner exclusive access to the fields of the socket
  63. * data structures, with the exception of the backlog queue. A few socket
  64. * operations can be done without taking the socket lock because they only
  65. * read socket information that never changes during the life of the socket.
  66. *
  67. * Socket operations may acquire the lock for the associated TIPC port if they
  68. * need to perform an operation on the port. If any routine needs to acquire
  69. * both the socket lock and the port lock it must take the socket lock first
  70. * to avoid the risk of deadlock.
  71. *
  72. * The dispatcher handling incoming messages cannot grab the socket lock in
  73. * the standard fashion, since invoked it runs at the BH level and cannot block.
  74. * Instead, it checks to see if the socket lock is currently owned by someone,
  75. * and either handles the message itself or adds it to the socket's backlog
  76. * queue; in the latter case the queued message is processed once the process
  77. * owning the socket lock releases it.
  78. *
  79. * NOTE: Releasing the socket lock while an operation is sleeping overcomes
  80. * the problem of a blocked socket operation preventing any other operations
  81. * from occurring. However, applications must be careful if they have
  82. * multiple threads trying to send (or receive) on the same socket, as these
  83. * operations might interfere with each other. For example, doing a connect
  84. * and a receive at the same time might allow the receive to consume the
  85. * ACK message meant for the connect. While additional work could be done
  86. * to try and overcome this, it doesn't seem to be worthwhile at the present.
  87. *
  88. * NOTE: Releasing the socket lock while an operation is sleeping also ensures
  89. * that another operation that must be performed in a non-blocking manner is
  90. * not delayed for very long because the lock has already been taken.
  91. *
  92. * NOTE: This code assumes that certain fields of a port/socket pair are
  93. * constant over its lifetime; such fields can be examined without taking
  94. * the socket lock and/or port lock, and do not need to be re-read even
  95. * after resuming processing after waiting. These fields include:
  96. * - socket type
  97. * - pointer to socket sk structure (aka tipc_sock structure)
  98. * - pointer to port structure
  99. * - port reference
  100. */
  101. #include "socket.h"
  102. /**
  103. * advance_rx_queue - discard first buffer in socket receive queue
  104. *
  105. * Caller must hold socket lock
  106. */
  107. static void advance_rx_queue(struct sock *sk)
  108. {
  109. kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
  110. }
  111. /**
  112. * reject_rx_queue - reject all buffers in socket receive queue
  113. *
  114. * Caller must hold socket lock
  115. */
  116. static void reject_rx_queue(struct sock *sk)
  117. {
  118. struct sk_buff *buf;
  119. u32 dnode;
  120. while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
  121. if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
  122. tipc_link_xmit(buf, dnode, 0);
  123. }
  124. }
  125. /**
  126. * tipc_sk_create - create a TIPC socket
  127. * @net: network namespace (must be default network)
  128. * @sock: pre-allocated socket structure
  129. * @protocol: protocol indicator (must be 0)
  130. * @kern: caused by kernel or by userspace?
  131. *
  132. * This routine creates additional data structures used by the TIPC socket,
  133. * initializes them, and links them together.
  134. *
  135. * Returns 0 on success, errno otherwise
  136. */
  137. static int tipc_sk_create(struct net *net, struct socket *sock,
  138. int protocol, int kern)
  139. {
  140. const struct proto_ops *ops;
  141. socket_state state;
  142. struct sock *sk;
  143. struct tipc_sock *tsk;
  144. struct tipc_port *port;
  145. u32 ref;
  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. if (!kern)
  168. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
  169. else
  170. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
  171. if (sk == NULL)
  172. return -ENOMEM;
  173. tsk = tipc_sk(sk);
  174. port = &tsk->port;
  175. ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE);
  176. if (!ref) {
  177. pr_warn("Socket registration failed, ref. table exhausted\n");
  178. sk_free(sk);
  179. return -ENOMEM;
  180. }
  181. /* Finish initializing socket data structures */
  182. sock->ops = ops;
  183. sock->state = state;
  184. sock_init_data(sock, sk);
  185. sk->sk_backlog_rcv = tipc_backlog_rcv;
  186. sk->sk_rcvbuf = sysctl_tipc_rmem[1];
  187. sk->sk_data_ready = tipc_data_ready;
  188. sk->sk_write_space = tipc_write_space;
  189. tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
  190. tsk->sent_unacked = 0;
  191. atomic_set(&tsk->dupl_rcvcnt, 0);
  192. tipc_port_unlock(port);
  193. if (sock->state == SS_READY) {
  194. tipc_port_set_unreturnable(port, true);
  195. if (sock->type == SOCK_DGRAM)
  196. tipc_port_set_unreliable(port, true);
  197. }
  198. return 0;
  199. }
  200. /**
  201. * tipc_sock_create_local - create TIPC socket from inside TIPC module
  202. * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
  203. *
  204. * We cannot use sock_creat_kern here because it bumps module user count.
  205. * Since socket owner and creator is the same module we must make sure
  206. * that module count remains zero for module local sockets, otherwise
  207. * we cannot do rmmod.
  208. *
  209. * Returns 0 on success, errno otherwise
  210. */
  211. int tipc_sock_create_local(int type, struct socket **res)
  212. {
  213. int rc;
  214. rc = sock_create_lite(AF_TIPC, type, 0, res);
  215. if (rc < 0) {
  216. pr_err("Failed to create kernel socket\n");
  217. return rc;
  218. }
  219. tipc_sk_create(&init_net, *res, 0, 1);
  220. return 0;
  221. }
  222. /**
  223. * tipc_sock_release_local - release socket created by tipc_sock_create_local
  224. * @sock: the socket to be released.
  225. *
  226. * Module reference count is not incremented when such sockets are created,
  227. * so we must keep it from being decremented when they are released.
  228. */
  229. void tipc_sock_release_local(struct socket *sock)
  230. {
  231. tipc_release(sock);
  232. sock->ops = NULL;
  233. sock_release(sock);
  234. }
  235. /**
  236. * tipc_sock_accept_local - accept a connection on a socket created
  237. * with tipc_sock_create_local. Use this function to avoid that
  238. * module reference count is inadvertently incremented.
  239. *
  240. * @sock: the accepting socket
  241. * @newsock: reference to the new socket to be created
  242. * @flags: socket flags
  243. */
  244. int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
  245. int flags)
  246. {
  247. struct sock *sk = sock->sk;
  248. int ret;
  249. ret = sock_create_lite(sk->sk_family, sk->sk_type,
  250. sk->sk_protocol, newsock);
  251. if (ret < 0)
  252. return ret;
  253. ret = tipc_accept(sock, *newsock, flags);
  254. if (ret < 0) {
  255. sock_release(*newsock);
  256. return ret;
  257. }
  258. (*newsock)->ops = sock->ops;
  259. return ret;
  260. }
  261. /**
  262. * tipc_release - destroy a TIPC socket
  263. * @sock: socket to destroy
  264. *
  265. * This routine cleans up any messages that are still queued on the socket.
  266. * For DGRAM and RDM socket types, all queued messages are rejected.
  267. * For SEQPACKET and STREAM socket types, the first message is rejected
  268. * and any others are discarded. (If the first message on a STREAM socket
  269. * is partially-read, it is discarded and the next one is rejected instead.)
  270. *
  271. * NOTE: Rejected messages are not necessarily returned to the sender! They
  272. * are returned or discarded according to the "destination droppable" setting
  273. * specified for the message by the sender.
  274. *
  275. * Returns 0 on success, errno otherwise
  276. */
  277. static int tipc_release(struct socket *sock)
  278. {
  279. struct sock *sk = sock->sk;
  280. struct tipc_sock *tsk;
  281. struct tipc_port *port;
  282. struct sk_buff *buf;
  283. u32 dnode;
  284. /*
  285. * Exit if socket isn't fully initialized (occurs when a failed accept()
  286. * releases a pre-allocated child socket that was never used)
  287. */
  288. if (sk == NULL)
  289. return 0;
  290. tsk = tipc_sk(sk);
  291. port = &tsk->port;
  292. lock_sock(sk);
  293. /*
  294. * Reject all unreceived messages, except on an active connection
  295. * (which disconnects locally & sends a 'FIN+' to peer)
  296. */
  297. while (sock->state != SS_DISCONNECTING) {
  298. buf = __skb_dequeue(&sk->sk_receive_queue);
  299. if (buf == NULL)
  300. break;
  301. if (TIPC_SKB_CB(buf)->handle != NULL)
  302. kfree_skb(buf);
  303. else {
  304. if ((sock->state == SS_CONNECTING) ||
  305. (sock->state == SS_CONNECTED)) {
  306. sock->state = SS_DISCONNECTING;
  307. tipc_port_disconnect(port->ref);
  308. }
  309. if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
  310. tipc_link_xmit(buf, dnode, 0);
  311. }
  312. }
  313. /* Destroy TIPC port; also disconnects an active connection and
  314. * sends a 'FIN-' to peer.
  315. */
  316. tipc_port_destroy(port);
  317. /* Discard any remaining (connection-based) messages in receive queue */
  318. __skb_queue_purge(&sk->sk_receive_queue);
  319. /* Reject any messages that accumulated in backlog queue */
  320. sock->state = SS_DISCONNECTING;
  321. release_sock(sk);
  322. sock_put(sk);
  323. sock->sk = NULL;
  324. return 0;
  325. }
  326. /**
  327. * tipc_bind - associate or disassocate TIPC name(s) with a socket
  328. * @sock: socket structure
  329. * @uaddr: socket address describing name(s) and desired operation
  330. * @uaddr_len: size of socket address data structure
  331. *
  332. * Name and name sequence binding is indicated using a positive scope value;
  333. * a negative scope value unbinds the specified name. Specifying no name
  334. * (i.e. a socket address length of 0) unbinds all names from the socket.
  335. *
  336. * Returns 0 on success, errno otherwise
  337. *
  338. * NOTE: This routine doesn't need to take the socket lock since it doesn't
  339. * access any non-constant socket information.
  340. */
  341. static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
  342. int uaddr_len)
  343. {
  344. struct sock *sk = sock->sk;
  345. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  346. struct tipc_sock *tsk = tipc_sk(sk);
  347. int res = -EINVAL;
  348. lock_sock(sk);
  349. if (unlikely(!uaddr_len)) {
  350. res = tipc_withdraw(&tsk->port, 0, NULL);
  351. goto exit;
  352. }
  353. if (uaddr_len < sizeof(struct sockaddr_tipc)) {
  354. res = -EINVAL;
  355. goto exit;
  356. }
  357. if (addr->family != AF_TIPC) {
  358. res = -EAFNOSUPPORT;
  359. goto exit;
  360. }
  361. if (addr->addrtype == TIPC_ADDR_NAME)
  362. addr->addr.nameseq.upper = addr->addr.nameseq.lower;
  363. else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
  364. res = -EAFNOSUPPORT;
  365. goto exit;
  366. }
  367. if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
  368. (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
  369. (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
  370. res = -EACCES;
  371. goto exit;
  372. }
  373. res = (addr->scope > 0) ?
  374. tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
  375. tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
  376. exit:
  377. release_sock(sk);
  378. return res;
  379. }
  380. /**
  381. * tipc_getname - get port ID of socket or peer socket
  382. * @sock: socket structure
  383. * @uaddr: area for returned socket address
  384. * @uaddr_len: area for returned length of socket address
  385. * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
  386. *
  387. * Returns 0 on success, errno otherwise
  388. *
  389. * NOTE: This routine doesn't need to take the socket lock since it only
  390. * accesses socket information that is unchanging (or which changes in
  391. * a completely predictable manner).
  392. */
  393. static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
  394. int *uaddr_len, int peer)
  395. {
  396. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  397. struct tipc_sock *tsk = tipc_sk(sock->sk);
  398. memset(addr, 0, sizeof(*addr));
  399. if (peer) {
  400. if ((sock->state != SS_CONNECTED) &&
  401. ((peer != 2) || (sock->state != SS_DISCONNECTING)))
  402. return -ENOTCONN;
  403. addr->addr.id.ref = tipc_port_peerport(&tsk->port);
  404. addr->addr.id.node = tipc_port_peernode(&tsk->port);
  405. } else {
  406. addr->addr.id.ref = tsk->port.ref;
  407. addr->addr.id.node = tipc_own_addr;
  408. }
  409. *uaddr_len = sizeof(*addr);
  410. addr->addrtype = TIPC_ADDR_ID;
  411. addr->family = AF_TIPC;
  412. addr->scope = 0;
  413. addr->addr.name.domain = 0;
  414. return 0;
  415. }
  416. /**
  417. * tipc_poll - read and possibly block on pollmask
  418. * @file: file structure associated with the socket
  419. * @sock: socket for which to calculate the poll bits
  420. * @wait: ???
  421. *
  422. * Returns pollmask value
  423. *
  424. * COMMENTARY:
  425. * It appears that the usual socket locking mechanisms are not useful here
  426. * since the pollmask info is potentially out-of-date the moment this routine
  427. * exits. TCP and other protocols seem to rely on higher level poll routines
  428. * to handle any preventable race conditions, so TIPC will do the same ...
  429. *
  430. * TIPC sets the returned events as follows:
  431. *
  432. * socket state flags set
  433. * ------------ ---------
  434. * unconnected no read flags
  435. * POLLOUT if port is not congested
  436. *
  437. * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
  438. * no write flags
  439. *
  440. * connected POLLIN/POLLRDNORM if data in rx queue
  441. * POLLOUT if port is not congested
  442. *
  443. * disconnecting POLLIN/POLLRDNORM/POLLHUP
  444. * no write flags
  445. *
  446. * listening POLLIN if SYN in rx queue
  447. * no write flags
  448. *
  449. * ready POLLIN/POLLRDNORM if data in rx queue
  450. * [connectionless] POLLOUT (since port cannot be congested)
  451. *
  452. * IMPORTANT: The fact that a read or write operation is indicated does NOT
  453. * imply that the operation will succeed, merely that it should be performed
  454. * and will not block.
  455. */
  456. static unsigned int tipc_poll(struct file *file, struct socket *sock,
  457. poll_table *wait)
  458. {
  459. struct sock *sk = sock->sk;
  460. struct tipc_sock *tsk = tipc_sk(sk);
  461. u32 mask = 0;
  462. sock_poll_wait(file, sk_sleep(sk), wait);
  463. switch ((int)sock->state) {
  464. case SS_UNCONNECTED:
  465. if (!tsk->link_cong)
  466. mask |= POLLOUT;
  467. break;
  468. case SS_READY:
  469. case SS_CONNECTED:
  470. if (!tsk->link_cong && !tipc_sk_conn_cong(tsk))
  471. mask |= POLLOUT;
  472. /* fall thru' */
  473. case SS_CONNECTING:
  474. case SS_LISTENING:
  475. if (!skb_queue_empty(&sk->sk_receive_queue))
  476. mask |= (POLLIN | POLLRDNORM);
  477. break;
  478. case SS_DISCONNECTING:
  479. mask = (POLLIN | POLLRDNORM | POLLHUP);
  480. break;
  481. }
  482. return mask;
  483. }
  484. /**
  485. * tipc_sendmcast - send multicast message
  486. * @sock: socket structure
  487. * @seq: destination address
  488. * @iov: message data to send
  489. * @dsz: total length of message data
  490. * @timeo: timeout to wait for wakeup
  491. *
  492. * Called from function tipc_sendmsg(), which has done all sanity checks
  493. * Returns the number of bytes sent on success, or errno
  494. */
  495. static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
  496. struct iovec *iov, size_t dsz, long timeo)
  497. {
  498. struct sock *sk = sock->sk;
  499. struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr;
  500. struct sk_buff *buf;
  501. uint mtu;
  502. int rc;
  503. msg_set_type(mhdr, TIPC_MCAST_MSG);
  504. msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
  505. msg_set_destport(mhdr, 0);
  506. msg_set_destnode(mhdr, 0);
  507. msg_set_nametype(mhdr, seq->type);
  508. msg_set_namelower(mhdr, seq->lower);
  509. msg_set_nameupper(mhdr, seq->upper);
  510. msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
  511. new_mtu:
  512. mtu = tipc_bclink_get_mtu();
  513. rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
  514. if (unlikely(rc < 0))
  515. return rc;
  516. do {
  517. rc = tipc_bclink_xmit(buf);
  518. if (likely(rc >= 0)) {
  519. rc = dsz;
  520. break;
  521. }
  522. if (rc == -EMSGSIZE)
  523. goto new_mtu;
  524. if (rc != -ELINKCONG)
  525. break;
  526. rc = tipc_wait_for_sndmsg(sock, &timeo);
  527. if (rc)
  528. kfree_skb_list(buf);
  529. } while (!rc);
  530. return rc;
  531. }
  532. /* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
  533. */
  534. void tipc_sk_mcast_rcv(struct sk_buff *buf)
  535. {
  536. struct tipc_msg *msg = buf_msg(buf);
  537. struct tipc_port_list dports = {0, NULL, };
  538. struct tipc_port_list *item;
  539. struct sk_buff *b;
  540. uint i, last, dst = 0;
  541. u32 scope = TIPC_CLUSTER_SCOPE;
  542. if (in_own_node(msg_orignode(msg)))
  543. scope = TIPC_NODE_SCOPE;
  544. /* Create destination port list: */
  545. tipc_nametbl_mc_translate(msg_nametype(msg),
  546. msg_namelower(msg),
  547. msg_nameupper(msg),
  548. scope,
  549. &dports);
  550. last = dports.count;
  551. if (!last) {
  552. kfree_skb(buf);
  553. return;
  554. }
  555. for (item = &dports; item; item = item->next) {
  556. for (i = 0; i < PLSIZE && ++dst <= last; i++) {
  557. b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
  558. if (!b) {
  559. pr_warn("Failed do clone mcast rcv buffer\n");
  560. continue;
  561. }
  562. msg_set_destport(msg, item->ports[i]);
  563. tipc_sk_rcv(b);
  564. }
  565. }
  566. tipc_port_list_free(&dports);
  567. }
  568. /**
  569. * tipc_sk_proto_rcv - receive a connection mng protocol message
  570. * @tsk: receiving socket
  571. * @dnode: node to send response message to, if any
  572. * @buf: buffer containing protocol message
  573. * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
  574. * (CONN_PROBE_REPLY) message should be forwarded.
  575. */
  576. static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
  577. struct sk_buff *buf)
  578. {
  579. struct tipc_msg *msg = buf_msg(buf);
  580. struct tipc_port *port = &tsk->port;
  581. int conn_cong;
  582. /* Ignore if connection cannot be validated: */
  583. if (!port->connected || !tipc_port_peer_msg(port, msg))
  584. goto exit;
  585. port->probing_state = TIPC_CONN_OK;
  586. if (msg_type(msg) == CONN_ACK) {
  587. conn_cong = tipc_sk_conn_cong(tsk);
  588. tsk->sent_unacked -= msg_msgcnt(msg);
  589. if (conn_cong)
  590. tipc_sock_wakeup(tsk);
  591. } else if (msg_type(msg) == CONN_PROBE) {
  592. if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
  593. return TIPC_OK;
  594. msg_set_type(msg, CONN_PROBE_REPLY);
  595. return TIPC_FWD_MSG;
  596. }
  597. /* Do nothing if msg_type() == CONN_PROBE_REPLY */
  598. exit:
  599. kfree_skb(buf);
  600. return TIPC_OK;
  601. }
  602. /**
  603. * dest_name_check - verify user is permitted to send to specified port name
  604. * @dest: destination address
  605. * @m: descriptor for message to be sent
  606. *
  607. * Prevents restricted configuration commands from being issued by
  608. * unauthorized users.
  609. *
  610. * Returns 0 if permission is granted, otherwise errno
  611. */
  612. static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
  613. {
  614. struct tipc_cfg_msg_hdr hdr;
  615. if (unlikely(dest->addrtype == TIPC_ADDR_ID))
  616. return 0;
  617. if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
  618. return 0;
  619. if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
  620. return 0;
  621. if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
  622. return -EACCES;
  623. if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
  624. return -EMSGSIZE;
  625. if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
  626. return -EFAULT;
  627. if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
  628. return -EACCES;
  629. return 0;
  630. }
  631. static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
  632. {
  633. struct sock *sk = sock->sk;
  634. struct tipc_sock *tsk = tipc_sk(sk);
  635. DEFINE_WAIT(wait);
  636. int done;
  637. do {
  638. int err = sock_error(sk);
  639. if (err)
  640. return err;
  641. if (sock->state == SS_DISCONNECTING)
  642. return -EPIPE;
  643. if (!*timeo_p)
  644. return -EAGAIN;
  645. if (signal_pending(current))
  646. return sock_intr_errno(*timeo_p);
  647. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  648. done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
  649. finish_wait(sk_sleep(sk), &wait);
  650. } while (!done);
  651. return 0;
  652. }
  653. /**
  654. * tipc_sendmsg - send message in connectionless manner
  655. * @iocb: if NULL, indicates that socket lock is already held
  656. * @sock: socket structure
  657. * @m: message to send
  658. * @dsz: amount of user data to be sent
  659. *
  660. * Message must have an destination specified explicitly.
  661. * Used for SOCK_RDM and SOCK_DGRAM messages,
  662. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  663. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  664. *
  665. * Returns the number of bytes sent on success, or errno otherwise
  666. */
  667. static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
  668. struct msghdr *m, size_t dsz)
  669. {
  670. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  671. struct sock *sk = sock->sk;
  672. struct tipc_sock *tsk = tipc_sk(sk);
  673. struct tipc_port *port = &tsk->port;
  674. struct tipc_msg *mhdr = &port->phdr;
  675. struct iovec *iov = m->msg_iov;
  676. u32 dnode, dport;
  677. struct sk_buff *buf;
  678. struct tipc_name_seq *seq = &dest->addr.nameseq;
  679. u32 mtu;
  680. long timeo;
  681. int rc = -EINVAL;
  682. if (unlikely(!dest))
  683. return -EDESTADDRREQ;
  684. if (unlikely((m->msg_namelen < sizeof(*dest)) ||
  685. (dest->family != AF_TIPC)))
  686. return -EINVAL;
  687. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  688. return -EMSGSIZE;
  689. if (iocb)
  690. lock_sock(sk);
  691. if (unlikely(sock->state != SS_READY)) {
  692. if (sock->state == SS_LISTENING) {
  693. rc = -EPIPE;
  694. goto exit;
  695. }
  696. if (sock->state != SS_UNCONNECTED) {
  697. rc = -EISCONN;
  698. goto exit;
  699. }
  700. if (tsk->port.published) {
  701. rc = -EOPNOTSUPP;
  702. goto exit;
  703. }
  704. if (dest->addrtype == TIPC_ADDR_NAME) {
  705. tsk->port.conn_type = dest->addr.name.name.type;
  706. tsk->port.conn_instance = dest->addr.name.name.instance;
  707. }
  708. }
  709. rc = dest_name_check(dest, m);
  710. if (rc)
  711. goto exit;
  712. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  713. if (dest->addrtype == TIPC_ADDR_MCAST) {
  714. rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
  715. goto exit;
  716. } else if (dest->addrtype == TIPC_ADDR_NAME) {
  717. u32 type = dest->addr.name.name.type;
  718. u32 inst = dest->addr.name.name.instance;
  719. u32 domain = dest->addr.name.domain;
  720. dnode = domain;
  721. msg_set_type(mhdr, TIPC_NAMED_MSG);
  722. msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
  723. msg_set_nametype(mhdr, type);
  724. msg_set_nameinst(mhdr, inst);
  725. msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
  726. dport = tipc_nametbl_translate(type, inst, &dnode);
  727. msg_set_destnode(mhdr, dnode);
  728. msg_set_destport(mhdr, dport);
  729. if (unlikely(!dport && !dnode)) {
  730. rc = -EHOSTUNREACH;
  731. goto exit;
  732. }
  733. } else if (dest->addrtype == TIPC_ADDR_ID) {
  734. dnode = dest->addr.id.node;
  735. msg_set_type(mhdr, TIPC_DIRECT_MSG);
  736. msg_set_lookup_scope(mhdr, 0);
  737. msg_set_destnode(mhdr, dnode);
  738. msg_set_destport(mhdr, dest->addr.id.ref);
  739. msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
  740. }
  741. new_mtu:
  742. mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
  743. rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
  744. if (rc < 0)
  745. goto exit;
  746. do {
  747. rc = tipc_link_xmit(buf, dnode, tsk->port.ref);
  748. if (likely(rc >= 0)) {
  749. if (sock->state != SS_READY)
  750. sock->state = SS_CONNECTING;
  751. rc = dsz;
  752. break;
  753. }
  754. if (rc == -EMSGSIZE)
  755. goto new_mtu;
  756. if (rc != -ELINKCONG)
  757. break;
  758. rc = tipc_wait_for_sndmsg(sock, &timeo);
  759. if (rc)
  760. kfree_skb_list(buf);
  761. } while (!rc);
  762. exit:
  763. if (iocb)
  764. release_sock(sk);
  765. return rc;
  766. }
  767. static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
  768. {
  769. struct sock *sk = sock->sk;
  770. struct tipc_sock *tsk = tipc_sk(sk);
  771. DEFINE_WAIT(wait);
  772. int done;
  773. do {
  774. int err = sock_error(sk);
  775. if (err)
  776. return err;
  777. if (sock->state == SS_DISCONNECTING)
  778. return -EPIPE;
  779. else if (sock->state != SS_CONNECTED)
  780. return -ENOTCONN;
  781. if (!*timeo_p)
  782. return -EAGAIN;
  783. if (signal_pending(current))
  784. return sock_intr_errno(*timeo_p);
  785. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  786. done = sk_wait_event(sk, timeo_p,
  787. (!tsk->link_cong &&
  788. !tipc_sk_conn_cong(tsk)) ||
  789. !tsk->port.connected);
  790. finish_wait(sk_sleep(sk), &wait);
  791. } while (!done);
  792. return 0;
  793. }
  794. /**
  795. * tipc_send_stream - send stream-oriented data
  796. * @iocb: (unused)
  797. * @sock: socket structure
  798. * @m: data to send
  799. * @dsz: total length of data to be transmitted
  800. *
  801. * Used for SOCK_STREAM data.
  802. *
  803. * Returns the number of bytes sent on success (or partial success),
  804. * or errno if no data sent
  805. */
  806. static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
  807. struct msghdr *m, size_t dsz)
  808. {
  809. struct sock *sk = sock->sk;
  810. struct tipc_sock *tsk = tipc_sk(sk);
  811. struct tipc_port *port = &tsk->port;
  812. struct tipc_msg *mhdr = &port->phdr;
  813. struct sk_buff *buf;
  814. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  815. u32 ref = port->ref;
  816. int rc = -EINVAL;
  817. long timeo;
  818. u32 dnode;
  819. uint mtu, send, sent = 0;
  820. /* Handle implied connection establishment */
  821. if (unlikely(dest)) {
  822. rc = tipc_sendmsg(iocb, sock, m, dsz);
  823. if (dsz && (dsz == rc))
  824. tsk->sent_unacked = 1;
  825. return rc;
  826. }
  827. if (dsz > (uint)INT_MAX)
  828. return -EMSGSIZE;
  829. if (iocb)
  830. lock_sock(sk);
  831. if (unlikely(sock->state != SS_CONNECTED)) {
  832. if (sock->state == SS_DISCONNECTING)
  833. rc = -EPIPE;
  834. else
  835. rc = -ENOTCONN;
  836. goto exit;
  837. }
  838. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  839. dnode = tipc_port_peernode(port);
  840. next:
  841. mtu = port->max_pkt;
  842. send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
  843. rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf);
  844. if (unlikely(rc < 0))
  845. goto exit;
  846. do {
  847. if (likely(!tipc_sk_conn_cong(tsk))) {
  848. rc = tipc_link_xmit(buf, dnode, ref);
  849. if (likely(!rc)) {
  850. tsk->sent_unacked++;
  851. sent += send;
  852. if (sent == dsz)
  853. break;
  854. goto next;
  855. }
  856. if (rc == -EMSGSIZE) {
  857. port->max_pkt = tipc_node_get_mtu(dnode, ref);
  858. goto next;
  859. }
  860. if (rc != -ELINKCONG)
  861. break;
  862. }
  863. rc = tipc_wait_for_sndpkt(sock, &timeo);
  864. if (rc)
  865. kfree_skb_list(buf);
  866. } while (!rc);
  867. exit:
  868. if (iocb)
  869. release_sock(sk);
  870. return sent ? sent : rc;
  871. }
  872. /**
  873. * tipc_send_packet - send a connection-oriented message
  874. * @iocb: if NULL, indicates that socket lock is already held
  875. * @sock: socket structure
  876. * @m: message to send
  877. * @dsz: length of data to be transmitted
  878. *
  879. * Used for SOCK_SEQPACKET messages.
  880. *
  881. * Returns the number of bytes sent on success, or errno otherwise
  882. */
  883. static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
  884. struct msghdr *m, size_t dsz)
  885. {
  886. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  887. return -EMSGSIZE;
  888. return tipc_send_stream(iocb, sock, m, dsz);
  889. }
  890. /**
  891. * auto_connect - complete connection setup to a remote port
  892. * @tsk: tipc socket structure
  893. * @msg: peer's response message
  894. *
  895. * Returns 0 on success, errno otherwise
  896. */
  897. static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
  898. {
  899. struct tipc_port *port = &tsk->port;
  900. struct socket *sock = tsk->sk.sk_socket;
  901. struct tipc_portid peer;
  902. peer.ref = msg_origport(msg);
  903. peer.node = msg_orignode(msg);
  904. __tipc_port_connect(port->ref, port, &peer);
  905. if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
  906. return -EINVAL;
  907. msg_set_importance(&port->phdr, (u32)msg_importance(msg));
  908. sock->state = SS_CONNECTED;
  909. return 0;
  910. }
  911. /**
  912. * set_orig_addr - capture sender's address for received message
  913. * @m: descriptor for message info
  914. * @msg: received message header
  915. *
  916. * Note: Address is not captured if not requested by receiver.
  917. */
  918. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  919. {
  920. DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
  921. if (addr) {
  922. addr->family = AF_TIPC;
  923. addr->addrtype = TIPC_ADDR_ID;
  924. memset(&addr->addr, 0, sizeof(addr->addr));
  925. addr->addr.id.ref = msg_origport(msg);
  926. addr->addr.id.node = msg_orignode(msg);
  927. addr->addr.name.domain = 0; /* could leave uninitialized */
  928. addr->scope = 0; /* could leave uninitialized */
  929. m->msg_namelen = sizeof(struct sockaddr_tipc);
  930. }
  931. }
  932. /**
  933. * anc_data_recv - optionally capture ancillary data for received message
  934. * @m: descriptor for message info
  935. * @msg: received message header
  936. * @tport: TIPC port associated with message
  937. *
  938. * Note: Ancillary data is not captured if not requested by receiver.
  939. *
  940. * Returns 0 if successful, otherwise errno
  941. */
  942. static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  943. struct tipc_port *tport)
  944. {
  945. u32 anc_data[3];
  946. u32 err;
  947. u32 dest_type;
  948. int has_name;
  949. int res;
  950. if (likely(m->msg_controllen == 0))
  951. return 0;
  952. /* Optionally capture errored message object(s) */
  953. err = msg ? msg_errcode(msg) : 0;
  954. if (unlikely(err)) {
  955. anc_data[0] = err;
  956. anc_data[1] = msg_data_sz(msg);
  957. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  958. if (res)
  959. return res;
  960. if (anc_data[1]) {
  961. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  962. msg_data(msg));
  963. if (res)
  964. return res;
  965. }
  966. }
  967. /* Optionally capture message destination object */
  968. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  969. switch (dest_type) {
  970. case TIPC_NAMED_MSG:
  971. has_name = 1;
  972. anc_data[0] = msg_nametype(msg);
  973. anc_data[1] = msg_namelower(msg);
  974. anc_data[2] = msg_namelower(msg);
  975. break;
  976. case TIPC_MCAST_MSG:
  977. has_name = 1;
  978. anc_data[0] = msg_nametype(msg);
  979. anc_data[1] = msg_namelower(msg);
  980. anc_data[2] = msg_nameupper(msg);
  981. break;
  982. case TIPC_CONN_MSG:
  983. has_name = (tport->conn_type != 0);
  984. anc_data[0] = tport->conn_type;
  985. anc_data[1] = tport->conn_instance;
  986. anc_data[2] = tport->conn_instance;
  987. break;
  988. default:
  989. has_name = 0;
  990. }
  991. if (has_name) {
  992. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  993. if (res)
  994. return res;
  995. }
  996. return 0;
  997. }
  998. static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
  999. {
  1000. struct sock *sk = sock->sk;
  1001. DEFINE_WAIT(wait);
  1002. long timeo = *timeop;
  1003. int err;
  1004. for (;;) {
  1005. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1006. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1007. if (sock->state == SS_DISCONNECTING) {
  1008. err = -ENOTCONN;
  1009. break;
  1010. }
  1011. release_sock(sk);
  1012. timeo = schedule_timeout(timeo);
  1013. lock_sock(sk);
  1014. }
  1015. err = 0;
  1016. if (!skb_queue_empty(&sk->sk_receive_queue))
  1017. break;
  1018. err = sock_intr_errno(timeo);
  1019. if (signal_pending(current))
  1020. break;
  1021. err = -EAGAIN;
  1022. if (!timeo)
  1023. break;
  1024. }
  1025. finish_wait(sk_sleep(sk), &wait);
  1026. *timeop = timeo;
  1027. return err;
  1028. }
  1029. /**
  1030. * tipc_recvmsg - receive packet-oriented message
  1031. * @iocb: (unused)
  1032. * @m: descriptor for message info
  1033. * @buf_len: total size of user buffer area
  1034. * @flags: receive flags
  1035. *
  1036. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  1037. * If the complete message doesn't fit in user area, truncate it.
  1038. *
  1039. * Returns size of returned message data, errno otherwise
  1040. */
  1041. static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
  1042. struct msghdr *m, size_t buf_len, int flags)
  1043. {
  1044. struct sock *sk = sock->sk;
  1045. struct tipc_sock *tsk = tipc_sk(sk);
  1046. struct tipc_port *port = &tsk->port;
  1047. struct sk_buff *buf;
  1048. struct tipc_msg *msg;
  1049. long timeo;
  1050. unsigned int sz;
  1051. u32 err;
  1052. int res;
  1053. /* Catch invalid receive requests */
  1054. if (unlikely(!buf_len))
  1055. return -EINVAL;
  1056. lock_sock(sk);
  1057. if (unlikely(sock->state == SS_UNCONNECTED)) {
  1058. res = -ENOTCONN;
  1059. goto exit;
  1060. }
  1061. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1062. restart:
  1063. /* Look for a message in receive queue; wait if necessary */
  1064. res = tipc_wait_for_rcvmsg(sock, &timeo);
  1065. if (res)
  1066. goto exit;
  1067. /* Look at first message in receive queue */
  1068. buf = skb_peek(&sk->sk_receive_queue);
  1069. msg = buf_msg(buf);
  1070. sz = msg_data_sz(msg);
  1071. err = msg_errcode(msg);
  1072. /* Discard an empty non-errored message & try again */
  1073. if ((!sz) && (!err)) {
  1074. advance_rx_queue(sk);
  1075. goto restart;
  1076. }
  1077. /* Capture sender's address (optional) */
  1078. set_orig_addr(m, msg);
  1079. /* Capture ancillary data (optional) */
  1080. res = anc_data_recv(m, msg, port);
  1081. if (res)
  1082. goto exit;
  1083. /* Capture message data (if valid) & compute return value (always) */
  1084. if (!err) {
  1085. if (unlikely(buf_len < sz)) {
  1086. sz = buf_len;
  1087. m->msg_flags |= MSG_TRUNC;
  1088. }
  1089. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
  1090. m->msg_iov, sz);
  1091. if (res)
  1092. goto exit;
  1093. res = sz;
  1094. } else {
  1095. if ((sock->state == SS_READY) ||
  1096. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  1097. res = 0;
  1098. else
  1099. res = -ECONNRESET;
  1100. }
  1101. /* Consume received message (optional) */
  1102. if (likely(!(flags & MSG_PEEK))) {
  1103. if ((sock->state != SS_READY) &&
  1104. (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
  1105. tipc_acknowledge(port->ref, tsk->rcv_unacked);
  1106. tsk->rcv_unacked = 0;
  1107. }
  1108. advance_rx_queue(sk);
  1109. }
  1110. exit:
  1111. release_sock(sk);
  1112. return res;
  1113. }
  1114. /**
  1115. * tipc_recv_stream - receive stream-oriented data
  1116. * @iocb: (unused)
  1117. * @m: descriptor for message info
  1118. * @buf_len: total size of user buffer area
  1119. * @flags: receive flags
  1120. *
  1121. * Used for SOCK_STREAM messages only. If not enough data is available
  1122. * will optionally wait for more; never truncates data.
  1123. *
  1124. * Returns size of returned message data, errno otherwise
  1125. */
  1126. static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
  1127. struct msghdr *m, size_t buf_len, int flags)
  1128. {
  1129. struct sock *sk = sock->sk;
  1130. struct tipc_sock *tsk = tipc_sk(sk);
  1131. struct tipc_port *port = &tsk->port;
  1132. struct sk_buff *buf;
  1133. struct tipc_msg *msg;
  1134. long timeo;
  1135. unsigned int sz;
  1136. int sz_to_copy, target, needed;
  1137. int sz_copied = 0;
  1138. u32 err;
  1139. int res = 0;
  1140. /* Catch invalid receive attempts */
  1141. if (unlikely(!buf_len))
  1142. return -EINVAL;
  1143. lock_sock(sk);
  1144. if (unlikely(sock->state == SS_UNCONNECTED)) {
  1145. res = -ENOTCONN;
  1146. goto exit;
  1147. }
  1148. target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
  1149. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1150. restart:
  1151. /* Look for a message in receive queue; wait if necessary */
  1152. res = tipc_wait_for_rcvmsg(sock, &timeo);
  1153. if (res)
  1154. goto exit;
  1155. /* Look at first message in receive queue */
  1156. buf = skb_peek(&sk->sk_receive_queue);
  1157. msg = buf_msg(buf);
  1158. sz = msg_data_sz(msg);
  1159. err = msg_errcode(msg);
  1160. /* Discard an empty non-errored message & try again */
  1161. if ((!sz) && (!err)) {
  1162. advance_rx_queue(sk);
  1163. goto restart;
  1164. }
  1165. /* Optionally capture sender's address & ancillary data of first msg */
  1166. if (sz_copied == 0) {
  1167. set_orig_addr(m, msg);
  1168. res = anc_data_recv(m, msg, port);
  1169. if (res)
  1170. goto exit;
  1171. }
  1172. /* Capture message data (if valid) & compute return value (always) */
  1173. if (!err) {
  1174. u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
  1175. sz -= offset;
  1176. needed = (buf_len - sz_copied);
  1177. sz_to_copy = (sz <= needed) ? sz : needed;
  1178. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
  1179. m->msg_iov, sz_to_copy);
  1180. if (res)
  1181. goto exit;
  1182. sz_copied += sz_to_copy;
  1183. if (sz_to_copy < sz) {
  1184. if (!(flags & MSG_PEEK))
  1185. TIPC_SKB_CB(buf)->handle =
  1186. (void *)(unsigned long)(offset + sz_to_copy);
  1187. goto exit;
  1188. }
  1189. } else {
  1190. if (sz_copied != 0)
  1191. goto exit; /* can't add error msg to valid data */
  1192. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  1193. res = 0;
  1194. else
  1195. res = -ECONNRESET;
  1196. }
  1197. /* Consume received message (optional) */
  1198. if (likely(!(flags & MSG_PEEK))) {
  1199. if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
  1200. tipc_acknowledge(port->ref, tsk->rcv_unacked);
  1201. tsk->rcv_unacked = 0;
  1202. }
  1203. advance_rx_queue(sk);
  1204. }
  1205. /* Loop around if more data is required */
  1206. if ((sz_copied < buf_len) && /* didn't get all requested data */
  1207. (!skb_queue_empty(&sk->sk_receive_queue) ||
  1208. (sz_copied < target)) && /* and more is ready or required */
  1209. (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
  1210. (!err)) /* and haven't reached a FIN */
  1211. goto restart;
  1212. exit:
  1213. release_sock(sk);
  1214. return sz_copied ? sz_copied : res;
  1215. }
  1216. /**
  1217. * tipc_write_space - wake up thread if port congestion is released
  1218. * @sk: socket
  1219. */
  1220. static void tipc_write_space(struct sock *sk)
  1221. {
  1222. struct socket_wq *wq;
  1223. rcu_read_lock();
  1224. wq = rcu_dereference(sk->sk_wq);
  1225. if (wq_has_sleeper(wq))
  1226. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1227. POLLWRNORM | POLLWRBAND);
  1228. rcu_read_unlock();
  1229. }
  1230. /**
  1231. * tipc_data_ready - wake up threads to indicate messages have been received
  1232. * @sk: socket
  1233. * @len: the length of messages
  1234. */
  1235. static void tipc_data_ready(struct sock *sk)
  1236. {
  1237. struct socket_wq *wq;
  1238. rcu_read_lock();
  1239. wq = rcu_dereference(sk->sk_wq);
  1240. if (wq_has_sleeper(wq))
  1241. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  1242. POLLRDNORM | POLLRDBAND);
  1243. rcu_read_unlock();
  1244. }
  1245. /**
  1246. * filter_connect - Handle all incoming messages for a connection-based socket
  1247. * @tsk: TIPC socket
  1248. * @msg: message
  1249. *
  1250. * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
  1251. */
  1252. static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
  1253. {
  1254. struct sock *sk = &tsk->sk;
  1255. struct tipc_port *port = &tsk->port;
  1256. struct socket *sock = sk->sk_socket;
  1257. struct tipc_msg *msg = buf_msg(*buf);
  1258. int retval = -TIPC_ERR_NO_PORT;
  1259. int res;
  1260. if (msg_mcast(msg))
  1261. return retval;
  1262. switch ((int)sock->state) {
  1263. case SS_CONNECTED:
  1264. /* Accept only connection-based messages sent by peer */
  1265. if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
  1266. if (unlikely(msg_errcode(msg))) {
  1267. sock->state = SS_DISCONNECTING;
  1268. __tipc_port_disconnect(port);
  1269. }
  1270. retval = TIPC_OK;
  1271. }
  1272. break;
  1273. case SS_CONNECTING:
  1274. /* Accept only ACK or NACK message */
  1275. if (unlikely(msg_errcode(msg))) {
  1276. sock->state = SS_DISCONNECTING;
  1277. sk->sk_err = ECONNREFUSED;
  1278. retval = TIPC_OK;
  1279. break;
  1280. }
  1281. if (unlikely(!msg_connected(msg)))
  1282. break;
  1283. res = auto_connect(tsk, msg);
  1284. if (res) {
  1285. sock->state = SS_DISCONNECTING;
  1286. sk->sk_err = -res;
  1287. retval = TIPC_OK;
  1288. break;
  1289. }
  1290. /* If an incoming message is an 'ACK-', it should be
  1291. * discarded here because it doesn't contain useful
  1292. * data. In addition, we should try to wake up
  1293. * connect() routine if sleeping.
  1294. */
  1295. if (msg_data_sz(msg) == 0) {
  1296. kfree_skb(*buf);
  1297. *buf = NULL;
  1298. if (waitqueue_active(sk_sleep(sk)))
  1299. wake_up_interruptible(sk_sleep(sk));
  1300. }
  1301. retval = TIPC_OK;
  1302. break;
  1303. case SS_LISTENING:
  1304. case SS_UNCONNECTED:
  1305. /* Accept only SYN message */
  1306. if (!msg_connected(msg) && !(msg_errcode(msg)))
  1307. retval = TIPC_OK;
  1308. break;
  1309. case SS_DISCONNECTING:
  1310. break;
  1311. default:
  1312. pr_err("Unknown socket state %u\n", sock->state);
  1313. }
  1314. return retval;
  1315. }
  1316. /**
  1317. * rcvbuf_limit - get proper overload limit of socket receive queue
  1318. * @sk: socket
  1319. * @buf: message
  1320. *
  1321. * For all connection oriented messages, irrespective of importance,
  1322. * the default overload value (i.e. 67MB) is set as limit.
  1323. *
  1324. * For all connectionless messages, by default new queue limits are
  1325. * as belows:
  1326. *
  1327. * TIPC_LOW_IMPORTANCE (4 MB)
  1328. * TIPC_MEDIUM_IMPORTANCE (8 MB)
  1329. * TIPC_HIGH_IMPORTANCE (16 MB)
  1330. * TIPC_CRITICAL_IMPORTANCE (32 MB)
  1331. *
  1332. * Returns overload limit according to corresponding message importance
  1333. */
  1334. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
  1335. {
  1336. struct tipc_msg *msg = buf_msg(buf);
  1337. if (msg_connected(msg))
  1338. return sysctl_tipc_rmem[2];
  1339. return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
  1340. msg_importance(msg);
  1341. }
  1342. /**
  1343. * filter_rcv - validate incoming message
  1344. * @sk: socket
  1345. * @buf: message
  1346. *
  1347. * Enqueues message on receive queue if acceptable; optionally handles
  1348. * disconnect indication for a connected socket.
  1349. *
  1350. * Called with socket lock already taken; port lock may also be taken.
  1351. *
  1352. * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
  1353. * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
  1354. */
  1355. static int filter_rcv(struct sock *sk, struct sk_buff *buf)
  1356. {
  1357. struct socket *sock = sk->sk_socket;
  1358. struct tipc_sock *tsk = tipc_sk(sk);
  1359. struct tipc_msg *msg = buf_msg(buf);
  1360. unsigned int limit = rcvbuf_limit(sk, buf);
  1361. u32 onode;
  1362. int rc = TIPC_OK;
  1363. if (unlikely(msg_user(msg) == CONN_MANAGER))
  1364. return tipc_sk_proto_rcv(tsk, &onode, buf);
  1365. /* Reject message if it is wrong sort of message for socket */
  1366. if (msg_type(msg) > TIPC_DIRECT_MSG)
  1367. return -TIPC_ERR_NO_PORT;
  1368. if (sock->state == SS_READY) {
  1369. if (msg_connected(msg))
  1370. return -TIPC_ERR_NO_PORT;
  1371. } else {
  1372. rc = filter_connect(tsk, &buf);
  1373. if (rc != TIPC_OK || buf == NULL)
  1374. return rc;
  1375. }
  1376. /* Reject message if there isn't room to queue it */
  1377. if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
  1378. return -TIPC_ERR_OVERLOAD;
  1379. /* Enqueue message */
  1380. TIPC_SKB_CB(buf)->handle = NULL;
  1381. __skb_queue_tail(&sk->sk_receive_queue, buf);
  1382. skb_set_owner_r(buf, sk);
  1383. sk->sk_data_ready(sk);
  1384. return TIPC_OK;
  1385. }
  1386. /**
  1387. * tipc_backlog_rcv - handle incoming message from backlog queue
  1388. * @sk: socket
  1389. * @buf: message
  1390. *
  1391. * Caller must hold socket lock, but not port lock.
  1392. *
  1393. * Returns 0
  1394. */
  1395. static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
  1396. {
  1397. int rc;
  1398. u32 onode;
  1399. struct tipc_sock *tsk = tipc_sk(sk);
  1400. uint truesize = buf->truesize;
  1401. rc = filter_rcv(sk, buf);
  1402. if (likely(!rc)) {
  1403. if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
  1404. atomic_add(truesize, &tsk->dupl_rcvcnt);
  1405. return 0;
  1406. }
  1407. if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
  1408. return 0;
  1409. tipc_link_xmit(buf, onode, 0);
  1410. return 0;
  1411. }
  1412. /**
  1413. * tipc_sk_rcv - handle incoming message
  1414. * @buf: buffer containing arriving message
  1415. * Consumes buffer
  1416. * Returns 0 if success, or errno: -EHOSTUNREACH
  1417. */
  1418. int tipc_sk_rcv(struct sk_buff *buf)
  1419. {
  1420. struct tipc_sock *tsk;
  1421. struct tipc_port *port;
  1422. struct sock *sk;
  1423. u32 dport = msg_destport(buf_msg(buf));
  1424. int rc = TIPC_OK;
  1425. uint limit;
  1426. u32 dnode;
  1427. /* Validate destination and message */
  1428. port = tipc_port_lock(dport);
  1429. if (unlikely(!port)) {
  1430. rc = tipc_msg_eval(buf, &dnode);
  1431. goto exit;
  1432. }
  1433. tsk = tipc_port_to_sock(port);
  1434. sk = &tsk->sk;
  1435. /* Queue message */
  1436. bh_lock_sock(sk);
  1437. if (!sock_owned_by_user(sk)) {
  1438. rc = filter_rcv(sk, buf);
  1439. } else {
  1440. if (sk->sk_backlog.len == 0)
  1441. atomic_set(&tsk->dupl_rcvcnt, 0);
  1442. limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
  1443. if (sk_add_backlog(sk, buf, limit))
  1444. rc = -TIPC_ERR_OVERLOAD;
  1445. }
  1446. bh_unlock_sock(sk);
  1447. tipc_port_unlock(port);
  1448. if (likely(!rc))
  1449. return 0;
  1450. exit:
  1451. if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
  1452. return -EHOSTUNREACH;
  1453. tipc_link_xmit(buf, dnode, 0);
  1454. return (rc < 0) ? -EHOSTUNREACH : 0;
  1455. }
  1456. static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
  1457. {
  1458. struct sock *sk = sock->sk;
  1459. DEFINE_WAIT(wait);
  1460. int done;
  1461. do {
  1462. int err = sock_error(sk);
  1463. if (err)
  1464. return err;
  1465. if (!*timeo_p)
  1466. return -ETIMEDOUT;
  1467. if (signal_pending(current))
  1468. return sock_intr_errno(*timeo_p);
  1469. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1470. done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
  1471. finish_wait(sk_sleep(sk), &wait);
  1472. } while (!done);
  1473. return 0;
  1474. }
  1475. /**
  1476. * tipc_connect - establish a connection to another TIPC port
  1477. * @sock: socket structure
  1478. * @dest: socket address for destination port
  1479. * @destlen: size of socket address data structure
  1480. * @flags: file-related flags associated with socket
  1481. *
  1482. * Returns 0 on success, errno otherwise
  1483. */
  1484. static int tipc_connect(struct socket *sock, struct sockaddr *dest,
  1485. int destlen, int flags)
  1486. {
  1487. struct sock *sk = sock->sk;
  1488. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1489. struct msghdr m = {NULL,};
  1490. long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
  1491. socket_state previous;
  1492. int res;
  1493. lock_sock(sk);
  1494. /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
  1495. if (sock->state == SS_READY) {
  1496. res = -EOPNOTSUPP;
  1497. goto exit;
  1498. }
  1499. /*
  1500. * Reject connection attempt using multicast address
  1501. *
  1502. * Note: send_msg() validates the rest of the address fields,
  1503. * so there's no need to do it here
  1504. */
  1505. if (dst->addrtype == TIPC_ADDR_MCAST) {
  1506. res = -EINVAL;
  1507. goto exit;
  1508. }
  1509. previous = sock->state;
  1510. switch (sock->state) {
  1511. case SS_UNCONNECTED:
  1512. /* Send a 'SYN-' to destination */
  1513. m.msg_name = dest;
  1514. m.msg_namelen = destlen;
  1515. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  1516. * indicate send_msg() is never blocked.
  1517. */
  1518. if (!timeout)
  1519. m.msg_flags = MSG_DONTWAIT;
  1520. res = tipc_sendmsg(NULL, sock, &m, 0);
  1521. if ((res < 0) && (res != -EWOULDBLOCK))
  1522. goto exit;
  1523. /* Just entered SS_CONNECTING state; the only
  1524. * difference is that return value in non-blocking
  1525. * case is EINPROGRESS, rather than EALREADY.
  1526. */
  1527. res = -EINPROGRESS;
  1528. case SS_CONNECTING:
  1529. if (previous == SS_CONNECTING)
  1530. res = -EALREADY;
  1531. if (!timeout)
  1532. goto exit;
  1533. timeout = msecs_to_jiffies(timeout);
  1534. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  1535. res = tipc_wait_for_connect(sock, &timeout);
  1536. break;
  1537. case SS_CONNECTED:
  1538. res = -EISCONN;
  1539. break;
  1540. default:
  1541. res = -EINVAL;
  1542. break;
  1543. }
  1544. exit:
  1545. release_sock(sk);
  1546. return res;
  1547. }
  1548. /**
  1549. * tipc_listen - allow socket to listen for incoming connections
  1550. * @sock: socket structure
  1551. * @len: (unused)
  1552. *
  1553. * Returns 0 on success, errno otherwise
  1554. */
  1555. static int tipc_listen(struct socket *sock, int len)
  1556. {
  1557. struct sock *sk = sock->sk;
  1558. int res;
  1559. lock_sock(sk);
  1560. if (sock->state != SS_UNCONNECTED)
  1561. res = -EINVAL;
  1562. else {
  1563. sock->state = SS_LISTENING;
  1564. res = 0;
  1565. }
  1566. release_sock(sk);
  1567. return res;
  1568. }
  1569. static int tipc_wait_for_accept(struct socket *sock, long timeo)
  1570. {
  1571. struct sock *sk = sock->sk;
  1572. DEFINE_WAIT(wait);
  1573. int err;
  1574. /* True wake-one mechanism for incoming connections: only
  1575. * one process gets woken up, not the 'whole herd'.
  1576. * Since we do not 'race & poll' for established sockets
  1577. * anymore, the common case will execute the loop only once.
  1578. */
  1579. for (;;) {
  1580. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  1581. TASK_INTERRUPTIBLE);
  1582. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1583. release_sock(sk);
  1584. timeo = schedule_timeout(timeo);
  1585. lock_sock(sk);
  1586. }
  1587. err = 0;
  1588. if (!skb_queue_empty(&sk->sk_receive_queue))
  1589. break;
  1590. err = -EINVAL;
  1591. if (sock->state != SS_LISTENING)
  1592. break;
  1593. err = sock_intr_errno(timeo);
  1594. if (signal_pending(current))
  1595. break;
  1596. err = -EAGAIN;
  1597. if (!timeo)
  1598. break;
  1599. }
  1600. finish_wait(sk_sleep(sk), &wait);
  1601. return err;
  1602. }
  1603. /**
  1604. * tipc_accept - wait for connection request
  1605. * @sock: listening socket
  1606. * @newsock: new socket that is to be connected
  1607. * @flags: file-related flags associated with socket
  1608. *
  1609. * Returns 0 on success, errno otherwise
  1610. */
  1611. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
  1612. {
  1613. struct sock *new_sk, *sk = sock->sk;
  1614. struct sk_buff *buf;
  1615. struct tipc_port *new_port;
  1616. struct tipc_msg *msg;
  1617. struct tipc_portid peer;
  1618. u32 new_ref;
  1619. long timeo;
  1620. int res;
  1621. lock_sock(sk);
  1622. if (sock->state != SS_LISTENING) {
  1623. res = -EINVAL;
  1624. goto exit;
  1625. }
  1626. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  1627. res = tipc_wait_for_accept(sock, timeo);
  1628. if (res)
  1629. goto exit;
  1630. buf = skb_peek(&sk->sk_receive_queue);
  1631. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
  1632. if (res)
  1633. goto exit;
  1634. new_sk = new_sock->sk;
  1635. new_port = &tipc_sk(new_sk)->port;
  1636. new_ref = new_port->ref;
  1637. msg = buf_msg(buf);
  1638. /* we lock on new_sk; but lockdep sees the lock on sk */
  1639. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  1640. /*
  1641. * Reject any stray messages received by new socket
  1642. * before the socket lock was taken (very, very unlikely)
  1643. */
  1644. reject_rx_queue(new_sk);
  1645. /* Connect new socket to it's peer */
  1646. peer.ref = msg_origport(msg);
  1647. peer.node = msg_orignode(msg);
  1648. tipc_port_connect(new_ref, &peer);
  1649. new_sock->state = SS_CONNECTED;
  1650. tipc_port_set_importance(new_port, msg_importance(msg));
  1651. if (msg_named(msg)) {
  1652. new_port->conn_type = msg_nametype(msg);
  1653. new_port->conn_instance = msg_nameinst(msg);
  1654. }
  1655. /*
  1656. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1657. * Respond to 'SYN+' by queuing it on new socket.
  1658. */
  1659. if (!msg_data_sz(msg)) {
  1660. struct msghdr m = {NULL,};
  1661. advance_rx_queue(sk);
  1662. tipc_send_packet(NULL, new_sock, &m, 0);
  1663. } else {
  1664. __skb_dequeue(&sk->sk_receive_queue);
  1665. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  1666. skb_set_owner_r(buf, new_sk);
  1667. }
  1668. release_sock(new_sk);
  1669. exit:
  1670. release_sock(sk);
  1671. return res;
  1672. }
  1673. /**
  1674. * tipc_shutdown - shutdown socket connection
  1675. * @sock: socket structure
  1676. * @how: direction to close (must be SHUT_RDWR)
  1677. *
  1678. * Terminates connection (if necessary), then purges socket's receive queue.
  1679. *
  1680. * Returns 0 on success, errno otherwise
  1681. */
  1682. static int tipc_shutdown(struct socket *sock, int how)
  1683. {
  1684. struct sock *sk = sock->sk;
  1685. struct tipc_sock *tsk = tipc_sk(sk);
  1686. struct tipc_port *port = &tsk->port;
  1687. struct sk_buff *buf;
  1688. u32 peer;
  1689. int res;
  1690. if (how != SHUT_RDWR)
  1691. return -EINVAL;
  1692. lock_sock(sk);
  1693. switch (sock->state) {
  1694. case SS_CONNECTING:
  1695. case SS_CONNECTED:
  1696. restart:
  1697. /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
  1698. buf = __skb_dequeue(&sk->sk_receive_queue);
  1699. if (buf) {
  1700. if (TIPC_SKB_CB(buf)->handle != NULL) {
  1701. kfree_skb(buf);
  1702. goto restart;
  1703. }
  1704. tipc_port_disconnect(port->ref);
  1705. if (tipc_msg_reverse(buf, &peer, TIPC_CONN_SHUTDOWN))
  1706. tipc_link_xmit(buf, peer, 0);
  1707. } else {
  1708. tipc_port_shutdown(port->ref);
  1709. }
  1710. sock->state = SS_DISCONNECTING;
  1711. /* fall through */
  1712. case SS_DISCONNECTING:
  1713. /* Discard any unreceived messages */
  1714. __skb_queue_purge(&sk->sk_receive_queue);
  1715. /* Wake up anyone sleeping in poll */
  1716. sk->sk_state_change(sk);
  1717. res = 0;
  1718. break;
  1719. default:
  1720. res = -ENOTCONN;
  1721. }
  1722. release_sock(sk);
  1723. return res;
  1724. }
  1725. /**
  1726. * tipc_setsockopt - set socket option
  1727. * @sock: socket structure
  1728. * @lvl: option level
  1729. * @opt: option identifier
  1730. * @ov: pointer to new option value
  1731. * @ol: length of option value
  1732. *
  1733. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  1734. * (to ease compatibility).
  1735. *
  1736. * Returns 0 on success, errno otherwise
  1737. */
  1738. static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
  1739. char __user *ov, unsigned int ol)
  1740. {
  1741. struct sock *sk = sock->sk;
  1742. struct tipc_sock *tsk = tipc_sk(sk);
  1743. struct tipc_port *port = &tsk->port;
  1744. u32 value;
  1745. int res;
  1746. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1747. return 0;
  1748. if (lvl != SOL_TIPC)
  1749. return -ENOPROTOOPT;
  1750. if (ol < sizeof(value))
  1751. return -EINVAL;
  1752. res = get_user(value, (u32 __user *)ov);
  1753. if (res)
  1754. return res;
  1755. lock_sock(sk);
  1756. switch (opt) {
  1757. case TIPC_IMPORTANCE:
  1758. res = tipc_port_set_importance(port, value);
  1759. break;
  1760. case TIPC_SRC_DROPPABLE:
  1761. if (sock->type != SOCK_STREAM)
  1762. tipc_port_set_unreliable(port, value);
  1763. else
  1764. res = -ENOPROTOOPT;
  1765. break;
  1766. case TIPC_DEST_DROPPABLE:
  1767. tipc_port_set_unreturnable(port, value);
  1768. break;
  1769. case TIPC_CONN_TIMEOUT:
  1770. tipc_sk(sk)->conn_timeout = value;
  1771. /* no need to set "res", since already 0 at this point */
  1772. break;
  1773. default:
  1774. res = -EINVAL;
  1775. }
  1776. release_sock(sk);
  1777. return res;
  1778. }
  1779. /**
  1780. * tipc_getsockopt - get socket option
  1781. * @sock: socket structure
  1782. * @lvl: option level
  1783. * @opt: option identifier
  1784. * @ov: receptacle for option value
  1785. * @ol: receptacle for length of option value
  1786. *
  1787. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  1788. * (to ease compatibility).
  1789. *
  1790. * Returns 0 on success, errno otherwise
  1791. */
  1792. static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
  1793. char __user *ov, int __user *ol)
  1794. {
  1795. struct sock *sk = sock->sk;
  1796. struct tipc_sock *tsk = tipc_sk(sk);
  1797. struct tipc_port *port = &tsk->port;
  1798. int len;
  1799. u32 value;
  1800. int res;
  1801. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1802. return put_user(0, ol);
  1803. if (lvl != SOL_TIPC)
  1804. return -ENOPROTOOPT;
  1805. res = get_user(len, ol);
  1806. if (res)
  1807. return res;
  1808. lock_sock(sk);
  1809. switch (opt) {
  1810. case TIPC_IMPORTANCE:
  1811. value = tipc_port_importance(port);
  1812. break;
  1813. case TIPC_SRC_DROPPABLE:
  1814. value = tipc_port_unreliable(port);
  1815. break;
  1816. case TIPC_DEST_DROPPABLE:
  1817. value = tipc_port_unreturnable(port);
  1818. break;
  1819. case TIPC_CONN_TIMEOUT:
  1820. value = tipc_sk(sk)->conn_timeout;
  1821. /* no need to set "res", since already 0 at this point */
  1822. break;
  1823. case TIPC_NODE_RECVQ_DEPTH:
  1824. value = 0; /* was tipc_queue_size, now obsolete */
  1825. break;
  1826. case TIPC_SOCK_RECVQ_DEPTH:
  1827. value = skb_queue_len(&sk->sk_receive_queue);
  1828. break;
  1829. default:
  1830. res = -EINVAL;
  1831. }
  1832. release_sock(sk);
  1833. if (res)
  1834. return res; /* "get" failed */
  1835. if (len < sizeof(value))
  1836. return -EINVAL;
  1837. if (copy_to_user(ov, &value, sizeof(value)))
  1838. return -EFAULT;
  1839. return put_user(sizeof(value), ol);
  1840. }
  1841. static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
  1842. {
  1843. struct tipc_sioc_ln_req lnr;
  1844. void __user *argp = (void __user *)arg;
  1845. switch (cmd) {
  1846. case SIOCGETLINKNAME:
  1847. if (copy_from_user(&lnr, argp, sizeof(lnr)))
  1848. return -EFAULT;
  1849. if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
  1850. lnr.linkname, TIPC_MAX_LINK_NAME)) {
  1851. if (copy_to_user(argp, &lnr, sizeof(lnr)))
  1852. return -EFAULT;
  1853. return 0;
  1854. }
  1855. return -EADDRNOTAVAIL;
  1856. default:
  1857. return -ENOIOCTLCMD;
  1858. }
  1859. }
  1860. /* Protocol switches for the various types of TIPC sockets */
  1861. static const struct proto_ops msg_ops = {
  1862. .owner = THIS_MODULE,
  1863. .family = AF_TIPC,
  1864. .release = tipc_release,
  1865. .bind = tipc_bind,
  1866. .connect = tipc_connect,
  1867. .socketpair = sock_no_socketpair,
  1868. .accept = sock_no_accept,
  1869. .getname = tipc_getname,
  1870. .poll = tipc_poll,
  1871. .ioctl = tipc_ioctl,
  1872. .listen = sock_no_listen,
  1873. .shutdown = tipc_shutdown,
  1874. .setsockopt = tipc_setsockopt,
  1875. .getsockopt = tipc_getsockopt,
  1876. .sendmsg = tipc_sendmsg,
  1877. .recvmsg = tipc_recvmsg,
  1878. .mmap = sock_no_mmap,
  1879. .sendpage = sock_no_sendpage
  1880. };
  1881. static const struct proto_ops packet_ops = {
  1882. .owner = THIS_MODULE,
  1883. .family = AF_TIPC,
  1884. .release = tipc_release,
  1885. .bind = tipc_bind,
  1886. .connect = tipc_connect,
  1887. .socketpair = sock_no_socketpair,
  1888. .accept = tipc_accept,
  1889. .getname = tipc_getname,
  1890. .poll = tipc_poll,
  1891. .ioctl = tipc_ioctl,
  1892. .listen = tipc_listen,
  1893. .shutdown = tipc_shutdown,
  1894. .setsockopt = tipc_setsockopt,
  1895. .getsockopt = tipc_getsockopt,
  1896. .sendmsg = tipc_send_packet,
  1897. .recvmsg = tipc_recvmsg,
  1898. .mmap = sock_no_mmap,
  1899. .sendpage = sock_no_sendpage
  1900. };
  1901. static const struct proto_ops stream_ops = {
  1902. .owner = THIS_MODULE,
  1903. .family = AF_TIPC,
  1904. .release = tipc_release,
  1905. .bind = tipc_bind,
  1906. .connect = tipc_connect,
  1907. .socketpair = sock_no_socketpair,
  1908. .accept = tipc_accept,
  1909. .getname = tipc_getname,
  1910. .poll = tipc_poll,
  1911. .ioctl = tipc_ioctl,
  1912. .listen = tipc_listen,
  1913. .shutdown = tipc_shutdown,
  1914. .setsockopt = tipc_setsockopt,
  1915. .getsockopt = tipc_getsockopt,
  1916. .sendmsg = tipc_send_stream,
  1917. .recvmsg = tipc_recv_stream,
  1918. .mmap = sock_no_mmap,
  1919. .sendpage = sock_no_sendpage
  1920. };
  1921. static const struct net_proto_family tipc_family_ops = {
  1922. .owner = THIS_MODULE,
  1923. .family = AF_TIPC,
  1924. .create = tipc_sk_create
  1925. };
  1926. static struct proto tipc_proto = {
  1927. .name = "TIPC",
  1928. .owner = THIS_MODULE,
  1929. .obj_size = sizeof(struct tipc_sock),
  1930. .sysctl_rmem = sysctl_tipc_rmem
  1931. };
  1932. static struct proto tipc_proto_kern = {
  1933. .name = "TIPC",
  1934. .obj_size = sizeof(struct tipc_sock),
  1935. .sysctl_rmem = sysctl_tipc_rmem
  1936. };
  1937. /**
  1938. * tipc_socket_init - initialize TIPC socket interface
  1939. *
  1940. * Returns 0 on success, errno otherwise
  1941. */
  1942. int tipc_socket_init(void)
  1943. {
  1944. int res;
  1945. res = proto_register(&tipc_proto, 1);
  1946. if (res) {
  1947. pr_err("Failed to register TIPC protocol type\n");
  1948. goto out;
  1949. }
  1950. res = sock_register(&tipc_family_ops);
  1951. if (res) {
  1952. pr_err("Failed to register TIPC socket type\n");
  1953. proto_unregister(&tipc_proto);
  1954. goto out;
  1955. }
  1956. out:
  1957. return res;
  1958. }
  1959. /**
  1960. * tipc_socket_stop - stop TIPC socket interface
  1961. */
  1962. void tipc_socket_stop(void)
  1963. {
  1964. sock_unregister(tipc_family_ops.family);
  1965. proto_unregister(&tipc_proto);
  1966. }