socket.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564
  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, current);
  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. /*
  620. * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
  621. */
  622. void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
  623. struct sk_buff *skb)
  624. {
  625. int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
  626. struct timespec ts[3];
  627. int empty = 1;
  628. struct skb_shared_hwtstamps *shhwtstamps =
  629. skb_hwtstamps(skb);
  630. /* Race occurred between timestamp enabling and packet
  631. receiving. Fill in the current time for now. */
  632. if (need_software_tstamp && skb->tstamp.tv64 == 0)
  633. __net_timestamp(skb);
  634. if (need_software_tstamp) {
  635. if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
  636. struct timeval tv;
  637. skb_get_timestamp(skb, &tv);
  638. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
  639. sizeof(tv), &tv);
  640. } else {
  641. skb_get_timestampns(skb, &ts[0]);
  642. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
  643. sizeof(ts[0]), &ts[0]);
  644. }
  645. }
  646. memset(ts, 0, sizeof(ts));
  647. if (sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE) &&
  648. ktime_to_timespec_cond(skb->tstamp, ts + 0))
  649. empty = 0;
  650. if (shhwtstamps) {
  651. if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
  652. ktime_to_timespec_cond(shhwtstamps->syststamp, ts + 1))
  653. empty = 0;
  654. if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
  655. ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts + 2))
  656. empty = 0;
  657. }
  658. if (!empty)
  659. put_cmsg(msg, SOL_SOCKET,
  660. SCM_TIMESTAMPING, sizeof(ts), &ts);
  661. }
  662. EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
  663. void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
  664. struct sk_buff *skb)
  665. {
  666. int ack;
  667. if (!sock_flag(sk, SOCK_WIFI_STATUS))
  668. return;
  669. if (!skb->wifi_acked_valid)
  670. return;
  671. ack = skb->wifi_acked;
  672. put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
  673. }
  674. EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
  675. static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
  676. struct sk_buff *skb)
  677. {
  678. if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
  679. put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
  680. sizeof(__u32), &skb->dropcount);
  681. }
  682. void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
  683. struct sk_buff *skb)
  684. {
  685. sock_recv_timestamp(msg, sk, skb);
  686. sock_recv_drops(msg, sk, skb);
  687. }
  688. EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
  689. static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
  690. struct msghdr *msg, size_t size, int flags)
  691. {
  692. struct sock_iocb *si = kiocb_to_siocb(iocb);
  693. sock_update_classid(sock->sk, current);
  694. si->sock = sock;
  695. si->scm = NULL;
  696. si->msg = msg;
  697. si->size = size;
  698. si->flags = flags;
  699. return sock->ops->recvmsg(iocb, sock, msg, size, flags);
  700. }
  701. static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
  702. struct msghdr *msg, size_t size, int flags)
  703. {
  704. int err = security_socket_recvmsg(sock, msg, size, flags);
  705. return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
  706. }
  707. int sock_recvmsg(struct socket *sock, struct msghdr *msg,
  708. size_t size, int flags)
  709. {
  710. struct kiocb iocb;
  711. struct sock_iocb siocb;
  712. int ret;
  713. init_sync_kiocb(&iocb, NULL);
  714. iocb.private = &siocb;
  715. ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
  716. if (-EIOCBQUEUED == ret)
  717. ret = wait_on_sync_kiocb(&iocb);
  718. return ret;
  719. }
  720. EXPORT_SYMBOL(sock_recvmsg);
  721. static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
  722. size_t size, int flags)
  723. {
  724. struct kiocb iocb;
  725. struct sock_iocb siocb;
  726. int ret;
  727. init_sync_kiocb(&iocb, NULL);
  728. iocb.private = &siocb;
  729. ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
  730. if (-EIOCBQUEUED == ret)
  731. ret = wait_on_sync_kiocb(&iocb);
  732. return ret;
  733. }
  734. /**
  735. * kernel_recvmsg - Receive a message from a socket (kernel space)
  736. * @sock: The socket to receive the message from
  737. * @msg: Received message
  738. * @vec: Input s/g array for message data
  739. * @num: Size of input s/g array
  740. * @size: Number of bytes to read
  741. * @flags: Message flags (MSG_DONTWAIT, etc...)
  742. *
  743. * On return the msg structure contains the scatter/gather array passed in the
  744. * vec argument. The array is modified so that it consists of the unfilled
  745. * portion of the original array.
  746. *
  747. * The returned value is the total number of bytes received, or an error.
  748. */
  749. int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
  750. struct kvec *vec, size_t num, size_t size, int flags)
  751. {
  752. mm_segment_t oldfs = get_fs();
  753. int result;
  754. set_fs(KERNEL_DS);
  755. /*
  756. * the following is safe, since for compiler definitions of kvec and
  757. * iovec are identical, yielding the same in-core layout and alignment
  758. */
  759. msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
  760. result = sock_recvmsg(sock, msg, size, flags);
  761. set_fs(oldfs);
  762. return result;
  763. }
  764. EXPORT_SYMBOL(kernel_recvmsg);
  765. static void sock_aio_dtor(struct kiocb *iocb)
  766. {
  767. kfree(iocb->private);
  768. }
  769. static ssize_t sock_sendpage(struct file *file, struct page *page,
  770. int offset, size_t size, loff_t *ppos, int more)
  771. {
  772. struct socket *sock;
  773. int flags;
  774. sock = file->private_data;
  775. flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  776. /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
  777. flags |= more;
  778. return kernel_sendpage(sock, page, offset, size, flags);
  779. }
  780. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  781. struct pipe_inode_info *pipe, size_t len,
  782. unsigned int flags)
  783. {
  784. struct socket *sock = file->private_data;
  785. if (unlikely(!sock->ops->splice_read))
  786. return -EINVAL;
  787. sock_update_classid(sock->sk, current);
  788. return sock->ops->splice_read(sock, ppos, pipe, len, flags);
  789. }
  790. static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
  791. struct sock_iocb *siocb)
  792. {
  793. if (!is_sync_kiocb(iocb)) {
  794. siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
  795. if (!siocb)
  796. return NULL;
  797. iocb->ki_dtor = sock_aio_dtor;
  798. }
  799. siocb->kiocb = iocb;
  800. iocb->private = siocb;
  801. return siocb;
  802. }
  803. static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
  804. struct file *file, const struct iovec *iov,
  805. unsigned long nr_segs)
  806. {
  807. struct socket *sock = file->private_data;
  808. size_t size = 0;
  809. int i;
  810. for (i = 0; i < nr_segs; i++)
  811. size += iov[i].iov_len;
  812. msg->msg_name = NULL;
  813. msg->msg_namelen = 0;
  814. msg->msg_control = NULL;
  815. msg->msg_controllen = 0;
  816. msg->msg_iov = (struct iovec *)iov;
  817. msg->msg_iovlen = nr_segs;
  818. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  819. return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
  820. }
  821. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  822. unsigned long nr_segs, loff_t pos)
  823. {
  824. struct sock_iocb siocb, *x;
  825. if (pos != 0)
  826. return -ESPIPE;
  827. if (iocb->ki_left == 0) /* Match SYS5 behaviour */
  828. return 0;
  829. x = alloc_sock_iocb(iocb, &siocb);
  830. if (!x)
  831. return -ENOMEM;
  832. return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  833. }
  834. static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
  835. struct file *file, const struct iovec *iov,
  836. unsigned long nr_segs)
  837. {
  838. struct socket *sock = file->private_data;
  839. size_t size = 0;
  840. int i;
  841. for (i = 0; i < nr_segs; i++)
  842. size += iov[i].iov_len;
  843. msg->msg_name = NULL;
  844. msg->msg_namelen = 0;
  845. msg->msg_control = NULL;
  846. msg->msg_controllen = 0;
  847. msg->msg_iov = (struct iovec *)iov;
  848. msg->msg_iovlen = nr_segs;
  849. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  850. if (sock->type == SOCK_SEQPACKET)
  851. msg->msg_flags |= MSG_EOR;
  852. return __sock_sendmsg(iocb, sock, msg, size);
  853. }
  854. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  855. unsigned long nr_segs, loff_t pos)
  856. {
  857. struct sock_iocb siocb, *x;
  858. if (pos != 0)
  859. return -ESPIPE;
  860. x = alloc_sock_iocb(iocb, &siocb);
  861. if (!x)
  862. return -ENOMEM;
  863. return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  864. }
  865. /*
  866. * Atomic setting of ioctl hooks to avoid race
  867. * with module unload.
  868. */
  869. static DEFINE_MUTEX(br_ioctl_mutex);
  870. static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
  871. void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
  872. {
  873. mutex_lock(&br_ioctl_mutex);
  874. br_ioctl_hook = hook;
  875. mutex_unlock(&br_ioctl_mutex);
  876. }
  877. EXPORT_SYMBOL(brioctl_set);
  878. static DEFINE_MUTEX(vlan_ioctl_mutex);
  879. static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
  880. void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
  881. {
  882. mutex_lock(&vlan_ioctl_mutex);
  883. vlan_ioctl_hook = hook;
  884. mutex_unlock(&vlan_ioctl_mutex);
  885. }
  886. EXPORT_SYMBOL(vlan_ioctl_set);
  887. static DEFINE_MUTEX(dlci_ioctl_mutex);
  888. static int (*dlci_ioctl_hook) (unsigned int, void __user *);
  889. void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
  890. {
  891. mutex_lock(&dlci_ioctl_mutex);
  892. dlci_ioctl_hook = hook;
  893. mutex_unlock(&dlci_ioctl_mutex);
  894. }
  895. EXPORT_SYMBOL(dlci_ioctl_set);
  896. static long sock_do_ioctl(struct net *net, struct socket *sock,
  897. unsigned int cmd, unsigned long arg)
  898. {
  899. int err;
  900. void __user *argp = (void __user *)arg;
  901. err = sock->ops->ioctl(sock, cmd, arg);
  902. /*
  903. * If this ioctl is unknown try to hand it down
  904. * to the NIC driver.
  905. */
  906. if (err == -ENOIOCTLCMD)
  907. err = dev_ioctl(net, cmd, argp);
  908. return err;
  909. }
  910. /*
  911. * With an ioctl, arg may well be a user mode pointer, but we don't know
  912. * what to do with it - that's up to the protocol still.
  913. */
  914. static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  915. {
  916. struct socket *sock;
  917. struct sock *sk;
  918. void __user *argp = (void __user *)arg;
  919. int pid, err;
  920. struct net *net;
  921. sock = file->private_data;
  922. sk = sock->sk;
  923. net = sock_net(sk);
  924. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
  925. err = dev_ioctl(net, cmd, argp);
  926. } else
  927. #ifdef CONFIG_WEXT_CORE
  928. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  929. err = dev_ioctl(net, cmd, argp);
  930. } else
  931. #endif
  932. switch (cmd) {
  933. case FIOSETOWN:
  934. case SIOCSPGRP:
  935. err = -EFAULT;
  936. if (get_user(pid, (int __user *)argp))
  937. break;
  938. err = f_setown(sock->file, pid, 1);
  939. break;
  940. case FIOGETOWN:
  941. case SIOCGPGRP:
  942. err = put_user(f_getown(sock->file),
  943. (int __user *)argp);
  944. break;
  945. case SIOCGIFBR:
  946. case SIOCSIFBR:
  947. case SIOCBRADDBR:
  948. case SIOCBRDELBR:
  949. err = -ENOPKG;
  950. if (!br_ioctl_hook)
  951. request_module("bridge");
  952. mutex_lock(&br_ioctl_mutex);
  953. if (br_ioctl_hook)
  954. err = br_ioctl_hook(net, cmd, argp);
  955. mutex_unlock(&br_ioctl_mutex);
  956. break;
  957. case SIOCGIFVLAN:
  958. case SIOCSIFVLAN:
  959. err = -ENOPKG;
  960. if (!vlan_ioctl_hook)
  961. request_module("8021q");
  962. mutex_lock(&vlan_ioctl_mutex);
  963. if (vlan_ioctl_hook)
  964. err = vlan_ioctl_hook(net, argp);
  965. mutex_unlock(&vlan_ioctl_mutex);
  966. break;
  967. case SIOCADDDLCI:
  968. case SIOCDELDLCI:
  969. err = -ENOPKG;
  970. if (!dlci_ioctl_hook)
  971. request_module("dlci");
  972. mutex_lock(&dlci_ioctl_mutex);
  973. if (dlci_ioctl_hook)
  974. err = dlci_ioctl_hook(cmd, argp);
  975. mutex_unlock(&dlci_ioctl_mutex);
  976. break;
  977. default:
  978. err = sock_do_ioctl(net, sock, cmd, arg);
  979. break;
  980. }
  981. return err;
  982. }
  983. int sock_create_lite(int family, int type, int protocol, struct socket **res)
  984. {
  985. int err;
  986. struct socket *sock = NULL;
  987. err = security_socket_create(family, type, protocol, 1);
  988. if (err)
  989. goto out;
  990. sock = sock_alloc();
  991. if (!sock) {
  992. err = -ENOMEM;
  993. goto out;
  994. }
  995. sock->type = type;
  996. err = security_socket_post_create(sock, family, type, protocol, 1);
  997. if (err)
  998. goto out_release;
  999. out:
  1000. *res = sock;
  1001. return err;
  1002. out_release:
  1003. sock_release(sock);
  1004. sock = NULL;
  1005. goto out;
  1006. }
  1007. EXPORT_SYMBOL(sock_create_lite);
  1008. /* No kernel lock held - perfect */
  1009. static unsigned int sock_poll(struct file *file, poll_table *wait)
  1010. {
  1011. struct socket *sock;
  1012. /*
  1013. * We can't return errors to poll, so it's either yes or no.
  1014. */
  1015. sock = file->private_data;
  1016. return sock->ops->poll(file, sock, wait);
  1017. }
  1018. static int sock_mmap(struct file *file, struct vm_area_struct *vma)
  1019. {
  1020. struct socket *sock = file->private_data;
  1021. return sock->ops->mmap(file, sock, vma);
  1022. }
  1023. static int sock_close(struct inode *inode, struct file *filp)
  1024. {
  1025. /*
  1026. * It was possible the inode is NULL we were
  1027. * closing an unfinished socket.
  1028. */
  1029. if (!inode) {
  1030. printk(KERN_DEBUG "sock_close: NULL inode\n");
  1031. return 0;
  1032. }
  1033. sock_release(SOCKET_I(inode));
  1034. return 0;
  1035. }
  1036. /*
  1037. * Update the socket async list
  1038. *
  1039. * Fasync_list locking strategy.
  1040. *
  1041. * 1. fasync_list is modified only under process context socket lock
  1042. * i.e. under semaphore.
  1043. * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
  1044. * or under socket lock
  1045. */
  1046. static int sock_fasync(int fd, struct file *filp, int on)
  1047. {
  1048. struct socket *sock = filp->private_data;
  1049. struct sock *sk = sock->sk;
  1050. struct socket_wq *wq;
  1051. if (sk == NULL)
  1052. return -EINVAL;
  1053. lock_sock(sk);
  1054. wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
  1055. fasync_helper(fd, filp, on, &wq->fasync_list);
  1056. if (!wq->fasync_list)
  1057. sock_reset_flag(sk, SOCK_FASYNC);
  1058. else
  1059. sock_set_flag(sk, SOCK_FASYNC);
  1060. release_sock(sk);
  1061. return 0;
  1062. }
  1063. /* This function may be called only under socket lock or callback_lock or rcu_lock */
  1064. int sock_wake_async(struct socket *sock, int how, int band)
  1065. {
  1066. struct socket_wq *wq;
  1067. if (!sock)
  1068. return -1;
  1069. rcu_read_lock();
  1070. wq = rcu_dereference(sock->wq);
  1071. if (!wq || !wq->fasync_list) {
  1072. rcu_read_unlock();
  1073. return -1;
  1074. }
  1075. switch (how) {
  1076. case SOCK_WAKE_WAITD:
  1077. if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
  1078. break;
  1079. goto call_kill;
  1080. case SOCK_WAKE_SPACE:
  1081. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
  1082. break;
  1083. /* fall through */
  1084. case SOCK_WAKE_IO:
  1085. call_kill:
  1086. kill_fasync(&wq->fasync_list, SIGIO, band);
  1087. break;
  1088. case SOCK_WAKE_URG:
  1089. kill_fasync(&wq->fasync_list, SIGURG, band);
  1090. }
  1091. rcu_read_unlock();
  1092. return 0;
  1093. }
  1094. EXPORT_SYMBOL(sock_wake_async);
  1095. int __sock_create(struct net *net, int family, int type, int protocol,
  1096. struct socket **res, int kern)
  1097. {
  1098. int err;
  1099. struct socket *sock;
  1100. const struct net_proto_family *pf;
  1101. /*
  1102. * Check protocol is in range
  1103. */
  1104. if (family < 0 || family >= NPROTO)
  1105. return -EAFNOSUPPORT;
  1106. if (type < 0 || type >= SOCK_MAX)
  1107. return -EINVAL;
  1108. /* Compatibility.
  1109. This uglymoron is moved from INET layer to here to avoid
  1110. deadlock in module load.
  1111. */
  1112. if (family == PF_INET && type == SOCK_PACKET) {
  1113. static int warned;
  1114. if (!warned) {
  1115. warned = 1;
  1116. printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
  1117. current->comm);
  1118. }
  1119. family = PF_PACKET;
  1120. }
  1121. err = security_socket_create(family, type, protocol, kern);
  1122. if (err)
  1123. return err;
  1124. /*
  1125. * Allocate the socket and allow the family to set things up. if
  1126. * the protocol is 0, the family is instructed to select an appropriate
  1127. * default.
  1128. */
  1129. sock = sock_alloc();
  1130. if (!sock) {
  1131. net_warn_ratelimited("socket: no more sockets\n");
  1132. return -ENFILE; /* Not exactly a match, but its the
  1133. closest posix thing */
  1134. }
  1135. sock->type = type;
  1136. #ifdef CONFIG_MODULES
  1137. /* Attempt to load a protocol module if the find failed.
  1138. *
  1139. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  1140. * requested real, full-featured networking support upon configuration.
  1141. * Otherwise module support will break!
  1142. */
  1143. if (rcu_access_pointer(net_families[family]) == NULL)
  1144. request_module("net-pf-%d", family);
  1145. #endif
  1146. rcu_read_lock();
  1147. pf = rcu_dereference(net_families[family]);
  1148. err = -EAFNOSUPPORT;
  1149. if (!pf)
  1150. goto out_release;
  1151. /*
  1152. * We will call the ->create function, that possibly is in a loadable
  1153. * module, so we have to bump that loadable module refcnt first.
  1154. */
  1155. if (!try_module_get(pf->owner))
  1156. goto out_release;
  1157. /* Now protected by module ref count */
  1158. rcu_read_unlock();
  1159. err = pf->create(net, sock, protocol, kern);
  1160. if (err < 0)
  1161. goto out_module_put;
  1162. /*
  1163. * Now to bump the refcnt of the [loadable] module that owns this
  1164. * socket at sock_release time we decrement its refcnt.
  1165. */
  1166. if (!try_module_get(sock->ops->owner))
  1167. goto out_module_busy;
  1168. /*
  1169. * Now that we're done with the ->create function, the [loadable]
  1170. * module can have its refcnt decremented
  1171. */
  1172. module_put(pf->owner);
  1173. err = security_socket_post_create(sock, family, type, protocol, kern);
  1174. if (err)
  1175. goto out_sock_release;
  1176. *res = sock;
  1177. return 0;
  1178. out_module_busy:
  1179. err = -EAFNOSUPPORT;
  1180. out_module_put:
  1181. sock->ops = NULL;
  1182. module_put(pf->owner);
  1183. out_sock_release:
  1184. sock_release(sock);
  1185. return err;
  1186. out_release:
  1187. rcu_read_unlock();
  1188. goto out_sock_release;
  1189. }
  1190. EXPORT_SYMBOL(__sock_create);
  1191. int sock_create(int family, int type, int protocol, struct socket **res)
  1192. {
  1193. return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
  1194. }
  1195. EXPORT_SYMBOL(sock_create);
  1196. int sock_create_kern(int family, int type, int protocol, struct socket **res)
  1197. {
  1198. return __sock_create(&init_net, family, type, protocol, res, 1);
  1199. }
  1200. EXPORT_SYMBOL(sock_create_kern);
  1201. SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
  1202. {
  1203. int retval;
  1204. struct socket *sock;
  1205. int flags;
  1206. /* Check the SOCK_* constants for consistency. */
  1207. BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
  1208. BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
  1209. BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
  1210. BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
  1211. flags = type & ~SOCK_TYPE_MASK;
  1212. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1213. return -EINVAL;
  1214. type &= SOCK_TYPE_MASK;
  1215. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1216. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1217. retval = sock_create(family, type, protocol, &sock);
  1218. if (retval < 0)
  1219. goto out;
  1220. if (retval == 0)
  1221. sockev_notify(SOCKEV_SOCKET, sock);
  1222. retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
  1223. if (retval < 0)
  1224. goto out_release;
  1225. out:
  1226. /* It may be already another descriptor 8) Not kernel problem. */
  1227. return retval;
  1228. out_release:
  1229. sock_release(sock);
  1230. return retval;
  1231. }
  1232. /*
  1233. * Create a pair of connected sockets.
  1234. */
  1235. SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
  1236. int __user *, usockvec)
  1237. {
  1238. struct socket *sock1, *sock2;
  1239. int fd1, fd2, err;
  1240. struct file *newfile1, *newfile2;
  1241. int flags;
  1242. flags = type & ~SOCK_TYPE_MASK;
  1243. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1244. return -EINVAL;
  1245. type &= SOCK_TYPE_MASK;
  1246. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1247. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1248. /*
  1249. * Obtain the first socket and check if the underlying protocol
  1250. * supports the socketpair call.
  1251. */
  1252. err = sock_create(family, type, protocol, &sock1);
  1253. if (err < 0)
  1254. goto out;
  1255. err = sock_create(family, type, protocol, &sock2);
  1256. if (err < 0)
  1257. goto out_release_1;
  1258. err = sock1->ops->socketpair(sock1, sock2);
  1259. if (err < 0)
  1260. goto out_release_both;
  1261. fd1 = sock_alloc_file(sock1, &newfile1, flags, NULL);
  1262. if (unlikely(fd1 < 0)) {
  1263. err = fd1;
  1264. goto out_release_both;
  1265. }
  1266. fd2 = sock_alloc_file(sock2, &newfile2, flags, NULL);
  1267. if (unlikely(fd2 < 0)) {
  1268. err = fd2;
  1269. fput(newfile1);
  1270. put_unused_fd(fd1);
  1271. sock_release(sock2);
  1272. goto out;
  1273. }
  1274. audit_fd_pair(fd1, fd2);
  1275. fd_install(fd1, newfile1);
  1276. fd_install(fd2, newfile2);
  1277. /* fd1 and fd2 may be already another descriptors.
  1278. * Not kernel problem.
  1279. */
  1280. err = put_user(fd1, &usockvec[0]);
  1281. if (!err)
  1282. err = put_user(fd2, &usockvec[1]);
  1283. if (!err)
  1284. return 0;
  1285. sys_close(fd2);
  1286. sys_close(fd1);
  1287. return err;
  1288. out_release_both:
  1289. sock_release(sock2);
  1290. out_release_1:
  1291. sock_release(sock1);
  1292. out:
  1293. return err;
  1294. }
  1295. /*
  1296. * Bind a name to a socket. Nothing much to do here since it's
  1297. * the protocol's responsibility to handle the local address.
  1298. *
  1299. * We move the socket address to kernel space before we call
  1300. * the protocol layer (having also checked the address is ok).
  1301. */
  1302. SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
  1303. {
  1304. struct socket *sock;
  1305. struct sockaddr_storage address;
  1306. int err, fput_needed;
  1307. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1308. if (sock) {
  1309. err = move_addr_to_kernel(umyaddr, addrlen, &address);
  1310. if (err >= 0) {
  1311. err = security_socket_bind(sock,
  1312. (struct sockaddr *)&address,
  1313. addrlen);
  1314. if (!err)
  1315. err = sock->ops->bind(sock,
  1316. (struct sockaddr *)
  1317. &address, addrlen);
  1318. }
  1319. fput_light(sock->file, fput_needed);
  1320. if (!err)
  1321. sockev_notify(SOCKEV_BIND, sock);
  1322. }
  1323. return err;
  1324. }
  1325. /*
  1326. * Perform a listen. Basically, we allow the protocol to do anything
  1327. * necessary for a listen, and if that works, we mark the socket as
  1328. * ready for listening.
  1329. */
  1330. SYSCALL_DEFINE2(listen, int, fd, int, backlog)
  1331. {
  1332. struct socket *sock;
  1333. int err, fput_needed;
  1334. int somaxconn;
  1335. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1336. if (sock) {
  1337. somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
  1338. if ((unsigned int)backlog > somaxconn)
  1339. backlog = somaxconn;
  1340. err = security_socket_listen(sock, backlog);
  1341. if (!err)
  1342. err = sock->ops->listen(sock, backlog);
  1343. fput_light(sock->file, fput_needed);
  1344. if (!err)
  1345. sockev_notify(SOCKEV_LISTEN, sock);
  1346. }
  1347. return err;
  1348. }
  1349. /*
  1350. * For accept, we attempt to create a new socket, set up the link
  1351. * with the client, wake up the client, then return the new
  1352. * connected fd. We collect the address of the connector in kernel
  1353. * space and move it to user at the very end. This is unclean because
  1354. * we open the socket then return an error.
  1355. *
  1356. * 1003.1g adds the ability to recvmsg() to query connection pending
  1357. * status to recvmsg. We need to add that support in a way thats
  1358. * clean when we restucture accept also.
  1359. */
  1360. SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1361. int __user *, upeer_addrlen, int, flags)
  1362. {
  1363. struct socket *sock, *newsock;
  1364. struct file *newfile;
  1365. int err, len, newfd, fput_needed;
  1366. struct sockaddr_storage address;
  1367. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1368. return -EINVAL;
  1369. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1370. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1371. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1372. if (!sock)
  1373. goto out;
  1374. err = -ENFILE;
  1375. newsock = sock_alloc();
  1376. if (!newsock)
  1377. goto out_put;
  1378. newsock->type = sock->type;
  1379. newsock->ops = sock->ops;
  1380. /*
  1381. * We don't need try_module_get here, as the listening socket (sock)
  1382. * has the protocol module (sock->ops->owner) held.
  1383. */
  1384. __module_get(newsock->ops->owner);
  1385. newfd = sock_alloc_file(newsock, &newfile, flags,
  1386. sock->sk->sk_prot_creator->name);
  1387. if (unlikely(newfd < 0)) {
  1388. err = newfd;
  1389. sock_release(newsock);
  1390. goto out_put;
  1391. }
  1392. err = security_socket_accept(sock, newsock);
  1393. if (err)
  1394. goto out_fd;
  1395. err = sock->ops->accept(sock, newsock, sock->file->f_flags);
  1396. if (err < 0)
  1397. goto out_fd;
  1398. if (upeer_sockaddr) {
  1399. if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
  1400. &len, 2) < 0) {
  1401. err = -ECONNABORTED;
  1402. goto out_fd;
  1403. }
  1404. err = move_addr_to_user(&address,
  1405. len, upeer_sockaddr, upeer_addrlen);
  1406. if (err < 0)
  1407. goto out_fd;
  1408. }
  1409. /* File flags are not inherited via accept() unlike another OSes. */
  1410. fd_install(newfd, newfile);
  1411. err = newfd;
  1412. if (!err)
  1413. sockev_notify(SOCKEV_ACCEPT, sock);
  1414. out_put:
  1415. fput_light(sock->file, fput_needed);
  1416. out:
  1417. return err;
  1418. out_fd:
  1419. fput(newfile);
  1420. put_unused_fd(newfd);
  1421. goto out_put;
  1422. }
  1423. SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1424. int __user *, upeer_addrlen)
  1425. {
  1426. return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
  1427. }
  1428. /*
  1429. * Attempt to connect to a socket with the server address. The address
  1430. * is in user space so we verify it is OK and move it to kernel space.
  1431. *
  1432. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1433. * break bindings
  1434. *
  1435. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1436. * other SEQPACKET protocols that take time to connect() as it doesn't
  1437. * include the -EINPROGRESS status for such sockets.
  1438. */
  1439. SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
  1440. int, addrlen)
  1441. {
  1442. struct socket *sock;
  1443. struct sockaddr_storage address;
  1444. int err, fput_needed;
  1445. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1446. if (!sock)
  1447. goto out;
  1448. err = move_addr_to_kernel(uservaddr, addrlen, &address);
  1449. if (err < 0)
  1450. goto out_put;
  1451. err =
  1452. security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
  1453. if (err)
  1454. goto out_put;
  1455. err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
  1456. sock->file->f_flags);
  1457. if (!err)
  1458. sockev_notify(SOCKEV_CONNECT, sock);
  1459. out_put:
  1460. fput_light(sock->file, fput_needed);
  1461. out:
  1462. return err;
  1463. }
  1464. /*
  1465. * Get the local address ('name') of a socket object. Move the obtained
  1466. * name to user space.
  1467. */
  1468. SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
  1469. int __user *, usockaddr_len)
  1470. {
  1471. struct socket *sock;
  1472. struct sockaddr_storage address;
  1473. int len, err, fput_needed;
  1474. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1475. if (!sock)
  1476. goto out;
  1477. err = security_socket_getsockname(sock);
  1478. if (err)
  1479. goto out_put;
  1480. err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
  1481. if (err)
  1482. goto out_put;
  1483. err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
  1484. out_put:
  1485. fput_light(sock->file, fput_needed);
  1486. out:
  1487. return err;
  1488. }
  1489. /*
  1490. * Get the remote address ('name') of a socket object. Move the obtained
  1491. * name to user space.
  1492. */
  1493. SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
  1494. int __user *, usockaddr_len)
  1495. {
  1496. struct socket *sock;
  1497. struct sockaddr_storage address;
  1498. int len, err, fput_needed;
  1499. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1500. if (sock != NULL) {
  1501. err = security_socket_getpeername(sock);
  1502. if (err) {
  1503. fput_light(sock->file, fput_needed);
  1504. return err;
  1505. }
  1506. err =
  1507. sock->ops->getname(sock, (struct sockaddr *)&address, &len,
  1508. 1);
  1509. if (!err)
  1510. err = move_addr_to_user(&address, len, usockaddr,
  1511. usockaddr_len);
  1512. fput_light(sock->file, fput_needed);
  1513. }
  1514. return err;
  1515. }
  1516. /*
  1517. * Send a datagram to a given address. We move the address into kernel
  1518. * space and check the user space data area is readable before invoking
  1519. * the protocol.
  1520. */
  1521. SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
  1522. unsigned int, flags, struct sockaddr __user *, addr,
  1523. int, addr_len)
  1524. {
  1525. struct socket *sock;
  1526. struct sockaddr_storage address;
  1527. int err;
  1528. struct msghdr msg;
  1529. struct iovec iov;
  1530. int fput_needed;
  1531. if (len > INT_MAX)
  1532. len = INT_MAX;
  1533. if (unlikely(!access_ok(VERIFY_READ, buff, len)))
  1534. return -EFAULT;
  1535. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1536. if (!sock)
  1537. goto out;
  1538. iov.iov_base = buff;
  1539. iov.iov_len = len;
  1540. msg.msg_name = NULL;
  1541. msg.msg_iov = &iov;
  1542. msg.msg_iovlen = 1;
  1543. msg.msg_control = NULL;
  1544. msg.msg_controllen = 0;
  1545. msg.msg_namelen = 0;
  1546. if (addr) {
  1547. err = move_addr_to_kernel(addr, addr_len, &address);
  1548. if (err < 0)
  1549. goto out_put;
  1550. msg.msg_name = (struct sockaddr *)&address;
  1551. msg.msg_namelen = addr_len;
  1552. }
  1553. if (sock->file->f_flags & O_NONBLOCK)
  1554. flags |= MSG_DONTWAIT;
  1555. msg.msg_flags = flags;
  1556. err = sock_sendmsg(sock, &msg, len);
  1557. out_put:
  1558. fput_light(sock->file, fput_needed);
  1559. out:
  1560. return err;
  1561. }
  1562. /*
  1563. * Send a datagram down a socket.
  1564. */
  1565. SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
  1566. unsigned int, flags)
  1567. {
  1568. return sys_sendto(fd, buff, len, flags, NULL, 0);
  1569. }
  1570. /*
  1571. * Receive a frame from the socket and optionally record the address of the
  1572. * sender. We verify the buffers are writable and if needed move the
  1573. * sender address from kernel to user space.
  1574. */
  1575. SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
  1576. unsigned int, flags, struct sockaddr __user *, addr,
  1577. int __user *, addr_len)
  1578. {
  1579. struct socket *sock;
  1580. struct iovec iov;
  1581. struct msghdr msg;
  1582. struct sockaddr_storage address;
  1583. int err, err2;
  1584. int fput_needed;
  1585. if (size > INT_MAX)
  1586. size = INT_MAX;
  1587. if (unlikely(!access_ok(VERIFY_WRITE, ubuf, size)))
  1588. return -EFAULT;
  1589. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1590. if (!sock)
  1591. goto out;
  1592. msg.msg_control = NULL;
  1593. msg.msg_controllen = 0;
  1594. msg.msg_iovlen = 1;
  1595. msg.msg_iov = &iov;
  1596. iov.iov_len = size;
  1597. iov.iov_base = ubuf;
  1598. /* Save some cycles and don't copy the address if not needed */
  1599. msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
  1600. /* We assume all kernel code knows the size of sockaddr_storage */
  1601. msg.msg_namelen = 0;
  1602. if (sock->file->f_flags & O_NONBLOCK)
  1603. flags |= MSG_DONTWAIT;
  1604. err = sock_recvmsg(sock, &msg, size, flags);
  1605. if (err >= 0 && addr != NULL) {
  1606. err2 = move_addr_to_user(&address,
  1607. msg.msg_namelen, addr, addr_len);
  1608. if (err2 < 0)
  1609. err = err2;
  1610. }
  1611. fput_light(sock->file, fput_needed);
  1612. out:
  1613. return err;
  1614. }
  1615. /*
  1616. * Receive a datagram from a socket.
  1617. */
  1618. asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
  1619. unsigned int flags)
  1620. {
  1621. return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1622. }
  1623. /*
  1624. * Set a socket option. Because we don't know the option lengths we have
  1625. * to pass the user mode parameter for the protocols to sort out.
  1626. */
  1627. SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
  1628. char __user *, optval, int, optlen)
  1629. {
  1630. int err, fput_needed;
  1631. struct socket *sock;
  1632. if (optlen < 0)
  1633. return -EINVAL;
  1634. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1635. if (sock != NULL) {
  1636. err = security_socket_setsockopt(sock, level, optname);
  1637. if (err)
  1638. goto out_put;
  1639. if (level == SOL_SOCKET)
  1640. err =
  1641. sock_setsockopt(sock, level, optname, optval,
  1642. optlen);
  1643. else
  1644. err =
  1645. sock->ops->setsockopt(sock, level, optname, optval,
  1646. optlen);
  1647. out_put:
  1648. fput_light(sock->file, fput_needed);
  1649. }
  1650. return err;
  1651. }
  1652. /*
  1653. * Get a socket option. Because we don't know the option lengths we have
  1654. * to pass a user mode parameter for the protocols to sort out.
  1655. */
  1656. SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
  1657. char __user *, optval, int __user *, optlen)
  1658. {
  1659. int err, fput_needed;
  1660. struct socket *sock;
  1661. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1662. if (sock != NULL) {
  1663. err = security_socket_getsockopt(sock, level, optname);
  1664. if (err)
  1665. goto out_put;
  1666. if (level == SOL_SOCKET)
  1667. err =
  1668. sock_getsockopt(sock, level, optname, optval,
  1669. optlen);
  1670. else
  1671. err =
  1672. sock->ops->getsockopt(sock, level, optname, optval,
  1673. optlen);
  1674. out_put:
  1675. fput_light(sock->file, fput_needed);
  1676. }
  1677. return err;
  1678. }
  1679. /*
  1680. * Shutdown a socket.
  1681. */
  1682. SYSCALL_DEFINE2(shutdown, int, fd, int, how)
  1683. {
  1684. int err, fput_needed;
  1685. struct socket *sock;
  1686. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1687. if (sock != NULL) {
  1688. sockev_notify(SOCKEV_SHUTDOWN, sock);
  1689. err = security_socket_shutdown(sock, how);
  1690. if (!err)
  1691. err = sock->ops->shutdown(sock, how);
  1692. fput_light(sock->file, fput_needed);
  1693. }
  1694. return err;
  1695. }
  1696. /* A couple of helpful macros for getting the address of the 32/64 bit
  1697. * fields which are the same type (int / unsigned) on our platforms.
  1698. */
  1699. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  1700. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  1701. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  1702. struct used_address {
  1703. struct sockaddr_storage name;
  1704. unsigned int name_len;
  1705. };
  1706. static int copy_msghdr_from_user(struct msghdr *kmsg,
  1707. struct msghdr __user *umsg)
  1708. {
  1709. if (copy_from_user(kmsg, umsg, sizeof(struct msghdr)))
  1710. return -EFAULT;
  1711. if (kmsg->msg_name == NULL)
  1712. kmsg->msg_namelen = 0;
  1713. if (kmsg->msg_namelen < 0)
  1714. return -EINVAL;
  1715. if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
  1716. kmsg->msg_namelen = sizeof(struct sockaddr_storage);
  1717. return 0;
  1718. }
  1719. static int ___sys_sendmsg(struct socket *sock, struct msghdr __user *msg,
  1720. struct msghdr *msg_sys, unsigned flags,
  1721. struct used_address *used_address)
  1722. {
  1723. struct compat_msghdr __user *msg_compat =
  1724. (struct compat_msghdr __user *)msg;
  1725. struct sockaddr_storage address;
  1726. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1727. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  1728. __attribute__ ((aligned(sizeof(__kernel_size_t))));
  1729. /* 20 is size of ipv6_pktinfo */
  1730. unsigned char *ctl_buf = ctl;
  1731. int err, ctl_len, iov_size, total_len;
  1732. err = -EFAULT;
  1733. if (MSG_CMSG_COMPAT & flags)
  1734. err = get_compat_msghdr(msg_sys, msg_compat);
  1735. else
  1736. err = copy_msghdr_from_user(msg_sys, msg);
  1737. if (err)
  1738. return err;
  1739. /* do not move before msg_sys is valid */
  1740. err = -EMSGSIZE;
  1741. if (msg_sys->msg_iovlen > UIO_MAXIOV)
  1742. goto out;
  1743. /* Check whether to allocate the iovec area */
  1744. err = -ENOMEM;
  1745. iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
  1746. if (msg_sys->msg_iovlen > UIO_FASTIOV) {
  1747. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1748. if (!iov)
  1749. goto out;
  1750. }
  1751. /* This will also move the address data into kernel space */
  1752. if (MSG_CMSG_COMPAT & flags) {
  1753. err = verify_compat_iovec(msg_sys, iov, &address, VERIFY_READ);
  1754. } else
  1755. err = verify_iovec(msg_sys, iov, &address, VERIFY_READ);
  1756. if (err < 0)
  1757. goto out_freeiov;
  1758. total_len = err;
  1759. err = -ENOBUFS;
  1760. if (msg_sys->msg_controllen > INT_MAX)
  1761. goto out_freeiov;
  1762. ctl_len = msg_sys->msg_controllen;
  1763. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  1764. err =
  1765. cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
  1766. sizeof(ctl));
  1767. if (err)
  1768. goto out_freeiov;
  1769. ctl_buf = msg_sys->msg_control;
  1770. ctl_len = msg_sys->msg_controllen;
  1771. } else if (ctl_len) {
  1772. if (ctl_len > sizeof(ctl)) {
  1773. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  1774. if (ctl_buf == NULL)
  1775. goto out_freeiov;
  1776. }
  1777. err = -EFAULT;
  1778. /*
  1779. * Careful! Before this, msg_sys->msg_control contains a user pointer.
  1780. * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
  1781. * checking falls down on this.
  1782. */
  1783. if (copy_from_user(ctl_buf,
  1784. (void __user __force *)msg_sys->msg_control,
  1785. ctl_len))
  1786. goto out_freectl;
  1787. msg_sys->msg_control = ctl_buf;
  1788. }
  1789. msg_sys->msg_flags = flags;
  1790. if (sock->file->f_flags & O_NONBLOCK)
  1791. msg_sys->msg_flags |= MSG_DONTWAIT;
  1792. /*
  1793. * If this is sendmmsg() and current destination address is same as
  1794. * previously succeeded address, omit asking LSM's decision.
  1795. * used_address->name_len is initialized to UINT_MAX so that the first
  1796. * destination address never matches.
  1797. */
  1798. if (used_address && msg_sys->msg_name &&
  1799. used_address->name_len == msg_sys->msg_namelen &&
  1800. !memcmp(&used_address->name, msg_sys->msg_name,
  1801. used_address->name_len)) {
  1802. err = sock_sendmsg_nosec(sock, msg_sys, total_len);
  1803. goto out_freectl;
  1804. }
  1805. err = sock_sendmsg(sock, msg_sys, total_len);
  1806. /*
  1807. * If this is sendmmsg() and sending to current destination address was
  1808. * successful, remember it.
  1809. */
  1810. if (used_address && err >= 0) {
  1811. used_address->name_len = msg_sys->msg_namelen;
  1812. if (msg_sys->msg_name)
  1813. memcpy(&used_address->name, msg_sys->msg_name,
  1814. used_address->name_len);
  1815. }
  1816. out_freectl:
  1817. if (ctl_buf != ctl)
  1818. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  1819. out_freeiov:
  1820. if (iov != iovstack)
  1821. sock_kfree_s(sock->sk, iov, iov_size);
  1822. out:
  1823. return err;
  1824. }
  1825. /*
  1826. * BSD sendmsg interface
  1827. */
  1828. long __sys_sendmsg(int fd, struct msghdr __user *msg, unsigned int flags)
  1829. {
  1830. int fput_needed, err;
  1831. struct msghdr msg_sys;
  1832. struct socket *sock;
  1833. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1834. if (!sock)
  1835. goto out;
  1836. err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
  1837. fput_light(sock->file, fput_needed);
  1838. out:
  1839. return err;
  1840. }
  1841. SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, flags)
  1842. {
  1843. if (flags & MSG_CMSG_COMPAT)
  1844. return -EINVAL;
  1845. return __sys_sendmsg(fd, msg, flags);
  1846. }
  1847. /*
  1848. * Linux sendmmsg interface
  1849. */
  1850. int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  1851. unsigned int flags)
  1852. {
  1853. int fput_needed, err, datagrams;
  1854. struct socket *sock;
  1855. struct mmsghdr __user *entry;
  1856. struct compat_mmsghdr __user *compat_entry;
  1857. struct msghdr msg_sys;
  1858. struct used_address used_address;
  1859. if (vlen > UIO_MAXIOV)
  1860. vlen = UIO_MAXIOV;
  1861. datagrams = 0;
  1862. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1863. if (!sock)
  1864. return err;
  1865. used_address.name_len = UINT_MAX;
  1866. entry = mmsg;
  1867. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  1868. err = 0;
  1869. while (datagrams < vlen) {
  1870. if (MSG_CMSG_COMPAT & flags) {
  1871. err = ___sys_sendmsg(sock, (struct msghdr __user *)compat_entry,
  1872. &msg_sys, flags, &used_address);
  1873. if (err < 0)
  1874. break;
  1875. err = __put_user(err, &compat_entry->msg_len);
  1876. ++compat_entry;
  1877. } else {
  1878. err = ___sys_sendmsg(sock,
  1879. (struct msghdr __user *)entry,
  1880. &msg_sys, flags, &used_address);
  1881. if (err < 0)
  1882. break;
  1883. err = put_user(err, &entry->msg_len);
  1884. ++entry;
  1885. }
  1886. if (err)
  1887. break;
  1888. ++datagrams;
  1889. }
  1890. fput_light(sock->file, fput_needed);
  1891. /* We only return an error if no datagrams were able to be sent */
  1892. if (datagrams != 0)
  1893. return datagrams;
  1894. return err;
  1895. }
  1896. SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
  1897. unsigned int, vlen, unsigned int, flags)
  1898. {
  1899. if (flags & MSG_CMSG_COMPAT)
  1900. return -EINVAL;
  1901. return __sys_sendmmsg(fd, mmsg, vlen, flags);
  1902. }
  1903. static int ___sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
  1904. struct msghdr *msg_sys, unsigned int flags, int nosec)
  1905. {
  1906. struct compat_msghdr __user *msg_compat =
  1907. (struct compat_msghdr __user *)msg;
  1908. struct iovec iovstack[UIO_FASTIOV];
  1909. struct iovec *iov = iovstack;
  1910. unsigned long cmsg_ptr;
  1911. int err, iov_size, total_len, len;
  1912. /* kernel mode address */
  1913. struct sockaddr_storage addr;
  1914. /* user mode address pointers */
  1915. struct sockaddr __user *uaddr;
  1916. int __user *uaddr_len;
  1917. if (MSG_CMSG_COMPAT & flags)
  1918. err = get_compat_msghdr(msg_sys, msg_compat);
  1919. else
  1920. err = copy_msghdr_from_user(msg_sys, msg);
  1921. if (err)
  1922. return err;
  1923. err = -EMSGSIZE;
  1924. if (msg_sys->msg_iovlen > UIO_MAXIOV)
  1925. goto out;
  1926. /* Check whether to allocate the iovec area */
  1927. err = -ENOMEM;
  1928. iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
  1929. if (msg_sys->msg_iovlen > UIO_FASTIOV) {
  1930. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1931. if (!iov)
  1932. goto out;
  1933. }
  1934. /* Save the user-mode address (verify_iovec will change the
  1935. * kernel msghdr to use the kernel address space)
  1936. */
  1937. uaddr = (__force void __user *)msg_sys->msg_name;
  1938. uaddr_len = COMPAT_NAMELEN(msg);
  1939. if (MSG_CMSG_COMPAT & flags)
  1940. err = verify_compat_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
  1941. else
  1942. err = verify_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
  1943. if (err < 0)
  1944. goto out_freeiov;
  1945. total_len = err;
  1946. cmsg_ptr = (unsigned long)msg_sys->msg_control;
  1947. msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
  1948. /* We assume all kernel code knows the size of sockaddr_storage */
  1949. msg_sys->msg_namelen = 0;
  1950. if (sock->file->f_flags & O_NONBLOCK)
  1951. flags |= MSG_DONTWAIT;
  1952. err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
  1953. total_len, flags);
  1954. if (err < 0)
  1955. goto out_freeiov;
  1956. len = err;
  1957. if (uaddr != NULL) {
  1958. err = move_addr_to_user(&addr,
  1959. msg_sys->msg_namelen, uaddr,
  1960. uaddr_len);
  1961. if (err < 0)
  1962. goto out_freeiov;
  1963. }
  1964. err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
  1965. COMPAT_FLAGS(msg));
  1966. if (err)
  1967. goto out_freeiov;
  1968. if (MSG_CMSG_COMPAT & flags)
  1969. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1970. &msg_compat->msg_controllen);
  1971. else
  1972. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1973. &msg->msg_controllen);
  1974. if (err)
  1975. goto out_freeiov;
  1976. err = len;
  1977. out_freeiov:
  1978. if (iov != iovstack)
  1979. sock_kfree_s(sock->sk, iov, iov_size);
  1980. out:
  1981. return err;
  1982. }
  1983. /*
  1984. * BSD recvmsg interface
  1985. */
  1986. long __sys_recvmsg(int fd, struct msghdr __user *msg, unsigned flags)
  1987. {
  1988. int fput_needed, err;
  1989. struct msghdr msg_sys;
  1990. struct socket *sock;
  1991. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1992. if (!sock)
  1993. goto out;
  1994. err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
  1995. fput_light(sock->file, fput_needed);
  1996. out:
  1997. return err;
  1998. }
  1999. SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
  2000. unsigned int, flags)
  2001. {
  2002. if (flags & MSG_CMSG_COMPAT)
  2003. return -EINVAL;
  2004. return __sys_recvmsg(fd, msg, flags);
  2005. }
  2006. /*
  2007. * Linux recvmmsg interface
  2008. */
  2009. int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  2010. unsigned int flags, struct timespec *timeout)
  2011. {
  2012. int fput_needed, err, datagrams;
  2013. struct socket *sock;
  2014. struct mmsghdr __user *entry;
  2015. struct compat_mmsghdr __user *compat_entry;
  2016. struct msghdr msg_sys;
  2017. struct timespec end_time;
  2018. if (timeout &&
  2019. poll_select_set_timeout(&end_time, timeout->tv_sec,
  2020. timeout->tv_nsec))
  2021. return -EINVAL;
  2022. datagrams = 0;
  2023. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  2024. if (!sock)
  2025. return err;
  2026. err = sock_error(sock->sk);
  2027. if (err) {
  2028. datagrams = err;
  2029. goto out_put;
  2030. }
  2031. entry = mmsg;
  2032. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  2033. while (datagrams < vlen) {
  2034. /*
  2035. * No need to ask LSM for more than the first datagram.
  2036. */
  2037. if (MSG_CMSG_COMPAT & flags) {
  2038. err = ___sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
  2039. &msg_sys, flags & ~MSG_WAITFORONE,
  2040. datagrams);
  2041. if (err < 0)
  2042. break;
  2043. err = __put_user(err, &compat_entry->msg_len);
  2044. ++compat_entry;
  2045. } else {
  2046. err = ___sys_recvmsg(sock,
  2047. (struct msghdr __user *)entry,
  2048. &msg_sys, flags & ~MSG_WAITFORONE,
  2049. datagrams);
  2050. if (err < 0)
  2051. break;
  2052. err = put_user(err, &entry->msg_len);
  2053. ++entry;
  2054. }
  2055. if (err)
  2056. break;
  2057. ++datagrams;
  2058. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  2059. if (flags & MSG_WAITFORONE)
  2060. flags |= MSG_DONTWAIT;
  2061. if (timeout) {
  2062. ktime_get_ts(timeout);
  2063. *timeout = timespec_sub(end_time, *timeout);
  2064. if (timeout->tv_sec < 0) {
  2065. timeout->tv_sec = timeout->tv_nsec = 0;
  2066. break;
  2067. }
  2068. /* Timeout, return less than vlen datagrams */
  2069. if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
  2070. break;
  2071. }
  2072. /* Out of band data, return right away */
  2073. if (msg_sys.msg_flags & MSG_OOB)
  2074. break;
  2075. }
  2076. if (err == 0)
  2077. goto out_put;
  2078. if (datagrams == 0) {
  2079. datagrams = err;
  2080. goto out_put;
  2081. }
  2082. /*
  2083. * We may return less entries than requested (vlen) if the
  2084. * sock is non block and there aren't enough datagrams...
  2085. */
  2086. if (err != -EAGAIN) {
  2087. /*
  2088. * ... or if recvmsg returns an error after we
  2089. * received some datagrams, where we record the
  2090. * error to return on the next call or if the
  2091. * app asks about it using getsockopt(SO_ERROR).
  2092. */
  2093. sock->sk->sk_err = -err;
  2094. }
  2095. if (err == 0)
  2096. goto out_put;
  2097. if (datagrams == 0) {
  2098. datagrams = err;
  2099. goto out_put;
  2100. }
  2101. /*
  2102. * We may return less entries than requested (vlen) if the
  2103. * sock is non block and there aren't enough datagrams...
  2104. */
  2105. if (err != -EAGAIN) {
  2106. /*
  2107. * ... or if recvmsg returns an error after we
  2108. * received some datagrams, where we record the
  2109. * error to return on the next call or if the
  2110. * app asks about it using getsockopt(SO_ERROR).
  2111. */
  2112. sock->sk->sk_err = -err;
  2113. }
  2114. out_put:
  2115. fput_light(sock->file, fput_needed);
  2116. return datagrams;
  2117. }
  2118. SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
  2119. unsigned int, vlen, unsigned int, flags,
  2120. struct timespec __user *, timeout)
  2121. {
  2122. int datagrams;
  2123. struct timespec timeout_sys;
  2124. if (flags & MSG_CMSG_COMPAT)
  2125. return -EINVAL;
  2126. if (!timeout)
  2127. return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
  2128. if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
  2129. return -EFAULT;
  2130. datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
  2131. if (datagrams > 0 &&
  2132. copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
  2133. datagrams = -EFAULT;
  2134. return datagrams;
  2135. }
  2136. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  2137. /* Argument list sizes for sys_socketcall */
  2138. #define AL(x) ((x) * sizeof(unsigned long))
  2139. static const unsigned char nargs[21] = {
  2140. AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
  2141. AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
  2142. AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
  2143. AL(4), AL(5), AL(4)
  2144. };
  2145. #undef AL
  2146. /*
  2147. * System call vectors.
  2148. *
  2149. * Argument checking cleaned up. Saved 20% in size.
  2150. * This function doesn't need to set the kernel lock because
  2151. * it is set by the callees.
  2152. */
  2153. SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
  2154. {
  2155. unsigned long a[6];
  2156. unsigned long a0, a1;
  2157. int err;
  2158. unsigned int len;
  2159. if (call < 1 || call > SYS_SENDMMSG)
  2160. return -EINVAL;
  2161. len = nargs[call];
  2162. if (len > sizeof(a))
  2163. return -EINVAL;
  2164. /* copy_from_user should be SMP safe. */
  2165. if (copy_from_user(a, args, len))
  2166. return -EFAULT;
  2167. audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  2168. a0 = a[0];
  2169. a1 = a[1];
  2170. switch (call) {
  2171. case SYS_SOCKET:
  2172. err = sys_socket(a0, a1, a[2]);
  2173. break;
  2174. case SYS_BIND:
  2175. err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  2176. break;
  2177. case SYS_CONNECT:
  2178. err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  2179. break;
  2180. case SYS_LISTEN:
  2181. err = sys_listen(a0, a1);
  2182. break;
  2183. case SYS_ACCEPT:
  2184. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  2185. (int __user *)a[2], 0);
  2186. break;
  2187. case SYS_GETSOCKNAME:
  2188. err =
  2189. sys_getsockname(a0, (struct sockaddr __user *)a1,
  2190. (int __user *)a[2]);
  2191. break;
  2192. case SYS_GETPEERNAME:
  2193. err =
  2194. sys_getpeername(a0, (struct sockaddr __user *)a1,
  2195. (int __user *)a[2]);
  2196. break;
  2197. case SYS_SOCKETPAIR:
  2198. err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  2199. break;
  2200. case SYS_SEND:
  2201. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  2202. break;
  2203. case SYS_SENDTO:
  2204. err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
  2205. (struct sockaddr __user *)a[4], a[5]);
  2206. break;
  2207. case SYS_RECV:
  2208. err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
  2209. break;
  2210. case SYS_RECVFROM:
  2211. err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  2212. (struct sockaddr __user *)a[4],
  2213. (int __user *)a[5]);
  2214. break;
  2215. case SYS_SHUTDOWN:
  2216. err = sys_shutdown(a0, a1);
  2217. break;
  2218. case SYS_SETSOCKOPT:
  2219. err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
  2220. break;
  2221. case SYS_GETSOCKOPT:
  2222. err =
  2223. sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  2224. (int __user *)a[4]);
  2225. break;
  2226. case SYS_SENDMSG:
  2227. err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
  2228. break;
  2229. case SYS_SENDMMSG:
  2230. err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
  2231. break;
  2232. case SYS_RECVMSG:
  2233. err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
  2234. break;
  2235. case SYS_RECVMMSG:
  2236. err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
  2237. (struct timespec __user *)a[4]);
  2238. break;
  2239. case SYS_ACCEPT4:
  2240. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  2241. (int __user *)a[2], a[3]);
  2242. break;
  2243. default:
  2244. err = -EINVAL;
  2245. break;
  2246. }
  2247. return err;
  2248. }
  2249. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  2250. /**
  2251. * sock_register - add a socket protocol handler
  2252. * @ops: description of protocol
  2253. *
  2254. * This function is called by a protocol handler that wants to
  2255. * advertise its address family, and have it linked into the
  2256. * socket interface. The value ops->family coresponds to the
  2257. * socket system call protocol family.
  2258. */
  2259. int sock_register(const struct net_proto_family *ops)
  2260. {
  2261. int err;
  2262. if (ops->family >= NPROTO) {
  2263. printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
  2264. NPROTO);
  2265. return -ENOBUFS;
  2266. }
  2267. spin_lock(&net_family_lock);
  2268. if (rcu_dereference_protected(net_families[ops->family],
  2269. lockdep_is_held(&net_family_lock)))
  2270. err = -EEXIST;
  2271. else {
  2272. rcu_assign_pointer(net_families[ops->family], ops);
  2273. err = 0;
  2274. }
  2275. spin_unlock(&net_family_lock);
  2276. printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
  2277. return err;
  2278. }
  2279. EXPORT_SYMBOL(sock_register);
  2280. /**
  2281. * sock_unregister - remove a protocol handler
  2282. * @family: protocol family to remove
  2283. *
  2284. * This function is called by a protocol handler that wants to
  2285. * remove its address family, and have it unlinked from the
  2286. * new socket creation.
  2287. *
  2288. * If protocol handler is a module, then it can use module reference
  2289. * counts to protect against new references. If protocol handler is not
  2290. * a module then it needs to provide its own protection in
  2291. * the ops->create routine.
  2292. */
  2293. void sock_unregister(int family)
  2294. {
  2295. BUG_ON(family < 0 || family >= NPROTO);
  2296. spin_lock(&net_family_lock);
  2297. RCU_INIT_POINTER(net_families[family], NULL);
  2298. spin_unlock(&net_family_lock);
  2299. synchronize_rcu();
  2300. printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
  2301. }
  2302. EXPORT_SYMBOL(sock_unregister);
  2303. static int __init sock_init(void)
  2304. {
  2305. int err;
  2306. /*
  2307. * Initialize sock SLAB cache.
  2308. */
  2309. sk_init();
  2310. /*
  2311. * Initialize skbuff SLAB cache
  2312. */
  2313. skb_init();
  2314. /*
  2315. * Initialize the protocols module.
  2316. */
  2317. init_inodecache();
  2318. err = register_filesystem(&sock_fs_type);
  2319. if (err)
  2320. goto out_fs;
  2321. sock_mnt = kern_mount(&sock_fs_type);
  2322. if (IS_ERR(sock_mnt)) {
  2323. err = PTR_ERR(sock_mnt);
  2324. goto out_mount;
  2325. }
  2326. /* The real protocol initialization is performed in later initcalls.
  2327. */
  2328. #ifdef CONFIG_NETFILTER
  2329. netfilter_init();
  2330. #endif
  2331. #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
  2332. skb_timestamping_init();
  2333. #endif
  2334. out:
  2335. return err;
  2336. out_mount:
  2337. unregister_filesystem(&sock_fs_type);
  2338. out_fs:
  2339. goto out;
  2340. }
  2341. core_initcall(sock_init); /* early initcall */
  2342. #ifdef CONFIG_PROC_FS
  2343. void socket_seq_show(struct seq_file *seq)
  2344. {
  2345. int cpu;
  2346. int counter = 0;
  2347. for_each_possible_cpu(cpu)
  2348. counter += per_cpu(sockets_in_use, cpu);
  2349. /* It can be negative, by the way. 8) */
  2350. if (counter < 0)
  2351. counter = 0;
  2352. seq_printf(seq, "sockets: used %d\n", counter);
  2353. }
  2354. #endif /* CONFIG_PROC_FS */
  2355. #ifdef CONFIG_COMPAT
  2356. static int do_siocgstamp(struct net *net, struct socket *sock,
  2357. unsigned int cmd, void __user *up)
  2358. {
  2359. mm_segment_t old_fs = get_fs();
  2360. struct timeval ktv;
  2361. int err;
  2362. set_fs(KERNEL_DS);
  2363. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
  2364. set_fs(old_fs);
  2365. if (!err)
  2366. err = compat_put_timeval(&ktv, up);
  2367. return err;
  2368. }
  2369. static int do_siocgstampns(struct net *net, struct socket *sock,
  2370. unsigned int cmd, void __user *up)
  2371. {
  2372. mm_segment_t old_fs = get_fs();
  2373. struct timespec kts;
  2374. int err;
  2375. set_fs(KERNEL_DS);
  2376. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
  2377. set_fs(old_fs);
  2378. if (!err)
  2379. err = compat_put_timespec(&kts, up);
  2380. return err;
  2381. }
  2382. static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
  2383. {
  2384. struct ifreq __user *uifr;
  2385. int err;
  2386. uifr = compat_alloc_user_space(sizeof(struct ifreq));
  2387. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2388. return -EFAULT;
  2389. err = dev_ioctl(net, SIOCGIFNAME, uifr);
  2390. if (err)
  2391. return err;
  2392. if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
  2393. return -EFAULT;
  2394. return 0;
  2395. }
  2396. static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
  2397. {
  2398. struct compat_ifconf ifc32;
  2399. struct ifconf ifc;
  2400. struct ifconf __user *uifc;
  2401. struct compat_ifreq __user *ifr32;
  2402. struct ifreq __user *ifr;
  2403. unsigned int i, j;
  2404. int err;
  2405. if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
  2406. return -EFAULT;
  2407. memset(&ifc, 0, sizeof(ifc));
  2408. if (ifc32.ifcbuf == 0) {
  2409. ifc32.ifc_len = 0;
  2410. ifc.ifc_len = 0;
  2411. ifc.ifc_req = NULL;
  2412. uifc = compat_alloc_user_space(sizeof(struct ifconf));
  2413. } else {
  2414. size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
  2415. sizeof(struct ifreq);
  2416. uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
  2417. ifc.ifc_len = len;
  2418. ifr = ifc.ifc_req = (void __user *)(uifc + 1);
  2419. ifr32 = compat_ptr(ifc32.ifcbuf);
  2420. for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
  2421. if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
  2422. return -EFAULT;
  2423. ifr++;
  2424. ifr32++;
  2425. }
  2426. }
  2427. if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
  2428. return -EFAULT;
  2429. err = dev_ioctl(net, SIOCGIFCONF, uifc);
  2430. if (err)
  2431. return err;
  2432. if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
  2433. return -EFAULT;
  2434. ifr = ifc.ifc_req;
  2435. ifr32 = compat_ptr(ifc32.ifcbuf);
  2436. for (i = 0, j = 0;
  2437. i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
  2438. i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
  2439. if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
  2440. return -EFAULT;
  2441. ifr32++;
  2442. ifr++;
  2443. }
  2444. if (ifc32.ifcbuf == 0) {
  2445. /* Translate from 64-bit structure multiple to
  2446. * a 32-bit one.
  2447. */
  2448. i = ifc.ifc_len;
  2449. i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
  2450. ifc32.ifc_len = i;
  2451. } else {
  2452. ifc32.ifc_len = i;
  2453. }
  2454. if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
  2455. return -EFAULT;
  2456. return 0;
  2457. }
  2458. static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
  2459. {
  2460. struct compat_ethtool_rxnfc __user *compat_rxnfc;
  2461. bool convert_in = false, convert_out = false;
  2462. size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
  2463. struct ethtool_rxnfc __user *rxnfc;
  2464. struct ifreq __user *ifr;
  2465. u32 rule_cnt = 0, actual_rule_cnt;
  2466. u32 ethcmd;
  2467. u32 data;
  2468. int ret;
  2469. if (get_user(data, &ifr32->ifr_ifru.ifru_data))
  2470. return -EFAULT;
  2471. compat_rxnfc = compat_ptr(data);
  2472. if (get_user(ethcmd, &compat_rxnfc->cmd))
  2473. return -EFAULT;
  2474. /* Most ethtool structures are defined without padding.
  2475. * Unfortunately struct ethtool_rxnfc is an exception.
  2476. */
  2477. switch (ethcmd) {
  2478. default:
  2479. break;
  2480. case ETHTOOL_GRXCLSRLALL:
  2481. /* Buffer size is variable */
  2482. if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
  2483. return -EFAULT;
  2484. if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
  2485. return -ENOMEM;
  2486. buf_size += rule_cnt * sizeof(u32);
  2487. /* fall through */
  2488. case ETHTOOL_GRXRINGS:
  2489. case ETHTOOL_GRXCLSRLCNT:
  2490. case ETHTOOL_GRXCLSRULE:
  2491. case ETHTOOL_SRXCLSRLINS:
  2492. convert_out = true;
  2493. /* fall through */
  2494. case ETHTOOL_SRXCLSRLDEL:
  2495. buf_size += sizeof(struct ethtool_rxnfc);
  2496. convert_in = true;
  2497. break;
  2498. }
  2499. ifr = compat_alloc_user_space(buf_size);
  2500. rxnfc = (void *)ifr + ALIGN(sizeof(struct ifreq), 8);
  2501. if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
  2502. return -EFAULT;
  2503. if (put_user(convert_in ? rxnfc : compat_ptr(data),
  2504. &ifr->ifr_ifru.ifru_data))
  2505. return -EFAULT;
  2506. if (convert_in) {
  2507. /* We expect there to be holes between fs.m_ext and
  2508. * fs.ring_cookie and at the end of fs, but nowhere else.
  2509. */
  2510. BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
  2511. sizeof(compat_rxnfc->fs.m_ext) !=
  2512. offsetof(struct ethtool_rxnfc, fs.m_ext) +
  2513. sizeof(rxnfc->fs.m_ext));
  2514. BUILD_BUG_ON(
  2515. offsetof(struct compat_ethtool_rxnfc, fs.location) -
  2516. offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
  2517. offsetof(struct ethtool_rxnfc, fs.location) -
  2518. offsetof(struct ethtool_rxnfc, fs.ring_cookie));
  2519. if (copy_in_user(rxnfc, compat_rxnfc,
  2520. (void *)(&rxnfc->fs.m_ext + 1) -
  2521. (void *)rxnfc) ||
  2522. copy_in_user(&rxnfc->fs.ring_cookie,
  2523. &compat_rxnfc->fs.ring_cookie,
  2524. (void *)(&rxnfc->fs.location + 1) -
  2525. (void *)&rxnfc->fs.ring_cookie) ||
  2526. copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
  2527. sizeof(rxnfc->rule_cnt)))
  2528. return -EFAULT;
  2529. }
  2530. ret = dev_ioctl(net, SIOCETHTOOL, ifr);
  2531. if (ret)
  2532. return ret;
  2533. if (convert_out) {
  2534. if (copy_in_user(compat_rxnfc, rxnfc,
  2535. (const void *)(&rxnfc->fs.m_ext + 1) -
  2536. (const void *)rxnfc) ||
  2537. copy_in_user(&compat_rxnfc->fs.ring_cookie,
  2538. &rxnfc->fs.ring_cookie,
  2539. (const void *)(&rxnfc->fs.location + 1) -
  2540. (const void *)&rxnfc->fs.ring_cookie) ||
  2541. copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
  2542. sizeof(rxnfc->rule_cnt)))
  2543. return -EFAULT;
  2544. if (ethcmd == ETHTOOL_GRXCLSRLALL) {
  2545. /* As an optimisation, we only copy the actual
  2546. * number of rules that the underlying
  2547. * function returned. Since Mallory might
  2548. * change the rule count in user memory, we
  2549. * check that it is less than the rule count
  2550. * originally given (as the user buffer size),
  2551. * which has been range-checked.
  2552. */
  2553. if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
  2554. return -EFAULT;
  2555. if (actual_rule_cnt < rule_cnt)
  2556. rule_cnt = actual_rule_cnt;
  2557. if (copy_in_user(&compat_rxnfc->rule_locs[0],
  2558. &rxnfc->rule_locs[0],
  2559. rule_cnt * sizeof(u32)))
  2560. return -EFAULT;
  2561. }
  2562. }
  2563. return 0;
  2564. }
  2565. static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
  2566. {
  2567. void __user *uptr;
  2568. compat_uptr_t uptr32;
  2569. struct ifreq __user *uifr;
  2570. uifr = compat_alloc_user_space(sizeof(*uifr));
  2571. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2572. return -EFAULT;
  2573. if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
  2574. return -EFAULT;
  2575. uptr = compat_ptr(uptr32);
  2576. if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
  2577. return -EFAULT;
  2578. return dev_ioctl(net, SIOCWANDEV, uifr);
  2579. }
  2580. static int bond_ioctl(struct net *net, unsigned int cmd,
  2581. struct compat_ifreq __user *ifr32)
  2582. {
  2583. struct ifreq kifr;
  2584. struct ifreq __user *uifr;
  2585. mm_segment_t old_fs;
  2586. int err;
  2587. u32 data;
  2588. void __user *datap;
  2589. switch (cmd) {
  2590. case SIOCBONDENSLAVE:
  2591. case SIOCBONDRELEASE:
  2592. case SIOCBONDSETHWADDR:
  2593. case SIOCBONDCHANGEACTIVE:
  2594. if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
  2595. return -EFAULT;
  2596. old_fs = get_fs();
  2597. set_fs(KERNEL_DS);
  2598. err = dev_ioctl(net, cmd,
  2599. (struct ifreq __user __force *) &kifr);
  2600. set_fs(old_fs);
  2601. return err;
  2602. case SIOCBONDSLAVEINFOQUERY:
  2603. case SIOCBONDINFOQUERY:
  2604. uifr = compat_alloc_user_space(sizeof(*uifr));
  2605. if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
  2606. return -EFAULT;
  2607. if (get_user(data, &ifr32->ifr_ifru.ifru_data))
  2608. return -EFAULT;
  2609. datap = compat_ptr(data);
  2610. if (put_user(datap, &uifr->ifr_ifru.ifru_data))
  2611. return -EFAULT;
  2612. return dev_ioctl(net, cmd, uifr);
  2613. default:
  2614. return -ENOIOCTLCMD;
  2615. }
  2616. }
  2617. static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
  2618. struct compat_ifreq __user *u_ifreq32)
  2619. {
  2620. struct ifreq __user *u_ifreq64;
  2621. char tmp_buf[IFNAMSIZ];
  2622. void __user *data64;
  2623. u32 data32;
  2624. if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
  2625. IFNAMSIZ))
  2626. return -EFAULT;
  2627. if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
  2628. return -EFAULT;
  2629. data64 = compat_ptr(data32);
  2630. u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
  2631. /* Don't check these user accesses, just let that get trapped
  2632. * in the ioctl handler instead.
  2633. */
  2634. if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
  2635. IFNAMSIZ))
  2636. return -EFAULT;
  2637. if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
  2638. return -EFAULT;
  2639. return dev_ioctl(net, cmd, u_ifreq64);
  2640. }
  2641. static int dev_ifsioc(struct net *net, struct socket *sock,
  2642. unsigned int cmd, struct compat_ifreq __user *uifr32)
  2643. {
  2644. struct ifreq __user *uifr;
  2645. int err;
  2646. uifr = compat_alloc_user_space(sizeof(*uifr));
  2647. if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
  2648. return -EFAULT;
  2649. err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
  2650. if (!err) {
  2651. switch (cmd) {
  2652. case SIOCGIFFLAGS:
  2653. case SIOCGIFMETRIC:
  2654. case SIOCGIFMTU:
  2655. case SIOCGIFMEM:
  2656. case SIOCGIFHWADDR:
  2657. case SIOCGIFINDEX:
  2658. case SIOCGIFADDR:
  2659. case SIOCGIFBRDADDR:
  2660. case SIOCGIFDSTADDR:
  2661. case SIOCGIFNETMASK:
  2662. case SIOCGIFPFLAGS:
  2663. case SIOCGIFTXQLEN:
  2664. case SIOCGMIIPHY:
  2665. case SIOCGMIIREG:
  2666. if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
  2667. err = -EFAULT;
  2668. break;
  2669. }
  2670. }
  2671. return err;
  2672. }
  2673. static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
  2674. struct compat_ifreq __user *uifr32)
  2675. {
  2676. struct ifreq ifr;
  2677. struct compat_ifmap __user *uifmap32;
  2678. mm_segment_t old_fs;
  2679. int err;
  2680. uifmap32 = &uifr32->ifr_ifru.ifru_map;
  2681. err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
  2682. err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
  2683. err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
  2684. err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
  2685. err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
  2686. err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
  2687. err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
  2688. if (err)
  2689. return -EFAULT;
  2690. old_fs = get_fs();
  2691. set_fs(KERNEL_DS);
  2692. err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
  2693. set_fs(old_fs);
  2694. if (cmd == SIOCGIFMAP && !err) {
  2695. err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
  2696. err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
  2697. err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
  2698. err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
  2699. err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
  2700. err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
  2701. err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
  2702. if (err)
  2703. err = -EFAULT;
  2704. }
  2705. return err;
  2706. }
  2707. static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
  2708. {
  2709. void __user *uptr;
  2710. compat_uptr_t uptr32;
  2711. struct ifreq __user *uifr;
  2712. uifr = compat_alloc_user_space(sizeof(*uifr));
  2713. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2714. return -EFAULT;
  2715. if (get_user(uptr32, &uifr32->ifr_data))
  2716. return -EFAULT;
  2717. uptr = compat_ptr(uptr32);
  2718. if (put_user(uptr, &uifr->ifr_data))
  2719. return -EFAULT;
  2720. return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
  2721. }
  2722. struct rtentry32 {
  2723. u32 rt_pad1;
  2724. struct sockaddr rt_dst; /* target address */
  2725. struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
  2726. struct sockaddr rt_genmask; /* target network mask (IP) */
  2727. unsigned short rt_flags;
  2728. short rt_pad2;
  2729. u32 rt_pad3;
  2730. unsigned char rt_tos;
  2731. unsigned char rt_class;
  2732. short rt_pad4;
  2733. short rt_metric; /* +1 for binary compatibility! */
  2734. /* char * */ u32 rt_dev; /* forcing the device at add */
  2735. u32 rt_mtu; /* per route MTU/Window */
  2736. u32 rt_window; /* Window clamping */
  2737. unsigned short rt_irtt; /* Initial RTT */
  2738. };
  2739. struct in6_rtmsg32 {
  2740. struct in6_addr rtmsg_dst;
  2741. struct in6_addr rtmsg_src;
  2742. struct in6_addr rtmsg_gateway;
  2743. u32 rtmsg_type;
  2744. u16 rtmsg_dst_len;
  2745. u16 rtmsg_src_len;
  2746. u32 rtmsg_metric;
  2747. u32 rtmsg_info;
  2748. u32 rtmsg_flags;
  2749. s32 rtmsg_ifindex;
  2750. };
  2751. static int routing_ioctl(struct net *net, struct socket *sock,
  2752. unsigned int cmd, void __user *argp)
  2753. {
  2754. int ret;
  2755. void *r = NULL;
  2756. struct in6_rtmsg r6;
  2757. struct rtentry r4;
  2758. char devname[16];
  2759. u32 rtdev;
  2760. mm_segment_t old_fs = get_fs();
  2761. if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
  2762. struct in6_rtmsg32 __user *ur6 = argp;
  2763. ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
  2764. 3 * sizeof(struct in6_addr));
  2765. ret |= __get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
  2766. ret |= __get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
  2767. ret |= __get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
  2768. ret |= __get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
  2769. ret |= __get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
  2770. ret |= __get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
  2771. ret |= __get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
  2772. r = (void *) &r6;
  2773. } else { /* ipv4 */
  2774. struct rtentry32 __user *ur4 = argp;
  2775. ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
  2776. 3 * sizeof(struct sockaddr));
  2777. ret |= __get_user(r4.rt_flags, &(ur4->rt_flags));
  2778. ret |= __get_user(r4.rt_metric, &(ur4->rt_metric));
  2779. ret |= __get_user(r4.rt_mtu, &(ur4->rt_mtu));
  2780. ret |= __get_user(r4.rt_window, &(ur4->rt_window));
  2781. ret |= __get_user(r4.rt_irtt, &(ur4->rt_irtt));
  2782. ret |= __get_user(rtdev, &(ur4->rt_dev));
  2783. if (rtdev) {
  2784. ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
  2785. r4.rt_dev = (char __user __force *)devname;
  2786. devname[15] = 0;
  2787. } else
  2788. r4.rt_dev = NULL;
  2789. r = (void *) &r4;
  2790. }
  2791. if (ret) {
  2792. ret = -EFAULT;
  2793. goto out;
  2794. }
  2795. set_fs(KERNEL_DS);
  2796. ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
  2797. set_fs(old_fs);
  2798. out:
  2799. return ret;
  2800. }
  2801. /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
  2802. * for some operations; this forces use of the newer bridge-utils that
  2803. * use compatible ioctls
  2804. */
  2805. static int old_bridge_ioctl(compat_ulong_t __user *argp)
  2806. {
  2807. compat_ulong_t tmp;
  2808. if (get_user(tmp, argp))
  2809. return -EFAULT;
  2810. if (tmp == BRCTL_GET_VERSION)
  2811. return BRCTL_VERSION + 1;
  2812. return -EINVAL;
  2813. }
  2814. static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
  2815. unsigned int cmd, unsigned long arg)
  2816. {
  2817. void __user *argp = compat_ptr(arg);
  2818. struct sock *sk = sock->sk;
  2819. struct net *net = sock_net(sk);
  2820. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
  2821. return siocdevprivate_ioctl(net, cmd, argp);
  2822. switch (cmd) {
  2823. case SIOCSIFBR:
  2824. case SIOCGIFBR:
  2825. return old_bridge_ioctl(argp);
  2826. case SIOCGIFNAME:
  2827. return dev_ifname32(net, argp);
  2828. case SIOCGIFCONF:
  2829. return dev_ifconf(net, argp);
  2830. case SIOCETHTOOL:
  2831. return ethtool_ioctl(net, argp);
  2832. case SIOCWANDEV:
  2833. return compat_siocwandev(net, argp);
  2834. case SIOCGIFMAP:
  2835. case SIOCSIFMAP:
  2836. return compat_sioc_ifmap(net, cmd, argp);
  2837. case SIOCBONDENSLAVE:
  2838. case SIOCBONDRELEASE:
  2839. case SIOCBONDSETHWADDR:
  2840. case SIOCBONDSLAVEINFOQUERY:
  2841. case SIOCBONDINFOQUERY:
  2842. case SIOCBONDCHANGEACTIVE:
  2843. return bond_ioctl(net, cmd, argp);
  2844. case SIOCADDRT:
  2845. case SIOCDELRT:
  2846. return routing_ioctl(net, sock, cmd, argp);
  2847. case SIOCGSTAMP:
  2848. return do_siocgstamp(net, sock, cmd, argp);
  2849. case SIOCGSTAMPNS:
  2850. return do_siocgstampns(net, sock, cmd, argp);
  2851. case SIOCSHWTSTAMP:
  2852. return compat_siocshwtstamp(net, argp);
  2853. case FIOSETOWN:
  2854. case SIOCSPGRP:
  2855. case FIOGETOWN:
  2856. case SIOCGPGRP:
  2857. case SIOCBRADDBR:
  2858. case SIOCBRDELBR:
  2859. case SIOCGIFVLAN:
  2860. case SIOCSIFVLAN:
  2861. case SIOCADDDLCI:
  2862. case SIOCDELDLCI:
  2863. return sock_ioctl(file, cmd, arg);
  2864. case SIOCGIFFLAGS:
  2865. case SIOCSIFFLAGS:
  2866. case SIOCGIFMETRIC:
  2867. case SIOCSIFMETRIC:
  2868. case SIOCGIFMTU:
  2869. case SIOCSIFMTU:
  2870. case SIOCGIFMEM:
  2871. case SIOCSIFMEM:
  2872. case SIOCGIFHWADDR:
  2873. case SIOCSIFHWADDR:
  2874. case SIOCADDMULTI:
  2875. case SIOCDELMULTI:
  2876. case SIOCGIFINDEX:
  2877. case SIOCGIFADDR:
  2878. case SIOCSIFADDR:
  2879. case SIOCSIFHWBROADCAST:
  2880. case SIOCDIFADDR:
  2881. case SIOCGIFBRDADDR:
  2882. case SIOCSIFBRDADDR:
  2883. case SIOCGIFDSTADDR:
  2884. case SIOCSIFDSTADDR:
  2885. case SIOCGIFNETMASK:
  2886. case SIOCSIFNETMASK:
  2887. case SIOCSIFPFLAGS:
  2888. case SIOCGIFPFLAGS:
  2889. case SIOCGIFTXQLEN:
  2890. case SIOCSIFTXQLEN:
  2891. case SIOCBRADDIF:
  2892. case SIOCBRDELIF:
  2893. case SIOCSIFNAME:
  2894. case SIOCGMIIPHY:
  2895. case SIOCGMIIREG:
  2896. case SIOCSMIIREG:
  2897. return dev_ifsioc(net, sock, cmd, argp);
  2898. case SIOCSARP:
  2899. case SIOCGARP:
  2900. case SIOCDARP:
  2901. case SIOCATMARK:
  2902. return sock_do_ioctl(net, sock, cmd, arg);
  2903. }
  2904. return -ENOIOCTLCMD;
  2905. }
  2906. static long compat_sock_ioctl(struct file *file, unsigned int cmd,
  2907. unsigned long arg)
  2908. {
  2909. struct socket *sock = file->private_data;
  2910. int ret = -ENOIOCTLCMD;
  2911. struct sock *sk;
  2912. struct net *net;
  2913. sk = sock->sk;
  2914. net = sock_net(sk);
  2915. if (sock->ops->compat_ioctl)
  2916. ret = sock->ops->compat_ioctl(sock, cmd, arg);
  2917. if (ret == -ENOIOCTLCMD &&
  2918. (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
  2919. ret = compat_wext_handle_ioctl(net, cmd, arg);
  2920. if (ret == -ENOIOCTLCMD)
  2921. ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
  2922. return ret;
  2923. }
  2924. #endif
  2925. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
  2926. {
  2927. return sock->ops->bind(sock, addr, addrlen);
  2928. }
  2929. EXPORT_SYMBOL(kernel_bind);
  2930. int kernel_listen(struct socket *sock, int backlog)
  2931. {
  2932. return sock->ops->listen(sock, backlog);
  2933. }
  2934. EXPORT_SYMBOL(kernel_listen);
  2935. int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
  2936. {
  2937. struct sock *sk = sock->sk;
  2938. int err;
  2939. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  2940. newsock);
  2941. if (err < 0)
  2942. goto done;
  2943. err = sock->ops->accept(sock, *newsock, flags);
  2944. if (err < 0) {
  2945. sock_release(*newsock);
  2946. *newsock = NULL;
  2947. goto done;
  2948. }
  2949. (*newsock)->ops = sock->ops;
  2950. __module_get((*newsock)->ops->owner);
  2951. done:
  2952. return err;
  2953. }
  2954. EXPORT_SYMBOL(kernel_accept);
  2955. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  2956. int flags)
  2957. {
  2958. return sock->ops->connect(sock, addr, addrlen, flags);
  2959. }
  2960. EXPORT_SYMBOL(kernel_connect);
  2961. int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
  2962. int *addrlen)
  2963. {
  2964. return sock->ops->getname(sock, addr, addrlen, 0);
  2965. }
  2966. EXPORT_SYMBOL(kernel_getsockname);
  2967. int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
  2968. int *addrlen)
  2969. {
  2970. return sock->ops->getname(sock, addr, addrlen, 1);
  2971. }
  2972. EXPORT_SYMBOL(kernel_getpeername);
  2973. int kernel_getsockopt(struct socket *sock, int level, int optname,
  2974. char *optval, int *optlen)
  2975. {
  2976. mm_segment_t oldfs = get_fs();
  2977. char __user *uoptval;
  2978. int __user *uoptlen;
  2979. int err;
  2980. uoptval = (char __user __force *) optval;
  2981. uoptlen = (int __user __force *) optlen;
  2982. set_fs(KERNEL_DS);
  2983. if (level == SOL_SOCKET)
  2984. err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
  2985. else
  2986. err = sock->ops->getsockopt(sock, level, optname, uoptval,
  2987. uoptlen);
  2988. set_fs(oldfs);
  2989. return err;
  2990. }
  2991. EXPORT_SYMBOL(kernel_getsockopt);
  2992. int kernel_setsockopt(struct socket *sock, int level, int optname,
  2993. char *optval, unsigned int optlen)
  2994. {
  2995. mm_segment_t oldfs = get_fs();
  2996. char __user *uoptval;
  2997. int err;
  2998. uoptval = (char __user __force *) optval;
  2999. set_fs(KERNEL_DS);
  3000. if (level == SOL_SOCKET)
  3001. err = sock_setsockopt(sock, level, optname, uoptval, optlen);
  3002. else
  3003. err = sock->ops->setsockopt(sock, level, optname, uoptval,
  3004. optlen);
  3005. set_fs(oldfs);
  3006. return err;
  3007. }
  3008. EXPORT_SYMBOL(kernel_setsockopt);
  3009. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  3010. size_t size, int flags)
  3011. {
  3012. sock_update_classid(sock->sk, current);
  3013. if (sock->ops->sendpage)
  3014. return sock->ops->sendpage(sock, page, offset, size, flags);
  3015. return sock_no_sendpage(sock, page, offset, size, flags);
  3016. }
  3017. EXPORT_SYMBOL(kernel_sendpage);
  3018. int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
  3019. {
  3020. mm_segment_t oldfs = get_fs();
  3021. int err;
  3022. set_fs(KERNEL_DS);
  3023. err = sock->ops->ioctl(sock, cmd, arg);
  3024. set_fs(oldfs);
  3025. return err;
  3026. }
  3027. EXPORT_SYMBOL(kernel_sock_ioctl);
  3028. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
  3029. {
  3030. return sock->ops->shutdown(sock, how);
  3031. }
  3032. EXPORT_SYMBOL(kernel_sock_shutdown);
  3033. int sockev_register_notify(struct notifier_block *nb)
  3034. {
  3035. return blocking_notifier_chain_register(&sockev_notifier_list, nb);
  3036. }
  3037. EXPORT_SYMBOL(sockev_register_notify);
  3038. int sockev_unregister_notify(struct notifier_block *nb)
  3039. {
  3040. return blocking_notifier_chain_unregister(&sockev_notifier_list, nb);
  3041. }
  3042. EXPORT_SYMBOL(sockev_unregister_notify);