socket.c 49 KB

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