socket.c 50 KB

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