socket.c 57 KB

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