socket.c 49 KB

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