socket.c 47 KB

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