af_unix.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948
  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 (wq_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_write_space = unix_write_space;
  669. sk->sk_max_ack_backlog = net->unx.sysctl_max_dgram_qlen;
  670. sk->sk_destruct = unix_sock_destructor;
  671. u = unix_sk(sk);
  672. u->path.dentry = NULL;
  673. u->path.mnt = NULL;
  674. spin_lock_init(&u->lock);
  675. atomic_long_set(&u->inflight, 0);
  676. INIT_LIST_HEAD(&u->link);
  677. mutex_init(&u->iolock); /* single task reading lock */
  678. mutex_init(&u->bindlock); /* single task binding lock */
  679. init_waitqueue_head(&u->peer_wait);
  680. init_waitqueue_func_entry(&u->peer_wake, unix_dgram_peer_wake_relay);
  681. unix_insert_socket(unix_sockets_unbound(sk), sk);
  682. out:
  683. if (sk == NULL)
  684. atomic_long_dec(&unix_nr_socks);
  685. else {
  686. local_bh_disable();
  687. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  688. local_bh_enable();
  689. }
  690. return sk;
  691. }
  692. static int unix_create(struct net *net, struct socket *sock, int protocol,
  693. int kern)
  694. {
  695. if (protocol && protocol != PF_UNIX)
  696. return -EPROTONOSUPPORT;
  697. sock->state = SS_UNCONNECTED;
  698. switch (sock->type) {
  699. case SOCK_STREAM:
  700. sock->ops = &unix_stream_ops;
  701. break;
  702. /*
  703. * Believe it or not BSD has AF_UNIX, SOCK_RAW though
  704. * nothing uses it.
  705. */
  706. case SOCK_RAW:
  707. sock->type = SOCK_DGRAM;
  708. case SOCK_DGRAM:
  709. sock->ops = &unix_dgram_ops;
  710. break;
  711. case SOCK_SEQPACKET:
  712. sock->ops = &unix_seqpacket_ops;
  713. break;
  714. default:
  715. return -ESOCKTNOSUPPORT;
  716. }
  717. return unix_create1(net, sock, kern) ? 0 : -ENOMEM;
  718. }
  719. static int unix_release(struct socket *sock)
  720. {
  721. struct sock *sk = sock->sk;
  722. if (!sk)
  723. return 0;
  724. unix_release_sock(sk, 0);
  725. sock->sk = NULL;
  726. return 0;
  727. }
  728. static int unix_autobind(struct socket *sock)
  729. {
  730. struct sock *sk = sock->sk;
  731. struct net *net = sock_net(sk);
  732. struct unix_sock *u = unix_sk(sk);
  733. static u32 ordernum = 1;
  734. struct unix_address *addr;
  735. int err;
  736. unsigned int retries = 0;
  737. err = mutex_lock_interruptible(&u->bindlock);
  738. if (err)
  739. return err;
  740. err = 0;
  741. if (u->addr)
  742. goto out;
  743. err = -ENOMEM;
  744. addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL);
  745. if (!addr)
  746. goto out;
  747. addr->name->sun_family = AF_UNIX;
  748. atomic_set(&addr->refcnt, 1);
  749. retry:
  750. addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
  751. addr->hash = unix_hash_fold(csum_partial(addr->name, addr->len, 0));
  752. spin_lock(&unix_table_lock);
  753. ordernum = (ordernum+1)&0xFFFFF;
  754. if (__unix_find_socket_byname(net, addr->name, addr->len, sock->type,
  755. addr->hash)) {
  756. spin_unlock(&unix_table_lock);
  757. /*
  758. * __unix_find_socket_byname() may take long time if many names
  759. * are already in use.
  760. */
  761. cond_resched();
  762. /* Give up if all names seems to be in use. */
  763. if (retries++ == 0xFFFFF) {
  764. err = -ENOSPC;
  765. kfree(addr);
  766. goto out;
  767. }
  768. goto retry;
  769. }
  770. addr->hash ^= sk->sk_type;
  771. __unix_remove_socket(sk);
  772. smp_store_release(&u->addr, addr);
  773. __unix_insert_socket(&unix_socket_table[addr->hash], sk);
  774. spin_unlock(&unix_table_lock);
  775. err = 0;
  776. out: mutex_unlock(&u->bindlock);
  777. return err;
  778. }
  779. static struct sock *unix_find_other(struct net *net,
  780. struct sockaddr_un *sunname, int len,
  781. int type, unsigned int hash, int *error)
  782. {
  783. struct sock *u;
  784. struct path path;
  785. int err = 0;
  786. if (sunname->sun_path[0]) {
  787. struct inode *inode;
  788. err = kern_path(sunname->sun_path, LOOKUP_FOLLOW, &path);
  789. if (err)
  790. goto fail;
  791. inode = d_real_inode(path.dentry);
  792. err = inode_permission(inode, MAY_WRITE);
  793. if (err)
  794. goto put_fail;
  795. err = -ECONNREFUSED;
  796. if (!S_ISSOCK(inode->i_mode))
  797. goto put_fail;
  798. u = unix_find_socket_byinode(inode);
  799. if (!u)
  800. goto put_fail;
  801. if (u->sk_type == type)
  802. touch_atime(&path);
  803. path_put(&path);
  804. err = -EPROTOTYPE;
  805. if (u->sk_type != type) {
  806. sock_put(u);
  807. goto fail;
  808. }
  809. } else {
  810. err = -ECONNREFUSED;
  811. u = unix_find_socket_byname(net, sunname, len, type, hash);
  812. if (u) {
  813. struct dentry *dentry;
  814. dentry = unix_sk(u)->path.dentry;
  815. if (dentry)
  816. touch_atime(&unix_sk(u)->path);
  817. } else
  818. goto fail;
  819. }
  820. return u;
  821. put_fail:
  822. path_put(&path);
  823. fail:
  824. *error = err;
  825. return NULL;
  826. }
  827. static int unix_mknod(const char *sun_path, umode_t mode, struct path *res)
  828. {
  829. struct dentry *dentry;
  830. struct path path;
  831. int err = 0;
  832. /*
  833. * Get the parent directory, calculate the hash for last
  834. * component.
  835. */
  836. dentry = kern_path_create(AT_FDCWD, sun_path, &path, 0);
  837. err = PTR_ERR(dentry);
  838. if (IS_ERR(dentry))
  839. return err;
  840. /*
  841. * All right, let's create it.
  842. */
  843. err = security_path_mknod(&path, dentry, mode, 0);
  844. if (!err) {
  845. err = vfs_mknod(d_inode(path.dentry), dentry, mode, 0);
  846. if (!err) {
  847. res->mnt = mntget(path.mnt);
  848. res->dentry = dget(dentry);
  849. }
  850. }
  851. done_path_create(&path, dentry);
  852. return err;
  853. }
  854. static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  855. {
  856. struct sock *sk = sock->sk;
  857. struct net *net = sock_net(sk);
  858. struct unix_sock *u = unix_sk(sk);
  859. struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
  860. char *sun_path = sunaddr->sun_path;
  861. int err;
  862. unsigned int hash;
  863. struct unix_address *addr;
  864. struct hlist_head *list;
  865. struct path path = { NULL, NULL };
  866. err = -EINVAL;
  867. if (addr_len < offsetofend(struct sockaddr_un, sun_family) ||
  868. sunaddr->sun_family != AF_UNIX)
  869. goto out;
  870. if (addr_len == sizeof(short)) {
  871. err = unix_autobind(sock);
  872. goto out;
  873. }
  874. err = unix_mkname(sunaddr, addr_len, &hash);
  875. if (err < 0)
  876. goto out;
  877. addr_len = err;
  878. if (sun_path[0]) {
  879. umode_t mode = S_IFSOCK |
  880. (SOCK_INODE(sock)->i_mode & ~current_umask());
  881. err = unix_mknod(sun_path, mode, &path);
  882. if (err) {
  883. if (err == -EEXIST)
  884. err = -EADDRINUSE;
  885. goto out;
  886. }
  887. }
  888. err = mutex_lock_interruptible(&u->bindlock);
  889. if (err)
  890. goto out_put;
  891. err = -EINVAL;
  892. if (u->addr)
  893. goto out_up;
  894. err = -ENOMEM;
  895. addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
  896. if (!addr)
  897. goto out_up;
  898. memcpy(addr->name, sunaddr, addr_len);
  899. addr->len = addr_len;
  900. addr->hash = hash ^ sk->sk_type;
  901. atomic_set(&addr->refcnt, 1);
  902. if (sun_path[0]) {
  903. addr->hash = UNIX_HASH_SIZE;
  904. hash = d_real_inode(path.dentry)->i_ino & (UNIX_HASH_SIZE - 1);
  905. spin_lock(&unix_table_lock);
  906. u->path = path;
  907. list = &unix_socket_table[hash];
  908. } else {
  909. spin_lock(&unix_table_lock);
  910. err = -EADDRINUSE;
  911. if (__unix_find_socket_byname(net, sunaddr, addr_len,
  912. sk->sk_type, hash)) {
  913. unix_release_addr(addr);
  914. goto out_unlock;
  915. }
  916. list = &unix_socket_table[addr->hash];
  917. }
  918. err = 0;
  919. __unix_remove_socket(sk);
  920. smp_store_release(&u->addr, addr);
  921. __unix_insert_socket(list, sk);
  922. out_unlock:
  923. spin_unlock(&unix_table_lock);
  924. out_up:
  925. mutex_unlock(&u->bindlock);
  926. out_put:
  927. if (err)
  928. path_put(&path);
  929. out:
  930. return err;
  931. }
  932. static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)
  933. {
  934. if (unlikely(sk1 == sk2) || !sk2) {
  935. unix_state_lock(sk1);
  936. return;
  937. }
  938. if (sk1 < sk2) {
  939. unix_state_lock(sk1);
  940. unix_state_lock_nested(sk2);
  941. } else {
  942. unix_state_lock(sk2);
  943. unix_state_lock_nested(sk1);
  944. }
  945. }
  946. static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2)
  947. {
  948. if (unlikely(sk1 == sk2) || !sk2) {
  949. unix_state_unlock(sk1);
  950. return;
  951. }
  952. unix_state_unlock(sk1);
  953. unix_state_unlock(sk2);
  954. }
  955. static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
  956. int alen, int flags)
  957. {
  958. struct sock *sk = sock->sk;
  959. struct net *net = sock_net(sk);
  960. struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr;
  961. struct sock *other;
  962. unsigned int hash;
  963. int err;
  964. err = -EINVAL;
  965. if (alen < offsetofend(struct sockaddr, sa_family))
  966. goto out;
  967. if (addr->sa_family != AF_UNSPEC) {
  968. err = unix_mkname(sunaddr, alen, &hash);
  969. if (err < 0)
  970. goto out;
  971. alen = err;
  972. if (test_bit(SOCK_PASSCRED, &sock->flags) &&
  973. !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0)
  974. goto out;
  975. restart:
  976. other = unix_find_other(net, sunaddr, alen, sock->type, hash, &err);
  977. if (!other)
  978. goto out;
  979. unix_state_double_lock(sk, other);
  980. /* Apparently VFS overslept socket death. Retry. */
  981. if (sock_flag(other, SOCK_DEAD)) {
  982. unix_state_double_unlock(sk, other);
  983. sock_put(other);
  984. goto restart;
  985. }
  986. err = -EPERM;
  987. if (!unix_may_send(sk, other))
  988. goto out_unlock;
  989. err = security_unix_may_send(sk->sk_socket, other->sk_socket);
  990. if (err)
  991. goto out_unlock;
  992. } else {
  993. /*
  994. * 1003.1g breaking connected state with AF_UNSPEC
  995. */
  996. other = NULL;
  997. unix_state_double_lock(sk, other);
  998. }
  999. /*
  1000. * If it was connected, reconnect.
  1001. */
  1002. if (unix_peer(sk)) {
  1003. struct sock *old_peer = unix_peer(sk);
  1004. unix_peer(sk) = other;
  1005. unix_dgram_peer_wake_disconnect_wakeup(sk, old_peer);
  1006. unix_state_double_unlock(sk, other);
  1007. if (other != old_peer)
  1008. unix_dgram_disconnected(sk, old_peer);
  1009. sock_put(old_peer);
  1010. } else {
  1011. unix_peer(sk) = other;
  1012. unix_state_double_unlock(sk, other);
  1013. }
  1014. return 0;
  1015. out_unlock:
  1016. unix_state_double_unlock(sk, other);
  1017. sock_put(other);
  1018. out:
  1019. return err;
  1020. }
  1021. static long unix_wait_for_peer(struct sock *other, long timeo)
  1022. {
  1023. struct unix_sock *u = unix_sk(other);
  1024. int sched;
  1025. DEFINE_WAIT(wait);
  1026. prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
  1027. sched = !sock_flag(other, SOCK_DEAD) &&
  1028. !(other->sk_shutdown & RCV_SHUTDOWN) &&
  1029. unix_recvq_full(other);
  1030. unix_state_unlock(other);
  1031. if (sched)
  1032. timeo = schedule_timeout(timeo);
  1033. finish_wait(&u->peer_wait, &wait);
  1034. return timeo;
  1035. }
  1036. static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
  1037. int addr_len, int flags)
  1038. {
  1039. struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
  1040. struct sock *sk = sock->sk;
  1041. struct net *net = sock_net(sk);
  1042. struct unix_sock *u = unix_sk(sk), *newu, *otheru;
  1043. struct sock *newsk = NULL;
  1044. struct sock *other = NULL;
  1045. struct sk_buff *skb = NULL;
  1046. unsigned int hash;
  1047. int st;
  1048. int err;
  1049. long timeo;
  1050. err = unix_mkname(sunaddr, addr_len, &hash);
  1051. if (err < 0)
  1052. goto out;
  1053. addr_len = err;
  1054. if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr &&
  1055. (err = unix_autobind(sock)) != 0)
  1056. goto out;
  1057. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  1058. /* First of all allocate resources.
  1059. If we will make it after state is locked,
  1060. we will have to recheck all again in any case.
  1061. */
  1062. err = -ENOMEM;
  1063. /* create new sock for complete connection */
  1064. newsk = unix_create1(sock_net(sk), NULL, 0);
  1065. if (newsk == NULL)
  1066. goto out;
  1067. /* Allocate skb for sending to listening sock */
  1068. skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
  1069. if (skb == NULL)
  1070. goto out;
  1071. restart:
  1072. /* Find listening sock. */
  1073. other = unix_find_other(net, sunaddr, addr_len, sk->sk_type, hash, &err);
  1074. if (!other)
  1075. goto out;
  1076. /* Latch state of peer */
  1077. unix_state_lock(other);
  1078. /* Apparently VFS overslept socket death. Retry. */
  1079. if (sock_flag(other, SOCK_DEAD)) {
  1080. unix_state_unlock(other);
  1081. sock_put(other);
  1082. goto restart;
  1083. }
  1084. err = -ECONNREFUSED;
  1085. if (other->sk_state != TCP_LISTEN)
  1086. goto out_unlock;
  1087. if (other->sk_shutdown & RCV_SHUTDOWN)
  1088. goto out_unlock;
  1089. if (unix_recvq_full(other)) {
  1090. err = -EAGAIN;
  1091. if (!timeo)
  1092. goto out_unlock;
  1093. timeo = unix_wait_for_peer(other, timeo);
  1094. err = sock_intr_errno(timeo);
  1095. if (signal_pending(current))
  1096. goto out;
  1097. sock_put(other);
  1098. goto restart;
  1099. }
  1100. /* Latch our state.
  1101. It is tricky place. We need to grab our state lock and cannot
  1102. drop lock on peer. It is dangerous because deadlock is
  1103. possible. Connect to self case and simultaneous
  1104. attempt to connect are eliminated by checking socket
  1105. state. other is TCP_LISTEN, if sk is TCP_LISTEN we
  1106. check this before attempt to grab lock.
  1107. Well, and we have to recheck the state after socket locked.
  1108. */
  1109. st = sk->sk_state;
  1110. switch (st) {
  1111. case TCP_CLOSE:
  1112. /* This is ok... continue with connect */
  1113. break;
  1114. case TCP_ESTABLISHED:
  1115. /* Socket is already connected */
  1116. err = -EISCONN;
  1117. goto out_unlock;
  1118. default:
  1119. err = -EINVAL;
  1120. goto out_unlock;
  1121. }
  1122. unix_state_lock_nested(sk);
  1123. if (sk->sk_state != st) {
  1124. unix_state_unlock(sk);
  1125. unix_state_unlock(other);
  1126. sock_put(other);
  1127. goto restart;
  1128. }
  1129. err = security_unix_stream_connect(sk, other, newsk);
  1130. if (err) {
  1131. unix_state_unlock(sk);
  1132. goto out_unlock;
  1133. }
  1134. /* The way is open! Fastly set all the necessary fields... */
  1135. sock_hold(sk);
  1136. unix_peer(newsk) = sk;
  1137. newsk->sk_state = TCP_ESTABLISHED;
  1138. newsk->sk_type = sk->sk_type;
  1139. init_peercred(newsk);
  1140. newu = unix_sk(newsk);
  1141. RCU_INIT_POINTER(newsk->sk_wq, &newu->peer_wq);
  1142. otheru = unix_sk(other);
  1143. /* copy address information from listening to new sock
  1144. *
  1145. * The contents of *(otheru->addr) and otheru->path
  1146. * are seen fully set up here, since we have found
  1147. * otheru in hash under unix_table_lock. Insertion
  1148. * into the hash chain we'd found it in had been done
  1149. * in an earlier critical area protected by unix_table_lock,
  1150. * the same one where we'd set *(otheru->addr) contents,
  1151. * as well as otheru->path and otheru->addr itself.
  1152. *
  1153. * Using smp_store_release() here to set newu->addr
  1154. * is enough to make those stores, as well as stores
  1155. * to newu->path visible to anyone who gets newu->addr
  1156. * by smp_load_acquire(). IOW, the same warranties
  1157. * as for unix_sock instances bound in unix_bind() or
  1158. * in unix_autobind().
  1159. */
  1160. if (otheru->path.dentry) {
  1161. path_get(&otheru->path);
  1162. newu->path = otheru->path;
  1163. }
  1164. atomic_inc(&otheru->addr->refcnt);
  1165. smp_store_release(&newu->addr, otheru->addr);
  1166. /* Set credentials */
  1167. copy_peercred(sk, other);
  1168. sock->state = SS_CONNECTED;
  1169. sk->sk_state = TCP_ESTABLISHED;
  1170. sock_hold(newsk);
  1171. smp_mb__after_atomic(); /* sock_hold() does an atomic_inc() */
  1172. unix_peer(sk) = newsk;
  1173. unix_state_unlock(sk);
  1174. /* take ten and and send info to listening sock */
  1175. spin_lock(&other->sk_receive_queue.lock);
  1176. __skb_queue_tail(&other->sk_receive_queue, skb);
  1177. spin_unlock(&other->sk_receive_queue.lock);
  1178. unix_state_unlock(other);
  1179. other->sk_data_ready(other);
  1180. sock_put(other);
  1181. return 0;
  1182. out_unlock:
  1183. if (other)
  1184. unix_state_unlock(other);
  1185. out:
  1186. kfree_skb(skb);
  1187. if (newsk)
  1188. unix_release_sock(newsk, 0);
  1189. if (other)
  1190. sock_put(other);
  1191. return err;
  1192. }
  1193. static int unix_socketpair(struct socket *socka, struct socket *sockb)
  1194. {
  1195. struct sock *ska = socka->sk, *skb = sockb->sk;
  1196. /* Join our sockets back to back */
  1197. sock_hold(ska);
  1198. sock_hold(skb);
  1199. unix_peer(ska) = skb;
  1200. unix_peer(skb) = ska;
  1201. init_peercred(ska);
  1202. init_peercred(skb);
  1203. if (ska->sk_type != SOCK_DGRAM) {
  1204. ska->sk_state = TCP_ESTABLISHED;
  1205. skb->sk_state = TCP_ESTABLISHED;
  1206. socka->state = SS_CONNECTED;
  1207. sockb->state = SS_CONNECTED;
  1208. }
  1209. return 0;
  1210. }
  1211. static void unix_sock_inherit_flags(const struct socket *old,
  1212. struct socket *new)
  1213. {
  1214. if (test_bit(SOCK_PASSCRED, &old->flags))
  1215. set_bit(SOCK_PASSCRED, &new->flags);
  1216. if (test_bit(SOCK_PASSSEC, &old->flags))
  1217. set_bit(SOCK_PASSSEC, &new->flags);
  1218. }
  1219. static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
  1220. {
  1221. struct sock *sk = sock->sk;
  1222. struct sock *tsk;
  1223. struct sk_buff *skb;
  1224. int err;
  1225. err = -EOPNOTSUPP;
  1226. if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
  1227. goto out;
  1228. err = -EINVAL;
  1229. if (sk->sk_state != TCP_LISTEN)
  1230. goto out;
  1231. /* If socket state is TCP_LISTEN it cannot change (for now...),
  1232. * so that no locks are necessary.
  1233. */
  1234. skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
  1235. if (!skb) {
  1236. /* This means receive shutdown. */
  1237. if (err == 0)
  1238. err = -EINVAL;
  1239. goto out;
  1240. }
  1241. tsk = skb->sk;
  1242. skb_free_datagram(sk, skb);
  1243. wake_up_interruptible(&unix_sk(sk)->peer_wait);
  1244. /* attach accepted sock to socket */
  1245. unix_state_lock(tsk);
  1246. newsock->state = SS_CONNECTED;
  1247. unix_sock_inherit_flags(sock, newsock);
  1248. sock_graft(tsk, newsock);
  1249. unix_state_unlock(tsk);
  1250. return 0;
  1251. out:
  1252. return err;
  1253. }
  1254. static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
  1255. {
  1256. struct sock *sk = sock->sk;
  1257. struct unix_address *addr;
  1258. DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, uaddr);
  1259. int err = 0;
  1260. if (peer) {
  1261. sk = unix_peer_get(sk);
  1262. err = -ENOTCONN;
  1263. if (!sk)
  1264. goto out;
  1265. err = 0;
  1266. } else {
  1267. sock_hold(sk);
  1268. }
  1269. addr = smp_load_acquire(&unix_sk(sk)->addr);
  1270. if (!addr) {
  1271. sunaddr->sun_family = AF_UNIX;
  1272. sunaddr->sun_path[0] = 0;
  1273. *uaddr_len = sizeof(short);
  1274. } else {
  1275. *uaddr_len = addr->len;
  1276. memcpy(sunaddr, addr->name, *uaddr_len);
  1277. }
  1278. sock_put(sk);
  1279. out:
  1280. return err;
  1281. }
  1282. static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
  1283. {
  1284. int i;
  1285. scm->fp = UNIXCB(skb).fp;
  1286. UNIXCB(skb).fp = NULL;
  1287. for (i = scm->fp->count-1; i >= 0; i--)
  1288. unix_notinflight(scm->fp->user, scm->fp->fp[i]);
  1289. }
  1290. static void unix_destruct_scm(struct sk_buff *skb)
  1291. {
  1292. struct scm_cookie scm;
  1293. memset(&scm, 0, sizeof(scm));
  1294. scm.pid = UNIXCB(skb).pid;
  1295. if (UNIXCB(skb).fp)
  1296. unix_detach_fds(&scm, skb);
  1297. /* Alas, it calls VFS */
  1298. /* So fscking what? fput() had been SMP-safe since the last Summer */
  1299. scm_destroy(&scm);
  1300. sock_wfree(skb);
  1301. }
  1302. /*
  1303. * The "user->unix_inflight" variable is protected by the garbage
  1304. * collection lock, and we just read it locklessly here. If you go
  1305. * over the limit, there might be a tiny race in actually noticing
  1306. * it across threads. Tough.
  1307. */
  1308. static inline bool too_many_unix_fds(struct task_struct *p)
  1309. {
  1310. struct user_struct *user = current_user();
  1311. if (unlikely(user->unix_inflight > task_rlimit(p, RLIMIT_NOFILE)))
  1312. return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
  1313. return false;
  1314. }
  1315. #define MAX_RECURSION_LEVEL 4
  1316. static int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
  1317. {
  1318. int i;
  1319. unsigned char max_level = 0;
  1320. if (too_many_unix_fds(current))
  1321. return -ETOOMANYREFS;
  1322. for (i = scm->fp->count - 1; i >= 0; i--) {
  1323. struct sock *sk = unix_get_socket(scm->fp->fp[i]);
  1324. if (sk)
  1325. max_level = max(max_level,
  1326. unix_sk(sk)->recursion_level);
  1327. }
  1328. if (unlikely(max_level > MAX_RECURSION_LEVEL))
  1329. return -ETOOMANYREFS;
  1330. /*
  1331. * Need to duplicate file references for the sake of garbage
  1332. * collection. Otherwise a socket in the fps might become a
  1333. * candidate for GC while the skb is not yet queued.
  1334. */
  1335. UNIXCB(skb).fp = scm_fp_dup(scm->fp);
  1336. if (!UNIXCB(skb).fp)
  1337. return -ENOMEM;
  1338. for (i = scm->fp->count - 1; i >= 0; i--)
  1339. unix_inflight(scm->fp->user, scm->fp->fp[i]);
  1340. return max_level;
  1341. }
  1342. static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds)
  1343. {
  1344. int err = 0;
  1345. UNIXCB(skb).pid = get_pid(scm->pid);
  1346. UNIXCB(skb).uid = scm->creds.uid;
  1347. UNIXCB(skb).gid = scm->creds.gid;
  1348. UNIXCB(skb).fp = NULL;
  1349. unix_get_secdata(scm, skb);
  1350. if (scm->fp && send_fds)
  1351. err = unix_attach_fds(scm, skb);
  1352. skb->destructor = unix_destruct_scm;
  1353. return err;
  1354. }
  1355. static bool unix_passcred_enabled(const struct socket *sock,
  1356. const struct sock *other)
  1357. {
  1358. return test_bit(SOCK_PASSCRED, &sock->flags) ||
  1359. !other->sk_socket ||
  1360. test_bit(SOCK_PASSCRED, &other->sk_socket->flags);
  1361. }
  1362. /*
  1363. * Some apps rely on write() giving SCM_CREDENTIALS
  1364. * We include credentials if source or destination socket
  1365. * asserted SOCK_PASSCRED.
  1366. */
  1367. static void maybe_add_creds(struct sk_buff *skb, const struct socket *sock,
  1368. const struct sock *other)
  1369. {
  1370. if (UNIXCB(skb).pid)
  1371. return;
  1372. if (unix_passcred_enabled(sock, other)) {
  1373. UNIXCB(skb).pid = get_pid(task_tgid(current));
  1374. current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid);
  1375. }
  1376. }
  1377. static int maybe_init_creds(struct scm_cookie *scm,
  1378. struct socket *socket,
  1379. const struct sock *other)
  1380. {
  1381. int err;
  1382. struct msghdr msg = { .msg_controllen = 0 };
  1383. err = scm_send(socket, &msg, scm, false);
  1384. if (err)
  1385. return err;
  1386. if (unix_passcred_enabled(socket, other)) {
  1387. scm->pid = get_pid(task_tgid(current));
  1388. current_uid_gid(&scm->creds.uid, &scm->creds.gid);
  1389. }
  1390. return err;
  1391. }
  1392. static bool unix_skb_scm_eq(struct sk_buff *skb,
  1393. struct scm_cookie *scm)
  1394. {
  1395. const struct unix_skb_parms *u = &UNIXCB(skb);
  1396. return u->pid == scm->pid &&
  1397. uid_eq(u->uid, scm->creds.uid) &&
  1398. gid_eq(u->gid, scm->creds.gid) &&
  1399. unix_secdata_eq(scm, skb);
  1400. }
  1401. /*
  1402. * Send AF_UNIX data.
  1403. */
  1404. static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
  1405. size_t len)
  1406. {
  1407. struct sock *sk = sock->sk;
  1408. struct net *net = sock_net(sk);
  1409. struct unix_sock *u = unix_sk(sk);
  1410. DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, msg->msg_name);
  1411. struct sock *other = NULL;
  1412. int namelen = 0; /* fake GCC */
  1413. int err;
  1414. unsigned int hash;
  1415. struct sk_buff *skb;
  1416. long timeo;
  1417. struct scm_cookie scm;
  1418. int max_level;
  1419. int data_len = 0;
  1420. int sk_locked;
  1421. wait_for_unix_gc();
  1422. err = scm_send(sock, msg, &scm, false);
  1423. if (err < 0)
  1424. return err;
  1425. err = -EOPNOTSUPP;
  1426. if (msg->msg_flags&MSG_OOB)
  1427. goto out;
  1428. if (msg->msg_namelen) {
  1429. err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
  1430. if (err < 0)
  1431. goto out;
  1432. namelen = err;
  1433. } else {
  1434. sunaddr = NULL;
  1435. err = -ENOTCONN;
  1436. other = unix_peer_get(sk);
  1437. if (!other)
  1438. goto out;
  1439. }
  1440. if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr
  1441. && (err = unix_autobind(sock)) != 0)
  1442. goto out;
  1443. err = -EMSGSIZE;
  1444. if (len > sk->sk_sndbuf - 32)
  1445. goto out;
  1446. if (len > SKB_MAX_ALLOC) {
  1447. data_len = min_t(size_t,
  1448. len - SKB_MAX_ALLOC,
  1449. MAX_SKB_FRAGS * PAGE_SIZE);
  1450. data_len = PAGE_ALIGN(data_len);
  1451. BUILD_BUG_ON(SKB_MAX_ALLOC < PAGE_SIZE);
  1452. }
  1453. skb = sock_alloc_send_pskb(sk, len - data_len, data_len,
  1454. msg->msg_flags & MSG_DONTWAIT, &err,
  1455. PAGE_ALLOC_COSTLY_ORDER);
  1456. if (skb == NULL)
  1457. goto out;
  1458. err = unix_scm_to_skb(&scm, skb, true);
  1459. if (err < 0)
  1460. goto out_free;
  1461. max_level = err + 1;
  1462. skb_put(skb, len - data_len);
  1463. skb->data_len = data_len;
  1464. skb->len = len;
  1465. err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
  1466. if (err)
  1467. goto out_free;
  1468. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1469. restart:
  1470. if (!other) {
  1471. err = -ECONNRESET;
  1472. if (sunaddr == NULL)
  1473. goto out_free;
  1474. other = unix_find_other(net, sunaddr, namelen, sk->sk_type,
  1475. hash, &err);
  1476. if (other == NULL)
  1477. goto out_free;
  1478. }
  1479. if (sk_filter(other, skb) < 0) {
  1480. /* Toss the packet but do not return any error to the sender */
  1481. err = len;
  1482. goto out_free;
  1483. }
  1484. sk_locked = 0;
  1485. unix_state_lock(other);
  1486. restart_locked:
  1487. err = -EPERM;
  1488. if (!unix_may_send(sk, other))
  1489. goto out_unlock;
  1490. if (unlikely(sock_flag(other, SOCK_DEAD))) {
  1491. /*
  1492. * Check with 1003.1g - what should
  1493. * datagram error
  1494. */
  1495. unix_state_unlock(other);
  1496. sock_put(other);
  1497. if (!sk_locked)
  1498. unix_state_lock(sk);
  1499. err = 0;
  1500. if (unix_peer(sk) == other) {
  1501. unix_peer(sk) = NULL;
  1502. unix_dgram_peer_wake_disconnect_wakeup(sk, other);
  1503. unix_state_unlock(sk);
  1504. unix_dgram_disconnected(sk, other);
  1505. sock_put(other);
  1506. err = -ECONNREFUSED;
  1507. } else {
  1508. unix_state_unlock(sk);
  1509. }
  1510. other = NULL;
  1511. if (err)
  1512. goto out_free;
  1513. goto restart;
  1514. }
  1515. err = -EPIPE;
  1516. if (other->sk_shutdown & RCV_SHUTDOWN)
  1517. goto out_unlock;
  1518. if (sk->sk_type != SOCK_SEQPACKET) {
  1519. err = security_unix_may_send(sk->sk_socket, other->sk_socket);
  1520. if (err)
  1521. goto out_unlock;
  1522. }
  1523. /* other == sk && unix_peer(other) != sk if
  1524. * - unix_peer(sk) == NULL, destination address bound to sk
  1525. * - unix_peer(sk) == sk by time of get but disconnected before lock
  1526. */
  1527. if (other != sk &&
  1528. unlikely(unix_peer(other) != sk && unix_recvq_full(other))) {
  1529. if (timeo) {
  1530. timeo = unix_wait_for_peer(other, timeo);
  1531. err = sock_intr_errno(timeo);
  1532. if (signal_pending(current))
  1533. goto out_free;
  1534. goto restart;
  1535. }
  1536. if (!sk_locked) {
  1537. unix_state_unlock(other);
  1538. unix_state_double_lock(sk, other);
  1539. }
  1540. if (unix_peer(sk) != other ||
  1541. unix_dgram_peer_wake_me(sk, other)) {
  1542. err = -EAGAIN;
  1543. sk_locked = 1;
  1544. goto out_unlock;
  1545. }
  1546. if (!sk_locked) {
  1547. sk_locked = 1;
  1548. goto restart_locked;
  1549. }
  1550. }
  1551. if (unlikely(sk_locked))
  1552. unix_state_unlock(sk);
  1553. if (sock_flag(other, SOCK_RCVTSTAMP))
  1554. __net_timestamp(skb);
  1555. maybe_add_creds(skb, sock, other);
  1556. skb_queue_tail(&other->sk_receive_queue, skb);
  1557. if (max_level > unix_sk(other)->recursion_level)
  1558. unix_sk(other)->recursion_level = max_level;
  1559. unix_state_unlock(other);
  1560. other->sk_data_ready(other);
  1561. sock_put(other);
  1562. scm_destroy(&scm);
  1563. return len;
  1564. out_unlock:
  1565. if (sk_locked)
  1566. unix_state_unlock(sk);
  1567. unix_state_unlock(other);
  1568. out_free:
  1569. kfree_skb(skb);
  1570. out:
  1571. if (other)
  1572. sock_put(other);
  1573. scm_destroy(&scm);
  1574. return err;
  1575. }
  1576. /* We use paged skbs for stream sockets, and limit occupancy to 32768
  1577. * bytes, and a minimun of a full page.
  1578. */
  1579. #define UNIX_SKB_FRAGS_SZ (PAGE_SIZE << get_order(32768))
  1580. static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
  1581. size_t len)
  1582. {
  1583. struct sock *sk = sock->sk;
  1584. struct sock *other = NULL;
  1585. int err, size;
  1586. struct sk_buff *skb;
  1587. int sent = 0;
  1588. struct scm_cookie scm;
  1589. bool fds_sent = false;
  1590. int max_level;
  1591. int data_len;
  1592. wait_for_unix_gc();
  1593. err = scm_send(sock, msg, &scm, false);
  1594. if (err < 0)
  1595. return err;
  1596. err = -EOPNOTSUPP;
  1597. if (msg->msg_flags&MSG_OOB)
  1598. goto out_err;
  1599. if (msg->msg_namelen) {
  1600. err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
  1601. goto out_err;
  1602. } else {
  1603. err = -ENOTCONN;
  1604. other = unix_peer(sk);
  1605. if (!other)
  1606. goto out_err;
  1607. }
  1608. if (sk->sk_shutdown & SEND_SHUTDOWN)
  1609. goto pipe_err;
  1610. while (sent < len) {
  1611. size = len - sent;
  1612. /* Keep two messages in the pipe so it schedules better */
  1613. size = min_t(int, size, (sk->sk_sndbuf >> 1) - 64);
  1614. /* allow fallback to order-0 allocations */
  1615. size = min_t(int, size, SKB_MAX_HEAD(0) + UNIX_SKB_FRAGS_SZ);
  1616. data_len = max_t(int, 0, size - SKB_MAX_HEAD(0));
  1617. data_len = min_t(size_t, size, PAGE_ALIGN(data_len));
  1618. skb = sock_alloc_send_pskb(sk, size - data_len, data_len,
  1619. msg->msg_flags & MSG_DONTWAIT, &err,
  1620. get_order(UNIX_SKB_FRAGS_SZ));
  1621. if (!skb)
  1622. goto out_err;
  1623. /* Only send the fds in the first buffer */
  1624. err = unix_scm_to_skb(&scm, skb, !fds_sent);
  1625. if (err < 0) {
  1626. kfree_skb(skb);
  1627. goto out_err;
  1628. }
  1629. max_level = err + 1;
  1630. fds_sent = true;
  1631. skb_put(skb, size - data_len);
  1632. skb->data_len = data_len;
  1633. skb->len = size;
  1634. err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size);
  1635. if (err) {
  1636. kfree_skb(skb);
  1637. goto out_err;
  1638. }
  1639. unix_state_lock(other);
  1640. if (sock_flag(other, SOCK_DEAD) ||
  1641. (other->sk_shutdown & RCV_SHUTDOWN))
  1642. goto pipe_err_free;
  1643. maybe_add_creds(skb, sock, other);
  1644. skb_queue_tail(&other->sk_receive_queue, skb);
  1645. if (max_level > unix_sk(other)->recursion_level)
  1646. unix_sk(other)->recursion_level = max_level;
  1647. unix_state_unlock(other);
  1648. other->sk_data_ready(other);
  1649. sent += size;
  1650. }
  1651. scm_destroy(&scm);
  1652. return sent;
  1653. pipe_err_free:
  1654. unix_state_unlock(other);
  1655. kfree_skb(skb);
  1656. pipe_err:
  1657. if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
  1658. send_sig(SIGPIPE, current, 0);
  1659. err = -EPIPE;
  1660. out_err:
  1661. scm_destroy(&scm);
  1662. return sent ? : err;
  1663. }
  1664. static ssize_t unix_stream_sendpage(struct socket *socket, struct page *page,
  1665. int offset, size_t size, int flags)
  1666. {
  1667. int err;
  1668. bool send_sigpipe = false;
  1669. bool init_scm = true;
  1670. struct scm_cookie scm;
  1671. struct sock *other, *sk = socket->sk;
  1672. struct sk_buff *skb, *newskb = NULL, *tail = NULL;
  1673. if (flags & MSG_OOB)
  1674. return -EOPNOTSUPP;
  1675. other = unix_peer(sk);
  1676. if (!other || sk->sk_state != TCP_ESTABLISHED)
  1677. return -ENOTCONN;
  1678. if (false) {
  1679. alloc_skb:
  1680. unix_state_unlock(other);
  1681. mutex_unlock(&unix_sk(other)->iolock);
  1682. newskb = sock_alloc_send_pskb(sk, 0, 0, flags & MSG_DONTWAIT,
  1683. &err, 0);
  1684. if (!newskb)
  1685. goto err;
  1686. }
  1687. /* we must acquire iolock as we modify already present
  1688. * skbs in the sk_receive_queue and mess with skb->len
  1689. */
  1690. err = mutex_lock_interruptible(&unix_sk(other)->iolock);
  1691. if (err) {
  1692. err = flags & MSG_DONTWAIT ? -EAGAIN : -ERESTARTSYS;
  1693. goto err;
  1694. }
  1695. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  1696. err = -EPIPE;
  1697. send_sigpipe = true;
  1698. goto err_unlock;
  1699. }
  1700. unix_state_lock(other);
  1701. if (sock_flag(other, SOCK_DEAD) ||
  1702. other->sk_shutdown & RCV_SHUTDOWN) {
  1703. err = -EPIPE;
  1704. send_sigpipe = true;
  1705. goto err_state_unlock;
  1706. }
  1707. if (init_scm) {
  1708. err = maybe_init_creds(&scm, socket, other);
  1709. if (err)
  1710. goto err_state_unlock;
  1711. init_scm = false;
  1712. }
  1713. skb = skb_peek_tail(&other->sk_receive_queue);
  1714. if (tail && tail == skb) {
  1715. skb = newskb;
  1716. } else if (!skb || !unix_skb_scm_eq(skb, &scm)) {
  1717. if (newskb) {
  1718. skb = newskb;
  1719. } else {
  1720. tail = skb;
  1721. goto alloc_skb;
  1722. }
  1723. } else if (newskb) {
  1724. /* this is fast path, we don't necessarily need to
  1725. * call to kfree_skb even though with newskb == NULL
  1726. * this - does no harm
  1727. */
  1728. consume_skb(newskb);
  1729. newskb = NULL;
  1730. }
  1731. if (skb_append_pagefrags(skb, page, offset, size)) {
  1732. tail = skb;
  1733. goto alloc_skb;
  1734. }
  1735. skb->len += size;
  1736. skb->data_len += size;
  1737. skb->truesize += size;
  1738. atomic_add(size, &sk->sk_wmem_alloc);
  1739. if (newskb) {
  1740. err = unix_scm_to_skb(&scm, skb, false);
  1741. if (err)
  1742. goto err_state_unlock;
  1743. spin_lock(&other->sk_receive_queue.lock);
  1744. __skb_queue_tail(&other->sk_receive_queue, newskb);
  1745. spin_unlock(&other->sk_receive_queue.lock);
  1746. }
  1747. unix_state_unlock(other);
  1748. mutex_unlock(&unix_sk(other)->iolock);
  1749. other->sk_data_ready(other);
  1750. scm_destroy(&scm);
  1751. return size;
  1752. err_state_unlock:
  1753. unix_state_unlock(other);
  1754. err_unlock:
  1755. mutex_unlock(&unix_sk(other)->iolock);
  1756. err:
  1757. kfree_skb(newskb);
  1758. if (send_sigpipe && !(flags & MSG_NOSIGNAL))
  1759. send_sig(SIGPIPE, current, 0);
  1760. if (!init_scm)
  1761. scm_destroy(&scm);
  1762. return err;
  1763. }
  1764. static int unix_seqpacket_sendmsg(struct socket *sock, struct msghdr *msg,
  1765. size_t len)
  1766. {
  1767. int err;
  1768. struct sock *sk = sock->sk;
  1769. err = sock_error(sk);
  1770. if (err)
  1771. return err;
  1772. if (sk->sk_state != TCP_ESTABLISHED)
  1773. return -ENOTCONN;
  1774. if (msg->msg_namelen)
  1775. msg->msg_namelen = 0;
  1776. return unix_dgram_sendmsg(sock, msg, len);
  1777. }
  1778. static int unix_seqpacket_recvmsg(struct socket *sock, struct msghdr *msg,
  1779. size_t size, int flags)
  1780. {
  1781. struct sock *sk = sock->sk;
  1782. if (sk->sk_state != TCP_ESTABLISHED)
  1783. return -ENOTCONN;
  1784. return unix_dgram_recvmsg(sock, msg, size, flags);
  1785. }
  1786. static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
  1787. {
  1788. struct unix_address *addr = smp_load_acquire(&unix_sk(sk)->addr);
  1789. if (addr) {
  1790. msg->msg_namelen = addr->len;
  1791. memcpy(msg->msg_name, addr->name, addr->len);
  1792. }
  1793. }
  1794. static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
  1795. size_t size, int flags)
  1796. {
  1797. struct scm_cookie scm;
  1798. struct sock *sk = sock->sk;
  1799. struct unix_sock *u = unix_sk(sk);
  1800. int noblock = flags & MSG_DONTWAIT;
  1801. struct sk_buff *skb;
  1802. int err;
  1803. int peeked, skip;
  1804. err = -EOPNOTSUPP;
  1805. if (flags&MSG_OOB)
  1806. goto out;
  1807. err = mutex_lock_interruptible(&u->iolock);
  1808. if (unlikely(err)) {
  1809. /* recvmsg() in non blocking mode is supposed to return -EAGAIN
  1810. * sk_rcvtimeo is not honored by mutex_lock_interruptible()
  1811. */
  1812. err = noblock ? -EAGAIN : -ERESTARTSYS;
  1813. goto out;
  1814. }
  1815. skip = sk_peek_offset(sk, flags);
  1816. skb = __skb_recv_datagram(sk, flags, &peeked, &skip, &err);
  1817. if (!skb) {
  1818. unix_state_lock(sk);
  1819. /* Signal EOF on disconnected non-blocking SEQPACKET socket. */
  1820. if (sk->sk_type == SOCK_SEQPACKET && err == -EAGAIN &&
  1821. (sk->sk_shutdown & RCV_SHUTDOWN))
  1822. err = 0;
  1823. unix_state_unlock(sk);
  1824. goto out_unlock;
  1825. }
  1826. wake_up_interruptible_sync_poll(&u->peer_wait,
  1827. POLLOUT | POLLWRNORM | POLLWRBAND);
  1828. if (msg->msg_name)
  1829. unix_copy_addr(msg, skb->sk);
  1830. if (size > skb->len - skip)
  1831. size = skb->len - skip;
  1832. else if (size < skb->len - skip)
  1833. msg->msg_flags |= MSG_TRUNC;
  1834. err = skb_copy_datagram_msg(skb, skip, msg, size);
  1835. if (err)
  1836. goto out_free;
  1837. if (sock_flag(sk, SOCK_RCVTSTAMP))
  1838. __sock_recv_timestamp(msg, sk, skb);
  1839. memset(&scm, 0, sizeof(scm));
  1840. scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
  1841. unix_set_secdata(&scm, skb);
  1842. if (!(flags & MSG_PEEK)) {
  1843. if (UNIXCB(skb).fp)
  1844. unix_detach_fds(&scm, skb);
  1845. sk_peek_offset_bwd(sk, skb->len);
  1846. } else {
  1847. /* It is questionable: on PEEK we could:
  1848. - do not return fds - good, but too simple 8)
  1849. - return fds, and do not return them on read (old strategy,
  1850. apparently wrong)
  1851. - clone fds (I chose it for now, it is the most universal
  1852. solution)
  1853. POSIX 1003.1g does not actually define this clearly
  1854. at all. POSIX 1003.1g doesn't define a lot of things
  1855. clearly however!
  1856. */
  1857. sk_peek_offset_fwd(sk, size);
  1858. if (UNIXCB(skb).fp)
  1859. scm.fp = scm_fp_dup(UNIXCB(skb).fp);
  1860. }
  1861. err = (flags & MSG_TRUNC) ? skb->len - skip : size;
  1862. scm_recv(sock, msg, &scm, flags);
  1863. out_free:
  1864. skb_free_datagram(sk, skb);
  1865. out_unlock:
  1866. mutex_unlock(&u->iolock);
  1867. out:
  1868. return err;
  1869. }
  1870. /*
  1871. * Sleep until more data has arrived. But check for races..
  1872. */
  1873. static long unix_stream_data_wait(struct sock *sk, long timeo,
  1874. struct sk_buff *last, unsigned int last_len,
  1875. bool freezable)
  1876. {
  1877. struct sk_buff *tail;
  1878. DEFINE_WAIT(wait);
  1879. unix_state_lock(sk);
  1880. for (;;) {
  1881. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1882. tail = skb_peek_tail(&sk->sk_receive_queue);
  1883. if (tail != last ||
  1884. (tail && tail->len != last_len) ||
  1885. sk->sk_err ||
  1886. (sk->sk_shutdown & RCV_SHUTDOWN) ||
  1887. signal_pending(current) ||
  1888. !timeo)
  1889. break;
  1890. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1891. unix_state_unlock(sk);
  1892. if (freezable)
  1893. timeo = freezable_schedule_timeout(timeo);
  1894. else
  1895. timeo = schedule_timeout(timeo);
  1896. unix_state_lock(sk);
  1897. if (sock_flag(sk, SOCK_DEAD))
  1898. break;
  1899. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1900. }
  1901. finish_wait(sk_sleep(sk), &wait);
  1902. unix_state_unlock(sk);
  1903. return timeo;
  1904. }
  1905. static unsigned int unix_skb_len(const struct sk_buff *skb)
  1906. {
  1907. return skb->len - UNIXCB(skb).consumed;
  1908. }
  1909. struct unix_stream_read_state {
  1910. int (*recv_actor)(struct sk_buff *, int, int,
  1911. struct unix_stream_read_state *);
  1912. struct socket *socket;
  1913. struct msghdr *msg;
  1914. struct pipe_inode_info *pipe;
  1915. size_t size;
  1916. int flags;
  1917. unsigned int splice_flags;
  1918. };
  1919. static int unix_stream_read_generic(struct unix_stream_read_state *state,
  1920. bool freezable)
  1921. {
  1922. struct scm_cookie scm;
  1923. struct socket *sock = state->socket;
  1924. struct sock *sk = sock->sk;
  1925. struct unix_sock *u = unix_sk(sk);
  1926. int copied = 0;
  1927. int flags = state->flags;
  1928. int noblock = flags & MSG_DONTWAIT;
  1929. bool check_creds = false;
  1930. int target;
  1931. int err = 0;
  1932. long timeo;
  1933. int skip;
  1934. size_t size = state->size;
  1935. unsigned int last_len;
  1936. if (unlikely(sk->sk_state != TCP_ESTABLISHED)) {
  1937. err = -EINVAL;
  1938. goto out;
  1939. }
  1940. if (unlikely(flags & MSG_OOB)) {
  1941. err = -EOPNOTSUPP;
  1942. goto out;
  1943. }
  1944. target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
  1945. timeo = sock_rcvtimeo(sk, noblock);
  1946. memset(&scm, 0, sizeof(scm));
  1947. /* Lock the socket to prevent queue disordering
  1948. * while sleeps in memcpy_tomsg
  1949. */
  1950. mutex_lock(&u->iolock);
  1951. if (flags & MSG_PEEK)
  1952. skip = sk_peek_offset(sk, flags);
  1953. else
  1954. skip = 0;
  1955. do {
  1956. int chunk;
  1957. bool drop_skb;
  1958. struct sk_buff *skb, *last;
  1959. unix_state_lock(sk);
  1960. if (sock_flag(sk, SOCK_DEAD)) {
  1961. err = -ECONNRESET;
  1962. goto unlock;
  1963. }
  1964. last = skb = skb_peek(&sk->sk_receive_queue);
  1965. last_len = last ? last->len : 0;
  1966. again:
  1967. if (skb == NULL) {
  1968. unix_sk(sk)->recursion_level = 0;
  1969. if (copied >= target)
  1970. goto unlock;
  1971. /*
  1972. * POSIX 1003.1g mandates this order.
  1973. */
  1974. err = sock_error(sk);
  1975. if (err)
  1976. goto unlock;
  1977. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1978. goto unlock;
  1979. unix_state_unlock(sk);
  1980. if (!timeo) {
  1981. err = -EAGAIN;
  1982. break;
  1983. }
  1984. mutex_unlock(&u->iolock);
  1985. timeo = unix_stream_data_wait(sk, timeo, last,
  1986. last_len, freezable);
  1987. if (signal_pending(current)) {
  1988. err = sock_intr_errno(timeo);
  1989. scm_destroy(&scm);
  1990. goto out;
  1991. }
  1992. mutex_lock(&u->iolock);
  1993. continue;
  1994. unlock:
  1995. unix_state_unlock(sk);
  1996. break;
  1997. }
  1998. while (skip >= unix_skb_len(skb)) {
  1999. skip -= unix_skb_len(skb);
  2000. last = skb;
  2001. last_len = skb->len;
  2002. skb = skb_peek_next(skb, &sk->sk_receive_queue);
  2003. if (!skb)
  2004. goto again;
  2005. }
  2006. unix_state_unlock(sk);
  2007. if (check_creds) {
  2008. /* Never glue messages from different writers */
  2009. if (!unix_skb_scm_eq(skb, &scm))
  2010. break;
  2011. } else if (test_bit(SOCK_PASSCRED, &sock->flags)) {
  2012. /* Copy credentials */
  2013. scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
  2014. unix_set_secdata(&scm, skb);
  2015. check_creds = true;
  2016. }
  2017. /* Copy address just once */
  2018. if (state->msg && state->msg->msg_name) {
  2019. DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr,
  2020. state->msg->msg_name);
  2021. unix_copy_addr(state->msg, skb->sk);
  2022. sunaddr = NULL;
  2023. }
  2024. chunk = min_t(unsigned int, unix_skb_len(skb) - skip, size);
  2025. skb_get(skb);
  2026. chunk = state->recv_actor(skb, skip, chunk, state);
  2027. drop_skb = !unix_skb_len(skb);
  2028. /* skb is only safe to use if !drop_skb */
  2029. consume_skb(skb);
  2030. if (chunk < 0) {
  2031. if (copied == 0)
  2032. copied = -EFAULT;
  2033. break;
  2034. }
  2035. copied += chunk;
  2036. size -= chunk;
  2037. if (drop_skb) {
  2038. /* the skb was touched by a concurrent reader;
  2039. * we should not expect anything from this skb
  2040. * anymore and assume it invalid - we can be
  2041. * sure it was dropped from the socket queue
  2042. *
  2043. * let's report a short read
  2044. */
  2045. err = 0;
  2046. break;
  2047. }
  2048. /* Mark read part of skb as used */
  2049. if (!(flags & MSG_PEEK)) {
  2050. UNIXCB(skb).consumed += chunk;
  2051. sk_peek_offset_bwd(sk, chunk);
  2052. if (UNIXCB(skb).fp)
  2053. unix_detach_fds(&scm, skb);
  2054. if (unix_skb_len(skb))
  2055. break;
  2056. skb_unlink(skb, &sk->sk_receive_queue);
  2057. consume_skb(skb);
  2058. if (scm.fp)
  2059. break;
  2060. } else {
  2061. /* It is questionable, see note in unix_dgram_recvmsg.
  2062. */
  2063. if (UNIXCB(skb).fp)
  2064. scm.fp = scm_fp_dup(UNIXCB(skb).fp);
  2065. sk_peek_offset_fwd(sk, chunk);
  2066. if (UNIXCB(skb).fp)
  2067. break;
  2068. skip = 0;
  2069. last = skb;
  2070. last_len = skb->len;
  2071. unix_state_lock(sk);
  2072. skb = skb_peek_next(skb, &sk->sk_receive_queue);
  2073. if (skb)
  2074. goto again;
  2075. unix_state_unlock(sk);
  2076. break;
  2077. }
  2078. } while (size);
  2079. mutex_unlock(&u->iolock);
  2080. if (state->msg)
  2081. scm_recv(sock, state->msg, &scm, flags);
  2082. else
  2083. scm_destroy(&scm);
  2084. out:
  2085. return copied ? : err;
  2086. }
  2087. static int unix_stream_read_actor(struct sk_buff *skb,
  2088. int skip, int chunk,
  2089. struct unix_stream_read_state *state)
  2090. {
  2091. int ret;
  2092. ret = skb_copy_datagram_msg(skb, UNIXCB(skb).consumed + skip,
  2093. state->msg, chunk);
  2094. return ret ?: chunk;
  2095. }
  2096. static int unix_stream_recvmsg(struct socket *sock, struct msghdr *msg,
  2097. size_t size, int flags)
  2098. {
  2099. struct unix_stream_read_state state = {
  2100. .recv_actor = unix_stream_read_actor,
  2101. .socket = sock,
  2102. .msg = msg,
  2103. .size = size,
  2104. .flags = flags
  2105. };
  2106. return unix_stream_read_generic(&state, true);
  2107. }
  2108. static ssize_t skb_unix_socket_splice(struct sock *sk,
  2109. struct pipe_inode_info *pipe,
  2110. struct splice_pipe_desc *spd)
  2111. {
  2112. int ret;
  2113. struct unix_sock *u = unix_sk(sk);
  2114. mutex_unlock(&u->iolock);
  2115. ret = splice_to_pipe(pipe, spd);
  2116. mutex_lock(&u->iolock);
  2117. return ret;
  2118. }
  2119. static int unix_stream_splice_actor(struct sk_buff *skb,
  2120. int skip, int chunk,
  2121. struct unix_stream_read_state *state)
  2122. {
  2123. return skb_splice_bits(skb, state->socket->sk,
  2124. UNIXCB(skb).consumed + skip,
  2125. state->pipe, chunk, state->splice_flags,
  2126. skb_unix_socket_splice);
  2127. }
  2128. static ssize_t unix_stream_splice_read(struct socket *sock, loff_t *ppos,
  2129. struct pipe_inode_info *pipe,
  2130. size_t size, unsigned int flags)
  2131. {
  2132. struct unix_stream_read_state state = {
  2133. .recv_actor = unix_stream_splice_actor,
  2134. .socket = sock,
  2135. .pipe = pipe,
  2136. .size = size,
  2137. .splice_flags = flags,
  2138. };
  2139. if (unlikely(*ppos))
  2140. return -ESPIPE;
  2141. if (sock->file->f_flags & O_NONBLOCK ||
  2142. flags & SPLICE_F_NONBLOCK)
  2143. state.flags = MSG_DONTWAIT;
  2144. return unix_stream_read_generic(&state, false);
  2145. }
  2146. static int unix_shutdown(struct socket *sock, int mode)
  2147. {
  2148. struct sock *sk = sock->sk;
  2149. struct sock *other;
  2150. if (mode < SHUT_RD || mode > SHUT_RDWR)
  2151. return -EINVAL;
  2152. /* This maps:
  2153. * SHUT_RD (0) -> RCV_SHUTDOWN (1)
  2154. * SHUT_WR (1) -> SEND_SHUTDOWN (2)
  2155. * SHUT_RDWR (2) -> SHUTDOWN_MASK (3)
  2156. */
  2157. ++mode;
  2158. unix_state_lock(sk);
  2159. sk->sk_shutdown |= mode;
  2160. other = unix_peer(sk);
  2161. if (other)
  2162. sock_hold(other);
  2163. unix_state_unlock(sk);
  2164. sk->sk_state_change(sk);
  2165. if (other &&
  2166. (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
  2167. int peer_mode = 0;
  2168. if (mode&RCV_SHUTDOWN)
  2169. peer_mode |= SEND_SHUTDOWN;
  2170. if (mode&SEND_SHUTDOWN)
  2171. peer_mode |= RCV_SHUTDOWN;
  2172. unix_state_lock(other);
  2173. other->sk_shutdown |= peer_mode;
  2174. unix_state_unlock(other);
  2175. other->sk_state_change(other);
  2176. if (peer_mode == SHUTDOWN_MASK)
  2177. sk_wake_async(other, SOCK_WAKE_WAITD, POLL_HUP);
  2178. else if (peer_mode & RCV_SHUTDOWN)
  2179. sk_wake_async(other, SOCK_WAKE_WAITD, POLL_IN);
  2180. }
  2181. if (other)
  2182. sock_put(other);
  2183. return 0;
  2184. }
  2185. long unix_inq_len(struct sock *sk)
  2186. {
  2187. struct sk_buff *skb;
  2188. long amount = 0;
  2189. if (sk->sk_state == TCP_LISTEN)
  2190. return -EINVAL;
  2191. spin_lock(&sk->sk_receive_queue.lock);
  2192. if (sk->sk_type == SOCK_STREAM ||
  2193. sk->sk_type == SOCK_SEQPACKET) {
  2194. skb_queue_walk(&sk->sk_receive_queue, skb)
  2195. amount += unix_skb_len(skb);
  2196. } else {
  2197. skb = skb_peek(&sk->sk_receive_queue);
  2198. if (skb)
  2199. amount = skb->len;
  2200. }
  2201. spin_unlock(&sk->sk_receive_queue.lock);
  2202. return amount;
  2203. }
  2204. EXPORT_SYMBOL_GPL(unix_inq_len);
  2205. long unix_outq_len(struct sock *sk)
  2206. {
  2207. return sk_wmem_alloc_get(sk);
  2208. }
  2209. EXPORT_SYMBOL_GPL(unix_outq_len);
  2210. static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  2211. {
  2212. struct sock *sk = sock->sk;
  2213. long amount = 0;
  2214. int err;
  2215. switch (cmd) {
  2216. case SIOCOUTQ:
  2217. amount = unix_outq_len(sk);
  2218. err = put_user(amount, (int __user *)arg);
  2219. break;
  2220. case SIOCINQ:
  2221. amount = unix_inq_len(sk);
  2222. if (amount < 0)
  2223. err = amount;
  2224. else
  2225. err = put_user(amount, (int __user *)arg);
  2226. break;
  2227. default:
  2228. err = -ENOIOCTLCMD;
  2229. break;
  2230. }
  2231. return err;
  2232. }
  2233. static unsigned int unix_poll(struct file *file, struct socket *sock, poll_table *wait)
  2234. {
  2235. struct sock *sk = sock->sk;
  2236. unsigned int mask;
  2237. sock_poll_wait(file, sk_sleep(sk), wait);
  2238. mask = 0;
  2239. /* exceptional events? */
  2240. if (sk->sk_err)
  2241. mask |= POLLERR;
  2242. if (sk->sk_shutdown == SHUTDOWN_MASK)
  2243. mask |= POLLHUP;
  2244. if (sk->sk_shutdown & RCV_SHUTDOWN)
  2245. mask |= POLLRDHUP | POLLIN | POLLRDNORM;
  2246. /* readable? */
  2247. if (!skb_queue_empty(&sk->sk_receive_queue))
  2248. mask |= POLLIN | POLLRDNORM;
  2249. /* Connection-based need to check for termination and startup */
  2250. if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) &&
  2251. sk->sk_state == TCP_CLOSE)
  2252. mask |= POLLHUP;
  2253. /*
  2254. * we set writable also when the other side has shut down the
  2255. * connection. This prevents stuck sockets.
  2256. */
  2257. if (unix_writable(sk))
  2258. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  2259. return mask;
  2260. }
  2261. static unsigned int unix_dgram_poll(struct file *file, struct socket *sock,
  2262. poll_table *wait)
  2263. {
  2264. struct sock *sk = sock->sk, *other;
  2265. unsigned int mask, writable;
  2266. sock_poll_wait(file, sk_sleep(sk), wait);
  2267. mask = 0;
  2268. /* exceptional events? */
  2269. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  2270. mask |= POLLERR |
  2271. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
  2272. if (sk->sk_shutdown & RCV_SHUTDOWN)
  2273. mask |= POLLRDHUP | POLLIN | POLLRDNORM;
  2274. if (sk->sk_shutdown == SHUTDOWN_MASK)
  2275. mask |= POLLHUP;
  2276. /* readable? */
  2277. if (!skb_queue_empty(&sk->sk_receive_queue))
  2278. mask |= POLLIN | POLLRDNORM;
  2279. /* Connection-based need to check for termination and startup */
  2280. if (sk->sk_type == SOCK_SEQPACKET) {
  2281. if (sk->sk_state == TCP_CLOSE)
  2282. mask |= POLLHUP;
  2283. /* connection hasn't started yet? */
  2284. if (sk->sk_state == TCP_SYN_SENT)
  2285. return mask;
  2286. }
  2287. /* No write status requested, avoid expensive OUT tests. */
  2288. if (!(poll_requested_events(wait) & (POLLWRBAND|POLLWRNORM|POLLOUT)))
  2289. return mask;
  2290. writable = unix_writable(sk);
  2291. if (writable) {
  2292. unix_state_lock(sk);
  2293. other = unix_peer(sk);
  2294. if (other && unix_peer(other) != sk &&
  2295. unix_recvq_full(other) &&
  2296. unix_dgram_peer_wake_me(sk, other))
  2297. writable = 0;
  2298. unix_state_unlock(sk);
  2299. }
  2300. if (writable)
  2301. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  2302. else
  2303. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  2304. return mask;
  2305. }
  2306. #ifdef CONFIG_PROC_FS
  2307. #define BUCKET_SPACE (BITS_PER_LONG - (UNIX_HASH_BITS + 1) - 1)
  2308. #define get_bucket(x) ((x) >> BUCKET_SPACE)
  2309. #define get_offset(x) ((x) & ((1L << BUCKET_SPACE) - 1))
  2310. #define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o))
  2311. static struct sock *unix_from_bucket(struct seq_file *seq, loff_t *pos)
  2312. {
  2313. unsigned long offset = get_offset(*pos);
  2314. unsigned long bucket = get_bucket(*pos);
  2315. struct sock *sk;
  2316. unsigned long count = 0;
  2317. for (sk = sk_head(&unix_socket_table[bucket]); sk; sk = sk_next(sk)) {
  2318. if (sock_net(sk) != seq_file_net(seq))
  2319. continue;
  2320. if (++count == offset)
  2321. break;
  2322. }
  2323. return sk;
  2324. }
  2325. static struct sock *unix_next_socket(struct seq_file *seq,
  2326. struct sock *sk,
  2327. loff_t *pos)
  2328. {
  2329. unsigned long bucket;
  2330. while (sk > (struct sock *)SEQ_START_TOKEN) {
  2331. sk = sk_next(sk);
  2332. if (!sk)
  2333. goto next_bucket;
  2334. if (sock_net(sk) == seq_file_net(seq))
  2335. return sk;
  2336. }
  2337. do {
  2338. sk = unix_from_bucket(seq, pos);
  2339. if (sk)
  2340. return sk;
  2341. next_bucket:
  2342. bucket = get_bucket(*pos) + 1;
  2343. *pos = set_bucket_offset(bucket, 1);
  2344. } while (bucket < ARRAY_SIZE(unix_socket_table));
  2345. return NULL;
  2346. }
  2347. static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
  2348. __acquires(unix_table_lock)
  2349. {
  2350. spin_lock(&unix_table_lock);
  2351. if (!*pos)
  2352. return SEQ_START_TOKEN;
  2353. if (get_bucket(*pos) >= ARRAY_SIZE(unix_socket_table))
  2354. return NULL;
  2355. return unix_next_socket(seq, NULL, pos);
  2356. }
  2357. static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2358. {
  2359. ++*pos;
  2360. return unix_next_socket(seq, v, pos);
  2361. }
  2362. static void unix_seq_stop(struct seq_file *seq, void *v)
  2363. __releases(unix_table_lock)
  2364. {
  2365. spin_unlock(&unix_table_lock);
  2366. }
  2367. static int unix_seq_show(struct seq_file *seq, void *v)
  2368. {
  2369. if (v == SEQ_START_TOKEN)
  2370. seq_puts(seq, "Num RefCount Protocol Flags Type St "
  2371. "Inode Path\n");
  2372. else {
  2373. struct sock *s = v;
  2374. struct unix_sock *u = unix_sk(s);
  2375. unix_state_lock(s);
  2376. seq_printf(seq, "%pK: %08X %08X %08X %04X %02X %5lu",
  2377. s,
  2378. atomic_read(&s->sk_refcnt),
  2379. 0,
  2380. s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
  2381. s->sk_type,
  2382. s->sk_socket ?
  2383. (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
  2384. (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
  2385. sock_i_ino(s));
  2386. if (u->addr) { // under unix_table_lock here
  2387. int i, len;
  2388. seq_putc(seq, ' ');
  2389. i = 0;
  2390. len = u->addr->len - sizeof(short);
  2391. if (!UNIX_ABSTRACT(s))
  2392. len--;
  2393. else {
  2394. seq_putc(seq, '@');
  2395. i++;
  2396. }
  2397. for ( ; i < len; i++)
  2398. seq_putc(seq, u->addr->name->sun_path[i]);
  2399. }
  2400. unix_state_unlock(s);
  2401. seq_putc(seq, '\n');
  2402. }
  2403. return 0;
  2404. }
  2405. static const struct seq_operations unix_seq_ops = {
  2406. .start = unix_seq_start,
  2407. .next = unix_seq_next,
  2408. .stop = unix_seq_stop,
  2409. .show = unix_seq_show,
  2410. };
  2411. static int unix_seq_open(struct inode *inode, struct file *file)
  2412. {
  2413. return seq_open_net(inode, file, &unix_seq_ops,
  2414. sizeof(struct seq_net_private));
  2415. }
  2416. static const struct file_operations unix_seq_fops = {
  2417. .owner = THIS_MODULE,
  2418. .open = unix_seq_open,
  2419. .read = seq_read,
  2420. .llseek = seq_lseek,
  2421. .release = seq_release_net,
  2422. };
  2423. #endif
  2424. static const struct net_proto_family unix_family_ops = {
  2425. .family = PF_UNIX,
  2426. .create = unix_create,
  2427. .owner = THIS_MODULE,
  2428. };
  2429. static int __net_init unix_net_init(struct net *net)
  2430. {
  2431. int error = -ENOMEM;
  2432. net->unx.sysctl_max_dgram_qlen = 10;
  2433. if (unix_sysctl_register(net))
  2434. goto out;
  2435. #ifdef CONFIG_PROC_FS
  2436. if (!proc_create("unix", 0, net->proc_net, &unix_seq_fops)) {
  2437. unix_sysctl_unregister(net);
  2438. goto out;
  2439. }
  2440. #endif
  2441. error = 0;
  2442. out:
  2443. return error;
  2444. }
  2445. static void __net_exit unix_net_exit(struct net *net)
  2446. {
  2447. unix_sysctl_unregister(net);
  2448. remove_proc_entry("unix", net->proc_net);
  2449. }
  2450. static struct pernet_operations unix_net_ops = {
  2451. .init = unix_net_init,
  2452. .exit = unix_net_exit,
  2453. };
  2454. static int __init af_unix_init(void)
  2455. {
  2456. int rc = -1;
  2457. BUILD_BUG_ON(sizeof(struct unix_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
  2458. rc = proto_register(&unix_proto, 1);
  2459. if (rc != 0) {
  2460. pr_crit("%s: Cannot create unix_sock SLAB cache!\n", __func__);
  2461. goto out;
  2462. }
  2463. sock_register(&unix_family_ops);
  2464. register_pernet_subsys(&unix_net_ops);
  2465. out:
  2466. return rc;
  2467. }
  2468. static void __exit af_unix_exit(void)
  2469. {
  2470. sock_unregister(PF_UNIX);
  2471. proto_unregister(&unix_proto);
  2472. unregister_pernet_subsys(&unix_net_ops);
  2473. }
  2474. /* Earlier than device_initcall() so that other drivers invoking
  2475. request_module() don't end up in a loop when modprobe tries
  2476. to use a UNIX socket. But later than subsys_initcall() because
  2477. we depend on stuff initialised there */
  2478. fs_initcall(af_unix_init);
  2479. module_exit(af_unix_exit);
  2480. MODULE_LICENSE("GPL");
  2481. MODULE_ALIAS_NETPROTO(PF_UNIX);