socket.c 74 KB

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