af_unix.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934
  1. /*
  2. * NET4: Implementation of BSD Unix domain sockets.
  3. *
  4. * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk>
  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. * Fixes:
  12. * Linus Torvalds : Assorted bug cures.
  13. * Niibe Yutaka : async I/O support.
  14. * Carsten Paeth : PF_UNIX check, address fixes.
  15. * Alan Cox : Limit size of allocated blocks.
  16. * Alan Cox : Fixed the stupid socketpair bug.
  17. * Alan Cox : BSD compatibility fine tuning.
  18. * Alan Cox : Fixed a bug in connect when interrupted.
  19. * Alan Cox : Sorted out a proper draft version of
  20. * file descriptor passing hacked up from
  21. * Mike Shaver's work.
  22. * Marty Leisner : Fixes to fd passing
  23. * Nick Nevin : recvmsg bugfix.
  24. * Alan Cox : Started proper garbage collector
  25. * Heiko EiBfeldt : Missing verify_area check
  26. * Alan Cox : Started POSIXisms
  27. * Andreas Schwab : Replace inode by dentry for proper
  28. * reference counting
  29. * Kirk Petersen : Made this a module
  30. * Christoph Rohland : Elegant non-blocking accept/connect algorithm.
  31. * Lots of bug fixes.
  32. * Alexey Kuznetosv : Repaired (I hope) bugs introduces
  33. * by above two patches.
  34. * Andrea Arcangeli : If possible we block in connect(2)
  35. * if the max backlog of the listen socket
  36. * is been reached. This won't break
  37. * old apps and it will avoid huge amount
  38. * of socks hashed (this for unix_gc()
  39. * performances reasons).
  40. * Security fix that limits the max
  41. * number of socks to 2*max_files and
  42. * the number of skb queueable in the
  43. * dgram receiver.
  44. * Artur Skawina : Hash function optimizations
  45. * Alexey Kuznetsov : Full scale SMP. Lot of bugs are introduced 8)
  46. * Malcolm Beattie : Set peercred for socketpair
  47. * Michal Ostrowski : Module initialization cleanup.
  48. * Arnaldo C. Melo : Remove MOD_{INC,DEC}_USE_COUNT,
  49. * the core infrastructure is doing that
  50. * for all net proto families now (2.5.69+)
  51. *
  52. *
  53. * Known differences from reference BSD that was tested:
  54. *
  55. * [TO FIX]
  56. * ECONNREFUSED is not returned from one end of a connected() socket to the
  57. * other the moment one end closes.
  58. * fstat() doesn't return st_dev=0, and give the blksize as high water mark
  59. * and a fake inode identifier (nor the BSD first socket fstat twice bug).
  60. * [NOT TO FIX]
  61. * accept() returns a path name even if the connecting socket has closed
  62. * in the meantime (BSD loses the path and gives up).
  63. * accept() returns 0 length path for an unbound connector. BSD returns 16
  64. * and a null first byte in the path (but not for gethost/peername - BSD bug ??)
  65. * socketpair(...SOCK_RAW..) doesn't panic the kernel.
  66. * BSD af_unix apparently has connect forgetting to block properly.
  67. * (need to check this with the POSIX spec in detail)
  68. *
  69. * Differences from 2.0.0-11-... (ANK)
  70. * Bug fixes and improvements.
  71. * - client shutdown killed server socket.
  72. * - removed all useless cli/sti pairs.
  73. *
  74. * Semantic changes/extensions.
  75. * - generic control message passing.
  76. * - SCM_CREDENTIALS control message.
  77. * - "Abstract" (not FS based) socket bindings.
  78. * Abstract names are sequences of bytes (not zero terminated)
  79. * started by 0, so that this name space does not intersect
  80. * with BSD names.
  81. */
  82. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  83. #include <linux/module.h>
  84. #include <linux/kernel.h>
  85. #include <linux/signal.h>
  86. #include <linux/sched.h>
  87. #include <linux/errno.h>
  88. #include <linux/string.h>
  89. #include <linux/stat.h>
  90. #include <linux/dcache.h>
  91. #include <linux/namei.h>
  92. #include <linux/socket.h>
  93. #include <linux/un.h>
  94. #include <linux/fcntl.h>
  95. #include <linux/termios.h>
  96. #include <linux/sockios.h>
  97. #include <linux/net.h>
  98. #include <linux/in.h>
  99. #include <linux/fs.h>
  100. #include <linux/slab.h>
  101. #include <asm/uaccess.h>
  102. #include <linux/skbuff.h>
  103. #include <linux/netdevice.h>
  104. #include <net/net_namespace.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/rtnetlink.h>
  114. #include <linux/mount.h>
  115. #include <net/checksum.h>
  116. #include <linux/security.h>
  117. #include <linux/freezer.h>
  118. struct hlist_head unix_socket_table[2 * UNIX_HASH_SIZE];
  119. EXPORT_SYMBOL_GPL(unix_socket_table);
  120. DEFINE_SPINLOCK(unix_table_lock);
  121. EXPORT_SYMBOL_GPL(unix_table_lock);
  122. static atomic_long_t unix_nr_socks;
  123. static struct hlist_head *unix_sockets_unbound(void *addr)
  124. {
  125. unsigned long hash = (unsigned long)addr;
  126. hash ^= hash >> 16;
  127. hash ^= hash >> 8;
  128. hash %= UNIX_HASH_SIZE;
  129. return &unix_socket_table[UNIX_HASH_SIZE + hash];
  130. }
  131. #define UNIX_ABSTRACT(sk) (unix_sk(sk)->addr->hash < UNIX_HASH_SIZE)
  132. #ifdef CONFIG_SECURITY_NETWORK
  133. static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  134. {
  135. UNIXCB(skb).secid = scm->secid;
  136. }
  137. static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  138. {
  139. scm->secid = UNIXCB(skb).secid;
  140. }
  141. static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)
  142. {
  143. return (scm->secid == UNIXCB(skb).secid);
  144. }
  145. #else
  146. static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  147. { }
  148. static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  149. { }
  150. static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)
  151. {
  152. return true;
  153. }
  154. #endif /* CONFIG_SECURITY_NETWORK */
  155. /*
  156. * SMP locking strategy:
  157. * hash table is protected with spinlock unix_table_lock
  158. * each socket state is protected by separate spin lock.
  159. */
  160. static inline unsigned int unix_hash_fold(__wsum n)
  161. {
  162. unsigned int hash = (__force unsigned int)csum_fold(n);
  163. hash ^= hash>>8;
  164. return hash&(UNIX_HASH_SIZE-1);
  165. }
  166. #define unix_peer(sk) (unix_sk(sk)->peer)
  167. static inline int unix_our_peer(struct sock *sk, struct sock *osk)
  168. {
  169. return unix_peer(osk) == sk;
  170. }
  171. static inline int unix_may_send(struct sock *sk, struct sock *osk)
  172. {
  173. return unix_peer(osk) == NULL || unix_our_peer(sk, osk);
  174. }
  175. static inline int unix_recvq_full(struct sock const *sk)
  176. {
  177. return skb_queue_len(&sk->sk_receive_queue) > sk->sk_max_ack_backlog;
  178. }
  179. struct sock *unix_peer_get(struct sock *s)
  180. {
  181. struct sock *peer;
  182. unix_state_lock(s);
  183. peer = unix_peer(s);
  184. if (peer)
  185. sock_hold(peer);
  186. unix_state_unlock(s);
  187. return peer;
  188. }
  189. EXPORT_SYMBOL_GPL(unix_peer_get);
  190. static inline void unix_release_addr(struct unix_address *addr)
  191. {
  192. if (atomic_dec_and_test(&addr->refcnt))
  193. kfree(addr);
  194. }
  195. /*
  196. * Check unix socket name:
  197. * - should be not zero length.
  198. * - if started by not zero, should be NULL terminated (FS object)
  199. * - if started by zero, it is abstract name.
  200. */
  201. static int unix_mkname(struct sockaddr_un *sunaddr, int len, unsigned int *hashp)
  202. {
  203. if (len <= sizeof(short) || len > sizeof(*sunaddr))
  204. return -EINVAL;
  205. if (!sunaddr || sunaddr->sun_family != AF_UNIX)
  206. return -EINVAL;
  207. if (sunaddr->sun_path[0]) {
  208. /*
  209. * This may look like an off by one error but it is a bit more
  210. * subtle. 108 is the longest valid AF_UNIX path for a binding.
  211. * sun_path[108] doesn't as such exist. However in kernel space
  212. * we are guaranteed that it is a valid memory location in our
  213. * kernel address buffer.
  214. */
  215. ((char *)sunaddr)[len] = 0;
  216. len = strlen(sunaddr->sun_path)+1+sizeof(short);
  217. return len;
  218. }
  219. *hashp = unix_hash_fold(csum_partial(sunaddr, len, 0));
  220. return len;
  221. }
  222. static void __unix_remove_socket(struct sock *sk)
  223. {
  224. sk_del_node_init(sk);
  225. }
  226. static void __unix_insert_socket(struct hlist_head *list, struct sock *sk)
  227. {
  228. WARN_ON(!sk_unhashed(sk));
  229. sk_add_node(sk, list);
  230. }
  231. static inline void unix_remove_socket(struct sock *sk)
  232. {
  233. spin_lock(&unix_table_lock);
  234. __unix_remove_socket(sk);
  235. spin_unlock(&unix_table_lock);
  236. }
  237. static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk)
  238. {
  239. spin_lock(&unix_table_lock);
  240. __unix_insert_socket(list, sk);
  241. spin_unlock(&unix_table_lock);
  242. }
  243. static struct sock *__unix_find_socket_byname(struct net *net,
  244. struct sockaddr_un *sunname,
  245. int len, int type, unsigned int hash)
  246. {
  247. struct sock *s;
  248. sk_for_each(s, &unix_socket_table[hash ^ type]) {
  249. struct unix_sock *u = unix_sk(s);
  250. if (!net_eq(sock_net(s), net))
  251. continue;
  252. if (u->addr->len == len &&
  253. !memcmp(u->addr->name, sunname, len))
  254. goto found;
  255. }
  256. s = NULL;
  257. found:
  258. return s;
  259. }
  260. static inline struct sock *unix_find_socket_byname(struct net *net,
  261. struct sockaddr_un *sunname,
  262. int len, int type,
  263. unsigned int hash)
  264. {
  265. struct sock *s;
  266. spin_lock(&unix_table_lock);
  267. s = __unix_find_socket_byname(net, sunname, len, type, hash);
  268. if (s)
  269. sock_hold(s);
  270. spin_unlock(&unix_table_lock);
  271. return s;
  272. }
  273. static struct sock *unix_find_socket_byinode(struct inode *i)
  274. {
  275. struct sock *s;
  276. spin_lock(&unix_table_lock);
  277. sk_for_each(s,
  278. &unix_socket_table[i->i_ino & (UNIX_HASH_SIZE - 1)]) {
  279. struct dentry *dentry = unix_sk(s)->path.dentry;
  280. if (dentry && d_real_inode(dentry) == i) {
  281. sock_hold(s);
  282. goto found;
  283. }
  284. }
  285. s = NULL;
  286. found:
  287. spin_unlock(&unix_table_lock);
  288. return s;
  289. }
  290. /* Support code for asymmetrically connected dgram sockets
  291. *
  292. * If a datagram socket is connected to a socket not itself connected
  293. * to the first socket (eg, /dev/log), clients may only enqueue more
  294. * messages if the present receive queue of the server socket is not
  295. * "too large". This means there's a second writeability condition
  296. * poll and sendmsg need to test. The dgram recv code will do a wake
  297. * up on the peer_wait wait queue of a socket upon reception of a
  298. * datagram which needs to be propagated to sleeping would-be writers
  299. * since these might not have sent anything so far. This can't be
  300. * accomplished via poll_wait because the lifetime of the server
  301. * socket might be less than that of its clients if these break their
  302. * association with it or if the server socket is closed while clients
  303. * are still connected to it and there's no way to inform "a polling
  304. * implementation" that it should let go of a certain wait queue
  305. *
  306. * In order to propagate a wake up, a wait_queue_t of the client
  307. * socket is enqueued on the peer_wait queue of the server socket
  308. * whose wake function does a wake_up on the ordinary client socket
  309. * wait queue. This connection is established whenever a write (or
  310. * poll for write) hit the flow control condition and broken when the
  311. * association to the server socket is dissolved or after a wake up
  312. * was relayed.
  313. */
  314. static int unix_dgram_peer_wake_relay(wait_queue_t *q, unsigned mode, int flags,
  315. void *key)
  316. {
  317. struct unix_sock *u;
  318. wait_queue_head_t *u_sleep;
  319. u = container_of(q, struct unix_sock, peer_wake);
  320. __remove_wait_queue(&unix_sk(u->peer_wake.private)->peer_wait,
  321. q);
  322. u->peer_wake.private = NULL;
  323. /* relaying can only happen while the wq still exists */
  324. u_sleep = sk_sleep(&u->sk);
  325. if (u_sleep)
  326. wake_up_interruptible_poll(u_sleep, key);
  327. return 0;
  328. }
  329. static int unix_dgram_peer_wake_connect(struct sock *sk, struct sock *other)
  330. {
  331. struct unix_sock *u, *u_other;
  332. int rc;
  333. u = unix_sk(sk);
  334. u_other = unix_sk(other);
  335. rc = 0;
  336. spin_lock(&u_other->peer_wait.lock);
  337. if (!u->peer_wake.private) {
  338. u->peer_wake.private = other;
  339. __add_wait_queue(&u_other->peer_wait, &u->peer_wake);
  340. rc = 1;
  341. }
  342. spin_unlock(&u_other->peer_wait.lock);
  343. return rc;
  344. }
  345. static void unix_dgram_peer_wake_disconnect(struct sock *sk,
  346. struct sock *other)
  347. {
  348. struct unix_sock *u, *u_other;
  349. u = unix_sk(sk);
  350. u_other = unix_sk(other);
  351. spin_lock(&u_other->peer_wait.lock);
  352. if (u->peer_wake.private == other) {
  353. __remove_wait_queue(&u_other->peer_wait, &u->peer_wake);
  354. u->peer_wake.private = NULL;
  355. }
  356. spin_unlock(&u_other->peer_wait.lock);
  357. }
  358. static void unix_dgram_peer_wake_disconnect_wakeup(struct sock *sk,
  359. struct sock *other)
  360. {
  361. unix_dgram_peer_wake_disconnect(sk, other);
  362. wake_up_interruptible_poll(sk_sleep(sk),
  363. POLLOUT |
  364. POLLWRNORM |
  365. POLLWRBAND);
  366. }
  367. /* preconditions:
  368. * - unix_peer(sk) == other
  369. * - association is stable
  370. */
  371. static int unix_dgram_peer_wake_me(struct sock *sk, struct sock *other)
  372. {
  373. int connected;
  374. connected = unix_dgram_peer_wake_connect(sk, other);
  375. if (unix_recvq_full(other))
  376. return 1;
  377. if (connected)
  378. unix_dgram_peer_wake_disconnect(sk, other);
  379. return 0;
  380. }
  381. static int unix_writable(const struct sock *sk)
  382. {
  383. return sk->sk_state != TCP_LISTEN &&
  384. (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
  385. }
  386. static void unix_write_space(struct sock *sk)
  387. {
  388. struct socket_wq *wq;
  389. rcu_read_lock();
  390. if (unix_writable(sk)) {
  391. wq = rcu_dereference(sk->sk_wq);
  392. if (skwq_has_sleeper(wq))
  393. wake_up_interruptible_sync_poll(&wq->wait,
  394. POLLOUT | POLLWRNORM | POLLWRBAND);
  395. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  396. }
  397. rcu_read_unlock();
  398. }
  399. /* When dgram socket disconnects (or changes its peer), we clear its receive
  400. * queue of packets arrived from previous peer. First, it allows to do
  401. * flow control based only on wmem_alloc; second, sk connected to peer
  402. * may receive messages only from that peer. */
  403. static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
  404. {
  405. if (!skb_queue_empty(&sk->sk_receive_queue)) {
  406. skb_queue_purge(&sk->sk_receive_queue);
  407. wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
  408. /* If one link of bidirectional dgram pipe is disconnected,
  409. * we signal error. Messages are lost. Do not make this,
  410. * when peer was not connected to us.
  411. */
  412. if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) {
  413. other->sk_err = ECONNRESET;
  414. other->sk_error_report(other);
  415. }
  416. }
  417. }
  418. static void unix_sock_destructor(struct sock *sk)
  419. {
  420. struct unix_sock *u = unix_sk(sk);
  421. skb_queue_purge(&sk->sk_receive_queue);
  422. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  423. WARN_ON(!sk_unhashed(sk));
  424. WARN_ON(sk->sk_socket);
  425. if (!sock_flag(sk, SOCK_DEAD)) {
  426. pr_info("Attempt to release alive unix socket: %p\n", sk);
  427. return;
  428. }
  429. if (u->addr)
  430. unix_release_addr(u->addr);
  431. atomic_long_dec(&unix_nr_socks);
  432. local_bh_disable();
  433. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  434. local_bh_enable();
  435. #ifdef UNIX_REFCNT_DEBUG
  436. pr_debug("UNIX %p is destroyed, %ld are still alive.\n", sk,
  437. atomic_long_read(&unix_nr_socks));
  438. #endif
  439. }
  440. static void unix_release_sock(struct sock *sk, int embrion)
  441. {
  442. struct unix_sock *u = unix_sk(sk);
  443. struct path path;
  444. struct sock *skpair;
  445. struct sk_buff *skb;
  446. int state;
  447. unix_remove_socket(sk);
  448. /* Clear state */
  449. unix_state_lock(sk);
  450. sock_orphan(sk);
  451. sk->sk_shutdown = SHUTDOWN_MASK;
  452. path = u->path;
  453. u->path.dentry = NULL;
  454. u->path.mnt = NULL;
  455. state = sk->sk_state;
  456. sk->sk_state = TCP_CLOSE;
  457. unix_state_unlock(sk);
  458. wake_up_interruptible_all(&u->peer_wait);
  459. skpair = unix_peer(sk);
  460. if (skpair != NULL) {
  461. if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
  462. unix_state_lock(skpair);
  463. /* No more writes */
  464. skpair->sk_shutdown = SHUTDOWN_MASK;
  465. if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
  466. skpair->sk_err = ECONNRESET;
  467. unix_state_unlock(skpair);
  468. skpair->sk_state_change(skpair);
  469. sk_wake_async(skpair, SOCK_WAKE_WAITD, POLL_HUP);
  470. }
  471. unix_dgram_peer_wake_disconnect(sk, skpair);
  472. sock_put(skpair); /* It may now die */
  473. unix_peer(sk) = NULL;
  474. }
  475. /* Try to flush out this socket. Throw out buffers at least */
  476. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  477. if (state == TCP_LISTEN)
  478. unix_release_sock(skb->sk, 1);
  479. /* passed fds are erased in the kfree_skb hook */
  480. UNIXCB(skb).consumed = skb->len;
  481. kfree_skb(skb);
  482. }
  483. if (path.dentry)
  484. path_put(&path);
  485. sock_put(sk);
  486. /* ---- Socket is dead now and most probably destroyed ---- */
  487. /*
  488. * Fixme: BSD difference: In BSD all sockets connected to us get
  489. * ECONNRESET and we die on the spot. In Linux we behave
  490. * like files and pipes do and wait for the last
  491. * dereference.
  492. *
  493. * Can't we simply set sock->err?
  494. *
  495. * What the above comment does talk about? --ANK(980817)
  496. */
  497. if (unix_tot_inflight)
  498. unix_gc(); /* Garbage collect fds */
  499. }
  500. static void init_peercred(struct sock *sk)
  501. {
  502. put_pid(sk->sk_peer_pid);
  503. if (sk->sk_peer_cred)
  504. put_cred(sk->sk_peer_cred);
  505. sk->sk_peer_pid = get_pid(task_tgid(current));
  506. sk->sk_peer_cred = get_current_cred();
  507. }
  508. static void copy_peercred(struct sock *sk, struct sock *peersk)
  509. {
  510. put_pid(sk->sk_peer_pid);
  511. if (sk->sk_peer_cred)
  512. put_cred(sk->sk_peer_cred);
  513. sk->sk_peer_pid = get_pid(peersk->sk_peer_pid);
  514. sk->sk_peer_cred = get_cred(peersk->sk_peer_cred);
  515. }
  516. static int unix_listen(struct socket *sock, int backlog)
  517. {
  518. int err;
  519. struct sock *sk = sock->sk;
  520. struct unix_sock *u = unix_sk(sk);
  521. struct pid *old_pid = NULL;
  522. err = -EOPNOTSUPP;
  523. if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
  524. goto out; /* Only stream/seqpacket sockets accept */
  525. err = -EINVAL;
  526. if (!u->addr)
  527. goto out; /* No listens on an unbound socket */
  528. unix_state_lock(sk);
  529. if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
  530. goto out_unlock;
  531. if (backlog > sk->sk_max_ack_backlog)
  532. wake_up_interruptible_all(&u->peer_wait);
  533. sk->sk_max_ack_backlog = backlog;
  534. sk->sk_state = TCP_LISTEN;
  535. /* set credentials so connect can copy them */
  536. init_peercred(sk);
  537. err = 0;
  538. out_unlock:
  539. unix_state_unlock(sk);
  540. put_pid(old_pid);
  541. out:
  542. return err;
  543. }
  544. static int unix_release(struct socket *);
  545. static int unix_bind(struct socket *, struct sockaddr *, int);
  546. static int unix_stream_connect(struct socket *, struct sockaddr *,
  547. int addr_len, int flags);
  548. static int unix_socketpair(struct socket *, struct socket *);
  549. static int unix_accept(struct socket *, struct socket *, int);
  550. static int unix_getname(struct socket *, struct sockaddr *, int *, int);
  551. static unsigned int unix_poll(struct file *, struct socket *, poll_table *);
  552. static unsigned int unix_dgram_poll(struct file *, struct socket *,
  553. poll_table *);
  554. static int unix_ioctl(struct socket *, unsigned int, unsigned long);
  555. static int unix_shutdown(struct socket *, int);
  556. static int unix_stream_sendmsg(struct socket *, struct msghdr *, size_t);
  557. static int unix_stream_recvmsg(struct socket *, struct msghdr *, size_t, int);
  558. static ssize_t unix_stream_sendpage(struct socket *, struct page *, int offset,
  559. size_t size, int flags);
  560. static ssize_t unix_stream_splice_read(struct socket *, loff_t *ppos,
  561. struct pipe_inode_info *, size_t size,
  562. unsigned int flags);
  563. static int unix_dgram_sendmsg(struct socket *, struct msghdr *, size_t);
  564. static int unix_dgram_recvmsg(struct socket *, struct msghdr *, size_t, int);
  565. static int unix_dgram_connect(struct socket *, struct sockaddr *,
  566. int, int);
  567. static int unix_seqpacket_sendmsg(struct socket *, struct msghdr *, size_t);
  568. static int unix_seqpacket_recvmsg(struct socket *, struct msghdr *, size_t,
  569. int);
  570. static int unix_set_peek_off(struct sock *sk, int val)
  571. {
  572. struct unix_sock *u = unix_sk(sk);
  573. if (mutex_lock_interruptible(&u->iolock))
  574. return -EINTR;
  575. sk->sk_peek_off = val;
  576. mutex_unlock(&u->iolock);
  577. return 0;
  578. }
  579. static const struct proto_ops unix_stream_ops = {
  580. .family = PF_UNIX,
  581. .owner = THIS_MODULE,
  582. .release = unix_release,
  583. .bind = unix_bind,
  584. .connect = unix_stream_connect,
  585. .socketpair = unix_socketpair,
  586. .accept = unix_accept,
  587. .getname = unix_getname,
  588. .poll = unix_poll,
  589. .ioctl = unix_ioctl,
  590. .listen = unix_listen,
  591. .shutdown = unix_shutdown,
  592. .setsockopt = sock_no_setsockopt,
  593. .getsockopt = sock_no_getsockopt,
  594. .sendmsg = unix_stream_sendmsg,
  595. .recvmsg = unix_stream_recvmsg,
  596. .mmap = sock_no_mmap,
  597. .sendpage = unix_stream_sendpage,
  598. .splice_read = unix_stream_splice_read,
  599. .set_peek_off = unix_set_peek_off,
  600. };
  601. static const struct proto_ops unix_dgram_ops = {
  602. .family = PF_UNIX,
  603. .owner = THIS_MODULE,
  604. .release = unix_release,
  605. .bind = unix_bind,
  606. .connect = unix_dgram_connect,
  607. .socketpair = unix_socketpair,
  608. .accept = sock_no_accept,
  609. .getname = unix_getname,
  610. .poll = unix_dgram_poll,
  611. .ioctl = unix_ioctl,
  612. .listen = sock_no_listen,
  613. .shutdown = unix_shutdown,
  614. .setsockopt = sock_no_setsockopt,
  615. .getsockopt = sock_no_getsockopt,
  616. .sendmsg = unix_dgram_sendmsg,
  617. .recvmsg = unix_dgram_recvmsg,
  618. .mmap = sock_no_mmap,
  619. .sendpage = sock_no_sendpage,
  620. .set_peek_off = unix_set_peek_off,
  621. };
  622. static const struct proto_ops unix_seqpacket_ops = {
  623. .family = PF_UNIX,
  624. .owner = THIS_MODULE,
  625. .release = unix_release,
  626. .bind = unix_bind,
  627. .connect = unix_stream_connect,
  628. .socketpair = unix_socketpair,
  629. .accept = unix_accept,
  630. .getname = unix_getname,
  631. .poll = unix_dgram_poll,
  632. .ioctl = unix_ioctl,
  633. .listen = unix_listen,
  634. .shutdown = unix_shutdown,
  635. .setsockopt = sock_no_setsockopt,
  636. .getsockopt = sock_no_getsockopt,
  637. .sendmsg = unix_seqpacket_sendmsg,
  638. .recvmsg = unix_seqpacket_recvmsg,
  639. .mmap = sock_no_mmap,
  640. .sendpage = sock_no_sendpage,
  641. .set_peek_off = unix_set_peek_off,
  642. };
  643. static struct proto unix_proto = {
  644. .name = "UNIX",
  645. .owner = THIS_MODULE,
  646. .obj_size = sizeof(struct unix_sock),
  647. };
  648. /*
  649. * AF_UNIX sockets do not interact with hardware, hence they
  650. * dont trigger interrupts - so it's safe for them to have
  651. * bh-unsafe locking for their sk_receive_queue.lock. Split off
  652. * this special lock-class by reinitializing the spinlock key:
  653. */
  654. static struct lock_class_key af_unix_sk_receive_queue_lock_key;
  655. static struct sock *unix_create1(struct net *net, struct socket *sock, int kern)
  656. {
  657. struct sock *sk = NULL;
  658. struct unix_sock *u;
  659. atomic_long_inc(&unix_nr_socks);
  660. if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files())
  661. goto out;
  662. sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto, kern);
  663. if (!sk)
  664. goto out;
  665. sock_init_data(sock, sk);
  666. lockdep_set_class(&sk->sk_receive_queue.lock,
  667. &af_unix_sk_receive_queue_lock_key);
  668. sk->sk_allocation = GFP_KERNEL_ACCOUNT;
  669. sk->sk_write_space = unix_write_space;
  670. sk->sk_max_ack_backlog = net->unx.sysctl_max_dgram_qlen;
  671. sk->sk_destruct = unix_sock_destructor;
  672. u = unix_sk(sk);
  673. u->path.dentry = NULL;
  674. u->path.mnt = NULL;
  675. spin_lock_init(&u->lock);
  676. atomic_long_set(&u->inflight, 0);
  677. INIT_LIST_HEAD(&u->link);
  678. mutex_init(&u->iolock); /* single task reading lock */
  679. mutex_init(&u->bindlock); /* single task binding lock */
  680. init_waitqueue_head(&u->peer_wait);
  681. init_waitqueue_func_entry(&u->peer_wake, unix_dgram_peer_wake_relay);
  682. unix_insert_socket(unix_sockets_unbound(sk), sk);
  683. out:
  684. if (sk == NULL)
  685. atomic_long_dec(&unix_nr_socks);
  686. else {
  687. local_bh_disable();
  688. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  689. local_bh_enable();
  690. }
  691. return sk;
  692. }
  693. static int unix_create(struct net *net, struct socket *sock, int protocol,
  694. int kern)
  695. {
  696. if (protocol && protocol != PF_UNIX)
  697. return -EPROTONOSUPPORT;
  698. sock->state = SS_UNCONNECTED;
  699. switch (sock->type) {
  700. case SOCK_STREAM:
  701. sock->ops = &unix_stream_ops;
  702. break;
  703. /*
  704. * Believe it or not BSD has AF_UNIX, SOCK_RAW though
  705. * nothing uses it.
  706. */
  707. case SOCK_RAW:
  708. sock->type = SOCK_DGRAM;
  709. case SOCK_DGRAM:
  710. sock->ops = &unix_dgram_ops;
  711. break;
  712. case SOCK_SEQPACKET:
  713. sock->ops = &unix_seqpacket_ops;
  714. break;
  715. default:
  716. return -ESOCKTNOSUPPORT;
  717. }
  718. return unix_create1(net, sock, kern) ? 0 : -ENOMEM;
  719. }
  720. static int unix_release(struct socket *sock)
  721. {
  722. struct sock *sk = sock->sk;
  723. if (!sk)
  724. return 0;
  725. unix_release_sock(sk, 0);
  726. sock->sk = NULL;
  727. return 0;
  728. }
  729. static int unix_autobind(struct socket *sock)
  730. {
  731. struct sock *sk = sock->sk;
  732. struct net *net = sock_net(sk);
  733. struct unix_sock *u = unix_sk(sk);
  734. static u32 ordernum = 1;
  735. struct unix_address *addr;
  736. int err;
  737. unsigned int retries = 0;
  738. err = mutex_lock_interruptible(&u->bindlock);
  739. if (err)
  740. return err;
  741. err = 0;
  742. if (u->addr)
  743. goto out;
  744. err = -ENOMEM;
  745. addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL);
  746. if (!addr)
  747. goto out;
  748. addr->name->sun_family = AF_UNIX;
  749. atomic_set(&addr->refcnt, 1);
  750. retry:
  751. addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
  752. addr->hash = unix_hash_fold(csum_partial(addr->name, addr->len, 0));
  753. spin_lock(&unix_table_lock);
  754. ordernum = (ordernum+1)&0xFFFFF;
  755. if (__unix_find_socket_byname(net, addr->name, addr->len, sock->type,
  756. addr->hash)) {
  757. spin_unlock(&unix_table_lock);
  758. /*
  759. * __unix_find_socket_byname() may take long time if many names
  760. * are already in use.
  761. */
  762. cond_resched();
  763. /* Give up if all names seems to be in use. */
  764. if (retries++ == 0xFFFFF) {
  765. err = -ENOSPC;
  766. kfree(addr);
  767. goto out;
  768. }
  769. goto retry;
  770. }
  771. addr->hash ^= sk->sk_type;
  772. __unix_remove_socket(sk);
  773. u->addr = addr;
  774. __unix_insert_socket(&unix_socket_table[addr->hash], sk);
  775. spin_unlock(&unix_table_lock);
  776. err = 0;
  777. out: mutex_unlock(&u->bindlock);
  778. return err;
  779. }
  780. static struct sock *unix_find_other(struct net *net,
  781. struct sockaddr_un *sunname, int len,
  782. int type, unsigned int hash, int *error)
  783. {
  784. struct sock *u;
  785. struct path path;
  786. int err = 0;
  787. if (sunname->sun_path[0]) {
  788. struct inode *inode;
  789. err = kern_path(sunname->sun_path, LOOKUP_FOLLOW, &path);
  790. if (err)
  791. goto fail;
  792. inode = d_real_inode(path.dentry);
  793. err = inode_permission(inode, MAY_WRITE);
  794. if (err)
  795. goto put_fail;
  796. err = -ECONNREFUSED;
  797. if (!S_ISSOCK(inode->i_mode))
  798. goto put_fail;
  799. u = unix_find_socket_byinode(inode);
  800. if (!u)
  801. goto put_fail;
  802. if (u->sk_type == type)
  803. touch_atime(&path);
  804. path_put(&path);
  805. err = -EPROTOTYPE;
  806. if (u->sk_type != type) {
  807. sock_put(u);
  808. goto fail;
  809. }
  810. } else {
  811. err = -ECONNREFUSED;
  812. u = unix_find_socket_byname(net, sunname, len, type, hash);
  813. if (u) {
  814. struct dentry *dentry;
  815. dentry = unix_sk(u)->path.dentry;
  816. if (dentry)
  817. touch_atime(&unix_sk(u)->path);
  818. } else
  819. goto fail;
  820. }
  821. return u;
  822. put_fail:
  823. path_put(&path);
  824. fail:
  825. *error = err;
  826. return NULL;
  827. }
  828. static int unix_mknod(const char *sun_path, umode_t mode, struct path *res)
  829. {
  830. struct dentry *dentry;
  831. struct path path;
  832. int err = 0;
  833. /*
  834. * Get the parent directory, calculate the hash for last
  835. * component.
  836. */
  837. dentry = kern_path_create(AT_FDCWD, sun_path, &path, 0);
  838. err = PTR_ERR(dentry);
  839. if (IS_ERR(dentry))
  840. return err;
  841. /*
  842. * All right, let's create it.
  843. */
  844. err = security_path_mknod(&path, dentry, mode, 0);
  845. if (!err) {
  846. err = vfs_mknod(d_inode(path.dentry), dentry, mode, 0);
  847. if (!err) {
  848. res->mnt = mntget(path.mnt);
  849. res->dentry = dget(dentry);
  850. }
  851. }
  852. done_path_create(&path, dentry);
  853. return err;
  854. }
  855. static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  856. {
  857. struct sock *sk = sock->sk;
  858. struct net *net = sock_net(sk);
  859. struct unix_sock *u = unix_sk(sk);
  860. struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
  861. char *sun_path = sunaddr->sun_path;
  862. int err;
  863. unsigned int hash;
  864. struct unix_address *addr;
  865. struct hlist_head *list;
  866. struct path path = { NULL, NULL };
  867. err = -EINVAL;
  868. if (addr_len < offsetofend(struct sockaddr_un, sun_family) ||
  869. sunaddr->sun_family != AF_UNIX)
  870. goto out;
  871. if (addr_len == sizeof(short)) {
  872. err = unix_autobind(sock);
  873. goto out;
  874. }
  875. err = unix_mkname(sunaddr, addr_len, &hash);
  876. if (err < 0)
  877. goto out;
  878. addr_len = err;
  879. if (sun_path[0]) {
  880. umode_t mode = S_IFSOCK |
  881. (SOCK_INODE(sock)->i_mode & ~current_umask());
  882. err = unix_mknod(sun_path, mode, &path);
  883. if (err) {
  884. if (err == -EEXIST)
  885. err = -EADDRINUSE;
  886. goto out;
  887. }
  888. }
  889. err = mutex_lock_interruptible(&u->bindlock);
  890. if (err)
  891. goto out_put;
  892. err = -EINVAL;
  893. if (u->addr)
  894. goto out_up;
  895. err = -ENOMEM;
  896. addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
  897. if (!addr)
  898. goto out_up;
  899. memcpy(addr->name, sunaddr, addr_len);
  900. addr->len = addr_len;
  901. addr->hash = hash ^ sk->sk_type;
  902. atomic_set(&addr->refcnt, 1);
  903. if (sun_path[0]) {
  904. addr->hash = UNIX_HASH_SIZE;
  905. hash = d_real_inode(path.dentry)->i_ino & (UNIX_HASH_SIZE - 1);
  906. spin_lock(&unix_table_lock);
  907. u->path = path;
  908. list = &unix_socket_table[hash];
  909. } else {
  910. spin_lock(&unix_table_lock);
  911. err = -EADDRINUSE;
  912. if (__unix_find_socket_byname(net, sunaddr, addr_len,
  913. sk->sk_type, hash)) {
  914. unix_release_addr(addr);
  915. goto out_unlock;
  916. }
  917. list = &unix_socket_table[addr->hash];
  918. }
  919. err = 0;
  920. __unix_remove_socket(sk);
  921. u->addr = addr;
  922. __unix_insert_socket(list, sk);
  923. out_unlock:
  924. spin_unlock(&unix_table_lock);
  925. out_up:
  926. mutex_unlock(&u->bindlock);
  927. out_put:
  928. if (err)
  929. path_put(&path);
  930. out:
  931. return err;
  932. }
  933. static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)
  934. {
  935. if (unlikely(sk1 == sk2) || !sk2) {
  936. unix_state_lock(sk1);
  937. return;
  938. }
  939. if (sk1 < sk2) {
  940. unix_state_lock(sk1);
  941. unix_state_lock_nested(sk2);
  942. } else {
  943. unix_state_lock(sk2);
  944. unix_state_lock_nested(sk1);
  945. }
  946. }
  947. static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2)
  948. {
  949. if (unlikely(sk1 == sk2) || !sk2) {
  950. unix_state_unlock(sk1);
  951. return;
  952. }
  953. unix_state_unlock(sk1);
  954. unix_state_unlock(sk2);
  955. }
  956. static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
  957. int alen, int flags)
  958. {
  959. struct sock *sk = sock->sk;
  960. struct net *net = sock_net(sk);
  961. struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr;
  962. struct sock *other;
  963. unsigned int hash;
  964. int err;
  965. err = -EINVAL;
  966. if (alen < offsetofend(struct sockaddr, sa_family))
  967. goto out;
  968. if (addr->sa_family != AF_UNSPEC) {
  969. err = unix_mkname(sunaddr, alen, &hash);
  970. if (err < 0)
  971. goto out;
  972. alen = err;
  973. if (test_bit(SOCK_PASSCRED, &sock->flags) &&
  974. !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0)
  975. goto out;
  976. restart:
  977. other = unix_find_other(net, sunaddr, alen, sock->type, hash, &err);
  978. if (!other)
  979. goto out;
  980. unix_state_double_lock(sk, other);
  981. /* Apparently VFS overslept socket death. Retry. */
  982. if (sock_flag(other, SOCK_DEAD)) {
  983. unix_state_double_unlock(sk, other);
  984. sock_put(other);
  985. goto restart;
  986. }
  987. err = -EPERM;
  988. if (!unix_may_send(sk, other))
  989. goto out_unlock;
  990. err = security_unix_may_send(sk->sk_socket, other->sk_socket);
  991. if (err)
  992. goto out_unlock;
  993. } else {
  994. /*
  995. * 1003.1g breaking connected state with AF_UNSPEC
  996. */
  997. other = NULL;
  998. unix_state_double_lock(sk, other);
  999. }
  1000. /*
  1001. * If it was connected, reconnect.
  1002. */
  1003. if (unix_peer(sk)) {
  1004. struct sock *old_peer = unix_peer(sk);
  1005. unix_peer(sk) = other;
  1006. unix_dgram_peer_wake_disconnect_wakeup(sk, old_peer);
  1007. unix_state_double_unlock(sk, other);
  1008. if (other != old_peer)
  1009. unix_dgram_disconnected(sk, old_peer);
  1010. sock_put(old_peer);
  1011. } else {
  1012. unix_peer(sk) = other;
  1013. unix_state_double_unlock(sk, other);
  1014. }
  1015. return 0;
  1016. out_unlock:
  1017. unix_state_double_unlock(sk, other);
  1018. sock_put(other);
  1019. out:
  1020. return err;
  1021. }
  1022. static long unix_wait_for_peer(struct sock *other, long timeo)
  1023. {
  1024. struct unix_sock *u = unix_sk(other);
  1025. int sched;
  1026. DEFINE_WAIT(wait);
  1027. prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
  1028. sched = !sock_flag(other, SOCK_DEAD) &&
  1029. !(other->sk_shutdown & RCV_SHUTDOWN) &&
  1030. unix_recvq_full(other);
  1031. unix_state_unlock(other);
  1032. if (sched)
  1033. timeo = schedule_timeout(timeo);
  1034. finish_wait(&u->peer_wait, &wait);
  1035. return timeo;
  1036. }
  1037. static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
  1038. int addr_len, int flags)
  1039. {
  1040. struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
  1041. struct sock *sk = sock->sk;
  1042. struct net *net = sock_net(sk);
  1043. struct unix_sock *u = unix_sk(sk), *newu, *otheru;
  1044. struct sock *newsk = NULL;
  1045. struct sock *other = NULL;
  1046. struct sk_buff *skb = NULL;
  1047. unsigned int hash;
  1048. int st;
  1049. int err;
  1050. long timeo;
  1051. err = unix_mkname(sunaddr, addr_len, &hash);
  1052. if (err < 0)
  1053. goto out;
  1054. addr_len = err;
  1055. if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr &&
  1056. (err = unix_autobind(sock)) != 0)
  1057. goto out;
  1058. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  1059. /* First of all allocate resources.
  1060. If we will make it after state is locked,
  1061. we will have to recheck all again in any case.
  1062. */
  1063. err = -ENOMEM;
  1064. /* create new sock for complete connection */
  1065. newsk = unix_create1(sock_net(sk), NULL, 0);
  1066. if (newsk == NULL)
  1067. goto out;
  1068. /* Allocate skb for sending to listening sock */
  1069. skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
  1070. if (skb == NULL)
  1071. goto out;
  1072. restart:
  1073. /* Find listening sock. */
  1074. other = unix_find_other(net, sunaddr, addr_len, sk->sk_type, hash, &err);
  1075. if (!other)
  1076. goto out;
  1077. /* Latch state of peer */
  1078. unix_state_lock(other);
  1079. /* Apparently VFS overslept socket death. Retry. */
  1080. if (sock_flag(other, SOCK_DEAD)) {
  1081. unix_state_unlock(other);
  1082. sock_put(other);
  1083. goto restart;
  1084. }
  1085. err = -ECONNREFUSED;
  1086. if (other->sk_state != TCP_LISTEN)
  1087. goto out_unlock;
  1088. if (other->sk_shutdown & RCV_SHUTDOWN)
  1089. goto out_unlock;
  1090. if (unix_recvq_full(other)) {
  1091. err = -EAGAIN;
  1092. if (!timeo)
  1093. goto out_unlock;
  1094. timeo = unix_wait_for_peer(other, timeo);
  1095. err = sock_intr_errno(timeo);
  1096. if (signal_pending(current))
  1097. goto out;
  1098. sock_put(other);
  1099. goto restart;
  1100. }
  1101. /* Latch our state.
  1102. It is tricky place. We need to grab our state lock and cannot
  1103. drop lock on peer. It is dangerous because deadlock is
  1104. possible. Connect to self case and simultaneous
  1105. attempt to connect are eliminated by checking socket
  1106. state. other is TCP_LISTEN, if sk is TCP_LISTEN we
  1107. check this before attempt to grab lock.
  1108. Well, and we have to recheck the state after socket locked.
  1109. */
  1110. st = sk->sk_state;
  1111. switch (st) {
  1112. case TCP_CLOSE:
  1113. /* This is ok... continue with connect */
  1114. break;
  1115. case TCP_ESTABLISHED:
  1116. /* Socket is already connected */
  1117. err = -EISCONN;
  1118. goto out_unlock;
  1119. default:
  1120. err = -EINVAL;
  1121. goto out_unlock;
  1122. }
  1123. unix_state_lock_nested(sk);
  1124. if (sk->sk_state != st) {
  1125. unix_state_unlock(sk);
  1126. unix_state_unlock(other);
  1127. sock_put(other);
  1128. goto restart;
  1129. }
  1130. err = security_unix_stream_connect(sk, other, newsk);
  1131. if (err) {
  1132. unix_state_unlock(sk);
  1133. goto out_unlock;
  1134. }
  1135. /* The way is open! Fastly set all the necessary fields... */
  1136. sock_hold(sk);
  1137. unix_peer(newsk) = sk;
  1138. newsk->sk_state = TCP_ESTABLISHED;
  1139. newsk->sk_type = sk->sk_type;
  1140. init_peercred(newsk);
  1141. newu = unix_sk(newsk);
  1142. RCU_INIT_POINTER(newsk->sk_wq, &newu->peer_wq);
  1143. otheru = unix_sk(other);
  1144. /* copy address information from listening to new sock*/
  1145. if (otheru->addr) {
  1146. atomic_inc(&otheru->addr->refcnt);
  1147. newu->addr = otheru->addr;
  1148. }
  1149. if (otheru->path.dentry) {
  1150. path_get(&otheru->path);
  1151. newu->path = otheru->path;
  1152. }
  1153. /* Set credentials */
  1154. copy_peercred(sk, other);
  1155. sock->state = SS_CONNECTED;
  1156. sk->sk_state = TCP_ESTABLISHED;
  1157. sock_hold(newsk);
  1158. smp_mb__after_atomic(); /* sock_hold() does an atomic_inc() */
  1159. unix_peer(sk) = newsk;
  1160. unix_state_unlock(sk);
  1161. /* take ten and and send info to listening sock */
  1162. spin_lock(&other->sk_receive_queue.lock);
  1163. __skb_queue_tail(&other->sk_receive_queue, skb);
  1164. spin_unlock(&other->sk_receive_queue.lock);
  1165. unix_state_unlock(other);
  1166. other->sk_data_ready(other);
  1167. sock_put(other);
  1168. return 0;
  1169. out_unlock:
  1170. if (other)
  1171. unix_state_unlock(other);
  1172. out:
  1173. kfree_skb(skb);
  1174. if (newsk)
  1175. unix_release_sock(newsk, 0);
  1176. if (other)
  1177. sock_put(other);
  1178. return err;
  1179. }
  1180. static int unix_socketpair(struct socket *socka, struct socket *sockb)
  1181. {
  1182. struct sock *ska = socka->sk, *skb = sockb->sk;
  1183. /* Join our sockets back to back */
  1184. sock_hold(ska);
  1185. sock_hold(skb);
  1186. unix_peer(ska) = skb;
  1187. unix_peer(skb) = ska;
  1188. init_peercred(ska);
  1189. init_peercred(skb);
  1190. if (ska->sk_type != SOCK_DGRAM) {
  1191. ska->sk_state = TCP_ESTABLISHED;
  1192. skb->sk_state = TCP_ESTABLISHED;
  1193. socka->state = SS_CONNECTED;
  1194. sockb->state = SS_CONNECTED;
  1195. }
  1196. return 0;
  1197. }
  1198. static void unix_sock_inherit_flags(const struct socket *old,
  1199. struct socket *new)
  1200. {
  1201. if (test_bit(SOCK_PASSCRED, &old->flags))
  1202. set_bit(SOCK_PASSCRED, &new->flags);
  1203. if (test_bit(SOCK_PASSSEC, &old->flags))
  1204. set_bit(SOCK_PASSSEC, &new->flags);
  1205. }
  1206. static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
  1207. {
  1208. struct sock *sk = sock->sk;
  1209. struct sock *tsk;
  1210. struct sk_buff *skb;
  1211. int err;
  1212. err = -EOPNOTSUPP;
  1213. if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
  1214. goto out;
  1215. err = -EINVAL;
  1216. if (sk->sk_state != TCP_LISTEN)
  1217. goto out;
  1218. /* If socket state is TCP_LISTEN it cannot change (for now...),
  1219. * so that no locks are necessary.
  1220. */
  1221. skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
  1222. if (!skb) {
  1223. /* This means receive shutdown. */
  1224. if (err == 0)
  1225. err = -EINVAL;
  1226. goto out;
  1227. }
  1228. tsk = skb->sk;
  1229. skb_free_datagram(sk, skb);
  1230. wake_up_interruptible(&unix_sk(sk)->peer_wait);
  1231. /* attach accepted sock to socket */
  1232. unix_state_lock(tsk);
  1233. newsock->state = SS_CONNECTED;
  1234. unix_sock_inherit_flags(sock, newsock);
  1235. sock_graft(tsk, newsock);
  1236. unix_state_unlock(tsk);
  1237. return 0;
  1238. out:
  1239. return err;
  1240. }
  1241. static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
  1242. {
  1243. struct sock *sk = sock->sk;
  1244. struct unix_sock *u;
  1245. DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, uaddr);
  1246. int err = 0;
  1247. if (peer) {
  1248. sk = unix_peer_get(sk);
  1249. err = -ENOTCONN;
  1250. if (!sk)
  1251. goto out;
  1252. err = 0;
  1253. } else {
  1254. sock_hold(sk);
  1255. }
  1256. u = unix_sk(sk);
  1257. unix_state_lock(sk);
  1258. if (!u->addr) {
  1259. sunaddr->sun_family = AF_UNIX;
  1260. sunaddr->sun_path[0] = 0;
  1261. *uaddr_len = sizeof(short);
  1262. } else {
  1263. struct unix_address *addr = u->addr;
  1264. *uaddr_len = addr->len;
  1265. memcpy(sunaddr, addr->name, *uaddr_len);
  1266. }
  1267. unix_state_unlock(sk);
  1268. sock_put(sk);
  1269. out:
  1270. return err;
  1271. }
  1272. static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
  1273. {
  1274. int i;
  1275. scm->fp = UNIXCB(skb).fp;
  1276. UNIXCB(skb).fp = NULL;
  1277. for (i = scm->fp->count-1; i >= 0; i--)
  1278. unix_notinflight(scm->fp->user, scm->fp->fp[i]);
  1279. }
  1280. static void unix_destruct_scm(struct sk_buff *skb)
  1281. {
  1282. struct scm_cookie scm;
  1283. memset(&scm, 0, sizeof(scm));
  1284. scm.pid = UNIXCB(skb).pid;
  1285. if (UNIXCB(skb).fp)
  1286. unix_detach_fds(&scm, skb);
  1287. /* Alas, it calls VFS */
  1288. /* So fscking what? fput() had been SMP-safe since the last Summer */
  1289. scm_destroy(&scm);
  1290. sock_wfree(skb);
  1291. }
  1292. /*
  1293. * The "user->unix_inflight" variable is protected by the garbage
  1294. * collection lock, and we just read it locklessly here. If you go
  1295. * over the limit, there might be a tiny race in actually noticing
  1296. * it across threads. Tough.
  1297. */
  1298. static inline bool too_many_unix_fds(struct task_struct *p)
  1299. {
  1300. struct user_struct *user = current_user();
  1301. if (unlikely(user->unix_inflight > task_rlimit(p, RLIMIT_NOFILE)))
  1302. return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
  1303. return false;
  1304. }
  1305. #define MAX_RECURSION_LEVEL 4
  1306. static int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
  1307. {
  1308. int i;
  1309. unsigned char max_level = 0;
  1310. if (too_many_unix_fds(current))
  1311. return -ETOOMANYREFS;
  1312. for (i = scm->fp->count - 1; i >= 0; i--) {
  1313. struct sock *sk = unix_get_socket(scm->fp->fp[i]);
  1314. if (sk)
  1315. max_level = max(max_level,
  1316. unix_sk(sk)->recursion_level);
  1317. }
  1318. if (unlikely(max_level > MAX_RECURSION_LEVEL))
  1319. return -ETOOMANYREFS;
  1320. /*
  1321. * Need to duplicate file references for the sake of garbage
  1322. * collection. Otherwise a socket in the fps might become a
  1323. * candidate for GC while the skb is not yet queued.
  1324. */
  1325. UNIXCB(skb).fp = scm_fp_dup(scm->fp);
  1326. if (!UNIXCB(skb).fp)
  1327. return -ENOMEM;
  1328. for (i = scm->fp->count - 1; i >= 0; i--)
  1329. unix_inflight(scm->fp->user, scm->fp->fp[i]);
  1330. return max_level;
  1331. }
  1332. static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds)
  1333. {
  1334. int err = 0;
  1335. UNIXCB(skb).pid = get_pid(scm->pid);
  1336. UNIXCB(skb).uid = scm->creds.uid;
  1337. UNIXCB(skb).gid = scm->creds.gid;
  1338. UNIXCB(skb).fp = NULL;
  1339. unix_get_secdata(scm, skb);
  1340. if (scm->fp && send_fds)
  1341. err = unix_attach_fds(scm, skb);
  1342. skb->destructor = unix_destruct_scm;
  1343. return err;
  1344. }
  1345. static bool unix_passcred_enabled(const struct socket *sock,
  1346. const struct sock *other)
  1347. {
  1348. return test_bit(SOCK_PASSCRED, &sock->flags) ||
  1349. !other->sk_socket ||
  1350. test_bit(SOCK_PASSCRED, &other->sk_socket->flags);
  1351. }
  1352. /*
  1353. * Some apps rely on write() giving SCM_CREDENTIALS
  1354. * We include credentials if source or destination socket
  1355. * asserted SOCK_PASSCRED.
  1356. */
  1357. static void maybe_add_creds(struct sk_buff *skb, const struct socket *sock,
  1358. const struct sock *other)
  1359. {
  1360. if (UNIXCB(skb).pid)
  1361. return;
  1362. if (unix_passcred_enabled(sock, other)) {
  1363. UNIXCB(skb).pid = get_pid(task_tgid(current));
  1364. current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid);
  1365. }
  1366. }
  1367. static int maybe_init_creds(struct scm_cookie *scm,
  1368. struct socket *socket,
  1369. const struct sock *other)
  1370. {
  1371. int err;
  1372. struct msghdr msg = { .msg_controllen = 0 };
  1373. err = scm_send(socket, &msg, scm, false);
  1374. if (err)
  1375. return err;
  1376. if (unix_passcred_enabled(socket, other)) {
  1377. scm->pid = get_pid(task_tgid(current));
  1378. current_uid_gid(&scm->creds.uid, &scm->creds.gid);
  1379. }
  1380. return err;
  1381. }
  1382. static bool unix_skb_scm_eq(struct sk_buff *skb,
  1383. struct scm_cookie *scm)
  1384. {
  1385. const struct unix_skb_parms *u = &UNIXCB(skb);
  1386. return u->pid == scm->pid &&
  1387. uid_eq(u->uid, scm->creds.uid) &&
  1388. gid_eq(u->gid, scm->creds.gid) &&
  1389. unix_secdata_eq(scm, skb);
  1390. }
  1391. /*
  1392. * Send AF_UNIX data.
  1393. */
  1394. static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
  1395. size_t len)
  1396. {
  1397. struct sock *sk = sock->sk;
  1398. struct net *net = sock_net(sk);
  1399. struct unix_sock *u = unix_sk(sk);
  1400. DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, msg->msg_name);
  1401. struct sock *other = NULL;
  1402. int namelen = 0; /* fake GCC */
  1403. int err;
  1404. unsigned int hash;
  1405. struct sk_buff *skb;
  1406. long timeo;
  1407. struct scm_cookie scm;
  1408. int max_level;
  1409. int data_len = 0;
  1410. int sk_locked;
  1411. wait_for_unix_gc();
  1412. err = scm_send(sock, msg, &scm, false);
  1413. if (err < 0)
  1414. return err;
  1415. err = -EOPNOTSUPP;
  1416. if (msg->msg_flags&MSG_OOB)
  1417. goto out;
  1418. if (msg->msg_namelen) {
  1419. err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
  1420. if (err < 0)
  1421. goto out;
  1422. namelen = err;
  1423. } else {
  1424. sunaddr = NULL;
  1425. err = -ENOTCONN;
  1426. other = unix_peer_get(sk);
  1427. if (!other)
  1428. goto out;
  1429. }
  1430. if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr
  1431. && (err = unix_autobind(sock)) != 0)
  1432. goto out;
  1433. err = -EMSGSIZE;
  1434. if (len > sk->sk_sndbuf - 32)
  1435. goto out;
  1436. if (len > SKB_MAX_ALLOC) {
  1437. data_len = min_t(size_t,
  1438. len - SKB_MAX_ALLOC,
  1439. MAX_SKB_FRAGS * PAGE_SIZE);
  1440. data_len = PAGE_ALIGN(data_len);
  1441. BUILD_BUG_ON(SKB_MAX_ALLOC < PAGE_SIZE);
  1442. }
  1443. skb = sock_alloc_send_pskb(sk, len - data_len, data_len,
  1444. msg->msg_flags & MSG_DONTWAIT, &err,
  1445. PAGE_ALLOC_COSTLY_ORDER);
  1446. if (skb == NULL)
  1447. goto out;
  1448. err = unix_scm_to_skb(&scm, skb, true);
  1449. if (err < 0)
  1450. goto out_free;
  1451. max_level = err + 1;
  1452. skb_put(skb, len - data_len);
  1453. skb->data_len = data_len;
  1454. skb->len = len;
  1455. err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
  1456. if (err)
  1457. goto out_free;
  1458. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1459. restart:
  1460. if (!other) {
  1461. err = -ECONNRESET;
  1462. if (sunaddr == NULL)
  1463. goto out_free;
  1464. other = unix_find_other(net, sunaddr, namelen, sk->sk_type,
  1465. hash, &err);
  1466. if (other == NULL)
  1467. goto out_free;
  1468. }
  1469. if (sk_filter(other, skb) < 0) {
  1470. /* Toss the packet but do not return any error to the sender */
  1471. err = len;
  1472. goto out_free;
  1473. }
  1474. sk_locked = 0;
  1475. unix_state_lock(other);
  1476. restart_locked:
  1477. err = -EPERM;
  1478. if (!unix_may_send(sk, other))
  1479. goto out_unlock;
  1480. if (unlikely(sock_flag(other, SOCK_DEAD))) {
  1481. /*
  1482. * Check with 1003.1g - what should
  1483. * datagram error
  1484. */
  1485. unix_state_unlock(other);
  1486. sock_put(other);
  1487. if (!sk_locked)
  1488. unix_state_lock(sk);
  1489. err = 0;
  1490. if (unix_peer(sk) == other) {
  1491. unix_peer(sk) = NULL;
  1492. unix_dgram_peer_wake_disconnect_wakeup(sk, other);
  1493. unix_state_unlock(sk);
  1494. unix_dgram_disconnected(sk, other);
  1495. sock_put(other);
  1496. err = -ECONNREFUSED;
  1497. } else {
  1498. unix_state_unlock(sk);
  1499. }
  1500. other = NULL;
  1501. if (err)
  1502. goto out_free;
  1503. goto restart;
  1504. }
  1505. err = -EPIPE;
  1506. if (other->sk_shutdown & RCV_SHUTDOWN)
  1507. goto out_unlock;
  1508. if (sk->sk_type != SOCK_SEQPACKET) {
  1509. err = security_unix_may_send(sk->sk_socket, other->sk_socket);
  1510. if (err)
  1511. goto out_unlock;
  1512. }
  1513. /* other == sk && unix_peer(other) != sk if
  1514. * - unix_peer(sk) == NULL, destination address bound to sk
  1515. * - unix_peer(sk) == sk by time of get but disconnected before lock
  1516. */
  1517. if (other != sk &&
  1518. unlikely(unix_peer(other) != sk && unix_recvq_full(other))) {
  1519. if (timeo) {
  1520. timeo = unix_wait_for_peer(other, timeo);
  1521. err = sock_intr_errno(timeo);
  1522. if (signal_pending(current))
  1523. goto out_free;
  1524. goto restart;
  1525. }
  1526. if (!sk_locked) {
  1527. unix_state_unlock(other);
  1528. unix_state_double_lock(sk, other);
  1529. }
  1530. if (unix_peer(sk) != other ||
  1531. unix_dgram_peer_wake_me(sk, other)) {
  1532. err = -EAGAIN;
  1533. sk_locked = 1;
  1534. goto out_unlock;
  1535. }
  1536. if (!sk_locked) {
  1537. sk_locked = 1;
  1538. goto restart_locked;
  1539. }
  1540. }
  1541. if (unlikely(sk_locked))
  1542. unix_state_unlock(sk);
  1543. if (sock_flag(other, SOCK_RCVTSTAMP))
  1544. __net_timestamp(skb);
  1545. maybe_add_creds(skb, sock, other);
  1546. skb_queue_tail(&other->sk_receive_queue, skb);
  1547. if (max_level > unix_sk(other)->recursion_level)
  1548. unix_sk(other)->recursion_level = max_level;
  1549. unix_state_unlock(other);
  1550. other->sk_data_ready(other);
  1551. sock_put(other);
  1552. scm_destroy(&scm);
  1553. return len;
  1554. out_unlock:
  1555. if (sk_locked)
  1556. unix_state_unlock(sk);
  1557. unix_state_unlock(other);
  1558. out_free:
  1559. kfree_skb(skb);
  1560. out:
  1561. if (other)
  1562. sock_put(other);
  1563. scm_destroy(&scm);
  1564. return err;
  1565. }
  1566. /* We use paged skbs for stream sockets, and limit occupancy to 32768
  1567. * bytes, and a minimun of a full page.
  1568. */
  1569. #define UNIX_SKB_FRAGS_SZ (PAGE_SIZE << get_order(32768))
  1570. static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
  1571. size_t len)
  1572. {
  1573. struct sock *sk = sock->sk;
  1574. struct sock *other = NULL;
  1575. int err, size;
  1576. struct sk_buff *skb;
  1577. int sent = 0;
  1578. struct scm_cookie scm;
  1579. bool fds_sent = false;
  1580. int max_level;
  1581. int data_len;
  1582. wait_for_unix_gc();
  1583. err = scm_send(sock, msg, &scm, false);
  1584. if (err < 0)
  1585. return err;
  1586. err = -EOPNOTSUPP;
  1587. if (msg->msg_flags&MSG_OOB)
  1588. goto out_err;
  1589. if (msg->msg_namelen) {
  1590. err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
  1591. goto out_err;
  1592. } else {
  1593. err = -ENOTCONN;
  1594. other = unix_peer(sk);
  1595. if (!other)
  1596. goto out_err;
  1597. }
  1598. if (sk->sk_shutdown & SEND_SHUTDOWN)
  1599. goto pipe_err;
  1600. while (sent < len) {
  1601. size = len - sent;
  1602. /* Keep two messages in the pipe so it schedules better */
  1603. size = min_t(int, size, (sk->sk_sndbuf >> 1) - 64);
  1604. /* allow fallback to order-0 allocations */
  1605. size = min_t(int, size, SKB_MAX_HEAD(0) + UNIX_SKB_FRAGS_SZ);
  1606. data_len = max_t(int, 0, size - SKB_MAX_HEAD(0));
  1607. data_len = min_t(size_t, size, PAGE_ALIGN(data_len));
  1608. skb = sock_alloc_send_pskb(sk, size - data_len, data_len,
  1609. msg->msg_flags & MSG_DONTWAIT, &err,
  1610. get_order(UNIX_SKB_FRAGS_SZ));
  1611. if (!skb)
  1612. goto out_err;
  1613. /* Only send the fds in the first buffer */
  1614. err = unix_scm_to_skb(&scm, skb, !fds_sent);
  1615. if (err < 0) {
  1616. kfree_skb(skb);
  1617. goto out_err;
  1618. }
  1619. max_level = err + 1;
  1620. fds_sent = true;
  1621. skb_put(skb, size - data_len);
  1622. skb->data_len = data_len;
  1623. skb->len = size;
  1624. err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size);
  1625. if (err) {
  1626. kfree_skb(skb);
  1627. goto out_err;
  1628. }
  1629. unix_state_lock(other);
  1630. if (sock_flag(other, SOCK_DEAD) ||
  1631. (other->sk_shutdown & RCV_SHUTDOWN))
  1632. goto pipe_err_free;
  1633. maybe_add_creds(skb, sock, other);
  1634. skb_queue_tail(&other->sk_receive_queue, skb);
  1635. if (max_level > unix_sk(other)->recursion_level)
  1636. unix_sk(other)->recursion_level = max_level;
  1637. unix_state_unlock(other);
  1638. other->sk_data_ready(other);
  1639. sent += size;
  1640. }
  1641. scm_destroy(&scm);
  1642. return sent;
  1643. pipe_err_free:
  1644. unix_state_unlock(other);
  1645. kfree_skb(skb);
  1646. pipe_err:
  1647. if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
  1648. send_sig(SIGPIPE, current, 0);
  1649. err = -EPIPE;
  1650. out_err:
  1651. scm_destroy(&scm);
  1652. return sent ? : err;
  1653. }
  1654. static ssize_t unix_stream_sendpage(struct socket *socket, struct page *page,
  1655. int offset, size_t size, int flags)
  1656. {
  1657. int err;
  1658. bool send_sigpipe = false;
  1659. bool init_scm = true;
  1660. struct scm_cookie scm;
  1661. struct sock *other, *sk = socket->sk;
  1662. struct sk_buff *skb, *newskb = NULL, *tail = NULL;
  1663. if (flags & MSG_OOB)
  1664. return -EOPNOTSUPP;
  1665. other = unix_peer(sk);
  1666. if (!other || sk->sk_state != TCP_ESTABLISHED)
  1667. return -ENOTCONN;
  1668. if (false) {
  1669. alloc_skb:
  1670. unix_state_unlock(other);
  1671. mutex_unlock(&unix_sk(other)->iolock);
  1672. newskb = sock_alloc_send_pskb(sk, 0, 0, flags & MSG_DONTWAIT,
  1673. &err, 0);
  1674. if (!newskb)
  1675. goto err;
  1676. }
  1677. /* we must acquire iolock as we modify already present
  1678. * skbs in the sk_receive_queue and mess with skb->len
  1679. */
  1680. err = mutex_lock_interruptible(&unix_sk(other)->iolock);
  1681. if (err) {
  1682. err = flags & MSG_DONTWAIT ? -EAGAIN : -ERESTARTSYS;
  1683. goto err;
  1684. }
  1685. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  1686. err = -EPIPE;
  1687. send_sigpipe = true;
  1688. goto err_unlock;
  1689. }
  1690. unix_state_lock(other);
  1691. if (sock_flag(other, SOCK_DEAD) ||
  1692. other->sk_shutdown & RCV_SHUTDOWN) {
  1693. err = -EPIPE;
  1694. send_sigpipe = true;
  1695. goto err_state_unlock;
  1696. }
  1697. if (init_scm) {
  1698. err = maybe_init_creds(&scm, socket, other);
  1699. if (err)
  1700. goto err_state_unlock;
  1701. init_scm = false;
  1702. }
  1703. skb = skb_peek_tail(&other->sk_receive_queue);
  1704. if (tail && tail == skb) {
  1705. skb = newskb;
  1706. } else if (!skb || !unix_skb_scm_eq(skb, &scm)) {
  1707. if (newskb) {
  1708. skb = newskb;
  1709. } else {
  1710. tail = skb;
  1711. goto alloc_skb;
  1712. }
  1713. } else if (newskb) {
  1714. /* this is fast path, we don't necessarily need to
  1715. * call to kfree_skb even though with newskb == NULL
  1716. * this - does no harm
  1717. */
  1718. consume_skb(newskb);
  1719. newskb = NULL;
  1720. }
  1721. if (skb_append_pagefrags(skb, page, offset, size)) {
  1722. tail = skb;
  1723. goto alloc_skb;
  1724. }
  1725. skb->len += size;
  1726. skb->data_len += size;
  1727. skb->truesize += size;
  1728. atomic_add(size, &sk->sk_wmem_alloc);
  1729. if (newskb) {
  1730. err = unix_scm_to_skb(&scm, skb, false);
  1731. if (err)
  1732. goto err_state_unlock;
  1733. spin_lock(&other->sk_receive_queue.lock);
  1734. __skb_queue_tail(&other->sk_receive_queue, newskb);
  1735. spin_unlock(&other->sk_receive_queue.lock);
  1736. }
  1737. unix_state_unlock(other);
  1738. mutex_unlock(&unix_sk(other)->iolock);
  1739. other->sk_data_ready(other);
  1740. scm_destroy(&scm);
  1741. return size;
  1742. err_state_unlock:
  1743. unix_state_unlock(other);
  1744. err_unlock:
  1745. mutex_unlock(&unix_sk(other)->iolock);
  1746. err:
  1747. kfree_skb(newskb);
  1748. if (send_sigpipe && !(flags & MSG_NOSIGNAL))
  1749. send_sig(SIGPIPE, current, 0);
  1750. if (!init_scm)
  1751. scm_destroy(&scm);
  1752. return err;
  1753. }
  1754. static int unix_seqpacket_sendmsg(struct socket *sock, struct msghdr *msg,
  1755. size_t len)
  1756. {
  1757. int err;
  1758. struct sock *sk = sock->sk;
  1759. err = sock_error(sk);
  1760. if (err)
  1761. return err;
  1762. if (sk->sk_state != TCP_ESTABLISHED)
  1763. return -ENOTCONN;
  1764. if (msg->msg_namelen)
  1765. msg->msg_namelen = 0;
  1766. return unix_dgram_sendmsg(sock, msg, len);
  1767. }
  1768. static int unix_seqpacket_recvmsg(struct socket *sock, struct msghdr *msg,
  1769. size_t size, int flags)
  1770. {
  1771. struct sock *sk = sock->sk;
  1772. if (sk->sk_state != TCP_ESTABLISHED)
  1773. return -ENOTCONN;
  1774. return unix_dgram_recvmsg(sock, msg, size, flags);
  1775. }
  1776. static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
  1777. {
  1778. struct unix_sock *u = unix_sk(sk);
  1779. if (u->addr) {
  1780. msg->msg_namelen = u->addr->len;
  1781. memcpy(msg->msg_name, u->addr->name, u->addr->len);
  1782. }
  1783. }
  1784. static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
  1785. size_t size, int flags)
  1786. {
  1787. struct scm_cookie scm;
  1788. struct sock *sk = sock->sk;
  1789. struct unix_sock *u = unix_sk(sk);
  1790. struct sk_buff *skb, *last;
  1791. long timeo;
  1792. int err;
  1793. int peeked, skip;
  1794. err = -EOPNOTSUPP;
  1795. if (flags&MSG_OOB)
  1796. goto out;
  1797. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1798. do {
  1799. mutex_lock(&u->iolock);
  1800. skip = sk_peek_offset(sk, flags);
  1801. skb = __skb_try_recv_datagram(sk, flags, &peeked, &skip, &err,
  1802. &last);
  1803. if (skb)
  1804. break;
  1805. mutex_unlock(&u->iolock);
  1806. if (err != -EAGAIN)
  1807. break;
  1808. } while (timeo &&
  1809. !__skb_wait_for_more_packets(sk, &err, &timeo, last));
  1810. if (!skb) { /* implies iolock unlocked */
  1811. unix_state_lock(sk);
  1812. /* Signal EOF on disconnected non-blocking SEQPACKET socket. */
  1813. if (sk->sk_type == SOCK_SEQPACKET && err == -EAGAIN &&
  1814. (sk->sk_shutdown & RCV_SHUTDOWN))
  1815. err = 0;
  1816. unix_state_unlock(sk);
  1817. goto out;
  1818. }
  1819. if (wq_has_sleeper(&u->peer_wait))
  1820. wake_up_interruptible_sync_poll(&u->peer_wait,
  1821. POLLOUT | POLLWRNORM |
  1822. POLLWRBAND);
  1823. if (msg->msg_name)
  1824. unix_copy_addr(msg, skb->sk);
  1825. if (size > skb->len - skip)
  1826. size = skb->len - skip;
  1827. else if (size < skb->len - skip)
  1828. msg->msg_flags |= MSG_TRUNC;
  1829. err = skb_copy_datagram_msg(skb, skip, msg, size);
  1830. if (err)
  1831. goto out_free;
  1832. if (sock_flag(sk, SOCK_RCVTSTAMP))
  1833. __sock_recv_timestamp(msg, sk, skb);
  1834. memset(&scm, 0, sizeof(scm));
  1835. scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
  1836. unix_set_secdata(&scm, skb);
  1837. if (!(flags & MSG_PEEK)) {
  1838. if (UNIXCB(skb).fp)
  1839. unix_detach_fds(&scm, skb);
  1840. sk_peek_offset_bwd(sk, skb->len);
  1841. } else {
  1842. /* It is questionable: on PEEK we could:
  1843. - do not return fds - good, but too simple 8)
  1844. - return fds, and do not return them on read (old strategy,
  1845. apparently wrong)
  1846. - clone fds (I chose it for now, it is the most universal
  1847. solution)
  1848. POSIX 1003.1g does not actually define this clearly
  1849. at all. POSIX 1003.1g doesn't define a lot of things
  1850. clearly however!
  1851. */
  1852. sk_peek_offset_fwd(sk, size);
  1853. if (UNIXCB(skb).fp)
  1854. scm.fp = scm_fp_dup(UNIXCB(skb).fp);
  1855. }
  1856. err = (flags & MSG_TRUNC) ? skb->len - skip : size;
  1857. scm_recv(sock, msg, &scm, flags);
  1858. out_free:
  1859. skb_free_datagram(sk, skb);
  1860. mutex_unlock(&u->iolock);
  1861. out:
  1862. return err;
  1863. }
  1864. /*
  1865. * Sleep until more data has arrived. But check for races..
  1866. */
  1867. static long unix_stream_data_wait(struct sock *sk, long timeo,
  1868. struct sk_buff *last, unsigned int last_len,
  1869. bool freezable)
  1870. {
  1871. struct sk_buff *tail;
  1872. DEFINE_WAIT(wait);
  1873. unix_state_lock(sk);
  1874. for (;;) {
  1875. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1876. tail = skb_peek_tail(&sk->sk_receive_queue);
  1877. if (tail != last ||
  1878. (tail && tail->len != last_len) ||
  1879. sk->sk_err ||
  1880. (sk->sk_shutdown & RCV_SHUTDOWN) ||
  1881. signal_pending(current) ||
  1882. !timeo)
  1883. break;
  1884. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1885. unix_state_unlock(sk);
  1886. if (freezable)
  1887. timeo = freezable_schedule_timeout(timeo);
  1888. else
  1889. timeo = schedule_timeout(timeo);
  1890. unix_state_lock(sk);
  1891. if (sock_flag(sk, SOCK_DEAD))
  1892. break;
  1893. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1894. }
  1895. finish_wait(sk_sleep(sk), &wait);
  1896. unix_state_unlock(sk);
  1897. return timeo;
  1898. }
  1899. static unsigned int unix_skb_len(const struct sk_buff *skb)
  1900. {
  1901. return skb->len - UNIXCB(skb).consumed;
  1902. }
  1903. struct unix_stream_read_state {
  1904. int (*recv_actor)(struct sk_buff *, int, int,
  1905. struct unix_stream_read_state *);
  1906. struct socket *socket;
  1907. struct msghdr *msg;
  1908. struct pipe_inode_info *pipe;
  1909. size_t size;
  1910. int flags;
  1911. unsigned int splice_flags;
  1912. };
  1913. static int unix_stream_read_generic(struct unix_stream_read_state *state,
  1914. bool freezable)
  1915. {
  1916. struct scm_cookie scm;
  1917. struct socket *sock = state->socket;
  1918. struct sock *sk = sock->sk;
  1919. struct unix_sock *u = unix_sk(sk);
  1920. int copied = 0;
  1921. int flags = state->flags;
  1922. int noblock = flags & MSG_DONTWAIT;
  1923. bool check_creds = false;
  1924. int target;
  1925. int err = 0;
  1926. long timeo;
  1927. int skip;
  1928. size_t size = state->size;
  1929. unsigned int last_len;
  1930. if (unlikely(sk->sk_state != TCP_ESTABLISHED)) {
  1931. err = -EINVAL;
  1932. goto out;
  1933. }
  1934. if (unlikely(flags & MSG_OOB)) {
  1935. err = -EOPNOTSUPP;
  1936. goto out;
  1937. }
  1938. target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
  1939. timeo = sock_rcvtimeo(sk, noblock);
  1940. memset(&scm, 0, sizeof(scm));
  1941. /* Lock the socket to prevent queue disordering
  1942. * while sleeps in memcpy_tomsg
  1943. */
  1944. mutex_lock(&u->iolock);
  1945. if (flags & MSG_PEEK)
  1946. skip = sk_peek_offset(sk, flags);
  1947. else
  1948. skip = 0;
  1949. do {
  1950. int chunk;
  1951. bool drop_skb;
  1952. struct sk_buff *skb, *last;
  1953. redo:
  1954. unix_state_lock(sk);
  1955. if (sock_flag(sk, SOCK_DEAD)) {
  1956. err = -ECONNRESET;
  1957. goto unlock;
  1958. }
  1959. last = skb = skb_peek(&sk->sk_receive_queue);
  1960. last_len = last ? last->len : 0;
  1961. again:
  1962. if (skb == NULL) {
  1963. unix_sk(sk)->recursion_level = 0;
  1964. if (copied >= target)
  1965. goto unlock;
  1966. /*
  1967. * POSIX 1003.1g mandates this order.
  1968. */
  1969. err = sock_error(sk);
  1970. if (err)
  1971. goto unlock;
  1972. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1973. goto unlock;
  1974. unix_state_unlock(sk);
  1975. if (!timeo) {
  1976. err = -EAGAIN;
  1977. break;
  1978. }
  1979. mutex_unlock(&u->iolock);
  1980. timeo = unix_stream_data_wait(sk, timeo, last,
  1981. last_len, freezable);
  1982. if (signal_pending(current)) {
  1983. err = sock_intr_errno(timeo);
  1984. scm_destroy(&scm);
  1985. goto out;
  1986. }
  1987. mutex_lock(&u->iolock);
  1988. goto redo;
  1989. unlock:
  1990. unix_state_unlock(sk);
  1991. break;
  1992. }
  1993. while (skip >= unix_skb_len(skb)) {
  1994. skip -= unix_skb_len(skb);
  1995. last = skb;
  1996. last_len = skb->len;
  1997. skb = skb_peek_next(skb, &sk->sk_receive_queue);
  1998. if (!skb)
  1999. goto again;
  2000. }
  2001. unix_state_unlock(sk);
  2002. if (check_creds) {
  2003. /* Never glue messages from different writers */
  2004. if (!unix_skb_scm_eq(skb, &scm))
  2005. break;
  2006. } else if (test_bit(SOCK_PASSCRED, &sock->flags)) {
  2007. /* Copy credentials */
  2008. scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
  2009. unix_set_secdata(&scm, skb);
  2010. check_creds = true;
  2011. }
  2012. /* Copy address just once */
  2013. if (state->msg && state->msg->msg_name) {
  2014. DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr,
  2015. state->msg->msg_name);
  2016. unix_copy_addr(state->msg, skb->sk);
  2017. sunaddr = NULL;
  2018. }
  2019. chunk = min_t(unsigned int, unix_skb_len(skb) - skip, size);
  2020. skb_get(skb);
  2021. chunk = state->recv_actor(skb, skip, chunk, state);
  2022. drop_skb = !unix_skb_len(skb);
  2023. /* skb is only safe to use if !drop_skb */
  2024. consume_skb(skb);
  2025. if (chunk < 0) {
  2026. if (copied == 0)
  2027. copied = -EFAULT;
  2028. break;
  2029. }
  2030. copied += chunk;
  2031. size -= chunk;
  2032. if (drop_skb) {
  2033. /* the skb was touched by a concurrent reader;
  2034. * we should not expect anything from this skb
  2035. * anymore and assume it invalid - we can be
  2036. * sure it was dropped from the socket queue
  2037. *
  2038. * let's report a short read
  2039. */
  2040. err = 0;
  2041. break;
  2042. }
  2043. /* Mark read part of skb as used */
  2044. if (!(flags & MSG_PEEK)) {
  2045. UNIXCB(skb).consumed += chunk;
  2046. sk_peek_offset_bwd(sk, chunk);
  2047. if (UNIXCB(skb).fp)
  2048. unix_detach_fds(&scm, skb);
  2049. if (unix_skb_len(skb))
  2050. break;
  2051. skb_unlink(skb, &sk->sk_receive_queue);
  2052. consume_skb(skb);
  2053. if (scm.fp)
  2054. break;
  2055. } else {
  2056. /* It is questionable, see note in unix_dgram_recvmsg.
  2057. */
  2058. if (UNIXCB(skb).fp)
  2059. scm.fp = scm_fp_dup(UNIXCB(skb).fp);
  2060. sk_peek_offset_fwd(sk, chunk);
  2061. if (UNIXCB(skb).fp)
  2062. break;
  2063. skip = 0;
  2064. last = skb;
  2065. last_len = skb->len;
  2066. unix_state_lock(sk);
  2067. skb = skb_peek_next(skb, &sk->sk_receive_queue);
  2068. if (skb)
  2069. goto again;
  2070. unix_state_unlock(sk);
  2071. break;
  2072. }
  2073. } while (size);
  2074. mutex_unlock(&u->iolock);
  2075. if (state->msg)
  2076. scm_recv(sock, state->msg, &scm, flags);
  2077. else
  2078. scm_destroy(&scm);
  2079. out:
  2080. return copied ? : err;
  2081. }
  2082. static int unix_stream_read_actor(struct sk_buff *skb,
  2083. int skip, int chunk,
  2084. struct unix_stream_read_state *state)
  2085. {
  2086. int ret;
  2087. ret = skb_copy_datagram_msg(skb, UNIXCB(skb).consumed + skip,
  2088. state->msg, chunk);
  2089. return ret ?: chunk;
  2090. }
  2091. static int unix_stream_recvmsg(struct socket *sock, struct msghdr *msg,
  2092. size_t size, int flags)
  2093. {
  2094. struct unix_stream_read_state state = {
  2095. .recv_actor = unix_stream_read_actor,
  2096. .socket = sock,
  2097. .msg = msg,
  2098. .size = size,
  2099. .flags = flags
  2100. };
  2101. return unix_stream_read_generic(&state, true);
  2102. }
  2103. static int unix_stream_splice_actor(struct sk_buff *skb,
  2104. int skip, int chunk,
  2105. struct unix_stream_read_state *state)
  2106. {
  2107. return skb_splice_bits(skb, state->socket->sk,
  2108. UNIXCB(skb).consumed + skip,
  2109. state->pipe, chunk, state->splice_flags);
  2110. }
  2111. static ssize_t unix_stream_splice_read(struct socket *sock, loff_t *ppos,
  2112. struct pipe_inode_info *pipe,
  2113. size_t size, unsigned int flags)
  2114. {
  2115. struct unix_stream_read_state state = {
  2116. .recv_actor = unix_stream_splice_actor,
  2117. .socket = sock,
  2118. .pipe = pipe,
  2119. .size = size,
  2120. .splice_flags = flags,
  2121. };
  2122. if (unlikely(*ppos))
  2123. return -ESPIPE;
  2124. if (sock->file->f_flags & O_NONBLOCK ||
  2125. flags & SPLICE_F_NONBLOCK)
  2126. state.flags = MSG_DONTWAIT;
  2127. return unix_stream_read_generic(&state, false);
  2128. }
  2129. static int unix_shutdown(struct socket *sock, int mode)
  2130. {
  2131. struct sock *sk = sock->sk;
  2132. struct sock *other;
  2133. if (mode < SHUT_RD || mode > SHUT_RDWR)
  2134. return -EINVAL;
  2135. /* This maps:
  2136. * SHUT_RD (0) -> RCV_SHUTDOWN (1)
  2137. * SHUT_WR (1) -> SEND_SHUTDOWN (2)
  2138. * SHUT_RDWR (2) -> SHUTDOWN_MASK (3)
  2139. */
  2140. ++mode;
  2141. unix_state_lock(sk);
  2142. sk->sk_shutdown |= mode;
  2143. other = unix_peer(sk);
  2144. if (other)
  2145. sock_hold(other);
  2146. unix_state_unlock(sk);
  2147. sk->sk_state_change(sk);
  2148. if (other &&
  2149. (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
  2150. int peer_mode = 0;
  2151. if (mode&RCV_SHUTDOWN)
  2152. peer_mode |= SEND_SHUTDOWN;
  2153. if (mode&SEND_SHUTDOWN)
  2154. peer_mode |= RCV_SHUTDOWN;
  2155. unix_state_lock(other);
  2156. other->sk_shutdown |= peer_mode;
  2157. unix_state_unlock(other);
  2158. other->sk_state_change(other);
  2159. if (peer_mode == SHUTDOWN_MASK)
  2160. sk_wake_async(other, SOCK_WAKE_WAITD, POLL_HUP);
  2161. else if (peer_mode & RCV_SHUTDOWN)
  2162. sk_wake_async(other, SOCK_WAKE_WAITD, POLL_IN);
  2163. }
  2164. if (other)
  2165. sock_put(other);
  2166. return 0;
  2167. }
  2168. long unix_inq_len(struct sock *sk)
  2169. {
  2170. struct sk_buff *skb;
  2171. long amount = 0;
  2172. if (sk->sk_state == TCP_LISTEN)
  2173. return -EINVAL;
  2174. spin_lock(&sk->sk_receive_queue.lock);
  2175. if (sk->sk_type == SOCK_STREAM ||
  2176. sk->sk_type == SOCK_SEQPACKET) {
  2177. skb_queue_walk(&sk->sk_receive_queue, skb)
  2178. amount += unix_skb_len(skb);
  2179. } else {
  2180. skb = skb_peek(&sk->sk_receive_queue);
  2181. if (skb)
  2182. amount = skb->len;
  2183. }
  2184. spin_unlock(&sk->sk_receive_queue.lock);
  2185. return amount;
  2186. }
  2187. EXPORT_SYMBOL_GPL(unix_inq_len);
  2188. long unix_outq_len(struct sock *sk)
  2189. {
  2190. return sk_wmem_alloc_get(sk);
  2191. }
  2192. EXPORT_SYMBOL_GPL(unix_outq_len);
  2193. static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  2194. {
  2195. struct sock *sk = sock->sk;
  2196. long amount = 0;
  2197. int err;
  2198. switch (cmd) {
  2199. case SIOCOUTQ:
  2200. amount = unix_outq_len(sk);
  2201. err = put_user(amount, (int __user *)arg);
  2202. break;
  2203. case SIOCINQ:
  2204. amount = unix_inq_len(sk);
  2205. if (amount < 0)
  2206. err = amount;
  2207. else
  2208. err = put_user(amount, (int __user *)arg);
  2209. break;
  2210. default:
  2211. err = -ENOIOCTLCMD;
  2212. break;
  2213. }
  2214. return err;
  2215. }
  2216. static unsigned int unix_poll(struct file *file, struct socket *sock, poll_table *wait)
  2217. {
  2218. struct sock *sk = sock->sk;
  2219. unsigned int mask;
  2220. sock_poll_wait(file, sk_sleep(sk), wait);
  2221. mask = 0;
  2222. /* exceptional events? */
  2223. if (sk->sk_err)
  2224. mask |= POLLERR;
  2225. if (sk->sk_shutdown == SHUTDOWN_MASK)
  2226. mask |= POLLHUP;
  2227. if (sk->sk_shutdown & RCV_SHUTDOWN)
  2228. mask |= POLLRDHUP | POLLIN | POLLRDNORM;
  2229. /* readable? */
  2230. if (!skb_queue_empty(&sk->sk_receive_queue))
  2231. mask |= POLLIN | POLLRDNORM;
  2232. /* Connection-based need to check for termination and startup */
  2233. if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) &&
  2234. sk->sk_state == TCP_CLOSE)
  2235. mask |= POLLHUP;
  2236. /*
  2237. * we set writable also when the other side has shut down the
  2238. * connection. This prevents stuck sockets.
  2239. */
  2240. if (unix_writable(sk))
  2241. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  2242. return mask;
  2243. }
  2244. static unsigned int unix_dgram_poll(struct file *file, struct socket *sock,
  2245. poll_table *wait)
  2246. {
  2247. struct sock *sk = sock->sk, *other;
  2248. unsigned int mask, writable;
  2249. sock_poll_wait(file, sk_sleep(sk), wait);
  2250. mask = 0;
  2251. /* exceptional events? */
  2252. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  2253. mask |= POLLERR |
  2254. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
  2255. if (sk->sk_shutdown & RCV_SHUTDOWN)
  2256. mask |= POLLRDHUP | POLLIN | POLLRDNORM;
  2257. if (sk->sk_shutdown == SHUTDOWN_MASK)
  2258. mask |= POLLHUP;
  2259. /* readable? */
  2260. if (!skb_queue_empty(&sk->sk_receive_queue))
  2261. mask |= POLLIN | POLLRDNORM;
  2262. /* Connection-based need to check for termination and startup */
  2263. if (sk->sk_type == SOCK_SEQPACKET) {
  2264. if (sk->sk_state == TCP_CLOSE)
  2265. mask |= POLLHUP;
  2266. /* connection hasn't started yet? */
  2267. if (sk->sk_state == TCP_SYN_SENT)
  2268. return mask;
  2269. }
  2270. /* No write status requested, avoid expensive OUT tests. */
  2271. if (!(poll_requested_events(wait) & (POLLWRBAND|POLLWRNORM|POLLOUT)))
  2272. return mask;
  2273. writable = unix_writable(sk);
  2274. if (writable) {
  2275. unix_state_lock(sk);
  2276. other = unix_peer(sk);
  2277. if (other && unix_peer(other) != sk &&
  2278. unix_recvq_full(other) &&
  2279. unix_dgram_peer_wake_me(sk, other))
  2280. writable = 0;
  2281. unix_state_unlock(sk);
  2282. }
  2283. if (writable)
  2284. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  2285. else
  2286. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  2287. return mask;
  2288. }
  2289. #ifdef CONFIG_PROC_FS
  2290. #define BUCKET_SPACE (BITS_PER_LONG - (UNIX_HASH_BITS + 1) - 1)
  2291. #define get_bucket(x) ((x) >> BUCKET_SPACE)
  2292. #define get_offset(x) ((x) & ((1L << BUCKET_SPACE) - 1))
  2293. #define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o))
  2294. static struct sock *unix_from_bucket(struct seq_file *seq, loff_t *pos)
  2295. {
  2296. unsigned long offset = get_offset(*pos);
  2297. unsigned long bucket = get_bucket(*pos);
  2298. struct sock *sk;
  2299. unsigned long count = 0;
  2300. for (sk = sk_head(&unix_socket_table[bucket]); sk; sk = sk_next(sk)) {
  2301. if (sock_net(sk) != seq_file_net(seq))
  2302. continue;
  2303. if (++count == offset)
  2304. break;
  2305. }
  2306. return sk;
  2307. }
  2308. static struct sock *unix_next_socket(struct seq_file *seq,
  2309. struct sock *sk,
  2310. loff_t *pos)
  2311. {
  2312. unsigned long bucket;
  2313. while (sk > (struct sock *)SEQ_START_TOKEN) {
  2314. sk = sk_next(sk);
  2315. if (!sk)
  2316. goto next_bucket;
  2317. if (sock_net(sk) == seq_file_net(seq))
  2318. return sk;
  2319. }
  2320. do {
  2321. sk = unix_from_bucket(seq, pos);
  2322. if (sk)
  2323. return sk;
  2324. next_bucket:
  2325. bucket = get_bucket(*pos) + 1;
  2326. *pos = set_bucket_offset(bucket, 1);
  2327. } while (bucket < ARRAY_SIZE(unix_socket_table));
  2328. return NULL;
  2329. }
  2330. static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
  2331. __acquires(unix_table_lock)
  2332. {
  2333. spin_lock(&unix_table_lock);
  2334. if (!*pos)
  2335. return SEQ_START_TOKEN;
  2336. if (get_bucket(*pos) >= ARRAY_SIZE(unix_socket_table))
  2337. return NULL;
  2338. return unix_next_socket(seq, NULL, pos);
  2339. }
  2340. static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2341. {
  2342. ++*pos;
  2343. return unix_next_socket(seq, v, pos);
  2344. }
  2345. static void unix_seq_stop(struct seq_file *seq, void *v)
  2346. __releases(unix_table_lock)
  2347. {
  2348. spin_unlock(&unix_table_lock);
  2349. }
  2350. static int unix_seq_show(struct seq_file *seq, void *v)
  2351. {
  2352. if (v == SEQ_START_TOKEN)
  2353. seq_puts(seq, "Num RefCount Protocol Flags Type St "
  2354. "Inode Path\n");
  2355. else {
  2356. struct sock *s = v;
  2357. struct unix_sock *u = unix_sk(s);
  2358. unix_state_lock(s);
  2359. seq_printf(seq, "%pK: %08X %08X %08X %04X %02X %5lu",
  2360. s,
  2361. atomic_read(&s->sk_refcnt),
  2362. 0,
  2363. s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
  2364. s->sk_type,
  2365. s->sk_socket ?
  2366. (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
  2367. (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
  2368. sock_i_ino(s));
  2369. if (u->addr) {
  2370. int i, len;
  2371. seq_putc(seq, ' ');
  2372. i = 0;
  2373. len = u->addr->len - sizeof(short);
  2374. if (!UNIX_ABSTRACT(s))
  2375. len--;
  2376. else {
  2377. seq_putc(seq, '@');
  2378. i++;
  2379. }
  2380. for ( ; i < len; i++)
  2381. seq_putc(seq, u->addr->name->sun_path[i] ?:
  2382. '@');
  2383. }
  2384. unix_state_unlock(s);
  2385. seq_putc(seq, '\n');
  2386. }
  2387. return 0;
  2388. }
  2389. static const struct seq_operations unix_seq_ops = {
  2390. .start = unix_seq_start,
  2391. .next = unix_seq_next,
  2392. .stop = unix_seq_stop,
  2393. .show = unix_seq_show,
  2394. };
  2395. static int unix_seq_open(struct inode *inode, struct file *file)
  2396. {
  2397. return seq_open_net(inode, file, &unix_seq_ops,
  2398. sizeof(struct seq_net_private));
  2399. }
  2400. static const struct file_operations unix_seq_fops = {
  2401. .owner = THIS_MODULE,
  2402. .open = unix_seq_open,
  2403. .read = seq_read,
  2404. .llseek = seq_lseek,
  2405. .release = seq_release_net,
  2406. };
  2407. #endif
  2408. static const struct net_proto_family unix_family_ops = {
  2409. .family = PF_UNIX,
  2410. .create = unix_create,
  2411. .owner = THIS_MODULE,
  2412. };
  2413. static int __net_init unix_net_init(struct net *net)
  2414. {
  2415. int error = -ENOMEM;
  2416. net->unx.sysctl_max_dgram_qlen = 10;
  2417. if (unix_sysctl_register(net))
  2418. goto out;
  2419. #ifdef CONFIG_PROC_FS
  2420. if (!proc_create("unix", 0, net->proc_net, &unix_seq_fops)) {
  2421. unix_sysctl_unregister(net);
  2422. goto out;
  2423. }
  2424. #endif
  2425. error = 0;
  2426. out:
  2427. return error;
  2428. }
  2429. static void __net_exit unix_net_exit(struct net *net)
  2430. {
  2431. unix_sysctl_unregister(net);
  2432. remove_proc_entry("unix", net->proc_net);
  2433. }
  2434. static struct pernet_operations unix_net_ops = {
  2435. .init = unix_net_init,
  2436. .exit = unix_net_exit,
  2437. };
  2438. static int __init af_unix_init(void)
  2439. {
  2440. int rc = -1;
  2441. BUILD_BUG_ON(sizeof(struct unix_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
  2442. rc = proto_register(&unix_proto, 1);
  2443. if (rc != 0) {
  2444. pr_crit("%s: Cannot create unix_sock SLAB cache!\n", __func__);
  2445. goto out;
  2446. }
  2447. sock_register(&unix_family_ops);
  2448. register_pernet_subsys(&unix_net_ops);
  2449. out:
  2450. return rc;
  2451. }
  2452. static void __exit af_unix_exit(void)
  2453. {
  2454. sock_unregister(PF_UNIX);
  2455. proto_unregister(&unix_proto);
  2456. unregister_pernet_subsys(&unix_net_ops);
  2457. }
  2458. /* Earlier than device_initcall() so that other drivers invoking
  2459. request_module() don't end up in a loop when modprobe tries
  2460. to use a UNIX socket. But later than subsys_initcall() because
  2461. we depend on stuff initialised there */
  2462. fs_initcall(af_unix_init);
  2463. module_exit(af_unix_exit);
  2464. MODULE_LICENSE("GPL");
  2465. MODULE_ALIAS_NETPROTO(PF_UNIX);