ip.c 19.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2010,2011  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

19 20 21
#include <grub/net/ip.h>
#include <grub/misc.h>
#include <grub/net/arp.h>
22
#include <grub/net/udp.h>
23 24 25 26
#include <grub/net/ethernet.h>
#include <grub/net.h>
#include <grub/net/netbuff.h>
#include <grub/mm.h>
27 28
#include <grub/priority_queue.h>
#include <grub/time.h>
29

30 31 32 33 34 35 36 37 38 39
struct iphdr {
  grub_uint8_t verhdrlen;
  grub_uint8_t service;
  grub_uint16_t len;
  grub_uint16_t ident;
  grub_uint16_t frags;
  grub_uint8_t ttl;
  grub_uint8_t protocol;
  grub_uint16_t chksum;
  grub_uint32_t src;
40
  grub_uint32_t dest;
41
} GRUB_PACKED ;
42

43
enum
44
{
45 46 47 48 49
  DONT_FRAGMENT =  0x4000,
  MORE_FRAGMENTS = 0x2000,
  OFFSET_MASK =    0x1fff
};

50 51 52 53 54 55 56 57 58
typedef grub_uint64_t ip6addr[2];

struct ip6hdr {
  grub_uint32_t version_class_flow;
  grub_uint16_t len;
  grub_uint8_t protocol;
  grub_uint8_t ttl;
  ip6addr src;
  ip6addr dest;
59
} GRUB_PACKED ;
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
static int
cmp (const void *a__, const void *b__)
{
  struct grub_net_buff *a_ = *(struct grub_net_buff **) a__;
  struct grub_net_buff *b_ = *(struct grub_net_buff **) b__;
  struct iphdr *a = (struct iphdr *) a_->data;
  struct iphdr *b = (struct iphdr *) b_->data;
  /* We want the first elements to be on top.  */
  if ((grub_be_to_cpu16 (a->frags) & OFFSET_MASK)
      < (grub_be_to_cpu16 (b->frags) & OFFSET_MASK))
    return +1;
  if ((grub_be_to_cpu16 (a->frags) & OFFSET_MASK)
      > (grub_be_to_cpu16 (b->frags) & OFFSET_MASK))
    return -1;
  return 0;
}

struct reassemble
{
  struct reassemble *next;
  grub_uint32_t source;
  grub_uint32_t dest;
  grub_uint16_t id;
  grub_uint8_t proto;
  grub_uint64_t last_time;
  grub_priority_queue_t pq;
87
  struct grub_net_buff *asm_netbuff;
88 89
  grub_size_t total_len;
  grub_size_t cur_ptr;
90
  grub_uint8_t ttl;
91 92
};

93
static struct reassemble *reassembles;
94

95
grub_uint16_t
96
grub_net_ip_chksum (void *ipv, grub_size_t len)
97
{
98 99
  grub_uint16_t *ip = (grub_uint16_t *) ipv;
  grub_uint32_t sum = 0;
100

101
  for (; len >= 2; len -= 2)
102
    {
103 104
      sum += grub_be_to_cpu16 (grub_get_unaligned16 (ip++));
      if (sum > 0xFFFF)
105 106
	sum -= 0xFFFF;
    }
107
  if (len)
108
    {
109
      sum += *((grub_uint8_t *) ip) << 8;
110 111 112
      if (sum > 0xFFFF)
	sum -= 0xFFFF;
    }
113

114 115 116
  if (sum >= 0xFFFF)
    sum -= 0xFFFF;

117
  return grub_cpu_to_be16 ((~sum) & 0x0000FFFF);
118 119
}

120 121 122 123 124 125 126 127
static int id = 0x2400;

static grub_err_t
send_fragmented (struct grub_net_network_level_interface * inf,
		 const grub_net_network_level_address_t * target,
		 struct grub_net_buff * nb,
		 grub_net_ip_protocol_t proto,
		 grub_net_link_level_address_t ll_target_addr)
