socket.c 49 KB

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