These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / SLOF / clients / net-snk / app / netlib / icmpv6.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 <stdint.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <sys/socket.h>
18 #include <netlib/ethernet.h>
19 #include <netlib/ipv6.h>
20 #include <netlib/icmpv6.h>
21 #include <netlib/ndp.h>
22 #include <netlib/dhcpv6.h>
23
24 static int ra_received = 0;
25
26 /**
27  * NET:
28  * @param  fd           socket fd
29  */
30 void
31 send_router_solicitation (int fd)
32 {
33         ip6_addr_t dest_addr;
34         uint8_t ether_packet[ETH_MTU_SIZE];
35         struct packeth headers;
36
37         headers.ip6h   = (struct ip6hdr *) (ether_packet + sizeof(struct ethhdr));
38         headers.icmp6h = (struct icmp6hdr *) (ether_packet +
39                           sizeof(struct ethhdr) +
40                           sizeof(struct ip6hdr));
41
42         /* Destination is "All routers multicast address" (link-local) */
43         dest_addr.part.prefix       = 0xff02000000000000ULL;
44         dest_addr.part.interface_id = 2;
45
46         /* Fill IPv6 header */
47         fill_ip6hdr (ether_packet + sizeof(struct ethhdr),
48                      ICMPv6_HEADER_SIZE + sizeof(struct router_solicitation),
49                      0x3a, //ICMPV6
50                      get_ipv6_address(), &dest_addr);
51
52         /* Fill ICMPv6 message */
53         headers.icmp6h->type = ICMPV6_ROUTER_SOLICITATION;
54         headers.icmp6h->code = 0;
55         headers.icmp6h->icmp6body.router_solicit.lladdr.type    = 1;
56         headers.icmp6h->icmp6body.router_solicit.lladdr.length  = 1;
57         memcpy( &(headers.icmp6h->icmp6body.router_solicit.lladdr.mac),
58                 get_mac_address(), 6);
59
60         send_ip (fd, headers.ip6h, sizeof(struct ip6hdr) +
61                    ICMPv6_HEADER_SIZE + sizeof(struct router_solicitation));
62 }
63
64 /**
65  * NET: Process prefix option in Router Advertisements
66  *
67  * @param  ip6_packet   pointer to an IPv6 packet
68  */
69 static void
70 handle_prefixoption (uint8_t *option)
71 {
72         ip6_addr_t prefix;
73         struct ip6addr_list_entry *new_address;
74         struct option_prefix *prefix_option;
75         struct prefix_info *prfx_info;
76
77         prefix_option = (struct option_prefix *) option;
78         memcpy( &(prefix.addr), &(prefix_option->prefix.addr), IPV6_ADDR_LENGTH);
79
80         /* Link-local adresses in RAs are nonsense */
81         if (ip6_is_linklocal(&prefix))
82                 return;
83
84         if (prefix_option->preferred_lifetime > prefix_option->valid_lifetime)
85                 return;
86
87         /* Add address created from prefix to IPv6 address list */
88         new_address = ip6_prefix2addr (prefix);
89         if (!new_address)
90                 return;
91
92         /* Process only prefixes we don't already have an adress from */
93         if (!unknown_prefix (&new_address->addr)) {
94                 return;
95         }
96
97         /* Fill struct prefix_info from data in RA and store it in new_address */
98         prfx_info = ip6_create_prefix_info();
99         if (!prfx_info)
100                 return;
101         memcpy (&(new_address->prfx_info), prfx_info, sizeof(struct prefix_info));
102
103         /* Add prefix received in RA to list of known prefixes */
104         ip6addr_add (new_address);
105 }
106
107 /**
108  * NET: Process source link layer addresses in Router Advertisements
109  *
110  * @param  ip6_packet   pointer to an IPv6 packet
111  */
112 static void
113 handle_source_lladdr ( struct option_ll_address *option, struct router *rtr)
114 {
115         memcpy (&(rtr->mac), &(option->mac), 6);
116 }
117
118 /**
119  * NET: Process ICMPv6 options in Router Advertisements
120  *
121  * @param  ip6_packet   pointer to an IPv6 packet
122  */
123 static void
124 process_ra_options (uint8_t *option, int32_t option_length, struct router *r)
125 {
126         while (option_length > 0) {
127                 switch (*option) {
128                         case ND_OPTION_SOURCE_LL_ADDR:
129                                 handle_source_lladdr ((struct option_ll_address *) option, r);
130                                 break;
131                         case ND_OPTION_PREFIX_INFO:
132                                 handle_prefixoption(option);
133                                 break;
134                         default:
135                                 break;
136                 }
137                 //option+1 is the length field. length is in units of 8 bytes
138                 option_length = option_length - (*(option+1) * 8);
139                 option = option + (*(option+1) * 8);
140         }
141
142         return;
143 }
144
145 /**
146  * NET: Process Router Advertisements
147  *
148  * @param  ip6_packet   pointer to an IPv6 packet
149  */
150 static void
151 handle_ra (struct icmp6hdr *icmp6h, uint8_t *ip6_packet)
152 {
153         uint8_t  *first_option;
154         int32_t option_length;
155         struct ip6hdr *ip6h;
156         struct router_advertisement *ra;
157         struct router *rtr;
158         ip6_addr_t *rtr_ip;
159         uint8_t rtr_mac[] = {0, 0, 0, 0, 0, 0};
160
161         ip6h = (struct ip6hdr *) ip6_packet;
162         ra = (struct router_advertisement *) &icmp6h->icmp6body.ra;
163         rtr_ip = (ip6_addr_t *) &ip6h->src;
164
165         rtr = find_router (&(ip6h->src));
166         if (!rtr) {
167                 rtr = router_create (rtr_mac, rtr_ip);
168                 router_add (rtr);
169         }
170
171         /* store info from router advertisement in router struct */
172         rtr->lifetime = ra->router_lifetime;
173         rtr->reachable_time = ra->reachable_time;
174         rtr->retrans_timer = ra->retrans_timer;
175
176         /* save flags concerning address (auto-) configuration */
177         ip6_state.managed_mode = ra->flags.managed;
178         ip6_state.other_config = ra->flags.other;
179
180         /* Process ICMPv6 options in Router Advertisement */
181         first_option = (uint8_t *) icmp6h + ICMPv6_HEADER_SIZE + 12;
182         option_length =  (uint8_t *) icmp6h + ip6h->pl - first_option;
183         process_ra_options( (uint8_t *) first_option, option_length, rtr);
184
185         ra_received = 1;
186 }
187
188 int is_ra_received(void)
189 {
190         return ra_received;
191 }
192
193 /**
194  * NET:
195  *
196  * @param  fd         socket fd
197  * @param  ip6_addr_t *dest_ip6
198  */
199 void
200 send_neighbour_solicitation (int fd, ip6_addr_t *dest_ip6)
201 {
202         ip6_addr_t snma;
203
204         uint8_t ether_packet[ETH_MTU_SIZE];
205         struct  packeth headers;
206
207         memset(ether_packet, 0, ETH_MTU_SIZE);
208         headers.ethh   = (struct ethhdr *) ether_packet;
209         headers.ip6h   = (struct ip6hdr *) (ether_packet + sizeof(struct ethhdr));
210         headers.icmp6h = (struct icmp6hdr *) (ether_packet +
211                           sizeof(struct ethhdr) +
212                           sizeof(struct ip6hdr));
213
214         /* Fill IPv6 header */
215         snma.part.prefix       = IPV6_SOLIC_NODE_PREFIX;
216         snma.part.interface_id = IPV6_SOLIC_NODE_IFACE_ID;
217         snma.addr[13]          = dest_ip6->addr[13];
218         snma.addr[14]          = dest_ip6->addr[14];
219         snma.addr[15]          = dest_ip6->addr[15];
220         fill_ip6hdr((uint8_t *) headers.ip6h,
221                    ICMPv6_HEADER_SIZE +
222                    sizeof(struct neighbour_solicitation),
223                    0x3a, //ICMPv6
224                    get_ipv6_address(), &snma);
225
226         /* Fill ICMPv6 message */
227         headers.icmp6h->type = ICMPV6_NEIGHBOUR_SOLICITATION;
228         headers.icmp6h->code = 0;
229         memcpy( &(headers.icmp6h->icmp6body.nghb_solicit.target),
230                 dest_ip6, IPV6_ADDR_LENGTH );
231         headers.icmp6h->icmp6body.nghb_solicit.lladdr.type    = 1;
232         headers.icmp6h->icmp6body.nghb_solicit.lladdr.length  = 1;
233         memcpy( &(headers.icmp6h->icmp6body.nghb_solicit.lladdr.mac),
234                 get_mac_address(), 6);
235
236         send_ip (fd, ether_packet + sizeof(struct ethhdr),
237                    sizeof(struct ip6hdr) + ICMPv6_HEADER_SIZE +
238                    sizeof(struct neighbour_solicitation));
239 }
240
241 /**
242  * NET:
243  *
244  * @param  fd           socket fd
245  * @param  ip6_packet   pointer to an IPv6 packet
246  * @param  icmp6hdr     pointer to the icmp6 header in ip6_packet
247  * @param  na_flags     Neighbour advertisment flags
248  */
249 static void
250 send_neighbour_advertisement (int fd, struct neighbor *target)
251 {
252         struct na_flags na_adv_flags;
253         uint8_t ether_packet[ETH_MTU_SIZE];
254         struct  packeth headers;
255
256
257         headers.ip6h   = (struct ip6hdr *) (ether_packet + sizeof(struct ethhdr));
258         headers.icmp6h = (struct icmp6hdr *) (ether_packet +
259                           sizeof(struct ethhdr) +
260                           sizeof(struct ip6hdr));
261
262         /* Fill IPv6 header */
263         fill_ip6hdr(ether_packet + sizeof(struct ethhdr),
264                    ICMPv6_HEADER_SIZE +
265                    sizeof(struct neighbour_advertisement),
266                    0x3a, //ICMPv6
267                    get_ipv6_address(), (ip6_addr_t *) &(target->ip.addr));
268
269         /* Fill ICMPv6 message */
270         memcpy( &(headers.icmp6h->icmp6body.nghb_adv.target),
271                 &(target->ip.addr), IPV6_ADDR_LENGTH );
272         headers.icmp6h->icmp6body.nghb_adv.lladdr.type    = 1;
273         headers.icmp6h->icmp6body.nghb_adv.lladdr.length  = 1;
274         memcpy( &(headers.icmp6h->icmp6body.nghb_adv.lladdr.mac),
275                 get_mac_address(), 6);
276
277         na_adv_flags.is_router = 0;
278         na_adv_flags.na_is_solicited = 1;
279         na_adv_flags.override = 1;
280
281         headers.icmp6h->type = ICMPV6_NEIGHBOUR_ADVERTISEMENT;
282         headers.icmp6h->code = 0;
283         headers.icmp6h->icmp6body.nghb_adv.router    = na_adv_flags.is_router;
284
285         headers.icmp6h->icmp6body.nghb_adv.solicited = na_adv_flags.na_is_solicited;
286         headers.icmp6h->icmp6body.nghb_adv.override  = na_adv_flags.override;
287         headers.icmp6h->icmp6body.nghb_adv.lladdr.type      = 2;
288         headers.icmp6h->icmp6body.nghb_adv.lladdr.length    = 1;
289
290         memset( &(headers.icmp6h->icmp6body.nghb_adv.target), 0,
291                 IPV6_ADDR_LENGTH );
292
293         if( na_adv_flags.na_is_solicited ) {
294                 memcpy( &(headers.icmp6h->icmp6body.nghb_adv.target),
295                         get_ipv6_address(), IPV6_ADDR_LENGTH);
296         }
297
298         memcpy( &(headers.icmp6h->icmp6body.nghb_adv.lladdr.mac),
299                 get_mac_address(), 6);
300
301         send_ip (fd, ether_packet + sizeof(struct ethhdr),
302                    sizeof(struct ip6hdr) + ICMPv6_HEADER_SIZE +
303                    sizeof(struct neighbour_advertisement));
304 }
305
306 /**
307  * NET:
308  *
309  * @param  fd           socket fd
310  * @param  ip6_packet   pointer to an IPv6 packet
311  */
312 static int8_t
313 handle_na (int fd, uint8_t *packet)
314 {
315         struct neighbor *n = NULL;
316         struct packeth headers;
317         ip6_addr_t ip;
318
319         headers.ethh = (struct ethhdr *) packet;
320         headers.ip6h = (struct ip6hdr *) ((unsigned char *) headers.ethh +
321                                                         sizeof(struct ethhdr));
322         headers.icmp6h = (struct icmp6hdr *) (packet +
323                                               sizeof(struct ethhdr) +
324                                               sizeof(struct ip6hdr));
325
326         memcpy(&(ip.addr), &(headers.ip6h->src), IPV6_ADDR_LENGTH);
327
328         n = find_neighbor (&ip);
329
330         if (!n) {
331                 n= (struct neighbor *)
332                         neighbor_create( packet, &headers );
333                 if (!n)
334                         return 0;
335                 if (!neighbor_add(n))
336                         return 0;
337         } else {
338                 memcpy (&(n->mac), &(headers.ethh->src_mac[0]), 6);
339
340                 if (n->eth_len > 0) {
341                         struct ethhdr * ethh = (struct ethhdr *) &(n->eth_frame);
342                         memcpy(ethh->dest_mac, &(n->mac), 6);
343                         send_ether (fd, &(n->eth_frame), n->eth_len + sizeof(struct ethhdr));
344                         n->eth_len = 0;
345                 }
346         }
347
348         return 1;
349 }
350
351 /**
352  * NET: Handles ICMPv6 messages
353  *
354  * @param  fd           socket fd
355  * @param  ip6_packet   pointer to an IPv6 packet
356  * @param  packetsize   size of ipv6_packet
357  */
358 int8_t
359 handle_icmpv6 (int fd, struct ethhdr *etherhdr,
360               uint8_t  *ip6_packet)
361 {
362
363         struct icmp6hdr *received_icmp6 = NULL;
364         struct ip6hdr *received_ip6     = NULL;
365         struct neighbor target;
366
367         received_ip6 =   (struct ip6hdr *) ip6_packet;
368         received_icmp6 = (struct icmp6hdr *) (ip6_packet +
369                           sizeof(struct ip6hdr));
370         memcpy( &(target.ip.addr), &(received_ip6->src),
371                 IPV6_ADDR_LENGTH );
372         memcpy( &(target.mac), etherhdr->src_mac, 6);
373
374         /* process ICMPv6 types */
375         switch(received_icmp6->type) {
376                 case ICMPV6_NEIGHBOUR_SOLICITATION:
377                         send_neighbour_advertisement(fd, &target);
378                         break;
379                 case ICMPV6_NEIGHBOUR_ADVERTISEMENT:
380                         handle_na(fd, (uint8_t *) ip6_packet - sizeof(struct ethhdr));
381                         break;
382                 case ICMPV6_ROUTER_ADVERTISEMENT:
383                         handle_ra(received_icmp6, (uint8_t *) received_ip6);
384                         break;
385                 default:
386                         return -1;
387         }
388
389         return 1;
390 }