socket.c 54 KB

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