1*e6550b3eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2*e6550b3eSThomas Gleixner // Copyright 2010 Cisco Systems, Inc.  All rights reserved.
3a6a5580cSJeff Kirsher 
4a6a5580cSJeff Kirsher #include <linux/kernel.h>
5a6a5580cSJeff Kirsher #include <linux/errno.h>
6a6a5580cSJeff Kirsher #include <linux/types.h>
7a6a5580cSJeff Kirsher #include <linux/slab.h>
8a6a5580cSJeff Kirsher 
9a6a5580cSJeff Kirsher #include "vnic_vic.h"
10a6a5580cSJeff Kirsher 
vic_provinfo_alloc(gfp_t flags,const u8 * oui,const u8 type)11a6a5580cSJeff Kirsher struct vic_provinfo *vic_provinfo_alloc(gfp_t flags, const u8 *oui,
12a6a5580cSJeff Kirsher 	const u8 type)
13a6a5580cSJeff Kirsher {
14a6a5580cSJeff Kirsher 	struct vic_provinfo *vp;
15a6a5580cSJeff Kirsher 
16a6a5580cSJeff Kirsher 	if (!oui)
17a6a5580cSJeff Kirsher 		return NULL;
18a6a5580cSJeff Kirsher 
19a6a5580cSJeff Kirsher 	vp = kzalloc(VIC_PROVINFO_MAX_DATA, flags);
20a6a5580cSJeff Kirsher 	if (!vp)
21a6a5580cSJeff Kirsher 		return NULL;
22a6a5580cSJeff Kirsher 
23a6a5580cSJeff Kirsher 	memcpy(vp->oui, oui, sizeof(vp->oui));
24a6a5580cSJeff Kirsher 	vp->type = type;
25a6a5580cSJeff Kirsher 	vp->length = htonl(sizeof(vp->num_tlvs));
26a6a5580cSJeff Kirsher 
27a6a5580cSJeff Kirsher 	return vp;
28a6a5580cSJeff Kirsher }
29a6a5580cSJeff Kirsher 
vic_provinfo_free(struct vic_provinfo * vp)30a6a5580cSJeff Kirsher void vic_provinfo_free(struct vic_provinfo *vp)
31a6a5580cSJeff Kirsher {
32a6a5580cSJeff Kirsher 	kfree(vp);
33a6a5580cSJeff Kirsher }
34a6a5580cSJeff Kirsher 
vic_provinfo_add_tlv(struct vic_provinfo * vp,u16 type,u16 length,const void * value)35a6a5580cSJeff Kirsher int vic_provinfo_add_tlv(struct vic_provinfo *vp, u16 type, u16 length,
36a6a5580cSJeff Kirsher 	const void *value)
37a6a5580cSJeff Kirsher {
38a6a5580cSJeff Kirsher 	struct vic_provinfo_tlv *tlv;
39a6a5580cSJeff Kirsher 
40a6a5580cSJeff Kirsher 	if (!vp || !value)
41a6a5580cSJeff Kirsher 		return -EINVAL;
42a6a5580cSJeff Kirsher 
43a6a5580cSJeff Kirsher 	if (ntohl(vp->length) + offsetof(struct vic_provinfo_tlv, value) +
44a6a5580cSJeff Kirsher 		length > VIC_PROVINFO_MAX_TLV_DATA)
45a6a5580cSJeff Kirsher 		return -ENOMEM;
46a6a5580cSJeff Kirsher 
47a6a5580cSJeff Kirsher 	tlv = (struct vic_provinfo_tlv *)((u8 *)vp->tlv +
48a6a5580cSJeff Kirsher 		ntohl(vp->length) - sizeof(vp->num_tlvs));
49a6a5580cSJeff Kirsher 
50a6a5580cSJeff Kirsher 	tlv->type = htons(type);
51a6a5580cSJeff Kirsher 	tlv->length = htons(length);
52a6a5580cSJeff Kirsher 	memcpy(tlv->value, value, length);
53a6a5580cSJeff Kirsher 
54a6a5580cSJeff Kirsher 	vp->num_tlvs = htonl(ntohl(vp->num_tlvs) + 1);
55a6a5580cSJeff Kirsher 	vp->length = htonl(ntohl(vp->length) +
56a6a5580cSJeff Kirsher 		offsetof(struct vic_provinfo_tlv, value) + length);
57a6a5580cSJeff Kirsher 
58a6a5580cSJeff Kirsher 	return 0;
59a6a5580cSJeff Kirsher }
60a6a5580cSJeff Kirsher 
vic_provinfo_size(struct vic_provinfo * vp)61a6a5580cSJeff Kirsher size_t vic_provinfo_size(struct vic_provinfo *vp)
62a6a5580cSJeff Kirsher {
63a6a5580cSJeff Kirsher 	return vp ?  ntohl(vp->length) + sizeof(*vp) - sizeof(vp->num_tlvs) : 0;
64a6a5580cSJeff Kirsher }
65