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