socket.c 49 KB

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