socket.c 46 KB

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