Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1 | /* |
| 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> |
| 20 | |
| 21 | #include "vsock_addr.h" |
| 22 | |
| 23 | void vsock_addr_init(struct sockaddr_vm *addr, u32 cid, u32 port) |
| 24 | { |
| 25 | memset(addr, 0, sizeof(*addr)); |
| 26 | addr->svm_family = AF_VSOCK; |
| 27 | addr->svm_cid = cid; |
| 28 | addr->svm_port = port; |
| 29 | } |
| 30 | EXPORT_SYMBOL_GPL(vsock_addr_init); |
| 31 | |
| 32 | int vsock_addr_validate(const struct sockaddr_vm *addr) |
| 33 | { |
| 34 | if (!addr) |
| 35 | return -EFAULT; |
| 36 | |
| 37 | if (addr->svm_family != AF_VSOCK) |
| 38 | return -EAFNOSUPPORT; |
| 39 | |
| 40 | if (addr->svm_zero[0] != 0) |
| 41 | return -EINVAL; |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | EXPORT_SYMBOL_GPL(vsock_addr_validate); |
| 46 | |
| 47 | bool vsock_addr_bound(const struct sockaddr_vm *addr) |
| 48 | { |
| 49 | return addr->svm_port != VMADDR_PORT_ANY; |
| 50 | } |
| 51 | EXPORT_SYMBOL_GPL(vsock_addr_bound); |
| 52 | |
| 53 | void vsock_addr_unbind(struct sockaddr_vm *addr) |
| 54 | { |
| 55 | vsock_addr_init(addr, VMADDR_CID_ANY, VMADDR_PORT_ANY); |
| 56 | } |
| 57 | EXPORT_SYMBOL_GPL(vsock_addr_unbind); |
| 58 | |
| 59 | bool vsock_addr_equals_addr(const struct sockaddr_vm *addr, |
| 60 | const struct sockaddr_vm *other) |
| 61 | { |
| 62 | return addr->svm_cid == other->svm_cid && |
| 63 | addr->svm_port == other->svm_port; |
| 64 | } |
| 65 | EXPORT_SYMBOL_GPL(vsock_addr_equals_addr); |
| 66 | |
| 67 | bool vsock_addr_equals_addr_any(const struct sockaddr_vm *addr, |
| 68 | const struct sockaddr_vm *other) |
| 69 | { |
| 70 | return (addr->svm_cid == VMADDR_CID_ANY || |
| 71 | other->svm_cid == VMADDR_CID_ANY || |
| 72 | addr->svm_cid == other->svm_cid) && |
| 73 | addr->svm_port == other->svm_port; |
| 74 | } |
| 75 | EXPORT_SYMBOL_GPL(vsock_addr_equals_addr_any); |
| 76 | |
| 77 | int vsock_addr_cast(const struct sockaddr *addr, |
| 78 | size_t len, struct sockaddr_vm **out_addr) |
| 79 | { |
| 80 | if (len < sizeof(**out_addr)) |
| 81 | return -EFAULT; |
| 82 | |
| 83 | *out_addr = (struct sockaddr_vm *)addr; |
| 84 | return vsock_addr_validate(*out_addr); |
| 85 | } |
| 86 | EXPORT_SYMBOL_GPL(vsock_addr_cast); |