socket.c 55 KB

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