128
{
129 130
  grub_size_t off = 0;
  grub_size_t fraglen;
131
  grub_err_t err;
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
  fraglen = (inf->card->mtu - sizeof (struct iphdr)) & ~7;
  id++;

  while (nb->tail - nb->data)
    {
      grub_size_t len = fraglen;
      struct grub_net_buff *nb2;
      struct iphdr *iph;

      if ((grub_ssize_t) len > nb->tail - nb->data)
	len = nb->tail - nb->data;
      nb2 = grub_netbuff_alloc (fraglen + sizeof (struct iphdr)
				+ GRUB_NET_MAX_LINK_HEADER_SIZE);
      if (!nb2)
	return grub_errno;
      err = grub_netbuff_reserve (nb2, GRUB_NET_MAX_LINK_HEADER_SIZE);
      if (err)
	return err;
      err = grub_netbuff_put (nb2, sizeof (struct iphdr));
      if (err)
	return err;

      iph = (struct iphdr *) nb2->data;
      iph->verhdrlen = ((4 << 4) | 5);
      iph->service = 0;
      iph->len = grub_cpu_to_be16 (len + sizeof (struct iphdr));
      iph->ident = grub_cpu_to_be16 (id);
      iph->frags = grub_cpu_to_be16 (off | (((grub_ssize_t) len 
					     == nb->tail - nb->data)
					    ? 0 : MORE_FRAGMENTS));
      iph->ttl = 0xff;
      iph->protocol = proto;
      iph->src = inf->address.ipv4;
      iph->dest = target->ipv4;
      off += len / 8;

      iph->chksum = 0;
      iph->chksum = grub_net_ip_chksum ((void *) nb2->data, sizeof (*iph));
      err = grub_netbuff_put (nb2, len);
      if (err)
	return err;
      grub_memcpy (iph + 1, nb->data, len);
      err = grub_netbuff_pull (nb, len);
      if (err)
	return err;
      err = send_ethernet_packet (inf, nb2, ll_target_addr,
				  GRUB_NET_ETHERTYPE_IP);
      if (err)
	return err;
    }
  return GRUB_ERR_NONE;
}

186
static grub_err_t
187 188
grub_net_send_ip4_packet (struct grub_net_network_level_interface *inf,
			  const grub_net_network_level_address_t *target,
189
			  const grub_net_link_level_address_t *ll_target_addr,
190
			  struct grub_net_buff *nb,
191
			  grub_net_ip_protocol_t proto)
192
{
193
  struct iphdr *iph;
194
  grub_err_t err;
195

196 197 198
  COMPILE_TIME_ASSERT (GRUB_NET_OUR_IPV4_HEADER_SIZE == sizeof (*iph));

  if (nb->tail - nb->data + sizeof (struct iphdr) > inf->card->mtu)
199
    return send_fragmented (inf, target, nb, proto, *ll_target_addr);
200

201 202 203
  err = grub_netbuff_push (nb, sizeof (*iph));
  if (err)
    return err;
204

205
  iph = (struct iphdr *) nb->data;
206 207
  iph->verhdrlen = ((4 << 4) | 5);
  iph->service = 0;
208
  iph->len = grub_cpu_to_be16 (nb->tail - nb->data);
209 210 211
  iph->ident = grub_cpu_to_be16 (++id);
  iph->frags = 0;
  iph->ttl = 0xff;
212
  iph->protocol = proto;
213 214
  iph->src = inf->address.ipv4;
  iph->dest = target->ipv4;
215 216

  iph->chksum = 0;
217
  iph->chksum = grub_net_ip_chksum ((void *) nb->data, sizeof (*iph));
218

219
  return send_ethernet_packet (inf, nb, *ll_target_addr,
220
			       GRUB_NET_ETHERTYPE_IP);
221
}
222

