socket.c 49 KB

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