socket.c 75 KB

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