socket.c 46 KB

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