socket.c 72 KB

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