223 224
static grub_err_t
handle_dgram (struct grub_net_buff *nb,
225
	      struct grub_net_card *card,
226
	      const grub_net_link_level_address_t *source_hwaddress,
227
	      const grub_net_link_level_address_t *hwaddress,
228 229
	      grub_net_ip_protocol_t proto,
	      const grub_net_network_level_address_t *source,
230 231
	      const grub_net_network_level_address_t *dest,
	      grub_uint8_t ttl)
232
{
233
  struct grub_net_network_level_interface *inf = NULL;
234
  grub_err_t err;
235
  int multicast = 0;
236
  
237
  /* DHCP needs special treatment since we don't know IP yet.  */
238
  {
239 240
    struct udphdr *udph;
    udph = (struct udphdr *) nb->data;
241
    if (proto == GRUB_NET_IP_UDP && grub_be_to_cpu16 (udph->dst) == 68)
242
      {
243
	const struct grub_net_bootp_packet *bootp;
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	if (udph->chksum)
	  {
	    grub_uint16_t chk, expected;
	    chk = udph->chksum;
	    udph->chksum = 0;
	    expected = grub_net_ip_transport_checksum (nb,
						       GRUB_NET_IP_UDP,
						       source,
						       dest);
	    if (expected != chk)
	      {
		grub_dprintf ("net", "Invalid UDP checksum. "
			      "Expected %x, got %x\n", 
			      grub_be_to_cpu16 (expected),
			      grub_be_to_cpu16 (chk));
		grub_netbuff_free (nb);
		return GRUB_ERR_NONE;
	      }
	    udph->chksum = chk;
	  }

	err = grub_netbuff_pull (nb, sizeof (*udph));
	if (err)
	  {
	    grub_netbuff_free (nb);
	    return err;
	  }
271 272

	bootp = (const struct grub_net_bootp_packet *) nb->data;
273
	
274 275 276
	FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
	  if (inf->card == card
	      && inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV
277
	      && inf->hwaddress.type == GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET
278
	      && grub_memcmp (inf->hwaddress.mac, &bootp->mac_addr,
279
			      sizeof (inf->hwaddress.mac)) == 0)
280 281 282
	    {
	      grub_net_process_dhcp (nb, inf->card);
	      grub_netbuff_free (nb);
283
	      return GRUB_ERR_NONE;
284
	    }
285
	grub_netbuff_free (nb);
286 287
	return GRUB_ERR_NONE;
      }
288
  }
289

290 291 292
  FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
  {
    if (inf->card == card
293
	&& grub_net_addr_cmp (&inf->address, dest) == 0
294 295
	&& grub_net_hwaddr_cmp (&inf->hwaddress, hwaddress) == 0)
      break;
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    /* Solicited node multicast.  */
    if (inf->card == card
	&& inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6
	&& dest->type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6
	&& dest->ipv6[0] == grub_be_to_cpu64_compile_time (0xff02ULL << 48)
	&& dest->ipv6[1] == (grub_be_to_cpu64_compile_time (0x01ff000000ULL)
			     | (inf->address.ipv6[1]
				& grub_be_to_cpu64_compile_time (0xffffff)))
	&& hwaddress->type == GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET
	&& hwaddress->mac[0] == 0x33 && hwaddress->mac[1] == 0x33
	&& hwaddress->mac[2] == 0xff
	&& hwaddress->mac[3] == ((grub_be_to_cpu64 (inf->address.ipv6[1]) 
				  >> 16) & 0xff)
	&& hwaddress->mac[4] == ((grub_be_to_cpu64 (inf->address.ipv6[1])
				  >> 8) & 0xff)
	&& hwaddress->mac[5] == ((grub_be_to_cpu64 (inf->address.ipv6[1])
				  >> 0) & 0xff))
313
      {
314 315
	multicast = 1;
	break;
316
      }
317 318
  }
 
319
  if (!inf && !(dest->type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6
320 321 322
		&& dest->ipv6[0] == grub_be_to_cpu64_compile_time (0xff02ULL
								   << 48)
		&& dest->ipv6[1] == grub_be_to_cpu64_compile_time (1)))
323 324 325
    {
      grub_netbuff_free (nb);
      return GRUB_ERR_NONE;
326
    }
327 328
  if (multicast)
    inf = NULL;
329

330
  switch (proto)
331
    {
332
    case GRUB_NET_IP_UDP:
333
      return grub_net_recv_udp_packet (nb, inf, source);
334
    case GRUB_NET_IP_TCP:
335
      return grub_net_recv_tcp_packet (nb, inf, source);
336
    case GRUB_NET_IP_ICMP:
337
      return grub_net_recv_icmp_packet (nb, inf, source_hwaddress, source);
338
    case GRUB_NET_IP_ICMPV6:
339 340
      return grub_net_recv_icmp6_packet (nb, card, inf, source_hwaddress,
					 source, dest, ttl);
341 342
    default:
      grub_netbuff_free (nb);
343 344
      break;
    }
345 346 347 348 349 350 351 352 353 354 355 356
  return GRUB_ERR_NONE;
}

