chap 6. ip addressing 6.1 Introduction in_ifaddr ifaddr generic form sockaddr_in sockaddr generic form network interfaces are assigned IP addresses 5 classes, A, B, C, D, E D is multicast 224.0.0.1 ... IP multicast means IP multicast addresses ... not ethernet level multicast not covering IP multicast routing issues here Typographical conventions for IP Figure 6.1 note ranges you are either a host or a router (or both) host typically has single interface router typically has multiple interfaces forwards pkts from one if to another over L3 you can have multiple i/fs and not route redundancy consideration "virtual web pages" bsd ... you are a router if kernel global ipforwarding is set: # sysctl -a ... | grep forwarding net.inet.ip.forwarding: 0 # sysctl -w net.inet.ip.forwarding=1 ipforwarding = 0, means you do NOT route ipfowarding = 1, means you do route /etc/rc.conf gateway_enable="YES" sets the ipforwarding switch as above not the default ... 6.2 code intro netinet/in.h - Internet address defs e.g., def of IP protocol numbers (UDP is 17) IPPORT_RESERVED 1024 (< reserved for root services) class masks (IN_CLASSD(i)) socket address, struct sockaddr_in setsockopt options inet sysctl info some kernel function prototypes and macros netinet/in_var.h - Internet interface definitions in_ifaddr - one allocated per IP on interface macros ... for mapping if to IP multicast address structure netinet/in.c - Internet init and utility functions basic ops on IP addresses and internet ioctls net/if.c - interface utility functions net/if_var.h NOTE: book ignores this struct ifaddr exists in here which is GENERIC version of "address" structure (points to in_ifaddr) globals in_ifaddr / struct in_ifaddr * / head of in_ifaddr structure in_interfaces - number of ip capable interfaces Figure 6.5 interface and address data structures ifnet -> pts to interfaces ifnet_addrs -> pts to global address list 6.4 sockaddr_in structure Figure 6.6 sockaddr_in Figure 6.7 sockaddr and sockaddr_in line up 6.5 in_ifaddr structure one per ip address allocated 6.6 address assignment Figure 6.9 ... example network, note: netmask is side of class part of "net", class A/B/C Figure 6.10 interface ioctl command list never mind how you get there! ... end up at in_control for IP ioctl commands Figure 6.13 in_control function if ifp set find 1st ip address for that i/f do preconditions do commands note: note: how command may be passed to interface for interface specific functionality (not used at this time) figure 6.14 precondition testing if ia is NULL, ... need a new address malloc it plug ptr into kernel general list plug into per if list plug ptrs into generic version of structure Note figure 6.15 ... siocsifaddr - now actually do it ... call in_ifinit to do that work ifp ptr, ia ptr, socket ip, scrub scrub - delete routes associated with this i/f note if ioctl causes it to be passed up to ether_ioctl where if_init is called device driver HARD reset. why? E.g., you need to turn promiscuous mode off arp_ifinit(ifp,ifa) is called grat. arp scrub route bindings ... #ifconfig wi0 wi0: flags=8843 mtu 1500 inet 10.0.0.1 netmask 0xff000000 broadcast 10.255.255.255 inet6 fe80::260:1dff:fef0:5203%wi0 prefixlen 64 scopeid 0x8 ether 00:60:1d:f0:52:03 media: IEEE 802.11 Wireless Ethernet autoselect (DS/2Mbps ) status: associated ssid "FreeBSD IBSS" 1:"" stationname "FreeBSD WaveLAN/IEEE node" channel 3 authmode OPEN powersavemode OFF powersavesleep 100 wepmode OFF weptxkey 1 #netstat -rn ... "clone route" ip "gateway" 10 link#8 UC 0 0 wi0 "link-layer route" hanging off of another clone route, note the L 209.180.166.94 00:e0:d0:10:ad:9e UHLW 3 0 ed0 110 Figure 6-19 metric (MTU) copied from i/f to address if BROADCAST capable i/f broadcast is subet | ~subnetmask 131.252.1.0 | ~255.255.255.0 -> 131.252.1.255 line 413 ...loopback destination Odd ... but likely because BSD algorithm for picking ip_src involves looking up likely match for that ip_src as ip_dst in the routing table ... thus figure out ip_src for IP pkt, and ifp to send it out of. install clone route and set flag to indicate that happened multicast ... put in 224.0.0.1 Figure 6-20 netmask straight forward SIOCSIFDSTADDR dest address assignment for pt. to pt. if, i.e., the other guy ME ----------------------- PEER 241 ... pass it to interface ioctl routine if route associated delete it add it Retrieving interface info Figure 6.22/Figure 6.23 gets are straight forward set is straight forward too ... Multiple IP addresses per interface now # ifconfig wi0 alias ... or -alias to remove e.g., SIOCGxxx SIOCSxxx operatoe only on 1st interface what if you want two addresses per i/f how can these two boxes talk? ---------------------------------------------------------- | | 1.1.1.1 2.2.2.2 add 2.2.2.1 ... SIOCAIFADDR can do it all SIOCDIFADDR to delete Figure 6.24 in_aliasreq structure address/broadcast or dst/netmask Figure 6.25 assume ia set to 1st ip address and first 2 memberes of in_aliasreq and ifreq are the same with delete we expect we have something to delete ... if search fails, nothing to do code falls thru to 6.14 ... note clever or scary use of structures ... after that assume we have a newly malloced in_ifaddr structure or an old one Figure 6.26 270 assume hostIsNew is true, and change it if it is not true len = 0 or address matches if mask len, therefore mask toss routes set mask if pt to pt ... if host is new or mask it new init interface if broadcast capable, deal with that Deleting IP Addresses SIOCDIFADDR ia points to in_ifaddr we want to delete Figure 6.27 scrub routes delete from interface address list and from in_ifaddr (internet address list) claims that deletion occurs here are interesting. more precisely the 1st chunk of code fixes up lists IFAFREE deletes the ifa, if ref. counts allow it. 6.7 Interface ioctl processing leioctl, slioctl, loioctl. Note: just because IOCTL exists, doesn't mean every i/f does something. in_ifinit called by SIOCSIFADDR (set ip ) in_ifinit issues SIOCSIFADDR thru i/f's own if_ioctl function leioctl, Figure 6.28 SIOCSIFADDR we are UP if af_inet turn the hw on ... good for arp! arpwhohas ... (note: this is done upstairs now) default: init it anyway note splx around the whole thing ... slioctl, Figure 6.29 up up up (Sorry, Shania) slip is IP only loioctl up So what does this do: # ifconfig ed0 1.2.3.4 1. potentially gets the netmask wrong! 2. grat arp 3. up as side effect 4. hw reinit 5. does something to routing table # ifconfig ed0 down # ifconfig ed0 up what does this do? 6.8 internet utility functions in netinet/in.c in_netof - return network and part of struct in_addr in, set host bits to 0 in_canforward - can route it in general (not routing lookup) not class D/E, loopback, etc. in_localaddr - directly connected address in_broadcast - is address broadcast as far as if ifp concerned 6.9 ifnet utility functions net/if.c ifa_ifwithaddr - search ifnet list for interface with matching ipaddr, unicast or broadcast ifa_ifwithdstaddr - search for destination address ifa_ifwithnet - search ifnet and find address on same network as input ifa_ifwithaf - same address family ifaof_ifpforaddr - returns ifaddr ptr, if a given ifp matches that address note: ifa OF ifp for a given address is idea of function name ifa_ifwithroute - if we have dst address (route index) as input, find associated ifa ifunit(char *name) -> return ifnet ptr for 1st interface by name