socket.c 54 KB

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