socket.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584
  1. /*
  2. * NET An implementation of the SOCKET network access protocol.
  3. *
  4. * Version: @(#)socket.c 1.1.93 18/02/95
  5. *
  6. * Authors: Orest Zborowski, <obz@Kodak.COM>
  7. * Ross Biro
  8. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  9. *
  10. * Fixes:
  11. * Anonymous : NOTSOCK/BADF cleanup. Error fix in
  12. * shutdown()
  13. * Alan Cox : verify_area() fixes
  14. * Alan Cox : Removed DDI
  15. * Jonathan Kamens : SOCK_DGRAM reconnect bug
  16. * Alan Cox : Moved a load of checks to the very
  17. * top level.
  18. * Alan Cox : Move address structures to/from user
  19. * mode above the protocol layers.
  20. * Rob Janssen : Allow 0 length sends.
  21. * Alan Cox : Asynchronous I/O support (cribbed from the
  22. * tty drivers).
  23. * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
  24. * Jeff Uphoff : Made max number of sockets command-line
  25. * configurable.
  26. * Matti Aarnio : Made the number of sockets dynamic,
  27. * to be allocated when needed, and mr.
  28. * Uphoff's max is used as max to be
  29. * allowed to allocate.
  30. * Linus : Argh. removed all the socket allocation
  31. * altogether: it's in the inode now.
  32. * Alan Cox : Made sock_alloc()/sock_release() public
  33. * for NetROM and future kernel nfsd type
  34. * stuff.
  35. * Alan Cox : sendmsg/recvmsg basics.
  36. * Tom Dyas : Export net symbols.
  37. * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
  38. * Alan Cox : Added thread locking to sys_* calls
  39. * for sockets. May have errors at the
  40. * moment.
  41. * Kevin Buhr : Fixed the dumb errors in the above.
  42. * Andi Kleen : Some small cleanups, optimizations,
  43. * and fixed a copy_from_user() bug.
  44. * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
  45. * Tigran Aivazian : Made listen(2) backlog sanity checks
  46. * protocol-independent
  47. *
  48. *
  49. * This program is free software; you can redistribute it and/or
  50. * modify it under the terms of the GNU General Public License
  51. * as published by the Free Software Foundation; either version
  52. * 2 of the License, or (at your option) any later version.
  53. *
  54. *
  55. * This module is effectively the top level interface to the BSD socket
  56. * paradigm.
  57. *
  58. * Based upon Swansea University Computer Society NET3.039
  59. */
  60. #include <linux/mm.h>
  61. #include <linux/socket.h>
  62. #include <linux/file.h>
  63. #include <linux/net.h>
  64. #include <linux/interrupt.h>
  65. #include <linux/thread_info.h>
  66. #include <linux/rcupdate.h>
  67. #include <linux/netdevice.h>
  68. #include <linux/proc_fs.h>
  69. #include <linux/seq_file.h>
  70. #include <linux/mutex.h>
  71. #include <linux/wanrouter.h>
  72. #include <linux/if_bridge.h>
  73. #include <linux/if_frad.h>
  74. #include <linux/if_vlan.h>
  75. #include <linux/init.h>
  76. #include <linux/poll.h>
  77. #include <linux/cache.h>
  78. #include <linux/module.h>
  79. #include <linux/highmem.h>
  80. #include <linux/mount.h>
  81. #include <linux/security.h>
  82. #include <linux/syscalls.h>
  83. #include <linux/compat.h>
  84. #include <linux/kmod.h>
  85. #include <linux/audit.h>
  86. #include <linux/wireless.h>
  87. #include <linux/nsproxy.h>
  88. #include <linux/magic.h>
  89. #include <linux/slab.h>
  90. #include <linux/xattr.h>
  91. #include <asm/uaccess.h>
  92. #include <asm/unistd.h>
  93. #include <net/compat.h>
  94. #include <net/wext.h>
  95. #include <net/cls_cgroup.h>
  96. #include <net/sock.h>
  97. #include <linux/netfilter.h>
  98. #include <linux/if_tun.h>
  99. #include <linux/ipv6_route.h>
  100. #include <linux/route.h>
  101. #include <linux/sockios.h>
  102. #include <linux/atalk.h>
  103. static BLOCKING_NOTIFIER_HEAD(sockev_notifier_list);
  104. static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
  105. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  106. unsigned long nr_segs, loff_t pos);
  107. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  108. unsigned long nr_segs, loff_t pos);
  109. static int sock_mmap(struct file *file, struct vm_area_struct *vma);
  110. static int sock_close(struct inode *inode, struct file *file);
  111. static unsigned int sock_poll(struct file *file,
  112. struct poll_table_struct *wait);
  113. static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  114. #ifdef CONFIG_COMPAT
  115. static long compat_sock_ioctl(struct file *file,
  116. unsigned int cmd, unsigned long arg);
  117. #endif
  118. static int sock_fasync(int fd, struct file *filp, int on);
  119. static ssize_t sock_sendpage(struct file *file, struct page *page,
  120. int offset, size_t size, loff_t *ppos, int more);
  121. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  122. struct pipe_inode_info *pipe, size_t len,
  123. unsigned int flags);
  124. /*
  125. * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
  126. * in the operation structures but are done directly via the socketcall() multiplexor.
  127. */
  128. static const struct file_operations socket_file_ops = {
  129. .owner = THIS_MODULE,
  130. .llseek = no_llseek,
  131. .aio_read = sock_aio_read,
  132. .aio_write = sock_aio_write,
  133. .poll = sock_poll,
  134. .unlocked_ioctl = sock_ioctl,
  135. #ifdef CONFIG_COMPAT
  136. .compat_ioctl = compat_sock_ioctl,
  137. #endif
  138. .mmap = sock_mmap,
  139. .open = sock_no_open, /* special open code to disallow open via /proc */
  140. .release = sock_close,
  141. .fasync = sock_fasync,
  142. .sendpage = sock_sendpage,
  143. .splice_write = generic_splice_sendpage,
  144. .splice_read = sock_splice_read,
  145. };
  146. /*
  147. * The protocol list. Each protocol is registered in here.
  148. */
  149. static DEFINE_SPINLOCK(net_family_lock);
  150. static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
  151. /*
  152. * Statistics counters of the socket lists
  153. */
  154. static DEFINE_PER_CPU(int, sockets_in_use);
  155. /*
  156. * Socket Event framework helpers
  157. */
  158. static void sockev_notify(unsigned long event, struct socket *sk)
  159. {
  160. blocking_notifier_call_chain(&sockev_notifier_list, event, sk);
  161. }
  162. /**
  163. * Support routines.
  164. * Move socket addresses back and forth across the kernel/user
  165. * divide and look after the messy bits.
  166. */
  167. /**
  168. * move_addr_to_kernel - copy a socket address into kernel space
  169. * @uaddr: Address in user space
  170. * @kaddr: Address in kernel space
  171. * @ulen: Length in user space
  172. *
  173. * The address is copied into kernel space. If the provided address is
  174. * too long an error code of -EINVAL is returned. If the copy gives
  175. * invalid addresses -EFAULT is returned. On a success 0 is returned.
  176. */
  177. int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
  178. {
  179. if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
  180. return -EINVAL;
  181. if (ulen == 0)
  182. return 0;
  183. if (copy_from_user(kaddr, uaddr, ulen))
  184. return -EFAULT;
  185. return audit_sockaddr(ulen, kaddr);
  186. }
  187. /**
  188. * move_addr_to_user - copy an address to user space
  189. * @kaddr: kernel space address
  190. * @klen: length of address in kernel
  191. * @uaddr: user space address
  192. * @ulen: pointer to user length field
  193. *
  194. * The value pointed to by ulen on entry is the buffer length available.
  195. * This is overwritten with the buffer space used. -EINVAL is returned
  196. * if an overlong buffer is specified or a negative buffer size. -EFAULT
  197. * is returned if either the buffer or the length field are not
  198. * accessible.
  199. * After copying the data up to the limit the user specifies, the true
  200. * length of the data is written over the length limit the user
  201. * specified. Zero is returned for a success.
  202. */
  203. static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
  204. void __user *uaddr, int __user *ulen)
  205. {
  206. int err;
  207. int len;
  208. BUG_ON(klen > sizeof(struct sockaddr_storage));
  209. err = get_user(len, ulen);
  210. if (err)
  211. return err;
  212. if (len > klen)
  213. len = klen;
  214. if (len < 0)
  215. return -EINVAL;
  216. if (len) {
  217. if (audit_sockaddr(klen, kaddr))
  218. return -ENOMEM;
  219. if (copy_to_user(uaddr, kaddr, len))
  220. return -EFAULT;
  221. }
  222. /*
  223. * "fromlen shall refer to the value before truncation.."
  224. * 1003.1g
  225. */
  226. return __put_user(klen, ulen);
  227. }
  228. static struct kmem_cache *sock_inode_cachep __read_mostly;
  229. static struct inode *sock_alloc_inode(struct super_block *sb)
  230. {
  231. struct socket_alloc *ei;
  232. struct socket_wq *wq;
  233. ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
  234. if (!ei)
  235. return NULL;
  236. wq = kmalloc(sizeof(*wq), GFP_KERNEL);
  237. if (!wq) {
  238. kmem_cache_free(sock_inode_cachep, ei);
  239. return NULL;
  240. }
  241. init_waitqueue_head(&wq->wait);
  242. wq->fasync_list = NULL;
  243. RCU_INIT_POINTER(ei->socket.wq, wq);
  244. ei->socket.state = SS_UNCONNECTED;
  245. ei->socket.flags = 0;
  246. ei->socket.ops = NULL;
  247. ei->socket.sk = NULL;
  248. ei->socket.file = NULL;
  249. return &ei->vfs_inode;
  250. }
  251. static void sock_destroy_inode(struct inode *inode)
  252. {
  253. struct socket_alloc *ei;
  254. struct socket_wq *wq;
  255. ei = container_of(inode, struct socket_alloc, vfs_inode);
  256. wq = rcu_dereference_protected(ei->socket.wq, 1);
  257. kfree_rcu(wq, rcu);
  258. kmem_cache_free(sock_inode_cachep, ei);
  259. }
  260. static void init_once(void *foo)
  261. {
  262. struct socket_alloc *ei = (struct socket_alloc *)foo;
  263. inode_init_once(&ei->vfs_inode);
  264. }
  265. static int init_inodecache(void)
  266. {
  267. sock_inode_cachep = kmem_cache_create("sock_inode_cache",
  268. sizeof(struct socket_alloc),
  269. 0,
  270. (SLAB_HWCACHE_ALIGN |
  271. SLAB_RECLAIM_ACCOUNT |
  272. SLAB_MEM_SPREAD),
  273. init_once);
  274. if (sock_inode_cachep == NULL)
  275. return -ENOMEM;
  276. return 0;
  277. }
  278. static const struct super_operations sockfs_ops = {
  279. .alloc_inode = sock_alloc_inode,
  280. .destroy_inode = sock_destroy_inode,
  281. .statfs = simple_statfs,
  282. };
  283. /*
  284. * sockfs_dname() is called from d_path().
  285. */
  286. static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
  287. {
  288. return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
  289. dentry->d_inode->i_ino);
  290. }
  291. static const struct dentry_operations sockfs_dentry_operations = {
  292. .d_dname = sockfs_dname,
  293. };
  294. static struct dentry *sockfs_mount(struct file_system_type *fs_type,
  295. int flags, const char *dev_name, void *data)
  296. {
  297. return mount_pseudo(fs_type, "socket:", &sockfs_ops,
  298. &sockfs_dentry_operations, SOCKFS_MAGIC);
  299. }
  300. static struct vfsmount *sock_mnt __read_mostly;
  301. static struct file_system_type sock_fs_type = {
  302. .name = "sockfs",
  303. .mount = sockfs_mount,
  304. .kill_sb = kill_anon_super,
  305. };
  306. /*
  307. * Obtains the first available file descriptor and sets it up for use.
  308. *
  309. * These functions create file structures and maps them to fd space
  310. * of the current process. On success it returns file descriptor
  311. * and file struct implicitly stored in sock->file.
  312. * Note that another thread may close file descriptor before we return
  313. * from this function. We use the fact that now we do not refer
  314. * to socket after mapping. If one day we will need it, this
  315. * function will increment ref. count on file by 1.
  316. *
  317. * In any case returned fd MAY BE not valid!
  318. * This race condition is unavoidable
  319. * with shared fd spaces, we cannot solve it inside kernel,
  320. * but we take care of internal coherence yet.
  321. */
  322. static int sock_alloc_file(struct socket *sock, struct file **f, int flags,
  323. const char *dname)
  324. {
  325. struct qstr name = { .name = "" };
  326. struct path path;
  327. struct file *file;
  328. int fd;
  329. fd = get_unused_fd_flags(flags);
  330. if (unlikely(fd < 0))
  331. return fd;
  332. if (dname) {
  333. name.name = dname;
  334. name.len = strlen(name.name);
  335. } else if (sock->sk) {
  336. name.name = sock->sk->sk_prot_creator->name;
  337. name.len = strlen(name.name);
  338. }
  339. path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
  340. if (unlikely(!path.dentry)) {
  341. put_unused_fd(fd);
  342. return -ENOMEM;
  343. }
  344. path.mnt = mntget(sock_mnt);
  345. d_instantiate(path.dentry, SOCK_INODE(sock));
  346. SOCK_INODE(sock)->i_fop = &socket_file_ops;
  347. file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
  348. &socket_file_ops);
  349. if (unlikely(!file)) {
  350. /* drop dentry, keep inode */
  351. ihold(path.dentry->d_inode);
  352. path_put(&path);
  353. put_unused_fd(fd);
  354. return -ENFILE;
  355. }
  356. sock->file = file;
  357. file->f_flags = O_RDWR | (flags & O_NONBLOCK);
  358. file->f_pos = 0;
  359. file->private_data = sock;
  360. *f = file;
  361. return fd;
  362. }
  363. int sock_map_fd(struct socket *sock, int flags)
  364. {
  365. struct file *newfile;
  366. int fd = sock_alloc_file(sock, &newfile, flags, NULL);
  367. if (likely(fd >= 0))
  368. fd_install(fd, newfile);
  369. return fd;
  370. }
  371. EXPORT_SYMBOL(sock_map_fd);
  372. static struct socket *sock_from_file(struct file *file, int *err)
  373. {
  374. if (file->f_op == &socket_file_ops)
  375. return file->private_data; /* set in sock_map_fd */
  376. *err = -ENOTSOCK;
  377. return NULL;
  378. }
  379. /**
  380. * sockfd_lookup - Go from a file number to its socket slot
  381. * @fd: file handle
  382. * @err: pointer to an error code return
  383. *
  384. * The file handle passed in is locked and the socket it is bound
  385. * too is returned. If an error occurs the err pointer is overwritten
  386. * with a negative errno code and NULL is returned. The function checks
  387. * for both invalid handles and passing a handle which is not a socket.
  388. *
  389. * On a success the socket object pointer is returned.
  390. */
  391. struct socket *sockfd_lookup(int fd, int *err)
  392. {
  393. struct file *file;
  394. struct socket *sock;
  395. file = fget(fd);
  396. if (!file) {
  397. *err = -EBADF;
  398. return NULL;
  399. }
  400. sock = sock_from_file(file, err);
  401. if (!sock)
  402. fput(file);
  403. return sock;
  404. }
  405. EXPORT_SYMBOL(sockfd_lookup);
  406. static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
  407. {
  408. struct file *file;
  409. struct socket *sock;
  410. *err = -EBADF;
  411. file = fget_light(fd, fput_needed);
  412. if (file) {
  413. sock = sock_from_file(file, err);
  414. if (sock)
  415. return sock;
  416. fput_light(file, *fput_needed);
  417. }
  418. return NULL;
  419. }
  420. #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
  421. #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
  422. #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
  423. static ssize_t sockfs_getxattr(struct dentry *dentry,
  424. const char *name, void *value, size_t size)
  425. {
  426. const char *proto_name;
  427. size_t proto_size;
  428. int error;
  429. error = -ENODATA;
  430. if (!strncmp(name, XATTR_NAME_SOCKPROTONAME, XATTR_NAME_SOCKPROTONAME_LEN)) {
  431. proto_name = dentry->d_name.name;
  432. proto_size = strlen(proto_name);
  433. if (value) {
  434. error = -ERANGE;
  435. if (proto_size + 1 > size)
  436. goto out;
  437. strncpy(value, proto_name, proto_size + 1);
  438. }
  439. error = proto_size + 1;
  440. }
  441. out:
  442. return error;
  443. }
  444. static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
  445. size_t size)
  446. {
  447. ssize_t len;
  448. ssize_t used = 0;
  449. len = security_inode_listsecurity(dentry->d_inode, buffer, size);
  450. if (len < 0)
  451. return len;
  452. used += len;
  453. if (buffer) {
  454. if (size < used)
  455. return -ERANGE;
  456. buffer += len;
  457. }
  458. len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
  459. used += len;
  460. if (buffer) {
  461. if (size < used)
  462. return -ERANGE;
  463. memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
  464. buffer += len;
  465. }
  466. return used;
  467. }
  468. int sockfs_setattr(struct dentry *dentry, struct iattr *iattr)
  469. {
  470. int err = simple_setattr(dentry, iattr);
  471. if (!err && (iattr->ia_valid & ATTR_UID)) {
  472. struct socket *sock = SOCKET_I(dentry->d_inode);
  473. sock->sk->sk_uid = iattr->ia_uid;
  474. }
  475. return err;
  476. }
  477. static const struct inode_operations sockfs_inode_ops = {
  478. .getxattr = sockfs_getxattr,
  479. .listxattr = sockfs_listxattr,
  480. .setattr = sockfs_setattr,
  481. };
  482. /**
  483. * sock_alloc - allocate a socket
  484. *
  485. * Allocate a new inode and socket object. The two are bound together
  486. * and initialised. The socket is then returned. If we are out of inodes
  487. * NULL is returned.
  488. */
  489. static struct socket *sock_alloc(void)
  490. {
  491. struct inode *inode;
  492. struct socket *sock;
  493. inode = new_inode_pseudo(sock_mnt->mnt_sb);
  494. if (!inode)
  495. return NULL;
  496. sock = SOCKET_I(inode);
  497. kmemcheck_annotate_bitfield(sock, type);
  498. inode->i_ino = get_next_ino();
  499. inode->i_mode = S_IFSOCK | S_IRWXUGO;
  500. inode->i_uid = current_fsuid();
  501. inode->i_gid = current_fsgid();
  502. inode->i_op = &sockfs_inode_ops;
  503. percpu_add(sockets_in_use, 1);
  504. return sock;
  505. }
  506. /*
  507. * In theory you can't get an open on this inode, but /proc provides
  508. * a back door. Remember to keep it shut otherwise you'll let the
  509. * creepy crawlies in.
  510. */
  511. static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
  512. {
  513. return -ENXIO;
  514. }
  515. const struct file_operations bad_sock_fops = {
  516. .owner = THIS_MODULE,
  517. .open = sock_no_open,
  518. .llseek = noop_llseek,
  519. };
  520. /**
  521. * sock_release - close a socket
  522. * @sock: socket to close
  523. *
  524. * The socket is released from the protocol stack if it has a release
  525. * callback, and the inode is then released if the socket is bound to
  526. * an inode not a file.
  527. */
  528. void sock_release(struct socket *sock)
  529. {
  530. if (sock->ops) {
  531. struct module *owner = sock->ops->owner;
  532. sock->ops->release(sock);
  533. sock->ops = NULL;
  534. module_put(owner);
  535. }
  536. if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
  537. printk(KERN_ERR "sock_release: fasync list not empty!\n");
  538. if (test_bit(SOCK_EXTERNALLY_ALLOCATED, &sock->flags))
  539. return;
  540. percpu_sub(sockets_in_use, 1);
  541. if (!sock->file) {
  542. iput(SOCK_INODE(sock));
  543. return;
  544. }
  545. sock->file = NULL;
  546. }
  547. EXPORT_SYMBOL(sock_release);
  548. void sock_tx_timestamp(struct sock *sk, __u8 *tx_flags)
  549. {
  550. *tx_flags = 0;
  551. if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
  552. *tx_flags |= SKBTX_HW_TSTAMP;
  553. if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
  554. *tx_flags |= SKBTX_SW_TSTAMP;
  555. if (sock_flag(sk, SOCK_WIFI_STATUS))
  556. *tx_flags |= SKBTX_WIFI_STATUS;
  557. }
  558. EXPORT_SYMBOL(sock_tx_timestamp);
  559. static inline int __sock_sendmsg_nosec(struct kiocb *iocb, struct socket *sock,
  560. struct msghdr *msg, size_t size)
  561. {
  562. struct sock_iocb *si = kiocb_to_siocb(iocb);
  563. sock_update_classid(sock->sk);
  564. sock_update_netprioidx(sock->sk);
  565. si->sock = sock;
  566. si->scm = NULL;
  567. si->msg = msg;
  568. si->size = size;
  569. return sock->ops->sendmsg(iocb, sock, msg, size);
  570. }
  571. static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
  572. struct msghdr *msg, size_t size)
  573. {
  574. int err = security_socket_sendmsg(sock, msg, size);
  575. return err ?: __sock_sendmsg_nosec(iocb, sock, msg, size);
  576. }
  577. int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  578. {
  579. struct kiocb iocb;
  580. struct sock_iocb siocb;
  581. int ret;
  582. init_sync_kiocb(&iocb, NULL);
  583. iocb.private = &siocb;
  584. ret = __sock_sendmsg(&iocb, sock, msg, size);
  585. if (-EIOCBQUEUED == ret)
  586. ret = wait_on_sync_kiocb(&iocb);
  587. return ret;
  588. }
  589. EXPORT_SYMBOL(sock_sendmsg);
  590. static int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg, size_t size)
  591. {
  592. struct kiocb iocb;
  593. struct sock_iocb siocb;
  594. int ret;
  595. init_sync_kiocb(&iocb, NULL);
  596. iocb.private = &siocb;
  597. ret = __sock_sendmsg_nosec(&iocb, sock, msg, size);
  598. if (-EIOCBQUEUED == ret)
  599. ret = wait_on_sync_kiocb(&iocb);
  600. return ret;
  601. }
  602. int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
  603. struct kvec *vec, size_t num, size_t size)
  604. {
  605. mm_segment_t oldfs = get_fs();
  606. int result;
  607. set_fs(KERNEL_DS);
  608. /*
  609. * the following is safe, since for compiler definitions of kvec and
  610. * iovec are identical, yielding the same in-core layout and alignment
  611. */
  612. msg->msg_iov = (struct iovec *)vec;
  613. msg->msg_iovlen = num;
  614. result = sock_sendmsg(sock, msg, size);
  615. set_fs(oldfs);
  616. return result;
  617. }
  618. EXPORT_SYMBOL(kernel_sendmsg);
  619. static int ktime2ts(ktime_t kt, struct timespec *ts)
  620. {
  621. if (kt.tv64) {
  622. *ts = ktime_to_timespec(kt);
  623. return 1;
  624. } else {
  625. return 0;
  626. }
  627. }
  628. /*
  629. * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
  630. */
  631. void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
  632. struct sk_buff *skb)
  633. {
  634. int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
  635. struct timespec ts[3];
  636. int empty = 1;
  637. struct skb_shared_hwtstamps *shhwtstamps =
  638. skb_hwtstamps(skb);
  639. /* Race occurred between timestamp enabling and packet
  640. receiving. Fill in the current time for now. */
  641. if (need_software_tstamp && skb->tstamp.tv64 == 0)
  642. __net_timestamp(skb);
  643. if (need_software_tstamp) {
  644. if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
  645. struct timeval tv;
  646. skb_get_timestamp(skb, &tv);
  647. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
  648. sizeof(tv), &tv);
  649. } else {
  650. skb_get_timestampns(skb, &ts[0]);
  651. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
  652. sizeof(ts[0]), &ts[0]);
  653. }
  654. }
  655. memset(ts, 0, sizeof(ts));
  656. if (skb->tstamp.tv64 &&
  657. sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
  658. skb_get_timestampns(skb, ts + 0);
  659. empty = 0;
  660. }
  661. if (shhwtstamps) {
  662. if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
  663. ktime2ts(shhwtstamps->syststamp, ts + 1))
  664. empty = 0;
  665. if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
  666. ktime2ts(shhwtstamps->hwtstamp, ts + 2))
  667. empty = 0;
  668. }
  669. if (!empty)
  670. put_cmsg(msg, SOL_SOCKET,
  671. SCM_TIMESTAMPING, sizeof(ts), &ts);
  672. }
  673. EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
  674. void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
  675. struct sk_buff *skb)
  676. {
  677. int ack;
  678. if (!sock_flag(sk, SOCK_WIFI_STATUS))
  679. return;
  680. if (!skb->wifi_acked_valid)
  681. return;
  682. ack = skb->wifi_acked;
  683. put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
  684. }
  685. EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
  686. static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
  687. struct sk_buff *skb)
  688. {
  689. if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
  690. put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
  691. sizeof(__u32), &skb->dropcount);
  692. }
  693. void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
  694. struct sk_buff *skb)
  695. {
  696. sock_recv_timestamp(msg, sk, skb);
  697. sock_recv_drops(msg, sk, skb);
  698. }
  699. EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
  700. static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
  701. struct msghdr *msg, size_t size, int flags)
  702. {
  703. struct sock_iocb *si = kiocb_to_siocb(iocb);
  704. sock_update_classid(sock->sk);
  705. si->sock = sock;
  706. si->scm = NULL;
  707. si->msg = msg;
  708. si->size = size;
  709. si->flags = flags;
  710. return sock->ops->recvmsg(iocb, sock, msg, size, flags);
  711. }
  712. static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
  713. struct msghdr *msg, size_t size, int flags)
  714. {
  715. int err = security_socket_recvmsg(sock, msg, size, flags);
  716. return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
  717. }
  718. int sock_recvmsg(struct socket *sock, struct msghdr *msg,
  719. size_t size, int flags)
  720. {
  721. struct kiocb iocb;
  722. struct sock_iocb siocb;
  723. int ret;
  724. init_sync_kiocb(&iocb, NULL);
  725. iocb.private = &siocb;
  726. ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
  727. if (-EIOCBQUEUED == ret)
  728. ret = wait_on_sync_kiocb(&iocb);
  729. return ret;
  730. }
  731. EXPORT_SYMBOL(sock_recvmsg);
  732. static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
  733. size_t size, int flags)
  734. {
  735. struct kiocb iocb;
  736. struct sock_iocb siocb;
  737. int ret;
  738. init_sync_kiocb(&iocb, NULL);
  739. iocb.private = &siocb;
  740. ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
  741. if (-EIOCBQUEUED == ret)
  742. ret = wait_on_sync_kiocb(&iocb);
  743. return ret;
  744. }
  745. /**
  746. * kernel_recvmsg - Receive a message from a socket (kernel space)
  747. * @sock: The socket to receive the message from
  748. * @msg: Received message
  749. * @vec: Input s/g array for message data
  750. * @num: Size of input s/g array
  751. * @size: Number of bytes to read
  752. * @flags: Message flags (MSG_DONTWAIT, etc...)
  753. *
  754. * On return the msg structure contains the scatter/gather array passed in the
  755. * vec argument. The array is modified so that it consists of the unfilled
  756. * portion of the original array.
  757. *
  758. * The returned value is the total number of bytes received, or an error.
  759. */
  760. int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
  761. struct kvec *vec, size_t num, size_t size, int flags)
  762. {
  763. mm_segment_t oldfs = get_fs();
  764. int result;
  765. set_fs(KERNEL_DS);
  766. /*
  767. * the following is safe, since for compiler definitions of kvec and
  768. * iovec are identical, yielding the same in-core layout and alignment
  769. */
  770. msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
  771. result = sock_recvmsg(sock, msg, size, flags);
  772. set_fs(oldfs);
  773. return result;
  774. }
  775. EXPORT_SYMBOL(kernel_recvmsg);
  776. static void sock_aio_dtor(struct kiocb *iocb)
  777. {
  778. kfree(iocb->private);
  779. }
  780. static ssize_t sock_sendpage(struct file *file, struct page *page,
  781. int offset, size_t size, loff_t *ppos, int more)
  782. {
  783. struct socket *sock;
  784. int flags;
  785. sock = file->private_data;
  786. flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  787. /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
  788. flags |= more;
  789. return kernel_sendpage(sock, page, offset, size, flags);
  790. }
  791. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  792. struct pipe_inode_info *pipe, size_t len,
  793. unsigned int flags)
  794. {
  795. struct socket *sock = file->private_data;
  796. if (unlikely(!sock->ops->splice_read))
  797. return -EINVAL;
  798. sock_update_classid(sock->sk);
  799. return sock->ops->splice_read(sock, ppos, pipe, len, flags);
  800. }
  801. static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
  802. struct sock_iocb *siocb)
  803. {
  804. if (!is_sync_kiocb(iocb)) {
  805. siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
  806. if (!siocb)
  807. return NULL;
  808. iocb->ki_dtor = sock_aio_dtor;
  809. }
  810. siocb->kiocb = iocb;
  811. iocb->private = siocb;
  812. return siocb;
  813. }
  814. static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
  815. struct file *file, const struct iovec *iov,
  816. unsigned long nr_segs)
  817. {
  818. struct socket *sock = file->private_data;
  819. size_t size = 0;
  820. int i;
  821. for (i = 0; i < nr_segs; i++)
  822. size += iov[i].iov_len;
  823. msg->msg_name = NULL;
  824. msg->msg_namelen = 0;
  825. msg->msg_control = NULL;
  826. msg->msg_controllen = 0;
  827. msg->msg_iov = (struct iovec *)iov;
  828. msg->msg_iovlen = nr_segs;
  829. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  830. return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
  831. }
  832. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  833. unsigned long nr_segs, loff_t pos)
  834. {
  835. struct sock_iocb siocb, *x;
  836. if (pos != 0)
  837. return -ESPIPE;
  838. if (iocb->ki_left == 0) /* Match SYS5 behaviour */
  839. return 0;
  840. x = alloc_sock_iocb(iocb, &siocb);
  841. if (!x)
  842. return -ENOMEM;
  843. return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  844. }
  845. static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
  846. struct file *file, const struct iovec *iov,
  847. unsigned long nr_segs)
  848. {
  849. struct socket *sock = file->private_data;
  850. size_t size = 0;
  851. int i;
  852. for (i = 0; i < nr_segs; i++)
  853. size += iov[i].iov_len;
  854. msg->msg_name = NULL;
  855. msg->msg_namelen = 0;
  856. msg->msg_control = NULL;
  857. msg->msg_controllen = 0;
  858. msg->msg_iov = (struct iovec *)iov;
  859. msg->msg_iovlen = nr_segs;
  860. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  861. if (sock->type == SOCK_SEQPACKET)
  862. msg->msg_flags |= MSG_EOR;
  863. return __sock_sendmsg(iocb, sock, msg, size);
  864. }
  865. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  866. unsigned long nr_segs, loff_t pos)
  867. {
  868. struct sock_iocb siocb, *x;
  869. if (pos != 0)
  870. return -ESPIPE;
  871. x = alloc_sock_iocb(iocb, &siocb);
  872. if (!x)
  873. return -ENOMEM;
  874. return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  875. }
  876. /*
  877. * Atomic setting of ioctl hooks to avoid race
  878. * with module unload.
  879. */
  880. static DEFINE_MUTEX(br_ioctl_mutex);
  881. static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
  882. void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
  883. {
  884. mutex_lock(&br_ioctl_mutex);
  885. br_ioctl_hook = hook;
  886. mutex_unlock(&br_ioctl_mutex);
  887. }
  888. EXPORT_SYMBOL(brioctl_set);
  889. static DEFINE_MUTEX(vlan_ioctl_mutex);
  890. static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
  891. void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
  892. {
  893. mutex_lock(&vlan_ioctl_mutex);
  894. vlan_ioctl_hook = hook;
  895. mutex_unlock(&vlan_ioctl_mutex);
  896. }
  897. EXPORT_SYMBOL(vlan_ioctl_set);
  898. static DEFINE_MUTEX(dlci_ioctl_mutex);
  899. static int (*dlci_ioctl_hook) (unsigned int, void __user *);
  900. void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
  901. {
  902. mutex_lock(&dlci_ioctl_mutex);
  903. dlci_ioctl_hook = hook;
  904. mutex_unlock(&dlci_ioctl_mutex);
  905. }
  906. EXPORT_SYMBOL(dlci_ioctl_set);
  907. static long sock_do_ioctl(struct net *net, struct socket *sock,
  908. unsigned int cmd, unsigned long arg)
  909. {
  910. int err;
  911. void __user *argp = (void __user *)arg;
  912. err = sock->ops->ioctl(sock, cmd, arg);
  913. /*
  914. * If this ioctl is unknown try to hand it down
  915. * to the NIC driver.
  916. */
  917. if (err == -ENOIOCTLCMD)
  918. err = dev_ioctl(net, cmd, argp);
  919. return err;
  920. }
  921. /*
  922. * With an ioctl, arg may well be a user mode pointer, but we don't know
  923. * what to do with it - that's up to the protocol still.
  924. */
  925. static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  926. {
  927. struct socket *sock;
  928. struct sock *sk;
  929. void __user *argp = (void __user *)arg;
  930. int pid, err;
  931. struct net *net;
  932. sock = file->private_data;
  933. sk = sock->sk;
  934. net = sock_net(sk);
  935. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
  936. err = dev_ioctl(net, cmd, argp);
  937. } else
  938. #ifdef CONFIG_WEXT_CORE
  939. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  940. err = dev_ioctl(net, cmd, argp);
  941. } else
  942. #endif
  943. switch (cmd) {
  944. case FIOSETOWN:
  945. case SIOCSPGRP:
  946. err = -EFAULT;
  947. if (get_user(pid, (int __user *)argp))
  948. break;
  949. err = f_setown(sock->file, pid, 1);
  950. break;
  951. case FIOGETOWN:
  952. case SIOCGPGRP:
  953. err = put_user(f_getown(sock->file),
  954. (int __user *)argp);
  955. break;
  956. case SIOCGIFBR:
  957. case SIOCSIFBR:
  958. case SIOCBRADDBR:
  959. case SIOCBRDELBR:
  960. err = -ENOPKG;
  961. if (!br_ioctl_hook)
  962. request_module("bridge");
  963. mutex_lock(&br_ioctl_mutex);
  964. if (br_ioctl_hook)
  965. err = br_ioctl_hook(net, cmd, argp);
  966. mutex_unlock(&br_ioctl_mutex);
  967. break;
  968. case SIOCGIFVLAN:
  969. case SIOCSIFVLAN:
  970. err = -ENOPKG;
  971. if (!vlan_ioctl_hook)
  972. request_module("8021q");
  973. mutex_lock(&vlan_ioctl_mutex);
  974. if (vlan_ioctl_hook)
  975. err = vlan_ioctl_hook(net, argp);
  976. mutex_unlock(&vlan_ioctl_mutex);
  977. break;
  978. case SIOCADDDLCI:
  979. case SIOCDELDLCI:
  980. err = -ENOPKG;
  981. if (!dlci_ioctl_hook)
  982. request_module("dlci");
  983. mutex_lock(&dlci_ioctl_mutex);
  984. if (dlci_ioctl_hook)
  985. err = dlci_ioctl_hook(cmd, argp);
  986. mutex_unlock(&dlci_ioctl_mutex);
  987. break;
  988. default:
  989. err = sock_do_ioctl(net, sock, cmd, arg);
  990. break;
  991. }
  992. return err;
  993. }
  994. int sock_create_lite(int family, int type, int protocol, struct socket **res)
  995. {
  996. int err;
  997. struct socket *sock = NULL;
  998. err = security_socket_create(family, type, protocol, 1);
  999. if (err)
  1000. goto out;
  1001. sock = sock_alloc();
  1002. if (!sock) {
  1003. err = -ENOMEM;
  1004. goto out;
  1005. }
  1006. sock->type = type;
  1007. err = security_socket_post_create(sock, family, type, protocol, 1);
  1008. if (err)
  1009. goto out_release;
  1010. out:
  1011. *res = sock;
  1012. return err;
  1013. out_release:
  1014. sock_release(sock);
  1015. sock = NULL;
  1016. goto out;
  1017. }
  1018. EXPORT_SYMBOL(sock_create_lite);
  1019. /* No kernel lock held - perfect */
  1020. static unsigned int sock_poll(struct file *file, poll_table *wait)
  1021. {
  1022. struct socket *sock;
  1023. /*
  1024. * We can't return errors to poll, so it's either yes or no.
  1025. */
  1026. sock = file->private_data;
  1027. return sock->ops->poll(file, sock, wait);
  1028. }
  1029. static int sock_mmap(struct file *file, struct vm_area_struct *vma)
  1030. {
  1031. struct socket *sock = file->private_data;
  1032. return sock->ops->mmap(file, sock, vma);
  1033. }
  1034. static int sock_close(struct inode *inode, struct file *filp)
  1035. {
  1036. /*
  1037. * It was possible the inode is NULL we were
  1038. * closing an unfinished socket.
  1039. */
  1040. if (!inode) {
  1041. printk(KERN_DEBUG "sock_close: NULL inode\n");
  1042. return 0;
  1043. }
  1044. sock_release(SOCKET_I(inode));
  1045. return 0;
  1046. }
  1047. /*
  1048. * Update the socket async list
  1049. *
  1050. * Fasync_list locking strategy.
  1051. *
  1052. * 1. fasync_list is modified only under process context socket lock
  1053. * i.e. under semaphore.
  1054. * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
  1055. * or under socket lock
  1056. */
  1057. static int sock_fasync(int fd, struct file *filp, int on)
  1058. {
  1059. struct socket *sock = filp->private_data;
  1060. struct sock *sk = sock->sk;
  1061. struct socket_wq *wq;
  1062. if (sk == NULL)
  1063. return -EINVAL;
  1064. lock_sock(sk);
  1065. wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
  1066. fasync_helper(fd, filp, on, &wq->fasync_list);
  1067. if (!wq->fasync_list)
  1068. sock_reset_flag(sk, SOCK_FASYNC);
  1069. else
  1070. sock_set_flag(sk, SOCK_FASYNC);
  1071. release_sock(sk);
  1072. return 0;
  1073. }
  1074. /* This function may be called only under socket lock or callback_lock or rcu_lock */
  1075. int sock_wake_async(struct socket *sock, int how, int band)
  1076. {
  1077. struct socket_wq *wq;
  1078. if (!sock)
  1079. return -1;
  1080. rcu_read_lock();
  1081. wq = rcu_dereference(sock->wq);
  1082. if (!wq || !wq->fasync_list) {
  1083. rcu_read_unlock();
  1084. return -1;
  1085. }
  1086. switch (how) {
  1087. case SOCK_WAKE_WAITD:
  1088. if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
  1089. break;
  1090. goto call_kill;
  1091. case SOCK_WAKE_SPACE:
  1092. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
  1093. break;
  1094. /* fall through */
  1095. case SOCK_WAKE_IO:
  1096. call_kill:
  1097. kill_fasync(&wq->fasync_list, SIGIO, band);
  1098. break;
  1099. case SOCK_WAKE_URG:
  1100. kill_fasync(&wq->fasync_list, SIGURG, band);
  1101. }
  1102. rcu_read_unlock();
  1103. return 0;
  1104. }
  1105. EXPORT_SYMBOL(sock_wake_async);
  1106. int __sock_create(struct net *net, int family, int type, int protocol,
  1107. struct socket **res, int kern)
  1108. {
  1109. int err;
  1110. struct socket *sock;
  1111. const struct net_proto_family *pf;
  1112. /*
  1113. * Check protocol is in range
  1114. */
  1115. if (family < 0 || family >= NPROTO)
  1116. return -EAFNOSUPPORT;
  1117. if (type < 0 || type >= SOCK_MAX)
  1118. return -EINVAL;
  1119. /* Compatibility.
  1120. This uglymoron is moved from INET layer to here to avoid
  1121. deadlock in module load.
  1122. */
  1123. if (family == PF_INET && type == SOCK_PACKET) {
  1124. static int warned;
  1125. if (!warned) {
  1126. warned = 1;
  1127. printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
  1128. current->comm);
  1129. }
  1130. family = PF_PACKET;
  1131. }
  1132. err = security_socket_create(family, type, protocol, kern);
  1133. if (err)
  1134. return err;
  1135. /*
  1136. * Allocate the socket and allow the family to set things up. if
  1137. * the protocol is 0, the family is instructed to select an appropriate
  1138. * default.
  1139. */
  1140. sock = sock_alloc();
  1141. if (!sock) {
  1142. net_warn_ratelimited("socket: no more sockets\n");
  1143. return -ENFILE; /* Not exactly a match, but its the
  1144. closest posix thing */
  1145. }
  1146. sock->type = type;
  1147. #ifdef CONFIG_MODULES
  1148. /* Attempt to load a protocol module if the find failed.
  1149. *
  1150. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  1151. * requested real, full-featured networking support upon configuration.
  1152. * Otherwise module support will break!
  1153. */
  1154. if (rcu_access_pointer(net_families[family]) == NULL)
  1155. request_module("net-pf-%d", family);
  1156. #endif
  1157. rcu_read_lock();
  1158. pf = rcu_dereference(net_families[family]);
  1159. err = -EAFNOSUPPORT;
  1160. if (!pf)
  1161. goto out_release;
  1162. /*
  1163. * We will call the ->create function, that possibly is in a loadable
  1164. * module, so we have to bump that loadable module refcnt first.
  1165. */
  1166. if (!try_module_get(pf->owner))
  1167. goto out_release;
  1168. /* Now protected by module ref count */
  1169. rcu_read_unlock();
  1170. err = pf->create(net, sock, protocol, kern);
  1171. if (err < 0)
  1172. goto out_module_put;
  1173. /*
  1174. * Now to bump the refcnt of the [loadable] module that owns this
  1175. * socket at sock_release time we decrement its refcnt.
  1176. */
  1177. if (!try_module_get(sock->ops->owner))
  1178. goto out_module_busy;
  1179. /*
  1180. * Now that we're done with the ->create function, the [loadable]
  1181. * module can have its refcnt decremented
  1182. */
  1183. module_put(pf->owner);
  1184. err = security_socket_post_create(sock, family, type, protocol, kern);
  1185. if (err)
  1186. goto out_sock_release;
  1187. *res = sock;
  1188. return 0;
  1189. out_module_busy:
  1190. err = -EAFNOSUPPORT;
  1191. out_module_put:
  1192. sock->ops = NULL;
  1193. module_put(pf->owner);
  1194. out_sock_release:
  1195. sock_release(sock);
  1196. return err;
  1197. out_release:
  1198. rcu_read_unlock();
  1199. goto out_sock_release;
  1200. }
  1201. EXPORT_SYMBOL(__sock_create);
  1202. int sock_create(int family, int type, int protocol, struct socket **res)
  1203. {
  1204. return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
  1205. }
  1206. EXPORT_SYMBOL(sock_create);
  1207. int sock_create_kern(int family, int type, int protocol, struct socket **res)
  1208. {
  1209. return __sock_create(&init_net, family, type, protocol, res, 1);
  1210. }
  1211. EXPORT_SYMBOL(sock_create_kern);
  1212. SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
  1213. {
  1214. int retval;
  1215. struct socket *sock;
  1216. int flags;
  1217. /* Check the SOCK_* constants for consistency. */
  1218. BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
  1219. BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
  1220. BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
  1221. BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
  1222. flags = type & ~SOCK_TYPE_MASK;
  1223. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1224. return -EINVAL;
  1225. type &= SOCK_TYPE_MASK;
  1226. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1227. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1228. retval = sock_create(family, type, protocol, &sock);
  1229. if (retval < 0)
  1230. goto out;
  1231. if (retval == 0)
  1232. sockev_notify(SOCKEV_SOCKET, sock);
  1233. retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
  1234. if (retval < 0)
  1235. goto out_release;
  1236. out:
  1237. /* It may be already another descriptor 8) Not kernel problem. */
  1238. return retval;
  1239. out_release:
  1240. sock_release(sock);
  1241. return retval;
  1242. }
  1243. /*
  1244. * Create a pair of connected sockets.
  1245. */
  1246. SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
  1247. int __user *, usockvec)
  1248. {
  1249. struct socket *sock1, *sock2;
  1250. int fd1, fd2, err;
  1251. struct file *newfile1, *newfile2;
  1252. int flags;
  1253. flags = type & ~SOCK_TYPE_MASK;
  1254. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1255. return -EINVAL;
  1256. type &= SOCK_TYPE_MASK;
  1257. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1258. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1259. /*
  1260. * Obtain the first socket and check if the underlying protocol
  1261. * supports the socketpair call.
  1262. */
  1263. err = sock_create(family, type, protocol, &sock1);
  1264. if (err < 0)
  1265. goto out;
  1266. err = sock_create(family, type, protocol, &sock2);
  1267. if (err < 0)
  1268. goto out_release_1;
  1269. err = sock1->ops->socketpair(sock1, sock2);
  1270. if (err < 0)
  1271. goto out_release_both;
  1272. fd1 = sock_alloc_file(sock1, &newfile1, flags, NULL);
  1273. if (unlikely(fd1 < 0)) {
  1274. err = fd1;
  1275. goto out_release_both;
  1276. }
  1277. fd2 = sock_alloc_file(sock2, &newfile2, flags, NULL);
  1278. if (unlikely(fd2 < 0)) {
  1279. err = fd2;
  1280. fput(newfile1);
  1281. put_unused_fd(fd1);
  1282. sock_release(sock2);
  1283. goto out;
  1284. }
  1285. audit_fd_pair(fd1, fd2);
  1286. fd_install(fd1, newfile1);
  1287. fd_install(fd2, newfile2);
  1288. /* fd1 and fd2 may be already another descriptors.
  1289. * Not kernel problem.
  1290. */
  1291. err = put_user(fd1, &usockvec[0]);
  1292. if (!err)
  1293. err = put_user(fd2, &usockvec[1]);
  1294. if (!err)
  1295. return 0;
  1296. sys_close(fd2);
  1297. sys_close(fd1);
  1298. return err;
  1299. out_release_both:
  1300. sock_release(sock2);
  1301. out_release_1:
  1302. sock_release(sock1);
  1303. out:
  1304. return err;
  1305. }
  1306. /*
  1307. * Bind a name to a socket. Nothing much to do here since it's
  1308. * the protocol's responsibility to handle the local address.
  1309. *
  1310. * We move the socket address to kernel space before we call
  1311. * the protocol layer (having also checked the address is ok).
  1312. */
  1313. SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
  1314. {
  1315. struct socket *sock;
  1316. struct sockaddr_storage address;
  1317. int err, fput_needed;
  1318. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1319. if (sock) {
  1320. err = move_addr_to_kernel(umyaddr, addrlen, &address);
  1321. if (err >= 0) {
  1322. err = security_socket_bind(sock,
  1323. (struct sockaddr *)&address,
  1324. addrlen);
  1325. if (!err)
  1326. err = sock->ops->bind(sock,
  1327. (struct sockaddr *)
  1328. &address, addrlen);
  1329. }
  1330. if (!err)
  1331. sockev_notify(SOCKEV_BIND, sock);
  1332. fput_light(sock->file, fput_needed);
  1333. }
  1334. return err;
  1335. }
  1336. /*
  1337. * Perform a listen. Basically, we allow the protocol to do anything
  1338. * necessary for a listen, and if that works, we mark the socket as
  1339. * ready for listening.
  1340. */
  1341. SYSCALL_DEFINE2(listen, int, fd, int, backlog)
  1342. {
  1343. struct socket *sock;
  1344. int err, fput_needed;
  1345. int somaxconn;
  1346. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1347. if (sock) {
  1348. somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
  1349. if ((unsigned int)backlog > somaxconn)
  1350. backlog = somaxconn;
  1351. err = security_socket_listen(sock, backlog);
  1352. if (!err)
  1353. err = sock->ops->listen(sock, backlog);
  1354. if (!err)
  1355. sockev_notify(SOCKEV_LISTEN, sock);
  1356. fput_light(sock->file, fput_needed);
  1357. }
  1358. return err;
  1359. }
  1360. /*
  1361. * For accept, we attempt to create a new socket, set up the link
  1362. * with the client, wake up the client, then return the new
  1363. * connected fd. We collect the address of the connector in kernel
  1364. * space and move it to user at the very end. This is unclean because
  1365. * we open the socket then return an error.
  1366. *
  1367. * 1003.1g adds the ability to recvmsg() to query connection pending
  1368. * status to recvmsg. We need to add that support in a way thats
  1369. * clean when we restucture accept also.
  1370. */
  1371. SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1372. int __user *, upeer_addrlen, int, flags)
  1373. {
  1374. struct socket *sock, *newsock;
  1375. struct file *newfile;
  1376. int err, len, newfd, fput_needed;
  1377. struct sockaddr_storage address;
  1378. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1379. return -EINVAL;
  1380. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1381. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1382. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1383. if (!sock)
  1384. goto out;
  1385. err = -ENFILE;
  1386. newsock = sock_alloc();
  1387. if (!newsock)
  1388. goto out_put;
  1389. newsock->type = sock->type;
  1390. newsock->ops = sock->ops;
  1391. /*
  1392. * We don't need try_module_get here, as the listening socket (sock)
  1393. * has the protocol module (sock->ops->owner) held.
  1394. */
  1395. __module_get(newsock->ops->owner);
  1396. newfd = sock_alloc_file(newsock, &newfile, flags,
  1397. sock->sk->sk_prot_creator->name);
  1398. if (unlikely(newfd < 0)) {
  1399. err = newfd;
  1400. sock_release(newsock);
  1401. goto out_put;
  1402. }
  1403. err = security_socket_accept(sock, newsock);
  1404. if (err)
  1405. goto out_fd;
  1406. err = sock->ops->accept(sock, newsock, sock->file->f_flags);
  1407. if (err < 0)
  1408. goto out_fd;
  1409. if (upeer_sockaddr) {
  1410. if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
  1411. &len, 2) < 0) {
  1412. err = -ECONNABORTED;
  1413. goto out_fd;
  1414. }
  1415. err = move_addr_to_user(&address,
  1416. len, upeer_sockaddr, upeer_addrlen);
  1417. if (err < 0)
  1418. goto out_fd;
  1419. }
  1420. /* File flags are not inherited via accept() unlike another OSes. */
  1421. fd_install(newfd, newfile);
  1422. err = newfd;
  1423. if (!err)
  1424. sockev_notify(SOCKEV_ACCEPT, sock);
  1425. out_put:
  1426. fput_light(sock->file, fput_needed);
  1427. out:
  1428. return err;
  1429. out_fd:
  1430. fput(newfile);
  1431. put_unused_fd(newfd);
  1432. goto out_put;
  1433. }
  1434. SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1435. int __user *, upeer_addrlen)
  1436. {
  1437. return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
  1438. }
  1439. /*
  1440. * Attempt to connect to a socket with the server address. The address
  1441. * is in user space so we verify it is OK and move it to kernel space.
  1442. *
  1443. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1444. * break bindings
  1445. *
  1446. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1447. * other SEQPACKET protocols that take time to connect() as it doesn't
  1448. * include the -EINPROGRESS status for such sockets.
  1449. */
  1450. SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
  1451. int, addrlen)
  1452. {
  1453. struct socket *sock;
  1454. struct sockaddr_storage address;
  1455. int err, fput_needed;
  1456. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1457. if (!sock)
  1458. goto out;
  1459. err = move_addr_to_kernel(uservaddr, addrlen, &address);
  1460. if (err < 0)
  1461. goto out_put;
  1462. err =
  1463. security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
  1464. if (err)
  1465. goto out_put;
  1466. err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
  1467. sock->file->f_flags);
  1468. if (!err)
  1469. sockev_notify(SOCKEV_CONNECT, sock);
  1470. out_put:
  1471. fput_light(sock->file, fput_needed);
  1472. out:
  1473. return err;
  1474. }
  1475. /*
  1476. * Get the local address ('name') of a socket object. Move the obtained
  1477. * name to user space.
  1478. */
  1479. SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
  1480. int __user *, usockaddr_len)
  1481. {
  1482. struct socket *sock;
  1483. struct sockaddr_storage address;
  1484. int len, err, fput_needed;
  1485. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1486. if (!sock)
  1487. goto out;
  1488. err = security_socket_getsockname(sock);
  1489. if (err)
  1490. goto out_put;
  1491. err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
  1492. if (err)
  1493. goto out_put;
  1494. err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
  1495. out_put:
  1496. fput_light(sock->file, fput_needed);
  1497. out:
  1498. return err;
  1499. }
  1500. /*
  1501. * Get the remote address ('name') of a socket object. Move the obtained
  1502. * name to user space.
  1503. */
  1504. SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
  1505. int __user *, usockaddr_len)
  1506. {
  1507. struct socket *sock;
  1508. struct sockaddr_storage address;
  1509. int len, err, fput_needed;
  1510. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1511. if (sock != NULL) {
  1512. err = security_socket_getpeername(sock);
  1513. if (err) {
  1514. fput_light(sock->file, fput_needed);
  1515. return err;
  1516. }
  1517. err =
  1518. sock->ops->getname(sock, (struct sockaddr *)&address, &len,
  1519. 1);
  1520. if (!err)
  1521. err = move_addr_to_user(&address, len, usockaddr,
  1522. usockaddr_len);
  1523. fput_light(sock->file, fput_needed);
  1524. }
  1525. return err;
  1526. }
  1527. /*
  1528. * Send a datagram to a given address. We move the address into kernel
  1529. * space and check the user space data area is readable before invoking
  1530. * the protocol.
  1531. */
  1532. SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
  1533. unsigned int, flags, struct sockaddr __user *, addr,
  1534. int, addr_len)
  1535. {
  1536. struct socket *sock;
  1537. struct sockaddr_storage address;
  1538. int err;
  1539. struct msghdr msg;
  1540. struct iovec iov;
  1541. int fput_needed;
  1542. if (len > INT_MAX)
  1543. len = INT_MAX;
  1544. if (unlikely(!access_ok(VERIFY_READ, buff, len)))
  1545. return -EFAULT;
  1546. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1547. if (!sock)
  1548. goto out;
  1549. iov.iov_base = buff;
  1550. iov.iov_len = len;
  1551. msg.msg_name = NULL;
  1552. msg.msg_iov = &iov;
  1553. msg.msg_iovlen = 1;
  1554. msg.msg_control = NULL;
  1555. msg.msg_controllen = 0;
  1556. msg.msg_namelen = 0;
  1557. if (addr) {
  1558. err = move_addr_to_kernel(addr, addr_len, &address);
  1559. if (err < 0)
  1560. goto out_put;
  1561. msg.msg_name = (struct sockaddr *)&address;
  1562. msg.msg_namelen = addr_len;
  1563. }
  1564. if (sock->file->f_flags & O_NONBLOCK)
  1565. flags |= MSG_DONTWAIT;
  1566. msg.msg_flags = flags;
  1567. err = sock_sendmsg(sock, &msg, len);
  1568. out_put:
  1569. fput_light(sock->file, fput_needed);
  1570. out:
  1571. return err;
  1572. }
  1573. /*
  1574. * Send a datagram down a socket.
  1575. */
  1576. SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
  1577. unsigned int, flags)
  1578. {
  1579. return sys_sendto(fd, buff, len, flags, NULL, 0);
  1580. }
  1581. /*
  1582. * Receive a frame from the socket and optionally record the address of the
  1583. * sender. We verify the buffers are writable and if needed move the
  1584. * sender address from kernel to user space.
  1585. */
  1586. SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
  1587. unsigned int, flags, struct sockaddr __user *, addr,
  1588. int __user *, addr_len)
  1589. {
  1590. struct socket *sock;
  1591. struct iovec iov;
  1592. struct msghdr msg;
  1593. struct sockaddr_storage address;
  1594. int err, err2;
  1595. int fput_needed;
  1596. if (size > INT_MAX)
  1597. size = INT_MAX;
  1598. if (unlikely(!access_ok(VERIFY_WRITE, ubuf, size)))
  1599. return -EFAULT;
  1600. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1601. if (!sock)
  1602. goto out;
  1603. msg.msg_control = NULL;
  1604. msg.msg_controllen = 0;
  1605. msg.msg_iovlen = 1;
  1606. msg.msg_iov = &iov;
  1607. iov.iov_len = size;
  1608. iov.iov_base = ubuf;
  1609. /* Save some cycles and don't copy the address if not needed */
  1610. msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
  1611. /* We assume all kernel code knows the size of sockaddr_storage */
  1612. msg.msg_namelen = 0;
  1613. if (sock->file->f_flags & O_NONBLOCK)
  1614. flags |= MSG_DONTWAIT;
  1615. err = sock_recvmsg(sock, &msg, size, flags);
  1616. if (err >= 0 && addr != NULL) {
  1617. err2 = move_addr_to_user(&address,
  1618. msg.msg_namelen, addr, addr_len);
  1619. if (err2 < 0)
  1620. err = err2;
  1621. }
  1622. fput_light(sock->file, fput_needed);
  1623. out:
  1624. return err;
  1625. }
  1626. /*
  1627. * Receive a datagram from a socket.
  1628. */
  1629. asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
  1630. unsigned int flags)
  1631. {
  1632. return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1633. }
  1634. /*
  1635. * Set a socket option. Because we don't know the option lengths we have
  1636. * to pass the user mode parameter for the protocols to sort out.
  1637. */
  1638. SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
  1639. char __user *, optval, int, optlen)
  1640. {
  1641. int err, fput_needed;
  1642. struct socket *sock;
  1643. if (optlen < 0)
  1644. return -EINVAL;
  1645. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1646. if (sock != NULL) {
  1647. err = security_socket_setsockopt(sock, level, optname);
  1648. if (err)
  1649. goto out_put;
  1650. if (level == SOL_SOCKET)
  1651. err =
  1652. sock_setsockopt(sock, level, optname, optval,
  1653. optlen);
  1654. else
  1655. err =
  1656. sock->ops->setsockopt(sock, level, optname, optval,
  1657. optlen);
  1658. out_put:
  1659. fput_light(sock->file, fput_needed);
  1660. }
  1661. return err;
  1662. }
  1663. /*
  1664. * Get a socket option. Because we don't know the option lengths we have
  1665. * to pass a user mode parameter for the protocols to sort out.
  1666. */
  1667. SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
  1668. char __user *, optval, int __user *, optlen)
  1669. {
  1670. int err, fput_needed;
  1671. struct socket *sock;
  1672. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1673. if (sock != NULL) {
  1674. err = security_socket_getsockopt(sock, level, optname);
  1675. if (err)
  1676. goto out_put;
  1677. if (level == SOL_SOCKET)
  1678. err =
  1679. sock_getsockopt(sock, level, optname, optval,
  1680. optlen);
  1681. else
  1682. err =
  1683. sock->ops->getsockopt(sock, level, optname, optval,
  1684. optlen);
  1685. out_put:
  1686. fput_light(sock->file, fput_needed);
  1687. }
  1688. return err;
  1689. }
  1690. /*
  1691. * Shutdown a socket.
  1692. */
  1693. SYSCALL_DEFINE2(shutdown, int, fd, int, how)
  1694. {
  1695. int err, fput_needed;
  1696. struct socket *sock;
  1697. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1698. if (sock != NULL) {
  1699. sockev_notify(SOCKEV_SHUTDOWN, sock);
  1700. err = security_socket_shutdown(sock, how);
  1701. if (!err)
  1702. err = sock->ops->shutdown(sock, how);
  1703. fput_light(sock->file, fput_needed);
  1704. }
  1705. return err;
  1706. }
  1707. /* A couple of helpful macros for getting the address of the 32/64 bit
  1708. * fields which are the same type (int / unsigned) on our platforms.
  1709. */
  1710. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  1711. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  1712. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  1713. struct used_address {
  1714. struct sockaddr_storage name;
  1715. unsigned int name_len;
  1716. };
  1717. static int copy_msghdr_from_user(struct msghdr *kmsg,
  1718. struct msghdr __user *umsg)
  1719. {
  1720. if (copy_from_user(kmsg, umsg, sizeof(struct msghdr)))
  1721. return -EFAULT;
  1722. if (kmsg->msg_name == NULL)
  1723. kmsg->msg_namelen = 0;
  1724. if (kmsg->msg_namelen < 0)
  1725. return -EINVAL;
  1726. if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
  1727. kmsg->msg_namelen = sizeof(struct sockaddr_storage);
  1728. return 0;
  1729. }
  1730. static int ___sys_sendmsg(struct socket *sock, struct msghdr __user *msg,
  1731. struct msghdr *msg_sys, unsigned flags,
  1732. struct used_address *used_address)
  1733. {
  1734. struct compat_msghdr __user *msg_compat =
  1735. (struct compat_msghdr __user *)msg;
  1736. struct sockaddr_storage address;
  1737. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1738. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  1739. __attribute__ ((aligned(sizeof(__kernel_size_t))));
  1740. /* 20 is size of ipv6_pktinfo */
  1741. unsigned char *ctl_buf = ctl;
  1742. int err, ctl_len, iov_size, total_len;
  1743. err = -EFAULT;
  1744. if (MSG_CMSG_COMPAT & flags)
  1745. err = get_compat_msghdr(msg_sys, msg_compat);
  1746. else
  1747. err = copy_msghdr_from_user(msg_sys, msg);
  1748. if (err)
  1749. return err;
  1750. /* do not move before msg_sys is valid */
  1751. err = -EMSGSIZE;
  1752. if (msg_sys->msg_iovlen > UIO_MAXIOV)
  1753. goto out;
  1754. /* Check whether to allocate the iovec area */
  1755. err = -ENOMEM;
  1756. iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
  1757. if (msg_sys->msg_iovlen > UIO_FASTIOV) {
  1758. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1759. if (!iov)
  1760. goto out;
  1761. }
  1762. /* This will also move the address data into kernel space */
  1763. if (MSG_CMSG_COMPAT & flags) {
  1764. err = verify_compat_iovec(msg_sys, iov, &address, VERIFY_READ);
  1765. } else
  1766. err = verify_iovec(msg_sys, iov, &address, VERIFY_READ);
  1767. if (err < 0)
  1768. goto out_freeiov;
  1769. total_len = err;
  1770. err = -ENOBUFS;
  1771. if (msg_sys->msg_controllen > INT_MAX)
  1772. goto out_freeiov;
  1773. ctl_len = msg_sys->msg_controllen;
  1774. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  1775. err =
  1776. cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
  1777. sizeof(ctl));
  1778. if (err)
  1779. goto out_freeiov;
  1780. ctl_buf = msg_sys->msg_control;
  1781. ctl_len = msg_sys->msg_controllen;
  1782. } else if (ctl_len) {
  1783. if (ctl_len > sizeof(ctl)) {
  1784. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  1785. if (ctl_buf == NULL)
  1786. goto out_freeiov;
  1787. }
  1788. err = -EFAULT;
  1789. /*
  1790. * Careful! Before this, msg_sys->msg_control contains a user pointer.
  1791. * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
  1792. * checking falls down on this.
  1793. */
  1794. if (copy_from_user(ctl_buf,
  1795. (void __user __force *)msg_sys->msg_control,
  1796. ctl_len))
  1797. goto out_freectl;
  1798. msg_sys->msg_control = ctl_buf;
  1799. }
  1800. msg_sys->msg_flags = flags;
  1801. if (sock->file->f_flags & O_NONBLOCK)
  1802. msg_sys->msg_flags |= MSG_DONTWAIT;
  1803. /*
  1804. * If this is sendmmsg() and current destination address is same as
  1805. * previously succeeded address, omit asking LSM's decision.
  1806. * used_address->name_len is initialized to UINT_MAX so that the first
  1807. * destination address never matches.
  1808. */
  1809. if (used_address && msg_sys->msg_name &&
  1810. used_address->name_len == msg_sys->msg_namelen &&
  1811. !memcmp(&used_address->name, msg_sys->msg_name,
  1812. used_address->name_len)) {
  1813. err = sock_sendmsg_nosec(sock, msg_sys, total_len);
  1814. goto out_freectl;
  1815. }
  1816. err = sock_sendmsg(sock, msg_sys, total_len);
  1817. /*
  1818. * If this is sendmmsg() and sending to current destination address was
  1819. * successful, remember it.
  1820. */
  1821. if (used_address && err >= 0) {
  1822. used_address->name_len = msg_sys->msg_namelen;
  1823. if (msg_sys->msg_name)
  1824. memcpy(&used_address->name, msg_sys->msg_name,
  1825. used_address->name_len);
  1826. }
  1827. out_freectl:
  1828. if (ctl_buf != ctl)
  1829. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  1830. out_freeiov:
  1831. if (iov != iovstack)
  1832. sock_kfree_s(sock->sk, iov, iov_size);
  1833. out:
  1834. return err;
  1835. }
  1836. /*
  1837. * BSD sendmsg interface
  1838. */
  1839. long __sys_sendmsg(int fd, struct msghdr __user *msg, unsigned int flags)
  1840. {
  1841. int fput_needed, err;
  1842. struct msghdr msg_sys;
  1843. struct socket *sock;
  1844. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1845. if (!sock)
  1846. goto out;
  1847. err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
  1848. fput_light(sock->file, fput_needed);
  1849. out:
  1850. return err;
  1851. }
  1852. SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, flags)
  1853. {
  1854. if (flags & MSG_CMSG_COMPAT)
  1855. return -EINVAL;
  1856. return __sys_sendmsg(fd, msg, flags);
  1857. }
  1858. /*
  1859. * Linux sendmmsg interface
  1860. */
  1861. int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  1862. unsigned int flags)
  1863. {
  1864. int fput_needed, err, datagrams;
  1865. struct socket *sock;
  1866. struct mmsghdr __user *entry;
  1867. struct compat_mmsghdr __user *compat_entry;
  1868. struct msghdr msg_sys;
  1869. struct used_address used_address;
  1870. if (vlen > UIO_MAXIOV)
  1871. vlen = UIO_MAXIOV;
  1872. datagrams = 0;
  1873. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1874. if (!sock)
  1875. return err;
  1876. used_address.name_len = UINT_MAX;
  1877. entry = mmsg;
  1878. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  1879. err = 0;
  1880. while (datagrams < vlen) {
  1881. if (MSG_CMSG_COMPAT & flags) {
  1882. err = ___sys_sendmsg(sock, (struct msghdr __user *)compat_entry,
  1883. &msg_sys, flags, &used_address);
  1884. if (err < 0)
  1885. break;
  1886. err = __put_user(err, &compat_entry->msg_len);
  1887. ++compat_entry;
  1888. } else {
  1889. err = ___sys_sendmsg(sock,
  1890. (struct msghdr __user *)entry,
  1891. &msg_sys, flags, &used_address);
  1892. if (err < 0)
  1893. break;
  1894. err = put_user(err, &entry->msg_len);
  1895. ++entry;
  1896. }
  1897. if (err)
  1898. break;
  1899. ++datagrams;
  1900. }
  1901. fput_light(sock->file, fput_needed);
  1902. /* We only return an error if no datagrams were able to be sent */
  1903. if (datagrams != 0)
  1904. return datagrams;
  1905. return err;
  1906. }
  1907. SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
  1908. unsigned int, vlen, unsigned int, flags)
  1909. {
  1910. if (flags & MSG_CMSG_COMPAT)
  1911. return -EINVAL;
  1912. return __sys_sendmmsg(fd, mmsg, vlen, flags);
  1913. }
  1914. static int ___sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
  1915. struct msghdr *msg_sys, unsigned int flags, int nosec)
  1916. {
  1917. struct compat_msghdr __user *msg_compat =
  1918. (struct compat_msghdr __user *)msg;
  1919. struct iovec iovstack[UIO_FASTIOV];
  1920. struct iovec *iov = iovstack;
  1921. unsigned long cmsg_ptr;
  1922. int err, iov_size, total_len, len;
  1923. /* kernel mode address */
  1924. struct sockaddr_storage addr;
  1925. /* user mode address pointers */
  1926. struct sockaddr __user *uaddr;
  1927. int __user *uaddr_len;
  1928. if (MSG_CMSG_COMPAT & flags)
  1929. err = get_compat_msghdr(msg_sys, msg_compat);
  1930. else
  1931. err = copy_msghdr_from_user(msg_sys, msg);
  1932. if (err)
  1933. return err;
  1934. err = -EMSGSIZE;
  1935. if (msg_sys->msg_iovlen > UIO_MAXIOV)
  1936. goto out;
  1937. /* Check whether to allocate the iovec area */
  1938. err = -ENOMEM;
  1939. iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
  1940. if (msg_sys->msg_iovlen > UIO_FASTIOV) {
  1941. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1942. if (!iov)
  1943. goto out;
  1944. }
  1945. /* Save the user-mode address (verify_iovec will change the
  1946. * kernel msghdr to use the kernel address space)
  1947. */
  1948. uaddr = (__force void __user *)msg_sys->msg_name;
  1949. uaddr_len = COMPAT_NAMELEN(msg);
  1950. if (MSG_CMSG_COMPAT & flags)
  1951. err = verify_compat_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
  1952. else
  1953. err = verify_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
  1954. if (err < 0)
  1955. goto out_freeiov;
  1956. total_len = err;
  1957. cmsg_ptr = (unsigned long)msg_sys->msg_control;
  1958. msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
  1959. /* We assume all kernel code knows the size of sockaddr_storage */
  1960. msg_sys->msg_namelen = 0;
  1961. if (sock->file->f_flags & O_NONBLOCK)
  1962. flags |= MSG_DONTWAIT;
  1963. err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
  1964. total_len, flags);
  1965. if (err < 0)
  1966. goto out_freeiov;
  1967. len = err;
  1968. if (uaddr != NULL) {
  1969. err = move_addr_to_user(&addr,
  1970. msg_sys->msg_namelen, uaddr,
  1971. uaddr_len);
  1972. if (err < 0)
  1973. goto out_freeiov;
  1974. }
  1975. err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
  1976. COMPAT_FLAGS(msg));
  1977. if (err)
  1978. goto out_freeiov;
  1979. if (MSG_CMSG_COMPAT & flags)
  1980. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1981. &msg_compat->msg_controllen);
  1982. else
  1983. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1984. &msg->msg_controllen);
  1985. if (err)
  1986. goto out_freeiov;
  1987. err = len;
  1988. out_freeiov:
  1989. if (iov != iovstack)
  1990. sock_kfree_s(sock->sk, iov, iov_size);
  1991. out:
  1992. return err;
  1993. }
  1994. /*
  1995. * BSD recvmsg interface
  1996. */
  1997. long __sys_recvmsg(int fd, struct msghdr __user *msg, unsigned flags)
  1998. {
  1999. int fput_needed, err;
  2000. struct msghdr msg_sys;
  2001. struct socket *sock;
  2002. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  2003. if (!sock)
  2004. goto out;
  2005. err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
  2006. fput_light(sock->file, fput_needed);
  2007. out:
  2008. return err;
  2009. }
  2010. SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
  2011. unsigned int, flags)
  2012. {
  2013. if (flags & MSG_CMSG_COMPAT)
  2014. return -EINVAL;
  2015. return __sys_recvmsg(fd, msg, flags);
  2016. }
  2017. /*
  2018. * Linux recvmmsg interface
  2019. */
  2020. int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  2021. unsigned int flags, struct timespec *timeout)
  2022. {
  2023. int fput_needed, err, datagrams;
  2024. struct socket *sock;
  2025. struct mmsghdr __user *entry;
  2026. struct compat_mmsghdr __user *compat_entry;
  2027. struct msghdr msg_sys;
  2028. struct timespec end_time;
  2029. if (timeout &&
  2030. poll_select_set_timeout(&end_time, timeout->tv_sec,
  2031. timeout->tv_nsec))
  2032. return -EINVAL;
  2033. datagrams = 0;
  2034. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  2035. if (!sock)
  2036. return err;
  2037. err = sock_error(sock->sk);
  2038. if (err) {
  2039. datagrams = err;
  2040. goto out_put;
  2041. }
  2042. entry = mmsg;
  2043. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  2044. while (datagrams < vlen) {
  2045. /*
  2046. * No need to ask LSM for more than the first datagram.
  2047. */
  2048. if (MSG_CMSG_COMPAT & flags) {
  2049. err = ___sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
  2050. &msg_sys, flags & ~MSG_WAITFORONE,
  2051. datagrams);
  2052. if (err < 0)
  2053. break;
  2054. err = __put_user(err, &compat_entry->msg_len);
  2055. ++compat_entry;
  2056. } else {
  2057. err = ___sys_recvmsg(sock,
  2058. (struct msghdr __user *)entry,
  2059. &msg_sys, flags & ~MSG_WAITFORONE,
  2060. datagrams);
  2061. if (err < 0)
  2062. break;
  2063. err = put_user(err, &entry->msg_len);
  2064. ++entry;
  2065. }
  2066. if (err)
  2067. break;
  2068. ++datagrams;
  2069. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  2070. if (flags & MSG_WAITFORONE)
  2071. flags |= MSG_DONTWAIT;
  2072. if (timeout) {
  2073. ktime_get_ts(timeout);
  2074. *timeout = timespec_sub(end_time, *timeout);
  2075. if (timeout->tv_sec < 0) {
  2076. timeout->tv_sec = timeout->tv_nsec = 0;
  2077. break;
  2078. }
  2079. /* Timeout, return less than vlen datagrams */
  2080. if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
  2081. break;
  2082. }
  2083. /* Out of band data, return right away */
  2084. if (msg_sys.msg_flags & MSG_OOB)
  2085. break;
  2086. }
  2087. if (err == 0)
  2088. goto out_put;
  2089. if (datagrams == 0) {
  2090. datagrams = err;
  2091. goto out_put;
  2092. }
  2093. /*
  2094. * We may return less entries than requested (vlen) if the
  2095. * sock is non block and there aren't enough datagrams...
  2096. */
  2097. if (err != -EAGAIN) {
  2098. /*
  2099. * ... or if recvmsg returns an error after we
  2100. * received some datagrams, where we record the
  2101. * error to return on the next call or if the
  2102. * app asks about it using getsockopt(SO_ERROR).
  2103. */
  2104. sock->sk->sk_err = -err;
  2105. }
  2106. if (err == 0)
  2107. goto out_put;
  2108. if (datagrams == 0) {
  2109. datagrams = err;
  2110. goto out_put;
  2111. }
  2112. /*
  2113. * We may return less entries than requested (vlen) if the
  2114. * sock is non block and there aren't enough datagrams...
  2115. */
  2116. if (err != -EAGAIN) {
  2117. /*
  2118. * ... or if recvmsg returns an error after we
  2119. * received some datagrams, where we record the
  2120. * error to return on the next call or if the
  2121. * app asks about it using getsockopt(SO_ERROR).
  2122. */
  2123. sock->sk->sk_err = -err;
  2124. }
  2125. out_put:
  2126. fput_light(sock->file, fput_needed);
  2127. return datagrams;
  2128. }
  2129. SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
  2130. unsigned int, vlen, unsigned int, flags,
  2131. struct timespec __user *, timeout)
  2132. {
  2133. int datagrams;
  2134. struct timespec timeout_sys;
  2135. if (flags & MSG_CMSG_COMPAT)
  2136. return -EINVAL;
  2137. if (!timeout)
  2138. return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
  2139. if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
  2140. return -EFAULT;
  2141. datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
  2142. if (datagrams > 0 &&
  2143. copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
  2144. datagrams = -EFAULT;
  2145. return datagrams;
  2146. }
  2147. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  2148. /* Argument list sizes for sys_socketcall */
  2149. #define AL(x) ((x) * sizeof(unsigned long))
  2150. static const unsigned char nargs[21] = {
  2151. AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
  2152. AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
  2153. AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
  2154. AL(4), AL(5), AL(4)
  2155. };
  2156. #undef AL
  2157. /*
  2158. * System call vectors.
  2159. *
  2160. * Argument checking cleaned up. Saved 20% in size.
  2161. * This function doesn't need to set the kernel lock because
  2162. * it is set by the callees.
  2163. */
  2164. SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
  2165. {
  2166. unsigned long a[6];
  2167. unsigned long a0, a1;
  2168. int err;
  2169. unsigned int len;
  2170. if (call < 1 || call > SYS_SENDMMSG)
  2171. return -EINVAL;
  2172. len = nargs[call];
  2173. if (len > sizeof(a))
  2174. return -EINVAL;
  2175. /* copy_from_user should be SMP safe. */
  2176. if (copy_from_user(a, args, len))
  2177. return -EFAULT;
  2178. audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  2179. a0 = a[0];
  2180. a1 = a[1];
  2181. switch (call) {
  2182. case SYS_SOCKET:
  2183. err = sys_socket(a0, a1, a[2]);
  2184. break;
  2185. case SYS_BIND:
  2186. err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  2187. break;
  2188. case SYS_CONNECT:
  2189. err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  2190. break;
  2191. case SYS_LISTEN:
  2192. err = sys_listen(a0, a1);
  2193. break;
  2194. case SYS_ACCEPT:
  2195. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  2196. (int __user *)a[2], 0);
  2197. break;
  2198. case SYS_GETSOCKNAME:
  2199. err =
  2200. sys_getsockname(a0, (struct sockaddr __user *)a1,
  2201. (int __user *)a[2]);
  2202. break;
  2203. case SYS_GETPEERNAME:
  2204. err =
  2205. sys_getpeername(a0, (struct sockaddr __user *)a1,
  2206. (int __user *)a[2]);
  2207. break;
  2208. case SYS_SOCKETPAIR:
  2209. err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  2210. break;
  2211. case SYS_SEND:
  2212. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  2213. break;
  2214. case SYS_SENDTO:
  2215. err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
  2216. (struct sockaddr __user *)a[4], a[5]);
  2217. break;
  2218. case SYS_RECV:
  2219. err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
  2220. break;
  2221. case SYS_RECVFROM:
  2222. err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  2223. (struct sockaddr __user *)a[4],
  2224. (int __user *)a[5]);
  2225. break;
  2226. case SYS_SHUTDOWN:
  2227. err = sys_shutdown(a0, a1);
  2228. break;
  2229. case SYS_SETSOCKOPT:
  2230. err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
  2231. break;
  2232. case SYS_GETSOCKOPT:
  2233. err =
  2234. sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  2235. (int __user *)a[4]);
  2236. break;
  2237. case SYS_SENDMSG:
  2238. err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
  2239. break;
  2240. case SYS_SENDMMSG:
  2241. err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
  2242. break;
  2243. case SYS_RECVMSG:
  2244. err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
  2245. break;
  2246. case SYS_RECVMMSG:
  2247. err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
  2248. (struct timespec __user *)a[4]);
  2249. break;
  2250. case SYS_ACCEPT4:
  2251. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  2252. (int __user *)a[2], a[3]);
  2253. break;
  2254. default:
  2255. err = -EINVAL;
  2256. break;
  2257. }
  2258. return err;
  2259. }
  2260. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  2261. /**
  2262. * sock_register - add a socket protocol handler
  2263. * @ops: description of protocol
  2264. *
  2265. * This function is called by a protocol handler that wants to
  2266. * advertise its address family, and have it linked into the
  2267. * socket interface. The value ops->family coresponds to the
  2268. * socket system call protocol family.
  2269. */
  2270. int sock_register(const struct net_proto_family *ops)
  2271. {
  2272. int err;
  2273. if (ops->family >= NPROTO) {
  2274. printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
  2275. NPROTO);
  2276. return -ENOBUFS;
  2277. }
  2278. spin_lock(&net_family_lock);
  2279. if (rcu_dereference_protected(net_families[ops->family],
  2280. lockdep_is_held(&net_family_lock)))
  2281. err = -EEXIST;
  2282. else {
  2283. rcu_assign_pointer(net_families[ops->family], ops);
  2284. err = 0;
  2285. }
  2286. spin_unlock(&net_family_lock);
  2287. printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
  2288. return err;
  2289. }
  2290. EXPORT_SYMBOL(sock_register);
  2291. /**
  2292. * sock_unregister - remove a protocol handler
  2293. * @family: protocol family to remove
  2294. *
  2295. * This function is called by a protocol handler that wants to
  2296. * remove its address family, and have it unlinked from the
  2297. * new socket creation.
  2298. *
  2299. * If protocol handler is a module, then it can use module reference
  2300. * counts to protect against new references. If protocol handler is not
  2301. * a module then it needs to provide its own protection in
  2302. * the ops->create routine.
  2303. */
  2304. void sock_unregister(int family)
  2305. {
  2306. BUG_ON(family < 0 || family >= NPROTO);
  2307. spin_lock(&net_family_lock);
  2308. RCU_INIT_POINTER(net_families[family], NULL);
  2309. spin_unlock(&net_family_lock);
  2310. synchronize_rcu();
  2311. printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
  2312. }
  2313. EXPORT_SYMBOL(sock_unregister);
  2314. static int __init sock_init(void)
  2315. {
  2316. int err;
  2317. /*
  2318. * Initialize the network sysctl infrastructure.
  2319. */
  2320. err = net_sysctl_init();
  2321. if (err)
  2322. goto out;
  2323. /*
  2324. * Initialize sock SLAB cache.
  2325. */
  2326. sk_init();
  2327. /*
  2328. * Initialize skbuff SLAB cache
  2329. */
  2330. skb_init();
  2331. /*
  2332. * Initialize the protocols module.
  2333. */
  2334. init_inodecache();
  2335. err = register_filesystem(&sock_fs_type);
  2336. if (err)
  2337. goto out_fs;
  2338. sock_mnt = kern_mount(&sock_fs_type);
  2339. if (IS_ERR(sock_mnt)) {
  2340. err = PTR_ERR(sock_mnt);
  2341. goto out_mount;
  2342. }
  2343. /* The real protocol initialization is performed in later initcalls.
  2344. */
  2345. #ifdef CONFIG_NETFILTER
  2346. netfilter_init();
  2347. #endif
  2348. #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
  2349. skb_timestamping_init();
  2350. #endif
  2351. out:
  2352. return err;
  2353. out_mount:
  2354. unregister_filesystem(&sock_fs_type);
  2355. out_fs:
  2356. goto out;
  2357. }
  2358. core_initcall(sock_init); /* early initcall */
  2359. #ifdef CONFIG_PROC_FS
  2360. void socket_seq_show(struct seq_file *seq)
  2361. {
  2362. int cpu;
  2363. int counter = 0;
  2364. for_each_possible_cpu(cpu)
  2365. counter += per_cpu(sockets_in_use, cpu);
  2366. /* It can be negative, by the way. 8) */
  2367. if (counter < 0)
  2368. counter = 0;
  2369. seq_printf(seq, "sockets: used %d\n", counter);
  2370. }
  2371. #endif /* CONFIG_PROC_FS */
  2372. #ifdef CONFIG_COMPAT
  2373. static int do_siocgstamp(struct net *net, struct socket *sock,
  2374. unsigned int cmd, void __user *up)
  2375. {
  2376. mm_segment_t old_fs = get_fs();
  2377. struct timeval ktv;
  2378. int err;
  2379. set_fs(KERNEL_DS);
  2380. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
  2381. set_fs(old_fs);
  2382. if (!err)
  2383. err = compat_put_timeval(&ktv, up);
  2384. return err;
  2385. }
  2386. static int do_siocgstampns(struct net *net, struct socket *sock,
  2387. unsigned int cmd, void __user *up)
  2388. {
  2389. mm_segment_t old_fs = get_fs();
  2390. struct timespec kts;
  2391. int err;
  2392. set_fs(KERNEL_DS);
  2393. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
  2394. set_fs(old_fs);
  2395. if (!err)
  2396. err = compat_put_timespec(&kts, up);
  2397. return err;
  2398. }
  2399. static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
  2400. {
  2401. struct ifreq __user *uifr;
  2402. int err;
  2403. uifr = compat_alloc_user_space(sizeof(struct ifreq));
  2404. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2405. return -EFAULT;
  2406. err = dev_ioctl(net, SIOCGIFNAME, uifr);
  2407. if (err)
  2408. return err;
  2409. if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
  2410. return -EFAULT;
  2411. return 0;
  2412. }
  2413. static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
  2414. {
  2415. struct compat_ifconf ifc32;
  2416. struct ifconf ifc;
  2417. struct ifconf __user *uifc;
  2418. struct compat_ifreq __user *ifr32;
  2419. struct ifreq __user *ifr;
  2420. unsigned int i, j;
  2421. int err;
  2422. if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
  2423. return -EFAULT;
  2424. memset(&ifc, 0, sizeof(ifc));
  2425. if (ifc32.ifcbuf == 0) {
  2426. ifc32.ifc_len = 0;
  2427. ifc.ifc_len = 0;
  2428. ifc.ifc_req = NULL;
  2429. uifc = compat_alloc_user_space(sizeof(struct ifconf));
  2430. } else {
  2431. size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
  2432. sizeof(struct ifreq);
  2433. uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
  2434. ifc.ifc_len = len;
  2435. ifr = ifc.ifc_req = (void __user *)(uifc + 1);
  2436. ifr32 = compat_ptr(ifc32.ifcbuf);
  2437. for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
  2438. if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
  2439. return -EFAULT;
  2440. ifr++;
  2441. ifr32++;
  2442. }
  2443. }
  2444. if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
  2445. return -EFAULT;
  2446. err = dev_ioctl(net, SIOCGIFCONF, uifc);
  2447. if (err)
  2448. return err;
  2449. if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
  2450. return -EFAULT;
  2451. ifr = ifc.ifc_req;
  2452. ifr32 = compat_ptr(ifc32.ifcbuf);
  2453. for (i = 0, j = 0;
  2454. i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
  2455. i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
  2456. if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
  2457. return -EFAULT;
  2458. ifr32++;
  2459. ifr++;
  2460. }
  2461. if (ifc32.ifcbuf == 0) {
  2462. /* Translate from 64-bit structure multiple to
  2463. * a 32-bit one.
  2464. */
  2465. i = ifc.ifc_len;
  2466. i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
  2467. ifc32.ifc_len = i;
  2468. } else {
  2469. ifc32.ifc_len = i;
  2470. }
  2471. if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
  2472. return -EFAULT;
  2473. return 0;
  2474. }
  2475. static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
  2476. {
  2477. struct compat_ethtool_rxnfc __user *compat_rxnfc;
  2478. bool convert_in = false, convert_out = false;
  2479. size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
  2480. struct ethtool_rxnfc __user *rxnfc;
  2481. struct ifreq __user *ifr;
  2482. u32 rule_cnt = 0, actual_rule_cnt;
  2483. u32 ethcmd;
  2484. u32 data;
  2485. int ret;
  2486. if (get_user(data, &ifr32->ifr_ifru.ifru_data))
  2487. return -EFAULT;
  2488. compat_rxnfc = compat_ptr(data);
  2489. if (get_user(ethcmd, &compat_rxnfc->cmd))
  2490. return -EFAULT;
  2491. /* Most ethtool structures are defined without padding.
  2492. * Unfortunately struct ethtool_rxnfc is an exception.
  2493. */
  2494. switch (ethcmd) {
  2495. default:
  2496. break;
  2497. case ETHTOOL_GRXCLSRLALL:
  2498. /* Buffer size is variable */
  2499. if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
  2500. return -EFAULT;
  2501. if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
  2502. return -ENOMEM;
  2503. buf_size += rule_cnt * sizeof(u32);
  2504. /* fall through */
  2505. case ETHTOOL_GRXRINGS:
  2506. case ETHTOOL_GRXCLSRLCNT:
  2507. case ETHTOOL_GRXCLSRULE:
  2508. case ETHTOOL_SRXCLSRLINS:
  2509. convert_out = true;
  2510. /* fall through */
  2511. case ETHTOOL_SRXCLSRLDEL:
  2512. buf_size += sizeof(struct ethtool_rxnfc);
  2513. convert_in = true;
  2514. break;
  2515. }
  2516. ifr = compat_alloc_user_space(buf_size);
  2517. rxnfc = (void *)ifr + ALIGN(sizeof(struct ifreq), 8);
  2518. if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
  2519. return -EFAULT;
  2520. if (put_user(convert_in ? rxnfc : compat_ptr(data),
  2521. &ifr->ifr_ifru.ifru_data))
  2522. return -EFAULT;
  2523. if (convert_in) {
  2524. /* We expect there to be holes between fs.m_ext and
  2525. * fs.ring_cookie and at the end of fs, but nowhere else.
  2526. */
  2527. BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
  2528. sizeof(compat_rxnfc->fs.m_ext) !=
  2529. offsetof(struct ethtool_rxnfc, fs.m_ext) +
  2530. sizeof(rxnfc->fs.m_ext));
  2531. BUILD_BUG_ON(
  2532. offsetof(struct compat_ethtool_rxnfc, fs.location) -
  2533. offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
  2534. offsetof(struct ethtool_rxnfc, fs.location) -
  2535. offsetof(struct ethtool_rxnfc, fs.ring_cookie));
  2536. if (copy_in_user(rxnfc, compat_rxnfc,
  2537. (void *)(&rxnfc->fs.m_ext + 1) -
  2538. (void *)rxnfc) ||
  2539. copy_in_user(&rxnfc->fs.ring_cookie,
  2540. &compat_rxnfc->fs.ring_cookie,
  2541. (void *)(&rxnfc->fs.location + 1) -
  2542. (void *)&rxnfc->fs.ring_cookie) ||
  2543. copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
  2544. sizeof(rxnfc->rule_cnt)))
  2545. return -EFAULT;
  2546. }
  2547. ret = dev_ioctl(net, SIOCETHTOOL, ifr);
  2548. if (ret)
  2549. return ret;
  2550. if (convert_out) {
  2551. if (copy_in_user(compat_rxnfc, rxnfc,
  2552. (const void *)(&rxnfc->fs.m_ext + 1) -
  2553. (const void *)rxnfc) ||
  2554. copy_in_user(&compat_rxnfc->fs.ring_cookie,
  2555. &rxnfc->fs.ring_cookie,
  2556. (const void *)(&rxnfc->fs.location + 1) -
  2557. (const void *)&rxnfc->fs.ring_cookie) ||
  2558. copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
  2559. sizeof(rxnfc->rule_cnt)))
  2560. return -EFAULT;
  2561. if (ethcmd == ETHTOOL_GRXCLSRLALL) {
  2562. /* As an optimisation, we only copy the actual
  2563. * number of rules that the underlying
  2564. * function returned. Since Mallory might
  2565. * change the rule count in user memory, we
  2566. * check that it is less than the rule count
  2567. * originally given (as the user buffer size),
  2568. * which has been range-checked.
  2569. */
  2570. if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
  2571. return -EFAULT;
  2572. if (actual_rule_cnt < rule_cnt)
  2573. rule_cnt = actual_rule_cnt;
  2574. if (copy_in_user(&compat_rxnfc->rule_locs[0],
  2575. &rxnfc->rule_locs[0],
  2576. rule_cnt * sizeof(u32)))
  2577. return -EFAULT;
  2578. }
  2579. }
  2580. return 0;
  2581. }
  2582. static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
  2583. {
  2584. void __user *uptr;
  2585. compat_uptr_t uptr32;
  2586. struct ifreq __user *uifr;
  2587. uifr = compat_alloc_user_space(sizeof(*uifr));
  2588. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2589. return -EFAULT;
  2590. if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
  2591. return -EFAULT;
  2592. uptr = compat_ptr(uptr32);
  2593. if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
  2594. return -EFAULT;
  2595. return dev_ioctl(net, SIOCWANDEV, uifr);
  2596. }
  2597. static int bond_ioctl(struct net *net, unsigned int cmd,
  2598. struct compat_ifreq __user *ifr32)
  2599. {
  2600. struct ifreq kifr;
  2601. struct ifreq __user *uifr;
  2602. mm_segment_t old_fs;
  2603. int err;
  2604. u32 data;
  2605. void __user *datap;
  2606. switch (cmd) {
  2607. case SIOCBONDENSLAVE:
  2608. case SIOCBONDRELEASE:
  2609. case SIOCBONDSETHWADDR:
  2610. case SIOCBONDCHANGEACTIVE:
  2611. if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
  2612. return -EFAULT;
  2613. old_fs = get_fs();
  2614. set_fs(KERNEL_DS);
  2615. err = dev_ioctl(net, cmd,
  2616. (struct ifreq __user __force *) &kifr);
  2617. set_fs(old_fs);
  2618. return err;
  2619. case SIOCBONDSLAVEINFOQUERY:
  2620. case SIOCBONDINFOQUERY:
  2621. uifr = compat_alloc_user_space(sizeof(*uifr));
  2622. if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
  2623. return -EFAULT;
  2624. if (get_user(data, &ifr32->ifr_ifru.ifru_data))
  2625. return -EFAULT;
  2626. datap = compat_ptr(data);
  2627. if (put_user(datap, &uifr->ifr_ifru.ifru_data))
  2628. return -EFAULT;
  2629. return dev_ioctl(net, cmd, uifr);
  2630. default:
  2631. return -ENOIOCTLCMD;
  2632. }
  2633. }
  2634. static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
  2635. struct compat_ifreq __user *u_ifreq32)
  2636. {
  2637. struct ifreq __user *u_ifreq64;
  2638. char tmp_buf[IFNAMSIZ];
  2639. void __user *data64;
  2640. u32 data32;
  2641. if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
  2642. IFNAMSIZ))
  2643. return -EFAULT;
  2644. if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
  2645. return -EFAULT;
  2646. data64 = compat_ptr(data32);
  2647. u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
  2648. /* Don't check these user accesses, just let that get trapped
  2649. * in the ioctl handler instead.
  2650. */
  2651. if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
  2652. IFNAMSIZ))
  2653. return -EFAULT;
  2654. if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
  2655. return -EFAULT;
  2656. return dev_ioctl(net, cmd, u_ifreq64);
  2657. }
  2658. static int dev_ifsioc(struct net *net, struct socket *sock,
  2659. unsigned int cmd, struct compat_ifreq __user *uifr32)
  2660. {
  2661. struct ifreq __user *uifr;
  2662. int err;
  2663. uifr = compat_alloc_user_space(sizeof(*uifr));
  2664. if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
  2665. return -EFAULT;
  2666. err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
  2667. if (!err) {
  2668. switch (cmd) {
  2669. case SIOCGIFFLAGS:
  2670. case SIOCGIFMETRIC:
  2671. case SIOCGIFMTU:
  2672. case SIOCGIFMEM:
  2673. case SIOCGIFHWADDR:
  2674. case SIOCGIFINDEX:
  2675. case SIOCGIFADDR:
  2676. case SIOCGIFBRDADDR:
  2677. case SIOCGIFDSTADDR:
  2678. case SIOCGIFNETMASK:
  2679. case SIOCGIFPFLAGS:
  2680. case SIOCGIFTXQLEN:
  2681. case SIOCGMIIPHY:
  2682. case SIOCGMIIREG:
  2683. if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
  2684. err = -EFAULT;
  2685. break;
  2686. }
  2687. }
  2688. return err;
  2689. }
  2690. static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
  2691. struct compat_ifreq __user *uifr32)
  2692. {
  2693. struct ifreq ifr;
  2694. struct compat_ifmap __user *uifmap32;
  2695. mm_segment_t old_fs;
  2696. int err;
  2697. uifmap32 = &uifr32->ifr_ifru.ifru_map;
  2698. err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
  2699. err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
  2700. err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
  2701. err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
  2702. err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
  2703. err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
  2704. err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
  2705. if (err)
  2706. return -EFAULT;
  2707. old_fs = get_fs();
  2708. set_fs(KERNEL_DS);
  2709. err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
  2710. set_fs(old_fs);
  2711. if (cmd == SIOCGIFMAP && !err) {
  2712. err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
  2713. err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
  2714. err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
  2715. err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
  2716. err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
  2717. err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
  2718. err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
  2719. if (err)
  2720. err = -EFAULT;
  2721. }
  2722. return err;
  2723. }
  2724. static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
  2725. {
  2726. void __user *uptr;
  2727. compat_uptr_t uptr32;
  2728. struct ifreq __user *uifr;
  2729. uifr = compat_alloc_user_space(sizeof(*uifr));
  2730. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2731. return -EFAULT;
  2732. if (get_user(uptr32, &uifr32->ifr_data))
  2733. return -EFAULT;
  2734. uptr = compat_ptr(uptr32);
  2735. if (put_user(uptr, &uifr->ifr_data))
  2736. return -EFAULT;
  2737. return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
  2738. }
  2739. struct rtentry32 {
  2740. u32 rt_pad1;
  2741. struct sockaddr rt_dst; /* target address */
  2742. struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
  2743. struct sockaddr rt_genmask; /* target network mask (IP) */
  2744. unsigned short rt_flags;
  2745. short rt_pad2;
  2746. u32 rt_pad3;
  2747. unsigned char rt_tos;
  2748. unsigned char rt_class;
  2749. short rt_pad4;
  2750. short rt_metric; /* +1 for binary compatibility! */
  2751. /* char * */ u32 rt_dev; /* forcing the device at add */
  2752. u32 rt_mtu; /* per route MTU/Window */
  2753. u32 rt_window; /* Window clamping */
  2754. unsigned short rt_irtt; /* Initial RTT */
  2755. };
  2756. struct in6_rtmsg32 {
  2757. struct in6_addr rtmsg_dst;
  2758. struct in6_addr rtmsg_src;
  2759. struct in6_addr rtmsg_gateway;
  2760. u32 rtmsg_type;
  2761. u16 rtmsg_dst_len;
  2762. u16 rtmsg_src_len;
  2763. u32 rtmsg_metric;
  2764. u32 rtmsg_info;
  2765. u32 rtmsg_flags;
  2766. s32 rtmsg_ifindex;
  2767. };
  2768. static int routing_ioctl(struct net *net, struct socket *sock,
  2769. unsigned int cmd, void __user *argp)
  2770. {
  2771. int ret;
  2772. void *r = NULL;
  2773. struct in6_rtmsg r6;
  2774. struct rtentry r4;
  2775. char devname[16];
  2776. u32 rtdev;
  2777. mm_segment_t old_fs = get_fs();
  2778. if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
  2779. struct in6_rtmsg32 __user *ur6 = argp;
  2780. ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
  2781. 3 * sizeof(struct in6_addr));
  2782. ret |= __get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
  2783. ret |= __get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
  2784. ret |= __get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
  2785. ret |= __get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
  2786. ret |= __get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
  2787. ret |= __get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
  2788. ret |= __get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
  2789. r = (void *) &r6;
  2790. } else { /* ipv4 */
  2791. struct rtentry32 __user *ur4 = argp;
  2792. ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
  2793. 3 * sizeof(struct sockaddr));
  2794. ret |= __get_user(r4.rt_flags, &(ur4->rt_flags));
  2795. ret |= __get_user(r4.rt_metric, &(ur4->rt_metric));
  2796. ret |= __get_user(r4.rt_mtu, &(ur4->rt_mtu));
  2797. ret |= __get_user(r4.rt_window, &(ur4->rt_window));
  2798. ret |= __get_user(r4.rt_irtt, &(ur4->rt_irtt));
  2799. ret |= __get_user(rtdev, &(ur4->rt_dev));
  2800. if (rtdev) {
  2801. ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
  2802. r4.rt_dev = (char __user __force *)devname;
  2803. devname[15] = 0;
  2804. } else
  2805. r4.rt_dev = NULL;
  2806. r = (void *) &r4;
  2807. }
  2808. if (ret) {
  2809. ret = -EFAULT;
  2810. goto out;
  2811. }
  2812. set_fs(KERNEL_DS);
  2813. ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
  2814. set_fs(old_fs);
  2815. out:
  2816. return ret;
  2817. }
  2818. /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
  2819. * for some operations; this forces use of the newer bridge-utils that
  2820. * use compatible ioctls
  2821. */
  2822. static int old_bridge_ioctl(compat_ulong_t __user *argp)
  2823. {
  2824. compat_ulong_t tmp;
  2825. if (get_user(tmp, argp))
  2826. return -EFAULT;
  2827. if (tmp == BRCTL_GET_VERSION)
  2828. return BRCTL_VERSION + 1;
  2829. return -EINVAL;
  2830. }
  2831. static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
  2832. unsigned int cmd, unsigned long arg)
  2833. {
  2834. void __user *argp = compat_ptr(arg);
  2835. struct sock *sk = sock->sk;
  2836. struct net *net = sock_net(sk);
  2837. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
  2838. return siocdevprivate_ioctl(net, cmd, argp);
  2839. switch (cmd) {
  2840. case SIOCSIFBR:
  2841. case SIOCGIFBR:
  2842. return old_bridge_ioctl(argp);
  2843. case SIOCGIFNAME:
  2844. return dev_ifname32(net, argp);
  2845. case SIOCGIFCONF:
  2846. return dev_ifconf(net, argp);
  2847. case SIOCETHTOOL:
  2848. return ethtool_ioctl(net, argp);
  2849. case SIOCWANDEV:
  2850. return compat_siocwandev(net, argp);
  2851. case SIOCGIFMAP:
  2852. case SIOCSIFMAP:
  2853. return compat_sioc_ifmap(net, cmd, argp);
  2854. case SIOCBONDENSLAVE:
  2855. case SIOCBONDRELEASE:
  2856. case SIOCBONDSETHWADDR:
  2857. case SIOCBONDSLAVEINFOQUERY:
  2858. case SIOCBONDINFOQUERY:
  2859. case SIOCBONDCHANGEACTIVE:
  2860. return bond_ioctl(net, cmd, argp);
  2861. case SIOCADDRT:
  2862. case SIOCDELRT:
  2863. return routing_ioctl(net, sock, cmd, argp);
  2864. case SIOCGSTAMP:
  2865. return do_siocgstamp(net, sock, cmd, argp);
  2866. case SIOCGSTAMPNS:
  2867. return do_siocgstampns(net, sock, cmd, argp);
  2868. case SIOCSHWTSTAMP:
  2869. return compat_siocshwtstamp(net, argp);
  2870. case FIOSETOWN:
  2871. case SIOCSPGRP:
  2872. case FIOGETOWN:
  2873. case SIOCGPGRP:
  2874. case SIOCBRADDBR:
  2875. case SIOCBRDELBR:
  2876. case SIOCGIFVLAN:
  2877. case SIOCSIFVLAN:
  2878. case SIOCADDDLCI:
  2879. case SIOCDELDLCI:
  2880. return sock_ioctl(file, cmd, arg);
  2881. case SIOCGIFFLAGS:
  2882. case SIOCSIFFLAGS:
  2883. case SIOCGIFMETRIC:
  2884. case SIOCSIFMETRIC:
  2885. case SIOCGIFMTU:
  2886. case SIOCSIFMTU:
  2887. case SIOCGIFMEM:
  2888. case SIOCSIFMEM:
  2889. case SIOCGIFHWADDR:
  2890. case SIOCSIFHWADDR:
  2891. case SIOCADDMULTI:
  2892. case SIOCDELMULTI:
  2893. case SIOCGIFINDEX:
  2894. case SIOCGIFADDR:
  2895. case SIOCSIFADDR:
  2896. case SIOCSIFHWBROADCAST:
  2897. case SIOCDIFADDR:
  2898. case SIOCGIFBRDADDR:
  2899. case SIOCSIFBRDADDR:
  2900. case SIOCGIFDSTADDR:
  2901. case SIOCSIFDSTADDR:
  2902. case SIOCGIFNETMASK:
  2903. case SIOCSIFNETMASK:
  2904. case SIOCSIFPFLAGS:
  2905. case SIOCGIFPFLAGS:
  2906. case SIOCGIFTXQLEN:
  2907. case SIOCSIFTXQLEN:
  2908. case SIOCBRADDIF:
  2909. case SIOCBRDELIF:
  2910. case SIOCSIFNAME:
  2911. case SIOCGMIIPHY:
  2912. case SIOCGMIIREG:
  2913. case SIOCSMIIREG:
  2914. return dev_ifsioc(net, sock, cmd, argp);
  2915. case SIOCSARP:
  2916. case SIOCGARP:
  2917. case SIOCDARP:
  2918. case SIOCATMARK:
  2919. return sock_do_ioctl(net, sock, cmd, arg);
  2920. }
  2921. return -ENOIOCTLCMD;
  2922. }
  2923. static long compat_sock_ioctl(struct file *file, unsigned int cmd,
  2924. unsigned long arg)
  2925. {
  2926. struct socket *sock = file->private_data;
  2927. int ret = -ENOIOCTLCMD;
  2928. struct sock *sk;
  2929. struct net *net;
  2930. sk = sock->sk;
  2931. net = sock_net(sk);
  2932. if (sock->ops->compat_ioctl)
  2933. ret = sock->ops->compat_ioctl(sock, cmd, arg);
  2934. if (ret == -ENOIOCTLCMD &&
  2935. (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
  2936. ret = compat_wext_handle_ioctl(net, cmd, arg);
  2937. if (ret == -ENOIOCTLCMD)
  2938. ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
  2939. return ret;
  2940. }
  2941. #endif
  2942. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
  2943. {
  2944. return sock->ops->bind(sock, addr, addrlen);
  2945. }
  2946. EXPORT_SYMBOL(kernel_bind);
  2947. int kernel_listen(struct socket *sock, int backlog)
  2948. {
  2949. return sock->ops->listen(sock, backlog);
  2950. }
  2951. EXPORT_SYMBOL(kernel_listen);
  2952. int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
  2953. {
  2954. struct sock *sk = sock->sk;
  2955. int err;
  2956. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  2957. newsock);
  2958. if (err < 0)
  2959. goto done;
  2960. err = sock->ops->accept(sock, *newsock, flags);
  2961. if (err < 0) {
  2962. sock_release(*newsock);
  2963. *newsock = NULL;
  2964. goto done;
  2965. }
  2966. (*newsock)->ops = sock->ops;
  2967. __module_get((*newsock)->ops->owner);
  2968. done:
  2969. return err;
  2970. }
  2971. EXPORT_SYMBOL(kernel_accept);
  2972. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  2973. int flags)
  2974. {
  2975. return sock->ops->connect(sock, addr, addrlen, flags);
  2976. }
  2977. EXPORT_SYMBOL(kernel_connect);
  2978. int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
  2979. int *addrlen)
  2980. {
  2981. return sock->ops->getname(sock, addr, addrlen, 0);
  2982. }
  2983. EXPORT_SYMBOL(kernel_getsockname);
  2984. int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
  2985. int *addrlen)
  2986. {
  2987. return sock->ops->getname(sock, addr, addrlen, 1);
  2988. }
  2989. EXPORT_SYMBOL(kernel_getpeername);
  2990. int kernel_getsockopt(struct socket *sock, int level, int optname,
  2991. char *optval, int *optlen)
  2992. {
  2993. mm_segment_t oldfs = get_fs();
  2994. char __user *uoptval;
  2995. int __user *uoptlen;
  2996. int err;
  2997. uoptval = (char __user __force *) optval;
  2998. uoptlen = (int __user __force *) optlen;
  2999. set_fs(KERNEL_DS);
  3000. if (level == SOL_SOCKET)
  3001. err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
  3002. else
  3003. err = sock->ops->getsockopt(sock, level, optname, uoptval,
  3004. uoptlen);
  3005. set_fs(oldfs);
  3006. return err;
  3007. }
  3008. EXPORT_SYMBOL(kernel_getsockopt);
  3009. int kernel_setsockopt(struct socket *sock, int level, int optname,
  3010. char *optval, unsigned int optlen)
  3011. {
  3012. mm_segment_t oldfs = get_fs();
  3013. char __user *uoptval;
  3014. int err;
  3015. uoptval = (char __user __force *) optval;
  3016. set_fs(KERNEL_DS);
  3017. if (level == SOL_SOCKET)
  3018. err = sock_setsockopt(sock, level, optname, uoptval, optlen);
  3019. else
  3020. err = sock->ops->setsockopt(sock, level, optname, uoptval,
  3021. optlen);
  3022. set_fs(oldfs);
  3023. return err;
  3024. }
  3025. EXPORT_SYMBOL(kernel_setsockopt);
  3026. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  3027. size_t size, int flags)
  3028. {
  3029. sock_update_classid(sock->sk);
  3030. if (sock->ops->sendpage)
  3031. return sock->ops->sendpage(sock, page, offset, size, flags);
  3032. return sock_no_sendpage(sock, page, offset, size, flags);
  3033. }
  3034. EXPORT_SYMBOL(kernel_sendpage);
  3035. int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
  3036. {
  3037. mm_segment_t oldfs = get_fs();
  3038. int err;
  3039. set_fs(KERNEL_DS);
  3040. err = sock->ops->ioctl(sock, cmd, arg);
  3041. set_fs(oldfs);
  3042. return err;
  3043. }
  3044. EXPORT_SYMBOL(kernel_sock_ioctl);
  3045. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
  3046. {
  3047. return sock->ops->shutdown(sock, how);
  3048. }
  3049. EXPORT_SYMBOL(kernel_sock_shutdown);
  3050. int sockev_register_notify(struct notifier_block *nb)
  3051. {
  3052. return blocking_notifier_chain_register(&sockev_notifier_list, nb);
  3053. }
  3054. EXPORT_SYMBOL(sockev_register_notify);
  3055. int sockev_unregister_notify(struct notifier_block *nb)
  3056. {
  3057. return blocking_notifier_chain_unregister(&sockev_notifier_list, nb);
  3058. }
  3059. EXPORT_SYMBOL(sockev_unregister_notify);