Routing

The Problem
===========

How do we get packets from A to B?

Assumptions
===========

Hosts
- have interfaces on to a subnet

Subnets
- each host on a subnet has the ability to communicate with any other

Routers
- hosts can have multiple interfaces
- put these on different subnets
- then machine can relay packets from hosts on one of its subnets
  to hosts on the other 

So:
- can view the network as an arbitrary topology of subnets connected
  by routers

High Level Solution
===================

Addresses
- first clue given by the problem: need to be able to name A and B
- need some way to identify the interfaces that the hosts have
- assign them a number, for now assume it can be assigned randomly
  (but doesn't have to be)

Routing Tables
- so once you've got things addressed a simple and obvious way to 
  think about things pops
- assume each node has a list of *all* the addresses,
  and where to send packets for that address

Router Behavior
- receive packet
- look at destination address
- figure out where to send it to (local, other node on subnet)
- send it there
- gets repeated until it gets to the destination

Hierarchical Addressing
=======================

Problem
- keeping a full table is too big (millions of nodes)
- perhaps possible for routers, but for end hosts!?

Aggregates
- provide a way to associate topologically-close groups of nodes
- within a group, nodes know the detailed location of hosts in the group
- outside of a group, nodes only know the way to the group
- basic unit of aggregation: the subnet, since all hosts on a subnet
  already sort-of know about each other
- other units: any cluster of subnets

Implementation
- prefixes
- that is, related addresses start with the same prefix
- "broad" routes for large groups of addresses will have short prefixes
- more specific routers for smaller groups will have longer prefixes
- not just a pure table lookup: now need to make "longest match"
- radix tree

Results
- nodes are *really* simple: 
  - need to know who they are
  - need to know what subnet they're on
  - everything else is just "out there", past a router on their subnet
- routers:
  - don't have to deal with every host
  - just subnets
  - or, groups of subnets i.e. "rest of the internet", "the company network"

IPv4 Addressing
===============
- now we'll go more lower level, and explain how addresses actually work in
  IPv4

What Gets Addressed
- each destination on the Internet gets an address
- destinations are host interfaces, not hosts
- hosts can have multiple interfaces

Address Format
- 32 bits (4 bytes) long
- 2^32 = 4294967296 (4.2 billion) combinations
- commonly written in "dotted quads", but of course there are other reps
- ex: 18.70.0.158 (to base 2 by quad)
      /  |   \___\____________     (to base 10)
00010010 01000110 00000000 10011110 = 306577566 {address}

Expressing Prefixes
- use something called a netmask
- it's a left aligned bitmap
- sometimes see it written out as something like 255.255.0.0, but can just
  say "16 bit bitmask", or /16


Filling the Table
=================
(On Hosts)

Loopback address (one per machine) - hosts & routers
- for testing
- doesn't leave machine 
- 127.0.0.1
- uses loopback interface
- adds entry: 127.0.0.1 via lo0

Netmask (one per interface) - hosts & routers
- tells you what's local and what's not 
- notation for expressing netmask same as IP prefix
- what do you know - notation not the only thing the same - netmask
  implies routing information!
- adds entry: 18.70 via (interface names)

Default route (one per machine) - hosts & most routers
- matches as a last resort / if nothing else matches / least specific route
- typically represented by 0.0.0.0/0
- a small subset of routers (with a lot (eg: 64mb) of memory!) on the Internet
  don't have a default, since they know all the routes they need to
- either configured in machine (just like IP address) or use:

Router Discovery - RFC1256 - hosts
- if there's more than one choice for default, or if you don't want to
  fix the default router's IP address
- routers announce their existance via periodic multicast ICMP messages with
  a preference number 
- hosts use most preferred router as default
- not widely used

ICMP Redirects - RFC792
- if there's multiple routers, your default may not actually be the router
  that a packet for a particular destination ought to go through
- the router will still forward the packet to the right destination, but
  may also send you an ICMP redirect, telling you the router that the packet
  should have been sent to
- you can then add that to your routing table, and save yourself a hop
- how does the router know? it knows if it forwards the packet out the same
  interface it came in
- Only interesting between endhosts and their next-hop routers.

Routing protocols
=================
- hosts shouldn't really do this, but some do
- routers speak routing protocols with each other
- dynamic routing, adjustment during failure, optimal paths, policy routing,
  reduced administration
- Two main kinds: Internal (in an Autonomous System)
  and External (between ASes)
- Autonomous System / AS: set of routers under common admin control
- internal (optimization of path length): RIP, RIPv2, OSPF, ISIS
- external (policy): BGP
- {int-ext-route}

Internal Routing
- amongst the routers of one AS
- routers share routing information with each other in order to find the
  "optimal" route
- "optimal" is with reference to a metric
- static routing
- distance vector (RIP, RIPv2) or link-state (OSPF, ISIS)

Distance vector (RIP/RIPv2)
- a.k.a. Bellman-Ford algorithm
- metric is number of hops (distance)
- tell your neighbours about all the networks you know about, and how far
  away they are
- neighbours add their distance to you to the number, and keep if they
  don't have a route to the network, or your route is a lower cost route
(need to firm up details - look at Huitema)
- XXX Split Horizon and Poison Reverse

Link state (OSPF, ISIS)
- tell neighbours about all the links you are connected to
- eventually all routers know about all links, and have a link-state
  database (matrix of metrics) [diagram]
- each router runs Dijkstra's Shortest Path algorithm to work out the
  optimal path to all destinations, and records the next hop

External Routing (BGP - RFC1771)
- chooses routes acording to policy
- policies can be economic (only send routes to people who pay me),
  political (only send routes to universities), or technical (send this
  group of routes to an AS, since we're closest to them)





