socket.c 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019
  1. /*
  2. * net/tipc/socket.c: TIPC socket API
  3. *
  4. * Copyright (c) 2001-2007, 2012-2019, Ericsson AB
  5. * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
  6. * Copyright (c) 2020-2021, Red Hat Inc
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the names of the copyright holders nor the names of its
  18. * contributors may be used to endorse or promote products derived from
  19. * this software without specific prior written permission.
  20. *
  21. * Alternatively, this software may be distributed under the terms of the
  22. * GNU General Public License ("GPL") version 2 as published by the Free
  23. * Software Foundation.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <linux/rhashtable.h>
  38. #include <linux/sched/signal.h>
  39. #include "core.h"
  40. #include "name_table.h"
  41. #include "node.h"
  42. #include "link.h"
  43. #include "name_distr.h"
  44. #include "socket.h"
  45. #include "bcast.h"
  46. #include "netlink.h"
  47. #include "group.h"
  48. #include "trace.h"
  49. #define NAGLE_START_INIT 4
  50. #define NAGLE_START_MAX 1024
  51. #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
  52. #define CONN_PROBING_INTV msecs_to_jiffies(3600000) /* [ms] => 1 h */
  53. #define TIPC_MAX_PORT 0xffffffff
  54. #define TIPC_MIN_PORT 1
  55. #define TIPC_ACK_RATE 4 /* ACK at 1/4 of rcv window size */
  56. enum {
  57. TIPC_LISTEN = TCP_LISTEN,
  58. TIPC_ESTABLISHED = TCP_ESTABLISHED,
  59. TIPC_OPEN = TCP_CLOSE,
  60. TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
  61. TIPC_CONNECTING = TCP_SYN_SENT,
  62. };
  63. struct sockaddr_pair {
  64. struct sockaddr_tipc sock;
  65. struct sockaddr_tipc member;
  66. };
  67. /**
  68. * struct tipc_sock - TIPC socket structure
  69. * @sk: socket - interacts with 'port' and with user via the socket API
  70. * @max_pkt: maximum packet size "hint" used when building messages sent by port
  71. * @maxnagle: maximum size of msg which can be subject to nagle
  72. * @portid: unique port identity in TIPC socket hash table
  73. * @phdr: preformatted message header used when sending messages
  74. * @cong_links: list of congested links
  75. * @publications: list of publications for port
  76. * @blocking_link: address of the congested link we are currently sleeping on
  77. * @pub_count: total # of publications port has made during its lifetime
  78. * @conn_timeout: the time we can wait for an unresponded setup request
  79. * @probe_unacked: probe has not received ack yet
  80. * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
  81. * @cong_link_cnt: number of congested links
  82. * @snt_unacked: # messages sent by socket, and not yet acked by peer
  83. * @snd_win: send window size
  84. * @peer_caps: peer capabilities mask
  85. * @rcv_unacked: # messages read by user, but not yet acked back to peer
  86. * @rcv_win: receive window size
  87. * @peer: 'connected' peer for dgram/rdm
  88. * @node: hash table node
  89. * @mc_method: cookie for use between socket and broadcast layer
  90. * @rcu: rcu struct for tipc_sock
  91. * @group: TIPC communications group
  92. * @oneway: message count in one direction (FIXME)
  93. * @nagle_start: current nagle value
  94. * @snd_backlog: send backlog count
  95. * @msg_acc: messages accepted; used in managing backlog and nagle
  96. * @pkt_cnt: TIPC socket packet count
  97. * @expect_ack: whether this TIPC socket is expecting an ack
  98. * @nodelay: setsockopt() TIPC_NODELAY setting
  99. * @group_is_open: TIPC socket group is fully open (FIXME)
  100. * @published: true if port has one or more associated names
  101. * @conn_addrtype: address type used when establishing connection
  102. */
  103. struct tipc_sock {
  104. struct sock sk;
  105. u32 max_pkt;
  106. u32 maxnagle;
  107. u32 portid;
  108. struct tipc_msg phdr;
  109. struct list_head cong_links;
  110. struct list_head publications;
  111. u32 pub_count;
  112. atomic_t dupl_rcvcnt;
  113. u16 conn_timeout;
  114. bool probe_unacked;
  115. u16 cong_link_cnt;
  116. u16 snt_unacked;
  117. u16 snd_win;
  118. u16 peer_caps;
  119. u16 rcv_unacked;
  120. u16 rcv_win;
  121. struct sockaddr_tipc peer;
  122. struct rhash_head node;
  123. struct tipc_mc_method mc_method;
  124. struct rcu_head rcu;
  125. struct tipc_group *group;
  126. u32 oneway;
  127. u32 nagle_start;
  128. u16 snd_backlog;
  129. u16 msg_acc;
  130. u16 pkt_cnt;
  131. bool expect_ack;
  132. bool nodelay;
  133. bool group_is_open;
  134. bool published;
  135. u8 conn_addrtype;
  136. };
  137. static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
  138. static void tipc_data_ready(struct sock *sk);
  139. static void tipc_write_space(struct sock *sk);
  140. static void tipc_sock_destruct(struct sock *sk);
  141. static int tipc_release(struct socket *sock);
  142. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
  143. bool kern);
  144. static void tipc_sk_timeout(struct timer_list *t);
  145. static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua);
  146. static int tipc_sk_withdraw(struct tipc_sock *tsk, struct tipc_uaddr *ua);
  147. static int tipc_sk_leave(struct tipc_sock *tsk);
  148. static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
  149. static int tipc_sk_insert(struct tipc_sock *tsk);
  150. static void tipc_sk_remove(struct tipc_sock *tsk);
  151. static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
  152. static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
  153. static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack);
  154. static int tipc_wait_for_connect(struct socket *sock, long *timeo_p);
  155. static const struct proto_ops packet_ops;
  156. static const struct proto_ops stream_ops;
  157. static const struct proto_ops msg_ops;
  158. static struct proto tipc_proto;
  159. static const struct rhashtable_params tsk_rht_params;
  160. static u32 tsk_own_node(struct tipc_sock *tsk)
  161. {
  162. return msg_prevnode(&tsk->phdr);
  163. }
  164. static u32 tsk_peer_node(struct tipc_sock *tsk)
  165. {
  166. return msg_destnode(&tsk->phdr);
  167. }
  168. static u32 tsk_peer_port(struct tipc_sock *tsk)
  169. {
  170. return msg_destport(&tsk->phdr);
  171. }
  172. static bool tsk_unreliable(struct tipc_sock *tsk)
  173. {
  174. return msg_src_droppable(&tsk->phdr) != 0;
  175. }
  176. static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
  177. {
  178. msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
  179. }
  180. static bool tsk_unreturnable(struct tipc_sock *tsk)
  181. {
  182. return msg_dest_droppable(&tsk->phdr) != 0;
  183. }
  184. static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
  185. {
  186. msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
  187. }
  188. static int tsk_importance(struct tipc_sock *tsk)
  189. {
  190. return msg_importance(&tsk->phdr);
  191. }
  192. static struct tipc_sock *tipc_sk(const struct sock *sk)
  193. {
  194. return container_of(sk, struct tipc_sock, sk);
  195. }
  196. int tsk_set_importance(struct sock *sk, int imp)
  197. {
  198. if (imp > TIPC_CRITICAL_IMPORTANCE)
  199. return -EINVAL;
  200. msg_set_importance(&tipc_sk(sk)->phdr, (u32)imp);
  201. return 0;
  202. }
  203. static bool tsk_conn_cong(struct tipc_sock *tsk)
  204. {
  205. return tsk->snt_unacked > tsk->snd_win;
  206. }
  207. static u16 tsk_blocks(int len)
  208. {
  209. return ((len / FLOWCTL_BLK_SZ) + 1);
  210. }
  211. /* tsk_blocks(): translate a buffer size in bytes to number of
  212. * advertisable blocks, taking into account the ratio truesize(len)/len
  213. * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
  214. */
  215. static u16 tsk_adv_blocks(int len)
  216. {
  217. return len / FLOWCTL_BLK_SZ / 4;
  218. }
  219. /* tsk_inc(): increment counter for sent or received data
  220. * - If block based flow control is not supported by peer we
  221. * fall back to message based ditto, incrementing the counter
  222. */
  223. static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
  224. {
  225. if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
  226. return ((msglen / FLOWCTL_BLK_SZ) + 1);
  227. return 1;
  228. }
  229. /* tsk_set_nagle - enable/disable nagle property by manipulating maxnagle
  230. */
  231. static void tsk_set_nagle(struct tipc_sock *tsk)
  232. {
  233. struct sock *sk = &tsk->sk;
  234. tsk->maxnagle = 0;
  235. if (sk->sk_type != SOCK_STREAM)
  236. return;
  237. if (tsk->nodelay)
  238. return;
  239. if (!(tsk->peer_caps & TIPC_NAGLE))
  240. return;
  241. /* Limit node local buffer size to avoid receive queue overflow */
  242. if (tsk->max_pkt == MAX_MSG_SIZE)
  243. tsk->maxnagle = 1500;
  244. else
  245. tsk->maxnagle = tsk->max_pkt;
  246. }
  247. /**
  248. * tsk_advance_rx_queue - discard first buffer in socket receive queue
  249. * @sk: network socket
  250. *
  251. * Caller must hold socket lock
  252. */
  253. static void tsk_advance_rx_queue(struct sock *sk)
  254. {
  255. trace_tipc_sk_advance_rx(sk, NULL, TIPC_DUMP_SK_RCVQ, " ");
  256. kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
  257. }
  258. /* tipc_sk_respond() : send response message back to sender
  259. */
  260. static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
  261. {
  262. u32 selector;
  263. u32 dnode;
  264. u32 onode = tipc_own_addr(sock_net(sk));
  265. if (!tipc_msg_reverse(onode, &skb, err))
  266. return;
  267. trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE, "@sk_respond!");
  268. dnode = msg_destnode(buf_msg(skb));
  269. selector = msg_origport(buf_msg(skb));
  270. tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
  271. }
  272. /**
  273. * tsk_rej_rx_queue - reject all buffers in socket receive queue
  274. * @sk: network socket
  275. * @error: response error code
  276. *
  277. * Caller must hold socket lock
  278. */
  279. static void tsk_rej_rx_queue(struct sock *sk, int error)
  280. {
  281. struct sk_buff *skb;
  282. while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
  283. tipc_sk_respond(sk, skb, error);
  284. }
  285. static bool tipc_sk_connected(const struct sock *sk)
  286. {
  287. return READ_ONCE(sk->sk_state) == TIPC_ESTABLISHED;
  288. }
  289. /* tipc_sk_type_connectionless - check if the socket is datagram socket
  290. * @sk: socket
  291. *
  292. * Returns true if connection less, false otherwise
  293. */
  294. static bool tipc_sk_type_connectionless(struct sock *sk)
  295. {
  296. return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
  297. }
  298. /* tsk_peer_msg - verify if message was sent by connected port's peer
  299. *
  300. * Handles cases where the node's network address has changed from
  301. * the default of <0.0.0> to its configured setting.
  302. */
  303. static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
  304. {
  305. struct sock *sk = &tsk->sk;
  306. u32 self = tipc_own_addr(sock_net(sk));
  307. u32 peer_port = tsk_peer_port(tsk);
  308. u32 orig_node, peer_node;
  309. if (unlikely(!tipc_sk_connected(sk)))
  310. return false;
  311. if (unlikely(msg_origport(msg) != peer_port))
  312. return false;
  313. orig_node = msg_orignode(msg);
  314. peer_node = tsk_peer_node(tsk);
  315. if (likely(orig_node == peer_node))
  316. return true;
  317. if (!orig_node && peer_node == self)
  318. return true;
  319. if (!peer_node && orig_node == self)
  320. return true;
  321. return false;
  322. }
  323. /* tipc_set_sk_state - set the sk_state of the socket
  324. * @sk: socket
  325. *
  326. * Caller must hold socket lock
  327. *
  328. * Returns 0 on success, errno otherwise
  329. */
  330. static int tipc_set_sk_state(struct sock *sk, int state)
  331. {
  332. int oldsk_state = sk->sk_state;
  333. int res = -EINVAL;
  334. switch (state) {
  335. case TIPC_OPEN:
  336. res = 0;
  337. break;
  338. case TIPC_LISTEN:
  339. case TIPC_CONNECTING:
  340. if (oldsk_state == TIPC_OPEN)
  341. res = 0;
  342. break;
  343. case TIPC_ESTABLISHED:
  344. if (oldsk_state == TIPC_CONNECTING ||
  345. oldsk_state == TIPC_OPEN)
  346. res = 0;
  347. break;
  348. case TIPC_DISCONNECTING:
  349. if (oldsk_state == TIPC_CONNECTING ||
  350. oldsk_state == TIPC_ESTABLISHED)
  351. res = 0;
  352. break;
  353. }
  354. if (!res)
  355. sk->sk_state = state;
  356. return res;
  357. }
  358. static int tipc_sk_sock_err(struct socket *sock, long *timeout)
  359. {
  360. struct sock *sk = sock->sk;
  361. int err = sock_error(sk);
  362. int typ = sock->type;
  363. if (err)
  364. return err;
  365. if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
  366. if (sk->sk_state == TIPC_DISCONNECTING)
  367. return -EPIPE;
  368. else if (!tipc_sk_connected(sk))
  369. return -ENOTCONN;
  370. }
  371. if (!*timeout)
  372. return -EAGAIN;
  373. if (signal_pending(current))
  374. return sock_intr_errno(*timeout);
  375. return 0;
  376. }
  377. #define tipc_wait_for_cond(sock_, timeo_, condition_) \
  378. ({ \
  379. DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
  380. struct sock *sk_; \
  381. int rc_; \
  382. \
  383. while ((rc_ = !(condition_))) { \
  384. /* coupled with smp_wmb() in tipc_sk_proto_rcv() */ \
  385. smp_rmb(); \
  386. sk_ = (sock_)->sk; \
  387. rc_ = tipc_sk_sock_err((sock_), timeo_); \
  388. if (rc_) \
  389. break; \
  390. add_wait_queue(sk_sleep(sk_), &wait_); \
  391. release_sock(sk_); \
  392. *(timeo_) = wait_woken(&wait_, TASK_INTERRUPTIBLE, *(timeo_)); \
  393. sched_annotate_sleep(); \
  394. lock_sock(sk_); \
  395. remove_wait_queue(sk_sleep(sk_), &wait_); \
  396. } \
  397. rc_; \
  398. })
  399. /**
  400. * tipc_sk_create - create a TIPC socket
  401. * @net: network namespace (must be default network)
  402. * @sock: pre-allocated socket structure
  403. * @protocol: protocol indicator (must be 0)
  404. * @kern: caused by kernel or by userspace?
  405. *
  406. * This routine creates additional data structures used by the TIPC socket,
  407. * initializes them, and links them together.
  408. *
  409. * Return: 0 on success, errno otherwise
  410. */
  411. static int tipc_sk_create(struct net *net, struct socket *sock,
  412. int protocol, int kern)
  413. {
  414. const struct proto_ops *ops;
  415. struct sock *sk;
  416. struct tipc_sock *tsk;
  417. struct tipc_msg *msg;
  418. /* Validate arguments */
  419. if (unlikely(protocol != 0))
  420. return -EPROTONOSUPPORT;
  421. switch (sock->type) {
  422. case SOCK_STREAM:
  423. ops = &stream_ops;
  424. break;
  425. case SOCK_SEQPACKET:
  426. ops = &packet_ops;
  427. break;
  428. case SOCK_DGRAM:
  429. case SOCK_RDM:
  430. ops = &msg_ops;
  431. break;
  432. default:
  433. return -EPROTOTYPE;
  434. }
  435. /* Allocate socket's protocol area */
  436. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
  437. if (sk == NULL)
  438. return -ENOMEM;
  439. tsk = tipc_sk(sk);
  440. tsk->max_pkt = MAX_PKT_DEFAULT;
  441. tsk->maxnagle = 0;
  442. tsk->nagle_start = NAGLE_START_INIT;
  443. INIT_LIST_HEAD(&tsk->publications);
  444. INIT_LIST_HEAD(&tsk->cong_links);
  445. msg = &tsk->phdr;
  446. /* Finish initializing socket data structures */
  447. sock->ops = ops;
  448. sock_init_data(sock, sk);
  449. tipc_set_sk_state(sk, TIPC_OPEN);
  450. if (tipc_sk_insert(tsk)) {
  451. sk_free(sk);
  452. pr_warn("Socket create failed; port number exhausted\n");
  453. return -EINVAL;
  454. }
  455. /* Ensure tsk is visible before we read own_addr. */
  456. smp_mb();
  457. tipc_msg_init(tipc_own_addr(net), msg, TIPC_LOW_IMPORTANCE,
  458. TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
  459. msg_set_origport(msg, tsk->portid);
  460. timer_setup(&sk->sk_timer, tipc_sk_timeout, 0);
  461. sk->sk_shutdown = 0;
  462. sk->sk_backlog_rcv = tipc_sk_backlog_rcv;
  463. sk->sk_rcvbuf = READ_ONCE(sysctl_tipc_rmem[1]);
  464. sk->sk_data_ready = tipc_data_ready;
  465. sk->sk_write_space = tipc_write_space;
  466. sk->sk_destruct = tipc_sock_destruct;
  467. tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
  468. tsk->group_is_open = true;
  469. atomic_set(&tsk->dupl_rcvcnt, 0);
  470. /* Start out with safe limits until we receive an advertised window */
  471. tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
  472. tsk->rcv_win = tsk->snd_win;
  473. if (tipc_sk_type_connectionless(sk)) {
  474. tsk_set_unreturnable(tsk, true);
  475. if (sock->type == SOCK_DGRAM)
  476. tsk_set_unreliable(tsk, true);
  477. }
  478. __skb_queue_head_init(&tsk->mc_method.deferredq);
  479. trace_tipc_sk_create(sk, NULL, TIPC_DUMP_NONE, " ");
  480. return 0;
  481. }
  482. static void tipc_sk_callback(struct rcu_head *head)
  483. {
  484. struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
  485. sock_put(&tsk->sk);
  486. }
  487. /* Caller should hold socket lock for the socket. */
  488. static void __tipc_shutdown(struct socket *sock, int error)
  489. {
  490. struct sock *sk = sock->sk;
  491. struct tipc_sock *tsk = tipc_sk(sk);
  492. struct net *net = sock_net(sk);
  493. long timeout = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
  494. u32 dnode = tsk_peer_node(tsk);
  495. struct sk_buff *skb;
  496. /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
  497. tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
  498. !tsk_conn_cong(tsk)));
  499. /* Push out delayed messages if in Nagle mode */
  500. tipc_sk_push_backlog(tsk, false);
  501. /* Remove pending SYN */
  502. __skb_queue_purge(&sk->sk_write_queue);
  503. /* Remove partially received buffer if any */
  504. skb = skb_peek(&sk->sk_receive_queue);
  505. if (skb && TIPC_SKB_CB(skb)->bytes_read) {
  506. __skb_unlink(skb, &sk->sk_receive_queue);
  507. kfree_skb(skb);
  508. }
  509. /* Reject all unreceived messages if connectionless */
  510. if (tipc_sk_type_connectionless(sk)) {
  511. tsk_rej_rx_queue(sk, error);
  512. return;
  513. }
  514. switch (sk->sk_state) {
  515. case TIPC_CONNECTING:
  516. case TIPC_ESTABLISHED:
  517. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  518. tipc_node_remove_conn(net, dnode, tsk->portid);
  519. /* Send a FIN+/- to its peer */
  520. skb = __skb_dequeue(&sk->sk_receive_queue);
  521. if (skb) {
  522. __skb_queue_purge(&sk->sk_receive_queue);
  523. tipc_sk_respond(sk, skb, error);
  524. break;
  525. }
  526. skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
  527. TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
  528. tsk_own_node(tsk), tsk_peer_port(tsk),
  529. tsk->portid, error);
  530. if (skb)
  531. tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
  532. break;
  533. case TIPC_LISTEN:
  534. /* Reject all SYN messages */
  535. tsk_rej_rx_queue(sk, error);
  536. break;
  537. default:
  538. __skb_queue_purge(&sk->sk_receive_queue);
  539. break;
  540. }
  541. }
  542. /**
  543. * tipc_release - destroy a TIPC socket
  544. * @sock: socket to destroy
  545. *
  546. * This routine cleans up any messages that are still queued on the socket.
  547. * For DGRAM and RDM socket types, all queued messages are rejected.
  548. * For SEQPACKET and STREAM socket types, the first message is rejected
  549. * and any others are discarded. (If the first message on a STREAM socket
  550. * is partially-read, it is discarded and the next one is rejected instead.)
  551. *
  552. * NOTE: Rejected messages are not necessarily returned to the sender! They
  553. * are returned or discarded according to the "destination droppable" setting
  554. * specified for the message by the sender.
  555. *
  556. * Return: 0 on success, errno otherwise
  557. */
  558. static int tipc_release(struct socket *sock)
  559. {
  560. struct sock *sk = sock->sk;
  561. struct tipc_sock *tsk;
  562. /*
  563. * Exit if socket isn't fully initialized (occurs when a failed accept()
  564. * releases a pre-allocated child socket that was never used)
  565. */
  566. if (sk == NULL)
  567. return 0;
  568. tsk = tipc_sk(sk);
  569. lock_sock(sk);
  570. trace_tipc_sk_release(sk, NULL, TIPC_DUMP_ALL, " ");
  571. __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
  572. sk->sk_shutdown = SHUTDOWN_MASK;
  573. tipc_sk_leave(tsk);
  574. tipc_sk_withdraw(tsk, NULL);
  575. __skb_queue_purge(&tsk->mc_method.deferredq);
  576. sk_stop_timer(sk, &sk->sk_timer);
  577. tipc_sk_remove(tsk);
  578. sock_orphan(sk);
  579. /* Reject any messages that accumulated in backlog queue */
  580. release_sock(sk);
  581. tipc_dest_list_purge(&tsk->cong_links);
  582. tsk->cong_link_cnt = 0;
  583. call_rcu(&tsk->rcu, tipc_sk_callback);
  584. sock->sk = NULL;
  585. return 0;
  586. }
  587. /**
  588. * __tipc_bind - associate or disassocate TIPC name(s) with a socket
  589. * @sock: socket structure
  590. * @skaddr: socket address describing name(s) and desired operation
  591. * @alen: size of socket address data structure
  592. *
  593. * Name and name sequence binding are indicated using a positive scope value;
  594. * a negative scope value unbinds the specified name. Specifying no name
  595. * (i.e. a socket address length of 0) unbinds all names from the socket.
  596. *
  597. * Return: 0 on success, errno otherwise
  598. *
  599. * NOTE: This routine doesn't need to take the socket lock since it doesn't
  600. * access any non-constant socket information.
  601. */
  602. static int __tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
  603. {
  604. struct tipc_uaddr *ua = (struct tipc_uaddr *)skaddr;
  605. struct tipc_sock *tsk = tipc_sk(sock->sk);
  606. bool unbind = false;
  607. if (unlikely(!alen))
  608. return tipc_sk_withdraw(tsk, NULL);
  609. if (ua->addrtype == TIPC_SERVICE_ADDR) {
  610. ua->addrtype = TIPC_SERVICE_RANGE;
  611. ua->sr.upper = ua->sr.lower;
  612. }
  613. if (ua->scope < 0) {
  614. unbind = true;
  615. ua->scope = -ua->scope;
  616. }
  617. /* Users may still use deprecated TIPC_ZONE_SCOPE */
  618. if (ua->scope != TIPC_NODE_SCOPE)
  619. ua->scope = TIPC_CLUSTER_SCOPE;
  620. if (tsk->group)
  621. return -EACCES;
  622. if (unbind)
  623. return tipc_sk_withdraw(tsk, ua);
  624. return tipc_sk_publish(tsk, ua);
  625. }
  626. int tipc_sk_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
  627. {
  628. int res;
  629. lock_sock(sock->sk);
  630. res = __tipc_bind(sock, skaddr, alen);
  631. release_sock(sock->sk);
  632. return res;
  633. }
  634. static int tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
  635. {
  636. struct tipc_uaddr *ua = (struct tipc_uaddr *)skaddr;
  637. u32 atype = ua->addrtype;
  638. if (alen) {
  639. if (!tipc_uaddr_valid(ua, alen))
  640. return -EINVAL;
  641. if (atype == TIPC_SOCKET_ADDR)
  642. return -EAFNOSUPPORT;
  643. if (ua->sr.type < TIPC_RESERVED_TYPES) {
  644. pr_warn_once("Can't bind to reserved service type %u\n",
  645. ua->sr.type);
  646. return -EACCES;
  647. }
  648. }
  649. return tipc_sk_bind(sock, skaddr, alen);
  650. }
  651. /**
  652. * tipc_getname - get port ID of socket or peer socket
  653. * @sock: socket structure
  654. * @uaddr: area for returned socket address
  655. * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
  656. *
  657. * Return: 0 on success, errno otherwise
  658. *
  659. * NOTE: This routine doesn't need to take the socket lock since it only
  660. * accesses socket information that is unchanging (or which changes in
  661. * a completely predictable manner).
  662. */
  663. static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
  664. int peer)
  665. {
  666. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  667. struct sock *sk = sock->sk;
  668. struct tipc_sock *tsk = tipc_sk(sk);
  669. memset(addr, 0, sizeof(*addr));
  670. if (peer) {
  671. if ((!tipc_sk_connected(sk)) &&
  672. ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
  673. return -ENOTCONN;
  674. addr->addr.id.ref = tsk_peer_port(tsk);
  675. addr->addr.id.node = tsk_peer_node(tsk);
  676. } else {
  677. addr->addr.id.ref = tsk->portid;
  678. addr->addr.id.node = tipc_own_addr(sock_net(sk));
  679. }
  680. addr->addrtype = TIPC_SOCKET_ADDR;
  681. addr->family = AF_TIPC;
  682. addr->scope = 0;
  683. addr->addr.name.domain = 0;
  684. return sizeof(*addr);
  685. }
  686. /**
  687. * tipc_poll - read and possibly block on pollmask
  688. * @file: file structure associated with the socket
  689. * @sock: socket for which to calculate the poll bits
  690. * @wait: ???
  691. *
  692. * Return: pollmask value
  693. *
  694. * COMMENTARY:
  695. * It appears that the usual socket locking mechanisms are not useful here
  696. * since the pollmask info is potentially out-of-date the moment this routine
  697. * exits. TCP and other protocols seem to rely on higher level poll routines
  698. * to handle any preventable race conditions, so TIPC will do the same ...
  699. *
  700. * IMPORTANT: The fact that a read or write operation is indicated does NOT
  701. * imply that the operation will succeed, merely that it should be performed
  702. * and will not block.
  703. */
  704. static __poll_t tipc_poll(struct file *file, struct socket *sock,
  705. poll_table *wait)
  706. {
  707. struct sock *sk = sock->sk;
  708. struct tipc_sock *tsk = tipc_sk(sk);
  709. __poll_t revents = 0;
  710. sock_poll_wait(file, sock, wait);
  711. trace_tipc_sk_poll(sk, NULL, TIPC_DUMP_ALL, " ");
  712. if (sk->sk_shutdown & RCV_SHUTDOWN)
  713. revents |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
  714. if (sk->sk_shutdown == SHUTDOWN_MASK)
  715. revents |= EPOLLHUP;
  716. switch (sk->sk_state) {
  717. case TIPC_ESTABLISHED:
  718. if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
  719. revents |= EPOLLOUT;
  720. fallthrough;
  721. case TIPC_LISTEN:
  722. case TIPC_CONNECTING:
  723. if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
  724. revents |= EPOLLIN | EPOLLRDNORM;
  725. break;
  726. case TIPC_OPEN:
  727. if (tsk->group_is_open && !tsk->cong_link_cnt)
  728. revents |= EPOLLOUT;
  729. if (!tipc_sk_type_connectionless(sk))
  730. break;
  731. if (skb_queue_empty_lockless(&sk->sk_receive_queue))
  732. break;
  733. revents |= EPOLLIN | EPOLLRDNORM;
  734. break;
  735. case TIPC_DISCONNECTING:
  736. revents = EPOLLIN | EPOLLRDNORM | EPOLLHUP;
  737. break;
  738. }
  739. return revents;
  740. }
  741. /**
  742. * tipc_sendmcast - send multicast message
  743. * @sock: socket structure
  744. * @ua: destination address struct
  745. * @msg: message to send
  746. * @dlen: length of data to send
  747. * @timeout: timeout to wait for wakeup
  748. *
  749. * Called from function tipc_sendmsg(), which has done all sanity checks
  750. * Return: the number of bytes sent on success, or errno
  751. */
  752. static int tipc_sendmcast(struct socket *sock, struct tipc_uaddr *ua,
  753. struct msghdr *msg, size_t dlen, long timeout)
  754. {
  755. struct sock *sk = sock->sk;
  756. struct tipc_sock *tsk = tipc_sk(sk);
  757. struct tipc_msg *hdr = &tsk->phdr;
  758. struct net *net = sock_net(sk);
  759. int mtu = tipc_bcast_get_mtu(net);
  760. struct sk_buff_head pkts;
  761. struct tipc_nlist dsts;
  762. int rc;
  763. if (tsk->group)
  764. return -EACCES;
  765. /* Block or return if any destination link is congested */
  766. rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
  767. if (unlikely(rc))
  768. return rc;
  769. /* Lookup destination nodes */
  770. tipc_nlist_init(&dsts, tipc_own_addr(net));
  771. tipc_nametbl_lookup_mcast_nodes(net, ua, &dsts);
  772. if (!dsts.local && !dsts.remote)
  773. return -EHOSTUNREACH;
  774. /* Build message header */
  775. msg_set_type(hdr, TIPC_MCAST_MSG);
  776. msg_set_hdr_sz(hdr, MCAST_H_SIZE);
  777. msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
  778. msg_set_destport(hdr, 0);
  779. msg_set_destnode(hdr, 0);
  780. msg_set_nametype(hdr, ua->sr.type);
  781. msg_set_namelower(hdr, ua->sr.lower);
  782. msg_set_nameupper(hdr, ua->sr.upper);
  783. /* Build message as chain of buffers */
  784. __skb_queue_head_init(&pkts);
  785. rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
  786. /* Send message if build was successful */
  787. if (unlikely(rc == dlen)) {
  788. trace_tipc_sk_sendmcast(sk, skb_peek(&pkts),
  789. TIPC_DUMP_SK_SNDQ, " ");
  790. rc = tipc_mcast_xmit(net, &pkts, &tsk->mc_method, &dsts,
  791. &tsk->cong_link_cnt);
  792. }
  793. tipc_nlist_purge(&dsts);
  794. return rc ? rc : dlen;
  795. }
  796. /**
  797. * tipc_send_group_msg - send a message to a member in the group
  798. * @net: network namespace
  799. * @tsk: tipc socket
  800. * @m: message to send
  801. * @mb: group member
  802. * @dnode: destination node
  803. * @dport: destination port
  804. * @dlen: total length of message data
  805. */
  806. static int tipc_send_group_msg(struct net *net, struct tipc_sock *tsk,
  807. struct msghdr *m, struct tipc_member *mb,
  808. u32 dnode, u32 dport, int dlen)
  809. {
  810. u16 bc_snd_nxt = tipc_group_bc_snd_nxt(tsk->group);
  811. struct tipc_mc_method *method = &tsk->mc_method;
  812. int blks = tsk_blocks(GROUP_H_SIZE + dlen);
  813. struct tipc_msg *hdr = &tsk->phdr;
  814. struct sk_buff_head pkts;
  815. int mtu, rc;
  816. /* Complete message header */
  817. msg_set_type(hdr, TIPC_GRP_UCAST_MSG);
  818. msg_set_hdr_sz(hdr, GROUP_H_SIZE);
  819. msg_set_destport(hdr, dport);
  820. msg_set_destnode(hdr, dnode);
  821. msg_set_grp_bc_seqno(hdr, bc_snd_nxt);
  822. /* Build message as chain of buffers */
  823. __skb_queue_head_init(&pkts);
  824. mtu = tipc_node_get_mtu(net, dnode, tsk->portid, false);
  825. rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
  826. if (unlikely(rc != dlen))
  827. return rc;
  828. /* Send message */
  829. rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
  830. if (unlikely(rc == -ELINKCONG)) {
  831. tipc_dest_push(&tsk->cong_links, dnode, 0);
  832. tsk->cong_link_cnt++;
  833. }
  834. /* Update send window */
  835. tipc_group_update_member(mb, blks);
  836. /* A broadcast sent within next EXPIRE period must follow same path */
  837. method->rcast = true;
  838. method->mandatory = true;
  839. return dlen;
  840. }
  841. /**
  842. * tipc_send_group_unicast - send message to a member in the group
  843. * @sock: socket structure
  844. * @m: message to send
  845. * @dlen: total length of message data
  846. * @timeout: timeout to wait for wakeup
  847. *
  848. * Called from function tipc_sendmsg(), which has done all sanity checks
  849. * Return: the number of bytes sent on success, or errno
  850. */
  851. static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m,
  852. int dlen, long timeout)
  853. {
  854. struct sock *sk = sock->sk;
  855. struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
  856. int blks = tsk_blocks(GROUP_H_SIZE + dlen);
  857. struct tipc_sock *tsk = tipc_sk(sk);
  858. struct net *net = sock_net(sk);
  859. struct tipc_member *mb = NULL;
  860. u32 node, port;
  861. int rc;
  862. node = ua->sk.node;
  863. port = ua->sk.ref;
  864. if (!port && !node)
  865. return -EHOSTUNREACH;
  866. /* Block or return if destination link or member is congested */
  867. rc = tipc_wait_for_cond(sock, &timeout,
  868. !tipc_dest_find(&tsk->cong_links, node, 0) &&
  869. tsk->group &&
  870. !tipc_group_cong(tsk->group, node, port, blks,
  871. &mb));
  872. if (unlikely(rc))
  873. return rc;
  874. if (unlikely(!mb))
  875. return -EHOSTUNREACH;
  876. rc = tipc_send_group_msg(net, tsk, m, mb, node, port, dlen);
  877. return rc ? rc : dlen;
  878. }
  879. /**
  880. * tipc_send_group_anycast - send message to any member with given identity
  881. * @sock: socket structure
  882. * @m: message to send
  883. * @dlen: total length of message data
  884. * @timeout: timeout to wait for wakeup
  885. *
  886. * Called from function tipc_sendmsg(), which has done all sanity checks
  887. * Return: the number of bytes sent on success, or errno
  888. */
  889. static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
  890. int dlen, long timeout)
  891. {
  892. struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
  893. struct sock *sk = sock->sk;
  894. struct tipc_sock *tsk = tipc_sk(sk);
  895. struct list_head *cong_links = &tsk->cong_links;
  896. int blks = tsk_blocks(GROUP_H_SIZE + dlen);
  897. struct tipc_msg *hdr = &tsk->phdr;
  898. struct tipc_member *first = NULL;
  899. struct tipc_member *mbr = NULL;
  900. struct net *net = sock_net(sk);
  901. u32 node, port, exclude;
  902. struct list_head dsts;
  903. int lookups = 0;
  904. int dstcnt, rc;
  905. bool cong;
  906. INIT_LIST_HEAD(&dsts);
  907. ua->sa.type = msg_nametype(hdr);
  908. ua->scope = msg_lookup_scope(hdr);
  909. while (++lookups < 4) {
  910. exclude = tipc_group_exclude(tsk->group);
  911. first = NULL;
  912. /* Look for a non-congested destination member, if any */
  913. while (1) {
  914. if (!tipc_nametbl_lookup_group(net, ua, &dsts, &dstcnt,
  915. exclude, false))
  916. return -EHOSTUNREACH;
  917. tipc_dest_pop(&dsts, &node, &port);
  918. cong = tipc_group_cong(tsk->group, node, port, blks,
  919. &mbr);
  920. if (!cong)
  921. break;
  922. if (mbr == first)
  923. break;
  924. if (!first)
  925. first = mbr;
  926. }
  927. /* Start over if destination was not in member list */
  928. if (unlikely(!mbr))
  929. continue;
  930. if (likely(!cong && !tipc_dest_find(cong_links, node, 0)))
  931. break;
  932. /* Block or return if destination link or member is congested */
  933. rc = tipc_wait_for_cond(sock, &timeout,
  934. !tipc_dest_find(cong_links, node, 0) &&
  935. tsk->group &&
  936. !tipc_group_cong(tsk->group, node, port,
  937. blks, &mbr));
  938. if (unlikely(rc))
  939. return rc;
  940. /* Send, unless destination disappeared while waiting */
  941. if (likely(mbr))
  942. break;
  943. }
  944. if (unlikely(lookups >= 4))
  945. return -EHOSTUNREACH;
  946. rc = tipc_send_group_msg(net, tsk, m, mbr, node, port, dlen);
  947. return rc ? rc : dlen;
  948. }
  949. /**
  950. * tipc_send_group_bcast - send message to all members in communication group
  951. * @sock: socket structure
  952. * @m: message to send
  953. * @dlen: total length of message data
  954. * @timeout: timeout to wait for wakeup
  955. *
  956. * Called from function tipc_sendmsg(), which has done all sanity checks
  957. * Return: the number of bytes sent on success, or errno
  958. */
  959. static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
  960. int dlen, long timeout)
  961. {
  962. struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
  963. struct sock *sk = sock->sk;
  964. struct net *net = sock_net(sk);
  965. struct tipc_sock *tsk = tipc_sk(sk);
  966. struct tipc_nlist *dsts;
  967. struct tipc_mc_method *method = &tsk->mc_method;
  968. bool ack = method->mandatory && method->rcast;
  969. int blks = tsk_blocks(MCAST_H_SIZE + dlen);
  970. struct tipc_msg *hdr = &tsk->phdr;
  971. int mtu = tipc_bcast_get_mtu(net);
  972. struct sk_buff_head pkts;
  973. int rc = -EHOSTUNREACH;
  974. /* Block or return if any destination link or member is congested */
  975. rc = tipc_wait_for_cond(sock, &timeout,
  976. !tsk->cong_link_cnt && tsk->group &&
  977. !tipc_group_bc_cong(tsk->group, blks));
  978. if (unlikely(rc))
  979. return rc;
  980. dsts = tipc_group_dests(tsk->group);
  981. if (!dsts->local && !dsts->remote)
  982. return -EHOSTUNREACH;
  983. /* Complete message header */
  984. if (ua) {
  985. msg_set_type(hdr, TIPC_GRP_MCAST_MSG);
  986. msg_set_nameinst(hdr, ua->sa.instance);
  987. } else {
  988. msg_set_type(hdr, TIPC_GRP_BCAST_MSG);
  989. msg_set_nameinst(hdr, 0);
  990. }
  991. msg_set_hdr_sz(hdr, GROUP_H_SIZE);
  992. msg_set_destport(hdr, 0);
  993. msg_set_destnode(hdr, 0);
  994. msg_set_grp_bc_seqno(hdr, tipc_group_bc_snd_nxt(tsk->group));
  995. /* Avoid getting stuck with repeated forced replicasts */
  996. msg_set_grp_bc_ack_req(hdr, ack);
  997. /* Build message as chain of buffers */
  998. __skb_queue_head_init(&pkts);
  999. rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
  1000. if (unlikely(rc != dlen))
  1001. return rc;
  1002. /* Send message */
  1003. rc = tipc_mcast_xmit(net, &pkts, method, dsts, &tsk->cong_link_cnt);
  1004. if (unlikely(rc))
  1005. return rc;
  1006. /* Update broadcast sequence number and send windows */
  1007. tipc_group_update_bc_members(tsk->group, blks, ack);
  1008. /* Broadcast link is now free to choose method for next broadcast */
  1009. method->mandatory = false;
  1010. method->expires = jiffies;
  1011. return dlen;
  1012. }
  1013. /**
  1014. * tipc_send_group_mcast - send message to all members with given identity
  1015. * @sock: socket structure
  1016. * @m: message to send
  1017. * @dlen: total length of message data
  1018. * @timeout: timeout to wait for wakeup
  1019. *
  1020. * Called from function tipc_sendmsg(), which has done all sanity checks
  1021. * Return: the number of bytes sent on success, or errno
  1022. */
  1023. static int tipc_send_group_mcast(struct socket *sock, struct msghdr *m,
  1024. int dlen, long timeout)
  1025. {
  1026. struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
  1027. struct sock *sk = sock->sk;
  1028. struct tipc_sock *tsk = tipc_sk(sk);
  1029. struct tipc_group *grp = tsk->group;
  1030. struct tipc_msg *hdr = &tsk->phdr;
  1031. struct net *net = sock_net(sk);
  1032. struct list_head dsts;
  1033. u32 dstcnt, exclude;
  1034. INIT_LIST_HEAD(&dsts);
  1035. ua->sa.type = msg_nametype(hdr);
  1036. ua->scope = msg_lookup_scope(hdr);
  1037. exclude = tipc_group_exclude(grp);
  1038. if (!tipc_nametbl_lookup_group(net, ua, &dsts, &dstcnt, exclude, true))
  1039. return -EHOSTUNREACH;
  1040. if (dstcnt == 1) {
  1041. tipc_dest_pop(&dsts, &ua->sk.node, &ua->sk.ref);
  1042. return tipc_send_group_unicast(sock, m, dlen, timeout);
  1043. }
  1044. tipc_dest_list_purge(&dsts);
  1045. return tipc_send_group_bcast(sock, m, dlen, timeout);
  1046. }
  1047. /**
  1048. * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
  1049. * @net: the associated network namespace
  1050. * @arrvq: queue with arriving messages, to be cloned after destination lookup
  1051. * @inputq: queue with cloned messages, delivered to socket after dest lookup
  1052. *
  1053. * Multi-threaded: parallel calls with reference to same queues may occur
  1054. */
  1055. void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
  1056. struct sk_buff_head *inputq)
  1057. {
  1058. u32 self = tipc_own_addr(net);
  1059. struct sk_buff *skb, *_skb;
  1060. u32 portid, onode;
  1061. struct sk_buff_head tmpq;
  1062. struct list_head dports;
  1063. struct tipc_msg *hdr;
  1064. struct tipc_uaddr ua;
  1065. int user, mtyp, hlen;
  1066. __skb_queue_head_init(&tmpq);
  1067. INIT_LIST_HEAD(&dports);
  1068. ua.addrtype = TIPC_SERVICE_RANGE;
  1069. /* tipc_skb_peek() increments the head skb's reference counter */
  1070. skb = tipc_skb_peek(arrvq, &inputq->lock);
  1071. for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
  1072. hdr = buf_msg(skb);
  1073. user = msg_user(hdr);
  1074. mtyp = msg_type(hdr);
  1075. hlen = skb_headroom(skb) + msg_hdr_sz(hdr);
  1076. onode = msg_orignode(hdr);
  1077. ua.sr.type = msg_nametype(hdr);
  1078. ua.sr.lower = msg_namelower(hdr);
  1079. ua.sr.upper = msg_nameupper(hdr);
  1080. if (onode == self)
  1081. ua.scope = TIPC_ANY_SCOPE;
  1082. else
  1083. ua.scope = TIPC_CLUSTER_SCOPE;
  1084. if (mtyp == TIPC_GRP_UCAST_MSG || user == GROUP_PROTOCOL) {
  1085. spin_lock_bh(&inputq->lock);
  1086. if (skb_peek(arrvq) == skb) {
  1087. __skb_dequeue(arrvq);
  1088. __skb_queue_tail(inputq, skb);
  1089. }
  1090. kfree_skb(skb);
  1091. spin_unlock_bh(&inputq->lock);
  1092. continue;
  1093. }
  1094. /* Group messages require exact scope match */
  1095. if (msg_in_group(hdr)) {
  1096. ua.sr.lower = 0;
  1097. ua.sr.upper = ~0;
  1098. ua.scope = msg_lookup_scope(hdr);
  1099. }
  1100. /* Create destination port list: */
  1101. tipc_nametbl_lookup_mcast_sockets(net, &ua, &dports);
  1102. /* Clone message per destination */
  1103. while (tipc_dest_pop(&dports, NULL, &portid)) {
  1104. _skb = __pskb_copy(skb, hlen, GFP_ATOMIC);
  1105. if (_skb) {
  1106. msg_set_destport(buf_msg(_skb), portid);
  1107. __skb_queue_tail(&tmpq, _skb);
  1108. continue;
  1109. }
  1110. pr_warn("Failed to clone mcast rcv buffer\n");
  1111. }
  1112. /* Append clones to inputq only if skb is still head of arrvq */
  1113. spin_lock_bh(&inputq->lock);
  1114. if (skb_peek(arrvq) == skb) {
  1115. skb_queue_splice_tail_init(&tmpq, inputq);
  1116. /* Decrement the skb's refcnt */
  1117. kfree_skb(__skb_dequeue(arrvq));
  1118. }
  1119. spin_unlock_bh(&inputq->lock);
  1120. __skb_queue_purge(&tmpq);
  1121. kfree_skb(skb);
  1122. }
  1123. tipc_sk_rcv(net, inputq);
  1124. }
  1125. /* tipc_sk_push_backlog(): send accumulated buffers in socket write queue
  1126. * when socket is in Nagle mode
  1127. */
  1128. static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack)
  1129. {
  1130. struct sk_buff_head *txq = &tsk->sk.sk_write_queue;
  1131. struct sk_buff *skb = skb_peek_tail(txq);
  1132. struct net *net = sock_net(&tsk->sk);
  1133. u32 dnode = tsk_peer_node(tsk);
  1134. int rc;
  1135. if (nagle_ack) {
  1136. tsk->pkt_cnt += skb_queue_len(txq);
  1137. if (!tsk->pkt_cnt || tsk->msg_acc / tsk->pkt_cnt < 2) {
  1138. tsk->oneway = 0;
  1139. if (tsk->nagle_start < NAGLE_START_MAX)
  1140. tsk->nagle_start *= 2;
  1141. tsk->expect_ack = false;
  1142. pr_debug("tsk %10u: bad nagle %u -> %u, next start %u!\n",
  1143. tsk->portid, tsk->msg_acc, tsk->pkt_cnt,
  1144. tsk->nagle_start);
  1145. } else {
  1146. tsk->nagle_start = NAGLE_START_INIT;
  1147. if (skb) {
  1148. msg_set_ack_required(buf_msg(skb));
  1149. tsk->expect_ack = true;
  1150. } else {
  1151. tsk->expect_ack = false;
  1152. }
  1153. }
  1154. tsk->msg_acc = 0;
  1155. tsk->pkt_cnt = 0;
  1156. }
  1157. if (!skb || tsk->cong_link_cnt)
  1158. return;
  1159. /* Do not send SYN again after congestion */
  1160. if (msg_is_syn(buf_msg(skb)))
  1161. return;
  1162. if (tsk->msg_acc)
  1163. tsk->pkt_cnt += skb_queue_len(txq);
  1164. tsk->snt_unacked += tsk->snd_backlog;
  1165. tsk->snd_backlog = 0;
  1166. rc = tipc_node_xmit(net, txq, dnode, tsk->portid);
  1167. if (rc == -ELINKCONG)
  1168. tsk->cong_link_cnt = 1;
  1169. }
  1170. /**
  1171. * tipc_sk_conn_proto_rcv - receive a connection mng protocol message
  1172. * @tsk: receiving socket
  1173. * @skb: pointer to message buffer.
  1174. * @inputq: buffer list containing the buffers
  1175. * @xmitq: output message area
  1176. */
  1177. static void tipc_sk_conn_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
  1178. struct sk_buff_head *inputq,
  1179. struct sk_buff_head *xmitq)
  1180. {
  1181. struct tipc_msg *hdr = buf_msg(skb);
  1182. u32 onode = tsk_own_node(tsk);
  1183. struct sock *sk = &tsk->sk;
  1184. int mtyp = msg_type(hdr);
  1185. bool was_cong;
  1186. /* Ignore if connection cannot be validated: */
  1187. if (!tsk_peer_msg(tsk, hdr)) {
  1188. trace_tipc_sk_drop_msg(sk, skb, TIPC_DUMP_NONE, "@proto_rcv!");
  1189. goto exit;
  1190. }
  1191. if (unlikely(msg_errcode(hdr))) {
  1192. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1193. tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
  1194. tsk_peer_port(tsk));
  1195. sk->sk_state_change(sk);
  1196. /* State change is ignored if socket already awake,
  1197. * - convert msg to abort msg and add to inqueue
  1198. */
  1199. msg_set_user(hdr, TIPC_CRITICAL_IMPORTANCE);
  1200. msg_set_type(hdr, TIPC_CONN_MSG);
  1201. msg_set_size(hdr, BASIC_H_SIZE);
  1202. msg_set_hdr_sz(hdr, BASIC_H_SIZE);
  1203. __skb_queue_tail(inputq, skb);
  1204. return;
  1205. }
  1206. tsk->probe_unacked = false;
  1207. if (mtyp == CONN_PROBE) {
  1208. msg_set_type(hdr, CONN_PROBE_REPLY);
  1209. if (tipc_msg_reverse(onode, &skb, TIPC_OK))
  1210. __skb_queue_tail(xmitq, skb);
  1211. return;
  1212. } else if (mtyp == CONN_ACK) {
  1213. was_cong = tsk_conn_cong(tsk);
  1214. tipc_sk_push_backlog(tsk, msg_nagle_ack(hdr));
  1215. tsk->snt_unacked -= msg_conn_ack(hdr);
  1216. if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
  1217. tsk->snd_win = msg_adv_win(hdr);
  1218. if (was_cong && !tsk_conn_cong(tsk))
  1219. sk->sk_write_space(sk);
  1220. } else if (mtyp != CONN_PROBE_REPLY) {
  1221. pr_warn("Received unknown CONN_PROTO msg\n");
  1222. }
  1223. exit:
  1224. kfree_skb(skb);
  1225. }
  1226. /**
  1227. * tipc_sendmsg - send message in connectionless manner
  1228. * @sock: socket structure
  1229. * @m: message to send
  1230. * @dsz: amount of user data to be sent
  1231. *
  1232. * Message must have an destination specified explicitly.
  1233. * Used for SOCK_RDM and SOCK_DGRAM messages,
  1234. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  1235. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  1236. *
  1237. * Return: the number of bytes sent on success, or errno otherwise
  1238. */
  1239. static int tipc_sendmsg(struct socket *sock,
  1240. struct msghdr *m, size_t dsz)
  1241. {
  1242. struct sock *sk = sock->sk;
  1243. int ret;
  1244. lock_sock(sk);
  1245. ret = __tipc_sendmsg(sock, m, dsz);
  1246. release_sock(sk);
  1247. return ret;
  1248. }
  1249. static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
  1250. {
  1251. struct sock *sk = sock->sk;
  1252. struct net *net = sock_net(sk);
  1253. struct tipc_sock *tsk = tipc_sk(sk);
  1254. struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
  1255. long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  1256. struct list_head *clinks = &tsk->cong_links;
  1257. bool syn = !tipc_sk_type_connectionless(sk);
  1258. struct tipc_group *grp = tsk->group;
  1259. struct tipc_msg *hdr = &tsk->phdr;
  1260. struct tipc_socket_addr skaddr;
  1261. struct sk_buff_head pkts;
  1262. int atype, mtu, rc;
  1263. if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
  1264. return -EMSGSIZE;
  1265. if (ua) {
  1266. if (!tipc_uaddr_valid(ua, m->msg_namelen))
  1267. return -EINVAL;
  1268. atype = ua->addrtype;
  1269. }
  1270. /* If socket belongs to a communication group follow other paths */
  1271. if (grp) {
  1272. if (!ua)
  1273. return tipc_send_group_bcast(sock, m, dlen, timeout);
  1274. if (atype == TIPC_SERVICE_ADDR)
  1275. return tipc_send_group_anycast(sock, m, dlen, timeout);
  1276. if (atype == TIPC_SOCKET_ADDR)
  1277. return tipc_send_group_unicast(sock, m, dlen, timeout);
  1278. if (atype == TIPC_SERVICE_RANGE)
  1279. return tipc_send_group_mcast(sock, m, dlen, timeout);
  1280. return -EINVAL;
  1281. }
  1282. if (!ua) {
  1283. ua = (struct tipc_uaddr *)&tsk->peer;
  1284. if (!syn && ua->family != AF_TIPC)
  1285. return -EDESTADDRREQ;
  1286. atype = ua->addrtype;
  1287. }
  1288. if (unlikely(syn)) {
  1289. if (sk->sk_state == TIPC_LISTEN)
  1290. return -EPIPE;
  1291. if (sk->sk_state != TIPC_OPEN)
  1292. return -EISCONN;
  1293. if (tsk->published)
  1294. return -EOPNOTSUPP;
  1295. if (atype == TIPC_SERVICE_ADDR)
  1296. tsk->conn_addrtype = atype;
  1297. msg_set_syn(hdr, 1);
  1298. }
  1299. memset(&skaddr, 0, sizeof(skaddr));
  1300. /* Determine destination */
  1301. if (atype == TIPC_SERVICE_RANGE) {
  1302. return tipc_sendmcast(sock, ua, m, dlen, timeout);
  1303. } else if (atype == TIPC_SERVICE_ADDR) {
  1304. skaddr.node = ua->lookup_node;
  1305. ua->scope = tipc_node2scope(skaddr.node);
  1306. if (!tipc_nametbl_lookup_anycast(net, ua, &skaddr))
  1307. return -EHOSTUNREACH;
  1308. } else if (atype == TIPC_SOCKET_ADDR) {
  1309. skaddr = ua->sk;
  1310. } else {
  1311. return -EINVAL;
  1312. }
  1313. /* Block or return if destination link is congested */
  1314. rc = tipc_wait_for_cond(sock, &timeout,
  1315. !tipc_dest_find(clinks, skaddr.node, 0));
  1316. if (unlikely(rc))
  1317. return rc;
  1318. /* Finally build message header */
  1319. msg_set_destnode(hdr, skaddr.node);
  1320. msg_set_destport(hdr, skaddr.ref);
  1321. if (atype == TIPC_SERVICE_ADDR) {
  1322. msg_set_type(hdr, TIPC_NAMED_MSG);
  1323. msg_set_hdr_sz(hdr, NAMED_H_SIZE);
  1324. msg_set_nametype(hdr, ua->sa.type);
  1325. msg_set_nameinst(hdr, ua->sa.instance);
  1326. msg_set_lookup_scope(hdr, ua->scope);
  1327. } else { /* TIPC_SOCKET_ADDR */
  1328. msg_set_type(hdr, TIPC_DIRECT_MSG);
  1329. msg_set_lookup_scope(hdr, 0);
  1330. msg_set_hdr_sz(hdr, BASIC_H_SIZE);
  1331. }
  1332. /* Add message body */
  1333. __skb_queue_head_init(&pkts);
  1334. mtu = tipc_node_get_mtu(net, skaddr.node, tsk->portid, true);
  1335. rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
  1336. if (unlikely(rc != dlen))
  1337. return rc;
  1338. if (unlikely(syn && !tipc_msg_skb_clone(&pkts, &sk->sk_write_queue))) {
  1339. __skb_queue_purge(&pkts);
  1340. return -ENOMEM;
  1341. }
  1342. /* Send message */
  1343. trace_tipc_sk_sendmsg(sk, skb_peek(&pkts), TIPC_DUMP_SK_SNDQ, " ");
  1344. rc = tipc_node_xmit(net, &pkts, skaddr.node, tsk->portid);
  1345. if (unlikely(rc == -ELINKCONG)) {
  1346. tipc_dest_push(clinks, skaddr.node, 0);
  1347. tsk->cong_link_cnt++;
  1348. rc = 0;
  1349. }
  1350. if (unlikely(syn && !rc)) {
  1351. tipc_set_sk_state(sk, TIPC_CONNECTING);
  1352. if (dlen && timeout) {
  1353. timeout = msecs_to_jiffies(timeout);
  1354. tipc_wait_for_connect(sock, &timeout);
  1355. }
  1356. }
  1357. return rc ? rc : dlen;
  1358. }
  1359. /**
  1360. * tipc_sendstream - send stream-oriented data
  1361. * @sock: socket structure
  1362. * @m: data to send
  1363. * @dsz: total length of data to be transmitted
  1364. *
  1365. * Used for SOCK_STREAM data.
  1366. *
  1367. * Return: the number of bytes sent on success (or partial success),
  1368. * or errno if no data sent
  1369. */
  1370. static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
  1371. {
  1372. struct sock *sk = sock->sk;
  1373. int ret;
  1374. lock_sock(sk);
  1375. ret = __tipc_sendstream(sock, m, dsz);
  1376. release_sock(sk);
  1377. return ret;
  1378. }
  1379. static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
  1380. {
  1381. struct sock *sk = sock->sk;
  1382. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  1383. long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  1384. struct sk_buff_head *txq = &sk->sk_write_queue;
  1385. struct tipc_sock *tsk = tipc_sk(sk);
  1386. struct tipc_msg *hdr = &tsk->phdr;
  1387. struct net *net = sock_net(sk);
  1388. struct sk_buff *skb;
  1389. u32 dnode = tsk_peer_node(tsk);
  1390. int maxnagle = tsk->maxnagle;
  1391. int maxpkt = tsk->max_pkt;
  1392. int send, sent = 0;
  1393. int blocks, rc = 0;
  1394. if (unlikely(dlen > INT_MAX))
  1395. return -EMSGSIZE;
  1396. /* Handle implicit connection setup */
  1397. if (unlikely(dest && sk->sk_state == TIPC_OPEN)) {
  1398. rc = __tipc_sendmsg(sock, m, dlen);
  1399. if (dlen && dlen == rc) {
  1400. tsk->peer_caps = tipc_node_get_capabilities(net, dnode);
  1401. tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
  1402. }
  1403. return rc;
  1404. }
  1405. do {
  1406. rc = tipc_wait_for_cond(sock, &timeout,
  1407. (!tsk->cong_link_cnt &&
  1408. !tsk_conn_cong(tsk) &&
  1409. tipc_sk_connected(sk)));
  1410. if (unlikely(rc))
  1411. break;
  1412. send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
  1413. blocks = tsk->snd_backlog;
  1414. if (tsk->oneway++ >= tsk->nagle_start && maxnagle &&
  1415. send <= maxnagle) {
  1416. rc = tipc_msg_append(hdr, m, send, maxnagle, txq);
  1417. if (unlikely(rc < 0))
  1418. break;
  1419. blocks += rc;
  1420. tsk->msg_acc++;
  1421. if (blocks <= 64 && tsk->expect_ack) {
  1422. tsk->snd_backlog = blocks;
  1423. sent += send;
  1424. break;
  1425. } else if (blocks > 64) {
  1426. tsk->pkt_cnt += skb_queue_len(txq);
  1427. } else {
  1428. skb = skb_peek_tail(txq);
  1429. if (skb) {
  1430. msg_set_ack_required(buf_msg(skb));
  1431. tsk->expect_ack = true;
  1432. } else {
  1433. tsk->expect_ack = false;
  1434. }
  1435. tsk->msg_acc = 0;
  1436. tsk->pkt_cnt = 0;
  1437. }
  1438. } else {
  1439. rc = tipc_msg_build(hdr, m, sent, send, maxpkt, txq);
  1440. if (unlikely(rc != send))
  1441. break;
  1442. blocks += tsk_inc(tsk, send + MIN_H_SIZE);
  1443. }
  1444. trace_tipc_sk_sendstream(sk, skb_peek(txq),
  1445. TIPC_DUMP_SK_SNDQ, " ");
  1446. rc = tipc_node_xmit(net, txq, dnode, tsk->portid);
  1447. if (unlikely(rc == -ELINKCONG)) {
  1448. tsk->cong_link_cnt = 1;
  1449. rc = 0;
  1450. }
  1451. if (likely(!rc)) {
  1452. tsk->snt_unacked += blocks;
  1453. tsk->snd_backlog = 0;
  1454. sent += send;
  1455. }
  1456. } while (sent < dlen && !rc);
  1457. return sent ? sent : rc;
  1458. }
  1459. /**
  1460. * tipc_send_packet - send a connection-oriented message
  1461. * @sock: socket structure
  1462. * @m: message to send
  1463. * @dsz: length of data to be transmitted
  1464. *
  1465. * Used for SOCK_SEQPACKET messages.
  1466. *
  1467. * Return: the number of bytes sent on success, or errno otherwise
  1468. */
  1469. static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
  1470. {
  1471. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  1472. return -EMSGSIZE;
  1473. return tipc_sendstream(sock, m, dsz);
  1474. }
  1475. /* tipc_sk_finish_conn - complete the setup of a connection
  1476. */
  1477. static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
  1478. u32 peer_node)
  1479. {
  1480. struct sock *sk = &tsk->sk;
  1481. struct net *net = sock_net(sk);
  1482. struct tipc_msg *msg = &tsk->phdr;
  1483. msg_set_syn(msg, 0);
  1484. msg_set_destnode(msg, peer_node);
  1485. msg_set_destport(msg, peer_port);
  1486. msg_set_type(msg, TIPC_CONN_MSG);
  1487. msg_set_lookup_scope(msg, 0);
  1488. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  1489. sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
  1490. tipc_set_sk_state(sk, TIPC_ESTABLISHED);
  1491. tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
  1492. tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid, true);
  1493. tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
  1494. tsk_set_nagle(tsk);
  1495. __skb_queue_purge(&sk->sk_write_queue);
  1496. if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
  1497. return;
  1498. /* Fall back to message based flow control */
  1499. tsk->rcv_win = FLOWCTL_MSG_WIN;
  1500. tsk->snd_win = FLOWCTL_MSG_WIN;
  1501. }
  1502. /**
  1503. * tipc_sk_set_orig_addr - capture sender's address for received message
  1504. * @m: descriptor for message info
  1505. * @skb: received message
  1506. *
  1507. * Note: Address is not captured if not requested by receiver.
  1508. */
  1509. static void tipc_sk_set_orig_addr(struct msghdr *m, struct sk_buff *skb)
  1510. {
  1511. DECLARE_SOCKADDR(struct sockaddr_pair *, srcaddr, m->msg_name);
  1512. struct tipc_msg *hdr = buf_msg(skb);
  1513. if (!srcaddr)
  1514. return;
  1515. srcaddr->sock.family = AF_TIPC;
  1516. srcaddr->sock.addrtype = TIPC_SOCKET_ADDR;
  1517. srcaddr->sock.scope = 0;
  1518. srcaddr->sock.addr.id.ref = msg_origport(hdr);
  1519. srcaddr->sock.addr.id.node = msg_orignode(hdr);
  1520. srcaddr->sock.addr.name.domain = 0;
  1521. m->msg_namelen = sizeof(struct sockaddr_tipc);
  1522. if (!msg_in_group(hdr))
  1523. return;
  1524. /* Group message users may also want to know sending member's id */
  1525. srcaddr->member.family = AF_TIPC;
  1526. srcaddr->member.addrtype = TIPC_SERVICE_ADDR;
  1527. srcaddr->member.scope = 0;
  1528. srcaddr->member.addr.name.name.type = msg_nametype(hdr);
  1529. srcaddr->member.addr.name.name.instance = TIPC_SKB_CB(skb)->orig_member;
  1530. srcaddr->member.addr.name.domain = 0;
  1531. m->msg_namelen = sizeof(*srcaddr);
  1532. }
  1533. /**
  1534. * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
  1535. * @m: descriptor for message info
  1536. * @skb: received message buffer
  1537. * @tsk: TIPC port associated with message
  1538. *
  1539. * Note: Ancillary data is not captured if not requested by receiver.
  1540. *
  1541. * Return: 0 if successful, otherwise errno
  1542. */
  1543. static int tipc_sk_anc_data_recv(struct msghdr *m, struct sk_buff *skb,
  1544. struct tipc_sock *tsk)
  1545. {
  1546. struct tipc_msg *hdr;
  1547. u32 data[3] = {0,};
  1548. bool has_addr;
  1549. int dlen, rc;
  1550. if (likely(m->msg_controllen == 0))
  1551. return 0;
  1552. hdr = buf_msg(skb);
  1553. dlen = msg_data_sz(hdr);
  1554. /* Capture errored message object, if any */
  1555. if (msg_errcode(hdr)) {
  1556. if (skb_linearize(skb))
  1557. return -ENOMEM;
  1558. hdr = buf_msg(skb);
  1559. data[0] = msg_errcode(hdr);
  1560. data[1] = dlen;
  1561. rc = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, data);
  1562. if (rc || !dlen)
  1563. return rc;
  1564. rc = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, dlen, msg_data(hdr));
  1565. if (rc)
  1566. return rc;
  1567. }
  1568. /* Capture TIPC_SERVICE_ADDR/RANGE destination address, if any */
  1569. switch (msg_type(hdr)) {
  1570. case TIPC_NAMED_MSG:
  1571. has_addr = true;
  1572. data[0] = msg_nametype(hdr);
  1573. data[1] = msg_namelower(hdr);
  1574. data[2] = data[1];
  1575. break;
  1576. case TIPC_MCAST_MSG:
  1577. has_addr = true;
  1578. data[0] = msg_nametype(hdr);
  1579. data[1] = msg_namelower(hdr);
  1580. data[2] = msg_nameupper(hdr);
  1581. break;
  1582. case TIPC_CONN_MSG:
  1583. has_addr = !!tsk->conn_addrtype;
  1584. data[0] = msg_nametype(&tsk->phdr);
  1585. data[1] = msg_nameinst(&tsk->phdr);
  1586. data[2] = data[1];
  1587. break;
  1588. default:
  1589. has_addr = false;
  1590. }
  1591. if (!has_addr)
  1592. return 0;
  1593. return put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, data);
  1594. }
  1595. static struct sk_buff *tipc_sk_build_ack(struct tipc_sock *tsk)
  1596. {
  1597. struct sock *sk = &tsk->sk;
  1598. struct sk_buff *skb = NULL;
  1599. struct tipc_msg *msg;
  1600. u32 peer_port = tsk_peer_port(tsk);
  1601. u32 dnode = tsk_peer_node(tsk);
  1602. if (!tipc_sk_connected(sk))
  1603. return NULL;
  1604. skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
  1605. dnode, tsk_own_node(tsk), peer_port,
  1606. tsk->portid, TIPC_OK);
  1607. if (!skb)
  1608. return NULL;
  1609. msg = buf_msg(skb);
  1610. msg_set_conn_ack(msg, tsk->rcv_unacked);
  1611. tsk->rcv_unacked = 0;
  1612. /* Adjust to and advertize the correct window limit */
  1613. if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
  1614. tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
  1615. msg_set_adv_win(msg, tsk->rcv_win);
  1616. }
  1617. return skb;
  1618. }
  1619. static void tipc_sk_send_ack(struct tipc_sock *tsk)
  1620. {
  1621. struct sk_buff *skb;
  1622. skb = tipc_sk_build_ack(tsk);
  1623. if (!skb)
  1624. return;
  1625. tipc_node_xmit_skb(sock_net(&tsk->sk), skb, tsk_peer_node(tsk),
  1626. msg_link_selector(buf_msg(skb)));
  1627. }
  1628. static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
  1629. {
  1630. struct sock *sk = sock->sk;
  1631. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  1632. long timeo = *timeop;
  1633. int err = sock_error(sk);
  1634. if (err)
  1635. return err;
  1636. for (;;) {
  1637. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1638. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  1639. err = -ENOTCONN;
  1640. break;
  1641. }
  1642. add_wait_queue(sk_sleep(sk), &wait);
  1643. release_sock(sk);
  1644. timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
  1645. sched_annotate_sleep();
  1646. lock_sock(sk);
  1647. remove_wait_queue(sk_sleep(sk), &wait);
  1648. }
  1649. err = 0;
  1650. if (!skb_queue_empty(&sk->sk_receive_queue))
  1651. break;
  1652. err = -EAGAIN;
  1653. if (!timeo)
  1654. break;
  1655. err = sock_intr_errno(timeo);
  1656. if (signal_pending(current))
  1657. break;
  1658. err = sock_error(sk);
  1659. if (err)
  1660. break;
  1661. }
  1662. *timeop = timeo;
  1663. return err;
  1664. }
  1665. /**
  1666. * tipc_recvmsg - receive packet-oriented message
  1667. * @sock: network socket
  1668. * @m: descriptor for message info
  1669. * @buflen: length of user buffer area
  1670. * @flags: receive flags
  1671. *
  1672. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  1673. * If the complete message doesn't fit in user area, truncate it.
  1674. *
  1675. * Return: size of returned message data, errno otherwise
  1676. */
  1677. static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
  1678. size_t buflen, int flags)
  1679. {
  1680. struct sock *sk = sock->sk;
  1681. bool connected = !tipc_sk_type_connectionless(sk);
  1682. struct tipc_sock *tsk = tipc_sk(sk);
  1683. int rc, err, hlen, dlen, copy;
  1684. struct tipc_skb_cb *skb_cb;
  1685. struct sk_buff_head xmitq;
  1686. struct tipc_msg *hdr;
  1687. struct sk_buff *skb;
  1688. bool grp_evt;
  1689. long timeout;
  1690. /* Catch invalid receive requests */
  1691. if (unlikely(!buflen))
  1692. return -EINVAL;
  1693. lock_sock(sk);
  1694. if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
  1695. rc = -ENOTCONN;
  1696. goto exit;
  1697. }
  1698. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1699. /* Step rcv queue to first msg with data or error; wait if necessary */
  1700. do {
  1701. rc = tipc_wait_for_rcvmsg(sock, &timeout);
  1702. if (unlikely(rc))
  1703. goto exit;
  1704. skb = skb_peek(&sk->sk_receive_queue);
  1705. skb_cb = TIPC_SKB_CB(skb);
  1706. hdr = buf_msg(skb);
  1707. dlen = msg_data_sz(hdr);
  1708. hlen = msg_hdr_sz(hdr);
  1709. err = msg_errcode(hdr);
  1710. grp_evt = msg_is_grp_evt(hdr);
  1711. if (likely(dlen || err))
  1712. break;
  1713. tsk_advance_rx_queue(sk);
  1714. } while (1);
  1715. /* Collect msg meta data, including error code and rejected data */
  1716. tipc_sk_set_orig_addr(m, skb);
  1717. rc = tipc_sk_anc_data_recv(m, skb, tsk);
  1718. if (unlikely(rc))
  1719. goto exit;
  1720. hdr = buf_msg(skb);
  1721. /* Capture data if non-error msg, otherwise just set return value */
  1722. if (likely(!err)) {
  1723. int offset = skb_cb->bytes_read;
  1724. copy = min_t(int, dlen - offset, buflen);
  1725. rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
  1726. if (unlikely(rc))
  1727. goto exit;
  1728. if (unlikely(offset + copy < dlen)) {
  1729. if (flags & MSG_EOR) {
  1730. if (!(flags & MSG_PEEK))
  1731. skb_cb->bytes_read = offset + copy;
  1732. } else {
  1733. m->msg_flags |= MSG_TRUNC;
  1734. skb_cb->bytes_read = 0;
  1735. }
  1736. } else {
  1737. if (flags & MSG_EOR)
  1738. m->msg_flags |= MSG_EOR;
  1739. skb_cb->bytes_read = 0;
  1740. }
  1741. } else {
  1742. copy = 0;
  1743. rc = 0;
  1744. if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control) {
  1745. rc = -ECONNRESET;
  1746. goto exit;
  1747. }
  1748. }
  1749. /* Mark message as group event if applicable */
  1750. if (unlikely(grp_evt)) {
  1751. if (msg_grp_evt(hdr) == TIPC_WITHDRAWN)
  1752. m->msg_flags |= MSG_EOR;
  1753. m->msg_flags |= MSG_OOB;
  1754. copy = 0;
  1755. }
  1756. /* Caption of data or error code/rejected data was successful */
  1757. if (unlikely(flags & MSG_PEEK))
  1758. goto exit;
  1759. /* Send group flow control advertisement when applicable */
  1760. if (tsk->group && msg_in_group(hdr) && !grp_evt) {
  1761. __skb_queue_head_init(&xmitq);
  1762. tipc_group_update_rcv_win(tsk->group, tsk_blocks(hlen + dlen),
  1763. msg_orignode(hdr), msg_origport(hdr),
  1764. &xmitq);
  1765. tipc_node_distr_xmit(sock_net(sk), &xmitq);
  1766. }
  1767. if (skb_cb->bytes_read)
  1768. goto exit;
  1769. tsk_advance_rx_queue(sk);
  1770. if (likely(!connected))
  1771. goto exit;
  1772. /* Send connection flow control advertisement when applicable */
  1773. tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
  1774. if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
  1775. tipc_sk_send_ack(tsk);
  1776. exit:
  1777. release_sock(sk);
  1778. return rc ? rc : copy;
  1779. }
  1780. /**
  1781. * tipc_recvstream - receive stream-oriented data
  1782. * @sock: network socket
  1783. * @m: descriptor for message info
  1784. * @buflen: total size of user buffer area
  1785. * @flags: receive flags
  1786. *
  1787. * Used for SOCK_STREAM messages only. If not enough data is available
  1788. * will optionally wait for more; never truncates data.
  1789. *
  1790. * Return: size of returned message data, errno otherwise
  1791. */
  1792. static int tipc_recvstream(struct socket *sock, struct msghdr *m,
  1793. size_t buflen, int flags)
  1794. {
  1795. struct sock *sk = sock->sk;
  1796. struct tipc_sock *tsk = tipc_sk(sk);
  1797. struct sk_buff *skb;
  1798. struct tipc_msg *hdr;
  1799. struct tipc_skb_cb *skb_cb;
  1800. bool peek = flags & MSG_PEEK;
  1801. int offset, required, copy, copied = 0;
  1802. int hlen, dlen, err, rc;
  1803. long timeout;
  1804. /* Catch invalid receive attempts */
  1805. if (unlikely(!buflen))
  1806. return -EINVAL;
  1807. lock_sock(sk);
  1808. if (unlikely(sk->sk_state == TIPC_OPEN)) {
  1809. rc = -ENOTCONN;
  1810. goto exit;
  1811. }
  1812. required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
  1813. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1814. do {
  1815. /* Look at first msg in receive queue; wait if necessary */
  1816. rc = tipc_wait_for_rcvmsg(sock, &timeout);
  1817. if (unlikely(rc))
  1818. break;
  1819. skb = skb_peek(&sk->sk_receive_queue);
  1820. skb_cb = TIPC_SKB_CB(skb);
  1821. hdr = buf_msg(skb);
  1822. dlen = msg_data_sz(hdr);
  1823. hlen = msg_hdr_sz(hdr);
  1824. err = msg_errcode(hdr);
  1825. /* Discard any empty non-errored (SYN-) message */
  1826. if (unlikely(!dlen && !err)) {
  1827. tsk_advance_rx_queue(sk);
  1828. continue;
  1829. }
  1830. /* Collect msg meta data, incl. error code and rejected data */
  1831. if (!copied) {
  1832. tipc_sk_set_orig_addr(m, skb);
  1833. rc = tipc_sk_anc_data_recv(m, skb, tsk);
  1834. if (rc)
  1835. break;
  1836. hdr = buf_msg(skb);
  1837. }
  1838. /* Copy data if msg ok, otherwise return error/partial data */
  1839. if (likely(!err)) {
  1840. offset = skb_cb->bytes_read;
  1841. copy = min_t(int, dlen - offset, buflen - copied);
  1842. rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
  1843. if (unlikely(rc))
  1844. break;
  1845. copied += copy;
  1846. offset += copy;
  1847. if (unlikely(offset < dlen)) {
  1848. if (!peek)
  1849. skb_cb->bytes_read = offset;
  1850. break;
  1851. }
  1852. } else {
  1853. rc = 0;
  1854. if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
  1855. rc = -ECONNRESET;
  1856. if (copied || rc)
  1857. break;
  1858. }
  1859. if (unlikely(peek))
  1860. break;
  1861. tsk_advance_rx_queue(sk);
  1862. /* Send connection flow control advertisement when applicable */
  1863. tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
  1864. if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
  1865. tipc_sk_send_ack(tsk);
  1866. /* Exit if all requested data or FIN/error received */
  1867. if (copied == buflen || err)
  1868. break;
  1869. } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
  1870. exit:
  1871. release_sock(sk);
  1872. return copied ? copied : rc;
  1873. }
  1874. /**
  1875. * tipc_write_space - wake up thread if port congestion is released
  1876. * @sk: socket
  1877. */
  1878. static void tipc_write_space(struct sock *sk)
  1879. {
  1880. struct socket_wq *wq;
  1881. rcu_read_lock();
  1882. wq = rcu_dereference(sk->sk_wq);
  1883. if (skwq_has_sleeper(wq))
  1884. wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
  1885. EPOLLWRNORM | EPOLLWRBAND);
  1886. rcu_read_unlock();
  1887. }
  1888. /**
  1889. * tipc_data_ready - wake up threads to indicate messages have been received
  1890. * @sk: socket
  1891. */
  1892. static void tipc_data_ready(struct sock *sk)
  1893. {
  1894. struct socket_wq *wq;
  1895. rcu_read_lock();
  1896. wq = rcu_dereference(sk->sk_wq);
  1897. if (skwq_has_sleeper(wq))
  1898. wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
  1899. EPOLLRDNORM | EPOLLRDBAND);
  1900. rcu_read_unlock();
  1901. }
  1902. static void tipc_sock_destruct(struct sock *sk)
  1903. {
  1904. __skb_queue_purge(&sk->sk_receive_queue);
  1905. }
  1906. static void tipc_sk_proto_rcv(struct sock *sk,
  1907. struct sk_buff_head *inputq,
  1908. struct sk_buff_head *xmitq)
  1909. {
  1910. struct sk_buff *skb = __skb_dequeue(inputq);
  1911. struct tipc_sock *tsk = tipc_sk(sk);
  1912. struct tipc_msg *hdr = buf_msg(skb);
  1913. struct tipc_group *grp = tsk->group;
  1914. bool wakeup = false;
  1915. switch (msg_user(hdr)) {
  1916. case CONN_MANAGER:
  1917. tipc_sk_conn_proto_rcv(tsk, skb, inputq, xmitq);
  1918. return;
  1919. case SOCK_WAKEUP:
  1920. tipc_dest_del(&tsk->cong_links, msg_orignode(hdr), 0);
  1921. /* coupled with smp_rmb() in tipc_wait_for_cond() */
  1922. smp_wmb();
  1923. tsk->cong_link_cnt--;
  1924. wakeup = true;
  1925. tipc_sk_push_backlog(tsk, false);
  1926. break;
  1927. case GROUP_PROTOCOL:
  1928. tipc_group_proto_rcv(grp, &wakeup, hdr, inputq, xmitq);
  1929. break;
  1930. case TOP_SRV:
  1931. tipc_group_member_evt(tsk->group, &wakeup, &sk->sk_rcvbuf,
  1932. hdr, inputq, xmitq);
  1933. break;
  1934. default:
  1935. break;
  1936. }
  1937. if (wakeup)
  1938. sk->sk_write_space(sk);
  1939. kfree_skb(skb);
  1940. }
  1941. /**
  1942. * tipc_sk_filter_connect - check incoming message for a connection-based socket
  1943. * @tsk: TIPC socket
  1944. * @skb: pointer to message buffer.
  1945. * @xmitq: for Nagle ACK if any
  1946. * Return: true if message should be added to receive queue, false otherwise
  1947. */
  1948. static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb,
  1949. struct sk_buff_head *xmitq)
  1950. {
  1951. struct sock *sk = &tsk->sk;
  1952. struct net *net = sock_net(sk);
  1953. struct tipc_msg *hdr = buf_msg(skb);
  1954. bool con_msg = msg_connected(hdr);
  1955. u32 pport = tsk_peer_port(tsk);
  1956. u32 pnode = tsk_peer_node(tsk);
  1957. u32 oport = msg_origport(hdr);
  1958. u32 onode = msg_orignode(hdr);
  1959. int err = msg_errcode(hdr);
  1960. unsigned long delay;
  1961. if (unlikely(msg_mcast(hdr)))
  1962. return false;
  1963. tsk->oneway = 0;
  1964. switch (sk->sk_state) {
  1965. case TIPC_CONNECTING:
  1966. /* Setup ACK */
  1967. if (likely(con_msg)) {
  1968. if (err)
  1969. break;
  1970. tipc_sk_finish_conn(tsk, oport, onode);
  1971. msg_set_importance(&tsk->phdr, msg_importance(hdr));
  1972. /* ACK+ message with data is added to receive queue */
  1973. if (msg_data_sz(hdr))
  1974. return true;
  1975. /* Empty ACK-, - wake up sleeping connect() and drop */
  1976. sk->sk_state_change(sk);
  1977. msg_set_dest_droppable(hdr, 1);
  1978. return false;
  1979. }
  1980. /* Ignore connectionless message if not from listening socket */
  1981. if (oport != pport || onode != pnode)
  1982. return false;
  1983. /* Rejected SYN */
  1984. if (err != TIPC_ERR_OVERLOAD)
  1985. break;
  1986. /* Prepare for new setup attempt if we have a SYN clone */
  1987. if (skb_queue_empty(&sk->sk_write_queue))
  1988. break;
  1989. get_random_bytes(&delay, 2);
  1990. delay %= (tsk->conn_timeout / 4);
  1991. delay = msecs_to_jiffies(delay + 100);
  1992. sk_reset_timer(sk, &sk->sk_timer, jiffies + delay);
  1993. return false;
  1994. case TIPC_OPEN:
  1995. case TIPC_DISCONNECTING:
  1996. return false;
  1997. case TIPC_LISTEN:
  1998. /* Accept only SYN message */
  1999. if (!msg_is_syn(hdr) &&
  2000. tipc_node_get_capabilities(net, onode) & TIPC_SYN_BIT)
  2001. return false;
  2002. if (!con_msg && !err)
  2003. return true;
  2004. return false;
  2005. case TIPC_ESTABLISHED:
  2006. if (!skb_queue_empty(&sk->sk_write_queue))
  2007. tipc_sk_push_backlog(tsk, false);
  2008. /* Accept only connection-based messages sent by peer */
  2009. if (likely(con_msg && !err && pport == oport &&
  2010. pnode == onode)) {
  2011. if (msg_ack_required(hdr)) {
  2012. struct sk_buff *skb;
  2013. skb = tipc_sk_build_ack(tsk);
  2014. if (skb) {
  2015. msg_set_nagle_ack(buf_msg(skb));
  2016. __skb_queue_tail(xmitq, skb);
  2017. }
  2018. }
  2019. return true;
  2020. }
  2021. if (!tsk_peer_msg(tsk, hdr))
  2022. return false;
  2023. if (!err)
  2024. return true;
  2025. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  2026. tipc_node_remove_conn(net, pnode, tsk->portid);
  2027. sk->sk_state_change(sk);
  2028. return true;
  2029. default:
  2030. pr_err("Unknown sk_state %u\n", sk->sk_state);
  2031. }
  2032. /* Abort connection setup attempt */
  2033. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  2034. sk->sk_err = ECONNREFUSED;
  2035. sk->sk_state_change(sk);
  2036. return true;
  2037. }
  2038. /**
  2039. * rcvbuf_limit - get proper overload limit of socket receive queue
  2040. * @sk: socket
  2041. * @skb: message
  2042. *
  2043. * For connection oriented messages, irrespective of importance,
  2044. * default queue limit is 2 MB.
  2045. *
  2046. * For connectionless messages, queue limits are based on message
  2047. * importance as follows:
  2048. *
  2049. * TIPC_LOW_IMPORTANCE (2 MB)
  2050. * TIPC_MEDIUM_IMPORTANCE (4 MB)
  2051. * TIPC_HIGH_IMPORTANCE (8 MB)
  2052. * TIPC_CRITICAL_IMPORTANCE (16 MB)
  2053. *
  2054. * Return: overload limit according to corresponding message importance
  2055. */
  2056. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
  2057. {
  2058. struct tipc_sock *tsk = tipc_sk(sk);
  2059. struct tipc_msg *hdr = buf_msg(skb);
  2060. if (unlikely(msg_in_group(hdr)))
  2061. return READ_ONCE(sk->sk_rcvbuf);
  2062. if (unlikely(!msg_connected(hdr)))
  2063. return READ_ONCE(sk->sk_rcvbuf) << msg_importance(hdr);
  2064. if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
  2065. return READ_ONCE(sk->sk_rcvbuf);
  2066. return FLOWCTL_MSG_LIM;
  2067. }
  2068. /**
  2069. * tipc_sk_filter_rcv - validate incoming message
  2070. * @sk: socket
  2071. * @skb: pointer to message.
  2072. * @xmitq: output message area (FIXME)
  2073. *
  2074. * Enqueues message on receive queue if acceptable; optionally handles
  2075. * disconnect indication for a connected socket.
  2076. *
  2077. * Called with socket lock already taken
  2078. */
  2079. static void tipc_sk_filter_rcv(struct sock *sk, struct sk_buff *skb,
  2080. struct sk_buff_head *xmitq)
  2081. {
  2082. bool sk_conn = !tipc_sk_type_connectionless(sk);
  2083. struct tipc_sock *tsk = tipc_sk(sk);
  2084. struct tipc_group *grp = tsk->group;
  2085. struct tipc_msg *hdr = buf_msg(skb);
  2086. struct net *net = sock_net(sk);
  2087. struct sk_buff_head inputq;
  2088. int mtyp = msg_type(hdr);
  2089. int limit, err = TIPC_OK;
  2090. trace_tipc_sk_filter_rcv(sk, skb, TIPC_DUMP_ALL, " ");
  2091. TIPC_SKB_CB(skb)->bytes_read = 0;
  2092. __skb_queue_head_init(&inputq);
  2093. __skb_queue_tail(&inputq, skb);
  2094. if (unlikely(!msg_isdata(hdr)))
  2095. tipc_sk_proto_rcv(sk, &inputq, xmitq);
  2096. if (unlikely(grp))
  2097. tipc_group_filter_msg(grp, &inputq, xmitq);
  2098. if (unlikely(!grp) && mtyp == TIPC_MCAST_MSG)
  2099. tipc_mcast_filter_msg(net, &tsk->mc_method.deferredq, &inputq);
  2100. /* Validate and add to receive buffer if there is space */
  2101. while ((skb = __skb_dequeue(&inputq))) {
  2102. hdr = buf_msg(skb);
  2103. limit = rcvbuf_limit(sk, skb);
  2104. if ((sk_conn && !tipc_sk_filter_connect(tsk, skb, xmitq)) ||
  2105. (!sk_conn && msg_connected(hdr)) ||
  2106. (!grp && msg_in_group(hdr)))
  2107. err = TIPC_ERR_NO_PORT;
  2108. else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit) {
  2109. trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL,
  2110. "err_overload2!");
  2111. atomic_inc(&sk->sk_drops);
  2112. err = TIPC_ERR_OVERLOAD;
  2113. }
  2114. if (unlikely(err)) {
  2115. if (tipc_msg_reverse(tipc_own_addr(net), &skb, err)) {
  2116. trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE,
  2117. "@filter_rcv!");
  2118. __skb_queue_tail(xmitq, skb);
  2119. }
  2120. err = TIPC_OK;
  2121. continue;
  2122. }
  2123. __skb_queue_tail(&sk->sk_receive_queue, skb);
  2124. skb_set_owner_r(skb, sk);
  2125. trace_tipc_sk_overlimit2(sk, skb, TIPC_DUMP_ALL,
  2126. "rcvq >90% allocated!");
  2127. sk->sk_data_ready(sk);
  2128. }
  2129. }
  2130. /**
  2131. * tipc_sk_backlog_rcv - handle incoming message from backlog queue
  2132. * @sk: socket
  2133. * @skb: message
  2134. *
  2135. * Caller must hold socket lock
  2136. */
  2137. static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  2138. {
  2139. unsigned int before = sk_rmem_alloc_get(sk);
  2140. struct sk_buff_head xmitq;
  2141. unsigned int added;
  2142. __skb_queue_head_init(&xmitq);
  2143. tipc_sk_filter_rcv(sk, skb, &xmitq);
  2144. added = sk_rmem_alloc_get(sk) - before;
  2145. atomic_add(added, &tipc_sk(sk)->dupl_rcvcnt);
  2146. /* Send pending response/rejected messages, if any */
  2147. tipc_node_distr_xmit(sock_net(sk), &xmitq);
  2148. return 0;
  2149. }
  2150. /**
  2151. * tipc_sk_enqueue - extract all buffers with destination 'dport' from
  2152. * inputq and try adding them to socket or backlog queue
  2153. * @inputq: list of incoming buffers with potentially different destinations
  2154. * @sk: socket where the buffers should be enqueued
  2155. * @dport: port number for the socket
  2156. * @xmitq: output queue
  2157. *
  2158. * Caller must hold socket lock
  2159. */
  2160. static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
  2161. u32 dport, struct sk_buff_head *xmitq)
  2162. {
  2163. unsigned long time_limit = jiffies + usecs_to_jiffies(20000);
  2164. struct sk_buff *skb;
  2165. unsigned int lim;
  2166. atomic_t *dcnt;
  2167. u32 onode;
  2168. while (skb_queue_len(inputq)) {
  2169. if (unlikely(time_after_eq(jiffies, time_limit)))
  2170. return;
  2171. skb = tipc_skb_dequeue(inputq, dport);
  2172. if (unlikely(!skb))
  2173. return;
  2174. /* Add message directly to receive queue if possible */
  2175. if (!sock_owned_by_user(sk)) {
  2176. tipc_sk_filter_rcv(sk, skb, xmitq);
  2177. continue;
  2178. }
  2179. /* Try backlog, compensating for double-counted bytes */
  2180. dcnt = &tipc_sk(sk)->dupl_rcvcnt;
  2181. if (!sk->sk_backlog.len)
  2182. atomic_set(dcnt, 0);
  2183. lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
  2184. if (likely(!sk_add_backlog(sk, skb, lim))) {
  2185. trace_tipc_sk_overlimit1(sk, skb, TIPC_DUMP_ALL,
  2186. "bklg & rcvq >90% allocated!");
  2187. continue;
  2188. }
  2189. trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL, "err_overload!");
  2190. /* Overload => reject message back to sender */
  2191. onode = tipc_own_addr(sock_net(sk));
  2192. atomic_inc(&sk->sk_drops);
  2193. if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD)) {
  2194. trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_ALL,
  2195. "@sk_enqueue!");
  2196. __skb_queue_tail(xmitq, skb);
  2197. }
  2198. break;
  2199. }
  2200. }
  2201. /**
  2202. * tipc_sk_rcv - handle a chain of incoming buffers
  2203. * @net: the associated network namespace
  2204. * @inputq: buffer list containing the buffers
  2205. * Consumes all buffers in list until inputq is empty
  2206. * Note: may be called in multiple threads referring to the same queue
  2207. */
  2208. void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
  2209. {
  2210. struct sk_buff_head xmitq;
  2211. u32 dnode, dport = 0;
  2212. int err;
  2213. struct tipc_sock *tsk;
  2214. struct sock *sk;
  2215. struct sk_buff *skb;
  2216. __skb_queue_head_init(&xmitq);
  2217. while (skb_queue_len(inputq)) {
  2218. dport = tipc_skb_peek_port(inputq, dport);
  2219. tsk = tipc_sk_lookup(net, dport);
  2220. if (likely(tsk)) {
  2221. sk = &tsk->sk;
  2222. if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
  2223. tipc_sk_enqueue(inputq, sk, dport, &xmitq);
  2224. spin_unlock_bh(&sk->sk_lock.slock);
  2225. }
  2226. /* Send pending response/rejected messages, if any */
  2227. tipc_node_distr_xmit(sock_net(sk), &xmitq);
  2228. sock_put(sk);
  2229. continue;
  2230. }
  2231. /* No destination socket => dequeue skb if still there */
  2232. skb = tipc_skb_dequeue(inputq, dport);
  2233. if (!skb)
  2234. return;
  2235. /* Try secondary lookup if unresolved named message */
  2236. err = TIPC_ERR_NO_PORT;
  2237. if (tipc_msg_lookup_dest(net, skb, &err))
  2238. goto xmit;
  2239. /* Prepare for message rejection */
  2240. if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
  2241. continue;
  2242. trace_tipc_sk_rej_msg(NULL, skb, TIPC_DUMP_NONE, "@sk_rcv!");
  2243. xmit:
  2244. dnode = msg_destnode(buf_msg(skb));
  2245. tipc_node_xmit_skb(net, skb, dnode, dport);
  2246. }
  2247. }
  2248. static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
  2249. {
  2250. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  2251. struct sock *sk = sock->sk;
  2252. int done;
  2253. do {
  2254. int err = sock_error(sk);
  2255. if (err)
  2256. return err;
  2257. if (!*timeo_p)
  2258. return -ETIMEDOUT;
  2259. if (signal_pending(current))
  2260. return sock_intr_errno(*timeo_p);
  2261. if (sk->sk_state == TIPC_DISCONNECTING)
  2262. break;
  2263. add_wait_queue(sk_sleep(sk), &wait);
  2264. done = sk_wait_event(sk, timeo_p, tipc_sk_connected(sk),
  2265. &wait);
  2266. remove_wait_queue(sk_sleep(sk), &wait);
  2267. } while (!done);
  2268. return 0;
  2269. }
  2270. static bool tipc_sockaddr_is_sane(struct sockaddr_tipc *addr)
  2271. {
  2272. if (addr->family != AF_TIPC)
  2273. return false;
  2274. if (addr->addrtype == TIPC_SERVICE_RANGE)
  2275. return (addr->addr.nameseq.lower <= addr->addr.nameseq.upper);
  2276. return (addr->addrtype == TIPC_SERVICE_ADDR ||
  2277. addr->addrtype == TIPC_SOCKET_ADDR);
  2278. }
  2279. /**
  2280. * tipc_connect - establish a connection to another TIPC port
  2281. * @sock: socket structure
  2282. * @dest: socket address for destination port
  2283. * @destlen: size of socket address data structure
  2284. * @flags: file-related flags associated with socket
  2285. *
  2286. * Return: 0 on success, errno otherwise
  2287. */
  2288. static int tipc_connect(struct socket *sock, struct sockaddr *dest,
  2289. int destlen, int flags)
  2290. {
  2291. struct sock *sk = sock->sk;
  2292. struct tipc_sock *tsk = tipc_sk(sk);
  2293. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  2294. struct msghdr m = {NULL,};
  2295. long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
  2296. int previous;
  2297. int res = 0;
  2298. if (destlen != sizeof(struct sockaddr_tipc))
  2299. return -EINVAL;
  2300. lock_sock(sk);
  2301. if (tsk->group) {
  2302. res = -EINVAL;
  2303. goto exit;
  2304. }
  2305. if (dst->family == AF_UNSPEC) {
  2306. memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
  2307. if (!tipc_sk_type_connectionless(sk))
  2308. res = -EINVAL;
  2309. goto exit;
  2310. }
  2311. if (!tipc_sockaddr_is_sane(dst)) {
  2312. res = -EINVAL;
  2313. goto exit;
  2314. }
  2315. /* DGRAM/RDM connect(), just save the destaddr */
  2316. if (tipc_sk_type_connectionless(sk)) {
  2317. memcpy(&tsk->peer, dest, destlen);
  2318. goto exit;
  2319. } else if (dst->addrtype == TIPC_SERVICE_RANGE) {
  2320. res = -EINVAL;
  2321. goto exit;
  2322. }
  2323. previous = sk->sk_state;
  2324. switch (sk->sk_state) {
  2325. case TIPC_OPEN:
  2326. /* Send a 'SYN-' to destination */
  2327. m.msg_name = dest;
  2328. m.msg_namelen = destlen;
  2329. iov_iter_kvec(&m.msg_iter, ITER_SOURCE, NULL, 0, 0);
  2330. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  2331. * indicate send_msg() is never blocked.
  2332. */
  2333. if (!timeout)
  2334. m.msg_flags = MSG_DONTWAIT;
  2335. res = __tipc_sendmsg(sock, &m, 0);
  2336. if ((res < 0) && (res != -EWOULDBLOCK))
  2337. goto exit;
  2338. /* Just entered TIPC_CONNECTING state; the only
  2339. * difference is that return value in non-blocking
  2340. * case is EINPROGRESS, rather than EALREADY.
  2341. */
  2342. res = -EINPROGRESS;
  2343. fallthrough;
  2344. case TIPC_CONNECTING:
  2345. if (!timeout) {
  2346. if (previous == TIPC_CONNECTING)
  2347. res = -EALREADY;
  2348. goto exit;
  2349. }
  2350. timeout = msecs_to_jiffies(timeout);
  2351. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  2352. res = tipc_wait_for_connect(sock, &timeout);
  2353. break;
  2354. case TIPC_ESTABLISHED:
  2355. res = -EISCONN;
  2356. break;
  2357. default:
  2358. res = -EINVAL;
  2359. }
  2360. exit:
  2361. release_sock(sk);
  2362. return res;
  2363. }
  2364. /**
  2365. * tipc_listen - allow socket to listen for incoming connections
  2366. * @sock: socket structure
  2367. * @len: (unused)
  2368. *
  2369. * Return: 0 on success, errno otherwise
  2370. */
  2371. static int tipc_listen(struct socket *sock, int len)
  2372. {
  2373. struct sock *sk = sock->sk;
  2374. int res;
  2375. lock_sock(sk);
  2376. res = tipc_set_sk_state(sk, TIPC_LISTEN);
  2377. release_sock(sk);
  2378. return res;
  2379. }
  2380. static int tipc_wait_for_accept(struct socket *sock, long timeo)
  2381. {
  2382. struct sock *sk = sock->sk;
  2383. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  2384. int err;
  2385. /* True wake-one mechanism for incoming connections: only
  2386. * one process gets woken up, not the 'whole herd'.
  2387. * Since we do not 'race & poll' for established sockets
  2388. * anymore, the common case will execute the loop only once.
  2389. */
  2390. for (;;) {
  2391. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  2392. add_wait_queue(sk_sleep(sk), &wait);
  2393. release_sock(sk);
  2394. timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
  2395. lock_sock(sk);
  2396. remove_wait_queue(sk_sleep(sk), &wait);
  2397. }
  2398. err = 0;
  2399. if (!skb_queue_empty(&sk->sk_receive_queue))
  2400. break;
  2401. err = -EAGAIN;
  2402. if (!timeo)
  2403. break;
  2404. err = sock_intr_errno(timeo);
  2405. if (signal_pending(current))
  2406. break;
  2407. }
  2408. return err;
  2409. }
  2410. /**
  2411. * tipc_accept - wait for connection request
  2412. * @sock: listening socket
  2413. * @new_sock: new socket that is to be connected
  2414. * @flags: file-related flags associated with socket
  2415. * @kern: caused by kernel or by userspace?
  2416. *
  2417. * Return: 0 on success, errno otherwise
  2418. */
  2419. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
  2420. bool kern)
  2421. {
  2422. struct sock *new_sk, *sk = sock->sk;
  2423. struct tipc_sock *new_tsock;
  2424. struct msghdr m = {NULL,};
  2425. struct tipc_msg *msg;
  2426. struct sk_buff *buf;
  2427. long timeo;
  2428. int res;
  2429. lock_sock(sk);
  2430. if (sk->sk_state != TIPC_LISTEN) {
  2431. res = -EINVAL;
  2432. goto exit;
  2433. }
  2434. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  2435. res = tipc_wait_for_accept(sock, timeo);
  2436. if (res)
  2437. goto exit;
  2438. buf = skb_peek(&sk->sk_receive_queue);
  2439. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
  2440. if (res)
  2441. goto exit;
  2442. security_sk_clone(sock->sk, new_sock->sk);
  2443. new_sk = new_sock->sk;
  2444. new_tsock = tipc_sk(new_sk);
  2445. msg = buf_msg(buf);
  2446. /* we lock on new_sk; but lockdep sees the lock on sk */
  2447. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  2448. /*
  2449. * Reject any stray messages received by new socket
  2450. * before the socket lock was taken (very, very unlikely)
  2451. */
  2452. tsk_rej_rx_queue(new_sk, TIPC_ERR_NO_PORT);
  2453. /* Connect new socket to it's peer */
  2454. tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
  2455. tsk_set_importance(new_sk, msg_importance(msg));
  2456. if (msg_named(msg)) {
  2457. new_tsock->conn_addrtype = TIPC_SERVICE_ADDR;
  2458. msg_set_nametype(&new_tsock->phdr, msg_nametype(msg));
  2459. msg_set_nameinst(&new_tsock->phdr, msg_nameinst(msg));
  2460. }
  2461. /*
  2462. * Respond to 'SYN-' by discarding it & returning 'ACK'.
  2463. * Respond to 'SYN+' by queuing it on new socket & returning 'ACK'.
  2464. */
  2465. if (!msg_data_sz(msg)) {
  2466. tsk_advance_rx_queue(sk);
  2467. } else {
  2468. __skb_dequeue(&sk->sk_receive_queue);
  2469. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  2470. skb_set_owner_r(buf, new_sk);
  2471. }
  2472. iov_iter_kvec(&m.msg_iter, ITER_SOURCE, NULL, 0, 0);
  2473. __tipc_sendstream(new_sock, &m, 0);
  2474. release_sock(new_sk);
  2475. exit:
  2476. release_sock(sk);
  2477. return res;
  2478. }
  2479. /**
  2480. * tipc_shutdown - shutdown socket connection
  2481. * @sock: socket structure
  2482. * @how: direction to close (must be SHUT_RDWR)
  2483. *
  2484. * Terminates connection (if necessary), then purges socket's receive queue.
  2485. *
  2486. * Return: 0 on success, errno otherwise
  2487. */
  2488. static int tipc_shutdown(struct socket *sock, int how)
  2489. {
  2490. struct sock *sk = sock->sk;
  2491. int res;
  2492. if (how != SHUT_RDWR)
  2493. return -EINVAL;
  2494. lock_sock(sk);
  2495. trace_tipc_sk_shutdown(sk, NULL, TIPC_DUMP_ALL, " ");
  2496. __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
  2497. sk->sk_shutdown = SHUTDOWN_MASK;
  2498. if (sk->sk_state == TIPC_DISCONNECTING) {
  2499. /* Discard any unreceived messages */
  2500. __skb_queue_purge(&sk->sk_receive_queue);
  2501. res = 0;
  2502. } else {
  2503. res = -ENOTCONN;
  2504. }
  2505. /* Wake up anyone sleeping in poll. */
  2506. sk->sk_state_change(sk);
  2507. release_sock(sk);
  2508. return res;
  2509. }
  2510. static void tipc_sk_check_probing_state(struct sock *sk,
  2511. struct sk_buff_head *list)
  2512. {
  2513. struct tipc_sock *tsk = tipc_sk(sk);
  2514. u32 pnode = tsk_peer_node(tsk);
  2515. u32 pport = tsk_peer_port(tsk);
  2516. u32 self = tsk_own_node(tsk);
  2517. u32 oport = tsk->portid;
  2518. struct sk_buff *skb;
  2519. if (tsk->probe_unacked) {
  2520. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  2521. sk->sk_err = ECONNABORTED;
  2522. tipc_node_remove_conn(sock_net(sk), pnode, pport);
  2523. sk->sk_state_change(sk);
  2524. return;
  2525. }
  2526. /* Prepare new probe */
  2527. skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, 0,
  2528. pnode, self, pport, oport, TIPC_OK);
  2529. if (skb)
  2530. __skb_queue_tail(list, skb);
  2531. tsk->probe_unacked = true;
  2532. sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
  2533. }
  2534. static void tipc_sk_retry_connect(struct sock *sk, struct sk_buff_head *list)
  2535. {
  2536. struct tipc_sock *tsk = tipc_sk(sk);
  2537. /* Try again later if dest link is congested */
  2538. if (tsk->cong_link_cnt) {
  2539. sk_reset_timer(sk, &sk->sk_timer,
  2540. jiffies + msecs_to_jiffies(100));
  2541. return;
  2542. }
  2543. /* Prepare SYN for retransmit */
  2544. tipc_msg_skb_clone(&sk->sk_write_queue, list);
  2545. }
  2546. static void tipc_sk_timeout(struct timer_list *t)
  2547. {
  2548. struct sock *sk = from_timer(sk, t, sk_timer);
  2549. struct tipc_sock *tsk = tipc_sk(sk);
  2550. u32 pnode = tsk_peer_node(tsk);
  2551. struct sk_buff_head list;
  2552. int rc = 0;
  2553. __skb_queue_head_init(&list);
  2554. bh_lock_sock(sk);
  2555. /* Try again later if socket is busy */
  2556. if (sock_owned_by_user(sk)) {
  2557. sk_reset_timer(sk, &sk->sk_timer, jiffies + HZ / 20);
  2558. bh_unlock_sock(sk);
  2559. sock_put(sk);
  2560. return;
  2561. }
  2562. if (sk->sk_state == TIPC_ESTABLISHED)
  2563. tipc_sk_check_probing_state(sk, &list);
  2564. else if (sk->sk_state == TIPC_CONNECTING)
  2565. tipc_sk_retry_connect(sk, &list);
  2566. bh_unlock_sock(sk);
  2567. if (!skb_queue_empty(&list))
  2568. rc = tipc_node_xmit(sock_net(sk), &list, pnode, tsk->portid);
  2569. /* SYN messages may cause link congestion */
  2570. if (rc == -ELINKCONG) {
  2571. tipc_dest_push(&tsk->cong_links, pnode, 0);
  2572. tsk->cong_link_cnt = 1;
  2573. }
  2574. sock_put(sk);
  2575. }
  2576. static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua)
  2577. {
  2578. struct sock *sk = &tsk->sk;
  2579. struct net *net = sock_net(sk);
  2580. struct tipc_socket_addr skaddr;
  2581. struct publication *p;
  2582. u32 key;
  2583. if (tipc_sk_connected(sk))
  2584. return -EINVAL;
  2585. key = tsk->portid + tsk->pub_count + 1;
  2586. if (key == tsk->portid)
  2587. return -EADDRINUSE;
  2588. skaddr.ref = tsk->portid;
  2589. skaddr.node = tipc_own_addr(net);
  2590. p = tipc_nametbl_publish(net, ua, &skaddr, key);
  2591. if (unlikely(!p))
  2592. return -EINVAL;
  2593. list_add(&p->binding_sock, &tsk->publications);
  2594. tsk->pub_count++;
  2595. tsk->published = true;
  2596. return 0;
  2597. }
  2598. static int tipc_sk_withdraw(struct tipc_sock *tsk, struct tipc_uaddr *ua)
  2599. {
  2600. struct net *net = sock_net(&tsk->sk);
  2601. struct publication *safe, *p;
  2602. struct tipc_uaddr _ua;
  2603. int rc = -EINVAL;
  2604. list_for_each_entry_safe(p, safe, &tsk->publications, binding_sock) {
  2605. if (!ua) {
  2606. tipc_uaddr(&_ua, TIPC_SERVICE_RANGE, p->scope,
  2607. p->sr.type, p->sr.lower, p->sr.upper);
  2608. tipc_nametbl_withdraw(net, &_ua, &p->sk, p->key);
  2609. continue;
  2610. }
  2611. /* Unbind specific publication */
  2612. if (p->scope != ua->scope)
  2613. continue;
  2614. if (p->sr.type != ua->sr.type)
  2615. continue;
  2616. if (p->sr.lower != ua->sr.lower)
  2617. continue;
  2618. if (p->sr.upper != ua->sr.upper)
  2619. break;
  2620. tipc_nametbl_withdraw(net, ua, &p->sk, p->key);
  2621. rc = 0;
  2622. break;
  2623. }
  2624. if (list_empty(&tsk->publications)) {
  2625. tsk->published = 0;
  2626. rc = 0;
  2627. }
  2628. return rc;
  2629. }
  2630. /* tipc_sk_reinit: set non-zero address in all existing sockets
  2631. * when we go from standalone to network mode.
  2632. */
  2633. void tipc_sk_reinit(struct net *net)
  2634. {
  2635. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2636. struct rhashtable_iter iter;
  2637. struct tipc_sock *tsk;
  2638. struct tipc_msg *msg;
  2639. rhashtable_walk_enter(&tn->sk_rht, &iter);
  2640. do {
  2641. rhashtable_walk_start(&iter);
  2642. while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
  2643. sock_hold(&tsk->sk);
  2644. rhashtable_walk_stop(&iter);
  2645. lock_sock(&tsk->sk);
  2646. msg = &tsk->phdr;
  2647. msg_set_prevnode(msg, tipc_own_addr(net));
  2648. msg_set_orignode(msg, tipc_own_addr(net));
  2649. release_sock(&tsk->sk);
  2650. rhashtable_walk_start(&iter);
  2651. sock_put(&tsk->sk);
  2652. }
  2653. rhashtable_walk_stop(&iter);
  2654. } while (tsk == ERR_PTR(-EAGAIN));
  2655. rhashtable_walk_exit(&iter);
  2656. }
  2657. static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
  2658. {
  2659. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2660. struct tipc_sock *tsk;
  2661. rcu_read_lock();
  2662. tsk = rhashtable_lookup(&tn->sk_rht, &portid, tsk_rht_params);
  2663. if (tsk)
  2664. sock_hold(&tsk->sk);
  2665. rcu_read_unlock();
  2666. return tsk;
  2667. }
  2668. static int tipc_sk_insert(struct tipc_sock *tsk)
  2669. {
  2670. struct sock *sk = &tsk->sk;
  2671. struct net *net = sock_net(sk);
  2672. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2673. u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
  2674. u32 portid = prandom_u32_max(remaining) + TIPC_MIN_PORT;
  2675. while (remaining--) {
  2676. portid++;
  2677. if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
  2678. portid = TIPC_MIN_PORT;
  2679. tsk->portid = portid;
  2680. sock_hold(&tsk->sk);
  2681. if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
  2682. tsk_rht_params))
  2683. return 0;
  2684. sock_put(&tsk->sk);
  2685. }
  2686. return -1;
  2687. }
  2688. static void tipc_sk_remove(struct tipc_sock *tsk)
  2689. {
  2690. struct sock *sk = &tsk->sk;
  2691. struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
  2692. if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
  2693. WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
  2694. __sock_put(sk);
  2695. }
  2696. }
  2697. static const struct rhashtable_params tsk_rht_params = {
  2698. .nelem_hint = 192,
  2699. .head_offset = offsetof(struct tipc_sock, node),
  2700. .key_offset = offsetof(struct tipc_sock, portid),
  2701. .key_len = sizeof(u32), /* portid */
  2702. .max_size = 1048576,
  2703. .min_size = 256,
  2704. .automatic_shrinking = true,
  2705. };
  2706. int tipc_sk_rht_init(struct net *net)
  2707. {
  2708. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2709. return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
  2710. }
  2711. void tipc_sk_rht_destroy(struct net *net)
  2712. {
  2713. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2714. /* Wait for socket readers to complete */
  2715. synchronize_net();
  2716. rhashtable_destroy(&tn->sk_rht);
  2717. }
  2718. static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
  2719. {
  2720. struct net *net = sock_net(&tsk->sk);
  2721. struct tipc_group *grp = tsk->group;
  2722. struct tipc_msg *hdr = &tsk->phdr;
  2723. struct tipc_uaddr ua;
  2724. int rc;
  2725. if (mreq->type < TIPC_RESERVED_TYPES)
  2726. return -EACCES;
  2727. if (mreq->scope > TIPC_NODE_SCOPE)
  2728. return -EINVAL;
  2729. if (mreq->scope != TIPC_NODE_SCOPE)
  2730. mreq->scope = TIPC_CLUSTER_SCOPE;
  2731. if (grp)
  2732. return -EACCES;
  2733. grp = tipc_group_create(net, tsk->portid, mreq, &tsk->group_is_open);
  2734. if (!grp)
  2735. return -ENOMEM;
  2736. tsk->group = grp;
  2737. msg_set_lookup_scope(hdr, mreq->scope);
  2738. msg_set_nametype(hdr, mreq->type);
  2739. msg_set_dest_droppable(hdr, true);
  2740. tipc_uaddr(&ua, TIPC_SERVICE_RANGE, mreq->scope,
  2741. mreq->type, mreq->instance, mreq->instance);
  2742. tipc_nametbl_build_group(net, grp, &ua);
  2743. rc = tipc_sk_publish(tsk, &ua);
  2744. if (rc) {
  2745. tipc_group_delete(net, grp);
  2746. tsk->group = NULL;
  2747. return rc;
  2748. }
  2749. /* Eliminate any risk that a broadcast overtakes sent JOINs */
  2750. tsk->mc_method.rcast = true;
  2751. tsk->mc_method.mandatory = true;
  2752. tipc_group_join(net, grp, &tsk->sk.sk_rcvbuf);
  2753. return rc;
  2754. }
  2755. static int tipc_sk_leave(struct tipc_sock *tsk)
  2756. {
  2757. struct net *net = sock_net(&tsk->sk);
  2758. struct tipc_group *grp = tsk->group;
  2759. struct tipc_uaddr ua;
  2760. int scope;
  2761. if (!grp)
  2762. return -EINVAL;
  2763. ua.addrtype = TIPC_SERVICE_RANGE;
  2764. tipc_group_self(grp, &ua.sr, &scope);
  2765. ua.scope = scope;
  2766. tipc_group_delete(net, grp);
  2767. tsk->group = NULL;
  2768. tipc_sk_withdraw(tsk, &ua);
  2769. return 0;
  2770. }
  2771. /**
  2772. * tipc_setsockopt - set socket option
  2773. * @sock: socket structure
  2774. * @lvl: option level
  2775. * @opt: option identifier
  2776. * @ov: pointer to new option value
  2777. * @ol: length of option value
  2778. *
  2779. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  2780. * (to ease compatibility).
  2781. *
  2782. * Return: 0 on success, errno otherwise
  2783. */
  2784. static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
  2785. sockptr_t ov, unsigned int ol)
  2786. {
  2787. struct sock *sk = sock->sk;
  2788. struct tipc_sock *tsk = tipc_sk(sk);
  2789. struct tipc_group_req mreq;
  2790. u32 value = 0;
  2791. int res = 0;
  2792. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  2793. return 0;
  2794. if (lvl != SOL_TIPC)
  2795. return -ENOPROTOOPT;
  2796. switch (opt) {
  2797. case TIPC_IMPORTANCE:
  2798. case TIPC_SRC_DROPPABLE:
  2799. case TIPC_DEST_DROPPABLE:
  2800. case TIPC_CONN_TIMEOUT:
  2801. case TIPC_NODELAY:
  2802. if (ol < sizeof(value))
  2803. return -EINVAL;
  2804. if (copy_from_sockptr(&value, ov, sizeof(u32)))
  2805. return -EFAULT;
  2806. break;
  2807. case TIPC_GROUP_JOIN:
  2808. if (ol < sizeof(mreq))
  2809. return -EINVAL;
  2810. if (copy_from_sockptr(&mreq, ov, sizeof(mreq)))
  2811. return -EFAULT;
  2812. break;
  2813. default:
  2814. if (!sockptr_is_null(ov) || ol)
  2815. return -EINVAL;
  2816. }
  2817. lock_sock(sk);
  2818. switch (opt) {
  2819. case TIPC_IMPORTANCE:
  2820. res = tsk_set_importance(sk, value);
  2821. break;
  2822. case TIPC_SRC_DROPPABLE:
  2823. if (sock->type != SOCK_STREAM)
  2824. tsk_set_unreliable(tsk, value);
  2825. else
  2826. res = -ENOPROTOOPT;
  2827. break;
  2828. case TIPC_DEST_DROPPABLE:
  2829. tsk_set_unreturnable(tsk, value);
  2830. break;
  2831. case TIPC_CONN_TIMEOUT:
  2832. tipc_sk(sk)->conn_timeout = value;
  2833. break;
  2834. case TIPC_MCAST_BROADCAST:
  2835. tsk->mc_method.rcast = false;
  2836. tsk->mc_method.mandatory = true;
  2837. break;
  2838. case TIPC_MCAST_REPLICAST:
  2839. tsk->mc_method.rcast = true;
  2840. tsk->mc_method.mandatory = true;
  2841. break;
  2842. case TIPC_GROUP_JOIN:
  2843. res = tipc_sk_join(tsk, &mreq);
  2844. break;
  2845. case TIPC_GROUP_LEAVE:
  2846. res = tipc_sk_leave(tsk);
  2847. break;
  2848. case TIPC_NODELAY:
  2849. tsk->nodelay = !!value;
  2850. tsk_set_nagle(tsk);
  2851. break;
  2852. default:
  2853. res = -EINVAL;
  2854. }
  2855. release_sock(sk);
  2856. return res;
  2857. }
  2858. /**
  2859. * tipc_getsockopt - get socket option
  2860. * @sock: socket structure
  2861. * @lvl: option level
  2862. * @opt: option identifier
  2863. * @ov: receptacle for option value
  2864. * @ol: receptacle for length of option value
  2865. *
  2866. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  2867. * (to ease compatibility).
  2868. *
  2869. * Return: 0 on success, errno otherwise
  2870. */
  2871. static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
  2872. char __user *ov, int __user *ol)
  2873. {
  2874. struct sock *sk = sock->sk;
  2875. struct tipc_sock *tsk = tipc_sk(sk);
  2876. struct tipc_service_range seq;
  2877. int len, scope;
  2878. u32 value;
  2879. int res;
  2880. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  2881. return put_user(0, ol);
  2882. if (lvl != SOL_TIPC)
  2883. return -ENOPROTOOPT;
  2884. res = get_user(len, ol);
  2885. if (res)
  2886. return res;
  2887. lock_sock(sk);
  2888. switch (opt) {
  2889. case TIPC_IMPORTANCE:
  2890. value = tsk_importance(tsk);
  2891. break;
  2892. case TIPC_SRC_DROPPABLE:
  2893. value = tsk_unreliable(tsk);
  2894. break;
  2895. case TIPC_DEST_DROPPABLE:
  2896. value = tsk_unreturnable(tsk);
  2897. break;
  2898. case TIPC_CONN_TIMEOUT:
  2899. value = tsk->conn_timeout;
  2900. /* no need to set "res", since already 0 at this point */
  2901. break;
  2902. case TIPC_NODE_RECVQ_DEPTH:
  2903. value = 0; /* was tipc_queue_size, now obsolete */
  2904. break;
  2905. case TIPC_SOCK_RECVQ_DEPTH:
  2906. value = skb_queue_len(&sk->sk_receive_queue);
  2907. break;
  2908. case TIPC_SOCK_RECVQ_USED:
  2909. value = sk_rmem_alloc_get(sk);
  2910. break;
  2911. case TIPC_GROUP_JOIN:
  2912. seq.type = 0;
  2913. if (tsk->group)
  2914. tipc_group_self(tsk->group, &seq, &scope);
  2915. value = seq.type;
  2916. break;
  2917. default:
  2918. res = -EINVAL;
  2919. }
  2920. release_sock(sk);
  2921. if (res)
  2922. return res; /* "get" failed */
  2923. if (len < sizeof(value))
  2924. return -EINVAL;
  2925. if (copy_to_user(ov, &value, sizeof(value)))
  2926. return -EFAULT;
  2927. return put_user(sizeof(value), ol);
  2928. }
  2929. static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  2930. {
  2931. struct net *net = sock_net(sock->sk);
  2932. struct tipc_sioc_nodeid_req nr = {0};
  2933. struct tipc_sioc_ln_req lnr;
  2934. void __user *argp = (void __user *)arg;
  2935. switch (cmd) {
  2936. case SIOCGETLINKNAME:
  2937. if (copy_from_user(&lnr, argp, sizeof(lnr)))
  2938. return -EFAULT;
  2939. if (!tipc_node_get_linkname(net,
  2940. lnr.bearer_id & 0xffff, lnr.peer,
  2941. lnr.linkname, TIPC_MAX_LINK_NAME)) {
  2942. if (copy_to_user(argp, &lnr, sizeof(lnr)))
  2943. return -EFAULT;
  2944. return 0;
  2945. }
  2946. return -EADDRNOTAVAIL;
  2947. case SIOCGETNODEID:
  2948. if (copy_from_user(&nr, argp, sizeof(nr)))
  2949. return -EFAULT;
  2950. if (!tipc_node_get_id(net, nr.peer, nr.node_id))
  2951. return -EADDRNOTAVAIL;
  2952. if (copy_to_user(argp, &nr, sizeof(nr)))
  2953. return -EFAULT;
  2954. return 0;
  2955. default:
  2956. return -ENOIOCTLCMD;
  2957. }
  2958. }
  2959. static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
  2960. {
  2961. struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
  2962. struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
  2963. u32 onode = tipc_own_addr(sock_net(sock1->sk));
  2964. tsk1->peer.family = AF_TIPC;
  2965. tsk1->peer.addrtype = TIPC_SOCKET_ADDR;
  2966. tsk1->peer.scope = TIPC_NODE_SCOPE;
  2967. tsk1->peer.addr.id.ref = tsk2->portid;
  2968. tsk1->peer.addr.id.node = onode;
  2969. tsk2->peer.family = AF_TIPC;
  2970. tsk2->peer.addrtype = TIPC_SOCKET_ADDR;
  2971. tsk2->peer.scope = TIPC_NODE_SCOPE;
  2972. tsk2->peer.addr.id.ref = tsk1->portid;
  2973. tsk2->peer.addr.id.node = onode;
  2974. tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
  2975. tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
  2976. return 0;
  2977. }
  2978. /* Protocol switches for the various types of TIPC sockets */
  2979. static const struct proto_ops msg_ops = {
  2980. .owner = THIS_MODULE,
  2981. .family = AF_TIPC,
  2982. .release = tipc_release,
  2983. .bind = tipc_bind,
  2984. .connect = tipc_connect,
  2985. .socketpair = tipc_socketpair,
  2986. .accept = sock_no_accept,
  2987. .getname = tipc_getname,
  2988. .poll = tipc_poll,
  2989. .ioctl = tipc_ioctl,
  2990. .listen = sock_no_listen,
  2991. .shutdown = tipc_shutdown,
  2992. .setsockopt = tipc_setsockopt,
  2993. .getsockopt = tipc_getsockopt,
  2994. .sendmsg = tipc_sendmsg,
  2995. .recvmsg = tipc_recvmsg,
  2996. .mmap = sock_no_mmap,
  2997. .sendpage = sock_no_sendpage
  2998. };
  2999. static const struct proto_ops packet_ops = {
  3000. .owner = THIS_MODULE,
  3001. .family = AF_TIPC,
  3002. .release = tipc_release,
  3003. .bind = tipc_bind,
  3004. .connect = tipc_connect,
  3005. .socketpair = tipc_socketpair,
  3006. .accept = tipc_accept,
  3007. .getname = tipc_getname,
  3008. .poll = tipc_poll,
  3009. .ioctl = tipc_ioctl,
  3010. .listen = tipc_listen,
  3011. .shutdown = tipc_shutdown,
  3012. .setsockopt = tipc_setsockopt,
  3013. .getsockopt = tipc_getsockopt,
  3014. .sendmsg = tipc_send_packet,
  3015. .recvmsg = tipc_recvmsg,
  3016. .mmap = sock_no_mmap,
  3017. .sendpage = sock_no_sendpage
  3018. };
  3019. static const struct proto_ops stream_ops = {
  3020. .owner = THIS_MODULE,
  3021. .family = AF_TIPC,
  3022. .release = tipc_release,
  3023. .bind = tipc_bind,
  3024. .connect = tipc_connect,
  3025. .socketpair = tipc_socketpair,
  3026. .accept = tipc_accept,
  3027. .getname = tipc_getname,
  3028. .poll = tipc_poll,
  3029. .ioctl = tipc_ioctl,
  3030. .listen = tipc_listen,
  3031. .shutdown = tipc_shutdown,
  3032. .setsockopt = tipc_setsockopt,
  3033. .getsockopt = tipc_getsockopt,
  3034. .sendmsg = tipc_sendstream,
  3035. .recvmsg = tipc_recvstream,
  3036. .mmap = sock_no_mmap,
  3037. .sendpage = sock_no_sendpage
  3038. };
  3039. static const struct net_proto_family tipc_family_ops = {
  3040. .owner = THIS_MODULE,
  3041. .family = AF_TIPC,
  3042. .create = tipc_sk_create
  3043. };
  3044. static struct proto tipc_proto = {
  3045. .name = "TIPC",
  3046. .owner = THIS_MODULE,
  3047. .obj_size = sizeof(struct tipc_sock),
  3048. .sysctl_rmem = sysctl_tipc_rmem
  3049. };
  3050. /**
  3051. * tipc_socket_init - initialize TIPC socket interface
  3052. *
  3053. * Return: 0 on success, errno otherwise
  3054. */
  3055. int tipc_socket_init(void)
  3056. {
  3057. int res;
  3058. res = proto_register(&tipc_proto, 1);
  3059. if (res) {
  3060. pr_err("Failed to register TIPC protocol type\n");
  3061. goto out;
  3062. }
  3063. res = sock_register(&tipc_family_ops);
  3064. if (res) {
  3065. pr_err("Failed to register TIPC socket type\n");
  3066. proto_unregister(&tipc_proto);
  3067. goto out;
  3068. }
  3069. out:
  3070. return res;
  3071. }
  3072. /**
  3073. * tipc_socket_stop - stop TIPC socket interface
  3074. */
  3075. void tipc_socket_stop(void)
  3076. {
  3077. sock_unregister(tipc_family_ops.family);
  3078. proto_unregister(&tipc_proto);
  3079. }
  3080. /* Caller should hold socket lock for the passed tipc socket. */
  3081. static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
  3082. {
  3083. u32 peer_node, peer_port;
  3084. u32 conn_type, conn_instance;
  3085. struct nlattr *nest;
  3086. peer_node = tsk_peer_node(tsk);
  3087. peer_port = tsk_peer_port(tsk);
  3088. conn_type = msg_nametype(&tsk->phdr);
  3089. conn_instance = msg_nameinst(&tsk->phdr);
  3090. nest = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_CON);
  3091. if (!nest)
  3092. return -EMSGSIZE;
  3093. if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
  3094. goto msg_full;
  3095. if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
  3096. goto msg_full;
  3097. if (tsk->conn_addrtype != 0) {
  3098. if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
  3099. goto msg_full;
  3100. if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, conn_type))
  3101. goto msg_full;
  3102. if (nla_put_u32(skb, TIPC_NLA_CON_INST, conn_instance))
  3103. goto msg_full;
  3104. }
  3105. nla_nest_end(skb, nest);
  3106. return 0;
  3107. msg_full:
  3108. nla_nest_cancel(skb, nest);
  3109. return -EMSGSIZE;
  3110. }
  3111. static int __tipc_nl_add_sk_info(struct sk_buff *skb, struct tipc_sock
  3112. *tsk)
  3113. {
  3114. struct net *net = sock_net(skb->sk);
  3115. struct sock *sk = &tsk->sk;
  3116. if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid) ||
  3117. nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr(net)))
  3118. return -EMSGSIZE;
  3119. if (tipc_sk_connected(sk)) {
  3120. if (__tipc_nl_add_sk_con(skb, tsk))
  3121. return -EMSGSIZE;
  3122. } else if (!list_empty(&tsk->publications)) {
  3123. if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
  3124. return -EMSGSIZE;
  3125. }
  3126. return 0;
  3127. }
  3128. /* Caller should hold socket lock for the passed tipc socket. */
  3129. static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
  3130. struct tipc_sock *tsk)
  3131. {
  3132. struct nlattr *attrs;
  3133. void *hdr;
  3134. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  3135. &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
  3136. if (!hdr)
  3137. goto msg_cancel;
  3138. attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
  3139. if (!attrs)
  3140. goto genlmsg_cancel;
  3141. if (__tipc_nl_add_sk_info(skb, tsk))
  3142. goto attr_msg_cancel;
  3143. nla_nest_end(skb, attrs);
  3144. genlmsg_end(skb, hdr);
  3145. return 0;
  3146. attr_msg_cancel:
  3147. nla_nest_cancel(skb, attrs);
  3148. genlmsg_cancel:
  3149. genlmsg_cancel(skb, hdr);
  3150. msg_cancel:
  3151. return -EMSGSIZE;
  3152. }
  3153. int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,
  3154. int (*skb_handler)(struct sk_buff *skb,
  3155. struct netlink_callback *cb,
  3156. struct tipc_sock *tsk))
  3157. {
  3158. struct rhashtable_iter *iter = (void *)cb->args[4];
  3159. struct tipc_sock *tsk;
  3160. int err;
  3161. rhashtable_walk_start(iter);
  3162. while ((tsk = rhashtable_walk_next(iter)) != NULL) {
  3163. if (IS_ERR(tsk)) {
  3164. err = PTR_ERR(tsk);
  3165. if (err == -EAGAIN) {
  3166. err = 0;
  3167. continue;
  3168. }
  3169. break;
  3170. }
  3171. sock_hold(&tsk->sk);
  3172. rhashtable_walk_stop(iter);
  3173. lock_sock(&tsk->sk);
  3174. err = skb_handler(skb, cb, tsk);
  3175. if (err) {
  3176. release_sock(&tsk->sk);
  3177. sock_put(&tsk->sk);
  3178. goto out;
  3179. }
  3180. release_sock(&tsk->sk);
  3181. rhashtable_walk_start(iter);
  3182. sock_put(&tsk->sk);
  3183. }
  3184. rhashtable_walk_stop(iter);
  3185. out:
  3186. return skb->len;
  3187. }
  3188. EXPORT_SYMBOL(tipc_nl_sk_walk);
  3189. int tipc_dump_start(struct netlink_callback *cb)
  3190. {
  3191. return __tipc_dump_start(cb, sock_net(cb->skb->sk));
  3192. }
  3193. EXPORT_SYMBOL(tipc_dump_start);
  3194. int __tipc_dump_start(struct netlink_callback *cb, struct net *net)
  3195. {
  3196. /* tipc_nl_name_table_dump() uses cb->args[0...3]. */
  3197. struct rhashtable_iter *iter = (void *)cb->args[4];
  3198. struct tipc_net *tn = tipc_net(net);
  3199. if (!iter) {
  3200. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  3201. if (!iter)
  3202. return -ENOMEM;
  3203. cb->args[4] = (long)iter;
  3204. }
  3205. rhashtable_walk_enter(&tn->sk_rht, iter);
  3206. return 0;
  3207. }
  3208. int tipc_dump_done(struct netlink_callback *cb)
  3209. {
  3210. struct rhashtable_iter *hti = (void *)cb->args[4];
  3211. rhashtable_walk_exit(hti);
  3212. kfree(hti);
  3213. return 0;
  3214. }
  3215. EXPORT_SYMBOL(tipc_dump_done);
  3216. int tipc_sk_fill_sock_diag(struct sk_buff *skb, struct netlink_callback *cb,
  3217. struct tipc_sock *tsk, u32 sk_filter_state,
  3218. u64 (*tipc_diag_gen_cookie)(struct sock *sk))
  3219. {
  3220. struct sock *sk = &tsk->sk;
  3221. struct nlattr *attrs;
  3222. struct nlattr *stat;
  3223. /*filter response w.r.t sk_state*/
  3224. if (!(sk_filter_state & (1 << sk->sk_state)))
  3225. return 0;
  3226. attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
  3227. if (!attrs)
  3228. goto msg_cancel;
  3229. if (__tipc_nl_add_sk_info(skb, tsk))
  3230. goto attr_msg_cancel;
  3231. if (nla_put_u32(skb, TIPC_NLA_SOCK_TYPE, (u32)sk->sk_type) ||
  3232. nla_put_u32(skb, TIPC_NLA_SOCK_TIPC_STATE, (u32)sk->sk_state) ||
  3233. nla_put_u32(skb, TIPC_NLA_SOCK_INO, sock_i_ino(sk)) ||
  3234. nla_put_u32(skb, TIPC_NLA_SOCK_UID,
  3235. from_kuid_munged(sk_user_ns(NETLINK_CB(cb->skb).sk),
  3236. sock_i_uid(sk))) ||
  3237. nla_put_u64_64bit(skb, TIPC_NLA_SOCK_COOKIE,
  3238. tipc_diag_gen_cookie(sk),
  3239. TIPC_NLA_SOCK_PAD))
  3240. goto attr_msg_cancel;
  3241. stat = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_STAT);
  3242. if (!stat)
  3243. goto attr_msg_cancel;
  3244. if (nla_put_u32(skb, TIPC_NLA_SOCK_STAT_RCVQ,
  3245. skb_queue_len(&sk->sk_receive_queue)) ||
  3246. nla_put_u32(skb, TIPC_NLA_SOCK_STAT_SENDQ,
  3247. skb_queue_len(&sk->sk_write_queue)) ||
  3248. nla_put_u32(skb, TIPC_NLA_SOCK_STAT_DROP,
  3249. atomic_read(&sk->sk_drops)))
  3250. goto stat_msg_cancel;
  3251. if (tsk->cong_link_cnt &&
  3252. nla_put_flag(skb, TIPC_NLA_SOCK_STAT_LINK_CONG))
  3253. goto stat_msg_cancel;
  3254. if (tsk_conn_cong(tsk) &&
  3255. nla_put_flag(skb, TIPC_NLA_SOCK_STAT_CONN_CONG))
  3256. goto stat_msg_cancel;
  3257. nla_nest_end(skb, stat);
  3258. if (tsk->group)
  3259. if (tipc_group_fill_sock_diag(tsk->group, skb))
  3260. goto stat_msg_cancel;
  3261. nla_nest_end(skb, attrs);
  3262. return 0;
  3263. stat_msg_cancel:
  3264. nla_nest_cancel(skb, stat);
  3265. attr_msg_cancel:
  3266. nla_nest_cancel(skb, attrs);
  3267. msg_cancel:
  3268. return -EMSGSIZE;
  3269. }
  3270. EXPORT_SYMBOL(tipc_sk_fill_sock_diag);
  3271. int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
  3272. {
  3273. return tipc_nl_sk_walk(skb, cb, __tipc_nl_add_sk);
  3274. }
  3275. /* Caller should hold socket lock for the passed tipc socket. */
  3276. static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
  3277. struct netlink_callback *cb,
  3278. struct publication *publ)
  3279. {
  3280. void *hdr;
  3281. struct nlattr *attrs;
  3282. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  3283. &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
  3284. if (!hdr)
  3285. goto msg_cancel;
  3286. attrs = nla_nest_start_noflag(skb, TIPC_NLA_PUBL);
  3287. if (!attrs)
  3288. goto genlmsg_cancel;
  3289. if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
  3290. goto attr_msg_cancel;
  3291. if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->sr.type))
  3292. goto attr_msg_cancel;
  3293. if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->sr.lower))
  3294. goto attr_msg_cancel;
  3295. if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->sr.upper))
  3296. goto attr_msg_cancel;
  3297. nla_nest_end(skb, attrs);
  3298. genlmsg_end(skb, hdr);
  3299. return 0;
  3300. attr_msg_cancel:
  3301. nla_nest_cancel(skb, attrs);
  3302. genlmsg_cancel:
  3303. genlmsg_cancel(skb, hdr);
  3304. msg_cancel:
  3305. return -EMSGSIZE;
  3306. }
  3307. /* Caller should hold socket lock for the passed tipc socket. */
  3308. static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
  3309. struct netlink_callback *cb,
  3310. struct tipc_sock *tsk, u32 *last_publ)
  3311. {
  3312. int err;
  3313. struct publication *p;
  3314. if (*last_publ) {
  3315. list_for_each_entry(p, &tsk->publications, binding_sock) {
  3316. if (p->key == *last_publ)
  3317. break;
  3318. }
  3319. if (list_entry_is_head(p, &tsk->publications, binding_sock)) {
  3320. /* We never set seq or call nl_dump_check_consistent()
  3321. * this means that setting prev_seq here will cause the
  3322. * consistence check to fail in the netlink callback
  3323. * handler. Resulting in the last NLMSG_DONE message
  3324. * having the NLM_F_DUMP_INTR flag set.
  3325. */
  3326. cb->prev_seq = 1;
  3327. *last_publ = 0;
  3328. return -EPIPE;
  3329. }
  3330. } else {
  3331. p = list_first_entry(&tsk->publications, struct publication,
  3332. binding_sock);
  3333. }
  3334. list_for_each_entry_from(p, &tsk->publications, binding_sock) {
  3335. err = __tipc_nl_add_sk_publ(skb, cb, p);
  3336. if (err) {
  3337. *last_publ = p->key;
  3338. return err;
  3339. }
  3340. }
  3341. *last_publ = 0;
  3342. return 0;
  3343. }
  3344. int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
  3345. {
  3346. int err;
  3347. u32 tsk_portid = cb->args[0];
  3348. u32 last_publ = cb->args[1];
  3349. u32 done = cb->args[2];
  3350. struct net *net = sock_net(skb->sk);
  3351. struct tipc_sock *tsk;
  3352. if (!tsk_portid) {
  3353. struct nlattr **attrs = genl_dumpit_info(cb)->attrs;
  3354. struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
  3355. if (!attrs[TIPC_NLA_SOCK])
  3356. return -EINVAL;
  3357. err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
  3358. attrs[TIPC_NLA_SOCK],
  3359. tipc_nl_sock_policy, NULL);
  3360. if (err)
  3361. return err;
  3362. if (!sock[TIPC_NLA_SOCK_REF])
  3363. return -EINVAL;
  3364. tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
  3365. }
  3366. if (done)
  3367. return 0;
  3368. tsk = tipc_sk_lookup(net, tsk_portid);
  3369. if (!tsk)
  3370. return -EINVAL;
  3371. lock_sock(&tsk->sk);
  3372. err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
  3373. if (!err)
  3374. done = 1;
  3375. release_sock(&tsk->sk);
  3376. sock_put(&tsk->sk);
  3377. cb->args[0] = tsk_portid;
  3378. cb->args[1] = last_publ;
  3379. cb->args[2] = done;
  3380. return skb->len;
  3381. }
  3382. /**
  3383. * tipc_sk_filtering - check if a socket should be traced
  3384. * @sk: the socket to be examined
  3385. *
  3386. * @sysctl_tipc_sk_filter is used as the socket tuple for filtering:
  3387. * (portid, sock type, name type, name lower, name upper)
  3388. *
  3389. * Return: true if the socket meets the socket tuple data
  3390. * (value 0 = 'any') or when there is no tuple set (all = 0),
  3391. * otherwise false
  3392. */
  3393. bool tipc_sk_filtering(struct sock *sk)
  3394. {
  3395. struct tipc_sock *tsk;
  3396. struct publication *p;
  3397. u32 _port, _sktype, _type, _lower, _upper;
  3398. u32 type = 0, lower = 0, upper = 0;
  3399. if (!sk)
  3400. return true;
  3401. tsk = tipc_sk(sk);
  3402. _port = sysctl_tipc_sk_filter[0];
  3403. _sktype = sysctl_tipc_sk_filter[1];
  3404. _type = sysctl_tipc_sk_filter[2];
  3405. _lower = sysctl_tipc_sk_filter[3];
  3406. _upper = sysctl_tipc_sk_filter[4];
  3407. if (!_port && !_sktype && !_type && !_lower && !_upper)
  3408. return true;
  3409. if (_port)
  3410. return (_port == tsk->portid);
  3411. if (_sktype && _sktype != sk->sk_type)
  3412. return false;
  3413. if (tsk->published) {
  3414. p = list_first_entry_or_null(&tsk->publications,
  3415. struct publication, binding_sock);
  3416. if (p) {
  3417. type = p->sr.type;
  3418. lower = p->sr.lower;
  3419. upper = p->sr.upper;
  3420. }
  3421. }
  3422. if (!tipc_sk_type_connectionless(sk)) {
  3423. type = msg_nametype(&tsk->phdr);
  3424. lower = msg_nameinst(&tsk->phdr);
  3425. upper = lower;
  3426. }
  3427. if ((_type && _type != type) || (_lower && _lower != lower) ||
  3428. (_upper && _upper != upper))
  3429. return false;
  3430. return true;
  3431. }
  3432. u32 tipc_sock_get_portid(struct sock *sk)
  3433. {
  3434. return (sk) ? (tipc_sk(sk))->portid : 0;
  3435. }
  3436. /**
  3437. * tipc_sk_overlimit1 - check if socket rx queue is about to be overloaded,
  3438. * both the rcv and backlog queues are considered
  3439. * @sk: tipc sk to be checked
  3440. * @skb: tipc msg to be checked
  3441. *
  3442. * Return: true if the socket rx queue allocation is > 90%, otherwise false
  3443. */
  3444. bool tipc_sk_overlimit1(struct sock *sk, struct sk_buff *skb)
  3445. {
  3446. atomic_t *dcnt = &tipc_sk(sk)->dupl_rcvcnt;
  3447. unsigned int lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
  3448. unsigned int qsize = sk->sk_backlog.len + sk_rmem_alloc_get(sk);
  3449. return (qsize > lim * 90 / 100);
  3450. }
  3451. /**
  3452. * tipc_sk_overlimit2 - check if socket rx queue is about to be overloaded,
  3453. * only the rcv queue is considered
  3454. * @sk: tipc sk to be checked
  3455. * @skb: tipc msg to be checked
  3456. *
  3457. * Return: true if the socket rx queue allocation is > 90%, otherwise false
  3458. */
  3459. bool tipc_sk_overlimit2(struct sock *sk, struct sk_buff *skb)
  3460. {
  3461. unsigned int lim = rcvbuf_limit(sk, skb);
  3462. unsigned int qsize = sk_rmem_alloc_get(sk);
  3463. return (qsize > lim * 90 / 100);
  3464. }
  3465. /**
  3466. * tipc_sk_dump - dump TIPC socket
  3467. * @sk: tipc sk to be dumped
  3468. * @dqueues: bitmask to decide if any socket queue to be dumped?
  3469. * - TIPC_DUMP_NONE: don't dump socket queues
  3470. * - TIPC_DUMP_SK_SNDQ: dump socket send queue
  3471. * - TIPC_DUMP_SK_RCVQ: dump socket rcv queue
  3472. * - TIPC_DUMP_SK_BKLGQ: dump socket backlog queue
  3473. * - TIPC_DUMP_ALL: dump all the socket queues above
  3474. * @buf: returned buffer of dump data in format
  3475. */
  3476. int tipc_sk_dump(struct sock *sk, u16 dqueues, char *buf)
  3477. {
  3478. int i = 0;
  3479. size_t sz = (dqueues) ? SK_LMAX : SK_LMIN;
  3480. u32 conn_type, conn_instance;
  3481. struct tipc_sock *tsk;
  3482. struct publication *p;
  3483. bool tsk_connected;
  3484. if (!sk) {
  3485. i += scnprintf(buf, sz, "sk data: (null)\n");
  3486. return i;
  3487. }
  3488. tsk = tipc_sk(sk);
  3489. tsk_connected = !tipc_sk_type_connectionless(sk);
  3490. i += scnprintf(buf, sz, "sk data: %u", sk->sk_type);
  3491. i += scnprintf(buf + i, sz - i, " %d", sk->sk_state);
  3492. i += scnprintf(buf + i, sz - i, " %x", tsk_own_node(tsk));
  3493. i += scnprintf(buf + i, sz - i, " %u", tsk->portid);
  3494. i += scnprintf(buf + i, sz - i, " | %u", tsk_connected);
  3495. if (tsk_connected) {
  3496. i += scnprintf(buf + i, sz - i, " %x", tsk_peer_node(tsk));
  3497. i += scnprintf(buf + i, sz - i, " %u", tsk_peer_port(tsk));
  3498. conn_type = msg_nametype(&tsk->phdr);
  3499. conn_instance = msg_nameinst(&tsk->phdr);
  3500. i += scnprintf(buf + i, sz - i, " %u", conn_type);
  3501. i += scnprintf(buf + i, sz - i, " %u", conn_instance);
  3502. }
  3503. i += scnprintf(buf + i, sz - i, " | %u", tsk->published);
  3504. if (tsk->published) {
  3505. p = list_first_entry_or_null(&tsk->publications,
  3506. struct publication, binding_sock);
  3507. i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.type : 0);
  3508. i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.lower : 0);
  3509. i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.upper : 0);
  3510. }
  3511. i += scnprintf(buf + i, sz - i, " | %u", tsk->snd_win);
  3512. i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_win);
  3513. i += scnprintf(buf + i, sz - i, " %u", tsk->max_pkt);
  3514. i += scnprintf(buf + i, sz - i, " %x", tsk->peer_caps);
  3515. i += scnprintf(buf + i, sz - i, " %u", tsk->cong_link_cnt);
  3516. i += scnprintf(buf + i, sz - i, " %u", tsk->snt_unacked);
  3517. i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_unacked);
  3518. i += scnprintf(buf + i, sz - i, " %u", atomic_read(&tsk->dupl_rcvcnt));
  3519. i += scnprintf(buf + i, sz - i, " %u", sk->sk_shutdown);
  3520. i += scnprintf(buf + i, sz - i, " | %d", sk_wmem_alloc_get(sk));
  3521. i += scnprintf(buf + i, sz - i, " %d", sk->sk_sndbuf);
  3522. i += scnprintf(buf + i, sz - i, " | %d", sk_rmem_alloc_get(sk));
  3523. i += scnprintf(buf + i, sz - i, " %d", sk->sk_rcvbuf);
  3524. i += scnprintf(buf + i, sz - i, " | %d\n", READ_ONCE(sk->sk_backlog.len));
  3525. if (dqueues & TIPC_DUMP_SK_SNDQ) {
  3526. i += scnprintf(buf + i, sz - i, "sk_write_queue: ");
  3527. i += tipc_list_dump(&sk->sk_write_queue, false, buf + i);
  3528. }
  3529. if (dqueues & TIPC_DUMP_SK_RCVQ) {
  3530. i += scnprintf(buf + i, sz - i, "sk_receive_queue: ");
  3531. i += tipc_list_dump(&sk->sk_receive_queue, false, buf + i);
  3532. }
  3533. if (dqueues & TIPC_DUMP_SK_BKLGQ) {
  3534. i += scnprintf(buf + i, sz - i, "sk_backlog:\n head ");
  3535. i += tipc_skb_dump(sk->sk_backlog.head, false, buf + i);
  3536. if (sk->sk_backlog.tail != sk->sk_backlog.head) {
  3537. i += scnprintf(buf + i, sz - i, " tail ");
  3538. i += tipc_skb_dump(sk->sk_backlog.tail, false,
  3539. buf + i);
  3540. }
  3541. }
  3542. return i;
  3543. }