socket.c 74 KB

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