socket.c 76 KB

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