socket.c 74 KB

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