socket.c 47 KB

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