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