static void
free_rsm (struct reassemble *rsm)
{
  struct grub_net_buff **nb;
  while ((nb = grub_priority_queue_top (rsm->pq)))
    {
      grub_netbuff_free (*nb);
      grub_priority_queue_pop (rsm->pq);
    }
357
  grub_netbuff_free (rsm->asm_netbuff);
358
  grub_priority_queue_destroy (rsm->pq);
359
  grub_free (rsm);
360 361 362 363 364 365 366 367
}

static void
free_old_fragments (void)
{
  struct reassemble *rsm, **prev;
  grub_uint64_t limit_time = grub_get_time_ms () - 90000;

368
  for (prev = &reassembles, rsm = *prev; rsm; rsm = *prev)
369 370 371 372 373
    if (rsm->last_time < limit_time)
      {
	*prev = rsm->next;
	free_rsm (rsm);
      }
374 375 376 377
    else
      {
	prev = &rsm->next;
      }
378 379
}

380
static grub_err_t
381 382 383 384
grub_net_recv_ip4_packets (struct grub_net_buff *nb,
			   struct grub_net_card *card,
			   const grub_net_link_level_address_t *hwaddress,
			   const grub_net_link_level_address_t *src_hwaddress)
385 386 387
{
  struct iphdr *iph = (struct iphdr *) nb->data;
  grub_err_t err;
388
  struct reassemble *rsm, **prev;
389

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
  if ((iph->verhdrlen >> 4) != 4)
    {
      grub_dprintf ("net", "Bad IP version: %d\n", (iph->verhdrlen >> 4));
      grub_netbuff_free (nb);
      return GRUB_ERR_NONE;
    }

  if ((iph->verhdrlen & 0xf) < 5)
    {
      grub_dprintf ("net", "IP header too short: %d\n",
		    (iph->verhdrlen & 0xf));
      grub_netbuff_free (nb);
      return GRUB_ERR_NONE;
    }

405 406
  if (nb->tail - nb->data < (grub_ssize_t) ((iph->verhdrlen & 0xf)
					    * sizeof (grub_uint32_t)))
407
    {
408
      grub_dprintf ("net", "IP packet too short: %" PRIdGRUB_SSIZE "\n",
409
		    (grub_ssize_t) (nb->tail - nb->data));
410
      grub_netbuff_free (nb);
411
      return GRUB_ERR_NONE;
412 413
    }

414
  /* Check size.  */
415 416
  {
    grub_size_t expected_size = grub_be_to_cpu16 (iph->len);
417
    grub_size_t actual_size = (nb->tail - nb->data);
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    if (actual_size > expected_size)
      {
	err = grub_netbuff_unput (nb, actual_size - expected_size);
	if (err)
	  {
	    grub_netbuff_free (nb);
	    return err;
	  }
      }
    if (actual_size < expected_size)
      {
	grub_dprintf ("net", "Cut IP packet actual: %" PRIuGRUB_SIZE 
		      ", expected %" PRIuGRUB_SIZE "\n", actual_size,
		      expected_size);
	grub_netbuff_free (nb);
	return GRUB_ERR_NONE;
      }
  }

437 438 439
  /* Unfragmented packet. Good.  */
  if (((grub_be_to_cpu16 (iph->frags) & MORE_FRAGMENTS) == 0)
      && (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK) == 0)
440
    {
441 442 443
      grub_net_network_level_address_t source;
      grub_net_network_level_address_t dest;

444 445 446 447 448 449 450
      err = grub_netbuff_pull (nb, ((iph->verhdrlen & 0xf)
				    * sizeof (grub_uint32_t)));
      if (err)
	{
	  grub_netbuff_free (nb);
	  return err;
	}
451 452 453 454 455 456 457

      source.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
      source.ipv4 = iph->src;

      dest.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
      dest.ipv4 = iph->dest;

458
      return handle_dgram (nb, card, src_hwaddress, hwaddress, iph->protocol,
459
			   &source, &dest, iph->ttl);
460
    }
461

462 463 464
  for (prev = &reassembles, rsm = *prev; rsm; prev = &rsm->next, rsm = *prev)
    if (rsm->source == iph->src && rsm->dest == iph->dest
	&& rsm->id == iph->ident && rsm->proto == iph->protocol)
465
      break;
466
  if (!rsm)
467
    {
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
      rsm = grub_malloc (sizeof (*rsm));
      if (!rsm)
	return grub_errno;
      rsm->source = iph->src;
      rsm->dest = iph->dest;
      rsm->id = iph->ident;
      rsm->proto = iph->protocol;
      rsm->next = reassembles;
      reassembles = rsm;
      prev = &reassembles;
      rsm->pq = grub_priority_queue_new (sizeof (struct grub_net_buff **), cmp);
      if (!rsm->pq)
	{
	  grub_free (rsm);
	  return grub_errno;
	}
484
      rsm->asm_netbuff = 0;
485 486
      rsm->total_len = 0;
      rsm->cur_ptr = 0;
487
      rsm->ttl = 0xff;
488
    }
489 490
  if (rsm->ttl > iph->ttl)
    rsm->ttl = iph->ttl;
491 492
  rsm->last_time = grub_get_time_ms ();
  free_old_fragments ();
493

494 495 496 497 498
  err = grub_priority_queue_push (rsm->pq, &nb);
  if (err)
    return err;

  if (!(grub_be_to_cpu16 (iph->frags) & MORE_FRAGMENTS))
499
    {
500 501 502
      rsm->total_len = (8 * (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK)
			+ (nb->tail - nb->data));
      rsm->total_len -= ((iph->verhdrlen & 0xf) * sizeof (grub_uint32_t));
503 504
      rsm->asm_netbuff = grub_netbuff_alloc (rsm->total_len);
      if (!rsm->asm_netbuff)
505 506 507 508 509 510
	{
	  *prev = rsm->next;
	  free_rsm (rsm);
	  return grub_errno;
	}
    }
511
  if (!rsm->asm_netbuff)
512 513 514 515 516 517 518 519 520 521 522
    return GRUB_ERR_NONE;

  while (1)
    {
      struct grub_net_buff **nb_top_p, *nb_top;
      grub_size_t copy;
      grub_size_t res_len;
      struct grub_net_buff *ret;
      grub_net_ip_protocol_t proto;
      grub_uint32_t src;
      grub_uint32_t dst;
523 524
      grub_net_network_level_address_t source;
      grub_net_network_level_address_t dest;
525
      grub_uint8_t ttl;
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541

      nb_top_p = grub_priority_queue_top (rsm->pq);
      if (!nb_top_p)
	return GRUB_ERR_NONE;
      nb_top = *nb_top_p;
      grub_priority_queue_pop (rsm->pq);
      iph = (struct iphdr *) nb_top->data;
      err = grub_netbuff_pull (nb_top, ((iph->verhdrlen & 0xf)
					* sizeof (grub_uint32_t)));
      if (err)
	{
	  grub_netbuff_free (nb_top);
	  return err;
	}
      if (rsm->cur_ptr < (grub_size_t) 8 * (grub_be_to_cpu16 (iph->frags)
					    & OFFSET_MASK))
542 543 544 545
	{
	  grub_netbuff_free (nb_top);
	  return GRUB_ERR_NONE;
	}
546 547 548 549 550 551 552 553 554 555 556 557 558 559

      rsm->cur_ptr = (8 * (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK)
		      + (nb_top->tail - nb_top->head));
      if ((grub_size_t) 8 * (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK)
	  >= rsm->total_len)
	{
	  grub_netbuff_free (nb_top);
	  continue;
	}
      copy = nb_top->tail - nb_top->data;
      if (rsm->total_len - 8 * (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK)
	  < copy)
	copy = rsm->total_len - 8 * (grub_be_to_cpu16 (iph->frags)
				     & OFFSET_MASK);
560 561
      grub_memcpy (&rsm->asm_netbuff->data[8 * (grub_be_to_cpu16 (iph->frags)
						& OFFSET_MASK)],
562 563 564
		   nb_top->data, copy);

      if ((grub_be_to_cpu16 (iph->frags) & MORE_FRAGMENTS))
565 566 567 568 569 570 571
	{
	  grub_netbuff_free (nb_top);
	  continue;
	}
      grub_netbuff_free (nb_top);

      ret = rsm->asm_netbuff;
572 573 574
      proto = rsm->proto;
      src = rsm->source;
      dst = rsm->dest;
575
      ttl = rsm->ttl;
576

577
      rsm->asm_netbuff = 0;
578 579 580
      res_len = rsm->total_len;
      *prev = rsm->next;
      free_rsm (rsm);
581 582

      if (grub_netbuff_put (ret, res_len))
583
	{
584 585
	  grub_netbuff_free (ret);
	  return GRUB_ERR_NONE;
586
	}
587 588 589 590 591 592 593

      source.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
      source.ipv4 = src;

      dest.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
      dest.ipv4 = dst;

594 595
      return handle_dgram (ret, card, src_hwaddress,
			   hwaddress, proto, &source, &dest,
596
			   ttl);
597
    }
598
}
599 600

