socket.c 74 KB

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