socket.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461
  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 <asm/uaccess.h>
  90. #include <asm/unistd.h>
  91. #include <net/compat.h>
  92. #include <net/wext.h>
  93. #include <net/sock.h>
  94. #include <linux/netfilter.h>
  95. static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
  96. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  97. unsigned long nr_segs, loff_t pos);
  98. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  99. unsigned long nr_segs, loff_t pos);
  100. static int sock_mmap(struct file *file, struct vm_area_struct *vma);
  101. static int sock_close(struct inode *inode, struct file *file);
  102. static unsigned int sock_poll(struct file *file,
  103. struct poll_table_struct *wait);
  104. static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  105. #ifdef CONFIG_COMPAT
  106. static long compat_sock_ioctl(struct file *file,
  107. unsigned int cmd, unsigned long arg);
  108. #endif
  109. static int sock_fasync(int fd, struct file *filp, int on);
  110. static ssize_t sock_sendpage(struct file *file, struct page *page,
  111. int offset, size_t size, loff_t *ppos, int more);
  112. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  113. struct pipe_inode_info *pipe, size_t len,
  114. unsigned int flags);
  115. /*
  116. * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
  117. * in the operation structures but are done directly via the socketcall() multiplexor.
  118. */
  119. static const struct file_operations socket_file_ops = {
  120. .owner = THIS_MODULE,
  121. .llseek = no_llseek,
  122. .aio_read = sock_aio_read,
  123. .aio_write = sock_aio_write,
  124. .poll = sock_poll,
  125. .unlocked_ioctl = sock_ioctl,
  126. #ifdef CONFIG_COMPAT
  127. .compat_ioctl = compat_sock_ioctl,
  128. #endif
  129. .mmap = sock_mmap,
  130. .open = sock_no_open, /* special open code to disallow open via /proc */
  131. .release = sock_close,
  132. .fasync = sock_fasync,
  133. .sendpage = sock_sendpage,
  134. .splice_write = generic_splice_sendpage,
  135. .splice_read = sock_splice_read,
  136. };
  137. /*
  138. * The protocol list. Each protocol is registered in here.
  139. */
  140. static DEFINE_SPINLOCK(net_family_lock);
  141. static const struct net_proto_family *net_families[NPROTO] __read_mostly;
  142. /*
  143. * Statistics counters of the socket lists
  144. */
  145. static DEFINE_PER_CPU(int, sockets_in_use) = 0;
  146. /*
  147. * Support routines.
  148. * Move socket addresses back and forth across the kernel/user
  149. * divide and look after the messy bits.
  150. */
  151. #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
  152. 16 for IP, 16 for IPX,
  153. 24 for IPv6,
  154. about 80 for AX.25
  155. must be at least one bigger than
  156. the AF_UNIX size (see net/unix/af_unix.c
  157. :unix_mkname()).
  158. */
  159. /**
  160. * move_addr_to_kernel - copy a socket address into kernel space
  161. * @uaddr: Address in user space
  162. * @kaddr: Address in kernel space
  163. * @ulen: Length in user space
  164. *
  165. * The address is copied into kernel space. If the provided address is
  166. * too long an error code of -EINVAL is returned. If the copy gives
  167. * invalid addresses -EFAULT is returned. On a success 0 is returned.
  168. */
  169. int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
  170. {
  171. if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
  172. return -EINVAL;
  173. if (ulen == 0)
  174. return 0;
  175. if (copy_from_user(kaddr, uaddr, ulen))
  176. return -EFAULT;
  177. return audit_sockaddr(ulen, kaddr);
  178. }
  179. /**
  180. * move_addr_to_user - copy an address to user space
  181. * @kaddr: kernel space address
  182. * @klen: length of address in kernel
  183. * @uaddr: user space address
  184. * @ulen: pointer to user length field
  185. *
  186. * The value pointed to by ulen on entry is the buffer length available.
  187. * This is overwritten with the buffer space used. -EINVAL is returned
  188. * if an overlong buffer is specified or a negative buffer size. -EFAULT
  189. * is returned if either the buffer or the length field are not
  190. * accessible.
  191. * After copying the data up to the limit the user specifies, the true
  192. * length of the data is written over the length limit the user
  193. * specified. Zero is returned for a success.
  194. */
  195. int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
  196. int __user *ulen)
  197. {
  198. int err;
  199. int len;
  200. err = get_user(len, ulen);
  201. if (err)
  202. return err;
  203. if (len > klen)
  204. len = klen;
  205. if (len < 0 || len > sizeof(struct sockaddr_storage))
  206. return -EINVAL;
  207. if (len) {
  208. if (audit_sockaddr(klen, kaddr))
  209. return -ENOMEM;
  210. if (copy_to_user(uaddr, kaddr, len))
  211. return -EFAULT;
  212. }
  213. /*
  214. * "fromlen shall refer to the value before truncation.."
  215. * 1003.1g
  216. */
  217. return __put_user(klen, ulen);
  218. }
  219. static struct kmem_cache *sock_inode_cachep __read_mostly;
  220. static struct inode *sock_alloc_inode(struct super_block *sb)
  221. {
  222. struct socket_alloc *ei;
  223. ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
  224. if (!ei)
  225. return NULL;
  226. init_waitqueue_head(&ei->socket.wait);
  227. ei->socket.fasync_list = NULL;
  228. ei->socket.state = SS_UNCONNECTED;
  229. ei->socket.flags = 0;
  230. ei->socket.ops = NULL;
  231. ei->socket.sk = NULL;
  232. ei->socket.file = NULL;
  233. return &ei->vfs_inode;
  234. }
  235. static void sock_destroy_inode(struct inode *inode)
  236. {
  237. kmem_cache_free(sock_inode_cachep,
  238. container_of(inode, struct socket_alloc, vfs_inode));
  239. }
  240. static void init_once(void *foo)
  241. {
  242. struct socket_alloc *ei = (struct socket_alloc *)foo;
  243. inode_init_once(&ei->vfs_inode);
  244. }
  245. static int init_inodecache(void)
  246. {
  247. sock_inode_cachep = kmem_cache_create("sock_inode_cache",
  248. sizeof(struct socket_alloc),
  249. 0,
  250. (SLAB_HWCACHE_ALIGN |
  251. SLAB_RECLAIM_ACCOUNT |
  252. SLAB_MEM_SPREAD),
  253. init_once);
  254. if (sock_inode_cachep == NULL)
  255. return -ENOMEM;
  256. return 0;
  257. }
  258. static const struct super_operations sockfs_ops = {
  259. .alloc_inode = sock_alloc_inode,
  260. .destroy_inode =sock_destroy_inode,
  261. .statfs = simple_statfs,
  262. };
  263. static int sockfs_get_sb(struct file_system_type *fs_type,
  264. int flags, const char *dev_name, void *data,
  265. struct vfsmount *mnt)
  266. {
  267. return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
  268. mnt);
  269. }
  270. static struct vfsmount *sock_mnt __read_mostly;
  271. static struct file_system_type sock_fs_type = {
  272. .name = "sockfs",
  273. .get_sb = sockfs_get_sb,
  274. .kill_sb = kill_anon_super,
  275. };
  276. static int sockfs_delete_dentry(struct dentry *dentry)
  277. {
  278. /*
  279. * At creation time, we pretended this dentry was hashed
  280. * (by clearing DCACHE_UNHASHED bit in d_flags)
  281. * At delete time, we restore the truth : not hashed.
  282. * (so that dput() can proceed correctly)
  283. */
  284. dentry->d_flags |= DCACHE_UNHASHED;
  285. return 0;
  286. }
  287. /*
  288. * sockfs_dname() is called from d_path().
  289. */
  290. static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
  291. {
  292. return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
  293. dentry->d_inode->i_ino);
  294. }
  295. static const struct dentry_operations sockfs_dentry_operations = {
  296. .d_delete = sockfs_delete_dentry,
  297. .d_dname = sockfs_dname,
  298. };
  299. /*
  300. * Obtains the first available file descriptor and sets it up for use.
  301. *
  302. * These functions create file structures and maps them to fd space
  303. * of the current process. On success it returns file descriptor
  304. * and file struct implicitly stored in sock->file.
  305. * Note that another thread may close file descriptor before we return
  306. * from this function. We use the fact that now we do not refer
  307. * to socket after mapping. If one day we will need it, this
  308. * function will increment ref. count on file by 1.
  309. *
  310. * In any case returned fd MAY BE not valid!
  311. * This race condition is unavoidable
  312. * with shared fd spaces, we cannot solve it inside kernel,
  313. * but we take care of internal coherence yet.
  314. */
  315. static int sock_alloc_fd(struct file **filep, int flags)
  316. {
  317. int fd;
  318. fd = get_unused_fd_flags(flags);
  319. if (likely(fd >= 0)) {
  320. struct file *file = get_empty_filp();
  321. *filep = file;
  322. if (unlikely(!file)) {
  323. put_unused_fd(fd);
  324. return -ENFILE;
  325. }
  326. } else
  327. *filep = NULL;
  328. return fd;
  329. }
  330. static int sock_attach_fd(struct socket *sock, struct file *file, int flags)
  331. {
  332. struct dentry *dentry;
  333. struct qstr name = { .name = "" };
  334. dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
  335. if (unlikely(!dentry))
  336. return -ENOMEM;
  337. dentry->d_op = &sockfs_dentry_operations;
  338. /*
  339. * We dont want to push this dentry into global dentry hash table.
  340. * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
  341. * This permits a working /proc/$pid/fd/XXX on sockets
  342. */
  343. dentry->d_flags &= ~DCACHE_UNHASHED;
  344. d_instantiate(dentry, SOCK_INODE(sock));
  345. sock->file = file;
  346. init_file(file, sock_mnt, dentry, FMODE_READ | FMODE_WRITE,
  347. &socket_file_ops);
  348. SOCK_INODE(sock)->i_fop = &socket_file_ops;
  349. file->f_flags = O_RDWR | (flags & O_NONBLOCK);
  350. file->f_pos = 0;
  351. file->private_data = sock;
  352. return 0;
  353. }
  354. int sock_map_fd(struct socket *sock, int flags)
  355. {
  356. struct file *newfile;
  357. int fd = sock_alloc_fd(&newfile, flags);
  358. if (likely(fd >= 0)) {
  359. int err = sock_attach_fd(sock, newfile, flags);
  360. if (unlikely(err < 0)) {
  361. put_filp(newfile);
  362. put_unused_fd(fd);
  363. return err;
  364. }
  365. fd_install(fd, newfile);
  366. }
  367. return fd;
  368. }
  369. static struct socket *sock_from_file(struct file *file, int *err)
  370. {
  371. if (file->f_op == &socket_file_ops)
  372. return file->private_data; /* set in sock_map_fd */
  373. *err = -ENOTSOCK;
  374. return NULL;
  375. }
  376. /**
  377. * sockfd_lookup - Go from a file number to its socket slot
  378. * @fd: file handle
  379. * @err: pointer to an error code return
  380. *
  381. * The file handle passed in is locked and the socket it is bound
  382. * too is returned. If an error occurs the err pointer is overwritten
  383. * with a negative errno code and NULL is returned. The function checks
  384. * for both invalid handles and passing a handle which is not a socket.
  385. *
  386. * On a success the socket object pointer is returned.
  387. */
  388. struct socket *sockfd_lookup(int fd, int *err)
  389. {
  390. struct file *file;
  391. struct socket *sock;
  392. file = fget(fd);
  393. if (!file) {
  394. *err = -EBADF;
  395. return NULL;
  396. }
  397. sock = sock_from_file(file, err);
  398. if (!sock)
  399. fput(file);
  400. return sock;
  401. }
  402. static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
  403. {
  404. struct file *file;
  405. struct socket *sock;
  406. *err = -EBADF;
  407. file = fget_light(fd, fput_needed);
  408. if (file) {
  409. sock = sock_from_file(file, err);
  410. if (sock)
  411. return sock;
  412. fput_light(file, *fput_needed);
  413. }
  414. return NULL;
  415. }
  416. /**
  417. * sock_alloc - allocate a socket
  418. *
  419. * Allocate a new inode and socket object. The two are bound together
  420. * and initialised. The socket is then returned. If we are out of inodes
  421. * NULL is returned.
  422. */
  423. static struct socket *sock_alloc(void)
  424. {
  425. struct inode *inode;
  426. struct socket *sock;
  427. inode = new_inode(sock_mnt->mnt_sb);
  428. if (!inode)
  429. return NULL;
  430. sock = SOCKET_I(inode);
  431. kmemcheck_annotate_bitfield(sock, type);
  432. inode->i_mode = S_IFSOCK | S_IRWXUGO;
  433. inode->i_uid = current_fsuid();
  434. inode->i_gid = current_fsgid();
  435. percpu_add(sockets_in_use, 1);
  436. return sock;
  437. }
  438. /*
  439. * In theory you can't get an open on this inode, but /proc provides
  440. * a back door. Remember to keep it shut otherwise you'll let the
  441. * creepy crawlies in.
  442. */
  443. static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
  444. {
  445. return -ENXIO;
  446. }
  447. const struct file_operations bad_sock_fops = {
  448. .owner = THIS_MODULE,
  449. .open = sock_no_open,
  450. };
  451. /**
  452. * sock_release - close a socket
  453. * @sock: socket to close
  454. *
  455. * The socket is released from the protocol stack if it has a release
  456. * callback, and the inode is then released if the socket is bound to
  457. * an inode not a file.
  458. */
  459. void sock_release(struct socket *sock)
  460. {
  461. if (sock->ops) {
  462. struct module *owner = sock->ops->owner;
  463. sock->ops->release(sock);
  464. sock->ops = NULL;
  465. module_put(owner);
  466. }
  467. if (sock->fasync_list)
  468. printk(KERN_ERR "sock_release: fasync list not empty!\n");
  469. percpu_sub(sockets_in_use, 1);
  470. if (!sock->file) {
  471. iput(SOCK_INODE(sock));
  472. return;
  473. }
  474. sock->file = NULL;
  475. }
  476. int sock_tx_timestamp(struct msghdr *msg, struct sock *sk,
  477. union skb_shared_tx *shtx)
  478. {
  479. shtx->flags = 0;
  480. if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
  481. shtx->hardware = 1;
  482. if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
  483. shtx->software = 1;
  484. return 0;
  485. }
  486. EXPORT_SYMBOL(sock_tx_timestamp);
  487. static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
  488. struct msghdr *msg, size_t size)
  489. {
  490. struct sock_iocb *si = kiocb_to_siocb(iocb);
  491. int err;
  492. si->sock = sock;
  493. si->scm = NULL;
  494. si->msg = msg;
  495. si->size = size;
  496. err = security_socket_sendmsg(sock, msg, size);
  497. if (err)
  498. return err;
  499. return sock->ops->sendmsg(iocb, sock, msg, size);
  500. }
  501. int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  502. {
  503. struct kiocb iocb;
  504. struct sock_iocb siocb;
  505. int ret;
  506. init_sync_kiocb(&iocb, NULL);
  507. iocb.private = &siocb;
  508. ret = __sock_sendmsg(&iocb, sock, msg, size);
  509. if (-EIOCBQUEUED == ret)
  510. ret = wait_on_sync_kiocb(&iocb);
  511. return ret;
  512. }
  513. int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
  514. struct kvec *vec, size_t num, size_t size)
  515. {
  516. mm_segment_t oldfs = get_fs();
  517. int result;
  518. set_fs(KERNEL_DS);
  519. /*
  520. * the following is safe, since for compiler definitions of kvec and
  521. * iovec are identical, yielding the same in-core layout and alignment
  522. */
  523. msg->msg_iov = (struct iovec *)vec;
  524. msg->msg_iovlen = num;
  525. result = sock_sendmsg(sock, msg, size);
  526. set_fs(oldfs);
  527. return result;
  528. }
  529. static int ktime2ts(ktime_t kt, struct timespec *ts)
  530. {
  531. if (kt.tv64) {
  532. *ts = ktime_to_timespec(kt);
  533. return 1;
  534. } else {
  535. return 0;
  536. }
  537. }
  538. /*
  539. * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
  540. */
  541. void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
  542. struct sk_buff *skb)
  543. {
  544. int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
  545. struct timespec ts[3];
  546. int empty = 1;
  547. struct skb_shared_hwtstamps *shhwtstamps =
  548. skb_hwtstamps(skb);
  549. /* Race occurred between timestamp enabling and packet
  550. receiving. Fill in the current time for now. */
  551. if (need_software_tstamp && skb->tstamp.tv64 == 0)
  552. __net_timestamp(skb);
  553. if (need_software_tstamp) {
  554. if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
  555. struct timeval tv;
  556. skb_get_timestamp(skb, &tv);
  557. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
  558. sizeof(tv), &tv);
  559. } else {
  560. struct timespec ts;
  561. skb_get_timestampns(skb, &ts);
  562. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
  563. sizeof(ts), &ts);
  564. }
  565. }
  566. memset(ts, 0, sizeof(ts));
  567. if (skb->tstamp.tv64 &&
  568. sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
  569. skb_get_timestampns(skb, ts + 0);
  570. empty = 0;
  571. }
  572. if (shhwtstamps) {
  573. if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
  574. ktime2ts(shhwtstamps->syststamp, ts + 1))
  575. empty = 0;
  576. if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
  577. ktime2ts(shhwtstamps->hwtstamp, ts + 2))
  578. empty = 0;
  579. }
  580. if (!empty)
  581. put_cmsg(msg, SOL_SOCKET,
  582. SCM_TIMESTAMPING, sizeof(ts), &ts);
  583. }
  584. EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
  585. static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
  586. struct msghdr *msg, size_t size, int flags)
  587. {
  588. int err;
  589. struct sock_iocb *si = kiocb_to_siocb(iocb);
  590. si->sock = sock;
  591. si->scm = NULL;
  592. si->msg = msg;
  593. si->size = size;
  594. si->flags = flags;
  595. err = security_socket_recvmsg(sock, msg, size, flags);
  596. if (err)
  597. return err;
  598. return sock->ops->recvmsg(iocb, sock, msg, size, flags);
  599. }
  600. int sock_recvmsg(struct socket *sock, struct msghdr *msg,
  601. size_t size, int flags)
  602. {
  603. struct kiocb iocb;
  604. struct sock_iocb siocb;
  605. int ret;
  606. init_sync_kiocb(&iocb, NULL);
  607. iocb.private = &siocb;
  608. ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
  609. if (-EIOCBQUEUED == ret)
  610. ret = wait_on_sync_kiocb(&iocb);
  611. return ret;
  612. }
  613. int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
  614. struct kvec *vec, size_t num, size_t size, int flags)
  615. {
  616. mm_segment_t oldfs = get_fs();
  617. int result;
  618. set_fs(KERNEL_DS);
  619. /*
  620. * the following is safe, since for compiler definitions of kvec and
  621. * iovec are identical, yielding the same in-core layout and alignment
  622. */
  623. msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
  624. result = sock_recvmsg(sock, msg, size, flags);
  625. set_fs(oldfs);
  626. return result;
  627. }
  628. static void sock_aio_dtor(struct kiocb *iocb)
  629. {
  630. kfree(iocb->private);
  631. }
  632. static ssize_t sock_sendpage(struct file *file, struct page *page,
  633. int offset, size_t size, loff_t *ppos, int more)
  634. {
  635. struct socket *sock;
  636. int flags;
  637. sock = file->private_data;
  638. flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
  639. if (more)
  640. flags |= MSG_MORE;
  641. return kernel_sendpage(sock, page, offset, size, flags);
  642. }
  643. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  644. struct pipe_inode_info *pipe, size_t len,
  645. unsigned int flags)
  646. {
  647. struct socket *sock = file->private_data;
  648. if (unlikely(!sock->ops->splice_read))
  649. return -EINVAL;
  650. return sock->ops->splice_read(sock, ppos, pipe, len, flags);
  651. }
  652. static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
  653. struct sock_iocb *siocb)
  654. {
  655. if (!is_sync_kiocb(iocb)) {
  656. siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
  657. if (!siocb)
  658. return NULL;
  659. iocb->ki_dtor = sock_aio_dtor;
  660. }
  661. siocb->kiocb = iocb;
  662. iocb->private = siocb;
  663. return siocb;
  664. }
  665. static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
  666. struct file *file, const struct iovec *iov,
  667. unsigned long nr_segs)
  668. {
  669. struct socket *sock = file->private_data;
  670. size_t size = 0;
  671. int i;
  672. for (i = 0; i < nr_segs; i++)
  673. size += iov[i].iov_len;
  674. msg->msg_name = NULL;
  675. msg->msg_namelen = 0;
  676. msg->msg_control = NULL;
  677. msg->msg_controllen = 0;
  678. msg->msg_iov = (struct iovec *)iov;
  679. msg->msg_iovlen = nr_segs;
  680. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  681. return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
  682. }
  683. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  684. unsigned long nr_segs, loff_t pos)
  685. {
  686. struct sock_iocb siocb, *x;
  687. if (pos != 0)
  688. return -ESPIPE;
  689. if (iocb->ki_left == 0) /* Match SYS5 behaviour */
  690. return 0;
  691. x = alloc_sock_iocb(iocb, &siocb);
  692. if (!x)
  693. return -ENOMEM;
  694. return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  695. }
  696. static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
  697. struct file *file, const struct iovec *iov,
  698. unsigned long nr_segs)
  699. {
  700. struct socket *sock = file->private_data;
  701. size_t size = 0;
  702. int i;
  703. for (i = 0; i < nr_segs; i++)
  704. size += iov[i].iov_len;
  705. msg->msg_name = NULL;
  706. msg->msg_namelen = 0;
  707. msg->msg_control = NULL;
  708. msg->msg_controllen = 0;
  709. msg->msg_iov = (struct iovec *)iov;
  710. msg->msg_iovlen = nr_segs;
  711. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  712. if (sock->type == SOCK_SEQPACKET)
  713. msg->msg_flags |= MSG_EOR;
  714. return __sock_sendmsg(iocb, sock, msg, size);
  715. }
  716. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  717. unsigned long nr_segs, loff_t pos)
  718. {
  719. struct sock_iocb siocb, *x;
  720. if (pos != 0)
  721. return -ESPIPE;
  722. x = alloc_sock_iocb(iocb, &siocb);
  723. if (!x)
  724. return -ENOMEM;
  725. return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  726. }
  727. /*
  728. * Atomic setting of ioctl hooks to avoid race
  729. * with module unload.
  730. */
  731. static DEFINE_MUTEX(br_ioctl_mutex);
  732. static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
  733. void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
  734. {
  735. mutex_lock(&br_ioctl_mutex);
  736. br_ioctl_hook = hook;
  737. mutex_unlock(&br_ioctl_mutex);
  738. }
  739. EXPORT_SYMBOL(brioctl_set);
  740. static DEFINE_MUTEX(vlan_ioctl_mutex);
  741. static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
  742. void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
  743. {
  744. mutex_lock(&vlan_ioctl_mutex);
  745. vlan_ioctl_hook = hook;
  746. mutex_unlock(&vlan_ioctl_mutex);
  747. }
  748. EXPORT_SYMBOL(vlan_ioctl_set);
  749. static DEFINE_MUTEX(dlci_ioctl_mutex);
  750. static int (*dlci_ioctl_hook) (unsigned int, void __user *);
  751. void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
  752. {
  753. mutex_lock(&dlci_ioctl_mutex);
  754. dlci_ioctl_hook = hook;
  755. mutex_unlock(&dlci_ioctl_mutex);
  756. }
  757. EXPORT_SYMBOL(dlci_ioctl_set);
  758. /*
  759. * With an ioctl, arg may well be a user mode pointer, but we don't know
  760. * what to do with it - that's up to the protocol still.
  761. */
  762. static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  763. {
  764. struct socket *sock;
  765. struct sock *sk;
  766. void __user *argp = (void __user *)arg;
  767. int pid, err;
  768. struct net *net;
  769. sock = file->private_data;
  770. sk = sock->sk;
  771. net = sock_net(sk);
  772. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
  773. err = dev_ioctl(net, cmd, argp);
  774. } else
  775. #ifdef CONFIG_WIRELESS_EXT
  776. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  777. err = dev_ioctl(net, cmd, argp);
  778. } else
  779. #endif /* CONFIG_WIRELESS_EXT */
  780. switch (cmd) {
  781. case FIOSETOWN:
  782. case SIOCSPGRP:
  783. err = -EFAULT;
  784. if (get_user(pid, (int __user *)argp))
  785. break;
  786. err = f_setown(sock->file, pid, 1);
  787. break;
  788. case FIOGETOWN:
  789. case SIOCGPGRP:
  790. err = put_user(f_getown(sock->file),
  791. (int __user *)argp);
  792. break;
  793. case SIOCGIFBR:
  794. case SIOCSIFBR:
  795. case SIOCBRADDBR:
  796. case SIOCBRDELBR:
  797. err = -ENOPKG;
  798. if (!br_ioctl_hook)
  799. request_module("bridge");
  800. mutex_lock(&br_ioctl_mutex);
  801. if (br_ioctl_hook)
  802. err = br_ioctl_hook(net, cmd, argp);
  803. mutex_unlock(&br_ioctl_mutex);
  804. break;
  805. case SIOCGIFVLAN:
  806. case SIOCSIFVLAN:
  807. err = -ENOPKG;
  808. if (!vlan_ioctl_hook)
  809. request_module("8021q");
  810. mutex_lock(&vlan_ioctl_mutex);
  811. if (vlan_ioctl_hook)
  812. err = vlan_ioctl_hook(net, argp);
  813. mutex_unlock(&vlan_ioctl_mutex);
  814. break;
  815. case SIOCADDDLCI:
  816. case SIOCDELDLCI:
  817. err = -ENOPKG;
  818. if (!dlci_ioctl_hook)
  819. request_module("dlci");
  820. mutex_lock(&dlci_ioctl_mutex);
  821. if (dlci_ioctl_hook)
  822. err = dlci_ioctl_hook(cmd, argp);
  823. mutex_unlock(&dlci_ioctl_mutex);
  824. break;
  825. default:
  826. err = sock->ops->ioctl(sock, cmd, arg);
  827. /*
  828. * If this ioctl is unknown try to hand it down
  829. * to the NIC driver.
  830. */
  831. if (err == -ENOIOCTLCMD)
  832. err = dev_ioctl(net, cmd, argp);
  833. break;
  834. }
  835. return err;
  836. }
  837. int sock_create_lite(int family, int type, int protocol, struct socket **res)
  838. {
  839. int err;
  840. struct socket *sock = NULL;
  841. err = security_socket_create(family, type, protocol, 1);
  842. if (err)
  843. goto out;
  844. sock = sock_alloc();
  845. if (!sock) {
  846. err = -ENOMEM;
  847. goto out;
  848. }
  849. sock->type = type;
  850. err = security_socket_post_create(sock, family, type, protocol, 1);
  851. if (err)
  852. goto out_release;
  853. out:
  854. *res = sock;
  855. return err;
  856. out_release:
  857. sock_release(sock);
  858. sock = NULL;
  859. goto out;
  860. }
  861. /* No kernel lock held - perfect */
  862. static unsigned int sock_poll(struct file *file, poll_table *wait)
  863. {
  864. struct socket *sock;
  865. /*
  866. * We can't return errors to poll, so it's either yes or no.
  867. */
  868. sock = file->private_data;
  869. return sock->ops->poll(file, sock, wait);
  870. }
  871. static int sock_mmap(struct file *file, struct vm_area_struct *vma)
  872. {
  873. struct socket *sock = file->private_data;
  874. return sock->ops->mmap(file, sock, vma);
  875. }
  876. static int sock_close(struct inode *inode, struct file *filp)
  877. {
  878. /*
  879. * It was possible the inode is NULL we were
  880. * closing an unfinished socket.
  881. */
  882. if (!inode) {
  883. printk(KERN_DEBUG "sock_close: NULL inode\n");
  884. return 0;
  885. }
  886. sock_release(SOCKET_I(inode));
  887. return 0;
  888. }
  889. /*
  890. * Update the socket async list
  891. *
  892. * Fasync_list locking strategy.
  893. *
  894. * 1. fasync_list is modified only under process context socket lock
  895. * i.e. under semaphore.
  896. * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
  897. * or under socket lock.
  898. * 3. fasync_list can be used from softirq context, so that
  899. * modification under socket lock have to be enhanced with
  900. * write_lock_bh(&sk->sk_callback_lock).
  901. * --ANK (990710)
  902. */
  903. static int sock_fasync(int fd, struct file *filp, int on)
  904. {
  905. struct fasync_struct *fa, *fna = NULL, **prev;
  906. struct socket *sock;
  907. struct sock *sk;
  908. if (on) {
  909. fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
  910. if (fna == NULL)
  911. return -ENOMEM;
  912. }
  913. sock = filp->private_data;
  914. sk = sock->sk;
  915. if (sk == NULL) {
  916. kfree(fna);
  917. return -EINVAL;
  918. }
  919. lock_sock(sk);
  920. spin_lock(&filp->f_lock);
  921. if (on)
  922. filp->f_flags |= FASYNC;
  923. else
  924. filp->f_flags &= ~FASYNC;
  925. spin_unlock(&filp->f_lock);
  926. prev = &(sock->fasync_list);
  927. for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
  928. if (fa->fa_file == filp)
  929. break;
  930. if (on) {
  931. if (fa != NULL) {
  932. write_lock_bh(&sk->sk_callback_lock);
  933. fa->fa_fd = fd;
  934. write_unlock_bh(&sk->sk_callback_lock);
  935. kfree(fna);
  936. goto out;
  937. }
  938. fna->fa_file = filp;
  939. fna->fa_fd = fd;
  940. fna->magic = FASYNC_MAGIC;
  941. fna->fa_next = sock->fasync_list;
  942. write_lock_bh(&sk->sk_callback_lock);
  943. sock->fasync_list = fna;
  944. sock_set_flag(sk, SOCK_FASYNC);
  945. write_unlock_bh(&sk->sk_callback_lock);
  946. } else {
  947. if (fa != NULL) {
  948. write_lock_bh(&sk->sk_callback_lock);
  949. *prev = fa->fa_next;
  950. if (!sock->fasync_list)
  951. sock_reset_flag(sk, SOCK_FASYNC);
  952. write_unlock_bh(&sk->sk_callback_lock);
  953. kfree(fa);
  954. }
  955. }
  956. out:
  957. release_sock(sock->sk);
  958. return 0;
  959. }
  960. /* This function may be called only under socket lock or callback_lock */
  961. int sock_wake_async(struct socket *sock, int how, int band)
  962. {
  963. if (!sock || !sock->fasync_list)
  964. return -1;
  965. switch (how) {
  966. case SOCK_WAKE_WAITD:
  967. if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
  968. break;
  969. goto call_kill;
  970. case SOCK_WAKE_SPACE:
  971. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
  972. break;
  973. /* fall through */
  974. case SOCK_WAKE_IO:
  975. call_kill:
  976. __kill_fasync(sock->fasync_list, SIGIO, band);
  977. break;
  978. case SOCK_WAKE_URG:
  979. __kill_fasync(sock->fasync_list, SIGURG, band);
  980. }
  981. return 0;
  982. }
  983. static int __sock_create(struct net *net, int family, int type, int protocol,
  984. struct socket **res, int kern)
  985. {
  986. int err;
  987. struct socket *sock;
  988. const struct net_proto_family *pf;
  989. /*
  990. * Check protocol is in range
  991. */
  992. if (family < 0 || family >= NPROTO)
  993. return -EAFNOSUPPORT;
  994. if (type < 0 || type >= SOCK_MAX)
  995. return -EINVAL;
  996. /* Compatibility.
  997. This uglymoron is moved from INET layer to here to avoid
  998. deadlock in module load.
  999. */
  1000. if (family == PF_INET && type == SOCK_PACKET) {
  1001. static int warned;
  1002. if (!warned) {
  1003. warned = 1;
  1004. printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
  1005. current->comm);
  1006. }
  1007. family = PF_PACKET;
  1008. }
  1009. err = security_socket_create(family, type, protocol, kern);
  1010. if (err)
  1011. return err;
  1012. /*
  1013. * Allocate the socket and allow the family to set things up. if
  1014. * the protocol is 0, the family is instructed to select an appropriate
  1015. * default.
  1016. */
  1017. sock = sock_alloc();
  1018. if (!sock) {
  1019. if (net_ratelimit())
  1020. printk(KERN_WARNING "socket: no more sockets\n");
  1021. return -ENFILE; /* Not exactly a match, but its the
  1022. closest posix thing */
  1023. }
  1024. sock->type = type;
  1025. #ifdef CONFIG_MODULES
  1026. /* Attempt to load a protocol module if the find failed.
  1027. *
  1028. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  1029. * requested real, full-featured networking support upon configuration.
  1030. * Otherwise module support will break!
  1031. */
  1032. if (net_families[family] == NULL)
  1033. request_module("net-pf-%d", family);
  1034. #endif
  1035. rcu_read_lock();
  1036. pf = rcu_dereference(net_families[family]);
  1037. err = -EAFNOSUPPORT;
  1038. if (!pf)
  1039. goto out_release;
  1040. /*
  1041. * We will call the ->create function, that possibly is in a loadable
  1042. * module, so we have to bump that loadable module refcnt first.
  1043. */
  1044. if (!try_module_get(pf->owner))
  1045. goto out_release;
  1046. /* Now protected by module ref count */
  1047. rcu_read_unlock();
  1048. err = pf->create(net, sock, protocol);
  1049. if (err < 0)
  1050. goto out_module_put;
  1051. /*
  1052. * Now to bump the refcnt of the [loadable] module that owns this
  1053. * socket at sock_release time we decrement its refcnt.
  1054. */
  1055. if (!try_module_get(sock->ops->owner))
  1056. goto out_module_busy;
  1057. /*
  1058. * Now that we're done with the ->create function, the [loadable]
  1059. * module can have its refcnt decremented
  1060. */
  1061. module_put(pf->owner);
  1062. err = security_socket_post_create(sock, family, type, protocol, kern);
  1063. if (err)
  1064. goto out_sock_release;
  1065. *res = sock;
  1066. return 0;
  1067. out_module_busy:
  1068. err = -EAFNOSUPPORT;
  1069. out_module_put:
  1070. sock->ops = NULL;
  1071. module_put(pf->owner);
  1072. out_sock_release:
  1073. sock_release(sock);
  1074. return err;
  1075. out_release:
  1076. rcu_read_unlock();
  1077. goto out_sock_release;
  1078. }
  1079. int sock_create(int family, int type, int protocol, struct socket **res)
  1080. {
  1081. return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
  1082. }
  1083. int sock_create_kern(int family, int type, int protocol, struct socket **res)
  1084. {
  1085. return __sock_create(&init_net, family, type, protocol, res, 1);
  1086. }
  1087. SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
  1088. {
  1089. int retval;
  1090. struct socket *sock;
  1091. int flags;
  1092. /* Check the SOCK_* constants for consistency. */
  1093. BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
  1094. BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
  1095. BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
  1096. BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
  1097. flags = type & ~SOCK_TYPE_MASK;
  1098. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1099. return -EINVAL;
  1100. type &= SOCK_TYPE_MASK;
  1101. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1102. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1103. retval = sock_create(family, type, protocol, &sock);
  1104. if (retval < 0)
  1105. goto out;
  1106. retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
  1107. if (retval < 0)
  1108. goto out_release;
  1109. out:
  1110. /* It may be already another descriptor 8) Not kernel problem. */
  1111. return retval;
  1112. out_release:
  1113. sock_release(sock);
  1114. return retval;
  1115. }
  1116. /*
  1117. * Create a pair of connected sockets.
  1118. */
  1119. SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
  1120. int __user *, usockvec)
  1121. {
  1122. struct socket *sock1, *sock2;
  1123. int fd1, fd2, err;
  1124. struct file *newfile1, *newfile2;
  1125. int flags;
  1126. flags = type & ~SOCK_TYPE_MASK;
  1127. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1128. return -EINVAL;
  1129. type &= SOCK_TYPE_MASK;
  1130. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1131. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1132. /*
  1133. * Obtain the first socket and check if the underlying protocol
  1134. * supports the socketpair call.
  1135. */
  1136. err = sock_create(family, type, protocol, &sock1);
  1137. if (err < 0)
  1138. goto out;
  1139. err = sock_create(family, type, protocol, &sock2);
  1140. if (err < 0)
  1141. goto out_release_1;
  1142. err = sock1->ops->socketpair(sock1, sock2);
  1143. if (err < 0)
  1144. goto out_release_both;
  1145. fd1 = sock_alloc_fd(&newfile1, flags & O_CLOEXEC);
  1146. if (unlikely(fd1 < 0)) {
  1147. err = fd1;
  1148. goto out_release_both;
  1149. }
  1150. fd2 = sock_alloc_fd(&newfile2, flags & O_CLOEXEC);
  1151. if (unlikely(fd2 < 0)) {
  1152. err = fd2;
  1153. put_filp(newfile1);
  1154. put_unused_fd(fd1);
  1155. goto out_release_both;
  1156. }
  1157. err = sock_attach_fd(sock1, newfile1, flags & O_NONBLOCK);
  1158. if (unlikely(err < 0)) {
  1159. goto out_fd2;
  1160. }
  1161. err = sock_attach_fd(sock2, newfile2, flags & O_NONBLOCK);
  1162. if (unlikely(err < 0)) {
  1163. fput(newfile1);
  1164. goto out_fd1;
  1165. }
  1166. audit_fd_pair(fd1, fd2);
  1167. fd_install(fd1, newfile1);
  1168. fd_install(fd2, newfile2);
  1169. /* fd1 and fd2 may be already another descriptors.
  1170. * Not kernel problem.
  1171. */
  1172. err = put_user(fd1, &usockvec[0]);
  1173. if (!err)
  1174. err = put_user(fd2, &usockvec[1]);
  1175. if (!err)
  1176. return 0;
  1177. sys_close(fd2);
  1178. sys_close(fd1);
  1179. return err;
  1180. out_release_both:
  1181. sock_release(sock2);
  1182. out_release_1:
  1183. sock_release(sock1);
  1184. out:
  1185. return err;
  1186. out_fd2:
  1187. put_filp(newfile1);
  1188. sock_release(sock1);
  1189. out_fd1:
  1190. put_filp(newfile2);
  1191. sock_release(sock2);
  1192. put_unused_fd(fd1);
  1193. put_unused_fd(fd2);
  1194. goto out;
  1195. }
  1196. /*
  1197. * Bind a name to a socket. Nothing much to do here since it's
  1198. * the protocol's responsibility to handle the local address.
  1199. *
  1200. * We move the socket address to kernel space before we call
  1201. * the protocol layer (having also checked the address is ok).
  1202. */
  1203. SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
  1204. {
  1205. struct socket *sock;
  1206. struct sockaddr_storage address;
  1207. int err, fput_needed;
  1208. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1209. if (sock) {
  1210. err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
  1211. if (err >= 0) {
  1212. err = security_socket_bind(sock,
  1213. (struct sockaddr *)&address,
  1214. addrlen);
  1215. if (!err)
  1216. err = sock->ops->bind(sock,
  1217. (struct sockaddr *)
  1218. &address, addrlen);
  1219. }
  1220. fput_light(sock->file, fput_needed);
  1221. }
  1222. return err;
  1223. }
  1224. /*
  1225. * Perform a listen. Basically, we allow the protocol to do anything
  1226. * necessary for a listen, and if that works, we mark the socket as
  1227. * ready for listening.
  1228. */
  1229. SYSCALL_DEFINE2(listen, int, fd, int, backlog)
  1230. {
  1231. struct socket *sock;
  1232. int err, fput_needed;
  1233. int somaxconn;
  1234. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1235. if (sock) {
  1236. somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
  1237. if ((unsigned)backlog > somaxconn)
  1238. backlog = somaxconn;
  1239. err = security_socket_listen(sock, backlog);
  1240. if (!err)
  1241. err = sock->ops->listen(sock, backlog);
  1242. fput_light(sock->file, fput_needed);
  1243. }
  1244. return err;
  1245. }
  1246. /*
  1247. * For accept, we attempt to create a new socket, set up the link
  1248. * with the client, wake up the client, then return the new
  1249. * connected fd. We collect the address of the connector in kernel
  1250. * space and move it to user at the very end. This is unclean because
  1251. * we open the socket then return an error.
  1252. *
  1253. * 1003.1g adds the ability to recvmsg() to query connection pending
  1254. * status to recvmsg. We need to add that support in a way thats
  1255. * clean when we restucture accept also.
  1256. */
  1257. SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1258. int __user *, upeer_addrlen, int, flags)
  1259. {
  1260. struct socket *sock, *newsock;
  1261. struct file *newfile;
  1262. int err, len, newfd, fput_needed;
  1263. struct sockaddr_storage address;
  1264. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1265. return -EINVAL;
  1266. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1267. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1268. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1269. if (!sock)
  1270. goto out;
  1271. err = -ENFILE;
  1272. if (!(newsock = sock_alloc()))
  1273. goto out_put;
  1274. newsock->type = sock->type;
  1275. newsock->ops = sock->ops;
  1276. /*
  1277. * We don't need try_module_get here, as the listening socket (sock)
  1278. * has the protocol module (sock->ops->owner) held.
  1279. */
  1280. __module_get(newsock->ops->owner);
  1281. newfd = sock_alloc_fd(&newfile, flags & O_CLOEXEC);
  1282. if (unlikely(newfd < 0)) {
  1283. err = newfd;
  1284. sock_release(newsock);
  1285. goto out_put;
  1286. }
  1287. err = sock_attach_fd(newsock, newfile, flags & O_NONBLOCK);
  1288. if (err < 0)
  1289. goto out_fd_simple;
  1290. err = security_socket_accept(sock, newsock);
  1291. if (err)
  1292. goto out_fd;
  1293. err = sock->ops->accept(sock, newsock, sock->file->f_flags);
  1294. if (err < 0)
  1295. goto out_fd;
  1296. if (upeer_sockaddr) {
  1297. if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
  1298. &len, 2) < 0) {
  1299. err = -ECONNABORTED;
  1300. goto out_fd;
  1301. }
  1302. err = move_addr_to_user((struct sockaddr *)&address,
  1303. len, upeer_sockaddr, upeer_addrlen);
  1304. if (err < 0)
  1305. goto out_fd;
  1306. }
  1307. /* File flags are not inherited via accept() unlike another OSes. */
  1308. fd_install(newfd, newfile);
  1309. err = newfd;
  1310. out_put:
  1311. fput_light(sock->file, fput_needed);
  1312. out:
  1313. return err;
  1314. out_fd_simple:
  1315. sock_release(newsock);
  1316. put_filp(newfile);
  1317. put_unused_fd(newfd);
  1318. goto out_put;
  1319. out_fd:
  1320. fput(newfile);
  1321. put_unused_fd(newfd);
  1322. goto out_put;
  1323. }
  1324. SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1325. int __user *, upeer_addrlen)
  1326. {
  1327. return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
  1328. }
  1329. /*
  1330. * Attempt to connect to a socket with the server address. The address
  1331. * is in user space so we verify it is OK and move it to kernel space.
  1332. *
  1333. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1334. * break bindings
  1335. *
  1336. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1337. * other SEQPACKET protocols that take time to connect() as it doesn't
  1338. * include the -EINPROGRESS status for such sockets.
  1339. */
  1340. SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
  1341. int, addrlen)
  1342. {
  1343. struct socket *sock;
  1344. struct sockaddr_storage address;
  1345. int err, fput_needed;
  1346. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1347. if (!sock)
  1348. goto out;
  1349. err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
  1350. if (err < 0)
  1351. goto out_put;
  1352. err =
  1353. security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
  1354. if (err)
  1355. goto out_put;
  1356. err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
  1357. sock->file->f_flags);
  1358. out_put:
  1359. fput_light(sock->file, fput_needed);
  1360. out:
  1361. return err;
  1362. }
  1363. /*
  1364. * Get the local address ('name') of a socket object. Move the obtained
  1365. * name to user space.
  1366. */
  1367. SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
  1368. int __user *, usockaddr_len)
  1369. {
  1370. struct socket *sock;
  1371. struct sockaddr_storage address;
  1372. int len, err, fput_needed;
  1373. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1374. if (!sock)
  1375. goto out;
  1376. err = security_socket_getsockname(sock);
  1377. if (err)
  1378. goto out_put;
  1379. err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
  1380. if (err)
  1381. goto out_put;
  1382. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
  1383. out_put:
  1384. fput_light(sock->file, fput_needed);
  1385. out:
  1386. return err;
  1387. }
  1388. /*
  1389. * Get the remote address ('name') of a socket object. Move the obtained
  1390. * name to user space.
  1391. */
  1392. SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
  1393. int __user *, usockaddr_len)
  1394. {
  1395. struct socket *sock;
  1396. struct sockaddr_storage address;
  1397. int len, err, fput_needed;
  1398. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1399. if (sock != NULL) {
  1400. err = security_socket_getpeername(sock);
  1401. if (err) {
  1402. fput_light(sock->file, fput_needed);
  1403. return err;
  1404. }
  1405. err =
  1406. sock->ops->getname(sock, (struct sockaddr *)&address, &len,
  1407. 1);
  1408. if (!err)
  1409. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
  1410. usockaddr_len);
  1411. fput_light(sock->file, fput_needed);
  1412. }
  1413. return err;
  1414. }
  1415. /*
  1416. * Send a datagram to a given address. We move the address into kernel
  1417. * space and check the user space data area is readable before invoking
  1418. * the protocol.
  1419. */
  1420. SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
  1421. unsigned, flags, struct sockaddr __user *, addr,
  1422. int, addr_len)
  1423. {
  1424. struct socket *sock;
  1425. struct sockaddr_storage address;
  1426. int err;
  1427. struct msghdr msg;
  1428. struct iovec iov;
  1429. int fput_needed;
  1430. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1431. if (!sock)
  1432. goto out;
  1433. iov.iov_base = buff;
  1434. iov.iov_len = len;
  1435. msg.msg_name = NULL;
  1436. msg.msg_iov = &iov;
  1437. msg.msg_iovlen = 1;
  1438. msg.msg_control = NULL;
  1439. msg.msg_controllen = 0;
  1440. msg.msg_namelen = 0;
  1441. if (addr) {
  1442. err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
  1443. if (err < 0)
  1444. goto out_put;
  1445. msg.msg_name = (struct sockaddr *)&address;
  1446. msg.msg_namelen = addr_len;
  1447. }
  1448. if (sock->file->f_flags & O_NONBLOCK)
  1449. flags |= MSG_DONTWAIT;
  1450. msg.msg_flags = flags;
  1451. err = sock_sendmsg(sock, &msg, len);
  1452. out_put:
  1453. fput_light(sock->file, fput_needed);
  1454. out:
  1455. return err;
  1456. }
  1457. /*
  1458. * Send a datagram down a socket.
  1459. */
  1460. SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
  1461. unsigned, flags)
  1462. {
  1463. return sys_sendto(fd, buff, len, flags, NULL, 0);
  1464. }
  1465. /*
  1466. * Receive a frame from the socket and optionally record the address of the
  1467. * sender. We verify the buffers are writable and if needed move the
  1468. * sender address from kernel to user space.
  1469. */
  1470. SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
  1471. unsigned, flags, struct sockaddr __user *, addr,
  1472. int __user *, addr_len)
  1473. {
  1474. struct socket *sock;
  1475. struct iovec iov;
  1476. struct msghdr msg;
  1477. struct sockaddr_storage address;
  1478. int err, err2;
  1479. int fput_needed;
  1480. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1481. if (!sock)
  1482. goto out;
  1483. msg.msg_control = NULL;
  1484. msg.msg_controllen = 0;
  1485. msg.msg_iovlen = 1;
  1486. msg.msg_iov = &iov;
  1487. iov.iov_len = size;
  1488. iov.iov_base = ubuf;
  1489. msg.msg_name = (struct sockaddr *)&address;
  1490. msg.msg_namelen = sizeof(address);
  1491. if (sock->file->f_flags & O_NONBLOCK)
  1492. flags |= MSG_DONTWAIT;
  1493. err = sock_recvmsg(sock, &msg, size, flags);
  1494. if (err >= 0 && addr != NULL) {
  1495. err2 = move_addr_to_user((struct sockaddr *)&address,
  1496. msg.msg_namelen, addr, addr_len);
  1497. if (err2 < 0)
  1498. err = err2;
  1499. }
  1500. fput_light(sock->file, fput_needed);
  1501. out:
  1502. return err;
  1503. }
  1504. /*
  1505. * Receive a datagram from a socket.
  1506. */
  1507. asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
  1508. unsigned flags)
  1509. {
  1510. return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1511. }
  1512. /*
  1513. * Set a socket option. Because we don't know the option lengths we have
  1514. * to pass the user mode parameter for the protocols to sort out.
  1515. */
  1516. SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
  1517. char __user *, optval, int, optlen)
  1518. {
  1519. int err, fput_needed;
  1520. struct socket *sock;
  1521. if (optlen < 0)
  1522. return -EINVAL;
  1523. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1524. if (sock != NULL) {
  1525. err = security_socket_setsockopt(sock, level, optname);
  1526. if (err)
  1527. goto out_put;
  1528. if (level == SOL_SOCKET)
  1529. err =
  1530. sock_setsockopt(sock, level, optname, optval,
  1531. optlen);
  1532. else
  1533. err =
  1534. sock->ops->setsockopt(sock, level, optname, optval,
  1535. optlen);
  1536. out_put:
  1537. fput_light(sock->file, fput_needed);
  1538. }
  1539. return err;
  1540. }
  1541. /*
  1542. * Get a socket option. Because we don't know the option lengths we have
  1543. * to pass a user mode parameter for the protocols to sort out.
  1544. */
  1545. SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
  1546. char __user *, optval, int __user *, optlen)
  1547. {
  1548. int err, fput_needed;
  1549. struct socket *sock;
  1550. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1551. if (sock != NULL) {
  1552. err = security_socket_getsockopt(sock, level, optname);
  1553. if (err)
  1554. goto out_put;
  1555. if (level == SOL_SOCKET)
  1556. err =
  1557. sock_getsockopt(sock, level, optname, optval,
  1558. optlen);
  1559. else
  1560. err =
  1561. sock->ops->getsockopt(sock, level, optname, optval,
  1562. optlen);
  1563. out_put:
  1564. fput_light(sock->file, fput_needed);
  1565. }
  1566. return err;
  1567. }
  1568. /*
  1569. * Shutdown a socket.
  1570. */
  1571. SYSCALL_DEFINE2(shutdown, int, fd, int, how)
  1572. {
  1573. int err, fput_needed;
  1574. struct socket *sock;
  1575. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1576. if (sock != NULL) {
  1577. err = security_socket_shutdown(sock, how);
  1578. if (!err)
  1579. err = sock->ops->shutdown(sock, how);
  1580. fput_light(sock->file, fput_needed);
  1581. }
  1582. return err;
  1583. }
  1584. /* A couple of helpful macros for getting the address of the 32/64 bit
  1585. * fields which are the same type (int / unsigned) on our platforms.
  1586. */
  1587. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  1588. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  1589. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  1590. /*
  1591. * BSD sendmsg interface
  1592. */
  1593. SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
  1594. {
  1595. struct compat_msghdr __user *msg_compat =
  1596. (struct compat_msghdr __user *)msg;
  1597. struct socket *sock;
  1598. struct sockaddr_storage address;
  1599. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1600. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  1601. __attribute__ ((aligned(sizeof(__kernel_size_t))));
  1602. /* 20 is size of ipv6_pktinfo */
  1603. unsigned char *ctl_buf = ctl;
  1604. struct msghdr msg_sys;
  1605. int err, ctl_len, iov_size, total_len;
  1606. int fput_needed;
  1607. err = -EFAULT;
  1608. if (MSG_CMSG_COMPAT & flags) {
  1609. if (get_compat_msghdr(&msg_sys, msg_compat))
  1610. return -EFAULT;
  1611. }
  1612. else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
  1613. return -EFAULT;
  1614. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1615. if (!sock)
  1616. goto out;
  1617. /* do not move before msg_sys is valid */
  1618. err = -EMSGSIZE;
  1619. if (msg_sys.msg_iovlen > UIO_MAXIOV)
  1620. goto out_put;
  1621. /* Check whether to allocate the iovec area */
  1622. err = -ENOMEM;
  1623. iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
  1624. if (msg_sys.msg_iovlen > UIO_FASTIOV) {
  1625. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1626. if (!iov)
  1627. goto out_put;
  1628. }
  1629. /* This will also move the address data into kernel space */
  1630. if (MSG_CMSG_COMPAT & flags) {
  1631. err = verify_compat_iovec(&msg_sys, iov,
  1632. (struct sockaddr *)&address,
  1633. VERIFY_READ);
  1634. } else
  1635. err = verify_iovec(&msg_sys, iov,
  1636. (struct sockaddr *)&address,
  1637. VERIFY_READ);
  1638. if (err < 0)
  1639. goto out_freeiov;
  1640. total_len = err;
  1641. err = -ENOBUFS;
  1642. if (msg_sys.msg_controllen > INT_MAX)
  1643. goto out_freeiov;
  1644. ctl_len = msg_sys.msg_controllen;
  1645. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  1646. err =
  1647. cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
  1648. sizeof(ctl));
  1649. if (err)
  1650. goto out_freeiov;
  1651. ctl_buf = msg_sys.msg_control;
  1652. ctl_len = msg_sys.msg_controllen;
  1653. } else if (ctl_len) {
  1654. if (ctl_len > sizeof(ctl)) {
  1655. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  1656. if (ctl_buf == NULL)
  1657. goto out_freeiov;
  1658. }
  1659. err = -EFAULT;
  1660. /*
  1661. * Careful! Before this, msg_sys.msg_control contains a user pointer.
  1662. * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
  1663. * checking falls down on this.
  1664. */
  1665. if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
  1666. ctl_len))
  1667. goto out_freectl;
  1668. msg_sys.msg_control = ctl_buf;
  1669. }
  1670. msg_sys.msg_flags = flags;
  1671. if (sock->file->f_flags & O_NONBLOCK)
  1672. msg_sys.msg_flags |= MSG_DONTWAIT;
  1673. err = sock_sendmsg(sock, &msg_sys, total_len);
  1674. out_freectl:
  1675. if (ctl_buf != ctl)
  1676. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  1677. out_freeiov:
  1678. if (iov != iovstack)
  1679. sock_kfree_s(sock->sk, iov, iov_size);
  1680. out_put:
  1681. fput_light(sock->file, fput_needed);
  1682. out:
  1683. return err;
  1684. }
  1685. /*
  1686. * BSD recvmsg interface
  1687. */
  1688. SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
  1689. unsigned int, flags)
  1690. {
  1691. struct compat_msghdr __user *msg_compat =
  1692. (struct compat_msghdr __user *)msg;
  1693. struct socket *sock;
  1694. struct iovec iovstack[UIO_FASTIOV];
  1695. struct iovec *iov = iovstack;
  1696. struct msghdr msg_sys;
  1697. unsigned long cmsg_ptr;
  1698. int err, iov_size, total_len, len;
  1699. int fput_needed;
  1700. /* kernel mode address */
  1701. struct sockaddr_storage addr;
  1702. /* user mode address pointers */
  1703. struct sockaddr __user *uaddr;
  1704. int __user *uaddr_len;
  1705. if (MSG_CMSG_COMPAT & flags) {
  1706. if (get_compat_msghdr(&msg_sys, msg_compat))
  1707. return -EFAULT;
  1708. }
  1709. else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
  1710. return -EFAULT;
  1711. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1712. if (!sock)
  1713. goto out;
  1714. err = -EMSGSIZE;
  1715. if (msg_sys.msg_iovlen > UIO_MAXIOV)
  1716. goto out_put;
  1717. /* Check whether to allocate the iovec area */
  1718. err = -ENOMEM;
  1719. iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
  1720. if (msg_sys.msg_iovlen > UIO_FASTIOV) {
  1721. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1722. if (!iov)
  1723. goto out_put;
  1724. }
  1725. /*
  1726. * Save the user-mode address (verify_iovec will change the
  1727. * kernel msghdr to use the kernel address space)
  1728. */
  1729. uaddr = (__force void __user *)msg_sys.msg_name;
  1730. uaddr_len = COMPAT_NAMELEN(msg);
  1731. if (MSG_CMSG_COMPAT & flags) {
  1732. err = verify_compat_iovec(&msg_sys, iov,
  1733. (struct sockaddr *)&addr,
  1734. VERIFY_WRITE);
  1735. } else
  1736. err = verify_iovec(&msg_sys, iov,
  1737. (struct sockaddr *)&addr,
  1738. VERIFY_WRITE);
  1739. if (err < 0)
  1740. goto out_freeiov;
  1741. total_len = err;
  1742. cmsg_ptr = (unsigned long)msg_sys.msg_control;
  1743. msg_sys.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
  1744. if (sock->file->f_flags & O_NONBLOCK)
  1745. flags |= MSG_DONTWAIT;
  1746. err = sock_recvmsg(sock, &msg_sys, total_len, flags);
  1747. if (err < 0)
  1748. goto out_freeiov;
  1749. len = err;
  1750. if (uaddr != NULL) {
  1751. err = move_addr_to_user((struct sockaddr *)&addr,
  1752. msg_sys.msg_namelen, uaddr,
  1753. uaddr_len);
  1754. if (err < 0)
  1755. goto out_freeiov;
  1756. }
  1757. err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
  1758. COMPAT_FLAGS(msg));
  1759. if (err)
  1760. goto out_freeiov;
  1761. if (MSG_CMSG_COMPAT & flags)
  1762. err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
  1763. &msg_compat->msg_controllen);
  1764. else
  1765. err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
  1766. &msg->msg_controllen);
  1767. if (err)
  1768. goto out_freeiov;
  1769. err = len;
  1770. out_freeiov:
  1771. if (iov != iovstack)
  1772. sock_kfree_s(sock->sk, iov, iov_size);
  1773. out_put:
  1774. fput_light(sock->file, fput_needed);
  1775. out:
  1776. return err;
  1777. }
  1778. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  1779. /* Argument list sizes for sys_socketcall */
  1780. #define AL(x) ((x) * sizeof(unsigned long))
  1781. static const unsigned char nargs[19]={
  1782. AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
  1783. AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
  1784. AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
  1785. AL(4)
  1786. };
  1787. #undef AL
  1788. /*
  1789. * System call vectors.
  1790. *
  1791. * Argument checking cleaned up. Saved 20% in size.
  1792. * This function doesn't need to set the kernel lock because
  1793. * it is set by the callees.
  1794. */
  1795. SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
  1796. {
  1797. unsigned long a[6];
  1798. unsigned long a0, a1;
  1799. int err;
  1800. unsigned int len;
  1801. if (call < 1 || call > SYS_ACCEPT4)
  1802. return -EINVAL;
  1803. len = nargs[call];
  1804. if (len > sizeof(a))
  1805. return -EINVAL;
  1806. /* copy_from_user should be SMP safe. */
  1807. if (copy_from_user(a, args, len))
  1808. return -EFAULT;
  1809. audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  1810. a0 = a[0];
  1811. a1 = a[1];
  1812. switch (call) {
  1813. case SYS_SOCKET:
  1814. err = sys_socket(a0, a1, a[2]);
  1815. break;
  1816. case SYS_BIND:
  1817. err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  1818. break;
  1819. case SYS_CONNECT:
  1820. err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  1821. break;
  1822. case SYS_LISTEN:
  1823. err = sys_listen(a0, a1);
  1824. break;
  1825. case SYS_ACCEPT:
  1826. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  1827. (int __user *)a[2], 0);
  1828. break;
  1829. case SYS_GETSOCKNAME:
  1830. err =
  1831. sys_getsockname(a0, (struct sockaddr __user *)a1,
  1832. (int __user *)a[2]);
  1833. break;
  1834. case SYS_GETPEERNAME:
  1835. err =
  1836. sys_getpeername(a0, (struct sockaddr __user *)a1,
  1837. (int __user *)a[2]);
  1838. break;
  1839. case SYS_SOCKETPAIR:
  1840. err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  1841. break;
  1842. case SYS_SEND:
  1843. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  1844. break;
  1845. case SYS_SENDTO:
  1846. err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
  1847. (struct sockaddr __user *)a[4], a[5]);
  1848. break;
  1849. case SYS_RECV:
  1850. err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
  1851. break;
  1852. case SYS_RECVFROM:
  1853. err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  1854. (struct sockaddr __user *)a[4],
  1855. (int __user *)a[5]);
  1856. break;
  1857. case SYS_SHUTDOWN:
  1858. err = sys_shutdown(a0, a1);
  1859. break;
  1860. case SYS_SETSOCKOPT:
  1861. err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
  1862. break;
  1863. case SYS_GETSOCKOPT:
  1864. err =
  1865. sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  1866. (int __user *)a[4]);
  1867. break;
  1868. case SYS_SENDMSG:
  1869. err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
  1870. break;
  1871. case SYS_RECVMSG:
  1872. err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
  1873. break;
  1874. case SYS_ACCEPT4:
  1875. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  1876. (int __user *)a[2], a[3]);
  1877. break;
  1878. default:
  1879. err = -EINVAL;
  1880. break;
  1881. }
  1882. return err;
  1883. }
  1884. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  1885. /**
  1886. * sock_register - add a socket protocol handler
  1887. * @ops: description of protocol
  1888. *
  1889. * This function is called by a protocol handler that wants to
  1890. * advertise its address family, and have it linked into the
  1891. * socket interface. The value ops->family coresponds to the
  1892. * socket system call protocol family.
  1893. */
  1894. int sock_register(const struct net_proto_family *ops)
  1895. {
  1896. int err;
  1897. if (ops->family >= NPROTO) {
  1898. printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
  1899. NPROTO);
  1900. return -ENOBUFS;
  1901. }
  1902. spin_lock(&net_family_lock);
  1903. if (net_families[ops->family])
  1904. err = -EEXIST;
  1905. else {
  1906. net_families[ops->family] = ops;
  1907. err = 0;
  1908. }
  1909. spin_unlock(&net_family_lock);
  1910. printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
  1911. return err;
  1912. }
  1913. /**
  1914. * sock_unregister - remove a protocol handler
  1915. * @family: protocol family to remove
  1916. *
  1917. * This function is called by a protocol handler that wants to
  1918. * remove its address family, and have it unlinked from the
  1919. * new socket creation.
  1920. *
  1921. * If protocol handler is a module, then it can use module reference
  1922. * counts to protect against new references. If protocol handler is not
  1923. * a module then it needs to provide its own protection in
  1924. * the ops->create routine.
  1925. */
  1926. void sock_unregister(int family)
  1927. {
  1928. BUG_ON(family < 0 || family >= NPROTO);
  1929. spin_lock(&net_family_lock);
  1930. net_families[family] = NULL;
  1931. spin_unlock(&net_family_lock);
  1932. synchronize_rcu();
  1933. printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
  1934. }
  1935. static int __init sock_init(void)
  1936. {
  1937. /*
  1938. * Initialize sock SLAB cache.
  1939. */
  1940. sk_init();
  1941. /*
  1942. * Initialize skbuff SLAB cache
  1943. */
  1944. skb_init();
  1945. /*
  1946. * Initialize the protocols module.
  1947. */
  1948. init_inodecache();
  1949. register_filesystem(&sock_fs_type);
  1950. sock_mnt = kern_mount(&sock_fs_type);
  1951. /* The real protocol initialization is performed in later initcalls.
  1952. */
  1953. #ifdef CONFIG_NETFILTER
  1954. netfilter_init();
  1955. #endif
  1956. return 0;
  1957. }
  1958. core_initcall(sock_init); /* early initcall */
  1959. #ifdef CONFIG_PROC_FS
  1960. void socket_seq_show(struct seq_file *seq)
  1961. {
  1962. int cpu;
  1963. int counter = 0;
  1964. for_each_possible_cpu(cpu)
  1965. counter += per_cpu(sockets_in_use, cpu);
  1966. /* It can be negative, by the way. 8) */
  1967. if (counter < 0)
  1968. counter = 0;
  1969. seq_printf(seq, "sockets: used %d\n", counter);
  1970. }
  1971. #endif /* CONFIG_PROC_FS */
  1972. #ifdef CONFIG_COMPAT
  1973. static long compat_sock_ioctl(struct file *file, unsigned cmd,
  1974. unsigned long arg)
  1975. {
  1976. struct socket *sock = file->private_data;
  1977. int ret = -ENOIOCTLCMD;
  1978. struct sock *sk;
  1979. struct net *net;
  1980. sk = sock->sk;
  1981. net = sock_net(sk);
  1982. if (sock->ops->compat_ioctl)
  1983. ret = sock->ops->compat_ioctl(sock, cmd, arg);
  1984. if (ret == -ENOIOCTLCMD &&
  1985. (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
  1986. ret = compat_wext_handle_ioctl(net, cmd, arg);
  1987. return ret;
  1988. }
  1989. #endif
  1990. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
  1991. {
  1992. return sock->ops->bind(sock, addr, addrlen);
  1993. }
  1994. int kernel_listen(struct socket *sock, int backlog)
  1995. {
  1996. return sock->ops->listen(sock, backlog);
  1997. }
  1998. int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
  1999. {
  2000. struct sock *sk = sock->sk;
  2001. int err;
  2002. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  2003. newsock);
  2004. if (err < 0)
  2005. goto done;
  2006. err = sock->ops->accept(sock, *newsock, flags);
  2007. if (err < 0) {
  2008. sock_release(*newsock);
  2009. *newsock = NULL;
  2010. goto done;
  2011. }
  2012. (*newsock)->ops = sock->ops;
  2013. __module_get((*newsock)->ops->owner);
  2014. done:
  2015. return err;
  2016. }
  2017. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  2018. int flags)
  2019. {
  2020. return sock->ops->connect(sock, addr, addrlen, flags);
  2021. }
  2022. int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
  2023. int *addrlen)
  2024. {
  2025. return sock->ops->getname(sock, addr, addrlen, 0);
  2026. }
  2027. int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
  2028. int *addrlen)
  2029. {
  2030. return sock->ops->getname(sock, addr, addrlen, 1);
  2031. }
  2032. int kernel_getsockopt(struct socket *sock, int level, int optname,
  2033. char *optval, int *optlen)
  2034. {
  2035. mm_segment_t oldfs = get_fs();
  2036. int err;
  2037. set_fs(KERNEL_DS);
  2038. if (level == SOL_SOCKET)
  2039. err = sock_getsockopt(sock, level, optname, optval, optlen);
  2040. else
  2041. err = sock->ops->getsockopt(sock, level, optname, optval,
  2042. optlen);
  2043. set_fs(oldfs);
  2044. return err;
  2045. }
  2046. int kernel_setsockopt(struct socket *sock, int level, int optname,
  2047. char *optval, unsigned int optlen)
  2048. {
  2049. mm_segment_t oldfs = get_fs();
  2050. int err;
  2051. set_fs(KERNEL_DS);
  2052. if (level == SOL_SOCKET)
  2053. err = sock_setsockopt(sock, level, optname, optval, optlen);
  2054. else
  2055. err = sock->ops->setsockopt(sock, level, optname, optval,
  2056. optlen);
  2057. set_fs(oldfs);
  2058. return err;
  2059. }
  2060. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  2061. size_t size, int flags)
  2062. {
  2063. if (sock->ops->sendpage)
  2064. return sock->ops->sendpage(sock, page, offset, size, flags);
  2065. return sock_no_sendpage(sock, page, offset, size, flags);
  2066. }
  2067. int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
  2068. {
  2069. mm_segment_t oldfs = get_fs();
  2070. int err;
  2071. set_fs(KERNEL_DS);
  2072. err = sock->ops->ioctl(sock, cmd, arg);
  2073. set_fs(oldfs);
  2074. return err;
  2075. }
  2076. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
  2077. {
  2078. return sock->ops->shutdown(sock, how);
  2079. }
  2080. EXPORT_SYMBOL(sock_create);
  2081. EXPORT_SYMBOL(sock_create_kern);
  2082. EXPORT_SYMBOL(sock_create_lite);
  2083. EXPORT_SYMBOL(sock_map_fd);
  2084. EXPORT_SYMBOL(sock_recvmsg);
  2085. EXPORT_SYMBOL(sock_register);
  2086. EXPORT_SYMBOL(sock_release);
  2087. EXPORT_SYMBOL(sock_sendmsg);
  2088. EXPORT_SYMBOL(sock_unregister);
  2089. EXPORT_SYMBOL(sock_wake_async);
  2090. EXPORT_SYMBOL(sockfd_lookup);
  2091. EXPORT_SYMBOL(kernel_sendmsg);
  2092. EXPORT_SYMBOL(kernel_recvmsg);
  2093. EXPORT_SYMBOL(kernel_bind);
  2094. EXPORT_SYMBOL(kernel_listen);
  2095. EXPORT_SYMBOL(kernel_accept);
  2096. EXPORT_SYMBOL(kernel_connect);
  2097. EXPORT_SYMBOL(kernel_getsockname);
  2098. EXPORT_SYMBOL(kernel_getpeername);
  2099. EXPORT_SYMBOL(kernel_getsockopt);
  2100. EXPORT_SYMBOL(kernel_setsockopt);
  2101. EXPORT_SYMBOL(kernel_sendpage);
  2102. EXPORT_SYMBOL(kernel_sock_ioctl);
  2103. EXPORT_SYMBOL(kernel_sock_shutdown);