socket.c 82 KB

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