socket.c 72 KB

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