These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / SLOF / clients / net-snk / app / netlib / ndp.c
1 /******************************************************************************
2  * Copyright (c) 2013 IBM Corporation
3  * All rights reserved.
4  * This program and the accompanying materials
5  * are made available under the terms of the BSD License
6  * which accompanies this distribution, and is available at
7  * http://www.opensource.org/licenses/bsd-license.php
8  *
9  * Contributors:
10  *     IBM Corporation - initial implementation
11  *****************************************************************************/
12
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <netlib/ipv6.h>
17 #include <netlib/icmpv6.h>
18 #include <netlib/ndp.h>
19
20 /* Neighbor cache */
21 static struct neighbor *first_neighbor;
22 static struct neighbor *last_neighbor;
23
24 /* Router list */
25 static struct router *first_router;
26 static struct router *last_router;
27
28 /*
29  * NET: add new router to list
30  * @param  struct router nghb  - new router
31  * @return true or false
32  */
33 int8_t
34 router_add (struct router *nghb )
35 {
36         if (nghb == NULL)
37                 return -1;
38
39         if (first_router == NULL) {
40                 first_router= nghb;
41                 last_router = first_router;
42                 last_router->next = NULL;
43         } else {
44                 last_router->next = nghb;
45                 last_router = nghb;
46                 last_router->next = NULL;
47         }
48         return 1; /* no error */
49 }
50
51 /*
52  * NET: create a new router
53  * @param  uint8_t *packet - received packet (Ethernet/IPv6/ICMPv6/ND_NghSlct)
54  * @param  struct packeth  - pointers to headers in packet
55  * @return pointer to new router
56  */
57 void *
58 router_create (uint8_t *mac, ip6_addr_t *ip)
59 {
60         struct router *new_router;
61
62         new_router = malloc (sizeof(struct router));
63         if( !new_router) {
64                 return 0;
65         }
66         memset (new_router, 0, sizeof(struct router));
67
68         /* fill neighbor struct */
69         memcpy (new_router->mac, mac, 6);
70         memcpy (&(new_router->ip.addr[0]), &(ip->addr[0]), IPV6_ADDR_LENGTH);
71
72         return new_router;
73 }
74
75 struct router *
76 find_router( ip6_addr_t *ip )
77 {
78         struct router *n = NULL;
79
80         for (n = first_router; n != NULL ; n=n->next)
81                 if (ip6_cmp (&(n->ip), ip))
82                         return n; /* router is already in list*/
83
84         return NULL; /* router is unknown */
85 }
86
87 /*
88  * NET: add new neighbor to list
89  * @param  struct neighbor nghb  - new neighbor
90  * @return true or false
91  */
92 int8_t
93 neighbor_add (struct neighbor *nghb)
94 {
95         if (nghb == NULL)
96                 return -1;
97
98         if (first_neighbor == NULL) {
99                 first_neighbor = nghb;
100                 last_neighbor = first_neighbor;
101                 last_neighbor->next = NULL;
102         } else {
103                 last_neighbor->next = nghb;
104                 last_neighbor = nghb;
105                 last_neighbor->next = NULL;
106         }
107
108         return 1; /* no error */
109 }
110
111 /*
112  * NET: create a new neighbor
113  * @param  uint8_t *packet - received packet (Ethernet/IPv6/ICMPv6/ND_NghSlct)
114  * @param  struct packeth  - pointers to headers in packet
115  * @return pointer         - pointer to new neighbor
116  *         NULL            - malloc failed
117  */
118 void *
119 neighbor_create (uint8_t *packet, struct packeth *headers)
120 {
121         struct neighbor *new_neighbor;
122
123         new_neighbor = malloc (sizeof(struct neighbor));
124         if( !new_neighbor )
125                 return NULL;
126
127         /* fill neighbor struct */
128         memcpy (&(new_neighbor->mac),
129                 &(headers->ethh->src_mac[0]), 6);
130         memcpy (&(new_neighbor->ip.addr), &(headers->ip6h->src), IPV6_ADDR_LENGTH);
131         new_neighbor->status = NB_INCOMPLETE;
132
133         return new_neighbor;
134 }
135
136 /**
137  * NET: Find neighbor with given IPv6 address in Neighbor Cache
138  *
139  * @param  ip - Pointer to IPv6 address
140  * @return pointer - pointer to client IPv6 address (e.g. ::1)
141  *         NULL    - Neighbor not found
142  */
143 struct neighbor *
144 find_neighbor (ip6_addr_t *ip)
145 {
146         struct neighbor *n = NULL;
147
148         for (n = first_neighbor; n != NULL ; n=n->next) {
149                 if (ip6_cmp (&(n->ip), ip)) {
150                         return n; /* neighbor is already in cache */
151                 }
152         }
153
154         return NULL; /* neighbor is unknown */
155 }
156
157 void ndp_init(void)
158 {
159         /* Router list */
160         first_router = NULL;
161         last_router = first_router;
162
163         /* Init Neighbour cache */
164         first_neighbor = NULL;
165         last_neighbor  = first_neighbor;
166 }