socket.c 49 KB

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