socket.c 47 KB

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