socket.c 58 KB

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