socket.c 72 KB

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