/[gxemul]/trunk/src/net.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/src/net.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations)
Mon Oct 8 16:17:48 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 63699 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.676 2005/04/07 15:14:55 debug Exp $

Changelog for GXemul:
---------------------

20030829	Skeleton. ELF stuff. Some instructions.
20030830	Simple test programs using +-*/^|&%, function calls,
		loops, and stuff like that work.
20030903	Adding more instructions, fixing some bugs.
20030907	Making adding of memory mapped devices easier, although
		the framework isn't built for speed.
		Adding a -q switch to turn of debug output.
20030911	Trying to fix some bugs. Minor changes. Some COP0
		registers are now meaningful.
20030919	Making APs (non-bootstrap cpus) available via a simple
		'mp' device. Implementing ll/lld and sc/scd (for atomic
		memory updates, needed by MP operating systems).
20030923	Minor updates: more instructions (divu, mulu, lwu,
		perhaps some more), and opcode usage statistics.
20030924	If the next instruction is nullified (for 'branch
		likely' type of instructions), counters for delays etc
		are now decreased as they should.
		Adding some comments.
		Adding instructions: movz, movn.
		Adding a simple mandelbrot test to mipstest.c.
20030925	Adding instructions: bltzl, bgezl, lh, lhu, sh, mfc*,
		mtc*.
		Adding a dummy instructions: sync, cache.
		Adding minimal DECstation PROM functionality: printf()
		and getsysid() callback functions.
		Beginning work on address translation.
20030927	Adding some more cop0 functionality (tlb stuff).
		Adding mc146818 real-time clock. (Skeleton stuff.)
20030928	Adding a dc7085 serial console device (dummy, but enough
		to output chars to the screen). NetBSD uses this for
		the MIPSMATE 5100.
20030929	Working on the TLB stuff.
		Adding instructions: srlv, tlbwr, tlbr, tlbp, eret.
20030930	Trying to find a bug which causes NetBSD to bug out, but
		it is really hard.
		Adding some a.out support (for loading an old
		OpenBSD 2.8/pmax kernel image).
		Adding instructions: lwc*, ldc*, swc1 and swc3.
		Beginning to add special code to handle the differences
		between R4000 (the default emulation) and R2000/R3000.
20031001	Symbol listings produced by 'nm -S' can be used to
		show symbolic names for addresses. (-S)
20031002	Fixing the i/d fake cache for R2000/R3000. It's still
		just an ugly hack, though.
		Fixing minor bugs to make the 3100 emulation use the
		dc device (serial console) correctly. So far, 5100 and
		3100 are the only ones that get far enough to print
		stuff, when booting NetBSD.
20031004	Adding skeleton Cobalt machine emulation (-E).
		Adding a dummy ns16550 serial controller, used by the
		Cobalt machine emulation.
20031006	Adding unaligned load/store instructions (lwl, lwr,
		ldl, ldr, swl, swr, sdl, sdr), although they are not
		tested yet.
		Fixed a "data modified on freelist" bug when running
		NetBSD/cobalt: setting the top bit of the index register
		when a tlbp fails (as the R4000 manual says) isn't
		sufficient, I had to clear the low bits as well.
		Adding break and syscall instructions, but they are not
		tested yet.
		Adding a 'gt' device, faking a PCI bus, for the Cobalt
		emulation.
20031008	Adding initial support for HPCmips (-F), a framebuffer
		device using X11. NetBSD/hpcmips can output pixels to
		the framebuffer, but that's about it.
20031009	Fixing the NetBSD/pmax bug: the "0/tftp/netbsd" style
		bootstring was only passed correctly in the bootinfo
		block, it needs to be passed as argv[0] as well.
		Adding instructions: mtlo, mthi.
		Rearrangning the source tree layout.
		Adding console input functionality. The NetBSD/cobalt
		kernel's ddb can now be interacted with.
20031010	Adding experimental (semi-useless) -t option, to show
		a function call tree while a program runs.
		Linux/cobalt now prints a few messages, but then hangs
		at "Calibrating delay loop..." unless an ugly hack is
		used (setting a word of memory at 0x801e472c to non-zero).
20031013	Adding a framebuffer device used in DECstation 3100;
		VFB01 for mono is implemented so far, not yet the
		VFB02 (color) variant.  Rewriting the framebuffer
		device so that it is usable by both HPCmips and DECstation
		emulation.
20031014	Minor fixes. Everything should compile and run ok
		both with and without X11.
20031015	Adding support for ECOFF binary images; text, data,
		and symbols are loaded. (Playing around with ultrixboot
		and ultrix kernels.)
20031016	The DECstation argv,argc stuff must be at 0xa0000000,
		not 0x80000000, or Ultrix kernels complain.
		Adding R2000/R3000 'rfe' instruction.
		Implementing more R2K/R3K tlb specific stuff, so that
		NetBSD boots and uses the tlb correctly, but much of
		it is ugly. (Needs to be separated in a cleaner way.)
		ECOFF symbols sizes are now calculated, so that offsets
		within symbols are usable.
20031017	DECstation bootstrings now automatically include the
		correct name of the kernel that is booting.
		Ultrix boots a bit.
20031018	ELF symbols are now read automatically from the binary.
		-t trace looks a bit better (string arguments are shown).
		Trying to get initial R5900 stuff working (the 128-bit
		CPU used in Playstation 2).
		Fixing a minor bug to make the VFB02 (color framebuffer)
		device work better, but it is still just 256 grayscales,
		not real color. Ultrix can now use the framebuffer (it
		calls it PMAX-CFB).
		A machine can now consist of CPUs of different types.
		Adding instructions: daddi, mov_xxx, mult_xx. The xxx
		instructions are not documented MIPS64 instructions,
		but NetBSD/playstation2 uses them. Perhaps VR5432
		instructions?
		Adding sign-extension to 32-bit mult.
		Adding Playstation 2 devices: dmac (DMA controller),
		gs (Graphic something?), and gif (graphics something
		else, which has access to the PS2's framebuffer).
		NetBSD/playstation2 works a bit, and prints a few
		bootup messages.
20031020	The cpu_type field of the cpu struct now contains
		usable values in a much better form than before. This
		simplifies adding of new CPU types.
20031021	Fixing an interrupt related bug: pc_last was used, but
		for interrupts this was incorrect. Fixed now.
		Fixing a load/store related bug: if a load into a
		register was aborted due to an exception, the register
		was still modified.
		The mc146818 rtc now reads its time from the system's
		time() function.
		Fixing another exception bug: if loading an instruction
		caused an exception, something bogus happened as the
		emulator tried to execute the instruction anyway. This
		has been fixed now.
20031023	Adding a quick hack which skips "while (reg --) ;"
		kind of loops.
		NetBSD/pmax suddenly reached userland (!), but only
		once and attempts to repeat it have failed. I believe
		it is problems with my interrupt handling system.
20031024	Adding 8-bit color palette support to the framebuffer.
		Connecting the pmax vdac device to the framebuffer's
		rgb palette.
		Fixing a bug in the dc device, so that console input
		is possible; interaction with NetBSD/pmax's built-in
		kernel debugger works now.
		Symbol sizes for file formats where symbol size isn't
		included are now calculated regardless of file format.
		Physical memory space can now be smaller than 64 bits,
		improving emulation speed a bit.
		Doing other minor performance enhancements by moving
		around some statements in critical parts of the code.
20031025	Minor changes to the dc device.
20031026	Adding support for reading symbols directly from
		a.out files. (Works with OpenBSD/pmax binaries.)
		Hardware devices may now register "tick functions" at
		specific cycle intervals in a generic fashion.
		All four channels of the dc serial controller device
		should now work; playing around with keyboard scan
		code generation when using the DECstation framebuffer.
		Making various (speed) improvements to the framebuffer
		device.
20031027	Playing around with the sii SCSI controller.
20031028	Minor fixes.
		Adding an SGI emulation mode (-G), and some ARCBIOS
		stuff, which SGIs seem to use.
		Adding getbitmap() to the DEC prom emulation layer,
		so some more -D x models become more usable.
		Adding a dummy 'ssc' serial console device for
		DECsystem 5400 emulation.
		Playing around with TURBOchannel stuff.
20031030	Minor fixes.
		Adding the sub instruction. (Not tested yet?)
		Sign-extending the results of multu, addi,addiu,
		add,addu,sub,subu,mfcZ.
		Adding a colorplanemask device for DECstation 3100.
		Fixed the NetBSD/pmax bug: I had forgotten to reset
		asid_match to 0 between tlb entry checks. :-)  Now
		userland runs nicely...
20031031	Fixing more bugs:  unaligned load/store could fail
		because of an exception, but registers could be "half
		updated". This has been fixed now.  (As a result,
		NetBSD/pmax can now run with any of r2000,r3000,r4000,
		r4400, or r5000.)
		Adding some R5K and R10000 stuff.  (Note: R5K is NOT
		R5000. Weird.)
		Adding dummy serial console (scc) for MAXINE.
		MAXINE also works with framebuffer, but there is no
		color palette yet (only black and white output).
20031101	Moving code chunks around to increase performance by
		a few percent.
		The opcode statistics option (-s) now shows opcode
		names, and not just numbers. :-)
		Fixing the bug which caused NetBSD/pmax to refuse
		input in serial console mode, but not in keyboard/
		framebuffer mode: the osconsole environment variable
		wasn't set correctly.
		Adding DEC PROM getchar() call.
		The transmitter scanner of the dc device now scans
		all four channels at once, for each tick, so serial
		output is (approximately) 4 times faster.
20031103	Adding a dummy BT459 vdac device, which does nothing
		but allows a PMAG-BA turbochannel graphics card to be
		used as framebuffer.
		Several DECstation machines (-D 2, 3, and 4) can now
		use TURBOchannel option card framebuffers as console,
		for output. (Keyboard input is still not implemented
		for those models.)  Only PMAG-AA (1280x1024x8) and
		PMAG-BA (1024x864x8), both using BT459 vdac, have
		been tested so far.
		Modifying the X11 routines so that several framebuffer
		windows now can be used simultaneously (if several
		graphics option cards are to be emulated concurrently).
20031104	DEC MIPSMATE 5100 (KN230) interrupts are shared
		between devices. I've added an ugly hack to allow
		that to work, which makes it possible to boot NetBSD
		into userland with serial console.
20031106	Removing the -S (symbol) option, as symbol files can
		now be given in any order together with other file
		names to be loaded.
		cookin tipped me about using (int64_t) (int32_t)
		casts instead of manually sign-extending values.
		Casting sometimes increases performance, sometimes
		decreases. It's tricky.
		Importing mips64emul into CVS.
20031107	Adding a generic ARC emulation mode.
		Increasing performance of the framebuffer by not
		updating it (or the XImage) if a write to the
		framebuffer contains exactly what is already in it.
		(This improves scrolling speed and initialization.)
		Adding initial MIPS16 support.
		Adding initial disk image support (-d command line
		option), but this will not be used until I get some
		kind of SCSI-controller emulation working.
20031108	Adding the first MIPS16 instructions: "move y,X",
		"ld y,D(x)", and "daddiu S,K" (but the last one
		doesn't work yet).
		Fixing the console environment variable for
		Enough of the 'asc' controller is now implemented
		to let NetBSD get past scsi disk detection when
		no disk images are used.
		DECstation machine type 2; both serial console and
		graphical console work now.
		Other X-windows bit-depths than 24 bits work now,
		but colors are still not correct in non-24 bit modes.
		Keypresses in X framebuffer windows are now
		translated into console keypresses. (Normal keys, but
		not cursor keys or other special keys.)
20031111	Adding support for X11 using non-24-bit output.
20031120	Adding X11 mouse event to emulated mouse event
		translation, but it's not tested yet.
		Trying to get more of the SCSI controller emulation
		to work.
20031124	Raw binaries can now be loaded into memory.
20031204	Adding srec binary support.
20031220	Adding some super-ugly arcbios emulation code.
		Making some progress on the SGI and ARC machine
		emulations.
20031222	SGI and ARC progress. Multiple CPUs are now added to
		the arcbios component tree (although NetBSD cannot
		actually use more than one).
20031228	Adding 'crime' and 'macepci' fake devices for SGI
		emulation.
		Finally implementing the cop0 'compare' register.
		Improvements to the ns16550 device, but it is still
		incomplete.
		SGI userland is now reached, but interaction is broken
		(due to the buggy ns16550).
20031229	Adding some more instructions: teq, dsllv
		Adding a Nintendo 64 emulation mode (skeleton).
		Adding R4300 and R12000 to the cpu list.
20031230	Adding bltzal, bltzall, bgezal, bgezall (not really
		tested yet).
		Fixing the 16550 serial controller device (by not
		supporting fifo, so in fact it emulates a 16450
		instead).  This causes NetBSD/sgimips to run nicely
		into userland, sysinst, and so on.
		Some ARC/RD94 interrupts seem to work ok now, but
		i/o interrupts are still not correctly implemented.
		NetBSD/arc userland is reached and can be interacted
		with, but there's no sysinst (?).
20040103	Trying to get some Irix stuff to work, but it's hard.
		Fixing some Cobalt/linux problems.
20040104	Adding a dummy 8250 device, so that Linux/sgimips can output
		console messages.
		Adding dmultu. (The same as dmult, so I'm not sure it's correct.
		Perhaps dmultu is correct and dmult is wrong...)
		Fixing a bug in unaligned load/stores of 64-bit values (a cast
		was needed).
		Linux/sgimips in 64-bit works a bit more than before.
		Adding simple (polled) input functionality to dev_zs.
		Making some progress on SGI-IP22 (IP32 still works best,
		though).
		Fixing the mc146818 clock device in ARC/NEC and SGI emulation
		modes, the year field was not correct.
		Adding a fake 'pref' instruction (lwc3).
20040106	Separating out memory.h from misc.h.
		Refactoring of a lot of small code fragments.
		The PCI bus device is now shared between Cobalt, SGI, and ARC.
		Support for RAM mirroring (dev_ram.c, not really tested yet).
		Ugly hack to select the largest of ELF string symbol tables,
		if there are more than one.
		Memory hole fix for ARCBIOS, and a fix for very large (>= 4GB)
		amounts of emulated RAM.
		TGA (DEC 21030) PCI graphics device. NetBSD/arc can boot with
		this card and use it as a framebuffer console.
20040107	Adding a fix (partly incorrect) to daddi, to allow Linux/sgimips
		to boot in 64-bit mode.
20040108	Fixing a sll/nop bug (rd==0 for nop, not sa==0 as before).
20040109	Trying to get an SGI-IP32 PROM image to boot.
20040110	Faking R10000 cache things.
		The PROM image boots, although it takes almost forever for it
		to realize that there is no keyboard.
		The 'gbe' SGI-IP32 graphics device works enough to display the
		Linux framebuffer penguin in the upper left corner :-)
20040111	-p and -P addresses can now be given as symbol names, not just
		numeric values.
		Experimenting with adding a PCIIDE (dev_wdc) controller to the
		Cobalt emulation.
20040120	Adding src/bintrans.c. No code yet, but this is a place for
		ideas to be written down.
		Increasing performance a little bit by inlining the check for
		interrupts (which occurs for every instruction).
20040124	Experimenting with pure userland (syscall) emulation.
20040127	Fixes for compiling under Solaris.
20040206	Some bintrans experiments.
20040209	Adding some simple Ultrix userland emulation syscalls.
20040211	Adding decprom_dump_txt_to_bin.c to the experiments/ dir.
		Adding a section to doc/ on how to use DECstation PROM dumps.
		Adding a hello world example to doc/ as well.
20040218	TURBOchannel slots that are empty now return a DBE exception,
		so that Ultrix and DECstation PROMs don't complain about
		broken TURBOchannel ROMs.
		Working some more on the machine-dependant interrupt stuff.
20040219	Trying out some Linux/DECstation kernels (semi-successfully).
20040222	YES! I finally found the bug that caused Linux/SGI-IP32 to only
		work on Alpha, not on 32-bit machines.  It was a shift left,
		probably done using 6 bits on alpha, 5 bits on 32-bit machines.
20040223	Some minimal DEC KN5800 progress; Ultrix prints some boot
		messages, detects 16 XMI R3000 cpus, and get a NULL panic.
		It's all fake, though, the CPUs don't actually work.
		Still, better than nothing :-)
20040225	An Ultrix OSF1 kernel with a ramdisk now boots :-)  (It was
		a problem with ultrixboot not giving the same arguments as
		NetBSD's boot program.)
20040225(later)	Fixing a bug in the DECstation dc serial device; digits 0-9
		were translated to numeric keypad 0-9, not the normal 0-9.
		(This caused Ultrix to print escape sequences instead of
		digits.)
20040226	Some progress on machine-dependant interrupt delivery
		for -D7 (Maxine) and -D4, and some more 'scc' serial
		controller featuers are implemented (but no interrupts/
		dma/keyboard/mouse stuff yet).
20040228	Progress on the scc controller; -D4 works in both serial
		console mode and with keyboard (graphical console), but no
		mouse yet.
20040301	SGI mace interrupts are now done using the new machine-
		independant interrupt system.
20040303	Fixing an R5900 bug; the lowest 6 bits have special meaning
		for coprocessor functions, not just 5 bits as on non-R5900
		CPUs. (This fixes a bug which caused NetBSD to crash.)
20040304	Adding enough (fake) DMA capabilities to the ioasic device
		to allow Ultrix to print boot messages in the -D3, -D4,
		and -D7 modes, and also print graphical console messages
		in -D4 and -D7 modes.
		-D11 (DEC5500) polled getchar added (to the 'ssc' device).
		Adding the 'madd' instruction (including R5900 weird stuff).
20040304(later)	Playstation 2's GIF can now copy 640x16 pixel chunks, allowing
		NetBSD to scroll up the framebuffer.  The cursor also works
		better now.
		Playstation 2 bootinfo RTC data should now be passed correctly
		to the running kernel.
		DECstation rtc year should be either 72 or 73, anything else
		will cause Ultrix to give a warning about invalid year.
20040306	Combining playstation2's dmac, interrupt, and timer devices
		into one (ps2_stuff).
		Adding some R5900 instructions: mfsa, mtsa, pmfhi, pmflo, por,
		lq, and sq.  (Most of them are just guesses, though.)
		Implementing my own XImage putpixel routine, which can be
		inlined... significantly faster than normal XPutPixel. :-)
20040307	Implementing the basic functionality of a "PMAG-CA" pixelstamp
		accellerated framebuffer device. Works with NetBSD and
		Ultrix, but no cursor or color support.
20040308	PMAG-CA, -DA, and -FA pixelstamps seem to work now.
		Adding a hack to allow a pmax/mach kernel to be loaded (it's
		a COFF file with 0 (!) sections).
		Initial test of bt459 + framebuffer cursor support.
20040309	Fixes/updates of dev_dec5800 and dev_ssc (and dev_decxmi) allow
		a KN5800 Ultrix-OSF1-ramdisk kernel to boot all the way into
		userland and be interacted with.
		The bt459 cursor should now look semi-nice, but it is still
		a bit fake.
20040310	Moving the DEC CCA stuff from src/machine.c into a separate
		device file (devices/dev_deccca.c).
		An ugly hack added to allow some more OSF/1 kernels (almost
		a.out, but without many of the header fields) to load.
20040314	Adding PMAG-JA and PMAG-RO (1280x1024 x 8-bit) TURBOchannel
		graphics devices. They work in Ultrix, but only monochrome
		and no cursor, because there are no ramdacs or such yet.
20040315	Pixelstamp solid fill now supports colors other than just
		zero-fill.
		Adding a (new) regression test skeleton.
20040321	Some really minor updates.
20040323	Fixes to allow SGI-IP20 and IP22 to work a bit better
		(aliased memory), and adding "private" firmware-like vectors
		to arcbios emul. An IP22 Irix kernel gets far enough to
		print an assertion warning (and then double panics). :-)
20040324	Adding a generalization hack to the SCC serial controller
		to work with SGI-IP19 (in addition to DECstations).
		Adding the 'sdc1' instruction.
		Some progress on various SGI emulation modes.
20040325	Minor updates.
20040326	Fixed a 'madd' bug (r5900). NetBSD/playstation2 now reaches
		userland correctly.  And a simple fix which allows NetBSD
		timer interrupts to be triggered; NetBSD uses T_MODE_CMPE
		(compare), while Linux uses _OVFE (overflow).
20040328	Linux on Playstation 2 boots a bit. The Playstation 2
		graphics controller has been extended to work better with
		NetBSD, and to include some Linux support as well.
		Some interrupt handling enhancements on Playstation 2,
		needed for Linux' dma.
		128-bit loads and stores (lq and sq) are allowed, although
		the top half of quadwords are not modified by other
		instructions. (Linux uses lq and sq.)
		Big-endian X Windows servers now display correct rgb color,
		not bgr as before.
20040330	Some minor updates to the documentation.
20040401	Adding a dummy ps2 OHCI device.
20040402	Progress on the asc SCSI controller.
20040406	Hack to allow ./configure, make to work on HP-UX B.11.00
		on HPPA-RISC, gcc 3.3.2. (Does not work with HP's cc.)
		More progress on the asc SCSI controller. Fixing INQUIRY,
		adding READ_CAPACITY, adding READ. Works a bit with NetBSD
		and some (but not all) Ultrix kernels, on DECstation type 2.
		Adding WRITE, SYNCRONIZE_CACHE.
		Mounting disks works in NetBSD :-)  It is a bit buggy,
		though. Or something else is buggy.
20040407	The bug is triggered by gunzip during NetBSD/pmax install.
20040408	Fixing a bug (non-nul-terminated string) which caused X11
		cursors to not display on Solaris.
		Unnecessary X11 redraws are skipped (removes some weird
		delays that existed before), and cursors are redrawn on
		window exposure. (The cursor functionality has been moved
		from dev_fb.c to x11.c.)
