socket.c 46 KB

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