socket.c 72 KB

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