socket.c 73 KB

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