socket.c 75 KB

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