blob: 82486ee55eaca4cc4f4b1cb414cc848c1239ec46 [file] [log] [blame]
Andy Kingd021c342013-02-06 14:23:56 +00001/*
2 * VMware vSockets Driver
3 *
4 * Copyright (C) 2007-2012 VMware, Inc. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation version 2 and no later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 */
15
16#include <linux/types.h>
17#include <linux/socket.h>
18#include <linux/stddef.h>
19#include <net/sock.h>
Asias He82a54d02013-07-25 17:39:34 +080020#include <net/vsock_addr.h>
Andy Kingd021c342013-02-06 14:23:56 +000021
22void vsock_addr_init(struct sockaddr_vm *addr, u32 cid, u32 port)
23{
24 memset(addr, 0, sizeof(*addr));
25 addr->svm_family = AF_VSOCK;
26 addr->svm_cid = cid;
27 addr->svm_port = port;
28}
29EXPORT_SYMBOL_GPL(vsock_addr_init);
30
31int vsock_addr_validate(const struct sockaddr_vm *addr)
32{
33 if (!addr)
34 return -EFAULT;
35
36 if (addr->svm_family != AF_VSOCK)
37 return -EAFNOSUPPORT;
38
39 if (addr->svm_zero[0] != 0)
40 return -EINVAL;
41
42 return 0;
43}
44EXPORT_SYMBOL_GPL(vsock_addr_validate);
45
46bool vsock_addr_bound(const struct sockaddr_vm *addr)
47{
48 return addr->svm_port != VMADDR_PORT_ANY;
49}
50EXPORT_SYMBOL_GPL(vsock_addr_bound);
51
52void vsock_addr_unbind(struct sockaddr_vm *addr)
53{
54 vsock_addr_init(addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
55}
56EXPORT_SYMBOL_GPL(vsock_addr_unbind);
57
58bool vsock_addr_equals_addr(const struct sockaddr_vm *addr,
59 const struct sockaddr_vm *other)
60{
61 return addr->svm_cid == other->svm_cid &&
62 addr->svm_port == other->svm_port;
63}
64EXPORT_SYMBOL_GPL(vsock_addr_equals_addr);
65
Andy Kingd021c342013-02-06 14:23:56 +000066int vsock_addr_cast(const struct sockaddr *addr,
67 size_t len, struct sockaddr_vm **out_addr)
68{
69 if (len < sizeof(**out_addr))
70 return -EFAULT;
71
72 *out_addr = (struct sockaddr_vm *)addr;
73 return vsock_addr_validate(*out_addr);
74}
75EXPORT_SYMBOL_GPL(vsock_addr_cast);