socket.c 46 KB

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