socket.c 43 KB

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