socket.c 89 KB

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