socket.c 53 KB

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