socket.c 75 KB

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