socket.c 51 KB

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