socket.c 91 KB

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