socket.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528
  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, sock, 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 net *net = sock_net(sk);
  782. struct tipc_member *mb = NULL;
  783. u32 node, port;
  784. int rc;
  785. node = dest->addr.id.node;
  786. port = dest->addr.id.ref;
  787. if (!port && !node)
  788. return -EHOSTUNREACH;
  789. /* Block or return if destination link or member is congested */
  790. rc = tipc_wait_for_cond(sock, &timeout,
  791. !tipc_dest_find(&tsk->cong_links, node, 0) &&
  792. tsk->group &&
  793. !tipc_group_cong(tsk->group, node, port, blks,
  794. &mb));
  795. if (unlikely(rc))
  796. return rc;
  797. if (unlikely(!mb))
  798. return -EHOSTUNREACH;
  799. rc = tipc_send_group_msg(net, tsk, m, mb, node, port, dlen);
  800. return rc ? rc : dlen;
  801. }
  802. /**
  803. * tipc_send_group_anycast - send message to any member with given identity
  804. * @sock: socket structure
  805. * @m: message to send
  806. * @dlen: total length of message data
  807. * @timeout: timeout to wait for wakeup
  808. *
  809. * Called from function tipc_sendmsg(), which has done all sanity checks
  810. * Returns the number of bytes sent on success, or errno
  811. */
  812. static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
  813. int dlen, long timeout)
  814. {
  815. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  816. struct sock *sk = sock->sk;
  817. struct tipc_sock *tsk = tipc_sk(sk);
  818. struct list_head *cong_links = &tsk->cong_links;
  819. int blks = tsk_blocks(GROUP_H_SIZE + dlen);
  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. while (++lookups < 4) {
  835. exclude = tipc_group_exclude(tsk->group);
  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(tsk->group, node, port, blks,
  844. &mbr);
  845. if (!cong)
  846. break;
  847. if (mbr == first)
  848. break;
  849. if (!first)
  850. first = mbr;
  851. }
  852. /* Start over if destination was not in member list */
  853. if (unlikely(!mbr))
  854. continue;
  855. if (likely(!cong && !tipc_dest_find(cong_links, node, 0)))
  856. break;
  857. /* Block or return if destination link or member is congested */
  858. rc = tipc_wait_for_cond(sock, &timeout,
  859. !tipc_dest_find(cong_links, node, 0) &&
  860. tsk->group &&
  861. !tipc_group_cong(tsk->group, node, port,
  862. blks, &mbr));
  863. if (unlikely(rc))
  864. return rc;
  865. /* Send, unless destination disappeared while waiting */
  866. if (likely(mbr))
  867. break;
  868. }
  869. if (unlikely(lookups >= 4))
  870. return -EHOSTUNREACH;
  871. rc = tipc_send_group_msg(net, tsk, m, mbr, node, port, dlen);
  872. return rc ? rc : dlen;
  873. }
  874. /**
  875. * tipc_send_group_bcast - send message to all members in communication group
  876. * @sk: socket structure
  877. * @m: message to send
  878. * @dlen: total length of message data
  879. * @timeout: timeout to wait for wakeup
  880. *
  881. * Called from function tipc_sendmsg(), which has done all sanity checks
  882. * Returns the number of bytes sent on success, or errno
  883. */
  884. static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
  885. int dlen, long timeout)
  886. {
  887. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  888. struct sock *sk = sock->sk;
  889. struct net *net = sock_net(sk);
  890. struct tipc_sock *tsk = tipc_sk(sk);
  891. struct tipc_nlist *dsts = tipc_group_dests(tsk->group);
  892. struct tipc_mc_method *method = &tsk->mc_method;
  893. bool ack = method->mandatory && method->rcast;
  894. int blks = tsk_blocks(MCAST_H_SIZE + dlen);
  895. struct tipc_msg *hdr = &tsk->phdr;
  896. int mtu = tipc_bcast_get_mtu(net);
  897. struct sk_buff_head pkts;
  898. int rc = -EHOSTUNREACH;
  899. if (!dsts->local && !dsts->remote)
  900. return -EHOSTUNREACH;
  901. /* Block or return if any destination link or member is congested */
  902. rc = tipc_wait_for_cond(sock, &timeout,
  903. !tsk->cong_link_cnt && tsk->group &&
  904. !tipc_group_bc_cong(tsk->group, blks));
  905. if (unlikely(rc))
  906. return rc;
  907. /* Complete message header */
  908. if (dest) {
  909. msg_set_type(hdr, TIPC_GRP_MCAST_MSG);
  910. msg_set_nameinst(hdr, dest->addr.name.name.instance);
  911. } else {
  912. msg_set_type(hdr, TIPC_GRP_BCAST_MSG);
  913. msg_set_nameinst(hdr, 0);
  914. }
  915. msg_set_hdr_sz(hdr, GROUP_H_SIZE);
  916. msg_set_destport(hdr, 0);
  917. msg_set_destnode(hdr, 0);
  918. msg_set_grp_bc_seqno(hdr, tipc_group_bc_snd_nxt(tsk->group));
  919. /* Avoid getting stuck with repeated forced replicasts */
  920. msg_set_grp_bc_ack_req(hdr, ack);
  921. /* Build message as chain of buffers */
  922. skb_queue_head_init(&pkts);
  923. rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
  924. if (unlikely(rc != dlen))
  925. return rc;
  926. /* Send message */
  927. rc = tipc_mcast_xmit(net, &pkts, method, dsts, &tsk->cong_link_cnt);
  928. if (unlikely(rc))
  929. return rc;
  930. /* Update broadcast sequence number and send windows */
  931. tipc_group_update_bc_members(tsk->group, blks, ack);
  932. /* Broadcast link is now free to choose method for next broadcast */
  933. method->mandatory = false;
  934. method->expires = jiffies;
  935. return dlen;
  936. }
  937. /**
  938. * tipc_send_group_mcast - send message to all members with given identity
  939. * @sock: socket structure
  940. * @m: message to send
  941. * @dlen: total length of message data
  942. * @timeout: timeout to wait for wakeup
  943. *
  944. * Called from function tipc_sendmsg(), which has done all sanity checks
  945. * Returns the number of bytes sent on success, or errno
  946. */
  947. static int tipc_send_group_mcast(struct socket *sock, struct msghdr *m,
  948. int dlen, long timeout)
  949. {
  950. struct sock *sk = sock->sk;
  951. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  952. struct tipc_sock *tsk = tipc_sk(sk);
  953. struct tipc_group *grp = tsk->group;
  954. struct tipc_msg *hdr = &tsk->phdr;
  955. struct net *net = sock_net(sk);
  956. u32 type, inst, scope, exclude;
  957. struct list_head dsts;
  958. u32 dstcnt;
  959. INIT_LIST_HEAD(&dsts);
  960. type = msg_nametype(hdr);
  961. inst = dest->addr.name.name.instance;
  962. scope = msg_lookup_scope(hdr);
  963. exclude = tipc_group_exclude(grp);
  964. if (!tipc_nametbl_lookup(net, type, inst, scope, &dsts,
  965. &dstcnt, exclude, true))
  966. return -EHOSTUNREACH;
  967. if (dstcnt == 1) {
  968. tipc_dest_pop(&dsts, &dest->addr.id.node, &dest->addr.id.ref);
  969. return tipc_send_group_unicast(sock, m, dlen, timeout);
  970. }
  971. tipc_dest_list_purge(&dsts);
  972. return tipc_send_group_bcast(sock, m, dlen, timeout);
  973. }
  974. /**
  975. * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
  976. * @arrvq: queue with arriving messages, to be cloned after destination lookup
  977. * @inputq: queue with cloned messages, delivered to socket after dest lookup
  978. *
  979. * Multi-threaded: parallel calls with reference to same queues may occur
  980. */
  981. void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
  982. struct sk_buff_head *inputq)
  983. {
  984. u32 self = tipc_own_addr(net);
  985. u32 type, lower, upper, scope;
  986. struct sk_buff *skb, *_skb;
  987. u32 portid, onode;
  988. struct sk_buff_head tmpq;
  989. struct list_head dports;
  990. struct tipc_msg *hdr;
  991. int user, mtyp, hlen;
  992. bool exact;
  993. __skb_queue_head_init(&tmpq);
  994. INIT_LIST_HEAD(&dports);
  995. skb = tipc_skb_peek(arrvq, &inputq->lock);
  996. for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
  997. hdr = buf_msg(skb);
  998. user = msg_user(hdr);
  999. mtyp = msg_type(hdr);
  1000. hlen = skb_headroom(skb) + msg_hdr_sz(hdr);
  1001. onode = msg_orignode(hdr);
  1002. type = msg_nametype(hdr);
  1003. if (mtyp == TIPC_GRP_UCAST_MSG || user == GROUP_PROTOCOL) {
  1004. spin_lock_bh(&inputq->lock);
  1005. if (skb_peek(arrvq) == skb) {
  1006. __skb_dequeue(arrvq);
  1007. __skb_queue_tail(inputq, skb);
  1008. }
  1009. kfree_skb(skb);
  1010. spin_unlock_bh(&inputq->lock);
  1011. continue;
  1012. }
  1013. /* Group messages require exact scope match */
  1014. if (msg_in_group(hdr)) {
  1015. lower = 0;
  1016. upper = ~0;
  1017. scope = msg_lookup_scope(hdr);
  1018. exact = true;
  1019. } else {
  1020. /* TIPC_NODE_SCOPE means "any scope" in this context */
  1021. if (onode == self)
  1022. scope = TIPC_NODE_SCOPE;
  1023. else
  1024. scope = TIPC_CLUSTER_SCOPE;
  1025. exact = false;
  1026. lower = msg_namelower(hdr);
  1027. upper = msg_nameupper(hdr);
  1028. }
  1029. /* Create destination port list: */
  1030. tipc_nametbl_mc_lookup(net, type, lower, upper,
  1031. scope, exact, &dports);
  1032. /* Clone message per destination */
  1033. while (tipc_dest_pop(&dports, NULL, &portid)) {
  1034. _skb = __pskb_copy(skb, hlen, GFP_ATOMIC);
  1035. if (_skb) {
  1036. msg_set_destport(buf_msg(_skb), portid);
  1037. __skb_queue_tail(&tmpq, _skb);
  1038. continue;
  1039. }
  1040. pr_warn("Failed to clone mcast rcv buffer\n");
  1041. }
  1042. /* Append to inputq if not already done by other thread */
  1043. spin_lock_bh(&inputq->lock);
  1044. if (skb_peek(arrvq) == skb) {
  1045. skb_queue_splice_tail_init(&tmpq, inputq);
  1046. kfree_skb(__skb_dequeue(arrvq));
  1047. }
  1048. spin_unlock_bh(&inputq->lock);
  1049. __skb_queue_purge(&tmpq);
  1050. kfree_skb(skb);
  1051. }
  1052. tipc_sk_rcv(net, inputq);
  1053. }
  1054. /**
  1055. * tipc_sk_conn_proto_rcv - receive a connection mng protocol message
  1056. * @tsk: receiving socket
  1057. * @skb: pointer to message buffer.
  1058. */
  1059. static void tipc_sk_conn_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
  1060. struct sk_buff_head *inputq,
  1061. struct sk_buff_head *xmitq)
  1062. {
  1063. struct tipc_msg *hdr = buf_msg(skb);
  1064. u32 onode = tsk_own_node(tsk);
  1065. struct sock *sk = &tsk->sk;
  1066. int mtyp = msg_type(hdr);
  1067. bool conn_cong;
  1068. /* Ignore if connection cannot be validated: */
  1069. if (!tsk_peer_msg(tsk, hdr))
  1070. goto exit;
  1071. if (unlikely(msg_errcode(hdr))) {
  1072. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1073. tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
  1074. tsk_peer_port(tsk));
  1075. sk->sk_state_change(sk);
  1076. /* State change is ignored if socket already awake,
  1077. * - convert msg to abort msg and add to inqueue
  1078. */
  1079. msg_set_user(hdr, TIPC_CRITICAL_IMPORTANCE);
  1080. msg_set_type(hdr, TIPC_CONN_MSG);
  1081. msg_set_size(hdr, BASIC_H_SIZE);
  1082. msg_set_hdr_sz(hdr, BASIC_H_SIZE);
  1083. __skb_queue_tail(inputq, skb);
  1084. return;
  1085. }
  1086. tsk->probe_unacked = false;
  1087. if (mtyp == CONN_PROBE) {
  1088. msg_set_type(hdr, CONN_PROBE_REPLY);
  1089. if (tipc_msg_reverse(onode, &skb, TIPC_OK))
  1090. __skb_queue_tail(xmitq, skb);
  1091. return;
  1092. } else if (mtyp == CONN_ACK) {
  1093. conn_cong = tsk_conn_cong(tsk);
  1094. tsk->snt_unacked -= msg_conn_ack(hdr);
  1095. if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
  1096. tsk->snd_win = msg_adv_win(hdr);
  1097. if (conn_cong)
  1098. sk->sk_write_space(sk);
  1099. } else if (mtyp != CONN_PROBE_REPLY) {
  1100. pr_warn("Received unknown CONN_PROTO msg\n");
  1101. }
  1102. exit:
  1103. kfree_skb(skb);
  1104. }
  1105. /**
  1106. * tipc_sendmsg - send message in connectionless manner
  1107. * @sock: socket structure
  1108. * @m: message to send
  1109. * @dsz: amount of user data to be sent
  1110. *
  1111. * Message must have an destination specified explicitly.
  1112. * Used for SOCK_RDM and SOCK_DGRAM messages,
  1113. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  1114. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  1115. *
  1116. * Returns the number of bytes sent on success, or errno otherwise
  1117. */
  1118. static int tipc_sendmsg(struct socket *sock,
  1119. struct msghdr *m, size_t dsz)
  1120. {
  1121. struct sock *sk = sock->sk;
  1122. int ret;
  1123. lock_sock(sk);
  1124. ret = __tipc_sendmsg(sock, m, dsz);
  1125. release_sock(sk);
  1126. return ret;
  1127. }
  1128. static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
  1129. {
  1130. struct sock *sk = sock->sk;
  1131. struct net *net = sock_net(sk);
  1132. struct tipc_sock *tsk = tipc_sk(sk);
  1133. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  1134. long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  1135. struct list_head *clinks = &tsk->cong_links;
  1136. bool syn = !tipc_sk_type_connectionless(sk);
  1137. struct tipc_group *grp = tsk->group;
  1138. struct tipc_msg *hdr = &tsk->phdr;
  1139. struct tipc_name_seq *seq;
  1140. struct sk_buff_head pkts;
  1141. u32 dport, dnode = 0;
  1142. u32 type, inst;
  1143. int mtu, rc;
  1144. if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
  1145. return -EMSGSIZE;
  1146. if (likely(dest)) {
  1147. if (unlikely(m->msg_namelen < sizeof(*dest)))
  1148. return -EINVAL;
  1149. if (unlikely(dest->family != AF_TIPC))
  1150. return -EINVAL;
  1151. }
  1152. if (grp) {
  1153. if (!dest)
  1154. return tipc_send_group_bcast(sock, m, dlen, timeout);
  1155. if (dest->addrtype == TIPC_ADDR_NAME)
  1156. return tipc_send_group_anycast(sock, m, dlen, timeout);
  1157. if (dest->addrtype == TIPC_ADDR_ID)
  1158. return tipc_send_group_unicast(sock, m, dlen, timeout);
  1159. if (dest->addrtype == TIPC_ADDR_MCAST)
  1160. return tipc_send_group_mcast(sock, m, dlen, timeout);
  1161. return -EINVAL;
  1162. }
  1163. if (unlikely(!dest)) {
  1164. dest = &tsk->peer;
  1165. if (!syn || dest->family != AF_TIPC)
  1166. return -EDESTADDRREQ;
  1167. }
  1168. if (unlikely(syn)) {
  1169. if (sk->sk_state == TIPC_LISTEN)
  1170. return -EPIPE;
  1171. if (sk->sk_state != TIPC_OPEN)
  1172. return -EISCONN;
  1173. if (tsk->published)
  1174. return -EOPNOTSUPP;
  1175. if (dest->addrtype == TIPC_ADDR_NAME) {
  1176. tsk->conn_type = dest->addr.name.name.type;
  1177. tsk->conn_instance = dest->addr.name.name.instance;
  1178. }
  1179. }
  1180. seq = &dest->addr.nameseq;
  1181. if (dest->addrtype == TIPC_ADDR_MCAST)
  1182. return tipc_sendmcast(sock, seq, m, dlen, timeout);
  1183. if (dest->addrtype == TIPC_ADDR_NAME) {
  1184. type = dest->addr.name.name.type;
  1185. inst = dest->addr.name.name.instance;
  1186. dnode = dest->addr.name.domain;
  1187. msg_set_type(hdr, TIPC_NAMED_MSG);
  1188. msg_set_hdr_sz(hdr, NAMED_H_SIZE);
  1189. msg_set_nametype(hdr, type);
  1190. msg_set_nameinst(hdr, inst);
  1191. msg_set_lookup_scope(hdr, tipc_node2scope(dnode));
  1192. dport = tipc_nametbl_translate(net, type, inst, &dnode);
  1193. msg_set_destnode(hdr, dnode);
  1194. msg_set_destport(hdr, dport);
  1195. if (unlikely(!dport && !dnode))
  1196. return -EHOSTUNREACH;
  1197. } else if (dest->addrtype == TIPC_ADDR_ID) {
  1198. dnode = dest->addr.id.node;
  1199. msg_set_type(hdr, TIPC_DIRECT_MSG);
  1200. msg_set_lookup_scope(hdr, 0);
  1201. msg_set_destnode(hdr, dnode);
  1202. msg_set_destport(hdr, dest->addr.id.ref);
  1203. msg_set_hdr_sz(hdr, BASIC_H_SIZE);
  1204. } else {
  1205. return -EINVAL;
  1206. }
  1207. /* Block or return if destination link is congested */
  1208. rc = tipc_wait_for_cond(sock, &timeout,
  1209. !tipc_dest_find(clinks, dnode, 0));
  1210. if (unlikely(rc))
  1211. return rc;
  1212. skb_queue_head_init(&pkts);
  1213. mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
  1214. rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
  1215. if (unlikely(rc != dlen))
  1216. return rc;
  1217. rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
  1218. if (unlikely(rc == -ELINKCONG)) {
  1219. tipc_dest_push(clinks, dnode, 0);
  1220. tsk->cong_link_cnt++;
  1221. rc = 0;
  1222. }
  1223. if (unlikely(syn && !rc))
  1224. tipc_set_sk_state(sk, TIPC_CONNECTING);
  1225. return rc ? rc : dlen;
  1226. }
  1227. /**
  1228. * tipc_sendstream - send stream-oriented data
  1229. * @sock: socket structure
  1230. * @m: data to send
  1231. * @dsz: total length of data to be transmitted
  1232. *
  1233. * Used for SOCK_STREAM data.
  1234. *
  1235. * Returns the number of bytes sent on success (or partial success),
  1236. * or errno if no data sent
  1237. */
  1238. static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
  1239. {
  1240. struct sock *sk = sock->sk;
  1241. int ret;
  1242. lock_sock(sk);
  1243. ret = __tipc_sendstream(sock, m, dsz);
  1244. release_sock(sk);
  1245. return ret;
  1246. }
  1247. static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
  1248. {
  1249. struct sock *sk = sock->sk;
  1250. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  1251. long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  1252. struct tipc_sock *tsk = tipc_sk(sk);
  1253. struct tipc_msg *hdr = &tsk->phdr;
  1254. struct net *net = sock_net(sk);
  1255. struct sk_buff_head pkts;
  1256. u32 dnode = tsk_peer_node(tsk);
  1257. int send, sent = 0;
  1258. int rc = 0;
  1259. skb_queue_head_init(&pkts);
  1260. if (unlikely(dlen > INT_MAX))
  1261. return -EMSGSIZE;
  1262. /* Handle implicit connection setup */
  1263. if (unlikely(dest)) {
  1264. rc = __tipc_sendmsg(sock, m, dlen);
  1265. if (dlen && dlen == rc) {
  1266. tsk->peer_caps = tipc_node_get_capabilities(net, dnode);
  1267. tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
  1268. }
  1269. return rc;
  1270. }
  1271. do {
  1272. rc = tipc_wait_for_cond(sock, &timeout,
  1273. (!tsk->cong_link_cnt &&
  1274. !tsk_conn_cong(tsk) &&
  1275. tipc_sk_connected(sk)));
  1276. if (unlikely(rc))
  1277. break;
  1278. send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
  1279. rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
  1280. if (unlikely(rc != send))
  1281. break;
  1282. rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
  1283. if (unlikely(rc == -ELINKCONG)) {
  1284. tsk->cong_link_cnt = 1;
  1285. rc = 0;
  1286. }
  1287. if (likely(!rc)) {
  1288. tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
  1289. sent += send;
  1290. }
  1291. } while (sent < dlen && !rc);
  1292. return sent ? sent : rc;
  1293. }
  1294. /**
  1295. * tipc_send_packet - send a connection-oriented message
  1296. * @sock: socket structure
  1297. * @m: message to send
  1298. * @dsz: length of data to be transmitted
  1299. *
  1300. * Used for SOCK_SEQPACKET messages.
  1301. *
  1302. * Returns the number of bytes sent on success, or errno otherwise
  1303. */
  1304. static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
  1305. {
  1306. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  1307. return -EMSGSIZE;
  1308. return tipc_sendstream(sock, m, dsz);
  1309. }
  1310. /* tipc_sk_finish_conn - complete the setup of a connection
  1311. */
  1312. static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
  1313. u32 peer_node)
  1314. {
  1315. struct sock *sk = &tsk->sk;
  1316. struct net *net = sock_net(sk);
  1317. struct tipc_msg *msg = &tsk->phdr;
  1318. msg_set_destnode(msg, peer_node);
  1319. msg_set_destport(msg, peer_port);
  1320. msg_set_type(msg, TIPC_CONN_MSG);
  1321. msg_set_lookup_scope(msg, 0);
  1322. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  1323. sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
  1324. tipc_set_sk_state(sk, TIPC_ESTABLISHED);
  1325. tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
  1326. tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
  1327. tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
  1328. if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
  1329. return;
  1330. /* Fall back to message based flow control */
  1331. tsk->rcv_win = FLOWCTL_MSG_WIN;
  1332. tsk->snd_win = FLOWCTL_MSG_WIN;
  1333. }
  1334. /**
  1335. * tipc_sk_set_orig_addr - capture sender's address for received message
  1336. * @m: descriptor for message info
  1337. * @hdr: received message header
  1338. *
  1339. * Note: Address is not captured if not requested by receiver.
  1340. */
  1341. static void tipc_sk_set_orig_addr(struct msghdr *m, struct sk_buff *skb)
  1342. {
  1343. DECLARE_SOCKADDR(struct sockaddr_pair *, srcaddr, m->msg_name);
  1344. struct tipc_msg *hdr = buf_msg(skb);
  1345. if (!srcaddr)
  1346. return;
  1347. srcaddr->sock.family = AF_TIPC;
  1348. srcaddr->sock.addrtype = TIPC_ADDR_ID;
  1349. srcaddr->sock.scope = 0;
  1350. srcaddr->sock.addr.id.ref = msg_origport(hdr);
  1351. srcaddr->sock.addr.id.node = msg_orignode(hdr);
  1352. srcaddr->sock.addr.name.domain = 0;
  1353. m->msg_namelen = sizeof(struct sockaddr_tipc);
  1354. if (!msg_in_group(hdr))
  1355. return;
  1356. /* Group message users may also want to know sending member's id */
  1357. srcaddr->member.family = AF_TIPC;
  1358. srcaddr->member.addrtype = TIPC_ADDR_NAME;
  1359. srcaddr->member.scope = 0;
  1360. srcaddr->member.addr.name.name.type = msg_nametype(hdr);
  1361. srcaddr->member.addr.name.name.instance = TIPC_SKB_CB(skb)->orig_member;
  1362. srcaddr->member.addr.name.domain = 0;
  1363. m->msg_namelen = sizeof(*srcaddr);
  1364. }
  1365. /**
  1366. * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
  1367. * @m: descriptor for message info
  1368. * @skb: received message buffer
  1369. * @tsk: TIPC port associated with message
  1370. *
  1371. * Note: Ancillary data is not captured if not requested by receiver.
  1372. *
  1373. * Returns 0 if successful, otherwise errno
  1374. */
  1375. static int tipc_sk_anc_data_recv(struct msghdr *m, struct sk_buff *skb,
  1376. struct tipc_sock *tsk)
  1377. {
  1378. struct tipc_msg *msg;
  1379. u32 anc_data[3];
  1380. u32 err;
  1381. u32 dest_type;
  1382. int has_name;
  1383. int res;
  1384. if (likely(m->msg_controllen == 0))
  1385. return 0;
  1386. msg = buf_msg(skb);
  1387. /* Optionally capture errored message object(s) */
  1388. err = msg ? msg_errcode(msg) : 0;
  1389. if (unlikely(err)) {
  1390. anc_data[0] = err;
  1391. anc_data[1] = msg_data_sz(msg);
  1392. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  1393. if (res)
  1394. return res;
  1395. if (anc_data[1]) {
  1396. if (skb_linearize(skb))
  1397. return -ENOMEM;
  1398. msg = buf_msg(skb);
  1399. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  1400. msg_data(msg));
  1401. if (res)
  1402. return res;
  1403. }
  1404. }
  1405. /* Optionally capture message destination object */
  1406. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  1407. switch (dest_type) {
  1408. case TIPC_NAMED_MSG:
  1409. has_name = 1;
  1410. anc_data[0] = msg_nametype(msg);
  1411. anc_data[1] = msg_namelower(msg);
  1412. anc_data[2] = msg_namelower(msg);
  1413. break;
  1414. case TIPC_MCAST_MSG:
  1415. has_name = 1;
  1416. anc_data[0] = msg_nametype(msg);
  1417. anc_data[1] = msg_namelower(msg);
  1418. anc_data[2] = msg_nameupper(msg);
  1419. break;
  1420. case TIPC_CONN_MSG:
  1421. has_name = (tsk->conn_type != 0);
  1422. anc_data[0] = tsk->conn_type;
  1423. anc_data[1] = tsk->conn_instance;
  1424. anc_data[2] = tsk->conn_instance;
  1425. break;
  1426. default:
  1427. has_name = 0;
  1428. }
  1429. if (has_name) {
  1430. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  1431. if (res)
  1432. return res;
  1433. }
  1434. return 0;
  1435. }
  1436. static void tipc_sk_send_ack(struct tipc_sock *tsk)
  1437. {
  1438. struct sock *sk = &tsk->sk;
  1439. struct net *net = sock_net(sk);
  1440. struct sk_buff *skb = NULL;
  1441. struct tipc_msg *msg;
  1442. u32 peer_port = tsk_peer_port(tsk);
  1443. u32 dnode = tsk_peer_node(tsk);
  1444. if (!tipc_sk_connected(sk))
  1445. return;
  1446. skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
  1447. dnode, tsk_own_node(tsk), peer_port,
  1448. tsk->portid, TIPC_OK);
  1449. if (!skb)
  1450. return;
  1451. msg = buf_msg(skb);
  1452. msg_set_conn_ack(msg, tsk->rcv_unacked);
  1453. tsk->rcv_unacked = 0;
  1454. /* Adjust to and advertize the correct window limit */
  1455. if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
  1456. tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
  1457. msg_set_adv_win(msg, tsk->rcv_win);
  1458. }
  1459. tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
  1460. }
  1461. static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
  1462. {
  1463. struct sock *sk = sock->sk;
  1464. DEFINE_WAIT(wait);
  1465. long timeo = *timeop;
  1466. int err = sock_error(sk);
  1467. if (err)
  1468. return err;
  1469. for (;;) {
  1470. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1471. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1472. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  1473. err = -ENOTCONN;
  1474. break;
  1475. }
  1476. release_sock(sk);
  1477. timeo = schedule_timeout(timeo);
  1478. lock_sock(sk);
  1479. }
  1480. err = 0;
  1481. if (!skb_queue_empty(&sk->sk_receive_queue))
  1482. break;
  1483. err = -EAGAIN;
  1484. if (!timeo)
  1485. break;
  1486. err = sock_intr_errno(timeo);
  1487. if (signal_pending(current))
  1488. break;
  1489. err = sock_error(sk);
  1490. if (err)
  1491. break;
  1492. }
  1493. finish_wait(sk_sleep(sk), &wait);
  1494. *timeop = timeo;
  1495. return err;
  1496. }
  1497. /**
  1498. * tipc_recvmsg - receive packet-oriented message
  1499. * @m: descriptor for message info
  1500. * @buflen: length of user buffer area
  1501. * @flags: receive flags
  1502. *
  1503. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  1504. * If the complete message doesn't fit in user area, truncate it.
  1505. *
  1506. * Returns size of returned message data, errno otherwise
  1507. */
  1508. static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
  1509. size_t buflen, int flags)
  1510. {
  1511. struct sock *sk = sock->sk;
  1512. bool connected = !tipc_sk_type_connectionless(sk);
  1513. struct tipc_sock *tsk = tipc_sk(sk);
  1514. int rc, err, hlen, dlen, copy;
  1515. struct sk_buff_head xmitq;
  1516. struct tipc_msg *hdr;
  1517. struct sk_buff *skb;
  1518. bool grp_evt;
  1519. long timeout;
  1520. /* Catch invalid receive requests */
  1521. if (unlikely(!buflen))
  1522. return -EINVAL;
  1523. lock_sock(sk);
  1524. if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
  1525. rc = -ENOTCONN;
  1526. goto exit;
  1527. }
  1528. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1529. /* Step rcv queue to first msg with data or error; wait if necessary */
  1530. do {
  1531. rc = tipc_wait_for_rcvmsg(sock, &timeout);
  1532. if (unlikely(rc))
  1533. goto exit;
  1534. skb = skb_peek(&sk->sk_receive_queue);
  1535. hdr = buf_msg(skb);
  1536. dlen = msg_data_sz(hdr);
  1537. hlen = msg_hdr_sz(hdr);
  1538. err = msg_errcode(hdr);
  1539. grp_evt = msg_is_grp_evt(hdr);
  1540. if (likely(dlen || err))
  1541. break;
  1542. tsk_advance_rx_queue(sk);
  1543. } while (1);
  1544. /* Collect msg meta data, including error code and rejected data */
  1545. tipc_sk_set_orig_addr(m, skb);
  1546. rc = tipc_sk_anc_data_recv(m, skb, tsk);
  1547. if (unlikely(rc))
  1548. goto exit;
  1549. hdr = buf_msg(skb);
  1550. /* Capture data if non-error msg, otherwise just set return value */
  1551. if (likely(!err)) {
  1552. copy = min_t(int, dlen, buflen);
  1553. if (unlikely(copy != dlen))
  1554. m->msg_flags |= MSG_TRUNC;
  1555. rc = skb_copy_datagram_msg(skb, hlen, m, copy);
  1556. } else {
  1557. copy = 0;
  1558. rc = 0;
  1559. if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control)
  1560. rc = -ECONNRESET;
  1561. }
  1562. if (unlikely(rc))
  1563. goto exit;
  1564. /* Mark message as group event if applicable */
  1565. if (unlikely(grp_evt)) {
  1566. if (msg_grp_evt(hdr) == TIPC_WITHDRAWN)
  1567. m->msg_flags |= MSG_EOR;
  1568. m->msg_flags |= MSG_OOB;
  1569. copy = 0;
  1570. }
  1571. /* Caption of data or error code/rejected data was successful */
  1572. if (unlikely(flags & MSG_PEEK))
  1573. goto exit;
  1574. /* Send group flow control advertisement when applicable */
  1575. if (tsk->group && msg_in_group(hdr) && !grp_evt) {
  1576. skb_queue_head_init(&xmitq);
  1577. tipc_group_update_rcv_win(tsk->group, tsk_blocks(hlen + dlen),
  1578. msg_orignode(hdr), msg_origport(hdr),
  1579. &xmitq);
  1580. tipc_node_distr_xmit(sock_net(sk), &xmitq);
  1581. }
  1582. tsk_advance_rx_queue(sk);
  1583. if (likely(!connected))
  1584. goto exit;
  1585. /* Send connection flow control advertisement when applicable */
  1586. tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
  1587. if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
  1588. tipc_sk_send_ack(tsk);
  1589. exit:
  1590. release_sock(sk);
  1591. return rc ? rc : copy;
  1592. }
  1593. /**
  1594. * tipc_recvstream - receive stream-oriented data
  1595. * @m: descriptor for message info
  1596. * @buflen: total size of user buffer area
  1597. * @flags: receive flags
  1598. *
  1599. * Used for SOCK_STREAM messages only. If not enough data is available
  1600. * will optionally wait for more; never truncates data.
  1601. *
  1602. * Returns size of returned message data, errno otherwise
  1603. */
  1604. static int tipc_recvstream(struct socket *sock, struct msghdr *m,
  1605. size_t buflen, int flags)
  1606. {
  1607. struct sock *sk = sock->sk;
  1608. struct tipc_sock *tsk = tipc_sk(sk);
  1609. struct sk_buff *skb;
  1610. struct tipc_msg *hdr;
  1611. struct tipc_skb_cb *skb_cb;
  1612. bool peek = flags & MSG_PEEK;
  1613. int offset, required, copy, copied = 0;
  1614. int hlen, dlen, err, rc;
  1615. long timeout;
  1616. /* Catch invalid receive attempts */
  1617. if (unlikely(!buflen))
  1618. return -EINVAL;
  1619. lock_sock(sk);
  1620. if (unlikely(sk->sk_state == TIPC_OPEN)) {
  1621. rc = -ENOTCONN;
  1622. goto exit;
  1623. }
  1624. required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
  1625. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1626. do {
  1627. /* Look at first msg in receive queue; wait if necessary */
  1628. rc = tipc_wait_for_rcvmsg(sock, &timeout);
  1629. if (unlikely(rc))
  1630. break;
  1631. skb = skb_peek(&sk->sk_receive_queue);
  1632. skb_cb = TIPC_SKB_CB(skb);
  1633. hdr = buf_msg(skb);
  1634. dlen = msg_data_sz(hdr);
  1635. hlen = msg_hdr_sz(hdr);
  1636. err = msg_errcode(hdr);
  1637. /* Discard any empty non-errored (SYN-) message */
  1638. if (unlikely(!dlen && !err)) {
  1639. tsk_advance_rx_queue(sk);
  1640. continue;
  1641. }
  1642. /* Collect msg meta data, incl. error code and rejected data */
  1643. if (!copied) {
  1644. tipc_sk_set_orig_addr(m, skb);
  1645. rc = tipc_sk_anc_data_recv(m, skb, tsk);
  1646. if (rc)
  1647. break;
  1648. hdr = buf_msg(skb);
  1649. }
  1650. /* Copy data if msg ok, otherwise return error/partial data */
  1651. if (likely(!err)) {
  1652. offset = skb_cb->bytes_read;
  1653. copy = min_t(int, dlen - offset, buflen - copied);
  1654. rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
  1655. if (unlikely(rc))
  1656. break;
  1657. copied += copy;
  1658. offset += copy;
  1659. if (unlikely(offset < dlen)) {
  1660. if (!peek)
  1661. skb_cb->bytes_read = offset;
  1662. break;
  1663. }
  1664. } else {
  1665. rc = 0;
  1666. if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
  1667. rc = -ECONNRESET;
  1668. if (copied || rc)
  1669. break;
  1670. }
  1671. if (unlikely(peek))
  1672. break;
  1673. tsk_advance_rx_queue(sk);
  1674. /* Send connection flow control advertisement when applicable */
  1675. tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
  1676. if (unlikely(tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE))
  1677. tipc_sk_send_ack(tsk);
  1678. /* Exit if all requested data or FIN/error received */
  1679. if (copied == buflen || err)
  1680. break;
  1681. } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
  1682. exit:
  1683. release_sock(sk);
  1684. return copied ? copied : rc;
  1685. }
  1686. /**
  1687. * tipc_write_space - wake up thread if port congestion is released
  1688. * @sk: socket
  1689. */
  1690. static void tipc_write_space(struct sock *sk)
  1691. {
  1692. struct socket_wq *wq;
  1693. rcu_read_lock();
  1694. wq = rcu_dereference(sk->sk_wq);
  1695. if (skwq_has_sleeper(wq))
  1696. wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
  1697. EPOLLWRNORM | EPOLLWRBAND);
  1698. rcu_read_unlock();
  1699. }
  1700. /**
  1701. * tipc_data_ready - wake up threads to indicate messages have been received
  1702. * @sk: socket
  1703. * @len: the length of messages
  1704. */
  1705. static void tipc_data_ready(struct sock *sk)
  1706. {
  1707. struct socket_wq *wq;
  1708. rcu_read_lock();
  1709. wq = rcu_dereference(sk->sk_wq);
  1710. if (skwq_has_sleeper(wq))
  1711. wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
  1712. EPOLLRDNORM | EPOLLRDBAND);
  1713. rcu_read_unlock();
  1714. }
  1715. static void tipc_sock_destruct(struct sock *sk)
  1716. {
  1717. __skb_queue_purge(&sk->sk_receive_queue);
  1718. }
  1719. static void tipc_sk_proto_rcv(struct sock *sk,
  1720. struct sk_buff_head *inputq,
  1721. struct sk_buff_head *xmitq)
  1722. {
  1723. struct sk_buff *skb = __skb_dequeue(inputq);
  1724. struct tipc_sock *tsk = tipc_sk(sk);
  1725. struct tipc_msg *hdr = buf_msg(skb);
  1726. struct tipc_group *grp = tsk->group;
  1727. bool wakeup = false;
  1728. switch (msg_user(hdr)) {
  1729. case CONN_MANAGER:
  1730. tipc_sk_conn_proto_rcv(tsk, skb, inputq, xmitq);
  1731. return;
  1732. case SOCK_WAKEUP:
  1733. tipc_dest_del(&tsk->cong_links, msg_orignode(hdr), 0);
  1734. tsk->cong_link_cnt--;
  1735. wakeup = true;
  1736. break;
  1737. case GROUP_PROTOCOL:
  1738. tipc_group_proto_rcv(grp, &wakeup, hdr, inputq, xmitq);
  1739. break;
  1740. case TOP_SRV:
  1741. tipc_group_member_evt(tsk->group, &wakeup, &sk->sk_rcvbuf,
  1742. hdr, inputq, xmitq);
  1743. break;
  1744. default:
  1745. break;
  1746. }
  1747. if (wakeup)
  1748. sk->sk_write_space(sk);
  1749. kfree_skb(skb);
  1750. }
  1751. /**
  1752. * tipc_filter_connect - Handle incoming message for a connection-based socket
  1753. * @tsk: TIPC socket
  1754. * @skb: pointer to message buffer. Set to NULL if buffer is consumed
  1755. *
  1756. * Returns true if everything ok, false otherwise
  1757. */
  1758. static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
  1759. {
  1760. struct sock *sk = &tsk->sk;
  1761. struct net *net = sock_net(sk);
  1762. struct tipc_msg *hdr = buf_msg(skb);
  1763. u32 pport = msg_origport(hdr);
  1764. u32 pnode = msg_orignode(hdr);
  1765. if (unlikely(msg_mcast(hdr)))
  1766. return false;
  1767. switch (sk->sk_state) {
  1768. case TIPC_CONNECTING:
  1769. /* Accept only ACK or NACK message */
  1770. if (unlikely(!msg_connected(hdr))) {
  1771. if (pport != tsk_peer_port(tsk) ||
  1772. pnode != tsk_peer_node(tsk))
  1773. return false;
  1774. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1775. sk->sk_err = ECONNREFUSED;
  1776. sk->sk_state_change(sk);
  1777. return true;
  1778. }
  1779. if (unlikely(msg_errcode(hdr))) {
  1780. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1781. sk->sk_err = ECONNREFUSED;
  1782. sk->sk_state_change(sk);
  1783. return true;
  1784. }
  1785. if (unlikely(!msg_isdata(hdr))) {
  1786. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1787. sk->sk_err = EINVAL;
  1788. sk->sk_state_change(sk);
  1789. return true;
  1790. }
  1791. tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
  1792. msg_set_importance(&tsk->phdr, msg_importance(hdr));
  1793. /* If 'ACK+' message, add to socket receive queue */
  1794. if (msg_data_sz(hdr))
  1795. return true;
  1796. /* If empty 'ACK-' message, wake up sleeping connect() */
  1797. sk->sk_data_ready(sk);
  1798. /* 'ACK-' message is neither accepted nor rejected: */
  1799. msg_set_dest_droppable(hdr, 1);
  1800. return false;
  1801. case TIPC_OPEN:
  1802. case TIPC_DISCONNECTING:
  1803. break;
  1804. case TIPC_LISTEN:
  1805. /* Accept only SYN message */
  1806. if (!msg_connected(hdr) && !(msg_errcode(hdr)))
  1807. return true;
  1808. break;
  1809. case TIPC_ESTABLISHED:
  1810. /* Accept only connection-based messages sent by peer */
  1811. if (unlikely(!tsk_peer_msg(tsk, hdr)))
  1812. return false;
  1813. if (unlikely(msg_errcode(hdr))) {
  1814. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1815. /* Let timer expire on it's own */
  1816. tipc_node_remove_conn(net, tsk_peer_node(tsk),
  1817. tsk->portid);
  1818. sk->sk_state_change(sk);
  1819. }
  1820. return true;
  1821. default:
  1822. pr_err("Unknown sk_state %u\n", sk->sk_state);
  1823. }
  1824. return false;
  1825. }
  1826. /**
  1827. * rcvbuf_limit - get proper overload limit of socket receive queue
  1828. * @sk: socket
  1829. * @skb: message
  1830. *
  1831. * For connection oriented messages, irrespective of importance,
  1832. * default queue limit is 2 MB.
  1833. *
  1834. * For connectionless messages, queue limits are based on message
  1835. * importance as follows:
  1836. *
  1837. * TIPC_LOW_IMPORTANCE (2 MB)
  1838. * TIPC_MEDIUM_IMPORTANCE (4 MB)
  1839. * TIPC_HIGH_IMPORTANCE (8 MB)
  1840. * TIPC_CRITICAL_IMPORTANCE (16 MB)
  1841. *
  1842. * Returns overload limit according to corresponding message importance
  1843. */
  1844. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
  1845. {
  1846. struct tipc_sock *tsk = tipc_sk(sk);
  1847. struct tipc_msg *hdr = buf_msg(skb);
  1848. if (unlikely(msg_in_group(hdr)))
  1849. return sk->sk_rcvbuf;
  1850. if (unlikely(!msg_connected(hdr)))
  1851. return sk->sk_rcvbuf << msg_importance(hdr);
  1852. if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
  1853. return sk->sk_rcvbuf;
  1854. return FLOWCTL_MSG_LIM;
  1855. }
  1856. /**
  1857. * tipc_sk_filter_rcv - validate incoming message
  1858. * @sk: socket
  1859. * @skb: pointer to message.
  1860. *
  1861. * Enqueues message on receive queue if acceptable; optionally handles
  1862. * disconnect indication for a connected socket.
  1863. *
  1864. * Called with socket lock already taken
  1865. *
  1866. */
  1867. static void tipc_sk_filter_rcv(struct sock *sk, struct sk_buff *skb,
  1868. struct sk_buff_head *xmitq)
  1869. {
  1870. bool sk_conn = !tipc_sk_type_connectionless(sk);
  1871. struct tipc_sock *tsk = tipc_sk(sk);
  1872. struct tipc_group *grp = tsk->group;
  1873. struct tipc_msg *hdr = buf_msg(skb);
  1874. struct net *net = sock_net(sk);
  1875. struct sk_buff_head inputq;
  1876. int limit, err = TIPC_OK;
  1877. TIPC_SKB_CB(skb)->bytes_read = 0;
  1878. __skb_queue_head_init(&inputq);
  1879. __skb_queue_tail(&inputq, skb);
  1880. if (unlikely(!msg_isdata(hdr)))
  1881. tipc_sk_proto_rcv(sk, &inputq, xmitq);
  1882. if (unlikely(grp))
  1883. tipc_group_filter_msg(grp, &inputq, xmitq);
  1884. /* Validate and add to receive buffer if there is space */
  1885. while ((skb = __skb_dequeue(&inputq))) {
  1886. hdr = buf_msg(skb);
  1887. limit = rcvbuf_limit(sk, skb);
  1888. if ((sk_conn && !tipc_sk_filter_connect(tsk, skb)) ||
  1889. (!sk_conn && msg_connected(hdr)) ||
  1890. (!grp && msg_in_group(hdr)))
  1891. err = TIPC_ERR_NO_PORT;
  1892. else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit) {
  1893. atomic_inc(&sk->sk_drops);
  1894. err = TIPC_ERR_OVERLOAD;
  1895. }
  1896. if (unlikely(err)) {
  1897. tipc_skb_reject(net, err, skb, xmitq);
  1898. err = TIPC_OK;
  1899. continue;
  1900. }
  1901. __skb_queue_tail(&sk->sk_receive_queue, skb);
  1902. skb_set_owner_r(skb, sk);
  1903. sk->sk_data_ready(sk);
  1904. }
  1905. }
  1906. /**
  1907. * tipc_sk_backlog_rcv - handle incoming message from backlog queue
  1908. * @sk: socket
  1909. * @skb: message
  1910. *
  1911. * Caller must hold socket lock
  1912. */
  1913. static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  1914. {
  1915. unsigned int before = sk_rmem_alloc_get(sk);
  1916. struct sk_buff_head xmitq;
  1917. unsigned int added;
  1918. __skb_queue_head_init(&xmitq);
  1919. tipc_sk_filter_rcv(sk, skb, &xmitq);
  1920. added = sk_rmem_alloc_get(sk) - before;
  1921. atomic_add(added, &tipc_sk(sk)->dupl_rcvcnt);
  1922. /* Send pending response/rejected messages, if any */
  1923. tipc_node_distr_xmit(sock_net(sk), &xmitq);
  1924. return 0;
  1925. }
  1926. /**
  1927. * tipc_sk_enqueue - extract all buffers with destination 'dport' from
  1928. * inputq and try adding them to socket or backlog queue
  1929. * @inputq: list of incoming buffers with potentially different destinations
  1930. * @sk: socket where the buffers should be enqueued
  1931. * @dport: port number for the socket
  1932. *
  1933. * Caller must hold socket lock
  1934. */
  1935. static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
  1936. u32 dport, struct sk_buff_head *xmitq)
  1937. {
  1938. unsigned long time_limit = jiffies + 2;
  1939. struct sk_buff *skb;
  1940. unsigned int lim;
  1941. atomic_t *dcnt;
  1942. u32 onode;
  1943. while (skb_queue_len(inputq)) {
  1944. if (unlikely(time_after_eq(jiffies, time_limit)))
  1945. return;
  1946. skb = tipc_skb_dequeue(inputq, dport);
  1947. if (unlikely(!skb))
  1948. return;
  1949. /* Add message directly to receive queue if possible */
  1950. if (!sock_owned_by_user(sk)) {
  1951. tipc_sk_filter_rcv(sk, skb, xmitq);
  1952. continue;
  1953. }
  1954. /* Try backlog, compensating for double-counted bytes */
  1955. dcnt = &tipc_sk(sk)->dupl_rcvcnt;
  1956. if (!sk->sk_backlog.len)
  1957. atomic_set(dcnt, 0);
  1958. lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
  1959. if (likely(!sk_add_backlog(sk, skb, lim)))
  1960. continue;
  1961. /* Overload => reject message back to sender */
  1962. onode = tipc_own_addr(sock_net(sk));
  1963. atomic_inc(&sk->sk_drops);
  1964. if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
  1965. __skb_queue_tail(xmitq, skb);
  1966. break;
  1967. }
  1968. }
  1969. /**
  1970. * tipc_sk_rcv - handle a chain of incoming buffers
  1971. * @inputq: buffer list containing the buffers
  1972. * Consumes all buffers in list until inputq is empty
  1973. * Note: may be called in multiple threads referring to the same queue
  1974. */
  1975. void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
  1976. {
  1977. struct sk_buff_head xmitq;
  1978. u32 dnode, dport = 0;
  1979. int err;
  1980. struct tipc_sock *tsk;
  1981. struct sock *sk;
  1982. struct sk_buff *skb;
  1983. __skb_queue_head_init(&xmitq);
  1984. while (skb_queue_len(inputq)) {
  1985. dport = tipc_skb_peek_port(inputq, dport);
  1986. tsk = tipc_sk_lookup(net, dport);
  1987. if (likely(tsk)) {
  1988. sk = &tsk->sk;
  1989. if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
  1990. tipc_sk_enqueue(inputq, sk, dport, &xmitq);
  1991. spin_unlock_bh(&sk->sk_lock.slock);
  1992. }
  1993. /* Send pending response/rejected messages, if any */
  1994. tipc_node_distr_xmit(sock_net(sk), &xmitq);
  1995. sock_put(sk);
  1996. continue;
  1997. }
  1998. /* No destination socket => dequeue skb if still there */
  1999. skb = tipc_skb_dequeue(inputq, dport);
  2000. if (!skb)
  2001. return;
  2002. /* Try secondary lookup if unresolved named message */
  2003. err = TIPC_ERR_NO_PORT;
  2004. if (tipc_msg_lookup_dest(net, skb, &err))
  2005. goto xmit;
  2006. /* Prepare for message rejection */
  2007. if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
  2008. continue;
  2009. xmit:
  2010. dnode = msg_destnode(buf_msg(skb));
  2011. tipc_node_xmit_skb(net, skb, dnode, dport);
  2012. }
  2013. }
  2014. static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
  2015. {
  2016. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  2017. struct sock *sk = sock->sk;
  2018. int done;
  2019. do {
  2020. int err = sock_error(sk);
  2021. if (err)
  2022. return err;
  2023. if (!*timeo_p)
  2024. return -ETIMEDOUT;
  2025. if (signal_pending(current))
  2026. return sock_intr_errno(*timeo_p);
  2027. add_wait_queue(sk_sleep(sk), &wait);
  2028. done = sk_wait_event(sk, timeo_p,
  2029. sk->sk_state != TIPC_CONNECTING, &wait);
  2030. remove_wait_queue(sk_sleep(sk), &wait);
  2031. } while (!done);
  2032. return 0;
  2033. }
  2034. /**
  2035. * tipc_connect - establish a connection to another TIPC port
  2036. * @sock: socket structure
  2037. * @dest: socket address for destination port
  2038. * @destlen: size of socket address data structure
  2039. * @flags: file-related flags associated with socket
  2040. *
  2041. * Returns 0 on success, errno otherwise
  2042. */
  2043. static int tipc_connect(struct socket *sock, struct sockaddr *dest,
  2044. int destlen, int flags)
  2045. {
  2046. struct sock *sk = sock->sk;
  2047. struct tipc_sock *tsk = tipc_sk(sk);
  2048. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  2049. struct msghdr m = {NULL,};
  2050. long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
  2051. int previous;
  2052. int res = 0;
  2053. if (destlen != sizeof(struct sockaddr_tipc))
  2054. return -EINVAL;
  2055. lock_sock(sk);
  2056. if (tsk->group) {
  2057. res = -EINVAL;
  2058. goto exit;
  2059. }
  2060. if (dst->family == AF_UNSPEC) {
  2061. memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
  2062. if (!tipc_sk_type_connectionless(sk))
  2063. res = -EINVAL;
  2064. goto exit;
  2065. } else if (dst->family != AF_TIPC) {
  2066. res = -EINVAL;
  2067. }
  2068. if (dst->addrtype != TIPC_ADDR_ID && dst->addrtype != TIPC_ADDR_NAME)
  2069. res = -EINVAL;
  2070. if (res)
  2071. goto exit;
  2072. /* DGRAM/RDM connect(), just save the destaddr */
  2073. if (tipc_sk_type_connectionless(sk)) {
  2074. memcpy(&tsk->peer, dest, destlen);
  2075. goto exit;
  2076. }
  2077. previous = sk->sk_state;
  2078. switch (sk->sk_state) {
  2079. case TIPC_OPEN:
  2080. /* Send a 'SYN-' to destination */
  2081. m.msg_name = dest;
  2082. m.msg_namelen = destlen;
  2083. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  2084. * indicate send_msg() is never blocked.
  2085. */
  2086. if (!timeout)
  2087. m.msg_flags = MSG_DONTWAIT;
  2088. res = __tipc_sendmsg(sock, &m, 0);
  2089. if ((res < 0) && (res != -EWOULDBLOCK))
  2090. goto exit;
  2091. /* Just entered TIPC_CONNECTING state; the only
  2092. * difference is that return value in non-blocking
  2093. * case is EINPROGRESS, rather than EALREADY.
  2094. */
  2095. res = -EINPROGRESS;
  2096. /* fall thru' */
  2097. case TIPC_CONNECTING:
  2098. if (!timeout) {
  2099. if (previous == TIPC_CONNECTING)
  2100. res = -EALREADY;
  2101. goto exit;
  2102. }
  2103. timeout = msecs_to_jiffies(timeout);
  2104. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  2105. res = tipc_wait_for_connect(sock, &timeout);
  2106. break;
  2107. case TIPC_ESTABLISHED:
  2108. res = -EISCONN;
  2109. break;
  2110. default:
  2111. res = -EINVAL;
  2112. }
  2113. exit:
  2114. release_sock(sk);
  2115. return res;
  2116. }
  2117. /**
  2118. * tipc_listen - allow socket to listen for incoming connections
  2119. * @sock: socket structure
  2120. * @len: (unused)
  2121. *
  2122. * Returns 0 on success, errno otherwise
  2123. */
  2124. static int tipc_listen(struct socket *sock, int len)
  2125. {
  2126. struct sock *sk = sock->sk;
  2127. int res;
  2128. lock_sock(sk);
  2129. res = tipc_set_sk_state(sk, TIPC_LISTEN);
  2130. release_sock(sk);
  2131. return res;
  2132. }
  2133. static int tipc_wait_for_accept(struct socket *sock, long timeo)
  2134. {
  2135. struct sock *sk = sock->sk;
  2136. DEFINE_WAIT(wait);
  2137. int err;
  2138. /* True wake-one mechanism for incoming connections: only
  2139. * one process gets woken up, not the 'whole herd'.
  2140. * Since we do not 'race & poll' for established sockets
  2141. * anymore, the common case will execute the loop only once.
  2142. */
  2143. for (;;) {
  2144. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  2145. TASK_INTERRUPTIBLE);
  2146. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  2147. release_sock(sk);
  2148. timeo = schedule_timeout(timeo);
  2149. lock_sock(sk);
  2150. }
  2151. err = 0;
  2152. if (!skb_queue_empty(&sk->sk_receive_queue))
  2153. break;
  2154. err = -EAGAIN;
  2155. if (!timeo)
  2156. break;
  2157. err = sock_intr_errno(timeo);
  2158. if (signal_pending(current))
  2159. break;
  2160. }
  2161. finish_wait(sk_sleep(sk), &wait);
  2162. return err;
  2163. }
  2164. /**
  2165. * tipc_accept - wait for connection request
  2166. * @sock: listening socket
  2167. * @newsock: new socket that is to be connected
  2168. * @flags: file-related flags associated with socket
  2169. *
  2170. * Returns 0 on success, errno otherwise
  2171. */
  2172. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
  2173. bool kern)
  2174. {
  2175. struct sock *new_sk, *sk = sock->sk;
  2176. struct sk_buff *buf;
  2177. struct tipc_sock *new_tsock;
  2178. struct tipc_msg *msg;
  2179. long timeo;
  2180. int res;
  2181. lock_sock(sk);
  2182. if (sk->sk_state != TIPC_LISTEN) {
  2183. res = -EINVAL;
  2184. goto exit;
  2185. }
  2186. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  2187. res = tipc_wait_for_accept(sock, timeo);
  2188. if (res)
  2189. goto exit;
  2190. buf = skb_peek(&sk->sk_receive_queue);
  2191. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
  2192. if (res)
  2193. goto exit;
  2194. security_sk_clone(sock->sk, new_sock->sk);
  2195. new_sk = new_sock->sk;
  2196. new_tsock = tipc_sk(new_sk);
  2197. msg = buf_msg(buf);
  2198. /* we lock on new_sk; but lockdep sees the lock on sk */
  2199. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  2200. /*
  2201. * Reject any stray messages received by new socket
  2202. * before the socket lock was taken (very, very unlikely)
  2203. */
  2204. tsk_rej_rx_queue(new_sk);
  2205. /* Connect new socket to it's peer */
  2206. tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
  2207. tsk_set_importance(new_tsock, msg_importance(msg));
  2208. if (msg_named(msg)) {
  2209. new_tsock->conn_type = msg_nametype(msg);
  2210. new_tsock->conn_instance = msg_nameinst(msg);
  2211. }
  2212. /*
  2213. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  2214. * Respond to 'SYN+' by queuing it on new socket.
  2215. */
  2216. if (!msg_data_sz(msg)) {
  2217. struct msghdr m = {NULL,};
  2218. tsk_advance_rx_queue(sk);
  2219. __tipc_sendstream(new_sock, &m, 0);
  2220. } else {
  2221. __skb_dequeue(&sk->sk_receive_queue);
  2222. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  2223. skb_set_owner_r(buf, new_sk);
  2224. }
  2225. release_sock(new_sk);
  2226. exit:
  2227. release_sock(sk);
  2228. return res;
  2229. }
  2230. /**
  2231. * tipc_shutdown - shutdown socket connection
  2232. * @sock: socket structure
  2233. * @how: direction to close (must be SHUT_RDWR)
  2234. *
  2235. * Terminates connection (if necessary), then purges socket's receive queue.
  2236. *
  2237. * Returns 0 on success, errno otherwise
  2238. */
  2239. static int tipc_shutdown(struct socket *sock, int how)
  2240. {
  2241. struct sock *sk = sock->sk;
  2242. int res;
  2243. if (how != SHUT_RDWR)
  2244. return -EINVAL;
  2245. lock_sock(sk);
  2246. __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
  2247. sk->sk_shutdown = SEND_SHUTDOWN;
  2248. if (sk->sk_state == TIPC_DISCONNECTING) {
  2249. /* Discard any unreceived messages */
  2250. __skb_queue_purge(&sk->sk_receive_queue);
  2251. /* Wake up anyone sleeping in poll */
  2252. sk->sk_state_change(sk);
  2253. res = 0;
  2254. } else {
  2255. res = -ENOTCONN;
  2256. }
  2257. release_sock(sk);
  2258. return res;
  2259. }
  2260. static void tipc_sk_timeout(struct timer_list *t)
  2261. {
  2262. struct sock *sk = from_timer(sk, t, sk_timer);
  2263. struct tipc_sock *tsk = tipc_sk(sk);
  2264. u32 peer_port = tsk_peer_port(tsk);
  2265. u32 peer_node = tsk_peer_node(tsk);
  2266. u32 own_node = tsk_own_node(tsk);
  2267. u32 own_port = tsk->portid;
  2268. struct net *net = sock_net(sk);
  2269. struct sk_buff *skb = NULL;
  2270. bh_lock_sock(sk);
  2271. if (!tipc_sk_connected(sk))
  2272. goto exit;
  2273. /* Try again later if socket is busy */
  2274. if (sock_owned_by_user(sk)) {
  2275. sk_reset_timer(sk, &sk->sk_timer, jiffies + HZ / 20);
  2276. goto exit;
  2277. }
  2278. if (tsk->probe_unacked) {
  2279. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  2280. tipc_node_remove_conn(net, peer_node, peer_port);
  2281. sk->sk_state_change(sk);
  2282. goto exit;
  2283. }
  2284. /* Send new probe */
  2285. skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, 0,
  2286. peer_node, own_node, peer_port, own_port,
  2287. TIPC_OK);
  2288. tsk->probe_unacked = true;
  2289. sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
  2290. exit:
  2291. bh_unlock_sock(sk);
  2292. if (skb)
  2293. tipc_node_xmit_skb(net, skb, peer_node, own_port);
  2294. sock_put(sk);
  2295. }
  2296. static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
  2297. struct tipc_name_seq const *seq)
  2298. {
  2299. struct sock *sk = &tsk->sk;
  2300. struct net *net = sock_net(sk);
  2301. struct publication *publ;
  2302. u32 key;
  2303. if (scope != TIPC_NODE_SCOPE)
  2304. scope = TIPC_CLUSTER_SCOPE;
  2305. if (tipc_sk_connected(sk))
  2306. return -EINVAL;
  2307. key = tsk->portid + tsk->pub_count + 1;
  2308. if (key == tsk->portid)
  2309. return -EADDRINUSE;
  2310. publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
  2311. scope, tsk->portid, key);
  2312. if (unlikely(!publ))
  2313. return -EINVAL;
  2314. list_add(&publ->binding_sock, &tsk->publications);
  2315. tsk->pub_count++;
  2316. tsk->published = 1;
  2317. return 0;
  2318. }
  2319. static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
  2320. struct tipc_name_seq const *seq)
  2321. {
  2322. struct net *net = sock_net(&tsk->sk);
  2323. struct publication *publ;
  2324. struct publication *safe;
  2325. int rc = -EINVAL;
  2326. if (scope != TIPC_NODE_SCOPE)
  2327. scope = TIPC_CLUSTER_SCOPE;
  2328. list_for_each_entry_safe(publ, safe, &tsk->publications, binding_sock) {
  2329. if (seq) {
  2330. if (publ->scope != scope)
  2331. continue;
  2332. if (publ->type != seq->type)
  2333. continue;
  2334. if (publ->lower != seq->lower)
  2335. continue;
  2336. if (publ->upper != seq->upper)
  2337. break;
  2338. tipc_nametbl_withdraw(net, publ->type, publ->lower,
  2339. publ->upper, publ->key);
  2340. rc = 0;
  2341. break;
  2342. }
  2343. tipc_nametbl_withdraw(net, publ->type, publ->lower,
  2344. publ->upper, publ->key);
  2345. rc = 0;
  2346. }
  2347. if (list_empty(&tsk->publications))
  2348. tsk->published = 0;
  2349. return rc;
  2350. }
  2351. /* tipc_sk_reinit: set non-zero address in all existing sockets
  2352. * when we go from standalone to network mode.
  2353. */
  2354. void tipc_sk_reinit(struct net *net)
  2355. {
  2356. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2357. struct rhashtable_iter iter;
  2358. struct tipc_sock *tsk;
  2359. struct tipc_msg *msg;
  2360. rhashtable_walk_enter(&tn->sk_rht, &iter);
  2361. do {
  2362. rhashtable_walk_start(&iter);
  2363. while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
  2364. spin_lock_bh(&tsk->sk.sk_lock.slock);
  2365. msg = &tsk->phdr;
  2366. msg_set_prevnode(msg, tipc_own_addr(net));
  2367. msg_set_orignode(msg, tipc_own_addr(net));
  2368. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2369. }
  2370. rhashtable_walk_stop(&iter);
  2371. } while (tsk == ERR_PTR(-EAGAIN));
  2372. rhashtable_walk_exit(&iter);
  2373. }
  2374. static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
  2375. {
  2376. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2377. struct tipc_sock *tsk;
  2378. rcu_read_lock();
  2379. tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
  2380. if (tsk)
  2381. sock_hold(&tsk->sk);
  2382. rcu_read_unlock();
  2383. return tsk;
  2384. }
  2385. static int tipc_sk_insert(struct tipc_sock *tsk)
  2386. {
  2387. struct sock *sk = &tsk->sk;
  2388. struct net *net = sock_net(sk);
  2389. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2390. u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
  2391. u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
  2392. while (remaining--) {
  2393. portid++;
  2394. if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
  2395. portid = TIPC_MIN_PORT;
  2396. tsk->portid = portid;
  2397. sock_hold(&tsk->sk);
  2398. if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
  2399. tsk_rht_params))
  2400. return 0;
  2401. sock_put(&tsk->sk);
  2402. }
  2403. return -1;
  2404. }
  2405. static void tipc_sk_remove(struct tipc_sock *tsk)
  2406. {
  2407. struct sock *sk = &tsk->sk;
  2408. struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
  2409. if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
  2410. WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
  2411. __sock_put(sk);
  2412. }
  2413. }
  2414. static const struct rhashtable_params tsk_rht_params = {
  2415. .nelem_hint = 192,
  2416. .head_offset = offsetof(struct tipc_sock, node),
  2417. .key_offset = offsetof(struct tipc_sock, portid),
  2418. .key_len = sizeof(u32), /* portid */
  2419. .max_size = 1048576,
  2420. .min_size = 256,
  2421. .automatic_shrinking = true,
  2422. };
  2423. int tipc_sk_rht_init(struct net *net)
  2424. {
  2425. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2426. return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
  2427. }
  2428. void tipc_sk_rht_destroy(struct net *net)
  2429. {
  2430. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2431. /* Wait for socket readers to complete */
  2432. synchronize_net();
  2433. rhashtable_destroy(&tn->sk_rht);
  2434. }
  2435. static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
  2436. {
  2437. struct net *net = sock_net(&tsk->sk);
  2438. struct tipc_group *grp = tsk->group;
  2439. struct tipc_msg *hdr = &tsk->phdr;
  2440. struct tipc_name_seq seq;
  2441. int rc;
  2442. if (mreq->type < TIPC_RESERVED_TYPES)
  2443. return -EACCES;
  2444. if (mreq->scope > TIPC_NODE_SCOPE)
  2445. return -EINVAL;
  2446. if (grp)
  2447. return -EACCES;
  2448. grp = tipc_group_create(net, tsk->portid, mreq, &tsk->group_is_open);
  2449. if (!grp)
  2450. return -ENOMEM;
  2451. tsk->group = grp;
  2452. msg_set_lookup_scope(hdr, mreq->scope);
  2453. msg_set_nametype(hdr, mreq->type);
  2454. msg_set_dest_droppable(hdr, true);
  2455. seq.type = mreq->type;
  2456. seq.lower = mreq->instance;
  2457. seq.upper = seq.lower;
  2458. tipc_nametbl_build_group(net, grp, mreq->type, mreq->scope);
  2459. rc = tipc_sk_publish(tsk, mreq->scope, &seq);
  2460. if (rc) {
  2461. tipc_group_delete(net, grp);
  2462. tsk->group = NULL;
  2463. return rc;
  2464. }
  2465. /* Eliminate any risk that a broadcast overtakes sent JOINs */
  2466. tsk->mc_method.rcast = true;
  2467. tsk->mc_method.mandatory = true;
  2468. tipc_group_join(net, grp, &tsk->sk.sk_rcvbuf);
  2469. return rc;
  2470. }
  2471. static int tipc_sk_leave(struct tipc_sock *tsk)
  2472. {
  2473. struct net *net = sock_net(&tsk->sk);
  2474. struct tipc_group *grp = tsk->group;
  2475. struct tipc_name_seq seq;
  2476. int scope;
  2477. if (!grp)
  2478. return -EINVAL;
  2479. tipc_group_self(grp, &seq, &scope);
  2480. tipc_group_delete(net, grp);
  2481. tsk->group = NULL;
  2482. tipc_sk_withdraw(tsk, scope, &seq);
  2483. return 0;
  2484. }
  2485. /**
  2486. * tipc_setsockopt - set socket option
  2487. * @sock: socket structure
  2488. * @lvl: option level
  2489. * @opt: option identifier
  2490. * @ov: pointer to new option value
  2491. * @ol: length of option value
  2492. *
  2493. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  2494. * (to ease compatibility).
  2495. *
  2496. * Returns 0 on success, errno otherwise
  2497. */
  2498. static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
  2499. char __user *ov, unsigned int ol)
  2500. {
  2501. struct sock *sk = sock->sk;
  2502. struct tipc_sock *tsk = tipc_sk(sk);
  2503. struct tipc_group_req mreq;
  2504. u32 value = 0;
  2505. int res = 0;
  2506. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  2507. return 0;
  2508. if (lvl != SOL_TIPC)
  2509. return -ENOPROTOOPT;
  2510. switch (opt) {
  2511. case TIPC_IMPORTANCE:
  2512. case TIPC_SRC_DROPPABLE:
  2513. case TIPC_DEST_DROPPABLE:
  2514. case TIPC_CONN_TIMEOUT:
  2515. if (ol < sizeof(value))
  2516. return -EINVAL;
  2517. if (get_user(value, (u32 __user *)ov))
  2518. return -EFAULT;
  2519. break;
  2520. case TIPC_GROUP_JOIN:
  2521. if (ol < sizeof(mreq))
  2522. return -EINVAL;
  2523. if (copy_from_user(&mreq, ov, sizeof(mreq)))
  2524. return -EFAULT;
  2525. break;
  2526. default:
  2527. if (ov || ol)
  2528. return -EINVAL;
  2529. }
  2530. lock_sock(sk);
  2531. switch (opt) {
  2532. case TIPC_IMPORTANCE:
  2533. res = tsk_set_importance(tsk, value);
  2534. break;
  2535. case TIPC_SRC_DROPPABLE:
  2536. if (sock->type != SOCK_STREAM)
  2537. tsk_set_unreliable(tsk, value);
  2538. else
  2539. res = -ENOPROTOOPT;
  2540. break;
  2541. case TIPC_DEST_DROPPABLE:
  2542. tsk_set_unreturnable(tsk, value);
  2543. break;
  2544. case TIPC_CONN_TIMEOUT:
  2545. tipc_sk(sk)->conn_timeout = value;
  2546. break;
  2547. case TIPC_MCAST_BROADCAST:
  2548. tsk->mc_method.rcast = false;
  2549. tsk->mc_method.mandatory = true;
  2550. break;
  2551. case TIPC_MCAST_REPLICAST:
  2552. tsk->mc_method.rcast = true;
  2553. tsk->mc_method.mandatory = true;
  2554. break;
  2555. case TIPC_GROUP_JOIN:
  2556. res = tipc_sk_join(tsk, &mreq);
  2557. break;
  2558. case TIPC_GROUP_LEAVE:
  2559. res = tipc_sk_leave(tsk);
  2560. break;
  2561. default:
  2562. res = -EINVAL;
  2563. }
  2564. release_sock(sk);
  2565. return res;
  2566. }
  2567. /**
  2568. * tipc_getsockopt - get socket option
  2569. * @sock: socket structure
  2570. * @lvl: option level
  2571. * @opt: option identifier
  2572. * @ov: receptacle for option value
  2573. * @ol: receptacle for length of option value
  2574. *
  2575. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  2576. * (to ease compatibility).
  2577. *
  2578. * Returns 0 on success, errno otherwise
  2579. */
  2580. static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
  2581. char __user *ov, int __user *ol)
  2582. {
  2583. struct sock *sk = sock->sk;
  2584. struct tipc_sock *tsk = tipc_sk(sk);
  2585. struct tipc_name_seq seq;
  2586. int len, scope;
  2587. u32 value;
  2588. int res;
  2589. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  2590. return put_user(0, ol);
  2591. if (lvl != SOL_TIPC)
  2592. return -ENOPROTOOPT;
  2593. res = get_user(len, ol);
  2594. if (res)
  2595. return res;
  2596. lock_sock(sk);
  2597. switch (opt) {
  2598. case TIPC_IMPORTANCE:
  2599. value = tsk_importance(tsk);
  2600. break;
  2601. case TIPC_SRC_DROPPABLE:
  2602. value = tsk_unreliable(tsk);
  2603. break;
  2604. case TIPC_DEST_DROPPABLE:
  2605. value = tsk_unreturnable(tsk);
  2606. break;
  2607. case TIPC_CONN_TIMEOUT:
  2608. value = tsk->conn_timeout;
  2609. /* no need to set "res", since already 0 at this point */
  2610. break;
  2611. case TIPC_NODE_RECVQ_DEPTH:
  2612. value = 0; /* was tipc_queue_size, now obsolete */
  2613. break;
  2614. case TIPC_SOCK_RECVQ_DEPTH:
  2615. value = skb_queue_len(&sk->sk_receive_queue);
  2616. break;
  2617. case TIPC_GROUP_JOIN:
  2618. seq.type = 0;
  2619. if (tsk->group)
  2620. tipc_group_self(tsk->group, &seq, &scope);
  2621. value = seq.type;
  2622. break;
  2623. default:
  2624. res = -EINVAL;
  2625. }
  2626. release_sock(sk);
  2627. if (res)
  2628. return res; /* "get" failed */
  2629. if (len < sizeof(value))
  2630. return -EINVAL;
  2631. if (copy_to_user(ov, &value, sizeof(value)))
  2632. return -EFAULT;
  2633. return put_user(sizeof(value), ol);
  2634. }
  2635. static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  2636. {
  2637. struct net *net = sock_net(sock->sk);
  2638. struct tipc_sioc_nodeid_req nr = {0};
  2639. struct tipc_sioc_ln_req lnr;
  2640. void __user *argp = (void __user *)arg;
  2641. switch (cmd) {
  2642. case SIOCGETLINKNAME:
  2643. if (copy_from_user(&lnr, argp, sizeof(lnr)))
  2644. return -EFAULT;
  2645. if (!tipc_node_get_linkname(net,
  2646. lnr.bearer_id & 0xffff, lnr.peer,
  2647. lnr.linkname, TIPC_MAX_LINK_NAME)) {
  2648. if (copy_to_user(argp, &lnr, sizeof(lnr)))
  2649. return -EFAULT;
  2650. return 0;
  2651. }
  2652. return -EADDRNOTAVAIL;
  2653. case SIOCGETNODEID:
  2654. if (copy_from_user(&nr, argp, sizeof(nr)))
  2655. return -EFAULT;
  2656. if (!tipc_node_get_id(net, nr.peer, nr.node_id))
  2657. return -EADDRNOTAVAIL;
  2658. if (copy_to_user(argp, &nr, sizeof(nr)))
  2659. return -EFAULT;
  2660. return 0;
  2661. default:
  2662. return -ENOIOCTLCMD;
  2663. }
  2664. }
  2665. static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
  2666. {
  2667. struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
  2668. struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
  2669. u32 onode = tipc_own_addr(sock_net(sock1->sk));
  2670. tsk1->peer.family = AF_TIPC;
  2671. tsk1->peer.addrtype = TIPC_ADDR_ID;
  2672. tsk1->peer.scope = TIPC_NODE_SCOPE;
  2673. tsk1->peer.addr.id.ref = tsk2->portid;
  2674. tsk1->peer.addr.id.node = onode;
  2675. tsk2->peer.family = AF_TIPC;
  2676. tsk2->peer.addrtype = TIPC_ADDR_ID;
  2677. tsk2->peer.scope = TIPC_NODE_SCOPE;
  2678. tsk2->peer.addr.id.ref = tsk1->portid;
  2679. tsk2->peer.addr.id.node = onode;
  2680. tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
  2681. tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
  2682. return 0;
  2683. }
  2684. /* Protocol switches for the various types of TIPC sockets */
  2685. static const struct proto_ops msg_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 = sock_no_accept,
  2693. .getname = tipc_getname,
  2694. .poll = tipc_poll,
  2695. .ioctl = tipc_ioctl,
  2696. .listen = sock_no_listen,
  2697. .shutdown = tipc_shutdown,
  2698. .setsockopt = tipc_setsockopt,
  2699. .getsockopt = tipc_getsockopt,
  2700. .sendmsg = tipc_sendmsg,
  2701. .recvmsg = tipc_recvmsg,
  2702. .mmap = sock_no_mmap,
  2703. .sendpage = sock_no_sendpage
  2704. };
  2705. static const struct proto_ops packet_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_send_packet,
  2721. .recvmsg = tipc_recvmsg,
  2722. .mmap = sock_no_mmap,
  2723. .sendpage = sock_no_sendpage
  2724. };
  2725. static const struct proto_ops stream_ops = {
  2726. .owner = THIS_MODULE,
  2727. .family = AF_TIPC,
  2728. .release = tipc_release,
  2729. .bind = tipc_bind,
  2730. .connect = tipc_connect,
  2731. .socketpair = tipc_socketpair,
  2732. .accept = tipc_accept,
  2733. .getname = tipc_getname,
  2734. .poll = tipc_poll,
  2735. .ioctl = tipc_ioctl,
  2736. .listen = tipc_listen,
  2737. .shutdown = tipc_shutdown,
  2738. .setsockopt = tipc_setsockopt,
  2739. .getsockopt = tipc_getsockopt,
  2740. .sendmsg = tipc_sendstream,
  2741. .recvmsg = tipc_recvstream,
  2742. .mmap = sock_no_mmap,
  2743. .sendpage = sock_no_sendpage
  2744. };
  2745. static const struct net_proto_family tipc_family_ops = {
  2746. .owner = THIS_MODULE,
  2747. .family = AF_TIPC,
  2748. .create = tipc_sk_create
  2749. };
  2750. static struct proto tipc_proto = {
  2751. .name = "TIPC",
  2752. .owner = THIS_MODULE,
  2753. .obj_size = sizeof(struct tipc_sock),
  2754. .sysctl_rmem = sysctl_tipc_rmem
  2755. };
  2756. /**
  2757. * tipc_socket_init - initialize TIPC socket interface
  2758. *
  2759. * Returns 0 on success, errno otherwise
  2760. */
  2761. int tipc_socket_init(void)
  2762. {
  2763. int res;
  2764. res = proto_register(&tipc_proto, 1);
  2765. if (res) {
  2766. pr_err("Failed to register TIPC protocol type\n");
  2767. goto out;
  2768. }
  2769. res = sock_register(&tipc_family_ops);
  2770. if (res) {
  2771. pr_err("Failed to register TIPC socket type\n");
  2772. proto_unregister(&tipc_proto);
  2773. goto out;
  2774. }
  2775. out:
  2776. return res;
  2777. }
  2778. /**
  2779. * tipc_socket_stop - stop TIPC socket interface
  2780. */
  2781. void tipc_socket_stop(void)
  2782. {
  2783. sock_unregister(tipc_family_ops.family);
  2784. proto_unregister(&tipc_proto);
  2785. }
  2786. /* Caller should hold socket lock for the passed tipc socket. */
  2787. static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
  2788. {
  2789. u32 peer_node;
  2790. u32 peer_port;
  2791. struct nlattr *nest;
  2792. peer_node = tsk_peer_node(tsk);
  2793. peer_port = tsk_peer_port(tsk);
  2794. nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
  2795. if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
  2796. goto msg_full;
  2797. if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
  2798. goto msg_full;
  2799. if (tsk->conn_type != 0) {
  2800. if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
  2801. goto msg_full;
  2802. if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
  2803. goto msg_full;
  2804. if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
  2805. goto msg_full;
  2806. }
  2807. nla_nest_end(skb, nest);
  2808. return 0;
  2809. msg_full:
  2810. nla_nest_cancel(skb, nest);
  2811. return -EMSGSIZE;
  2812. }
  2813. static int __tipc_nl_add_sk_info(struct sk_buff *skb, struct tipc_sock
  2814. *tsk)
  2815. {
  2816. struct net *net = sock_net(skb->sk);
  2817. struct sock *sk = &tsk->sk;
  2818. if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid) ||
  2819. nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr(net)))
  2820. return -EMSGSIZE;
  2821. if (tipc_sk_connected(sk)) {
  2822. if (__tipc_nl_add_sk_con(skb, tsk))
  2823. return -EMSGSIZE;
  2824. } else if (!list_empty(&tsk->publications)) {
  2825. if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
  2826. return -EMSGSIZE;
  2827. }
  2828. return 0;
  2829. }
  2830. /* Caller should hold socket lock for the passed tipc socket. */
  2831. static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
  2832. struct tipc_sock *tsk)
  2833. {
  2834. struct nlattr *attrs;
  2835. void *hdr;
  2836. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  2837. &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
  2838. if (!hdr)
  2839. goto msg_cancel;
  2840. attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
  2841. if (!attrs)
  2842. goto genlmsg_cancel;
  2843. if (__tipc_nl_add_sk_info(skb, tsk))
  2844. goto attr_msg_cancel;
  2845. nla_nest_end(skb, attrs);
  2846. genlmsg_end(skb, hdr);
  2847. return 0;
  2848. attr_msg_cancel:
  2849. nla_nest_cancel(skb, attrs);
  2850. genlmsg_cancel:
  2851. genlmsg_cancel(skb, hdr);
  2852. msg_cancel:
  2853. return -EMSGSIZE;
  2854. }
  2855. int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,
  2856. int (*skb_handler)(struct sk_buff *skb,
  2857. struct netlink_callback *cb,
  2858. struct tipc_sock *tsk))
  2859. {
  2860. struct rhashtable_iter *iter = (void *)cb->args[4];
  2861. struct tipc_sock *tsk;
  2862. int err;
  2863. rhashtable_walk_start(iter);
  2864. while ((tsk = rhashtable_walk_next(iter)) != NULL) {
  2865. if (IS_ERR(tsk)) {
  2866. err = PTR_ERR(tsk);
  2867. if (err == -EAGAIN) {
  2868. err = 0;
  2869. continue;
  2870. }
  2871. break;
  2872. }
  2873. sock_hold(&tsk->sk);
  2874. rhashtable_walk_stop(iter);
  2875. lock_sock(&tsk->sk);
  2876. err = skb_handler(skb, cb, tsk);
  2877. if (err) {
  2878. release_sock(&tsk->sk);
  2879. sock_put(&tsk->sk);
  2880. goto out;
  2881. }
  2882. release_sock(&tsk->sk);
  2883. rhashtable_walk_start(iter);
  2884. sock_put(&tsk->sk);
  2885. }
  2886. rhashtable_walk_stop(iter);
  2887. out:
  2888. return skb->len;
  2889. }
  2890. EXPORT_SYMBOL(tipc_nl_sk_walk);
  2891. int tipc_dump_start(struct netlink_callback *cb)
  2892. {
  2893. return __tipc_dump_start(cb, sock_net(cb->skb->sk));
  2894. }
  2895. EXPORT_SYMBOL(tipc_dump_start);
  2896. int __tipc_dump_start(struct netlink_callback *cb, struct net *net)
  2897. {
  2898. /* tipc_nl_name_table_dump() uses cb->args[0...3]. */
  2899. struct rhashtable_iter *iter = (void *)cb->args[4];
  2900. struct tipc_net *tn = tipc_net(net);
  2901. if (!iter) {
  2902. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  2903. if (!iter)
  2904. return -ENOMEM;
  2905. cb->args[4] = (long)iter;
  2906. }
  2907. rhashtable_walk_enter(&tn->sk_rht, iter);
  2908. return 0;
  2909. }
  2910. int tipc_dump_done(struct netlink_callback *cb)
  2911. {
  2912. struct rhashtable_iter *hti = (void *)cb->args[4];
  2913. rhashtable_walk_exit(hti);
  2914. kfree(hti);
  2915. return 0;
  2916. }
  2917. EXPORT_SYMBOL(tipc_dump_done);
  2918. int tipc_sk_fill_sock_diag(struct sk_buff *skb, struct netlink_callback *cb,
  2919. struct tipc_sock *tsk, u32 sk_filter_state,
  2920. u64 (*tipc_diag_gen_cookie)(struct sock *sk))
  2921. {
  2922. struct sock *sk = &tsk->sk;
  2923. struct nlattr *attrs;
  2924. struct nlattr *stat;
  2925. /*filter response w.r.t sk_state*/
  2926. if (!(sk_filter_state & (1 << sk->sk_state)))
  2927. return 0;
  2928. attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
  2929. if (!attrs)
  2930. goto msg_cancel;
  2931. if (__tipc_nl_add_sk_info(skb, tsk))
  2932. goto attr_msg_cancel;
  2933. if (nla_put_u32(skb, TIPC_NLA_SOCK_TYPE, (u32)sk->sk_type) ||
  2934. nla_put_u32(skb, TIPC_NLA_SOCK_TIPC_STATE, (u32)sk->sk_state) ||
  2935. nla_put_u32(skb, TIPC_NLA_SOCK_INO, sock_i_ino(sk)) ||
  2936. nla_put_u32(skb, TIPC_NLA_SOCK_UID,
  2937. from_kuid_munged(sk_user_ns(NETLINK_CB(cb->skb).sk),
  2938. sock_i_uid(sk))) ||
  2939. nla_put_u64_64bit(skb, TIPC_NLA_SOCK_COOKIE,
  2940. tipc_diag_gen_cookie(sk),
  2941. TIPC_NLA_SOCK_PAD))
  2942. goto attr_msg_cancel;
  2943. stat = nla_nest_start(skb, TIPC_NLA_SOCK_STAT);
  2944. if (!stat)
  2945. goto attr_msg_cancel;
  2946. if (nla_put_u32(skb, TIPC_NLA_SOCK_STAT_RCVQ,
  2947. skb_queue_len(&sk->sk_receive_queue)) ||
  2948. nla_put_u32(skb, TIPC_NLA_SOCK_STAT_SENDQ,
  2949. skb_queue_len(&sk->sk_write_queue)) ||
  2950. nla_put_u32(skb, TIPC_NLA_SOCK_STAT_DROP,
  2951. atomic_read(&sk->sk_drops)))
  2952. goto stat_msg_cancel;
  2953. if (tsk->cong_link_cnt &&
  2954. nla_put_flag(skb, TIPC_NLA_SOCK_STAT_LINK_CONG))
  2955. goto stat_msg_cancel;
  2956. if (tsk_conn_cong(tsk) &&
  2957. nla_put_flag(skb, TIPC_NLA_SOCK_STAT_CONN_CONG))
  2958. goto stat_msg_cancel;
  2959. nla_nest_end(skb, stat);
  2960. if (tsk->group)
  2961. if (tipc_group_fill_sock_diag(tsk->group, skb))
  2962. goto stat_msg_cancel;
  2963. nla_nest_end(skb, attrs);
  2964. return 0;
  2965. stat_msg_cancel:
  2966. nla_nest_cancel(skb, stat);
  2967. attr_msg_cancel:
  2968. nla_nest_cancel(skb, attrs);
  2969. msg_cancel:
  2970. return -EMSGSIZE;
  2971. }
  2972. EXPORT_SYMBOL(tipc_sk_fill_sock_diag);
  2973. int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2974. {
  2975. return tipc_nl_sk_walk(skb, cb, __tipc_nl_add_sk);
  2976. }
  2977. /* Caller should hold socket lock for the passed tipc socket. */
  2978. static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
  2979. struct netlink_callback *cb,
  2980. struct publication *publ)
  2981. {
  2982. void *hdr;
  2983. struct nlattr *attrs;
  2984. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  2985. &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
  2986. if (!hdr)
  2987. goto msg_cancel;
  2988. attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
  2989. if (!attrs)
  2990. goto genlmsg_cancel;
  2991. if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
  2992. goto attr_msg_cancel;
  2993. if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
  2994. goto attr_msg_cancel;
  2995. if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
  2996. goto attr_msg_cancel;
  2997. if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
  2998. goto attr_msg_cancel;
  2999. nla_nest_end(skb, attrs);
  3000. genlmsg_end(skb, hdr);
  3001. return 0;
  3002. attr_msg_cancel:
  3003. nla_nest_cancel(skb, attrs);
  3004. genlmsg_cancel:
  3005. genlmsg_cancel(skb, hdr);
  3006. msg_cancel:
  3007. return -EMSGSIZE;
  3008. }
  3009. /* Caller should hold socket lock for the passed tipc socket. */
  3010. static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
  3011. struct netlink_callback *cb,
  3012. struct tipc_sock *tsk, u32 *last_publ)
  3013. {
  3014. int err;
  3015. struct publication *p;
  3016. if (*last_publ) {
  3017. list_for_each_entry(p, &tsk->publications, binding_sock) {
  3018. if (p->key == *last_publ)
  3019. break;
  3020. }
  3021. if (p->key != *last_publ) {
  3022. /* We never set seq or call nl_dump_check_consistent()
  3023. * this means that setting prev_seq here will cause the
  3024. * consistence check to fail in the netlink callback
  3025. * handler. Resulting in the last NLMSG_DONE message
  3026. * having the NLM_F_DUMP_INTR flag set.
  3027. */
  3028. cb->prev_seq = 1;
  3029. *last_publ = 0;
  3030. return -EPIPE;
  3031. }
  3032. } else {
  3033. p = list_first_entry(&tsk->publications, struct publication,
  3034. binding_sock);
  3035. }
  3036. list_for_each_entry_from(p, &tsk->publications, binding_sock) {
  3037. err = __tipc_nl_add_sk_publ(skb, cb, p);
  3038. if (err) {
  3039. *last_publ = p->key;
  3040. return err;
  3041. }
  3042. }
  3043. *last_publ = 0;
  3044. return 0;
  3045. }
  3046. int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
  3047. {
  3048. int err;
  3049. u32 tsk_portid = cb->args[0];
  3050. u32 last_publ = cb->args[1];
  3051. u32 done = cb->args[2];
  3052. struct net *net = sock_net(skb->sk);
  3053. struct tipc_sock *tsk;
  3054. if (!tsk_portid) {
  3055. struct nlattr **attrs;
  3056. struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
  3057. err = tipc_nlmsg_parse(cb->nlh, &attrs);
  3058. if (err)
  3059. return err;
  3060. if (!attrs[TIPC_NLA_SOCK])
  3061. return -EINVAL;
  3062. err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
  3063. attrs[TIPC_NLA_SOCK],
  3064. tipc_nl_sock_policy, NULL);
  3065. if (err)
  3066. return err;
  3067. if (!sock[TIPC_NLA_SOCK_REF])
  3068. return -EINVAL;
  3069. tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
  3070. }
  3071. if (done)
  3072. return 0;
  3073. tsk = tipc_sk_lookup(net, tsk_portid);
  3074. if (!tsk)
  3075. return -EINVAL;
  3076. lock_sock(&tsk->sk);
  3077. err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
  3078. if (!err)
  3079. done = 1;
  3080. release_sock(&tsk->sk);
  3081. sock_put(&tsk->sk);
  3082. cb->args[0] = tsk_portid;
  3083. cb->args[1] = last_publ;
  3084. cb->args[2] = done;
  3085. return skb->len;
  3086. }