socket.c 83 KB

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