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