socket.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892
  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. *
  159. * This routine creates additional data structures used by the TIPC socket,
  160. * initializes them, and links them together.
  161. *
  162. * Returns 0 on success, errno otherwise
  163. */
  164. static int tipc_create(struct net *net, struct socket *sock, int protocol)
  165. {
  166. const struct proto_ops *ops;
  167. socket_state state;
  168. struct sock *sk;
  169. struct tipc_port *tp_ptr;
  170. /* Validate arguments */
  171. if (net != &init_net)
  172. return -EAFNOSUPPORT;
  173. if (unlikely(protocol != 0))
  174. return -EPROTONOSUPPORT;
  175. switch (sock->type) {
  176. case SOCK_STREAM:
  177. ops = &stream_ops;
  178. state = SS_UNCONNECTED;
  179. break;
  180. case SOCK_SEQPACKET:
  181. ops = &packet_ops;
  182. state = SS_UNCONNECTED;
  183. break;
  184. case SOCK_DGRAM:
  185. case SOCK_RDM:
  186. ops = &msg_ops;
  187. state = SS_READY;
  188. break;
  189. default:
  190. return -EPROTOTYPE;
  191. }
  192. /* Allocate socket's protocol area */
  193. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
  194. if (sk == NULL)
  195. return -ENOMEM;
  196. /* Allocate TIPC port for socket to use */
  197. tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
  198. TIPC_LOW_IMPORTANCE);
  199. if (unlikely(!tp_ptr)) {
  200. sk_free(sk);
  201. return -ENOMEM;
  202. }
  203. /* Finish initializing socket data structures */
  204. sock->ops = ops;
  205. sock->state = state;
  206. sock_init_data(sock, sk);
  207. sk->sk_rcvtimeo = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
  208. sk->sk_backlog_rcv = backlog_rcv;
  209. tipc_sk(sk)->p = tp_ptr;
  210. spin_unlock_bh(tp_ptr->lock);
  211. if (sock->state == SS_READY) {
  212. tipc_set_portunreturnable(tp_ptr->ref, 1);
  213. if (sock->type == SOCK_DGRAM)
  214. tipc_set_portunreliable(tp_ptr->ref, 1);
  215. }
  216. atomic_inc(&tipc_user_count);
  217. return 0;
  218. }
  219. /**
  220. * release - destroy a TIPC socket
  221. * @sock: socket to destroy
  222. *
  223. * This routine cleans up any messages that are still queued on the socket.
  224. * For DGRAM and RDM socket types, all queued messages are rejected.
  225. * For SEQPACKET and STREAM socket types, the first message is rejected
  226. * and any others are discarded. (If the first message on a STREAM socket
  227. * is partially-read, it is discarded and the next one is rejected instead.)
  228. *
  229. * NOTE: Rejected messages are not necessarily returned to the sender! They
  230. * are returned or discarded according to the "destination droppable" setting
  231. * specified for the message by the sender.
  232. *
  233. * Returns 0 on success, errno otherwise
  234. */
  235. static int release(struct socket *sock)
  236. {
  237. struct sock *sk = sock->sk;
  238. struct tipc_port *tport;
  239. struct sk_buff *buf;
  240. int res;
  241. /*
  242. * Exit if socket isn't fully initialized (occurs when a failed accept()
  243. * releases a pre-allocated child socket that was never used)
  244. */
  245. if (sk == NULL)
  246. return 0;
  247. tport = tipc_sk_port(sk);
  248. lock_sock(sk);
  249. /*
  250. * Reject all unreceived messages, except on an active connection
  251. * (which disconnects locally & sends a 'FIN+' to peer)
  252. */
  253. while (sock->state != SS_DISCONNECTING) {
  254. buf = __skb_dequeue(&sk->sk_receive_queue);
  255. if (buf == NULL)
  256. break;
  257. atomic_dec(&tipc_queue_size);
  258. if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
  259. buf_discard(buf);
  260. else {
  261. if ((sock->state == SS_CONNECTING) ||
  262. (sock->state == SS_CONNECTED)) {
  263. sock->state = SS_DISCONNECTING;
  264. tipc_disconnect(tport->ref);
  265. }
  266. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  267. }
  268. }
  269. /*
  270. * Delete TIPC port; this ensures no more messages are queued
  271. * (also disconnects an active connection & sends a 'FIN-' to peer)
  272. */
  273. res = tipc_deleteport(tport->ref);
  274. /* Discard any remaining (connection-based) messages in receive queue */
  275. discard_rx_queue(sk);
  276. /* Reject any messages that accumulated in backlog queue */
  277. sock->state = SS_DISCONNECTING;
  278. release_sock(sk);
  279. sock_put(sk);
  280. sock->sk = NULL;
  281. atomic_dec(&tipc_user_count);
  282. return res;
  283. }
  284. /**
  285. * bind - associate or disassocate TIPC name(s) with a socket
  286. * @sock: socket structure
  287. * @uaddr: socket address describing name(s) and desired operation
  288. * @uaddr_len: size of socket address data structure
  289. *
  290. * Name and name sequence binding is indicated using a positive scope value;
  291. * a negative scope value unbinds the specified name. Specifying no name
  292. * (i.e. a socket address length of 0) unbinds all names from the socket.
  293. *
  294. * Returns 0 on success, errno otherwise
  295. *
  296. * NOTE: This routine doesn't need to take the socket lock since it doesn't
  297. * access any non-constant socket information.
  298. */
  299. static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
  300. {
  301. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  302. u32 portref = tipc_sk_port(sock->sk)->ref;
  303. if (unlikely(!uaddr_len))
  304. return tipc_withdraw(portref, 0, NULL);
  305. if (uaddr_len < sizeof(struct sockaddr_tipc))
  306. return -EINVAL;
  307. if (addr->family != AF_TIPC)
  308. return -EAFNOSUPPORT;
  309. if (addr->addrtype == TIPC_ADDR_NAME)
  310. addr->addr.nameseq.upper = addr->addr.nameseq.lower;
  311. else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
  312. return -EAFNOSUPPORT;
  313. return (addr->scope > 0) ?
  314. tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
  315. tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
  316. }
  317. /**
  318. * get_name - get port ID of socket or peer socket
  319. * @sock: socket structure
  320. * @uaddr: area for returned socket address
  321. * @uaddr_len: area for returned length of socket address
  322. * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
  323. *
  324. * Returns 0 on success, errno otherwise
  325. *
  326. * NOTE: This routine doesn't need to take the socket lock since it only
  327. * accesses socket information that is unchanging (or which changes in
  328. * a completely predictable manner).
  329. */
  330. static int get_name(struct socket *sock, struct sockaddr *uaddr,
  331. int *uaddr_len, int peer)
  332. {
  333. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  334. struct tipc_sock *tsock = tipc_sk(sock->sk);
  335. if (peer) {
  336. if ((sock->state != SS_CONNECTED) &&
  337. ((peer != 2) || (sock->state != SS_DISCONNECTING)))
  338. return -ENOTCONN;
  339. addr->addr.id.ref = tsock->peer_name.ref;
  340. addr->addr.id.node = tsock->peer_name.node;
  341. } else {
  342. tipc_ownidentity(tsock->p->ref, &addr->addr.id);
  343. }
  344. *uaddr_len = sizeof(*addr);
  345. addr->addrtype = TIPC_ADDR_ID;
  346. addr->family = AF_TIPC;
  347. addr->scope = 0;
  348. addr->addr.name.domain = 0;
  349. return 0;
  350. }
  351. /**
  352. * poll - read and possibly block on pollmask
  353. * @file: file structure associated with the socket
  354. * @sock: socket for which to calculate the poll bits
  355. * @wait: ???
  356. *
  357. * Returns pollmask value
  358. *
  359. * COMMENTARY:
  360. * It appears that the usual socket locking mechanisms are not useful here
  361. * since the pollmask info is potentially out-of-date the moment this routine
  362. * exits. TCP and other protocols seem to rely on higher level poll routines
  363. * to handle any preventable race conditions, so TIPC will do the same ...
  364. *
  365. * TIPC sets the returned events as follows:
  366. * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
  367. * or if a connection-oriented socket is does not have an active connection
  368. * (i.e. a read operation will not block).
  369. * b) POLLOUT is set except when a socket's connection has been terminated
  370. * (i.e. a write operation will not block).
  371. * c) POLLHUP is set when a socket's connection has been terminated.
  372. *
  373. * IMPORTANT: The fact that a read or write operation will not block does NOT
  374. * imply that the operation will succeed!
  375. */
  376. static unsigned int poll(struct file *file, struct socket *sock,
  377. poll_table *wait)
  378. {
  379. struct sock *sk = sock->sk;
  380. u32 mask;
  381. poll_wait(file, sk->sk_sleep, wait);
  382. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  383. (sock->state == SS_UNCONNECTED) ||
  384. (sock->state == SS_DISCONNECTING))
  385. mask = (POLLRDNORM | POLLIN);
  386. else
  387. mask = 0;
  388. if (sock->state == SS_DISCONNECTING)
  389. mask |= POLLHUP;
  390. else
  391. mask |= POLLOUT;
  392. return mask;
  393. }
  394. /**
  395. * dest_name_check - verify user is permitted to send to specified port name
  396. * @dest: destination address
  397. * @m: descriptor for message to be sent
  398. *
  399. * Prevents restricted configuration commands from being issued by
  400. * unauthorized users.
  401. *
  402. * Returns 0 if permission is granted, otherwise errno
  403. */
  404. static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
  405. {
  406. struct tipc_cfg_msg_hdr hdr;
  407. if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
  408. return 0;
  409. if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
  410. return 0;
  411. if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
  412. return -EACCES;
  413. if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
  414. return -EFAULT;
  415. if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
  416. return -EACCES;
  417. return 0;
  418. }
  419. /**
  420. * send_msg - send message in connectionless manner
  421. * @iocb: if NULL, indicates that socket lock is already held
  422. * @sock: socket structure
  423. * @m: message to send
  424. * @total_len: length of message
  425. *
  426. * Message must have an destination specified explicitly.
  427. * Used for SOCK_RDM and SOCK_DGRAM messages,
  428. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  429. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  430. *
  431. * Returns the number of bytes sent on success, or errno otherwise
  432. */
  433. static int send_msg(struct kiocb *iocb, struct socket *sock,
  434. struct msghdr *m, size_t total_len)
  435. {
  436. struct sock *sk = sock->sk;
  437. struct tipc_port *tport = tipc_sk_port(sk);
  438. struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
  439. int needs_conn;
  440. int res = -EINVAL;
  441. if (unlikely(!dest))
  442. return -EDESTADDRREQ;
  443. if (unlikely((m->msg_namelen < sizeof(*dest)) ||
  444. (dest->family != AF_TIPC)))
  445. return -EINVAL;
  446. if (iocb)
  447. lock_sock(sk);
  448. needs_conn = (sock->state != SS_READY);
  449. if (unlikely(needs_conn)) {
  450. if (sock->state == SS_LISTENING) {
  451. res = -EPIPE;
  452. goto exit;
  453. }
  454. if (sock->state != SS_UNCONNECTED) {
  455. res = -EISCONN;
  456. goto exit;
  457. }
  458. if ((tport->published) ||
  459. ((sock->type == SOCK_STREAM) && (total_len != 0))) {
  460. res = -EOPNOTSUPP;
  461. goto exit;
  462. }
  463. if (dest->addrtype == TIPC_ADDR_NAME) {
  464. tport->conn_type = dest->addr.name.name.type;
  465. tport->conn_instance = dest->addr.name.name.instance;
  466. }
  467. /* Abort any pending connection attempts (very unlikely) */
  468. reject_rx_queue(sk);
  469. }
  470. do {
  471. if (dest->addrtype == TIPC_ADDR_NAME) {
  472. if ((res = dest_name_check(dest, m)))
  473. break;
  474. res = tipc_send2name(tport->ref,
  475. &dest->addr.name.name,
  476. dest->addr.name.domain,
  477. m->msg_iovlen,
  478. m->msg_iov);
  479. }
  480. else if (dest->addrtype == TIPC_ADDR_ID) {
  481. res = tipc_send2port(tport->ref,
  482. &dest->addr.id,
  483. m->msg_iovlen,
  484. m->msg_iov);
  485. }
  486. else if (dest->addrtype == TIPC_ADDR_MCAST) {
  487. if (needs_conn) {
  488. res = -EOPNOTSUPP;
  489. break;
  490. }
  491. if ((res = dest_name_check(dest, m)))
  492. break;
  493. res = tipc_multicast(tport->ref,
  494. &dest->addr.nameseq,
  495. 0,
  496. m->msg_iovlen,
  497. m->msg_iov);
  498. }
  499. if (likely(res != -ELINKCONG)) {
  500. if (needs_conn && (res >= 0)) {
  501. sock->state = SS_CONNECTING;
  502. }
  503. break;
  504. }
  505. if (m->msg_flags & MSG_DONTWAIT) {
  506. res = -EWOULDBLOCK;
  507. break;
  508. }
  509. release_sock(sk);
  510. res = wait_event_interruptible(*sk->sk_sleep,
  511. !tport->congested);
  512. lock_sock(sk);
  513. if (res)
  514. break;
  515. } while (1);
  516. exit:
  517. if (iocb)
  518. release_sock(sk);
  519. return res;
  520. }
  521. /**
  522. * send_packet - send a connection-oriented message
  523. * @iocb: if NULL, indicates that socket lock is already held
  524. * @sock: socket structure
  525. * @m: message to send
  526. * @total_len: length of message
  527. *
  528. * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
  529. *
  530. * Returns the number of bytes sent on success, or errno otherwise
  531. */
  532. static int send_packet(struct kiocb *iocb, struct socket *sock,
  533. struct msghdr *m, size_t total_len)
  534. {
  535. struct sock *sk = sock->sk;
  536. struct tipc_port *tport = tipc_sk_port(sk);
  537. struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
  538. int res;
  539. /* Handle implied connection establishment */
  540. if (unlikely(dest))
  541. return send_msg(iocb, sock, m, total_len);
  542. if (iocb)
  543. lock_sock(sk);
  544. do {
  545. if (unlikely(sock->state != SS_CONNECTED)) {
  546. if (sock->state == SS_DISCONNECTING)
  547. res = -EPIPE;
  548. else
  549. res = -ENOTCONN;
  550. break;
  551. }
  552. res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov);
  553. if (likely(res != -ELINKCONG)) {
  554. break;
  555. }
  556. if (m->msg_flags & MSG_DONTWAIT) {
  557. res = -EWOULDBLOCK;
  558. break;
  559. }
  560. release_sock(sk);
  561. res = wait_event_interruptible(*sk->sk_sleep,
  562. (!tport->congested || !tport->connected));
  563. lock_sock(sk);
  564. if (res)
  565. break;
  566. } while (1);
  567. if (iocb)
  568. release_sock(sk);
  569. return res;
  570. }
  571. /**
  572. * send_stream - send stream-oriented data
  573. * @iocb: (unused)
  574. * @sock: socket structure
  575. * @m: data to send
  576. * @total_len: total length of data to be sent
  577. *
  578. * Used for SOCK_STREAM data.
  579. *
  580. * Returns the number of bytes sent on success (or partial success),
  581. * or errno if no data sent
  582. */
  583. static int send_stream(struct kiocb *iocb, struct socket *sock,
  584. struct msghdr *m, size_t total_len)
  585. {
  586. struct sock *sk = sock->sk;
  587. struct tipc_port *tport = tipc_sk_port(sk);
  588. struct msghdr my_msg;
  589. struct iovec my_iov;
  590. struct iovec *curr_iov;
  591. int curr_iovlen;
  592. char __user *curr_start;
  593. u32 hdr_size;
  594. int curr_left;
  595. int bytes_to_send;
  596. int bytes_sent;
  597. int res;
  598. lock_sock(sk);
  599. /* Handle special cases where there is no connection */
  600. if (unlikely(sock->state != SS_CONNECTED)) {
  601. if (sock->state == SS_UNCONNECTED) {
  602. res = send_packet(NULL, sock, m, total_len);
  603. goto exit;
  604. } else if (sock->state == SS_DISCONNECTING) {
  605. res = -EPIPE;
  606. goto exit;
  607. } else {
  608. res = -ENOTCONN;
  609. goto exit;
  610. }
  611. }
  612. if (unlikely(m->msg_name)) {
  613. res = -EISCONN;
  614. goto exit;
  615. }
  616. /*
  617. * Send each iovec entry using one or more messages
  618. *
  619. * Note: This algorithm is good for the most likely case
  620. * (i.e. one large iovec entry), but could be improved to pass sets
  621. * of small iovec entries into send_packet().
  622. */
  623. curr_iov = m->msg_iov;
  624. curr_iovlen = m->msg_iovlen;
  625. my_msg.msg_iov = &my_iov;
  626. my_msg.msg_iovlen = 1;
  627. my_msg.msg_flags = m->msg_flags;
  628. my_msg.msg_name = NULL;
  629. bytes_sent = 0;
  630. hdr_size = msg_hdr_sz(&tport->phdr);
  631. while (curr_iovlen--) {
  632. curr_start = curr_iov->iov_base;
  633. curr_left = curr_iov->iov_len;
  634. while (curr_left) {
  635. bytes_to_send = tport->max_pkt - hdr_size;
  636. if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
  637. bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
  638. if (curr_left < bytes_to_send)
  639. bytes_to_send = curr_left;
  640. my_iov.iov_base = curr_start;
  641. my_iov.iov_len = bytes_to_send;
  642. if ((res = send_packet(NULL, sock, &my_msg, 0)) < 0) {
  643. if (bytes_sent)
  644. res = bytes_sent;
  645. goto exit;
  646. }
  647. curr_left -= bytes_to_send;
  648. curr_start += bytes_to_send;
  649. bytes_sent += bytes_to_send;
  650. }
  651. curr_iov++;
  652. }
  653. res = bytes_sent;
  654. exit:
  655. release_sock(sk);
  656. return res;
  657. }
  658. /**
  659. * auto_connect - complete connection setup to a remote port
  660. * @sock: socket structure
  661. * @msg: peer's response message
  662. *
  663. * Returns 0 on success, errno otherwise
  664. */
  665. static int auto_connect(struct socket *sock, struct tipc_msg *msg)
  666. {
  667. struct tipc_sock *tsock = tipc_sk(sock->sk);
  668. if (msg_errcode(msg)) {
  669. sock->state = SS_DISCONNECTING;
  670. return -ECONNREFUSED;
  671. }
  672. tsock->peer_name.ref = msg_origport(msg);
  673. tsock->peer_name.node = msg_orignode(msg);
  674. tipc_connect2port(tsock->p->ref, &tsock->peer_name);
  675. tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
  676. sock->state = SS_CONNECTED;
  677. return 0;
  678. }
  679. /**
  680. * set_orig_addr - capture sender's address for received message
  681. * @m: descriptor for message info
  682. * @msg: received message header
  683. *
  684. * Note: Address is not captured if not requested by receiver.
  685. */
  686. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  687. {
  688. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
  689. if (addr) {
  690. addr->family = AF_TIPC;
  691. addr->addrtype = TIPC_ADDR_ID;
  692. addr->addr.id.ref = msg_origport(msg);
  693. addr->addr.id.node = msg_orignode(msg);
  694. addr->addr.name.domain = 0; /* could leave uninitialized */
  695. addr->scope = 0; /* could leave uninitialized */
  696. m->msg_namelen = sizeof(struct sockaddr_tipc);
  697. }
  698. }
  699. /**
  700. * anc_data_recv - optionally capture ancillary data for received message
  701. * @m: descriptor for message info
  702. * @msg: received message header
  703. * @tport: TIPC port associated with message
  704. *
  705. * Note: Ancillary data is not captured if not requested by receiver.
  706. *
  707. * Returns 0 if successful, otherwise errno
  708. */
  709. static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  710. struct tipc_port *tport)
  711. {
  712. u32 anc_data[3];
  713. u32 err;
  714. u32 dest_type;
  715. int has_name;
  716. int res;
  717. if (likely(m->msg_controllen == 0))
  718. return 0;
  719. /* Optionally capture errored message object(s) */
  720. err = msg ? msg_errcode(msg) : 0;
  721. if (unlikely(err)) {
  722. anc_data[0] = err;
  723. anc_data[1] = msg_data_sz(msg);
  724. if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data)))
  725. return res;
  726. if (anc_data[1] &&
  727. (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  728. msg_data(msg))))
  729. return res;
  730. }
  731. /* Optionally capture message destination object */
  732. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  733. switch (dest_type) {
  734. case TIPC_NAMED_MSG:
  735. has_name = 1;
  736. anc_data[0] = msg_nametype(msg);
  737. anc_data[1] = msg_namelower(msg);
  738. anc_data[2] = msg_namelower(msg);
  739. break;
  740. case TIPC_MCAST_MSG:
  741. has_name = 1;
  742. anc_data[0] = msg_nametype(msg);
  743. anc_data[1] = msg_namelower(msg);
  744. anc_data[2] = msg_nameupper(msg);
  745. break;
  746. case TIPC_CONN_MSG:
  747. has_name = (tport->conn_type != 0);
  748. anc_data[0] = tport->conn_type;
  749. anc_data[1] = tport->conn_instance;
  750. anc_data[2] = tport->conn_instance;
  751. break;
  752. default:
  753. has_name = 0;
  754. }
  755. if (has_name &&
  756. (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data)))
  757. return res;
  758. return 0;
  759. }
  760. /**
  761. * recv_msg - receive packet-oriented message
  762. * @iocb: (unused)
  763. * @m: descriptor for message info
  764. * @buf_len: total size of user buffer area
  765. * @flags: receive flags
  766. *
  767. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  768. * If the complete message doesn't fit in user area, truncate it.
  769. *
  770. * Returns size of returned message data, errno otherwise
  771. */
  772. static int recv_msg(struct kiocb *iocb, struct socket *sock,
  773. struct msghdr *m, size_t buf_len, int flags)
  774. {
  775. struct sock *sk = sock->sk;
  776. struct tipc_port *tport = tipc_sk_port(sk);
  777. struct sk_buff *buf;
  778. struct tipc_msg *msg;
  779. unsigned int sz;
  780. u32 err;
  781. int res;
  782. /* Catch invalid receive requests */
  783. if (m->msg_iovlen != 1)
  784. return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
  785. if (unlikely(!buf_len))
  786. return -EINVAL;
  787. lock_sock(sk);
  788. if (unlikely(sock->state == SS_UNCONNECTED)) {
  789. res = -ENOTCONN;
  790. goto exit;
  791. }
  792. restart:
  793. /* Look for a message in receive queue; wait if necessary */
  794. while (skb_queue_empty(&sk->sk_receive_queue)) {
  795. if (sock->state == SS_DISCONNECTING) {
  796. res = -ENOTCONN;
  797. goto exit;
  798. }
  799. if (flags & MSG_DONTWAIT) {
  800. res = -EWOULDBLOCK;
  801. goto exit;
  802. }
  803. release_sock(sk);
  804. res = wait_event_interruptible(*sk->sk_sleep,
  805. (!skb_queue_empty(&sk->sk_receive_queue) ||
  806. (sock->state == SS_DISCONNECTING)));
  807. lock_sock(sk);
  808. if (res)
  809. goto exit;
  810. }
  811. /* Look at first message in receive queue */
  812. buf = skb_peek(&sk->sk_receive_queue);
  813. msg = buf_msg(buf);
  814. sz = msg_data_sz(msg);
  815. err = msg_errcode(msg);
  816. /* Complete connection setup for an implied connect */
  817. if (unlikely(sock->state == SS_CONNECTING)) {
  818. res = auto_connect(sock, msg);
  819. if (res)
  820. goto exit;
  821. }
  822. /* Discard an empty non-errored message & try again */
  823. if ((!sz) && (!err)) {
  824. advance_rx_queue(sk);
  825. goto restart;
  826. }
  827. /* Capture sender's address (optional) */
  828. set_orig_addr(m, msg);
  829. /* Capture ancillary data (optional) */
  830. res = anc_data_recv(m, msg, tport);
  831. if (res)
  832. goto exit;
  833. /* Capture message data (if valid) & compute return value (always) */
  834. if (!err) {
  835. if (unlikely(buf_len < sz)) {
  836. sz = buf_len;
  837. m->msg_flags |= MSG_TRUNC;
  838. }
  839. if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
  840. sz))) {
  841. res = -EFAULT;
  842. goto exit;
  843. }
  844. res = sz;
  845. } else {
  846. if ((sock->state == SS_READY) ||
  847. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  848. res = 0;
  849. else
  850. res = -ECONNRESET;
  851. }
  852. /* Consume received message (optional) */
  853. if (likely(!(flags & MSG_PEEK))) {
  854. if ((sock->state != SS_READY) &&
  855. (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  856. tipc_acknowledge(tport->ref, tport->conn_unacked);
  857. advance_rx_queue(sk);
  858. }
  859. exit:
  860. release_sock(sk);
  861. return res;
  862. }
  863. /**
  864. * recv_stream - receive stream-oriented data
  865. * @iocb: (unused)
  866. * @m: descriptor for message info
  867. * @buf_len: total size of user buffer area
  868. * @flags: receive flags
  869. *
  870. * Used for SOCK_STREAM messages only. If not enough data is available
  871. * will optionally wait for more; never truncates data.
  872. *
  873. * Returns size of returned message data, errno otherwise
  874. */
  875. static int recv_stream(struct kiocb *iocb, struct socket *sock,
  876. struct msghdr *m, size_t buf_len, int flags)
  877. {
  878. struct sock *sk = sock->sk;
  879. struct tipc_port *tport = tipc_sk_port(sk);
  880. struct sk_buff *buf;
  881. struct tipc_msg *msg;
  882. unsigned int sz;
  883. int sz_to_copy;
  884. int sz_copied = 0;
  885. int needed;
  886. char __user *crs = m->msg_iov->iov_base;
  887. unsigned char *buf_crs;
  888. u32 err;
  889. int res = 0;
  890. /* Catch invalid receive attempts */
  891. if (m->msg_iovlen != 1)
  892. return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
  893. if (unlikely(!buf_len))
  894. return -EINVAL;
  895. lock_sock(sk);
  896. if (unlikely((sock->state == SS_UNCONNECTED) ||
  897. (sock->state == SS_CONNECTING))) {
  898. res = -ENOTCONN;
  899. goto exit;
  900. }
  901. restart:
  902. /* Look for a message in receive queue; wait if necessary */
  903. while (skb_queue_empty(&sk->sk_receive_queue)) {
  904. if (sock->state == SS_DISCONNECTING) {
  905. res = -ENOTCONN;
  906. goto exit;
  907. }
  908. if (flags & MSG_DONTWAIT) {
  909. res = -EWOULDBLOCK;
  910. goto exit;
  911. }
  912. release_sock(sk);
  913. res = wait_event_interruptible(*sk->sk_sleep,
  914. (!skb_queue_empty(&sk->sk_receive_queue) ||
  915. (sock->state == SS_DISCONNECTING)));
  916. lock_sock(sk);
  917. if (res)
  918. goto exit;
  919. }
  920. /* Look at first message in receive queue */
  921. buf = skb_peek(&sk->sk_receive_queue);
  922. msg = buf_msg(buf);
  923. sz = msg_data_sz(msg);
  924. err = msg_errcode(msg);
  925. /* Discard an empty non-errored message & try again */
  926. if ((!sz) && (!err)) {
  927. advance_rx_queue(sk);
  928. goto restart;
  929. }
  930. /* Optionally capture sender's address & ancillary data of first msg */
  931. if (sz_copied == 0) {
  932. set_orig_addr(m, msg);
  933. res = anc_data_recv(m, msg, tport);
  934. if (res)
  935. goto exit;
  936. }
  937. /* Capture message data (if valid) & compute return value (always) */
  938. if (!err) {
  939. buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
  940. sz = (unsigned char *)msg + msg_size(msg) - buf_crs;
  941. needed = (buf_len - sz_copied);
  942. sz_to_copy = (sz <= needed) ? sz : needed;
  943. if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
  944. res = -EFAULT;
  945. goto exit;
  946. }
  947. sz_copied += sz_to_copy;
  948. if (sz_to_copy < sz) {
  949. if (!(flags & MSG_PEEK))
  950. TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
  951. goto exit;
  952. }
  953. crs += sz_to_copy;
  954. } else {
  955. if (sz_copied != 0)
  956. goto exit; /* can't add error msg to valid data */
  957. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  958. res = 0;
  959. else
  960. res = -ECONNRESET;
  961. }
  962. /* Consume received message (optional) */
  963. if (likely(!(flags & MSG_PEEK))) {
  964. if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  965. tipc_acknowledge(tport->ref, tport->conn_unacked);
  966. advance_rx_queue(sk);
  967. }
  968. /* Loop around if more data is required */
  969. if ((sz_copied < buf_len) /* didn't get all requested data */
  970. && (!skb_queue_empty(&sk->sk_receive_queue) ||
  971. (flags & MSG_WAITALL))
  972. /* ... and more is ready or required */
  973. && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
  974. && (!err) /* ... and haven't reached a FIN */
  975. )
  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);
  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, 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. default:
  1490. res = -EINVAL;
  1491. }
  1492. release_sock(sk);
  1493. if (res) {
  1494. /* "get" failed */
  1495. }
  1496. else if (len < sizeof(value)) {
  1497. res = -EINVAL;
  1498. }
  1499. else if (copy_to_user(ov, &value, sizeof(value))) {
  1500. res = -EFAULT;
  1501. }
  1502. else {
  1503. res = put_user(sizeof(value), ol);
  1504. }
  1505. return res;
  1506. }
  1507. /**
  1508. * Protocol switches for the various types of TIPC sockets
  1509. */
  1510. static const struct proto_ops msg_ops = {
  1511. .owner = THIS_MODULE,
  1512. .family = AF_TIPC,
  1513. .release = release,
  1514. .bind = bind,
  1515. .connect = connect,
  1516. .socketpair = sock_no_socketpair,
  1517. .accept = accept,
  1518. .getname = get_name,
  1519. .poll = poll,
  1520. .ioctl = sock_no_ioctl,
  1521. .listen = listen,
  1522. .shutdown = shutdown,
  1523. .setsockopt = setsockopt,
  1524. .getsockopt = getsockopt,
  1525. .sendmsg = send_msg,
  1526. .recvmsg = recv_msg,
  1527. .mmap = sock_no_mmap,
  1528. .sendpage = sock_no_sendpage
  1529. };
  1530. static const struct proto_ops packet_ops = {
  1531. .owner = THIS_MODULE,
  1532. .family = AF_TIPC,
  1533. .release = release,
  1534. .bind = bind,
  1535. .connect = connect,
  1536. .socketpair = sock_no_socketpair,
  1537. .accept = accept,
  1538. .getname = get_name,
  1539. .poll = poll,
  1540. .ioctl = sock_no_ioctl,
  1541. .listen = listen,
  1542. .shutdown = shutdown,
  1543. .setsockopt = setsockopt,
  1544. .getsockopt = getsockopt,
  1545. .sendmsg = send_packet,
  1546. .recvmsg = recv_msg,
  1547. .mmap = sock_no_mmap,
  1548. .sendpage = sock_no_sendpage
  1549. };
  1550. static const struct proto_ops stream_ops = {
  1551. .owner = THIS_MODULE,
  1552. .family = AF_TIPC,
  1553. .release = release,
  1554. .bind = bind,
  1555. .connect = connect,
  1556. .socketpair = sock_no_socketpair,
  1557. .accept = accept,
  1558. .getname = get_name,
  1559. .poll = poll,
  1560. .ioctl = sock_no_ioctl,
  1561. .listen = listen,
  1562. .shutdown = shutdown,
  1563. .setsockopt = setsockopt,
  1564. .getsockopt = getsockopt,
  1565. .sendmsg = send_stream,
  1566. .recvmsg = recv_stream,
  1567. .mmap = sock_no_mmap,
  1568. .sendpage = sock_no_sendpage
  1569. };
  1570. static const struct net_proto_family tipc_family_ops = {
  1571. .owner = THIS_MODULE,
  1572. .family = AF_TIPC,
  1573. .create = tipc_create
  1574. };
  1575. static struct proto tipc_proto = {
  1576. .name = "TIPC",
  1577. .owner = THIS_MODULE,
  1578. .obj_size = sizeof(struct tipc_sock)
  1579. };
  1580. /**
  1581. * tipc_socket_init - initialize TIPC socket interface
  1582. *
  1583. * Returns 0 on success, errno otherwise
  1584. */
  1585. int tipc_socket_init(void)
  1586. {
  1587. int res;
  1588. res = proto_register(&tipc_proto, 1);
  1589. if (res) {
  1590. err("Failed to register TIPC protocol type\n");
  1591. goto out;
  1592. }
  1593. res = sock_register(&tipc_family_ops);
  1594. if (res) {
  1595. err("Failed to register TIPC socket type\n");
  1596. proto_unregister(&tipc_proto);
  1597. goto out;
  1598. }
  1599. sockets_enabled = 1;
  1600. out:
  1601. return res;
  1602. }
  1603. /**
  1604. * tipc_socket_stop - stop TIPC socket interface
  1605. */
  1606. void tipc_socket_stop(void)
  1607. {
  1608. if (!sockets_enabled)
  1609. return;
  1610. sockets_enabled = 0;
  1611. sock_unregister(tipc_family_ops.family);
  1612. proto_unregister(&tipc_proto);
  1613. }