socket.c 74 KB

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