socket.c 90 KB

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