socket.c 61 KB

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