socket.c 86 KB

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