socket.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039
  1. /*
  2. * net/tipc/socket.c: TIPC socket API
  3. *
  4. * Copyright (c) 2001-2007, 2012 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 "port.h"
  38. #include <linux/export.h>
  39. #include <net/sock.h>
  40. #define SS_LISTENING -1 /* socket is listening */
  41. #define SS_READY -2 /* socket is connectionless */
  42. #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
  43. struct tipc_sock {
  44. struct sock sk;
  45. struct tipc_port *p;
  46. struct tipc_portid peer_name;
  47. unsigned int conn_timeout;
  48. };
  49. #define tipc_sk(sk) ((struct tipc_sock *)(sk))
  50. #define tipc_sk_port(sk) (tipc_sk(sk)->p)
  51. static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
  52. static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
  53. static void wakeupdispatch(struct tipc_port *tport);
  54. static void tipc_data_ready(struct sock *sk, int len);
  55. static void tipc_write_space(struct sock *sk);
  56. static int release(struct socket *sock);
  57. static int accept(struct socket *sock, struct socket *new_sock, int flags);
  58. static const struct proto_ops packet_ops;
  59. static const struct proto_ops stream_ops;
  60. static const struct proto_ops msg_ops;
  61. static struct proto tipc_proto;
  62. static struct proto tipc_proto_kern;
  63. /*
  64. * Revised TIPC socket locking policy:
  65. *
  66. * Most socket operations take the standard socket lock when they start
  67. * and hold it until they finish (or until they need to sleep). Acquiring
  68. * this lock grants the owner exclusive access to the fields of the socket
  69. * data structures, with the exception of the backlog queue. A few socket
  70. * operations can be done without taking the socket lock because they only
  71. * read socket information that never changes during the life of the socket.
  72. *
  73. * Socket operations may acquire the lock for the associated TIPC port if they
  74. * need to perform an operation on the port. If any routine needs to acquire
  75. * both the socket lock and the port lock it must take the socket lock first
  76. * to avoid the risk of deadlock.
  77. *
  78. * The dispatcher handling incoming messages cannot grab the socket lock in
  79. * the standard fashion, since invoked it runs at the BH level and cannot block.
  80. * Instead, it checks to see if the socket lock is currently owned by someone,
  81. * and either handles the message itself or adds it to the socket's backlog
  82. * queue; in the latter case the queued message is processed once the process
  83. * owning the socket lock releases it.
  84. *
  85. * NOTE: Releasing the socket lock while an operation is sleeping overcomes
  86. * the problem of a blocked socket operation preventing any other operations
  87. * from occurring. However, applications must be careful if they have
  88. * multiple threads trying to send (or receive) on the same socket, as these
  89. * operations might interfere with each other. For example, doing a connect
  90. * and a receive at the same time might allow the receive to consume the
  91. * ACK message meant for the connect. While additional work could be done
  92. * to try and overcome this, it doesn't seem to be worthwhile at the present.
  93. *
  94. * NOTE: Releasing the socket lock while an operation is sleeping also ensures
  95. * that another operation that must be performed in a non-blocking manner is
  96. * not delayed for very long because the lock has already been taken.
  97. *
  98. * NOTE: This code assumes that certain fields of a port/socket pair are
  99. * constant over its lifetime; such fields can be examined without taking
  100. * the socket lock and/or port lock, and do not need to be re-read even
  101. * after resuming processing after waiting. These fields include:
  102. * - socket type
  103. * - pointer to socket sk structure (aka tipc_sock structure)
  104. * - pointer to port structure
  105. * - port reference
  106. */
  107. /**
  108. * advance_rx_queue - discard first buffer in socket receive queue
  109. *
  110. * Caller must hold socket lock
  111. */
  112. static void advance_rx_queue(struct sock *sk)
  113. {
  114. kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
  115. }
  116. /**
  117. * reject_rx_queue - reject all buffers in socket receive queue
  118. *
  119. * Caller must hold socket lock
  120. */
  121. static void reject_rx_queue(struct sock *sk)
  122. {
  123. struct sk_buff *buf;
  124. while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
  125. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  126. }
  127. /**
  128. * tipc_sk_create - create a TIPC socket
  129. * @net: network namespace (must be default network)
  130. * @sock: pre-allocated socket structure
  131. * @protocol: protocol indicator (must be 0)
  132. * @kern: caused by kernel or by userspace?
  133. *
  134. * This routine creates additional data structures used by the TIPC socket,
  135. * initializes them, and links them together.
  136. *
  137. * Returns 0 on success, errno otherwise
  138. */
  139. static int tipc_sk_create(struct net *net, struct socket *sock, int protocol,
  140. int kern)
  141. {
  142. const struct proto_ops *ops;
  143. socket_state state;
  144. struct sock *sk;
  145. struct tipc_port *tp_ptr;
  146. /* Validate arguments */
  147. if (unlikely(protocol != 0))
  148. return -EPROTONOSUPPORT;
  149. switch (sock->type) {
  150. case SOCK_STREAM:
  151. ops = &stream_ops;
  152. state = SS_UNCONNECTED;
  153. break;
  154. case SOCK_SEQPACKET:
  155. ops = &packet_ops;
  156. state = SS_UNCONNECTED;
  157. break;
  158. case SOCK_DGRAM:
  159. case SOCK_RDM:
  160. ops = &msg_ops;
  161. state = SS_READY;
  162. break;
  163. default:
  164. return -EPROTOTYPE;
  165. }
  166. /* Allocate socket's protocol area */
  167. if (!kern)
  168. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
  169. else
  170. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
  171. if (sk == NULL)
  172. return -ENOMEM;
  173. /* Allocate TIPC port for socket to use */
  174. tp_ptr = tipc_createport(sk, &dispatch, &wakeupdispatch,
  175. TIPC_LOW_IMPORTANCE);
  176. if (unlikely(!tp_ptr)) {
  177. sk_free(sk);
  178. return -ENOMEM;
  179. }
  180. /* Finish initializing socket data structures */
  181. sock->ops = ops;
  182. sock->state = state;
  183. sock_init_data(sock, sk);
  184. sk->sk_backlog_rcv = backlog_rcv;
  185. sk->sk_rcvbuf = sysctl_tipc_rmem[1];
  186. sk->sk_data_ready = tipc_data_ready;
  187. sk->sk_write_space = tipc_write_space;
  188. tipc_sk(sk)->p = tp_ptr;
  189. tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
  190. spin_unlock_bh(tp_ptr->lock);
  191. if (sock->state == SS_READY) {
  192. tipc_set_portunreturnable(tp_ptr->ref, 1);
  193. if (sock->type == SOCK_DGRAM)
  194. tipc_set_portunreliable(tp_ptr->ref, 1);
  195. }
  196. return 0;
  197. }
  198. /**
  199. * tipc_sock_create_local - create TIPC socket from inside TIPC module
  200. * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
  201. *
  202. * We cannot use sock_creat_kern here because it bumps module user count.
  203. * Since socket owner and creator is the same module we must make sure
  204. * that module count remains zero for module local sockets, otherwise
  205. * we cannot do rmmod.
  206. *
  207. * Returns 0 on success, errno otherwise
  208. */
  209. int tipc_sock_create_local(int type, struct socket **res)
  210. {
  211. int rc;
  212. rc = sock_create_lite(AF_TIPC, type, 0, res);
  213. if (rc < 0) {
  214. pr_err("Failed to create kernel socket\n");
  215. return rc;
  216. }
  217. tipc_sk_create(&init_net, *res, 0, 1);
  218. return 0;
  219. }
  220. /**
  221. * tipc_sock_release_local - release socket created by tipc_sock_create_local
  222. * @sock: the socket to be released.
  223. *
  224. * Module reference count is not incremented when such sockets are created,
  225. * so we must keep it from being decremented when they are released.
  226. */
  227. void tipc_sock_release_local(struct socket *sock)
  228. {
  229. release(sock);
  230. sock->ops = NULL;
  231. sock_release(sock);
  232. }
  233. /**
  234. * tipc_sock_accept_local - accept a connection on a socket created
  235. * with tipc_sock_create_local. Use this function to avoid that
  236. * module reference count is inadvertently incremented.
  237. *
  238. * @sock: the accepting socket
  239. * @newsock: reference to the new socket to be created
  240. * @flags: socket flags
  241. */
  242. int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
  243. int flags)
  244. {
  245. struct sock *sk = sock->sk;
  246. int ret;
  247. ret = sock_create_lite(sk->sk_family, sk->sk_type,
  248. sk->sk_protocol, newsock);
  249. if (ret < 0)
  250. return ret;
  251. ret = accept(sock, *newsock, flags);
  252. if (ret < 0) {
  253. sock_release(*newsock);
  254. return ret;
  255. }
  256. (*newsock)->ops = sock->ops;
  257. return ret;
  258. }
  259. /**
  260. * release - destroy a TIPC socket
  261. * @sock: socket to destroy
  262. *
  263. * This routine cleans up any messages that are still queued on the socket.
  264. * For DGRAM and RDM socket types, all queued messages are rejected.
  265. * For SEQPACKET and STREAM socket types, the first message is rejected
  266. * and any others are discarded. (If the first message on a STREAM socket
  267. * is partially-read, it is discarded and the next one is rejected instead.)
  268. *
  269. * NOTE: Rejected messages are not necessarily returned to the sender! They
  270. * are returned or discarded according to the "destination droppable" setting
  271. * specified for the message by the sender.
  272. *
  273. * Returns 0 on success, errno otherwise
  274. */
  275. static int release(struct socket *sock)
  276. {
  277. struct sock *sk = sock->sk;
  278. struct tipc_port *tport;
  279. struct sk_buff *buf;
  280. int res;
  281. /*
  282. * Exit if socket isn't fully initialized (occurs when a failed accept()
  283. * releases a pre-allocated child socket that was never used)
  284. */
  285. if (sk == NULL)
  286. return 0;
  287. tport = tipc_sk_port(sk);
  288. lock_sock(sk);
  289. /*
  290. * Reject all unreceived messages, except on an active connection
  291. * (which disconnects locally & sends a 'FIN+' to peer)
  292. */
  293. while (sock->state != SS_DISCONNECTING) {
  294. buf = __skb_dequeue(&sk->sk_receive_queue);
  295. if (buf == NULL)
  296. break;
  297. if (TIPC_SKB_CB(buf)->handle != NULL)
  298. kfree_skb(buf);
  299. else {
  300. if ((sock->state == SS_CONNECTING) ||
  301. (sock->state == SS_CONNECTED)) {
  302. sock->state = SS_DISCONNECTING;
  303. tipc_disconnect(tport->ref);
  304. }
  305. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  306. }
  307. }
  308. /*
  309. * Delete TIPC port; this ensures no more messages are queued
  310. * (also disconnects an active connection & sends a 'FIN-' to peer)
  311. */
  312. res = tipc_deleteport(tport);
  313. /* Discard any remaining (connection-based) messages in receive queue */
  314. __skb_queue_purge(&sk->sk_receive_queue);
  315. /* Reject any messages that accumulated in backlog queue */
  316. sock->state = SS_DISCONNECTING;
  317. release_sock(sk);
  318. sock_put(sk);
  319. sock->sk = NULL;
  320. return res;
  321. }
  322. /**
  323. * bind - associate or disassocate TIPC name(s) with a socket
  324. * @sock: socket structure
  325. * @uaddr: socket address describing name(s) and desired operation
  326. * @uaddr_len: size of socket address data structure
  327. *
  328. * Name and name sequence binding is indicated using a positive scope value;
  329. * a negative scope value unbinds the specified name. Specifying no name
  330. * (i.e. a socket address length of 0) unbinds all names from the socket.
  331. *
  332. * Returns 0 on success, errno otherwise
  333. *
  334. * NOTE: This routine doesn't need to take the socket lock since it doesn't
  335. * access any non-constant socket information.
  336. */
  337. static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
  338. {
  339. struct sock *sk = sock->sk;
  340. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  341. struct tipc_port *tport = tipc_sk_port(sock->sk);
  342. int res = -EINVAL;
  343. lock_sock(sk);
  344. if (unlikely(!uaddr_len)) {
  345. res = tipc_withdraw(tport, 0, NULL);
  346. goto exit;
  347. }
  348. if (uaddr_len < sizeof(struct sockaddr_tipc)) {
  349. res = -EINVAL;
  350. goto exit;
  351. }
  352. if (addr->family != AF_TIPC) {
  353. res = -EAFNOSUPPORT;
  354. goto exit;
  355. }
  356. if (addr->addrtype == TIPC_ADDR_NAME)
  357. addr->addr.nameseq.upper = addr->addr.nameseq.lower;
  358. else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
  359. res = -EAFNOSUPPORT;
  360. goto exit;
  361. }
  362. if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
  363. (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
  364. (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
  365. res = -EACCES;
  366. goto exit;
  367. }
  368. res = (addr->scope > 0) ?
  369. tipc_publish(tport, addr->scope, &addr->addr.nameseq) :
  370. tipc_withdraw(tport, -addr->scope, &addr->addr.nameseq);
  371. exit:
  372. release_sock(sk);
  373. return res;
  374. }
  375. /**
  376. * get_name - get port ID of socket or peer socket
  377. * @sock: socket structure
  378. * @uaddr: area for returned socket address
  379. * @uaddr_len: area for returned length of socket address
  380. * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
  381. *
  382. * Returns 0 on success, errno otherwise
  383. *
  384. * NOTE: This routine doesn't need to take the socket lock since it only
  385. * accesses socket information that is unchanging (or which changes in
  386. * a completely predictable manner).
  387. */
  388. static int get_name(struct socket *sock, struct sockaddr *uaddr,
  389. int *uaddr_len, int peer)
  390. {
  391. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  392. struct tipc_sock *tsock = tipc_sk(sock->sk);
  393. memset(addr, 0, sizeof(*addr));
  394. if (peer) {
  395. if ((sock->state != SS_CONNECTED) &&
  396. ((peer != 2) || (sock->state != SS_DISCONNECTING)))
  397. return -ENOTCONN;
  398. addr->addr.id.ref = tsock->peer_name.ref;
  399. addr->addr.id.node = tsock->peer_name.node;
  400. } else {
  401. addr->addr.id.ref = tsock->p->ref;
  402. addr->addr.id.node = tipc_own_addr;
  403. }
  404. *uaddr_len = sizeof(*addr);
  405. addr->addrtype = TIPC_ADDR_ID;
  406. addr->family = AF_TIPC;
  407. addr->scope = 0;
  408. addr->addr.name.domain = 0;
  409. return 0;
  410. }
  411. /**
  412. * poll - read and possibly block on pollmask
  413. * @file: file structure associated with the socket
  414. * @sock: socket for which to calculate the poll bits
  415. * @wait: ???
  416. *
  417. * Returns pollmask value
  418. *
  419. * COMMENTARY:
  420. * It appears that the usual socket locking mechanisms are not useful here
  421. * since the pollmask info is potentially out-of-date the moment this routine
  422. * exits. TCP and other protocols seem to rely on higher level poll routines
  423. * to handle any preventable race conditions, so TIPC will do the same ...
  424. *
  425. * TIPC sets the returned events as follows:
  426. *
  427. * socket state flags set
  428. * ------------ ---------
  429. * unconnected no read flags
  430. * POLLOUT if port is not congested
  431. *
  432. * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
  433. * no write flags
  434. *
  435. * connected POLLIN/POLLRDNORM if data in rx queue
  436. * POLLOUT if port is not congested
  437. *
  438. * disconnecting POLLIN/POLLRDNORM/POLLHUP
  439. * no write flags
  440. *
  441. * listening POLLIN if SYN in rx queue
  442. * no write flags
  443. *
  444. * ready POLLIN/POLLRDNORM if data in rx queue
  445. * [connectionless] POLLOUT (since port cannot be congested)
  446. *
  447. * IMPORTANT: The fact that a read or write operation is indicated does NOT
  448. * imply that the operation will succeed, merely that it should be performed
  449. * and will not block.
  450. */
  451. static unsigned int poll(struct file *file, struct socket *sock,
  452. poll_table *wait)
  453. {
  454. struct sock *sk = sock->sk;
  455. u32 mask = 0;
  456. sock_poll_wait(file, sk_sleep(sk), wait);
  457. switch ((int)sock->state) {
  458. case SS_UNCONNECTED:
  459. if (!tipc_sk_port(sk)->congested)
  460. mask |= POLLOUT;
  461. break;
  462. case SS_READY:
  463. case SS_CONNECTED:
  464. if (!tipc_sk_port(sk)->congested)
  465. mask |= POLLOUT;
  466. /* fall thru' */
  467. case SS_CONNECTING:
  468. case SS_LISTENING:
  469. if (!skb_queue_empty(&sk->sk_receive_queue))
  470. mask |= (POLLIN | POLLRDNORM);
  471. break;
  472. case SS_DISCONNECTING:
  473. mask = (POLLIN | POLLRDNORM | POLLHUP);
  474. break;
  475. }
  476. return mask;
  477. }
  478. /**
  479. * dest_name_check - verify user is permitted to send to specified port name
  480. * @dest: destination address
  481. * @m: descriptor for message to be sent
  482. *
  483. * Prevents restricted configuration commands from being issued by
  484. * unauthorized users.
  485. *
  486. * Returns 0 if permission is granted, otherwise errno
  487. */
  488. static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
  489. {
  490. struct tipc_cfg_msg_hdr hdr;
  491. if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
  492. return 0;
  493. if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
  494. return 0;
  495. if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
  496. return -EACCES;
  497. if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
  498. return -EMSGSIZE;
  499. if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
  500. return -EFAULT;
  501. if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
  502. return -EACCES;
  503. return 0;
  504. }
  505. static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
  506. {
  507. struct sock *sk = sock->sk;
  508. struct tipc_port *tport = tipc_sk_port(sk);
  509. DEFINE_WAIT(wait);
  510. int done;
  511. do {
  512. int err = sock_error(sk);
  513. if (err)
  514. return err;
  515. if (sock->state == SS_DISCONNECTING)
  516. return -EPIPE;
  517. if (!*timeo_p)
  518. return -EAGAIN;
  519. if (signal_pending(current))
  520. return sock_intr_errno(*timeo_p);
  521. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  522. done = sk_wait_event(sk, timeo_p, !tport->congested);
  523. finish_wait(sk_sleep(sk), &wait);
  524. } while (!done);
  525. return 0;
  526. }
  527. /**
  528. * send_msg - send message in connectionless manner
  529. * @iocb: if NULL, indicates that socket lock is already held
  530. * @sock: socket structure
  531. * @m: message to send
  532. * @total_len: length of message
  533. *
  534. * Message must have an destination specified explicitly.
  535. * Used for SOCK_RDM and SOCK_DGRAM messages,
  536. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  537. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  538. *
  539. * Returns the number of bytes sent on success, or errno otherwise
  540. */
  541. static int send_msg(struct kiocb *iocb, struct socket *sock,
  542. struct msghdr *m, size_t total_len)
  543. {
  544. struct sock *sk = sock->sk;
  545. struct tipc_port *tport = tipc_sk_port(sk);
  546. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  547. int needs_conn;
  548. long timeo;
  549. int res = -EINVAL;
  550. if (unlikely(!dest))
  551. return -EDESTADDRREQ;
  552. if (unlikely((m->msg_namelen < sizeof(*dest)) ||
  553. (dest->family != AF_TIPC)))
  554. return -EINVAL;
  555. if (total_len > TIPC_MAX_USER_MSG_SIZE)
  556. return -EMSGSIZE;
  557. if (iocb)
  558. lock_sock(sk);
  559. needs_conn = (sock->state != SS_READY);
  560. if (unlikely(needs_conn)) {
  561. if (sock->state == SS_LISTENING) {
  562. res = -EPIPE;
  563. goto exit;
  564. }
  565. if (sock->state != SS_UNCONNECTED) {
  566. res = -EISCONN;
  567. goto exit;
  568. }
  569. if (tport->published) {
  570. res = -EOPNOTSUPP;
  571. goto exit;
  572. }
  573. if (dest->addrtype == TIPC_ADDR_NAME) {
  574. tport->conn_type = dest->addr.name.name.type;
  575. tport->conn_instance = dest->addr.name.name.instance;
  576. }
  577. /* Abort any pending connection attempts (very unlikely) */
  578. reject_rx_queue(sk);
  579. }
  580. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  581. do {
  582. if (dest->addrtype == TIPC_ADDR_NAME) {
  583. res = dest_name_check(dest, m);
  584. if (res)
  585. break;
  586. res = tipc_send2name(tport->ref,
  587. &dest->addr.name.name,
  588. dest->addr.name.domain,
  589. m->msg_iov,
  590. total_len);
  591. } else if (dest->addrtype == TIPC_ADDR_ID) {
  592. res = tipc_send2port(tport->ref,
  593. &dest->addr.id,
  594. m->msg_iov,
  595. total_len);
  596. } else if (dest->addrtype == TIPC_ADDR_MCAST) {
  597. if (needs_conn) {
  598. res = -EOPNOTSUPP;
  599. break;
  600. }
  601. res = dest_name_check(dest, m);
  602. if (res)
  603. break;
  604. res = tipc_multicast(tport->ref,
  605. &dest->addr.nameseq,
  606. m->msg_iov,
  607. total_len);
  608. }
  609. if (likely(res != -ELINKCONG)) {
  610. if (needs_conn && (res >= 0))
  611. sock->state = SS_CONNECTING;
  612. break;
  613. }
  614. res = tipc_wait_for_sndmsg(sock, &timeo);
  615. if (res)
  616. break;
  617. } while (1);
  618. exit:
  619. if (iocb)
  620. release_sock(sk);
  621. return res;
  622. }
  623. static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
  624. {
  625. struct sock *sk = sock->sk;
  626. struct tipc_port *tport = tipc_sk_port(sk);
  627. DEFINE_WAIT(wait);
  628. int done;
  629. do {
  630. int err = sock_error(sk);
  631. if (err)
  632. return err;
  633. if (sock->state == SS_DISCONNECTING)
  634. return -EPIPE;
  635. else if (sock->state != SS_CONNECTED)
  636. return -ENOTCONN;
  637. if (!*timeo_p)
  638. return -EAGAIN;
  639. if (signal_pending(current))
  640. return sock_intr_errno(*timeo_p);
  641. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  642. done = sk_wait_event(sk, timeo_p,
  643. (!tport->congested || !tport->connected));
  644. finish_wait(sk_sleep(sk), &wait);
  645. } while (!done);
  646. return 0;
  647. }
  648. /**
  649. * send_packet - send a connection-oriented message
  650. * @iocb: if NULL, indicates that socket lock is already held
  651. * @sock: socket structure
  652. * @m: message to send
  653. * @total_len: length of message
  654. *
  655. * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
  656. *
  657. * Returns the number of bytes sent on success, or errno otherwise
  658. */
  659. static int send_packet(struct kiocb *iocb, struct socket *sock,
  660. struct msghdr *m, size_t total_len)
  661. {
  662. struct sock *sk = sock->sk;
  663. struct tipc_port *tport = tipc_sk_port(sk);
  664. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  665. int res = -EINVAL;
  666. long timeo;
  667. /* Handle implied connection establishment */
  668. if (unlikely(dest))
  669. return send_msg(iocb, sock, m, total_len);
  670. if (total_len > TIPC_MAX_USER_MSG_SIZE)
  671. return -EMSGSIZE;
  672. if (iocb)
  673. lock_sock(sk);
  674. if (unlikely(sock->state != SS_CONNECTED)) {
  675. if (sock->state == SS_DISCONNECTING)
  676. res = -EPIPE;
  677. else
  678. res = -ENOTCONN;
  679. goto exit;
  680. }
  681. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  682. do {
  683. res = tipc_send(tport->ref, m->msg_iov, total_len);
  684. if (likely(res != -ELINKCONG))
  685. break;
  686. res = tipc_wait_for_sndpkt(sock, &timeo);
  687. if (res)
  688. break;
  689. } while (1);
  690. exit:
  691. if (iocb)
  692. release_sock(sk);
  693. return res;
  694. }
  695. /**
  696. * send_stream - send stream-oriented data
  697. * @iocb: (unused)
  698. * @sock: socket structure
  699. * @m: data to send
  700. * @total_len: total length of data to be sent
  701. *
  702. * Used for SOCK_STREAM data.
  703. *
  704. * Returns the number of bytes sent on success (or partial success),
  705. * or errno if no data sent
  706. */
  707. static int send_stream(struct kiocb *iocb, struct socket *sock,
  708. struct msghdr *m, size_t total_len)
  709. {
  710. struct sock *sk = sock->sk;
  711. struct tipc_port *tport = tipc_sk_port(sk);
  712. struct msghdr my_msg;
  713. struct iovec my_iov;
  714. struct iovec *curr_iov;
  715. int curr_iovlen;
  716. char __user *curr_start;
  717. u32 hdr_size;
  718. int curr_left;
  719. int bytes_to_send;
  720. int bytes_sent;
  721. int res;
  722. lock_sock(sk);
  723. /* Handle special cases where there is no connection */
  724. if (unlikely(sock->state != SS_CONNECTED)) {
  725. if (sock->state == SS_UNCONNECTED)
  726. res = send_packet(NULL, sock, m, total_len);
  727. else
  728. res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
  729. goto exit;
  730. }
  731. if (unlikely(m->msg_name)) {
  732. res = -EISCONN;
  733. goto exit;
  734. }
  735. if (total_len > (unsigned int)INT_MAX) {
  736. res = -EMSGSIZE;
  737. goto exit;
  738. }
  739. /*
  740. * Send each iovec entry using one or more messages
  741. *
  742. * Note: This algorithm is good for the most likely case
  743. * (i.e. one large iovec entry), but could be improved to pass sets
  744. * of small iovec entries into send_packet().
  745. */
  746. curr_iov = m->msg_iov;
  747. curr_iovlen = m->msg_iovlen;
  748. my_msg.msg_iov = &my_iov;
  749. my_msg.msg_iovlen = 1;
  750. my_msg.msg_flags = m->msg_flags;
  751. my_msg.msg_name = NULL;
  752. bytes_sent = 0;
  753. hdr_size = msg_hdr_sz(&tport->phdr);
  754. while (curr_iovlen--) {
  755. curr_start = curr_iov->iov_base;
  756. curr_left = curr_iov->iov_len;
  757. while (curr_left) {
  758. bytes_to_send = tport->max_pkt - hdr_size;
  759. if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
  760. bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
  761. if (curr_left < bytes_to_send)
  762. bytes_to_send = curr_left;
  763. my_iov.iov_base = curr_start;
  764. my_iov.iov_len = bytes_to_send;
  765. res = send_packet(NULL, sock, &my_msg, bytes_to_send);
  766. if (res < 0) {
  767. if (bytes_sent)
  768. res = bytes_sent;
  769. goto exit;
  770. }
  771. curr_left -= bytes_to_send;
  772. curr_start += bytes_to_send;
  773. bytes_sent += bytes_to_send;
  774. }
  775. curr_iov++;
  776. }
  777. res = bytes_sent;
  778. exit:
  779. release_sock(sk);
  780. return res;
  781. }
  782. /**
  783. * auto_connect - complete connection setup to a remote port
  784. * @sock: socket structure
  785. * @msg: peer's response message
  786. *
  787. * Returns 0 on success, errno otherwise
  788. */
  789. static int auto_connect(struct socket *sock, struct tipc_msg *msg)
  790. {
  791. struct tipc_sock *tsock = tipc_sk(sock->sk);
  792. struct tipc_port *p_ptr;
  793. tsock->peer_name.ref = msg_origport(msg);
  794. tsock->peer_name.node = msg_orignode(msg);
  795. p_ptr = tipc_port_deref(tsock->p->ref);
  796. if (!p_ptr)
  797. return -EINVAL;
  798. __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
  799. if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
  800. return -EINVAL;
  801. msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
  802. sock->state = SS_CONNECTED;
  803. return 0;
  804. }
  805. /**
  806. * set_orig_addr - capture sender's address for received message
  807. * @m: descriptor for message info
  808. * @msg: received message header
  809. *
  810. * Note: Address is not captured if not requested by receiver.
  811. */
  812. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  813. {
  814. DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
  815. if (addr) {
  816. addr->family = AF_TIPC;
  817. addr->addrtype = TIPC_ADDR_ID;
  818. memset(&addr->addr, 0, sizeof(addr->addr));
  819. addr->addr.id.ref = msg_origport(msg);
  820. addr->addr.id.node = msg_orignode(msg);
  821. addr->addr.name.domain = 0; /* could leave uninitialized */
  822. addr->scope = 0; /* could leave uninitialized */
  823. m->msg_namelen = sizeof(struct sockaddr_tipc);
  824. }
  825. }
  826. /**
  827. * anc_data_recv - optionally capture ancillary data for received message
  828. * @m: descriptor for message info
  829. * @msg: received message header
  830. * @tport: TIPC port associated with message
  831. *
  832. * Note: Ancillary data is not captured if not requested by receiver.
  833. *
  834. * Returns 0 if successful, otherwise errno
  835. */
  836. static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  837. struct tipc_port *tport)
  838. {
  839. u32 anc_data[3];
  840. u32 err;
  841. u32 dest_type;
  842. int has_name;
  843. int res;
  844. if (likely(m->msg_controllen == 0))
  845. return 0;
  846. /* Optionally capture errored message object(s) */
  847. err = msg ? msg_errcode(msg) : 0;
  848. if (unlikely(err)) {
  849. anc_data[0] = err;
  850. anc_data[1] = msg_data_sz(msg);
  851. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  852. if (res)
  853. return res;
  854. if (anc_data[1]) {
  855. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  856. msg_data(msg));
  857. if (res)
  858. return res;
  859. }
  860. }
  861. /* Optionally capture message destination object */
  862. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  863. switch (dest_type) {
  864. case TIPC_NAMED_MSG:
  865. has_name = 1;
  866. anc_data[0] = msg_nametype(msg);
  867. anc_data[1] = msg_namelower(msg);
  868. anc_data[2] = msg_namelower(msg);
  869. break;
  870. case TIPC_MCAST_MSG:
  871. has_name = 1;
  872. anc_data[0] = msg_nametype(msg);
  873. anc_data[1] = msg_namelower(msg);
  874. anc_data[2] = msg_nameupper(msg);
  875. break;
  876. case TIPC_CONN_MSG:
  877. has_name = (tport->conn_type != 0);
  878. anc_data[0] = tport->conn_type;
  879. anc_data[1] = tport->conn_instance;
  880. anc_data[2] = tport->conn_instance;
  881. break;
  882. default:
  883. has_name = 0;
  884. }
  885. if (has_name) {
  886. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  887. if (res)
  888. return res;
  889. }
  890. return 0;
  891. }
  892. static int tipc_wait_for_rcvmsg(struct socket *sock, long timeo)
  893. {
  894. struct sock *sk = sock->sk;
  895. DEFINE_WAIT(wait);
  896. int err;
  897. for (;;) {
  898. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  899. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  900. if (sock->state == SS_DISCONNECTING) {
  901. err = -ENOTCONN;
  902. break;
  903. }
  904. release_sock(sk);
  905. timeo = schedule_timeout(timeo);
  906. lock_sock(sk);
  907. }
  908. err = 0;
  909. if (!skb_queue_empty(&sk->sk_receive_queue))
  910. break;
  911. err = sock_intr_errno(timeo);
  912. if (signal_pending(current))
  913. break;
  914. err = -EAGAIN;
  915. if (!timeo)
  916. break;
  917. }
  918. finish_wait(sk_sleep(sk), &wait);
  919. return err;
  920. }
  921. /**
  922. * recv_msg - receive packet-oriented message
  923. * @iocb: (unused)
  924. * @m: descriptor for message info
  925. * @buf_len: total size of user buffer area
  926. * @flags: receive flags
  927. *
  928. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  929. * If the complete message doesn't fit in user area, truncate it.
  930. *
  931. * Returns size of returned message data, errno otherwise
  932. */
  933. static int recv_msg(struct kiocb *iocb, struct socket *sock,
  934. struct msghdr *m, size_t buf_len, int flags)
  935. {
  936. struct sock *sk = sock->sk;
  937. struct tipc_port *tport = tipc_sk_port(sk);
  938. struct sk_buff *buf;
  939. struct tipc_msg *msg;
  940. long timeo;
  941. unsigned int sz;
  942. u32 err;
  943. int res;
  944. /* Catch invalid receive requests */
  945. if (unlikely(!buf_len))
  946. return -EINVAL;
  947. lock_sock(sk);
  948. if (unlikely(sock->state == SS_UNCONNECTED)) {
  949. res = -ENOTCONN;
  950. goto exit;
  951. }
  952. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  953. restart:
  954. /* Look for a message in receive queue; wait if necessary */
  955. res = tipc_wait_for_rcvmsg(sock, timeo);
  956. if (res)
  957. goto exit;
  958. /* Look at first message in receive queue */
  959. buf = skb_peek(&sk->sk_receive_queue);
  960. msg = buf_msg(buf);
  961. sz = msg_data_sz(msg);
  962. err = msg_errcode(msg);
  963. /* Discard an empty non-errored message & try again */
  964. if ((!sz) && (!err)) {
  965. advance_rx_queue(sk);
  966. goto restart;
  967. }
  968. /* Capture sender's address (optional) */
  969. set_orig_addr(m, msg);
  970. /* Capture ancillary data (optional) */
  971. res = anc_data_recv(m, msg, tport);
  972. if (res)
  973. goto exit;
  974. /* Capture message data (if valid) & compute return value (always) */
  975. if (!err) {
  976. if (unlikely(buf_len < sz)) {
  977. sz = buf_len;
  978. m->msg_flags |= MSG_TRUNC;
  979. }
  980. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
  981. m->msg_iov, sz);
  982. if (res)
  983. goto exit;
  984. res = sz;
  985. } else {
  986. if ((sock->state == SS_READY) ||
  987. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  988. res = 0;
  989. else
  990. res = -ECONNRESET;
  991. }
  992. /* Consume received message (optional) */
  993. if (likely(!(flags & MSG_PEEK))) {
  994. if ((sock->state != SS_READY) &&
  995. (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  996. tipc_acknowledge(tport->ref, tport->conn_unacked);
  997. advance_rx_queue(sk);
  998. }
  999. exit:
  1000. release_sock(sk);
  1001. return res;
  1002. }
  1003. /**
  1004. * recv_stream - receive stream-oriented data
  1005. * @iocb: (unused)
  1006. * @m: descriptor for message info
  1007. * @buf_len: total size of user buffer area
  1008. * @flags: receive flags
  1009. *
  1010. * Used for SOCK_STREAM messages only. If not enough data is available
  1011. * will optionally wait for more; never truncates data.
  1012. *
  1013. * Returns size of returned message data, errno otherwise
  1014. */
  1015. static int recv_stream(struct kiocb *iocb, struct socket *sock,
  1016. struct msghdr *m, size_t buf_len, int flags)
  1017. {
  1018. struct sock *sk = sock->sk;
  1019. struct tipc_port *tport = tipc_sk_port(sk);
  1020. struct sk_buff *buf;
  1021. struct tipc_msg *msg;
  1022. long timeo;
  1023. unsigned int sz;
  1024. int sz_to_copy, target, needed;
  1025. int sz_copied = 0;
  1026. u32 err;
  1027. int res = 0;
  1028. /* Catch invalid receive attempts */
  1029. if (unlikely(!buf_len))
  1030. return -EINVAL;
  1031. lock_sock(sk);
  1032. if (unlikely(sock->state == SS_UNCONNECTED)) {
  1033. res = -ENOTCONN;
  1034. goto exit;
  1035. }
  1036. target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
  1037. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1038. restart:
  1039. /* Look for a message in receive queue; wait if necessary */
  1040. res = tipc_wait_for_rcvmsg(sock, timeo);
  1041. if (res)
  1042. goto exit;
  1043. /* Look at first message in receive queue */
  1044. buf = skb_peek(&sk->sk_receive_queue);
  1045. msg = buf_msg(buf);
  1046. sz = msg_data_sz(msg);
  1047. err = msg_errcode(msg);
  1048. /* Discard an empty non-errored message & try again */
  1049. if ((!sz) && (!err)) {
  1050. advance_rx_queue(sk);
  1051. goto restart;
  1052. }
  1053. /* Optionally capture sender's address & ancillary data of first msg */
  1054. if (sz_copied == 0) {
  1055. set_orig_addr(m, msg);
  1056. res = anc_data_recv(m, msg, tport);
  1057. if (res)
  1058. goto exit;
  1059. }
  1060. /* Capture message data (if valid) & compute return value (always) */
  1061. if (!err) {
  1062. u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
  1063. sz -= offset;
  1064. needed = (buf_len - sz_copied);
  1065. sz_to_copy = (sz <= needed) ? sz : needed;
  1066. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
  1067. m->msg_iov, sz_to_copy);
  1068. if (res)
  1069. goto exit;
  1070. sz_copied += sz_to_copy;
  1071. if (sz_to_copy < sz) {
  1072. if (!(flags & MSG_PEEK))
  1073. TIPC_SKB_CB(buf)->handle =
  1074. (void *)(unsigned long)(offset + sz_to_copy);
  1075. goto exit;
  1076. }
  1077. } else {
  1078. if (sz_copied != 0)
  1079. goto exit; /* can't add error msg to valid data */
  1080. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  1081. res = 0;
  1082. else
  1083. res = -ECONNRESET;
  1084. }
  1085. /* Consume received message (optional) */
  1086. if (likely(!(flags & MSG_PEEK))) {
  1087. if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  1088. tipc_acknowledge(tport->ref, tport->conn_unacked);
  1089. advance_rx_queue(sk);
  1090. }
  1091. /* Loop around if more data is required */
  1092. if ((sz_copied < buf_len) && /* didn't get all requested data */
  1093. (!skb_queue_empty(&sk->sk_receive_queue) ||
  1094. (sz_copied < target)) && /* and more is ready or required */
  1095. (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
  1096. (!err)) /* and haven't reached a FIN */
  1097. goto restart;
  1098. exit:
  1099. release_sock(sk);
  1100. return sz_copied ? sz_copied : res;
  1101. }
  1102. /**
  1103. * tipc_write_space - wake up thread if port congestion is released
  1104. * @sk: socket
  1105. */
  1106. static void tipc_write_space(struct sock *sk)
  1107. {
  1108. struct socket_wq *wq;
  1109. rcu_read_lock();
  1110. wq = rcu_dereference(sk->sk_wq);
  1111. if (wq_has_sleeper(wq))
  1112. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1113. POLLWRNORM | POLLWRBAND);
  1114. rcu_read_unlock();
  1115. }
  1116. /**
  1117. * tipc_data_ready - wake up threads to indicate messages have been received
  1118. * @sk: socket
  1119. * @len: the length of messages
  1120. */
  1121. static void tipc_data_ready(struct sock *sk, int len)
  1122. {
  1123. struct socket_wq *wq;
  1124. rcu_read_lock();
  1125. wq = rcu_dereference(sk->sk_wq);
  1126. if (wq_has_sleeper(wq))
  1127. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  1128. POLLRDNORM | POLLRDBAND);
  1129. rcu_read_unlock();
  1130. }
  1131. /**
  1132. * filter_connect - Handle all incoming messages for a connection-based socket
  1133. * @tsock: TIPC socket
  1134. * @msg: message
  1135. *
  1136. * Returns TIPC error status code and socket error status code
  1137. * once it encounters some errors
  1138. */
  1139. static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
  1140. {
  1141. struct socket *sock = tsock->sk.sk_socket;
  1142. struct tipc_msg *msg = buf_msg(*buf);
  1143. struct sock *sk = &tsock->sk;
  1144. u32 retval = TIPC_ERR_NO_PORT;
  1145. int res;
  1146. if (msg_mcast(msg))
  1147. return retval;
  1148. switch ((int)sock->state) {
  1149. case SS_CONNECTED:
  1150. /* Accept only connection-based messages sent by peer */
  1151. if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
  1152. if (unlikely(msg_errcode(msg))) {
  1153. sock->state = SS_DISCONNECTING;
  1154. __tipc_disconnect(tsock->p);
  1155. }
  1156. retval = TIPC_OK;
  1157. }
  1158. break;
  1159. case SS_CONNECTING:
  1160. /* Accept only ACK or NACK message */
  1161. if (unlikely(msg_errcode(msg))) {
  1162. sock->state = SS_DISCONNECTING;
  1163. sk->sk_err = ECONNREFUSED;
  1164. retval = TIPC_OK;
  1165. break;
  1166. }
  1167. if (unlikely(!msg_connected(msg)))
  1168. break;
  1169. res = auto_connect(sock, msg);
  1170. if (res) {
  1171. sock->state = SS_DISCONNECTING;
  1172. sk->sk_err = -res;
  1173. retval = TIPC_OK;
  1174. break;
  1175. }
  1176. /* If an incoming message is an 'ACK-', it should be
  1177. * discarded here because it doesn't contain useful
  1178. * data. In addition, we should try to wake up
  1179. * connect() routine if sleeping.
  1180. */
  1181. if (msg_data_sz(msg) == 0) {
  1182. kfree_skb(*buf);
  1183. *buf = NULL;
  1184. if (waitqueue_active(sk_sleep(sk)))
  1185. wake_up_interruptible(sk_sleep(sk));
  1186. }
  1187. retval = TIPC_OK;
  1188. break;
  1189. case SS_LISTENING:
  1190. case SS_UNCONNECTED:
  1191. /* Accept only SYN message */
  1192. if (!msg_connected(msg) && !(msg_errcode(msg)))
  1193. retval = TIPC_OK;
  1194. break;
  1195. case SS_DISCONNECTING:
  1196. break;
  1197. default:
  1198. pr_err("Unknown socket state %u\n", sock->state);
  1199. }
  1200. return retval;
  1201. }
  1202. /**
  1203. * rcvbuf_limit - get proper overload limit of socket receive queue
  1204. * @sk: socket
  1205. * @buf: message
  1206. *
  1207. * For all connection oriented messages, irrespective of importance,
  1208. * the default overload value (i.e. 67MB) is set as limit.
  1209. *
  1210. * For all connectionless messages, by default new queue limits are
  1211. * as belows:
  1212. *
  1213. * TIPC_LOW_IMPORTANCE (4 MB)
  1214. * TIPC_MEDIUM_IMPORTANCE (8 MB)
  1215. * TIPC_HIGH_IMPORTANCE (16 MB)
  1216. * TIPC_CRITICAL_IMPORTANCE (32 MB)
  1217. *
  1218. * Returns overload limit according to corresponding message importance
  1219. */
  1220. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
  1221. {
  1222. struct tipc_msg *msg = buf_msg(buf);
  1223. if (msg_connected(msg))
  1224. return sysctl_tipc_rmem[2];
  1225. return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
  1226. msg_importance(msg);
  1227. }
  1228. /**
  1229. * filter_rcv - validate incoming message
  1230. * @sk: socket
  1231. * @buf: message
  1232. *
  1233. * Enqueues message on receive queue if acceptable; optionally handles
  1234. * disconnect indication for a connected socket.
  1235. *
  1236. * Called with socket lock already taken; port lock may also be taken.
  1237. *
  1238. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  1239. */
  1240. static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
  1241. {
  1242. struct socket *sock = sk->sk_socket;
  1243. struct tipc_msg *msg = buf_msg(buf);
  1244. unsigned int limit = rcvbuf_limit(sk, buf);
  1245. u32 res = TIPC_OK;
  1246. /* Reject message if it is wrong sort of message for socket */
  1247. if (msg_type(msg) > TIPC_DIRECT_MSG)
  1248. return TIPC_ERR_NO_PORT;
  1249. if (sock->state == SS_READY) {
  1250. if (msg_connected(msg))
  1251. return TIPC_ERR_NO_PORT;
  1252. } else {
  1253. res = filter_connect(tipc_sk(sk), &buf);
  1254. if (res != TIPC_OK || buf == NULL)
  1255. return res;
  1256. }
  1257. /* Reject message if there isn't room to queue it */
  1258. if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
  1259. return TIPC_ERR_OVERLOAD;
  1260. /* Enqueue message */
  1261. TIPC_SKB_CB(buf)->handle = NULL;
  1262. __skb_queue_tail(&sk->sk_receive_queue, buf);
  1263. skb_set_owner_r(buf, sk);
  1264. sk->sk_data_ready(sk, 0);
  1265. return TIPC_OK;
  1266. }
  1267. /**
  1268. * backlog_rcv - handle incoming message from backlog queue
  1269. * @sk: socket
  1270. * @buf: message
  1271. *
  1272. * Caller must hold socket lock, but not port lock.
  1273. *
  1274. * Returns 0
  1275. */
  1276. static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
  1277. {
  1278. u32 res;
  1279. res = filter_rcv(sk, buf);
  1280. if (res)
  1281. tipc_reject_msg(buf, res);
  1282. return 0;
  1283. }
  1284. /**
  1285. * dispatch - handle incoming message
  1286. * @tport: TIPC port that received message
  1287. * @buf: message
  1288. *
  1289. * Called with port lock already taken.
  1290. *
  1291. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  1292. */
  1293. static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
  1294. {
  1295. struct sock *sk = tport->sk;
  1296. u32 res;
  1297. /*
  1298. * Process message if socket is unlocked; otherwise add to backlog queue
  1299. *
  1300. * This code is based on sk_receive_skb(), but must be distinct from it
  1301. * since a TIPC-specific filter/reject mechanism is utilized
  1302. */
  1303. bh_lock_sock(sk);
  1304. if (!sock_owned_by_user(sk)) {
  1305. res = filter_rcv(sk, buf);
  1306. } else {
  1307. if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
  1308. res = TIPC_ERR_OVERLOAD;
  1309. else
  1310. res = TIPC_OK;
  1311. }
  1312. bh_unlock_sock(sk);
  1313. return res;
  1314. }
  1315. /**
  1316. * wakeupdispatch - wake up port after congestion
  1317. * @tport: port to wakeup
  1318. *
  1319. * Called with port lock already taken.
  1320. */
  1321. static void wakeupdispatch(struct tipc_port *tport)
  1322. {
  1323. struct sock *sk = tport->sk;
  1324. sk->sk_write_space(sk);
  1325. }
  1326. static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
  1327. {
  1328. struct sock *sk = sock->sk;
  1329. DEFINE_WAIT(wait);
  1330. int done;
  1331. do {
  1332. int err = sock_error(sk);
  1333. if (err)
  1334. return err;
  1335. if (!*timeo_p)
  1336. return -ETIMEDOUT;
  1337. if (signal_pending(current))
  1338. return sock_intr_errno(*timeo_p);
  1339. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1340. done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
  1341. finish_wait(sk_sleep(sk), &wait);
  1342. } while (!done);
  1343. return 0;
  1344. }
  1345. /**
  1346. * connect - establish a connection to another TIPC port
  1347. * @sock: socket structure
  1348. * @dest: socket address for destination port
  1349. * @destlen: size of socket address data structure
  1350. * @flags: file-related flags associated with socket
  1351. *
  1352. * Returns 0 on success, errno otherwise
  1353. */
  1354. static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
  1355. int flags)
  1356. {
  1357. struct sock *sk = sock->sk;
  1358. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1359. struct msghdr m = {NULL,};
  1360. long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
  1361. socket_state previous;
  1362. int res;
  1363. lock_sock(sk);
  1364. /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
  1365. if (sock->state == SS_READY) {
  1366. res = -EOPNOTSUPP;
  1367. goto exit;
  1368. }
  1369. /*
  1370. * Reject connection attempt using multicast address
  1371. *
  1372. * Note: send_msg() validates the rest of the address fields,
  1373. * so there's no need to do it here
  1374. */
  1375. if (dst->addrtype == TIPC_ADDR_MCAST) {
  1376. res = -EINVAL;
  1377. goto exit;
  1378. }
  1379. previous = sock->state;
  1380. switch (sock->state) {
  1381. case SS_UNCONNECTED:
  1382. /* Send a 'SYN-' to destination */
  1383. m.msg_name = dest;
  1384. m.msg_namelen = destlen;
  1385. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  1386. * indicate send_msg() is never blocked.
  1387. */
  1388. if (!timeout)
  1389. m.msg_flags = MSG_DONTWAIT;
  1390. res = send_msg(NULL, sock, &m, 0);
  1391. if ((res < 0) && (res != -EWOULDBLOCK))
  1392. goto exit;
  1393. /* Just entered SS_CONNECTING state; the only
  1394. * difference is that return value in non-blocking
  1395. * case is EINPROGRESS, rather than EALREADY.
  1396. */
  1397. res = -EINPROGRESS;
  1398. case SS_CONNECTING:
  1399. if (previous == SS_CONNECTING)
  1400. res = -EALREADY;
  1401. if (!timeout)
  1402. goto exit;
  1403. timeout = msecs_to_jiffies(timeout);
  1404. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  1405. res = tipc_wait_for_connect(sock, &timeout);
  1406. break;
  1407. case SS_CONNECTED:
  1408. res = -EISCONN;
  1409. break;
  1410. default:
  1411. res = -EINVAL;
  1412. break;
  1413. }
  1414. exit:
  1415. release_sock(sk);
  1416. return res;
  1417. }
  1418. /**
  1419. * listen - allow socket to listen for incoming connections
  1420. * @sock: socket structure
  1421. * @len: (unused)
  1422. *
  1423. * Returns 0 on success, errno otherwise
  1424. */
  1425. static int listen(struct socket *sock, int len)
  1426. {
  1427. struct sock *sk = sock->sk;
  1428. int res;
  1429. lock_sock(sk);
  1430. if (sock->state != SS_UNCONNECTED)
  1431. res = -EINVAL;
  1432. else {
  1433. sock->state = SS_LISTENING;
  1434. res = 0;
  1435. }
  1436. release_sock(sk);
  1437. return res;
  1438. }
  1439. static int tipc_wait_for_accept(struct socket *sock, long timeo)
  1440. {
  1441. struct sock *sk = sock->sk;
  1442. DEFINE_WAIT(wait);
  1443. int err;
  1444. /* True wake-one mechanism for incoming connections: only
  1445. * one process gets woken up, not the 'whole herd'.
  1446. * Since we do not 'race & poll' for established sockets
  1447. * anymore, the common case will execute the loop only once.
  1448. */
  1449. for (;;) {
  1450. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  1451. TASK_INTERRUPTIBLE);
  1452. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1453. release_sock(sk);
  1454. timeo = schedule_timeout(timeo);
  1455. lock_sock(sk);
  1456. }
  1457. err = 0;
  1458. if (!skb_queue_empty(&sk->sk_receive_queue))
  1459. break;
  1460. err = -EINVAL;
  1461. if (sock->state != SS_LISTENING)
  1462. break;
  1463. err = sock_intr_errno(timeo);
  1464. if (signal_pending(current))
  1465. break;
  1466. err = -EAGAIN;
  1467. if (!timeo)
  1468. break;
  1469. }
  1470. finish_wait(sk_sleep(sk), &wait);
  1471. return err;
  1472. }
  1473. /**
  1474. * accept - wait for connection request
  1475. * @sock: listening socket
  1476. * @newsock: new socket that is to be connected
  1477. * @flags: file-related flags associated with socket
  1478. *
  1479. * Returns 0 on success, errno otherwise
  1480. */
  1481. static int accept(struct socket *sock, struct socket *new_sock, int flags)
  1482. {
  1483. struct sock *new_sk, *sk = sock->sk;
  1484. struct sk_buff *buf;
  1485. struct tipc_sock *new_tsock;
  1486. struct tipc_port *new_tport;
  1487. struct tipc_msg *msg;
  1488. u32 new_ref;
  1489. long timeo;
  1490. int res;
  1491. lock_sock(sk);
  1492. if (sock->state != SS_LISTENING) {
  1493. res = -EINVAL;
  1494. goto exit;
  1495. }
  1496. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  1497. res = tipc_wait_for_accept(sock, timeo);
  1498. if (res)
  1499. goto exit;
  1500. buf = skb_peek(&sk->sk_receive_queue);
  1501. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
  1502. if (res)
  1503. goto exit;
  1504. new_sk = new_sock->sk;
  1505. new_tsock = tipc_sk(new_sk);
  1506. new_tport = new_tsock->p;
  1507. new_ref = new_tport->ref;
  1508. msg = buf_msg(buf);
  1509. /* we lock on new_sk; but lockdep sees the lock on sk */
  1510. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  1511. /*
  1512. * Reject any stray messages received by new socket
  1513. * before the socket lock was taken (very, very unlikely)
  1514. */
  1515. reject_rx_queue(new_sk);
  1516. /* Connect new socket to it's peer */
  1517. new_tsock->peer_name.ref = msg_origport(msg);
  1518. new_tsock->peer_name.node = msg_orignode(msg);
  1519. tipc_connect(new_ref, &new_tsock->peer_name);
  1520. new_sock->state = SS_CONNECTED;
  1521. tipc_set_portimportance(new_ref, msg_importance(msg));
  1522. if (msg_named(msg)) {
  1523. new_tport->conn_type = msg_nametype(msg);
  1524. new_tport->conn_instance = msg_nameinst(msg);
  1525. }
  1526. /*
  1527. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1528. * Respond to 'SYN+' by queuing it on new socket.
  1529. */
  1530. if (!msg_data_sz(msg)) {
  1531. struct msghdr m = {NULL,};
  1532. advance_rx_queue(sk);
  1533. send_packet(NULL, new_sock, &m, 0);
  1534. } else {
  1535. __skb_dequeue(&sk->sk_receive_queue);
  1536. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  1537. skb_set_owner_r(buf, new_sk);
  1538. }
  1539. release_sock(new_sk);
  1540. exit:
  1541. release_sock(sk);
  1542. return res;
  1543. }
  1544. /**
  1545. * shutdown - shutdown socket connection
  1546. * @sock: socket structure
  1547. * @how: direction to close (must be SHUT_RDWR)
  1548. *
  1549. * Terminates connection (if necessary), then purges socket's receive queue.
  1550. *
  1551. * Returns 0 on success, errno otherwise
  1552. */
  1553. static int shutdown(struct socket *sock, int how)
  1554. {
  1555. struct sock *sk = sock->sk;
  1556. struct tipc_port *tport = tipc_sk_port(sk);
  1557. struct sk_buff *buf;
  1558. int res;
  1559. if (how != SHUT_RDWR)
  1560. return -EINVAL;
  1561. lock_sock(sk);
  1562. switch (sock->state) {
  1563. case SS_CONNECTING:
  1564. case SS_CONNECTED:
  1565. restart:
  1566. /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
  1567. buf = __skb_dequeue(&sk->sk_receive_queue);
  1568. if (buf) {
  1569. if (TIPC_SKB_CB(buf)->handle != NULL) {
  1570. kfree_skb(buf);
  1571. goto restart;
  1572. }
  1573. tipc_disconnect(tport->ref);
  1574. tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
  1575. } else {
  1576. tipc_shutdown(tport->ref);
  1577. }
  1578. sock->state = SS_DISCONNECTING;
  1579. /* fall through */
  1580. case SS_DISCONNECTING:
  1581. /* Discard any unreceived messages */
  1582. __skb_queue_purge(&sk->sk_receive_queue);
  1583. /* Wake up anyone sleeping in poll */
  1584. sk->sk_state_change(sk);
  1585. res = 0;
  1586. break;
  1587. default:
  1588. res = -ENOTCONN;
  1589. }
  1590. release_sock(sk);
  1591. return res;
  1592. }
  1593. /**
  1594. * setsockopt - set socket option
  1595. * @sock: socket structure
  1596. * @lvl: option level
  1597. * @opt: option identifier
  1598. * @ov: pointer to new option value
  1599. * @ol: length of option value
  1600. *
  1601. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  1602. * (to ease compatibility).
  1603. *
  1604. * Returns 0 on success, errno otherwise
  1605. */
  1606. static int setsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
  1607. unsigned int ol)
  1608. {
  1609. struct sock *sk = sock->sk;
  1610. struct tipc_port *tport = tipc_sk_port(sk);
  1611. u32 value;
  1612. int res;
  1613. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1614. return 0;
  1615. if (lvl != SOL_TIPC)
  1616. return -ENOPROTOOPT;
  1617. if (ol < sizeof(value))
  1618. return -EINVAL;
  1619. res = get_user(value, (u32 __user *)ov);
  1620. if (res)
  1621. return res;
  1622. lock_sock(sk);
  1623. switch (opt) {
  1624. case TIPC_IMPORTANCE:
  1625. res = tipc_set_portimportance(tport->ref, value);
  1626. break;
  1627. case TIPC_SRC_DROPPABLE:
  1628. if (sock->type != SOCK_STREAM)
  1629. res = tipc_set_portunreliable(tport->ref, value);
  1630. else
  1631. res = -ENOPROTOOPT;
  1632. break;
  1633. case TIPC_DEST_DROPPABLE:
  1634. res = tipc_set_portunreturnable(tport->ref, value);
  1635. break;
  1636. case TIPC_CONN_TIMEOUT:
  1637. tipc_sk(sk)->conn_timeout = value;
  1638. /* no need to set "res", since already 0 at this point */
  1639. break;
  1640. default:
  1641. res = -EINVAL;
  1642. }
  1643. release_sock(sk);
  1644. return res;
  1645. }
  1646. /**
  1647. * getsockopt - get socket option
  1648. * @sock: socket structure
  1649. * @lvl: option level
  1650. * @opt: option identifier
  1651. * @ov: receptacle for option value
  1652. * @ol: receptacle for length of option value
  1653. *
  1654. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  1655. * (to ease compatibility).
  1656. *
  1657. * Returns 0 on success, errno otherwise
  1658. */
  1659. static int getsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
  1660. int __user *ol)
  1661. {
  1662. struct sock *sk = sock->sk;
  1663. struct tipc_port *tport = tipc_sk_port(sk);
  1664. int len;
  1665. u32 value;
  1666. int res;
  1667. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1668. return put_user(0, ol);
  1669. if (lvl != SOL_TIPC)
  1670. return -ENOPROTOOPT;
  1671. res = get_user(len, ol);
  1672. if (res)
  1673. return res;
  1674. lock_sock(sk);
  1675. switch (opt) {
  1676. case TIPC_IMPORTANCE:
  1677. res = tipc_portimportance(tport->ref, &value);
  1678. break;
  1679. case TIPC_SRC_DROPPABLE:
  1680. res = tipc_portunreliable(tport->ref, &value);
  1681. break;
  1682. case TIPC_DEST_DROPPABLE:
  1683. res = tipc_portunreturnable(tport->ref, &value);
  1684. break;
  1685. case TIPC_CONN_TIMEOUT:
  1686. value = tipc_sk(sk)->conn_timeout;
  1687. /* no need to set "res", since already 0 at this point */
  1688. break;
  1689. case TIPC_NODE_RECVQ_DEPTH:
  1690. value = 0; /* was tipc_queue_size, now obsolete */
  1691. break;
  1692. case TIPC_SOCK_RECVQ_DEPTH:
  1693. value = skb_queue_len(&sk->sk_receive_queue);
  1694. break;
  1695. default:
  1696. res = -EINVAL;
  1697. }
  1698. release_sock(sk);
  1699. if (res)
  1700. return res; /* "get" failed */
  1701. if (len < sizeof(value))
  1702. return -EINVAL;
  1703. if (copy_to_user(ov, &value, sizeof(value)))
  1704. return -EFAULT;
  1705. return put_user(sizeof(value), ol);
  1706. }
  1707. /* Protocol switches for the various types of TIPC sockets */
  1708. static const struct proto_ops msg_ops = {
  1709. .owner = THIS_MODULE,
  1710. .family = AF_TIPC,
  1711. .release = release,
  1712. .bind = bind,
  1713. .connect = connect,
  1714. .socketpair = sock_no_socketpair,
  1715. .accept = sock_no_accept,
  1716. .getname = get_name,
  1717. .poll = poll,
  1718. .ioctl = sock_no_ioctl,
  1719. .listen = sock_no_listen,
  1720. .shutdown = shutdown,
  1721. .setsockopt = setsockopt,
  1722. .getsockopt = getsockopt,
  1723. .sendmsg = send_msg,
  1724. .recvmsg = recv_msg,
  1725. .mmap = sock_no_mmap,
  1726. .sendpage = sock_no_sendpage
  1727. };
  1728. static const struct proto_ops packet_ops = {
  1729. .owner = THIS_MODULE,
  1730. .family = AF_TIPC,
  1731. .release = release,
  1732. .bind = bind,
  1733. .connect = connect,
  1734. .socketpair = sock_no_socketpair,
  1735. .accept = accept,
  1736. .getname = get_name,
  1737. .poll = poll,
  1738. .ioctl = sock_no_ioctl,
  1739. .listen = listen,
  1740. .shutdown = shutdown,
  1741. .setsockopt = setsockopt,
  1742. .getsockopt = getsockopt,
  1743. .sendmsg = send_packet,
  1744. .recvmsg = recv_msg,
  1745. .mmap = sock_no_mmap,
  1746. .sendpage = sock_no_sendpage
  1747. };
  1748. static const struct proto_ops stream_ops = {
  1749. .owner = THIS_MODULE,
  1750. .family = AF_TIPC,
  1751. .release = release,
  1752. .bind = bind,
  1753. .connect = connect,
  1754. .socketpair = sock_no_socketpair,
  1755. .accept = accept,
  1756. .getname = get_name,
  1757. .poll = poll,
  1758. .ioctl = sock_no_ioctl,
  1759. .listen = listen,
  1760. .shutdown = shutdown,
  1761. .setsockopt = setsockopt,
  1762. .getsockopt = getsockopt,
  1763. .sendmsg = send_stream,
  1764. .recvmsg = recv_stream,
  1765. .mmap = sock_no_mmap,
  1766. .sendpage = sock_no_sendpage
  1767. };
  1768. static const struct net_proto_family tipc_family_ops = {
  1769. .owner = THIS_MODULE,
  1770. .family = AF_TIPC,
  1771. .create = tipc_sk_create
  1772. };
  1773. static struct proto tipc_proto = {
  1774. .name = "TIPC",
  1775. .owner = THIS_MODULE,
  1776. .obj_size = sizeof(struct tipc_sock),
  1777. .sysctl_rmem = sysctl_tipc_rmem
  1778. };
  1779. static struct proto tipc_proto_kern = {
  1780. .name = "TIPC",
  1781. .obj_size = sizeof(struct tipc_sock),
  1782. .sysctl_rmem = sysctl_tipc_rmem
  1783. };
  1784. /**
  1785. * tipc_socket_init - initialize TIPC socket interface
  1786. *
  1787. * Returns 0 on success, errno otherwise
  1788. */
  1789. int tipc_socket_init(void)
  1790. {
  1791. int res;
  1792. res = proto_register(&tipc_proto, 1);
  1793. if (res) {
  1794. pr_err("Failed to register TIPC protocol type\n");
  1795. goto out;
  1796. }
  1797. res = sock_register(&tipc_family_ops);
  1798. if (res) {
  1799. pr_err("Failed to register TIPC socket type\n");
  1800. proto_unregister(&tipc_proto);
  1801. goto out;
  1802. }
  1803. out:
  1804. return res;
  1805. }
  1806. /**
  1807. * tipc_socket_stop - stop TIPC socket interface
  1808. */
  1809. void tipc_socket_stop(void)
  1810. {
  1811. sock_unregister(tipc_family_ops.family);
  1812. proto_unregister(&tipc_proto);
  1813. }