vnic_vic.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2010 Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This program is free software; you may redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  9. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  10. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  12. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  13. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  14. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  15. * SOFTWARE.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/types.h>
  21. #include <linux/slab.h>
  22. #include "vnic_vic.h"
  23. struct vic_provinfo *vic_provinfo_alloc(gfp_t flags, const u8 *oui,
  24. const u8 type)
  25. {
  26. struct vic_provinfo *vp;
  27. if (!oui)
  28. return NULL;
  29. vp = kzalloc(VIC_PROVINFO_MAX_DATA, flags);
  30. if (!vp)
  31. return NULL;
  32. memcpy(vp->oui, oui, sizeof(vp->oui));
  33. vp->type = type;
  34. vp->length = htonl(sizeof(vp->num_tlvs));
  35. return vp;
  36. }
  37. void vic_provinfo_free(struct vic_provinfo *vp)
  38. {
  39. kfree(vp);
  40. }
  41. int vic_provinfo_add_tlv(struct vic_provinfo *vp, u16 type, u16 length,
  42. const void *value)
  43. {
  44. struct vic_provinfo_tlv *tlv;
  45. if (!vp || !value)
  46. return -EINVAL;
  47. if (ntohl(vp->length) + offsetof(struct vic_provinfo_tlv, value) +
  48. length > VIC_PROVINFO_MAX_TLV_DATA)
  49. return -ENOMEM;
  50. tlv = (struct vic_provinfo_tlv *)((u8 *)vp->tlv +
  51. ntohl(vp->length) - sizeof(vp->num_tlvs));
  52. tlv->type = htons(type);
  53. tlv->length = htons(length);
  54. memcpy(tlv->value, value, length);
  55. vp->num_tlvs = htonl(ntohl(vp->num_tlvs) + 1);
  56. vp->length = htonl(ntohl(vp->length) +
  57. offsetof(struct vic_provinfo_tlv, value) + length);
  58. return 0;
  59. }
  60. size_t vic_provinfo_size(struct vic_provinfo *vp)
  61. {
  62. return vp ? ntohl(vp->length) + sizeof(*vp) - sizeof(vp->num_tlvs) : 0;
  63. }