socket.c 105 KB

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