socket.c 62 KB

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