socket.c 53 KB

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