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