socket.c 72 KB

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