socket.c 51 KB

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