socket.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  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. if (res <= 0) {
  1344. if (res == 0)
  1345. res = -ETIMEDOUT;
  1346. return res;
  1347. }
  1348. lock_sock(sk);
  1349. }
  1350. if (unlikely(sock->state == SS_DISCONNECTING))
  1351. res = sock_error(sk);
  1352. else
  1353. res = 0;
  1354. exit:
  1355. release_sock(sk);
  1356. return res;
  1357. }
  1358. /**
  1359. * listen - allow socket to listen for incoming connections
  1360. * @sock: socket structure
  1361. * @len: (unused)
  1362. *
  1363. * Returns 0 on success, errno otherwise
  1364. */
  1365. static int listen(struct socket *sock, int len)
  1366. {
  1367. struct sock *sk = sock->sk;
  1368. int res;
  1369. lock_sock(sk);
  1370. if (sock->state != SS_UNCONNECTED)
  1371. res = -EINVAL;
  1372. else {
  1373. sock->state = SS_LISTENING;
  1374. res = 0;
  1375. }
  1376. release_sock(sk);
  1377. return res;
  1378. }
  1379. /**
  1380. * accept - wait for connection request
  1381. * @sock: listening socket
  1382. * @newsock: new socket that is to be connected
  1383. * @flags: file-related flags associated with socket
  1384. *
  1385. * Returns 0 on success, errno otherwise
  1386. */
  1387. static int accept(struct socket *sock, struct socket *new_sock, int flags)
  1388. {
  1389. struct sock *new_sk, *sk = sock->sk;
  1390. struct sk_buff *buf;
  1391. struct tipc_sock *new_tsock;
  1392. struct tipc_port *new_tport;
  1393. struct tipc_msg *msg;
  1394. u32 new_ref;
  1395. int res;
  1396. lock_sock(sk);
  1397. if (sock->state != SS_LISTENING) {
  1398. res = -EINVAL;
  1399. goto exit;
  1400. }
  1401. while (skb_queue_empty(&sk->sk_receive_queue)) {
  1402. if (flags & O_NONBLOCK) {
  1403. res = -EWOULDBLOCK;
  1404. goto exit;
  1405. }
  1406. release_sock(sk);
  1407. res = wait_event_interruptible(*sk_sleep(sk),
  1408. (!skb_queue_empty(&sk->sk_receive_queue)));
  1409. lock_sock(sk);
  1410. if (res)
  1411. goto exit;
  1412. }
  1413. buf = skb_peek(&sk->sk_receive_queue);
  1414. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
  1415. if (res)
  1416. goto exit;
  1417. new_sk = new_sock->sk;
  1418. new_tsock = tipc_sk(new_sk);
  1419. new_tport = new_tsock->p;
  1420. new_ref = new_tport->ref;
  1421. msg = buf_msg(buf);
  1422. /* we lock on new_sk; but lockdep sees the lock on sk */
  1423. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  1424. /*
  1425. * Reject any stray messages received by new socket
  1426. * before the socket lock was taken (very, very unlikely)
  1427. */
  1428. reject_rx_queue(new_sk);
  1429. /* Connect new socket to it's peer */
  1430. new_tsock->peer_name.ref = msg_origport(msg);
  1431. new_tsock->peer_name.node = msg_orignode(msg);
  1432. tipc_connect(new_ref, &new_tsock->peer_name);
  1433. new_sock->state = SS_CONNECTED;
  1434. tipc_set_portimportance(new_ref, msg_importance(msg));
  1435. if (msg_named(msg)) {
  1436. new_tport->conn_type = msg_nametype(msg);
  1437. new_tport->conn_instance = msg_nameinst(msg);
  1438. }
  1439. /*
  1440. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1441. * Respond to 'SYN+' by queuing it on new socket.
  1442. */
  1443. if (!msg_data_sz(msg)) {
  1444. struct msghdr m = {NULL,};
  1445. advance_rx_queue(sk);
  1446. send_packet(NULL, new_sock, &m, 0);
  1447. } else {
  1448. __skb_dequeue(&sk->sk_receive_queue);
  1449. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  1450. skb_set_owner_r(buf, new_sk);
  1451. }
  1452. release_sock(new_sk);
  1453. exit:
  1454. release_sock(sk);
  1455. return res;
  1456. }
  1457. /**
  1458. * shutdown - shutdown socket connection
  1459. * @sock: socket structure
  1460. * @how: direction to close (must be SHUT_RDWR)
  1461. *
  1462. * Terminates connection (if necessary), then purges socket's receive queue.
  1463. *
  1464. * Returns 0 on success, errno otherwise
  1465. */
  1466. static int shutdown(struct socket *sock, int how)
  1467. {
  1468. struct sock *sk = sock->sk;
  1469. struct tipc_port *tport = tipc_sk_port(sk);
  1470. struct sk_buff *buf;
  1471. int res;
  1472. if (how != SHUT_RDWR)
  1473. return -EINVAL;
  1474. lock_sock(sk);
  1475. switch (sock->state) {
  1476. case SS_CONNECTING:
  1477. case SS_CONNECTED:
  1478. restart:
  1479. /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
  1480. buf = __skb_dequeue(&sk->sk_receive_queue);
  1481. if (buf) {
  1482. if (TIPC_SKB_CB(buf)->handle != NULL) {
  1483. kfree_skb(buf);
  1484. goto restart;
  1485. }
  1486. tipc_disconnect(tport->ref);
  1487. tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
  1488. } else {
  1489. tipc_shutdown(tport->ref);
  1490. }
  1491. sock->state = SS_DISCONNECTING;
  1492. /* fall through */
  1493. case SS_DISCONNECTING:
  1494. /* Discard any unreceived messages */
  1495. __skb_queue_purge(&sk->sk_receive_queue);
  1496. /* Wake up anyone sleeping in poll */
  1497. sk->sk_state_change(sk);
  1498. res = 0;
  1499. break;
  1500. default:
  1501. res = -ENOTCONN;
  1502. }
  1503. release_sock(sk);
  1504. return res;
  1505. }
  1506. /**
  1507. * setsockopt - set socket option
  1508. * @sock: socket structure
  1509. * @lvl: option level
  1510. * @opt: option identifier
  1511. * @ov: pointer to new option value
  1512. * @ol: length of option value
  1513. *
  1514. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  1515. * (to ease compatibility).
  1516. *
  1517. * Returns 0 on success, errno otherwise
  1518. */
  1519. static int setsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
  1520. unsigned int ol)
  1521. {
  1522. struct sock *sk = sock->sk;
  1523. struct tipc_port *tport = tipc_sk_port(sk);
  1524. u32 value;
  1525. int res;
  1526. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1527. return 0;
  1528. if (lvl != SOL_TIPC)
  1529. return -ENOPROTOOPT;
  1530. if (ol < sizeof(value))
  1531. return -EINVAL;
  1532. res = get_user(value, (u32 __user *)ov);
  1533. if (res)
  1534. return res;
  1535. lock_sock(sk);
  1536. switch (opt) {
  1537. case TIPC_IMPORTANCE:
  1538. res = tipc_set_portimportance(tport->ref, value);
  1539. break;
  1540. case TIPC_SRC_DROPPABLE:
  1541. if (sock->type != SOCK_STREAM)
  1542. res = tipc_set_portunreliable(tport->ref, value);
  1543. else
  1544. res = -ENOPROTOOPT;
  1545. break;
  1546. case TIPC_DEST_DROPPABLE:
  1547. res = tipc_set_portunreturnable(tport->ref, value);
  1548. break;
  1549. case TIPC_CONN_TIMEOUT:
  1550. tipc_sk(sk)->conn_timeout = value;
  1551. /* no need to set "res", since already 0 at this point */
  1552. break;
  1553. default:
  1554. res = -EINVAL;
  1555. }
  1556. release_sock(sk);
  1557. return res;
  1558. }
  1559. /**
  1560. * getsockopt - get socket option
  1561. * @sock: socket structure
  1562. * @lvl: option level
  1563. * @opt: option identifier
  1564. * @ov: receptacle for option value
  1565. * @ol: receptacle for length of option value
  1566. *
  1567. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  1568. * (to ease compatibility).
  1569. *
  1570. * Returns 0 on success, errno otherwise
  1571. */
  1572. static int getsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
  1573. int __user *ol)
  1574. {
  1575. struct sock *sk = sock->sk;
  1576. struct tipc_port *tport = tipc_sk_port(sk);
  1577. int len;
  1578. u32 value;
  1579. int res;
  1580. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1581. return put_user(0, ol);
  1582. if (lvl != SOL_TIPC)
  1583. return -ENOPROTOOPT;
  1584. res = get_user(len, ol);
  1585. if (res)
  1586. return res;
  1587. lock_sock(sk);
  1588. switch (opt) {
  1589. case TIPC_IMPORTANCE:
  1590. res = tipc_portimportance(tport->ref, &value);
  1591. break;
  1592. case TIPC_SRC_DROPPABLE:
  1593. res = tipc_portunreliable(tport->ref, &value);
  1594. break;
  1595. case TIPC_DEST_DROPPABLE:
  1596. res = tipc_portunreturnable(tport->ref, &value);
  1597. break;
  1598. case TIPC_CONN_TIMEOUT:
  1599. value = tipc_sk(sk)->conn_timeout;
  1600. /* no need to set "res", since already 0 at this point */
  1601. break;
  1602. case TIPC_NODE_RECVQ_DEPTH:
  1603. value = 0; /* was tipc_queue_size, now obsolete */
  1604. break;
  1605. case TIPC_SOCK_RECVQ_DEPTH:
  1606. value = skb_queue_len(&sk->sk_receive_queue);
  1607. break;
  1608. default:
  1609. res = -EINVAL;
  1610. }
  1611. release_sock(sk);
  1612. if (res)
  1613. return res; /* "get" failed */
  1614. if (len < sizeof(value))
  1615. return -EINVAL;
  1616. if (copy_to_user(ov, &value, sizeof(value)))
  1617. return -EFAULT;
  1618. return put_user(sizeof(value), ol);
  1619. }
  1620. /* Protocol switches for the various types of TIPC sockets */
  1621. static const struct proto_ops msg_ops = {
  1622. .owner = THIS_MODULE,
  1623. .family = AF_TIPC,
  1624. .release = release,
  1625. .bind = bind,
  1626. .connect = connect,
  1627. .socketpair = sock_no_socketpair,
  1628. .accept = sock_no_accept,
  1629. .getname = get_name,
  1630. .poll = poll,
  1631. .ioctl = sock_no_ioctl,
  1632. .listen = sock_no_listen,
  1633. .shutdown = shutdown,
  1634. .setsockopt = setsockopt,
  1635. .getsockopt = getsockopt,
  1636. .sendmsg = send_msg,
  1637. .recvmsg = recv_msg,
  1638. .mmap = sock_no_mmap,
  1639. .sendpage = sock_no_sendpage
  1640. };
  1641. static const struct proto_ops packet_ops = {
  1642. .owner = THIS_MODULE,
  1643. .family = AF_TIPC,
  1644. .release = release,
  1645. .bind = bind,
  1646. .connect = connect,
  1647. .socketpair = sock_no_socketpair,
  1648. .accept = accept,
  1649. .getname = get_name,
  1650. .poll = poll,
  1651. .ioctl = sock_no_ioctl,
  1652. .listen = listen,
  1653. .shutdown = shutdown,
  1654. .setsockopt = setsockopt,
  1655. .getsockopt = getsockopt,
  1656. .sendmsg = send_packet,
  1657. .recvmsg = recv_msg,
  1658. .mmap = sock_no_mmap,
  1659. .sendpage = sock_no_sendpage
  1660. };
  1661. static const struct proto_ops stream_ops = {
  1662. .owner = THIS_MODULE,
  1663. .family = AF_TIPC,
  1664. .release = release,
  1665. .bind = bind,
  1666. .connect = connect,
  1667. .socketpair = sock_no_socketpair,
  1668. .accept = accept,
  1669. .getname = get_name,
  1670. .poll = poll,
  1671. .ioctl = sock_no_ioctl,
  1672. .listen = listen,
  1673. .shutdown = shutdown,
  1674. .setsockopt = setsockopt,
  1675. .getsockopt = getsockopt,
  1676. .sendmsg = send_stream,
  1677. .recvmsg = recv_stream,
  1678. .mmap = sock_no_mmap,
  1679. .sendpage = sock_no_sendpage
  1680. };
  1681. static const struct net_proto_family tipc_family_ops = {
  1682. .owner = THIS_MODULE,
  1683. .family = AF_TIPC,
  1684. .create = tipc_sk_create
  1685. };
  1686. static struct proto tipc_proto = {
  1687. .name = "TIPC",
  1688. .owner = THIS_MODULE,
  1689. .obj_size = sizeof(struct tipc_sock),
  1690. .sysctl_rmem = sysctl_tipc_rmem
  1691. };
  1692. static struct proto tipc_proto_kern = {
  1693. .name = "TIPC",
  1694. .obj_size = sizeof(struct tipc_sock),
  1695. .sysctl_rmem = sysctl_tipc_rmem
  1696. };
  1697. /**
  1698. * tipc_socket_init - initialize TIPC socket interface
  1699. *
  1700. * Returns 0 on success, errno otherwise
  1701. */
  1702. int tipc_socket_init(void)
  1703. {
  1704. int res;
  1705. res = proto_register(&tipc_proto, 1);
  1706. if (res) {
  1707. pr_err("Failed to register TIPC protocol type\n");
  1708. goto out;
  1709. }
  1710. res = sock_register(&tipc_family_ops);
  1711. if (res) {
  1712. pr_err("Failed to register TIPC socket type\n");
  1713. proto_unregister(&tipc_proto);
  1714. goto out;
  1715. }
  1716. sockets_enabled = 1;
  1717. out:
  1718. return res;
  1719. }
  1720. /**
  1721. * tipc_socket_stop - stop TIPC socket interface
  1722. */
  1723. void tipc_socket_stop(void)
  1724. {
  1725. if (!sockets_enabled)
  1726. return;
  1727. sockets_enabled = 0;
  1728. sock_unregister(tipc_family_ops.family);
  1729. proto_unregister(&tipc_proto);
  1730. }