socket.c 46 KB

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