Scott Feldman | f8bd909 | 2010-05-17 22:50:19 -0700 | [diff] [blame] | 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 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/slab.h> |
| 23 | |
| 24 | #include "vnic_vic.h" |
| 25 | |
| 26 | struct vic_provinfo *vic_provinfo_alloc(gfp_t flags, u8 *oui, u8 type) |
| 27 | { |
Dan Carpenter | fc0ba8e | 2010-06-09 21:59:03 +0000 | [diff] [blame] | 28 | struct vic_provinfo *vp; |
Scott Feldman | f8bd909 | 2010-05-17 22:50:19 -0700 | [diff] [blame] | 29 | |
Dan Carpenter | fc0ba8e | 2010-06-09 21:59:03 +0000 | [diff] [blame] | 30 | vp = kzalloc(VIC_PROVINFO_MAX_DATA, flags); |
| 31 | if (!vp) |
Scott Feldman | f8bd909 | 2010-05-17 22:50:19 -0700 | [diff] [blame] | 32 | return NULL; |
| 33 | |
| 34 | memcpy(vp->oui, oui, sizeof(vp->oui)); |
| 35 | vp->type = type; |
| 36 | vp->length = htonl(sizeof(vp->num_tlvs)); |
| 37 | |
| 38 | return vp; |
| 39 | } |
| 40 | |
| 41 | void vic_provinfo_free(struct vic_provinfo *vp) |
| 42 | { |
| 43 | kfree(vp); |
| 44 | } |
| 45 | |
| 46 | int vic_provinfo_add_tlv(struct vic_provinfo *vp, u16 type, u16 length, |
| 47 | void *value) |
| 48 | { |
| 49 | struct vic_provinfo_tlv *tlv; |
| 50 | |
| 51 | if (!vp || !value) |
| 52 | return -EINVAL; |
| 53 | |
| 54 | if (ntohl(vp->length) + sizeof(*tlv) + length > |
| 55 | VIC_PROVINFO_MAX_TLV_DATA) |
| 56 | return -ENOMEM; |
| 57 | |
| 58 | tlv = (struct vic_provinfo_tlv *)((u8 *)vp->tlv + |
| 59 | ntohl(vp->length) - sizeof(vp->num_tlvs)); |
| 60 | |
| 61 | tlv->type = htons(type); |
| 62 | tlv->length = htons(length); |
| 63 | memcpy(tlv->value, value, length); |
| 64 | |
| 65 | vp->num_tlvs = htonl(ntohl(vp->num_tlvs) + 1); |
| 66 | vp->length = htonl(ntohl(vp->length) + sizeof(*tlv) + length); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | size_t vic_provinfo_size(struct vic_provinfo *vp) |
| 72 | { |
| 73 | return vp ? ntohl(vp->length) + sizeof(*vp) - sizeof(vp->num_tlvs) : 0; |
| 74 | } |