20040411	Fixing the DC7085 device so that Ultrix doesn't behave weird
		if both tx and rx interrupts occur at the same time.
		More advancements on the asc SCSI controller.
		More disk image filename prefixes are now recognized; c (for
		CD-ROM, as before), d for disk, b for boot device, r for
		read-only, and 0-7 for scsi id.
		Mounting disks works in Ultrix. Installing to disk usually
		crashes for various reasons, but an OSF/1 install gets
		relatively far (similar to the NetBSD/pmax install).
20040412	Trying to find the bug.
20040415	Finally found and fixed the bug; SCSI reads and writes
		(actually, any data in or data out) can be split up into
		multiple DMA transfers. That stuff was only partially
		implemented, and the part that was implemented was buggy.
		It works now. NetBSD/pmax and Ultrix 4.3 seems to like
		the SCSI stuff enough to install almost all the way.
20040415 (more)	Adding a hack which allows a host's cdrom device to be used as
		a cdrom device inside the emulator, eg /dev/cd0c.
		Making the cycle counter int64_t instead of long, as a 'long'
		overflows too easily on 32-bit machines. (The bug is still
		there, though.)
		I've now verified that a full NetBSD/pmax install can be done.
		If using a PMAG-AA graphics board, startx brings up X :-)
		mips64emul can be compiled inside NetBSD inside mips64emul,
		and it can run NetBSD in that environment. (I'm getting
		dizzy... :-)
20040417	Moving some coprocessor stuff from cpu.c to coproc.c.
20040424	Adding a BT455 vdac for PMAG-AA. Black and white are now
		rendered correctly in Xpmax.
		Adding colormap support to the BT459 device, for PMAG-BA.
20040425	Fixing a buffer length bug, which caused an Ultrix 4.5
		install to bug out on an i386 host.
20040429	FPU experiments.
20040502	More FPU experiments.
		Speedup for exception debug messages:  in quiet mode, debug
		messages were still evaluated, which took a relatively
		large amount of time.
20040503	Most FPU stuff fixed, but there is at least one known bug
		left; ps axu in NetBSD triggers it (ps loops forever).
20040504	A default install of Ultrix 4.5 succeeded! It boots up with
		a graphical login.
		Fixing the keyboard repetition bug (a lk201 "up" (release)
		scancode is now sent after every key).
20040505	Both CR and LF now produce the same lk201 scancode, so that
		pressing 'enter' works as expected in Ultrix.
20040506	Adding a vaddr to paddr translation cache, causing a speedup
		of perhaps 50% or more.
20040507	Fixing PMAG-BA color for Ultrix. (Ultrix relies on interrupts
		coming from the TURBOchannel slot to update the palette.)
20040508	Fixing cursor positioning for PMAG-BA.
20040511	Prints current nr of instructions per seconds, not only
		average, when using -N.
20040515	Some more bintrans experiments.
20040606	Adding ARCBIOS GetReadStatus() and Read().
		Adding some instructions: tlt, tltu, tge, tgeu, tne.
20040607	Adding the dsub instruction.
		Some minimal progress on SGI-IP30 emulation.
		Applying a patch from Juli Mallett to src/file.c (I'm not
		sure yet if it breaks or fixes anything).
		Some minor fixes for SGI-IP22 (such as faked board revision
		numbers).
20040608	ll/sc should now fail if any unrelated load/store occurs.
		Minor changes to the configure script.
		Adding some ifdefs around code which is not often used
		(the mfhi/mflo delay, and the last_used TLB experimental
		code); this might cause a tiny speedup.
20040609	Minor fixes.
20040610	Various minor SGI fixes (64-bit ARCS stuff, progress on the
		CRIME/MACE interrupt system, and some other random things).
20040611	More crime/mace progress, and some more work on pckbc.
		KN5800 progress: adding a XMI->BI adapter device; a disk
		controller is detected (but it is just a dummy so far).
20040612	Adding "dev_unreadable", which simplifies making memory
		areas unreadable. (NetBSD on SGI-IP22 no longer detects
		non-existant hpc1 and hpc2 busses.)
		Implementing rudimentary support for IP22 "local0" and
		"local1" interrupts, and "mappable" local interrupts.
		Some progress on the WDSC SCSI controller on IP22, enough
		to let NetBSD get past the disk detection and enter
		userland!  :-)
		The zs (zilog serial) device now works well enough to let
		NetBSD/sgimips be interacted with on IP22. :-)  (Though
		it is very ugly and hardcoded.)
20040613	IP32 didn't work last night, because there were too many
		tick functions registered. That has been increased now.
		Trying out NetBSD/sgimips 2.0 beta kernels. There are some
		differences compared to 1.6.2, which I'm trying to solve.
		Interrupt fixes for IP32: _serial and _misc are different.
		Separation of IP22 (Full-house) and IP24 (Guiness).
20040614	Modifying the memory layout for IP20,22,24,26 (RAM is now
		offset by 128MB, leaving room for EISA registers and such),
		and moving around some code chunks. This is not well
		tested yet, but seems to work.
		Moving parts of the tiny translation cache, as suggested
		by Juli Mallett.  It seems that the speedup isn't as
		apparent as it was a few weeks ago, though. :-(
		Speedups due to not translating addresses into symbol
		names unless the symbol name is actually printed.
		Added support for loading old big-endian (Irix) ECOFF
		kernels (0x60 0x01 as the first two bytes).
20040615 (late)	Adding enough SGI IP20 (Indigo) support to let NetBSD 2.0
		enter userland :-)  No interrupt specifics are implemented
		yet, so it hangs while doing terminal output.
20040618	Experimenting with the WDSC SCSI controller for IP20,22,24.
20040620	Adding a program which converts SGI prom dumps from text
		capture to binary, and some hacks to try to make such an
		IP22 PROM to work better in the emulator.
20040621	Removing the Nintendo 64 emulation mode, as it is too
		uninteresting to support.
		Adding SCSI tape device support (read-only, so far).
		Fixing a bug which caused the cursor to be corrupted if new
		data was written to the framebuffer, but the cursor wasn't
		moved.
20040622(early)	Finally! Making progress on the SCSI tape stuff; when going
		past the end of a file, automagically switch to the beginning
		of the next.
20040622(late)	Trying to track down the last SCSI tape bugs.
		Removing _all_ dynamic binary translation code (bintrans),
		starting from scratch again.
20040623(early)	Performing a general code cleanup (comments, fixing stuff
		that led to compiler warnings, ...).
		Disabling MIPS16 support by default, and making it a
		configure time option to enable it (--mips16). This gives
		a few percent speed increase overall.
		Increasing performance by assuming that instruction loads
		(reading from memory) will be at the same page as the last
		load.  (Several percent speedup.)
		Moving the list of kernels that can be found on the net from
		README to doc/.
20040624	Finally! I found and fixed the bug which caused 'ps', 'top',
		'xclock', and other programs in NetBSD/pmax to behave weird.
		Increasing performance by a few percent by running as many
		instructions in a row as possible, before checking for
		hardware ticks.
		When booting from SCSI tapes on DECstation, the bootstring
		now contains 'tz' instead of 'rz'.
		Adding a second ARC machine mode, "Acer PICA-61", -A2.
		Disabling the support for "instruction delays" by default
		(it has to be enabled manually in misc.h now, but is never
		used anywhere anyway).
		Other minor optimizations (moving around stuff in the
		cpu struct in misc.h, and caching cpu->pc in cpu.c).
		Separating the tiny translation cache into two, one for
		code and one for data. This gives a few percent speed
		increase.
20040625(early)	I think now is a good time for a "feature freeze",
		to let the code stabilize and then make some kind of
		first release.
20040625(later)	Adding a -v (verbose) command line option. If -v is not
		specified, the emulator goes into -q (quiet) mode just before
		it starts to execute MIPS code.
20040627	The configure script now adds -fomit-frame-pointer to the
		compile flags if the $CC seems to be able to handle that.
		Found and fixed a serious interrupt bug in BT459 (Ultrix'
		behaviour required a hack, which was incorrect), so
		performance for machines using the PMAG-BA framebuffer is
		now improved.
		For X11 bitdepths other than 8 or 24, a warning message
		is printed at startup.
		A number of other minor fixes, optimizations, updated
		comments and so on.
		Adding a BUGS file, a list of known bugs.
		Adding a minimal man page, doc/mips64emul.1.
20040628	Hacks for faking the existance of a second level cache
		(ARCBIOS and other places).
		An important fix for dc7085: tx interrupts should happen
		before rx interrupts, not the other way around as it was
		before. (This speeds up NetBSD boot on DECstation, and
		fixes a bug which Ultrix triggered on heavy keyboard input.)
		A couple of other minor fixes.
		Framebuffer fix: there was a bug which caused the rightmost/
		bottom pixel to sometimes not be updated, when running in
		scaledown mode. This is now fixed.
		Adding a small program which removes "zero holes" from
		harddisk image files.
20040629	More minor fixes.
20040629(later)	Adding -A3 (NEC RISCstation 2200) (this is similar to
		the 2250 model that NetBSD/arc can already boot all the
		way into userland and be interacted with), and -A4
		(Deskstation Tyne).
		Some more minor fixes.
20040630	Adding support for 15 and 16 bits X11 framebuffers,
		and converting from XYPixmap to ZPixmap (this fixes the
		problem of updates appearing in "layers" on some X
		servers).
		The pixels in the mouse cursor (for BT459) are now colored
		as the emulated OS sets them, although no transparency
		masking is done on the edges of the cursor yet. (In plain
		English:  the mouse cursor is no longer just a white solid
		square, you can actually see the mouse cursor image
		on the white square.)

==============  RELEASE 0.1  ==============

20040701	The -j option now takes a name, the of the kernel as passed
		on to the bootloader.  ("netbsd" is the default name.)
		Adding support to load bootstrap code directly from a disk
		image, for DECstation.  Both NetBSD/pmax and Ultrix boot
		straight of a disk image now, with no need to supply a
		kernel filename on the command line.  (Ultrix still needs
		-j vmunix, though, to boot from /vmunix instead of /netbsd.)
20040702	Minor bugfix (some new untested code for X11 keypresses was
		incorrect).
20040702(later)	Adding an ugly hack for CDROMs in FreeBSD; if an fread() isn't
		done at a 2048-byte aligned offset, it will fail. The hack
		tries to read at 2048-byte aligned offsets and move around
		buffers to make it work.
		Adding video off (screen blanking) support to BT459.

==============  RELEASE 0.1.1  ==============

20040702(later)	Cleanup to remove compiler warnings (Compaq's cc, Solaris' cc,
		and gcc 3.3.3/3.3.4 in Linux), mostly by putting ULL on large
		numeric constants.
		Better support for scaledown of BT459 cursors, but still not
		color-averaging.
		Beginning the work on adding better memory latency support
		(instruction delays), enabled by the --delays configure option.
20040703	Modifications to the configure script so that a config.h file
		is created, containing things that were passed along as
		-Dxxx on each cc command line before.
		More work on instruction latency support; trying to separate
		the concepts of nr of cycles and nr of instructions.
20040704	Working on R2000/R3000 caches.
		Adding a '--caches' option to the configure script.
		Various small optimizations.
		R3000 caches finally work. (I know that there is at least one
		bug, regarding interrupt response.)
20040705	Working on the 'le' device, and on a generic (device
		independant) networking framework. le can transmit and receive
		packets, and the network framework fakes ARP responses from a
		fake gateway machine (at a fixed ip address, 10.0.0.254).
		Adding a '-c' command line option, which makes emulated_hz
		automatically adjust itself to the current number of emulated
		cycles per host CPU second (measured at regular intervals).
20040707	Removing the '-c' option again, and making it the default
		behaviour of the emulator to automatically adjust clock
		interrupts to runtime speed (as long as it is above 1 MHz).
		(This can be overridden by specifying a static clock rate with
		the -I option.)
		Updating the doc/ stuff a bit.
		Generalization of the DECstation bootblock loading, to work
		with Sprite/pmax. Lots of other minor modifications to make
		Sprite work, such as adding support for DECstation "jump table"
		PROM functions, in addition to the old callback functions.
		Sprite boots from a disk image, starting the kernel if the
		argument "-j vmsprite" is used, but it seems to not like the
		DBE exceptions caused by reading empty TURBOchannel slots. :-/
20040708	Minor changes and perhaps some tiny speed improvements.
		The Lance chip is (apparently) supposed to set the length of
		received packets to len+4. (I've not found this in any 
		documentation, but this is what NetBSD expects.) So now, ICMP
		echo replies work :-)  UDP works in the outgoing direction,
		in the incoming direction, tcpdump can see the packets but they
		seem to be ignored anyway. (Weird.)
		Adding a separate virtual-address-to-host-page translation
		cache, 1-entry for loads, 1-entry for stores. (For now, it
		only works on R4000 as there are conflicts with cache usage
		on R3000).
		Changing the lower clock speed bound from 1 MHz to 1.5 MHz.
20040709	Incoming UDP checksums were wrong, but are now set to zero
		and NetBSD inside the emulator now accepts the packets (eg.
		nameserver responses).  Host lookups and even tftp file
		transfers (using UDP) work now :-)
		Adding a section on Networking to the Technical documentation,
		and a preliminary NetBSD/pmax install instruction for network
		installs to the User documentation.
		Some updates to the man page.
20040709(later)	Fix to the TURBOchannel code to allow Sprite to get past the
		card detection. Seems to still work with Ultrix and NetBSD.
		This also makes Linux/DECstation properly recognize both the
		Lance controller and the SCSI controller. Linux 2.4.26 from
		Debian boots nicely in framebuffer mode :-)
20040710	Some bits in the KN02 CSR that were supposed to be readonly
		weren't. That has been fixed, and this allows Linux/DECstation
		to get past SCSI detection. :-)
		Minor updates to the ASC controller, which makes Linux and
		OpenBSD/pmax like the controller enough to be able to access
		SCSI devices. OpenBSD/pmax boots from a disk image for the
		first time. :-)  Linux detects SCSI disks, but I have no
		bootable Linux diskimage to test this with.
		Updating the doc/ to include instructions on how to install
		OpenBSD/pmax onto a disk image.
		Naively added a PMAGB-BA (1280x1024x8) in hopes that it would
		basically be a PMAG-BA (1024x864x8) in higher resolution,
		but it didn't work that way. I'll have to look into this later.
		Adding a -o option, useful for selecting '-s' (single user
		mode) during OpenBSD install and other things.
		After a lot of debugging, a serious bug related to the tiny
		cache was found; Linux just changes the ASID and returns when
		switching between processes in some occasions without actually
		_writing_ to the TLB, and I had forgotten to invalidate the
		tiny cache on such a change.
20040711(early)	I've been trying to repeat the OpenBSD install from yesterday,
		but appart from the first initial install (which was
		successful), I've only been able to do one more. Several
		attempts have failed with a filesystem panic in the middle
		of install. I'm not sure why.
20040711	I found the "bug": wget downloaded the simpleroot28.fs.gz file
		as read-only, and gunzip preserved those flags. Thus, OpenBSD's
		installer crashed as it didn't get its writes through to the
		disk.
		Parts of the 1280x1024x8 PMAGB-BA graphics card has been
		implemented, it works (unaccelerated) in NetBSD and OpenBSD,
		but Ultrix does not seem to like it.
		Cleaned up the BT459 cursor offset stuff a bit.
		Trying to make the emulated mouse coordinates follow the host's
		mouse' coordinates (for lk201, DECstation), by
		"de-accelerating" the data sent to the emulated OS.
20040711(later)	Fix so that Sprite detects the PMAG-BA correctly.
		Adding some stuff about NFS via UDP to the documentation.
		Fixed the 'update flag' for seconds, so now Sprite doesn't
		crash because of timer-related issues anymore.
		Fixing KN02 interrupt masks a bit more, to make Sprite not
		crash. Sprite now runs quite well.
20040712	Working on IP/UDP fragementation issues. Incoming UDP packets
		from the outside world can now be broken up into fragments
		for the guest OS. (This allows, for example, OpenBSD/pmax to
		be installed via nfs.)  Outgoing fragmented packets are NOT
		yet handled.
		Linux doesn't use 64-bit file offsets by default, which is
		needed when using large disk images (more than 2GB), so the
		configure script has now been modified to add necessary
		compiler flags for Linux.
20040713	Trying out some minor optimizations.
		Refreshing the UDP implementation in src/net.c a little.
20040714	Updating the documentation a little on how to experiment
		with a Debian Linux install kernel for DECstations.
		A 'mini.iso' Linux image for DECstation has different fields
		at offsets 0x10 and 0x14, so I'm guessing that the first is
		the load address and the second is the initial PC value.
		Hopefully this doesn't break anything.
		Some initial TCP hacks, but not much is working yet.
		Some updates for IP30:  The load/store 1-entry cache didn't
		work too well with IP30 memory, so it's only turned on for
		"MMU4K" now. (This needs to be fixed some better way.)
		Adding a hack which allows Linux/Octane to use ARC write()
		and getchild() on IP30. Linux uses ARCBIOS_SPB_SIGNATURE as a
		64-bit field, it was 32-bit before.
		Making ugly hacks to the arcbios emulation to semi-support
		64-bit equivalents of 32-bit structures.
20040716	Minor fixes to the configure script (and a few other places)
		to make the sources compile out-of-the-box on HP-UX (ia64
		and HPPA), old OpenBSD/pmax (inside the emulator itself), and
		Tru64 (OSF/1) on Alpha.
		A couple of other minor fixes.
20040717	A little TCP progress; OpenBSD/pmax likes my SYN+ACK replies,
		and tries to send out data, but NetBSD/pmax just drops the
		SYN+ACK packets.
		Trial-and-error led me to change the 64-bit ARCS component
		struct again (Linux/IP30 likes it now). I'm not sure about all 
		of the offsets yet, but some things seem to work.
		More 64-bit ARCS updates (memory descriptors etc).
		Better memory offset fix for IP30, similar to how I did it for
		IP22 etc. (Hopefully this doesn't break anything else.)
		Adding a MardiGras graphics controller skeleton for SGI-IP30
		(dev_sgi_mardigras.c).
		Thanks to Stanislaw Skowronek for dual-licensing mgras.h.
		Finally rewrote get_symbol_name() to O(log n) instead of O(n)
		(Stanislaw's Linux kernel had so many symbols that tracing
		with the old get_symbol_name() was unbareably slow).
		Removing all of the experimental tlbmod tag optimization code
		(the 1-entry load/store cache), as it causes more trouble than
		the performance gain was worth.
20040718	The MardiGras device works well enough to let Linux draw the
		SGI logo and output text.
		A bunch of other minor changes.
20040719	Trying to move out all of the instruction_trace stuff from the
		main cpu loop (for two reasons: a little performance gain,
		and to make it easier to add a GUI later on).
20040720	Finally found and fixed the ethernet/tcp bug. The hardware
		address is comprised of 6 bytes, where the _first_ byte should
		have a zero as the lowest bit, not the last byte. (This causes
		NetBSD and Linux running in the emulator to accept my SYN+ACK
		packets.)
		Getting the first nameserver address from /etc/resolv.conf.
		(This is not used yet, but could be useful if/when I add
		internal DHCP support.)
		Working more on the TCP stuff; TCP seems to be almost working,
		the only immediate problem left is that the guest OS gets
		stuck in the closing and last-ack states, when it shouldn't.
		It is now possible to install NetBSD and OpenBSD via ftp. :-)
20040721	Trying to fix the last-ack bug, by sending an RST after the
		end of a connection. (Probably not a correct fix, but seems
		to work?)
		Adding a my_fseek() function, which works like fseek() but
		with off_t instead of long, so that large disk images can
		be used on systems where long is 32 bits.
20040722	Trying to fix some more TCP related bugs.
20040725	Changing the inlined asm statement in bintrans_alpha.c into
		a call to a hardcoded array of bytes that do the same thing
		(an instruction cache invalidation). This allows the same
		invalidation code to be used regardless of compiler.
		Some other minor changes.
20040726	Minor updates. The configure script is now more verbose.
		A Debian/IP22 Linux tftp boot kernel requires ARCS memory to
		be FreeMemory, not FreeContiguous. (This should still work with
		other SGI and ARC OSes.)
		Fix for ARCS write(), so it returns good write count and
		success result (0).
		Some hacks to the IP22 memory controller, to fake 72MB RAM
		in bank 0.
		The IP22 Debian kernel reaches userland (ramdisk) when run
		with -G24 -M72 -CR4400, if a special hack is done to the
		zs device.
20040730	Removing mgras.h, as I'm not sure a file dual-licensed this way
		would work. (Dual-licensing as two separate files would work
		though.)
		Preparing for the upcoming release (0.2).
20040801	Fixing the 512 vs 2048 cdrom sector size bug; I hadn't 
		implemented the mode select SCSI command. (It still isn't
		really implemented.)
		A bug which crashes the emulator is triggered when run with
		new NetBSD 2.0_BETA snapshots on a Linux/i386 host. I'm not
		sure why.
		UDP packets sent to the gateway (at 10.0.0.254) are now
		forwarded to the machine that the host uses as its nameserver.
		Some other minor fixes.

==============  RELEASE 0.2  ==============

20040803	A post-3.5 OpenBSD/sgimips kernel snapshot with ramdisk seems
		to boot fine in the emulator, all the way to userland, and
		can be interacted with.
		Adding a -y option, used to set how many (random) instructions
		to run from each CPU at max (useful for SMP instruction
		interleave experiments).
		Importing a 8x16 console font from FreeBSD (vt220l.816).
		Adding a skeleton for a 80x25 text console device (dev_vga),
		useful for some ARC modes. (Character output is possible, but
		no cursor yet.)
		Adding a dev_zero device (returns zeroes on read).
		OpenBSD/arc 2.3 can get all the way to userland with -A4 (if
		the wdc devices are commented out) but bugs out there, probably
		because of interrupt issues.
		Adding a -A5 ARC emulation mode (Microsoft-Jazz, "MIPS Magnum")
		which NetBSD seems to like. No interrupt specifics yet, so
		it hangs while waiting for SCSI.
20040804	Some dev_mp updates.
		The -y switch has to do with number of cycles, not number
		of instructions; variable names have been changed to reflect
		this.
