socket.c 74 KB

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