socket.c 71 KB

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