20040805	Minor updates. Adding some more CPU types/names, but they
		are probably bogus.
		Adding a MeshCube emulation mode. Just a skeleton so far, but
		enough to let a Linux kernel print some boot messages.
		Adding the 'deret' instruction.
20040806	Adding include/impactsr-bsd.h (a newer version of what was in
		mgras.h before, and this time with only a BSD-style license),
		but it is not used yet.
20040810	Some Au1500 updates.
20040811	Adding the 'clz', 'clo', 'dclz', and 'dclo' special2 (MIPS32
		and MIPS64) instructions.
		More Au1500 updates.
20040812	Using fseeko(), when it is available.
		Other minor updates.
		Adding a NetGear WG602 emulation mode skeleton (-g); after
		a lot of trial and error, a Linux kernel (WG602_V1715.img)
		gets all the way to userland, but hangs there.
20040818	Adding signal handlers to better cope with CTRL-Z and CTRL-C.
		Adding a simple interactive single-step debugger which is
		activated by CTRL-C. (Commands so far: continue, dump, help,
		itrace, quit, registers, step, trace, version)
20040818(later)	Adding a 'tlbdump' debugger command, and some other minor
		fixes.
20040819	Minor updates. Adding an 'unassemble' debugger command.
20040822	Minor updates to the regression testing framework.
20040824	Minor updates based on feedback from Alec Voropay
		(configure script updates for Cygwin and documentation).
20040826	Minor updates.
		Adding a cursor to the VGA text console device.
		Changing all old 11:22:..55:66 ethernet macs to 10:20..60,
		still hardcoded though.
20040828	Minor updates.
20040829	mips64emul is 1 year old today :-)
20040901	tests/README now lists "all" MIPS opcodes. This list should
		be updated whenever a new opcode is implemented, or when a
		regression test is added. (A combination of instructions from
		the TX79 manual, the GNU assembler, and the MIPS64 manual).
		Hopefully I haven't missed too many.
		Adding a section on regression testing to doc/technical.html.
20040902	Finally beginning the work on separating out the stuff from
		main.c into a "struct emul". Very time-consuming.
		Some minor fixes for LL/SC on R10000.
20040905	Moving more stuff from main.c into struct emul. Unfortunately,
		it seems that this causes a slowdown of the emulator.
		Userland emulation is now only used if --userland is used
		when running configure.
		Modifying src/symbol.c to not use global variables.
20040906	Minor update.
20040914	Using $COPTIM when detecting which compiler flags to use in
		the configure script. (This makes sure that combinations of
		flags should work.)
		There'll probably be a 0.2.1 release some time soon, but I'll
		do some more clean-up first.
		Minor update to the detection of ECOFF files, but I don't like
		it; sometimes the endianness of the magic value seems to be
		swapped, but it doesn't have to do with endianness of the
		actual data?
20040916	Minor updates. Adding an Example section to the manpage, but
		as I'm not really familiar with manpage formatting, it will
		need to be rewritten later.
20040917	Finally making the coprocessor instructions disassemblable
		even when not running.
		Doing some testing for the 0.2.1 release.

==============  RELEASE 0.2.1  ==============

20040923	Updating the documentation about how to (try to) install
		Debian GNU/Linux.
20040924	Some more updates to the documentation.
20040925	Adding overflow stuff to 'add' and 'sub'.
20040926	Minor updates: possibly a fix to 'sltiu' (the imm value
		should be treated as signed, and then converted to unsigned,
		according to the MIPS64 manual), and removing the
		'last_was_rfe' stuff (again).
		OpenBSD/arc used speed-hack jumps with other deltas than just
		+/- 1 (it used -3 iirc), so the jump speedhack should now
		support any delta. Also adding bgtzl and blezl as possible
		instructions for the speed-hack jumps. (This needs to be
		tested more.)
20040928	Minor updates. Some ARC stuff ("arcdiag" runs now).
		cpu_register_dump() now also dumps coprocessor registers.
20040929	More ARC updates. Making the code look a tiny bit nicer
		than before. "arcdiag.ip22" works for -G22 (SGI-IP22).
		Apparently the overflow support in the 'add' instruction
		was incorrect, so I disabled it.
20041002	Trying to install Ultrix in the emulator, but the installer
		crashes; found (and fixed) the bug rather quickly: the "fix"
		I implemented a few days ago for the 'sub' instruction
		(according to the MIPS64 manual) caused the bug.
20041004	Changing the behaviour of the -j command line option. The
		default is now "" (or taken from the last filename given on
		the command line), not "netbsd". In practice, this doesn't
		change much, except that -j netbsd.pmax is no longer needed
		when installing NetBSD.
		Adding a COMPILE_DATE string to config.h.
20041007	Adding a NEC RISCserver 4200 model (-A6), and some more
		updates to the ARC component tree generator.
20041008	The 'll' instruction should be signed, not unsigned as before.
		This (and some other minor fixes) causes Irix on SGI-IP32 (O2)
		to actually boot far enough to print its first boot messages :)
		Working on some new dynamic bintrans code. Enough is now
		implemented so that the 'nop' instruction is translated
		and there is support for Alpha, i386 and UltraSparc backends,
		but performance is about 50% worse than when running without
		bintrans. (This is as expected, though.)
20041009	Minor updates to the documentation.
		Using mprotect() to make sure that the code created dynamically
		by the bintrans subsystem is allowed to be executed. (This
		affects newer OpenBSD systems, and possibly others.)
		The translated code chunks now only get one argument passed to
		them, the (struct cpu *) of the current cpu.
20041010	Hack to dev_le.c which makes Ultrix accept the initialization
		of the LANCE controller. (This goes against the LANCE
		documentation though.)
		In src/net.c, a fix for Ultrix (which seems to send larger
		ethernet packets than the actual TCP/IP contents). The hack to
		dev_le.c and this fix is enough to let Ultrix access the
		Internet.
		For DECstation, when booting without a disk image (or when
		"-O" is used on the command line), use "tftp" instead of "rzX"
		for the boot string.
20041011	Adding cache size variables to the emul struct, so that these
		can be set on a per-machine basis (or potentially manually
		on the command line).
20041012	Mach/PMAX now passes the LK201 keyboard self-test (although
		the keyboard ID is still bogus).
20041013	Minor updates.
		Hacks to the ASC SCSI controller for Mach/PMAX, hopefully this
		will not break support for other OSes.
20041014	Minor fix to src/emul.c for reading bootblocks at the end of
		a disk or cdrom image (thanks to Alexandru Lazar for making me
		aware of this).
		Adding "gets()" to src/dec_prom.c.
		Working a bit on ARC stuff. Importing pica.h from NetBSD.
		Minor updates to the ARC component tree for PICA-61.
		Adding a dev_jazz.c (mostly for PICA-61).
		Renaming dev_jazz.c into dev_pica.c. Working on PICA timer
		and interrupt specifics.
20041016	Adding some dummy entries to lk201.c to reduce debug output.
		Some bintrans updates (don't run in delay slots or nullified
		slots, read directly from host memory and not via memory_rw(),
		try mmap() before malloc() at startup, and many other minor
		updates).
		Adding bintrans_mips.c for 64-bit MIPS hosts, but it is not
		used yet.
20041017	Minor updates.
20041018	Update to dev_mc146818 to allow Mach to boot a bit further.
		The "hardware random" in dev_mp.c now returns up to 64 bits
		of random.
20041019	Minor updates to the way cache sizes are used throughout the
		code. Should be mostly ok for R[234]x00.
		src/file.c now loads files using NO_EXCEPTIONS. Whether this
		is good or bad, I'm not sure.
20041020	Adding a Linksys WRT54G emulation skeleton (-H).
20041021	Minor updates.
		R1[024]000 cache size bits in the config register should now
		be ok.
		Trying to make dev_asc.c work better with PICA.
		More work on PICA interrupts (but they are broken now).
20041022	Generalizing the dev_vga text console device so that it can be
		used in other resolutions than just 80x25. Works with
		OpenBSD/arc.
		emul->boot_string_argument is now empty by default (except
		for DECstation modes, where it is "-a").
		Speedup of dev_ram by using mmap() instead of malloc().
		Using mmap() in memory.c as well, which reduces memory usage
		when emulating large memory sizes if the memory isn't actually
		written to.
20041023	Minor updates.
20041024	Updates to the PC-style keyboard controller, used by PICA.
		Updates to the PICA (Jazz) interrupt system. Both NetBSD/arc
		and OpenBSD/arc now reach userland with PICA emulation, and
		can be interacted with (there are a few programs on the
		INSTALL kernel ramdisks). In the case of OpenBSD, a VGA text
		console and PC-style keyboard controller is used, NetBSD
		runs on serial console.
		Adding a framework for DMA transfer for the ASC SCSI
		controller.
		Implementing a R4030 DMA controller for PICA, enough to let
		OpenBSD/arc and NetBSD/arc be installed on an emulated
		Pica. :-)
		Updates to the documentation.
20041025	Working on ISA interrupts for PICA.
		Adding an Olivetti M700 emulation mode (-A7).
		Better separation of PICA and M700 stuff (which I accidentally
		mixed up before, I thought the M700 Linux kernel would 
		also work on PICA because it almost booted).
		Writing a skeleton G364 framebuffer for M700, enough to show
		the Linux penguin and some text, although scrolling isn't
		correctly implemented yet.
		Adding a dummy SONIC (ethernet) device, dev_sn, for PICA.
		Fixing the passing of OSLOADOPTIONS for ARC, the default is
		now "-aN" which works fine with OpenBSD/arc and NetBSD/arc.
20041027	Minor updates.
20041029	Adding a Sony NeWS "newsmips" emulation mode skeleton (-f).
		Found and fixed a bug which prevented Linux/IP32 from running
		(the speed-hack-jump-optimization fix I made a few weeks ago
		was buggy).
		Adding the trunc.w.fmt and trunc.l.fmt instructions, although
		the are probably not really tested yet.
		Changes to how floating point values are handled in
		src/coproc.c, but right now it is probably very unstable.
20041101	I had accidentally removed the instructions on how to install
		Ultrix from doc/index.html. They are back now.
		Adding a -Z option, which makes it easier to run dual- or
		tripple-head with Ultrix. (Default nr of graphics cards
		without -X is 0, with -X is 1.)
		Minor update which makes it possible to switch to the left
		monitor when running tripple-head, not just right as before.
		When using more than one framebuffer window, and the host's
		mouse cursor is in a different window than the emulated mouse
		cursor, the emulated mouse will now try to move "very far",
		so that it in practice changes screen.
		Running Ultrix with dual- and tripple-head now feels really
		great.
20041101(later)	OpenBSD/arc and Linux/Olivetti-M700 don't both work at the
		same time with the speed-hack stuff. So, from now on, you
		need to add -J for Linux, and add nothing for openbsd.
20041102	Minor update for OSF/1 V4.0 (include sys/time.h in src/net.c
		and add -D_POSIX_PII_SOCKET to the C compiler flags).
20041103	Minor updates for the release.
		For some reason, Mach/PMAX caused the emulator to bug out on
		SunOS/sparc64 (compiled in 64-bit mode); a minor update/hack
		to dev_asc fixed this.

==============  RELEASE 0.2.2  ==============

20041103	Minor updates.
20041104	Minor updates.
20041105	Running with different framebuffer windows on different X11
		displays works now (even with different bit depths and
		endiannesses on different displays). A new command line option
		(-z) adds DISPLAYs that should be used.
		Update regarding how DECstation BT459 cursors are used;
		transparency :-) and some other bug fixes.
20041106	More bt459 updates. The cursor color seems to be correct for
		NetBSD, OpenBSD, Ultrix, and Sprite.
		Some minor bintrans updates (redesigning some things).
20041107	More bintrans updates (probably broken for non-Alpha targets).
		Moving doc/mips64emul.1 to man/.
20041108	Some updates.
20041109	More updates. Bintrans experiments mostly.
20041110	Some minor bintrans updates.
20041111	Minor updates.
20041112	A little rewrite of the bintrans system (again :-), this time
		a lot more naďve and non-optimizing, in order to support delay
		slots in a much simpler way.
		Ultrix 4.5 boots into a usable desktop on my home machine in
		3min 28sec, compared to 6-8 minutes without bintrans.
20041113	Some minor bintrans updates.
20041114	More bintrans updates. Ultrix now boots in exactly 3 minutes
		on my home machine.
20041115	More bintrans updates.
20041116	Bintrans updates.
20041117	Working on dev_dec_ioasic and related issues.
		Adding support for letting translated code access devices in
		some cases (such as framebuffers).
20041118	Moving some MIPS registers into Alpha registers, which gives
		a speed improvement.
		Beginning to write an i386 bintrans backend. Skeleton stuff
		works, lui, jr/jalr, addiu/daddiu/andi/ori/xori, j/jal,
		addu/daddu/subu/xor/or/nor/and.
20041119	dsubu/sll/srl/sra, rfe,mfc0,dmfc0, beq,bne, delayed branches.
		Some load/store (but not for bigendian emulation yet.)
		Time to reach Ultrix 4.5's graphical login on a 2.8 GHz Xeon
		host is now down to 20 seconds!
		Adding bgez, bltz, bgtz, and blez to the i386 backend.
20041120	Minor updates (bintrans related mostly).
		Time to reach Ultrix login on the Xeon is now 11 seconds.
		Adding 'mult', 'multu' and a some parts of mtc0 to the Alpha
		backend.
		The transparency updates to the X11 cursor support made the
		OpenBSD/arc cursor disappear; that has been fixed now.
		Unfortunately, something with Acer Pica emulation is broken
		when bintrans is enabled.
20041121	Making tlbwr, tlbwi, tlbp, tlbr callable directly from
		translated code.
		Adding sltiu, slti, slt, and sltu to the i386 backend.
20041122	More bintrans updates.
		With the Alpha backend, the status and entryhi registers
		can (in some cases) be written without exiting to the main
		loop. Ultrix boot time until a usable desktop is reached
		is about 1 min 35 seconds on the 533 MHz pca56.
		Adding srlv, srav, and sllv to the i386 backend.
20041123	Removing the special handling of high physical addresses for
		DECstation emulation from the main memory handling code, and
		replacing it with a mirror device instead. (This results in
		a tiny increase in performance, and cleaner code.)
		Various minor updates.
20041124	Ripping out _all_ bintrans load/store code, because I have
		a new idea I'd like to try out.
		A total rewrite of the load/store system. It works when
		emulating 32-bit MIPS, but not for 64-bit code yet.
		Some minor updates to the dev_fb, but no speed improvement.
		Making the 'le' ethernet device' SRAM work with bintrans.
20041125	Various updates.
		Adding a little "bootup logo" to the framebuffer.
		There is now one translate_address() for R3000-style MMUs,
		and one for the other types. (This gives a tiny speed
		improvement.)
20041126	Minor updates, bintrans.
		Fixing the bug which caused OpenBSD/arc (R4000) to bug out;
		it was introduced between the 7:th and 10:th of November
		when moving up the check for interrupts to above the code
		which runs bintrans code, in src/cpu.c.
		Adding movn and movz to the Alpha bintrans backend.
20041127	Various minor updates.
20041128	Making the R2000/R3000 caches work with bintrans, even in
		isolated mode. (Not true cache emulation, but it works with
		NetBSD/pmax, OpenBSD/pmax, and Ultrix.)
		Making the default cache size for R3000 4KB instr, 4 KB data;
		a real R3000 could have 64KB each, but emulated OSes run
		faster when they think the cache is smaller :-)
		Updates to the i386 backend: the nr of executed instructions
		is now placed in ebp at all times, and some support for
		mtc0 similar to how it is done in the Alpha backend has been
		added. A full NetBSD/pmax 1.6.2 install can now be done in
		5 minutes 35 seconds, on a 2.8 GHz Xeon host (with -bD2 -M20).
		Adding mult and multu to the i386 bintrans backend.
		Reducing the number of malloc/free calls used by the
		diskimage subsystem.
20041129	Minor updates to the Alpha bintrans backend.
20041130	Trying to fix the bug which prevents Linux from working
		with bintrans. It _seems_ to work now. (Pages could in some
		cases be written to after they were translated, but that
		has been fixed now.)
		A couple of other minor fixes.
		Minor updates to the Alpha backend (directly using Alpha
		registers in some cases, instead of loading into temporaries).
		Updates to the i386 backend (special hacks for 32-bit
		MIPS emulation, which are fast on i386, for example only
		updating half of the pc register).
20041201	More updates to the i386 backend, similar to those yesterday.
		Preparing for release 0.2.3.
		Adding a generic load/store mechanism, which is used when the
		32-bit optimized version cannot be used (for R4000 etc).

==============  RELEASE 0.2.3  ==============

20041202	If ALWAYS_SIGNEXTEND_32 is defined in misc.h, and an
		incorrectly extended register is detected, the emulator now
		exits instead of continues.
		Removing the LAST_USED_TLB_EXPERIMENT stuff.
		Minor updates to work better with Windows NT's ARCINST.EXE;
		printing 0x9b via arcbios becomes ESC + '[', and the ARC
		memory descriptor stuff has been generalized a bit more.
		Adding arbios hacks for Open(), Seek(), GetRelativeTime(),
		and Read() to allow WinNT's SETUPLDR to read the filesystem
		on the diskimage used for booting.
20041203	Adding a terminal emulation layer which converts arcbios
		stdout writes to "VGA text console" cell characters. Seems
		to work with Windows NT and arcdiag.
		Adding a 8x8 font to dev_vga.
		Adding more ARC components to the component tree (for PICA
		emulation).
20041204	Minor updates.
		More updates to dev_vga. Adding a 8x10 font.
		Adding a hack so that the framebuffer logo is visible at
		startup with dev_vga. (It disappears at the first scroll.)
		A minor fix for a bug which was triggered when running
		dual- or tripple-head, on 2 or 3 actual X11 displays.
20041205	Fixing a bintrans bug.
		Some other minor updates (some of them bintrans related).
20041206	Moving the web page to http://gavare.se.
		Adding a hack for mmap() which supports anonymous mapping
		using /dev/zero, but not using MAP_ANON{,YMOUS}.
		Separating out opcodes.h, cop0.h, and cpu_types.h from misc.h.
20041207	Minor bintrans update. (In some cases, it isn't necessary
		to return to the main loop, when translating from a new page.)
		Some other minor i386 bintrans backend optimizations.
		And some other minor updates.
		i386 backend update: the lowest 32 bits of the pc register
		are now placed in an i386 register.
