socket.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304
  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;
  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:
  1202. fput(newfile);
  1203. put_unused_fd(newfd);
  1204. goto out_put;
  1205. }
  1206. /*
  1207. * Attempt to connect to a socket with the server address. The address
  1208. * is in user space so we verify it is OK and move it to kernel space.
  1209. *
  1210. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1211. * break bindings
  1212. *
  1213. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1214. * other SEQPACKET protocols that take time to connect() as it doesn't
  1215. * include the -EINPROGRESS status for such sockets.
  1216. */
  1217. asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr,
  1218. int addrlen)
  1219. {
  1220. struct socket *sock;
  1221. char address[MAX_SOCK_ADDR];
  1222. int err, fput_needed;
  1223. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1224. if (!sock)
  1225. goto out;
  1226. err = move_addr_to_kernel(uservaddr, addrlen, address);
  1227. if (err < 0)
  1228. goto out_put;
  1229. err =
  1230. security_socket_connect(sock, (struct sockaddr *)address, addrlen);
  1231. if (err)
  1232. goto out_put;
  1233. err = sock->ops->connect(sock, (struct sockaddr *)address, addrlen,
  1234. sock->file->f_flags);
  1235. out_put:
  1236. fput_light(sock->file, fput_needed);
  1237. out:
  1238. return err;
  1239. }
  1240. /*
  1241. * Get the local address ('name') of a socket object. Move the obtained
  1242. * name to user space.
  1243. */
  1244. asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr,
  1245. int __user *usockaddr_len)
  1246. {
  1247. struct socket *sock;
  1248. char address[MAX_SOCK_ADDR];
  1249. int len, err, fput_needed;
  1250. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1251. if (!sock)
  1252. goto out;
  1253. err = security_socket_getsockname(sock);
  1254. if (err)
  1255. goto out_put;
  1256. err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
  1257. if (err)
  1258. goto out_put;
  1259. err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
  1260. out_put:
  1261. fput_light(sock->file, fput_needed);
  1262. out:
  1263. return err;
  1264. }
  1265. /*
  1266. * Get the remote address ('name') of a socket object. Move the obtained
  1267. * name to user space.
  1268. */
  1269. asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr,
  1270. int __user *usockaddr_len)
  1271. {
  1272. struct socket *sock;
  1273. char address[MAX_SOCK_ADDR];
  1274. int len, err, fput_needed;
  1275. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1276. if (sock != NULL) {
  1277. err = security_socket_getpeername(sock);
  1278. if (err) {
  1279. fput_light(sock->file, fput_needed);
  1280. return err;
  1281. }
  1282. err =
  1283. sock->ops->getname(sock, (struct sockaddr *)address, &len,
  1284. 1);
  1285. if (!err)
  1286. err = move_addr_to_user(address, len, usockaddr,
  1287. usockaddr_len);
  1288. fput_light(sock->file, fput_needed);
  1289. }
  1290. return err;
  1291. }
  1292. /*
  1293. * Send a datagram to a given address. We move the address into kernel
  1294. * space and check the user space data area is readable before invoking
  1295. * the protocol.
  1296. */
  1297. asmlinkage long sys_sendto(int fd, void __user *buff, size_t len,
  1298. unsigned flags, struct sockaddr __user *addr,
  1299. int addr_len)
  1300. {
  1301. struct socket *sock;
  1302. char address[MAX_SOCK_ADDR];
  1303. int err;
  1304. struct msghdr msg;
  1305. struct iovec iov;
  1306. int fput_needed;
  1307. struct file *sock_file;
  1308. sock_file = fget_light(fd, &fput_needed);
  1309. err = -EBADF;
  1310. if (!sock_file)
  1311. goto out;
  1312. sock = sock_from_file(sock_file, &err);
  1313. if (!sock)
  1314. goto out_put;
  1315. iov.iov_base = buff;
  1316. iov.iov_len = len;
  1317. msg.msg_name = NULL;
  1318. msg.msg_iov = &iov;
  1319. msg.msg_iovlen = 1;
  1320. msg.msg_control = NULL;
  1321. msg.msg_controllen = 0;
  1322. msg.msg_namelen = 0;
  1323. if (addr) {
  1324. err = move_addr_to_kernel(addr, addr_len, address);
  1325. if (err < 0)
  1326. goto out_put;
  1327. msg.msg_name = address;
  1328. msg.msg_namelen = addr_len;
  1329. }
  1330. if (sock->file->f_flags & O_NONBLOCK)
  1331. flags |= MSG_DONTWAIT;
  1332. msg.msg_flags = flags;
  1333. err = sock_sendmsg(sock, &msg, len);
  1334. out_put:
  1335. fput_light(sock_file, fput_needed);
  1336. out:
  1337. return err;
  1338. }
  1339. /*
  1340. * Send a datagram down a socket.
  1341. */
  1342. asmlinkage long sys_send(int fd, void __user *buff, size_t len, unsigned flags)
  1343. {
  1344. return sys_sendto(fd, buff, len, flags, NULL, 0);
  1345. }
  1346. /*
  1347. * Receive a frame from the socket and optionally record the address of the
  1348. * sender. We verify the buffers are writable and if needed move the
  1349. * sender address from kernel to user space.
  1350. */
  1351. asmlinkage long sys_recvfrom(int fd, void __user *ubuf, size_t size,
  1352. unsigned flags, struct sockaddr __user *addr,
  1353. int __user *addr_len)
  1354. {
  1355. struct socket *sock;
  1356. struct iovec iov;
  1357. struct msghdr msg;
  1358. char address[MAX_SOCK_ADDR];
  1359. int err, err2;
  1360. struct file *sock_file;
  1361. int fput_needed;
  1362. sock_file = fget_light(fd, &fput_needed);
  1363. err = -EBADF;
  1364. if (!sock_file)
  1365. goto out;
  1366. sock = sock_from_file(sock_file, &err);
  1367. if (!sock)
  1368. goto out_put;
  1369. msg.msg_control = NULL;
  1370. msg.msg_controllen = 0;
  1371. msg.msg_iovlen = 1;
  1372. msg.msg_iov = &iov;
  1373. iov.iov_len = size;
  1374. iov.iov_base = ubuf;
  1375. msg.msg_name = address;
  1376. msg.msg_namelen = MAX_SOCK_ADDR;
  1377. if (sock->file->f_flags & O_NONBLOCK)
  1378. flags |= MSG_DONTWAIT;
  1379. err = sock_recvmsg(sock, &msg, size, flags);
  1380. if (err >= 0 && addr != NULL) {
  1381. err2 = move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
  1382. if (err2 < 0)
  1383. err = err2;
  1384. }
  1385. out_put:
  1386. fput_light(sock_file, fput_needed);
  1387. out:
  1388. return err;
  1389. }
  1390. /*
  1391. * Receive a datagram from a socket.
  1392. */
  1393. asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
  1394. unsigned flags)
  1395. {
  1396. return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1397. }
  1398. /*
  1399. * Set a socket option. Because we don't know the option lengths we have
  1400. * to pass the user mode parameter for the protocols to sort out.
  1401. */
  1402. asmlinkage long sys_setsockopt(int fd, int level, int optname,
  1403. char __user *optval, int optlen)
  1404. {
  1405. int err, fput_needed;
  1406. struct socket *sock;
  1407. if (optlen < 0)
  1408. return -EINVAL;
  1409. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1410. if (sock != NULL) {
  1411. err = security_socket_setsockopt(sock, level, optname);
  1412. if (err)
  1413. goto out_put;
  1414. if (level == SOL_SOCKET)
  1415. err =
  1416. sock_setsockopt(sock, level, optname, optval,
  1417. optlen);
  1418. else
  1419. err =
  1420. sock->ops->setsockopt(sock, level, optname, optval,
  1421. optlen);
  1422. out_put:
  1423. fput_light(sock->file, fput_needed);
  1424. }
  1425. return err;
  1426. }
  1427. /*
  1428. * Get a socket option. Because we don't know the option lengths we have
  1429. * to pass a user mode parameter for the protocols to sort out.
  1430. */
  1431. asmlinkage long sys_getsockopt(int fd, int level, int optname,
  1432. char __user *optval, int __user *optlen)
  1433. {
  1434. int err, fput_needed;
  1435. struct socket *sock;
  1436. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1437. if (sock != NULL) {
  1438. err = security_socket_getsockopt(sock, level, optname);
  1439. if (err)
  1440. goto out_put;
  1441. if (level == SOL_SOCKET)
  1442. err =
  1443. sock_getsockopt(sock, level, optname, optval,
  1444. optlen);
  1445. else
  1446. err =
  1447. sock->ops->getsockopt(sock, level, optname, optval,
  1448. optlen);
  1449. out_put:
  1450. fput_light(sock->file, fput_needed);
  1451. }
  1452. return err;
  1453. }
  1454. /*
  1455. * Shutdown a socket.
  1456. */
  1457. asmlinkage long sys_shutdown(int fd, int how)
  1458. {
  1459. int err, fput_needed;
  1460. struct socket *sock;
  1461. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1462. if (sock != NULL) {
  1463. err = security_socket_shutdown(sock, how);
  1464. if (!err)
  1465. err = sock->ops->shutdown(sock, how);
  1466. fput_light(sock->file, fput_needed);
  1467. }
  1468. return err;
  1469. }
  1470. /* A couple of helpful macros for getting the address of the 32/64 bit
  1471. * fields which are the same type (int / unsigned) on our platforms.
  1472. */
  1473. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  1474. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  1475. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  1476. /*
  1477. * BSD sendmsg interface
  1478. */
  1479. asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
  1480. {
  1481. struct compat_msghdr __user *msg_compat =
  1482. (struct compat_msghdr __user *)msg;
  1483. struct socket *sock;
  1484. char address[MAX_SOCK_ADDR];
  1485. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1486. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  1487. __attribute__ ((aligned(sizeof(__kernel_size_t))));
  1488. /* 20 is size of ipv6_pktinfo */
  1489. unsigned char *ctl_buf = ctl;
  1490. struct msghdr msg_sys;
  1491. int err, ctl_len, iov_size, total_len;
  1492. int fput_needed;
  1493. err = -EFAULT;
  1494. if (MSG_CMSG_COMPAT & flags) {
  1495. if (get_compat_msghdr(&msg_sys, msg_compat))
  1496. return -EFAULT;
  1497. }
  1498. else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
  1499. return -EFAULT;
  1500. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1501. if (!sock)
  1502. goto out;
  1503. /* do not move before msg_sys is valid */
  1504. err = -EMSGSIZE;
  1505. if (msg_sys.msg_iovlen > UIO_MAXIOV)
  1506. goto out_put;
  1507. /* Check whether to allocate the iovec area */
  1508. err = -ENOMEM;
  1509. iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
  1510. if (msg_sys.msg_iovlen > UIO_FASTIOV) {
  1511. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1512. if (!iov)
  1513. goto out_put;
  1514. }
  1515. /* This will also move the address data into kernel space */
  1516. if (MSG_CMSG_COMPAT & flags) {
  1517. err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
  1518. } else
  1519. err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
  1520. if (err < 0)
  1521. goto out_freeiov;
  1522. total_len = err;
  1523. err = -ENOBUFS;
  1524. if (msg_sys.msg_controllen > INT_MAX)
  1525. goto out_freeiov;
  1526. ctl_len = msg_sys.msg_controllen;
  1527. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  1528. err =
  1529. cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
  1530. sizeof(ctl));
  1531. if (err)
  1532. goto out_freeiov;
  1533. ctl_buf = msg_sys.msg_control;
  1534. ctl_len = msg_sys.msg_controllen;
  1535. } else if (ctl_len) {
  1536. if (ctl_len > sizeof(ctl)) {
  1537. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  1538. if (ctl_buf == NULL)
  1539. goto out_freeiov;
  1540. }
  1541. err = -EFAULT;
  1542. /*
  1543. * Careful! Before this, msg_sys.msg_control contains a user pointer.
  1544. * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
  1545. * checking falls down on this.
  1546. */
  1547. if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
  1548. ctl_len))
  1549. goto out_freectl;
  1550. msg_sys.msg_control = ctl_buf;
  1551. }
  1552. msg_sys.msg_flags = flags;
  1553. if (sock->file->f_flags & O_NONBLOCK)
  1554. msg_sys.msg_flags |= MSG_DONTWAIT;
  1555. err = sock_sendmsg(sock, &msg_sys, total_len);
  1556. out_freectl:
  1557. if (ctl_buf != ctl)
  1558. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  1559. out_freeiov:
  1560. if (iov != iovstack)
  1561. sock_kfree_s(sock->sk, iov, iov_size);
  1562. out_put:
  1563. fput_light(sock->file, fput_needed);
  1564. out:
  1565. return err;
  1566. }
  1567. /*
  1568. * BSD recvmsg interface
  1569. */
  1570. asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg,
  1571. unsigned int flags)
  1572. {
  1573. struct compat_msghdr __user *msg_compat =
  1574. (struct compat_msghdr __user *)msg;
  1575. struct socket *sock;
  1576. struct iovec iovstack[UIO_FASTIOV];
  1577. struct iovec *iov = iovstack;
  1578. struct msghdr msg_sys;
  1579. unsigned long cmsg_ptr;
  1580. int err, iov_size, total_len, len;
  1581. int fput_needed;
  1582. /* kernel mode address */
  1583. char addr[MAX_SOCK_ADDR];
  1584. /* user mode address pointers */
  1585. struct sockaddr __user *uaddr;
  1586. int __user *uaddr_len;
  1587. if (MSG_CMSG_COMPAT & flags) {
  1588. if (get_compat_msghdr(&msg_sys, msg_compat))
  1589. return -EFAULT;
  1590. }
  1591. else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
  1592. return -EFAULT;
  1593. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1594. if (!sock)
  1595. goto out;
  1596. err = -EMSGSIZE;
  1597. if (msg_sys.msg_iovlen > UIO_MAXIOV)
  1598. goto out_put;
  1599. /* Check whether to allocate the iovec area */
  1600. err = -ENOMEM;
  1601. iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
  1602. if (msg_sys.msg_iovlen > UIO_FASTIOV) {
  1603. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1604. if (!iov)
  1605. goto out_put;
  1606. }
  1607. /*
  1608. * Save the user-mode address (verify_iovec will change the
  1609. * kernel msghdr to use the kernel address space)
  1610. */
  1611. uaddr = (void __user *)msg_sys.msg_name;
  1612. uaddr_len = COMPAT_NAMELEN(msg);
  1613. if (MSG_CMSG_COMPAT & flags) {
  1614. err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
  1615. } else
  1616. err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
  1617. if (err < 0)
  1618. goto out_freeiov;
  1619. total_len = err;
  1620. cmsg_ptr = (unsigned long)msg_sys.msg_control;
  1621. msg_sys.msg_flags = 0;
  1622. if (MSG_CMSG_COMPAT & flags)
  1623. msg_sys.msg_flags = MSG_CMSG_COMPAT;
  1624. if (sock->file->f_flags & O_NONBLOCK)
  1625. flags |= MSG_DONTWAIT;
  1626. err = sock_recvmsg(sock, &msg_sys, total_len, flags);
  1627. if (err < 0)
  1628. goto out_freeiov;
  1629. len = err;
  1630. if (uaddr != NULL) {
  1631. err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr,
  1632. uaddr_len);
  1633. if (err < 0)
  1634. goto out_freeiov;
  1635. }
  1636. err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
  1637. COMPAT_FLAGS(msg));
  1638. if (err)
  1639. goto out_freeiov;
  1640. if (MSG_CMSG_COMPAT & flags)
  1641. err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
  1642. &msg_compat->msg_controllen);
  1643. else
  1644. err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
  1645. &msg->msg_controllen);
  1646. if (err)
  1647. goto out_freeiov;
  1648. err = len;
  1649. out_freeiov:
  1650. if (iov != iovstack)
  1651. sock_kfree_s(sock->sk, iov, iov_size);
  1652. out_put:
  1653. fput_light(sock->file, fput_needed);
  1654. out:
  1655. return err;
  1656. }
  1657. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  1658. /* Argument list sizes for sys_socketcall */
  1659. #define AL(x) ((x) * sizeof(unsigned long))
  1660. static const unsigned char nargs[18]={
  1661. AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
  1662. AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
  1663. AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)
  1664. };
  1665. #undef AL
  1666. /*
  1667. * System call vectors.
  1668. *
  1669. * Argument checking cleaned up. Saved 20% in size.
  1670. * This function doesn't need to set the kernel lock because
  1671. * it is set by the callees.
  1672. */
  1673. asmlinkage long sys_socketcall(int call, unsigned long __user *args)
  1674. {
  1675. unsigned long a[6];
  1676. unsigned long a0, a1;
  1677. int err;
  1678. if (call < 1 || call > SYS_RECVMSG)
  1679. return -EINVAL;
  1680. /* copy_from_user should be SMP safe. */
  1681. if (copy_from_user(a, args, nargs[call]))
  1682. return -EFAULT;
  1683. err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  1684. if (err)
  1685. return err;
  1686. a0 = a[0];
  1687. a1 = a[1];
  1688. switch (call) {
  1689. case SYS_SOCKET:
  1690. err = sys_socket(a0, a1, a[2]);
  1691. break;
  1692. case SYS_BIND:
  1693. err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  1694. break;
  1695. case SYS_CONNECT:
  1696. err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  1697. break;
  1698. case SYS_LISTEN:
  1699. err = sys_listen(a0, a1);
  1700. break;
  1701. case SYS_ACCEPT:
  1702. err =
  1703. sys_accept(a0, (struct sockaddr __user *)a1,
  1704. (int __user *)a[2]);
  1705. break;
  1706. case SYS_GETSOCKNAME:
  1707. err =
  1708. sys_getsockname(a0, (struct sockaddr __user *)a1,
  1709. (int __user *)a[2]);
  1710. break;
  1711. case SYS_GETPEERNAME:
  1712. err =
  1713. sys_getpeername(a0, (struct sockaddr __user *)a1,
  1714. (int __user *)a[2]);
  1715. break;
  1716. case SYS_SOCKETPAIR:
  1717. err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  1718. break;
  1719. case SYS_SEND:
  1720. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  1721. break;
  1722. case SYS_SENDTO:
  1723. err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
  1724. (struct sockaddr __user *)a[4], a[5]);
  1725. break;
  1726. case SYS_RECV:
  1727. err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
  1728. break;
  1729. case SYS_RECVFROM:
  1730. err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  1731. (struct sockaddr __user *)a[4],
  1732. (int __user *)a[5]);
  1733. break;
  1734. case SYS_SHUTDOWN:
  1735. err = sys_shutdown(a0, a1);
  1736. break;
  1737. case SYS_SETSOCKOPT:
  1738. err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
  1739. break;
  1740. case SYS_GETSOCKOPT:
  1741. err =
  1742. sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  1743. (int __user *)a[4]);
  1744. break;
  1745. case SYS_SENDMSG:
  1746. err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
  1747. break;
  1748. case SYS_RECVMSG:
  1749. err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
  1750. break;
  1751. default:
  1752. err = -EINVAL;
  1753. break;
  1754. }
  1755. return err;
  1756. }
  1757. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  1758. /**
  1759. * sock_register - add a socket protocol handler
  1760. * @ops: description of protocol
  1761. *
  1762. * This function is called by a protocol handler that wants to
  1763. * advertise its address family, and have it linked into the
  1764. * socket interface. The value ops->family coresponds to the
  1765. * socket system call protocol family.
  1766. */
  1767. int sock_register(const struct net_proto_family *ops)
  1768. {
  1769. int err;
  1770. if (ops->family >= NPROTO) {
  1771. printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
  1772. NPROTO);
  1773. return -ENOBUFS;
  1774. }
  1775. spin_lock(&net_family_lock);
  1776. if (net_families[ops->family])
  1777. err = -EEXIST;
  1778. else {
  1779. net_families[ops->family] = ops;
  1780. err = 0;
  1781. }
  1782. spin_unlock(&net_family_lock);
  1783. printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
  1784. return err;
  1785. }
  1786. /**
  1787. * sock_unregister - remove a protocol handler
  1788. * @family: protocol family to remove
  1789. *
  1790. * This function is called by a protocol handler that wants to
  1791. * remove its address family, and have it unlinked from the
  1792. * new socket creation.
  1793. *
  1794. * If protocol handler is a module, then it can use module reference
  1795. * counts to protect against new references. If protocol handler is not
  1796. * a module then it needs to provide its own protection in
  1797. * the ops->create routine.
  1798. */
  1799. void sock_unregister(int family)
  1800. {
  1801. BUG_ON(family < 0 || family >= NPROTO);
  1802. spin_lock(&net_family_lock);
  1803. net_families[family] = NULL;
  1804. spin_unlock(&net_family_lock);
  1805. synchronize_rcu();
  1806. printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
  1807. }
  1808. static int __init sock_init(void)
  1809. {
  1810. /*
  1811. * Initialize sock SLAB cache.
  1812. */
  1813. sk_init();
  1814. /*
  1815. * Initialize skbuff SLAB cache
  1816. */
  1817. skb_init();
  1818. /*
  1819. * Initialize the protocols module.
  1820. */
  1821. init_inodecache();
  1822. register_filesystem(&sock_fs_type);
  1823. sock_mnt = kern_mount(&sock_fs_type);
  1824. /* The real protocol initialization is performed in later initcalls.
  1825. */
  1826. #ifdef CONFIG_NETFILTER
  1827. netfilter_init();
  1828. #endif
  1829. return 0;
  1830. }
  1831. core_initcall(sock_init); /* early initcall */
  1832. #ifdef CONFIG_PROC_FS
  1833. void socket_seq_show(struct seq_file *seq)
  1834. {
  1835. int cpu;
  1836. int counter = 0;
  1837. for_each_possible_cpu(cpu)
  1838. counter += per_cpu(sockets_in_use, cpu);
  1839. /* It can be negative, by the way. 8) */
  1840. if (counter < 0)
  1841. counter = 0;
  1842. seq_printf(seq, "sockets: used %d\n", counter);
  1843. }
  1844. #endif /* CONFIG_PROC_FS */
  1845. #ifdef CONFIG_COMPAT
  1846. static long compat_sock_ioctl(struct file *file, unsigned cmd,
  1847. unsigned long arg)
  1848. {
  1849. struct socket *sock = file->private_data;
  1850. int ret = -ENOIOCTLCMD;
  1851. if (sock->ops->compat_ioctl)
  1852. ret = sock->ops->compat_ioctl(sock, cmd, arg);
  1853. return ret;
  1854. }
  1855. #endif
  1856. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
  1857. {
  1858. return sock->ops->bind(sock, addr, addrlen);
  1859. }
  1860. int kernel_listen(struct socket *sock, int backlog)
  1861. {
  1862. return sock->ops->listen(sock, backlog);
  1863. }
  1864. int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
  1865. {
  1866. struct sock *sk = sock->sk;
  1867. int err;
  1868. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  1869. newsock);
  1870. if (err < 0)
  1871. goto done;
  1872. err = sock->ops->accept(sock, *newsock, flags);
  1873. if (err < 0) {
  1874. sock_release(*newsock);
  1875. goto done;
  1876. }
  1877. (*newsock)->ops = sock->ops;
  1878. done:
  1879. return err;
  1880. }
  1881. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  1882. int flags)
  1883. {
  1884. return sock->ops->connect(sock, addr, addrlen, flags);
  1885. }
  1886. int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
  1887. int *addrlen)
  1888. {
  1889. return sock->ops->getname(sock, addr, addrlen, 0);
  1890. }
  1891. int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
  1892. int *addrlen)
  1893. {
  1894. return sock->ops->getname(sock, addr, addrlen, 1);
  1895. }
  1896. int kernel_getsockopt(struct socket *sock, int level, int optname,
  1897. char *optval, int *optlen)
  1898. {
  1899. mm_segment_t oldfs = get_fs();
  1900. int err;
  1901. set_fs(KERNEL_DS);
  1902. if (level == SOL_SOCKET)
  1903. err = sock_getsockopt(sock, level, optname, optval, optlen);
  1904. else
  1905. err = sock->ops->getsockopt(sock, level, optname, optval,
  1906. optlen);
  1907. set_fs(oldfs);
  1908. return err;
  1909. }
  1910. int kernel_setsockopt(struct socket *sock, int level, int optname,
  1911. char *optval, int optlen)
  1912. {
  1913. mm_segment_t oldfs = get_fs();
  1914. int err;
  1915. set_fs(KERNEL_DS);
  1916. if (level == SOL_SOCKET)
  1917. err = sock_setsockopt(sock, level, optname, optval, optlen);
  1918. else
  1919. err = sock->ops->setsockopt(sock, level, optname, optval,
  1920. optlen);
  1921. set_fs(oldfs);
  1922. return err;
  1923. }
  1924. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  1925. size_t size, int flags)
  1926. {
  1927. if (sock->ops->sendpage)
  1928. return sock->ops->sendpage(sock, page, offset, size, flags);
  1929. return sock_no_sendpage(sock, page, offset, size, flags);
  1930. }
  1931. int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
  1932. {
  1933. mm_segment_t oldfs = get_fs();
  1934. int err;
  1935. set_fs(KERNEL_DS);
  1936. err = sock->ops->ioctl(sock, cmd, arg);
  1937. set_fs(oldfs);
  1938. return err;
  1939. }
  1940. /* ABI emulation layers need these two */
  1941. EXPORT_SYMBOL(move_addr_to_kernel);
  1942. EXPORT_SYMBOL(move_addr_to_user);
  1943. EXPORT_SYMBOL(sock_create);
  1944. EXPORT_SYMBOL(sock_create_kern);
  1945. EXPORT_SYMBOL(sock_create_lite);
  1946. EXPORT_SYMBOL(sock_map_fd);
  1947. EXPORT_SYMBOL(sock_recvmsg);
  1948. EXPORT_SYMBOL(sock_register);
  1949. EXPORT_SYMBOL(sock_release);
  1950. EXPORT_SYMBOL(sock_sendmsg);
  1951. EXPORT_SYMBOL(sock_unregister);
  1952. EXPORT_SYMBOL(sock_wake_async);
  1953. EXPORT_SYMBOL(sockfd_lookup);
  1954. EXPORT_SYMBOL(kernel_sendmsg);
  1955. EXPORT_SYMBOL(kernel_recvmsg);
  1956. EXPORT_SYMBOL(kernel_bind);
  1957. EXPORT_SYMBOL(kernel_listen);
  1958. EXPORT_SYMBOL(kernel_accept);
  1959. EXPORT_SYMBOL(kernel_connect);
  1960. EXPORT_SYMBOL(kernel_getsockname);
  1961. EXPORT_SYMBOL(kernel_getpeername);
  1962. EXPORT_SYMBOL(kernel_getsockopt);
  1963. EXPORT_SYMBOL(kernel_setsockopt);
  1964. EXPORT_SYMBOL(kernel_sendpage);
  1965. EXPORT_SYMBOL(kernel_sock_ioctl);