socket.c 46 KB

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