socket.c 74 KB

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