socket.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876
  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_, timeout_, condition_) \
  322. ({ \
  323. int rc_ = 0; \
  324. int done_ = 0; \
  325. \
  326. while (!(condition_) && !done_) { \
  327. struct sock *sk_ = sock->sk; \
  328. DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
  329. \
  330. rc_ = tipc_sk_sock_err(sock_, timeout_); \
  331. if (rc_) \
  332. break; \
  333. prepare_to_wait(sk_sleep(sk_), &wait_, \
  334. TASK_INTERRUPTIBLE); \
  335. done_ = sk_wait_event(sk_, timeout_, \
  336. (condition_), &wait_); \
  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. tsk->probe_unacked = false;
  771. if (mtyp == CONN_PROBE) {
  772. msg_set_type(hdr, CONN_PROBE_REPLY);
  773. if (tipc_msg_reverse(onode, &skb, TIPC_OK))
  774. __skb_queue_tail(xmitq, skb);
  775. return;
  776. } else if (mtyp == CONN_ACK) {
  777. conn_cong = tsk_conn_cong(tsk);
  778. tsk->snt_unacked -= msg_conn_ack(hdr);
  779. if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
  780. tsk->snd_win = msg_adv_win(hdr);
  781. if (conn_cong)
  782. sk->sk_write_space(sk);
  783. } else if (mtyp != CONN_PROBE_REPLY) {
  784. pr_warn("Received unknown CONN_PROTO msg\n");
  785. }
  786. exit:
  787. kfree_skb(skb);
  788. }
  789. /**
  790. * tipc_sendmsg - send message in connectionless manner
  791. * @sock: socket structure
  792. * @m: message to send
  793. * @dsz: amount of user data to be sent
  794. *
  795. * Message must have an destination specified explicitly.
  796. * Used for SOCK_RDM and SOCK_DGRAM messages,
  797. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  798. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  799. *
  800. * Returns the number of bytes sent on success, or errno otherwise
  801. */
  802. static int tipc_sendmsg(struct socket *sock,
  803. struct msghdr *m, size_t dsz)
  804. {
  805. struct sock *sk = sock->sk;
  806. int ret;
  807. lock_sock(sk);
  808. ret = __tipc_sendmsg(sock, m, dsz);
  809. release_sock(sk);
  810. return ret;
  811. }
  812. static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
  813. {
  814. struct sock *sk = sock->sk;
  815. struct net *net = sock_net(sk);
  816. struct tipc_sock *tsk = tipc_sk(sk);
  817. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  818. long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  819. struct list_head *clinks = &tsk->cong_links;
  820. bool syn = !tipc_sk_type_connectionless(sk);
  821. struct tipc_msg *hdr = &tsk->phdr;
  822. struct tipc_name_seq *seq;
  823. struct sk_buff_head pkts;
  824. u32 type, inst, domain;
  825. u32 dnode, dport;
  826. int mtu, rc;
  827. if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
  828. return -EMSGSIZE;
  829. if (unlikely(!dest)) {
  830. dest = &tsk->peer;
  831. if (!syn || dest->family != AF_TIPC)
  832. return -EDESTADDRREQ;
  833. }
  834. if (unlikely(m->msg_namelen < sizeof(*dest)))
  835. return -EINVAL;
  836. if (unlikely(dest->family != AF_TIPC))
  837. return -EINVAL;
  838. if (unlikely(syn)) {
  839. if (sk->sk_state == TIPC_LISTEN)
  840. return -EPIPE;
  841. if (sk->sk_state != TIPC_OPEN)
  842. return -EISCONN;
  843. if (tsk->published)
  844. return -EOPNOTSUPP;
  845. if (dest->addrtype == TIPC_ADDR_NAME) {
  846. tsk->conn_type = dest->addr.name.name.type;
  847. tsk->conn_instance = dest->addr.name.name.instance;
  848. }
  849. }
  850. seq = &dest->addr.nameseq;
  851. if (dest->addrtype == TIPC_ADDR_MCAST)
  852. return tipc_sendmcast(sock, seq, m, dlen, timeout);
  853. if (dest->addrtype == TIPC_ADDR_NAME) {
  854. type = dest->addr.name.name.type;
  855. inst = dest->addr.name.name.instance;
  856. domain = dest->addr.name.domain;
  857. dnode = domain;
  858. msg_set_type(hdr, TIPC_NAMED_MSG);
  859. msg_set_hdr_sz(hdr, NAMED_H_SIZE);
  860. msg_set_nametype(hdr, type);
  861. msg_set_nameinst(hdr, inst);
  862. msg_set_lookup_scope(hdr, tipc_addr_scope(domain));
  863. dport = tipc_nametbl_translate(net, type, inst, &dnode);
  864. msg_set_destnode(hdr, dnode);
  865. msg_set_destport(hdr, dport);
  866. if (unlikely(!dport && !dnode))
  867. return -EHOSTUNREACH;
  868. } else if (dest->addrtype == TIPC_ADDR_ID) {
  869. dnode = dest->addr.id.node;
  870. msg_set_type(hdr, TIPC_DIRECT_MSG);
  871. msg_set_lookup_scope(hdr, 0);
  872. msg_set_destnode(hdr, dnode);
  873. msg_set_destport(hdr, dest->addr.id.ref);
  874. msg_set_hdr_sz(hdr, BASIC_H_SIZE);
  875. }
  876. /* Block or return if destination link is congested */
  877. rc = tipc_wait_for_cond(sock, &timeout, !u32_find(clinks, dnode));
  878. if (unlikely(rc))
  879. return rc;
  880. skb_queue_head_init(&pkts);
  881. mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
  882. rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
  883. if (unlikely(rc != dlen))
  884. return rc;
  885. rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
  886. if (unlikely(rc == -ELINKCONG)) {
  887. u32_push(clinks, dnode);
  888. tsk->cong_link_cnt++;
  889. rc = 0;
  890. }
  891. if (unlikely(syn && !rc))
  892. tipc_set_sk_state(sk, TIPC_CONNECTING);
  893. return rc ? rc : dlen;
  894. }
  895. /**
  896. * tipc_sendstream - send stream-oriented data
  897. * @sock: socket structure
  898. * @m: data to send
  899. * @dsz: total length of data to be transmitted
  900. *
  901. * Used for SOCK_STREAM data.
  902. *
  903. * Returns the number of bytes sent on success (or partial success),
  904. * or errno if no data sent
  905. */
  906. static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
  907. {
  908. struct sock *sk = sock->sk;
  909. int ret;
  910. lock_sock(sk);
  911. ret = __tipc_sendstream(sock, m, dsz);
  912. release_sock(sk);
  913. return ret;
  914. }
  915. static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
  916. {
  917. struct sock *sk = sock->sk;
  918. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  919. long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  920. struct tipc_sock *tsk = tipc_sk(sk);
  921. struct tipc_msg *hdr = &tsk->phdr;
  922. struct net *net = sock_net(sk);
  923. struct sk_buff_head pkts;
  924. u32 dnode = tsk_peer_node(tsk);
  925. int send, sent = 0;
  926. int rc = 0;
  927. skb_queue_head_init(&pkts);
  928. if (unlikely(dlen > INT_MAX))
  929. return -EMSGSIZE;
  930. /* Handle implicit connection setup */
  931. if (unlikely(dest)) {
  932. rc = __tipc_sendmsg(sock, m, dlen);
  933. if (dlen && (dlen == rc))
  934. tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
  935. return rc;
  936. }
  937. do {
  938. rc = tipc_wait_for_cond(sock, &timeout,
  939. (!tsk->cong_link_cnt &&
  940. !tsk_conn_cong(tsk) &&
  941. tipc_sk_connected(sk)));
  942. if (unlikely(rc))
  943. break;
  944. send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
  945. rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
  946. if (unlikely(rc != send))
  947. break;
  948. rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
  949. if (unlikely(rc == -ELINKCONG)) {
  950. tsk->cong_link_cnt = 1;
  951. rc = 0;
  952. }
  953. if (likely(!rc)) {
  954. tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
  955. sent += send;
  956. }
  957. } while (sent < dlen && !rc);
  958. return sent ? sent : rc;
  959. }
  960. /**
  961. * tipc_send_packet - send a connection-oriented message
  962. * @sock: socket structure
  963. * @m: message to send
  964. * @dsz: length of data to be transmitted
  965. *
  966. * Used for SOCK_SEQPACKET messages.
  967. *
  968. * Returns the number of bytes sent on success, or errno otherwise
  969. */
  970. static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
  971. {
  972. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  973. return -EMSGSIZE;
  974. return tipc_sendstream(sock, m, dsz);
  975. }
  976. /* tipc_sk_finish_conn - complete the setup of a connection
  977. */
  978. static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
  979. u32 peer_node)
  980. {
  981. struct sock *sk = &tsk->sk;
  982. struct net *net = sock_net(sk);
  983. struct tipc_msg *msg = &tsk->phdr;
  984. msg_set_destnode(msg, peer_node);
  985. msg_set_destport(msg, peer_port);
  986. msg_set_type(msg, TIPC_CONN_MSG);
  987. msg_set_lookup_scope(msg, 0);
  988. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  989. sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
  990. tipc_set_sk_state(sk, TIPC_ESTABLISHED);
  991. tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
  992. tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
  993. tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
  994. if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
  995. return;
  996. /* Fall back to message based flow control */
  997. tsk->rcv_win = FLOWCTL_MSG_WIN;
  998. tsk->snd_win = FLOWCTL_MSG_WIN;
  999. }
  1000. /**
  1001. * set_orig_addr - capture sender's address for received message
  1002. * @m: descriptor for message info
  1003. * @msg: received message header
  1004. *
  1005. * Note: Address is not captured if not requested by receiver.
  1006. */
  1007. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  1008. {
  1009. DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
  1010. if (addr) {
  1011. addr->family = AF_TIPC;
  1012. addr->addrtype = TIPC_ADDR_ID;
  1013. memset(&addr->addr, 0, sizeof(addr->addr));
  1014. addr->addr.id.ref = msg_origport(msg);
  1015. addr->addr.id.node = msg_orignode(msg);
  1016. addr->addr.name.domain = 0; /* could leave uninitialized */
  1017. addr->scope = 0; /* could leave uninitialized */
  1018. m->msg_namelen = sizeof(struct sockaddr_tipc);
  1019. }
  1020. }
  1021. /**
  1022. * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
  1023. * @m: descriptor for message info
  1024. * @msg: received message header
  1025. * @tsk: TIPC port associated with message
  1026. *
  1027. * Note: Ancillary data is not captured if not requested by receiver.
  1028. *
  1029. * Returns 0 if successful, otherwise errno
  1030. */
  1031. static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  1032. struct tipc_sock *tsk)
  1033. {
  1034. u32 anc_data[3];
  1035. u32 err;
  1036. u32 dest_type;
  1037. int has_name;
  1038. int res;
  1039. if (likely(m->msg_controllen == 0))
  1040. return 0;
  1041. /* Optionally capture errored message object(s) */
  1042. err = msg ? msg_errcode(msg) : 0;
  1043. if (unlikely(err)) {
  1044. anc_data[0] = err;
  1045. anc_data[1] = msg_data_sz(msg);
  1046. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  1047. if (res)
  1048. return res;
  1049. if (anc_data[1]) {
  1050. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  1051. msg_data(msg));
  1052. if (res)
  1053. return res;
  1054. }
  1055. }
  1056. /* Optionally capture message destination object */
  1057. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  1058. switch (dest_type) {
  1059. case TIPC_NAMED_MSG:
  1060. has_name = 1;
  1061. anc_data[0] = msg_nametype(msg);
  1062. anc_data[1] = msg_namelower(msg);
  1063. anc_data[2] = msg_namelower(msg);
  1064. break;
  1065. case TIPC_MCAST_MSG:
  1066. has_name = 1;
  1067. anc_data[0] = msg_nametype(msg);
  1068. anc_data[1] = msg_namelower(msg);
  1069. anc_data[2] = msg_nameupper(msg);
  1070. break;
  1071. case TIPC_CONN_MSG:
  1072. has_name = (tsk->conn_type != 0);
  1073. anc_data[0] = tsk->conn_type;
  1074. anc_data[1] = tsk->conn_instance;
  1075. anc_data[2] = tsk->conn_instance;
  1076. break;
  1077. default:
  1078. has_name = 0;
  1079. }
  1080. if (has_name) {
  1081. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  1082. if (res)
  1083. return res;
  1084. }
  1085. return 0;
  1086. }
  1087. static void tipc_sk_send_ack(struct tipc_sock *tsk)
  1088. {
  1089. struct sock *sk = &tsk->sk;
  1090. struct net *net = sock_net(sk);
  1091. struct sk_buff *skb = NULL;
  1092. struct tipc_msg *msg;
  1093. u32 peer_port = tsk_peer_port(tsk);
  1094. u32 dnode = tsk_peer_node(tsk);
  1095. if (!tipc_sk_connected(sk))
  1096. return;
  1097. skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
  1098. dnode, tsk_own_node(tsk), peer_port,
  1099. tsk->portid, TIPC_OK);
  1100. if (!skb)
  1101. return;
  1102. msg = buf_msg(skb);
  1103. msg_set_conn_ack(msg, tsk->rcv_unacked);
  1104. tsk->rcv_unacked = 0;
  1105. /* Adjust to and advertize the correct window limit */
  1106. if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
  1107. tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
  1108. msg_set_adv_win(msg, tsk->rcv_win);
  1109. }
  1110. tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
  1111. }
  1112. static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
  1113. {
  1114. struct sock *sk = sock->sk;
  1115. DEFINE_WAIT(wait);
  1116. long timeo = *timeop;
  1117. int err;
  1118. for (;;) {
  1119. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1120. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1121. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  1122. err = -ENOTCONN;
  1123. break;
  1124. }
  1125. release_sock(sk);
  1126. timeo = schedule_timeout(timeo);
  1127. lock_sock(sk);
  1128. }
  1129. err = 0;
  1130. if (!skb_queue_empty(&sk->sk_receive_queue))
  1131. break;
  1132. err = -EAGAIN;
  1133. if (!timeo)
  1134. break;
  1135. err = sock_intr_errno(timeo);
  1136. if (signal_pending(current))
  1137. break;
  1138. }
  1139. finish_wait(sk_sleep(sk), &wait);
  1140. *timeop = timeo;
  1141. return err;
  1142. }
  1143. /**
  1144. * tipc_recvmsg - receive packet-oriented message
  1145. * @m: descriptor for message info
  1146. * @buflen: length of user buffer area
  1147. * @flags: receive flags
  1148. *
  1149. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  1150. * If the complete message doesn't fit in user area, truncate it.
  1151. *
  1152. * Returns size of returned message data, errno otherwise
  1153. */
  1154. static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
  1155. size_t buflen, int flags)
  1156. {
  1157. struct sock *sk = sock->sk;
  1158. struct tipc_sock *tsk = tipc_sk(sk);
  1159. struct sk_buff *skb;
  1160. struct tipc_msg *hdr;
  1161. bool connected = !tipc_sk_type_connectionless(sk);
  1162. int rc, err, hlen, dlen, copy;
  1163. long timeout;
  1164. /* Catch invalid receive requests */
  1165. if (unlikely(!buflen))
  1166. return -EINVAL;
  1167. lock_sock(sk);
  1168. if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
  1169. rc = -ENOTCONN;
  1170. goto exit;
  1171. }
  1172. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1173. do {
  1174. /* Look at first msg in receive queue; wait if necessary */
  1175. rc = tipc_wait_for_rcvmsg(sock, &timeout);
  1176. if (unlikely(rc))
  1177. goto exit;
  1178. skb = skb_peek(&sk->sk_receive_queue);
  1179. hdr = buf_msg(skb);
  1180. dlen = msg_data_sz(hdr);
  1181. hlen = msg_hdr_sz(hdr);
  1182. err = msg_errcode(hdr);
  1183. if (likely(dlen || err))
  1184. break;
  1185. tsk_advance_rx_queue(sk);
  1186. } while (1);
  1187. /* Collect msg meta data, including error code and rejected data */
  1188. set_orig_addr(m, hdr);
  1189. rc = tipc_sk_anc_data_recv(m, hdr, tsk);
  1190. if (unlikely(rc))
  1191. goto exit;
  1192. /* Capture data if non-error msg, otherwise just set return value */
  1193. if (likely(!err)) {
  1194. copy = min_t(int, dlen, buflen);
  1195. if (unlikely(copy != dlen))
  1196. m->msg_flags |= MSG_TRUNC;
  1197. rc = skb_copy_datagram_msg(skb, hlen, m, copy);
  1198. } else {
  1199. copy = 0;
  1200. rc = 0;
  1201. if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control)
  1202. rc = -ECONNRESET;
  1203. }
  1204. if (unlikely(rc))
  1205. goto exit;
  1206. /* Caption of data or error code/rejected data was successful */
  1207. if (unlikely(flags & MSG_PEEK))
  1208. goto exit;
  1209. tsk_advance_rx_queue(sk);
  1210. if (likely(!connected))
  1211. goto exit;
  1212. /* Send connection flow control ack when applicable */
  1213. tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
  1214. if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
  1215. tipc_sk_send_ack(tsk);
  1216. exit:
  1217. release_sock(sk);
  1218. return rc ? rc : copy;
  1219. }
  1220. /**
  1221. * tipc_recvstream - receive stream-oriented data
  1222. * @m: descriptor for message info
  1223. * @buflen: total size of user buffer area
  1224. * @flags: receive flags
  1225. *
  1226. * Used for SOCK_STREAM messages only. If not enough data is available
  1227. * will optionally wait for more; never truncates data.
  1228. *
  1229. * Returns size of returned message data, errno otherwise
  1230. */
  1231. static int tipc_recvstream(struct socket *sock, struct msghdr *m,
  1232. size_t buflen, int flags)
  1233. {
  1234. struct sock *sk = sock->sk;
  1235. struct tipc_sock *tsk = tipc_sk(sk);
  1236. struct sk_buff *skb;
  1237. struct tipc_msg *hdr;
  1238. struct tipc_skb_cb *skb_cb;
  1239. bool peek = flags & MSG_PEEK;
  1240. int offset, required, copy, copied = 0;
  1241. int hlen, dlen, err, rc;
  1242. long timeout;
  1243. /* Catch invalid receive attempts */
  1244. if (unlikely(!buflen))
  1245. return -EINVAL;
  1246. lock_sock(sk);
  1247. if (unlikely(sk->sk_state == TIPC_OPEN)) {
  1248. rc = -ENOTCONN;
  1249. goto exit;
  1250. }
  1251. required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
  1252. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1253. do {
  1254. /* Look at first msg in receive queue; wait if necessary */
  1255. rc = tipc_wait_for_rcvmsg(sock, &timeout);
  1256. if (unlikely(rc))
  1257. break;
  1258. skb = skb_peek(&sk->sk_receive_queue);
  1259. skb_cb = TIPC_SKB_CB(skb);
  1260. hdr = buf_msg(skb);
  1261. dlen = msg_data_sz(hdr);
  1262. hlen = msg_hdr_sz(hdr);
  1263. err = msg_errcode(hdr);
  1264. /* Discard any empty non-errored (SYN-) message */
  1265. if (unlikely(!dlen && !err)) {
  1266. tsk_advance_rx_queue(sk);
  1267. continue;
  1268. }
  1269. /* Collect msg meta data, incl. error code and rejected data */
  1270. if (!copied) {
  1271. set_orig_addr(m, hdr);
  1272. rc = tipc_sk_anc_data_recv(m, hdr, tsk);
  1273. if (rc)
  1274. break;
  1275. }
  1276. /* Copy data if msg ok, otherwise return error/partial data */
  1277. if (likely(!err)) {
  1278. offset = skb_cb->bytes_read;
  1279. copy = min_t(int, dlen - offset, buflen - copied);
  1280. rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
  1281. if (unlikely(rc))
  1282. break;
  1283. copied += copy;
  1284. offset += copy;
  1285. if (unlikely(offset < dlen)) {
  1286. if (!peek)
  1287. skb_cb->bytes_read = offset;
  1288. break;
  1289. }
  1290. } else {
  1291. rc = 0;
  1292. if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
  1293. rc = -ECONNRESET;
  1294. if (copied || rc)
  1295. break;
  1296. }
  1297. if (unlikely(peek))
  1298. break;
  1299. tsk_advance_rx_queue(sk);
  1300. /* Send connection flow control advertisement when applicable */
  1301. tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
  1302. if (unlikely(tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE))
  1303. tipc_sk_send_ack(tsk);
  1304. /* Exit if all requested data or FIN/error received */
  1305. if (copied == buflen || err)
  1306. break;
  1307. } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
  1308. exit:
  1309. release_sock(sk);
  1310. return copied ? copied : rc;
  1311. }
  1312. /**
  1313. * tipc_write_space - wake up thread if port congestion is released
  1314. * @sk: socket
  1315. */
  1316. static void tipc_write_space(struct sock *sk)
  1317. {
  1318. struct socket_wq *wq;
  1319. rcu_read_lock();
  1320. wq = rcu_dereference(sk->sk_wq);
  1321. if (skwq_has_sleeper(wq))
  1322. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1323. POLLWRNORM | POLLWRBAND);
  1324. rcu_read_unlock();
  1325. }
  1326. /**
  1327. * tipc_data_ready - wake up threads to indicate messages have been received
  1328. * @sk: socket
  1329. * @len: the length of messages
  1330. */
  1331. static void tipc_data_ready(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, POLLIN |
  1338. POLLRDNORM | POLLRDBAND);
  1339. rcu_read_unlock();
  1340. }
  1341. static void tipc_sock_destruct(struct sock *sk)
  1342. {
  1343. __skb_queue_purge(&sk->sk_receive_queue);
  1344. }
  1345. /**
  1346. * filter_connect - Handle all incoming messages for a connection-based socket
  1347. * @tsk: TIPC socket
  1348. * @skb: pointer to message buffer. Set to NULL if buffer is consumed
  1349. *
  1350. * Returns true if everything ok, false otherwise
  1351. */
  1352. static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
  1353. {
  1354. struct sock *sk = &tsk->sk;
  1355. struct net *net = sock_net(sk);
  1356. struct tipc_msg *hdr = buf_msg(skb);
  1357. if (unlikely(msg_mcast(hdr)))
  1358. return false;
  1359. switch (sk->sk_state) {
  1360. case TIPC_CONNECTING:
  1361. /* Accept only ACK or NACK message */
  1362. if (unlikely(!msg_connected(hdr)))
  1363. return false;
  1364. if (unlikely(msg_errcode(hdr))) {
  1365. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1366. sk->sk_err = ECONNREFUSED;
  1367. return true;
  1368. }
  1369. if (unlikely(!msg_isdata(hdr))) {
  1370. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1371. sk->sk_err = EINVAL;
  1372. return true;
  1373. }
  1374. tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
  1375. msg_set_importance(&tsk->phdr, msg_importance(hdr));
  1376. /* If 'ACK+' message, add to socket receive queue */
  1377. if (msg_data_sz(hdr))
  1378. return true;
  1379. /* If empty 'ACK-' message, wake up sleeping connect() */
  1380. if (waitqueue_active(sk_sleep(sk)))
  1381. wake_up_interruptible(sk_sleep(sk));
  1382. /* 'ACK-' message is neither accepted nor rejected: */
  1383. msg_set_dest_droppable(hdr, 1);
  1384. return false;
  1385. case TIPC_OPEN:
  1386. case TIPC_DISCONNECTING:
  1387. break;
  1388. case TIPC_LISTEN:
  1389. /* Accept only SYN message */
  1390. if (!msg_connected(hdr) && !(msg_errcode(hdr)))
  1391. return true;
  1392. break;
  1393. case TIPC_ESTABLISHED:
  1394. /* Accept only connection-based messages sent by peer */
  1395. if (unlikely(!tsk_peer_msg(tsk, hdr)))
  1396. return false;
  1397. if (unlikely(msg_errcode(hdr))) {
  1398. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1399. /* Let timer expire on it's own */
  1400. tipc_node_remove_conn(net, tsk_peer_node(tsk),
  1401. tsk->portid);
  1402. sk->sk_state_change(sk);
  1403. }
  1404. return true;
  1405. default:
  1406. pr_err("Unknown sk_state %u\n", sk->sk_state);
  1407. }
  1408. return false;
  1409. }
  1410. /**
  1411. * rcvbuf_limit - get proper overload limit of socket receive queue
  1412. * @sk: socket
  1413. * @skb: message
  1414. *
  1415. * For connection oriented messages, irrespective of importance,
  1416. * default queue limit is 2 MB.
  1417. *
  1418. * For connectionless messages, queue limits are based on message
  1419. * importance as follows:
  1420. *
  1421. * TIPC_LOW_IMPORTANCE (2 MB)
  1422. * TIPC_MEDIUM_IMPORTANCE (4 MB)
  1423. * TIPC_HIGH_IMPORTANCE (8 MB)
  1424. * TIPC_CRITICAL_IMPORTANCE (16 MB)
  1425. *
  1426. * Returns overload limit according to corresponding message importance
  1427. */
  1428. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
  1429. {
  1430. struct tipc_sock *tsk = tipc_sk(sk);
  1431. struct tipc_msg *hdr = buf_msg(skb);
  1432. if (unlikely(!msg_connected(hdr)))
  1433. return sk->sk_rcvbuf << msg_importance(hdr);
  1434. if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
  1435. return sk->sk_rcvbuf;
  1436. return FLOWCTL_MSG_LIM;
  1437. }
  1438. /**
  1439. * filter_rcv - validate incoming message
  1440. * @sk: socket
  1441. * @skb: pointer to message.
  1442. *
  1443. * Enqueues message on receive queue if acceptable; optionally handles
  1444. * disconnect indication for a connected socket.
  1445. *
  1446. * Called with socket lock already taken
  1447. *
  1448. * Returns true if message was added to socket receive queue, otherwise false
  1449. */
  1450. static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
  1451. struct sk_buff_head *xmitq)
  1452. {
  1453. struct tipc_sock *tsk = tipc_sk(sk);
  1454. struct tipc_msg *hdr = buf_msg(skb);
  1455. unsigned int limit = rcvbuf_limit(sk, skb);
  1456. int err = TIPC_OK;
  1457. int usr = msg_user(hdr);
  1458. u32 onode;
  1459. if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
  1460. tipc_sk_proto_rcv(tsk, skb, xmitq);
  1461. return false;
  1462. }
  1463. if (unlikely(usr == SOCK_WAKEUP)) {
  1464. onode = msg_orignode(hdr);
  1465. kfree_skb(skb);
  1466. u32_del(&tsk->cong_links, onode);
  1467. tsk->cong_link_cnt--;
  1468. sk->sk_write_space(sk);
  1469. return false;
  1470. }
  1471. /* Drop if illegal message type */
  1472. if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
  1473. kfree_skb(skb);
  1474. return false;
  1475. }
  1476. /* Reject if wrong message type for current socket state */
  1477. if (tipc_sk_type_connectionless(sk)) {
  1478. if (msg_connected(hdr)) {
  1479. err = TIPC_ERR_NO_PORT;
  1480. goto reject;
  1481. }
  1482. } else if (unlikely(!filter_connect(tsk, skb))) {
  1483. err = TIPC_ERR_NO_PORT;
  1484. goto reject;
  1485. }
  1486. /* Reject message if there isn't room to queue it */
  1487. if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
  1488. err = TIPC_ERR_OVERLOAD;
  1489. goto reject;
  1490. }
  1491. /* Enqueue message */
  1492. TIPC_SKB_CB(skb)->bytes_read = 0;
  1493. __skb_queue_tail(&sk->sk_receive_queue, skb);
  1494. skb_set_owner_r(skb, sk);
  1495. sk->sk_data_ready(sk);
  1496. return true;
  1497. reject:
  1498. if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
  1499. __skb_queue_tail(xmitq, skb);
  1500. return false;
  1501. }
  1502. /**
  1503. * tipc_backlog_rcv - handle incoming message from backlog queue
  1504. * @sk: socket
  1505. * @skb: message
  1506. *
  1507. * Caller must hold socket lock
  1508. *
  1509. * Returns 0
  1510. */
  1511. static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  1512. {
  1513. unsigned int truesize = skb->truesize;
  1514. struct sk_buff_head xmitq;
  1515. u32 dnode, selector;
  1516. __skb_queue_head_init(&xmitq);
  1517. if (likely(filter_rcv(sk, skb, &xmitq))) {
  1518. atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
  1519. return 0;
  1520. }
  1521. if (skb_queue_empty(&xmitq))
  1522. return 0;
  1523. /* Send response/rejected message */
  1524. skb = __skb_dequeue(&xmitq);
  1525. dnode = msg_destnode(buf_msg(skb));
  1526. selector = msg_origport(buf_msg(skb));
  1527. tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
  1528. return 0;
  1529. }
  1530. /**
  1531. * tipc_sk_enqueue - extract all buffers with destination 'dport' from
  1532. * inputq and try adding them to socket or backlog queue
  1533. * @inputq: list of incoming buffers with potentially different destinations
  1534. * @sk: socket where the buffers should be enqueued
  1535. * @dport: port number for the socket
  1536. *
  1537. * Caller must hold socket lock
  1538. */
  1539. static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
  1540. u32 dport, struct sk_buff_head *xmitq)
  1541. {
  1542. unsigned long time_limit = jiffies + 2;
  1543. struct sk_buff *skb;
  1544. unsigned int lim;
  1545. atomic_t *dcnt;
  1546. u32 onode;
  1547. while (skb_queue_len(inputq)) {
  1548. if (unlikely(time_after_eq(jiffies, time_limit)))
  1549. return;
  1550. skb = tipc_skb_dequeue(inputq, dport);
  1551. if (unlikely(!skb))
  1552. return;
  1553. /* Add message directly to receive queue if possible */
  1554. if (!sock_owned_by_user(sk)) {
  1555. filter_rcv(sk, skb, xmitq);
  1556. continue;
  1557. }
  1558. /* Try backlog, compensating for double-counted bytes */
  1559. dcnt = &tipc_sk(sk)->dupl_rcvcnt;
  1560. if (!sk->sk_backlog.len)
  1561. atomic_set(dcnt, 0);
  1562. lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
  1563. if (likely(!sk_add_backlog(sk, skb, lim)))
  1564. continue;
  1565. /* Overload => reject message back to sender */
  1566. onode = tipc_own_addr(sock_net(sk));
  1567. if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
  1568. __skb_queue_tail(xmitq, skb);
  1569. break;
  1570. }
  1571. }
  1572. /**
  1573. * tipc_sk_rcv - handle a chain of incoming buffers
  1574. * @inputq: buffer list containing the buffers
  1575. * Consumes all buffers in list until inputq is empty
  1576. * Note: may be called in multiple threads referring to the same queue
  1577. */
  1578. void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
  1579. {
  1580. struct sk_buff_head xmitq;
  1581. u32 dnode, dport = 0;
  1582. int err;
  1583. struct tipc_sock *tsk;
  1584. struct sock *sk;
  1585. struct sk_buff *skb;
  1586. __skb_queue_head_init(&xmitq);
  1587. while (skb_queue_len(inputq)) {
  1588. dport = tipc_skb_peek_port(inputq, dport);
  1589. tsk = tipc_sk_lookup(net, dport);
  1590. if (likely(tsk)) {
  1591. sk = &tsk->sk;
  1592. if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
  1593. tipc_sk_enqueue(inputq, sk, dport, &xmitq);
  1594. spin_unlock_bh(&sk->sk_lock.slock);
  1595. }
  1596. /* Send pending response/rejected messages, if any */
  1597. while ((skb = __skb_dequeue(&xmitq))) {
  1598. dnode = msg_destnode(buf_msg(skb));
  1599. tipc_node_xmit_skb(net, skb, dnode, dport);
  1600. }
  1601. sock_put(sk);
  1602. continue;
  1603. }
  1604. /* No destination socket => dequeue skb if still there */
  1605. skb = tipc_skb_dequeue(inputq, dport);
  1606. if (!skb)
  1607. return;
  1608. /* Try secondary lookup if unresolved named message */
  1609. err = TIPC_ERR_NO_PORT;
  1610. if (tipc_msg_lookup_dest(net, skb, &err))
  1611. goto xmit;
  1612. /* Prepare for message rejection */
  1613. if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
  1614. continue;
  1615. xmit:
  1616. dnode = msg_destnode(buf_msg(skb));
  1617. tipc_node_xmit_skb(net, skb, dnode, dport);
  1618. }
  1619. }
  1620. static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
  1621. {
  1622. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  1623. struct sock *sk = sock->sk;
  1624. int done;
  1625. do {
  1626. int err = sock_error(sk);
  1627. if (err)
  1628. return err;
  1629. if (!*timeo_p)
  1630. return -ETIMEDOUT;
  1631. if (signal_pending(current))
  1632. return sock_intr_errno(*timeo_p);
  1633. add_wait_queue(sk_sleep(sk), &wait);
  1634. done = sk_wait_event(sk, timeo_p,
  1635. sk->sk_state != TIPC_CONNECTING, &wait);
  1636. remove_wait_queue(sk_sleep(sk), &wait);
  1637. } while (!done);
  1638. return 0;
  1639. }
  1640. /**
  1641. * tipc_connect - establish a connection to another TIPC port
  1642. * @sock: socket structure
  1643. * @dest: socket address for destination port
  1644. * @destlen: size of socket address data structure
  1645. * @flags: file-related flags associated with socket
  1646. *
  1647. * Returns 0 on success, errno otherwise
  1648. */
  1649. static int tipc_connect(struct socket *sock, struct sockaddr *dest,
  1650. int destlen, int flags)
  1651. {
  1652. struct sock *sk = sock->sk;
  1653. struct tipc_sock *tsk = tipc_sk(sk);
  1654. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1655. struct msghdr m = {NULL,};
  1656. long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
  1657. int previous;
  1658. int res = 0;
  1659. lock_sock(sk);
  1660. /* DGRAM/RDM connect(), just save the destaddr */
  1661. if (tipc_sk_type_connectionless(sk)) {
  1662. if (dst->family == AF_UNSPEC) {
  1663. memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
  1664. } else if (destlen != sizeof(struct sockaddr_tipc)) {
  1665. res = -EINVAL;
  1666. } else {
  1667. memcpy(&tsk->peer, dest, destlen);
  1668. }
  1669. goto exit;
  1670. }
  1671. /*
  1672. * Reject connection attempt using multicast address
  1673. *
  1674. * Note: send_msg() validates the rest of the address fields,
  1675. * so there's no need to do it here
  1676. */
  1677. if (dst->addrtype == TIPC_ADDR_MCAST) {
  1678. res = -EINVAL;
  1679. goto exit;
  1680. }
  1681. previous = sk->sk_state;
  1682. switch (sk->sk_state) {
  1683. case TIPC_OPEN:
  1684. /* Send a 'SYN-' to destination */
  1685. m.msg_name = dest;
  1686. m.msg_namelen = destlen;
  1687. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  1688. * indicate send_msg() is never blocked.
  1689. */
  1690. if (!timeout)
  1691. m.msg_flags = MSG_DONTWAIT;
  1692. res = __tipc_sendmsg(sock, &m, 0);
  1693. if ((res < 0) && (res != -EWOULDBLOCK))
  1694. goto exit;
  1695. /* Just entered TIPC_CONNECTING state; the only
  1696. * difference is that return value in non-blocking
  1697. * case is EINPROGRESS, rather than EALREADY.
  1698. */
  1699. res = -EINPROGRESS;
  1700. /* fall thru' */
  1701. case TIPC_CONNECTING:
  1702. if (!timeout) {
  1703. if (previous == TIPC_CONNECTING)
  1704. res = -EALREADY;
  1705. goto exit;
  1706. }
  1707. timeout = msecs_to_jiffies(timeout);
  1708. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  1709. res = tipc_wait_for_connect(sock, &timeout);
  1710. break;
  1711. case TIPC_ESTABLISHED:
  1712. res = -EISCONN;
  1713. break;
  1714. default:
  1715. res = -EINVAL;
  1716. }
  1717. exit:
  1718. release_sock(sk);
  1719. return res;
  1720. }
  1721. /**
  1722. * tipc_listen - allow socket to listen for incoming connections
  1723. * @sock: socket structure
  1724. * @len: (unused)
  1725. *
  1726. * Returns 0 on success, errno otherwise
  1727. */
  1728. static int tipc_listen(struct socket *sock, int len)
  1729. {
  1730. struct sock *sk = sock->sk;
  1731. int res;
  1732. lock_sock(sk);
  1733. res = tipc_set_sk_state(sk, TIPC_LISTEN);
  1734. release_sock(sk);
  1735. return res;
  1736. }
  1737. static int tipc_wait_for_accept(struct socket *sock, long timeo)
  1738. {
  1739. struct sock *sk = sock->sk;
  1740. DEFINE_WAIT(wait);
  1741. int err;
  1742. /* True wake-one mechanism for incoming connections: only
  1743. * one process gets woken up, not the 'whole herd'.
  1744. * Since we do not 'race & poll' for established sockets
  1745. * anymore, the common case will execute the loop only once.
  1746. */
  1747. for (;;) {
  1748. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  1749. TASK_INTERRUPTIBLE);
  1750. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1751. release_sock(sk);
  1752. timeo = schedule_timeout(timeo);
  1753. lock_sock(sk);
  1754. }
  1755. err = 0;
  1756. if (!skb_queue_empty(&sk->sk_receive_queue))
  1757. break;
  1758. err = -EAGAIN;
  1759. if (!timeo)
  1760. break;
  1761. err = sock_intr_errno(timeo);
  1762. if (signal_pending(current))
  1763. break;
  1764. }
  1765. finish_wait(sk_sleep(sk), &wait);
  1766. return err;
  1767. }
  1768. /**
  1769. * tipc_accept - wait for connection request
  1770. * @sock: listening socket
  1771. * @newsock: new socket that is to be connected
  1772. * @flags: file-related flags associated with socket
  1773. *
  1774. * Returns 0 on success, errno otherwise
  1775. */
  1776. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
  1777. bool kern)
  1778. {
  1779. struct sock *new_sk, *sk = sock->sk;
  1780. struct sk_buff *buf;
  1781. struct tipc_sock *new_tsock;
  1782. struct tipc_msg *msg;
  1783. long timeo;
  1784. int res;
  1785. lock_sock(sk);
  1786. if (sk->sk_state != TIPC_LISTEN) {
  1787. res = -EINVAL;
  1788. goto exit;
  1789. }
  1790. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  1791. res = tipc_wait_for_accept(sock, timeo);
  1792. if (res)
  1793. goto exit;
  1794. buf = skb_peek(&sk->sk_receive_queue);
  1795. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
  1796. if (res)
  1797. goto exit;
  1798. security_sk_clone(sock->sk, new_sock->sk);
  1799. new_sk = new_sock->sk;
  1800. new_tsock = tipc_sk(new_sk);
  1801. msg = buf_msg(buf);
  1802. /* we lock on new_sk; but lockdep sees the lock on sk */
  1803. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  1804. /*
  1805. * Reject any stray messages received by new socket
  1806. * before the socket lock was taken (very, very unlikely)
  1807. */
  1808. tsk_rej_rx_queue(new_sk);
  1809. /* Connect new socket to it's peer */
  1810. tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
  1811. tsk_set_importance(new_tsock, msg_importance(msg));
  1812. if (msg_named(msg)) {
  1813. new_tsock->conn_type = msg_nametype(msg);
  1814. new_tsock->conn_instance = msg_nameinst(msg);
  1815. }
  1816. /*
  1817. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1818. * Respond to 'SYN+' by queuing it on new socket.
  1819. */
  1820. if (!msg_data_sz(msg)) {
  1821. struct msghdr m = {NULL,};
  1822. tsk_advance_rx_queue(sk);
  1823. __tipc_sendstream(new_sock, &m, 0);
  1824. } else {
  1825. __skb_dequeue(&sk->sk_receive_queue);
  1826. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  1827. skb_set_owner_r(buf, new_sk);
  1828. }
  1829. release_sock(new_sk);
  1830. exit:
  1831. release_sock(sk);
  1832. return res;
  1833. }
  1834. /**
  1835. * tipc_shutdown - shutdown socket connection
  1836. * @sock: socket structure
  1837. * @how: direction to close (must be SHUT_RDWR)
  1838. *
  1839. * Terminates connection (if necessary), then purges socket's receive queue.
  1840. *
  1841. * Returns 0 on success, errno otherwise
  1842. */
  1843. static int tipc_shutdown(struct socket *sock, int how)
  1844. {
  1845. struct sock *sk = sock->sk;
  1846. int res;
  1847. if (how != SHUT_RDWR)
  1848. return -EINVAL;
  1849. lock_sock(sk);
  1850. __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
  1851. sk->sk_shutdown = SEND_SHUTDOWN;
  1852. if (sk->sk_state == TIPC_DISCONNECTING) {
  1853. /* Discard any unreceived messages */
  1854. __skb_queue_purge(&sk->sk_receive_queue);
  1855. /* Wake up anyone sleeping in poll */
  1856. sk->sk_state_change(sk);
  1857. res = 0;
  1858. } else {
  1859. res = -ENOTCONN;
  1860. }
  1861. release_sock(sk);
  1862. return res;
  1863. }
  1864. static void tipc_sk_timeout(unsigned long data)
  1865. {
  1866. struct tipc_sock *tsk = (struct tipc_sock *)data;
  1867. struct sock *sk = &tsk->sk;
  1868. struct sk_buff *skb = NULL;
  1869. u32 peer_port, peer_node;
  1870. u32 own_node = tsk_own_node(tsk);
  1871. bh_lock_sock(sk);
  1872. if (!tipc_sk_connected(sk)) {
  1873. bh_unlock_sock(sk);
  1874. goto exit;
  1875. }
  1876. peer_port = tsk_peer_port(tsk);
  1877. peer_node = tsk_peer_node(tsk);
  1878. if (tsk->probe_unacked) {
  1879. if (!sock_owned_by_user(sk)) {
  1880. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1881. tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
  1882. tsk_peer_port(tsk));
  1883. sk->sk_state_change(sk);
  1884. } else {
  1885. /* Try again later */
  1886. sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
  1887. }
  1888. bh_unlock_sock(sk);
  1889. goto exit;
  1890. }
  1891. skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
  1892. INT_H_SIZE, 0, peer_node, own_node,
  1893. peer_port, tsk->portid, TIPC_OK);
  1894. tsk->probe_unacked = true;
  1895. sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
  1896. bh_unlock_sock(sk);
  1897. if (skb)
  1898. tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
  1899. exit:
  1900. sock_put(sk);
  1901. }
  1902. static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
  1903. struct tipc_name_seq const *seq)
  1904. {
  1905. struct sock *sk = &tsk->sk;
  1906. struct net *net = sock_net(sk);
  1907. struct publication *publ;
  1908. u32 key;
  1909. if (tipc_sk_connected(sk))
  1910. return -EINVAL;
  1911. key = tsk->portid + tsk->pub_count + 1;
  1912. if (key == tsk->portid)
  1913. return -EADDRINUSE;
  1914. publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
  1915. scope, tsk->portid, key);
  1916. if (unlikely(!publ))
  1917. return -EINVAL;
  1918. list_add(&publ->pport_list, &tsk->publications);
  1919. tsk->pub_count++;
  1920. tsk->published = 1;
  1921. return 0;
  1922. }
  1923. static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
  1924. struct tipc_name_seq const *seq)
  1925. {
  1926. struct net *net = sock_net(&tsk->sk);
  1927. struct publication *publ;
  1928. struct publication *safe;
  1929. int rc = -EINVAL;
  1930. list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
  1931. if (seq) {
  1932. if (publ->scope != scope)
  1933. continue;
  1934. if (publ->type != seq->type)
  1935. continue;
  1936. if (publ->lower != seq->lower)
  1937. continue;
  1938. if (publ->upper != seq->upper)
  1939. break;
  1940. tipc_nametbl_withdraw(net, publ->type, publ->lower,
  1941. publ->ref, publ->key);
  1942. rc = 0;
  1943. break;
  1944. }
  1945. tipc_nametbl_withdraw(net, publ->type, publ->lower,
  1946. publ->ref, publ->key);
  1947. rc = 0;
  1948. }
  1949. if (list_empty(&tsk->publications))
  1950. tsk->published = 0;
  1951. return rc;
  1952. }
  1953. /* tipc_sk_reinit: set non-zero address in all existing sockets
  1954. * when we go from standalone to network mode.
  1955. */
  1956. void tipc_sk_reinit(struct net *net)
  1957. {
  1958. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1959. struct rhashtable_iter iter;
  1960. struct tipc_sock *tsk;
  1961. struct tipc_msg *msg;
  1962. rhashtable_walk_enter(&tn->sk_rht, &iter);
  1963. do {
  1964. tsk = ERR_PTR(rhashtable_walk_start(&iter));
  1965. if (tsk)
  1966. continue;
  1967. while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
  1968. spin_lock_bh(&tsk->sk.sk_lock.slock);
  1969. msg = &tsk->phdr;
  1970. msg_set_prevnode(msg, tn->own_addr);
  1971. msg_set_orignode(msg, tn->own_addr);
  1972. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  1973. }
  1974. rhashtable_walk_stop(&iter);
  1975. } while (tsk == ERR_PTR(-EAGAIN));
  1976. }
  1977. static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
  1978. {
  1979. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1980. struct tipc_sock *tsk;
  1981. rcu_read_lock();
  1982. tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
  1983. if (tsk)
  1984. sock_hold(&tsk->sk);
  1985. rcu_read_unlock();
  1986. return tsk;
  1987. }
  1988. static int tipc_sk_insert(struct tipc_sock *tsk)
  1989. {
  1990. struct sock *sk = &tsk->sk;
  1991. struct net *net = sock_net(sk);
  1992. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1993. u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
  1994. u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
  1995. while (remaining--) {
  1996. portid++;
  1997. if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
  1998. portid = TIPC_MIN_PORT;
  1999. tsk->portid = portid;
  2000. sock_hold(&tsk->sk);
  2001. if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
  2002. tsk_rht_params))
  2003. return 0;
  2004. sock_put(&tsk->sk);
  2005. }
  2006. return -1;
  2007. }
  2008. static void tipc_sk_remove(struct tipc_sock *tsk)
  2009. {
  2010. struct sock *sk = &tsk->sk;
  2011. struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
  2012. if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
  2013. WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
  2014. __sock_put(sk);
  2015. }
  2016. }
  2017. static const struct rhashtable_params tsk_rht_params = {
  2018. .nelem_hint = 192,
  2019. .head_offset = offsetof(struct tipc_sock, node),
  2020. .key_offset = offsetof(struct tipc_sock, portid),
  2021. .key_len = sizeof(u32), /* portid */
  2022. .max_size = 1048576,
  2023. .min_size = 256,
  2024. .automatic_shrinking = true,
  2025. };
  2026. int tipc_sk_rht_init(struct net *net)
  2027. {
  2028. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2029. return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
  2030. }
  2031. void tipc_sk_rht_destroy(struct net *net)
  2032. {
  2033. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2034. /* Wait for socket readers to complete */
  2035. synchronize_net();
  2036. rhashtable_destroy(&tn->sk_rht);
  2037. }
  2038. /**
  2039. * tipc_setsockopt - set socket option
  2040. * @sock: socket structure
  2041. * @lvl: option level
  2042. * @opt: option identifier
  2043. * @ov: pointer to new option value
  2044. * @ol: length of option value
  2045. *
  2046. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  2047. * (to ease compatibility).
  2048. *
  2049. * Returns 0 on success, errno otherwise
  2050. */
  2051. static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
  2052. char __user *ov, unsigned int ol)
  2053. {
  2054. struct sock *sk = sock->sk;
  2055. struct tipc_sock *tsk = tipc_sk(sk);
  2056. u32 value = 0;
  2057. int res = 0;
  2058. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  2059. return 0;
  2060. if (lvl != SOL_TIPC)
  2061. return -ENOPROTOOPT;
  2062. switch (opt) {
  2063. case TIPC_IMPORTANCE:
  2064. case TIPC_SRC_DROPPABLE:
  2065. case TIPC_DEST_DROPPABLE:
  2066. case TIPC_CONN_TIMEOUT:
  2067. if (ol < sizeof(value))
  2068. return -EINVAL;
  2069. res = get_user(value, (u32 __user *)ov);
  2070. if (res)
  2071. return res;
  2072. break;
  2073. default:
  2074. if (ov || ol)
  2075. return -EINVAL;
  2076. }
  2077. lock_sock(sk);
  2078. switch (opt) {
  2079. case TIPC_IMPORTANCE:
  2080. res = tsk_set_importance(tsk, value);
  2081. break;
  2082. case TIPC_SRC_DROPPABLE:
  2083. if (sock->type != SOCK_STREAM)
  2084. tsk_set_unreliable(tsk, value);
  2085. else
  2086. res = -ENOPROTOOPT;
  2087. break;
  2088. case TIPC_DEST_DROPPABLE:
  2089. tsk_set_unreturnable(tsk, value);
  2090. break;
  2091. case TIPC_CONN_TIMEOUT:
  2092. tipc_sk(sk)->conn_timeout = value;
  2093. break;
  2094. case TIPC_MCAST_BROADCAST:
  2095. tsk->mc_method.rcast = false;
  2096. tsk->mc_method.mandatory = true;
  2097. break;
  2098. case TIPC_MCAST_REPLICAST:
  2099. tsk->mc_method.rcast = true;
  2100. tsk->mc_method.mandatory = true;
  2101. break;
  2102. default:
  2103. res = -EINVAL;
  2104. }
  2105. release_sock(sk);
  2106. return res;
  2107. }
  2108. /**
  2109. * tipc_getsockopt - get socket option
  2110. * @sock: socket structure
  2111. * @lvl: option level
  2112. * @opt: option identifier
  2113. * @ov: receptacle for option value
  2114. * @ol: receptacle for length of option value
  2115. *
  2116. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  2117. * (to ease compatibility).
  2118. *
  2119. * Returns 0 on success, errno otherwise
  2120. */
  2121. static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
  2122. char __user *ov, int __user *ol)
  2123. {
  2124. struct sock *sk = sock->sk;
  2125. struct tipc_sock *tsk = tipc_sk(sk);
  2126. int len;
  2127. u32 value;
  2128. int res;
  2129. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  2130. return put_user(0, ol);
  2131. if (lvl != SOL_TIPC)
  2132. return -ENOPROTOOPT;
  2133. res = get_user(len, ol);
  2134. if (res)
  2135. return res;
  2136. lock_sock(sk);
  2137. switch (opt) {
  2138. case TIPC_IMPORTANCE:
  2139. value = tsk_importance(tsk);
  2140. break;
  2141. case TIPC_SRC_DROPPABLE:
  2142. value = tsk_unreliable(tsk);
  2143. break;
  2144. case TIPC_DEST_DROPPABLE:
  2145. value = tsk_unreturnable(tsk);
  2146. break;
  2147. case TIPC_CONN_TIMEOUT:
  2148. value = tsk->conn_timeout;
  2149. /* no need to set "res", since already 0 at this point */
  2150. break;
  2151. case TIPC_NODE_RECVQ_DEPTH:
  2152. value = 0; /* was tipc_queue_size, now obsolete */
  2153. break;
  2154. case TIPC_SOCK_RECVQ_DEPTH:
  2155. value = skb_queue_len(&sk->sk_receive_queue);
  2156. break;
  2157. default:
  2158. res = -EINVAL;
  2159. }
  2160. release_sock(sk);
  2161. if (res)
  2162. return res; /* "get" failed */
  2163. if (len < sizeof(value))
  2164. return -EINVAL;
  2165. if (copy_to_user(ov, &value, sizeof(value)))
  2166. return -EFAULT;
  2167. return put_user(sizeof(value), ol);
  2168. }
  2169. static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  2170. {
  2171. struct sock *sk = sock->sk;
  2172. struct tipc_sioc_ln_req lnr;
  2173. void __user *argp = (void __user *)arg;
  2174. switch (cmd) {
  2175. case SIOCGETLINKNAME:
  2176. if (copy_from_user(&lnr, argp, sizeof(lnr)))
  2177. return -EFAULT;
  2178. if (!tipc_node_get_linkname(sock_net(sk),
  2179. lnr.bearer_id & 0xffff, lnr.peer,
  2180. lnr.linkname, TIPC_MAX_LINK_NAME)) {
  2181. if (copy_to_user(argp, &lnr, sizeof(lnr)))
  2182. return -EFAULT;
  2183. return 0;
  2184. }
  2185. return -EADDRNOTAVAIL;
  2186. default:
  2187. return -ENOIOCTLCMD;
  2188. }
  2189. }
  2190. static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
  2191. {
  2192. struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
  2193. struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
  2194. u32 onode = tipc_own_addr(sock_net(sock1->sk));
  2195. tsk1->peer.family = AF_TIPC;
  2196. tsk1->peer.addrtype = TIPC_ADDR_ID;
  2197. tsk1->peer.scope = TIPC_NODE_SCOPE;
  2198. tsk1->peer.addr.id.ref = tsk2->portid;
  2199. tsk1->peer.addr.id.node = onode;
  2200. tsk2->peer.family = AF_TIPC;
  2201. tsk2->peer.addrtype = TIPC_ADDR_ID;
  2202. tsk2->peer.scope = TIPC_NODE_SCOPE;
  2203. tsk2->peer.addr.id.ref = tsk1->portid;
  2204. tsk2->peer.addr.id.node = onode;
  2205. tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
  2206. tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
  2207. return 0;
  2208. }
  2209. /* Protocol switches for the various types of TIPC sockets */
  2210. static const struct proto_ops msg_ops = {
  2211. .owner = THIS_MODULE,
  2212. .family = AF_TIPC,
  2213. .release = tipc_release,
  2214. .bind = tipc_bind,
  2215. .connect = tipc_connect,
  2216. .socketpair = tipc_socketpair,
  2217. .accept = sock_no_accept,
  2218. .getname = tipc_getname,
  2219. .poll = tipc_poll,
  2220. .ioctl = tipc_ioctl,
  2221. .listen = sock_no_listen,
  2222. .shutdown = tipc_shutdown,
  2223. .setsockopt = tipc_setsockopt,
  2224. .getsockopt = tipc_getsockopt,
  2225. .sendmsg = tipc_sendmsg,
  2226. .recvmsg = tipc_recvmsg,
  2227. .mmap = sock_no_mmap,
  2228. .sendpage = sock_no_sendpage
  2229. };
  2230. static const struct proto_ops packet_ops = {
  2231. .owner = THIS_MODULE,
  2232. .family = AF_TIPC,
  2233. .release = tipc_release,
  2234. .bind = tipc_bind,
  2235. .connect = tipc_connect,
  2236. .socketpair = tipc_socketpair,
  2237. .accept = tipc_accept,
  2238. .getname = tipc_getname,
  2239. .poll = tipc_poll,
  2240. .ioctl = tipc_ioctl,
  2241. .listen = tipc_listen,
  2242. .shutdown = tipc_shutdown,
  2243. .setsockopt = tipc_setsockopt,
  2244. .getsockopt = tipc_getsockopt,
  2245. .sendmsg = tipc_send_packet,
  2246. .recvmsg = tipc_recvmsg,
  2247. .mmap = sock_no_mmap,
  2248. .sendpage = sock_no_sendpage
  2249. };
  2250. static const struct proto_ops stream_ops = {
  2251. .owner = THIS_MODULE,
  2252. .family = AF_TIPC,
  2253. .release = tipc_release,
  2254. .bind = tipc_bind,
  2255. .connect = tipc_connect,
  2256. .socketpair = tipc_socketpair,
  2257. .accept = tipc_accept,
  2258. .getname = tipc_getname,
  2259. .poll = tipc_poll,
  2260. .ioctl = tipc_ioctl,
  2261. .listen = tipc_listen,
  2262. .shutdown = tipc_shutdown,
  2263. .setsockopt = tipc_setsockopt,
  2264. .getsockopt = tipc_getsockopt,
  2265. .sendmsg = tipc_sendstream,
  2266. .recvmsg = tipc_recvstream,
  2267. .mmap = sock_no_mmap,
  2268. .sendpage = sock_no_sendpage
  2269. };
  2270. static const struct net_proto_family tipc_family_ops = {
  2271. .owner = THIS_MODULE,
  2272. .family = AF_TIPC,
  2273. .create = tipc_sk_create
  2274. };
  2275. static struct proto tipc_proto = {
  2276. .name = "TIPC",
  2277. .owner = THIS_MODULE,
  2278. .obj_size = sizeof(struct tipc_sock),
  2279. .sysctl_rmem = sysctl_tipc_rmem
  2280. };
  2281. /**
  2282. * tipc_socket_init - initialize TIPC socket interface
  2283. *
  2284. * Returns 0 on success, errno otherwise
  2285. */
  2286. int tipc_socket_init(void)
  2287. {
  2288. int res;
  2289. res = proto_register(&tipc_proto, 1);
  2290. if (res) {
  2291. pr_err("Failed to register TIPC protocol type\n");
  2292. goto out;
  2293. }
  2294. res = sock_register(&tipc_family_ops);
  2295. if (res) {
  2296. pr_err("Failed to register TIPC socket type\n");
  2297. proto_unregister(&tipc_proto);
  2298. goto out;
  2299. }
  2300. out:
  2301. return res;
  2302. }
  2303. /**
  2304. * tipc_socket_stop - stop TIPC socket interface
  2305. */
  2306. void tipc_socket_stop(void)
  2307. {
  2308. sock_unregister(tipc_family_ops.family);
  2309. proto_unregister(&tipc_proto);
  2310. }
  2311. /* Caller should hold socket lock for the passed tipc socket. */
  2312. static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
  2313. {
  2314. u32 peer_node;
  2315. u32 peer_port;
  2316. struct nlattr *nest;
  2317. peer_node = tsk_peer_node(tsk);
  2318. peer_port = tsk_peer_port(tsk);
  2319. nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
  2320. if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
  2321. goto msg_full;
  2322. if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
  2323. goto msg_full;
  2324. if (tsk->conn_type != 0) {
  2325. if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
  2326. goto msg_full;
  2327. if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
  2328. goto msg_full;
  2329. if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
  2330. goto msg_full;
  2331. }
  2332. nla_nest_end(skb, nest);
  2333. return 0;
  2334. msg_full:
  2335. nla_nest_cancel(skb, nest);
  2336. return -EMSGSIZE;
  2337. }
  2338. /* Caller should hold socket lock for the passed tipc socket. */
  2339. static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
  2340. struct tipc_sock *tsk)
  2341. {
  2342. int err;
  2343. void *hdr;
  2344. struct nlattr *attrs;
  2345. struct net *net = sock_net(skb->sk);
  2346. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2347. struct sock *sk = &tsk->sk;
  2348. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  2349. &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
  2350. if (!hdr)
  2351. goto msg_cancel;
  2352. attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
  2353. if (!attrs)
  2354. goto genlmsg_cancel;
  2355. if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
  2356. goto attr_msg_cancel;
  2357. if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
  2358. goto attr_msg_cancel;
  2359. if (tipc_sk_connected(sk)) {
  2360. err = __tipc_nl_add_sk_con(skb, tsk);
  2361. if (err)
  2362. goto attr_msg_cancel;
  2363. } else if (!list_empty(&tsk->publications)) {
  2364. if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
  2365. goto attr_msg_cancel;
  2366. }
  2367. nla_nest_end(skb, attrs);
  2368. genlmsg_end(skb, hdr);
  2369. return 0;
  2370. attr_msg_cancel:
  2371. nla_nest_cancel(skb, attrs);
  2372. genlmsg_cancel:
  2373. genlmsg_cancel(skb, hdr);
  2374. msg_cancel:
  2375. return -EMSGSIZE;
  2376. }
  2377. int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2378. {
  2379. int err;
  2380. struct tipc_sock *tsk;
  2381. const struct bucket_table *tbl;
  2382. struct rhash_head *pos;
  2383. struct net *net = sock_net(skb->sk);
  2384. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2385. u32 tbl_id = cb->args[0];
  2386. u32 prev_portid = cb->args[1];
  2387. rcu_read_lock();
  2388. tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
  2389. for (; tbl_id < tbl->size; tbl_id++) {
  2390. rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
  2391. spin_lock_bh(&tsk->sk.sk_lock.slock);
  2392. if (prev_portid && prev_portid != tsk->portid) {
  2393. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2394. continue;
  2395. }
  2396. err = __tipc_nl_add_sk(skb, cb, tsk);
  2397. if (err) {
  2398. prev_portid = tsk->portid;
  2399. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2400. goto out;
  2401. }
  2402. prev_portid = 0;
  2403. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2404. }
  2405. }
  2406. out:
  2407. rcu_read_unlock();
  2408. cb->args[0] = tbl_id;
  2409. cb->args[1] = prev_portid;
  2410. return skb->len;
  2411. }
  2412. /* Caller should hold socket lock for the passed tipc socket. */
  2413. static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
  2414. struct netlink_callback *cb,
  2415. struct publication *publ)
  2416. {
  2417. void *hdr;
  2418. struct nlattr *attrs;
  2419. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  2420. &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
  2421. if (!hdr)
  2422. goto msg_cancel;
  2423. attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
  2424. if (!attrs)
  2425. goto genlmsg_cancel;
  2426. if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
  2427. goto attr_msg_cancel;
  2428. if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
  2429. goto attr_msg_cancel;
  2430. if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
  2431. goto attr_msg_cancel;
  2432. if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
  2433. goto attr_msg_cancel;
  2434. nla_nest_end(skb, attrs);
  2435. genlmsg_end(skb, hdr);
  2436. return 0;
  2437. attr_msg_cancel:
  2438. nla_nest_cancel(skb, attrs);
  2439. genlmsg_cancel:
  2440. genlmsg_cancel(skb, hdr);
  2441. msg_cancel:
  2442. return -EMSGSIZE;
  2443. }
  2444. /* Caller should hold socket lock for the passed tipc socket. */
  2445. static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
  2446. struct netlink_callback *cb,
  2447. struct tipc_sock *tsk, u32 *last_publ)
  2448. {
  2449. int err;
  2450. struct publication *p;
  2451. if (*last_publ) {
  2452. list_for_each_entry(p, &tsk->publications, pport_list) {
  2453. if (p->key == *last_publ)
  2454. break;
  2455. }
  2456. if (p->key != *last_publ) {
  2457. /* We never set seq or call nl_dump_check_consistent()
  2458. * this means that setting prev_seq here will cause the
  2459. * consistence check to fail in the netlink callback
  2460. * handler. Resulting in the last NLMSG_DONE message
  2461. * having the NLM_F_DUMP_INTR flag set.
  2462. */
  2463. cb->prev_seq = 1;
  2464. *last_publ = 0;
  2465. return -EPIPE;
  2466. }
  2467. } else {
  2468. p = list_first_entry(&tsk->publications, struct publication,
  2469. pport_list);
  2470. }
  2471. list_for_each_entry_from(p, &tsk->publications, pport_list) {
  2472. err = __tipc_nl_add_sk_publ(skb, cb, p);
  2473. if (err) {
  2474. *last_publ = p->key;
  2475. return err;
  2476. }
  2477. }
  2478. *last_publ = 0;
  2479. return 0;
  2480. }
  2481. int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2482. {
  2483. int err;
  2484. u32 tsk_portid = cb->args[0];
  2485. u32 last_publ = cb->args[1];
  2486. u32 done = cb->args[2];
  2487. struct net *net = sock_net(skb->sk);
  2488. struct tipc_sock *tsk;
  2489. if (!tsk_portid) {
  2490. struct nlattr **attrs;
  2491. struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
  2492. err = tipc_nlmsg_parse(cb->nlh, &attrs);
  2493. if (err)
  2494. return err;
  2495. if (!attrs[TIPC_NLA_SOCK])
  2496. return -EINVAL;
  2497. err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
  2498. attrs[TIPC_NLA_SOCK],
  2499. tipc_nl_sock_policy, NULL);
  2500. if (err)
  2501. return err;
  2502. if (!sock[TIPC_NLA_SOCK_REF])
  2503. return -EINVAL;
  2504. tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
  2505. }
  2506. if (done)
  2507. return 0;
  2508. tsk = tipc_sk_lookup(net, tsk_portid);
  2509. if (!tsk)
  2510. return -EINVAL;
  2511. lock_sock(&tsk->sk);
  2512. err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
  2513. if (!err)
  2514. done = 1;
  2515. release_sock(&tsk->sk);
  2516. sock_put(&tsk->sk);
  2517. cb->args[0] = tsk_portid;
  2518. cb->args[1] = last_publ;
  2519. cb->args[2] = done;
  2520. return skb->len;
  2521. }