af_unix.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127
  1. /*
  2. * NET4: Implementation of BSD Unix domain sockets.
  3. *
  4. * Authors: Alan Cox, <alan.cox@linux.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Version: $Id: af_unix.c,v 1.1.1.1 2007/06/12 07:27:14 eyryu Exp $
  12. *
  13. * Fixes:
  14. * Linus Torvalds : Assorted bug cures.
  15. * Niibe Yutaka : async I/O support.
  16. * Carsten Paeth : PF_UNIX check, address fixes.
  17. * Alan Cox : Limit size of allocated blocks.
  18. * Alan Cox : Fixed the stupid socketpair bug.
  19. * Alan Cox : BSD compatibility fine tuning.
  20. * Alan Cox : Fixed a bug in connect when interrupted.
  21. * Alan Cox : Sorted out a proper draft version of
  22. * file descriptor passing hacked up from
  23. * Mike Shaver's work.
  24. * Marty Leisner : Fixes to fd passing
  25. * Nick Nevin : recvmsg bugfix.
  26. * Alan Cox : Started proper garbage collector
  27. * Heiko EiBfeldt : Missing verify_area check
  28. * Alan Cox : Started POSIXisms
  29. * Andreas Schwab : Replace inode by dentry for proper
  30. * reference counting
  31. * Kirk Petersen : Made this a module
  32. * Christoph Rohland : Elegant non-blocking accept/connect algorithm.
  33. * Lots of bug fixes.
  34. * Alexey Kuznetosv : Repaired (I hope) bugs introduces
  35. * by above two patches.
  36. * Andrea Arcangeli : If possible we block in connect(2)
  37. * if the max backlog of the listen socket
  38. * is been reached. This won't break
  39. * old apps and it will avoid huge amount
  40. * of socks hashed (this for unix_gc()
  41. * performances reasons).
  42. * Security fix that limits the max
  43. * number of socks to 2*max_files and
  44. * the number of skb queueable in the
  45. * dgram receiver.
  46. * Artur Skawina : Hash function optimizations
  47. * Alexey Kuznetsov : Full scale SMP. Lot of bugs are introduced 8)
  48. * Malcolm Beattie : Set peercred for socketpair
  49. * Michal Ostrowski : Module initialization cleanup.
  50. * Arnaldo C. Melo : Remove MOD_{INC,DEC}_USE_COUNT,
  51. * the core infrastructure is doing that
  52. * for all net proto families now (2.5.69+)
  53. *
  54. *
  55. * Known differences from reference BSD that was tested:
  56. *
  57. * [TO FIX]
  58. * ECONNREFUSED is not returned from one end of a connected() socket to the
  59. * other the moment one end closes.
  60. * fstat() doesn't return st_dev=0, and give the blksize as high water mark
  61. * and a fake inode identifier (nor the BSD first socket fstat twice bug).
  62. * [NOT TO FIX]
  63. * accept() returns a path name even if the connecting socket has closed
  64. * in the meantime (BSD loses the path and gives up).
  65. * accept() returns 0 length path for an unbound connector. BSD returns 16
  66. * and a null first byte in the path (but not for gethost/peername - BSD bug ??)
  67. * socketpair(...SOCK_RAW..) doesn't panic the kernel.
  68. * BSD af_unix apparently has connect forgetting to block properly.
  69. * (need to check this with the POSIX spec in detail)
  70. *
  71. * Differences from 2.0.0-11-... (ANK)
  72. * Bug fixes and improvements.
  73. * - client shutdown killed server socket.
  74. * - removed all useless cli/sti pairs.
  75. *
  76. * Semantic changes/extensions.
  77. * - generic control message passing.
  78. * - SCM_CREDENTIALS control message.
  79. * - "Abstract" (not FS based) socket bindings.
  80. * Abstract names are sequences of bytes (not zero terminated)
  81. * started by 0, so that this name space does not intersect
  82. * with BSD names.
  83. */
  84. #include <linux/module.h>
  85. #include <linux/kernel.h>
  86. #include <linux/signal.h>
  87. #include <linux/sched.h>
  88. #include <linux/errno.h>
  89. #include <linux/string.h>
  90. #include <linux/stat.h>
  91. #include <linux/dcache.h>
  92. #include <linux/namei.h>
  93. #include <linux/socket.h>
  94. #include <linux/un.h>
  95. #include <linux/fcntl.h>
  96. #include <linux/termios.h>
  97. #include <linux/sockios.h>
  98. #include <linux/net.h>
  99. #include <linux/in.h>
  100. #include <linux/fs.h>
  101. #include <linux/slab.h>
  102. #include <asm/uaccess.h>
  103. #include <linux/skbuff.h>
  104. #include <linux/netdevice.h>
  105. #include <net/sock.h>
  106. #include <net/tcp_states.h>
  107. #include <net/af_unix.h>
  108. #include <linux/proc_fs.h>
  109. #include <linux/seq_file.h>
  110. #include <net/scm.h>
  111. #include <linux/init.h>
  112. #include <linux/poll.h>
  113. #include <linux/smp_lock.h>
  114. #include <linux/rtnetlink.h>
  115. #include <linux/mount.h>
  116. #include <net/checksum.h>
  117. #include <linux/security.h>
  118. int sysctl_unix_max_dgram_qlen __read_mostly = 10;
  119. struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1];
  120. DEFINE_SPINLOCK(unix_table_lock);
  121. static atomic_t unix_nr_socks = ATOMIC_INIT(0);
  122. #define unix_sockets_unbound (&unix_socket_table[UNIX_HASH_SIZE])
  123. #define UNIX_ABSTRACT(sk) (unix_sk(sk)->addr->hash != UNIX_HASH_SIZE)
  124. #ifdef CONFIG_SECURITY_NETWORK
  125. static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  126. {
  127. memcpy(UNIXSID(skb), &scm->secid, sizeof(u32));
  128. }
  129. static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  130. {
  131. scm->secid = *UNIXSID(skb);
  132. }
  133. #else
  134. static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  135. { }
  136. static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  137. { }
  138. #endif /* CONFIG_SECURITY_NETWORK */
  139. /*
  140. * SMP locking strategy:
  141. * hash table is protected with spinlock unix_table_lock
  142. * each socket state is protected by separate rwlock.
  143. */
  144. static inline unsigned unix_hash_fold(__wsum n)
  145. {
  146. unsigned hash = (__force unsigned)n;
  147. hash ^= hash>>16;
  148. hash ^= hash>>8;
  149. return hash&(UNIX_HASH_SIZE-1);
  150. }
  151. #define unix_peer(sk) (unix_sk(sk)->peer)
  152. static inline int unix_our_peer(struct sock *sk, struct sock *osk)
  153. {
  154. return unix_peer(osk) == sk;
  155. }
  156. static inline int unix_may_send(struct sock *sk, struct sock *osk)
  157. {
  158. return (unix_peer(osk) == NULL || unix_our_peer(sk, osk));
  159. }
  160. static struct sock *unix_peer_get(struct sock *s)
  161. {
  162. struct sock *peer;
  163. unix_state_lock(s);
  164. peer = unix_peer(s);
  165. if (peer)
  166. sock_hold(peer);
  167. unix_state_unlock(s);
  168. return peer;
  169. }
  170. static inline void unix_release_addr(struct unix_address *addr)
  171. {
  172. if (atomic_dec_and_test(&addr->refcnt))
  173. kfree(addr);
  174. }
  175. /*
  176. * Check unix socket name:
  177. * - should be not zero length.
  178. * - if started by not zero, should be NULL terminated (FS object)
  179. * - if started by zero, it is abstract name.
  180. */
  181. static int unix_mkname(struct sockaddr_un * sunaddr, int len, unsigned *hashp)
  182. {
  183. if (len <= sizeof(short) || len > sizeof(*sunaddr))
  184. return -EINVAL;
  185. if (!sunaddr || sunaddr->sun_family != AF_UNIX)
  186. return -EINVAL;
  187. if (sunaddr->sun_path[0]) {
  188. /*
  189. * This may look like an off by one error but it is a bit more
  190. * subtle. 108 is the longest valid AF_UNIX path for a binding.
  191. * sun_path[108] doesnt as such exist. However in kernel space
  192. * we are guaranteed that it is a valid memory location in our
  193. * kernel address buffer.
  194. */
  195. ((char *)sunaddr)[len]=0;
  196. len = strlen(sunaddr->sun_path)+1+sizeof(short);
  197. return len;
  198. }
  199. *hashp = unix_hash_fold(csum_partial((char*)sunaddr, len, 0));
  200. return len;
  201. }
  202. static void __unix_remove_socket(struct sock *sk)
  203. {
  204. sk_del_node_init(sk);
  205. }
  206. static void __unix_insert_socket(struct hlist_head *list, struct sock *sk)
  207. {
  208. BUG_TRAP(sk_unhashed(sk));
  209. sk_add_node(sk, list);
  210. }
  211. static inline void unix_remove_socket(struct sock *sk)
  212. {
  213. spin_lock(&unix_table_lock);
  214. __unix_remove_socket(sk);
  215. spin_unlock(&unix_table_lock);
  216. }
  217. static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk)
  218. {
  219. spin_lock(&unix_table_lock);
  220. __unix_insert_socket(list, sk);
  221. spin_unlock(&unix_table_lock);
  222. }
  223. static struct sock *__unix_find_socket_byname(struct sockaddr_un *sunname,
  224. int len, int type, unsigned hash)
  225. {
  226. struct sock *s;
  227. struct hlist_node *node;
  228. sk_for_each(s, node, &unix_socket_table[hash ^ type]) {
  229. struct unix_sock *u = unix_sk(s);
  230. if (u->addr->len == len &&
  231. !memcmp(u->addr->name, sunname, len))
  232. goto found;
  233. }
  234. s = NULL;
  235. found:
  236. return s;
  237. }
  238. static inline struct sock *unix_find_socket_byname(struct sockaddr_un *sunname,
  239. int len, int type,
  240. unsigned hash)
  241. {
  242. struct sock *s;
  243. spin_lock(&unix_table_lock);
  244. s = __unix_find_socket_byname(sunname, len, type, hash);
  245. if (s)
  246. sock_hold(s);
  247. spin_unlock(&unix_table_lock);
  248. return s;
  249. }
  250. static struct sock *unix_find_socket_byinode(struct inode *i)
  251. {
  252. struct sock *s;
  253. struct hlist_node *node;
  254. spin_lock(&unix_table_lock);
  255. sk_for_each(s, node,
  256. &unix_socket_table[i->i_ino & (UNIX_HASH_SIZE - 1)]) {
  257. struct dentry *dentry = unix_sk(s)->dentry;
  258. if(dentry && dentry->d_inode == i)
  259. {
  260. sock_hold(s);
  261. goto found;
  262. }
  263. }
  264. s = NULL;
  265. found:
  266. spin_unlock(&unix_table_lock);
  267. return s;
  268. }
  269. static inline int unix_writable(struct sock *sk)
  270. {
  271. return (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
  272. }
  273. static void unix_write_space(struct sock *sk)
  274. {
  275. read_lock(&sk->sk_callback_lock);
  276. if (unix_writable(sk)) {
  277. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  278. wake_up_interruptible(sk->sk_sleep);
  279. sk_wake_async(sk, 2, POLL_OUT);
  280. }
  281. read_unlock(&sk->sk_callback_lock);
  282. }
  283. /* When dgram socket disconnects (or changes its peer), we clear its receive
  284. * queue of packets arrived from previous peer. First, it allows to do
  285. * flow control based only on wmem_alloc; second, sk connected to peer
  286. * may receive messages only from that peer. */
  287. static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
  288. {
  289. if (!skb_queue_empty(&sk->sk_receive_queue)) {
  290. skb_queue_purge(&sk->sk_receive_queue);
  291. wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
  292. /* If one link of bidirectional dgram pipe is disconnected,
  293. * we signal error. Messages are lost. Do not make this,
  294. * when peer was not connected to us.
  295. */
  296. if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) {
  297. other->sk_err = ECONNRESET;
  298. other->sk_error_report(other);
  299. }
  300. }
  301. }
  302. static void unix_sock_destructor(struct sock *sk)
  303. {
  304. struct unix_sock *u = unix_sk(sk);
  305. skb_queue_purge(&sk->sk_receive_queue);
  306. BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
  307. BUG_TRAP(sk_unhashed(sk));
  308. BUG_TRAP(!sk->sk_socket);
  309. if (!sock_flag(sk, SOCK_DEAD)) {
  310. printk("Attempt to release alive unix socket: %p\n", sk);
  311. return;
  312. }
  313. if (u->addr)
  314. unix_release_addr(u->addr);
  315. atomic_dec(&unix_nr_socks);
  316. #ifdef UNIX_REFCNT_DEBUG
  317. printk(KERN_DEBUG "UNIX %p is destroyed, %d are still alive.\n", sk, atomic_read(&unix_nr_socks));
  318. #endif
  319. }
  320. static int unix_release_sock (struct sock *sk, int embrion)
  321. {
  322. struct unix_sock *u = unix_sk(sk);
  323. struct dentry *dentry;
  324. struct vfsmount *mnt;
  325. struct sock *skpair;
  326. struct sk_buff *skb;
  327. int state;
  328. unix_remove_socket(sk);
  329. /* Clear state */
  330. unix_state_lock(sk);
  331. sock_orphan(sk);
  332. sk->sk_shutdown = SHUTDOWN_MASK;
  333. dentry = u->dentry;
  334. u->dentry = NULL;
  335. mnt = u->mnt;
  336. u->mnt = NULL;
  337. state = sk->sk_state;
  338. sk->sk_state = TCP_CLOSE;
  339. unix_state_unlock(sk);
  340. wake_up_interruptible_all(&u->peer_wait);
  341. skpair=unix_peer(sk);
  342. if (skpair!=NULL) {
  343. if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
  344. unix_state_lock(skpair);
  345. /* No more writes */
  346. skpair->sk_shutdown = SHUTDOWN_MASK;
  347. if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
  348. skpair->sk_err = ECONNRESET;
  349. unix_state_unlock(skpair);
  350. skpair->sk_state_change(skpair);
  351. read_lock(&skpair->sk_callback_lock);
  352. sk_wake_async(skpair,1,POLL_HUP);
  353. read_unlock(&skpair->sk_callback_lock);
  354. }
  355. sock_put(skpair); /* It may now die */
  356. unix_peer(sk) = NULL;
  357. }
  358. /* Try to flush out this socket. Throw out buffers at least */
  359. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  360. if (state==TCP_LISTEN)
  361. unix_release_sock(skb->sk, 1);
  362. /* passed fds are erased in the kfree_skb hook */
  363. kfree_skb(skb);
  364. }
  365. if (dentry) {
  366. dput(dentry);
  367. mntput(mnt);
  368. }
  369. sock_put(sk);
  370. /* ---- Socket is dead now and most probably destroyed ---- */
  371. /*
  372. * Fixme: BSD difference: In BSD all sockets connected to use get
  373. * ECONNRESET and we die on the spot. In Linux we behave
  374. * like files and pipes do and wait for the last
  375. * dereference.
  376. *
  377. * Can't we simply set sock->err?
  378. *
  379. * What the above comment does talk about? --ANK(980817)
  380. */
  381. if (atomic_read(&unix_tot_inflight))
  382. unix_gc(); /* Garbage collect fds */
  383. return 0;
  384. }
  385. static int unix_listen(struct socket *sock, int backlog)
  386. {
  387. int err;
  388. struct sock *sk = sock->sk;
  389. struct unix_sock *u = unix_sk(sk);
  390. err = -EOPNOTSUPP;
  391. if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
  392. goto out; /* Only stream/seqpacket sockets accept */
  393. err = -EINVAL;
  394. if (!u->addr)
  395. goto out; /* No listens on an unbound socket */
  396. unix_state_lock(sk);
  397. if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
  398. goto out_unlock;
  399. if (backlog > sk->sk_max_ack_backlog)
  400. wake_up_interruptible_all(&u->peer_wait);
  401. sk->sk_max_ack_backlog = backlog;
  402. sk->sk_state = TCP_LISTEN;
  403. /* set credentials so connect can copy them */
  404. sk->sk_peercred.pid = current->tgid;
  405. sk->sk_peercred.uid = current->euid;
  406. sk->sk_peercred.gid = current->egid;
  407. err = 0;
  408. out_unlock:
  409. unix_state_unlock(sk);
  410. out:
  411. return err;
  412. }
  413. static int unix_release(struct socket *);
  414. static int unix_bind(struct socket *, struct sockaddr *, int);
  415. static int unix_stream_connect(struct socket *, struct sockaddr *,
  416. int addr_len, int flags);
  417. static int unix_socketpair(struct socket *, struct socket *);
  418. static int unix_accept(struct socket *, struct socket *, int);
  419. static int unix_getname(struct socket *, struct sockaddr *, int *, int);
  420. static unsigned int unix_poll(struct file *, struct socket *, poll_table *);
  421. static int unix_ioctl(struct socket *, unsigned int, unsigned long);
  422. static int unix_shutdown(struct socket *, int);
  423. static int unix_stream_sendmsg(struct kiocb *, struct socket *,
  424. struct msghdr *, size_t);
  425. static int unix_stream_recvmsg(struct kiocb *, struct socket *,
  426. struct msghdr *, size_t, int);
  427. static int unix_dgram_sendmsg(struct kiocb *, struct socket *,
  428. struct msghdr *, size_t);
  429. static int unix_dgram_recvmsg(struct kiocb *, struct socket *,
  430. struct msghdr *, size_t, int);
  431. static int unix_dgram_connect(struct socket *, struct sockaddr *,
  432. int, int);
  433. static int unix_seqpacket_sendmsg(struct kiocb *, struct socket *,
  434. struct msghdr *, size_t);
  435. static const struct proto_ops unix_stream_ops = {
  436. .family = PF_UNIX,
  437. .owner = THIS_MODULE,
  438. .release = unix_release,
  439. .bind = unix_bind,
  440. .connect = unix_stream_connect,
  441. .socketpair = unix_socketpair,
  442. .accept = unix_accept,
  443. .getname = unix_getname,
  444. .poll = unix_poll,
  445. .ioctl = unix_ioctl,
  446. .listen = unix_listen,
  447. .shutdown = unix_shutdown,
  448. .setsockopt = sock_no_setsockopt,
  449. .getsockopt = sock_no_getsockopt,
  450. .sendmsg = unix_stream_sendmsg,
  451. .recvmsg = unix_stream_recvmsg,
  452. .mmap = sock_no_mmap,
  453. .sendpage = sock_no_sendpage,
  454. };
  455. static const struct proto_ops unix_dgram_ops = {
  456. .family = PF_UNIX,
  457. .owner = THIS_MODULE,
  458. .release = unix_release,
  459. .bind = unix_bind,
  460. .connect = unix_dgram_connect,
  461. .socketpair = unix_socketpair,
  462. .accept = sock_no_accept,
  463. .getname = unix_getname,
  464. .poll = datagram_poll,
  465. .ioctl = unix_ioctl,
  466. .listen = sock_no_listen,
  467. .shutdown = unix_shutdown,
  468. .setsockopt = sock_no_setsockopt,
  469. .getsockopt = sock_no_getsockopt,
  470. .sendmsg = unix_dgram_sendmsg,
  471. .recvmsg = unix_dgram_recvmsg,
  472. .mmap = sock_no_mmap,
  473. .sendpage = sock_no_sendpage,
  474. };
  475. static const struct proto_ops unix_seqpacket_ops = {
  476. .family = PF_UNIX,
  477. .owner = THIS_MODULE,
  478. .release = unix_release,
  479. .bind = unix_bind,
  480. .connect = unix_stream_connect,
  481. .socketpair = unix_socketpair,
  482. .accept = unix_accept,
  483. .getname = unix_getname,
  484. .poll = datagram_poll,
  485. .ioctl = unix_ioctl,
  486. .listen = unix_listen,
  487. .shutdown = unix_shutdown,
  488. .setsockopt = sock_no_setsockopt,
  489. .getsockopt = sock_no_getsockopt,
  490. .sendmsg = unix_seqpacket_sendmsg,
  491. .recvmsg = unix_dgram_recvmsg,
  492. .mmap = sock_no_mmap,
  493. .sendpage = sock_no_sendpage,
  494. };
  495. static struct proto unix_proto = {
  496. .name = "UNIX",
  497. .owner = THIS_MODULE,
  498. .obj_size = sizeof(struct unix_sock),
  499. };
  500. /*
  501. * AF_UNIX sockets do not interact with hardware, hence they
  502. * dont trigger interrupts - so it's safe for them to have
  503. * bh-unsafe locking for their sk_receive_queue.lock. Split off
  504. * this special lock-class by reinitializing the spinlock key:
  505. */
  506. static struct lock_class_key af_unix_sk_receive_queue_lock_key;
  507. static struct sock * unix_create1(struct socket *sock)
  508. {
  509. struct sock *sk = NULL;
  510. struct unix_sock *u;
  511. if (atomic_read(&unix_nr_socks) >= 2*get_max_files())
  512. goto out;
  513. sk = sk_alloc(PF_UNIX, GFP_KERNEL, &unix_proto, 1);
  514. if (!sk)
  515. goto out;
  516. atomic_inc(&unix_nr_socks);
  517. sock_init_data(sock,sk);
  518. lockdep_set_class(&sk->sk_receive_queue.lock,
  519. &af_unix_sk_receive_queue_lock_key);
  520. sk->sk_write_space = unix_write_space;
  521. sk->sk_max_ack_backlog = sysctl_unix_max_dgram_qlen;
  522. sk->sk_destruct = unix_sock_destructor;
  523. u = unix_sk(sk);
  524. u->dentry = NULL;
  525. u->mnt = NULL;
  526. spin_lock_init(&u->lock);
  527. atomic_set(&u->inflight, sock ? 0 : -1);
  528. mutex_init(&u->readlock); /* single task reading lock */
  529. init_waitqueue_head(&u->peer_wait);
  530. unix_insert_socket(unix_sockets_unbound, sk);
  531. out:
  532. return sk;
  533. }
  534. static int unix_create(struct socket *sock, int protocol)
  535. {
  536. if (protocol && protocol != PF_UNIX)
  537. return -EPROTONOSUPPORT;
  538. sock->state = SS_UNCONNECTED;
  539. switch (sock->type) {
  540. case SOCK_STREAM:
  541. sock->ops = &unix_stream_ops;
  542. break;
  543. /*
  544. * Believe it or not BSD has AF_UNIX, SOCK_RAW though
  545. * nothing uses it.
  546. */
  547. case SOCK_RAW:
  548. sock->type=SOCK_DGRAM;
  549. case SOCK_DGRAM:
  550. sock->ops = &unix_dgram_ops;
  551. break;
  552. case SOCK_SEQPACKET:
  553. sock->ops = &unix_seqpacket_ops;
  554. break;
  555. default:
  556. return -ESOCKTNOSUPPORT;
  557. }
  558. return unix_create1(sock) ? 0 : -ENOMEM;
  559. }
  560. static int unix_release(struct socket *sock)
  561. {
  562. struct sock *sk = sock->sk;
  563. if (!sk)
  564. return 0;
  565. sock->sk = NULL;
  566. return unix_release_sock (sk, 0);
  567. }
  568. static int unix_autobind(struct socket *sock)
  569. {
  570. struct sock *sk = sock->sk;
  571. struct unix_sock *u = unix_sk(sk);
  572. static u32 ordernum = 1;
  573. struct unix_address * addr;
  574. int err;
  575. mutex_lock(&u->readlock);
  576. err = 0;
  577. if (u->addr)
  578. goto out;
  579. err = -ENOMEM;
  580. addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL);
  581. if (!addr)
  582. goto out;
  583. addr->name->sun_family = AF_UNIX;
  584. atomic_set(&addr->refcnt, 1);
  585. retry:
  586. addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
  587. addr->hash = unix_hash_fold(csum_partial((void*)addr->name, addr->len, 0));
  588. spin_lock(&unix_table_lock);
  589. ordernum = (ordernum+1)&0xFFFFF;
  590. if (__unix_find_socket_byname(addr->name, addr->len, sock->type,
  591. addr->hash)) {
  592. spin_unlock(&unix_table_lock);
  593. /* Sanity yield. It is unusual case, but yet... */
  594. if (!(ordernum&0xFF))
  595. yield();
  596. goto retry;
  597. }
  598. addr->hash ^= sk->sk_type;
  599. __unix_remove_socket(sk);
  600. u->addr = addr;
  601. __unix_insert_socket(&unix_socket_table[addr->hash], sk);
  602. spin_unlock(&unix_table_lock);
  603. err = 0;
  604. out: mutex_unlock(&u->readlock);
  605. return err;
  606. }
  607. static struct sock *unix_find_other(struct sockaddr_un *sunname, int len,
  608. int type, unsigned hash, int *error)
  609. {
  610. struct sock *u;
  611. struct nameidata nd;
  612. int err = 0;
  613. if (sunname->sun_path[0]) {
  614. err = path_lookup(sunname->sun_path, LOOKUP_FOLLOW, &nd);
  615. if (err)
  616. goto fail;
  617. err = vfs_permission(&nd, MAY_WRITE);
  618. if (err)
  619. goto put_fail;
  620. err = -ECONNREFUSED;
  621. if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
  622. goto put_fail;
  623. u=unix_find_socket_byinode(nd.dentry->d_inode);
  624. if (!u)
  625. goto put_fail;
  626. if (u->sk_type == type)
  627. touch_atime(nd.mnt, nd.dentry);
  628. path_release(&nd);
  629. err=-EPROTOTYPE;
  630. if (u->sk_type != type) {
  631. sock_put(u);
  632. goto fail;
  633. }
  634. } else {
  635. err = -ECONNREFUSED;
  636. u=unix_find_socket_byname(sunname, len, type, hash);
  637. if (u) {
  638. struct dentry *dentry;
  639. dentry = unix_sk(u)->dentry;
  640. if (dentry)
  641. touch_atime(unix_sk(u)->mnt, dentry);
  642. } else
  643. goto fail;
  644. }
  645. return u;
  646. put_fail:
  647. path_release(&nd);
  648. fail:
  649. *error=err;
  650. return NULL;
  651. }
  652. static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  653. {
  654. struct sock *sk = sock->sk;
  655. struct unix_sock *u = unix_sk(sk);
  656. struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
  657. struct dentry * dentry = NULL;
  658. struct nameidata nd;
  659. int err;
  660. unsigned hash;
  661. struct unix_address *addr;
  662. struct hlist_head *list;
  663. err = -EINVAL;
  664. if (sunaddr->sun_family != AF_UNIX)
  665. goto out;
  666. if (addr_len==sizeof(short)) {
  667. err = unix_autobind(sock);
  668. goto out;
  669. }
  670. err = unix_mkname(sunaddr, addr_len, &hash);
  671. if (err < 0)
  672. goto out;
  673. addr_len = err;
  674. mutex_lock(&u->readlock);
  675. err = -EINVAL;
  676. if (u->addr)
  677. goto out_up;
  678. err = -ENOMEM;
  679. addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
  680. if (!addr)
  681. goto out_up;
  682. memcpy(addr->name, sunaddr, addr_len);
  683. addr->len = addr_len;
  684. addr->hash = hash ^ sk->sk_type;
  685. atomic_set(&addr->refcnt, 1);
  686. if (sunaddr->sun_path[0]) {
  687. unsigned int mode;
  688. err = 0;
  689. /*
  690. * Get the parent directory, calculate the hash for last
  691. * component.
  692. */
  693. err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
  694. if (err)
  695. goto out_mknod_parent;
  696. dentry = lookup_create(&nd, 0);
  697. err = PTR_ERR(dentry);
  698. if (IS_ERR(dentry))
  699. goto out_mknod_unlock;
  700. /*
  701. * All right, let's create it.
  702. */
  703. mode = S_IFSOCK |
  704. (SOCK_INODE(sock)->i_mode & ~current->fs->umask);
  705. err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
  706. if (err)
  707. goto out_mknod_dput;
  708. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  709. dput(nd.dentry);
  710. nd.dentry = dentry;
  711. addr->hash = UNIX_HASH_SIZE;
  712. }
  713. spin_lock(&unix_table_lock);
  714. if (!sunaddr->sun_path[0]) {
  715. err = -EADDRINUSE;
  716. if (__unix_find_socket_byname(sunaddr, addr_len,
  717. sk->sk_type, hash)) {
  718. unix_release_addr(addr);
  719. goto out_unlock;
  720. }
  721. list = &unix_socket_table[addr->hash];
  722. } else {
  723. list = &unix_socket_table[dentry->d_inode->i_ino & (UNIX_HASH_SIZE-1)];
  724. u->dentry = nd.dentry;
  725. u->mnt = nd.mnt;
  726. }
  727. err = 0;
  728. __unix_remove_socket(sk);
  729. u->addr = addr;
  730. __unix_insert_socket(list, sk);
  731. out_unlock:
  732. spin_unlock(&unix_table_lock);
  733. out_up:
  734. mutex_unlock(&u->readlock);
  735. out:
  736. return err;
  737. out_mknod_dput:
  738. dput(dentry);
  739. out_mknod_unlock:
  740. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  741. path_release(&nd);
  742. out_mknod_parent:
  743. if (err==-EEXIST)
  744. err=-EADDRINUSE;
  745. unix_release_addr(addr);
  746. goto out_up;
  747. }
  748. static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)
  749. {
  750. if (unlikely(sk1 == sk2) || !sk2) {
  751. unix_state_lock(sk1);
  752. return;
  753. }
  754. if (sk1 < sk2) {
  755. unix_state_lock(sk1);
  756. unix_state_lock_nested(sk2);
  757. } else {
  758. unix_state_lock(sk2);
  759. unix_state_lock_nested(sk1);
  760. }
  761. }
  762. static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2)
  763. {
  764. if (unlikely(sk1 == sk2) || !sk2) {
  765. unix_state_unlock(sk1);
  766. return;
  767. }
  768. unix_state_unlock(sk1);
  769. unix_state_unlock(sk2);
  770. }
  771. static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
  772. int alen, int flags)
  773. {
  774. struct sock *sk = sock->sk;
  775. struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
  776. struct sock *other;
  777. unsigned hash;
  778. int err;
  779. if (addr->sa_family != AF_UNSPEC) {
  780. err = unix_mkname(sunaddr, alen, &hash);
  781. if (err < 0)
  782. goto out;
  783. alen = err;
  784. if (test_bit(SOCK_PASSCRED, &sock->flags) &&
  785. !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0)
  786. goto out;
  787. restart:
  788. other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
  789. if (!other)
  790. goto out;
  791. unix_state_double_lock(sk, other);
  792. /* Apparently VFS overslept socket death. Retry. */
  793. if (sock_flag(other, SOCK_DEAD)) {
  794. unix_state_double_unlock(sk, other);
  795. sock_put(other);
  796. goto restart;
  797. }
  798. err = -EPERM;
  799. if (!unix_may_send(sk, other))
  800. goto out_unlock;
  801. err = security_unix_may_send(sk->sk_socket, other->sk_socket);
  802. if (err)
  803. goto out_unlock;
  804. } else {
  805. /*
  806. * 1003.1g breaking connected state with AF_UNSPEC
  807. */
  808. other = NULL;
  809. unix_state_double_lock(sk, other);
  810. }
  811. /*
  812. * If it was connected, reconnect.
  813. */
  814. if (unix_peer(sk)) {
  815. struct sock *old_peer = unix_peer(sk);
  816. unix_peer(sk)=other;
  817. unix_state_double_unlock(sk, other);
  818. if (other != old_peer)
  819. unix_dgram_disconnected(sk, old_peer);
  820. sock_put(old_peer);
  821. } else {
  822. unix_peer(sk)=other;
  823. unix_state_double_unlock(sk, other);
  824. }
  825. return 0;
  826. out_unlock:
  827. unix_state_double_unlock(sk, other);
  828. sock_put(other);
  829. out:
  830. return err;
  831. }
  832. static long unix_wait_for_peer(struct sock *other, long timeo)
  833. {
  834. struct unix_sock *u = unix_sk(other);
  835. int sched;
  836. DEFINE_WAIT(wait);
  837. prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
  838. sched = !sock_flag(other, SOCK_DEAD) &&
  839. !(other->sk_shutdown & RCV_SHUTDOWN) &&
  840. (skb_queue_len(&other->sk_receive_queue) >
  841. other->sk_max_ack_backlog);
  842. unix_state_unlock(other);
  843. if (sched)
  844. timeo = schedule_timeout(timeo);
  845. finish_wait(&u->peer_wait, &wait);
  846. return timeo;
  847. }
  848. static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
  849. int addr_len, int flags)
  850. {
  851. struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
  852. struct sock *sk = sock->sk;
  853. struct unix_sock *u = unix_sk(sk), *newu, *otheru;
  854. struct sock *newsk = NULL;
  855. struct sock *other = NULL;
  856. struct sk_buff *skb = NULL;
  857. unsigned hash;
  858. int st;
  859. int err;
  860. long timeo;
  861. err = unix_mkname(sunaddr, addr_len, &hash);
  862. if (err < 0)
  863. goto out;
  864. addr_len = err;
  865. if (test_bit(SOCK_PASSCRED, &sock->flags)
  866. && !u->addr && (err = unix_autobind(sock)) != 0)
  867. goto out;
  868. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  869. /* First of all allocate resources.
  870. If we will make it after state is locked,
  871. we will have to recheck all again in any case.
  872. */
  873. err = -ENOMEM;
  874. /* create new sock for complete connection */
  875. newsk = unix_create1(NULL);
  876. if (newsk == NULL)
  877. goto out;
  878. /* Allocate skb for sending to listening sock */
  879. skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
  880. if (skb == NULL)
  881. goto out;
  882. restart:
  883. /* Find listening sock. */
  884. other = unix_find_other(sunaddr, addr_len, sk->sk_type, hash, &err);
  885. if (!other)
  886. goto out;
  887. /* Latch state of peer */
  888. unix_state_lock(other);
  889. /* Apparently VFS overslept socket death. Retry. */
  890. if (sock_flag(other, SOCK_DEAD)) {
  891. unix_state_unlock(other);
  892. sock_put(other);
  893. goto restart;
  894. }
  895. err = -ECONNREFUSED;
  896. if (other->sk_state != TCP_LISTEN)
  897. goto out_unlock;
  898. if (skb_queue_len(&other->sk_receive_queue) >
  899. other->sk_max_ack_backlog) {
  900. err = -EAGAIN;
  901. if (!timeo)
  902. goto out_unlock;
  903. timeo = unix_wait_for_peer(other, timeo);
  904. err = sock_intr_errno(timeo);
  905. if (signal_pending(current))
  906. goto out;
  907. sock_put(other);
  908. goto restart;
  909. }
  910. /* Latch our state.
  911. It is tricky place. We need to grab write lock and cannot
  912. drop lock on peer. It is dangerous because deadlock is
  913. possible. Connect to self case and simultaneous
  914. attempt to connect are eliminated by checking socket
  915. state. other is TCP_LISTEN, if sk is TCP_LISTEN we
  916. check this before attempt to grab lock.
  917. Well, and we have to recheck the state after socket locked.
  918. */
  919. st = sk->sk_state;
  920. switch (st) {
  921. case TCP_CLOSE:
  922. /* This is ok... continue with connect */
  923. break;
  924. case TCP_ESTABLISHED:
  925. /* Socket is already connected */
  926. err = -EISCONN;
  927. goto out_unlock;
  928. default:
  929. err = -EINVAL;
  930. goto out_unlock;
  931. }
  932. unix_state_lock_nested(sk);
  933. if (sk->sk_state != st) {
  934. unix_state_unlock(sk);
  935. unix_state_unlock(other);
  936. sock_put(other);
  937. goto restart;
  938. }
  939. err = security_unix_stream_connect(sock, other->sk_socket, newsk);
  940. if (err) {
  941. unix_state_unlock(sk);
  942. goto out_unlock;
  943. }
  944. /* The way is open! Fastly set all the necessary fields... */
  945. sock_hold(sk);
  946. unix_peer(newsk) = sk;
  947. newsk->sk_state = TCP_ESTABLISHED;
  948. newsk->sk_type = sk->sk_type;
  949. newsk->sk_peercred.pid = current->tgid;
  950. newsk->sk_peercred.uid = current->euid;
  951. newsk->sk_peercred.gid = current->egid;
  952. newu = unix_sk(newsk);
  953. newsk->sk_sleep = &newu->peer_wait;
  954. otheru = unix_sk(other);
  955. /* copy address information from listening to new sock*/
  956. if (otheru->addr) {
  957. atomic_inc(&otheru->addr->refcnt);
  958. newu->addr = otheru->addr;
  959. }
  960. if (otheru->dentry) {
  961. newu->dentry = dget(otheru->dentry);
  962. newu->mnt = mntget(otheru->mnt);
  963. }
  964. /* Set credentials */
  965. sk->sk_peercred = other->sk_peercred;
  966. sock->state = SS_CONNECTED;
  967. sk->sk_state = TCP_ESTABLISHED;
  968. sock_hold(newsk);
  969. smp_mb__after_atomic_inc(); /* sock_hold() does an atomic_inc() */
  970. unix_peer(sk) = newsk;
  971. unix_state_unlock(sk);
  972. /* take ten and and send info to listening sock */
  973. spin_lock(&other->sk_receive_queue.lock);
  974. __skb_queue_tail(&other->sk_receive_queue, skb);
  975. /* Undo artificially decreased inflight after embrion
  976. * is installed to listening socket. */
  977. atomic_inc(&newu->inflight);
  978. spin_unlock(&other->sk_receive_queue.lock);
  979. unix_state_unlock(other);
  980. other->sk_data_ready(other, 0);
  981. sock_put(other);
  982. return 0;
  983. out_unlock:
  984. if (other)
  985. unix_state_unlock(other);
  986. out:
  987. if (skb)
  988. kfree_skb(skb);
  989. if (newsk)
  990. unix_release_sock(newsk, 0);
  991. if (other)
  992. sock_put(other);
  993. return err;
  994. }
  995. static int unix_socketpair(struct socket *socka, struct socket *sockb)
  996. {
  997. struct sock *ska=socka->sk, *skb = sockb->sk;
  998. /* Join our sockets back to back */
  999. sock_hold(ska);
  1000. sock_hold(skb);
  1001. unix_peer(ska)=skb;
  1002. unix_peer(skb)=ska;
  1003. ska->sk_peercred.pid = skb->sk_peercred.pid = current->tgid;
  1004. ska->sk_peercred.uid = skb->sk_peercred.uid = current->euid;
  1005. ska->sk_peercred.gid = skb->sk_peercred.gid = current->egid;
  1006. if (ska->sk_type != SOCK_DGRAM) {
  1007. ska->sk_state = TCP_ESTABLISHED;
  1008. skb->sk_state = TCP_ESTABLISHED;
  1009. socka->state = SS_CONNECTED;
  1010. sockb->state = SS_CONNECTED;
  1011. }
  1012. return 0;
  1013. }
  1014. static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
  1015. {
  1016. struct sock *sk = sock->sk;
  1017. struct sock *tsk;
  1018. struct sk_buff *skb;
  1019. int err;
  1020. err = -EOPNOTSUPP;
  1021. if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
  1022. goto out;
  1023. err = -EINVAL;
  1024. if (sk->sk_state != TCP_LISTEN)
  1025. goto out;
  1026. /* If socket state is TCP_LISTEN it cannot change (for now...),
  1027. * so that no locks are necessary.
  1028. */
  1029. skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
  1030. if (!skb) {
  1031. /* This means receive shutdown. */
  1032. if (err == 0)
  1033. err = -EINVAL;
  1034. goto out;
  1035. }
  1036. tsk = skb->sk;
  1037. skb_free_datagram(sk, skb);
  1038. wake_up_interruptible(&unix_sk(sk)->peer_wait);
  1039. /* attach accepted sock to socket */
  1040. unix_state_lock(tsk);
  1041. newsock->state = SS_CONNECTED;
  1042. sock_graft(tsk, newsock);
  1043. unix_state_unlock(tsk);
  1044. return 0;
  1045. out:
  1046. return err;
  1047. }
  1048. static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
  1049. {
  1050. struct sock *sk = sock->sk;
  1051. struct unix_sock *u;
  1052. struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
  1053. int err = 0;
  1054. if (peer) {
  1055. sk = unix_peer_get(sk);
  1056. err = -ENOTCONN;
  1057. if (!sk)
  1058. goto out;
  1059. err = 0;
  1060. } else {
  1061. sock_hold(sk);
  1062. }
  1063. u = unix_sk(sk);
  1064. unix_state_lock(sk);
  1065. if (!u->addr) {
  1066. sunaddr->sun_family = AF_UNIX;
  1067. sunaddr->sun_path[0] = 0;
  1068. *uaddr_len = sizeof(short);
  1069. } else {
  1070. struct unix_address *addr = u->addr;
  1071. *uaddr_len = addr->len;
  1072. memcpy(sunaddr, addr->name, *uaddr_len);
  1073. }
  1074. unix_state_unlock(sk);
  1075. sock_put(sk);
  1076. out:
  1077. return err;
  1078. }
  1079. static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
  1080. {
  1081. int i;
  1082. scm->fp = UNIXCB(skb).fp;
  1083. skb->destructor = sock_wfree;
  1084. UNIXCB(skb).fp = NULL;
  1085. for (i=scm->fp->count-1; i>=0; i--)
  1086. unix_notinflight(scm->fp->fp[i]);
  1087. }
  1088. static void unix_destruct_fds(struct sk_buff *skb)
  1089. {
  1090. struct scm_cookie scm;
  1091. memset(&scm, 0, sizeof(scm));
  1092. unix_detach_fds(&scm, skb);
  1093. /* Alas, it calls VFS */
  1094. /* So fscking what? fput() had been SMP-safe since the last Summer */
  1095. scm_destroy(&scm);
  1096. sock_wfree(skb);
  1097. }
  1098. static void unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
  1099. {
  1100. int i;
  1101. for (i=scm->fp->count-1; i>=0; i--)
  1102. unix_inflight(scm->fp->fp[i]);
  1103. UNIXCB(skb).fp = scm->fp;
  1104. skb->destructor = unix_destruct_fds;
  1105. scm->fp = NULL;
  1106. }
  1107. /*
  1108. * Send AF_UNIX data.
  1109. */
  1110. static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock,
  1111. struct msghdr *msg, size_t len)
  1112. {
  1113. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1114. struct sock *sk = sock->sk;
  1115. struct unix_sock *u = unix_sk(sk);
  1116. struct sockaddr_un *sunaddr=msg->msg_name;
  1117. struct sock *other = NULL;
  1118. int namelen = 0; /* fake GCC */
  1119. int err;
  1120. unsigned hash;
  1121. struct sk_buff *skb;
  1122. long timeo;
  1123. struct scm_cookie tmp_scm;
  1124. if (NULL == siocb->scm)
  1125. siocb->scm = &tmp_scm;
  1126. err = scm_send(sock, msg, siocb->scm);
  1127. if (err < 0)
  1128. return err;
  1129. err = -EOPNOTSUPP;
  1130. if (msg->msg_flags&MSG_OOB)
  1131. goto out;
  1132. if (msg->msg_namelen) {
  1133. err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
  1134. if (err < 0)
  1135. goto out;
  1136. namelen = err;
  1137. } else {
  1138. sunaddr = NULL;
  1139. err = -ENOTCONN;
  1140. other = unix_peer_get(sk);
  1141. if (!other)
  1142. goto out;
  1143. }
  1144. if (test_bit(SOCK_PASSCRED, &sock->flags)
  1145. && !u->addr && (err = unix_autobind(sock)) != 0)
  1146. goto out;
  1147. err = -EMSGSIZE;
  1148. if (len > sk->sk_sndbuf - 32)
  1149. goto out;
  1150. skb = sock_alloc_send_skb(sk, len, msg->msg_flags&MSG_DONTWAIT, &err);
  1151. if (skb==NULL)
  1152. goto out;
  1153. memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
  1154. if (siocb->scm->fp)
  1155. unix_attach_fds(siocb->scm, skb);
  1156. unix_get_secdata(siocb->scm, skb);
  1157. skb->h.raw = skb->data;
  1158. err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
  1159. if (err)
  1160. goto out_free;
  1161. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1162. restart:
  1163. if (!other) {
  1164. err = -ECONNRESET;
  1165. if (sunaddr == NULL)
  1166. goto out_free;
  1167. other = unix_find_other(sunaddr, namelen, sk->sk_type,
  1168. hash, &err);
  1169. if (other==NULL)
  1170. goto out_free;
  1171. }
  1172. unix_state_lock(other);
  1173. err = -EPERM;
  1174. if (!unix_may_send(sk, other))
  1175. goto out_unlock;
  1176. if (sock_flag(other, SOCK_DEAD)) {
  1177. /*
  1178. * Check with 1003.1g - what should
  1179. * datagram error
  1180. */
  1181. unix_state_unlock(other);
  1182. sock_put(other);
  1183. err = 0;
  1184. unix_state_lock(sk);
  1185. if (unix_peer(sk) == other) {
  1186. unix_peer(sk)=NULL;
  1187. unix_state_unlock(sk);
  1188. unix_dgram_disconnected(sk, other);
  1189. sock_put(other);
  1190. err = -ECONNREFUSED;
  1191. } else {
  1192. unix_state_unlock(sk);
  1193. }
  1194. other = NULL;
  1195. if (err)
  1196. goto out_free;
  1197. goto restart;
  1198. }
  1199. err = -EPIPE;
  1200. if (other->sk_shutdown & RCV_SHUTDOWN)
  1201. goto out_unlock;
  1202. if (sk->sk_type != SOCK_SEQPACKET) {
  1203. err = security_unix_may_send(sk->sk_socket, other->sk_socket);
  1204. if (err)
  1205. goto out_unlock;
  1206. }
  1207. if (unix_peer(other) != sk &&
  1208. (skb_queue_len(&other->sk_receive_queue) >
  1209. other->sk_max_ack_backlog)) {
  1210. if (!timeo) {
  1211. err = -EAGAIN;
  1212. goto out_unlock;
  1213. }
  1214. timeo = unix_wait_for_peer(other, timeo);
  1215. err = sock_intr_errno(timeo);
  1216. if (signal_pending(current))
  1217. goto out_free;
  1218. goto restart;
  1219. }
  1220. skb_queue_tail(&other->sk_receive_queue, skb);
  1221. unix_state_unlock(other);
  1222. other->sk_data_ready(other, len);
  1223. sock_put(other);
  1224. scm_destroy(siocb->scm);
  1225. return len;
  1226. out_unlock:
  1227. unix_state_unlock(other);
  1228. out_free:
  1229. kfree_skb(skb);
  1230. out:
  1231. if (other)
  1232. sock_put(other);
  1233. scm_destroy(siocb->scm);
  1234. return err;
  1235. }
  1236. static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
  1237. struct msghdr *msg, size_t len)
  1238. {
  1239. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1240. struct sock *sk = sock->sk;
  1241. struct sock *other = NULL;
  1242. struct sockaddr_un *sunaddr=msg->msg_name;
  1243. int err,size;
  1244. struct sk_buff *skb;
  1245. int sent=0;
  1246. struct scm_cookie tmp_scm;
  1247. if (NULL == siocb->scm)
  1248. siocb->scm = &tmp_scm;
  1249. err = scm_send(sock, msg, siocb->scm);
  1250. if (err < 0)
  1251. return err;
  1252. err = -EOPNOTSUPP;
  1253. if (msg->msg_flags&MSG_OOB)
  1254. goto out_err;
  1255. if (msg->msg_namelen) {
  1256. err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
  1257. goto out_err;
  1258. } else {
  1259. sunaddr = NULL;
  1260. err = -ENOTCONN;
  1261. other = unix_peer(sk);
  1262. if (!other)
  1263. goto out_err;
  1264. }
  1265. if (sk->sk_shutdown & SEND_SHUTDOWN)
  1266. goto pipe_err;
  1267. while(sent < len)
  1268. {
  1269. /*
  1270. * Optimisation for the fact that under 0.01% of X
  1271. * messages typically need breaking up.
  1272. */
  1273. size = len-sent;
  1274. /* Keep two messages in the pipe so it schedules better */
  1275. if (size > ((sk->sk_sndbuf >> 1) - 64))
  1276. size = (sk->sk_sndbuf >> 1) - 64;
  1277. if (size > SKB_MAX_ALLOC)
  1278. size = SKB_MAX_ALLOC;
  1279. /*
  1280. * Grab a buffer
  1281. */
  1282. skb=sock_alloc_send_skb(sk,size,msg->msg_flags&MSG_DONTWAIT, &err);
  1283. if (skb==NULL)
  1284. goto out_err;
  1285. /*
  1286. * If you pass two values to the sock_alloc_send_skb
  1287. * it tries to grab the large buffer with GFP_NOFS
  1288. * (which can fail easily), and if it fails grab the
  1289. * fallback size buffer which is under a page and will
  1290. * succeed. [Alan]
  1291. */
  1292. size = min_t(int, size, skb_tailroom(skb));
  1293. memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
  1294. if (siocb->scm->fp)
  1295. unix_attach_fds(siocb->scm, skb);
  1296. if ((err = memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size)) != 0) {
  1297. kfree_skb(skb);
  1298. goto out_err;
  1299. }
  1300. unix_state_lock(other);
  1301. if (sock_flag(other, SOCK_DEAD) ||
  1302. (other->sk_shutdown & RCV_SHUTDOWN))
  1303. goto pipe_err_free;
  1304. skb_queue_tail(&other->sk_receive_queue, skb);
  1305. unix_state_unlock(other);
  1306. other->sk_data_ready(other, size);
  1307. sent+=size;
  1308. }
  1309. scm_destroy(siocb->scm);
  1310. siocb->scm = NULL;
  1311. return sent;
  1312. pipe_err_free:
  1313. unix_state_unlock(other);
  1314. kfree_skb(skb);
  1315. pipe_err:
  1316. if (sent==0 && !(msg->msg_flags&MSG_NOSIGNAL))
  1317. send_sig(SIGPIPE,current,0);
  1318. err = -EPIPE;
  1319. out_err:
  1320. scm_destroy(siocb->scm);
  1321. siocb->scm = NULL;
  1322. return sent ? : err;
  1323. }
  1324. static int unix_seqpacket_sendmsg(struct kiocb *kiocb, struct socket *sock,
  1325. struct msghdr *msg, size_t len)
  1326. {
  1327. int err;
  1328. struct sock *sk = sock->sk;
  1329. err = sock_error(sk);
  1330. if (err)
  1331. return err;
  1332. if (sk->sk_state != TCP_ESTABLISHED)
  1333. return -ENOTCONN;
  1334. if (msg->msg_namelen)
  1335. msg->msg_namelen = 0;
  1336. return unix_dgram_sendmsg(kiocb, sock, msg, len);
  1337. }
  1338. static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
  1339. {
  1340. struct unix_sock *u = unix_sk(sk);
  1341. msg->msg_namelen = 0;
  1342. if (u->addr) {
  1343. msg->msg_namelen = u->addr->len;
  1344. memcpy(msg->msg_name, u->addr->name, u->addr->len);
  1345. }
  1346. }
  1347. static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock,
  1348. struct msghdr *msg, size_t size,
  1349. int flags)
  1350. {
  1351. struct sock_iocb *siocb = kiocb_to_siocb(iocb);
  1352. struct scm_cookie tmp_scm;
  1353. struct sock *sk = sock->sk;
  1354. struct unix_sock *u = unix_sk(sk);
  1355. int noblock = flags & MSG_DONTWAIT;
  1356. struct sk_buff *skb;
  1357. int err;
  1358. err = -EOPNOTSUPP;
  1359. if (flags&MSG_OOB)
  1360. goto out;
  1361. msg->msg_namelen = 0;
  1362. mutex_lock(&u->readlock);
  1363. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1364. if (!skb)
  1365. goto out_unlock;
  1366. wake_up_interruptible(&u->peer_wait);
  1367. if (msg->msg_name)
  1368. unix_copy_addr(msg, skb->sk);
  1369. if (size > skb->len)
  1370. size = skb->len;
  1371. else if (size < skb->len)
  1372. msg->msg_flags |= MSG_TRUNC;
  1373. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, size);
  1374. if (err)
  1375. goto out_free;
  1376. if (!siocb->scm) {
  1377. siocb->scm = &tmp_scm;
  1378. memset(&tmp_scm, 0, sizeof(tmp_scm));
  1379. }
  1380. siocb->scm->creds = *UNIXCREDS(skb);
  1381. unix_set_secdata(siocb->scm, skb);
  1382. if (!(flags & MSG_PEEK))
  1383. {
  1384. if (UNIXCB(skb).fp)
  1385. unix_detach_fds(siocb->scm, skb);
  1386. }
  1387. else
  1388. {
  1389. /* It is questionable: on PEEK we could:
  1390. - do not return fds - good, but too simple 8)
  1391. - return fds, and do not return them on read (old strategy,
  1392. apparently wrong)
  1393. - clone fds (I chose it for now, it is the most universal
  1394. solution)
  1395. POSIX 1003.1g does not actually define this clearly
  1396. at all. POSIX 1003.1g doesn't define a lot of things
  1397. clearly however!
  1398. */
  1399. if (UNIXCB(skb).fp)
  1400. siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
  1401. }
  1402. err = size;
  1403. scm_recv(sock, msg, siocb->scm, flags);
  1404. out_free:
  1405. skb_free_datagram(sk,skb);
  1406. out_unlock:
  1407. mutex_unlock(&u->readlock);
  1408. out:
  1409. return err;
  1410. }
  1411. /*
  1412. * Sleep until data has arrive. But check for races..
  1413. */
  1414. static long unix_stream_data_wait(struct sock * sk, long timeo)
  1415. {
  1416. DEFINE_WAIT(wait);
  1417. unix_state_lock(sk);
  1418. for (;;) {
  1419. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  1420. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  1421. sk->sk_err ||
  1422. (sk->sk_shutdown & RCV_SHUTDOWN) ||
  1423. signal_pending(current) ||
  1424. !timeo)
  1425. break;
  1426. set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  1427. unix_state_unlock(sk);
  1428. timeo = schedule_timeout(timeo);
  1429. unix_state_lock(sk);
  1430. clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  1431. }
  1432. finish_wait(sk->sk_sleep, &wait);
  1433. unix_state_unlock(sk);
  1434. return timeo;
  1435. }
  1436. static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
  1437. struct msghdr *msg, size_t size,
  1438. int flags)
  1439. {
  1440. struct sock_iocb *siocb = kiocb_to_siocb(iocb);
  1441. struct scm_cookie tmp_scm;
  1442. struct sock *sk = sock->sk;
  1443. struct unix_sock *u = unix_sk(sk);
  1444. struct sockaddr_un *sunaddr=msg->msg_name;
  1445. int copied = 0;
  1446. int check_creds = 0;
  1447. int target;
  1448. int err = 0;
  1449. long timeo;
  1450. err = -EINVAL;
  1451. if (sk->sk_state != TCP_ESTABLISHED)
  1452. goto out;
  1453. err = -EOPNOTSUPP;
  1454. if (flags&MSG_OOB)
  1455. goto out;
  1456. target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
  1457. timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
  1458. msg->msg_namelen = 0;
  1459. /* Lock the socket to prevent queue disordering
  1460. * while sleeps in memcpy_tomsg
  1461. */
  1462. if (!siocb->scm) {
  1463. siocb->scm = &tmp_scm;
  1464. memset(&tmp_scm, 0, sizeof(tmp_scm));
  1465. }
  1466. mutex_lock(&u->readlock);
  1467. do
  1468. {
  1469. int chunk;
  1470. struct sk_buff *skb;
  1471. skb = skb_dequeue(&sk->sk_receive_queue);
  1472. if (skb==NULL)
  1473. {
  1474. if (copied >= target)
  1475. break;
  1476. /*
  1477. * POSIX 1003.1g mandates this order.
  1478. */
  1479. if ((err = sock_error(sk)) != 0)
  1480. break;
  1481. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1482. break;
  1483. err = -EAGAIN;
  1484. if (!timeo)
  1485. break;
  1486. mutex_unlock(&u->readlock);
  1487. timeo = unix_stream_data_wait(sk, timeo);
  1488. if (signal_pending(current)) {
  1489. err = sock_intr_errno(timeo);
  1490. goto out;
  1491. }
  1492. mutex_lock(&u->readlock);
  1493. continue;
  1494. }
  1495. if (check_creds) {
  1496. /* Never glue messages from different writers */
  1497. if (memcmp(UNIXCREDS(skb), &siocb->scm->creds, sizeof(siocb->scm->creds)) != 0) {
  1498. skb_queue_head(&sk->sk_receive_queue, skb);
  1499. break;
  1500. }
  1501. } else {
  1502. /* Copy credentials */
  1503. siocb->scm->creds = *UNIXCREDS(skb);
  1504. check_creds = 1;
  1505. }
  1506. /* Copy address just once */
  1507. if (sunaddr)
  1508. {
  1509. unix_copy_addr(msg, skb->sk);
  1510. sunaddr = NULL;
  1511. }
  1512. chunk = min_t(unsigned int, skb->len, size);
  1513. if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
  1514. skb_queue_head(&sk->sk_receive_queue, skb);
  1515. if (copied == 0)
  1516. copied = -EFAULT;
  1517. break;
  1518. }
  1519. copied += chunk;
  1520. size -= chunk;
  1521. /* Mark read part of skb as used */
  1522. if (!(flags & MSG_PEEK))
  1523. {
  1524. skb_pull(skb, chunk);
  1525. if (UNIXCB(skb).fp)
  1526. unix_detach_fds(siocb->scm, skb);
  1527. /* put the skb back if we didn't use it up.. */
  1528. if (skb->len)
  1529. {
  1530. skb_queue_head(&sk->sk_receive_queue, skb);
  1531. break;
  1532. }
  1533. kfree_skb(skb);
  1534. if (siocb->scm->fp)
  1535. break;
  1536. }
  1537. else
  1538. {
  1539. /* It is questionable, see note in unix_dgram_recvmsg.
  1540. */
  1541. if (UNIXCB(skb).fp)
  1542. siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
  1543. /* put message back and return */
  1544. skb_queue_head(&sk->sk_receive_queue, skb);
  1545. break;
  1546. }
  1547. } while (size);
  1548. mutex_unlock(&u->readlock);
  1549. scm_recv(sock, msg, siocb->scm, flags);
  1550. out:
  1551. return copied ? : err;
  1552. }
  1553. static int unix_shutdown(struct socket *sock, int mode)
  1554. {
  1555. struct sock *sk = sock->sk;
  1556. struct sock *other;
  1557. mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
  1558. if (mode) {
  1559. unix_state_lock(sk);
  1560. sk->sk_shutdown |= mode;
  1561. other=unix_peer(sk);
  1562. if (other)
  1563. sock_hold(other);
  1564. unix_state_unlock(sk);
  1565. sk->sk_state_change(sk);
  1566. if (other &&
  1567. (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
  1568. int peer_mode = 0;
  1569. if (mode&RCV_SHUTDOWN)
  1570. peer_mode |= SEND_SHUTDOWN;
  1571. if (mode&SEND_SHUTDOWN)
  1572. peer_mode |= RCV_SHUTDOWN;
  1573. unix_state_lock(other);
  1574. other->sk_shutdown |= peer_mode;
  1575. unix_state_unlock(other);
  1576. other->sk_state_change(other);
  1577. read_lock(&other->sk_callback_lock);
  1578. if (peer_mode == SHUTDOWN_MASK)
  1579. sk_wake_async(other,1,POLL_HUP);
  1580. else if (peer_mode & RCV_SHUTDOWN)
  1581. sk_wake_async(other,1,POLL_IN);
  1582. read_unlock(&other->sk_callback_lock);
  1583. }
  1584. if (other)
  1585. sock_put(other);
  1586. }
  1587. return 0;
  1588. }
  1589. static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1590. {
  1591. struct sock *sk = sock->sk;
  1592. long amount=0;
  1593. int err;
  1594. switch(cmd)
  1595. {
  1596. case SIOCOUTQ:
  1597. amount = atomic_read(&sk->sk_wmem_alloc);
  1598. err = put_user(amount, (int __user *)arg);
  1599. break;
  1600. case SIOCINQ:
  1601. {
  1602. struct sk_buff *skb;
  1603. if (sk->sk_state == TCP_LISTEN) {
  1604. err = -EINVAL;
  1605. break;
  1606. }
  1607. spin_lock(&sk->sk_receive_queue.lock);
  1608. if (sk->sk_type == SOCK_STREAM ||
  1609. sk->sk_type == SOCK_SEQPACKET) {
  1610. skb_queue_walk(&sk->sk_receive_queue, skb)
  1611. amount += skb->len;
  1612. } else {
  1613. skb = skb_peek(&sk->sk_receive_queue);
  1614. if (skb)
  1615. amount=skb->len;
  1616. }
  1617. spin_unlock(&sk->sk_receive_queue.lock);
  1618. err = put_user(amount, (int __user *)arg);
  1619. break;
  1620. }
  1621. default:
  1622. err = -ENOIOCTLCMD;
  1623. break;
  1624. }
  1625. return err;
  1626. }
  1627. static unsigned int unix_poll(struct file * file, struct socket *sock, poll_table *wait)
  1628. {
  1629. struct sock *sk = sock->sk;
  1630. unsigned int mask;
  1631. poll_wait(file, sk->sk_sleep, wait);
  1632. mask = 0;
  1633. /* exceptional events? */
  1634. if (sk->sk_err)
  1635. mask |= POLLERR;
  1636. if (sk->sk_shutdown == SHUTDOWN_MASK)
  1637. mask |= POLLHUP;
  1638. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1639. mask |= POLLRDHUP;
  1640. /* readable? */
  1641. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  1642. (sk->sk_shutdown & RCV_SHUTDOWN))
  1643. mask |= POLLIN | POLLRDNORM;
  1644. /* Connection-based need to check for termination and startup */
  1645. if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) && sk->sk_state == TCP_CLOSE)
  1646. mask |= POLLHUP;
  1647. /*
  1648. * we set writable also when the other side has shut down the
  1649. * connection. This prevents stuck sockets.
  1650. */
  1651. if (unix_writable(sk))
  1652. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  1653. return mask;
  1654. }
  1655. #ifdef CONFIG_PROC_FS
  1656. static struct sock *unix_seq_idx(int *iter, loff_t pos)
  1657. {
  1658. loff_t off = 0;
  1659. struct sock *s;
  1660. for (s = first_unix_socket(iter); s; s = next_unix_socket(iter, s)) {
  1661. if (off == pos)
  1662. return s;
  1663. ++off;
  1664. }
  1665. return NULL;
  1666. }
  1667. static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
  1668. {
  1669. spin_lock(&unix_table_lock);
  1670. return *pos ? unix_seq_idx(seq->private, *pos - 1) : ((void *) 1);
  1671. }
  1672. static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1673. {
  1674. ++*pos;
  1675. if (v == (void *)1)
  1676. return first_unix_socket(seq->private);
  1677. return next_unix_socket(seq->private, v);
  1678. }
  1679. static void unix_seq_stop(struct seq_file *seq, void *v)
  1680. {
  1681. spin_unlock(&unix_table_lock);
  1682. }
  1683. static int unix_seq_show(struct seq_file *seq, void *v)
  1684. {
  1685. if (v == (void *)1)
  1686. seq_puts(seq, "Num RefCount Protocol Flags Type St "
  1687. "Inode Path\n");
  1688. else {
  1689. struct sock *s = v;
  1690. struct unix_sock *u = unix_sk(s);
  1691. unix_state_lock(s);
  1692. seq_printf(seq, "%p: %08X %08X %08X %04X %02X %5lu",
  1693. s,
  1694. atomic_read(&s->sk_refcnt),
  1695. 0,
  1696. s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
  1697. s->sk_type,
  1698. s->sk_socket ?
  1699. (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
  1700. (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
  1701. sock_i_ino(s));
  1702. if (u->addr) {
  1703. int i, len;
  1704. seq_putc(seq, ' ');
  1705. i = 0;
  1706. len = u->addr->len - sizeof(short);
  1707. if (!UNIX_ABSTRACT(s))
  1708. len--;
  1709. else {
  1710. seq_putc(seq, '@');
  1711. i++;
  1712. }
  1713. for ( ; i < len; i++)
  1714. seq_putc(seq, u->addr->name->sun_path[i]);
  1715. }
  1716. unix_state_unlock(s);
  1717. seq_putc(seq, '\n');
  1718. }
  1719. return 0;
  1720. }
  1721. static struct seq_operations unix_seq_ops = {
  1722. .start = unix_seq_start,
  1723. .next = unix_seq_next,
  1724. .stop = unix_seq_stop,
  1725. .show = unix_seq_show,
  1726. };
  1727. static int unix_seq_open(struct inode *inode, struct file *file)
  1728. {
  1729. struct seq_file *seq;
  1730. int rc = -ENOMEM;
  1731. int *iter = kmalloc(sizeof(int), GFP_KERNEL);
  1732. if (!iter)
  1733. goto out;
  1734. rc = seq_open(file, &unix_seq_ops);
  1735. if (rc)
  1736. goto out_kfree;
  1737. seq = file->private_data;
  1738. seq->private = iter;
  1739. *iter = 0;
  1740. out:
  1741. return rc;
  1742. out_kfree:
  1743. kfree(iter);
  1744. goto out;
  1745. }
  1746. static const struct file_operations unix_seq_fops = {
  1747. .owner = THIS_MODULE,
  1748. .open = unix_seq_open,
  1749. .read = seq_read,
  1750. .llseek = seq_lseek,
  1751. .release = seq_release_private,
  1752. };
  1753. #endif
  1754. static struct net_proto_family unix_family_ops = {
  1755. .family = PF_UNIX,
  1756. .create = unix_create,
  1757. .owner = THIS_MODULE,
  1758. };
  1759. static int __init af_unix_init(void)
  1760. {
  1761. int rc = -1;
  1762. struct sk_buff *dummy_skb;
  1763. BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof(dummy_skb->cb));
  1764. rc = proto_register(&unix_proto, 1);
  1765. if (rc != 0) {
  1766. printk(KERN_CRIT "%s: Cannot create unix_sock SLAB cache!\n",
  1767. __FUNCTION__);
  1768. goto out;
  1769. }
  1770. sock_register(&unix_family_ops);
  1771. #ifdef CONFIG_PROC_FS
  1772. proc_net_fops_create("unix", 0, &unix_seq_fops);
  1773. #endif
  1774. unix_sysctl_register();
  1775. out:
  1776. return rc;
  1777. }
  1778. static void __exit af_unix_exit(void)
  1779. {
  1780. sock_unregister(PF_UNIX);
  1781. unix_sysctl_unregister();
  1782. proc_net_remove("unix");
  1783. proto_unregister(&unix_proto);
  1784. }
  1785. module_init(af_unix_init);
  1786. module_exit(af_unix_exit);
  1787. MODULE_LICENSE("GPL");
  1788. MODULE_ALIAS_NETPROTO(PF_UNIX);