socket.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  1. /*
  2. * net/tipc/socket.c: TIPC socket API
  3. *
  4. * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
  5. * Copyright (c) 2004-2008, 2010-2013, 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 "core.h"
  37. #include "port.h"
  38. #include "node.h"
  39. #include <linux/export.h>
  40. #define SS_LISTENING -1 /* socket is listening */
  41. #define SS_READY -2 /* socket is connectionless */
  42. #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
  43. static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
  44. static void tipc_data_ready(struct sock *sk);
  45. static void tipc_write_space(struct sock *sk);
  46. static int tipc_release(struct socket *sock);
  47. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
  48. static const struct proto_ops packet_ops;
  49. static const struct proto_ops stream_ops;
  50. static const struct proto_ops msg_ops;
  51. static struct proto tipc_proto;
  52. static struct proto tipc_proto_kern;
  53. /*
  54. * Revised TIPC socket locking policy:
  55. *
  56. * Most socket operations take the standard socket lock when they start
  57. * and hold it until they finish (or until they need to sleep). Acquiring
  58. * this lock grants the owner exclusive access to the fields of the socket
  59. * data structures, with the exception of the backlog queue. A few socket
  60. * operations can be done without taking the socket lock because they only
  61. * read socket information that never changes during the life of the socket.
  62. *
  63. * Socket operations may acquire the lock for the associated TIPC port if they
  64. * need to perform an operation on the port. If any routine needs to acquire
  65. * both the socket lock and the port lock it must take the socket lock first
  66. * to avoid the risk of deadlock.
  67. *
  68. * The dispatcher handling incoming messages cannot grab the socket lock in
  69. * the standard fashion, since invoked it runs at the BH level and cannot block.
  70. * Instead, it checks to see if the socket lock is currently owned by someone,
  71. * and either handles the message itself or adds it to the socket's backlog
  72. * queue; in the latter case the queued message is processed once the process
  73. * owning the socket lock releases it.
  74. *
  75. * NOTE: Releasing the socket lock while an operation is sleeping overcomes
  76. * the problem of a blocked socket operation preventing any other operations
  77. * from occurring. However, applications must be careful if they have
  78. * multiple threads trying to send (or receive) on the same socket, as these
  79. * operations might interfere with each other. For example, doing a connect
  80. * and a receive at the same time might allow the receive to consume the
  81. * ACK message meant for the connect. While additional work could be done
  82. * to try and overcome this, it doesn't seem to be worthwhile at the present.
  83. *
  84. * NOTE: Releasing the socket lock while an operation is sleeping also ensures
  85. * that another operation that must be performed in a non-blocking manner is
  86. * not delayed for very long because the lock has already been taken.
  87. *
  88. * NOTE: This code assumes that certain fields of a port/socket pair are
  89. * constant over its lifetime; such fields can be examined without taking
  90. * the socket lock and/or port lock, and do not need to be re-read even
  91. * after resuming processing after waiting. These fields include:
  92. * - socket type
  93. * - pointer to socket sk structure (aka tipc_sock structure)
  94. * - pointer to port structure
  95. * - port reference
  96. */
  97. #include "socket.h"
  98. /**
  99. * advance_rx_queue - discard first buffer in socket receive queue
  100. *
  101. * Caller must hold socket lock
  102. */
  103. static void advance_rx_queue(struct sock *sk)
  104. {
  105. kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
  106. }
  107. /**
  108. * reject_rx_queue - reject all buffers in socket receive queue
  109. *
  110. * Caller must hold socket lock
  111. */
  112. static void reject_rx_queue(struct sock *sk)
  113. {
  114. struct sk_buff *buf;
  115. while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
  116. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  117. }
  118. /**
  119. * tipc_sk_create - create a TIPC socket
  120. * @net: network namespace (must be default network)
  121. * @sock: pre-allocated socket structure
  122. * @protocol: protocol indicator (must be 0)
  123. * @kern: caused by kernel or by userspace?
  124. *
  125. * This routine creates additional data structures used by the TIPC socket,
  126. * initializes them, and links them together.
  127. *
  128. * Returns 0 on success, errno otherwise
  129. */
  130. static int tipc_sk_create(struct net *net, struct socket *sock,
  131. int protocol, int kern)
  132. {
  133. const struct proto_ops *ops;
  134. socket_state state;
  135. struct sock *sk;
  136. struct tipc_sock *tsk;
  137. struct tipc_port *port;
  138. u32 ref;
  139. /* Validate arguments */
  140. if (unlikely(protocol != 0))
  141. return -EPROTONOSUPPORT;
  142. switch (sock->type) {
  143. case SOCK_STREAM:
  144. ops = &stream_ops;
  145. state = SS_UNCONNECTED;
  146. break;
  147. case SOCK_SEQPACKET:
  148. ops = &packet_ops;
  149. state = SS_UNCONNECTED;
  150. break;
  151. case SOCK_DGRAM:
  152. case SOCK_RDM:
  153. ops = &msg_ops;
  154. state = SS_READY;
  155. break;
  156. default:
  157. return -EPROTOTYPE;
  158. }
  159. /* Allocate socket's protocol area */
  160. if (!kern)
  161. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
  162. else
  163. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
  164. if (sk == NULL)
  165. return -ENOMEM;
  166. tsk = tipc_sk(sk);
  167. port = &tsk->port;
  168. ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE);
  169. if (!ref) {
  170. pr_warn("Socket registration failed, ref. table exhausted\n");
  171. sk_free(sk);
  172. return -ENOMEM;
  173. }
  174. /* Finish initializing socket data structures */
  175. sock->ops = ops;
  176. sock->state = state;
  177. sock_init_data(sock, sk);
  178. sk->sk_backlog_rcv = tipc_backlog_rcv;
  179. sk->sk_rcvbuf = sysctl_tipc_rmem[1];
  180. sk->sk_data_ready = tipc_data_ready;
  181. sk->sk_write_space = tipc_write_space;
  182. tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
  183. atomic_set(&tsk->dupl_rcvcnt, 0);
  184. tipc_port_unlock(port);
  185. if (sock->state == SS_READY) {
  186. tipc_port_set_unreturnable(port, true);
  187. if (sock->type == SOCK_DGRAM)
  188. tipc_port_set_unreliable(port, true);
  189. }
  190. return 0;
  191. }
  192. /**
  193. * tipc_sock_create_local - create TIPC socket from inside TIPC module
  194. * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
  195. *
  196. * We cannot use sock_creat_kern here because it bumps module user count.
  197. * Since socket owner and creator is the same module we must make sure
  198. * that module count remains zero for module local sockets, otherwise
  199. * we cannot do rmmod.
  200. *
  201. * Returns 0 on success, errno otherwise
  202. */
  203. int tipc_sock_create_local(int type, struct socket **res)
  204. {
  205. int rc;
  206. rc = sock_create_lite(AF_TIPC, type, 0, res);
  207. if (rc < 0) {
  208. pr_err("Failed to create kernel socket\n");
  209. return rc;
  210. }
  211. tipc_sk_create(&init_net, *res, 0, 1);
  212. return 0;
  213. }
  214. /**
  215. * tipc_sock_release_local - release socket created by tipc_sock_create_local
  216. * @sock: the socket to be released.
  217. *
  218. * Module reference count is not incremented when such sockets are created,
  219. * so we must keep it from being decremented when they are released.
  220. */
  221. void tipc_sock_release_local(struct socket *sock)
  222. {
  223. tipc_release(sock);
  224. sock->ops = NULL;
  225. sock_release(sock);
  226. }
  227. /**
  228. * tipc_sock_accept_local - accept a connection on a socket created
  229. * with tipc_sock_create_local. Use this function to avoid that
  230. * module reference count is inadvertently incremented.
  231. *
  232. * @sock: the accepting socket
  233. * @newsock: reference to the new socket to be created
  234. * @flags: socket flags
  235. */
  236. int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
  237. int flags)
  238. {
  239. struct sock *sk = sock->sk;
  240. int ret;
  241. ret = sock_create_lite(sk->sk_family, sk->sk_type,
  242. sk->sk_protocol, newsock);
  243. if (ret < 0)
  244. return ret;
  245. ret = tipc_accept(sock, *newsock, flags);
  246. if (ret < 0) {
  247. sock_release(*newsock);
  248. return ret;
  249. }
  250. (*newsock)->ops = sock->ops;
  251. return ret;
  252. }
  253. /**
  254. * tipc_release - destroy a TIPC socket
  255. * @sock: socket to destroy
  256. *
  257. * This routine cleans up any messages that are still queued on the socket.
  258. * For DGRAM and RDM socket types, all queued messages are rejected.
  259. * For SEQPACKET and STREAM socket types, the first message is rejected
  260. * and any others are discarded. (If the first message on a STREAM socket
  261. * is partially-read, it is discarded and the next one is rejected instead.)
  262. *
  263. * NOTE: Rejected messages are not necessarily returned to the sender! They
  264. * are returned or discarded according to the "destination droppable" setting
  265. * specified for the message by the sender.
  266. *
  267. * Returns 0 on success, errno otherwise
  268. */
  269. static int tipc_release(struct socket *sock)
  270. {
  271. struct sock *sk = sock->sk;
  272. struct tipc_sock *tsk;
  273. struct tipc_port *port;
  274. struct sk_buff *buf;
  275. /*
  276. * Exit if socket isn't fully initialized (occurs when a failed accept()
  277. * releases a pre-allocated child socket that was never used)
  278. */
  279. if (sk == NULL)
  280. return 0;
  281. tsk = tipc_sk(sk);
  282. port = &tsk->port;
  283. lock_sock(sk);
  284. /*
  285. * Reject all unreceived messages, except on an active connection
  286. * (which disconnects locally & sends a 'FIN+' to peer)
  287. */
  288. while (sock->state != SS_DISCONNECTING) {
  289. buf = __skb_dequeue(&sk->sk_receive_queue);
  290. if (buf == NULL)
  291. break;
  292. if (TIPC_SKB_CB(buf)->handle != NULL)
  293. kfree_skb(buf);
  294. else {
  295. if ((sock->state == SS_CONNECTING) ||
  296. (sock->state == SS_CONNECTED)) {
  297. sock->state = SS_DISCONNECTING;
  298. tipc_port_disconnect(port->ref);
  299. }
  300. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  301. }
  302. }
  303. /* Destroy TIPC port; also disconnects an active connection and
  304. * sends a 'FIN-' to peer.
  305. */
  306. tipc_port_destroy(port);
  307. /* Discard any remaining (connection-based) messages in receive queue */
  308. __skb_queue_purge(&sk->sk_receive_queue);
  309. /* Reject any messages that accumulated in backlog queue */
  310. sock->state = SS_DISCONNECTING;
  311. release_sock(sk);
  312. sock_put(sk);
  313. sock->sk = NULL;
  314. return 0;
  315. }
  316. /**
  317. * tipc_bind - associate or disassocate TIPC name(s) with a socket
  318. * @sock: socket structure
  319. * @uaddr: socket address describing name(s) and desired operation
  320. * @uaddr_len: size of socket address data structure
  321. *
  322. * Name and name sequence binding is indicated using a positive scope value;
  323. * a negative scope value unbinds the specified name. Specifying no name
  324. * (i.e. a socket address length of 0) unbinds all names from the socket.
  325. *
  326. * Returns 0 on success, errno otherwise
  327. *
  328. * NOTE: This routine doesn't need to take the socket lock since it doesn't
  329. * access any non-constant socket information.
  330. */
  331. static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
  332. int uaddr_len)
  333. {
  334. struct sock *sk = sock->sk;
  335. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  336. struct tipc_sock *tsk = tipc_sk(sk);
  337. int res = -EINVAL;
  338. lock_sock(sk);
  339. if (unlikely(!uaddr_len)) {
  340. res = tipc_withdraw(&tsk->port, 0, NULL);
  341. goto exit;
  342. }
  343. if (uaddr_len < sizeof(struct sockaddr_tipc)) {
  344. res = -EINVAL;
  345. goto exit;
  346. }
  347. if (addr->family != AF_TIPC) {
  348. res = -EAFNOSUPPORT;
  349. goto exit;
  350. }
  351. if (addr->addrtype == TIPC_ADDR_NAME)
  352. addr->addr.nameseq.upper = addr->addr.nameseq.lower;
  353. else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
  354. res = -EAFNOSUPPORT;
  355. goto exit;
  356. }
  357. if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
  358. (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
  359. (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
  360. res = -EACCES;
  361. goto exit;
  362. }
  363. res = (addr->scope > 0) ?
  364. tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
  365. tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
  366. exit:
  367. release_sock(sk);
  368. return res;
  369. }
  370. /**
  371. * tipc_getname - get port ID of socket or peer socket
  372. * @sock: socket structure
  373. * @uaddr: area for returned socket address
  374. * @uaddr_len: area for returned length of socket address
  375. * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
  376. *
  377. * Returns 0 on success, errno otherwise
  378. *
  379. * NOTE: This routine doesn't need to take the socket lock since it only
  380. * accesses socket information that is unchanging (or which changes in
  381. * a completely predictable manner).
  382. */
  383. static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
  384. int *uaddr_len, int peer)
  385. {
  386. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  387. struct tipc_sock *tsk = tipc_sk(sock->sk);
  388. memset(addr, 0, sizeof(*addr));
  389. if (peer) {
  390. if ((sock->state != SS_CONNECTED) &&
  391. ((peer != 2) || (sock->state != SS_DISCONNECTING)))
  392. return -ENOTCONN;
  393. addr->addr.id.ref = tipc_port_peerport(&tsk->port);
  394. addr->addr.id.node = tipc_port_peernode(&tsk->port);
  395. } else {
  396. addr->addr.id.ref = tsk->port.ref;
  397. addr->addr.id.node = tipc_own_addr;
  398. }
  399. *uaddr_len = sizeof(*addr);
  400. addr->addrtype = TIPC_ADDR_ID;
  401. addr->family = AF_TIPC;
  402. addr->scope = 0;
  403. addr->addr.name.domain = 0;
  404. return 0;
  405. }
  406. /**
  407. * tipc_poll - read and possibly block on pollmask
  408. * @file: file structure associated with the socket
  409. * @sock: socket for which to calculate the poll bits
  410. * @wait: ???
  411. *
  412. * Returns pollmask value
  413. *
  414. * COMMENTARY:
  415. * It appears that the usual socket locking mechanisms are not useful here
  416. * since the pollmask info is potentially out-of-date the moment this routine
  417. * exits. TCP and other protocols seem to rely on higher level poll routines
  418. * to handle any preventable race conditions, so TIPC will do the same ...
  419. *
  420. * TIPC sets the returned events as follows:
  421. *
  422. * socket state flags set
  423. * ------------ ---------
  424. * unconnected no read flags
  425. * POLLOUT if port is not congested
  426. *
  427. * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
  428. * no write flags
  429. *
  430. * connected POLLIN/POLLRDNORM if data in rx queue
  431. * POLLOUT if port is not congested
  432. *
  433. * disconnecting POLLIN/POLLRDNORM/POLLHUP
  434. * no write flags
  435. *
  436. * listening POLLIN if SYN in rx queue
  437. * no write flags
  438. *
  439. * ready POLLIN/POLLRDNORM if data in rx queue
  440. * [connectionless] POLLOUT (since port cannot be congested)
  441. *
  442. * IMPORTANT: The fact that a read or write operation is indicated does NOT
  443. * imply that the operation will succeed, merely that it should be performed
  444. * and will not block.
  445. */
  446. static unsigned int tipc_poll(struct file *file, struct socket *sock,
  447. poll_table *wait)
  448. {
  449. struct sock *sk = sock->sk;
  450. struct tipc_sock *tsk = tipc_sk(sk);
  451. u32 mask = 0;
  452. sock_poll_wait(file, sk_sleep(sk), wait);
  453. switch ((int)sock->state) {
  454. case SS_UNCONNECTED:
  455. if (!tsk->port.congested)
  456. mask |= POLLOUT;
  457. break;
  458. case SS_READY:
  459. case SS_CONNECTED:
  460. if (!tsk->port.congested)
  461. mask |= POLLOUT;
  462. /* fall thru' */
  463. case SS_CONNECTING:
  464. case SS_LISTENING:
  465. if (!skb_queue_empty(&sk->sk_receive_queue))
  466. mask |= (POLLIN | POLLRDNORM);
  467. break;
  468. case SS_DISCONNECTING:
  469. mask = (POLLIN | POLLRDNORM | POLLHUP);
  470. break;
  471. }
  472. return mask;
  473. }
  474. /**
  475. * dest_name_check - verify user is permitted to send to specified port name
  476. * @dest: destination address
  477. * @m: descriptor for message to be sent
  478. *
  479. * Prevents restricted configuration commands from being issued by
  480. * unauthorized users.
  481. *
  482. * Returns 0 if permission is granted, otherwise errno
  483. */
  484. static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
  485. {
  486. struct tipc_cfg_msg_hdr hdr;
  487. if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
  488. return 0;
  489. if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
  490. return 0;
  491. if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
  492. return -EACCES;
  493. if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
  494. return -EMSGSIZE;
  495. if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
  496. return -EFAULT;
  497. if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
  498. return -EACCES;
  499. return 0;
  500. }
  501. static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
  502. {
  503. struct sock *sk = sock->sk;
  504. struct tipc_sock *tsk = tipc_sk(sk);
  505. DEFINE_WAIT(wait);
  506. int done;
  507. do {
  508. int err = sock_error(sk);
  509. if (err)
  510. return err;
  511. if (sock->state == SS_DISCONNECTING)
  512. return -EPIPE;
  513. if (!*timeo_p)
  514. return -EAGAIN;
  515. if (signal_pending(current))
  516. return sock_intr_errno(*timeo_p);
  517. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  518. done = sk_wait_event(sk, timeo_p, !tsk->port.congested);
  519. finish_wait(sk_sleep(sk), &wait);
  520. } while (!done);
  521. return 0;
  522. }
  523. /**
  524. * tipc_sendmsg - send message in connectionless manner
  525. * @iocb: if NULL, indicates that socket lock is already held
  526. * @sock: socket structure
  527. * @m: message to send
  528. * @total_len: length of message
  529. *
  530. * Message must have an destination specified explicitly.
  531. * Used for SOCK_RDM and SOCK_DGRAM messages,
  532. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  533. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  534. *
  535. * Returns the number of bytes sent on success, or errno otherwise
  536. */
  537. static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
  538. struct msghdr *m, size_t total_len)
  539. {
  540. struct sock *sk = sock->sk;
  541. struct tipc_sock *tsk = tipc_sk(sk);
  542. struct tipc_port *port = &tsk->port;
  543. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  544. int needs_conn;
  545. long timeo;
  546. int res = -EINVAL;
  547. if (unlikely(!dest))
  548. return -EDESTADDRREQ;
  549. if (unlikely((m->msg_namelen < sizeof(*dest)) ||
  550. (dest->family != AF_TIPC)))
  551. return -EINVAL;
  552. if (total_len > TIPC_MAX_USER_MSG_SIZE)
  553. return -EMSGSIZE;
  554. if (iocb)
  555. lock_sock(sk);
  556. needs_conn = (sock->state != SS_READY);
  557. if (unlikely(needs_conn)) {
  558. if (sock->state == SS_LISTENING) {
  559. res = -EPIPE;
  560. goto exit;
  561. }
  562. if (sock->state != SS_UNCONNECTED) {
  563. res = -EISCONN;
  564. goto exit;
  565. }
  566. if (tsk->port.published) {
  567. res = -EOPNOTSUPP;
  568. goto exit;
  569. }
  570. if (dest->addrtype == TIPC_ADDR_NAME) {
  571. tsk->port.conn_type = dest->addr.name.name.type;
  572. tsk->port.conn_instance = dest->addr.name.name.instance;
  573. }
  574. /* Abort any pending connection attempts (very unlikely) */
  575. reject_rx_queue(sk);
  576. }
  577. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  578. do {
  579. if (dest->addrtype == TIPC_ADDR_NAME) {
  580. res = dest_name_check(dest, m);
  581. if (res)
  582. break;
  583. res = tipc_send2name(port,
  584. &dest->addr.name.name,
  585. dest->addr.name.domain,
  586. m->msg_iov,
  587. total_len);
  588. } else if (dest->addrtype == TIPC_ADDR_ID) {
  589. res = tipc_send2port(port,
  590. &dest->addr.id,
  591. m->msg_iov,
  592. total_len);
  593. } else if (dest->addrtype == TIPC_ADDR_MCAST) {
  594. if (needs_conn) {
  595. res = -EOPNOTSUPP;
  596. break;
  597. }
  598. res = dest_name_check(dest, m);
  599. if (res)
  600. break;
  601. res = tipc_port_mcast_xmit(port,
  602. &dest->addr.nameseq,
  603. m->msg_iov,
  604. total_len);
  605. }
  606. if (likely(res != -ELINKCONG)) {
  607. if (needs_conn && (res >= 0))
  608. sock->state = SS_CONNECTING;
  609. break;
  610. }
  611. res = tipc_wait_for_sndmsg(sock, &timeo);
  612. if (res)
  613. break;
  614. } while (1);
  615. exit:
  616. if (iocb)
  617. release_sock(sk);
  618. return res;
  619. }
  620. static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
  621. {
  622. struct sock *sk = sock->sk;
  623. struct tipc_sock *tsk = tipc_sk(sk);
  624. struct tipc_port *port = &tsk->port;
  625. DEFINE_WAIT(wait);
  626. int done;
  627. do {
  628. int err = sock_error(sk);
  629. if (err)
  630. return err;
  631. if (sock->state == SS_DISCONNECTING)
  632. return -EPIPE;
  633. else if (sock->state != SS_CONNECTED)
  634. return -ENOTCONN;
  635. if (!*timeo_p)
  636. return -EAGAIN;
  637. if (signal_pending(current))
  638. return sock_intr_errno(*timeo_p);
  639. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  640. done = sk_wait_event(sk, timeo_p,
  641. (!port->congested || !port->connected));
  642. finish_wait(sk_sleep(sk), &wait);
  643. } while (!done);
  644. return 0;
  645. }
  646. /**
  647. * tipc_send_packet - send a connection-oriented message
  648. * @iocb: if NULL, indicates that socket lock is already held
  649. * @sock: socket structure
  650. * @m: message to send
  651. * @total_len: length of message
  652. *
  653. * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
  654. *
  655. * Returns the number of bytes sent on success, or errno otherwise
  656. */
  657. static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
  658. struct msghdr *m, size_t total_len)
  659. {
  660. struct sock *sk = sock->sk;
  661. struct tipc_sock *tsk = tipc_sk(sk);
  662. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  663. int res = -EINVAL;
  664. long timeo;
  665. /* Handle implied connection establishment */
  666. if (unlikely(dest))
  667. return tipc_sendmsg(iocb, sock, m, total_len);
  668. if (total_len > TIPC_MAX_USER_MSG_SIZE)
  669. return -EMSGSIZE;
  670. if (iocb)
  671. lock_sock(sk);
  672. if (unlikely(sock->state != SS_CONNECTED)) {
  673. if (sock->state == SS_DISCONNECTING)
  674. res = -EPIPE;
  675. else
  676. res = -ENOTCONN;
  677. goto exit;
  678. }
  679. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  680. do {
  681. res = tipc_send(&tsk->port, m->msg_iov, total_len);
  682. if (likely(res != -ELINKCONG))
  683. break;
  684. res = tipc_wait_for_sndpkt(sock, &timeo);
  685. if (res)
  686. break;
  687. } while (1);
  688. exit:
  689. if (iocb)
  690. release_sock(sk);
  691. return res;
  692. }
  693. /**
  694. * tipc_send_stream - send stream-oriented data
  695. * @iocb: (unused)
  696. * @sock: socket structure
  697. * @m: data to send
  698. * @total_len: total length of data to be sent
  699. *
  700. * Used for SOCK_STREAM data.
  701. *
  702. * Returns the number of bytes sent on success (or partial success),
  703. * or errno if no data sent
  704. */
  705. static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
  706. struct msghdr *m, size_t total_len)
  707. {
  708. struct sock *sk = sock->sk;
  709. struct tipc_sock *tsk = tipc_sk(sk);
  710. struct msghdr my_msg;
  711. struct iovec my_iov;
  712. struct iovec *curr_iov;
  713. int curr_iovlen;
  714. char __user *curr_start;
  715. u32 hdr_size;
  716. int curr_left;
  717. int bytes_to_send;
  718. int bytes_sent;
  719. int res;
  720. lock_sock(sk);
  721. /* Handle special cases where there is no connection */
  722. if (unlikely(sock->state != SS_CONNECTED)) {
  723. if (sock->state == SS_UNCONNECTED)
  724. res = tipc_send_packet(NULL, sock, m, total_len);
  725. else
  726. res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
  727. goto exit;
  728. }
  729. if (unlikely(m->msg_name)) {
  730. res = -EISCONN;
  731. goto exit;
  732. }
  733. if (total_len > (unsigned int)INT_MAX) {
  734. res = -EMSGSIZE;
  735. goto exit;
  736. }
  737. /*
  738. * Send each iovec entry using one or more messages
  739. *
  740. * Note: This algorithm is good for the most likely case
  741. * (i.e. one large iovec entry), but could be improved to pass sets
  742. * of small iovec entries into send_packet().
  743. */
  744. curr_iov = m->msg_iov;
  745. curr_iovlen = m->msg_iovlen;
  746. my_msg.msg_iov = &my_iov;
  747. my_msg.msg_iovlen = 1;
  748. my_msg.msg_flags = m->msg_flags;
  749. my_msg.msg_name = NULL;
  750. bytes_sent = 0;
  751. hdr_size = msg_hdr_sz(&tsk->port.phdr);
  752. while (curr_iovlen--) {
  753. curr_start = curr_iov->iov_base;
  754. curr_left = curr_iov->iov_len;
  755. while (curr_left) {
  756. bytes_to_send = tsk->port.max_pkt - hdr_size;
  757. if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
  758. bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
  759. if (curr_left < bytes_to_send)
  760. bytes_to_send = curr_left;
  761. my_iov.iov_base = curr_start;
  762. my_iov.iov_len = bytes_to_send;
  763. res = tipc_send_packet(NULL, sock, &my_msg,
  764. bytes_to_send);
  765. if (res < 0) {
  766. if (bytes_sent)
  767. res = bytes_sent;
  768. goto exit;
  769. }
  770. curr_left -= bytes_to_send;
  771. curr_start += bytes_to_send;
  772. bytes_sent += bytes_to_send;
  773. }
  774. curr_iov++;
  775. }
  776. res = bytes_sent;
  777. exit:
  778. release_sock(sk);
  779. return res;
  780. }
  781. /**
  782. * auto_connect - complete connection setup to a remote port
  783. * @tsk: tipc socket structure
  784. * @msg: peer's response message
  785. *
  786. * Returns 0 on success, errno otherwise
  787. */
  788. static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
  789. {
  790. struct tipc_port *port = &tsk->port;
  791. struct socket *sock = tsk->sk.sk_socket;
  792. struct tipc_portid peer;
  793. peer.ref = msg_origport(msg);
  794. peer.node = msg_orignode(msg);
  795. __tipc_port_connect(port->ref, port, &peer);
  796. if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
  797. return -EINVAL;
  798. msg_set_importance(&port->phdr, (u32)msg_importance(msg));
  799. sock->state = SS_CONNECTED;
  800. return 0;
  801. }
  802. /**
  803. * set_orig_addr - capture sender's address for received message
  804. * @m: descriptor for message info
  805. * @msg: received message header
  806. *
  807. * Note: Address is not captured if not requested by receiver.
  808. */
  809. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  810. {
  811. DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
  812. if (addr) {
  813. addr->family = AF_TIPC;
  814. addr->addrtype = TIPC_ADDR_ID;
  815. memset(&addr->addr, 0, sizeof(addr->addr));
  816. addr->addr.id.ref = msg_origport(msg);
  817. addr->addr.id.node = msg_orignode(msg);
  818. addr->addr.name.domain = 0; /* could leave uninitialized */
  819. addr->scope = 0; /* could leave uninitialized */
  820. m->msg_namelen = sizeof(struct sockaddr_tipc);
  821. }
  822. }
  823. /**
  824. * anc_data_recv - optionally capture ancillary data for received message
  825. * @m: descriptor for message info
  826. * @msg: received message header
  827. * @tport: TIPC port associated with message
  828. *
  829. * Note: Ancillary data is not captured if not requested by receiver.
  830. *
  831. * Returns 0 if successful, otherwise errno
  832. */
  833. static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  834. struct tipc_port *tport)
  835. {
  836. u32 anc_data[3];
  837. u32 err;
  838. u32 dest_type;
  839. int has_name;
  840. int res;
  841. if (likely(m->msg_controllen == 0))
  842. return 0;
  843. /* Optionally capture errored message object(s) */
  844. err = msg ? msg_errcode(msg) : 0;
  845. if (unlikely(err)) {
  846. anc_data[0] = err;
  847. anc_data[1] = msg_data_sz(msg);
  848. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  849. if (res)
  850. return res;
  851. if (anc_data[1]) {
  852. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  853. msg_data(msg));
  854. if (res)
  855. return res;
  856. }
  857. }
  858. /* Optionally capture message destination object */
  859. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  860. switch (dest_type) {
  861. case TIPC_NAMED_MSG:
  862. has_name = 1;
  863. anc_data[0] = msg_nametype(msg);
  864. anc_data[1] = msg_namelower(msg);
  865. anc_data[2] = msg_namelower(msg);
  866. break;
  867. case TIPC_MCAST_MSG:
  868. has_name = 1;
  869. anc_data[0] = msg_nametype(msg);
  870. anc_data[1] = msg_namelower(msg);
  871. anc_data[2] = msg_nameupper(msg);
  872. break;
  873. case TIPC_CONN_MSG:
  874. has_name = (tport->conn_type != 0);
  875. anc_data[0] = tport->conn_type;
  876. anc_data[1] = tport->conn_instance;
  877. anc_data[2] = tport->conn_instance;
  878. break;
  879. default:
  880. has_name = 0;
  881. }
  882. if (has_name) {
  883. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  884. if (res)
  885. return res;
  886. }
  887. return 0;
  888. }
  889. static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
  890. {
  891. struct sock *sk = sock->sk;
  892. DEFINE_WAIT(wait);
  893. long timeo = *timeop;
  894. int err;
  895. for (;;) {
  896. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  897. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  898. if (sock->state == SS_DISCONNECTING) {
  899. err = -ENOTCONN;
  900. break;
  901. }
  902. release_sock(sk);
  903. timeo = schedule_timeout(timeo);
  904. lock_sock(sk);
  905. }
  906. err = 0;
  907. if (!skb_queue_empty(&sk->sk_receive_queue))
  908. break;
  909. err = sock_intr_errno(timeo);
  910. if (signal_pending(current))
  911. break;
  912. err = -EAGAIN;
  913. if (!timeo)
  914. break;
  915. }
  916. finish_wait(sk_sleep(sk), &wait);
  917. *timeop = timeo;
  918. return err;
  919. }
  920. /**
  921. * tipc_recvmsg - receive packet-oriented message
  922. * @iocb: (unused)
  923. * @m: descriptor for message info
  924. * @buf_len: total size of user buffer area
  925. * @flags: receive flags
  926. *
  927. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  928. * If the complete message doesn't fit in user area, truncate it.
  929. *
  930. * Returns size of returned message data, errno otherwise
  931. */
  932. static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
  933. struct msghdr *m, size_t buf_len, int flags)
  934. {
  935. struct sock *sk = sock->sk;
  936. struct tipc_sock *tsk = tipc_sk(sk);
  937. struct tipc_port *port = &tsk->port;
  938. struct sk_buff *buf;
  939. struct tipc_msg *msg;
  940. long timeo;
  941. unsigned int sz;
  942. u32 err;
  943. int res;
  944. /* Catch invalid receive requests */
  945. if (unlikely(!buf_len))
  946. return -EINVAL;
  947. lock_sock(sk);
  948. if (unlikely(sock->state == SS_UNCONNECTED)) {
  949. res = -ENOTCONN;
  950. goto exit;
  951. }
  952. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  953. restart:
  954. /* Look for a message in receive queue; wait if necessary */
  955. res = tipc_wait_for_rcvmsg(sock, &timeo);
  956. if (res)
  957. goto exit;
  958. /* Look at first message in receive queue */
  959. buf = skb_peek(&sk->sk_receive_queue);
  960. msg = buf_msg(buf);
  961. sz = msg_data_sz(msg);
  962. err = msg_errcode(msg);
  963. /* Discard an empty non-errored message & try again */
  964. if ((!sz) && (!err)) {
  965. advance_rx_queue(sk);
  966. goto restart;
  967. }
  968. /* Capture sender's address (optional) */
  969. set_orig_addr(m, msg);
  970. /* Capture ancillary data (optional) */
  971. res = anc_data_recv(m, msg, port);
  972. if (res)
  973. goto exit;
  974. /* Capture message data (if valid) & compute return value (always) */
  975. if (!err) {
  976. if (unlikely(buf_len < sz)) {
  977. sz = buf_len;
  978. m->msg_flags |= MSG_TRUNC;
  979. }
  980. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
  981. m->msg_iov, sz);
  982. if (res)
  983. goto exit;
  984. res = sz;
  985. } else {
  986. if ((sock->state == SS_READY) ||
  987. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  988. res = 0;
  989. else
  990. res = -ECONNRESET;
  991. }
  992. /* Consume received message (optional) */
  993. if (likely(!(flags & MSG_PEEK))) {
  994. if ((sock->state != SS_READY) &&
  995. (++port->conn_unacked >= TIPC_CONNACK_INTV))
  996. tipc_acknowledge(port->ref, port->conn_unacked);
  997. advance_rx_queue(sk);
  998. }
  999. exit:
  1000. release_sock(sk);
  1001. return res;
  1002. }
  1003. /**
  1004. * tipc_recv_stream - receive stream-oriented data
  1005. * @iocb: (unused)
  1006. * @m: descriptor for message info
  1007. * @buf_len: total size of user buffer area
  1008. * @flags: receive flags
  1009. *
  1010. * Used for SOCK_STREAM messages only. If not enough data is available
  1011. * will optionally wait for more; never truncates data.
  1012. *
  1013. * Returns size of returned message data, errno otherwise
  1014. */
  1015. static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
  1016. struct msghdr *m, size_t buf_len, int flags)
  1017. {
  1018. struct sock *sk = sock->sk;
  1019. struct tipc_sock *tsk = tipc_sk(sk);
  1020. struct tipc_port *port = &tsk->port;
  1021. struct sk_buff *buf;
  1022. struct tipc_msg *msg;
  1023. long timeo;
  1024. unsigned int sz;
  1025. int sz_to_copy, target, needed;
  1026. int sz_copied = 0;
  1027. u32 err;
  1028. int res = 0;
  1029. /* Catch invalid receive attempts */
  1030. if (unlikely(!buf_len))
  1031. return -EINVAL;
  1032. lock_sock(sk);
  1033. if (unlikely(sock->state == SS_UNCONNECTED)) {
  1034. res = -ENOTCONN;
  1035. goto exit;
  1036. }
  1037. target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
  1038. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1039. restart:
  1040. /* Look for a message in receive queue; wait if necessary */
  1041. res = tipc_wait_for_rcvmsg(sock, &timeo);
  1042. if (res)
  1043. goto exit;
  1044. /* Look at first message in receive queue */
  1045. buf = skb_peek(&sk->sk_receive_queue);
  1046. msg = buf_msg(buf);
  1047. sz = msg_data_sz(msg);
  1048. err = msg_errcode(msg);
  1049. /* Discard an empty non-errored message & try again */
  1050. if ((!sz) && (!err)) {
  1051. advance_rx_queue(sk);
  1052. goto restart;
  1053. }
  1054. /* Optionally capture sender's address & ancillary data of first msg */
  1055. if (sz_copied == 0) {
  1056. set_orig_addr(m, msg);
  1057. res = anc_data_recv(m, msg, port);
  1058. if (res)
  1059. goto exit;
  1060. }
  1061. /* Capture message data (if valid) & compute return value (always) */
  1062. if (!err) {
  1063. u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
  1064. sz -= offset;
  1065. needed = (buf_len - sz_copied);
  1066. sz_to_copy = (sz <= needed) ? sz : needed;
  1067. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
  1068. m->msg_iov, sz_to_copy);
  1069. if (res)
  1070. goto exit;
  1071. sz_copied += sz_to_copy;
  1072. if (sz_to_copy < sz) {
  1073. if (!(flags & MSG_PEEK))
  1074. TIPC_SKB_CB(buf)->handle =
  1075. (void *)(unsigned long)(offset + sz_to_copy);
  1076. goto exit;
  1077. }
  1078. } else {
  1079. if (sz_copied != 0)
  1080. goto exit; /* can't add error msg to valid data */
  1081. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  1082. res = 0;
  1083. else
  1084. res = -ECONNRESET;
  1085. }
  1086. /* Consume received message (optional) */
  1087. if (likely(!(flags & MSG_PEEK))) {
  1088. if (unlikely(++port->conn_unacked >= TIPC_CONNACK_INTV))
  1089. tipc_acknowledge(port->ref, port->conn_unacked);
  1090. advance_rx_queue(sk);
  1091. }
  1092. /* Loop around if more data is required */
  1093. if ((sz_copied < buf_len) && /* didn't get all requested data */
  1094. (!skb_queue_empty(&sk->sk_receive_queue) ||
  1095. (sz_copied < target)) && /* and more is ready or required */
  1096. (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
  1097. (!err)) /* and haven't reached a FIN */
  1098. goto restart;
  1099. exit:
  1100. release_sock(sk);
  1101. return sz_copied ? sz_copied : res;
  1102. }
  1103. /**
  1104. * tipc_write_space - wake up thread if port congestion is released
  1105. * @sk: socket
  1106. */
  1107. static void tipc_write_space(struct sock *sk)
  1108. {
  1109. struct socket_wq *wq;
  1110. rcu_read_lock();
  1111. wq = rcu_dereference(sk->sk_wq);
  1112. if (wq_has_sleeper(wq))
  1113. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1114. POLLWRNORM | POLLWRBAND);
  1115. rcu_read_unlock();
  1116. }
  1117. /**
  1118. * tipc_data_ready - wake up threads to indicate messages have been received
  1119. * @sk: socket
  1120. * @len: the length of messages
  1121. */
  1122. static void tipc_data_ready(struct sock *sk)
  1123. {
  1124. struct socket_wq *wq;
  1125. rcu_read_lock();
  1126. wq = rcu_dereference(sk->sk_wq);
  1127. if (wq_has_sleeper(wq))
  1128. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  1129. POLLRDNORM | POLLRDBAND);
  1130. rcu_read_unlock();
  1131. }
  1132. /**
  1133. * filter_connect - Handle all incoming messages for a connection-based socket
  1134. * @tsk: TIPC socket
  1135. * @msg: message
  1136. *
  1137. * Returns TIPC error status code and socket error status code
  1138. * once it encounters some errors
  1139. */
  1140. static u32 filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
  1141. {
  1142. struct sock *sk = &tsk->sk;
  1143. struct tipc_port *port = &tsk->port;
  1144. struct socket *sock = sk->sk_socket;
  1145. struct tipc_msg *msg = buf_msg(*buf);
  1146. u32 retval = TIPC_ERR_NO_PORT;
  1147. int res;
  1148. if (msg_mcast(msg))
  1149. return retval;
  1150. switch ((int)sock->state) {
  1151. case SS_CONNECTED:
  1152. /* Accept only connection-based messages sent by peer */
  1153. if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
  1154. if (unlikely(msg_errcode(msg))) {
  1155. sock->state = SS_DISCONNECTING;
  1156. __tipc_port_disconnect(port);
  1157. }
  1158. retval = TIPC_OK;
  1159. }
  1160. break;
  1161. case SS_CONNECTING:
  1162. /* Accept only ACK or NACK message */
  1163. if (unlikely(msg_errcode(msg))) {
  1164. sock->state = SS_DISCONNECTING;
  1165. sk->sk_err = ECONNREFUSED;
  1166. retval = TIPC_OK;
  1167. break;
  1168. }
  1169. if (unlikely(!msg_connected(msg)))
  1170. break;
  1171. res = auto_connect(tsk, msg);
  1172. if (res) {
  1173. sock->state = SS_DISCONNECTING;
  1174. sk->sk_err = -res;
  1175. retval = TIPC_OK;
  1176. break;
  1177. }
  1178. /* If an incoming message is an 'ACK-', it should be
  1179. * discarded here because it doesn't contain useful
  1180. * data. In addition, we should try to wake up
  1181. * connect() routine if sleeping.
  1182. */
  1183. if (msg_data_sz(msg) == 0) {
  1184. kfree_skb(*buf);
  1185. *buf = NULL;
  1186. if (waitqueue_active(sk_sleep(sk)))
  1187. wake_up_interruptible(sk_sleep(sk));
  1188. }
  1189. retval = TIPC_OK;
  1190. break;
  1191. case SS_LISTENING:
  1192. case SS_UNCONNECTED:
  1193. /* Accept only SYN message */
  1194. if (!msg_connected(msg) && !(msg_errcode(msg)))
  1195. retval = TIPC_OK;
  1196. break;
  1197. case SS_DISCONNECTING:
  1198. break;
  1199. default:
  1200. pr_err("Unknown socket state %u\n", sock->state);
  1201. }
  1202. return retval;
  1203. }
  1204. /**
  1205. * rcvbuf_limit - get proper overload limit of socket receive queue
  1206. * @sk: socket
  1207. * @buf: message
  1208. *
  1209. * For all connection oriented messages, irrespective of importance,
  1210. * the default overload value (i.e. 67MB) is set as limit.
  1211. *
  1212. * For all connectionless messages, by default new queue limits are
  1213. * as belows:
  1214. *
  1215. * TIPC_LOW_IMPORTANCE (4 MB)
  1216. * TIPC_MEDIUM_IMPORTANCE (8 MB)
  1217. * TIPC_HIGH_IMPORTANCE (16 MB)
  1218. * TIPC_CRITICAL_IMPORTANCE (32 MB)
  1219. *
  1220. * Returns overload limit according to corresponding message importance
  1221. */
  1222. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
  1223. {
  1224. struct tipc_msg *msg = buf_msg(buf);
  1225. if (msg_connected(msg))
  1226. return sysctl_tipc_rmem[2];
  1227. return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
  1228. msg_importance(msg);
  1229. }
  1230. /**
  1231. * filter_rcv - validate incoming message
  1232. * @sk: socket
  1233. * @buf: message
  1234. *
  1235. * Enqueues message on receive queue if acceptable; optionally handles
  1236. * disconnect indication for a connected socket.
  1237. *
  1238. * Called with socket lock already taken; port lock may also be taken.
  1239. *
  1240. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  1241. */
  1242. static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
  1243. {
  1244. struct socket *sock = sk->sk_socket;
  1245. struct tipc_sock *tsk = tipc_sk(sk);
  1246. struct tipc_msg *msg = buf_msg(buf);
  1247. unsigned int limit = rcvbuf_limit(sk, buf);
  1248. u32 res = TIPC_OK;
  1249. /* Reject message if it is wrong sort of message for socket */
  1250. if (msg_type(msg) > TIPC_DIRECT_MSG)
  1251. return TIPC_ERR_NO_PORT;
  1252. if (sock->state == SS_READY) {
  1253. if (msg_connected(msg))
  1254. return TIPC_ERR_NO_PORT;
  1255. } else {
  1256. res = filter_connect(tsk, &buf);
  1257. if (res != TIPC_OK || buf == NULL)
  1258. return res;
  1259. }
  1260. /* Reject message if there isn't room to queue it */
  1261. if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
  1262. return TIPC_ERR_OVERLOAD;
  1263. /* Enqueue message */
  1264. TIPC_SKB_CB(buf)->handle = NULL;
  1265. __skb_queue_tail(&sk->sk_receive_queue, buf);
  1266. skb_set_owner_r(buf, sk);
  1267. sk->sk_data_ready(sk);
  1268. return TIPC_OK;
  1269. }
  1270. /**
  1271. * tipc_backlog_rcv - handle incoming message from backlog queue
  1272. * @sk: socket
  1273. * @buf: message
  1274. *
  1275. * Caller must hold socket lock, but not port lock.
  1276. *
  1277. * Returns 0
  1278. */
  1279. static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
  1280. {
  1281. u32 res;
  1282. struct tipc_sock *tsk = tipc_sk(sk);
  1283. uint truesize = buf->truesize;
  1284. res = filter_rcv(sk, buf);
  1285. if (unlikely(res))
  1286. tipc_reject_msg(buf, res);
  1287. if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
  1288. atomic_add(truesize, &tsk->dupl_rcvcnt);
  1289. return 0;
  1290. }
  1291. /**
  1292. * tipc_sk_rcv - handle incoming message
  1293. * @buf: buffer containing arriving message
  1294. * Consumes buffer
  1295. * Returns 0 if success, or errno: -EHOSTUNREACH
  1296. */
  1297. int tipc_sk_rcv(struct sk_buff *buf)
  1298. {
  1299. struct tipc_sock *tsk;
  1300. struct tipc_port *port;
  1301. struct sock *sk;
  1302. u32 dport = msg_destport(buf_msg(buf));
  1303. int err = TIPC_OK;
  1304. uint limit;
  1305. /* Forward unresolved named message */
  1306. if (unlikely(!dport)) {
  1307. tipc_net_route_msg(buf);
  1308. return 0;
  1309. }
  1310. /* Validate destination */
  1311. port = tipc_port_lock(dport);
  1312. if (unlikely(!port)) {
  1313. err = TIPC_ERR_NO_PORT;
  1314. goto exit;
  1315. }
  1316. tsk = tipc_port_to_sock(port);
  1317. sk = &tsk->sk;
  1318. /* Queue message */
  1319. bh_lock_sock(sk);
  1320. if (!sock_owned_by_user(sk)) {
  1321. err = filter_rcv(sk, buf);
  1322. } else {
  1323. if (sk->sk_backlog.len == 0)
  1324. atomic_set(&tsk->dupl_rcvcnt, 0);
  1325. limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
  1326. if (sk_add_backlog(sk, buf, limit))
  1327. err = TIPC_ERR_OVERLOAD;
  1328. }
  1329. bh_unlock_sock(sk);
  1330. tipc_port_unlock(port);
  1331. if (likely(!err))
  1332. return 0;
  1333. exit:
  1334. tipc_reject_msg(buf, err);
  1335. return -EHOSTUNREACH;
  1336. }
  1337. static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
  1338. {
  1339. struct sock *sk = sock->sk;
  1340. DEFINE_WAIT(wait);
  1341. int done;
  1342. do {
  1343. int err = sock_error(sk);
  1344. if (err)
  1345. return err;
  1346. if (!*timeo_p)
  1347. return -ETIMEDOUT;
  1348. if (signal_pending(current))
  1349. return sock_intr_errno(*timeo_p);
  1350. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1351. done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
  1352. finish_wait(sk_sleep(sk), &wait);
  1353. } while (!done);
  1354. return 0;
  1355. }
  1356. /**
  1357. * tipc_connect - establish a connection to another TIPC port
  1358. * @sock: socket structure
  1359. * @dest: socket address for destination port
  1360. * @destlen: size of socket address data structure
  1361. * @flags: file-related flags associated with socket
  1362. *
  1363. * Returns 0 on success, errno otherwise
  1364. */
  1365. static int tipc_connect(struct socket *sock, struct sockaddr *dest,
  1366. int destlen, int flags)
  1367. {
  1368. struct sock *sk = sock->sk;
  1369. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1370. struct msghdr m = {NULL,};
  1371. long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
  1372. socket_state previous;
  1373. int res;
  1374. lock_sock(sk);
  1375. /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
  1376. if (sock->state == SS_READY) {
  1377. res = -EOPNOTSUPP;
  1378. goto exit;
  1379. }
  1380. /*
  1381. * Reject connection attempt using multicast address
  1382. *
  1383. * Note: send_msg() validates the rest of the address fields,
  1384. * so there's no need to do it here
  1385. */
  1386. if (dst->addrtype == TIPC_ADDR_MCAST) {
  1387. res = -EINVAL;
  1388. goto exit;
  1389. }
  1390. previous = sock->state;
  1391. switch (sock->state) {
  1392. case SS_UNCONNECTED:
  1393. /* Send a 'SYN-' to destination */
  1394. m.msg_name = dest;
  1395. m.msg_namelen = destlen;
  1396. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  1397. * indicate send_msg() is never blocked.
  1398. */
  1399. if (!timeout)
  1400. m.msg_flags = MSG_DONTWAIT;
  1401. res = tipc_sendmsg(NULL, sock, &m, 0);
  1402. if ((res < 0) && (res != -EWOULDBLOCK))
  1403. goto exit;
  1404. /* Just entered SS_CONNECTING state; the only
  1405. * difference is that return value in non-blocking
  1406. * case is EINPROGRESS, rather than EALREADY.
  1407. */
  1408. res = -EINPROGRESS;
  1409. case SS_CONNECTING:
  1410. if (previous == SS_CONNECTING)
  1411. res = -EALREADY;
  1412. if (!timeout)
  1413. goto exit;
  1414. timeout = msecs_to_jiffies(timeout);
  1415. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  1416. res = tipc_wait_for_connect(sock, &timeout);
  1417. break;
  1418. case SS_CONNECTED:
  1419. res = -EISCONN;
  1420. break;
  1421. default:
  1422. res = -EINVAL;
  1423. break;
  1424. }
  1425. exit:
  1426. release_sock(sk);
  1427. return res;
  1428. }
  1429. /**
  1430. * tipc_listen - allow socket to listen for incoming connections
  1431. * @sock: socket structure
  1432. * @len: (unused)
  1433. *
  1434. * Returns 0 on success, errno otherwise
  1435. */
  1436. static int tipc_listen(struct socket *sock, int len)
  1437. {
  1438. struct sock *sk = sock->sk;
  1439. int res;
  1440. lock_sock(sk);
  1441. if (sock->state != SS_UNCONNECTED)
  1442. res = -EINVAL;
  1443. else {
  1444. sock->state = SS_LISTENING;
  1445. res = 0;
  1446. }
  1447. release_sock(sk);
  1448. return res;
  1449. }
  1450. static int tipc_wait_for_accept(struct socket *sock, long timeo)
  1451. {
  1452. struct sock *sk = sock->sk;
  1453. DEFINE_WAIT(wait);
  1454. int err;
  1455. /* True wake-one mechanism for incoming connections: only
  1456. * one process gets woken up, not the 'whole herd'.
  1457. * Since we do not 'race & poll' for established sockets
  1458. * anymore, the common case will execute the loop only once.
  1459. */
  1460. for (;;) {
  1461. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  1462. TASK_INTERRUPTIBLE);
  1463. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1464. release_sock(sk);
  1465. timeo = schedule_timeout(timeo);
  1466. lock_sock(sk);
  1467. }
  1468. err = 0;
  1469. if (!skb_queue_empty(&sk->sk_receive_queue))
  1470. break;
  1471. err = -EINVAL;
  1472. if (sock->state != SS_LISTENING)
  1473. break;
  1474. err = sock_intr_errno(timeo);
  1475. if (signal_pending(current))
  1476. break;
  1477. err = -EAGAIN;
  1478. if (!timeo)
  1479. break;
  1480. }
  1481. finish_wait(sk_sleep(sk), &wait);
  1482. return err;
  1483. }
  1484. /**
  1485. * tipc_accept - wait for connection request
  1486. * @sock: listening socket
  1487. * @newsock: new socket that is to be connected
  1488. * @flags: file-related flags associated with socket
  1489. *
  1490. * Returns 0 on success, errno otherwise
  1491. */
  1492. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
  1493. {
  1494. struct sock *new_sk, *sk = sock->sk;
  1495. struct sk_buff *buf;
  1496. struct tipc_port *new_port;
  1497. struct tipc_msg *msg;
  1498. struct tipc_portid peer;
  1499. u32 new_ref;
  1500. long timeo;
  1501. int res;
  1502. lock_sock(sk);
  1503. if (sock->state != SS_LISTENING) {
  1504. res = -EINVAL;
  1505. goto exit;
  1506. }
  1507. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  1508. res = tipc_wait_for_accept(sock, timeo);
  1509. if (res)
  1510. goto exit;
  1511. buf = skb_peek(&sk->sk_receive_queue);
  1512. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
  1513. if (res)
  1514. goto exit;
  1515. new_sk = new_sock->sk;
  1516. new_port = &tipc_sk(new_sk)->port;
  1517. new_ref = new_port->ref;
  1518. msg = buf_msg(buf);
  1519. /* we lock on new_sk; but lockdep sees the lock on sk */
  1520. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  1521. /*
  1522. * Reject any stray messages received by new socket
  1523. * before the socket lock was taken (very, very unlikely)
  1524. */
  1525. reject_rx_queue(new_sk);
  1526. /* Connect new socket to it's peer */
  1527. peer.ref = msg_origport(msg);
  1528. peer.node = msg_orignode(msg);
  1529. tipc_port_connect(new_ref, &peer);
  1530. new_sock->state = SS_CONNECTED;
  1531. tipc_port_set_importance(new_port, msg_importance(msg));
  1532. if (msg_named(msg)) {
  1533. new_port->conn_type = msg_nametype(msg);
  1534. new_port->conn_instance = msg_nameinst(msg);
  1535. }
  1536. /*
  1537. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1538. * Respond to 'SYN+' by queuing it on new socket.
  1539. */
  1540. if (!msg_data_sz(msg)) {
  1541. struct msghdr m = {NULL,};
  1542. advance_rx_queue(sk);
  1543. tipc_send_packet(NULL, new_sock, &m, 0);
  1544. } else {
  1545. __skb_dequeue(&sk->sk_receive_queue);
  1546. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  1547. skb_set_owner_r(buf, new_sk);
  1548. }
  1549. release_sock(new_sk);
  1550. exit:
  1551. release_sock(sk);
  1552. return res;
  1553. }
  1554. /**
  1555. * tipc_shutdown - shutdown socket connection
  1556. * @sock: socket structure
  1557. * @how: direction to close (must be SHUT_RDWR)
  1558. *
  1559. * Terminates connection (if necessary), then purges socket's receive queue.
  1560. *
  1561. * Returns 0 on success, errno otherwise
  1562. */
  1563. static int tipc_shutdown(struct socket *sock, int how)
  1564. {
  1565. struct sock *sk = sock->sk;
  1566. struct tipc_sock *tsk = tipc_sk(sk);
  1567. struct tipc_port *port = &tsk->port;
  1568. struct sk_buff *buf;
  1569. int res;
  1570. if (how != SHUT_RDWR)
  1571. return -EINVAL;
  1572. lock_sock(sk);
  1573. switch (sock->state) {
  1574. case SS_CONNECTING:
  1575. case SS_CONNECTED:
  1576. restart:
  1577. /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
  1578. buf = __skb_dequeue(&sk->sk_receive_queue);
  1579. if (buf) {
  1580. if (TIPC_SKB_CB(buf)->handle != NULL) {
  1581. kfree_skb(buf);
  1582. goto restart;
  1583. }
  1584. tipc_port_disconnect(port->ref);
  1585. tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
  1586. } else {
  1587. tipc_port_shutdown(port->ref);
  1588. }
  1589. sock->state = SS_DISCONNECTING;
  1590. /* fall through */
  1591. case SS_DISCONNECTING:
  1592. /* Discard any unreceived messages */
  1593. __skb_queue_purge(&sk->sk_receive_queue);
  1594. /* Wake up anyone sleeping in poll */
  1595. sk->sk_state_change(sk);
  1596. res = 0;
  1597. break;
  1598. default:
  1599. res = -ENOTCONN;
  1600. }
  1601. release_sock(sk);
  1602. return res;
  1603. }
  1604. /**
  1605. * tipc_setsockopt - set socket option
  1606. * @sock: socket structure
  1607. * @lvl: option level
  1608. * @opt: option identifier
  1609. * @ov: pointer to new option value
  1610. * @ol: length of option value
  1611. *
  1612. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  1613. * (to ease compatibility).
  1614. *
  1615. * Returns 0 on success, errno otherwise
  1616. */
  1617. static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
  1618. char __user *ov, unsigned int ol)
  1619. {
  1620. struct sock *sk = sock->sk;
  1621. struct tipc_sock *tsk = tipc_sk(sk);
  1622. struct tipc_port *port = &tsk->port;
  1623. u32 value;
  1624. int res;
  1625. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1626. return 0;
  1627. if (lvl != SOL_TIPC)
  1628. return -ENOPROTOOPT;
  1629. if (ol < sizeof(value))
  1630. return -EINVAL;
  1631. res = get_user(value, (u32 __user *)ov);
  1632. if (res)
  1633. return res;
  1634. lock_sock(sk);
  1635. switch (opt) {
  1636. case TIPC_IMPORTANCE:
  1637. tipc_port_set_importance(port, value);
  1638. break;
  1639. case TIPC_SRC_DROPPABLE:
  1640. if (sock->type != SOCK_STREAM)
  1641. tipc_port_set_unreliable(port, value);
  1642. else
  1643. res = -ENOPROTOOPT;
  1644. break;
  1645. case TIPC_DEST_DROPPABLE:
  1646. tipc_port_set_unreturnable(port, value);
  1647. break;
  1648. case TIPC_CONN_TIMEOUT:
  1649. tipc_sk(sk)->conn_timeout = value;
  1650. /* no need to set "res", since already 0 at this point */
  1651. break;
  1652. default:
  1653. res = -EINVAL;
  1654. }
  1655. release_sock(sk);
  1656. return res;
  1657. }
  1658. /**
  1659. * tipc_getsockopt - get socket option
  1660. * @sock: socket structure
  1661. * @lvl: option level
  1662. * @opt: option identifier
  1663. * @ov: receptacle for option value
  1664. * @ol: receptacle for length of option value
  1665. *
  1666. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  1667. * (to ease compatibility).
  1668. *
  1669. * Returns 0 on success, errno otherwise
  1670. */
  1671. static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
  1672. char __user *ov, int __user *ol)
  1673. {
  1674. struct sock *sk = sock->sk;
  1675. struct tipc_sock *tsk = tipc_sk(sk);
  1676. struct tipc_port *port = &tsk->port;
  1677. int len;
  1678. u32 value;
  1679. int res;
  1680. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1681. return put_user(0, ol);
  1682. if (lvl != SOL_TIPC)
  1683. return -ENOPROTOOPT;
  1684. res = get_user(len, ol);
  1685. if (res)
  1686. return res;
  1687. lock_sock(sk);
  1688. switch (opt) {
  1689. case TIPC_IMPORTANCE:
  1690. value = tipc_port_importance(port);
  1691. break;
  1692. case TIPC_SRC_DROPPABLE:
  1693. value = tipc_port_unreliable(port);
  1694. break;
  1695. case TIPC_DEST_DROPPABLE:
  1696. value = tipc_port_unreturnable(port);
  1697. break;
  1698. case TIPC_CONN_TIMEOUT:
  1699. value = tipc_sk(sk)->conn_timeout;
  1700. /* no need to set "res", since already 0 at this point */
  1701. break;
  1702. case TIPC_NODE_RECVQ_DEPTH:
  1703. value = 0; /* was tipc_queue_size, now obsolete */
  1704. break;
  1705. case TIPC_SOCK_RECVQ_DEPTH:
  1706. value = skb_queue_len(&sk->sk_receive_queue);
  1707. break;
  1708. default:
  1709. res = -EINVAL;
  1710. }
  1711. release_sock(sk);
  1712. if (res)
  1713. return res; /* "get" failed */
  1714. if (len < sizeof(value))
  1715. return -EINVAL;
  1716. if (copy_to_user(ov, &value, sizeof(value)))
  1717. return -EFAULT;
  1718. return put_user(sizeof(value), ol);
  1719. }
  1720. int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
  1721. {
  1722. struct tipc_sioc_ln_req lnr;
  1723. void __user *argp = (void __user *)arg;
  1724. switch (cmd) {
  1725. case SIOCGETLINKNAME:
  1726. if (copy_from_user(&lnr, argp, sizeof(lnr)))
  1727. return -EFAULT;
  1728. if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
  1729. lnr.linkname, TIPC_MAX_LINK_NAME)) {
  1730. if (copy_to_user(argp, &lnr, sizeof(lnr)))
  1731. return -EFAULT;
  1732. return 0;
  1733. }
  1734. return -EADDRNOTAVAIL;
  1735. break;
  1736. default:
  1737. return -ENOIOCTLCMD;
  1738. }
  1739. }
  1740. /* Protocol switches for the various types of TIPC sockets */
  1741. static const struct proto_ops msg_ops = {
  1742. .owner = THIS_MODULE,
  1743. .family = AF_TIPC,
  1744. .release = tipc_release,
  1745. .bind = tipc_bind,
  1746. .connect = tipc_connect,
  1747. .socketpair = sock_no_socketpair,
  1748. .accept = sock_no_accept,
  1749. .getname = tipc_getname,
  1750. .poll = tipc_poll,
  1751. .ioctl = tipc_ioctl,
  1752. .listen = sock_no_listen,
  1753. .shutdown = tipc_shutdown,
  1754. .setsockopt = tipc_setsockopt,
  1755. .getsockopt = tipc_getsockopt,
  1756. .sendmsg = tipc_sendmsg,
  1757. .recvmsg = tipc_recvmsg,
  1758. .mmap = sock_no_mmap,
  1759. .sendpage = sock_no_sendpage
  1760. };
  1761. static const struct proto_ops packet_ops = {
  1762. .owner = THIS_MODULE,
  1763. .family = AF_TIPC,
  1764. .release = tipc_release,
  1765. .bind = tipc_bind,
  1766. .connect = tipc_connect,
  1767. .socketpair = sock_no_socketpair,
  1768. .accept = tipc_accept,
  1769. .getname = tipc_getname,
  1770. .poll = tipc_poll,
  1771. .ioctl = tipc_ioctl,
  1772. .listen = tipc_listen,
  1773. .shutdown = tipc_shutdown,
  1774. .setsockopt = tipc_setsockopt,
  1775. .getsockopt = tipc_getsockopt,
  1776. .sendmsg = tipc_send_packet,
  1777. .recvmsg = tipc_recvmsg,
  1778. .mmap = sock_no_mmap,
  1779. .sendpage = sock_no_sendpage
  1780. };
  1781. static const struct proto_ops stream_ops = {
  1782. .owner = THIS_MODULE,
  1783. .family = AF_TIPC,
  1784. .release = tipc_release,
  1785. .bind = tipc_bind,
  1786. .connect = tipc_connect,
  1787. .socketpair = sock_no_socketpair,
  1788. .accept = tipc_accept,
  1789. .getname = tipc_getname,
  1790. .poll = tipc_poll,
  1791. .ioctl = tipc_ioctl,
  1792. .listen = tipc_listen,
  1793. .shutdown = tipc_shutdown,
  1794. .setsockopt = tipc_setsockopt,
  1795. .getsockopt = tipc_getsockopt,
  1796. .sendmsg = tipc_send_stream,
  1797. .recvmsg = tipc_recv_stream,
  1798. .mmap = sock_no_mmap,
  1799. .sendpage = sock_no_sendpage
  1800. };
  1801. static const struct net_proto_family tipc_family_ops = {
  1802. .owner = THIS_MODULE,
  1803. .family = AF_TIPC,
  1804. .create = tipc_sk_create
  1805. };
  1806. static struct proto tipc_proto = {
  1807. .name = "TIPC",
  1808. .owner = THIS_MODULE,
  1809. .obj_size = sizeof(struct tipc_sock),
  1810. .sysctl_rmem = sysctl_tipc_rmem
  1811. };
  1812. static struct proto tipc_proto_kern = {
  1813. .name = "TIPC",
  1814. .obj_size = sizeof(struct tipc_sock),
  1815. .sysctl_rmem = sysctl_tipc_rmem
  1816. };
  1817. /**
  1818. * tipc_socket_init - initialize TIPC socket interface
  1819. *
  1820. * Returns 0 on success, errno otherwise
  1821. */
  1822. int tipc_socket_init(void)
  1823. {
  1824. int res;
  1825. res = proto_register(&tipc_proto, 1);
  1826. if (res) {
  1827. pr_err("Failed to register TIPC protocol type\n");
  1828. goto out;
  1829. }
  1830. res = sock_register(&tipc_family_ops);
  1831. if (res) {
  1832. pr_err("Failed to register TIPC socket type\n");
  1833. proto_unregister(&tipc_proto);
  1834. goto out;
  1835. }
  1836. out:
  1837. return res;
  1838. }
  1839. /**
  1840. * tipc_socket_stop - stop TIPC socket interface
  1841. */
  1842. void tipc_socket_stop(void)
  1843. {
  1844. sock_unregister(tipc_family_ops.family);
  1845. proto_unregister(&tipc_proto);
  1846. }