1 /******************************************************************************
2 * Copyright (c) 2013 IBM Corporation
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
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
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>
24 static int ra_received = 0;
31 send_router_solicitation (int fd)
34 uint8_t ether_packet[ETH_MTU_SIZE];
35 struct packeth headers;
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));
42 /* Destination is "All routers multicast address" (link-local) */
43 dest_addr.part.prefix = 0xff02000000000000ULL;
44 dest_addr.part.interface_id = 2;
46 /* Fill IPv6 header */
47 fill_ip6hdr (ether_packet + sizeof(struct ethhdr),
48 ICMPv6_HEADER_SIZE + sizeof(struct router_solicitation),
50 get_ipv6_address(), &dest_addr);
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);
60 send_ip (fd, headers.ip6h, sizeof(struct ip6hdr) +
61 ICMPv6_HEADER_SIZE + sizeof(struct router_solicitation));
65 * NET: Process prefix option in Router Advertisements
67 * @param ip6_packet pointer to an IPv6 packet
70 handle_prefixoption (uint8_t *option)
73 struct ip6addr_list_entry *new_address;
74 struct option_prefix *prefix_option;
75 struct prefix_info *prfx_info;
77 prefix_option = (struct option_prefix *) option;
78 memcpy( &(prefix.addr), &(prefix_option->prefix.addr), IPV6_ADDR_LENGTH);
80 /* Link-local adresses in RAs are nonsense */
81 if (ip6_is_linklocal(&prefix))
84 if (prefix_option->preferred_lifetime > prefix_option->valid_lifetime)
87 /* Add address created from prefix to IPv6 address list */
88 new_address = ip6_prefix2addr (prefix);
92 /* Process only prefixes we don't already have an adress from */
93 if (!unknown_prefix (&new_address->addr)) {
97 /* Fill struct prefix_info from data in RA and store it in new_address */
98 prfx_info = ip6_create_prefix_info();
101 memcpy (&(new_address->prfx_info), prfx_info, sizeof(struct prefix_info));
103 /* Add prefix received in RA to list of known prefixes */
104 ip6addr_add (new_address);
108 * NET: Process source link layer addresses in Router Advertisements
110 * @param ip6_packet pointer to an IPv6 packet
113 handle_source_lladdr ( struct option_ll_address *option, struct router *rtr)
115 memcpy (&(rtr->mac), &(option->mac), 6);
119 * NET: Process ICMPv6 options in Router Advertisements
121 * @param ip6_packet pointer to an IPv6 packet
124 process_ra_options (uint8_t *option, int32_t option_length, struct router *r)
126 while (option_length > 0) {
128 case ND_OPTION_SOURCE_LL_ADDR:
129 handle_source_lladdr ((struct option_ll_address *) option, r);
131 case ND_OPTION_PREFIX_INFO:
132 handle_prefixoption(option);
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);
146 * NET: Process Router Advertisements
148 * @param ip6_packet pointer to an IPv6 packet
151 handle_ra (struct icmp6hdr *icmp6h, uint8_t *ip6_packet)
153 uint8_t *first_option;
154 int32_t option_length;
156 struct router_advertisement *ra;
159 uint8_t rtr_mac[] = {0, 0, 0, 0, 0, 0};
161 ip6h = (struct ip6hdr *) ip6_packet;
162 ra = (struct router_advertisement *) &icmp6h->icmp6body.ra;
163 rtr_ip = (ip6_addr_t *) &ip6h->src;
165 rtr = find_router (&(ip6h->src));
167 rtr = router_create (rtr_mac, rtr_ip);
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;
176 /* save flags concerning address (auto-) configuration */
177 ip6_state.managed_mode = ra->flags.managed;
178 ip6_state.other_config = ra->flags.other;
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);
188 int is_ra_received(void)
196 * @param fd socket fd
197 * @param ip6_addr_t *dest_ip6
200 send_neighbour_solicitation (int fd, ip6_addr_t *dest_ip6)
204 uint8_t ether_packet[ETH_MTU_SIZE];
205 struct packeth headers;
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));
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,
222 sizeof(struct neighbour_solicitation),
224 get_ipv6_address(), &snma);
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);
236 send_ip (fd, ether_packet + sizeof(struct ethhdr),
237 sizeof(struct ip6hdr) + ICMPv6_HEADER_SIZE +
238 sizeof(struct neighbour_solicitation));
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
250 send_neighbour_advertisement (int fd, struct neighbor *target)
252 struct na_flags na_adv_flags;
253 uint8_t ether_packet[ETH_MTU_SIZE];
254 struct packeth headers;
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));
262 /* Fill IPv6 header */
263 fill_ip6hdr(ether_packet + sizeof(struct ethhdr),
265 sizeof(struct neighbour_advertisement),
267 get_ipv6_address(), (ip6_addr_t *) &(target->ip.addr));
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);
277 na_adv_flags.is_router = 0;
278 na_adv_flags.na_is_solicited = 1;
279 na_adv_flags.override = 1;
281 headers.icmp6h->type = ICMPV6_NEIGHBOUR_ADVERTISEMENT;
282 headers.icmp6h->code = 0;
283 headers.icmp6h->icmp6body.nghb_adv.router = na_adv_flags.is_router;
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;
290 memset( &(headers.icmp6h->icmp6body.nghb_adv.target), 0,
293 if( na_adv_flags.na_is_solicited ) {
294 memcpy( &(headers.icmp6h->icmp6body.nghb_adv.target),
295 get_ipv6_address(), IPV6_ADDR_LENGTH);
298 memcpy( &(headers.icmp6h->icmp6body.nghb_adv.lladdr.mac),
299 get_mac_address(), 6);
301 send_ip (fd, ether_packet + sizeof(struct ethhdr),
302 sizeof(struct ip6hdr) + ICMPv6_HEADER_SIZE +
303 sizeof(struct neighbour_advertisement));
309 * @param fd socket fd
310 * @param ip6_packet pointer to an IPv6 packet
313 handle_na (int fd, uint8_t *packet)
315 struct neighbor *n = NULL;
316 struct packeth headers;
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));
326 memcpy(&(ip.addr), &(headers.ip6h->src), IPV6_ADDR_LENGTH);
328 n = find_neighbor (&ip);
331 n= (struct neighbor *)
332 neighbor_create( packet, &headers );
335 if (!neighbor_add(n))
338 memcpy (&(n->mac), &(headers.ethh->src_mac[0]), 6);
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));
352 * NET: Handles ICMPv6 messages
354 * @param fd socket fd
355 * @param ip6_packet pointer to an IPv6 packet
356 * @param packetsize size of ipv6_packet
359 handle_icmpv6 (int fd, struct ethhdr *etherhdr,
363 struct icmp6hdr *received_icmp6 = NULL;
364 struct ip6hdr *received_ip6 = NULL;
365 struct neighbor target;
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),
372 memcpy( &(target.mac), etherhdr->src_mac, 6);
374 /* process ICMPv6 types */
375 switch(received_icmp6->type) {
376 case ICMPV6_NEIGHBOUR_SOLICITATION:
377 send_neighbour_advertisement(fd, &target);
379 case ICMPV6_NEIGHBOUR_ADVERTISEMENT:
380 handle_na(fd, (uint8_t *) ip6_packet - sizeof(struct ethhdr));
382 case ICMPV6_ROUTER_ADVERTISEMENT:
383 handle_ra(received_icmp6, (uint8_t *) received_ip6);