socket.c 72 KB

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