socket.c 74 KB

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