chap 5: interfaces: slip/loopback slip/loopback/more ioctl sloutput looutput slioctl loioctl note that loopback only has an output routine. what does it do? note that slip/ppp/loopback are all virtual ... but in a different sense. slip/ppp must use some sort of OS serial processing. loopback of course just feeds packets back to IP. 5.2 code intro net/if_slvar.h - slip definitions net/if_sl.c - slip driver net/if_loop.c - loopback driver global variables sl_softc - slip interface. array of [units as index] loif - grant total of one ... stats: tk_nin # serial interface io # pstat -t of course, netstat -in is also maintained Name Mtu Network Address Ipkts Ierrs Opkts Oerrs Coll lp0* 1500 0 0 0 0 0 mvif0 16384 74700 0 0 0 0 lo0 16384 2194 0 2194 0 0 lo0 16384 ::1/128 ::1 0 - 0 - - lo0 16384 fe80:3::1/6 fe80:3::1 0 - 0 - - lo0 16384 127 127.0.0.1 2194 - 2194 - - ppp0* 1500 0 0 0 0 0 sl0* 552 0 0 0 0 0 ******************************************************************************** faith 1500 0 0 0 0 0 wi0 1500 00:02:2d:1f:40:60 153265 0 132700 0 66 wi0 1500 131.252.222/2 131.252.222.4 227965 - 124873 - - wi0 1500 fe80:7::202 fe80:7::202:2dff: 0 - 0 - - 5.3 slip interface serial line connection: C -------- rs-232 with twisted t/r -------------- C C --- modem ---------------------------------modem --- C (router) Slip encapsulates packets, but has no header. It uses a form of byte-stuffing to escape the command encapsulation problem. What if encapsulation byte appears in i/o stream as data? SLIP END = 0xc0 escaped by this ruleset: if in data, prepend SLIP ESC 0xdb, changed to 0xdc ESC itself if it appears, prefixed with 0xdb, changed to 0xdd. no header, therefore no type field. SLIP Line discipline: SLIPDISC ttys have line desciplines: e.g., classic cooked (shell), raw (or half-cooked) with vi. slip appears to tty system as its own line discipline. passes incoming pkts to IP input q passes outgoing pkts to tty device device driver functions: slattach - attach function, init/attach sl_softc to ifnet slinit - initialize sloutput - queue outgoing pkts slioctl - standard ioctl function sl_btom - convert device buffer to mbufs slopen - attach sl_softc to tty and init driver slclose - detach sltioctl - process tty ioctls slstart - dequeue pkt and begin to xmit on tty device slinput - process incoming byte from tty supports VJ compression, and even auto-detect of same. Figure 5.8 slip device driver slip init: slopen/slinit slattach driver function inits sl_softc BUT that doesn't accomplish anything. slattach(8) app must bind tty and device driver (set baud rate, etc) to get things going. Choose a serial device; e.g., /dev/tty01. issue ioctl to setup SLIP line discipline on that device. slopen is slip line discipline open function, called from tty land. dev - kernel device id not used. tp - pointer to tty structure for loop searches for first unused slip softc if tty ptr not set (found one) call slinit ... points tty to softc points softc to tty notes baud rate flushes tty queues slinit - inits sl_softc structure allocates mbuf cluster used for incoming chars until we get a frame sc_buf pts to start of frame sc_mp pts to next byte sc_ep points to end slinit leaves 148 bytes at front to deal with possible compressed header expansion MCLBYTES 2048 SLBUFSIZE 2048 - can't get bigger in terms of MTU SLIP_HDRLEN - 16 SLIP bpf header BUFOFFSET 148 - max size of was compressed, now expanded tcp/ip + bpf hdr SLMAX 1900 - max size of compressed slip packet stored in a cluster SLMTU - 296 - suggested MTU optimized for multi-process vs throughput. SLIP_HIWAT - max number of bytes to queue in tty output queue SLIP input slinput tty delivers bytes one at a time. calls slinput. ERRORMASK ... c is an int, and the tty driver can or in control info in the high bits. indicate an error ... deal with state machine cases put ordinary character in buffer Figure 5.13 FRAME_END code if SC_ERROR basically a discard bpt tap examine 1st byte to figure out if compressed or not note: sl_btom ... buffer to mbuf chain queue ... on ip only Figure 5.15 sloutput if_output called by ip, function ptr to sloutput sanity check to make sure IP must have tty must be on ... carrier up, else modem went away get q ptr get ip hdr ptr if no icmp ... throw away check ip_tos LOWDELAY flag point to fast q q full check else enqueue if no chars on outq call start routine slstart function, Figure 5.16 sloutput may call it tty device driver may call it (interrupt side equivalent) slstart adds bytes to output clist tty t_oproc function drains q and xmits bytes deq from fast q before slow q if no pkts done if bpf save to bpfbuf array walking thru mbufs if TCP compress header if bpf actually do bpf thing Figure 5.17 actual packet output code if low on buffers free if cc is zero put frame end Figure 5.20 loop thru mbuf chain data ptr end of mbuf buffer ptr while not at end ... etc ... high-level-points: keep in mind that slip itself has no checksum (ppp does). in your judgement is slip "safe" for packets; e.g., what happens if you have an application that uses UDP and does so with UDP checksums turned off? is slip too cavalier about the data? slclose function: Figure 5.23 what does splimp do here? why splimp? sltioctl - tty-side ioctls ifioctl can call slioctl in theory also covers tty ioctls SLIOCGUNIT - get unit # 5.4 loopback interface not much to it ... basically queue up for ip input Figure 5.26 note that multicast as well as SIMPLEX devices will be looped back, by default. int looutput - if bpf loopback for bpf prepends 4 byte header with address family if blackhole silently discard if reject noisyly discard