socket.c 51 KB

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