socket.c 54 KB

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