socket.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  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. #include "link.h"
  43. #define SS_LISTENING -1 /* socket is listening */
  44. #define SS_READY -2 /* socket is connectionless */
  45. #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
  46. #define TIPC_FWD_MSG 1
  47. static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
  48. static void tipc_data_ready(struct sock *sk);
  49. static void tipc_write_space(struct sock *sk);
  50. static int tipc_release(struct socket *sock);
  51. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
  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_xmit2(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_xmit2(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_sk_proto_rcv - receive a connection mng protocol message
  486. * @tsk: receiving socket
  487. * @dnode: node to send response message to, if any
  488. * @buf: buffer containing protocol message
  489. * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
  490. * (CONN_PROBE_REPLY) message should be forwarded.
  491. */
  492. int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode, struct sk_buff *buf)
  493. {
  494. struct tipc_msg *msg = buf_msg(buf);
  495. struct tipc_port *port = &tsk->port;
  496. int conn_cong;
  497. /* Ignore if connection cannot be validated: */
  498. if (!port->connected || !tipc_port_peer_msg(port, msg))
  499. goto exit;
  500. port->probing_state = TIPC_CONN_OK;
  501. if (msg_type(msg) == CONN_ACK) {
  502. conn_cong = tipc_sk_conn_cong(tsk);
  503. tsk->sent_unacked -= msg_msgcnt(msg);
  504. if (conn_cong)
  505. tipc_sock_wakeup(tsk);
  506. } else if (msg_type(msg) == CONN_PROBE) {
  507. if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
  508. return TIPC_OK;
  509. msg_set_type(msg, CONN_PROBE_REPLY);
  510. return TIPC_FWD_MSG;
  511. }
  512. /* Do nothing if msg_type() == CONN_PROBE_REPLY */
  513. exit:
  514. kfree_skb(buf);
  515. return TIPC_OK;
  516. }
  517. /**
  518. * dest_name_check - verify user is permitted to send to specified port name
  519. * @dest: destination address
  520. * @m: descriptor for message to be sent
  521. *
  522. * Prevents restricted configuration commands from being issued by
  523. * unauthorized users.
  524. *
  525. * Returns 0 if permission is granted, otherwise errno
  526. */
  527. static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
  528. {
  529. struct tipc_cfg_msg_hdr hdr;
  530. if (unlikely(dest->addrtype == TIPC_ADDR_ID))
  531. return 0;
  532. if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
  533. return 0;
  534. if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
  535. return 0;
  536. if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
  537. return -EACCES;
  538. if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
  539. return -EMSGSIZE;
  540. if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
  541. return -EFAULT;
  542. if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
  543. return -EACCES;
  544. return 0;
  545. }
  546. static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
  547. {
  548. struct sock *sk = sock->sk;
  549. struct tipc_sock *tsk = tipc_sk(sk);
  550. DEFINE_WAIT(wait);
  551. int done;
  552. do {
  553. int err = sock_error(sk);
  554. if (err)
  555. return err;
  556. if (sock->state == SS_DISCONNECTING)
  557. return -EPIPE;
  558. if (!*timeo_p)
  559. return -EAGAIN;
  560. if (signal_pending(current))
  561. return sock_intr_errno(*timeo_p);
  562. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  563. done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
  564. finish_wait(sk_sleep(sk), &wait);
  565. } while (!done);
  566. return 0;
  567. }
  568. /**
  569. * tipc_sendmcast - send multicast message
  570. * @sock: socket structure
  571. * @seq: destination address
  572. * @iov: message data to send
  573. * @dsz: total length of message data
  574. * @timeo: timeout to wait for wakeup
  575. *
  576. * Called from function tipc_sendmsg(), which has done all sanity checks
  577. * Returns the number of bytes sent on success, or errno
  578. */
  579. static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
  580. struct iovec *iov, size_t dsz, long timeo)
  581. {
  582. struct sock *sk = sock->sk;
  583. struct tipc_sock *tsk = tipc_sk(sk);
  584. int rc;
  585. do {
  586. if (sock->state != SS_READY) {
  587. rc = -EOPNOTSUPP;
  588. break;
  589. }
  590. rc = tipc_port_mcast_xmit(&tsk->port, seq, iov, dsz);
  591. if (likely(rc >= 0)) {
  592. if (sock->state != SS_READY)
  593. sock->state = SS_CONNECTING;
  594. break;
  595. }
  596. if (rc != -ELINKCONG)
  597. break;
  598. rc = tipc_wait_for_sndmsg(sock, &timeo);
  599. } while (!rc);
  600. return rc;
  601. }
  602. /**
  603. * tipc_sendmsg - send message in connectionless manner
  604. * @iocb: if NULL, indicates that socket lock is already held
  605. * @sock: socket structure
  606. * @m: message to send
  607. * @dsz: amount of user data to be sent
  608. *
  609. * Message must have an destination specified explicitly.
  610. * Used for SOCK_RDM and SOCK_DGRAM messages,
  611. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  612. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  613. *
  614. * Returns the number of bytes sent on success, or errno otherwise
  615. */
  616. static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
  617. struct msghdr *m, size_t dsz)
  618. {
  619. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  620. struct sock *sk = sock->sk;
  621. struct tipc_sock *tsk = tipc_sk(sk);
  622. struct tipc_port *port = &tsk->port;
  623. struct tipc_msg *mhdr = &port->phdr;
  624. struct iovec *iov = m->msg_iov;
  625. u32 dnode, dport;
  626. struct sk_buff *buf;
  627. struct tipc_name_seq *seq = &dest->addr.nameseq;
  628. u32 mtu;
  629. long timeo;
  630. int rc = -EINVAL;
  631. if (unlikely(!dest))
  632. return -EDESTADDRREQ;
  633. if (unlikely((m->msg_namelen < sizeof(*dest)) ||
  634. (dest->family != AF_TIPC)))
  635. return -EINVAL;
  636. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  637. return -EMSGSIZE;
  638. if (iocb)
  639. lock_sock(sk);
  640. if (unlikely(sock->state != SS_READY)) {
  641. if (sock->state == SS_LISTENING) {
  642. rc = -EPIPE;
  643. goto exit;
  644. }
  645. if (sock->state != SS_UNCONNECTED) {
  646. rc = -EISCONN;
  647. goto exit;
  648. }
  649. if (tsk->port.published) {
  650. rc = -EOPNOTSUPP;
  651. goto exit;
  652. }
  653. if (dest->addrtype == TIPC_ADDR_NAME) {
  654. tsk->port.conn_type = dest->addr.name.name.type;
  655. tsk->port.conn_instance = dest->addr.name.name.instance;
  656. }
  657. }
  658. rc = dest_name_check(dest, m);
  659. if (rc)
  660. goto exit;
  661. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  662. if (dest->addrtype == TIPC_ADDR_MCAST) {
  663. rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
  664. goto exit;
  665. } else if (dest->addrtype == TIPC_ADDR_NAME) {
  666. u32 type = dest->addr.name.name.type;
  667. u32 inst = dest->addr.name.name.instance;
  668. u32 domain = dest->addr.name.domain;
  669. dnode = domain;
  670. msg_set_type(mhdr, TIPC_NAMED_MSG);
  671. msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
  672. msg_set_nametype(mhdr, type);
  673. msg_set_nameinst(mhdr, inst);
  674. msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
  675. dport = tipc_nametbl_translate(type, inst, &dnode);
  676. msg_set_destnode(mhdr, dnode);
  677. msg_set_destport(mhdr, dport);
  678. if (unlikely(!dport && !dnode)) {
  679. rc = -EHOSTUNREACH;
  680. goto exit;
  681. }
  682. } else if (dest->addrtype == TIPC_ADDR_ID) {
  683. dnode = dest->addr.id.node;
  684. msg_set_type(mhdr, TIPC_DIRECT_MSG);
  685. msg_set_lookup_scope(mhdr, 0);
  686. msg_set_destnode(mhdr, dnode);
  687. msg_set_destport(mhdr, dest->addr.id.ref);
  688. msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
  689. }
  690. new_mtu:
  691. mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
  692. rc = tipc_msg_build2(mhdr, iov, 0, dsz, mtu, &buf);
  693. if (rc < 0)
  694. goto exit;
  695. do {
  696. rc = tipc_link_xmit2(buf, dnode, tsk->port.ref);
  697. if (likely(rc >= 0)) {
  698. if (sock->state != SS_READY)
  699. sock->state = SS_CONNECTING;
  700. rc = dsz;
  701. break;
  702. }
  703. if (rc == -EMSGSIZE)
  704. goto new_mtu;
  705. if (rc != -ELINKCONG)
  706. break;
  707. rc = tipc_wait_for_sndmsg(sock, &timeo);
  708. } while (!rc);
  709. exit:
  710. if (iocb)
  711. release_sock(sk);
  712. return rc;
  713. }
  714. static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
  715. {
  716. struct sock *sk = sock->sk;
  717. struct tipc_sock *tsk = tipc_sk(sk);
  718. DEFINE_WAIT(wait);
  719. int done;
  720. do {
  721. int err = sock_error(sk);
  722. if (err)
  723. return err;
  724. if (sock->state == SS_DISCONNECTING)
  725. return -EPIPE;
  726. else if (sock->state != SS_CONNECTED)
  727. return -ENOTCONN;
  728. if (!*timeo_p)
  729. return -EAGAIN;
  730. if (signal_pending(current))
  731. return sock_intr_errno(*timeo_p);
  732. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  733. done = sk_wait_event(sk, timeo_p,
  734. (!tsk->link_cong &&
  735. !tipc_sk_conn_cong(tsk)) ||
  736. !tsk->port.connected);
  737. finish_wait(sk_sleep(sk), &wait);
  738. } while (!done);
  739. return 0;
  740. }
  741. /**
  742. * tipc_send_stream - send stream-oriented data
  743. * @iocb: (unused)
  744. * @sock: socket structure
  745. * @m: data to send
  746. * @dsz: total length of data to be transmitted
  747. *
  748. * Used for SOCK_STREAM data.
  749. *
  750. * Returns the number of bytes sent on success (or partial success),
  751. * or errno if no data sent
  752. */
  753. static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
  754. struct msghdr *m, size_t dsz)
  755. {
  756. struct sock *sk = sock->sk;
  757. struct tipc_sock *tsk = tipc_sk(sk);
  758. struct tipc_port *port = &tsk->port;
  759. struct tipc_msg *mhdr = &port->phdr;
  760. struct sk_buff *buf;
  761. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  762. u32 ref = port->ref;
  763. int rc = -EINVAL;
  764. long timeo;
  765. u32 dnode;
  766. uint mtu, send, sent = 0;
  767. /* Handle implied connection establishment */
  768. if (unlikely(dest)) {
  769. rc = tipc_sendmsg(iocb, sock, m, dsz);
  770. if (dsz && (dsz == rc))
  771. tsk->sent_unacked = 1;
  772. return rc;
  773. }
  774. if (dsz > (uint)INT_MAX)
  775. return -EMSGSIZE;
  776. if (iocb)
  777. lock_sock(sk);
  778. if (unlikely(sock->state != SS_CONNECTED)) {
  779. if (sock->state == SS_DISCONNECTING)
  780. rc = -EPIPE;
  781. else
  782. rc = -ENOTCONN;
  783. goto exit;
  784. }
  785. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  786. dnode = tipc_port_peernode(port);
  787. next:
  788. mtu = port->max_pkt;
  789. send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
  790. rc = tipc_msg_build2(mhdr, m->msg_iov, sent, send, mtu, &buf);
  791. if (unlikely(rc < 0))
  792. goto exit;
  793. do {
  794. if (likely(!tipc_sk_conn_cong(tsk))) {
  795. rc = tipc_link_xmit2(buf, dnode, ref);
  796. if (likely(!rc)) {
  797. tsk->sent_unacked++;
  798. sent += send;
  799. if (sent == dsz)
  800. break;
  801. goto next;
  802. }
  803. if (rc == -EMSGSIZE) {
  804. port->max_pkt = tipc_node_get_mtu(dnode, ref);
  805. goto next;
  806. }
  807. if (rc != -ELINKCONG)
  808. break;
  809. }
  810. rc = tipc_wait_for_sndpkt(sock, &timeo);
  811. } while (!rc);
  812. exit:
  813. if (iocb)
  814. release_sock(sk);
  815. return sent ? sent : rc;
  816. }
  817. /**
  818. * tipc_send_packet - send a connection-oriented message
  819. * @iocb: if NULL, indicates that socket lock is already held
  820. * @sock: socket structure
  821. * @m: message to send
  822. * @dsz: length of data to be transmitted
  823. *
  824. * Used for SOCK_SEQPACKET messages.
  825. *
  826. * Returns the number of bytes sent on success, or errno otherwise
  827. */
  828. static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
  829. struct msghdr *m, size_t dsz)
  830. {
  831. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  832. return -EMSGSIZE;
  833. return tipc_send_stream(iocb, sock, m, dsz);
  834. }
  835. /**
  836. * auto_connect - complete connection setup to a remote port
  837. * @tsk: tipc socket structure
  838. * @msg: peer's response message
  839. *
  840. * Returns 0 on success, errno otherwise
  841. */
  842. static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
  843. {
  844. struct tipc_port *port = &tsk->port;
  845. struct socket *sock = tsk->sk.sk_socket;
  846. struct tipc_portid peer;
  847. peer.ref = msg_origport(msg);
  848. peer.node = msg_orignode(msg);
  849. __tipc_port_connect(port->ref, port, &peer);
  850. if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
  851. return -EINVAL;
  852. msg_set_importance(&port->phdr, (u32)msg_importance(msg));
  853. sock->state = SS_CONNECTED;
  854. return 0;
  855. }
  856. /**
  857. * set_orig_addr - capture sender's address for received message
  858. * @m: descriptor for message info
  859. * @msg: received message header
  860. *
  861. * Note: Address is not captured if not requested by receiver.
  862. */
  863. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  864. {
  865. DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
  866. if (addr) {
  867. addr->family = AF_TIPC;
  868. addr->addrtype = TIPC_ADDR_ID;
  869. memset(&addr->addr, 0, sizeof(addr->addr));
  870. addr->addr.id.ref = msg_origport(msg);
  871. addr->addr.id.node = msg_orignode(msg);
  872. addr->addr.name.domain = 0; /* could leave uninitialized */
  873. addr->scope = 0; /* could leave uninitialized */
  874. m->msg_namelen = sizeof(struct sockaddr_tipc);
  875. }
  876. }
  877. /**
  878. * anc_data_recv - optionally capture ancillary data for received message
  879. * @m: descriptor for message info
  880. * @msg: received message header
  881. * @tport: TIPC port associated with message
  882. *
  883. * Note: Ancillary data is not captured if not requested by receiver.
  884. *
  885. * Returns 0 if successful, otherwise errno
  886. */
  887. static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  888. struct tipc_port *tport)
  889. {
  890. u32 anc_data[3];
  891. u32 err;
  892. u32 dest_type;
  893. int has_name;
  894. int res;
  895. if (likely(m->msg_controllen == 0))
  896. return 0;
  897. /* Optionally capture errored message object(s) */
  898. err = msg ? msg_errcode(msg) : 0;
  899. if (unlikely(err)) {
  900. anc_data[0] = err;
  901. anc_data[1] = msg_data_sz(msg);
  902. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  903. if (res)
  904. return res;
  905. if (anc_data[1]) {
  906. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  907. msg_data(msg));
  908. if (res)
  909. return res;
  910. }
  911. }
  912. /* Optionally capture message destination object */
  913. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  914. switch (dest_type) {
  915. case TIPC_NAMED_MSG:
  916. has_name = 1;
  917. anc_data[0] = msg_nametype(msg);
  918. anc_data[1] = msg_namelower(msg);
  919. anc_data[2] = msg_namelower(msg);
  920. break;
  921. case TIPC_MCAST_MSG:
  922. has_name = 1;
  923. anc_data[0] = msg_nametype(msg);
  924. anc_data[1] = msg_namelower(msg);
  925. anc_data[2] = msg_nameupper(msg);
  926. break;
  927. case TIPC_CONN_MSG:
  928. has_name = (tport->conn_type != 0);
  929. anc_data[0] = tport->conn_type;
  930. anc_data[1] = tport->conn_instance;
  931. anc_data[2] = tport->conn_instance;
  932. break;
  933. default:
  934. has_name = 0;
  935. }
  936. if (has_name) {
  937. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  938. if (res)
  939. return res;
  940. }
  941. return 0;
  942. }
  943. static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
  944. {
  945. struct sock *sk = sock->sk;
  946. DEFINE_WAIT(wait);
  947. long timeo = *timeop;
  948. int err;
  949. for (;;) {
  950. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  951. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  952. if (sock->state == SS_DISCONNECTING) {
  953. err = -ENOTCONN;
  954. break;
  955. }
  956. release_sock(sk);
  957. timeo = schedule_timeout(timeo);
  958. lock_sock(sk);
  959. }
  960. err = 0;
  961. if (!skb_queue_empty(&sk->sk_receive_queue))
  962. break;
  963. err = sock_intr_errno(timeo);
  964. if (signal_pending(current))
  965. break;
  966. err = -EAGAIN;
  967. if (!timeo)
  968. break;
  969. }
  970. finish_wait(sk_sleep(sk), &wait);
  971. *timeop = timeo;
  972. return err;
  973. }
  974. /**
  975. * tipc_recvmsg - receive packet-oriented message
  976. * @iocb: (unused)
  977. * @m: descriptor for message info
  978. * @buf_len: total size of user buffer area
  979. * @flags: receive flags
  980. *
  981. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  982. * If the complete message doesn't fit in user area, truncate it.
  983. *
  984. * Returns size of returned message data, errno otherwise
  985. */
  986. static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
  987. struct msghdr *m, size_t buf_len, int flags)
  988. {
  989. struct sock *sk = sock->sk;
  990. struct tipc_sock *tsk = tipc_sk(sk);
  991. struct tipc_port *port = &tsk->port;
  992. struct sk_buff *buf;
  993. struct tipc_msg *msg;
  994. long timeo;
  995. unsigned int sz;
  996. u32 err;
  997. int res;
  998. /* Catch invalid receive requests */
  999. if (unlikely(!buf_len))
  1000. return -EINVAL;
  1001. lock_sock(sk);
  1002. if (unlikely(sock->state == SS_UNCONNECTED)) {
  1003. res = -ENOTCONN;
  1004. goto exit;
  1005. }
  1006. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1007. restart:
  1008. /* Look for a message in receive queue; wait if necessary */
  1009. res = tipc_wait_for_rcvmsg(sock, &timeo);
  1010. if (res)
  1011. goto exit;
  1012. /* Look at first message in receive queue */
  1013. buf = skb_peek(&sk->sk_receive_queue);
  1014. msg = buf_msg(buf);
  1015. sz = msg_data_sz(msg);
  1016. err = msg_errcode(msg);
  1017. /* Discard an empty non-errored message & try again */
  1018. if ((!sz) && (!err)) {
  1019. advance_rx_queue(sk);
  1020. goto restart;
  1021. }
  1022. /* Capture sender's address (optional) */
  1023. set_orig_addr(m, msg);
  1024. /* Capture ancillary data (optional) */
  1025. res = anc_data_recv(m, msg, port);
  1026. if (res)
  1027. goto exit;
  1028. /* Capture message data (if valid) & compute return value (always) */
  1029. if (!err) {
  1030. if (unlikely(buf_len < sz)) {
  1031. sz = buf_len;
  1032. m->msg_flags |= MSG_TRUNC;
  1033. }
  1034. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
  1035. m->msg_iov, sz);
  1036. if (res)
  1037. goto exit;
  1038. res = sz;
  1039. } else {
  1040. if ((sock->state == SS_READY) ||
  1041. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  1042. res = 0;
  1043. else
  1044. res = -ECONNRESET;
  1045. }
  1046. /* Consume received message (optional) */
  1047. if (likely(!(flags & MSG_PEEK))) {
  1048. if ((sock->state != SS_READY) &&
  1049. (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
  1050. tipc_acknowledge(port->ref, tsk->rcv_unacked);
  1051. tsk->rcv_unacked = 0;
  1052. }
  1053. advance_rx_queue(sk);
  1054. }
  1055. exit:
  1056. release_sock(sk);
  1057. return res;
  1058. }
  1059. /**
  1060. * tipc_recv_stream - receive stream-oriented data
  1061. * @iocb: (unused)
  1062. * @m: descriptor for message info
  1063. * @buf_len: total size of user buffer area
  1064. * @flags: receive flags
  1065. *
  1066. * Used for SOCK_STREAM messages only. If not enough data is available
  1067. * will optionally wait for more; never truncates data.
  1068. *
  1069. * Returns size of returned message data, errno otherwise
  1070. */
  1071. static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
  1072. struct msghdr *m, size_t buf_len, int flags)
  1073. {
  1074. struct sock *sk = sock->sk;
  1075. struct tipc_sock *tsk = tipc_sk(sk);
  1076. struct tipc_port *port = &tsk->port;
  1077. struct sk_buff *buf;
  1078. struct tipc_msg *msg;
  1079. long timeo;
  1080. unsigned int sz;
  1081. int sz_to_copy, target, needed;
  1082. int sz_copied = 0;
  1083. u32 err;
  1084. int res = 0;
  1085. /* Catch invalid receive attempts */
  1086. if (unlikely(!buf_len))
  1087. return -EINVAL;
  1088. lock_sock(sk);
  1089. if (unlikely(sock->state == SS_UNCONNECTED)) {
  1090. res = -ENOTCONN;
  1091. goto exit;
  1092. }
  1093. target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
  1094. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1095. restart:
  1096. /* Look for a message in receive queue; wait if necessary */
  1097. res = tipc_wait_for_rcvmsg(sock, &timeo);
  1098. if (res)
  1099. goto exit;
  1100. /* Look at first message in receive queue */
  1101. buf = skb_peek(&sk->sk_receive_queue);
  1102. msg = buf_msg(buf);
  1103. sz = msg_data_sz(msg);
  1104. err = msg_errcode(msg);
  1105. /* Discard an empty non-errored message & try again */
  1106. if ((!sz) && (!err)) {
  1107. advance_rx_queue(sk);
  1108. goto restart;
  1109. }
  1110. /* Optionally capture sender's address & ancillary data of first msg */
  1111. if (sz_copied == 0) {
  1112. set_orig_addr(m, msg);
  1113. res = anc_data_recv(m, msg, port);
  1114. if (res)
  1115. goto exit;
  1116. }
  1117. /* Capture message data (if valid) & compute return value (always) */
  1118. if (!err) {
  1119. u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
  1120. sz -= offset;
  1121. needed = (buf_len - sz_copied);
  1122. sz_to_copy = (sz <= needed) ? sz : needed;
  1123. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
  1124. m->msg_iov, sz_to_copy);
  1125. if (res)
  1126. goto exit;
  1127. sz_copied += sz_to_copy;
  1128. if (sz_to_copy < sz) {
  1129. if (!(flags & MSG_PEEK))
  1130. TIPC_SKB_CB(buf)->handle =
  1131. (void *)(unsigned long)(offset + sz_to_copy);
  1132. goto exit;
  1133. }
  1134. } else {
  1135. if (sz_copied != 0)
  1136. goto exit; /* can't add error msg to valid data */
  1137. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  1138. res = 0;
  1139. else
  1140. res = -ECONNRESET;
  1141. }
  1142. /* Consume received message (optional) */
  1143. if (likely(!(flags & MSG_PEEK))) {
  1144. if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
  1145. tipc_acknowledge(port->ref, tsk->rcv_unacked);
  1146. tsk->rcv_unacked = 0;
  1147. }
  1148. advance_rx_queue(sk);
  1149. }
  1150. /* Loop around if more data is required */
  1151. if ((sz_copied < buf_len) && /* didn't get all requested data */
  1152. (!skb_queue_empty(&sk->sk_receive_queue) ||
  1153. (sz_copied < target)) && /* and more is ready or required */
  1154. (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
  1155. (!err)) /* and haven't reached a FIN */
  1156. goto restart;
  1157. exit:
  1158. release_sock(sk);
  1159. return sz_copied ? sz_copied : res;
  1160. }
  1161. /**
  1162. * tipc_write_space - wake up thread if port congestion is released
  1163. * @sk: socket
  1164. */
  1165. static void tipc_write_space(struct sock *sk)
  1166. {
  1167. struct socket_wq *wq;
  1168. rcu_read_lock();
  1169. wq = rcu_dereference(sk->sk_wq);
  1170. if (wq_has_sleeper(wq))
  1171. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1172. POLLWRNORM | POLLWRBAND);
  1173. rcu_read_unlock();
  1174. }
  1175. /**
  1176. * tipc_data_ready - wake up threads to indicate messages have been received
  1177. * @sk: socket
  1178. * @len: the length of messages
  1179. */
  1180. static void tipc_data_ready(struct sock *sk)
  1181. {
  1182. struct socket_wq *wq;
  1183. rcu_read_lock();
  1184. wq = rcu_dereference(sk->sk_wq);
  1185. if (wq_has_sleeper(wq))
  1186. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  1187. POLLRDNORM | POLLRDBAND);
  1188. rcu_read_unlock();
  1189. }
  1190. /**
  1191. * filter_connect - Handle all incoming messages for a connection-based socket
  1192. * @tsk: TIPC socket
  1193. * @msg: message
  1194. *
  1195. * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
  1196. */
  1197. static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
  1198. {
  1199. struct sock *sk = &tsk->sk;
  1200. struct tipc_port *port = &tsk->port;
  1201. struct socket *sock = sk->sk_socket;
  1202. struct tipc_msg *msg = buf_msg(*buf);
  1203. int retval = -TIPC_ERR_NO_PORT;
  1204. int res;
  1205. if (msg_mcast(msg))
  1206. return retval;
  1207. switch ((int)sock->state) {
  1208. case SS_CONNECTED:
  1209. /* Accept only connection-based messages sent by peer */
  1210. if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
  1211. if (unlikely(msg_errcode(msg))) {
  1212. sock->state = SS_DISCONNECTING;
  1213. __tipc_port_disconnect(port);
  1214. }
  1215. retval = TIPC_OK;
  1216. }
  1217. break;
  1218. case SS_CONNECTING:
  1219. /* Accept only ACK or NACK message */
  1220. if (unlikely(msg_errcode(msg))) {
  1221. sock->state = SS_DISCONNECTING;
  1222. sk->sk_err = ECONNREFUSED;
  1223. retval = TIPC_OK;
  1224. break;
  1225. }
  1226. if (unlikely(!msg_connected(msg)))
  1227. break;
  1228. res = auto_connect(tsk, msg);
  1229. if (res) {
  1230. sock->state = SS_DISCONNECTING;
  1231. sk->sk_err = -res;
  1232. retval = TIPC_OK;
  1233. break;
  1234. }
  1235. /* If an incoming message is an 'ACK-', it should be
  1236. * discarded here because it doesn't contain useful
  1237. * data. In addition, we should try to wake up
  1238. * connect() routine if sleeping.
  1239. */
  1240. if (msg_data_sz(msg) == 0) {
  1241. kfree_skb(*buf);
  1242. *buf = NULL;
  1243. if (waitqueue_active(sk_sleep(sk)))
  1244. wake_up_interruptible(sk_sleep(sk));
  1245. }
  1246. retval = TIPC_OK;
  1247. break;
  1248. case SS_LISTENING:
  1249. case SS_UNCONNECTED:
  1250. /* Accept only SYN message */
  1251. if (!msg_connected(msg) && !(msg_errcode(msg)))
  1252. retval = TIPC_OK;
  1253. break;
  1254. case SS_DISCONNECTING:
  1255. break;
  1256. default:
  1257. pr_err("Unknown socket state %u\n", sock->state);
  1258. }
  1259. return retval;
  1260. }
  1261. /**
  1262. * rcvbuf_limit - get proper overload limit of socket receive queue
  1263. * @sk: socket
  1264. * @buf: message
  1265. *
  1266. * For all connection oriented messages, irrespective of importance,
  1267. * the default overload value (i.e. 67MB) is set as limit.
  1268. *
  1269. * For all connectionless messages, by default new queue limits are
  1270. * as belows:
  1271. *
  1272. * TIPC_LOW_IMPORTANCE (4 MB)
  1273. * TIPC_MEDIUM_IMPORTANCE (8 MB)
  1274. * TIPC_HIGH_IMPORTANCE (16 MB)
  1275. * TIPC_CRITICAL_IMPORTANCE (32 MB)
  1276. *
  1277. * Returns overload limit according to corresponding message importance
  1278. */
  1279. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
  1280. {
  1281. struct tipc_msg *msg = buf_msg(buf);
  1282. if (msg_connected(msg))
  1283. return sysctl_tipc_rmem[2];
  1284. return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
  1285. msg_importance(msg);
  1286. }
  1287. /**
  1288. * filter_rcv - validate incoming message
  1289. * @sk: socket
  1290. * @buf: message
  1291. *
  1292. * Enqueues message on receive queue if acceptable; optionally handles
  1293. * disconnect indication for a connected socket.
  1294. *
  1295. * Called with socket lock already taken; port lock may also be taken.
  1296. *
  1297. * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
  1298. * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
  1299. */
  1300. static int filter_rcv(struct sock *sk, struct sk_buff *buf)
  1301. {
  1302. struct socket *sock = sk->sk_socket;
  1303. struct tipc_sock *tsk = tipc_sk(sk);
  1304. struct tipc_msg *msg = buf_msg(buf);
  1305. unsigned int limit = rcvbuf_limit(sk, buf);
  1306. u32 onode;
  1307. int rc = TIPC_OK;
  1308. if (unlikely(msg_user(msg) == CONN_MANAGER))
  1309. return tipc_sk_proto_rcv(tsk, &onode, buf);
  1310. /* Reject message if it is wrong sort of message for socket */
  1311. if (msg_type(msg) > TIPC_DIRECT_MSG)
  1312. return -TIPC_ERR_NO_PORT;
  1313. if (sock->state == SS_READY) {
  1314. if (msg_connected(msg))
  1315. return -TIPC_ERR_NO_PORT;
  1316. } else {
  1317. rc = filter_connect(tsk, &buf);
  1318. if (rc != TIPC_OK || buf == NULL)
  1319. return rc;
  1320. }
  1321. /* Reject message if there isn't room to queue it */
  1322. if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
  1323. return -TIPC_ERR_OVERLOAD;
  1324. /* Enqueue message */
  1325. TIPC_SKB_CB(buf)->handle = NULL;
  1326. __skb_queue_tail(&sk->sk_receive_queue, buf);
  1327. skb_set_owner_r(buf, sk);
  1328. sk->sk_data_ready(sk);
  1329. return TIPC_OK;
  1330. }
  1331. /**
  1332. * tipc_backlog_rcv - handle incoming message from backlog queue
  1333. * @sk: socket
  1334. * @buf: message
  1335. *
  1336. * Caller must hold socket lock, but not port lock.
  1337. *
  1338. * Returns 0
  1339. */
  1340. static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
  1341. {
  1342. int rc;
  1343. u32 onode;
  1344. struct tipc_sock *tsk = tipc_sk(sk);
  1345. uint truesize = buf->truesize;
  1346. rc = filter_rcv(sk, buf);
  1347. if (likely(!rc)) {
  1348. if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
  1349. atomic_add(truesize, &tsk->dupl_rcvcnt);
  1350. return 0;
  1351. }
  1352. if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
  1353. return 0;
  1354. tipc_link_xmit2(buf, onode, 0);
  1355. return 0;
  1356. }
  1357. /**
  1358. * tipc_sk_rcv - handle incoming message
  1359. * @buf: buffer containing arriving message
  1360. * Consumes buffer
  1361. * Returns 0 if success, or errno: -EHOSTUNREACH
  1362. */
  1363. int tipc_sk_rcv(struct sk_buff *buf)
  1364. {
  1365. struct tipc_sock *tsk;
  1366. struct tipc_port *port;
  1367. struct sock *sk;
  1368. u32 dport = msg_destport(buf_msg(buf));
  1369. int rc = TIPC_OK;
  1370. uint limit;
  1371. u32 dnode;
  1372. /* Validate destination and message */
  1373. port = tipc_port_lock(dport);
  1374. if (unlikely(!port)) {
  1375. rc = tipc_msg_eval(buf, &dnode);
  1376. goto exit;
  1377. }
  1378. tsk = tipc_port_to_sock(port);
  1379. sk = &tsk->sk;
  1380. /* Queue message */
  1381. bh_lock_sock(sk);
  1382. if (!sock_owned_by_user(sk)) {
  1383. rc = filter_rcv(sk, buf);
  1384. } else {
  1385. if (sk->sk_backlog.len == 0)
  1386. atomic_set(&tsk->dupl_rcvcnt, 0);
  1387. limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
  1388. if (sk_add_backlog(sk, buf, limit))
  1389. rc = -TIPC_ERR_OVERLOAD;
  1390. }
  1391. bh_unlock_sock(sk);
  1392. tipc_port_unlock(port);
  1393. if (likely(!rc))
  1394. return 0;
  1395. exit:
  1396. if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
  1397. return -EHOSTUNREACH;
  1398. tipc_link_xmit2(buf, dnode, 0);
  1399. return (rc < 0) ? -EHOSTUNREACH : 0;
  1400. }
  1401. static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
  1402. {
  1403. struct sock *sk = sock->sk;
  1404. DEFINE_WAIT(wait);
  1405. int done;
  1406. do {
  1407. int err = sock_error(sk);
  1408. if (err)
  1409. return err;
  1410. if (!*timeo_p)
  1411. return -ETIMEDOUT;
  1412. if (signal_pending(current))
  1413. return sock_intr_errno(*timeo_p);
  1414. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1415. done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
  1416. finish_wait(sk_sleep(sk), &wait);
  1417. } while (!done);
  1418. return 0;
  1419. }
  1420. /**
  1421. * tipc_connect - establish a connection to another TIPC port
  1422. * @sock: socket structure
  1423. * @dest: socket address for destination port
  1424. * @destlen: size of socket address data structure
  1425. * @flags: file-related flags associated with socket
  1426. *
  1427. * Returns 0 on success, errno otherwise
  1428. */
  1429. static int tipc_connect(struct socket *sock, struct sockaddr *dest,
  1430. int destlen, int flags)
  1431. {
  1432. struct sock *sk = sock->sk;
  1433. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1434. struct msghdr m = {NULL,};
  1435. long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
  1436. socket_state previous;
  1437. int res;
  1438. lock_sock(sk);
  1439. /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
  1440. if (sock->state == SS_READY) {
  1441. res = -EOPNOTSUPP;
  1442. goto exit;
  1443. }
  1444. /*
  1445. * Reject connection attempt using multicast address
  1446. *
  1447. * Note: send_msg() validates the rest of the address fields,
  1448. * so there's no need to do it here
  1449. */
  1450. if (dst->addrtype == TIPC_ADDR_MCAST) {
  1451. res = -EINVAL;
  1452. goto exit;
  1453. }
  1454. previous = sock->state;
  1455. switch (sock->state) {
  1456. case SS_UNCONNECTED:
  1457. /* Send a 'SYN-' to destination */
  1458. m.msg_name = dest;
  1459. m.msg_namelen = destlen;
  1460. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  1461. * indicate send_msg() is never blocked.
  1462. */
  1463. if (!timeout)
  1464. m.msg_flags = MSG_DONTWAIT;
  1465. res = tipc_sendmsg(NULL, sock, &m, 0);
  1466. if ((res < 0) && (res != -EWOULDBLOCK))
  1467. goto exit;
  1468. /* Just entered SS_CONNECTING state; the only
  1469. * difference is that return value in non-blocking
  1470. * case is EINPROGRESS, rather than EALREADY.
  1471. */
  1472. res = -EINPROGRESS;
  1473. case SS_CONNECTING:
  1474. if (previous == SS_CONNECTING)
  1475. res = -EALREADY;
  1476. if (!timeout)
  1477. goto exit;
  1478. timeout = msecs_to_jiffies(timeout);
  1479. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  1480. res = tipc_wait_for_connect(sock, &timeout);
  1481. break;
  1482. case SS_CONNECTED:
  1483. res = -EISCONN;
  1484. break;
  1485. default:
  1486. res = -EINVAL;
  1487. break;
  1488. }
  1489. exit:
  1490. release_sock(sk);
  1491. return res;
  1492. }
  1493. /**
  1494. * tipc_listen - allow socket to listen for incoming connections
  1495. * @sock: socket structure
  1496. * @len: (unused)
  1497. *
  1498. * Returns 0 on success, errno otherwise
  1499. */
  1500. static int tipc_listen(struct socket *sock, int len)
  1501. {
  1502. struct sock *sk = sock->sk;
  1503. int res;
  1504. lock_sock(sk);
  1505. if (sock->state != SS_UNCONNECTED)
  1506. res = -EINVAL;
  1507. else {
  1508. sock->state = SS_LISTENING;
  1509. res = 0;
  1510. }
  1511. release_sock(sk);
  1512. return res;
  1513. }
  1514. static int tipc_wait_for_accept(struct socket *sock, long timeo)
  1515. {
  1516. struct sock *sk = sock->sk;
  1517. DEFINE_WAIT(wait);
  1518. int err;
  1519. /* True wake-one mechanism for incoming connections: only
  1520. * one process gets woken up, not the 'whole herd'.
  1521. * Since we do not 'race & poll' for established sockets
  1522. * anymore, the common case will execute the loop only once.
  1523. */
  1524. for (;;) {
  1525. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  1526. TASK_INTERRUPTIBLE);
  1527. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1528. release_sock(sk);
  1529. timeo = schedule_timeout(timeo);
  1530. lock_sock(sk);
  1531. }
  1532. err = 0;
  1533. if (!skb_queue_empty(&sk->sk_receive_queue))
  1534. break;
  1535. err = -EINVAL;
  1536. if (sock->state != SS_LISTENING)
  1537. break;
  1538. err = sock_intr_errno(timeo);
  1539. if (signal_pending(current))
  1540. break;
  1541. err = -EAGAIN;
  1542. if (!timeo)
  1543. break;
  1544. }
  1545. finish_wait(sk_sleep(sk), &wait);
  1546. return err;
  1547. }
  1548. /**
  1549. * tipc_accept - wait for connection request
  1550. * @sock: listening socket
  1551. * @newsock: new socket that is to be connected
  1552. * @flags: file-related flags associated with socket
  1553. *
  1554. * Returns 0 on success, errno otherwise
  1555. */
  1556. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
  1557. {
  1558. struct sock *new_sk, *sk = sock->sk;
  1559. struct sk_buff *buf;
  1560. struct tipc_port *new_port;
  1561. struct tipc_msg *msg;
  1562. struct tipc_portid peer;
  1563. u32 new_ref;
  1564. long timeo;
  1565. int res;
  1566. lock_sock(sk);
  1567. if (sock->state != SS_LISTENING) {
  1568. res = -EINVAL;
  1569. goto exit;
  1570. }
  1571. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  1572. res = tipc_wait_for_accept(sock, timeo);
  1573. if (res)
  1574. goto exit;
  1575. buf = skb_peek(&sk->sk_receive_queue);
  1576. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
  1577. if (res)
  1578. goto exit;
  1579. new_sk = new_sock->sk;
  1580. new_port = &tipc_sk(new_sk)->port;
  1581. new_ref = new_port->ref;
  1582. msg = buf_msg(buf);
  1583. /* we lock on new_sk; but lockdep sees the lock on sk */
  1584. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  1585. /*
  1586. * Reject any stray messages received by new socket
  1587. * before the socket lock was taken (very, very unlikely)
  1588. */
  1589. reject_rx_queue(new_sk);
  1590. /* Connect new socket to it's peer */
  1591. peer.ref = msg_origport(msg);
  1592. peer.node = msg_orignode(msg);
  1593. tipc_port_connect(new_ref, &peer);
  1594. new_sock->state = SS_CONNECTED;
  1595. tipc_port_set_importance(new_port, msg_importance(msg));
  1596. if (msg_named(msg)) {
  1597. new_port->conn_type = msg_nametype(msg);
  1598. new_port->conn_instance = msg_nameinst(msg);
  1599. }
  1600. /*
  1601. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1602. * Respond to 'SYN+' by queuing it on new socket.
  1603. */
  1604. if (!msg_data_sz(msg)) {
  1605. struct msghdr m = {NULL,};
  1606. advance_rx_queue(sk);
  1607. tipc_send_packet(NULL, new_sock, &m, 0);
  1608. } else {
  1609. __skb_dequeue(&sk->sk_receive_queue);
  1610. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  1611. skb_set_owner_r(buf, new_sk);
  1612. }
  1613. release_sock(new_sk);
  1614. exit:
  1615. release_sock(sk);
  1616. return res;
  1617. }
  1618. /**
  1619. * tipc_shutdown - shutdown socket connection
  1620. * @sock: socket structure
  1621. * @how: direction to close (must be SHUT_RDWR)
  1622. *
  1623. * Terminates connection (if necessary), then purges socket's receive queue.
  1624. *
  1625. * Returns 0 on success, errno otherwise
  1626. */
  1627. static int tipc_shutdown(struct socket *sock, int how)
  1628. {
  1629. struct sock *sk = sock->sk;
  1630. struct tipc_sock *tsk = tipc_sk(sk);
  1631. struct tipc_port *port = &tsk->port;
  1632. struct sk_buff *buf;
  1633. u32 peer;
  1634. int res;
  1635. if (how != SHUT_RDWR)
  1636. return -EINVAL;
  1637. lock_sock(sk);
  1638. switch (sock->state) {
  1639. case SS_CONNECTING:
  1640. case SS_CONNECTED:
  1641. restart:
  1642. /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
  1643. buf = __skb_dequeue(&sk->sk_receive_queue);
  1644. if (buf) {
  1645. if (TIPC_SKB_CB(buf)->handle != NULL) {
  1646. kfree_skb(buf);
  1647. goto restart;
  1648. }
  1649. tipc_port_disconnect(port->ref);
  1650. if (tipc_msg_reverse(buf, &peer, TIPC_CONN_SHUTDOWN))
  1651. tipc_link_xmit2(buf, peer, 0);
  1652. } else {
  1653. tipc_port_shutdown(port->ref);
  1654. }
  1655. sock->state = SS_DISCONNECTING;
  1656. /* fall through */
  1657. case SS_DISCONNECTING:
  1658. /* Discard any unreceived messages */
  1659. __skb_queue_purge(&sk->sk_receive_queue);
  1660. /* Wake up anyone sleeping in poll */
  1661. sk->sk_state_change(sk);
  1662. res = 0;
  1663. break;
  1664. default:
  1665. res = -ENOTCONN;
  1666. }
  1667. release_sock(sk);
  1668. return res;
  1669. }
  1670. /**
  1671. * tipc_setsockopt - set socket option
  1672. * @sock: socket structure
  1673. * @lvl: option level
  1674. * @opt: option identifier
  1675. * @ov: pointer to new option value
  1676. * @ol: length of option value
  1677. *
  1678. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  1679. * (to ease compatibility).
  1680. *
  1681. * Returns 0 on success, errno otherwise
  1682. */
  1683. static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
  1684. char __user *ov, unsigned int ol)
  1685. {
  1686. struct sock *sk = sock->sk;
  1687. struct tipc_sock *tsk = tipc_sk(sk);
  1688. struct tipc_port *port = &tsk->port;
  1689. u32 value;
  1690. int res;
  1691. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1692. return 0;
  1693. if (lvl != SOL_TIPC)
  1694. return -ENOPROTOOPT;
  1695. if (ol < sizeof(value))
  1696. return -EINVAL;
  1697. res = get_user(value, (u32 __user *)ov);
  1698. if (res)
  1699. return res;
  1700. lock_sock(sk);
  1701. switch (opt) {
  1702. case TIPC_IMPORTANCE:
  1703. tipc_port_set_importance(port, value);
  1704. break;
  1705. case TIPC_SRC_DROPPABLE:
  1706. if (sock->type != SOCK_STREAM)
  1707. tipc_port_set_unreliable(port, value);
  1708. else
  1709. res = -ENOPROTOOPT;
  1710. break;
  1711. case TIPC_DEST_DROPPABLE:
  1712. tipc_port_set_unreturnable(port, value);
  1713. break;
  1714. case TIPC_CONN_TIMEOUT:
  1715. tipc_sk(sk)->conn_timeout = value;
  1716. /* no need to set "res", since already 0 at this point */
  1717. break;
  1718. default:
  1719. res = -EINVAL;
  1720. }
  1721. release_sock(sk);
  1722. return res;
  1723. }
  1724. /**
  1725. * tipc_getsockopt - get socket option
  1726. * @sock: socket structure
  1727. * @lvl: option level
  1728. * @opt: option identifier
  1729. * @ov: receptacle for option value
  1730. * @ol: receptacle for length of option value
  1731. *
  1732. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  1733. * (to ease compatibility).
  1734. *
  1735. * Returns 0 on success, errno otherwise
  1736. */
  1737. static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
  1738. char __user *ov, int __user *ol)
  1739. {
  1740. struct sock *sk = sock->sk;
  1741. struct tipc_sock *tsk = tipc_sk(sk);
  1742. struct tipc_port *port = &tsk->port;
  1743. int len;
  1744. u32 value;
  1745. int res;
  1746. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1747. return put_user(0, ol);
  1748. if (lvl != SOL_TIPC)
  1749. return -ENOPROTOOPT;
  1750. res = get_user(len, ol);
  1751. if (res)
  1752. return res;
  1753. lock_sock(sk);
  1754. switch (opt) {
  1755. case TIPC_IMPORTANCE:
  1756. value = tipc_port_importance(port);
  1757. break;
  1758. case TIPC_SRC_DROPPABLE:
  1759. value = tipc_port_unreliable(port);
  1760. break;
  1761. case TIPC_DEST_DROPPABLE:
  1762. value = tipc_port_unreturnable(port);
  1763. break;
  1764. case TIPC_CONN_TIMEOUT:
  1765. value = tipc_sk(sk)->conn_timeout;
  1766. /* no need to set "res", since already 0 at this point */
  1767. break;
  1768. case TIPC_NODE_RECVQ_DEPTH:
  1769. value = 0; /* was tipc_queue_size, now obsolete */
  1770. break;
  1771. case TIPC_SOCK_RECVQ_DEPTH:
  1772. value = skb_queue_len(&sk->sk_receive_queue);
  1773. break;
  1774. default:
  1775. res = -EINVAL;
  1776. }
  1777. release_sock(sk);
  1778. if (res)
  1779. return res; /* "get" failed */
  1780. if (len < sizeof(value))
  1781. return -EINVAL;
  1782. if (copy_to_user(ov, &value, sizeof(value)))
  1783. return -EFAULT;
  1784. return put_user(sizeof(value), ol);
  1785. }
  1786. int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
  1787. {
  1788. struct tipc_sioc_ln_req lnr;
  1789. void __user *argp = (void __user *)arg;
  1790. switch (cmd) {
  1791. case SIOCGETLINKNAME:
  1792. if (copy_from_user(&lnr, argp, sizeof(lnr)))
  1793. return -EFAULT;
  1794. if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
  1795. lnr.linkname, TIPC_MAX_LINK_NAME)) {
  1796. if (copy_to_user(argp, &lnr, sizeof(lnr)))
  1797. return -EFAULT;
  1798. return 0;
  1799. }
  1800. return -EADDRNOTAVAIL;
  1801. break;
  1802. default:
  1803. return -ENOIOCTLCMD;
  1804. }
  1805. }
  1806. /* Protocol switches for the various types of TIPC sockets */
  1807. static const struct proto_ops msg_ops = {
  1808. .owner = THIS_MODULE,
  1809. .family = AF_TIPC,
  1810. .release = tipc_release,
  1811. .bind = tipc_bind,
  1812. .connect = tipc_connect,
  1813. .socketpair = sock_no_socketpair,
  1814. .accept = sock_no_accept,
  1815. .getname = tipc_getname,
  1816. .poll = tipc_poll,
  1817. .ioctl = tipc_ioctl,
  1818. .listen = sock_no_listen,
  1819. .shutdown = tipc_shutdown,
  1820. .setsockopt = tipc_setsockopt,
  1821. .getsockopt = tipc_getsockopt,
  1822. .sendmsg = tipc_sendmsg,
  1823. .recvmsg = tipc_recvmsg,
  1824. .mmap = sock_no_mmap,
  1825. .sendpage = sock_no_sendpage
  1826. };
  1827. static const struct proto_ops packet_ops = {
  1828. .owner = THIS_MODULE,
  1829. .family = AF_TIPC,
  1830. .release = tipc_release,
  1831. .bind = tipc_bind,
  1832. .connect = tipc_connect,
  1833. .socketpair = sock_no_socketpair,
  1834. .accept = tipc_accept,
  1835. .getname = tipc_getname,
  1836. .poll = tipc_poll,
  1837. .ioctl = tipc_ioctl,
  1838. .listen = tipc_listen,
  1839. .shutdown = tipc_shutdown,
  1840. .setsockopt = tipc_setsockopt,
  1841. .getsockopt = tipc_getsockopt,
  1842. .sendmsg = tipc_send_packet,
  1843. .recvmsg = tipc_recvmsg,
  1844. .mmap = sock_no_mmap,
  1845. .sendpage = sock_no_sendpage
  1846. };
  1847. static const struct proto_ops stream_ops = {
  1848. .owner = THIS_MODULE,
  1849. .family = AF_TIPC,
  1850. .release = tipc_release,
  1851. .bind = tipc_bind,
  1852. .connect = tipc_connect,
  1853. .socketpair = sock_no_socketpair,
  1854. .accept = tipc_accept,
  1855. .getname = tipc_getname,
  1856. .poll = tipc_poll,
  1857. .ioctl = tipc_ioctl,
  1858. .listen = tipc_listen,
  1859. .shutdown = tipc_shutdown,
  1860. .setsockopt = tipc_setsockopt,
  1861. .getsockopt = tipc_getsockopt,
  1862. .sendmsg = tipc_send_stream,
  1863. .recvmsg = tipc_recv_stream,
  1864. .mmap = sock_no_mmap,
  1865. .sendpage = sock_no_sendpage
  1866. };
  1867. static const struct net_proto_family tipc_family_ops = {
  1868. .owner = THIS_MODULE,
  1869. .family = AF_TIPC,
  1870. .create = tipc_sk_create
  1871. };
  1872. static struct proto tipc_proto = {
  1873. .name = "TIPC",
  1874. .owner = THIS_MODULE,
  1875. .obj_size = sizeof(struct tipc_sock),
  1876. .sysctl_rmem = sysctl_tipc_rmem
  1877. };
  1878. static struct proto tipc_proto_kern = {
  1879. .name = "TIPC",
  1880. .obj_size = sizeof(struct tipc_sock),
  1881. .sysctl_rmem = sysctl_tipc_rmem
  1882. };
  1883. /**
  1884. * tipc_socket_init - initialize TIPC socket interface
  1885. *
  1886. * Returns 0 on success, errno otherwise
  1887. */
  1888. int tipc_socket_init(void)
  1889. {
  1890. int res;
  1891. res = proto_register(&tipc_proto, 1);
  1892. if (res) {
  1893. pr_err("Failed to register TIPC protocol type\n");
  1894. goto out;
  1895. }
  1896. res = sock_register(&tipc_family_ops);
  1897. if (res) {
  1898. pr_err("Failed to register TIPC socket type\n");
  1899. proto_unregister(&tipc_proto);
  1900. goto out;
  1901. }
  1902. out:
  1903. return res;
  1904. }
  1905. /**
  1906. * tipc_socket_stop - stop TIPC socket interface
  1907. */
  1908. void tipc_socket_stop(void)
  1909. {
  1910. sock_unregister(tipc_family_ops.family);
  1911. proto_unregister(&tipc_proto);
  1912. }