static grub_err_t
601 602
grub_net_send_ip6_packet (struct grub_net_network_level_interface *inf,
			  const grub_net_network_level_address_t *target,
603
			  const grub_net_link_level_address_t *ll_target_addr,
604
			  struct grub_net_buff *nb,
605 606 607
			  grub_net_ip_protocol_t proto)
{
  struct ip6hdr *iph;
608
  grub_err_t err;
609 610 611 612 613 614

  COMPILE_TIME_ASSERT (GRUB_NET_OUR_IPV6_HEADER_SIZE == sizeof (*iph));

  if (nb->tail - nb->data + sizeof (struct iphdr) > inf->card->mtu)
    return grub_error (GRUB_ERR_NET_PACKET_TOO_BIG, "packet too big");

615 616 617
  err = grub_netbuff_push (nb, sizeof (*iph));
  if (err)
    return err;
618

619
  iph = (struct ip6hdr *) nb->data;
620
  iph->version_class_flow = grub_cpu_to_be32_compile_time ((6 << 28));
621
  iph->len = grub_cpu_to_be16 (nb->tail - nb->data - sizeof (*iph));
622 623 624 625 626
  iph->protocol = proto;
  iph->ttl = 0xff;
  grub_memcpy (&iph->src, inf->address.ipv6, sizeof (iph->src));
  grub_memcpy (&iph->dest, target->ipv6, sizeof (iph->dest));

627
  return send_ethernet_packet (inf, nb, *ll_target_addr,
628
			       GRUB_NET_ETHERTYPE_IP6);
629 630 631
}

