socket.c 72 KB

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