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