socket.c 85 KB

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