socket.c 61 KB

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