socket.c 71 KB

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