socket.c 77 KB

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