socket.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882
  1. /*
  2. * net/tipc/socket.c: TIPC socket API
  3. *
  4. * Copyright (c) 2001-2007, 2012-2015, 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 <linux/rhashtable.h>
  37. #include "core.h"
  38. #include "name_table.h"
  39. #include "node.h"
  40. #include "link.h"
  41. #include "name_distr.h"
  42. #include "socket.h"
  43. #include "bcast.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 msecs_to_jiffies(3600000) /* [ms] => 1 h */
  48. #define TIPC_FWD_MSG 1
  49. #define TIPC_CONN_OK 0
  50. #define TIPC_CONN_PROBING 1
  51. #define TIPC_MAX_PORT 0xffffffff
  52. #define TIPC_MIN_PORT 1
  53. /**
  54. * struct tipc_sock - TIPC socket structure
  55. * @sk: socket - interacts with 'port' and with user via the socket API
  56. * @connected: non-zero if port is currently connected to a peer port
  57. * @conn_type: TIPC type used when connection was established
  58. * @conn_instance: TIPC instance used when connection was established
  59. * @published: non-zero if port has one or more associated names
  60. * @max_pkt: maximum packet size "hint" used when building messages sent by port
  61. * @portid: unique port identity in TIPC socket hash table
  62. * @phdr: preformatted message header used when sending messages
  63. * @port_list: adjacent ports in TIPC's global list of ports
  64. * @publications: list of publications for port
  65. * @pub_count: total # of publications port has made during its lifetime
  66. * @probing_state:
  67. * @probing_intv:
  68. * @conn_timeout: the time we can wait for an unresponded setup request
  69. * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
  70. * @link_cong: non-zero if owner must sleep because of link congestion
  71. * @sent_unacked: # messages sent by socket, and not yet acked by peer
  72. * @rcv_unacked: # messages read by user, but not yet acked back to peer
  73. * @remote: 'connected' peer for dgram/rdm
  74. * @node: hash table node
  75. * @rcu: rcu struct for tipc_sock
  76. */
  77. struct tipc_sock {
  78. struct sock sk;
  79. int connected;
  80. u32 conn_type;
  81. u32 conn_instance;
  82. int published;
  83. u32 max_pkt;
  84. u32 portid;
  85. struct tipc_msg phdr;
  86. struct list_head sock_list;
  87. struct list_head publications;
  88. u32 pub_count;
  89. u32 probing_state;
  90. unsigned long probing_intv;
  91. uint conn_timeout;
  92. atomic_t dupl_rcvcnt;
  93. bool link_cong;
  94. uint sent_unacked;
  95. uint rcv_unacked;
  96. struct sockaddr_tipc remote;
  97. struct rhash_head node;
  98. struct rcu_head rcu;
  99. };
  100. static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
  101. static void tipc_data_ready(struct sock *sk);
  102. static void tipc_write_space(struct sock *sk);
  103. static void tipc_sock_destruct(struct sock *sk);
  104. static int tipc_release(struct socket *sock);
  105. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
  106. static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
  107. static void tipc_sk_timeout(unsigned long data);
  108. static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
  109. struct tipc_name_seq const *seq);
  110. static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
  111. struct tipc_name_seq const *seq);
  112. static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
  113. static int tipc_sk_insert(struct tipc_sock *tsk);
  114. static void tipc_sk_remove(struct tipc_sock *tsk);
  115. static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
  116. size_t dsz);
  117. static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
  118. static const struct proto_ops packet_ops;
  119. static const struct proto_ops stream_ops;
  120. static const struct proto_ops msg_ops;
  121. static struct proto tipc_proto;
  122. static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
  123. [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC },
  124. [TIPC_NLA_SOCK_ADDR] = { .type = NLA_U32 },
  125. [TIPC_NLA_SOCK_REF] = { .type = NLA_U32 },
  126. [TIPC_NLA_SOCK_CON] = { .type = NLA_NESTED },
  127. [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG }
  128. };
  129. static const struct rhashtable_params tsk_rht_params;
  130. /*
  131. * Revised TIPC socket locking policy:
  132. *
  133. * Most socket operations take the standard socket lock when they start
  134. * and hold it until they finish (or until they need to sleep). Acquiring
  135. * this lock grants the owner exclusive access to the fields of the socket
  136. * data structures, with the exception of the backlog queue. A few socket
  137. * operations can be done without taking the socket lock because they only
  138. * read socket information that never changes during the life of the socket.
  139. *
  140. * Socket operations may acquire the lock for the associated TIPC port if they
  141. * need to perform an operation on the port. If any routine needs to acquire
  142. * both the socket lock and the port lock it must take the socket lock first
  143. * to avoid the risk of deadlock.
  144. *
  145. * The dispatcher handling incoming messages cannot grab the socket lock in
  146. * the standard fashion, since invoked it runs at the BH level and cannot block.
  147. * Instead, it checks to see if the socket lock is currently owned by someone,
  148. * and either handles the message itself or adds it to the socket's backlog
  149. * queue; in the latter case the queued message is processed once the process
  150. * owning the socket lock releases it.
  151. *
  152. * NOTE: Releasing the socket lock while an operation is sleeping overcomes
  153. * the problem of a blocked socket operation preventing any other operations
  154. * from occurring. However, applications must be careful if they have
  155. * multiple threads trying to send (or receive) on the same socket, as these
  156. * operations might interfere with each other. For example, doing a connect
  157. * and a receive at the same time might allow the receive to consume the
  158. * ACK message meant for the connect. While additional work could be done
  159. * to try and overcome this, it doesn't seem to be worthwhile at the present.
  160. *
  161. * NOTE: Releasing the socket lock while an operation is sleeping also ensures
  162. * that another operation that must be performed in a non-blocking manner is
  163. * not delayed for very long because the lock has already been taken.
  164. *
  165. * NOTE: This code assumes that certain fields of a port/socket pair are
  166. * constant over its lifetime; such fields can be examined without taking
  167. * the socket lock and/or port lock, and do not need to be re-read even
  168. * after resuming processing after waiting. These fields include:
  169. * - socket type
  170. * - pointer to socket sk structure (aka tipc_sock structure)
  171. * - pointer to port structure
  172. * - port reference
  173. */
  174. static u32 tsk_own_node(struct tipc_sock *tsk)
  175. {
  176. return msg_prevnode(&tsk->phdr);
  177. }
  178. static u32 tsk_peer_node(struct tipc_sock *tsk)
  179. {
  180. return msg_destnode(&tsk->phdr);
  181. }
  182. static u32 tsk_peer_port(struct tipc_sock *tsk)
  183. {
  184. return msg_destport(&tsk->phdr);
  185. }
  186. static bool tsk_unreliable(struct tipc_sock *tsk)
  187. {
  188. return msg_src_droppable(&tsk->phdr) != 0;
  189. }
  190. static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
  191. {
  192. msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
  193. }
  194. static bool tsk_unreturnable(struct tipc_sock *tsk)
  195. {
  196. return msg_dest_droppable(&tsk->phdr) != 0;
  197. }
  198. static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
  199. {
  200. msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
  201. }
  202. static int tsk_importance(struct tipc_sock *tsk)
  203. {
  204. return msg_importance(&tsk->phdr);
  205. }
  206. static int tsk_set_importance(struct tipc_sock *tsk, int imp)
  207. {
  208. if (imp > TIPC_CRITICAL_IMPORTANCE)
  209. return -EINVAL;
  210. msg_set_importance(&tsk->phdr, (u32)imp);
  211. return 0;
  212. }
  213. static struct tipc_sock *tipc_sk(const struct sock *sk)
  214. {
  215. return container_of(sk, struct tipc_sock, sk);
  216. }
  217. static int tsk_conn_cong(struct tipc_sock *tsk)
  218. {
  219. return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN;
  220. }
  221. /**
  222. * tsk_advance_rx_queue - discard first buffer in socket receive queue
  223. *
  224. * Caller must hold socket lock
  225. */
  226. static void tsk_advance_rx_queue(struct sock *sk)
  227. {
  228. kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
  229. }
  230. /* tipc_sk_respond() : send response message back to sender
  231. */
  232. static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
  233. {
  234. u32 selector;
  235. u32 dnode;
  236. u32 onode = tipc_own_addr(sock_net(sk));
  237. if (!tipc_msg_reverse(onode, &skb, err))
  238. return;
  239. dnode = msg_destnode(buf_msg(skb));
  240. selector = msg_origport(buf_msg(skb));
  241. tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
  242. }
  243. /**
  244. * tsk_rej_rx_queue - reject all buffers in socket receive queue
  245. *
  246. * Caller must hold socket lock
  247. */
  248. static void tsk_rej_rx_queue(struct sock *sk)
  249. {
  250. struct sk_buff *skb;
  251. while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
  252. tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
  253. }
  254. /* tsk_peer_msg - verify if message was sent by connected port's peer
  255. *
  256. * Handles cases where the node's network address has changed from
  257. * the default of <0.0.0> to its configured setting.
  258. */
  259. static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
  260. {
  261. struct tipc_net *tn = net_generic(sock_net(&tsk->sk), tipc_net_id);
  262. u32 peer_port = tsk_peer_port(tsk);
  263. u32 orig_node;
  264. u32 peer_node;
  265. if (unlikely(!tsk->connected))
  266. return false;
  267. if (unlikely(msg_origport(msg) != peer_port))
  268. return false;
  269. orig_node = msg_orignode(msg);
  270. peer_node = tsk_peer_node(tsk);
  271. if (likely(orig_node == peer_node))
  272. return true;
  273. if (!orig_node && (peer_node == tn->own_addr))
  274. return true;
  275. if (!peer_node && (orig_node == tn->own_addr))
  276. return true;
  277. return false;
  278. }
  279. /**
  280. * tipc_sk_create - create a TIPC socket
  281. * @net: network namespace (must be default network)
  282. * @sock: pre-allocated socket structure
  283. * @protocol: protocol indicator (must be 0)
  284. * @kern: caused by kernel or by userspace?
  285. *
  286. * This routine creates additional data structures used by the TIPC socket,
  287. * initializes them, and links them together.
  288. *
  289. * Returns 0 on success, errno otherwise
  290. */
  291. static int tipc_sk_create(struct net *net, struct socket *sock,
  292. int protocol, int kern)
  293. {
  294. struct tipc_net *tn;
  295. const struct proto_ops *ops;
  296. socket_state state;
  297. struct sock *sk;
  298. struct tipc_sock *tsk;
  299. struct tipc_msg *msg;
  300. /* Validate arguments */
  301. if (unlikely(protocol != 0))
  302. return -EPROTONOSUPPORT;
  303. switch (sock->type) {
  304. case SOCK_STREAM:
  305. ops = &stream_ops;
  306. state = SS_UNCONNECTED;
  307. break;
  308. case SOCK_SEQPACKET:
  309. ops = &packet_ops;
  310. state = SS_UNCONNECTED;
  311. break;
  312. case SOCK_DGRAM:
  313. case SOCK_RDM:
  314. ops = &msg_ops;
  315. state = SS_READY;
  316. break;
  317. default:
  318. return -EPROTOTYPE;
  319. }
  320. /* Allocate socket's protocol area */
  321. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
  322. if (sk == NULL)
  323. return -ENOMEM;
  324. tsk = tipc_sk(sk);
  325. tsk->max_pkt = MAX_PKT_DEFAULT;
  326. INIT_LIST_HEAD(&tsk->publications);
  327. msg = &tsk->phdr;
  328. tn = net_generic(sock_net(sk), tipc_net_id);
  329. tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
  330. NAMED_H_SIZE, 0);
  331. /* Finish initializing socket data structures */
  332. sock->ops = ops;
  333. sock->state = state;
  334. sock_init_data(sock, sk);
  335. if (tipc_sk_insert(tsk)) {
  336. pr_warn("Socket create failed; port numbrer exhausted\n");
  337. return -EINVAL;
  338. }
  339. msg_set_origport(msg, tsk->portid);
  340. setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
  341. sk->sk_backlog_rcv = tipc_backlog_rcv;
  342. sk->sk_rcvbuf = sysctl_tipc_rmem[1];
  343. sk->sk_data_ready = tipc_data_ready;
  344. sk->sk_write_space = tipc_write_space;
  345. sk->sk_destruct = tipc_sock_destruct;
  346. tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
  347. tsk->sent_unacked = 0;
  348. atomic_set(&tsk->dupl_rcvcnt, 0);
  349. if (sock->state == SS_READY) {
  350. tsk_set_unreturnable(tsk, true);
  351. if (sock->type == SOCK_DGRAM)
  352. tsk_set_unreliable(tsk, true);
  353. }
  354. return 0;
  355. }
  356. static void tipc_sk_callback(struct rcu_head *head)
  357. {
  358. struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
  359. sock_put(&tsk->sk);
  360. }
  361. /**
  362. * tipc_release - destroy a TIPC socket
  363. * @sock: socket to destroy
  364. *
  365. * This routine cleans up any messages that are still queued on the socket.
  366. * For DGRAM and RDM socket types, all queued messages are rejected.
  367. * For SEQPACKET and STREAM socket types, the first message is rejected
  368. * and any others are discarded. (If the first message on a STREAM socket
  369. * is partially-read, it is discarded and the next one is rejected instead.)
  370. *
  371. * NOTE: Rejected messages are not necessarily returned to the sender! They
  372. * are returned or discarded according to the "destination droppable" setting
  373. * specified for the message by the sender.
  374. *
  375. * Returns 0 on success, errno otherwise
  376. */
  377. static int tipc_release(struct socket *sock)
  378. {
  379. struct sock *sk = sock->sk;
  380. struct net *net;
  381. struct tipc_sock *tsk;
  382. struct sk_buff *skb;
  383. u32 dnode;
  384. /*
  385. * Exit if socket isn't fully initialized (occurs when a failed accept()
  386. * releases a pre-allocated child socket that was never used)
  387. */
  388. if (sk == NULL)
  389. return 0;
  390. net = sock_net(sk);
  391. tsk = tipc_sk(sk);
  392. lock_sock(sk);
  393. /*
  394. * Reject all unreceived messages, except on an active connection
  395. * (which disconnects locally & sends a 'FIN+' to peer)
  396. */
  397. dnode = tsk_peer_node(tsk);
  398. while (sock->state != SS_DISCONNECTING) {
  399. skb = __skb_dequeue(&sk->sk_receive_queue);
  400. if (skb == NULL)
  401. break;
  402. if (TIPC_SKB_CB(skb)->handle != NULL)
  403. kfree_skb(skb);
  404. else {
  405. if ((sock->state == SS_CONNECTING) ||
  406. (sock->state == SS_CONNECTED)) {
  407. sock->state = SS_DISCONNECTING;
  408. tsk->connected = 0;
  409. tipc_node_remove_conn(net, dnode, tsk->portid);
  410. }
  411. tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
  412. }
  413. }
  414. tipc_sk_withdraw(tsk, 0, NULL);
  415. sk_stop_timer(sk, &sk->sk_timer);
  416. tipc_sk_remove(tsk);
  417. if (tsk->connected) {
  418. skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
  419. TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
  420. tsk_own_node(tsk), tsk_peer_port(tsk),
  421. tsk->portid, TIPC_ERR_NO_PORT);
  422. if (skb)
  423. tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
  424. tipc_node_remove_conn(net, dnode, tsk->portid);
  425. }
  426. /* Reject any messages that accumulated in backlog queue */
  427. sock->state = SS_DISCONNECTING;
  428. release_sock(sk);
  429. call_rcu(&tsk->rcu, tipc_sk_callback);
  430. sock->sk = NULL;
  431. return 0;
  432. }
  433. /**
  434. * tipc_bind - associate or disassocate TIPC name(s) with a socket
  435. * @sock: socket structure
  436. * @uaddr: socket address describing name(s) and desired operation
  437. * @uaddr_len: size of socket address data structure
  438. *
  439. * Name and name sequence binding is indicated using a positive scope value;
  440. * a negative scope value unbinds the specified name. Specifying no name
  441. * (i.e. a socket address length of 0) unbinds all names from the socket.
  442. *
  443. * Returns 0 on success, errno otherwise
  444. *
  445. * NOTE: This routine doesn't need to take the socket lock since it doesn't
  446. * access any non-constant socket information.
  447. */
  448. static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
  449. int uaddr_len)
  450. {
  451. struct sock *sk = sock->sk;
  452. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  453. struct tipc_sock *tsk = tipc_sk(sk);
  454. int res = -EINVAL;
  455. lock_sock(sk);
  456. if (unlikely(!uaddr_len)) {
  457. res = tipc_sk_withdraw(tsk, 0, NULL);
  458. goto exit;
  459. }
  460. if (uaddr_len < sizeof(struct sockaddr_tipc)) {
  461. res = -EINVAL;
  462. goto exit;
  463. }
  464. if (addr->family != AF_TIPC) {
  465. res = -EAFNOSUPPORT;
  466. goto exit;
  467. }
  468. if (addr->addrtype == TIPC_ADDR_NAME)
  469. addr->addr.nameseq.upper = addr->addr.nameseq.lower;
  470. else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
  471. res = -EAFNOSUPPORT;
  472. goto exit;
  473. }
  474. if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
  475. (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
  476. (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
  477. res = -EACCES;
  478. goto exit;
  479. }
  480. res = (addr->scope > 0) ?
  481. tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
  482. tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
  483. exit:
  484. release_sock(sk);
  485. return res;
  486. }
  487. /**
  488. * tipc_getname - get port ID of socket or peer socket
  489. * @sock: socket structure
  490. * @uaddr: area for returned socket address
  491. * @uaddr_len: area for returned length of socket address
  492. * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
  493. *
  494. * Returns 0 on success, errno otherwise
  495. *
  496. * NOTE: This routine doesn't need to take the socket lock since it only
  497. * accesses socket information that is unchanging (or which changes in
  498. * a completely predictable manner).
  499. */
  500. static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
  501. int *uaddr_len, int peer)
  502. {
  503. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  504. struct tipc_sock *tsk = tipc_sk(sock->sk);
  505. struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
  506. memset(addr, 0, sizeof(*addr));
  507. if (peer) {
  508. if ((sock->state != SS_CONNECTED) &&
  509. ((peer != 2) || (sock->state != SS_DISCONNECTING)))
  510. return -ENOTCONN;
  511. addr->addr.id.ref = tsk_peer_port(tsk);
  512. addr->addr.id.node = tsk_peer_node(tsk);
  513. } else {
  514. addr->addr.id.ref = tsk->portid;
  515. addr->addr.id.node = tn->own_addr;
  516. }
  517. *uaddr_len = sizeof(*addr);
  518. addr->addrtype = TIPC_ADDR_ID;
  519. addr->family = AF_TIPC;
  520. addr->scope = 0;
  521. addr->addr.name.domain = 0;
  522. return 0;
  523. }
  524. /**
  525. * tipc_poll - read and possibly block on pollmask
  526. * @file: file structure associated with the socket
  527. * @sock: socket for which to calculate the poll bits
  528. * @wait: ???
  529. *
  530. * Returns pollmask value
  531. *
  532. * COMMENTARY:
  533. * It appears that the usual socket locking mechanisms are not useful here
  534. * since the pollmask info is potentially out-of-date the moment this routine
  535. * exits. TCP and other protocols seem to rely on higher level poll routines
  536. * to handle any preventable race conditions, so TIPC will do the same ...
  537. *
  538. * TIPC sets the returned events as follows:
  539. *
  540. * socket state flags set
  541. * ------------ ---------
  542. * unconnected no read flags
  543. * POLLOUT if port is not congested
  544. *
  545. * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
  546. * no write flags
  547. *
  548. * connected POLLIN/POLLRDNORM if data in rx queue
  549. * POLLOUT if port is not congested
  550. *
  551. * disconnecting POLLIN/POLLRDNORM/POLLHUP
  552. * no write flags
  553. *
  554. * listening POLLIN if SYN in rx queue
  555. * no write flags
  556. *
  557. * ready POLLIN/POLLRDNORM if data in rx queue
  558. * [connectionless] POLLOUT (since port cannot be congested)
  559. *
  560. * IMPORTANT: The fact that a read or write operation is indicated does NOT
  561. * imply that the operation will succeed, merely that it should be performed
  562. * and will not block.
  563. */
  564. static unsigned int tipc_poll(struct file *file, struct socket *sock,
  565. poll_table *wait)
  566. {
  567. struct sock *sk = sock->sk;
  568. struct tipc_sock *tsk = tipc_sk(sk);
  569. u32 mask = 0;
  570. sock_poll_wait(file, sk_sleep(sk), wait);
  571. switch ((int)sock->state) {
  572. case SS_UNCONNECTED:
  573. if (!tsk->link_cong)
  574. mask |= POLLOUT;
  575. break;
  576. case SS_READY:
  577. case SS_CONNECTED:
  578. if (!tsk->link_cong && !tsk_conn_cong(tsk))
  579. mask |= POLLOUT;
  580. /* fall thru' */
  581. case SS_CONNECTING:
  582. case SS_LISTENING:
  583. if (!skb_queue_empty(&sk->sk_receive_queue))
  584. mask |= (POLLIN | POLLRDNORM);
  585. break;
  586. case SS_DISCONNECTING:
  587. mask = (POLLIN | POLLRDNORM | POLLHUP);
  588. break;
  589. }
  590. return mask;
  591. }
  592. /**
  593. * tipc_sendmcast - send multicast message
  594. * @sock: socket structure
  595. * @seq: destination address
  596. * @msg: message to send
  597. * @dsz: total length of message data
  598. * @timeo: timeout to wait for wakeup
  599. *
  600. * Called from function tipc_sendmsg(), which has done all sanity checks
  601. * Returns the number of bytes sent on success, or errno
  602. */
  603. static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
  604. struct msghdr *msg, size_t dsz, long timeo)
  605. {
  606. struct sock *sk = sock->sk;
  607. struct tipc_sock *tsk = tipc_sk(sk);
  608. struct net *net = sock_net(sk);
  609. struct tipc_msg *mhdr = &tsk->phdr;
  610. struct sk_buff_head pktchain;
  611. struct iov_iter save = msg->msg_iter;
  612. uint mtu;
  613. int rc;
  614. msg_set_type(mhdr, TIPC_MCAST_MSG);
  615. msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
  616. msg_set_destport(mhdr, 0);
  617. msg_set_destnode(mhdr, 0);
  618. msg_set_nametype(mhdr, seq->type);
  619. msg_set_namelower(mhdr, seq->lower);
  620. msg_set_nameupper(mhdr, seq->upper);
  621. msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
  622. skb_queue_head_init(&pktchain);
  623. new_mtu:
  624. mtu = tipc_bcast_get_mtu(net);
  625. rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &pktchain);
  626. if (unlikely(rc < 0))
  627. return rc;
  628. do {
  629. rc = tipc_bcast_xmit(net, &pktchain);
  630. if (likely(!rc))
  631. return dsz;
  632. if (rc == -ELINKCONG) {
  633. tsk->link_cong = 1;
  634. rc = tipc_wait_for_sndmsg(sock, &timeo);
  635. if (!rc)
  636. continue;
  637. }
  638. __skb_queue_purge(&pktchain);
  639. if (rc == -EMSGSIZE) {
  640. msg->msg_iter = save;
  641. goto new_mtu;
  642. }
  643. break;
  644. } while (1);
  645. return rc;
  646. }
  647. /**
  648. * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
  649. * @arrvq: queue with arriving messages, to be cloned after destination lookup
  650. * @inputq: queue with cloned messages, delivered to socket after dest lookup
  651. *
  652. * Multi-threaded: parallel calls with reference to same queues may occur
  653. */
  654. void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
  655. struct sk_buff_head *inputq)
  656. {
  657. struct tipc_msg *msg;
  658. struct tipc_plist dports;
  659. u32 portid;
  660. u32 scope = TIPC_CLUSTER_SCOPE;
  661. struct sk_buff_head tmpq;
  662. uint hsz;
  663. struct sk_buff *skb, *_skb;
  664. __skb_queue_head_init(&tmpq);
  665. tipc_plist_init(&dports);
  666. skb = tipc_skb_peek(arrvq, &inputq->lock);
  667. for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
  668. msg = buf_msg(skb);
  669. hsz = skb_headroom(skb) + msg_hdr_sz(msg);
  670. if (in_own_node(net, msg_orignode(msg)))
  671. scope = TIPC_NODE_SCOPE;
  672. /* Create destination port list and message clones: */
  673. tipc_nametbl_mc_translate(net,
  674. msg_nametype(msg), msg_namelower(msg),
  675. msg_nameupper(msg), scope, &dports);
  676. portid = tipc_plist_pop(&dports);
  677. for (; portid; portid = tipc_plist_pop(&dports)) {
  678. _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
  679. if (_skb) {
  680. msg_set_destport(buf_msg(_skb), portid);
  681. __skb_queue_tail(&tmpq, _skb);
  682. continue;
  683. }
  684. pr_warn("Failed to clone mcast rcv buffer\n");
  685. }
  686. /* Append to inputq if not already done by other thread */
  687. spin_lock_bh(&inputq->lock);
  688. if (skb_peek(arrvq) == skb) {
  689. skb_queue_splice_tail_init(&tmpq, inputq);
  690. kfree_skb(__skb_dequeue(arrvq));
  691. }
  692. spin_unlock_bh(&inputq->lock);
  693. __skb_queue_purge(&tmpq);
  694. kfree_skb(skb);
  695. }
  696. tipc_sk_rcv(net, inputq);
  697. }
  698. /**
  699. * tipc_sk_proto_rcv - receive a connection mng protocol message
  700. * @tsk: receiving socket
  701. * @skb: pointer to message buffer.
  702. */
  703. static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
  704. struct sk_buff_head *xmitq)
  705. {
  706. struct sock *sk = &tsk->sk;
  707. u32 onode = tsk_own_node(tsk);
  708. struct tipc_msg *hdr = buf_msg(skb);
  709. int mtyp = msg_type(hdr);
  710. int conn_cong;
  711. /* Ignore if connection cannot be validated: */
  712. if (!tsk_peer_msg(tsk, hdr))
  713. goto exit;
  714. tsk->probing_state = TIPC_CONN_OK;
  715. if (mtyp == CONN_PROBE) {
  716. msg_set_type(hdr, CONN_PROBE_REPLY);
  717. if (tipc_msg_reverse(onode, &skb, TIPC_OK))
  718. __skb_queue_tail(xmitq, skb);
  719. return;
  720. } else if (mtyp == CONN_ACK) {
  721. conn_cong = tsk_conn_cong(tsk);
  722. tsk->sent_unacked -= msg_msgcnt(hdr);
  723. if (conn_cong)
  724. sk->sk_write_space(sk);
  725. } else if (mtyp != CONN_PROBE_REPLY) {
  726. pr_warn("Received unknown CONN_PROTO msg\n");
  727. }
  728. exit:
  729. kfree_skb(skb);
  730. }
  731. static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
  732. {
  733. struct sock *sk = sock->sk;
  734. struct tipc_sock *tsk = tipc_sk(sk);
  735. DEFINE_WAIT(wait);
  736. int done;
  737. do {
  738. int err = sock_error(sk);
  739. if (err)
  740. return err;
  741. if (sock->state == SS_DISCONNECTING)
  742. return -EPIPE;
  743. if (!*timeo_p)
  744. return -EAGAIN;
  745. if (signal_pending(current))
  746. return sock_intr_errno(*timeo_p);
  747. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  748. done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
  749. finish_wait(sk_sleep(sk), &wait);
  750. } while (!done);
  751. return 0;
  752. }
  753. /**
  754. * tipc_sendmsg - send message in connectionless manner
  755. * @sock: socket structure
  756. * @m: message to send
  757. * @dsz: amount of user data to be sent
  758. *
  759. * Message must have an destination specified explicitly.
  760. * Used for SOCK_RDM and SOCK_DGRAM messages,
  761. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  762. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  763. *
  764. * Returns the number of bytes sent on success, or errno otherwise
  765. */
  766. static int tipc_sendmsg(struct socket *sock,
  767. struct msghdr *m, size_t dsz)
  768. {
  769. struct sock *sk = sock->sk;
  770. int ret;
  771. lock_sock(sk);
  772. ret = __tipc_sendmsg(sock, m, dsz);
  773. release_sock(sk);
  774. return ret;
  775. }
  776. static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
  777. {
  778. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  779. struct sock *sk = sock->sk;
  780. struct tipc_sock *tsk = tipc_sk(sk);
  781. struct net *net = sock_net(sk);
  782. struct tipc_msg *mhdr = &tsk->phdr;
  783. u32 dnode, dport;
  784. struct sk_buff_head pktchain;
  785. struct sk_buff *skb;
  786. struct tipc_name_seq *seq;
  787. struct iov_iter save;
  788. u32 mtu;
  789. long timeo;
  790. int rc;
  791. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  792. return -EMSGSIZE;
  793. if (unlikely(!dest)) {
  794. if (tsk->connected && sock->state == SS_READY)
  795. dest = &tsk->remote;
  796. else
  797. return -EDESTADDRREQ;
  798. } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
  799. dest->family != AF_TIPC) {
  800. return -EINVAL;
  801. }
  802. if (unlikely(sock->state != SS_READY)) {
  803. if (sock->state == SS_LISTENING)
  804. return -EPIPE;
  805. if (sock->state != SS_UNCONNECTED)
  806. return -EISCONN;
  807. if (tsk->published)
  808. return -EOPNOTSUPP;
  809. if (dest->addrtype == TIPC_ADDR_NAME) {
  810. tsk->conn_type = dest->addr.name.name.type;
  811. tsk->conn_instance = dest->addr.name.name.instance;
  812. }
  813. }
  814. seq = &dest->addr.nameseq;
  815. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  816. if (dest->addrtype == TIPC_ADDR_MCAST) {
  817. return tipc_sendmcast(sock, seq, m, dsz, timeo);
  818. } else if (dest->addrtype == TIPC_ADDR_NAME) {
  819. u32 type = dest->addr.name.name.type;
  820. u32 inst = dest->addr.name.name.instance;
  821. u32 domain = dest->addr.name.domain;
  822. dnode = domain;
  823. msg_set_type(mhdr, TIPC_NAMED_MSG);
  824. msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
  825. msg_set_nametype(mhdr, type);
  826. msg_set_nameinst(mhdr, inst);
  827. msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
  828. dport = tipc_nametbl_translate(net, type, inst, &dnode);
  829. msg_set_destnode(mhdr, dnode);
  830. msg_set_destport(mhdr, dport);
  831. if (unlikely(!dport && !dnode))
  832. return -EHOSTUNREACH;
  833. } else if (dest->addrtype == TIPC_ADDR_ID) {
  834. dnode = dest->addr.id.node;
  835. msg_set_type(mhdr, TIPC_DIRECT_MSG);
  836. msg_set_lookup_scope(mhdr, 0);
  837. msg_set_destnode(mhdr, dnode);
  838. msg_set_destport(mhdr, dest->addr.id.ref);
  839. msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
  840. }
  841. skb_queue_head_init(&pktchain);
  842. save = m->msg_iter;
  843. new_mtu:
  844. mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
  845. rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &pktchain);
  846. if (rc < 0)
  847. return rc;
  848. do {
  849. skb = skb_peek(&pktchain);
  850. TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
  851. rc = tipc_node_xmit(net, &pktchain, dnode, tsk->portid);
  852. if (likely(!rc)) {
  853. if (sock->state != SS_READY)
  854. sock->state = SS_CONNECTING;
  855. return dsz;
  856. }
  857. if (rc == -ELINKCONG) {
  858. tsk->link_cong = 1;
  859. rc = tipc_wait_for_sndmsg(sock, &timeo);
  860. if (!rc)
  861. continue;
  862. }
  863. __skb_queue_purge(&pktchain);
  864. if (rc == -EMSGSIZE) {
  865. m->msg_iter = save;
  866. goto new_mtu;
  867. }
  868. break;
  869. } while (1);
  870. return rc;
  871. }
  872. static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
  873. {
  874. struct sock *sk = sock->sk;
  875. struct tipc_sock *tsk = tipc_sk(sk);
  876. DEFINE_WAIT(wait);
  877. int done;
  878. do {
  879. int err = sock_error(sk);
  880. if (err)
  881. return err;
  882. if (sock->state == SS_DISCONNECTING)
  883. return -EPIPE;
  884. else if (sock->state != SS_CONNECTED)
  885. return -ENOTCONN;
  886. if (!*timeo_p)
  887. return -EAGAIN;
  888. if (signal_pending(current))
  889. return sock_intr_errno(*timeo_p);
  890. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  891. done = sk_wait_event(sk, timeo_p,
  892. (!tsk->link_cong &&
  893. !tsk_conn_cong(tsk)) ||
  894. !tsk->connected);
  895. finish_wait(sk_sleep(sk), &wait);
  896. } while (!done);
  897. return 0;
  898. }
  899. /**
  900. * tipc_send_stream - send stream-oriented data
  901. * @sock: socket structure
  902. * @m: data to send
  903. * @dsz: total length of data to be transmitted
  904. *
  905. * Used for SOCK_STREAM data.
  906. *
  907. * Returns the number of bytes sent on success (or partial success),
  908. * or errno if no data sent
  909. */
  910. static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
  911. {
  912. struct sock *sk = sock->sk;
  913. int ret;
  914. lock_sock(sk);
  915. ret = __tipc_send_stream(sock, m, dsz);
  916. release_sock(sk);
  917. return ret;
  918. }
  919. static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
  920. {
  921. struct sock *sk = sock->sk;
  922. struct net *net = sock_net(sk);
  923. struct tipc_sock *tsk = tipc_sk(sk);
  924. struct tipc_msg *mhdr = &tsk->phdr;
  925. struct sk_buff_head pktchain;
  926. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  927. u32 portid = tsk->portid;
  928. int rc = -EINVAL;
  929. long timeo;
  930. u32 dnode;
  931. uint mtu, send, sent = 0;
  932. struct iov_iter save;
  933. /* Handle implied connection establishment */
  934. if (unlikely(dest)) {
  935. rc = __tipc_sendmsg(sock, m, dsz);
  936. if (dsz && (dsz == rc))
  937. tsk->sent_unacked = 1;
  938. return rc;
  939. }
  940. if (dsz > (uint)INT_MAX)
  941. return -EMSGSIZE;
  942. if (unlikely(sock->state != SS_CONNECTED)) {
  943. if (sock->state == SS_DISCONNECTING)
  944. return -EPIPE;
  945. else
  946. return -ENOTCONN;
  947. }
  948. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  949. dnode = tsk_peer_node(tsk);
  950. skb_queue_head_init(&pktchain);
  951. next:
  952. save = m->msg_iter;
  953. mtu = tsk->max_pkt;
  954. send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
  955. rc = tipc_msg_build(mhdr, m, sent, send, mtu, &pktchain);
  956. if (unlikely(rc < 0))
  957. return rc;
  958. do {
  959. if (likely(!tsk_conn_cong(tsk))) {
  960. rc = tipc_node_xmit(net, &pktchain, dnode, portid);
  961. if (likely(!rc)) {
  962. tsk->sent_unacked++;
  963. sent += send;
  964. if (sent == dsz)
  965. return dsz;
  966. goto next;
  967. }
  968. if (rc == -EMSGSIZE) {
  969. __skb_queue_purge(&pktchain);
  970. tsk->max_pkt = tipc_node_get_mtu(net, dnode,
  971. portid);
  972. m->msg_iter = save;
  973. goto next;
  974. }
  975. if (rc != -ELINKCONG)
  976. break;
  977. tsk->link_cong = 1;
  978. }
  979. rc = tipc_wait_for_sndpkt(sock, &timeo);
  980. } while (!rc);
  981. __skb_queue_purge(&pktchain);
  982. return sent ? sent : rc;
  983. }
  984. /**
  985. * tipc_send_packet - send a connection-oriented message
  986. * @sock: socket structure
  987. * @m: message to send
  988. * @dsz: length of data to be transmitted
  989. *
  990. * Used for SOCK_SEQPACKET messages.
  991. *
  992. * Returns the number of bytes sent on success, or errno otherwise
  993. */
  994. static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
  995. {
  996. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  997. return -EMSGSIZE;
  998. return tipc_send_stream(sock, m, dsz);
  999. }
  1000. /* tipc_sk_finish_conn - complete the setup of a connection
  1001. */
  1002. static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
  1003. u32 peer_node)
  1004. {
  1005. struct sock *sk = &tsk->sk;
  1006. struct net *net = sock_net(sk);
  1007. struct tipc_msg *msg = &tsk->phdr;
  1008. msg_set_destnode(msg, peer_node);
  1009. msg_set_destport(msg, peer_port);
  1010. msg_set_type(msg, TIPC_CONN_MSG);
  1011. msg_set_lookup_scope(msg, 0);
  1012. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  1013. tsk->probing_intv = CONN_PROBING_INTERVAL;
  1014. tsk->probing_state = TIPC_CONN_OK;
  1015. tsk->connected = 1;
  1016. sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
  1017. tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
  1018. tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
  1019. }
  1020. /**
  1021. * set_orig_addr - capture sender's address for received message
  1022. * @m: descriptor for message info
  1023. * @msg: received message header
  1024. *
  1025. * Note: Address is not captured if not requested by receiver.
  1026. */
  1027. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  1028. {
  1029. DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
  1030. if (addr) {
  1031. addr->family = AF_TIPC;
  1032. addr->addrtype = TIPC_ADDR_ID;
  1033. memset(&addr->addr, 0, sizeof(addr->addr));
  1034. addr->addr.id.ref = msg_origport(msg);
  1035. addr->addr.id.node = msg_orignode(msg);
  1036. addr->addr.name.domain = 0; /* could leave uninitialized */
  1037. addr->scope = 0; /* could leave uninitialized */
  1038. m->msg_namelen = sizeof(struct sockaddr_tipc);
  1039. }
  1040. }
  1041. /**
  1042. * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
  1043. * @m: descriptor for message info
  1044. * @msg: received message header
  1045. * @tsk: TIPC port associated with message
  1046. *
  1047. * Note: Ancillary data is not captured if not requested by receiver.
  1048. *
  1049. * Returns 0 if successful, otherwise errno
  1050. */
  1051. static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  1052. struct tipc_sock *tsk)
  1053. {
  1054. u32 anc_data[3];
  1055. u32 err;
  1056. u32 dest_type;
  1057. int has_name;
  1058. int res;
  1059. if (likely(m->msg_controllen == 0))
  1060. return 0;
  1061. /* Optionally capture errored message object(s) */
  1062. err = msg ? msg_errcode(msg) : 0;
  1063. if (unlikely(err)) {
  1064. anc_data[0] = err;
  1065. anc_data[1] = msg_data_sz(msg);
  1066. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  1067. if (res)
  1068. return res;
  1069. if (anc_data[1]) {
  1070. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  1071. msg_data(msg));
  1072. if (res)
  1073. return res;
  1074. }
  1075. }
  1076. /* Optionally capture message destination object */
  1077. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  1078. switch (dest_type) {
  1079. case TIPC_NAMED_MSG:
  1080. has_name = 1;
  1081. anc_data[0] = msg_nametype(msg);
  1082. anc_data[1] = msg_namelower(msg);
  1083. anc_data[2] = msg_namelower(msg);
  1084. break;
  1085. case TIPC_MCAST_MSG:
  1086. has_name = 1;
  1087. anc_data[0] = msg_nametype(msg);
  1088. anc_data[1] = msg_namelower(msg);
  1089. anc_data[2] = msg_nameupper(msg);
  1090. break;
  1091. case TIPC_CONN_MSG:
  1092. has_name = (tsk->conn_type != 0);
  1093. anc_data[0] = tsk->conn_type;
  1094. anc_data[1] = tsk->conn_instance;
  1095. anc_data[2] = tsk->conn_instance;
  1096. break;
  1097. default:
  1098. has_name = 0;
  1099. }
  1100. if (has_name) {
  1101. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  1102. if (res)
  1103. return res;
  1104. }
  1105. return 0;
  1106. }
  1107. static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
  1108. {
  1109. struct net *net = sock_net(&tsk->sk);
  1110. struct sk_buff *skb = NULL;
  1111. struct tipc_msg *msg;
  1112. u32 peer_port = tsk_peer_port(tsk);
  1113. u32 dnode = tsk_peer_node(tsk);
  1114. if (!tsk->connected)
  1115. return;
  1116. skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
  1117. dnode, tsk_own_node(tsk), peer_port,
  1118. tsk->portid, TIPC_OK);
  1119. if (!skb)
  1120. return;
  1121. msg = buf_msg(skb);
  1122. msg_set_msgcnt(msg, ack);
  1123. tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
  1124. }
  1125. static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
  1126. {
  1127. struct sock *sk = sock->sk;
  1128. DEFINE_WAIT(wait);
  1129. long timeo = *timeop;
  1130. int err;
  1131. for (;;) {
  1132. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1133. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1134. if (sock->state == SS_DISCONNECTING) {
  1135. err = -ENOTCONN;
  1136. break;
  1137. }
  1138. release_sock(sk);
  1139. timeo = schedule_timeout(timeo);
  1140. lock_sock(sk);
  1141. }
  1142. err = 0;
  1143. if (!skb_queue_empty(&sk->sk_receive_queue))
  1144. break;
  1145. err = -EAGAIN;
  1146. if (!timeo)
  1147. break;
  1148. err = sock_intr_errno(timeo);
  1149. if (signal_pending(current))
  1150. break;
  1151. }
  1152. finish_wait(sk_sleep(sk), &wait);
  1153. *timeop = timeo;
  1154. return err;
  1155. }
  1156. /**
  1157. * tipc_recvmsg - receive packet-oriented message
  1158. * @m: descriptor for message info
  1159. * @buf_len: total size of user buffer area
  1160. * @flags: receive flags
  1161. *
  1162. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  1163. * If the complete message doesn't fit in user area, truncate it.
  1164. *
  1165. * Returns size of returned message data, errno otherwise
  1166. */
  1167. static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
  1168. int flags)
  1169. {
  1170. struct sock *sk = sock->sk;
  1171. struct tipc_sock *tsk = tipc_sk(sk);
  1172. struct sk_buff *buf;
  1173. struct tipc_msg *msg;
  1174. long timeo;
  1175. unsigned int sz;
  1176. u32 err;
  1177. int res;
  1178. /* Catch invalid receive requests */
  1179. if (unlikely(!buf_len))
  1180. return -EINVAL;
  1181. lock_sock(sk);
  1182. if (unlikely(sock->state == SS_UNCONNECTED)) {
  1183. res = -ENOTCONN;
  1184. goto exit;
  1185. }
  1186. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1187. restart:
  1188. /* Look for a message in receive queue; wait if necessary */
  1189. res = tipc_wait_for_rcvmsg(sock, &timeo);
  1190. if (res)
  1191. goto exit;
  1192. /* Look at first message in receive queue */
  1193. buf = skb_peek(&sk->sk_receive_queue);
  1194. msg = buf_msg(buf);
  1195. sz = msg_data_sz(msg);
  1196. err = msg_errcode(msg);
  1197. /* Discard an empty non-errored message & try again */
  1198. if ((!sz) && (!err)) {
  1199. tsk_advance_rx_queue(sk);
  1200. goto restart;
  1201. }
  1202. /* Capture sender's address (optional) */
  1203. set_orig_addr(m, msg);
  1204. /* Capture ancillary data (optional) */
  1205. res = tipc_sk_anc_data_recv(m, msg, tsk);
  1206. if (res)
  1207. goto exit;
  1208. /* Capture message data (if valid) & compute return value (always) */
  1209. if (!err) {
  1210. if (unlikely(buf_len < sz)) {
  1211. sz = buf_len;
  1212. m->msg_flags |= MSG_TRUNC;
  1213. }
  1214. res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
  1215. if (res)
  1216. goto exit;
  1217. res = sz;
  1218. } else {
  1219. if ((sock->state == SS_READY) ||
  1220. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  1221. res = 0;
  1222. else
  1223. res = -ECONNRESET;
  1224. }
  1225. /* Consume received message (optional) */
  1226. if (likely(!(flags & MSG_PEEK))) {
  1227. if ((sock->state != SS_READY) &&
  1228. (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
  1229. tipc_sk_send_ack(tsk, tsk->rcv_unacked);
  1230. tsk->rcv_unacked = 0;
  1231. }
  1232. tsk_advance_rx_queue(sk);
  1233. }
  1234. exit:
  1235. release_sock(sk);
  1236. return res;
  1237. }
  1238. /**
  1239. * tipc_recv_stream - receive stream-oriented data
  1240. * @m: descriptor for message info
  1241. * @buf_len: total size of user buffer area
  1242. * @flags: receive flags
  1243. *
  1244. * Used for SOCK_STREAM messages only. If not enough data is available
  1245. * will optionally wait for more; never truncates data.
  1246. *
  1247. * Returns size of returned message data, errno otherwise
  1248. */
  1249. static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
  1250. size_t buf_len, int flags)
  1251. {
  1252. struct sock *sk = sock->sk;
  1253. struct tipc_sock *tsk = tipc_sk(sk);
  1254. struct sk_buff *buf;
  1255. struct tipc_msg *msg;
  1256. long timeo;
  1257. unsigned int sz;
  1258. int sz_to_copy, target, needed;
  1259. int sz_copied = 0;
  1260. u32 err;
  1261. int res = 0;
  1262. /* Catch invalid receive attempts */
  1263. if (unlikely(!buf_len))
  1264. return -EINVAL;
  1265. lock_sock(sk);
  1266. if (unlikely(sock->state == SS_UNCONNECTED)) {
  1267. res = -ENOTCONN;
  1268. goto exit;
  1269. }
  1270. target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
  1271. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1272. restart:
  1273. /* Look for a message in receive queue; wait if necessary */
  1274. res = tipc_wait_for_rcvmsg(sock, &timeo);
  1275. if (res)
  1276. goto exit;
  1277. /* Look at first message in receive queue */
  1278. buf = skb_peek(&sk->sk_receive_queue);
  1279. msg = buf_msg(buf);
  1280. sz = msg_data_sz(msg);
  1281. err = msg_errcode(msg);
  1282. /* Discard an empty non-errored message & try again */
  1283. if ((!sz) && (!err)) {
  1284. tsk_advance_rx_queue(sk);
  1285. goto restart;
  1286. }
  1287. /* Optionally capture sender's address & ancillary data of first msg */
  1288. if (sz_copied == 0) {
  1289. set_orig_addr(m, msg);
  1290. res = tipc_sk_anc_data_recv(m, msg, tsk);
  1291. if (res)
  1292. goto exit;
  1293. }
  1294. /* Capture message data (if valid) & compute return value (always) */
  1295. if (!err) {
  1296. u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
  1297. sz -= offset;
  1298. needed = (buf_len - sz_copied);
  1299. sz_to_copy = (sz <= needed) ? sz : needed;
  1300. res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
  1301. m, sz_to_copy);
  1302. if (res)
  1303. goto exit;
  1304. sz_copied += sz_to_copy;
  1305. if (sz_to_copy < sz) {
  1306. if (!(flags & MSG_PEEK))
  1307. TIPC_SKB_CB(buf)->handle =
  1308. (void *)(unsigned long)(offset + sz_to_copy);
  1309. goto exit;
  1310. }
  1311. } else {
  1312. if (sz_copied != 0)
  1313. goto exit; /* can't add error msg to valid data */
  1314. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  1315. res = 0;
  1316. else
  1317. res = -ECONNRESET;
  1318. }
  1319. /* Consume received message (optional) */
  1320. if (likely(!(flags & MSG_PEEK))) {
  1321. if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
  1322. tipc_sk_send_ack(tsk, tsk->rcv_unacked);
  1323. tsk->rcv_unacked = 0;
  1324. }
  1325. tsk_advance_rx_queue(sk);
  1326. }
  1327. /* Loop around if more data is required */
  1328. if ((sz_copied < buf_len) && /* didn't get all requested data */
  1329. (!skb_queue_empty(&sk->sk_receive_queue) ||
  1330. (sz_copied < target)) && /* and more is ready or required */
  1331. (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
  1332. (!err)) /* and haven't reached a FIN */
  1333. goto restart;
  1334. exit:
  1335. release_sock(sk);
  1336. return sz_copied ? sz_copied : res;
  1337. }
  1338. /**
  1339. * tipc_write_space - wake up thread if port congestion is released
  1340. * @sk: socket
  1341. */
  1342. static void tipc_write_space(struct sock *sk)
  1343. {
  1344. struct socket_wq *wq;
  1345. rcu_read_lock();
  1346. wq = rcu_dereference(sk->sk_wq);
  1347. if (wq_has_sleeper(wq))
  1348. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1349. POLLWRNORM | POLLWRBAND);
  1350. rcu_read_unlock();
  1351. }
  1352. /**
  1353. * tipc_data_ready - wake up threads to indicate messages have been received
  1354. * @sk: socket
  1355. * @len: the length of messages
  1356. */
  1357. static void tipc_data_ready(struct sock *sk)
  1358. {
  1359. struct socket_wq *wq;
  1360. rcu_read_lock();
  1361. wq = rcu_dereference(sk->sk_wq);
  1362. if (wq_has_sleeper(wq))
  1363. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  1364. POLLRDNORM | POLLRDBAND);
  1365. rcu_read_unlock();
  1366. }
  1367. static void tipc_sock_destruct(struct sock *sk)
  1368. {
  1369. __skb_queue_purge(&sk->sk_receive_queue);
  1370. }
  1371. /**
  1372. * filter_connect - Handle all incoming messages for a connection-based socket
  1373. * @tsk: TIPC socket
  1374. * @skb: pointer to message buffer. Set to NULL if buffer is consumed
  1375. *
  1376. * Returns true if everything ok, false otherwise
  1377. */
  1378. static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
  1379. {
  1380. struct sock *sk = &tsk->sk;
  1381. struct net *net = sock_net(sk);
  1382. struct socket *sock = sk->sk_socket;
  1383. struct tipc_msg *hdr = buf_msg(skb);
  1384. if (unlikely(msg_mcast(hdr)))
  1385. return false;
  1386. switch ((int)sock->state) {
  1387. case SS_CONNECTED:
  1388. /* Accept only connection-based messages sent by peer */
  1389. if (unlikely(!tsk_peer_msg(tsk, hdr)))
  1390. return false;
  1391. if (unlikely(msg_errcode(hdr))) {
  1392. sock->state = SS_DISCONNECTING;
  1393. tsk->connected = 0;
  1394. /* Let timer expire on it's own */
  1395. tipc_node_remove_conn(net, tsk_peer_node(tsk),
  1396. tsk->portid);
  1397. }
  1398. return true;
  1399. case SS_CONNECTING:
  1400. /* Accept only ACK or NACK message */
  1401. if (unlikely(!msg_connected(hdr)))
  1402. return false;
  1403. if (unlikely(msg_errcode(hdr))) {
  1404. sock->state = SS_DISCONNECTING;
  1405. sk->sk_err = ECONNREFUSED;
  1406. return true;
  1407. }
  1408. if (unlikely(!msg_isdata(hdr))) {
  1409. sock->state = SS_DISCONNECTING;
  1410. sk->sk_err = EINVAL;
  1411. return true;
  1412. }
  1413. tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
  1414. msg_set_importance(&tsk->phdr, msg_importance(hdr));
  1415. sock->state = SS_CONNECTED;
  1416. /* If 'ACK+' message, add to socket receive queue */
  1417. if (msg_data_sz(hdr))
  1418. return true;
  1419. /* If empty 'ACK-' message, wake up sleeping connect() */
  1420. if (waitqueue_active(sk_sleep(sk)))
  1421. wake_up_interruptible(sk_sleep(sk));
  1422. /* 'ACK-' message is neither accepted nor rejected: */
  1423. msg_set_dest_droppable(hdr, 1);
  1424. return false;
  1425. case SS_LISTENING:
  1426. case SS_UNCONNECTED:
  1427. /* Accept only SYN message */
  1428. if (!msg_connected(hdr) && !(msg_errcode(hdr)))
  1429. return true;
  1430. break;
  1431. case SS_DISCONNECTING:
  1432. break;
  1433. default:
  1434. pr_err("Unknown socket state %u\n", sock->state);
  1435. }
  1436. return false;
  1437. }
  1438. /**
  1439. * rcvbuf_limit - get proper overload limit of socket receive queue
  1440. * @sk: socket
  1441. * @buf: message
  1442. *
  1443. * For all connection oriented messages, irrespective of importance,
  1444. * the default overload value (i.e. 67MB) is set as limit.
  1445. *
  1446. * For all connectionless messages, by default new queue limits are
  1447. * as belows:
  1448. *
  1449. * TIPC_LOW_IMPORTANCE (4 MB)
  1450. * TIPC_MEDIUM_IMPORTANCE (8 MB)
  1451. * TIPC_HIGH_IMPORTANCE (16 MB)
  1452. * TIPC_CRITICAL_IMPORTANCE (32 MB)
  1453. *
  1454. * Returns overload limit according to corresponding message importance
  1455. */
  1456. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
  1457. {
  1458. struct tipc_msg *msg = buf_msg(buf);
  1459. if (msg_connected(msg))
  1460. return sysctl_tipc_rmem[2];
  1461. return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
  1462. msg_importance(msg);
  1463. }
  1464. /**
  1465. * filter_rcv - validate incoming message
  1466. * @sk: socket
  1467. * @skb: pointer to message.
  1468. *
  1469. * Enqueues message on receive queue if acceptable; optionally handles
  1470. * disconnect indication for a connected socket.
  1471. *
  1472. * Called with socket lock already taken
  1473. *
  1474. * Returns true if message was added to socket receive queue, otherwise false
  1475. */
  1476. static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
  1477. struct sk_buff_head *xmitq)
  1478. {
  1479. struct socket *sock = sk->sk_socket;
  1480. struct tipc_sock *tsk = tipc_sk(sk);
  1481. struct tipc_msg *hdr = buf_msg(skb);
  1482. unsigned int limit = rcvbuf_limit(sk, skb);
  1483. int err = TIPC_OK;
  1484. int usr = msg_user(hdr);
  1485. if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
  1486. tipc_sk_proto_rcv(tsk, skb, xmitq);
  1487. return false;
  1488. }
  1489. if (unlikely(usr == SOCK_WAKEUP)) {
  1490. kfree_skb(skb);
  1491. tsk->link_cong = 0;
  1492. sk->sk_write_space(sk);
  1493. return false;
  1494. }
  1495. /* Drop if illegal message type */
  1496. if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
  1497. kfree_skb(skb);
  1498. return false;
  1499. }
  1500. /* Reject if wrong message type for current socket state */
  1501. if (unlikely(sock->state == SS_READY)) {
  1502. if (msg_connected(hdr)) {
  1503. err = TIPC_ERR_NO_PORT;
  1504. goto reject;
  1505. }
  1506. } else if (unlikely(!filter_connect(tsk, skb))) {
  1507. err = TIPC_ERR_NO_PORT;
  1508. goto reject;
  1509. }
  1510. /* Reject message if there isn't room to queue it */
  1511. if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
  1512. err = TIPC_ERR_OVERLOAD;
  1513. goto reject;
  1514. }
  1515. /* Enqueue message */
  1516. TIPC_SKB_CB(skb)->handle = NULL;
  1517. __skb_queue_tail(&sk->sk_receive_queue, skb);
  1518. skb_set_owner_r(skb, sk);
  1519. sk->sk_data_ready(sk);
  1520. return true;
  1521. reject:
  1522. if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
  1523. __skb_queue_tail(xmitq, skb);
  1524. return false;
  1525. }
  1526. /**
  1527. * tipc_backlog_rcv - handle incoming message from backlog queue
  1528. * @sk: socket
  1529. * @skb: message
  1530. *
  1531. * Caller must hold socket lock
  1532. *
  1533. * Returns 0
  1534. */
  1535. static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  1536. {
  1537. unsigned int truesize = skb->truesize;
  1538. struct sk_buff_head xmitq;
  1539. u32 dnode, selector;
  1540. __skb_queue_head_init(&xmitq);
  1541. if (likely(filter_rcv(sk, skb, &xmitq))) {
  1542. atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
  1543. return 0;
  1544. }
  1545. if (skb_queue_empty(&xmitq))
  1546. return 0;
  1547. /* Send response/rejected message */
  1548. skb = __skb_dequeue(&xmitq);
  1549. dnode = msg_destnode(buf_msg(skb));
  1550. selector = msg_origport(buf_msg(skb));
  1551. tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
  1552. return 0;
  1553. }
  1554. /**
  1555. * tipc_sk_enqueue - extract all buffers with destination 'dport' from
  1556. * inputq and try adding them to socket or backlog queue
  1557. * @inputq: list of incoming buffers with potentially different destinations
  1558. * @sk: socket where the buffers should be enqueued
  1559. * @dport: port number for the socket
  1560. *
  1561. * Caller must hold socket lock
  1562. */
  1563. static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
  1564. u32 dport, struct sk_buff_head *xmitq)
  1565. {
  1566. unsigned long time_limit = jiffies + 2;
  1567. struct sk_buff *skb;
  1568. unsigned int lim;
  1569. atomic_t *dcnt;
  1570. u32 onode;
  1571. while (skb_queue_len(inputq)) {
  1572. if (unlikely(time_after_eq(jiffies, time_limit)))
  1573. return;
  1574. skb = tipc_skb_dequeue(inputq, dport);
  1575. if (unlikely(!skb))
  1576. return;
  1577. /* Add message directly to receive queue if possible */
  1578. if (!sock_owned_by_user(sk)) {
  1579. filter_rcv(sk, skb, xmitq);
  1580. continue;
  1581. }
  1582. /* Try backlog, compensating for double-counted bytes */
  1583. dcnt = &tipc_sk(sk)->dupl_rcvcnt;
  1584. if (!sk->sk_backlog.len)
  1585. atomic_set(dcnt, 0);
  1586. lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
  1587. if (likely(!sk_add_backlog(sk, skb, lim)))
  1588. continue;
  1589. /* Overload => reject message back to sender */
  1590. onode = tipc_own_addr(sock_net(sk));
  1591. if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
  1592. __skb_queue_tail(xmitq, skb);
  1593. break;
  1594. }
  1595. }
  1596. /**
  1597. * tipc_sk_rcv - handle a chain of incoming buffers
  1598. * @inputq: buffer list containing the buffers
  1599. * Consumes all buffers in list until inputq is empty
  1600. * Note: may be called in multiple threads referring to the same queue
  1601. */
  1602. void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
  1603. {
  1604. struct sk_buff_head xmitq;
  1605. u32 dnode, dport = 0;
  1606. int err;
  1607. struct tipc_sock *tsk;
  1608. struct sock *sk;
  1609. struct sk_buff *skb;
  1610. __skb_queue_head_init(&xmitq);
  1611. while (skb_queue_len(inputq)) {
  1612. dport = tipc_skb_peek_port(inputq, dport);
  1613. tsk = tipc_sk_lookup(net, dport);
  1614. if (likely(tsk)) {
  1615. sk = &tsk->sk;
  1616. if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
  1617. tipc_sk_enqueue(inputq, sk, dport, &xmitq);
  1618. spin_unlock_bh(&sk->sk_lock.slock);
  1619. }
  1620. /* Send pending response/rejected messages, if any */
  1621. while ((skb = __skb_dequeue(&xmitq))) {
  1622. dnode = msg_destnode(buf_msg(skb));
  1623. tipc_node_xmit_skb(net, skb, dnode, dport);
  1624. }
  1625. sock_put(sk);
  1626. continue;
  1627. }
  1628. /* No destination socket => dequeue skb if still there */
  1629. skb = tipc_skb_dequeue(inputq, dport);
  1630. if (!skb)
  1631. return;
  1632. /* Try secondary lookup if unresolved named message */
  1633. err = TIPC_ERR_NO_PORT;
  1634. if (tipc_msg_lookup_dest(net, skb, &err))
  1635. goto xmit;
  1636. /* Prepare for message rejection */
  1637. if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
  1638. continue;
  1639. xmit:
  1640. dnode = msg_destnode(buf_msg(skb));
  1641. tipc_node_xmit_skb(net, skb, dnode, dport);
  1642. }
  1643. }
  1644. static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
  1645. {
  1646. struct sock *sk = sock->sk;
  1647. DEFINE_WAIT(wait);
  1648. int done;
  1649. do {
  1650. int err = sock_error(sk);
  1651. if (err)
  1652. return err;
  1653. if (!*timeo_p)
  1654. return -ETIMEDOUT;
  1655. if (signal_pending(current))
  1656. return sock_intr_errno(*timeo_p);
  1657. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1658. done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
  1659. finish_wait(sk_sleep(sk), &wait);
  1660. } while (!done);
  1661. return 0;
  1662. }
  1663. /**
  1664. * tipc_connect - establish a connection to another TIPC port
  1665. * @sock: socket structure
  1666. * @dest: socket address for destination port
  1667. * @destlen: size of socket address data structure
  1668. * @flags: file-related flags associated with socket
  1669. *
  1670. * Returns 0 on success, errno otherwise
  1671. */
  1672. static int tipc_connect(struct socket *sock, struct sockaddr *dest,
  1673. int destlen, int flags)
  1674. {
  1675. struct sock *sk = sock->sk;
  1676. struct tipc_sock *tsk = tipc_sk(sk);
  1677. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1678. struct msghdr m = {NULL,};
  1679. long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
  1680. socket_state previous;
  1681. int res = 0;
  1682. lock_sock(sk);
  1683. /* DGRAM/RDM connect(), just save the destaddr */
  1684. if (sock->state == SS_READY) {
  1685. if (dst->family == AF_UNSPEC) {
  1686. memset(&tsk->remote, 0, sizeof(struct sockaddr_tipc));
  1687. tsk->connected = 0;
  1688. } else if (destlen != sizeof(struct sockaddr_tipc)) {
  1689. res = -EINVAL;
  1690. } else {
  1691. memcpy(&tsk->remote, dest, destlen);
  1692. tsk->connected = 1;
  1693. }
  1694. goto exit;
  1695. }
  1696. /*
  1697. * Reject connection attempt using multicast address
  1698. *
  1699. * Note: send_msg() validates the rest of the address fields,
  1700. * so there's no need to do it here
  1701. */
  1702. if (dst->addrtype == TIPC_ADDR_MCAST) {
  1703. res = -EINVAL;
  1704. goto exit;
  1705. }
  1706. previous = sock->state;
  1707. switch (sock->state) {
  1708. case SS_UNCONNECTED:
  1709. /* Send a 'SYN-' to destination */
  1710. m.msg_name = dest;
  1711. m.msg_namelen = destlen;
  1712. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  1713. * indicate send_msg() is never blocked.
  1714. */
  1715. if (!timeout)
  1716. m.msg_flags = MSG_DONTWAIT;
  1717. res = __tipc_sendmsg(sock, &m, 0);
  1718. if ((res < 0) && (res != -EWOULDBLOCK))
  1719. goto exit;
  1720. /* Just entered SS_CONNECTING state; the only
  1721. * difference is that return value in non-blocking
  1722. * case is EINPROGRESS, rather than EALREADY.
  1723. */
  1724. res = -EINPROGRESS;
  1725. case SS_CONNECTING:
  1726. if (previous == SS_CONNECTING)
  1727. res = -EALREADY;
  1728. if (!timeout)
  1729. goto exit;
  1730. timeout = msecs_to_jiffies(timeout);
  1731. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  1732. res = tipc_wait_for_connect(sock, &timeout);
  1733. break;
  1734. case SS_CONNECTED:
  1735. res = -EISCONN;
  1736. break;
  1737. default:
  1738. res = -EINVAL;
  1739. break;
  1740. }
  1741. exit:
  1742. release_sock(sk);
  1743. return res;
  1744. }
  1745. /**
  1746. * tipc_listen - allow socket to listen for incoming connections
  1747. * @sock: socket structure
  1748. * @len: (unused)
  1749. *
  1750. * Returns 0 on success, errno otherwise
  1751. */
  1752. static int tipc_listen(struct socket *sock, int len)
  1753. {
  1754. struct sock *sk = sock->sk;
  1755. int res;
  1756. lock_sock(sk);
  1757. if (sock->state != SS_UNCONNECTED)
  1758. res = -EINVAL;
  1759. else {
  1760. sock->state = SS_LISTENING;
  1761. res = 0;
  1762. }
  1763. release_sock(sk);
  1764. return res;
  1765. }
  1766. static int tipc_wait_for_accept(struct socket *sock, long timeo)
  1767. {
  1768. struct sock *sk = sock->sk;
  1769. DEFINE_WAIT(wait);
  1770. int err;
  1771. /* True wake-one mechanism for incoming connections: only
  1772. * one process gets woken up, not the 'whole herd'.
  1773. * Since we do not 'race & poll' for established sockets
  1774. * anymore, the common case will execute the loop only once.
  1775. */
  1776. for (;;) {
  1777. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  1778. TASK_INTERRUPTIBLE);
  1779. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1780. release_sock(sk);
  1781. timeo = schedule_timeout(timeo);
  1782. lock_sock(sk);
  1783. }
  1784. err = 0;
  1785. if (!skb_queue_empty(&sk->sk_receive_queue))
  1786. break;
  1787. err = -EINVAL;
  1788. if (sock->state != SS_LISTENING)
  1789. break;
  1790. err = -EAGAIN;
  1791. if (!timeo)
  1792. break;
  1793. err = sock_intr_errno(timeo);
  1794. if (signal_pending(current))
  1795. break;
  1796. }
  1797. finish_wait(sk_sleep(sk), &wait);
  1798. return err;
  1799. }
  1800. /**
  1801. * tipc_accept - wait for connection request
  1802. * @sock: listening socket
  1803. * @newsock: new socket that is to be connected
  1804. * @flags: file-related flags associated with socket
  1805. *
  1806. * Returns 0 on success, errno otherwise
  1807. */
  1808. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
  1809. {
  1810. struct sock *new_sk, *sk = sock->sk;
  1811. struct sk_buff *buf;
  1812. struct tipc_sock *new_tsock;
  1813. struct tipc_msg *msg;
  1814. long timeo;
  1815. int res;
  1816. lock_sock(sk);
  1817. if (sock->state != SS_LISTENING) {
  1818. res = -EINVAL;
  1819. goto exit;
  1820. }
  1821. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  1822. res = tipc_wait_for_accept(sock, timeo);
  1823. if (res)
  1824. goto exit;
  1825. buf = skb_peek(&sk->sk_receive_queue);
  1826. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
  1827. if (res)
  1828. goto exit;
  1829. security_sk_clone(sock->sk, new_sock->sk);
  1830. new_sk = new_sock->sk;
  1831. new_tsock = tipc_sk(new_sk);
  1832. msg = buf_msg(buf);
  1833. /* we lock on new_sk; but lockdep sees the lock on sk */
  1834. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  1835. /*
  1836. * Reject any stray messages received by new socket
  1837. * before the socket lock was taken (very, very unlikely)
  1838. */
  1839. tsk_rej_rx_queue(new_sk);
  1840. /* Connect new socket to it's peer */
  1841. tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
  1842. new_sock->state = SS_CONNECTED;
  1843. tsk_set_importance(new_tsock, msg_importance(msg));
  1844. if (msg_named(msg)) {
  1845. new_tsock->conn_type = msg_nametype(msg);
  1846. new_tsock->conn_instance = msg_nameinst(msg);
  1847. }
  1848. /*
  1849. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1850. * Respond to 'SYN+' by queuing it on new socket.
  1851. */
  1852. if (!msg_data_sz(msg)) {
  1853. struct msghdr m = {NULL,};
  1854. tsk_advance_rx_queue(sk);
  1855. __tipc_send_stream(new_sock, &m, 0);
  1856. } else {
  1857. __skb_dequeue(&sk->sk_receive_queue);
  1858. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  1859. skb_set_owner_r(buf, new_sk);
  1860. }
  1861. release_sock(new_sk);
  1862. exit:
  1863. release_sock(sk);
  1864. return res;
  1865. }
  1866. /**
  1867. * tipc_shutdown - shutdown socket connection
  1868. * @sock: socket structure
  1869. * @how: direction to close (must be SHUT_RDWR)
  1870. *
  1871. * Terminates connection (if necessary), then purges socket's receive queue.
  1872. *
  1873. * Returns 0 on success, errno otherwise
  1874. */
  1875. static int tipc_shutdown(struct socket *sock, int how)
  1876. {
  1877. struct sock *sk = sock->sk;
  1878. struct net *net = sock_net(sk);
  1879. struct tipc_sock *tsk = tipc_sk(sk);
  1880. struct sk_buff *skb;
  1881. u32 dnode = tsk_peer_node(tsk);
  1882. u32 dport = tsk_peer_port(tsk);
  1883. u32 onode = tipc_own_addr(net);
  1884. u32 oport = tsk->portid;
  1885. int res;
  1886. if (how != SHUT_RDWR)
  1887. return -EINVAL;
  1888. lock_sock(sk);
  1889. switch (sock->state) {
  1890. case SS_CONNECTING:
  1891. case SS_CONNECTED:
  1892. restart:
  1893. dnode = tsk_peer_node(tsk);
  1894. /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
  1895. skb = __skb_dequeue(&sk->sk_receive_queue);
  1896. if (skb) {
  1897. if (TIPC_SKB_CB(skb)->handle != NULL) {
  1898. kfree_skb(skb);
  1899. goto restart;
  1900. }
  1901. tipc_sk_respond(sk, skb, TIPC_CONN_SHUTDOWN);
  1902. } else {
  1903. skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
  1904. TIPC_CONN_MSG, SHORT_H_SIZE,
  1905. 0, dnode, onode, dport, oport,
  1906. TIPC_CONN_SHUTDOWN);
  1907. if (skb)
  1908. tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
  1909. }
  1910. tsk->connected = 0;
  1911. sock->state = SS_DISCONNECTING;
  1912. tipc_node_remove_conn(net, dnode, tsk->portid);
  1913. /* fall through */
  1914. case SS_DISCONNECTING:
  1915. /* Discard any unreceived messages */
  1916. __skb_queue_purge(&sk->sk_receive_queue);
  1917. /* Wake up anyone sleeping in poll */
  1918. sk->sk_state_change(sk);
  1919. res = 0;
  1920. break;
  1921. default:
  1922. res = -ENOTCONN;
  1923. }
  1924. release_sock(sk);
  1925. return res;
  1926. }
  1927. static void tipc_sk_timeout(unsigned long data)
  1928. {
  1929. struct tipc_sock *tsk = (struct tipc_sock *)data;
  1930. struct sock *sk = &tsk->sk;
  1931. struct sk_buff *skb = NULL;
  1932. u32 peer_port, peer_node;
  1933. u32 own_node = tsk_own_node(tsk);
  1934. bh_lock_sock(sk);
  1935. if (!tsk->connected) {
  1936. bh_unlock_sock(sk);
  1937. goto exit;
  1938. }
  1939. peer_port = tsk_peer_port(tsk);
  1940. peer_node = tsk_peer_node(tsk);
  1941. if (tsk->probing_state == TIPC_CONN_PROBING) {
  1942. if (!sock_owned_by_user(sk)) {
  1943. sk->sk_socket->state = SS_DISCONNECTING;
  1944. tsk->connected = 0;
  1945. tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
  1946. tsk_peer_port(tsk));
  1947. sk->sk_state_change(sk);
  1948. } else {
  1949. /* Try again later */
  1950. sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
  1951. }
  1952. } else {
  1953. skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
  1954. INT_H_SIZE, 0, peer_node, own_node,
  1955. peer_port, tsk->portid, TIPC_OK);
  1956. tsk->probing_state = TIPC_CONN_PROBING;
  1957. sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
  1958. }
  1959. bh_unlock_sock(sk);
  1960. if (skb)
  1961. tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
  1962. exit:
  1963. sock_put(sk);
  1964. }
  1965. static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
  1966. struct tipc_name_seq const *seq)
  1967. {
  1968. struct net *net = sock_net(&tsk->sk);
  1969. struct publication *publ;
  1970. u32 key;
  1971. if (tsk->connected)
  1972. return -EINVAL;
  1973. key = tsk->portid + tsk->pub_count + 1;
  1974. if (key == tsk->portid)
  1975. return -EADDRINUSE;
  1976. publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
  1977. scope, tsk->portid, key);
  1978. if (unlikely(!publ))
  1979. return -EINVAL;
  1980. list_add(&publ->pport_list, &tsk->publications);
  1981. tsk->pub_count++;
  1982. tsk->published = 1;
  1983. return 0;
  1984. }
  1985. static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
  1986. struct tipc_name_seq const *seq)
  1987. {
  1988. struct net *net = sock_net(&tsk->sk);
  1989. struct publication *publ;
  1990. struct publication *safe;
  1991. int rc = -EINVAL;
  1992. list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
  1993. if (seq) {
  1994. if (publ->scope != scope)
  1995. continue;
  1996. if (publ->type != seq->type)
  1997. continue;
  1998. if (publ->lower != seq->lower)
  1999. continue;
  2000. if (publ->upper != seq->upper)
  2001. break;
  2002. tipc_nametbl_withdraw(net, publ->type, publ->lower,
  2003. publ->ref, publ->key);
  2004. rc = 0;
  2005. break;
  2006. }
  2007. tipc_nametbl_withdraw(net, publ->type, publ->lower,
  2008. publ->ref, publ->key);
  2009. rc = 0;
  2010. }
  2011. if (list_empty(&tsk->publications))
  2012. tsk->published = 0;
  2013. return rc;
  2014. }
  2015. /* tipc_sk_reinit: set non-zero address in all existing sockets
  2016. * when we go from standalone to network mode.
  2017. */
  2018. void tipc_sk_reinit(struct net *net)
  2019. {
  2020. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2021. const struct bucket_table *tbl;
  2022. struct rhash_head *pos;
  2023. struct tipc_sock *tsk;
  2024. struct tipc_msg *msg;
  2025. int i;
  2026. rcu_read_lock();
  2027. tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
  2028. for (i = 0; i < tbl->size; i++) {
  2029. rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
  2030. spin_lock_bh(&tsk->sk.sk_lock.slock);
  2031. msg = &tsk->phdr;
  2032. msg_set_prevnode(msg, tn->own_addr);
  2033. msg_set_orignode(msg, tn->own_addr);
  2034. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2035. }
  2036. }
  2037. rcu_read_unlock();
  2038. }
  2039. static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
  2040. {
  2041. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2042. struct tipc_sock *tsk;
  2043. rcu_read_lock();
  2044. tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
  2045. if (tsk)
  2046. sock_hold(&tsk->sk);
  2047. rcu_read_unlock();
  2048. return tsk;
  2049. }
  2050. static int tipc_sk_insert(struct tipc_sock *tsk)
  2051. {
  2052. struct sock *sk = &tsk->sk;
  2053. struct net *net = sock_net(sk);
  2054. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2055. u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
  2056. u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
  2057. while (remaining--) {
  2058. portid++;
  2059. if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
  2060. portid = TIPC_MIN_PORT;
  2061. tsk->portid = portid;
  2062. sock_hold(&tsk->sk);
  2063. if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
  2064. tsk_rht_params))
  2065. return 0;
  2066. sock_put(&tsk->sk);
  2067. }
  2068. return -1;
  2069. }
  2070. static void tipc_sk_remove(struct tipc_sock *tsk)
  2071. {
  2072. struct sock *sk = &tsk->sk;
  2073. struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
  2074. if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
  2075. WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
  2076. __sock_put(sk);
  2077. }
  2078. }
  2079. static const struct rhashtable_params tsk_rht_params = {
  2080. .nelem_hint = 192,
  2081. .head_offset = offsetof(struct tipc_sock, node),
  2082. .key_offset = offsetof(struct tipc_sock, portid),
  2083. .key_len = sizeof(u32), /* portid */
  2084. .max_size = 1048576,
  2085. .min_size = 256,
  2086. .automatic_shrinking = true,
  2087. };
  2088. int tipc_sk_rht_init(struct net *net)
  2089. {
  2090. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2091. return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
  2092. }
  2093. void tipc_sk_rht_destroy(struct net *net)
  2094. {
  2095. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2096. /* Wait for socket readers to complete */
  2097. synchronize_net();
  2098. rhashtable_destroy(&tn->sk_rht);
  2099. }
  2100. /**
  2101. * tipc_setsockopt - set socket option
  2102. * @sock: socket structure
  2103. * @lvl: option level
  2104. * @opt: option identifier
  2105. * @ov: pointer to new option value
  2106. * @ol: length of option value
  2107. *
  2108. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  2109. * (to ease compatibility).
  2110. *
  2111. * Returns 0 on success, errno otherwise
  2112. */
  2113. static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
  2114. char __user *ov, unsigned int ol)
  2115. {
  2116. struct sock *sk = sock->sk;
  2117. struct tipc_sock *tsk = tipc_sk(sk);
  2118. u32 value;
  2119. int res;
  2120. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  2121. return 0;
  2122. if (lvl != SOL_TIPC)
  2123. return -ENOPROTOOPT;
  2124. if (ol < sizeof(value))
  2125. return -EINVAL;
  2126. res = get_user(value, (u32 __user *)ov);
  2127. if (res)
  2128. return res;
  2129. lock_sock(sk);
  2130. switch (opt) {
  2131. case TIPC_IMPORTANCE:
  2132. res = tsk_set_importance(tsk, value);
  2133. break;
  2134. case TIPC_SRC_DROPPABLE:
  2135. if (sock->type != SOCK_STREAM)
  2136. tsk_set_unreliable(tsk, value);
  2137. else
  2138. res = -ENOPROTOOPT;
  2139. break;
  2140. case TIPC_DEST_DROPPABLE:
  2141. tsk_set_unreturnable(tsk, value);
  2142. break;
  2143. case TIPC_CONN_TIMEOUT:
  2144. tipc_sk(sk)->conn_timeout = value;
  2145. /* no need to set "res", since already 0 at this point */
  2146. break;
  2147. default:
  2148. res = -EINVAL;
  2149. }
  2150. release_sock(sk);
  2151. return res;
  2152. }
  2153. /**
  2154. * tipc_getsockopt - get socket option
  2155. * @sock: socket structure
  2156. * @lvl: option level
  2157. * @opt: option identifier
  2158. * @ov: receptacle for option value
  2159. * @ol: receptacle for length of option value
  2160. *
  2161. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  2162. * (to ease compatibility).
  2163. *
  2164. * Returns 0 on success, errno otherwise
  2165. */
  2166. static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
  2167. char __user *ov, int __user *ol)
  2168. {
  2169. struct sock *sk = sock->sk;
  2170. struct tipc_sock *tsk = tipc_sk(sk);
  2171. int len;
  2172. u32 value;
  2173. int res;
  2174. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  2175. return put_user(0, ol);
  2176. if (lvl != SOL_TIPC)
  2177. return -ENOPROTOOPT;
  2178. res = get_user(len, ol);
  2179. if (res)
  2180. return res;
  2181. lock_sock(sk);
  2182. switch (opt) {
  2183. case TIPC_IMPORTANCE:
  2184. value = tsk_importance(tsk);
  2185. break;
  2186. case TIPC_SRC_DROPPABLE:
  2187. value = tsk_unreliable(tsk);
  2188. break;
  2189. case TIPC_DEST_DROPPABLE:
  2190. value = tsk_unreturnable(tsk);
  2191. break;
  2192. case TIPC_CONN_TIMEOUT:
  2193. value = tsk->conn_timeout;
  2194. /* no need to set "res", since already 0 at this point */
  2195. break;
  2196. case TIPC_NODE_RECVQ_DEPTH:
  2197. value = 0; /* was tipc_queue_size, now obsolete */
  2198. break;
  2199. case TIPC_SOCK_RECVQ_DEPTH:
  2200. value = skb_queue_len(&sk->sk_receive_queue);
  2201. break;
  2202. default:
  2203. res = -EINVAL;
  2204. }
  2205. release_sock(sk);
  2206. if (res)
  2207. return res; /* "get" failed */
  2208. if (len < sizeof(value))
  2209. return -EINVAL;
  2210. if (copy_to_user(ov, &value, sizeof(value)))
  2211. return -EFAULT;
  2212. return put_user(sizeof(value), ol);
  2213. }
  2214. static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  2215. {
  2216. struct sock *sk = sock->sk;
  2217. struct tipc_sioc_ln_req lnr;
  2218. void __user *argp = (void __user *)arg;
  2219. switch (cmd) {
  2220. case SIOCGETLINKNAME:
  2221. if (copy_from_user(&lnr, argp, sizeof(lnr)))
  2222. return -EFAULT;
  2223. if (!tipc_node_get_linkname(sock_net(sk),
  2224. lnr.bearer_id & 0xffff, lnr.peer,
  2225. lnr.linkname, TIPC_MAX_LINK_NAME)) {
  2226. if (copy_to_user(argp, &lnr, sizeof(lnr)))
  2227. return -EFAULT;
  2228. return 0;
  2229. }
  2230. return -EADDRNOTAVAIL;
  2231. default:
  2232. return -ENOIOCTLCMD;
  2233. }
  2234. }
  2235. /* Protocol switches for the various types of TIPC sockets */
  2236. static const struct proto_ops msg_ops = {
  2237. .owner = THIS_MODULE,
  2238. .family = AF_TIPC,
  2239. .release = tipc_release,
  2240. .bind = tipc_bind,
  2241. .connect = tipc_connect,
  2242. .socketpair = sock_no_socketpair,
  2243. .accept = sock_no_accept,
  2244. .getname = tipc_getname,
  2245. .poll = tipc_poll,
  2246. .ioctl = tipc_ioctl,
  2247. .listen = sock_no_listen,
  2248. .shutdown = tipc_shutdown,
  2249. .setsockopt = tipc_setsockopt,
  2250. .getsockopt = tipc_getsockopt,
  2251. .sendmsg = tipc_sendmsg,
  2252. .recvmsg = tipc_recvmsg,
  2253. .mmap = sock_no_mmap,
  2254. .sendpage = sock_no_sendpage
  2255. };
  2256. static const struct proto_ops packet_ops = {
  2257. .owner = THIS_MODULE,
  2258. .family = AF_TIPC,
  2259. .release = tipc_release,
  2260. .bind = tipc_bind,
  2261. .connect = tipc_connect,
  2262. .socketpair = sock_no_socketpair,
  2263. .accept = tipc_accept,
  2264. .getname = tipc_getname,
  2265. .poll = tipc_poll,
  2266. .ioctl = tipc_ioctl,
  2267. .listen = tipc_listen,
  2268. .shutdown = tipc_shutdown,
  2269. .setsockopt = tipc_setsockopt,
  2270. .getsockopt = tipc_getsockopt,
  2271. .sendmsg = tipc_send_packet,
  2272. .recvmsg = tipc_recvmsg,
  2273. .mmap = sock_no_mmap,
  2274. .sendpage = sock_no_sendpage
  2275. };
  2276. static const struct proto_ops stream_ops = {
  2277. .owner = THIS_MODULE,
  2278. .family = AF_TIPC,
  2279. .release = tipc_release,
  2280. .bind = tipc_bind,
  2281. .connect = tipc_connect,
  2282. .socketpair = sock_no_socketpair,
  2283. .accept = tipc_accept,
  2284. .getname = tipc_getname,
  2285. .poll = tipc_poll,
  2286. .ioctl = tipc_ioctl,
  2287. .listen = tipc_listen,
  2288. .shutdown = tipc_shutdown,
  2289. .setsockopt = tipc_setsockopt,
  2290. .getsockopt = tipc_getsockopt,
  2291. .sendmsg = tipc_send_stream,
  2292. .recvmsg = tipc_recv_stream,
  2293. .mmap = sock_no_mmap,
  2294. .sendpage = sock_no_sendpage
  2295. };
  2296. static const struct net_proto_family tipc_family_ops = {
  2297. .owner = THIS_MODULE,
  2298. .family = AF_TIPC,
  2299. .create = tipc_sk_create
  2300. };
  2301. static struct proto tipc_proto = {
  2302. .name = "TIPC",
  2303. .owner = THIS_MODULE,
  2304. .obj_size = sizeof(struct tipc_sock),
  2305. .sysctl_rmem = sysctl_tipc_rmem
  2306. };
  2307. /**
  2308. * tipc_socket_init - initialize TIPC socket interface
  2309. *
  2310. * Returns 0 on success, errno otherwise
  2311. */
  2312. int tipc_socket_init(void)
  2313. {
  2314. int res;
  2315. res = proto_register(&tipc_proto, 1);
  2316. if (res) {
  2317. pr_err("Failed to register TIPC protocol type\n");
  2318. goto out;
  2319. }
  2320. res = sock_register(&tipc_family_ops);
  2321. if (res) {
  2322. pr_err("Failed to register TIPC socket type\n");
  2323. proto_unregister(&tipc_proto);
  2324. goto out;
  2325. }
  2326. out:
  2327. return res;
  2328. }
  2329. /**
  2330. * tipc_socket_stop - stop TIPC socket interface
  2331. */
  2332. void tipc_socket_stop(void)
  2333. {
  2334. sock_unregister(tipc_family_ops.family);
  2335. proto_unregister(&tipc_proto);
  2336. }
  2337. /* Caller should hold socket lock for the passed tipc socket. */
  2338. static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
  2339. {
  2340. u32 peer_node;
  2341. u32 peer_port;
  2342. struct nlattr *nest;
  2343. peer_node = tsk_peer_node(tsk);
  2344. peer_port = tsk_peer_port(tsk);
  2345. nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
  2346. if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
  2347. goto msg_full;
  2348. if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
  2349. goto msg_full;
  2350. if (tsk->conn_type != 0) {
  2351. if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
  2352. goto msg_full;
  2353. if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
  2354. goto msg_full;
  2355. if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
  2356. goto msg_full;
  2357. }
  2358. nla_nest_end(skb, nest);
  2359. return 0;
  2360. msg_full:
  2361. nla_nest_cancel(skb, nest);
  2362. return -EMSGSIZE;
  2363. }
  2364. /* Caller should hold socket lock for the passed tipc socket. */
  2365. static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
  2366. struct tipc_sock *tsk)
  2367. {
  2368. int err;
  2369. void *hdr;
  2370. struct nlattr *attrs;
  2371. struct net *net = sock_net(skb->sk);
  2372. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2373. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  2374. &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
  2375. if (!hdr)
  2376. goto msg_cancel;
  2377. attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
  2378. if (!attrs)
  2379. goto genlmsg_cancel;
  2380. if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
  2381. goto attr_msg_cancel;
  2382. if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
  2383. goto attr_msg_cancel;
  2384. if (tsk->connected) {
  2385. err = __tipc_nl_add_sk_con(skb, tsk);
  2386. if (err)
  2387. goto attr_msg_cancel;
  2388. } else if (!list_empty(&tsk->publications)) {
  2389. if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
  2390. goto attr_msg_cancel;
  2391. }
  2392. nla_nest_end(skb, attrs);
  2393. genlmsg_end(skb, hdr);
  2394. return 0;
  2395. attr_msg_cancel:
  2396. nla_nest_cancel(skb, attrs);
  2397. genlmsg_cancel:
  2398. genlmsg_cancel(skb, hdr);
  2399. msg_cancel:
  2400. return -EMSGSIZE;
  2401. }
  2402. int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2403. {
  2404. int err;
  2405. struct tipc_sock *tsk;
  2406. const struct bucket_table *tbl;
  2407. struct rhash_head *pos;
  2408. struct net *net = sock_net(skb->sk);
  2409. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2410. u32 tbl_id = cb->args[0];
  2411. u32 prev_portid = cb->args[1];
  2412. rcu_read_lock();
  2413. tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
  2414. for (; tbl_id < tbl->size; tbl_id++) {
  2415. rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
  2416. spin_lock_bh(&tsk->sk.sk_lock.slock);
  2417. if (prev_portid && prev_portid != tsk->portid) {
  2418. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2419. continue;
  2420. }
  2421. err = __tipc_nl_add_sk(skb, cb, tsk);
  2422. if (err) {
  2423. prev_portid = tsk->portid;
  2424. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2425. goto out;
  2426. }
  2427. prev_portid = 0;
  2428. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2429. }
  2430. }
  2431. out:
  2432. rcu_read_unlock();
  2433. cb->args[0] = tbl_id;
  2434. cb->args[1] = prev_portid;
  2435. return skb->len;
  2436. }
  2437. /* Caller should hold socket lock for the passed tipc socket. */
  2438. static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
  2439. struct netlink_callback *cb,
  2440. struct publication *publ)
  2441. {
  2442. void *hdr;
  2443. struct nlattr *attrs;
  2444. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  2445. &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
  2446. if (!hdr)
  2447. goto msg_cancel;
  2448. attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
  2449. if (!attrs)
  2450. goto genlmsg_cancel;
  2451. if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
  2452. goto attr_msg_cancel;
  2453. if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
  2454. goto attr_msg_cancel;
  2455. if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
  2456. goto attr_msg_cancel;
  2457. if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
  2458. goto attr_msg_cancel;
  2459. nla_nest_end(skb, attrs);
  2460. genlmsg_end(skb, hdr);
  2461. return 0;
  2462. attr_msg_cancel:
  2463. nla_nest_cancel(skb, attrs);
  2464. genlmsg_cancel:
  2465. genlmsg_cancel(skb, hdr);
  2466. msg_cancel:
  2467. return -EMSGSIZE;
  2468. }
  2469. /* Caller should hold socket lock for the passed tipc socket. */
  2470. static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
  2471. struct netlink_callback *cb,
  2472. struct tipc_sock *tsk, u32 *last_publ)
  2473. {
  2474. int err;
  2475. struct publication *p;
  2476. if (*last_publ) {
  2477. list_for_each_entry(p, &tsk->publications, pport_list) {
  2478. if (p->key == *last_publ)
  2479. break;
  2480. }
  2481. if (p->key != *last_publ) {
  2482. /* We never set seq or call nl_dump_check_consistent()
  2483. * this means that setting prev_seq here will cause the
  2484. * consistence check to fail in the netlink callback
  2485. * handler. Resulting in the last NLMSG_DONE message
  2486. * having the NLM_F_DUMP_INTR flag set.
  2487. */
  2488. cb->prev_seq = 1;
  2489. *last_publ = 0;
  2490. return -EPIPE;
  2491. }
  2492. } else {
  2493. p = list_first_entry(&tsk->publications, struct publication,
  2494. pport_list);
  2495. }
  2496. list_for_each_entry_from(p, &tsk->publications, pport_list) {
  2497. err = __tipc_nl_add_sk_publ(skb, cb, p);
  2498. if (err) {
  2499. *last_publ = p->key;
  2500. return err;
  2501. }
  2502. }
  2503. *last_publ = 0;
  2504. return 0;
  2505. }
  2506. int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2507. {
  2508. int err;
  2509. u32 tsk_portid = cb->args[0];
  2510. u32 last_publ = cb->args[1];
  2511. u32 done = cb->args[2];
  2512. struct net *net = sock_net(skb->sk);
  2513. struct tipc_sock *tsk;
  2514. if (!tsk_portid) {
  2515. struct nlattr **attrs;
  2516. struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
  2517. err = tipc_nlmsg_parse(cb->nlh, &attrs);
  2518. if (err)
  2519. return err;
  2520. if (!attrs[TIPC_NLA_SOCK])
  2521. return -EINVAL;
  2522. err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
  2523. attrs[TIPC_NLA_SOCK],
  2524. tipc_nl_sock_policy);
  2525. if (err)
  2526. return err;
  2527. if (!sock[TIPC_NLA_SOCK_REF])
  2528. return -EINVAL;
  2529. tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
  2530. }
  2531. if (done)
  2532. return 0;
  2533. tsk = tipc_sk_lookup(net, tsk_portid);
  2534. if (!tsk)
  2535. return -EINVAL;
  2536. lock_sock(&tsk->sk);
  2537. err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
  2538. if (!err)
  2539. done = 1;
  2540. release_sock(&tsk->sk);
  2541. sock_put(&tsk->sk);
  2542. cb->args[0] = tsk_portid;
  2543. cb->args[1] = last_publ;
  2544. cb->args[2] = done;
  2545. return skb->len;
  2546. }