grub_err_t
632 633
grub_net_send_ip_packet (struct grub_net_network_level_interface *inf,
			 const grub_net_network_level_address_t *target,
634
			 const grub_net_link_level_address_t *ll_target_addr,
635
			 struct grub_net_buff *nb,
636 637 638 639 640
			 grub_net_ip_protocol_t proto)
{
  switch (target->type)
    {
    case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4:
641
      return grub_net_send_ip4_packet (inf, target, ll_target_addr, nb, proto);
642
    case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6:
643
      return grub_net_send_ip6_packet (inf, target, ll_target_addr, nb, proto);
644
    default:
645
      return grub_error (GRUB_ERR_BUG, "not an IP");
646 647 648
    }
}

649
static grub_err_t
650 651 652 653
grub_net_recv_ip6_packets (struct grub_net_buff *nb,
			   struct grub_net_card *card,
			   const grub_net_link_level_address_t *hwaddress,
			   const grub_net_link_level_address_t *src_hwaddress)
654 655 656 657 658 659 660 661 662
{
  struct ip6hdr *iph = (struct ip6hdr *) nb->data;
  grub_err_t err;
  grub_net_network_level_address_t source;
  grub_net_network_level_address_t dest;

  if (nb->tail - nb->data < (grub_ssize_t) sizeof (*iph))
    {
      grub_dprintf ("net", "IP packet too short: %" PRIdGRUB_SSIZE "\n",
663
		    (grub_ssize_t) (nb->tail - nb->data));
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
      grub_netbuff_free (nb);
      return GRUB_ERR_NONE;
    }

  err = grub_netbuff_pull (nb, sizeof (*iph));
  if (err)
    {
      grub_netbuff_free (nb);
      return err;
    }

  /* Check size.  */
  {
    grub_size_t expected_size = grub_be_to_cpu16 (iph->len);
    grub_size_t actual_size = (nb->tail - nb->data);
    if (actual_size > expected_size)
      {
	err = grub_netbuff_unput (nb, actual_size - expected_size);
	if (err)
	  {
	    grub_netbuff_free (nb);
	    return err;
	  }
      }
    if (actual_size < expected_size)
      {
	grub_dprintf ("net", "Cut IP packet actual: %" PRIuGRUB_SIZE 
		      ", expected %" PRIuGRUB_SIZE "\n", actual_size,
		      expected_size);
	grub_netbuff_free (nb);
	return GRUB_ERR_NONE;
      }
  }

  source.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
  dest.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
  grub_memcpy (source.ipv6, &iph->src, sizeof (source.ipv6));
701
  grub_memcpy (dest.ipv6, &iph->dest, sizeof (dest.ipv6));
702

703
  return handle_dgram (nb, card, src_hwaddress, hwaddress, iph->protocol,
704
		       &source, &dest, iph->ttl);
705
}
706 707

grub_err_t
708 709 710 711
grub_net_recv_ip_packets (struct grub_net_buff *nb,
			  struct grub_net_card *card,
			  const grub_net_link_level_address_t *hwaddress,
			  const grub_net_link_level_address_t *src_hwaddress)
712 713
{
  struct iphdr *iph = (struct iphdr *) nb->data;
714

715
  if ((iph->verhdrlen >> 4) == 4)
716
    return grub_net_recv_ip4_packets (nb, card, hwaddress, src_hwaddress);
717
  if ((iph->verhdrlen >> 4) == 6)
718
    return grub_net_recv_ip6_packets (nb, card, hwaddress, src_hwaddress);
719 720
  grub_dprintf ("net", "Bad IP version: %d\n", (iph->verhdrlen >> 4));
  grub_netbuff_free (nb);
721 722
  return GRUB_ERR_NONE;
}