socket.c 49 KB

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