socket.c 90 KB

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