socket.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  1. /*
  2. * net/tipc/socket.c: TIPC socket API
  3. *
  4. * Copyright (c) 2001-2007, Ericsson AB
  5. * Copyright (c) 2004-2007, 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 <linux/module.h>
  37. #include <linux/types.h>
  38. #include <linux/net.h>
  39. #include <linux/socket.h>
  40. #include <linux/errno.h>
  41. #include <linux/mm.h>
  42. #include <linux/slab.h>
  43. #include <linux/poll.h>
  44. #include <linux/fcntl.h>
  45. #include <linux/mutex.h>
  46. #include <asm/string.h>
  47. #include <asm/atomic.h>
  48. #include <net/sock.h>
  49. #include <linux/tipc.h>
  50. #include <linux/tipc_config.h>
  51. #include <net/tipc/tipc_msg.h>
  52. #include <net/tipc/tipc_port.h>
  53. #include "core.h"
  54. #define SS_LISTENING -1 /* socket is listening */
  55. #define SS_READY -2 /* socket is connectionless */
  56. #define OVERLOAD_LIMIT_BASE 5000
  57. #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
  58. struct tipc_sock {
  59. struct sock sk;
  60. struct tipc_port *p;
  61. struct mutex lock;
  62. };
  63. #define tipc_sk(sk) ((struct tipc_sock*)sk)
  64. static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
  65. static void wakeupdispatch(struct tipc_port *tport);
  66. static const struct proto_ops packet_ops;
  67. static const struct proto_ops stream_ops;
  68. static const struct proto_ops msg_ops;
  69. static struct proto tipc_proto;
  70. static int sockets_enabled = 0;
  71. static atomic_t tipc_queue_size = ATOMIC_INIT(0);
  72. /*
  73. * sock_lock(): Lock a port/socket pair. lock_sock() can
  74. * not be used here, since the same lock must protect ports
  75. * with non-socket interfaces.
  76. * See net.c for description of locking policy.
  77. */
  78. static void sock_lock(struct tipc_sock* tsock)
  79. {
  80. spin_lock_bh(tsock->p->lock);
  81. }
  82. /*
  83. * sock_unlock(): Unlock a port/socket pair
  84. */
  85. static void sock_unlock(struct tipc_sock* tsock)
  86. {
  87. spin_unlock_bh(tsock->p->lock);
  88. }
  89. /**
  90. * advance_queue - discard first buffer in queue
  91. * @tsock: TIPC socket
  92. */
  93. static void advance_queue(struct tipc_sock *tsock)
  94. {
  95. sock_lock(tsock);
  96. buf_discard(skb_dequeue(&tsock->sk.sk_receive_queue));
  97. sock_unlock(tsock);
  98. atomic_dec(&tipc_queue_size);
  99. }
  100. /**
  101. * tipc_create - create a TIPC socket
  102. * @sock: pre-allocated socket structure
  103. * @protocol: protocol indicator (must be 0)
  104. *
  105. * This routine creates and attaches a 'struct sock' to the 'struct socket',
  106. * then create and attaches a TIPC port to the 'struct sock' part.
  107. *
  108. * Returns 0 on success, errno otherwise
  109. */
  110. static int tipc_create(struct net *net, struct socket *sock, int protocol)
  111. {
  112. struct tipc_sock *tsock;
  113. struct tipc_port *port;
  114. struct sock *sk;
  115. u32 ref;
  116. if (net != &init_net)
  117. return -EAFNOSUPPORT;
  118. if (unlikely(protocol != 0))
  119. return -EPROTONOSUPPORT;
  120. ref = tipc_createport_raw(NULL, &dispatch, &wakeupdispatch, TIPC_LOW_IMPORTANCE);
  121. if (unlikely(!ref))
  122. return -ENOMEM;
  123. sock->state = SS_UNCONNECTED;
  124. switch (sock->type) {
  125. case SOCK_STREAM:
  126. sock->ops = &stream_ops;
  127. break;
  128. case SOCK_SEQPACKET:
  129. sock->ops = &packet_ops;
  130. break;
  131. case SOCK_DGRAM:
  132. tipc_set_portunreliable(ref, 1);
  133. /* fall through */
  134. case SOCK_RDM:
  135. tipc_set_portunreturnable(ref, 1);
  136. sock->ops = &msg_ops;
  137. sock->state = SS_READY;
  138. break;
  139. default:
  140. tipc_deleteport(ref);
  141. return -EPROTOTYPE;
  142. }
  143. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
  144. if (!sk) {
  145. tipc_deleteport(ref);
  146. return -ENOMEM;
  147. }
  148. sock_init_data(sock, sk);
  149. sk->sk_rcvtimeo = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
  150. tsock = tipc_sk(sk);
  151. port = tipc_get_port(ref);
  152. tsock->p = port;
  153. port->usr_handle = tsock;
  154. mutex_init(&tsock->lock);
  155. dbg("sock_create: %x\n",tsock);
  156. atomic_inc(&tipc_user_count);
  157. return 0;
  158. }
  159. /**
  160. * release - destroy a TIPC socket
  161. * @sock: socket to destroy
  162. *
  163. * This routine cleans up any messages that are still queued on the socket.
  164. * For DGRAM and RDM socket types, all queued messages are rejected.
  165. * For SEQPACKET and STREAM socket types, the first message is rejected
  166. * and any others are discarded. (If the first message on a STREAM socket
  167. * is partially-read, it is discarded and the next one is rejected instead.)
  168. *
  169. * NOTE: Rejected messages are not necessarily returned to the sender! They
  170. * are returned or discarded according to the "destination droppable" setting
  171. * specified for the message by the sender.
  172. *
  173. * Returns 0 on success, errno otherwise
  174. */
  175. static int release(struct socket *sock)
  176. {
  177. struct tipc_sock *tsock = tipc_sk(sock->sk);
  178. struct sock *sk = sock->sk;
  179. int res = TIPC_OK;
  180. struct sk_buff *buf;
  181. dbg("sock_delete: %x\n",tsock);
  182. if (!tsock)
  183. return 0;
  184. mutex_lock(&tsock->lock);
  185. if (!sock->sk) {
  186. mutex_unlock(&tsock->lock);
  187. return 0;
  188. }
  189. /* Reject unreceived messages, unless no longer connected */
  190. while (sock->state != SS_DISCONNECTING) {
  191. sock_lock(tsock);
  192. buf = skb_dequeue(&sk->sk_receive_queue);
  193. if (!buf)
  194. tsock->p->usr_handle = NULL;
  195. sock_unlock(tsock);
  196. if (!buf)
  197. break;
  198. if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
  199. buf_discard(buf);
  200. else
  201. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  202. atomic_dec(&tipc_queue_size);
  203. }
  204. /* Delete TIPC port */
  205. res = tipc_deleteport(tsock->p->ref);
  206. sock->sk = NULL;
  207. /* Discard any remaining messages */
  208. while ((buf = skb_dequeue(&sk->sk_receive_queue))) {
  209. buf_discard(buf);
  210. atomic_dec(&tipc_queue_size);
  211. }
  212. mutex_unlock(&tsock->lock);
  213. sock_put(sk);
  214. atomic_dec(&tipc_user_count);
  215. return res;
  216. }
  217. /**
  218. * bind - associate or disassocate TIPC name(s) with a socket
  219. * @sock: socket structure
  220. * @uaddr: socket address describing name(s) and desired operation
  221. * @uaddr_len: size of socket address data structure
  222. *
  223. * Name and name sequence binding is indicated using a positive scope value;
  224. * a negative scope value unbinds the specified name. Specifying no name
  225. * (i.e. a socket address length of 0) unbinds all names from the socket.
  226. *
  227. * Returns 0 on success, errno otherwise
  228. */
  229. static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
  230. {
  231. struct tipc_sock *tsock = tipc_sk(sock->sk);
  232. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  233. int res;
  234. if (mutex_lock_interruptible(&tsock->lock))
  235. return -ERESTARTSYS;
  236. if (unlikely(!uaddr_len)) {
  237. res = tipc_withdraw(tsock->p->ref, 0, NULL);
  238. goto exit;
  239. }
  240. if (uaddr_len < sizeof(struct sockaddr_tipc)) {
  241. res = -EINVAL;
  242. goto exit;
  243. }
  244. if (addr->family != AF_TIPC) {
  245. res = -EAFNOSUPPORT;
  246. goto exit;
  247. }
  248. if (addr->addrtype == TIPC_ADDR_NAME)
  249. addr->addr.nameseq.upper = addr->addr.nameseq.lower;
  250. else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
  251. res = -EAFNOSUPPORT;
  252. goto exit;
  253. }
  254. if (addr->scope > 0)
  255. res = tipc_publish(tsock->p->ref, addr->scope,
  256. &addr->addr.nameseq);
  257. else
  258. res = tipc_withdraw(tsock->p->ref, -addr->scope,
  259. &addr->addr.nameseq);
  260. exit:
  261. mutex_unlock(&tsock->lock);
  262. return res;
  263. }
  264. /**
  265. * get_name - get port ID of socket or peer socket
  266. * @sock: socket structure
  267. * @uaddr: area for returned socket address
  268. * @uaddr_len: area for returned length of socket address
  269. * @peer: 0 to obtain socket name, 1 to obtain peer socket name
  270. *
  271. * Returns 0 on success, errno otherwise
  272. */
  273. static int get_name(struct socket *sock, struct sockaddr *uaddr,
  274. int *uaddr_len, int peer)
  275. {
  276. struct tipc_sock *tsock = tipc_sk(sock->sk);
  277. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  278. u32 res;
  279. if (mutex_lock_interruptible(&tsock->lock))
  280. return -ERESTARTSYS;
  281. *uaddr_len = sizeof(*addr);
  282. addr->addrtype = TIPC_ADDR_ID;
  283. addr->family = AF_TIPC;
  284. addr->scope = 0;
  285. if (peer)
  286. res = tipc_peer(tsock->p->ref, &addr->addr.id);
  287. else
  288. res = tipc_ownidentity(tsock->p->ref, &addr->addr.id);
  289. addr->addr.name.domain = 0;
  290. mutex_unlock(&tsock->lock);
  291. return res;
  292. }
  293. /**
  294. * poll - read and possibly block on pollmask
  295. * @file: file structure associated with the socket
  296. * @sock: socket for which to calculate the poll bits
  297. * @wait: ???
  298. *
  299. * Returns pollmask value
  300. *
  301. * COMMENTARY:
  302. * It appears that the usual socket locking mechanisms are not useful here
  303. * since the pollmask info is potentially out-of-date the moment this routine
  304. * exits. TCP and other protocols seem to rely on higher level poll routines
  305. * to handle any preventable race conditions, so TIPC will do the same ...
  306. *
  307. * TIPC sets the returned events as follows:
  308. * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
  309. * or if a connection-oriented socket is does not have an active connection
  310. * (i.e. a read operation will not block).
  311. * b) POLLOUT is set except when a socket's connection has been terminated
  312. * (i.e. a write operation will not block).
  313. * c) POLLHUP is set when a socket's connection has been terminated.
  314. *
  315. * IMPORTANT: The fact that a read or write operation will not block does NOT
  316. * imply that the operation will succeed!
  317. */
  318. static unsigned int poll(struct file *file, struct socket *sock,
  319. poll_table *wait)
  320. {
  321. struct sock *sk = sock->sk;
  322. u32 mask;
  323. poll_wait(file, sk->sk_sleep, wait);
  324. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  325. (sock->state == SS_UNCONNECTED) ||
  326. (sock->state == SS_DISCONNECTING))
  327. mask = (POLLRDNORM | POLLIN);
  328. else
  329. mask = 0;
  330. if (sock->state == SS_DISCONNECTING)
  331. mask |= POLLHUP;
  332. else
  333. mask |= POLLOUT;
  334. return mask;
  335. }
  336. /**
  337. * dest_name_check - verify user is permitted to send to specified port name
  338. * @dest: destination address
  339. * @m: descriptor for message to be sent
  340. *
  341. * Prevents restricted configuration commands from being issued by
  342. * unauthorized users.
  343. *
  344. * Returns 0 if permission is granted, otherwise errno
  345. */
  346. static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
  347. {
  348. struct tipc_cfg_msg_hdr hdr;
  349. if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
  350. return 0;
  351. if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
  352. return 0;
  353. if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
  354. return -EACCES;
  355. if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
  356. return -EFAULT;
  357. if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
  358. return -EACCES;
  359. return 0;
  360. }
  361. /**
  362. * send_msg - send message in connectionless manner
  363. * @iocb: (unused)
  364. * @sock: socket structure
  365. * @m: message to send
  366. * @total_len: length of message
  367. *
  368. * Message must have an destination specified explicitly.
  369. * Used for SOCK_RDM and SOCK_DGRAM messages,
  370. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  371. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  372. *
  373. * Returns the number of bytes sent on success, or errno otherwise
  374. */
  375. static int send_msg(struct kiocb *iocb, struct socket *sock,
  376. struct msghdr *m, size_t total_len)
  377. {
  378. struct tipc_sock *tsock = tipc_sk(sock->sk);
  379. struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
  380. struct sk_buff *buf;
  381. int needs_conn;
  382. int res = -EINVAL;
  383. if (unlikely(!dest))
  384. return -EDESTADDRREQ;
  385. if (unlikely((m->msg_namelen < sizeof(*dest)) ||
  386. (dest->family != AF_TIPC)))
  387. return -EINVAL;
  388. needs_conn = (sock->state != SS_READY);
  389. if (unlikely(needs_conn)) {
  390. if (sock->state == SS_LISTENING)
  391. return -EPIPE;
  392. if (sock->state != SS_UNCONNECTED)
  393. return -EISCONN;
  394. if ((tsock->p->published) ||
  395. ((sock->type == SOCK_STREAM) && (total_len != 0)))
  396. return -EOPNOTSUPP;
  397. if (dest->addrtype == TIPC_ADDR_NAME) {
  398. tsock->p->conn_type = dest->addr.name.name.type;
  399. tsock->p->conn_instance = dest->addr.name.name.instance;
  400. }
  401. }
  402. if (mutex_lock_interruptible(&tsock->lock))
  403. return -ERESTARTSYS;
  404. if (needs_conn) {
  405. /* Abort any pending connection attempts (very unlikely) */
  406. while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
  407. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  408. atomic_dec(&tipc_queue_size);
  409. }
  410. sock->state = SS_CONNECTING;
  411. }
  412. do {
  413. if (dest->addrtype == TIPC_ADDR_NAME) {
  414. if ((res = dest_name_check(dest, m)))
  415. goto exit;
  416. res = tipc_send2name(tsock->p->ref,
  417. &dest->addr.name.name,
  418. dest->addr.name.domain,
  419. m->msg_iovlen,
  420. m->msg_iov);
  421. }
  422. else if (dest->addrtype == TIPC_ADDR_ID) {
  423. res = tipc_send2port(tsock->p->ref,
  424. &dest->addr.id,
  425. m->msg_iovlen,
  426. m->msg_iov);
  427. }
  428. else if (dest->addrtype == TIPC_ADDR_MCAST) {
  429. if (needs_conn) {
  430. res = -EOPNOTSUPP;
  431. goto exit;
  432. }
  433. if ((res = dest_name_check(dest, m)))
  434. goto exit;
  435. res = tipc_multicast(tsock->p->ref,
  436. &dest->addr.nameseq,
  437. 0,
  438. m->msg_iovlen,
  439. m->msg_iov);
  440. }
  441. if (likely(res != -ELINKCONG)) {
  442. exit:
  443. mutex_unlock(&tsock->lock);
  444. return res;
  445. }
  446. if (m->msg_flags & MSG_DONTWAIT) {
  447. res = -EWOULDBLOCK;
  448. goto exit;
  449. }
  450. if (wait_event_interruptible(*sock->sk->sk_sleep,
  451. !tsock->p->congested)) {
  452. res = -ERESTARTSYS;
  453. goto exit;
  454. }
  455. } while (1);
  456. }
  457. /**
  458. * send_packet - send a connection-oriented message
  459. * @iocb: (unused)
  460. * @sock: socket structure
  461. * @m: message to send
  462. * @total_len: length of message
  463. *
  464. * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
  465. *
  466. * Returns the number of bytes sent on success, or errno otherwise
  467. */
  468. static int send_packet(struct kiocb *iocb, struct socket *sock,
  469. struct msghdr *m, size_t total_len)
  470. {
  471. struct tipc_sock *tsock = tipc_sk(sock->sk);
  472. struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
  473. int res;
  474. /* Handle implied connection establishment */
  475. if (unlikely(dest))
  476. return send_msg(iocb, sock, m, total_len);
  477. if (mutex_lock_interruptible(&tsock->lock)) {
  478. return -ERESTARTSYS;
  479. }
  480. do {
  481. if (unlikely(sock->state != SS_CONNECTED)) {
  482. if (sock->state == SS_DISCONNECTING)
  483. res = -EPIPE;
  484. else
  485. res = -ENOTCONN;
  486. goto exit;
  487. }
  488. res = tipc_send(tsock->p->ref, m->msg_iovlen, m->msg_iov);
  489. if (likely(res != -ELINKCONG)) {
  490. exit:
  491. mutex_unlock(&tsock->lock);
  492. return res;
  493. }
  494. if (m->msg_flags & MSG_DONTWAIT) {
  495. res = -EWOULDBLOCK;
  496. goto exit;
  497. }
  498. if (wait_event_interruptible(*sock->sk->sk_sleep,
  499. !tsock->p->congested)) {
  500. res = -ERESTARTSYS;
  501. goto exit;
  502. }
  503. } while (1);
  504. }
  505. /**
  506. * send_stream - send stream-oriented data
  507. * @iocb: (unused)
  508. * @sock: socket structure
  509. * @m: data to send
  510. * @total_len: total length of data to be sent
  511. *
  512. * Used for SOCK_STREAM data.
  513. *
  514. * Returns the number of bytes sent on success (or partial success),
  515. * or errno if no data sent
  516. */
  517. static int send_stream(struct kiocb *iocb, struct socket *sock,
  518. struct msghdr *m, size_t total_len)
  519. {
  520. struct tipc_port *tport;
  521. struct msghdr my_msg;
  522. struct iovec my_iov;
  523. struct iovec *curr_iov;
  524. int curr_iovlen;
  525. char __user *curr_start;
  526. u32 hdr_size;
  527. int curr_left;
  528. int bytes_to_send;
  529. int bytes_sent;
  530. int res;
  531. /* Handle special cases where there is no connection */
  532. if (unlikely(sock->state != SS_CONNECTED)) {
  533. if (sock->state == SS_UNCONNECTED)
  534. return send_packet(iocb, sock, m, total_len);
  535. else if (sock->state == SS_DISCONNECTING)
  536. return -EPIPE;
  537. else
  538. return -ENOTCONN;
  539. }
  540. if (unlikely(m->msg_name))
  541. return -EISCONN;
  542. /*
  543. * Send each iovec entry using one or more messages
  544. *
  545. * Note: This algorithm is good for the most likely case
  546. * (i.e. one large iovec entry), but could be improved to pass sets
  547. * of small iovec entries into send_packet().
  548. */
  549. curr_iov = m->msg_iov;
  550. curr_iovlen = m->msg_iovlen;
  551. my_msg.msg_iov = &my_iov;
  552. my_msg.msg_iovlen = 1;
  553. my_msg.msg_flags = m->msg_flags;
  554. my_msg.msg_name = NULL;
  555. bytes_sent = 0;
  556. tport = tipc_sk(sock->sk)->p;
  557. hdr_size = msg_hdr_sz(&tport->phdr);
  558. while (curr_iovlen--) {
  559. curr_start = curr_iov->iov_base;
  560. curr_left = curr_iov->iov_len;
  561. while (curr_left) {
  562. bytes_to_send = tport->max_pkt - hdr_size;
  563. if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
  564. bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
  565. if (curr_left < bytes_to_send)
  566. bytes_to_send = curr_left;
  567. my_iov.iov_base = curr_start;
  568. my_iov.iov_len = bytes_to_send;
  569. if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0) {
  570. if (bytes_sent != 0)
  571. res = bytes_sent;
  572. return res;
  573. }
  574. curr_left -= bytes_to_send;
  575. curr_start += bytes_to_send;
  576. bytes_sent += bytes_to_send;
  577. }
  578. curr_iov++;
  579. }
  580. return bytes_sent;
  581. }
  582. /**
  583. * auto_connect - complete connection setup to a remote port
  584. * @sock: socket structure
  585. * @tsock: TIPC-specific socket structure
  586. * @msg: peer's response message
  587. *
  588. * Returns 0 on success, errno otherwise
  589. */
  590. static int auto_connect(struct socket *sock, struct tipc_sock *tsock,
  591. struct tipc_msg *msg)
  592. {
  593. struct tipc_portid peer;
  594. if (msg_errcode(msg)) {
  595. sock->state = SS_DISCONNECTING;
  596. return -ECONNREFUSED;
  597. }
  598. peer.ref = msg_origport(msg);
  599. peer.node = msg_orignode(msg);
  600. tipc_connect2port(tsock->p->ref, &peer);
  601. tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
  602. sock->state = SS_CONNECTED;
  603. return 0;
  604. }
  605. /**
  606. * set_orig_addr - capture sender's address for received message
  607. * @m: descriptor for message info
  608. * @msg: received message header
  609. *
  610. * Note: Address is not captured if not requested by receiver.
  611. */
  612. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  613. {
  614. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
  615. if (addr) {
  616. addr->family = AF_TIPC;
  617. addr->addrtype = TIPC_ADDR_ID;
  618. addr->addr.id.ref = msg_origport(msg);
  619. addr->addr.id.node = msg_orignode(msg);
  620. addr->addr.name.domain = 0; /* could leave uninitialized */
  621. addr->scope = 0; /* could leave uninitialized */
  622. m->msg_namelen = sizeof(struct sockaddr_tipc);
  623. }
  624. }
  625. /**
  626. * anc_data_recv - optionally capture ancillary data for received message
  627. * @m: descriptor for message info
  628. * @msg: received message header
  629. * @tport: TIPC port associated with message
  630. *
  631. * Note: Ancillary data is not captured if not requested by receiver.
  632. *
  633. * Returns 0 if successful, otherwise errno
  634. */
  635. static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  636. struct tipc_port *tport)
  637. {
  638. u32 anc_data[3];
  639. u32 err;
  640. u32 dest_type;
  641. int has_name;
  642. int res;
  643. if (likely(m->msg_controllen == 0))
  644. return 0;
  645. /* Optionally capture errored message object(s) */
  646. err = msg ? msg_errcode(msg) : 0;
  647. if (unlikely(err)) {
  648. anc_data[0] = err;
  649. anc_data[1] = msg_data_sz(msg);
  650. if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data)))
  651. return res;
  652. if (anc_data[1] &&
  653. (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  654. msg_data(msg))))
  655. return res;
  656. }
  657. /* Optionally capture message destination object */
  658. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  659. switch (dest_type) {
  660. case TIPC_NAMED_MSG:
  661. has_name = 1;
  662. anc_data[0] = msg_nametype(msg);
  663. anc_data[1] = msg_namelower(msg);
  664. anc_data[2] = msg_namelower(msg);
  665. break;
  666. case TIPC_MCAST_MSG:
  667. has_name = 1;
  668. anc_data[0] = msg_nametype(msg);
  669. anc_data[1] = msg_namelower(msg);
  670. anc_data[2] = msg_nameupper(msg);
  671. break;
  672. case TIPC_CONN_MSG:
  673. has_name = (tport->conn_type != 0);
  674. anc_data[0] = tport->conn_type;
  675. anc_data[1] = tport->conn_instance;
  676. anc_data[2] = tport->conn_instance;
  677. break;
  678. default:
  679. has_name = 0;
  680. }
  681. if (has_name &&
  682. (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data)))
  683. return res;
  684. return 0;
  685. }
  686. /**
  687. * recv_msg - receive packet-oriented message
  688. * @iocb: (unused)
  689. * @m: descriptor for message info
  690. * @buf_len: total size of user buffer area
  691. * @flags: receive flags
  692. *
  693. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  694. * If the complete message doesn't fit in user area, truncate it.
  695. *
  696. * Returns size of returned message data, errno otherwise
  697. */
  698. static int recv_msg(struct kiocb *iocb, struct socket *sock,
  699. struct msghdr *m, size_t buf_len, int flags)
  700. {
  701. struct tipc_sock *tsock = tipc_sk(sock->sk);
  702. struct sk_buff *buf;
  703. struct tipc_msg *msg;
  704. unsigned int q_len;
  705. unsigned int sz;
  706. u32 err;
  707. int res;
  708. /* Currently doesn't support receiving into multiple iovec entries */
  709. if (m->msg_iovlen != 1)
  710. return -EOPNOTSUPP;
  711. /* Catch invalid receive attempts */
  712. if (unlikely(!buf_len))
  713. return -EINVAL;
  714. if (sock->type == SOCK_SEQPACKET) {
  715. if (unlikely(sock->state == SS_UNCONNECTED))
  716. return -ENOTCONN;
  717. if (unlikely((sock->state == SS_DISCONNECTING) &&
  718. (skb_queue_len(&sock->sk->sk_receive_queue) == 0)))
  719. return -ENOTCONN;
  720. }
  721. /* Look for a message in receive queue; wait if necessary */
  722. if (unlikely(mutex_lock_interruptible(&tsock->lock)))
  723. return -ERESTARTSYS;
  724. restart:
  725. if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
  726. (flags & MSG_DONTWAIT))) {
  727. res = -EWOULDBLOCK;
  728. goto exit;
  729. }
  730. if ((res = wait_event_interruptible(
  731. *sock->sk->sk_sleep,
  732. ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
  733. (sock->state == SS_DISCONNECTING))) )) {
  734. goto exit;
  735. }
  736. /* Catch attempt to receive on an already terminated connection */
  737. /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
  738. if (!q_len) {
  739. res = -ENOTCONN;
  740. goto exit;
  741. }
  742. /* Get access to first message in receive queue */
  743. buf = skb_peek(&sock->sk->sk_receive_queue);
  744. msg = buf_msg(buf);
  745. sz = msg_data_sz(msg);
  746. err = msg_errcode(msg);
  747. /* Complete connection setup for an implied connect */
  748. if (unlikely(sock->state == SS_CONNECTING)) {
  749. if ((res = auto_connect(sock, tsock, msg)))
  750. goto exit;
  751. }
  752. /* Discard an empty non-errored message & try again */
  753. if ((!sz) && (!err)) {
  754. advance_queue(tsock);
  755. goto restart;
  756. }
  757. /* Capture sender's address (optional) */
  758. set_orig_addr(m, msg);
  759. /* Capture ancillary data (optional) */
  760. if ((res = anc_data_recv(m, msg, tsock->p)))
  761. goto exit;
  762. /* Capture message data (if valid) & compute return value (always) */
  763. if (!err) {
  764. if (unlikely(buf_len < sz)) {
  765. sz = buf_len;
  766. m->msg_flags |= MSG_TRUNC;
  767. }
  768. if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
  769. sz))) {
  770. res = -EFAULT;
  771. goto exit;
  772. }
  773. res = sz;
  774. } else {
  775. if ((sock->state == SS_READY) ||
  776. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  777. res = 0;
  778. else
  779. res = -ECONNRESET;
  780. }
  781. /* Consume received message (optional) */
  782. if (likely(!(flags & MSG_PEEK))) {
  783. if ((sock->state != SS_READY) &&
  784. (++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  785. tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
  786. advance_queue(tsock);
  787. }
  788. exit:
  789. mutex_unlock(&tsock->lock);
  790. return res;
  791. }
  792. /**
  793. * recv_stream - receive stream-oriented data
  794. * @iocb: (unused)
  795. * @m: descriptor for message info
  796. * @buf_len: total size of user buffer area
  797. * @flags: receive flags
  798. *
  799. * Used for SOCK_STREAM messages only. If not enough data is available
  800. * will optionally wait for more; never truncates data.
  801. *
  802. * Returns size of returned message data, errno otherwise
  803. */
  804. static int recv_stream(struct kiocb *iocb, struct socket *sock,
  805. struct msghdr *m, size_t buf_len, int flags)
  806. {
  807. struct tipc_sock *tsock = tipc_sk(sock->sk);
  808. struct sk_buff *buf;
  809. struct tipc_msg *msg;
  810. unsigned int q_len;
  811. unsigned int sz;
  812. int sz_to_copy;
  813. int sz_copied = 0;
  814. int needed;
  815. char __user *crs = m->msg_iov->iov_base;
  816. unsigned char *buf_crs;
  817. u32 err;
  818. int res;
  819. /* Currently doesn't support receiving into multiple iovec entries */
  820. if (m->msg_iovlen != 1)
  821. return -EOPNOTSUPP;
  822. /* Catch invalid receive attempts */
  823. if (unlikely(!buf_len))
  824. return -EINVAL;
  825. if (unlikely(sock->state == SS_DISCONNECTING)) {
  826. if (skb_queue_len(&sock->sk->sk_receive_queue) == 0)
  827. return -ENOTCONN;
  828. } else if (unlikely(sock->state != SS_CONNECTED))
  829. return -ENOTCONN;
  830. /* Look for a message in receive queue; wait if necessary */
  831. if (unlikely(mutex_lock_interruptible(&tsock->lock)))
  832. return -ERESTARTSYS;
  833. restart:
  834. if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
  835. (flags & MSG_DONTWAIT))) {
  836. res = -EWOULDBLOCK;
  837. goto exit;
  838. }
  839. if ((res = wait_event_interruptible(
  840. *sock->sk->sk_sleep,
  841. ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
  842. (sock->state == SS_DISCONNECTING))) )) {
  843. goto exit;
  844. }
  845. /* Catch attempt to receive on an already terminated connection */
  846. /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
  847. if (!q_len) {
  848. res = -ENOTCONN;
  849. goto exit;
  850. }
  851. /* Get access to first message in receive queue */
  852. buf = skb_peek(&sock->sk->sk_receive_queue);
  853. msg = buf_msg(buf);
  854. sz = msg_data_sz(msg);
  855. err = msg_errcode(msg);
  856. /* Discard an empty non-errored message & try again */
  857. if ((!sz) && (!err)) {
  858. advance_queue(tsock);
  859. goto restart;
  860. }
  861. /* Optionally capture sender's address & ancillary data of first msg */
  862. if (sz_copied == 0) {
  863. set_orig_addr(m, msg);
  864. if ((res = anc_data_recv(m, msg, tsock->p)))
  865. goto exit;
  866. }
  867. /* Capture message data (if valid) & compute return value (always) */
  868. if (!err) {
  869. buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
  870. sz = (unsigned char *)msg + msg_size(msg) - buf_crs;
  871. needed = (buf_len - sz_copied);
  872. sz_to_copy = (sz <= needed) ? sz : needed;
  873. if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
  874. res = -EFAULT;
  875. goto exit;
  876. }
  877. sz_copied += sz_to_copy;
  878. if (sz_to_copy < sz) {
  879. if (!(flags & MSG_PEEK))
  880. TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
  881. goto exit;
  882. }
  883. crs += sz_to_copy;
  884. } else {
  885. if (sz_copied != 0)
  886. goto exit; /* can't add error msg to valid data */
  887. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  888. res = 0;
  889. else
  890. res = -ECONNRESET;
  891. }
  892. /* Consume received message (optional) */
  893. if (likely(!(flags & MSG_PEEK))) {
  894. if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  895. tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
  896. advance_queue(tsock);
  897. }
  898. /* Loop around if more data is required */
  899. if ((sz_copied < buf_len) /* didn't get all requested data */
  900. && (!skb_queue_empty(&sock->sk->sk_receive_queue) ||
  901. (flags & MSG_WAITALL))
  902. /* ... and more is ready or required */
  903. && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
  904. && (!err) /* ... and haven't reached a FIN */
  905. )
  906. goto restart;
  907. exit:
  908. mutex_unlock(&tsock->lock);
  909. return sz_copied ? sz_copied : res;
  910. }
  911. /**
  912. * rx_queue_full - determine if receive queue can accept another message
  913. * @msg: message to be added to queue
  914. * @queue_size: current size of queue
  915. * @base: nominal maximum size of queue
  916. *
  917. * Returns 1 if queue is unable to accept message, 0 otherwise
  918. */
  919. static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
  920. {
  921. u32 threshold;
  922. u32 imp = msg_importance(msg);
  923. if (imp == TIPC_LOW_IMPORTANCE)
  924. threshold = base;
  925. else if (imp == TIPC_MEDIUM_IMPORTANCE)
  926. threshold = base * 2;
  927. else if (imp == TIPC_HIGH_IMPORTANCE)
  928. threshold = base * 100;
  929. else
  930. return 0;
  931. if (msg_connected(msg))
  932. threshold *= 4;
  933. return (queue_size >= threshold);
  934. }
  935. /**
  936. * async_disconnect - wrapper function used to disconnect port
  937. * @portref: TIPC port reference (passed as pointer-sized value)
  938. */
  939. static void async_disconnect(unsigned long portref)
  940. {
  941. tipc_disconnect((u32)portref);
  942. }
  943. /**
  944. * dispatch - handle arriving message
  945. * @tport: TIPC port that received message
  946. * @buf: message
  947. *
  948. * Called with port locked. Must not take socket lock to avoid deadlock risk.
  949. *
  950. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  951. */
  952. static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
  953. {
  954. struct tipc_msg *msg = buf_msg(buf);
  955. struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
  956. struct socket *sock;
  957. u32 recv_q_len;
  958. /* Reject message if socket is closing */
  959. if (!tsock)
  960. return TIPC_ERR_NO_PORT;
  961. /* Reject message if it is wrong sort of message for socket */
  962. /*
  963. * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
  964. * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
  965. * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
  966. */
  967. sock = tsock->sk.sk_socket;
  968. if (sock->state == SS_READY) {
  969. if (msg_connected(msg)) {
  970. msg_dbg(msg, "dispatch filter 1\n");
  971. return TIPC_ERR_NO_PORT;
  972. }
  973. } else {
  974. if (msg_mcast(msg)) {
  975. msg_dbg(msg, "dispatch filter 2\n");
  976. return TIPC_ERR_NO_PORT;
  977. }
  978. if (sock->state == SS_CONNECTED) {
  979. if (!msg_connected(msg)) {
  980. msg_dbg(msg, "dispatch filter 3\n");
  981. return TIPC_ERR_NO_PORT;
  982. }
  983. }
  984. else if (sock->state == SS_CONNECTING) {
  985. if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
  986. msg_dbg(msg, "dispatch filter 4\n");
  987. return TIPC_ERR_NO_PORT;
  988. }
  989. }
  990. else if (sock->state == SS_LISTENING) {
  991. if (msg_connected(msg) || msg_errcode(msg)) {
  992. msg_dbg(msg, "dispatch filter 5\n");
  993. return TIPC_ERR_NO_PORT;
  994. }
  995. }
  996. else if (sock->state == SS_DISCONNECTING) {
  997. msg_dbg(msg, "dispatch filter 6\n");
  998. return TIPC_ERR_NO_PORT;
  999. }
  1000. else /* (sock->state == SS_UNCONNECTED) */ {
  1001. if (msg_connected(msg) || msg_errcode(msg)) {
  1002. msg_dbg(msg, "dispatch filter 7\n");
  1003. return TIPC_ERR_NO_PORT;
  1004. }
  1005. }
  1006. }
  1007. /* Reject message if there isn't room to queue it */
  1008. recv_q_len = (u32)atomic_read(&tipc_queue_size);
  1009. if (unlikely(recv_q_len >= OVERLOAD_LIMIT_BASE)) {
  1010. if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE))
  1011. return TIPC_ERR_OVERLOAD;
  1012. }
  1013. recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue);
  1014. if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
  1015. if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
  1016. return TIPC_ERR_OVERLOAD;
  1017. }
  1018. /* Initiate connection termination for an incoming 'FIN' */
  1019. if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
  1020. sock->state = SS_DISCONNECTING;
  1021. /* Note: Use signal since port lock is already taken! */
  1022. tipc_k_signal((Handler)async_disconnect, tport->ref);
  1023. }
  1024. /* Enqueue message (finally!) */
  1025. msg_dbg(msg,"<DISP<: ");
  1026. TIPC_SKB_CB(buf)->handle = msg_data(msg);
  1027. atomic_inc(&tipc_queue_size);
  1028. skb_queue_tail(&sock->sk->sk_receive_queue, buf);
  1029. if (waitqueue_active(sock->sk->sk_sleep))
  1030. wake_up_interruptible(sock->sk->sk_sleep);
  1031. return TIPC_OK;
  1032. }
  1033. /**
  1034. * wakeupdispatch - wake up port after congestion
  1035. * @tport: port to wakeup
  1036. *
  1037. * Called with port lock on.
  1038. */
  1039. static void wakeupdispatch(struct tipc_port *tport)
  1040. {
  1041. struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
  1042. if (waitqueue_active(tsock->sk.sk_sleep))
  1043. wake_up_interruptible(tsock->sk.sk_sleep);
  1044. }
  1045. /**
  1046. * connect - establish a connection to another TIPC port
  1047. * @sock: socket structure
  1048. * @dest: socket address for destination port
  1049. * @destlen: size of socket address data structure
  1050. * @flags: (unused)
  1051. *
  1052. * Returns 0 on success, errno otherwise
  1053. */
  1054. static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
  1055. int flags)
  1056. {
  1057. struct tipc_sock *tsock = tipc_sk(sock->sk);
  1058. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1059. struct msghdr m = {NULL,};
  1060. struct sk_buff *buf;
  1061. struct tipc_msg *msg;
  1062. int res;
  1063. /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
  1064. if (sock->state == SS_READY)
  1065. return -EOPNOTSUPP;
  1066. /* For now, TIPC does not support the non-blocking form of connect() */
  1067. if (flags & O_NONBLOCK)
  1068. return -EWOULDBLOCK;
  1069. /* Issue Posix-compliant error code if socket is in the wrong state */
  1070. if (sock->state == SS_LISTENING)
  1071. return -EOPNOTSUPP;
  1072. if (sock->state == SS_CONNECTING)
  1073. return -EALREADY;
  1074. if (sock->state != SS_UNCONNECTED)
  1075. return -EISCONN;
  1076. /*
  1077. * Reject connection attempt using multicast address
  1078. *
  1079. * Note: send_msg() validates the rest of the address fields,
  1080. * so there's no need to do it here
  1081. */
  1082. if (dst->addrtype == TIPC_ADDR_MCAST)
  1083. return -EINVAL;
  1084. /* Send a 'SYN-' to destination */
  1085. m.msg_name = dest;
  1086. m.msg_namelen = destlen;
  1087. res = send_msg(NULL, sock, &m, 0);
  1088. if (res < 0) {
  1089. sock->state = SS_DISCONNECTING;
  1090. return res;
  1091. }
  1092. if (mutex_lock_interruptible(&tsock->lock))
  1093. return -ERESTARTSYS;
  1094. /* Wait for destination's 'ACK' response */
  1095. res = wait_event_interruptible_timeout(*sock->sk->sk_sleep,
  1096. skb_queue_len(&sock->sk->sk_receive_queue),
  1097. sock->sk->sk_rcvtimeo);
  1098. buf = skb_peek(&sock->sk->sk_receive_queue);
  1099. if (res > 0) {
  1100. msg = buf_msg(buf);
  1101. res = auto_connect(sock, tsock, msg);
  1102. if (!res) {
  1103. if (!msg_data_sz(msg))
  1104. advance_queue(tsock);
  1105. }
  1106. } else {
  1107. if (res == 0)
  1108. res = -ETIMEDOUT;
  1109. else
  1110. ; /* leave "res" unchanged */
  1111. sock->state = SS_DISCONNECTING;
  1112. }
  1113. mutex_unlock(&tsock->lock);
  1114. return res;
  1115. }
  1116. /**
  1117. * listen - allow socket to listen for incoming connections
  1118. * @sock: socket structure
  1119. * @len: (unused)
  1120. *
  1121. * Returns 0 on success, errno otherwise
  1122. */
  1123. static int listen(struct socket *sock, int len)
  1124. {
  1125. /* REQUIRES SOCKET LOCKING OF SOME SORT? */
  1126. if (sock->state == SS_READY)
  1127. return -EOPNOTSUPP;
  1128. if (sock->state != SS_UNCONNECTED)
  1129. return -EINVAL;
  1130. sock->state = SS_LISTENING;
  1131. return 0;
  1132. }
  1133. /**
  1134. * accept - wait for connection request
  1135. * @sock: listening socket
  1136. * @newsock: new socket that is to be connected
  1137. * @flags: file-related flags associated with socket
  1138. *
  1139. * Returns 0 on success, errno otherwise
  1140. */
  1141. static int accept(struct socket *sock, struct socket *newsock, int flags)
  1142. {
  1143. struct tipc_sock *tsock = tipc_sk(sock->sk);
  1144. struct sk_buff *buf;
  1145. int res = -EFAULT;
  1146. if (sock->state == SS_READY)
  1147. return -EOPNOTSUPP;
  1148. if (sock->state != SS_LISTENING)
  1149. return -EINVAL;
  1150. if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
  1151. (flags & O_NONBLOCK)))
  1152. return -EWOULDBLOCK;
  1153. if (mutex_lock_interruptible(&tsock->lock))
  1154. return -ERESTARTSYS;
  1155. if (wait_event_interruptible(*sock->sk->sk_sleep,
  1156. skb_queue_len(&sock->sk->sk_receive_queue))) {
  1157. res = -ERESTARTSYS;
  1158. goto exit;
  1159. }
  1160. buf = skb_peek(&sock->sk->sk_receive_queue);
  1161. res = tipc_create(sock_net(sock->sk), newsock, 0);
  1162. if (!res) {
  1163. struct tipc_sock *new_tsock = tipc_sk(newsock->sk);
  1164. struct tipc_portid id;
  1165. struct tipc_msg *msg = buf_msg(buf);
  1166. u32 new_ref = new_tsock->p->ref;
  1167. id.ref = msg_origport(msg);
  1168. id.node = msg_orignode(msg);
  1169. tipc_connect2port(new_ref, &id);
  1170. newsock->state = SS_CONNECTED;
  1171. tipc_set_portimportance(new_ref, msg_importance(msg));
  1172. if (msg_named(msg)) {
  1173. new_tsock->p->conn_type = msg_nametype(msg);
  1174. new_tsock->p->conn_instance = msg_nameinst(msg);
  1175. }
  1176. /*
  1177. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1178. * Respond to 'SYN+' by queuing it on new socket.
  1179. */
  1180. msg_dbg(msg,"<ACC<: ");
  1181. if (!msg_data_sz(msg)) {
  1182. struct msghdr m = {NULL,};
  1183. send_packet(NULL, newsock, &m, 0);
  1184. advance_queue(tsock);
  1185. } else {
  1186. sock_lock(tsock);
  1187. skb_dequeue(&sock->sk->sk_receive_queue);
  1188. sock_unlock(tsock);
  1189. skb_queue_head(&newsock->sk->sk_receive_queue, buf);
  1190. }
  1191. }
  1192. exit:
  1193. mutex_unlock(&tsock->lock);
  1194. return res;
  1195. }
  1196. /**
  1197. * shutdown - shutdown socket connection
  1198. * @sock: socket structure
  1199. * @how: direction to close (must be SHUT_RDWR)
  1200. *
  1201. * Terminates connection (if necessary), then purges socket's receive queue.
  1202. *
  1203. * Returns 0 on success, errno otherwise
  1204. */
  1205. static int shutdown(struct socket *sock, int how)
  1206. {
  1207. struct tipc_sock* tsock = tipc_sk(sock->sk);
  1208. struct sk_buff *buf;
  1209. int res;
  1210. if (how != SHUT_RDWR)
  1211. return -EINVAL;
  1212. if (mutex_lock_interruptible(&tsock->lock))
  1213. return -ERESTARTSYS;
  1214. sock_lock(tsock);
  1215. switch (sock->state) {
  1216. case SS_CONNECTED:
  1217. /* Send 'FIN+' or 'FIN-' message to peer */
  1218. sock_unlock(tsock);
  1219. restart:
  1220. if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
  1221. atomic_dec(&tipc_queue_size);
  1222. if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
  1223. buf_discard(buf);
  1224. goto restart;
  1225. }
  1226. tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
  1227. }
  1228. else {
  1229. tipc_shutdown(tsock->p->ref);
  1230. }
  1231. sock_lock(tsock);
  1232. /* fall through */
  1233. case SS_DISCONNECTING:
  1234. /* Discard any unreceived messages */
  1235. while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
  1236. atomic_dec(&tipc_queue_size);
  1237. buf_discard(buf);
  1238. }
  1239. tsock->p->conn_unacked = 0;
  1240. /* fall through */
  1241. case SS_CONNECTING:
  1242. sock->state = SS_DISCONNECTING;
  1243. res = 0;
  1244. break;
  1245. default:
  1246. res = -ENOTCONN;
  1247. }
  1248. sock_unlock(tsock);
  1249. mutex_unlock(&tsock->lock);
  1250. return res;
  1251. }
  1252. /**
  1253. * setsockopt - set socket option
  1254. * @sock: socket structure
  1255. * @lvl: option level
  1256. * @opt: option identifier
  1257. * @ov: pointer to new option value
  1258. * @ol: length of option value
  1259. *
  1260. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  1261. * (to ease compatibility).
  1262. *
  1263. * Returns 0 on success, errno otherwise
  1264. */
  1265. static int setsockopt(struct socket *sock,
  1266. int lvl, int opt, char __user *ov, int ol)
  1267. {
  1268. struct tipc_sock *tsock = tipc_sk(sock->sk);
  1269. u32 value;
  1270. int res;
  1271. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1272. return 0;
  1273. if (lvl != SOL_TIPC)
  1274. return -ENOPROTOOPT;
  1275. if (ol < sizeof(value))
  1276. return -EINVAL;
  1277. if ((res = get_user(value, (u32 __user *)ov)))
  1278. return res;
  1279. if (mutex_lock_interruptible(&tsock->lock))
  1280. return -ERESTARTSYS;
  1281. switch (opt) {
  1282. case TIPC_IMPORTANCE:
  1283. res = tipc_set_portimportance(tsock->p->ref, value);
  1284. break;
  1285. case TIPC_SRC_DROPPABLE:
  1286. if (sock->type != SOCK_STREAM)
  1287. res = tipc_set_portunreliable(tsock->p->ref, value);
  1288. else
  1289. res = -ENOPROTOOPT;
  1290. break;
  1291. case TIPC_DEST_DROPPABLE:
  1292. res = tipc_set_portunreturnable(tsock->p->ref, value);
  1293. break;
  1294. case TIPC_CONN_TIMEOUT:
  1295. sock->sk->sk_rcvtimeo = msecs_to_jiffies(value);
  1296. break;
  1297. default:
  1298. res = -EINVAL;
  1299. }
  1300. mutex_unlock(&tsock->lock);
  1301. return res;
  1302. }
  1303. /**
  1304. * getsockopt - get socket option
  1305. * @sock: socket structure
  1306. * @lvl: option level
  1307. * @opt: option identifier
  1308. * @ov: receptacle for option value
  1309. * @ol: receptacle for length of option value
  1310. *
  1311. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  1312. * (to ease compatibility).
  1313. *
  1314. * Returns 0 on success, errno otherwise
  1315. */
  1316. static int getsockopt(struct socket *sock,
  1317. int lvl, int opt, char __user *ov, int __user *ol)
  1318. {
  1319. struct tipc_sock *tsock = tipc_sk(sock->sk);
  1320. int len;
  1321. u32 value;
  1322. int res;
  1323. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1324. return put_user(0, ol);
  1325. if (lvl != SOL_TIPC)
  1326. return -ENOPROTOOPT;
  1327. if ((res = get_user(len, ol)))
  1328. return res;
  1329. if (mutex_lock_interruptible(&tsock->lock))
  1330. return -ERESTARTSYS;
  1331. switch (opt) {
  1332. case TIPC_IMPORTANCE:
  1333. res = tipc_portimportance(tsock->p->ref, &value);
  1334. break;
  1335. case TIPC_SRC_DROPPABLE:
  1336. res = tipc_portunreliable(tsock->p->ref, &value);
  1337. break;
  1338. case TIPC_DEST_DROPPABLE:
  1339. res = tipc_portunreturnable(tsock->p->ref, &value);
  1340. break;
  1341. case TIPC_CONN_TIMEOUT:
  1342. value = jiffies_to_msecs(sock->sk->sk_rcvtimeo);
  1343. break;
  1344. default:
  1345. res = -EINVAL;
  1346. }
  1347. if (res) {
  1348. /* "get" failed */
  1349. }
  1350. else if (len < sizeof(value)) {
  1351. res = -EINVAL;
  1352. }
  1353. else if ((res = copy_to_user(ov, &value, sizeof(value)))) {
  1354. /* couldn't return value */
  1355. }
  1356. else {
  1357. res = put_user(sizeof(value), ol);
  1358. }
  1359. mutex_unlock(&tsock->lock);
  1360. return res;
  1361. }
  1362. /**
  1363. * Protocol switches for the various types of TIPC sockets
  1364. */
  1365. static const struct proto_ops msg_ops = {
  1366. .owner = THIS_MODULE,
  1367. .family = AF_TIPC,
  1368. .release = release,
  1369. .bind = bind,
  1370. .connect = connect,
  1371. .socketpair = sock_no_socketpair,
  1372. .accept = accept,
  1373. .getname = get_name,
  1374. .poll = poll,
  1375. .ioctl = sock_no_ioctl,
  1376. .listen = listen,
  1377. .shutdown = shutdown,
  1378. .setsockopt = setsockopt,
  1379. .getsockopt = getsockopt,
  1380. .sendmsg = send_msg,
  1381. .recvmsg = recv_msg,
  1382. .mmap = sock_no_mmap,
  1383. .sendpage = sock_no_sendpage
  1384. };
  1385. static const struct proto_ops packet_ops = {
  1386. .owner = THIS_MODULE,
  1387. .family = AF_TIPC,
  1388. .release = release,
  1389. .bind = bind,
  1390. .connect = connect,
  1391. .socketpair = sock_no_socketpair,
  1392. .accept = accept,
  1393. .getname = get_name,
  1394. .poll = poll,
  1395. .ioctl = sock_no_ioctl,
  1396. .listen = listen,
  1397. .shutdown = shutdown,
  1398. .setsockopt = setsockopt,
  1399. .getsockopt = getsockopt,
  1400. .sendmsg = send_packet,
  1401. .recvmsg = recv_msg,
  1402. .mmap = sock_no_mmap,
  1403. .sendpage = sock_no_sendpage
  1404. };
  1405. static const struct proto_ops stream_ops = {
  1406. .owner = THIS_MODULE,
  1407. .family = AF_TIPC,
  1408. .release = release,
  1409. .bind = bind,
  1410. .connect = connect,
  1411. .socketpair = sock_no_socketpair,
  1412. .accept = accept,
  1413. .getname = get_name,
  1414. .poll = poll,
  1415. .ioctl = sock_no_ioctl,
  1416. .listen = listen,
  1417. .shutdown = shutdown,
  1418. .setsockopt = setsockopt,
  1419. .getsockopt = getsockopt,
  1420. .sendmsg = send_stream,
  1421. .recvmsg = recv_stream,
  1422. .mmap = sock_no_mmap,
  1423. .sendpage = sock_no_sendpage
  1424. };
  1425. static const struct net_proto_family tipc_family_ops = {
  1426. .owner = THIS_MODULE,
  1427. .family = AF_TIPC,
  1428. .create = tipc_create
  1429. };
  1430. static struct proto tipc_proto = {
  1431. .name = "TIPC",
  1432. .owner = THIS_MODULE,
  1433. .obj_size = sizeof(struct tipc_sock)
  1434. };
  1435. /**
  1436. * tipc_socket_init - initialize TIPC socket interface
  1437. *
  1438. * Returns 0 on success, errno otherwise
  1439. */
  1440. int tipc_socket_init(void)
  1441. {
  1442. int res;
  1443. res = proto_register(&tipc_proto, 1);
  1444. if (res) {
  1445. err("Failed to register TIPC protocol type\n");
  1446. goto out;
  1447. }
  1448. res = sock_register(&tipc_family_ops);
  1449. if (res) {
  1450. err("Failed to register TIPC socket type\n");
  1451. proto_unregister(&tipc_proto);
  1452. goto out;
  1453. }
  1454. sockets_enabled = 1;
  1455. out:
  1456. return res;
  1457. }
  1458. /**
  1459. * tipc_socket_stop - stop TIPC socket interface
  1460. */
  1461. void tipc_socket_stop(void)
  1462. {
  1463. if (!sockets_enabled)
  1464. return;
  1465. sockets_enabled = 0;
  1466. sock_unregister(tipc_family_ops.family);
  1467. proto_unregister(&tipc_proto);
  1468. }