socket.c 72 KB

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