socket.c 74 KB

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