socket.c 74 KB

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