socket.c 45 KB

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