socket.c 74 KB

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