socket.c 46 KB

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