ip multicast class D address 224.0.0.0 .. 239.255.255.255 each D address is a multicast Group (G) interfaces must join/leave groups, programmed top-down by ip IGMP used to communicate this to local mrouter multicast routing used between mrouters to tie src/dst together broadcast networks typically multicast too: ethernet 802.11 if interface has it, IFF_MULTICAST set in if_flags udp supports it. tcp does not. RFC 1112: host requirements: Level 0: none Level 1: can send but not receive Level 2: can send/recvieve note: ok to be "multicast" on one i/f out of N, if multi-homed Net/3 can support multicast routing too Well-known IP mgroups ... Note 224.0.1.1 as interesting Level-2 box must join 224.0.0.1 224.0.0.2 mcast routers 224.0.0.4 dvmrp routers 12.2 code intro netinet/if_ether.h, ethernet multicast structure netinet/in.h - Internet multicast structure netinet/in_var.h - netinet/ip_var.h net/if_ethersubr.c netinet/in.c netinet/ip_input.c netinet/ip_output.c global variables ether_ipmulticast_min - smallest IEEE address for IP ether_ipmulticast_max - biggest ip_mrouter - pointer to socket created by multicast routing daemon stats: not a lot ... 12.3 ethernet multicast addresses multicast is broadcast with a hw filter OR put another way we do not need to use broadcast which causes IP to have to filter out the dg question/s: what is the SLIP address for "multicast" ? how do we map IP multicast to ethernet hw multicast? and vice versa? Do we need arp? IP to Ethernet Multicast address mapping Figure 12.5 IEEE IANA block starts with: 01:00:5E what might 01:cc:cc be then? low-order 23 bits of IP mapped into MAC address. bit 24 set to 0 Note Figure 12.6 macro gives us ip input, mac output 12.4 ether_multi structure Figure 12.7 ether_multi structure note that this is not IP specific. A given ethernet/802.11 device may be limited in terms of the number of hw filters, therefore IP may have to deal with this (plus the ip/mac mapping is imperfect anyway) note that single entry may allow range, but probably set to one one address if: enm_addrlo = enm_addrhi refcount == 1 Figure 12.8 softc has arpcom which points to this list 224.0.1.2, 224.0.0.2 224.0.0.1 sgi-dogfight, all routers, all everything On the other hand, MAC might map to 225.0.0.1 as opposed to 224.0.0.1 Figure 12.9 ETHER_LOOKUP_MULTI searches this list 12.5 ethernet multicast reception ioctls to add multicast ips ... to a given ethernet interface. A router daemon wishing to use 224.0.0.9 would therefore program one or more interfaces to recv. pkts associated with that multicast ip. 12.6 in_multi structure For each per interface address list, we append multicast addresses. To in_ifaddr Figure 12.12 in_multi (ip specific) some IGMP state here ... (when we need to do next report) Figure 12.13 for hookup picture Remember that MAC/multicasts hang off of arpcom structure IN_LOOKUP_MULTI searches multicast list for a particular multicast IP. note: inm set to NULL if this fails. 12.7 ip_moptions used for setting options (man ip to see this) like TTL on outgoing packets Figure 12.15 ttl loop(back) - writes come back up stack to interested sockets if ifp is null ... sent to "default interface" (may not be what you wanted) 12.8 multicast socket options if - select i/f ttl - set ttl loopback - enable or disable loopback join a multicast group leave a multicast group 12.9 multicast ttl 2 purposes with IPv4: 1. limit packet ttl in conventional sense. 2. possibly limit multicast packet to remain within administrative area (local routing domain) aka scope ... note: this state is not a likely possibility. multicast router may have ttl threshold ... if packet ttl < n, drops packet something around "32" is often used for same site. Whether or not this works is up to network engineers (maybe ... if then). See Figure 12.18 Note RFC 1546 note on anycast ... which is a vague term meaning: find a "host" for this packet, please mr. network. anycast is likely implemented with special unicast addresses that are somehow well-known locally. Although multicast seems like a better idea. 12.10 ip_setmoptions function: overview: doh ... set multicast options ... apply same to multicast packets as they go out optname - which option imop - ptr to ip_moptions structure, if non-null should mod existing struture else allocate new one and set it m - ptr to input data Figure 12.19 if no imo, allocate and set to defaults ttl 1 loopback yes no members set options ... if no options that are different than defaults delete structure Figure 12.20 setting the multicast i/f aim socket at multicast i/f (on multi-homed box, this may be necessity) index: IP unicast for i/f if 0, delete and revert to default behavior Use INADDR_TO_IFP to do real work set ifp Figure 12.21 ... setting ttl Why does ttl default to 1? Figure 12.22 setting loopback why loopback? Comment from net/if_ethersubr.c/ether_output routine: /* * If a simplex interface, and the packet is being sent to our * Ethernet address or a broadcast address, loopback a copy. * XXX To make a simplex device behave exactly like a duplex * device, we should copy in the case of sending to our own * ethernet address (thus letting the original actually appear * on the wire). However, we don't do that here for security * reasons and compatibility with the original behavior. */ if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) { int csum_flags = 0; if (m->m_pkthdr.csum_flags & CSUM_IP) csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID); if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR); if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); SIMPLEX doesn't apply to multicast. If you want to get your own multicast back, you should set this. If you don't care (sending video and only one sender app, but no desire to display what is sent) ... then don't set it. Or a multicast routing daemon might want to send and not recv its own thoughts. Still no telling what your ethernet i/f might do, so it is probably good conservative coding to be ready for this case, and throw them out if you sent them. 12.11 joining G IP_ADD_MEMBERSHIP on G, say 224.0.0.9 bound to i/f by definition. We must somehow have an interface to do this as we must bind the G to the if (and program the hw, and/or possibly do IGMP) Figure 12.24 ip_mreq (multicast request) structure ip_setmoptions in_addmulti or in_delmulti Figure 12.25 for calling possibilities Figure 12.26 from ip_output/setoptions function note: if no interface, we try and look up the multicast address ... in the routing table! 224/24 bound to some interface ? This is default behavior. Note the reasons multicast might fail... if G already bound to i/f, we don't need to do that again EADDRINUSE (hmmm... do the ERROR numbers used always make sense in the context where used?) no ... ... if it all works, call in_addmulti to do it to the ifp in_addmulti function maintains per i/f list Figure 12.28 ... i/f was not member of group malloc in_multi as seen in Figure 12.12 get per ifp address list if none ... we aren't going anywhere plug it into in_ifaddr multicast list use ioctl to tell driver about it using a socket let igmp know about it Figure 12.29 what slip thinks about it Figure 12.30 what loopback things about it sure! easy (true) ethernet though: leioctl: SIOCADDMULTI and SIOCDELMULTI ioctl calls: use generic ether_addmulti to add driver/hw level function if change, reset hw note hw note: if driver must use ALL MULTICAST prom. variation, it sets IFF_ALLMULTI flag on its i/f as a warning to strangers. ether_addmulti function driver calls it to add multicast address, uses ether_multi list Figure 12.32 ether_addmulti, 1st half: first do init if address family is not IP explicit address, copy into lo and hi vars if IP and 0.0.0.0 lo set to ether_min, which is 01:00:5e:00:00:00 hi set to ether_max, which is 01:00:5e:7f:ff:ff so INADDR_ANY means logically listen to all IP multicast (not all multicast) this is for multicast IP routers mrouted issues SIOCADDMULTI with INADDR_ANY if not INADDR_ANY call ETHER_MAP_IP_MULTICAST on addrlo and set addrhi to it no way to ask for a range! Figure 12.33 ether_addmulti, 2nd half make sure multicast address is multicast ETHER_LOOKUP_MULTI see if address range is already in list if so, increment reference count now malloc new record, copy in, link to list, and count reference why reference count? Figure 12.34 relationships between ip_moptions, in_multi, ether_multi options ip pov hw pov 12.12 leaving an IP multicast group ip_setmoptions: IP_DROP_MEMBERSHIP case validity check/s form pointer check address validity if interface address specified get ifnet ptr. search membership array for membership info requested if we failed to find it, EADDRNOTAVAIL call in_delmulti to update in_multi list 2nd for loop gets rid of unused entry by shifting subsequent entries up in_delmulti function delete only if reference count is 0 Figure 12.36 igmp_leavegroup - send leave group message unlink from list tell driver to update its multicast list via ioctl SIOCDELMULTI free structure ether_delmulti, Figure 12.37 same code as addmulti to init lookup address, enm is ptr output decrement reference count quit if still have references unlink and free 12.13 ip_getmoptions read options out Figure 12.38 optname - option to fetch imo - options structure mp - mbuf m_get allocates an mbuf to hold the data per options init pointer to pt into mbuf space if IP_MULTICAST_IF return IP address from ia associated with ifp ttl loop 12.14 Multicast input processing/ipintr ether_input sets M_MCAST flag for mbuf chain places pkt on ip input queue Figure 12.39 input code: pkt has been checked for errors/options processed ip pts to ip header if dst is multicast if we are multicast router pass packet to ip_mforward for kernel-level processing may eat it or not and fall thru ip_mforward may process IPIP tunnel packets if returns 0, ipintr processing continues if next protocol is IGMP, pass upstairs to mrouted (IGMP is join/leave of local-link G) rest executed anyway IN_LOOKUP_MULTI search list of per if groups if match not found, ignore ... (important: list ditch stand as hw multicast maps > 1 ip multicast) goto next means give up on this packet goto ours means process it ... 12.15: multicast output processing: ip_output function Figure 12.40 ip_output if ip_dst is multicast set M_MCAST flag lookup multicast options set ttl, ifp else set default ttl make sure outgoing i/f supports multicast, else bail with ENETUNREACH if no ip_src yet, get it from 1st in list More considerations: should pkt be received on outgoing interface? should pkt be forwarded to other interfaces? should pkt be transmitted on outgoing interface? See Figure 12.41 loopback or not? lookup loopback flag if set, call ip_mloopback basically call looutput which ironically is input! not considered for forwarding as this is done during input processing if necessary else IP_FORWARDING flag used recursively to call this function by ip_mforward if ip_mrouter and no forwarding flag set call ip_mforward on it if ttl is 0 or loopback (error at this point) drop it send the packet at this pt ... why no ICMP if ttl is 0? note: loopback handle via ip_mloopback call ip_mloopback function: figure 12.42 copy the packet loopback a copy 12.16 performance multicast packets not desired may not be filtered out by hw ethernet card may go into all multicast mode ... due to imperfect hw linear search used in list processing how could this linear search stuff be improved 12.17 summary ip_moptions is probably most important structure ethernet layer address structure is mostly not a range except for one case