socket.c 63 KB

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