socket.c 74 KB

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