20041208	Adding GetConfigurationData() and some support for config
		data, to src/arcbios.c.
		Adding a bogus 0xbd SCSI command (used by Windows NT). It is
		not listed in http://www.danbbs.dk/~dino/SCSI/SCSI2-D.html.
		If the framebuffer cursor contains more than 1 color, then
		the host's X11 cursor disappears. (Nice for DECstation
		emulation with emulated X.)
		For ARC and SGI emulation, if an exception occurs before an
		exception handler is installed, the emulator now exits
		nicely (as suggested by Alec Voropay).
		A couple of minor updates to the ARCBIOS emulation subsystem.
		The single step debugger is now automatically entered when
		all CPUs have stopped running, unless there was a clean
		shutdown of some kind (PROM halt() call, or similar).
		Adding a -V option for starting up in a paused state, into
		the single-step debugger.
		Adding a note about 'mmon' to the documentation
		(http://www.brouhaha.com/~eric/software/mmon/).
20041209	Fixes to devices/console.c which makes cursor keys and such
		a bit more reliable.
		ARCBIOS hack/update which creates memory descriptors _after_
		loading the executable. (Seems to work with OpenBSD/arc,
		NetBSD/arc, arcdiag, IRIX, NetBSD/sgimips, OpenBSD/sgi, and
		some Windows NT executables.)
		ARCBIOS support for cursor keys (ESC + '[' ==> 0x9b).
		A bintrans update (for 32-bit emulation) which speeds up
		jumps between pages, if code is already translated.
		Changing the default bintrans cache from 20 to 24 MB.
20041210	Optimizing unaligned load/stores a little bit in src/cpu.c.
		Omiting the check for nr of executed bintrans instructions
		on some forward jumps.
		Adding the 'syscall' and 'break' instructions to the
		bintrans backends.
		Allowing more bits of the status register to be written to
		from within inside translated code, on R3000.
		Getting rid of the final pixel when hiding the host's mouse
		cursor.
		store_buf() now copies data 8 or 4 bytes at a time, when
		possible. (This speeds up emulated ROM disk reads, etc.)
		Tiny bug fix: coprocessor unusable exceptions are now also
		generated (for coproc 1..3) even when in kernel mode, if the
		coprocessors are not enabled. This allows a Debian installation
		to proceed further than before. (It's still very unstable,
		though.)
20041212	Updating doc/index.html with better Debian installation
		instructions.
		If SLOWSERIALINTERRUPTS is defined at compile time, interrupts
		from the dc7085 device will not come as often as they normally
		do. This makes Debian seem more stable.
		Decreasing the bintrans cache to 20 MB again.
		Updating some files in preparation for a 0.2.4 release.
20041213	Updating the docs on how to install NetBSD 2.0/pmax, and also
		some updates to the section on installing Debian.
		32-bit bintrans backend optimization: don't inline large
		chunks of code, such as general jumps.
20041214	Minor fix for coproc unusable for R4000 (it's the PC that,
		matters, not the KSU bits).
		Separating out the debugger from emul.c into debugger.c.
		Rewriting parts of the debugger.
		Removing the -U command line option, as it wasn't really
		useful. Also removing the -P option.
		Renaming all instances of dumppoint to breakpoint, as that
		is what it really is.
		When a breakpoint is reached, the single-step debugger is
		entered, instead of just turning on instruction trace.
		Adding a 'breakpoints' debugger command.
		Better fix for coproc unusable on R4000: the KSU bits matter,
		but the ERL and EXL bits override that.
		Fix which allows Debian to boot directly from a disk image
		(with DELO). (It reads multiple separate areas from disk.)
		Update to the SLOWSERIALINTERRUPTS stuff, making it even
		slower.
		Fixes based on feedback from Alec Voropay (-Q with ARC
		emulation skips the setup of arcbios data structures in
		memory, and no sign-extension _after_ writing a 32-bit
		value to a 64-bit coproc 0 register).
		Adding a 'devices' command to the debugger.
		The 'registers' and 'tlbdump' commands now take an optional
		argument (a cpu id).
		Adding rudimentary tab-completion and cursor key stuff to
		debugger_readline().
		Adding some more debugger commands: 'bintrans' and 'machine'.
20041215	Adding a 'devstate' command; implementing a skeleton for a
		state function for the bt459 device.
		Implementing yet another variant of the SLOWSERIALINTERRUPTS
		stuff.
		Implementing more of the different exception offsets (taking
		CAUSE_IV and STATUS_BEV into account).
		hpc_bootinfo should now be correctly filled on big-endian
		hosts.
		Always shift left by 12, not by pageshift, to get physical
		addresses on MMU4K etc. (Thanks to Alec Voropay for noticing
		this.)
20041216	The KN02's CSR can now be read from bintranslated code.
		Adding a dummy dev_sgi_mec.
20041217	The default framebuffer and model settings for -F (hpcmips)
		should now be almost like Cassiopeia E-500.
		Changing -DSLOWSERIALINTERRUPTS into a command line option, -U.
20041218	Continuing a little bit on the mec controller.
		Removing lots of #include <math.h> that weren't really used.
20041219	Fixing stuff that broke because of the pageshift bugfix.
		Adding an argument to the s (step) debugger command, for doing
		more than 1 step at a time.
		ARCBIOS components representing disk images are now created
		to actually match the disk images in use, and some other
		arcbios-related updates; adding a dummy GetComponent().
		Adding a 'lookup' command to the debugger, for symbol lookups.
		Adding a "NEC Express RISCserver" mode (NEC-R96, -A8).
		Adding a dummy ARCBIOS GetFileInformation(), GetTime(), and
		SetEnvironmentVariable().
20041220	Improved command line editing (including command history)
		in the debugger.
		Separating some more .h files from each other, and fixing
		some Solaris compiler warnings.
20041221	Minor updates.
20041222	Minor updates; hpcmips (BE300, VR41xx) stuff.
		The 'register' debugger command is now 'reg', and it can
		be used to modify registers, not just read them.
		The syntax for hpcmips (-F) is now -F xx, where xx is a
		machine model identifier. (1 = BE300.)
20041223	Some really minor updates.
20041226	Minor updates to doc/index.html (NetBSD 1.6.2 -> 2.0, and
		some other rearrangements).
		Many updates to the debugger (better register manipulation,
		breakpoint manipulation, and other updates).
		Fix to dev_cons.c to allow the regression tests to work again.
		The configure script now tries to detect the presence of a
		MIPS cross compiler. (Used by "make regtest".)
		Regression tests are now run both with and without bintrans.
20041227	Some hacks to the VR41xx code to allow Linux for BE300 to
		get far enough to show the penguin on the framebuffer.
20041228	Merging dev_kn01_csr.c and dev_vdac.c into dev_kn01.c.
20041229	Various updates to the debugger (nicer tlb output and other
		things).
		Some floating point fixes in src/coproc.c (mov is not
		an arithmetic instruction), and in src/cpu.c (ldcX/sdcX in
		32-bit mode uses register pairs).
		'-O' now also affects the bootstring for SGI and ARC emulation.
		Bintrans updates (slightly faster 32-bit load/store on alpha).
		Updates to the i386 backend too, but no real speed improvement.
20041230	Cleaning up parts of the 64-bit virtual-to-physical code for
		R10000, and per-machine default TLB entries can now be set
		for SGI and ARC machines.
		Fix: SGI-IP27 is ARC64, not ARCS.
20050101	Minor updates.
20050102	Minor updates.
		Fixing a 32-bit 'addu' bug in the bintrans backends.
		Allowing fast load/stores even in 64-bit bintrans mode, if
		the top 32 bits are either 0x00000000 or 0xffffffff (for Alpha
		only).
		Re-enabling ctc0/cfc0 (but what do they do?).
		Adding beql, bnel, blezl, and bgtzl to the Alpha backend.
20050103	Adding fast 32-bit load/store for 64-bit mode emulation to
		the i386 backend too (similar to the Alpha code). Not really
		tested yet, though.
		Adding an incomplete regression test case for lwl/lwr/ldl/ldr.
		Playing around with bintranslated lwl and lwr for Alpha.
20040104	Changing many occurances of pica to jazz.
		Various other updates.
20050105	Fixing some more bintrans bugs (both Alpha and i386).
		Unaligned stores that cause tlb refill exceptions should now
		cause TLBS exceptions, not TLBL.
		Adding experimental swl and swr to the Alpha backend.
		Adding lwl, lwr, swl, and swr to the i386 backend.
20050106	Adding another hpcmips model (Casio E-105, -F2), and doing
		some updates to the VR41xx code. NetBSD/hpcmips prints some
		boot messages.
20050108	Minor updates.
20050109	dev_dec5500_ioboard.c and dev_sgec.c => dev_kn220.c.
		dev_crime.c, _mace.c, and _macepci.c => dev_sgi_ip32.c.
		Also adding dev_sgi_mec, _ust, and _mte into dev_sgi_ip32.c.
		A slight license change. Still revised BSD-style, though.
		memory_v2p.c is now included separately for MMU10K and
		MMU8K.
		Fixing a NS16550 bug, triggered by NetBSD 2.0, but not 1.6.2.
		Refreshing the UltraSPARC bintrans backend skeleton.
		Merging dev_decbi, _deccca, and _decxmi into dev_dec5800.c.
		Sparc backend instructions done so far: mthi/mtlo/mfhi/mflo,
		lui, addu, daddu, subu, dsubu, and, or, nor, xor, sll, dsll,
		srl, and sra.
		Adding more sparc backend instructions: addiu, daddiu, xori,
		ori, andi, srlv, srav, sllv, slt, sltu, slti, sltiu.
20050110	Changing the default bintrans cache to 16 MB, and some other
		minor updates.
		Adding div and divu to the i386 backend (but not Alpha yet).
		More work on ARCBIOS emulation.
		Trying to find a bug which affects Linux on Playstation 2 in
		bintrans mode.
20050111	Moving around some Playstation 2 stuff, but I haven't found
		the bug yet. It is triggered by load/stores.
		More ARCBIOS updates, enough to let Windows NT partition
		disks in some rudimentary fashion.
20050112	Testing for release 0.2.4.
		Fixes to suppress compiler warnings.

==============  RELEASE 0.2.4  ==============

20050113	Minor updates.
20050114	Fix to the Alpha bintrans backend to allow compilation with
		old versions of gcc (2.95.4).

==============  RELEASE 0.2.4.1  ==============

20050115	Various updates and fixes: some IP32 stuff, the debugger,
		ns16550 loopback tx isn't transmitted out anymore, ...
		Removing old/broken R10000 cache hacks, which weren't really
		used.
20050116	Minor updates to the documentation on using PROM images.
		Adding ARCBIOS function 0x100 (used by IRIX when returning
		from main, but undocumented).
		MC146818 updates (mostly SGI-related).
		ARCS64 updates (testing with an OpenBSD snapshot in IP27
		mode). This causes Linux/IP30 to not work. Maybe IP27 and
		IP30 differ, even though both are 64-bit?
		Removing some nonsensical ARCS64 code from machine.c.
		Better handling of 128MB and 512MB memory offsets used by
		various SGI models.
		Trying to revert the ARCS64 changes (OpenBSD/sgi does
		seem to be aware of 64-bit vs 32-bit data structures in
		_some_ places, but not all), to make Linux/IP30 work again.
		Adding "power off" capability to the RTC, as used on IP32
		(and possibly IP30 and others).
		Some IP30 updates.
20050117	Debugger updates (symbolic register names instead of just rX,
		and using %08x instead of %016llx when emulating 32-bit CPUs
		in more places than before).
		Removing the dummy sgi_nasid and sgi_cpuinfo devices.
		Also using symbolic names for coprocessor 0 registers.
		Adding DEV_MP_MEMORY to dev_mp.c.
		Adding a 'put' command to the debugger.
		ARCBIOS function 0x100 used by IRIX seems to _NOT_ be a
		ReturnFromMain(), but something else undocumented.
		The count and compare registers are now 32-bit in all
		places, as they should be. (This causes, among other things,
		OpenBSD/sgi to not hang randomly in userspace anymore.)
		On breakpoints, the debugger is now entered _at_ the
		instruction at the breakpoint, not after it.
		Some cursor keys now work when inputed via X.
		Refreshing the MC146818 device a bit more.
20050118	Trying to add some support for less-than-4KB virtual pages,
		used by at least VR4131. Thanks to Alexander Yurchenko for
		noticing this. (I'm assuming for now that all R41xx work
		this way, which is not necessarily true.) It doesn't really
		work yet though.
		Renicing the "loading files" messages and other things
		displayed during startup.
		Changing the disassembly output of ori, xori, and andi to
		unsigned hex immediate, instead of decimal (as suggested
		by Alec Voropay).
		configure-script update for HP-UX, and switching from using
		inet_aton() to inet_pton() (as suggested by Nils Weller).
		Also adding -lnsl on Solaris, if required by inet_pton().
		Lots of minor R4100-related updates.
20050119	Correcting the R4100 config register in src/coproc.c, and
		a minor update to dev_vr41xx.
		Finally began a redesign/remodelling/cleanup that I have had
		in mind for quite some time... moving many things that were
		in struct emul into a new struct machine.
		Userland emulation now works with bintrans.
		Refreshing the LANCE controller (dev_le.c).
		Fixing the LK201 keyboard id.
20050120	Continuing on the remodelling/cleanup.
		Fixing the SCSI bug (which was triggered sometimes by
		NetBSD 2.0/pmax on Linux/i386 hosts).
		Adding a speed-limit hack to the mc146818 device when running
		in DECstation mode (limiting to emulated 30 MHz clock, so
		that Ultrix doesn't freak out).
		Adding an ugly workaround for the floating-point bug which
		is triggered when running NetBSD/pmax 2.0 on an Alpha host.
		The count/compare interrupt will not be triggered now, if
		the compare register is left untouched.
		Many, many other fixes...
20050121	Continuing the remodelling/cleanup. (Mostly working on the
		network stack, and on moving towards multiple emulations
		with multiple machines per emulation.)
		Fixbug: not clearing lowest parts of lo0 and hi on tlbr
		(seems to increase performance when emulating Linux?).
20050122	Continuing the remodelling/cleanup.
		Linux on DECstation uses a non-used part of the RTC registers
		for the year value; this is supported now, so Linux thinks
		it is 2005 and not 2000.
		Began hacking on something to reply to Debian's DHCP requests,
		but it's not working yet.
20050123	Continuing the remodelling/cleanup.
20050124	Continuing the remodelling/cleanup.
		Converting the dev_vga charcell memory to support direct
		bintrans access (similar to how dev_fb works), and fixing a
		couple of bintrans bugs in the process.
		The emulator now compiles under OpenBSD/arc 2.3 without
		crashing (mostly due to the bintrans fixes, but also some
		minor updates to the configure script).
20050125	Continuing the remodelling/cleanup.
		The '-a' option was missing in the Hello World example in the
		documentation. (Thanks to Soohyun Cho for noticing this.)
20050126	Continuing the remodelling/cleanup. Moving around stuff in
		the header files, etc. Adding a '-K' command line option, which
		forces the debugger to be entered at the exit of a simulation,
		regardless of failure or success. Beginning to work on the
		config file parser.
		Splitting doc/index.html into experiments.html, guestoses.html,
		intro.html, and misc.html.
		Updating the man page and adding a skeleton section about the
		configure files to doc/misc.html.
20050127	Minor documentation updates.
20050128	Continuing the remodelling/cleanup, mostly working on the
		config file parser (adding a couple of machine words, enough
		to run simple emulations, and adding support for multi-line
		comments using tuborgs).
		Removing some command line options for the least working
		emulation modes (-e, -f, -g, -E, -H), adding new -E and -e
		options for selecting machine type.
		Moving global variables from src/x11.c into struct machine (a
		bit buggy, but it seems to almost work).
20050129	Removing the Playstation 2 mode (-B) and hpcmips modes (-F)
		from the command line as well.
		Changing the -T command line option from meaning "trace on bad
		address" to meaning "enter the single-step debugger on bad
		address".
		More updates to the configuration file parser (nested tuborg
		comments, more options, ...).
		Making -s a global setting, not just affecting one machine.
		Trying to fix the X11 event stuff... but it's so ugly that it
		must be rewritten later.
		Continuing the multi-emul cleanup.
		Bugfixes and other updates to dev_vga.
20050130	Continuing the remodelling/cleanup. Finally moving out the
		MIPS dependant stuff of the cpu struct into its own struct.
		Renaming cpu.c to cpu_mips.c, and cpu_common.c to cpu.c.
		Adding a dummy cpu_ppc.c.
		Removing the UltraSPARC bintrans backend.
		Many other minor updates.
		src/file.c should now be free from MIPS-dependancies.
20050131	Continuing a little bit more on src/file.c. PPC ELFs can now
		be loaded, it seems.
		Continuing on src/cpu_ppc.c.
		'mips' is undefined by the configure script, if it is defined
		by default. (Fixes build on at least OpenBSD/arc and
		NetBSD/arc, where gcc defines 'mips'.)
		A couple of other minor fixes.
		Removing the "Changing framebuffer resolution" section from
		doc/misc.h (because it's buggy and not very useful anway).
		Adding a mystrtoull(), used on systems where there is no
		strtoull() in libc.
		Adding 'add_x11_display' to the configure file parser 
		(corresponding to the -z command line option).
		Continuing the multi-emul machine cleanup.
20050201	Minor updates (man page, RELEASE, README).
		Continuing the cleanup.
		Adding a 'name' field to the emul struct, and adding a command
		to the debugger ("focus") to make it possible to switch focus
		to different machines (in different emuls).
		Beginning to work on the PPC disassembler etc. Hello World
		for linux-ppc64 can be disassembled :-)
20050202	Adding a hack for reading symbols from Microsoft's variant of
		COFF files.
		Adding a dummy cpu_sparc.c and include/cpu_sparc.h.
		Cleaning up more to support multiple cpu families.
		Various other minor updates.
		Fixing another old-gcc-on-Alpha problem.
20050203	Bintrans cache size is now variable, settable by a new
		configuration file option 'bintrans_size'.
		The debugger can now theoretically call disassembler functions
		for cpu families with non-fixed instruction word length.
		Working more on the mec controller. It now works well enough
		to let both NetBSD/sgimips and OpenBSD/sgi connect to the
		outside world using ftp :-)
		Continuing on the cleanup of the networking subsystem.
20050204	Continuing the cleanup.
		Working on a way to use separate xterms for serial ports and
		other console input, when emulating multiple machines (or one
		machine with multiple serial lines active).
20050205	Minor documentation updates.
20050206	Moving console.c from devices/ to src/, and continuing the
		work on using separate windows for each serial console.
		Trying to get OpenBSD/sgi to boot with root-on-nfs on an
		emulated NetBSD/pmax server, but no success in setting up
		the server yet.
20050207	Continuing on the console cleanup.
		Adding a 'start_paused' configuration file option, and a
		'pause' command to the debugger.
20050208	Everything now builds with --withoutmips.
		Continuing on the documentation on how to run OpenBSD/sgi, but
		no actual success yet.
		sizeof => (int)sizeof in the configure script (as suggested by
		Nils Weller).
20050209	Adding a check for -lm to the configure script.
		Continuing on the cleanup: trying to make memory_rw non-MIPS
		dependant.
		Trying to make a better fix for the cdrom-block-size problems
		on FreeBSD. (It now works with a Windows NT 4.0 cdrom in my
		drive.)
		Began a clean-up of the userland subsystem.
20050210	Continuing the userland cleanup.
		IBM's Hello World example for Linux/PPC64 runs fine now.
20050211	Continuing the cleanup. Removing the --userland configure
		option (because support for userland is always included now).
		Working more on getting OpenBSD/sgi to boot with root on
		nfs. (Booting with the ramdisk kernel, and mounting root via
		nfs works, but not yet from the generic kernel.)
		Major update to the manpage.
		Removing the -G command line option (SGI modes).
20050212	Updating the documentation (experimental devices: dev_cons
		and dev_mp, better hello.c, and some other things).
20050213	Some minor fixes: documentation, 80 columns in some source
		files, better configure script options.
		Adding some more PPC instructions.
		Added a NOFPU flag to the MIPS cpu flags, so that executing
		FPU instructions on for example VR4xxx will fail (as suggested
		by Alexander Yurchenko).
20050214	Implementing more PPC instructions.
		Adding dev_pmppc.
20050215	Continuing the work on PPC emulation. Adding a (mostly non-
		working) NetBSD/powerpc userland mode, a (buggy)
		show_trace_tree thing (simliar to the MIPS version).
20050216	Continuing...
20050218	Continuing the clean-up. (Merging the devices and devstate
		debugger commands, more 80-column cleanup, some documentation
		updates, ...).
20050219	Removing the -D, -A, and -a command line options. Updating the
		documentation, in preparation for the next release.
		Adding interrupt stuff to dev_cons.
		Single-stepping now looks/works better with bintrans enabled.
		Beginning the first phase of release testing; various minor
		updates to make everything build cleanly on Solaris.
20050220	Continuing testing for the release...
                
==============  RELEASE 0.3  ==============

20050221	Minor updates. Some more clean-up.
		Beginning on the new device registry stuff.
20050222	Continuing on the device stuff, and doing various other kinds
		of clean-up.
		Adding a dummy BeBox mode.
		Making the pc register common for all cpu families.
		Adding some more PPC instructions and fixing some bugs.
20050223	Continuing on the BeBox stuff, and adding more instructions.
		Adding an ns16550 to the VR4131 emulation (which is probably
		a close enough fake to the VR4131's SIU unit).
20050224	Minor updates. Adding dummy PReP, macppc, and DB64360 modes.
		Continuing on the device registry rewrite.
20050225	Continuing on the device stuff.
20050226	Continuing more on the device rewrite.
		Separating the "testmips" machine into testmips and baremips
		(and similarly with the ppc machine).
		Redesigning the device registry again :-)
		Adding a "device" command to the config file parser.
		Adding "device add" and "device remove" to the debugger.
		Removing pcidevs.h, because it was almost unused.
20050228	Correcting the Sprite disk image url in the documentation.
20050301	Adding an URISC cpu emulation mode (single-opcode machine).
20050303	Adding some files to the experiments directory (rssb_as.c,
		rssb_as.README, urisc_test.s).
		Continuing on the device stuff.
20050304	Minor documentation update. Also, the SPARC, PPC, and URISC
		modes are now enabled by default in the configure script.
		Some minor PPC updates (adding a VGA device to the bebox
		emulation mode).
20050305	Moving the static i386 bintrans runchunk code snippet (and the
		others) to be dynamically generated. (This allows the code to
		compile on i386 with old gcc.)
		Loading PPC64 ELFs now sets R2 to the TOC base.
		Changing the name of the emulator from mips64emul to GXemul.
		Splitting out the configuration file part of the documentation
		into its own file (configfiles.html).
20050306	Some really minor documentation updates.
		Adding a -D command line option (for "fully deterministic"
		behaviour).
20050308	Minor PPC updates. Adding a dummy OpenFirmware emulation layer.
20050309	Adding a hack for systems without inet_pton (such as Cygwin in
		Windows) as suggested by Soohyun Cho. (And updating the
		configure script too.)
		Adding a dummy HPPA cpu family.
		Some more OpenFirmware updates.
		Faster loading of badly aligned ELF regions.
20050311	Minor updates. Adding a dummy "NEC MobilePro 780" hpcmips
		machine mode; disabling direct bintrans access to framebuffers
		that are not 4K page aligned.
20050312	Adding an ugly KIU hack to the VR41xx device (which enables
		NetBSD/hpcmips MobilePro 780 keyboard input).
20050313	Adding a dummy "pcic" device (a pcmcia card controller).
		Adding a dummy Alpha cpu emulation mode.
		Fixing a strcmp length bug (thanks to Alexander Yurchenko for
		noticing the bug).
20050314	Some minor bintrans-related updates in preparation for a new
		bintrans subsystem: command line option -b now means "old
		bintrans", -B means "disable bintrans", and using no option at
		all selects "new bintrans".
		Better generation of MAC addresses when emulating multiple
		machines and/or NICs.
		Minor documentation updates (regarding configuration files).
20050315	Adding dummy standby, suspend, and hibernate MIPS opcodes.
		RTC interrupt hack for VR4121 (hpcmips).
		Enough of the pcic is now emulated to let NetBSD/hpcmips detect
		a PCMCIA harddisk controller card (but there is no support for
		ISA/PCMCIA interrupts yet).
		Adding preliminary instructions on how to install
		NetBSD/hpcmips.
		Continuing the attempt to get harddisks working with interrupts
		(pcic, wdc on hpcmips).
20050318	Minor updates. (Fixing disassembly of MIPS bgtz etc., 
		continuing on the device cleanup, ...)
20050319	Minor updates.
20050320	Minor updates.
20050322	Various minor updates.
20050323	Some more minor updates.
20050328	VR41xx-related updates (keyboard stuff: the space key and
		shifted and ctrled keys are now working in userland (ie
		NetBSD/hpcmips' ramdisk installer).
		Also adding simple cursor key support to the VR41xx kiu.
20050329	Some progress on the wdc.
		Updating the documentation of how to (possibly) install
		NetBSD/hpcmips, once it is working.
		Adding delays before wdc interrupts; this allows NetBSD
		2.0/hpcmips to be successfully installed!
		Mirroring physical addresses 0x8....... to 0x00000000 on
		hpcmips; this makes it possible to run X11 inside
		NetBSD/hpcmips :-)
		Updating the documentation regarding NetBSD/hpcmips.
		Fixing 16-bit vs 15-bit color in dev_fb.
20050330	Print a warning when the user attempts to load a gzipped
		file. (Thanks to Juan RP for making me aware of this "bug".)
20050331	Importing aic7xxx_reg.h from NetBSD.
		Adding a "-x" command line option, which forces xterms for
		each emulated serial port to always be opened.
		Adding a MobilePro 770 mode (same as 780, but different
		framebuffer address which allows bintrans = fast scrolling),
		and a MobilePro 800 (with 800x600 pixels framebuffer :-).
20050401	Minor updates.
20050402	Minor updates. (The standby and suspend instructions are
		bintransed as NOPs, and some minor documentation updates.)
20050403	Adding an Agenda VR3 mode, and playing around with a Linux
		kernel image, but not much success yet.
		Changing BIFB_D16_FFFF -> BIFB_D16_0000 for the hpcmips 
		framebuffers, causing NetBSD to boot with correct colors.
		New syntax for loading raw files: loadaddr:skiplen:
		initialpc:filename. (This is necessary to boot the Linux VR3
		kernels.)
		The Linux VR3 kernel boots in both serial console mode and
		using the framebuffer, but it panics relatively early.
20050404	Continuing on the AHC, and some other minor updates.
20050405	Adding a note in doc/experimental.html about "root1.2.6.cramfs"
		(thanks to Alec Voropay for noticing that it wasn't part
		of root1.2.6.kernel-8.00).
		Also adding a note about another cramfs image.
		-o options are now added to the command line passed to the
		Linux kernel, when emulating the VR3.
		Adding a MobilePro 880 mode, and a dummy IBM WorkPad Z50 mode.
20050406	Connecting the VR3 serial controller to irq 9 (Linux calls this
		irq 17), and some other interrupt-related cleanups.
		Reducing the memory overhead per bintranslated page. (Hopefully
		this makes things faster, or at least not slower...)
20050407	Some more cleanup regarding command line argument passing for
		the hpcmips modes.
		Playing with Linux kernels for MobilePro 770 and 800; they get
		as far as mounting a root filesystem, but then crash.
		Doing some testing for the next release.

==============  RELEASE 0.3.1  ==============


1 /*
2 * Copyright (C) 2004-2005 Anders Gavare. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *
28 * $Id: net.c,v 1.72 2005/03/14 19:14:05 debug Exp $
29 *
30 * Emulated (ethernet / internet) network support.
31 *
32 *
33 * NOTE: This is just an ugly hack, and just barely enough to get some
34 * Internet networking up and running for the guest OS.
35 *
36 * TODO: o) TCP: fin/ack stuff, and connection time-outs and
37 * connection refused (reset on connect?), resend
38 * data to the guest OS if no ack has arrived for
39 * some time (? buffers?)
40 * http://www.tcpipguide.com/free/
41 * t_TCPConnectionTermination-2.htm
42 * o) remove the netbsd-specific options in the tcp header (?)
43 * o) Outgoing UDP packet fragment support.
44 * o) IPv6 (outgoing, incoming, and the nameserver/gateway)
45 * o) Incoming connections
46 * o) if multiple NICs are connected to the same network,
47 * they should be able to see each other's packets, and
48 * they should have different MAC addresses!
49 *
50 *
51 * The emulated NIC has a MAC address of (for example) 10:20:30:40:50:60.
52 * From the emulated environment, the only other machine existing on the
53 * network is a "gateway" or "firewall", which has an address of
54 * 60:50:40:30:20:10. This module (net.c) contains the emulation of that
55 * gateway. It works like a NAT firewall, but emulated in userland software.
56 *
57 * The gateway uses IPv4 address 10.0.0.254, the guest OS (inside the
58 * emulator) could use any 10.x.x.x address, except 10.0.0.254. A suitable
59 * choice is, for example 10.0.0.1.
60 *
61 *
62 * NOTE: The 'extra' argument used in many functions in this file is a pointer
63 * to something unique for each controller, so that if multiple controllers
64 * are emulated concurrently, they will not get packets that aren't meant
65 * for some other controller.
66 *
67 *
68 * <------------------ a network ------------------------------>
69 * ^ ^ ^
70 * | | |
71 * a NIC connected another NIC the gateway
72 * to the network |
73 * v
74 * outside
75 * world
76 *
77 * The gateway isn't connected as a NIC, but is an "implicit" machine on the
78 * network.
79 *
80 * (See http://www.sinclair.org.au/keith/networking/vendor.html for a list
81 * of ethernet MAC assignments.)
82 */
83
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <unistd.h>
88 #include <errno.h>
89 #include <sys/types.h>
90 #include <sys/time.h>
91 #include <sys/socket.h>
92 #include <netinet/in.h>
93 #include <arpa/inet.h>
94 #include <netdb.h>
95 #include <fcntl.h>
96 #include <signal.h>
97
98 #include "machine.h"
99 #include "misc.h"
100 #include "net.h"
101
102
103 /* #define debug fatal */
104
105
106 #define ADDR_IPV4 1
107 #define ADDR_IPV6 2
108 #define ADDR_ETHERNET 3
109
110
111 /*
112 * net_debugaddr():
113 *
114 * Print an address using debug().
115 */
116 static void net_debugaddr(void *ipv4_addr, int type)
117 {
118 int i;
119 unsigned char *p = ipv4_addr;
120
121 switch (type) {
122 case ADDR_IPV4:
123 for (i=0; i<4; i++)
124 debug("%s%i", i? "." : "", p[i]);
125 break;
126 case ADDR_IPV6:
127 for (i=0; i<16; i+=2)
128 debug("%s%4x", i? ":" : "", p[i] * 256 + p[i+1]);
129 break;
130 case ADDR_ETHERNET:
131 for (i=0; i<6; i++)
132 debug("%s%02x", i? ":" : "", p[i]);
133 break;
134 default:
135 fatal("( net_debugaddr(): UNIMPLEMTED type %i )\n", type);
136 }
137 }
138
139
140 /*
141 * net_generate_unique_mac():
142 *
143 * Generate a "unique" serial number for a machine. The machine's serial
144 * number is combined with the machine's current number of NICs to form a
145 * more-or-less valid MAC address.
146 *
147 * The return value (6 bytes) are written to macbuf.
148 */
149 void net_generate_unique_mac(struct machine *machine, unsigned char *macbuf)
150 {
151 int x, y;
152
153 if (macbuf == NULL || machine == NULL) {
154 fatal("**\n** net_generate_unique_mac(): NULL ptr\n**\n");
155 return;
156 }
157
158 x = machine->serial_nr;
159 y = machine->nr_of_nics;
160
161 /*
162 * TODO: What is a good starting value? Right now, it looks like this:
163 *
164 * +-----------+-------------------+-------------+-------------+
165 * | 16 bits | 16 bits machine | 12 bits | 4 bits of |
166 * | fixed | serial nr | NIC nr(*) | zeroes |
167 * +-----------+-------------------+-------------+-------------+
168 *
169 * (*) = almost
170 */
171 macbuf[0] = 0x10;
172 macbuf[1] = 0x20;
173 macbuf[2] = x >> 8;
174 macbuf[3] = x & 255;
175 macbuf[4] = y / 15;
176 macbuf[5] = (y % 15) * 0x10 + 0x10;
177
178 /* TODO: Remember the mac addresses somewhere? */
179 machine->nr_of_nics ++;
180 }
181
182
183 /*
184 * net_ip_checksum():
185 *
186 * Fill in an IP header checksum. (This works for ICMP too.)
187 * chksumoffset should be 10 for IP headers, and len = 20.
188 * For ICMP packets, chksumoffset = 2 and len = length of the ICMP packet.
189 */
190 void net_ip_checksum(unsigned char *ip_header, int chksumoffset, int len)
191 {
192 int i;
193 uint32_t sum = 0;
194
195 for (i=0; i<len; i+=2)
196 if (i != chksumoffset) {
197 uint16_t w = (ip_header[i] << 8) + ip_header[i+1];
198 sum += w;
199 while (sum > 65535) {
200 int to_add = sum >> 16;
201 sum = (sum & 0xffff) + to_add;
202 }
203 }
204
205 sum ^= 0xffff;
206 ip_header[chksumoffset + 0] = sum >> 8;
207 ip_header[chksumoffset + 1] = sum & 0xff;
208 }
209
210
211 /*
212 * net_ip_tcp_checksum():
213 *
214 * Fill in a TCP header checksum. This differs slightly from the IP
215 * checksum. The checksum is calculated on a pseudo header, the actual
216 * TCP header, and the data. This is what the pseudo header looks like:
217 *
218 * uint32_t srcaddr;
219 * uint32_t dstaddr;
220 * uint16_t protocol; (= 6 for tcp)
221 * uint16_t tcp_len;
222 *
223 * tcp_len is length of header PLUS data. The psedo header is created
224 * internally here, and does not need to be supplied by the caller.
225 */
226 static void net_ip_tcp_checksum(unsigned char *tcp_header, int chksumoffset,
227 int tcp_len, unsigned char *srcaddr, unsigned char *dstaddr,
228 int udpflag)
229 {
230 int i, pad = 0;
231 unsigned char pseudoh[12];
232 uint32_t sum = 0;
233
234 memcpy(pseudoh + 0, srcaddr, 4);
235 memcpy(pseudoh + 4, dstaddr, 4);
236 pseudoh[8] = 0x00;
237 pseudoh[9] = udpflag? 17 : 6;
238 pseudoh[10] = tcp_len >> 8;
239 pseudoh[11] = tcp_len & 255;
240
241 for (i=0; i<12; i+=2) {
242 uint16_t w = (pseudoh[i] << 8) + pseudoh[i+1];
243 sum += w;
244 while (sum > 65535) {
245 int to_add = sum >> 16;
246 sum = (sum & 0xffff) + to_add;
247 }
248 }
249
250 if (tcp_len & 1) {
251 tcp_len ++;
252 pad = 1;
253 }
254
255 for (i=0; i<tcp_len; i+=2)
256 if (i != chksumoffset) {
257 uint16_t w;
258 if (!pad || i < tcp_len-2)
259 w = (tcp_header[i] << 8) + tcp_header[i+1];
260 else
261 w = (tcp_header[i] << 8) + 0x00;
262 sum += w;
263 while (sum > 65535) {
264 int to_add = sum >> 16;
265 sum = (sum & 0xffff) + to_add;
266 }
267 }
268
269 sum ^= 0xffff;
270 tcp_header[chksumoffset + 0] = sum >> 8;
271 tcp_header[chksumoffset + 1] = sum & 0xff;
272 }
273
274
275 /*
276 * net_allocate_packet_link():
277 *
278 * This routine allocates an ethernet_packet_link struct, and adds it at
279 * the end of the packet chain. A data buffer is allocated (and zeroed),
280 * and the data, extra, and len fields of the link are set.
281 *
282 * Return value is a pointer to the link on success. It doesn't return on
283 * failure.
284 */
285 static struct ethernet_packet_link *net_allocate_packet_link(
286 struct net *net, void *extra, int len)
287 {
288 struct ethernet_packet_link *lp;
289
290 lp = malloc(sizeof(struct ethernet_packet_link));
291 if (lp == NULL) {
292 fprintf(stderr, "net_allocate_packet_link(): out of memory\n");
293 exit(1);
294 }
295
296 /* memset(lp, 0, sizeof(struct ethernet_packet_link)); */
297
298 lp->len = len;
299 lp->extra = extra;
300 lp->data = malloc(len);
301 if (lp->data == NULL) {
302 fprintf(stderr, "net_allocate_packet_link(): out of memory\n");
303 exit(1);
304 }
305 lp->next = NULL;
306
307 /* TODO: maybe this is not necessary: */
308 memset(lp->data, 0, len);
309
310 /* Add last in the link chain: */
311 lp->prev = net->last_ethernet_packet;
312 if (lp->prev != NULL)
313 lp->prev->next = lp;
314 else
315 net->first_ethernet_packet = lp;
316 net->last_ethernet_packet = lp;
317
318 return lp;
319 }
320
321
322 /*
323 * net_ip_icmp():
324 *
325 * Handle an ICMP packet.
326 *
327 * The IP header (at offset 14) could look something like
328 *
329 * ver=45 tos=00 len=0054 id=001a ofs=0000 ttl=ff p=01 sum=a87e
330 * src=0a000005 dst=03050607
331 *
332 * and the ICMP specific data (beginning at offset 34):
333 *
334 * type=08 code=00 chksum=b8bf
335 * 000c0008d5cee94089190c0008090a0b
336 * 0c0d0e0f101112131415161718191a1b
337 * 1c1d1e1f202122232425262728292a2b
338 * 2c2d2e2f3031323334353637
339 */
340 static void net_ip_icmp(struct net *net, void *extra,
341 unsigned char *packet, int len)
342 {
343 int type;
344 struct ethernet_packet_link *lp;
345
346 type = packet[34];
347
348 switch (type) {
349 case 8: /* ECHO request */
350 debug("[ ICMP echo ]\n");
351 lp = net_allocate_packet_link(net, extra, len);
352
353 /* Copy the old packet first: */
354 memcpy(lp->data, packet, len);
355
356 /* Switch to and from ethernet addresses: */
357 memcpy(lp->data + 0, packet + 6, 6);
358 memcpy(lp->data + 6, packet + 0, 6);
359
360 /* Switch to and from IP addresses: */
361 memcpy(lp->data + 26, packet + 30, 4);
362 memcpy(lp->data + 30, packet + 26, 4);
363
364 /* Change from echo REQUEST to echo REPLY: */
365 lp->data[34] = 0x00;
366
367 /* Decrease the TTL to a low value: */
368 lp->data[22] = 2;
369
370 /* Recalculate ICMP checksum: */
371 net_ip_checksum(lp->data + 34, 2, len - 34);
372
373 /* Recalculate IP header checksum: */
374 net_ip_checksum(lp->data + 14, 10, 20);
375
376 break;
377 default:
378 fatal("[ net: ICMP type %i not yet implemented ]\n", type);
379 }
380 }
381
382
383 /*
384 * tcp_closeconnection():
385 *
386 * Helper function which closes down a TCP connection completely.
387 */
388 static void tcp_closeconnection(struct net *net, int con_id)
389 {
390 close(net->tcp_connections[con_id].socket);
391 net->tcp_connections[con_id].state = TCP_OUTSIDE_DISCONNECTED;
392 net->tcp_connections[con_id].in_use = 0;
393 net->tcp_connections[con_id].incoming_buf_len = 0;
394 }
395
396
397 /*
398 * net_ip_tcp_connectionreply():
399 *
400 * When changing from state _TRYINGTOCONNECT to _CONNECTED, then this
401 * function should be called with connecting set to 1.
402 *
403 * To send a generic ack reply, set connecting to 0.
404 *
405 * To send data (PSH), set data to non-NULL and datalen to the length.
406 *
407 * This creates an ethernet packet for the guest OS with an ACK to the
408 * initial SYN packet.
409 */
410 static void net_ip_tcp_connectionreply(struct net *net, void *extra,
411 int con_id, int connecting, unsigned char *data, int datalen, int rst)
412 {
413 struct ethernet_packet_link *lp;
414 int tcp_length, ip_len, option_len = 20;
415
416 if (connecting)
417 net->tcp_connections[con_id].outside_acknr =
418 net->tcp_connections[con_id].inside_seqnr + 1;
419
420 net->tcp_connections[con_id].tcp_id ++;
421 tcp_length = 20 + option_len + datalen;
422 ip_len = 20 + tcp_length;
423 lp = net_allocate_packet_link(net, extra, 14 + ip_len);
424
425 /* Ethernet header: */
426 memcpy(lp->data + 0, net->tcp_connections[con_id].ethernet_address, 6);
427 memcpy(lp->data + 6, net->gateway_ethernet_addr, 6);
428 lp->data[12] = 0x08; /* IP = 0x0800 */
429 lp->data[13] = 0x00;
430
431 /* IP header: */
432 lp->data[14] = 0x45; /* ver */
433 lp->data[15] = 0x10; /* tos */
434 lp->data[16] = ip_len >> 8;
435 lp->data[17] = ip_len & 0xff;
436 lp->data[18] = net->tcp_connections[con_id].tcp_id >> 8;
437 lp->data[19] = net->tcp_connections[con_id].tcp_id & 0xff;
438 lp->data[20] = 0x40; /* don't fragment */
439 lp->data[21] = 0x00;
440 lp->data[22] = 0x40; /* ttl */
441 lp->data[23] = 6; /* p = TCP */
442 memcpy(lp->data + 26, net->tcp_connections[con_id].
443 outside_ip_address, 4);
444 memcpy(lp->data + 30, net->tcp_connections[con_id].
445 inside_ip_address, 4);
446 net_ip_checksum(lp->data + 14, 10, 20);
447
448 /* TCP header and options at offset 34: */
449 lp->data[34] = net->tcp_connections[con_id].outside_tcp_port >> 8;
450 lp->data[35] = net->tcp_connections[con_id].outside_tcp_port & 0xff;
451 lp->data[36] = net->tcp_connections[con_id].inside_tcp_port >> 8;
452 lp->data[37] = net->tcp_connections[con_id].inside_tcp_port & 0xff;
453 lp->data[38] = (net->tcp_connections[con_id].
454 outside_seqnr >> 24) & 0xff;
455 lp->data[39] = (net->tcp_connections[con_id].
456 outside_seqnr >> 16) & 0xff;
457 lp->data[40] = (net->tcp_connections[con_id].
458 outside_seqnr >> 8) & 0xff;
459 lp->data[41] = net->tcp_connections[con_id].
460 outside_seqnr & 0xff;
461 lp->data[42] = (net->tcp_connections[con_id].
462 outside_acknr >> 24) & 0xff;
463 lp->data[43] = (net->tcp_connections[con_id].
464 outside_acknr >> 16) & 0xff;
465 lp->data[44] = (net->tcp_connections[con_id].
466 outside_acknr >> 8) & 0xff;
467 lp->data[45] = net->tcp_connections[con_id].outside_acknr & 0xff;
468
469 /* Control */
470 lp->data[46] = (option_len + 20) / 4 * 0x10;
471 lp->data[47] = 0x10; /* ACK */
472 if (connecting)
473 lp->data[47] |= 0x02; /* SYN */
474 if (net->tcp_connections[con_id].state == TCP_OUTSIDE_CONNECTED)
475 lp->data[47] |= 0x08; /* PSH */
476 if (rst)
477 lp->data[47] |= 0x04; /* RST */
478 if (net->tcp_connections[con_id].state >= TCP_OUTSIDE_DISCONNECTED)
479 lp->data[47] |= 0x01; /* FIN */
480
481 /* Window */
482 lp->data[48] = 0x10;
483 lp->data[49] = 0x00;
484
485 /* no urgent ptr */
486
487 /* options */
488 /* TODO: HAHA, this is ugly */
489 lp->data[54] = 0x02;
490 lp->data[55] = 0x04;
491 lp->data[56] = 0x05;
492 lp->data[57] = 0xb4;
493 lp->data[58] = 0x01;
494 lp->data[59] = 0x03;
495 lp->data[60] = 0x03;
496 lp->data[61] = 0x00;
497 lp->data[62] = 0x01;
498 lp->data[63] = 0x01;
499 lp->data[64] = 0x08;
500 lp->data[65] = 0x0a;
501 lp->data[66] = (net->timestamp >> 24) & 0xff;
502 lp->data[67] = (net->timestamp >> 16) & 0xff;
503 lp->data[68] = (net->timestamp >> 8) & 0xff;
504 lp->data[69] = net->timestamp & 0xff;
505 lp->data[70] = (net->tcp_connections[con_id].
506 inside_timestamp >> 24) & 0xff;
507 lp->data[71] = (net->tcp_connections[con_id].
508 inside_timestamp >> 16) & 0xff;
509 lp->data[72] = (net->tcp_connections[con_id].
510 inside_timestamp >> 8) & 0xff;
511 lp->data[73] = net->tcp_connections[con_id].
512 inside_timestamp & 0xff;
513
514 /* data: */
515 if (data != NULL) {
516 memcpy(lp->data + 74, data, datalen);
517 net->tcp_connections[con_id].outside_seqnr += datalen;
518 }
519
520 /* Checksum: */
521 net_ip_tcp_checksum(lp->data + 34, 16, tcp_length,
522 lp->data + 26, lp->data + 30, 0);
523
524 #if 0
525 {
526 int i;
527 fatal("[ net_ip_tcp_connectionreply(%i): ", connecting);
528 for (i=0; i<ip_len+14; i++)
529 fatal("%02x", lp->data[i]);
530 fatal(" ]\n");
531 }
532 #endif
533
534 if (connecting)
535 net->tcp_connections[con_id].outside_seqnr ++;
536 }
537
538
539 /*
540 * net_ip_tcp():
541 *
542 * Handle a TCP packet comming from the emulated OS.
543 *
544 * The IP header (at offset 14) could look something like
545 *
546 * ver=45 tos=00 len=003c id=0006 ofs=0000 ttl=40 p=11 sum=b798
547 * src=0a000001 dst=c1abcdef
548 *
549 * TCP header, at offset 34:
550 *
551 * srcport=fffe dstport=0015 seqnr=af419a1d acknr=00000000
552 * control=a002 window=4000 checksum=fe58 urgent=0000
553 * and then "options and padding" and then data.
554 * (020405b4010303000101080a0000000000000000)
555 *
556 * See the following URLs for good descriptions of TCP:
557 *
558 * http://www.networksorcery.com/enp/protocol/tcp.htm
559 * http://www.tcpipguide.com/free/t_TCPIPTransmissionControlProtocolTCP.htm
560 */
561 static void net_ip_tcp(struct net *net, void *extra,
562 unsigned char *packet, int len)
563 {
564 int con_id, free_con_id, i, res;
565 int srcport, dstport, data_offset, window, checksum, urgptr;
566 int syn, ack, psh, rst, urg, fin;
567 uint32_t seqnr, acknr;
568 struct sockaddr_in remote_ip;
569 fd_set rfds;
570 struct timeval tv;
571 int send_ofs;
572
573 #if 0
574 fatal("[ net: TCP: ");
575 for (i=0; i<26; i++)
576 fatal("%02x", packet[i]);
577 fatal(" ");
578 #endif
579
580 srcport = (packet[34] << 8) + packet[35];
581 dstport = (packet[36] << 8) + packet[37];
582
583 seqnr = (packet[38] << 24) + (packet[39] << 16)
584 + (packet[40] << 8) + packet[41];
585 acknr = (packet[42] << 24) + (packet[43] << 16)
586 + (packet[44] << 8) + packet[45];
587
588 #if 0
589 fatal("%i.%i.%i.%i:%i -> %i.%i.%i.%i:%i, seqnr=%lli acknr=%lli ",
590 packet[26], packet[27], packet[28], packet[29], srcport,
591 packet[30], packet[31], packet[32], packet[33], dstport,
592 (long long)seqnr, (long long)acknr);
593 #endif
594
595 data_offset = (packet[46] >> 4) * 4 + 34;
596 /* data_offset is now data offset within packet :-) */
597
598 urg = packet[47] & 32;
599 ack = packet[47] & 16;
600 psh = packet[47] & 8;
601 rst = packet[47] & 4;
602 syn = packet[47] & 2;
603 fin = packet[47] & 1;
604 window = (packet[48] << 8) + packet[49];
605 checksum = (packet[50] << 8) + packet[51];
606 urgptr = (packet[52] << 8) + packet[53];
607
608 #if 0
609 fatal(urg? "URG " : "");
610 fatal(ack? "ACK " : "");
611 fatal(psh? "PSH " : "");
612 fatal(rst? "RST " : "");
613 fatal(syn? "SYN " : "");
614 fatal(fin? "FIN " : "");
615
616 fatal("window=0x%04x checksum=0x%04x urgptr=0x%04x ",
617 window, checksum, urgptr);
618
619 fatal("options=");
620 for (i=34+20; i<data_offset; i++)
621 fatal("%02x", packet[i]);
622
623 fatal(" data=");
624 for (i=data_offset; i<len; i++)
625 fatal("%02x", packet[i]);
626
627 fatal(" ]\n");
628 #endif
629
630 net_ip_tcp_checksum(packet + 34, 16, len - 34,
631 packet + 26, packet + 30, 0);
632 if (packet[50] * 256 + packet[51] != checksum) {
633 debug("TCP: dropping packet because of checksum mismatch "
634 "(0x%04x != 0x%04x)\n", packet[50] * 256 + packet[51],
635 checksum);
636
637 return;
638 }
639
640 /* Does this packet belong to a current connection? */
641 con_id = free_con_id = -1;
642 for (i=0; i<MAX_TCP_CONNECTIONS; i++) {
643 if (!net->tcp_connections[i].in_use)
644 free_con_id = i;
645 if (net->tcp_connections[i].in_use &&
646 net->tcp_connections[i].inside_tcp_port == srcport &&
647 net->tcp_connections[i].outside_tcp_port == dstport &&
648 memcmp(net->tcp_connections[i].inside_ip_address,
649 packet + 26, 4) == 0 &&
650 memcmp(net->tcp_connections[i].outside_ip_address,
651 packet + 30, 4) == 0) {
652 con_id = i;
653 break;
654 }
655 }
656
657 /*
658 * Unknown connection, and not SYN? Then drop the packet.
659 * TODO: Send back RST?
660 */
661 if (con_id < 0 && !syn) {
662 debug("[ net: TCP: dropping packet from unknown connection,"
663 " %i.%i.%i.%i:%i -> %i.%i.%i.%i:%i %s%s%s%s%s]\n",
664 packet[26], packet[27], packet[28], packet[29], srcport,
665 packet[30], packet[31], packet[32], packet[33], dstport,
666 fin? "FIN ": "", syn? "SYN ": "", ack? "ACK ": "",
667 psh? "PSH ": "", rst? "RST ": "");
668 return;
669 }
670
671 /* Known connection, and SYN? Then ignore the packet. */
672 if (con_id >= 0 && syn) {
673 debug("[ net: TCP: ignoring redundant SYN packet from known"
674 " connection, %i.%i.%i.%i:%i -> %i.%i.%i.%i:%i ]\n",
675 packet[26], packet[27], packet[28], packet[29], srcport,
676 packet[30], packet[31], packet[32], packet[33], dstport);
677 return;
678 }
679
680 /*
681 * A new outgoing connection?
682 */
683 if (con_id < 0 && syn) {
684 debug("[ net: TCP: new outgoing connection, %i.%i.%i.%i:%i"
685 " -> %i.%i.%i.%i:%i ]\n",
686 packet[26], packet[27], packet[28], packet[29], srcport,
687 packet[30], packet[31], packet[32], packet[33], dstport);
688
689 /* Find a free connection id to use: */
690 if (free_con_id < 0) {
691 #if 1
692 /*
693 * TODO: Reuse the oldest one currently in use, or
694 * just drop the new connection attempt? Drop for now.
695 */
696 fatal("[ TOO MANY TCP CONNECTIONS IN USE! "
697 "Increase MAX_TCP_CONNECTIONS! ]\n");
698 return;
699 #else
700 int i;
701 int64_t oldest = net->
702 tcp_connections[0].last_used_timestamp;
703 free_con_id = 0;
704
705 fatal("[ NO FREE TCP SLOTS, REUSING OLDEST ONE ]\n");
706 for (i=0; i<MAX_TCP_CONNECTIONS; i++)
707 if (net->tcp_connections[i].
708 last_used_timestamp < oldest) {
709 oldest = net->tcp_connections[i].
710 last_used_timestamp;
711 free_con_id = i;
712 }
713 tcp_closeconnection(net, free_con_id);
714 #endif
715 }
716
717 con_id = free_con_id;
718 memset(&net->tcp_connections[con_id], 0,
719 sizeof(struct tcp_connection));
720
721 memcpy(net->tcp_connections[con_id].ethernet_address,
722 packet + 6, 6);
723 memcpy(net->tcp_connections[con_id].inside_ip_address,
724 packet + 26, 4);
725 net->tcp_connections[con_id].inside_tcp_port = srcport;
726 memcpy(net->tcp_connections[con_id].outside_ip_address,
727 packet + 30, 4);
728 net->tcp_connections[con_id].outside_tcp_port = dstport;
729
730 net->tcp_connections[con_id].socket =
731 socket(AF_INET, SOCK_STREAM, 0);
732 if (net->tcp_connections[con_id].socket < 0) {
733 fatal("[ net: TCP: socket() returned %i ]\n",
734 net->tcp_connections[con_id].socket);
735 return;
736 }
737
738 debug("[ new tcp outgoing socket=%i ]\n",
739 net->tcp_connections[con_id].socket);
740
741 net->tcp_connections[con_id].in_use = 1;
742
743 /* Set the socket to non-blocking: */
744 res = fcntl(net->tcp_connections[con_id].socket, F_GETFL);
745 fcntl(net->tcp_connections[con_id].socket, F_SETFL,
746 res | O_NONBLOCK);
747
748 remote_ip.sin_family = AF_INET;
749 memcpy((unsigned char *)&remote_ip.sin_addr,
750 net->tcp_connections[con_id].outside_ip_address, 4);
751 remote_ip.sin_port = htons(
752 net->tcp_connections[con_id].outside_tcp_port);
753
754 res = connect(net->tcp_connections[con_id].socket,
755 (struct sockaddr *)&remote_ip, sizeof(remote_ip));
756
757 /* connect can return -1, and errno = EINPROGRESS
758 as we might not have connected right away. */
759
760 net->tcp_connections[con_id].state =
761 TCP_OUTSIDE_TRYINGTOCONNECT;
762
763 net->tcp_connections[con_id].outside_acknr = 0;
764 net->tcp_connections[con_id].outside_seqnr =
765 ((random() & 0xffff) << 16) + (random() & 0xffff);
766 }
767
768 if (rst) {
769 debug("[ 'rst': disconnecting TCP connection %i ]\n", con_id);
770 net_ip_tcp_connectionreply(net, extra, con_id, 0, NULL, 0, 1);
771 tcp_closeconnection(net, con_id);
772 return;
773 }
774
775 if (ack && net->tcp_connections[con_id].state
776 == TCP_OUTSIDE_DISCONNECTED2) {
777 debug("[ 'ack': guestOS's final termination of TCP "
778 "connection %i ]\n", con_id);
779
780 /* Send an RST? (TODO, this is wrong...) */
781 net_ip_tcp_connectionreply(net, extra, con_id, 0, NULL, 0, 1);
782
783 /* ... and forget about this connection: */
784 tcp_closeconnection(net, con_id);
785 return;
786 }
787
788 if (fin && net->tcp_connections[con_id].state
789 == TCP_OUTSIDE_DISCONNECTED) {
790 debug("[ 'fin': response to outside's disconnection of "
791 "TCP connection %i ]\n", con_id);
792
793 /* Send an ACK: */
794 net->tcp_connections[con_id].state = TCP_OUTSIDE_CONNECTED;
795 net_ip_tcp_connectionreply(net, extra, con_id, 0, NULL, 0, 0);
796 net->tcp_connections[con_id].state = TCP_OUTSIDE_DISCONNECTED2;
797 return;
798 }
799
800 if (fin) {
801 debug("[ 'fin': guestOS disconnecting TCP connection %i ]\n",
802 con_id);
803
804 /* Send ACK: */
805 net_ip_tcp_connectionreply(net, extra, con_id, 0, NULL, 0, 0);
806 net->tcp_connections[con_id].state = TCP_OUTSIDE_DISCONNECTED2;
807
808 /* Return and send FIN: */
809 goto ret;
810 }
811
812 if (ack) {
813 debug("ACK %i bytes, inside_acknr=%u outside_seqnr=%u\n",
814 net->tcp_connections[con_id].incoming_buf_len,
815 net->tcp_connections[con_id].inside_acknr,
816 net->tcp_connections[con_id].outside_seqnr);
817 net->tcp_connections[con_id].inside_acknr = acknr;
818 if (net->tcp_connections[con_id].inside_acknr ==
819 net->tcp_connections[con_id].outside_seqnr &&
820 net->tcp_connections[con_id].incoming_buf_len != 0) {
821 debug(" all acked\n");
822 net->tcp_connections[con_id].incoming_buf_len = 0;
823 }
824 }
825
826 net->tcp_connections[con_id].inside_seqnr = seqnr;
827
828 /* TODO: This is hardcoded for a specific NetBSD packet: */
829 if (packet[34 + 30] == 0x08 && packet[34 + 31] == 0x0a)
830 net->tcp_connections[con_id].inside_timestamp =
831 (packet[34 + 32 + 0] << 24) +
832 (packet[34 + 32 + 1] << 16) +
833 (packet[34 + 32 + 2] << 8) +
834 (packet[34 + 32 + 3] << 0);
835
836
837 net->timestamp ++;
838 net->tcp_connections[con_id].last_used_timestamp = net->timestamp;
839
840
841 if (net->tcp_connections[con_id].state != TCP_OUTSIDE_CONNECTED) {
842 debug("[ not connected to outside ]\n");
843 return;
844 }
845
846
847 if (data_offset >= len)
848 return;
849
850
851 /*
852 * We are here if this is a known connection, and data is to be
853 * transmitted to the outside world.
854 */
855
856 send_ofs = data_offset;
857 send_ofs += ((int32_t)net->tcp_connections[con_id].outside_acknr
858 - (int32_t)seqnr);
859 #if 1
860 debug("[ %i bytes of tcp data to be sent, beginning at seqnr %u, ",
861 len - data_offset, seqnr);
862 debug("outside is at acknr %u ==> %i actual bytes to be sent ]\n",
863 net->tcp_connections[con_id].outside_acknr, len - send_ofs);
864 #endif
865
866 /* Drop outgoing packet if the guest OS' seqnr is not
867 the same as we have acked. (We have missed something, perhaps.) */
868 if (seqnr != net->tcp_connections[con_id].outside_acknr) {
869 debug("!! outgoing TCP packet dropped (seqnr = %u, "
870 "outside_acknr = %u)\n", seqnr,
871 net->tcp_connections[con_id].outside_acknr);
872 goto ret;
873 }
874
875 if (len - send_ofs > 0) {
876 /* Is the socket available for output? */
877 FD_ZERO(&rfds); /* write */
878 FD_SET(net->tcp_connections[con_id].socket, &rfds);
879 tv.tv_sec = tv.tv_usec = 0;
880 errno = 0;
881 res = select(net->tcp_connections[con_id].socket+1,
882 NULL, &rfds, NULL, &tv);
883 if (res < 1) {
884 net->tcp_connections[con_id].state =
885 TCP_OUTSIDE_DISCONNECTED;
886 debug("[ TCP: disconnect on select for writing ]\n");
887 goto ret;
888 }
889
890 res = write(net->tcp_connections[con_id].socket,
891 packet + send_ofs, len - send_ofs);
892
893 if (res > 0) {
894 net->tcp_connections[con_id].outside_acknr += res;
895 } else if (errno == EAGAIN) {
896 /* Just ignore this attempt. */
897 return;
898 } else {
899 debug("[ error writing %i bytes to TCP connection %i:"
900 " errno = %i ]\n", len - send_ofs, con_id, errno);
901 net->tcp_connections[con_id].state =
902 TCP_OUTSIDE_DISCONNECTED;
903 debug("[ TCP: disconnect on write() ]\n");
904 goto ret;
905 }
906 }
907
908 ret:
909 /* Send an ACK (or FIN) to the guest OS: */
910 net_ip_tcp_connectionreply(net, extra, con_id, 0, NULL, 0, 0);
911 }
912
913
914 /*
915 * net_ip_udp():
916 *
917 * Handle a UDP packet.
918 *
919 * (See http://www.networksorcery.com/enp/protocol/udp.htm.)
920 *
921 * The IP header (at offset 14) could look something like
922 *
923 * ver=45 tos=00 len=003c id=0006 ofs=0000 ttl=40 p=11 sum=b798
924 * src=0a000001 dst=c1abcdef
925 *
926 * and the UDP data (beginning at offset 34):
927 *
928 * srcport=fffc dstport=0035 length=0028 chksum=76b6
929 * 43e20100000100000000000003667470066e6574627364036f726700001c0001
930 */
931 static void net_ip_udp(struct net *net, void *extra,
932 unsigned char *packet, int len)
933 {
934 int con_id, free_con_id, i, srcport, dstport, udp_len;
935 ssize_t res;
936 struct sockaddr_in remote_ip;
937
938 if ((packet[20] & 0x3f) != 0) {
939 fatal("[ net_ip_udp(): WARNING! fragmented UDP "
940 "packet, TODO ]\n");
941 return;
942 }
943
944 srcport = (packet[34] << 8) + packet[35];
945 dstport = (packet[36] << 8) + packet[37];
946 udp_len = (packet[38] << 8) + packet[39];
947 /* chksum at offset 40 and 41 */
948
949 debug("[ net: UDP: ");
950 debug("srcport=%i dstport=%i len=%i ", srcport, dstport, udp_len);
951 for (i=42; i<len; i++) {
952 if (packet[i] >= ' ' && packet[i] < 127)
953 debug("%c", packet[i]);
954 else
955 debug("[%02x]", packet[i]);
956 }
957 debug(" ]\n");
958
959 /* Is this "connection" new, or a currently ongoing one? */
960 con_id = free_con_id = -1;
961 for (i=0; i<MAX_UDP_CONNECTIONS; i++) {
962 if (!net->udp_connections[i].in_use)
963 free_con_id = i;
964 if (net->udp_connections[i].in_use &&
965 net->udp_connections[i].inside_udp_port == srcport &&
966 net->udp_connections[i].outside_udp_port == dstport &&
967 memcmp(net->udp_connections[i].inside_ip_address,
968 packet + 26, 4) == 0 &&
969 memcmp(net->udp_connections[i].outside_ip_address,
970 packet + 30, 4) == 0) {
971 con_id = i;
972 break;
973 }
974 }
975
976 debug("&& UDP connection is ");
977 if (con_id >= 0)
978 debug("ONGOING");
979 else {
980 debug("NEW");
981 if (free_con_id < 0) {
982 int i;
983 int64_t oldest = net->
984 udp_connections[0].last_used_timestamp;
985 free_con_id = 0;
986
987 debug(", NO FREE SLOTS, REUSING OLDEST ONE");
988 for (i=0; i<MAX_UDP_CONNECTIONS; i++)
989 if (net->udp_connections[i].
990 last_used_timestamp < oldest) {
991 oldest = net->udp_connections[i].
992 last_used_timestamp;
993 free_con_id = i;
994 }
995 close(net->udp_connections[free_con_id].socket);
996 }
997 con_id = free_con_id;
998 memset(&net->udp_connections[con_id], 0,
999 sizeof(struct udp_connection));
1000
1001 memcpy(net->udp_connections[con_id].ethernet_address,
1002 packet + 6, 6);
1003 memcpy(net->udp_connections[con_id].inside_ip_address,
1004 packet + 26, 4);
1005 net->udp_connections[con_id].inside_udp_port = srcport;
1006 memcpy(net->udp_connections[con_id].outside_ip_address,
1007 packet + 30, 4);
1008 net->udp_connections[con_id].outside_udp_port = dstport;
1009
1010 net->udp_connections[con_id].socket = socket(AF_INET,
1011 SOCK_DGRAM, 0);
1012 if (net->udp_connections[con_id].socket < 0) {
1013 fatal("[ net: UDP: socket() returned %i ]\n",
1014 net->udp_connections[con_id].socket);
1015 return;
1016 }
1017
1018 debug(" {socket=%i}", net->udp_connections[con_id].socket);
1019
1020 net->udp_connections[con_id].in_use = 1;
1021
1022 /* Set the socket to non-blocking: */
1023 res = fcntl(net->udp_connections[con_id].socket, F_GETFL);
1024 fcntl(net->udp_connections[con_id].socket, F_SETFL,
1025 res | O_NONBLOCK);
1026 }
1027
1028 debug(", connection id %i\n", con_id);
1029
1030 net->timestamp ++;
1031 net->udp_connections[con_id].last_used_timestamp = net->timestamp;
1032
1033 remote_ip.sin_family = AF_INET;
1034 memcpy((unsigned char *)&remote_ip.sin_addr,
1035 net->udp_connections[con_id].outside_ip_address, 4);
1036
1037 /*
1038 * Special case for the nameserver: If a UDP packet is sent to
1039 * the gateway, it will be forwarded to the nameserver, if it is
1040 * known.
1041 */
1042 if (net->nameserver_known &&
1043 memcmp(net->udp_connections[con_id].outside_ip_address,
1044 &net->gateway_ipv4_addr[0], 4) == 0) {
1045 memcpy((unsigned char *)&remote_ip.sin_addr,
1046 &net->nameserver_ipv4, 4);
1047 net->udp_connections[con_id].fake_ns = 1;
1048 }
1049
1050 remote_ip.sin_port = htons(
1051 net->udp_connections[con_id].outside_udp_port);
1052
1053 res = sendto(net->udp_connections[con_id].socket, packet + 42,
1054 len - 42, 0, (const struct sockaddr *)&remote_ip,
1055 sizeof(remote_ip));
1056
1057 if (res != len-42)
1058 debug("[ net: UDP: unable to send %i bytes ]\n", len-42);
1059 else
1060 debug("[ net: UDP: OK!!! ]\n");
1061 }
1062
1063
1064 /*
1065 * net_ip():
1066 *
1067 * Handle an IP packet, coming from the emulated NIC.
1068 */
1069 static void net_ip(struct net *net, void *extra,
1070 unsigned char *packet, int len)
1071 {
1072 #if 1
1073 int i;
1074
1075 debug("[ net: IP: ");
1076 debug("ver=%02x ", packet[14]);
1077 debug("tos=%02x ", packet[15]);
1078 debug("len=%02x%02x ", packet[16], packet[17]);
1079 debug("id=%02x%02x ", packet[18], packet[19]);
1080 debug("ofs=%02x%02x ", packet[20], packet[21]);
1081 debug("ttl=%02x ", packet[22]);
1082 debug("p=%02x ", packet[23]);
1083 debug("sum=%02x%02x ", packet[24], packet[25]);
1084 debug("src=%02x%02x%02x%02x ",
1085 packet[26], packet[27], packet[28], packet[29]);
1086 debug("dst=%02x%02x%02x%02x ",
1087 packet[30], packet[31], packet[32], packet[33]);
1088 for (i=34; i<len; i++)
1089 debug("%02x", packet[i]);
1090 debug(" ]\n");
1091 #endif
1092
1093 /* Cut off overflowing tail data: */
1094 if (len > 14 + packet[16]*256 + packet[17])
1095 len = 14 + packet[16]*256 + packet[17];
1096
1097 if (packet[14] == 0x45) {
1098 /* IPv4: */
1099 switch (packet[23]) {
1100 case 1: /* ICMP */
1101 net_ip_icmp(net, extra, packet, len);
1102 break;
1103 case 6: /* TCP */
1104 net_ip_tcp(net, extra, packet, len);
1105 break;
1106 case 17:/* UDP */
1107 net_ip_udp(net, extra, packet, len);
1108 break;
1109 default:
1110 fatal("[ net: IP: UNIMPLEMENTED protocol %i ]\n",
1111 packet[23]);
1112 }
1113 } else
1114 fatal("[ net: IP: UNIMPLEMENTED ip, first byte = 0x%02x ]\n",
1115 packet[14]);
1116 }
1117
1118
1119 /*
1120 * net_ip_broadcast_dhcp():
1121 *
1122 * Handle an IPv4 DHCP broadcast packet, coming from the emulated NIC.
1123 *
1124 * Read http://www.ietf.org/rfc/rfc2131.txt for details on DHCP.
1125 * (And http://users.telenet.be/mydotcom/library/network/dhcp.htm.)
1126 */
1127 static void net_ip_broadcast_dhcp(struct net *net, void *extra,
1128 unsigned char *packet, int len)
1129 {
1130 /*
1131 * TODO
1132 */
1133 #if 0
1134 struct ethernet_packet_link *lp;
1135 int i;
1136
1137 fatal("[ net: IPv4 DHCP: ");
1138 #if 0
1139 fatal("ver=%02x ", packet[14]);
1140 fatal("tos=%02x ", packet[15]);
1141 fatal("len=%02x%02x ", packet[16], packet[17]);
1142 fatal("id=%02x%02x ", packet[18], packet[19]);
1143 fatal("ofs=%02x%02x ", packet[20], packet[21]);
1144 fatal("ttl=%02x ", packet[22]);
1145 fatal("p=%02x ", packet[23]);
1146 fatal("sum=%02x%02x ", packet[24], packet[25]);
1147 #endif
1148 fatal("src=%02x%02x%02x%02x ",
1149 packet[26], packet[27], packet[28], packet[29]);
1150 fatal("dst=%02x%02x%02x%02x ",
1151 packet[30], packet[31], packet[32], packet[33]);
1152 #if 0
1153 for (i=34; i<len; i++)
1154 fatal("%02x", packet[i]);
1155 #endif
1156
1157 if (len < 34 + 8 + 236) {
1158 fatal("[ DHCP packet too short? Len=%i ]\n", len);
1159 return;
1160 }
1161
1162 /*
1163 * UDP data (at offset 34):
1164 *
1165 * srcport=0044 dstport=0043 length=0134 chksum=a973
1166 * data = 01010600d116d276000000000000000000000000000000
1167 * 0000000000102030405060...0000...638253633501...000
1168 */
1169
1170 fatal("op=%02x ", packet[42]);
1171 fatal("htype=%02x ", packet[43]);
1172 fatal("hlen=%02x ", packet[44]);
1173 fatal("hops=%02x ", packet[45]);
1174 fatal("xid=%02x%02x%02x%02x ", packet[46], packet[47],
1175 packet[48], packet[49]);
1176 fatal("secs=%02x%02x ", packet[50], packet[51]);
1177 fatal("flags=%02x%02x ", packet[52], packet[53]);
1178 fatal("ciaddr=%02x%02x%02x%02x ", packet[54], packet[55],
1179 packet[56], packet[57]);
1180 fatal("yiaddr=%02x%02x%02x%02x ", packet[58], packet[59],
1181 packet[60], packet[61]);
1182 fatal("siaddr=%02x%02x%02x%02x ", packet[62], packet[63],
1183 packet[64], packet[65]);
1184 fatal("giaddr=%02x%02x%02x%02x ", packet[66], packet[67],
1185 packet[68], packet[69]);
1186 fatal("chaddr=");
1187 for (i=70; i<70+16; i++)
1188 fatal("%02x", packet[i]);
1189 /*
1190 | sname (64) |
1191 | file (128) |
1192 */
1193 fatal(" ]\n");
1194
1195 lp = net_allocate_packet_link(net, extra, len);
1196
1197 /* Copy the old packet first: */
1198 memcpy(lp->data, packet, len);
1199
1200 /* We are sending to the client, from the gateway: */
1201 memcpy(lp->data + 0, packet + 6, 6);
1202 memcpy(lp->data + 6, net->gateway_ethernet_addr, 6);
1203
1204 memcpy(lp->data + 26, &net->gateway_ipv4_addr[0], 4);
1205 lp->data[30] = 0xff;
1206 lp->data[31] = 0xff;
1207 lp->data[32] = 0xff;
1208 lp->data[33] = 0xff;
1209
1210 /* Switch src and dst ports: */
1211 memcpy(lp->data + 34, packet + 36, 2);
1212 memcpy(lp->data + 36, packet + 34, 2);
1213
1214 /* Client's (yiaddr) IPv4 address: */
1215 lp->data[58] = 10;
1216 lp->data[59] = 0;
1217 lp->data[60] = 0;
1218 lp->data[61] = 1;
1219
1220 /* Server's IPv4 address: (giaddr) */
1221 memcpy(lp->data + 66, &net->gateway_ipv4_addr[0], 4);
1222
1223 /* This is a Reply: */
1224 lp->data[42] = 0x02;
1225
1226 sprintf(lp->data + 70+16+64, "gxemul");
1227
1228 /* Recalculate IP header checksum: */
1229 net_ip_checksum(lp->data + 14, 10, 20);
1230
1231 /* ... and the UDP checksum: */
1232 net_ip_tcp_checksum(lp->data + 34, 6, len - 34 - 8,
1233 lp->data + 26, lp->data + 30, 1);
1234
1235
1236 /* Debug dump: */
1237 packet = lp->data;
1238 fatal("[ net: IPv4 DHCP REPLY: ");
1239 for (i=0; i<14; i++)
1240 fatal("%02x", packet[i]);
1241 fatal("ver=%02x ", packet[14]);
1242 fatal("tos=%02x ", packet[15]);
1243 fatal("len=%02x%02x ", packet[16], packet[17]);
1244 fatal("id=%02x%02x ", packet[18], packet[19]);
1245 fatal("ofs=%02x%02x ", packet[20], packet[21]);
1246 fatal("ttl=%02x ", packet[22]);
1247 fatal("p=%02x ", packet[23]);
1248 fatal("sum=%02x%02x ", packet[24], packet[25]);
1249 fatal("src=%02x%02x%02x%02x ",
1250 packet[26], packet[27], packet[28], packet[29]);
1251 fatal("dst=%02x%02x%02x%02x ",
1252 packet[30], packet[31], packet[32], packet[33]);
1253 fatal("op=%02x ", packet[42]);
1254 fatal("htype=%02x ", packet[43]);
1255 fatal("hlen=%02x ", packet[44]);
1256 fatal("hops=%02x ", packet[45]);
1257 fatal("xid=%02x%02x%02x%02x ", packet[46], packet[47],
1258 packet[48], packet[49]);
1259 fatal("secs=%02x%02x ", packet[50], packet[51]);
1260 fatal("flags=%02x%02x ", packet[52], packet[53]);
1261 fatal("ciaddr=%02x%02x%02x%02x ", packet[54], packet[55],
1262 packet[56], packet[57]);
1263 fatal("yiaddr=%02x%02x%02x%02x ", packet[58], packet[59],
1264 packet[60], packet[61]);
1265 fatal("siaddr=%02x%02x%02x%02x ", packet[62], packet[63],
1266 packet[64], packet[65]);
1267 fatal("giaddr=%02x%02x%02x%02x ", packet[66], packet[67],
1268 packet[68], packet[69]);
1269 fatal("chaddr=");
1270 for (i=70; i<70+16; i++)
1271 fatal("%02x", packet[i]);
1272 fatal(" ]\n");
1273
1274 #endif
1275 }
1276
1277
1278 /*
1279 * net_ip_broadcast():
1280 *
1281 * Handle an IP broadcast packet, coming from the emulated NIC.
1282 * (This is usually a DHCP request, or similar.)
1283 */
1284 static void net_ip_broadcast(struct net *net, void *extra,
1285 unsigned char *packet, int len)
1286 {
1287 unsigned char *p = (void *) &net->netmask_ipv4;
1288 uint32_t x, y;
1289 int i, xl, warning = 0, match = 0;
1290
1291 #if 0
1292 fatal("[ net: IP BROADCAST: ");
1293 fatal("ver=%02x ", packet[14]);
1294 fatal("tos=%02x ", packet[15]);
1295 fatal("len=%02x%02x ", packet[16], packet[17]);
1296 fatal("id=%02x%02x ", packet[18], packet[19]);
1297 fatal("ofs=%02x%02x ", packet[20], packet[21]);
1298 fatal("ttl=%02x ", packet[22]);
1299 fatal("p=%02x ", packet[23]);
1300 fatal("sum=%02x%02x ", packet[24], packet[25]);
1301 fatal("src=%02x%02x%02x%02x ",
1302 packet[26], packet[27], packet[28], packet[29]);
1303 fatal("dst=%02x%02x%02x%02x ",
1304 packet[30], packet[31], packet[32], packet[33]);
1305 for (i=34; i<len; i++)
1306 fatal("%02x", packet[i]);
1307 fatal(" ]\n");
1308 #endif
1309
1310 /* Check for 10.0.0.255 first, maybe some guest OSes think that
1311 it's a /24 network, regardless of what it actually is. */
1312 y = (packet[30] << 24) + (packet[31] << 16) +
1313 (packet[32] << 8) + packet[33];
1314
1315 x = (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3];
1316 /* Example: x = 10.0.0.0 */
1317 x |= 255;
1318
1319 if (x == y) {
1320 warning = 1;
1321 match = 1;
1322 }
1323
1324 xl = 32 - net->netmask_ipv4_len;
1325 x |= (1 << xl) - 1;
1326 /* x = 10.255.255.255 */
1327
1328 if (x == y)
1329 match = 1;
1330 if (y == 0xffffffff)
1331 match = 1;
1332
1333 if (warning)
1334 fatal("[ net_ip_broadcast(): warning: broadcast to "
1335 "0x%08x, expecting broadcast to 0x%08x or "
1336 "0xffffffff ]\n", y, x);
1337
1338 /* Cut off overflowing tail data: */
1339 if (len > 14 + packet[16]*256 + packet[17])
1340 len = 14 + packet[16]*256 + packet[17];
1341
1342 /* Check for known packets: */
1343 if (packet[14] == 0x45 && /* IPv4 */
1344 packet[23] == 0x11 && /* UDP */
1345 packet[34] == 0 && packet[35] == 68 && /* DHCP client */
1346 packet[36] == 0 && packet[37] == 67) { /* DHCP server */
1347 net_ip_broadcast_dhcp(net, extra, packet, len);
1348 return;
1349 }
1350
1351 /* Unknown packet: */
1352 fatal("[ net: UNIMPLEMENTED IP BROADCAST: ");
1353 fatal("ver=%02x ", packet[14]);
1354 fatal("tos=%02x ", packet[15]);
1355 fatal("len=%02x%02x ", packet[16], packet[17]);
1356 fatal("id=%02x%02x ", packet[18], packet[19]);
1357 fatal("ofs=%02x%02x ", packet[20], packet[21]);
1358 fatal("ttl=%02x ", packet[22]);
1359 fatal("p=%02x ", packet[23]);
1360 fatal("sum=%02x%02x ", packet[24], packet[25]);
1361 fatal("src=%02x%02x%02x%02x ",
1362 packet[26], packet[27], packet[28], packet[29]);
1363 fatal("dst=%02x%02x%02x%02x ",
1364 packet[30], packet[31], packet[32], packet[33]);
1365 for (i=34; i<len; i++)
1366 fatal("%02x", packet[i]);
1367 fatal(" ]\n");
1368 }
1369
1370
1371 /*
1372 * net_arp():
1373 *
1374 * Handle an ARP (or RARP) packet, coming from the emulated NIC.
1375 *
1376 * An ARP packet might look like this:
1377 *
1378 * ARP header:
1379 * ARP hardware addr family: 0001
1380 * ARP protocol addr family: 0800
1381 * ARP addr lengths: 06 04
1382 * ARP request: 0001
1383 * ARP from: 112233445566 01020304
1384 * ARP to: 000000000000 01020301
1385 *
1386 * An ARP request with a 'to' IP value of the gateway should cause an
1387 * ARP response packet to be created.
1388 *
1389 * An ARP request with the same from and to IP addresses should be ignored.
1390 * (This would be a host testing to see if there is an IP collision.)
1391 */
1392 static void net_arp(struct net *net, void *extra,
1393 unsigned char *packet, int len, int reverse)
1394 {
1395 int q;
1396 int i;
1397
1398 /* TODO: This debug dump assumes ethernet->IPv4 translation: */
1399 if (reverse)
1400 debug("[ net: RARP: ");
1401 else
1402 debug("[ net: ARP: ");
1403 for (i=0; i<2; i++)
1404 debug("%02x", packet[i]);
1405 debug(" ");
1406 for (i=2; i<4; i++)
1407 debug("%02x", packet[i]);
1408 debug(" ");
1409 debug("%02x", packet[4]);
1410 debug(" ");
1411 debug("%02x", packet[5]);
1412 debug(" req=");
1413 debug("%02x", packet[6]); /* Request type */
1414 debug("%02x", packet[7]);
1415 debug(" from=");
1416 for (i=8; i<18; i++)
1417 debug("%02x", packet[i]);
1418 debug(" to=");
1419 for (i=18; i<28; i++)
1420 debug("%02x", packet[i]);
1421 debug(" ]\n");
1422
1423 if (packet[0] == 0x00 && packet[1] == 0x01 &&
1424 packet[2] == 0x08 && packet[3] == 0x00 &&
1425 packet[4] == 0x06 && packet[5] == 0x04) {
1426 int r = (packet[6] << 8) + packet[7];
1427 struct ethernet_packet_link *lp;
1428
1429 switch (r) {
1430 case 1: /* Request */
1431 /* Only create a reply if this was meant for the
1432 gateway: */
1433 if (memcmp(packet+24, net->gateway_ipv4_addr, 4) != 0)
1434 break;
1435
1436 lp = net_allocate_packet_link(net, extra, len + 14);
1437
1438 /* Copy the old packet first: */
1439 memcpy(lp->data + 14, packet, len);
1440
1441 /* Add ethernet ARP header: */
1442 memcpy(lp->data + 0, lp->data + 8 + 14, 6);
1443 memcpy(lp->data + 6, net->gateway_ethernet_addr, 6);
1444 lp->data[12] = 0x08; lp->data[13] = 0x06;
1445
1446 /* Address of the emulated machine: */
1447 memcpy(lp->data + 18 + 14, lp->data + 8 + 14, 10);
1448
1449 /* Address of the gateway: */
1450 memcpy(lp->data + 8 + 14, net->gateway_ethernet_addr,
1451 6);
1452 memcpy(lp->data + 14 + 14, net->gateway_ipv4_addr, 4);
1453
1454 /* This is a Reply: */
1455 lp->data[6 + 14] = 0x00; lp->data[7 + 14] = 0x02;
1456
1457 break;
1458 case 3: /* Reverse Request */
1459 lp = net_allocate_packet_link(net, extra, len + 14);
1460
1461 /* Copy the old packet first: */
1462 memcpy(lp->data + 14, packet, len);
1463
1464 /* Add ethernet RARP header: */
1465 memcpy(lp->data + 0, packet + 8, 6);
1466 memcpy(lp->data + 6, net->gateway_ethernet_addr, 6);
1467 lp->data[12] = 0x80; lp->data[13] = 0x35;
1468
1469 /* This is a RARP reply: */
1470 lp->data[6 + 14] = 0x00; lp->data[7 + 14] = 0x04;
1471
1472 /* Address of the gateway: */
1473 memcpy(lp->data + 8 + 14, net->gateway_ethernet_addr,
1474 6);
1475 memcpy(lp->data + 14 + 14, net->gateway_ipv4_addr, 4);
1476
1477 /* MAC address of emulated machine: */
1478 memcpy(lp->data + 18 + 14, packet + 8, 6);
1479
1480 /*
1481 * IP address of the emulated machine: Automagically
1482 * generated from the MAC address. :-)
1483 *
1484 * packet+8 points to the client's mac address,
1485 * for example 10:20:30:x0:y0:z0, where x,y,z are
1486 * 1..15.
1487 * 10:20:30:10:10:10 results in 10.0.0.1.
1488 */
1489 q = (((packet[8 + 3]) >> 4) - 1);
1490 q = q*15 + (((packet[8 + 4]) >> 4) - 1);
1491 q = q*15 + (((packet[8 + 5]) >> 4) - 1);
1492 lp->data[24 + 14] = 10;
1493 lp->data[25 + 14] = q / 225; q /= 15;
1494 lp->data[26 + 14] = q / 15; q /= 15;
1495 lp->data[27 + 14] = q + 1;
1496
1497 break;
1498 case 2: /* Reply */
1499 case 4: /* Reverse Reply */
1500 default:
1501 fatal("[ net: ARP: UNIMPLEMENTED request type "
1502 "0x%04x ]\n", r);
1503 }
1504 } else {
1505 fatal("[ net: ARP: UNIMPLEMENTED arp packet type: ");
1506 for (i=0; i<len; i++)
1507 fatal("%02x", packet[i]);
1508 fatal(" ]\n");
1509 }
1510 }
1511
1512
1513 /*
1514 * net_ethernet_rx_avail():
1515 *
1516 * Return 1 if there is a packet available for this 'extra' pointer, otherwise
1517 * return 0.
1518 *
1519 * Appart from actually checking for incoming packets from the outside world,
1520 * this function basically works like net_ethernet_rx() but it only receives
1521 * a return value telling us whether there is a packet or not, we don't
1522 * actually get the packet.
1523 */
1524 int net_ethernet_rx_avail(struct net *net, void *extra)
1525 {
1526 int received_packets_this_tick = 0;
1527 int max_packets_this_tick = 200;
1528 int con_id;
1529
1530 if (net == NULL)
1531 return 0;
1532
1533 /*
1534 * UDP:
1535 */
1536 for (con_id=0; con_id<MAX_UDP_CONNECTIONS; con_id++) {
1537 ssize_t res;
1538 unsigned char buf[66000];
1539 unsigned char udp_data[66008];
1540 struct sockaddr_in from;
1541 socklen_t from_len = sizeof(from);
1542 int ip_len, udp_len;
1543 struct ethernet_packet_link *lp;
1544 int max_per_packet;
1545 int bytes_converted = 0;
1546 int this_packets_data_length;
1547 int fragment_ofs = 0;
1548
1549 if (received_packets_this_tick > max_packets_this_tick)
1550 break;
1551
1552 if (!net->udp_connections[con_id].in_use)
1553 continue;
1554
1555 if (net->udp_connections[con_id].socket < 0) {
1556 fatal("INTERNAL ERROR in net.c, udp socket < 0 "
1557 "but in use?\n");
1558 continue;
1559 }
1560
1561 res = recvfrom(net->udp_connections[con_id].socket, buf,
1562 sizeof(buf), 0, (struct sockaddr *)&from, &from_len);
1563
1564 /* No more incoming UDP on this connection? */
1565 if (res < 0)
1566 continue;
1567
1568 net->timestamp ++;
1569 net->udp_connections[con_id].last_used_timestamp =
1570 net->timestamp;
1571
1572 net->udp_connections[con_id].udp_id ++;
1573
1574 /*
1575 * Special case for the nameserver: If a UDP packet is
1576 * received from the nameserver (if the nameserver's IP is
1577 * known), fake it so that it comes from the gateway instead.
1578 */
1579 if (net->udp_connections[con_id].fake_ns)
1580 memcpy(((unsigned char *)(&from))+4,
1581 &net->gateway_ipv4_addr[0], 4);
1582
1583 /*
1584 * We now have a UDP packet of size 'res' which we need
1585 * turn into one or more ethernet packets for the emulated
1586 * operating system. Ethernet packets are at most 1518
1587 * bytes long. With some margin, that means we can have
1588 * about 1500 bytes per packet.
1589 *
1590 * Ethernet = 14 bytes
1591 * IP = 20 bytes
1592 * (UDP = 8 bytes + data)
1593 *
1594 * So data can be at most max_per_packet - 34. For UDP
1595 * fragments, each multiple should (?) be a multiple of
1596 * 8 bytes, except the last which doesn't have any such
1597 * restriction.
1598 */
1599 max_per_packet = 1500;
1600
1601 /* UDP: */
1602 udp_len = res + 8;
1603 /* from[2..3] = outside_udp_port */
1604 udp_data[0] = ((unsigned char *)&from)[2];
1605 udp_data[1] = ((unsigned char *)&from)[3];
1606 udp_data[2] = (net->udp_connections[con_id].
1607 inside_udp_port >> 8) & 0xff;
1608 udp_data[3] = net->udp_connections[con_id].
1609 inside_udp_port & 0xff;
1610 udp_data[4] = udp_len >> 8;
1611 udp_data[5] = udp_len & 0xff;
1612 udp_data[6] = 0;
1613 udp_data[7] = 0;
1614 memcpy(udp_data + 8, buf, res);
1615 /*
1616 * TODO: UDP checksum, if necessary. At least NetBSD
1617 * and OpenBSD accept UDP packets with 0x0000 in the
1618 * checksum field anyway.
1619 */
1620
1621 while (bytes_converted < udp_len) {
1622 this_packets_data_length = udp_len - bytes_converted;
1623
1624 /* Do we need to fragment? */
1625 if (this_packets_data_length > max_per_packet-34) {
1626 this_packets_data_length =
1627 max_per_packet - 34;
1628 while (this_packets_data_length & 7)
1629 this_packets_data_length --;
1630 }
1631
1632 ip_len = 20 + this_packets_data_length;
1633
1634 lp = net_allocate_packet_link(net, extra,
1635 14 + 20 + this_packets_data_length);
1636
1637 /* Ethernet header: */
1638 memcpy(lp->data + 0, net->udp_connections[con_id].
1639 ethernet_address, 6);
1640 memcpy(lp->data + 6, net->gateway_ethernet_addr, 6);
1641 lp->data[12] = 0x08; /* IP = 0x0800 */
1642 lp->data[13] = 0x00;
1643
1644 /* IP header: */
1645 lp->data[14] = 0x45; /* ver */
1646 lp->data[15] = 0x00; /* tos */
1647 lp->data[16] = ip_len >> 8;
1648 lp->data[17] = ip_len & 0xff;
1649 lp->data[18] = net->udp_connections[con_id].udp_id >> 8;
1650 lp->data[19] = net->udp_connections[con_id].udp_id
1651 & 0xff;
1652 lp->data[20] = (fragment_ofs >> 8);
1653 if (bytes_converted + this_packets_data_length
1654 < udp_len)
1655 lp->data[20] |= 0x20; /* More fragments */
1656 lp->data[21] = fragment_ofs & 0xff;
1657 lp->data[22] = 0x40; /* ttl */
1658 lp->data[23] = 17; /* p = UDP */
1659 lp->data[26] = ((unsigned char *)&from)[4];
1660 lp->data[27] = ((unsigned char *)&from)[5];
1661 lp->data[28] = ((unsigned char *)&from)[6];
1662 lp->data[29] = ((unsigned char *)&from)[7];
1663 memcpy(lp->data + 30, net->udp_connections[con_id].
1664 inside_ip_address, 4);
1665 net_ip_checksum(lp->data + 14, 10, 20);
1666
1667 memcpy(lp->data+34, udp_data + bytes_converted,
1668 this_packets_data_length);
1669
1670 bytes_converted += this_packets_data_length;
1671 fragment_ofs = bytes_converted / 8;
1672
1673 received_packets_this_tick ++;
1674 }
1675
1676 /* This makes sure we check this connection AGAIN
1677 for more incoming UDP packets, before moving to the
1678 next connection: */
1679 con_id --;
1680 }
1681
1682 /*
1683 * TCP:
1684 */
1685 for (con_id=0; con_id<MAX_TCP_CONNECTIONS; con_id++) {
1686 unsigned char buf[66000];
1687 ssize_t res, res2;
1688 fd_set rfds;
1689 struct timeval tv;
1690
1691 if (received_packets_this_tick > max_packets_this_tick)
1692 break;
1693
1694 if (!net->tcp_connections[con_id].in_use)
1695 continue;
1696
1697 if (net->tcp_connections[con_id].socket < 0) {
1698 fatal("INTERNAL ERROR in net.c, tcp socket < 0"
1699 " but in use?\n");
1700 continue;
1701 }
1702
1703 if (net->tcp_connections[con_id].incoming_buf == NULL) {
1704 net->tcp_connections[con_id].incoming_buf =
1705 malloc(TCP_INCOMING_BUF_LEN);
1706 if (net->tcp_connections[con_id].incoming_buf == NULL) {
1707 printf("out of memory allocating "
1708 "incoming_buf for con_id %i\n", con_id);
1709 exit(1);
1710 }
1711 }
1712
1713 if (net->tcp_connections[con_id].state >=
1714 TCP_OUTSIDE_DISCONNECTED)
1715 continue;
1716
1717 /* Is the socket available for output? */
1718 FD_ZERO(&rfds); /* write */
1719 FD_SET(net->tcp_connections[con_id].socket, &rfds);
1720 tv.tv_sec = tv.tv_usec = 0;
1721 errno = 0;
1722 res = select(net->tcp_connections[con_id].socket+1,
1723 NULL, &rfds, NULL, &tv);
1724
1725 if (errno == ECONNREFUSED) {
1726 fatal("[ ECONNREFUSED: TODO ]\n");
1727 net->tcp_connections[con_id].state =
1728 TCP_OUTSIDE_DISCONNECTED;
1729 fatal("CHANGING TO TCP_OUTSIDE_DISCONNECTED "
1730 "(refused connection)\n");
1731 continue;
1732 }
1733
1734 if (errno == ETIMEDOUT) {
1735 fatal("[ ETIMEDOUT: TODO ]\n");
1736 /* TODO */
1737 net->tcp_connections[con_id].state =
1738 TCP_OUTSIDE_DISCONNECTED;
1739 fatal("CHANGING TO TCP_OUTSIDE_DISCONNECTED "
1740 "(timeout)\n");
1741 continue;
1742 }
1743
1744 if (net->tcp_connections[con_id].state ==
1745 TCP_OUTSIDE_TRYINGTOCONNECT && res > 0) {
1746 net->tcp_connections[con_id].state =
1747 TCP_OUTSIDE_CONNECTED;
1748 debug("CHANGING TO TCP_OUTSIDE_CONNECTED\n");
1749 net_ip_tcp_connectionreply(net, extra, con_id, 1,
1750 NULL, 0, 0);
1751 }
1752
1753 if (net->tcp_connections[con_id].state ==
1754 TCP_OUTSIDE_CONNECTED && res < 1) {
1755 continue;
1756 }
1757
1758 /*
1759 * Does this connection have unacknowledged data? Then, if
1760 * enough number of rounds have passed, try to resend it using
1761 * the old value of seqnr.
1762 */
1763 if (net->tcp_connections[con_id].incoming_buf_len != 0) {
1764 net->tcp_connections[con_id].incoming_buf_rounds ++;
1765 if (net->tcp_connections[con_id].incoming_buf_rounds >
1766 10000) {
1767 debug(" at seqnr %u but backing back to %u,"
1768 " resending %i bytes\n",
1769 net->tcp_connections[con_id].outside_seqnr,
1770 net->tcp_connections[con_id].
1771 incoming_buf_seqnr,
1772 net->tcp_connections[con_id].
1773 incoming_buf_len);
1774
1775 net->tcp_connections[con_id].
1776 incoming_buf_rounds = 0;
1777 net->tcp_connections[con_id].outside_seqnr =
1778 net->tcp_connections[con_id].
1779 incoming_buf_seqnr;
1780
1781 net_ip_tcp_connectionreply(net, extra, con_id,
1782 0, net->tcp_connections[con_id].
1783 incoming_buf,
1784 net->tcp_connections[con_id].
1785 incoming_buf_len, 0);
1786 }
1787 continue;
1788 }
1789
1790 /* Don't receive unless the guest OS is ready! */
1791 if (((int32_t)net->tcp_connections[con_id].outside_seqnr -
1792 (int32_t)net->tcp_connections[con_id].inside_acknr) > 0) {
1793 /* fatal("YOYO 1! outside_seqnr - inside_acknr = %i\n",
1794 net->tcp_connections[con_id].outside_seqnr -
1795 net->tcp_connections[con_id].inside_acknr); */
1796 continue;
1797 }
1798
1799 /* Is there incoming data available on the socket? */
1800 FD_ZERO(&rfds); /* read */
1801 FD_SET(net->tcp_connections[con_id].socket, &rfds);
1802 tv.tv_sec = tv.tv_usec = 0;
1803 res2 = select(net->tcp_connections[con_id].socket+1, &rfds,
1804 NULL, NULL, &tv);
1805
1806 /* No more incoming TCP data on this connection? */
1807 if (res2 < 1)
1808 continue;
1809
1810 res = read(net->tcp_connections[con_id].socket, buf, 1400);
1811 if (res > 0) {
1812 /* debug("\n -{- %lli -}-\n", (long long)res); */
1813 net->tcp_connections[con_id].incoming_buf_len = res;
1814 net->tcp_connections[con_id].incoming_buf_rounds = 0;
1815 net->tcp_connections[con_id].incoming_buf_seqnr =
1816 net->tcp_connections[con_id].outside_seqnr;
1817 debug(" putting %i bytes (seqnr %u) in the incoming "
1818 "buf\n", res, net->tcp_connections[con_id].
1819 incoming_buf_seqnr);
1820 memcpy(net->tcp_connections[con_id].incoming_buf,
1821 buf, res);
1822
1823 net_ip_tcp_connectionreply(net, extra, con_id, 0,
1824 buf, res, 0);
1825 } else if (res == 0) {
1826 net->tcp_connections[con_id].state =
1827 TCP_OUTSIDE_DISCONNECTED;
1828 debug("CHANGING TO TCP_OUTSIDE_DISCONNECTED, read"
1829 " res=0\n");
1830 net_ip_tcp_connectionreply(net, extra, con_id, 0,
1831 NULL, 0, 0);
1832 } else {
1833 net->tcp_connections[con_id].state =
1834 TCP_OUTSIDE_DISCONNECTED;
1835 fatal("CHANGING TO TCP_OUTSIDE_DISCONNECTED, "
1836 "read res<=0, errno = %i\n", errno);
1837 net_ip_tcp_connectionreply(net, extra, con_id, 0,
1838 NULL, 0, 0);
1839 }
1840
1841 net->timestamp ++;
1842 net->tcp_connections[con_id].last_used_timestamp =
1843 net->timestamp;
1844 }
1845
1846 return net_ethernet_rx(net, extra, NULL, NULL);
1847 }
1848
1849
1850 /*
1851 * net_ethernet_rx():
1852 *
1853 * Receive an ethernet packet. (This means handing over an already prepared
1854 * packet from this module (net.c) to a specific ethernet controller device.)
1855 *
1856 * Return value is 1 if there was a packet available. *packetp and *lenp
1857 * will be set to the packet's data pointer and length, respectively, and
1858 * the packet will be removed from the linked list). If there was no packet
1859 * available, 0 is returned.
1860 *
1861 * If packetp is NULL, then the search is aborted as soon as a packet with
1862 * the correct 'extra' field is found, and a 1 is returned, but as packetp
1863 * is NULL we can't return the actual packet. (This is the internal form
1864 * if net_ethernet_rx_avail().)
1865 */
1866 int net_ethernet_rx(struct net *net, void *extra,
1867 unsigned char **packetp, int *lenp)
1868 {
1869 struct ethernet_packet_link *lp, *prev;
1870
1871 if (net == NULL)
1872 return 0;
1873
1874 /* Find the first packet which has the right 'extra' field. */
1875
1876 lp = net->first_ethernet_packet;
1877 prev = NULL;
1878 while (lp != NULL) {
1879 if (lp->extra == extra) {
1880 /* We found a packet for this controller! */
1881 if (packetp == NULL || lenp == NULL)
1882 return 1;
1883
1884 /* Let's return it: */
1885 (*packetp) = lp->data;
1886 (*lenp) = lp->len;
1887
1888 /* Remove this link from the linked list: */
1889 if (prev == NULL)
1890 net->first_ethernet_packet = lp->next;
1891 else
1892 prev->next = lp->next;
1893
1894 if (lp->next == NULL)
1895 net->last_ethernet_packet = prev;
1896 else
1897 lp->next->prev = prev;
1898
1899 free(lp);
1900
1901 /* ... and return successfully: */
1902 return 1;
1903 }
1904
1905 prev = lp;
1906 lp = lp->next;
1907 }
1908
1909 /* No packet found. :-( */
1910 return 0;
1911 }
1912
1913
1914 /*
1915 * net_ethernet_tx():
1916 *
1917 * Transmit an ethernet packet, as seen from the emulated ethernet controller.
1918 * If the packet can be handled here, it will not necessarily be transmitted
1919 * to the outside world.
1920 */
1921 void net_ethernet_tx(struct net *net, void *extra,
1922 unsigned char *packet, int len)
1923 {
1924 int i, n;
1925
1926 if (net == NULL)
1927 return;
1928
1929 /* Drop too small packets: */
1930 if (len < 20)
1931 return;
1932
1933 /* Copy this packet to all other NICs on this network: */
1934 if (extra != NULL && net->n_nics > 0) {
1935 for (i=0; i<net->n_nics; i++)
1936 if (extra != net->nic_extra[i]) {
1937 struct ethernet_packet_link *lp;
1938 lp = net_allocate_packet_link(net,
1939 net->nic_extra[i], len);
1940
1941 /* Copy the entire packet: */
1942 memcpy(lp->data, packet, len);
1943 }
1944 }
1945
1946 /* Drop packets that are not destined for the gateway: */
1947 if (memcmp(packet, net->gateway_ethernet_addr, 6) != 0
1948 && packet[0] != 0xff && packet[0] != 0x00)
1949 return;
1950
1951 #if 0
1952 fatal("[ net: ethernet: ");
1953 for (i=0; i<6; i++)
1954 fatal("%02x", packet[i]);
1955 fatal(" ");
1956 for (i=6; i<12; i++)
1957 fatal("%02x", packet[i]);
1958 fatal(" ");
1959 for (i=12; i<14; i++)
1960 fatal("%02x", packet[i]);
1961 fatal(" ");
1962 for (i=14; i<len; i++)
1963 fatal("%02x", packet[i]);
1964 fatal(" ]\n");
1965 #endif
1966
1967 /* Sprite: */
1968 if (packet[12] == 0x05 && packet[13] == 0x00) {
1969 /* TODO. */
1970 fatal("[ net: TX: UNIMPLEMENTED Sprite packet ]\n");
1971 return;
1972 }
1973
1974 /* IP: */
1975 if (packet[12] == 0x08 && packet[13] == 0x00) {
1976 /* Routed via the gateway? */
1977 if (memcmp(packet+0, net->gateway_ethernet_addr, 6) == 0) {
1978 net_ip(net, extra, packet, len);
1979 return;
1980 }
1981
1982 /* Broadcast? (DHCP does this.) */
1983 n = 0;
1984 for (i=0; i<6; i++)
1985 if (packet[i] == 0xff)
1986 n++;
1987 if (n == 6) {
1988 net_ip_broadcast(net, extra, packet, len);
1989 return;
1990 }
1991
1992 if (net->n_nics < 2) {
1993 fatal("[ net: TX: IP packet not for gateway, "
1994 "and not broadcast: ");
1995 for (i=0; i<14; i++)
1996 fatal("%02x", packet[i]);
1997 fatal(" ]\n");
1998 }
1999 return;
2000 }
2001
2002 /* ARP: */
2003 if (packet[12] == 0x08 && packet[13] == 0x06) {
2004 if (len != 60)
2005 fatal("[ net_ethernet_tx: WARNING! unusual "
2006 "ARP len (%i) ]\n", len);
2007 net_arp(net, extra, packet + 14, len - 14, 0);
2008 return;
2009 }
2010
2011 /* RARP: */
2012 if (packet[12] == 0x80 && packet[13] == 0x35) {
2013 net_arp(net, extra, packet + 14, len - 14, 1);
2014 return;
2015 }
2016
2017 /* IPv6: */
2018 if (packet[12] == 0x86 && packet[13] == 0xdd) {
2019 /* TODO. */
2020 fatal("[ net: TX: UNIMPLEMENTED IPv6 packet ]\n");
2021 return;
2022 }
2023
2024 fatal("[ net: TX: UNIMPLEMENTED ethernet packet type 0x%02x%02x! ]\n",
2025 packet[12], packet[13]);
2026 }
2027
2028
2029 /*
2030 * parse_resolvconf():
2031 *
2032 * This function parses "/etc/resolv.conf" to figure out the nameserver
2033 * and domain used by the host.
2034 */
2035 static void parse_resolvconf(struct net *net)
2036 {
2037 FILE *f;
2038 char buf[8000];
2039 size_t len;
2040 int res;
2041 unsigned int i, start;
2042
2043 /*
2044 * This is a very ugly hack, which tries to figure out which
2045 * nameserver the host uses by looking for the string 'nameserver'
2046 * in /etc/resolv.conf.
2047 *
2048 * This can later on be used for DHCP autoconfiguration. (TODO)
2049 *
2050 * TODO: This is hardcoded to use /etc/resolv.conf. Not all
2051 * operating systems use that filename.
2052 *
2053 * TODO: This is hardcoded for AF_INET (that is, IPv4).
2054 *
2055 * TODO: This assumes that the first nameserver listed is the
2056 * one to use.
2057 */
2058 f = fopen("/etc/resolv.conf", "r");
2059 if (f == NULL)
2060 return;
2061
2062 /* TODO: get rid of the hardcoded values */
2063 memset(buf, 0, sizeof(buf));
2064 len = fread(buf, 1, sizeof(buf) - 100, f);
2065 fclose(f);
2066 buf[sizeof(buf) - 1] = '\0';
2067
2068 for (i=0; i<len; i++)
2069 if (strncmp(buf+i, "nameserver", 10) == 0) {
2070 char *p;
2071
2072 /*
2073 * "nameserver" (1 or more whitespace)
2074 * "x.y.z.w" (non-digit)
2075 */
2076
2077 /* debug("found nameserver at offset %i\n", i); */
2078 i += 10;
2079 while (i<len && (buf[i]==' ' || buf[i]=='\t'))
2080 i++;
2081 if (i >= len)
2082 break;
2083 start = i;
2084
2085 p = buf+start;
2086 while ((*p >= '0' && *p <= '9') || *p == '.')
2087 p++;
2088 *p = '\0';
2089
2090 #ifdef HAVE_INET_PTON
2091 res = inet_pton(AF_INET, buf + start,
2092 &net->nameserver_ipv4);
2093 #else
2094 res = inet_aton(buf + start, &net->nameserver_ipv4);
2095 #endif
2096 if (res < 1)
2097 break;
2098
2099 net->nameserver_known = 1;
2100 break;
2101 }
2102
2103 for (i=0; i<len; i++)
2104 if (strncmp(buf+i, "domain", 6) == 0) {
2105 /* "domain" (1 or more whitespace) domain_name */
2106 i += 6;
2107 while (i<len && (buf[i]==' ' || buf[i]=='\t'))
2108 i++;
2109 if (i >= len)
2110 break;
2111
2112 start = i;
2113 while (i<len && buf[i]!='\n' && buf[i]!='\r')
2114 i++;
2115 if (i < len)
2116 buf[i] = '\0';
2117 /* fatal("DOMAIN='%s'\n", buf + start); */
2118 net->domain_name = strdup(buf + start);
2119 break;
2120 }
2121 }
2122
2123
2124 /*
2125 * net_add_nic():
2126 *
2127 * Add a NIC to a network. (All NICs on a network will see each other's
2128 * packets.)
2129 */
2130 void net_add_nic(struct net *net, void *extra, unsigned char *macaddr)
2131 {
2132 if (net == NULL)
2133 return;
2134
2135 if (extra == NULL) {
2136 fprintf(stderr, "net_add_nic(): extra = NULL\n");
2137 exit(1);
2138 }
2139
2140 net->n_nics ++;
2141 net->nic_extra = realloc(net->nic_extra, sizeof(void *)
2142 * net->n_nics);
2143 if (net->nic_extra == NULL) {
2144 fprintf(stderr, "net_add_nic(): out of memory\n");
2145 exit(1);
2146 }
2147
2148 net->nic_extra[net->n_nics - 1] = extra;
2149 }
2150
2151
2152 /*
2153 * net_gateway_init():
2154 *
2155 * This function creates a "gateway" machine (for example at IPv4 address
2156 * 10.0.0.254, if the net is 10.0.0.0/8), which acts as a gateway/router/
2157 * nameserver etc.
2158 */
2159 static void net_gateway_init(struct net *net)
2160 {
2161 unsigned char *p = (void *) &net->netmask_ipv4;
2162 uint32_t x;
2163 int xl;
2164
2165 x = (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3];
2166 xl = 32 - net->netmask_ipv4_len;
2167 if (xl > 8)
2168 xl = 8;
2169 x |= ((1 << xl) - 1) & ~1;
2170
2171 net->gateway_ipv4_addr[0] = x >> 24;
2172 net->gateway_ipv4_addr[1] = x >> 16;
2173 net->gateway_ipv4_addr[2] = x >> 8;
2174 net->gateway_ipv4_addr[3] = x;
2175
2176 net->gateway_ethernet_addr[0] = 0x60;
2177 net->gateway_ethernet_addr[1] = 0x50;
2178 net->gateway_ethernet_addr[2] = 0x40;
2179 net->gateway_ethernet_addr[3] = 0x30;
2180 net->gateway_ethernet_addr[4] = 0x20;
2181 net->gateway_ethernet_addr[5] = 0x10;
2182 }
2183
2184
2185 /*
2186 * net_dumpinfo():
2187 *
2188 * Called from the debugger's "machine" command, to print some info about
2189 * a network.
2190 */
2191 void net_dumpinfo(struct net *net)
2192 {
2193 int iadd = 4;
2194
2195 debug("net: ");
2196
2197 net_debugaddr(&net->netmask_ipv4, ADDR_IPV4);
2198 debug("/%i", net->netmask_ipv4_len);
2199
2200 debug(" (max outgoing: TCP=%i, UDP=%i)\n",
2201 MAX_TCP_CONNECTIONS, MAX_UDP_CONNECTIONS);
2202
2203 debug_indentation(iadd);
2204
2205 debug("gateway: ");
2206 net_debugaddr(&net->gateway_ipv4_addr, ADDR_IPV4);
2207 debug(" (");
2208 net_debugaddr(&net->gateway_ethernet_addr, ADDR_ETHERNET);
2209 debug(")\n");
2210
2211 debug_indentation(iadd);
2212 if (net->domain_name != NULL && net->domain_name[0])
2213 debug("domain \"%s\", ", net->domain_name);
2214 if (!net->nameserver_known) {
2215 debug("(could not determine nameserver)");
2216 } else {
2217 debug("nameserver ");
2218 net_debugaddr(&net->nameserver_ipv4, ADDR_IPV4);
2219 }
2220 debug("\n");
2221 debug_indentation(-iadd);
2222
2223 debug_indentation(-iadd);
2224 }
2225
2226
2227 /*
2228 * net_init():
2229 *
2230 * This function creates a network, and returns a pointer to it.
2231 * Example: ipv4addr should be something like "10.0.0.0", netipv4len = 8.
2232 *
2233 * (On failure, exit() is called.)
2234 */
2235 struct net *net_init(struct emul *emul, int init_flags,
2236 char *ipv4addr, int netipv4len)
2237 {
2238 struct net *net;
2239 int res;
2240
2241 net = malloc(sizeof(struct net));
2242 if (net == NULL) {
2243 fprintf(stderr, "net_init(): out of memory\n");
2244 exit(1);
2245 }
2246
2247 memset(net, 0, sizeof(struct net));
2248
2249 /* Set the back pointer: */
2250 net->emul = emul;
2251
2252 /* Sane defaults: */
2253 net->timestamp = 0;
2254 net->first_ethernet_packet = net->last_ethernet_packet = NULL;
2255
2256 #ifdef HAVE_INET_PTON
2257 res = inet_pton(AF_INET, ipv4addr, &net->netmask_ipv4);
2258 #else
2259 res = inet_aton(ipv4addr, &net->netmask_ipv4);
2260 #endif
2261 if (res < 1) {
2262 fprintf(stderr, "net_init(): could not parse IPv4 address"
2263 " '%s'\n", ipv4addr);
2264 exit(1);
2265 }
2266
2267 if (netipv4len < 1 || netipv4len > 30) {
2268 fprintf(stderr, "net_init(): extremely weird ipv4 "
2269 "network length (%i)\n", netipv4len);
2270 exit(1);
2271 }
2272 net->netmask_ipv4_len = netipv4len;
2273
2274 net->nameserver_known = 0;
2275 net->domain_name = "";
2276 parse_resolvconf(net);
2277
2278 if (init_flags & NET_INIT_FLAG_GATEWAY)
2279 net_gateway_init(net);
2280
2281 net_dumpinfo(net);
2282
2283 /* This is neccessary when using the real network: */
2284 signal(SIGPIPE, SIG_IGN);
2285
2286 return net;
2287 }
2288

  ViewVC Help
Powered by ViewVC 1.1.26