socket.c 83 KB

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