vsock_addr.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #include <linux/types.h>
  16. #include <linux/socket.h>
  17. #include <linux/stddef.h>
  18. #include <net/sock.h>
  19. #include <net/vsock_addr.h>
  20. void vsock_addr_init(struct sockaddr_vm *addr, u32 cid, u32 port)
  21. {
  22. memset(addr, 0, sizeof(*addr));
  23. addr->svm_family = AF_VSOCK;
  24. addr->svm_cid = cid;
  25. addr->svm_port = port;
  26. }
  27. EXPORT_SYMBOL_GPL(vsock_addr_init);
  28. int vsock_addr_validate(const struct sockaddr_vm *addr)
  29. {
  30. if (!addr)
  31. return -EFAULT;
  32. if (addr->svm_family != AF_VSOCK)
  33. return -EAFNOSUPPORT;
  34. if (addr->svm_zero[0] != 0)
  35. return -EINVAL;
  36. return 0;
  37. }
  38. EXPORT_SYMBOL_GPL(vsock_addr_validate);
  39. bool vsock_addr_bound(const struct sockaddr_vm *addr)
  40. {
  41. return addr->svm_port != VMADDR_PORT_ANY;
  42. }
  43. EXPORT_SYMBOL_GPL(vsock_addr_bound);
  44. void vsock_addr_unbind(struct sockaddr_vm *addr)
  45. {
  46. vsock_addr_init(addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  47. }
  48. EXPORT_SYMBOL_GPL(vsock_addr_unbind);
  49. bool vsock_addr_equals_addr(const struct sockaddr_vm *addr,
  50. const struct sockaddr_vm *other)
  51. {
  52. return addr->svm_cid == other->svm_cid &&
  53. addr->svm_port == other->svm_port;
  54. }
  55. EXPORT_SYMBOL_GPL(vsock_addr_equals_addr);
  56. int vsock_addr_cast(const struct sockaddr *addr,
  57. size_t len, struct sockaddr_vm **out_addr)
  58. {
  59. if (len < sizeof(**out_addr))
  60. return -EFAULT;
  61. *out_addr = (struct sockaddr_vm *)addr;
  62. return vsock_addr_validate(*out_addr);
  63. }
  64. EXPORT_SYMBOL_GPL(vsock_addr_cast);