socket.c 53 KB

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