socket.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  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. if (sock->state == SS_UNCONNECTED)
  675. res = send_packet(NULL, sock, m, total_len);
  676. else
  677. res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
  678. goto exit;
  679. }
  680. if (unlikely(m->msg_name)) {
  681. res = -EISCONN;
  682. goto exit;
  683. }
  684. if (total_len > (unsigned int)INT_MAX) {
  685. res = -EMSGSIZE;
  686. goto exit;
  687. }
  688. /*
  689. * Send each iovec entry using one or more messages
  690. *
  691. * Note: This algorithm is good for the most likely case
  692. * (i.e. one large iovec entry), but could be improved to pass sets
  693. * of small iovec entries into send_packet().
  694. */
  695. curr_iov = m->msg_iov;
  696. curr_iovlen = m->msg_iovlen;
  697. my_msg.msg_iov = &my_iov;
  698. my_msg.msg_iovlen = 1;
  699. my_msg.msg_flags = m->msg_flags;
  700. my_msg.msg_name = NULL;
  701. bytes_sent = 0;
  702. hdr_size = msg_hdr_sz(&tport->phdr);
  703. while (curr_iovlen--) {
  704. curr_start = curr_iov->iov_base;
  705. curr_left = curr_iov->iov_len;
  706. while (curr_left) {
  707. bytes_to_send = tport->max_pkt - hdr_size;
  708. if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
  709. bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
  710. if (curr_left < bytes_to_send)
  711. bytes_to_send = curr_left;
  712. my_iov.iov_base = curr_start;
  713. my_iov.iov_len = bytes_to_send;
  714. res = send_packet(NULL, sock, &my_msg, bytes_to_send);
  715. if (res < 0) {
  716. if (bytes_sent)
  717. res = bytes_sent;
  718. goto exit;
  719. }
  720. curr_left -= bytes_to_send;
  721. curr_start += bytes_to_send;
  722. bytes_sent += bytes_to_send;
  723. }
  724. curr_iov++;
  725. }
  726. res = bytes_sent;
  727. exit:
  728. release_sock(sk);
  729. return res;
  730. }
  731. /**
  732. * auto_connect - complete connection setup to a remote port
  733. * @sock: socket structure
  734. * @msg: peer's response message
  735. *
  736. * Returns 0 on success, errno otherwise
  737. */
  738. static int auto_connect(struct socket *sock, struct tipc_msg *msg)
  739. {
  740. struct tipc_sock *tsock = tipc_sk(sock->sk);
  741. struct tipc_port *p_ptr;
  742. tsock->peer_name.ref = msg_origport(msg);
  743. tsock->peer_name.node = msg_orignode(msg);
  744. p_ptr = tipc_port_deref(tsock->p->ref);
  745. if (!p_ptr)
  746. return -EINVAL;
  747. __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
  748. if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
  749. return -EINVAL;
  750. msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
  751. sock->state = SS_CONNECTED;
  752. return 0;
  753. }
  754. /**
  755. * set_orig_addr - capture sender's address for received message
  756. * @m: descriptor for message info
  757. * @msg: received message header
  758. *
  759. * Note: Address is not captured if not requested by receiver.
  760. */
  761. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  762. {
  763. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
  764. if (addr) {
  765. addr->family = AF_TIPC;
  766. addr->addrtype = TIPC_ADDR_ID;
  767. memset(&addr->addr, 0, sizeof(addr->addr));
  768. addr->addr.id.ref = msg_origport(msg);
  769. addr->addr.id.node = msg_orignode(msg);
  770. addr->addr.name.domain = 0; /* could leave uninitialized */
  771. addr->scope = 0; /* could leave uninitialized */
  772. m->msg_namelen = sizeof(struct sockaddr_tipc);
  773. }
  774. }
  775. /**
  776. * anc_data_recv - optionally capture ancillary data for received message
  777. * @m: descriptor for message info
  778. * @msg: received message header
  779. * @tport: TIPC port associated with message
  780. *
  781. * Note: Ancillary data is not captured if not requested by receiver.
  782. *
  783. * Returns 0 if successful, otherwise errno
  784. */
  785. static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  786. struct tipc_port *tport)
  787. {
  788. u32 anc_data[3];
  789. u32 err;
  790. u32 dest_type;
  791. int has_name;
  792. int res;
  793. if (likely(m->msg_controllen == 0))
  794. return 0;
  795. /* Optionally capture errored message object(s) */
  796. err = msg ? msg_errcode(msg) : 0;
  797. if (unlikely(err)) {
  798. anc_data[0] = err;
  799. anc_data[1] = msg_data_sz(msg);
  800. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  801. if (res)
  802. return res;
  803. if (anc_data[1]) {
  804. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  805. msg_data(msg));
  806. if (res)
  807. return res;
  808. }
  809. }
  810. /* Optionally capture message destination object */
  811. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  812. switch (dest_type) {
  813. case TIPC_NAMED_MSG:
  814. has_name = 1;
  815. anc_data[0] = msg_nametype(msg);
  816. anc_data[1] = msg_namelower(msg);
  817. anc_data[2] = msg_namelower(msg);
  818. break;
  819. case TIPC_MCAST_MSG:
  820. has_name = 1;
  821. anc_data[0] = msg_nametype(msg);
  822. anc_data[1] = msg_namelower(msg);
  823. anc_data[2] = msg_nameupper(msg);
  824. break;
  825. case TIPC_CONN_MSG:
  826. has_name = (tport->conn_type != 0);
  827. anc_data[0] = tport->conn_type;
  828. anc_data[1] = tport->conn_instance;
  829. anc_data[2] = tport->conn_instance;
  830. break;
  831. default:
  832. has_name = 0;
  833. }
  834. if (has_name) {
  835. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  836. if (res)
  837. return res;
  838. }
  839. return 0;
  840. }
  841. /**
  842. * recv_msg - receive packet-oriented message
  843. * @iocb: (unused)
  844. * @m: descriptor for message info
  845. * @buf_len: total size of user buffer area
  846. * @flags: receive flags
  847. *
  848. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  849. * If the complete message doesn't fit in user area, truncate it.
  850. *
  851. * Returns size of returned message data, errno otherwise
  852. */
  853. static int recv_msg(struct kiocb *iocb, struct socket *sock,
  854. struct msghdr *m, size_t buf_len, int flags)
  855. {
  856. struct sock *sk = sock->sk;
  857. struct tipc_port *tport = tipc_sk_port(sk);
  858. struct sk_buff *buf;
  859. struct tipc_msg *msg;
  860. long timeout;
  861. unsigned int sz;
  862. u32 err;
  863. int res;
  864. /* Catch invalid receive requests */
  865. if (unlikely(!buf_len))
  866. return -EINVAL;
  867. lock_sock(sk);
  868. if (unlikely(sock->state == SS_UNCONNECTED)) {
  869. res = -ENOTCONN;
  870. goto exit;
  871. }
  872. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  873. restart:
  874. /* Look for a message in receive queue; wait if necessary */
  875. while (skb_queue_empty(&sk->sk_receive_queue)) {
  876. if (sock->state == SS_DISCONNECTING) {
  877. res = -ENOTCONN;
  878. goto exit;
  879. }
  880. if (timeout <= 0L) {
  881. res = timeout ? timeout : -EWOULDBLOCK;
  882. goto exit;
  883. }
  884. release_sock(sk);
  885. timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
  886. tipc_rx_ready(sock),
  887. timeout);
  888. lock_sock(sk);
  889. }
  890. /* Look at first message in receive queue */
  891. buf = skb_peek(&sk->sk_receive_queue);
  892. msg = buf_msg(buf);
  893. sz = msg_data_sz(msg);
  894. err = msg_errcode(msg);
  895. /* Discard an empty non-errored message & try again */
  896. if ((!sz) && (!err)) {
  897. advance_rx_queue(sk);
  898. goto restart;
  899. }
  900. /* Capture sender's address (optional) */
  901. set_orig_addr(m, msg);
  902. /* Capture ancillary data (optional) */
  903. res = anc_data_recv(m, msg, tport);
  904. if (res)
  905. goto exit;
  906. /* Capture message data (if valid) & compute return value (always) */
  907. if (!err) {
  908. if (unlikely(buf_len < sz)) {
  909. sz = buf_len;
  910. m->msg_flags |= MSG_TRUNC;
  911. }
  912. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
  913. m->msg_iov, sz);
  914. if (res)
  915. goto exit;
  916. res = sz;
  917. } else {
  918. if ((sock->state == SS_READY) ||
  919. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  920. res = 0;
  921. else
  922. res = -ECONNRESET;
  923. }
  924. /* Consume received message (optional) */
  925. if (likely(!(flags & MSG_PEEK))) {
  926. if ((sock->state != SS_READY) &&
  927. (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  928. tipc_acknowledge(tport->ref, tport->conn_unacked);
  929. advance_rx_queue(sk);
  930. }
  931. exit:
  932. release_sock(sk);
  933. return res;
  934. }
  935. /**
  936. * recv_stream - receive stream-oriented data
  937. * @iocb: (unused)
  938. * @m: descriptor for message info
  939. * @buf_len: total size of user buffer area
  940. * @flags: receive flags
  941. *
  942. * Used for SOCK_STREAM messages only. If not enough data is available
  943. * will optionally wait for more; never truncates data.
  944. *
  945. * Returns size of returned message data, errno otherwise
  946. */
  947. static int recv_stream(struct kiocb *iocb, struct socket *sock,
  948. struct msghdr *m, size_t buf_len, int flags)
  949. {
  950. struct sock *sk = sock->sk;
  951. struct tipc_port *tport = tipc_sk_port(sk);
  952. struct sk_buff *buf;
  953. struct tipc_msg *msg;
  954. long timeout;
  955. unsigned int sz;
  956. int sz_to_copy, target, needed;
  957. int sz_copied = 0;
  958. u32 err;
  959. int res = 0;
  960. /* Catch invalid receive attempts */
  961. if (unlikely(!buf_len))
  962. return -EINVAL;
  963. lock_sock(sk);
  964. if (unlikely((sock->state == SS_UNCONNECTED))) {
  965. res = -ENOTCONN;
  966. goto exit;
  967. }
  968. target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
  969. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  970. restart:
  971. /* Look for a message in receive queue; wait if necessary */
  972. while (skb_queue_empty(&sk->sk_receive_queue)) {
  973. if (sock->state == SS_DISCONNECTING) {
  974. res = -ENOTCONN;
  975. goto exit;
  976. }
  977. if (timeout <= 0L) {
  978. res = timeout ? timeout : -EWOULDBLOCK;
  979. goto exit;
  980. }
  981. release_sock(sk);
  982. timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
  983. tipc_rx_ready(sock),
  984. timeout);
  985. lock_sock(sk);
  986. }
  987. /* Look at first message in receive queue */
  988. buf = skb_peek(&sk->sk_receive_queue);
  989. msg = buf_msg(buf);
  990. sz = msg_data_sz(msg);
  991. err = msg_errcode(msg);
  992. /* Discard an empty non-errored message & try again */
  993. if ((!sz) && (!err)) {
  994. advance_rx_queue(sk);
  995. goto restart;
  996. }
  997. /* Optionally capture sender's address & ancillary data of first msg */
  998. if (sz_copied == 0) {
  999. set_orig_addr(m, msg);
  1000. res = anc_data_recv(m, msg, tport);
  1001. if (res)
  1002. goto exit;
  1003. }
  1004. /* Capture message data (if valid) & compute return value (always) */
  1005. if (!err) {
  1006. u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
  1007. sz -= offset;
  1008. needed = (buf_len - sz_copied);
  1009. sz_to_copy = (sz <= needed) ? sz : needed;
  1010. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
  1011. m->msg_iov, sz_to_copy);
  1012. if (res)
  1013. goto exit;
  1014. sz_copied += sz_to_copy;
  1015. if (sz_to_copy < sz) {
  1016. if (!(flags & MSG_PEEK))
  1017. TIPC_SKB_CB(buf)->handle =
  1018. (void *)(unsigned long)(offset + sz_to_copy);
  1019. goto exit;
  1020. }
  1021. } else {
  1022. if (sz_copied != 0)
  1023. goto exit; /* can't add error msg to valid data */
  1024. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  1025. res = 0;
  1026. else
  1027. res = -ECONNRESET;
  1028. }
  1029. /* Consume received message (optional) */
  1030. if (likely(!(flags & MSG_PEEK))) {
  1031. if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  1032. tipc_acknowledge(tport->ref, tport->conn_unacked);
  1033. advance_rx_queue(sk);
  1034. }
  1035. /* Loop around if more data is required */
  1036. if ((sz_copied < buf_len) && /* didn't get all requested data */
  1037. (!skb_queue_empty(&sk->sk_receive_queue) ||
  1038. (sz_copied < target)) && /* and more is ready or required */
  1039. (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
  1040. (!err)) /* and haven't reached a FIN */
  1041. goto restart;
  1042. exit:
  1043. release_sock(sk);
  1044. return sz_copied ? sz_copied : res;
  1045. }
  1046. /**
  1047. * tipc_write_space - wake up thread if port congestion is released
  1048. * @sk: socket
  1049. */
  1050. static void tipc_write_space(struct sock *sk)
  1051. {
  1052. struct socket_wq *wq;
  1053. rcu_read_lock();
  1054. wq = rcu_dereference(sk->sk_wq);
  1055. if (wq_has_sleeper(wq))
  1056. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1057. POLLWRNORM | POLLWRBAND);
  1058. rcu_read_unlock();
  1059. }
  1060. /**
  1061. * tipc_data_ready - wake up threads to indicate messages have been received
  1062. * @sk: socket
  1063. * @len: the length of messages
  1064. */
  1065. static void tipc_data_ready(struct sock *sk, int len)
  1066. {
  1067. struct socket_wq *wq;
  1068. rcu_read_lock();
  1069. wq = rcu_dereference(sk->sk_wq);
  1070. if (wq_has_sleeper(wq))
  1071. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  1072. POLLRDNORM | POLLRDBAND);
  1073. rcu_read_unlock();
  1074. }
  1075. /**
  1076. * filter_connect - Handle all incoming messages for a connection-based socket
  1077. * @tsock: TIPC socket
  1078. * @msg: message
  1079. *
  1080. * Returns TIPC error status code and socket error status code
  1081. * once it encounters some errors
  1082. */
  1083. static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
  1084. {
  1085. struct socket *sock = tsock->sk.sk_socket;
  1086. struct tipc_msg *msg = buf_msg(*buf);
  1087. struct sock *sk = &tsock->sk;
  1088. u32 retval = TIPC_ERR_NO_PORT;
  1089. int res;
  1090. if (msg_mcast(msg))
  1091. return retval;
  1092. switch ((int)sock->state) {
  1093. case SS_CONNECTED:
  1094. /* Accept only connection-based messages sent by peer */
  1095. if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
  1096. if (unlikely(msg_errcode(msg))) {
  1097. sock->state = SS_DISCONNECTING;
  1098. __tipc_disconnect(tsock->p);
  1099. }
  1100. retval = TIPC_OK;
  1101. }
  1102. break;
  1103. case SS_CONNECTING:
  1104. /* Accept only ACK or NACK message */
  1105. if (unlikely(msg_errcode(msg))) {
  1106. sock->state = SS_DISCONNECTING;
  1107. sk->sk_err = ECONNREFUSED;
  1108. retval = TIPC_OK;
  1109. break;
  1110. }
  1111. if (unlikely(!msg_connected(msg)))
  1112. break;
  1113. res = auto_connect(sock, msg);
  1114. if (res) {
  1115. sock->state = SS_DISCONNECTING;
  1116. sk->sk_err = -res;
  1117. retval = TIPC_OK;
  1118. break;
  1119. }
  1120. /* If an incoming message is an 'ACK-', it should be
  1121. * discarded here because it doesn't contain useful
  1122. * data. In addition, we should try to wake up
  1123. * connect() routine if sleeping.
  1124. */
  1125. if (msg_data_sz(msg) == 0) {
  1126. kfree_skb(*buf);
  1127. *buf = NULL;
  1128. if (waitqueue_active(sk_sleep(sk)))
  1129. wake_up_interruptible(sk_sleep(sk));
  1130. }
  1131. retval = TIPC_OK;
  1132. break;
  1133. case SS_LISTENING:
  1134. case SS_UNCONNECTED:
  1135. /* Accept only SYN message */
  1136. if (!msg_connected(msg) && !(msg_errcode(msg)))
  1137. retval = TIPC_OK;
  1138. break;
  1139. case SS_DISCONNECTING:
  1140. break;
  1141. default:
  1142. pr_err("Unknown socket state %u\n", sock->state);
  1143. }
  1144. return retval;
  1145. }
  1146. /**
  1147. * rcvbuf_limit - get proper overload limit of socket receive queue
  1148. * @sk: socket
  1149. * @buf: message
  1150. *
  1151. * For all connection oriented messages, irrespective of importance,
  1152. * the default overload value (i.e. 67MB) is set as limit.
  1153. *
  1154. * For all connectionless messages, by default new queue limits are
  1155. * as belows:
  1156. *
  1157. * TIPC_LOW_IMPORTANCE (4 MB)
  1158. * TIPC_MEDIUM_IMPORTANCE (8 MB)
  1159. * TIPC_HIGH_IMPORTANCE (16 MB)
  1160. * TIPC_CRITICAL_IMPORTANCE (32 MB)
  1161. *
  1162. * Returns overload limit according to corresponding message importance
  1163. */
  1164. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
  1165. {
  1166. struct tipc_msg *msg = buf_msg(buf);
  1167. if (msg_connected(msg))
  1168. return sysctl_tipc_rmem[2];
  1169. return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
  1170. msg_importance(msg);
  1171. }
  1172. /**
  1173. * filter_rcv - validate incoming message
  1174. * @sk: socket
  1175. * @buf: message
  1176. *
  1177. * Enqueues message on receive queue if acceptable; optionally handles
  1178. * disconnect indication for a connected socket.
  1179. *
  1180. * Called with socket lock already taken; port lock may also be taken.
  1181. *
  1182. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  1183. */
  1184. static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
  1185. {
  1186. struct socket *sock = sk->sk_socket;
  1187. struct tipc_msg *msg = buf_msg(buf);
  1188. unsigned int limit = rcvbuf_limit(sk, buf);
  1189. u32 res = TIPC_OK;
  1190. /* Reject message if it is wrong sort of message for socket */
  1191. if (msg_type(msg) > TIPC_DIRECT_MSG)
  1192. return TIPC_ERR_NO_PORT;
  1193. if (sock->state == SS_READY) {
  1194. if (msg_connected(msg))
  1195. return TIPC_ERR_NO_PORT;
  1196. } else {
  1197. res = filter_connect(tipc_sk(sk), &buf);
  1198. if (res != TIPC_OK || buf == NULL)
  1199. return res;
  1200. }
  1201. /* Reject message if there isn't room to queue it */
  1202. if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
  1203. return TIPC_ERR_OVERLOAD;
  1204. /* Enqueue message */
  1205. TIPC_SKB_CB(buf)->handle = NULL;
  1206. __skb_queue_tail(&sk->sk_receive_queue, buf);
  1207. skb_set_owner_r(buf, sk);
  1208. sk->sk_data_ready(sk, 0);
  1209. return TIPC_OK;
  1210. }
  1211. /**
  1212. * backlog_rcv - handle incoming message from backlog queue
  1213. * @sk: socket
  1214. * @buf: message
  1215. *
  1216. * Caller must hold socket lock, but not port lock.
  1217. *
  1218. * Returns 0
  1219. */
  1220. static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
  1221. {
  1222. u32 res;
  1223. res = filter_rcv(sk, buf);
  1224. if (res)
  1225. tipc_reject_msg(buf, res);
  1226. return 0;
  1227. }
  1228. /**
  1229. * dispatch - handle incoming message
  1230. * @tport: TIPC port that received message
  1231. * @buf: message
  1232. *
  1233. * Called with port lock already taken.
  1234. *
  1235. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  1236. */
  1237. static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
  1238. {
  1239. struct sock *sk = tport->sk;
  1240. u32 res;
  1241. /*
  1242. * Process message if socket is unlocked; otherwise add to backlog queue
  1243. *
  1244. * This code is based on sk_receive_skb(), but must be distinct from it
  1245. * since a TIPC-specific filter/reject mechanism is utilized
  1246. */
  1247. bh_lock_sock(sk);
  1248. if (!sock_owned_by_user(sk)) {
  1249. res = filter_rcv(sk, buf);
  1250. } else {
  1251. if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
  1252. res = TIPC_ERR_OVERLOAD;
  1253. else
  1254. res = TIPC_OK;
  1255. }
  1256. bh_unlock_sock(sk);
  1257. return res;
  1258. }
  1259. /**
  1260. * wakeupdispatch - wake up port after congestion
  1261. * @tport: port to wakeup
  1262. *
  1263. * Called with port lock already taken.
  1264. */
  1265. static void wakeupdispatch(struct tipc_port *tport)
  1266. {
  1267. struct sock *sk = tport->sk;
  1268. sk->sk_write_space(sk);
  1269. }
  1270. /**
  1271. * connect - establish a connection to another TIPC port
  1272. * @sock: socket structure
  1273. * @dest: socket address for destination port
  1274. * @destlen: size of socket address data structure
  1275. * @flags: file-related flags associated with socket
  1276. *
  1277. * Returns 0 on success, errno otherwise
  1278. */
  1279. static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
  1280. int flags)
  1281. {
  1282. struct sock *sk = sock->sk;
  1283. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1284. struct msghdr m = {NULL,};
  1285. unsigned int timeout;
  1286. int res;
  1287. lock_sock(sk);
  1288. /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
  1289. if (sock->state == SS_READY) {
  1290. res = -EOPNOTSUPP;
  1291. goto exit;
  1292. }
  1293. /*
  1294. * Reject connection attempt using multicast address
  1295. *
  1296. * Note: send_msg() validates the rest of the address fields,
  1297. * so there's no need to do it here
  1298. */
  1299. if (dst->addrtype == TIPC_ADDR_MCAST) {
  1300. res = -EINVAL;
  1301. goto exit;
  1302. }
  1303. timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
  1304. switch (sock->state) {
  1305. case SS_UNCONNECTED:
  1306. /* Send a 'SYN-' to destination */
  1307. m.msg_name = dest;
  1308. m.msg_namelen = destlen;
  1309. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  1310. * indicate send_msg() is never blocked.
  1311. */
  1312. if (!timeout)
  1313. m.msg_flags = MSG_DONTWAIT;
  1314. res = send_msg(NULL, sock, &m, 0);
  1315. if ((res < 0) && (res != -EWOULDBLOCK))
  1316. goto exit;
  1317. /* Just entered SS_CONNECTING state; the only
  1318. * difference is that return value in non-blocking
  1319. * case is EINPROGRESS, rather than EALREADY.
  1320. */
  1321. res = -EINPROGRESS;
  1322. break;
  1323. case SS_CONNECTING:
  1324. res = -EALREADY;
  1325. break;
  1326. case SS_CONNECTED:
  1327. res = -EISCONN;
  1328. break;
  1329. default:
  1330. res = -EINVAL;
  1331. goto exit;
  1332. }
  1333. if (sock->state == SS_CONNECTING) {
  1334. if (!timeout)
  1335. goto exit;
  1336. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  1337. release_sock(sk);
  1338. res = wait_event_interruptible_timeout(*sk_sleep(sk),
  1339. sock->state != SS_CONNECTING,
  1340. timeout ? (long)msecs_to_jiffies(timeout)
  1341. : MAX_SCHEDULE_TIMEOUT);
  1342. if (res <= 0) {
  1343. if (res == 0)
  1344. res = -ETIMEDOUT;
  1345. return res;
  1346. }
  1347. lock_sock(sk);
  1348. }
  1349. if (unlikely(sock->state == SS_DISCONNECTING))
  1350. res = sock_error(sk);
  1351. else
  1352. res = 0;
  1353. exit:
  1354. release_sock(sk);
  1355. return res;
  1356. }
  1357. /**
  1358. * listen - allow socket to listen for incoming connections
  1359. * @sock: socket structure
  1360. * @len: (unused)
  1361. *
  1362. * Returns 0 on success, errno otherwise
  1363. */
  1364. static int listen(struct socket *sock, int len)
  1365. {
  1366. struct sock *sk = sock->sk;
  1367. int res;
  1368. lock_sock(sk);
  1369. if (sock->state != SS_UNCONNECTED)
  1370. res = -EINVAL;
  1371. else {
  1372. sock->state = SS_LISTENING;
  1373. res = 0;
  1374. }
  1375. release_sock(sk);
  1376. return res;
  1377. }
  1378. /**
  1379. * accept - wait for connection request
  1380. * @sock: listening socket
  1381. * @newsock: new socket that is to be connected
  1382. * @flags: file-related flags associated with socket
  1383. *
  1384. * Returns 0 on success, errno otherwise
  1385. */
  1386. static int accept(struct socket *sock, struct socket *new_sock, int flags)
  1387. {
  1388. struct sock *new_sk, *sk = sock->sk;
  1389. struct sk_buff *buf;
  1390. struct tipc_sock *new_tsock;
  1391. struct tipc_port *new_tport;
  1392. struct tipc_msg *msg;
  1393. u32 new_ref;
  1394. int res;
  1395. lock_sock(sk);
  1396. if (sock->state != SS_LISTENING) {
  1397. res = -EINVAL;
  1398. goto exit;
  1399. }
  1400. while (skb_queue_empty(&sk->sk_receive_queue)) {
  1401. if (flags & O_NONBLOCK) {
  1402. res = -EWOULDBLOCK;
  1403. goto exit;
  1404. }
  1405. release_sock(sk);
  1406. res = wait_event_interruptible(*sk_sleep(sk),
  1407. (!skb_queue_empty(&sk->sk_receive_queue)));
  1408. lock_sock(sk);
  1409. if (res)
  1410. goto exit;
  1411. }
  1412. buf = skb_peek(&sk->sk_receive_queue);
  1413. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
  1414. if (res)
  1415. goto exit;
  1416. new_sk = new_sock->sk;
  1417. new_tsock = tipc_sk(new_sk);
  1418. new_tport = new_tsock->p;
  1419. new_ref = new_tport->ref;
  1420. msg = buf_msg(buf);
  1421. /* we lock on new_sk; but lockdep sees the lock on sk */
  1422. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  1423. /*
  1424. * Reject any stray messages received by new socket
  1425. * before the socket lock was taken (very, very unlikely)
  1426. */
  1427. reject_rx_queue(new_sk);
  1428. /* Connect new socket to it's peer */
  1429. new_tsock->peer_name.ref = msg_origport(msg);
  1430. new_tsock->peer_name.node = msg_orignode(msg);
  1431. tipc_connect(new_ref, &new_tsock->peer_name);
  1432. new_sock->state = SS_CONNECTED;
  1433. tipc_set_portimportance(new_ref, msg_importance(msg));
  1434. if (msg_named(msg)) {
  1435. new_tport->conn_type = msg_nametype(msg);
  1436. new_tport->conn_instance = msg_nameinst(msg);
  1437. }
  1438. /*
  1439. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1440. * Respond to 'SYN+' by queuing it on new socket.
  1441. */
  1442. if (!msg_data_sz(msg)) {
  1443. struct msghdr m = {NULL,};
  1444. advance_rx_queue(sk);
  1445. send_packet(NULL, new_sock, &m, 0);
  1446. } else {
  1447. __skb_dequeue(&sk->sk_receive_queue);
  1448. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  1449. skb_set_owner_r(buf, new_sk);
  1450. }
  1451. release_sock(new_sk);
  1452. exit:
  1453. release_sock(sk);
  1454. return res;
  1455. }
  1456. /**
  1457. * shutdown - shutdown socket connection
  1458. * @sock: socket structure
  1459. * @how: direction to close (must be SHUT_RDWR)
  1460. *
  1461. * Terminates connection (if necessary), then purges socket's receive queue.
  1462. *
  1463. * Returns 0 on success, errno otherwise
  1464. */
  1465. static int shutdown(struct socket *sock, int how)
  1466. {
  1467. struct sock *sk = sock->sk;
  1468. struct tipc_port *tport = tipc_sk_port(sk);
  1469. struct sk_buff *buf;
  1470. int res;
  1471. if (how != SHUT_RDWR)
  1472. return -EINVAL;
  1473. lock_sock(sk);
  1474. switch (sock->state) {
  1475. case SS_CONNECTING:
  1476. case SS_CONNECTED:
  1477. restart:
  1478. /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
  1479. buf = __skb_dequeue(&sk->sk_receive_queue);
  1480. if (buf) {
  1481. if (TIPC_SKB_CB(buf)->handle != NULL) {
  1482. kfree_skb(buf);
  1483. goto restart;
  1484. }
  1485. tipc_disconnect(tport->ref);
  1486. tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
  1487. } else {
  1488. tipc_shutdown(tport->ref);
  1489. }
  1490. sock->state = SS_DISCONNECTING;
  1491. /* fall through */
  1492. case SS_DISCONNECTING:
  1493. /* Discard any unreceived messages */
  1494. __skb_queue_purge(&sk->sk_receive_queue);
  1495. /* Wake up anyone sleeping in poll */
  1496. sk->sk_state_change(sk);
  1497. res = 0;
  1498. break;
  1499. default:
  1500. res = -ENOTCONN;
  1501. }
  1502. release_sock(sk);
  1503. return res;
  1504. }
  1505. /**
  1506. * setsockopt - set socket option
  1507. * @sock: socket structure
  1508. * @lvl: option level
  1509. * @opt: option identifier
  1510. * @ov: pointer to new option value
  1511. * @ol: length of option value
  1512. *
  1513. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  1514. * (to ease compatibility).
  1515. *
  1516. * Returns 0 on success, errno otherwise
  1517. */
  1518. static int setsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
  1519. unsigned int ol)
  1520. {
  1521. struct sock *sk = sock->sk;
  1522. struct tipc_port *tport = tipc_sk_port(sk);
  1523. u32 value;
  1524. int res;
  1525. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1526. return 0;
  1527. if (lvl != SOL_TIPC)
  1528. return -ENOPROTOOPT;
  1529. if (ol < sizeof(value))
  1530. return -EINVAL;
  1531. res = get_user(value, (u32 __user *)ov);
  1532. if (res)
  1533. return res;
  1534. lock_sock(sk);
  1535. switch (opt) {
  1536. case TIPC_IMPORTANCE:
  1537. res = tipc_set_portimportance(tport->ref, value);
  1538. break;
  1539. case TIPC_SRC_DROPPABLE:
  1540. if (sock->type != SOCK_STREAM)
  1541. res = tipc_set_portunreliable(tport->ref, value);
  1542. else
  1543. res = -ENOPROTOOPT;
  1544. break;
  1545. case TIPC_DEST_DROPPABLE:
  1546. res = tipc_set_portunreturnable(tport->ref, value);
  1547. break;
  1548. case TIPC_CONN_TIMEOUT:
  1549. tipc_sk(sk)->conn_timeout = value;
  1550. /* no need to set "res", since already 0 at this point */
  1551. break;
  1552. default:
  1553. res = -EINVAL;
  1554. }
  1555. release_sock(sk);
  1556. return res;
  1557. }
  1558. /**
  1559. * getsockopt - get socket option
  1560. * @sock: socket structure
  1561. * @lvl: option level
  1562. * @opt: option identifier
  1563. * @ov: receptacle for option value
  1564. * @ol: receptacle for length of option value
  1565. *
  1566. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  1567. * (to ease compatibility).
  1568. *
  1569. * Returns 0 on success, errno otherwise
  1570. */
  1571. static int getsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
  1572. int __user *ol)
  1573. {
  1574. struct sock *sk = sock->sk;
  1575. struct tipc_port *tport = tipc_sk_port(sk);
  1576. int len;
  1577. u32 value;
  1578. int res;
  1579. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1580. return put_user(0, ol);
  1581. if (lvl != SOL_TIPC)
  1582. return -ENOPROTOOPT;
  1583. res = get_user(len, ol);
  1584. if (res)
  1585. return res;
  1586. lock_sock(sk);
  1587. switch (opt) {
  1588. case TIPC_IMPORTANCE:
  1589. res = tipc_portimportance(tport->ref, &value);
  1590. break;
  1591. case TIPC_SRC_DROPPABLE:
  1592. res = tipc_portunreliable(tport->ref, &value);
  1593. break;
  1594. case TIPC_DEST_DROPPABLE:
  1595. res = tipc_portunreturnable(tport->ref, &value);
  1596. break;
  1597. case TIPC_CONN_TIMEOUT:
  1598. value = tipc_sk(sk)->conn_timeout;
  1599. /* no need to set "res", since already 0 at this point */
  1600. break;
  1601. case TIPC_NODE_RECVQ_DEPTH:
  1602. value = 0; /* was tipc_queue_size, now obsolete */
  1603. break;
  1604. case TIPC_SOCK_RECVQ_DEPTH:
  1605. value = skb_queue_len(&sk->sk_receive_queue);
  1606. break;
  1607. default:
  1608. res = -EINVAL;
  1609. }
  1610. release_sock(sk);
  1611. if (res)
  1612. return res; /* "get" failed */
  1613. if (len < sizeof(value))
  1614. return -EINVAL;
  1615. if (copy_to_user(ov, &value, sizeof(value)))
  1616. return -EFAULT;
  1617. return put_user(sizeof(value), ol);
  1618. }
  1619. /* Protocol switches for the various types of TIPC sockets */
  1620. static const struct proto_ops msg_ops = {
  1621. .owner = THIS_MODULE,
  1622. .family = AF_TIPC,
  1623. .release = release,
  1624. .bind = bind,
  1625. .connect = connect,
  1626. .socketpair = sock_no_socketpair,
  1627. .accept = sock_no_accept,
  1628. .getname = get_name,
  1629. .poll = poll,
  1630. .ioctl = sock_no_ioctl,
  1631. .listen = sock_no_listen,
  1632. .shutdown = shutdown,
  1633. .setsockopt = setsockopt,
  1634. .getsockopt = getsockopt,
  1635. .sendmsg = send_msg,
  1636. .recvmsg = recv_msg,
  1637. .mmap = sock_no_mmap,
  1638. .sendpage = sock_no_sendpage
  1639. };
  1640. static const struct proto_ops packet_ops = {
  1641. .owner = THIS_MODULE,
  1642. .family = AF_TIPC,
  1643. .release = release,
  1644. .bind = bind,
  1645. .connect = connect,
  1646. .socketpair = sock_no_socketpair,
  1647. .accept = accept,
  1648. .getname = get_name,
  1649. .poll = poll,
  1650. .ioctl = sock_no_ioctl,
  1651. .listen = listen,
  1652. .shutdown = shutdown,
  1653. .setsockopt = setsockopt,
  1654. .getsockopt = getsockopt,
  1655. .sendmsg = send_packet,
  1656. .recvmsg = recv_msg,
  1657. .mmap = sock_no_mmap,
  1658. .sendpage = sock_no_sendpage
  1659. };
  1660. static const struct proto_ops stream_ops = {
  1661. .owner = THIS_MODULE,
  1662. .family = AF_TIPC,
  1663. .release = release,
  1664. .bind = bind,
  1665. .connect = connect,
  1666. .socketpair = sock_no_socketpair,
  1667. .accept = accept,
  1668. .getname = get_name,
  1669. .poll = poll,
  1670. .ioctl = sock_no_ioctl,
  1671. .listen = listen,
  1672. .shutdown = shutdown,
  1673. .setsockopt = setsockopt,
  1674. .getsockopt = getsockopt,
  1675. .sendmsg = send_stream,
  1676. .recvmsg = recv_stream,
  1677. .mmap = sock_no_mmap,
  1678. .sendpage = sock_no_sendpage
  1679. };
  1680. static const struct net_proto_family tipc_family_ops = {
  1681. .owner = THIS_MODULE,
  1682. .family = AF_TIPC,
  1683. .create = tipc_sk_create
  1684. };
  1685. static struct proto tipc_proto = {
  1686. .name = "TIPC",
  1687. .owner = THIS_MODULE,
  1688. .obj_size = sizeof(struct tipc_sock),
  1689. .sysctl_rmem = sysctl_tipc_rmem
  1690. };
  1691. static struct proto tipc_proto_kern = {
  1692. .name = "TIPC",
  1693. .obj_size = sizeof(struct tipc_sock),
  1694. .sysctl_rmem = sysctl_tipc_rmem
  1695. };
  1696. /**
  1697. * tipc_socket_init - initialize TIPC socket interface
  1698. *
  1699. * Returns 0 on success, errno otherwise
  1700. */
  1701. int tipc_socket_init(void)
  1702. {
  1703. int res;
  1704. res = proto_register(&tipc_proto, 1);
  1705. if (res) {
  1706. pr_err("Failed to register TIPC protocol type\n");
  1707. goto out;
  1708. }
  1709. res = sock_register(&tipc_family_ops);
  1710. if (res) {
  1711. pr_err("Failed to register TIPC socket type\n");
  1712. proto_unregister(&tipc_proto);
  1713. goto out;
  1714. }
  1715. sockets_enabled = 1;
  1716. out:
  1717. return res;
  1718. }
  1719. /**
  1720. * tipc_socket_stop - stop TIPC socket interface
  1721. */
  1722. void tipc_socket_stop(void)
  1723. {
  1724. if (!sockets_enabled)
  1725. return;
  1726. sockets_enabled = 0;
  1727. sock_unregister(tipc_family_ops.family);
  1728. proto_unregister(&tipc_proto);
  1729. }